# set up plots: filename, ext = splitext(sys.argv[0]) if isdir(filename + '/'): rmtree(filename + '/') myplot = PlotFenics(filename + str(Nxy)) myplot.set_varname('a_target') myplot.plot_vtk(a_target_fn) # define objective function: regul = LaplacianPrior({ 'Vm': Vm, 'gamma': 5e-4, 'beta': 5e-4, 'm0': a_target_fn }) waveobj = ObjectiveAcoustic(wavepde, mysrc, 'a', regul) waveobj.obsop = obsop # noisy data print 'generate noisy data' waveobj.solvefwd() skip = 20 myplot.plot_timeseries(waveobj.solfwd[0], 'pd', 0, skip, dl.Function(V)) DD = waveobj.Bp[:] noiselevel = 0.1 # = 10% for ii, dd in enumerate(DD): np.random.seed(11) nbobspt, dimsol = dd.shape sigmas = np.sqrt((dd**2).sum(axis=1) / dimsol) * noiselevel rndnoise = np.random.randn(nbobspt * dimsol).reshape((nbobspt, dimsol)) DD[ii] = dd + sigmas.reshape((len(sigmas), 1)) * rndnoise
mpilocalrank = MPI.rank(mpicomm_local) mpiglobalrank = MPI.rank(mpicomm_global) mpiworldsize = MPI.size(dl.mpi_comm_world()) print 'mpiworldrank={}, mpiglobalrank={}, mpilocalrank={}, sources={}, timestep=[{},{}]'.format(\ mpiworldrank, mpiglobalrank, mpilocalrank, sources,\ timesteps[0], timesteps[-1]) # observation operator: obspts = [[ii * float(X) / float(Nxy), Y] for ii in range(1, Nxy)] tfilterpts = [t0, t1, t2, tf] obsop = TimeObsPtwise({'V': V, 'Points': obspts}, tfilterpts) # define objective function: if FDGRAD: waveobj = ObjectiveAcoustic(mpicomm_global, Wave, [Ricker, Pt, srcv],\ sources, timesteps, PARAM) else: # REGULARIZATION: amg = 'hypre_amg' #regul = V_TV(Vl, {'k':k, 'eps':eps, 'amg':amg,\ #'print':PRINT, 'GNhessian':False}) regul = V_TVPD(Vl, {'k':k, 'eps':eps, 'amg':amg,\ 'rescaledradiusdual':1.0, 'print':PRINT, 'PCGN':False}) waveobj = ObjectiveAcoustic(mpicomm_global, Wave, [Ricker, Pt, srcv],\ sources, timesteps, PARAM, regul) waveobj.obsop = obsop #waveobj.GN = True # Generate synthetic observations if PRINT: print 'generate noisy data' waveobj.solvefwd()
Wave.update({'b':bt, 'a':at, 't0':0.0, 'tf':tf, 'Dt':Dt,\ 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) # 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)] tfilterpts = [t0, t1, t2, tf] obsop = TimeObsPtwise({'V':V, 'Points':obspts}, tfilterpts) # Regularization regula = LaplacianPrior({'Vm':Vl, 'gamma':1e-4, 'beta':1e-4, 'm0':at}) regulab = SingleRegularization(regula, 'a', (not mpirank)) # define objective function: waveobjab = ObjectiveAcoustic(Wave, [Ricker, Pt, srcv], 'a', regulab) waveobjabnoregul = ObjectiveAcoustic(Wave, [Ricker, Pt, srcv], 'a') waveobjab.obsop = obsop waveobjabnoregul.obsop = obsop # Generate synthetic observations if mpirank == 0: print 'generate noisy data' waveobjab.solvefwd() DD = waveobjab.Bp[:] if NOISE: SNRdB = 20.0 # [dB], i.e, log10(mu/sigma) = SNRdB/10 np.random.seed(11) for ii, dd in enumerate(DD): if mpirank == 0: print 'source {}'.format(ii) nbobspt, dimsol = dd.shape #mu = np.abs(dd).mean(axis=1)
# 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() waveobj.dd = dd # Plot observations # fig = plt.figure() # for ii in range(len(obspts)): # ax = fig.add_subplot(3,3,ii+1) # ax.plot(waveobj.times, waveobj.dd[ii,:], 'k--') # ax.plot(waveobj.times, waveobj.Bp[ii,:], 'r--') # ax.set_title('recv '+str(ii+1))
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) """
# set up plots: filename, ext = splitext(sys.argv[0]) #if mpirank == 0 and isdir(filename + '/'): rmtree(filename + '/') #MPI.barrier(mpicomm) myplot = PlotFenics(filename + str(Nxy)) myplot.set_varname('b_target') myplot.plot_vtk(b_target_fn) # define objective function: regul = LaplacianPrior({ 'Vm': Vm, 'gamma': 5e-4, 'beta': 5e-4, 'm0': a_target_fn }) waveobj = ObjectiveAcoustic(wavepde, mysrc, 'b', regul) waveobj.obsop = obsop # noisy data #TODO: to be fixed for parallel computation if mpirank == 0: print 'generate noisy data' waveobj.solvefwd() skip = 20 myplot.plot_timeseries(waveobj.solfwd[0], 'pd', 0, skip, dl.Function(V)) DD = waveobj.Bp[:] noiselevel = 0.1 # = 10% for ii, dd in enumerate(DD): np.random.seed(11) nbobspt, dimsol = dd.shape sigmas = np.sqrt((dd**2).sum(axis=1) / dimsol) * noiselevel rndnoise = np.random.randn(nbobspt * dimsol).reshape((nbobspt, dimsol))
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)
# parameters Vm = wavepde.Vm V = wavepde.V lenobspts = obsop.PtwiseObs.nbPts # set up plots: #filename, ext = splitext(sys.argv[0]) #myplot = PlotFenics(outputdirectory + filename + str(freq)) #MPI.barrier(mpicomm) #myplot.set_varname('b_target') #myplot.plot_vtk(b_target_fn) if mpirank == 0: print 'Define objective function' # define objective function: waveobj = ObjectiveAcoustic(wavepde, mysrc, 'b', regul) waveobj.obsop = obsop # noisy data if mpirank == 0: print 'generate noisy data' waveobj.solvefwd() DD = waveobj.Bp[:] noiselevel = 0.1 # = 10% for ii, dd in enumerate(DD): np.random.seed(11) nbobspt, dimsol = dd.shape sigmas = np.sqrt((dd**2).sum(axis=1)/dimsol)*noiselevel rndnoise = np.random.randn(nbobspt*dimsol).reshape((nbobspt, dimsol)) DD[ii] = dd + sigmas.reshape((len(sigmas),1))*rndnoise waveobj.dd = DD waveobj.solvefwd_cost()
# source: if mpirank == 0: print 'sources' srcloc = [[ii / 10., 1.0] for ii in range(1, 10, 2)] #srcloc = [[0.1, 0.9]] Ricker = RickerWavelet(freq, 1e-10) Pt = PointSources(V, srcloc) src = dl.Function(V) srcv = src.vector() mysrc = [Ricker, Pt, srcv] # define objective function: wavepde.update({'a':a_target_fn, 'b':b_target_fn, \ 't0':t0, 'tf':tf, 'Dt':Dt, 'u0init':dl.Function(V), 'utinit':dl.Function(V)}) regul = LaplacianPrior({'Vm': Vm, 'gamma': 1e-4, 'beta': 1e-4, 'm0': 1.0}) waveobj = ObjectiveAcoustic(wavepde, mysrc, 'b', regul) obsop = TimeObsPtwise({'V': V, 'Points': obspts}, t0tf) waveobj.obsop = obsop # Save MAP point, or assemble Hessian at MAP point if SAVE_MAP: # noisy data if mpirank == 0: print 'generate noisy data' waveobj.solvefwd() DD = waveobj.Bp[:] noiselevel = 0.1 # = 10% for ii, dd in enumerate(DD): np.random.seed(11) nbobspt, dimsol = dd.shape sigmas = np.sqrt((dd**2).sum(axis=1) / dimsol) * noiselevel rndnoise = np.random.randn(nbobspt * dimsol).reshape((nbobspt, dimsol))