-
Notifications
You must be signed in to change notification settings - Fork 42
/
base.py
360 lines (288 loc) · 13.8 KB
/
base.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
###############################################################################
#
# SWIT: Seismic Waveform Inversion Toolbox
#
# by Haipeng Li at USTC, [email protected]
#
# June, 2021
#
# Base module
#
###############################################################################
import os
from multiprocessing import cpu_count
import numpy as np
class simulate(object):
''' simulate class defining all parameters for seismic wavefield modeling
'''
def __init__(self, model, source, receiver, system):
''' define all parameters here
'''
self.model = model
self.source = source
self.receiver = receiver
self.system = system
self.initialize() # initialize
def __check(self):
''' check all parameters are acceptable
'''
dx = self.model.dx
dt = self.model.dt
f0 = self.source.f0
vpmin = np.min(self.model.vp)
vpmax = np.max(self.model.vp)
dt_req = np.sqrt(3.0/8.0) * dx / vpmax
dx_req = vpmin / f0 / 10.
f0_req = vpmin / dx / 10.
# check model size
if np.shape(self.model.vp) != (self.model.nx, self.model.nz):
raise ValueError('Wrong dimensions of Vp, not consistant with nx = %d, nz = %d'%(self.model.nx, self.model.nz))
if np.shape(self.model.rho) != (self.model.nx, self.model.nz):
raise ValueError('Wrong dimensions of Rho, not consistant with nx = %d, nz = %d'%(self.model.nx, self.model.nz))
# check path format
if self.system.homepath[-1] != '/':
self.system.homepath += '/'
# Check the stable condition: 4-th order FD: dt <= sqrt(3/8) * dx / vmax
if dt_req <= dt:
raise ValueError('modeling stability: dt = %.4f ms > dt_required = %.4f ms: ' % (dt*1000, dt_req*1000))
# Check the dispersion condition: dx <= vmin/(10*f0)
if dx_req < dx:
print('Warning: modeling dispersion, dx = %.2f m > dx_required = %.2f m' %(dx, dx_req))
print('Warning: modeling dispersion, f0 = %.2f Hz > f0_required = %.2f Hz' %(f0, f0_req))
if (self.source.xz[:,0].min() < self.model.xx.min() or
self.source.xz[:,0].max() > self.model.xx.max() or
self.receiver.xz[:,0].min() < self.model.xx.min() or
self.receiver.xz[:,0].max() > self.model.xx.max()):
raise ValueError('source or receiver coordinates are out of range')
# Check the system and set the number of CPUs
self.system.mpiproc = min([self.system.mpiproc, self.source.n, cpu_count() // 2])
# Use one thread in calling scipy to do the filtering
os.environ["OMP_NUM_THREADS"] = "1" # export OMP_NUM_THREADS=1
def __builddir(self):
''' Build and clean the working folders
'''
homepath = self.system.homepath
# clean the previous data if exists
folder_list = [homepath+'data/syn',
homepath+'data/tempdata',
homepath+'parfile',
homepath+'outputs',
homepath+'figures',
]
for _, ifolder in enumerate(folder_list):
if os.path.exists(ifolder):
os.system('rm -r %s' % ifolder)
# creat the working folders
folder_list = ['-p ' + homepath, # Home folder
homepath+'data', # Data folder
homepath+'data/obs/', # Observed data
homepath+'data/syn/', # Synthetic data
homepath+'data/tempdata/', # Tempdata data
homepath+'parfile', # Parfile
homepath+'parfile/forward_parfile/', # Forward parfile
homepath+'parfile/forward_source/', # Forward source
homepath+'parfile/adjoint_parfile/', # Adjoint parfile
homepath+'parfile/adjoint_source/', # Adjoint source
homepath+'parfile/model/', # Model
homepath+'outputs', # Outputs
homepath+'outputs/velocity/', # Outputs: velocity
homepath+'outputs/gradient/', # Outputs: gradient
homepath+'outputs/direction/', # Outputs: direction
homepath+'outputs/LBFGS_memory/', # Outputs: LBFGS_memory
homepath+'figures', # Figures
homepath+'figures/model/', # Figures: gradient and velocity
homepath+'figures/waveform/', # Figures: waveform
]
for _, ifolder in enumerate(folder_list):
if not os.path.exists(ifolder):
os.system('mkdir %s' % ifolder)
def __screenprint(self):
''' Print information to screen
'''
# print the primary modeling parameters
print('*****************************************************')
print('\n Seismic Waveform Inversion Toolbox \n')
print('*****************************************************\n')
print('Forward modeling : nx, nz = %d, %d' %(self.model.nx, self.model.nz) )
print('Forward modeling : dx = %.1f m' %(self.model.dx))
print('Forward modeling : dt = %.2f ms, %d steps' %(self.model.dt * 1000, self.model.nt))
print('Forward modeling : %d shots run in mpi, %d CPU available'%(self.system.mpiproc, cpu_count() // 2))
def initialize(self):
''' Initialize the simulation
'''
self.__check() # Check simulation parameter
self.__builddir() # Build and clean working folder
self.__screenprint() # Print to screen
class model(object):
''' model parameters for wavefiled simulation (2D or 3D)
'''
def __init__(self, nx, nz, dx, dt, nt, fs, pml, vp, rho):
# basic model
self.nx = nx
self.nz = nz
self.dx = dx
self.dt = dt
self.nt = nt
self.fs = fs
self.pml = pml
self.nx_pml = self.nx + self.pml * 2
self.nz_pml = self.nz + self.pml * (2 - self.fs)
# coordinate points (x, z), and the time array
self.xx = np.arange(0, self.nx * self.dx, self.dx)
self.zz = np.arange(0, self.nz * self.dx, self.dx)
self.t = np.linspace(0, self.dt*self.nt, num=self.nt, endpoint=False)
# velocity, density, etc.
self.rho = rho # density in kg/m^3
self.vp = vp # p-wave velocity in m/s
self.vpmax = vp.max() # maximum vp
self.vpmin = vp.min() # mininum vp
# output
self.savesnap = 0
self.savestep = 1
# Notice:
# the forward modeling code always uses free surface, no matter self.fs = True or False
class source(object):
''' source parameters for forward wavefield simulation (2D or 3D)
'''
def __init__(self, f0, n, xz, wavelet):
self.f0 = f0
self.n = n
self.xz = xz
self.wavelet = wavelet
class receiver(object):
''' receiver parameters for forward wavefield simulation (2D or 3D)
'''
def __init__(self, n, xz):
self.n = n
self.xz = xz
class system(object):
''' system setting
'''
def __init__(self, homepath, mpiproc, figaspect=1):
self.homepath = homepath
self.mpiproc = mpiproc
self.figaspect = figaspect
# optimize class
class optimize(object):
''' FWI optimation parameters
'''
def __init__(self, misfit_type, scheme, maxiter, step_length, vpmax, vpmin, marine_or_land,
grad_mute, grad_smooth,
fre_filter, fre_low, fre_high,
mute_late_arrival, mute_late_window, normalize,
mute_offset_short, mute_offset_long,
mute_offset_short_dis, mute_offset_long_dis, grad_mask=None):
''' Define all parameters
'''
# basic inversion parameters
self.iter = 0
self.misfit_type = misfit_type
self.scheme = scheme
self.maxiter = maxiter
self.step_length = step_length
self.vpmax = vpmax
self.vpmin = vpmin
self.marine_or_land = marine_or_land
# gradient preconditioning
self.grad_mute = grad_mute
self.grad_smooth = grad_smooth
self.grad_mask = grad_mask
# data filter
self.fre_filter = fre_filter
self.fre_low = fre_low
self.fre_high = fre_high
# pick first break and mute later arrivals
self.mute_late_arrival = mute_late_arrival
self.mute_late_window = mute_late_window
# data normalization
self.normalize = normalize
# data offset mute
self.mute_offset_short = mute_offset_short
self.mute_offset_long = mute_offset_long
self.mute_offset_short_dis = mute_offset_short_dis # (units: m)
self.mute_offset_long_dis = mute_offset_long_dis # (units: m)
# set the taper for muting the gradient around the source
if self.marine_or_land.lower() in ['marine', 'offshore']:
self.grad_thred = 0.0
elif self.marine_or_land.lower() in ['land', 'onshore']:
self.grad_thred = 0.001
else:
raise ValueError('not supported modeling marine_or_land: %s'%(self.marine_or_land))
# initilize
self.initialize()
def __check(self):
''' check parameters.
'''
if self.scheme not in ['NLCG', 'LBFGS']:
raise ValueError('not supported inversion scheme: %s' % self.scheme)
if self.misfit_type not in ['Waveform', 'Envelope', 'Traveltime', 'Globalcorrelation', 'RTM']:
raise ValueError('not supported misfit function: %s' % self.misfit_type)
if self.misfit_type in ['RTM']:
self.maxiter = 1
if ('Max-Trace' not in self.normalize and
'L1-Event' not in self.normalize and
'L2-Event' not in self.normalize and
'L1-Trace' not in self.normalize and
'L2-Trace' not in self.normalize and
'None' not in self.normalize):
raise ValueError('not supported normalization:', self.normalize)
if self.fre_filter not in ['Lowpass', 'Bandpass', 'Highpass', 'None']:
raise ValueError('not supported frequency filter: %s' %self.fre_filter)
if self.vpmax < self.vpmin:
raise ValueError('vpmax=%f m/s is less than vpmin=%f m/s\n' %(self.vpmax, self.vpmin))
if self.fre_low > self.fre_high:
raise ValueError('fre_low > fre_high')
if self.mute_offset_short_dis > self.mute_offset_long_dis:
raise ValueError('mute_offset_short_dis > mute_offset_long_dis')
def __screenprint(self):
''' Print information to screen.
'''
# basic inversion parameter
print('Inversion scheme : %s' % self.scheme)
print('Inversion misfit : %s' % self.misfit_type)
print('Inversion maxiter: %d' % self.maxiter)
print('Inversion step : %.3f' % self.step_length)
print('Inversion vpmin : %.1f m/s' % self.vpmin)
print('Inversion vpmax : %.1f m/s' % self.vpmax)
print('Gradient mute : %d grids on top' % self.grad_mute)
print('Gradient smooth : %d grids Gaussian smooth' % self.grad_smooth)
# filtering
if self.fre_filter in ['None']:
print('Data processing : no filtering')
elif self.fre_filter in ['Bandpass']:
print('Data processing : %s, %.2f ~ %.2f Hz' %(self.fre_filter, self.fre_low, self.fre_high))
elif self.fre_filter in ['Lowpass']:
print('Data processing : %s, < %.2f Hz' %(self.fre_filter, self.fre_low))
elif self.fre_filter in ['Highpass']:
print('Data processing : %s, > %.2f Hz' %(self.fre_filter, self.fre_high))
# pick and mute
if self.mute_late_arrival:
if self.mute_late_window > 0:
print('Data processing : time window, %.2f s after the first break' % self.mute_late_window)
else:
print('Data processing : time window, %.2f s mute first arrivals for RTM' % abs(self.mute_late_window))
else:
print('Data processing : no time window')
# mute offset
if self.mute_offset_short:
print('Data processing : mute short offset, %.1f m' % self.mute_offset_short_dis)
else:
print('Data processing : mute short offset, none')
if self.mute_offset_long:
print('Data processing : mute long offset, %.1f m' % self.mute_offset_long_dis)
else:
print('Data processing : mute long offset, none')
# normalize
if self.normalize in ['None']:
print('Data processing : no normalization (keep AVO effect)')
else:
#print('Data processing : normalization, %s' % (', '.join(self.normalize)))
print('Data processing : normalization, %s' % (self.normalize))
print('Data processing : OMP Threads = %s' %os.environ["OMP_NUM_THREADS"])
print('\nsee more in json-parameter files under parfile folder\n')
print('*****************************************************\n')
def initialize(self):
''' Initialize the optimazation.
'''
self.__check() # Check simulation parameter
self.__screenprint()