def backward(ctx, grad_output): grad_output = grad_output.to('cpu') grad_output = grad_output.detach().numpy() # Adjoint linearized modeling u0 = forward_modeling(ctx.model, ctx.src.coordinates.data, ctx.src.data, ctx.rec.coordinates.data, save=True)[1] g = adjoint_born(ctx.model, ctx.rec.coordinates.data, grad_output.data[:], u=u0) # Remove padding nb = ctx.model.nbpml g = torch.from_numpy(g[nb:-nb, nb:-nb]).to(ctx.device) return g.view(1, 1, g.shape[0], g.shape[1]), None, None, None, None
# Wavefields in memory t1 = time.time() opF, u0 = forward_modeling(model, geometry.src_positions, geometry.src.data, geometry.rec_positions, save=True, u_return=True, op_return=True, tsub_factor=12, space_order=space_order) g, summary1, summary2 = adjoint_born(model, geometry.rec_positions, dpred.data[:], u=u0, is_residual=True, op_forward=opF, tsub_factor=12, space_order=space_order) t2 = time.time() print("Save in memory. Time [s]: ", t2 - t1) # Remove pml g = g[model.nbpml:-model.nbpml, model.nbpml:-model.nbpml] # remove padding g = np.reshape(g, -1, order='F') # Chunk up gradient and write to bucket. Add gradients to SQS queue chunk_size = get_chunk_size(len(g), num_chunks) idx_count = 0 for j in range(num_chunks):
ufi, isic=True, dt=dt, factor=8) t4 = time.time() print('Adjoint: ', t4 - t3) print('Total: ', (t2 - t1) + (t4 - t3)) timings[j] = (t2 - t1) + (t4 - t3) timings.dump('timing_sigsbee_frequencies.dat') # Checkpointing d0, _ = forward_modeling(model0, src.coordinates.data, src.data, rec_t.coordinates.data) ta = time.time() op_predicted = forward_modeling(model0, src.coordinates.data, src.data, rec_t.coordinates.data, op_return=True, dt=dt) f1, g1 = adjoint_born(model0, rec_t.coordinates.data, d0.data, op_forward=op_predicted, dt=dt) tb = time.time() print('Optimal checkpointing: ', tb - ta) timings_oc = np.array(tb - ta) timings_oc.dump('timing_sigsbee_optimal_checkpointing.dat')
rec_t.coordinates.data, isic=True, dt=dt) t2 = time.time() print(t2 - t1) # Adjoint print("Adjoint J") d0, u0 = forward_modeling(model0, src.coordinates.data, src.data, rec_t.coordinates.data, dt=dt, save=True) t1 = time.time() dm = adjoint_born(model0, rec_t.coordinates.data, dD_hat.data, u0, isic=True, dt=dt) t2 = time.time() print(t2 - t1) # Adjoint test a = np.dot(dD_hat.flatten(), dD.flatten()) b = np.dot(dm_hat.flatten(), dm.flatten()) print("Adjoint test J") print("Difference: ", a - b) print("Relative error: ", a / b - 1)
# Receiver for predicted data rec = Receiver(name='rec', grid=model0.grid, npoint=101, ntime=nt) rec.coordinates.data[:, 0] = np.linspace(0, model0.domain_size[0], num=101) rec.coordinates.data[:, 1] = 20. # Save wavefields dpred_data, u0 = forward_modeling(model0, src.coordinates.data, src.data, rec.coordinates.data, save=True, dt=dt) g1 = adjoint_born(model0, rec.coordinates.data, dpred_data[:] - dobs.data[:], u=u0, dt=dt) # Checkpointing op_predicted = forward_modeling(model0, src.coordinates.data, src.data, rec.coordinates.data, op_return=True, dt=dt) f2, g2 = adjoint_born(model0, rec.coordinates.data, dobs.data, op_forward=op_predicted, dt=dt)
src_type='Ricker', f0=f0) geometry0 = AcquisitionGeometry(model0, rec_coordinates, src_coordinates, t0=0.0, tn=tn, src_type='Ricker', f0=f0) # Nonlinear modeling dobs = forward_modeling(model, geometry, save=False, op_return=False)[0] dpred = forward_modeling(model0, geometry0, save=False, op_return=False)[0] dpred.data[:] = dpred.data[:] - dobs.data[:] # residual #qad = adjoint_modeling(model, geometry) # Linearized modeling #dlin = forward_born(model, geometry, isic=False) # Gradient opF, u0 = forward_modeling(model0, geometry0, save=True, u_return=True, op_return=True, tsub_factor=2) g = adjoint_born(model0, dpred, u=u0, op_forward=opF, tsub_factor=2) plt.imshow(np.transpose(g.data), vmin=-2e-1, vmax=2e-1) plt.show()
# Wavefields in memory t1 = time.time() checkpointing = True if checkpointing is False: opF, u0 = forward_modeling(model, geometry.src_positions, geometry.src.data, geometry.rec_positions, save=True, u_return=True, op_return=True, tsub_factor=12) g = adjoint_born(model, geometry.rec_positions, dpred.data[:], u=u0, is_residual=True, op_forward=opF, tsub_factor=12)[0] else: opF, u0 = forward_modeling(model, geometry.src_positions, geometry.src.data, geometry.rec_positions, save=False, u_return=True, op_return=True) g = adjoint_born(model, geometry.rec_positions, dpred.data[:], u=u0,
rec.coordinates.data[:, 0] = np.linspace(0, model0.domain_size[0], num=101) rec.coordinates.data[:, 1] = 20. dpred, u0 = forward_modeling(model0, src.coordinates.data, src.data, rec.coordinates.data, save=True, dt=dt, tsub_factor=2) #g1 = adjoint_born(model0, rec.coordinates.data, disic, u=u0, dt=dt, isic=False) # plt.imshow(np.transpose(g), vmin=-1e1, vmax=1e1); plt.show() g1 = adjoint_born(model0, rec.coordinates.data, dpred - dobs, u=u0, dt=dt, isic=False) op_predicted = forward_modeling(model0, src.coordinates.data, src.data, rec.coordinates.data, op_return=True, dt=dt) f1, g2 = adjoint_born(model0, rec.coordinates.data, dobs, op_forward=op_predicted, dt=dt, is_residual=False)