def adjoint_test_rho(): import numpy as np from pysit import PML, RectangularDomain, CartesianMesh, PointSource, ReceiverSet, Shot, VariableDensityAcousticWave, generate_seismic_data, PointReceiver, RickerWavelet from pysit.gallery.horizontal_reflector import horizontal_reflector # Setup # Define Domain pmlx = PML(0.1, 1000, ftype='quadratic') pmlz = PML(0.1, 1000, ftype='quadratic') x_config = (0.1, 1.0, pmlx, pmlx) z_config = (0.1, 1.0, pmlz, pmlz) d = RectangularDomain( x_config, z_config ) m = CartesianMesh(d, 70,80 ) # Generate true wave speed # (M = C^-2 - C0^-2) C,C0,m,d = horizontal_reflector(m) w=1.3 M = [w*C, C/w] M0 = [C0, C0] # Set up shots Nshots = 1 shots = [] xmin = d.x.lbound xmax = d.x.rbound nx = m.x.n zmin = d.z.lbound zmax = d.z.rbound for i in xrange(Nshots): # Define source location and type # source = PointSource(d, (xmax*(i+1.0)/(Nshots+1.0), 0.1), RickerWavelet(10.0)) source = PointSource(m, (.188888, 0.18888), RickerWavelet(10.0)) # Define set of receivers zpos = zmin + (1./9.)*zmax xpos = np.linspace(xmin, xmax, nx) receivers = ReceiverSet(m, [PointReceiver(m, (x, zpos)) for x in xpos]) # Create and store the shot shot = Shot(source, receivers) shots.append(shot) # Define and configure the wave solver trange=(0.,3.0) solver = VariableDensityAcousticWave(m, formulation='scalar', model_parameters={'kappa': M[0], 'rho' : M[1]}, spatial_accuracy_order=2, trange=trange, use_cpp_acceleration=False, time_accuracy_order=2) # Generate synthetic Seismic data np.random.seed(1) print('Generating data...') wavefields=[] base_model = solver.ModelParameters(m,{'kappa': M[0], 'rho':M[1]}) generate_seismic_data(shots, solver, base_model, wavefields=wavefields) tools = TemporalModeling(solver) m0 = solver.ModelParameters(m,{'kappa': M[0], 'rho':M[1]}) m1 = m0.perturbation() v=uniform(.5,2.2,len(m0.rho)).reshape((len(m0.rho),1)) #pertubation of m2 m1.rho=1.0/v fwdret = tools.forward_model(shot, m0, 1, ['wavefield', 'dWaveOp', 'simdata']) dWaveOp0 = fwdret['dWaveOp'] inc_field = fwdret['wavefield'] data = fwdret['simdata'] #data += np.random.rand(*data.shape) linfwdret = tools.linear_forward_model_rho(shot, m0, m1, ['simdata'],wavefield=inc_field) lindata = linfwdret['simdata'] adjret = tools.adjoint_model(shot, m0, data, 1, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0,wavefield=inc_field) # multiplied adjmodel by an additional m2 model. adjmodel = adjret['imaging_condition'].rho #adjmodel = 1.0/adjmodel #m1_C = m1.C print "data space ", np.sum(data*lindata)*solver.dt print "model space ", np.dot(v.T, adjmodel).squeeze()*np.prod(m.deltas) print "their diff ", np.dot(v.T, adjmodel).squeeze()*np.prod(m.deltas)-np.sum(data*lindata)*solver.dt
def adjoint_test(): #if __name__ == '__main__': import numpy as np from pysit import PML, RectangularDomain, CartesianMesh, PointSource, ReceiverSet, Shot, ConstantDensityAcousticWave, generate_seismic_data, PointReceiver, RickerWavelet from pysit.gallery import horizontal_reflector # Setup # Define Domain pmlx = PML(0.1, 1000, ftype='quadratic') pmlz = PML(0.1, 1000, ftype='quadratic') x_config = (0.1, 1.0, pmlx, pmlx) z_config = (0.1, 0.8, pmlz, pmlz) d = RectangularDomain( x_config, z_config ) m = CartesianMesh(d, 90, 70) # Generate true wave speed # (M = C^-2 - C0^-2) C0, C = horizontal_reflector(m) # Set up shots Nshots = 1 shots = [] xmin = d.x.lbound xmax = d.x.rbound nx = m.x.n zmin = d.z.lbound zmax = d.z.rbound for i in xrange(Nshots): # Define source location and type # source = PointSource(d, (xmax*(i+1.0)/(Nshots+1.0), 0.1), RickerWavelet(10.0)) source = PointSource(m, (.188888, 0.18888), RickerWavelet(10.0)) # Define set of receivers zpos = zmin + (1./9.)*zmax xpos = np.linspace(xmin, xmax, nx) receivers = ReceiverSet(m, [PointReceiver(m, (x, zpos)) for x in xpos]) # Create and store the shot shot = Shot(source, receivers) shots.append(shot) # Define and configure the wave solver trange=(0.,3.0) solver = ConstantDensityAcousticWave(m, formulation='ode', # formulation='scalar', model_parameters={'C': C}, spatial_accuracy_order=4, # spatial_shifted_differences=True, # cfl_safety=0.01, trange=trange, time_accuracy_order=4) # Generate synthetic Seismic data np.random.seed(1) print('Generating data...') wavefields=[] base_model = solver.ModelParameters(m,{'C': C}) generate_seismic_data(shots, solver, base_model, wavefields=wavefields) tools = TemporalModeling(solver) m0 = solver.ModelParameters(m,{'C': C0}) m1 = m0.perturbation() m1 += np.random.rand(*m1.data.shape) fwdret = tools.forward_model(shot, m0, ['wavefield', 'dWaveOp', 'simdata']) dWaveOp0 = fwdret['dWaveOp'] inc_field = fwdret['wavefield'] data = fwdret['simdata'] # data += np.random.rand(*data.shape) linfwdret = tools.linear_forward_model(shot, m0, m1, ['simdata']) lindata = linfwdret['simdata'] adjret = tools.adjoint_model(shot, m0, data, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0) adjmodel = adjret['imaging_condition'].asarray() adj_field = adjret['adjointfield'] m1 = m1.asarray() print data.shape, solver.nsteps print np.sum(data*lindata)*solver.dt print np.dot(m1.T, adjmodel).squeeze()*np.prod(m.deltas) print np.dot(m1.T, adjmodel).squeeze()*np.prod(m.deltas)-np.sum(data*lindata)*solver.dt qs = adj_field qhat = 0.0 dt = solver.dt for k in xrange(solver.nsteps): t = k * dt qhat += qs[k]*(np.exp(-1j*2.0*np.pi*10.0*t)*dt)
def adjoint_test(frequencies=[10.0, 10.5, 10.1413515123], plots=False, data_noise=0.0, purefrequency=False): # default frequencies are enough to indicate a bug due to integer offsets import numpy as np import matplotlib.pyplot as plt from pysit import PML, RectangularDomain, CartesianMesh, PointSource, ReceiverSet, Shot, ConstantDensityAcousticWave, generate_seismic_data, PointReceiver, RickerWavelet, FrequencyModeling, ConstantDensityHelmholtz, vis from pysit.gallery import horizontal_reflector # Define Domain pmlx = PML(0.3, 100, ftype='quadratic') pmlz = PML(0.3, 100, ftype='quadratic') x_config = (0.1, 1.0, pmlx, pmlx) z_config = (0.1, 0.8, pmlz, pmlz) d = RectangularDomain( x_config, z_config ) m = CartesianMesh(d, 90, 70) # Generate true wave speed # (M = C^-2 - C0^-2) C0, C = horizontal_reflector(m) # Set up shots Nshots = 1 shots = [] xmin = d.x.lbound xmax = d.x.rbound nx = m.x.n zmin = d.z.lbound zmax = d.z.rbound point_approx = 'delta' for i in range(Nshots): # Define source location and type source = PointSource(m, (.188888, 0.18888), RickerWavelet(10.0), approximation=point_approx) # Define set of receivers zpos = zmin + (1./9.)*zmax xpos = np.linspace(xmin, xmax, nx) receivers = ReceiverSet(m, [PointReceiver(m, (x, zpos)) for x in xpos]) # Create and store the shot shot = Shot(source, receivers) shots.append(shot) # Define and configure the wave solver trange=(0.,3.0) solver = ConstantDensityAcousticWave(m, # formulation='ode', formulation='scalar', model_parameters={'C': C}, spatial_accuracy_order=4, # spatial_shifted_differences=True, # cfl_safety=0.01, trange=trange, time_accuracy_order=4) tools = HybridModeling(solver) m0 = solver.ModelParameters(m,{'C': C0}) solver_frequency = ConstantDensityHelmholtz(m, model_parameters={'C': C0}, spatial_shifted_differences=True, spatial_accuracy_order=4) frequencytools = FrequencyModeling(solver_frequency) m0_freq = solver_frequency.ModelParameters(m,{'C': C0}) np.random.seed(0) m1 = m0.perturbation() pert = np.random.rand(*m1.data.shape) m1 += pert # freqs = [10.5514213] #[3.0, 5.0, 10.0] # freqs = [10.5] # freqs = np.linspace(3,19,8) freqs = frequencies fwdret = tools.forward_model(shot, m0, freqs, ['wavefield', 'dWaveOp', 'simdata_time']) dWaveOp0 = fwdret['dWaveOp'] data = fwdret['simdata_time'] u0hat = fwdret['wavefield'][freqs[0]] data += data_noise*np.random.rand(*data.shape) dhat = dict() for nu in freqs: dhat[nu]=0 assert data.shape[0] == solver.nsteps for k in range(solver.nsteps): t = k*solver.dt for nu in freqs: dhat[nu] += data[k,:]*np.exp(-1j*2*np.pi*nu*t)*solver.dt print("Hybrid:") linfwdret = tools.linear_forward_model(shot, m0, m1, freqs, ['simdata','wavefield1','simdata_time']) lindata = linfwdret['simdata'] lindata_time = linfwdret['simdata_time'] u1hat = linfwdret['wavefield1'][freqs[0]] adjret = tools.adjoint_model(shot, m0, data, freqs, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0) qhat = adjret['adjointfield'][freqs[0]] adjmodel = adjret['imaging_condition'].asarray() m1_ = m1.asarray() temp_data_prod = 0.0 for nu in freqs: temp_data_prod += np.dot(lindata[nu].reshape(dhat[nu].shape), np.conj(dhat[nu])) print(temp_data_prod) print(np.dot(m1_.T, np.conj(adjmodel)).squeeze()*np.prod(m.deltas)) print(np.dot(m1_.T, np.conj(adjmodel)).squeeze()*np.prod(m.deltas) - temp_data_prod) if purefrequency: print("Frequency:") linfwdret_freq = frequencytools.linear_forward_model(shot, m0, m1, freqs, ['simdata','wavefield1', 'dWaveOp0']) lindata_freq = linfwdret_freq['simdata'] u1hat_freq = linfwdret_freq['wavefield1'][freqs[0]] dWaveOp0_freq = linfwdret_freq['dWaveOp0'] adjret_freq = frequencytools.adjoint_model(shot, m0, dhat, freqs, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0_freq) qhat_freq = adjret_freq['adjointfield'][freqs[0]] adjmodel_freq = adjret_freq['imaging_condition'].asarray() temp_data_prod = 0.0 for nu in freqs: temp_data_prod += np.dot(lindata_freq[nu].reshape(dhat[nu].shape).T, np.conj(dhat[nu])) print(temp_data_prod.squeeze()) print(np.dot(m1_.T, np.conj(adjmodel_freq)).squeeze()*np.prod(m.deltas)) print(np.dot(m1_.T, np.conj(adjmodel_freq)).squeeze()*np.prod(m.deltas) - temp_data_prod.squeeze()) if plots: xx, zz = d.generate_grid() sl = [(xx>=0.1) & (xx<=0.99) & (zz>=0.1) & (zz<0.8)] pml_null = PML(0.0,100) x_bulk = (0.1, 1.0, 90, pml_null, pml_null) z_bulk = (0.1, 0.8, 70, pml_null, pml_null) d_bulk = Domain( (x_bulk, z_bulk) ) def clims(*args): rclim = min([np.real(x).min() for x in args]), max([np.real(x).max() for x in args]) iclim = min([np.imag(x).min() for x in args]), max([np.imag(x).max() for x in args]) return rclim, iclim qrclim, qiclim = clims(qhat, qhat_freq) u1rclim, u1iclim = clims(u1hat, u1hat_freq) plt.figure() plt.subplot(2,3,1) display_on_grid(np.real(u0hat[sl]), d_bulk) plt.title(r're(${\hat u_0}$)') plt.subplot(2,3,4) display_on_grid(np.imag(u0hat[sl]), d_bulk) plt.title(r'im(${\hat u_0}$)') plt.subplot(2,3,2) display_on_grid(np.real(qhat[sl]), d_bulk, clim=qrclim) plt.title(r're(${\hat q}$) H') plt.subplot(2,3,5) display_on_grid(np.imag(qhat[sl]), d_bulk, clim=qiclim) plt.title(r'im(${\hat q}$) H') plt.subplot(2,3,3) display_on_grid(np.real(u1hat[sl]), d_bulk, clim=u1rclim) plt.title(r're(${\hat u_1}$) H') plt.subplot(2,3,6) display_on_grid(np.imag(u1hat[sl]), d_bulk, clim=u1iclim) plt.title(r'im(${\hat u_1}$) H') plt.show() plt.figure() plt.subplot(2,3,1) display_on_grid(np.real(u0hat[sl]), d_bulk) plt.title(r're(${\hat u_0}$)') plt.subplot(2,3,4) display_on_grid(np.imag(u0hat[sl]), d_bulk) plt.title(r'im(${\hat u_0}$)') plt.subplot(2,3,2) display_on_grid(np.real(qhat_freq[sl]), d_bulk, clim=qrclim) plt.title(r're(${\hat q}$) P') plt.subplot(2,3,5) display_on_grid(np.imag(qhat_freq[sl]), d_bulk, clim=qiclim) plt.title(r'im(${\hat q}$) P') plt.subplot(2,3,3) display_on_grid(np.real(u1hat_freq[sl]), d_bulk, clim=u1rclim) plt.title(r're(${\hat u_1}$) P') plt.subplot(2,3,6) display_on_grid(np.imag(u1hat_freq[sl]), d_bulk, clim=u1iclim) plt.title(r'im(${\hat u_1}$) P') plt.show()
# (M = C^-2 - C0^-2) C0, C = horizontal_reflector(m) # Set up shots shots = list() xmin = d.x.lbound xmax = d.x.rbound nx = m.x.n zmin = d.z.lbound zmax = d.z.rbound point_approx = 'delta' # Define source location and type source = PointSource(m, (.188888, 0.18888), RickerWavelet(10.0), approximation=point_approx) # Define set of receivers zpos = zmin + (1./9.)*zmax xpos = np.linspace(xmin, xmax, nx) receivers = ReceiverSet(m, [PointReceiver(m, (x, zpos)) for x in xpos]) # Create and store the shot shot = Shot(source, receivers) shots.append(shot) # Define and configure the wave solver trange=(0.,3.0) solver = ConstantDensityAcousticWave(m, formulation='scalar', model_parameters={'C': C},
def adjoint_test(): #if __name__ == '__main__': # from pysit import * import numpy as np import matplotlib.pyplot as plt from pysit import PML, Dirichlet, RectangularDomain, CartesianMesh, PointSource, ReceiverSet, Shot, ConstantDensityAcousticWave,ConstantDensityHelmholtz, generate_seismic_data, PointReceiver, RickerWavelet from pysit.gallery import horizontal_reflector # Define Domain bc = PML(0.3, 100, ftype='quadratic') # bc = Dirichlet() x_config = (0.1, 1.0, bc, bc) z_config = (0.1, 0.8, bc, bc) d = RectangularDomain( x_config, z_config ) m = CartesianMesh(d, 90, 70) # Generate true wave speed # (M = C^-2 - C0^-2) C0, C = horizontal_reflector(m) # Set up shots Nshots = 1 shots = [] xmin = d.x.lbound xmax = d.x.rbound nx = m.x.n zmin = d.z.lbound zmax = d.z.rbound point_approx = 'delta' for i in xrange(Nshots): # Define source location and type source = PointSource(m, (.188888, 0.18888), RickerWavelet(10.0), approximation=point_approx) # Define set of receivers zpos = zmin + (1./9.)*zmax xpos = np.linspace(xmin, xmax, nx) receivers = ReceiverSet(m, [PointReceiver(m, (x, zpos)) for x in xpos]) # Create and store the shot shot = Shot(source, receivers) shots.append(shot) # Define and configure the wave solver trange=(0.,3.0) solver = ConstantDensityAcousticWave(m, formulation='scalar', model_parameters={'C': C}, spatial_accuracy_order=4, trange=trange, time_accuracy_order=6) # Generate synthetic Seismic data print('Generating data...') base_model = solver.ModelParameters(m,{'C': C}) generate_seismic_data(shots, solver, base_model) solver_frequency = ConstantDensityHelmholtz(m, model_parameters={'C': C0}, spatial_shifted_differences=True, spatial_accuracy_order=4) tools = FrequencyModeling(solver_frequency) m0 = solver_frequency.ModelParameters(m,{'C': C0}) np.random.seed(0) m1 = m0.perturbation() # m1 += M m1 += np.random.rand(*m1.data.shape) freqs = [10.0, 10.5, 10.123334145252] # freqs = np.linspace(3,20,20) fwdret = tools.forward_model(shot, m0, freqs, ['wavefield', 'dWaveOp', 'simdata']) data = fwdret['simdata'] dWaveOp0 = fwdret['dWaveOp'] u0hat = fwdret['wavefield'][freqs[0]] # data -= shot.receivers.interpolate_data(solver.ts()) # data *= -1 # for nu in freqs: # data[nu] += np.random.rand(*data[nu].shape) linfwdret = tools.linear_forward_model(shot, m0, m1, freqs, ['simdata','wavefield1']) lindata = linfwdret['simdata'] u1hat = linfwdret['wavefield1'][freqs[0]] adjret = tools.adjoint_model(shot, m0, data, freqs, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0) qhat = adjret['adjointfield'][freqs[0]] adjmodel = adjret['imaging_condition'].data # adjret2 = tools.adjoint_model(shot, m0, lindata_time, freqs, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0) ## qhat = adjret['adjointfield'][freqs[0]] # adjmodel2 = adjret2['imaging_condition'].view(np.ndarray) m1 = m1.data temp_data_prod = 0.0 for nu in freqs: temp_data_prod += np.dot(lindata[nu].reshape(data[nu].shape).T, np.conj(data[nu])) print temp_data_prod.squeeze() print np.dot(m1.T, np.conj(adjmodel)).squeeze()*np.prod(m.deltas) print np.dot(m1.T, np.conj(adjmodel)).squeeze()*np.prod(m.deltas) - temp_data_prod.squeeze()
def adjoint_test_rho(): #if __name__ == '__main__': # from pysit import * import numpy as np import matplotlib.pyplot as plt from numpy.random import uniform from pysit import PML, Dirichlet, RectangularDomain, CartesianMesh, PointSource, ReceiverSet, Shot, ConstantDensityAcousticWave,VariableDensityHelmholtz, generate_seismic_data, PointReceiver, RickerWavelet from pysit.gallery.horizontal_reflector import horizontal_reflector # Define Domain bc = PML(0.3, 100, ftype='quadratic') # bc = Dirichlet() x_config = (0.1, 1.0, bc, bc) z_config = (0.1, 0.8, bc, bc) d = RectangularDomain( x_config, z_config ) m = CartesianMesh(d, 70, 90) # Generate true wave speed # (M = C^-2 - C0^-2) C,C0,m,d = horizontal_reflector(m) w=1.3 M = [w*C, C/w] M0 = [C0, C0] # Set up shots Nshots = 1 shots = [] xmin = d.x.lbound xmax = d.x.rbound nx = m.x.n zmin = d.z.lbound zmax = d.z.rbound point_approx = 'delta' for i in xrange(Nshots): # Define source location and type source = PointSource(m, (.188888, 0.18888), RickerWavelet(10.0), approximation=point_approx) # Define set of receivers zpos = zmin + (1./9.)*zmax xpos = np.linspace(xmin, xmax, nx) receivers = ReceiverSet(m, [PointReceiver(m, (x, zpos)) for x in xpos]) # Create and store the shot shot = Shot(source, receivers) shots.append(shot) # Define and configure the wave solver #trange=(0.,3.0) freqs = [3.0,5.0,7.0] solver = VariableDensityHelmholtz(m, model_parameters={'kappa': M[0], 'rho' : M[1]}, spatial_shifted_differences=False, spatial_accuracy_order=2) # Generate synthetic Seismic data print('Generating data...') base_model = solver.ModelParameters(m,{'kappa': M[0], 'rho' : M[1]}) generate_seismic_data(shots, solver, base_model,frequencies=freqs) tools = FrequencyModeling(solver) m0 = solver.ModelParameters(m,{'kappa': M[0], 'rho' : M[1]}) np.random.seed(0) m1 = m0.perturbation() # v is pertubation of model 1/rho. (which we have declared as m1). Thus, rho is 1/v. v = uniform(0.5,1.5,len(m0.rho)).reshape((len(m0.rho),1)) m1.rho += 1.0/v # freqs = np.linspace(3,20,20) fwdret = tools.forward_model(shot, m0, freqs, ['wavefield', 'dWaveOp', 'simdata']) data = fwdret['simdata'] dWaveOp0 = fwdret['dWaveOp'] u0hat = fwdret['wavefield'] # data -= shot.receivers.interpolate_data(solver.ts()) # data *= -1 # for nu in freqs: # data[nu] += np.random.rand(*data[nu].shape) linfwdret = tools.linear_forward_model_rho(shot, m0, m1, freqs, ['simdata','wavefield1'], wavefield=u0hat) lindata = linfwdret['simdata'] #u1hat = linfwdret['wavefield1'][freqs[0]] adjret = tools.adjoint_model(shot, m0, data, freqs, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0,wavefield=u0hat) qhat = adjret['adjointfield'][freqs[0]] adjmodel = adjret['imaging_condition'].rho # adjret2 = tools.adjoint_model(shot, m0, lindata_time, freqs, return_parameters=['imaging_condition', 'adjointfield'], dWaveOp=dWaveOp0) ## qhat = adjret['adjointfield'][freqs[0]] # adjmodel2 = adjret2['imaging_condition'].view(np.ndarray) temp_data_prod = 0.0 for nu in freqs: temp_data_prod += np.dot(lindata[nu].reshape(data[nu].shape).T, np.conj(data[nu])) print "data space: ", temp_data_prod.squeeze() print "model space: ", np.dot(v.T, np.conj(adjmodel)).squeeze()*np.prod(m.deltas) print "their diff: ", np.dot(v.T, np.conj(adjmodel)).squeeze()*np.prod(m.deltas) - temp_data_prod.squeeze()