def RunAcoustic(q): Nxy = 100 tf = 0.1 # Final time direction = 0 u0_expr = Expression(\ '100*pow(x[i]-.25,2)*pow(x[i]-0.75,2)*(x[i]<=0.75)*(x[i]>=0.25)', i=direction) class LeftRight(SubDomain): def inside(self, x, on_boundary): return (x[direction] < 1e-16 or x[direction] > 1.0 - 1e-16) \ and on_boundary h = 1. / Nxy mesh = UnitSquareMesh(Nxy, Nxy, "crossed") V = FunctionSpace(mesh, 'Lagrange', q) Vl = FunctionSpace(mesh, 'Lagrange', 1) Dt = h / (q * 10.) Wave = AcousticWave({'V': V, 'Vl': Vl, 'Vr': Vl}) Wave.verbose = True Wave.lump = True Wave.set_abc(mesh, LeftRight()) Wave.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':interpolate(u0_expr, V), 'utinit':Function(V)}) sol, tmp = Wave.solve()
def RunAcoustic(q): Nxy = 100 tf = 0.1 # Final time direction = 0 u0_expr = Expression("100*pow(x[i]-.25,2)*pow(x[i]-0.75,2)*(x[i]<=0.75)*(x[i]>=0.25)", i=direction) class LeftRight(SubDomain): def inside(self, x, on_boundary): return (x[direction] < 1e-16 or x[direction] > 1.0 - 1e-16) and on_boundary h = 1.0 / Nxy mesh = UnitSquareMesh(Nxy, Nxy, "crossed") V = FunctionSpace(mesh, "Lagrange", q) Vl = FunctionSpace(mesh, "Lagrange", 1) Dt = h / (q * 10.0) Wave = AcousticWave({"V": V, "Vl": Vl, "Vr": Vl}) Wave.verbose = True Wave.lump = True Wave.set_abc(mesh, LeftRight()) Wave.update( { "lambda": 1.0, "rho": 1.0, "t0": 0.0, "tf": tf, "Dt": Dt, "u0init": interpolate(u0_expr, V), "utinit": Function(V), } ) sol, tmp = Wave.solve()
def run_test(): q = 3 Nxy = 100 tf = 0.1 h = 1./Nxy mesh = UnitSquareMesh(Nxy, Nxy, "crossed") V = FunctionSpace(mesh, 'Lagrange', q) Vl = FunctionSpace(mesh, 'Lagrange', 1) Dt = h/(q*10.) u0_expr = Expression(\ '100*pow(x[i]-.25,2)*pow(x[i]-0.75,2)*(x[i]<=0.75)*(x[i]>=0.25)', i=0) Wave = AcousticWave({'V':V, 'Vl':Vl, 'Vr':Vl}) Wave.lump = True Wave.timestepper = 'backward' Wave.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':interpolate(u0_expr, V), 'utinit':Function(V)}) K = Wave.K u = Wave.u0 u.vector()[:] = 1.0 b = Wave.u1 for ii in range(100): K*u.vector() (K*u.vector()).array() b.vector()[:] = (K*u.vector()).array() b.vector()[:] = 0.0 b.vector().axpy(1.0, K*u.vector()) b.vector()[:] = (K*u.vector()).array() + (K*u.vector()).array() b.vector()[:] = (K*u.vector() + K*u.vector()).array() b.vector()[:] = 2.*u.vector().array() + u.vector().array() + \ Dt*u.vector().array() b.vector()[:] = 0.0 b.vector().axpy(2.0, u.vector()) b.vector().axpy(1.0, u.vector()) b.vector().axpy(Dt, u.vector()) b.assign(u) b.vector().zero()
def run_test(): q = 3 Nxy = 100 tf = 0.1 h = 1. / Nxy mesh = UnitSquareMesh(Nxy, Nxy, "crossed") V = FunctionSpace(mesh, 'Lagrange', q) Vl = FunctionSpace(mesh, 'Lagrange', 1) Dt = h / (q * 10.) u0_expr = Expression(\ '100*pow(x[i]-.25,2)*pow(x[i]-0.75,2)*(x[i]<=0.75)*(x[i]>=0.25)', i=0) Wave = AcousticWave({'V': V, 'Vl': Vl, 'Vr': Vl}) Wave.lump = True Wave.timestepper = 'backward' Wave.update({'lambda':1.0, 'rho':1.0, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':interpolate(u0_expr, V), 'utinit':Function(V)}) K = Wave.K u = Wave.u0 u.vector()[:] = 1.0 b = Wave.u1 for ii in range(100): K * u.vector() (K * u.vector()).array() b.vector()[:] = (K * u.vector()).array() b.vector()[:] = 0.0 b.vector().axpy(1.0, K * u.vector()) b.vector()[:] = (K * u.vector()).array() + (K * u.vector()).array() b.vector()[:] = (K * u.vector() + K * u.vector()).array() b.vector()[:] = 2.*u.vector().array() + u.vector().array() + \ Dt*u.vector().array() b.vector()[:] = 0.0 b.vector().axpy(2.0, u.vector()) b.vector().axpy(1.0, u.vector()) b.vector().axpy(Dt, u.vector()) b.assign(u) b.vector().zero()
class AllFour(SubDomain): def inside(self, x, on_boundary): 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, '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:
and on_boundary q = 2 if myrank == 0: print '\npolynomial order = {}'.format(q) #alpha = 3. 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, 'Vl':Vl, 'Vr':Vl}) #Wave.verbose = True Wave.timestepper = 'centered' Wave.lump = True Wave.set_abc(mesh, LeftRight(), True) Wave.exact = interpolate(exact_expr, V) Wave.update({'lambda':lam, 'rho':rho, '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:
# target medium medformula = "A*A*(x[1]>=0.5) + 2.0*2.0*(x[1]<0.5)" TargetMedExpr = dl.Expression(medformula, A=1.5) TargetMed = dl.interpolate(TargetMedExpr, Vl) myplot.set_varname("target_medium") myplot.plot_vtk(TargetMed) # perturbation # PerturbationMedExpr = dl.Expression(medformula, A=1.0) # PerturbMed = dl.interpolate(PerturbationMedExpr, Vl) # myplot.set_varname('perturb_medium') # myplot.plot_vtk(PerturbMed) # observation operator: obspts = [[x / 10.0, 1.0] for x in range(1, 10)] obsop = TimeObsPtwise({"V": V, "Points": obspts}) # define pde operator: wavepde = AcousticWave({"V": V, "Vl": Vl, "Vr": Vl}) wavepde.timestepper = "backward" wavepde.lump = True wavepde.set_abc(mesh, ABC(), True) wavepde.update( {"lambda": TargetMed, "rho": 1.0, "t0": t0, "tf": tf, "Dt": Dt, "u0init": dl.Function(V), "utinit": dl.Function(V)} ) wavepde.ftime = mysrc # define objective function: waveobj = ObjectiveAcoustic(wavepde) waveobj.obsop = obsop # data print "generate data" waveobj.solvefwd() myplot.plot_timeseries(waveobj.solfwd, "pd", 0, 40, fctV) dd = waveobj.Bp.copy()
V = FunctionSpace(mesh, 'Lagrange', r) Pt = PointSources(V, [[.5, 1.]]) mydelta = Pt[0] src = Function(V) srcv = src.vector() def mysrc(tt): srcv.zero() srcv.axpy(Ricker(tt), mydelta) return srcv # Computation: if myrank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt) Wave = AcousticWave({'V': V, 'Vm': Vl}) #Wave.verbose = True Wave.timestepper = 'backward' Wave.lump = True #Wave.set_abc(mesh, AllFour(), True) lambda_target = Expression('1.0 + 3.0*(' \ '(x[0]>=0.3)*(x[0]<=0.7)*(x[1]>=0.3)*(x[1]<=0.7))') lambda_target_fn = interpolate(lambda_target, Vl) Wave.update({'b':lambda_target_fn, 'a':1.0, \ 't0':0.0, 'tf':tf, 'Dt':Dt, 'u0init':Function(V), 'utinit':Function(V)}) Wave.ftime = mysrc sol, tmp = Wave.solve() # Observations myObs = TimeObsPtwise({ 'V': V, 'Points': [[.5, .2], [.5, .8], [.2, .5], [.8, .5]]
def u0_boundary(x, on_boundary): return on_boundary ubc = Constant("0.0") q = 2 if myrank == 0: print '\npolynomial order = {}'.format(q) for Nxy in NN: h = 1./Nxy if myrank == 0: print '\n\th = {}'.format(h) mesh = UnitSquareMesh(Nxy, Nxy, "crossed") V = FunctionSpace(mesh, 'Lagrange', q) Vl = FunctionSpace(mesh, 'Lagrange', 1) Dt = h/(q*8.*np.sqrt(2)) Wave = AcousticWave({'V':V, 'Vm':Vl}) Wave.timestepper = 'backward' Wave.lump = True Wave.exact = Function(V) Wave.bc = DirichletBC(V, ubc, u0_boundary) Wave.update({'b':8.0, 'a':2.0, '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: CONVORDER = [] for ii in range(len(ERROR)-1):
return (x[direction] < 1e-16 or x[direction] > 1.0-1e-16) and on_boundary ubc = Constant("0.0") 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):
#Pt = PointSources(V, [[0.1,y_src], [0.25,y_src], [0.4,y_src],\ #[0.6,y_src], [0.75,y_src], [0.9,y_src]]) Pt = PointSources(V, [[0.1, y_src], [0.4, y_src], [0.6, y_src], [0.9, y_src]]) srcv = dl.Function(V).vector() # Boundary conditions: class ABCdom(dl.SubDomain): def inside(self, x, on_boundary): return on_boundary and (x[1] < Y) Wave = AcousticWave({ 'V': V, 'Vm': Vl }, { 'print': False, 'lumpM': True, 'timestepper': 'backward' }) Wave.set_abc(mesh, ABCdom(), lumpD=False) at, bt, ct, _, _ = targetmediumparameters(Vl, X) a0, b0, _, _, _ = initmediumparameters(Vl, X) Wave.update({'b':bt, 'a':at, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) if PRINT: print 'nb of src={}, nb of timesteps={}'.format(len(Pt.src_loc), Wave.Nt) sources, timesteps = partition_work(mpicomm_local, mpicomm_global, \ len(Pt.src_loc), Wave.Nt)
b_target_fn = dl.interpolate(b_target, Vm) a_target = dl.Expression(\ '1.0 + 0.4*(x[0]<=0.7)*(x[0]>=0.3)*(x[1]<=0.7)*(x[1]>=0.3)') a_target_fn = dl.interpolate(a_target, Vm) checkdt(Dt, 1. / Nxy, r, np.sqrt(2.0), False) # observation operator: obspts = [[0.0, ii/10.] for ii in range(1,10)] + \ [[1.0, ii/10.] for ii in range(1,10)] + \ [[ii/10., 0.0] for ii in range(1,10)] + \ [[ii/10., 1.0] for ii in range(1,10)] obsop = TimeObsPtwise({'V': V, 'Points': obspts}, [t0, t1, t2, tf]) # define pde operator: wavepde = AcousticWave({'V': V, 'Vm': Vm}) wavepde.timestepper = 'backward' wavepde.lump = True wavepde.update({'a':a_target_fn, 'b':b_target_fn, \ 't0':t0, 'tf':tf, 'Dt':Dt, 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) # parameters Vm = wavepde.Vm V = wavepde.V lenobspts = obsop.PtwiseObs.nbPts # set up plots: filename, ext = splitext(sys.argv[0]) if isdir(filename + '/'): rmtree(filename + '/') myplot = PlotFenics(filename + str(Nxy)) myplot.set_varname('a_target')
q = 2 if myrank == 0: print '\npolynomial order = {}'.format(q) #alpha = 3. 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:
# Source term: Ricker = RickerWavelet(fpeak, 1e-10) # Boundary conditions: #class AllFour(SubDomain): # def inside(self, x, on_boundary): # return on_boundary V = FunctionSpace(mesh, 'Lagrange', r) Pt = PointSources(V, [[.5,1.]]) 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 = 'backward' Wave.lump = True #Wave.set_abc(mesh, AllFour(), True) lambda_target = Expression('1.0 + 3.0*(' \ '(x[0]>=0.3)*(x[0]<=0.7)*(x[1]>=0.3)*(x[1]<=0.7))') lambda_target_fn = interpolate(lambda_target, Vl) Wave.update({'lambda':lambda_target_fn, 'rho':1.0, \ 't0':0.0, 'tf':tf, 'Dt':Dt, 'u0init':Function(V), 'utinit':Function(V)}) Wave.ftime = mysrc sol, tmp = Wave.solve() if not mycomm == None: MPI.barrier(mycomm) # Observations myObs = TimeObsPtwise({'V':V, 'Points':[[.5,.2], [.5,.8], [.2,.5], [.8,.5]]}) Bp = np.zeros((4, len(sol)))
if isdir(filename + '/'): rmtree(filename + '/') myplot = PlotFenics(filename) boolplot = 100 print 'Compute most accurate solution as reference' qq = 20 N = int(qq/cmin) 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)
t=tf) 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()
def run_test(fpeak, lambdamin, lambdamax, Nxy, tfilterpts, r, Dt, skip): h = 1./Nxy checkdt(Dt, h, r, np.sqrt(lambdamax), True) mesh = dl.UnitSquareMesh(Nxy, Nxy) Vl = dl.FunctionSpace(mesh, 'Lagrange', 1) V = dl.FunctionSpace(mesh, 'Lagrange', r) fctV = dl.Function(V) # set up plots: filename, ext = splitext(sys.argv[0]) if isdir(filename + '/'): rmtree(filename + '/') myplot = PlotFenics(filename) # source: Ricker = RickerWavelet(fpeak, 1e-10) Pt = PointSources(V, [[0.5,0.5]]) mydelta = Pt[0].array() def mysrc(tt): return Ricker(tt)*mydelta # target medium: lambda_target = dl.Expression('lmin + x[0]*(lmax-lmin)', \ lmin=lambdamin, lmax=lambdamax) lambda_target_fn = dl.interpolate(lambda_target, Vl) myplot.set_varname('lambda_target') myplot.plot_vtk(lambda_target_fn) # initial medium: lambda_init = dl.Constant(lambdamin) lambda_init_fn = dl.interpolate(lambda_init, Vl) myplot.set_varname('lambda_init') myplot.plot_vtk(lambda_init_fn) # observation operator: #obspts = [[0.2, 0.5], [0.5, 0.2], [0.5, 0.8], [0.8, 0.5]] obspts = [[0.2, ii/10.] for ii in range(2,9)] + \ [[0.8, ii/10.] for ii in range(2,9)] + \ [[ii/10., 0.2] for ii in range(3,8)] + \ [[ii/10., 0.8] for ii in range(3,8)] obsop = TimeObsPtwise({'V':V, 'Points':obspts}, tfilterpts) # define pde operator: wavepde = AcousticWave({'V':V, 'Vl':Vl, 'Vr':Vl}) wavepde.timestepper = 'centered' wavepde.lump = True wavepde.set_abc(mesh, LeftRight(), True) wavepde.update({'lambda':lambda_target_fn, 'rho':1.0, \ 't0':t0, 'tf':tf, 'Dt':Dt, 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) wavepde.ftime = mysrc # define objective function: waveobj = ObjectiveAcoustic(wavepde) waveobj.obsop = obsop # data print 'generate noisy data' waveobj.solvefwd() myplot.plot_timeseries(waveobj.solfwd, 'pd', 0, skip, fctV) dd = waveobj.Bp.copy() nbobspt, dimsol = dd.shape noiselevel = 0.1 # = 10% sigmas = np.sqrt((dd**2).sum(axis=1)/dimsol)*noiselevel rndnoise = np.random.randn(nbobspt*dimsol).reshape((nbobspt, dimsol)) waveobj.dd = dd + sigmas.reshape((len(sigmas),1))*rndnoise # gradient print 'generate observations' waveobj.update_m(lambda_init_fn) waveobj.solvefwd_cost() cost1 = waveobj.misfit print 'misfit = {}'.format(waveobj.misfit) myplot.plot_timeseries(waveobj.solfwd, 'p', 0, skip, fctV) # Plot data and observations fig = plt.figure() if len(obspts) > 9: fig.set_size_inches(20., 15.) for ii in range(len(obspts)): if len(obspts) == 4: ax = fig.add_subplot(2,2,ii+1) else: ax = fig.add_subplot(4,6,ii+1) ax.plot(waveobj.PDE.times, waveobj.dd[ii,:], 'k--') ax.plot(waveobj.PDE.times, waveobj.Bp[ii,:], 'b') ax.set_title('Plot'+str(ii)) fig.savefig(filename + '/observations.eps') print 'compute gradient' waveobj.solveadj_constructgrad() myplot.plot_timeseries(waveobj.soladj, 'v', 0, skip, fctV) MG = waveobj.MGv.array().copy() myplot.set_varname('grad') myplot.plot_vtk(waveobj.Grad) print 'check gradient with FD' Medium = np.zeros((5, Vl.dim())) for ii in range(5): smoothperturb = dl.Expression('sin(n*pi*x[0])*sin(n*pi*x[1])', n=ii+1) smoothperturb_fn = dl.interpolate(smoothperturb, Vl) Medium[ii,:] = smoothperturb_fn.vector().array() checkgradfd_med(waveobj, Medium, 1e-6, [1e-5, 1e-4]) print 'check Hessian with FD' checkhessfd_med(waveobj, Medium, 1e-6, [1e-1, 1e-2, 1e-3, 1e-4, 1e-5], False)
print 'Compute most accurate solution as reference' qq = 20 N = int(qq / cmin) 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, '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)
V = dl.FunctionSpace(mesh, 'Lagrange', r) y_src = 1.0 # 0.1->transmission, 1.0->reflection Pt = PointSources(V, [[0.5 * X, y_src]]) mydelta = Pt[0] def mysrc(tt): return mydelta * Ricker(tt) # Computation: if mpirank == 0: print '\n\th = {}, Dt = {}'.format(h, Dt) Wave = AcousticWave({ 'V': V, 'Vm': Vl }, { 'print': (not mpirank), 'lumpM': True, 'timestepper': 'backward' }) Wave.set_abc(mesh, ABCdom(), lumpD=False) #Wave.exact = dl.Function(V) Wave.ftime = mysrc # af, bf, _, _, _ = targetmediumparameters(Vl, X, myplot) # Wave.update({'b':bf, 'a':af, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) sol, _, _, error = Wave.solve() if mpirank == 0: print 'relative error = {:.5e}'.format(error) MPI.barrier(mesh.mpi_comm())
Ricker = RickerWavelet(fpeak, 1e-10) # Boundary conditions: class AllFour(SubDomain): def inside(self, x, on_boundary): 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:
def run_test(fpeak, lambdamin, lambdamax, Nxy, tfilterpts, r, Dt, skip): h = 1. / Nxy checkdt(Dt, h, r, np.sqrt(lambdamax), True) mesh = dl.UnitSquareMesh(Nxy, Nxy) Vl = dl.FunctionSpace(mesh, 'Lagrange', 1) V = dl.FunctionSpace(mesh, 'Lagrange', r) fctV = dl.Function(V) # set up plots: filename, ext = splitext(sys.argv[0]) if isdir(filename + '/'): rmtree(filename + '/') myplot = PlotFenics(filename) # source: Ricker = RickerWavelet(fpeak, 1e-10) Pt = PointSources(V, [[0.5, 0.5]]) mydelta = Pt[0].array() def mysrc(tt): return Ricker(tt) * mydelta # target medium: lambda_target = dl.Expression('lmin + x[0]*(lmax-lmin)', \ lmin=lambdamin, lmax=lambdamax) lambda_target_fn = dl.interpolate(lambda_target, Vl) myplot.set_varname('lambda_target') myplot.plot_vtk(lambda_target_fn) # initial medium: lambda_init = dl.Constant(lambdamin) lambda_init_fn = dl.interpolate(lambda_init, Vl) myplot.set_varname('lambda_init') myplot.plot_vtk(lambda_init_fn) # observation operator: #obspts = [[0.2, 0.5], [0.5, 0.2], [0.5, 0.8], [0.8, 0.5]] obspts = [[0.2, ii/10.] for ii in range(2,9)] + \ [[0.8, ii/10.] for ii in range(2,9)] + \ [[ii/10., 0.2] for ii in range(3,8)] + \ [[ii/10., 0.8] for ii in range(3,8)] obsop = TimeObsPtwise({'V': V, 'Points': obspts}, tfilterpts) # define pde operator: wavepde = AcousticWave({'V': V, 'Vl': Vl, 'Vr': Vl}) wavepde.timestepper = 'backward' wavepde.lump = True wavepde.set_abc(mesh, LeftRight(), True) wavepde.update({'lambda':lambda_target_fn, 'rho':1.0, \ 't0':t0, 'tf':tf, 'Dt':Dt, 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) wavepde.ftime = mysrc # define objective function: waveobj = ObjectiveAcoustic(wavepde) waveobj.obsop = obsop # data print 'generate data' waveobj.solvefwd() myplot.plot_timeseries(waveobj.solfwd, 'pd', 0, skip, fctV) dd = waveobj.Bp.copy() # gradient print 'generate observations' waveobj.dd = dd waveobj.update_m(lambda_init_fn) waveobj.solvefwd_cost() cost1 = waveobj.misfit print 'misfit = {}'.format(waveobj.misfit) myplot.plot_timeseries(waveobj.solfwd, 'p', 0, skip, fctV) # Plot data and observations fig = plt.figure() if len(obspts) > 9: fig.set_size_inches(20., 15.) for ii in range(len(obspts)): if len(obspts) == 4: ax = fig.add_subplot(2, 2, ii + 1) else: ax = fig.add_subplot(4, 6, ii + 1) ax.plot(waveobj.PDE.times, waveobj.dd[ii, :], 'k--') ax.plot(waveobj.PDE.times, waveobj.Bp[ii, :], 'b') ax.set_title('Plot' + str(ii)) fig.savefig(filename + '/observations.eps') print 'compute gradient' waveobj.solveadj_constructgrad() myplot.plot_timeseries(waveobj.soladj, 'v', 0, skip, fctV) MG = waveobj.MGv.array().copy() myplot.set_varname('grad') myplot.plot_vtk(waveobj.Grad) """
tf = 0.2/c # Final time exact_expr = Expression(\ '(pow(t,2)-(pow(x[0]-0.5,2)+pow(x[1]-0.5,2)))*(sqrt(pow(x[0]-0.5,2)+pow(x[1]-0.5,2))<=t)', \ t=tf) 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)
# Boundary conditions: def u0_boundary(x, on_boundary): return (x[direction] < 1e-16 or x[direction] > 1.0 - 1e-16) and on_boundary ubc = Constant("0.0") for Nxy in NN: h = 1. / Nxy print '\n\th = {}'.format(h) mesh = UnitSquareMesh(Nxy, Nxy, "crossed") q = 1 # This example is solved 'exactly' for q>=2 V = FunctionSpace(mesh, 'Lagrange', q) Dt = h / (q * 5. * np.sqrt(c2)) Wave = AcousticWave({'V': V, 'Vl': V, 'Vr': V}) Wave.timestepper = 'backward' Wave.lump = True Wave.exact = interpolate(exact_expr, V) Wave.bc = DirichletBC(V, ubc, u0_boundary) Wave.update({'lambda':lam, 'rho':rho, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':Function(V), 'utinit':interpolate(utinit_expr, V)}) test = TestFunction(V) source = assemble(Constant('2') * test * dx) Wave.ftime = lambda tt: tt * source.array() sol, error = Wave.solve() ERROR.append(error) print 'relative error = {:.5e}'.format(error) # Convergence order: CONVORDER = []