-
Notifications
You must be signed in to change notification settings - Fork 7
/
02_inversion_no_regularization.py
158 lines (133 loc) · 6.82 KB
/
02_inversion_no_regularization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import numpy as np
import torch
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use("agg")
from scipy import integrate
import sys
import os
sys.path.append("../../../../")
from ADFWI.propagator import *
from ADFWI.model import *
from ADFWI.view import *
from ADFWI.utils import *
from ADFWI.survey import *
from ADFWI.fwi import *
import warnings
warnings.filterwarnings("ignore")
if __name__ == "__main__":
project_path = "./data/"
if not os.path.exists(os.path.join(project_path,"model")):
os.makedirs(os.path.join(project_path,"model"))
if not os.path.exists(os.path.join(project_path,"waveform")):
os.makedirs(os.path.join(project_path,"waveform"))
if not os.path.exists(os.path.join(project_path,"survey")):
os.makedirs(os.path.join(project_path,"survey"))
if not os.path.exists(os.path.join(project_path,"inversion-no_regularization")):
os.makedirs(os.path.join(project_path,"inversion-no_regularization"))
#------------------------------------------------------
# Basic Parameters
#------------------------------------------------------
device = "cuda:0"
dtype = torch.float32
ox,oz = 0,0
nz,nx = 88,200
dx,dz = 40, 40
nt,dt = 1600, 0.003
nabc = 30
f0 = 5
free_surface = True
# Load the Marmousi model dataset from the specified directory.
marmousi_model = load_marmousi_model(in_dir="../../../datasets/marmousi2_source")
# Create coordinate arrays for x and z based on the grid size.
x = np.linspace(5000, 5000 + dx * nx, nx)
z = np.linspace(0, dz * nz, nz)
true_model = resample_marmousi_model(x, z, marmousi_model)
smooth_model = get_smooth_marmousi_model(true_model, gaussian_kernel=6)
# Initialize primary wave velocity (vp) and density (rho) for the model.
vp_init = smooth_model['vp'].T # Transpose to match dimensions
rho_init = np.power(vp_init, 0.25) * 310 # Calculate density based on vp
# Extract true model properties for comparison.
vp_true = true_model['vp'].T # Transpose for consistency
rho_true = np.power(vp_true, 0.25) * 310 # Calculate true density
model = AcousticModel(ox,oz,nx,nz,dx,dz,
vp_init,rho_init,
vp_bound=[vp_true.min(),vp_true.max()],
vp_grad=True,rho_grad=False,
free_surface=free_surface,
abc_type="PML",abc_jerjan_alpha=0.007,nabc=nabc,
auto_update_rho=True,
device=device,dtype=dtype)
model.save(os.path.join(project_path,"model/init_model.npz"))
print(model.__repr__())
model._plot_vp_rho(figsize=(12,5),wspace=0.15,cbar_pad_fraction=0.02,cbar_height=0.04,cmap='coolwarm',save_path=os.path.join(project_path,"model/init_vp_rho.png"),show=False)
#------------------------------------------------------
# Source And Receiver
#------------------------------------------------------
# source
src_z = np.array([1 for i in range(2, nx-1, 5)]) # Z-coordinates for sources
src_x = np.array([i for i in range(2, nx-1, 5)]) # X-coordinates for sources
src_t,src_v = wavelet(nt,dt,f0,amp0=1)
src_v = integrate.cumtrapz(src_v, axis=-1, initial=0) #Integrate
source = Source(nt=nt,dt=dt,f0=f0)
for i in range(len(src_x)):
source.add_source(src_x=src_x[i],src_z=src_z[i],src_wavelet=src_v,src_type="mt",src_mt=np.array([[1,0,0],[0,1,0],[0,0,1]]))
source.plot_wavelet(save_path=os.path.join(project_path,"survey/wavelets.png"),show=False)
# receiver
rcv_z = np.array([1 for i in range(0, nx, 1)]) # Z-coordinates for receivers
rcv_x = np.array([j for j in range(0, nx, 1)]) # X-coordinates for receivers
receiver = Receiver(nt=nt,dt=dt)
for i in range(len(rcv_x)):
receiver.add_receiver(rcv_x=rcv_x[i],rcv_z=rcv_z[i],rcv_type="pr")
# survey
survey = Survey(source=source,receiver=receiver)
print(survey.__repr__())
survey.plot(model.vp,cmap='coolwarm',save_path=os.path.join(project_path,"survey/observed_system_init.png"),show=False)
#------------------------------------------------------
# Waveform Propagator
#------------------------------------------------------
F = AcousticPropagator(model,survey,device=device)
damp = F.damp
plot_damp(damp,save_path=os.path.join(project_path,"model/boundary_condition_init.png"),show=False)
# load data
d_obs = SeismicData(survey)
d_obs.load(os.path.join(project_path,"waveform/obs_data.npz"))
print(d_obs.__repr__())
# optimizer
iteration = 500
optimizer = torch.optim.Adam(model.parameters(), lr=10)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,step_size=300,gamma=0.75,last_epoch=-1)
# Setup misfit function
from ADFWI.fwi.misfit import Misfit_global_correlation
loss_fn = Misfit_global_correlation(dt=1)
# gradient processor
grad_mask = np.ones_like(vp_init)
grad_mask[:12,:] = 0
gradient_processor = GradProcessor(grad_mask=grad_mask)
# Initialize the acoustic full waveform inversion (FWI) object.
fwi = AcousticFWI(propagator=F,
model=model,
optimizer=optimizer,
scheduler=scheduler,
loss_fn=loss_fn,
obs_data=d_obs,
gradient_processor=gradient_processor,
waveform_normalize=True,
cache_result=True,
save_fig_epoch=50,
save_fig_path=os.path.join(project_path, "inversion-no_regularization"))
fwi.forward(iteration=iteration,batch_size=None,checkpoint_segments=2)
iter_vp = fwi.iter_vp
iter_loss = fwi.iter_loss
np.savez(os.path.join(project_path,"inversion-no_regularization/iter_vp.npz"),data=np.array(iter_vp))
np.savez(os.path.join(project_path,"inversion-no_regularization/iter_loss.npz"),data=np.array(iter_loss))
#------------------------------------------------------
# Visualize the Inversion Results
#------------------------------------------------------
from ADFWI.view.inverted_loss_model import plot_misfit,plot_initial_and_inverted,animate_inversion_process
# misfit
plot_misfit(iter_loss = iter_loss, save_path=os.path.join(project_path,"inversion-no_regularization/misfit.png"),show=False)
# inverted results
plot_initial_and_inverted(vp_init=vp_init,iter_vp=iter_vp,save_path=os.path.join(project_path,"inversion-no_regularization/inverted_res.png"),show=False)
# inversion animation
animate_inversion_process(iter_vp=iter_vp,vmin=vp_true.min(),vmax=vp_true.max(),save_path=os.path.join(project_path, "inversion-no_regularization/inversion_process.gif"),fps=10)