q = 2 if myrank == 0: print '\npolynomial order = {}'.format(q) alpha = 3. for Nxy in NN: h = 1./Nxy mesh = UnitSquareMesh(Nxy, Nxy) V = FunctionSpace(mesh, 'Lagrange', q) Vl = FunctionSpace(mesh, 'Lagrange', 1) Dt = h/(q*alpha*c) if myrank == 0: print '\n\th = {} - Dt = {}'.format(h, Dt) Wave = AcousticWave({'V':V, 'Vl':Vl, 'Vr':Vl}) Wave.timestepper = 'backward' Wave.lump = True Wave.exact = interpolate(uex_expr, V) Wave.bc = DirichletBC(V, ubc, u0_boundary) Wave.update({'lambda':lam, 'rho':rho, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':interpolate(u0_expr, V), 'utinit':Function(V)}) sol, error = Wave.solve() ERROR.append(error) if myrank == 0: print 'relative error = {:.5e}'.format(error) if not mycomm == None: MPI.barrier(mycomm) if myrank == 0: # Order of convergence: CONVORDER = [] for ii in range(len(ERROR)-1): CONVORDER.append(np.log(ERROR[ii+1]/ERROR[ii])/np.log((1./NN[ii+1])/(1./NN[ii]))) print '\n\norder of convergence:', CONVORDER
def source(tt): return Expression('6*(sqrt(pow(x[0]-0.5,2)+pow(x[1]-0.5,2))<=t)', t=tt) for Nxy in NN: h = 1./Nxy print '\n\th = {}'.format(h) mesh = UnitSquareMesh(Nxy, Nxy, "crossed") q = 1 # Polynomial order V = FunctionSpace(mesh, 'Lagrange', q) Dt = h/(q*5.*c) Wave = AcousticWave({'V':V, 'Vl':V, 'Vr':V}) Wave.timestepper = 'backward' Wave.lump = True #Wave.verbose = True Wave.exact = interpolate(exact_expr, V) Wave.update({'lambda':lam, 'rho':rho, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':Function(V), 'utinit':Function(V)}) test = TestFunction(V) def srcterm(tt): src_expr = source(tt) src_vect = assemble(src_expr*test*dx) return src_vect.array() Wave.ftime = srcterm sol, error = Wave.solve() ERROR.append(error) print 'relative error = {:.5e}'.format(error) # Convergence order: CONVORDER = [] for ii in range(len(ERROR)-1):
return on_boundary for r in RR: V = FunctionSpace(mesh, 'Lagrange', r) Pt = PointSources(V, [[.5,.5]]) mydelta = Pt[0].array() def mysrc(tt): return Ricker(tt)*mydelta # Computation: if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt) Wave = AcousticWave({'V':V, 'Vl':Vl, 'Vr':Vl}) #Wave.verbose = True Wave.timestepper = 'centered' Wave.lump = True Wave.set_abc(mesh, AllFour(), True) Wave.exact = Function(V) Wave.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':Function(V), 'utinit':Function(V)}) Wave.ftime = mysrc sol, error = Wave.solve() ERROR.append(error) if myrank == 0: print 'relative error = {:.5e}'.format(error) if not mycomm == None: MPI.barrier(mycomm) # Plots: try: boolplot = int(sys.argv[1]) except: boolplot = 0 if boolplot > 0: filename, ext = splitext(sys.argv[0])
Pt = PointSources(Vex, [[.5, .5]]) mydelta = Pt[0].array() def mysrc(tt): return Ricker(tt) * mydelta Waveex = AcousticWave({'V': Vex, 'Vm': Vl}) Waveex.timestepper = 'backward' Waveex.lump = True Waveex.update({'a':1.0, 'b':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':dl.Function(Vex), 'utinit':dl.Function(Vex)}) Waveex.ftime = mysrc sol, _ = Waveex.solve() Waveex.exact = dl.Function(Vex) normex = Waveex.computeabserror() # plot myplot.set_varname('u-q' + str(qq)) plotu = dl.Function(Vex) for index, uu in enumerate(sol): if index % boolplot == 0: setfct(plotu, uu[0]) myplot.plot_vtk(plotu, index) myplot.gather_vtkplots() print 'Check different spatial sampling' QQ = [4, 5, 6, 10] for qq in QQ: N = int(qq / cmin) h = 1. / N
for r in RR: V = FunctionSpace(mesh, 'Lagrange', r) Pt = PointSources(V, [[.5, .5]]) mydelta = Pt[0].array() def mysrc(tt): return Ricker(tt) * mydelta # Computation: if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt) Wave = AcousticWave({'V': V, 'Vm': Vl}) #Wave.verbose = True Wave.timestepper = 'centered' Wave.lump = True Wave.set_abc(mesh, AllFour(), True) Wave.exact = Function(V) Wave.update({'b':1.0, 'a':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':Function(V), 'utinit':Function(V)}) Wave.ftime = mysrc sol, error = Wave.solve() ERROR.append(error) if myrank == 0: print 'relative error = {:.5e}'.format(error) if not mycomm == None: MPI.barrier(mycomm) # Plots: try: boolplot = int(sys.argv[1]) except: boolplot = 0 if boolplot > 0: filename, ext = splitext(sys.argv[0])
alpha = 5. for Nxy in NN: h = 1. / Nxy mesh = UnitSquareMesh(Nxy, Nxy) V = FunctionSpace(mesh, 'Lagrange', q) Vl = FunctionSpace(mesh, 'Lagrange', 1) Dt = h / (q * alpha * c) if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt) Wave = AcousticWave({'V': V, 'Vm': Vl}) #Wave.verbose = True Wave.timestepper = 'centered' Wave.lump = True Wave.set_abc(mesh, LeftRight(), True) Wave.exact = interpolate(exact_expr, V) Wave.update({'b':b, 'a':a, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':interpolate(u0_expr, V), 'utinit':Function(V)}) Wave.ftime = lambda t: 0.0 sol, error = Wave.solve() ERROR.append(error) if myrank == 0: print 'relative error = {:.5e}'.format(error) if not mycomm == None: MPI.barrier(mycomm) if myrank == 0: # Order of convergence: CONVORDER = [] for ii in range(len(ERROR) - 1): CONVORDER.append( np.log(ERROR[ii + 1] / ERROR[ii]) / np.log( (1. / NN[ii + 1]) / (1. / NN[ii])))
h = 1./N mesh = dl.UnitSquareMesh(N,N) Vl = dl.FunctionSpace(mesh, 'Lagrange', 1) Vex = dl.FunctionSpace(mesh, 'Lagrange', r) Pt = PointSources(Vex, [[.5,.5]]) mydelta = Pt[0].array() def mysrc(tt): return Ricker(tt)*mydelta Waveex = AcousticWave({'V':Vex, 'Vl':Vl, 'Vr':Vl}) Waveex.timestepper = 'backward' Waveex.lump = True Waveex.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':dl.Function(Vex), 'utinit':dl.Function(Vex)}) Waveex.ftime = mysrc sol,_ = Waveex.solve() Waveex.exact = dl.Function(Vex) normex = Waveex.computeabserror() # plot myplot.set_varname('u-q'+str(qq)) plotu = dl.Function(Vex) for index, uu in enumerate(sol): if index%boolplot == 0: setfct(plotu, uu[0]) myplot.plot_vtk(plotu, index) myplot.gather_vtkplots() print 'Check different spatial sampling' QQ = [4, 5, 6, 10] for qq in QQ: N = int(qq/cmin) h = 1./N