forked from PyLops/pylops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoway.py
451 lines (378 loc) · 13.2 KB
/
twoway.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
__all__ = ["AcousticWave2D"]
from typing import Any, NewType, Tuple
import numpy as np
from pylops import LinearOperator
from pylops.utils import deps
from pylops.utils.decorators import reshaped
from pylops.utils.typing import DTypeLike, InputDimsLike, NDArray, SamplingLike
devito_message = deps.devito_import("the twoway module")
if devito_message is None:
from examples.seismic import AcquisitionGeometry, Model
from examples.seismic.acoustic import AcousticWaveSolver
from ._twoway import _CustomSource
else:
AcousticWaveSolver = Any
AcousticWaveSolverType = NewType("AcousticWaveSolver", AcousticWaveSolver)
class AcousticWave2D(LinearOperator):
"""Devito Acoustic propagator.
Parameters
----------
shape : :obj:`tuple` or :obj:`numpy.ndarray`
Model shape ``(nx, nz)``
origin : :obj:`tuple` or :obj:`numpy.ndarray`
Model origin ``(ox, oz)``
spacing : :obj:`tuple` or :obj:`numpy.ndarray`
Model spacing ``(dx, dz)``
vp : :obj:`numpy.ndarray`
Velocity model in m/s
src_x : :obj:`numpy.ndarray`
Source x-coordinates in m
src_z : :obj:`numpy.ndarray` or :obj:`float`
Source z-coordinates in m
rec_x : :obj:`numpy.ndarray`
Receiver x-coordinates in m
rec_z : :obj:`numpy.ndarray` or :obj:`float`
Receiver z-coordinates in m
t0 : :obj:`float`
Initial time in ms
tn : :obj:`int`
Final time in ms
src_type : :obj:`str`
Source type
space_order : :obj:`int`, optional
Spatial ordering of FD stencil
nbl : :obj:`int`, optional
Number ordering of samples in absorbing boundaries
f0 : :obj:`float`, optional
Source peak frequency (Hz)
checkpointing : :obj:`bool`, optional
Use checkpointing (``True``) or not (``False``). Note that
using checkpointing is needed when dealing with large models
but it will slow down computations
dtype : :obj:`str`, optional
Type of elements in input array.
name : :obj:`str`, optional
Name of operator (to be used by :func:`pylops.utils.describe.describe`)
Attributes
----------
shape : :obj:`tuple`
Operator shape
explicit : :obj:`bool`
Operator contains a matrix that can be solved explicitly (``True``) or
not (``False``)
"""
def __init__(
self,
shape: InputDimsLike,
origin: SamplingLike,
spacing: SamplingLike,
vp: NDArray,
src_x: NDArray,
src_z: NDArray,
rec_x: NDArray,
rec_z: NDArray,
t0: float,
tn: float,
src_type: str = "Ricker",
space_order: int = 6,
nbl: int = 20,
f0: float = 20.0,
checkpointing: bool = False,
dtype: DTypeLike = "float32",
name: str = "A",
) -> None:
if devito_message is not None:
raise NotImplementedError(devito_message)
# create model
self._create_model(shape, origin, spacing, vp, space_order, nbl)
self._create_geometry(src_x, src_z, rec_x, rec_z, t0, tn, src_type, f0=f0)
self.checkpointing = checkpointing
super().__init__(
dtype=np.dtype(dtype),
dims=vp.shape,
dimsd=(len(src_x), len(rec_x), self.geometry.nt),
explicit=False,
name=name,
)
@staticmethod
def _crop_model(m: NDArray, nbl: int) -> NDArray:
"""Remove absorbing boundaries from model"""
return m[nbl:-nbl, nbl:-nbl]
def _create_model(
self,
shape: InputDimsLike,
origin: SamplingLike,
spacing: SamplingLike,
vp: NDArray,
space_order: int = 6,
nbl: int = 20,
) -> None:
"""Create model
Parameters
----------
shape : :obj:`numpy.ndarray`
Model shape ``(nx, nz)``
origin : :obj:`numpy.ndarray`
Model origin ``(ox, oz)``
spacing : :obj:`numpy.ndarray`
Model spacing ``(dx, dz)``
vp : :obj:`numpy.ndarray`
Velocity model in m/s
space_order : :obj:`int`, optional
Spatial ordering of FD stencil
nbl : :obj:`int`, optional
Number ordering of samples in absorbing boundaries
"""
self.space_order = space_order
self.model = Model(
space_order=space_order,
vp=vp * 1e-3,
origin=origin,
shape=shape,
dtype=np.float32,
spacing=spacing,
nbl=nbl,
bcs="damp",
)
def _create_geometry(
self,
src_x: NDArray,
src_z: NDArray,
rec_x: NDArray,
rec_z: NDArray,
t0: float,
tn: float,
src_type: str,
f0: float = 20.0,
) -> None:
"""Create geometry and time axis
Parameters
----------
src_x : :obj:`numpy.ndarray`
Source x-coordinates in m
src_z : :obj:`numpy.ndarray` or :obj:`float`
Source z-coordinates in m
rec_x : :obj:`numpy.ndarray`
Receiver x-coordinates in m
rec_z : :obj:`numpy.ndarray` or :obj:`float`
Receiver z-coordinates in m
t0 : :obj:`float`
Initial time
tn : :obj:`int`
Final time in ms
src_type : :obj:`str`
Source type
f0 : :obj:`float`, optional
Source peak frequency (Hz)
"""
nsrc, nrec = len(src_x), len(rec_x)
src_coordinates = np.empty((nsrc, 2))
src_coordinates[:, 0] = src_x
src_coordinates[:, 1] = src_z
rec_coordinates = np.empty((nrec, 2))
rec_coordinates[:, 0] = rec_x
rec_coordinates[:, 1] = rec_z
self.geometry = AcquisitionGeometry(
self.model,
rec_coordinates,
src_coordinates,
t0,
tn,
src_type=src_type,
f0=None if f0 is None else f0 * 1e-3,
)
def updatesrc(self, wav):
"""Update source wavelet
This routines is used to allow users to pass a custom source
wavelet to replace the source wavelet generated when the
object is initialized
Parameters
----------
wav : :obj:`numpy.ndarray`
Wavelet
"""
wav_padded = np.pad(wav, (0, self.geometry.nt - len(wav)))
self.wav = _CustomSource(
name="src",
grid=self.model.grid,
wav=wav_padded,
time_range=self.geometry.time_axis,
)
def _srcillumination_oneshot(self, solver: AcousticWaveSolverType, isrc: int) -> Tuple[NDArray, NDArray]:
"""Source wavefield and illumination for one shot
Parameters
----------
solver : :obj:`AcousticWaveSolver`
Devito's solver object.
isrc : :obj:`int`
Index of source to model
Returns
-------
u0 : :obj:`np.ndarray`
Source wavefield
src_ill : :obj:`np.ndarray`
Source illumination
"""
# assign source location to source object with custom wavelet
if hasattr(self, "wav"):
self.wav.coordinates.data[0, :] = self.geometry.src_positions[isrc, :]
# source wavefield
u0 = solver.forward(
save=True, src=None if not hasattr(self, "wav") else self.wav
)[1]
# source illumination
src_ill = self._crop_model((u0.data**2).sum(axis=0), self.model.nbl)
return u0, src_ill
def srcillumination_allshots(self, savewav: bool = False) -> None:
"""Source wavefield and illumination for all shots
Parameters
----------
savewav : :obj:`bool`, optional
Save source wavefield (``True``) or not (``False``)
"""
# create geometry for single source
geometry = AcquisitionGeometry(
self.model,
self.geometry.rec_positions,
self.geometry.src_positions[0, :],
self.geometry.t0,
self.geometry.tn,
f0=self.geometry.f0,
src_type=self.geometry.src_type,
)
solver = AcousticWaveSolver(self.model, geometry, space_order=self.space_order)
nsrc = self.geometry.src_positions.shape[0]
if savewav:
self.src_wavefield = []
self.src_illumination = np.zeros(self.model.shape)
for isrc in range(nsrc):
solver.geometry.src_positions = self.geometry.src_positions[isrc, :]
src_wav, src_ill = self._srcillumination_oneshot(solver, isrc)
if savewav:
self.src_wavefield.append(src_wav)
self.src_illumination += src_ill
def _born_oneshot(self, solver: AcousticWaveSolverType, dm: NDArray) -> NDArray:
"""Born modelling for one shot
Parameters
----------
solver : :obj:`AcousticWaveSolver`
Devito's solver object.
dm : :obj:`np.ndarray`
Model perturbation
Returns
-------
d : :obj:`np.ndarray`
Data
"""
# set perturbation
dmext = np.zeros(self.model.grid.shape, dtype=np.float32)
dmext[self.model.nbl : -self.model.nbl, self.model.nbl : -self.model.nbl] = dm
# assign source location to source object with custom wavelet
if hasattr(self, "wav"):
self.wav.coordinates.data[0, :] = solver.geometry.src_positions[:]
d = solver.jacobian(dmext, src=None if not hasattr(self, "wav") else self.wav)[
0
]
d = d.resample(solver.geometry.dt).data[:][: solver.geometry.nt].T
return d
def _born_allshots(self, dm: NDArray) -> NDArray:
"""Born modelling for all shots
Parameters
-----------
dm : :obj:`np.ndarray`
Model perturbation
Returns
-------
dtot : :obj:`np.ndarray`
Data for all shots
"""
# create geometry for single source
geometry = AcquisitionGeometry(
self.model,
self.geometry.rec_positions,
self.geometry.src_positions[0, :],
self.geometry.t0,
self.geometry.tn,
f0=self.geometry.f0,
src_type=self.geometry.src_type,
)
# solve
solver = AcousticWaveSolver(self.model, geometry, space_order=self.space_order)
nsrc = self.geometry.src_positions.shape[0]
dtot = []
for isrc in range(nsrc):
solver.geometry.src_positions = self.geometry.src_positions[isrc, :]
d = self._born_oneshot(solver, dm)
dtot.append(d)
dtot = np.array(dtot).reshape(nsrc, d.shape[0], d.shape[1])
return dtot
def _bornadj_oneshot(self, solver: AcousticWaveSolverType, isrc, dobs):
"""Adjoint born modelling for one shot
Parameters
----------
solver : :obj:`AcousticWaveSolver`
Devito's solver object.
isrc : :obj:`float`
Index of source to model
dobs : :obj:`np.ndarray`
Observed data to inject
Returns
-------
model : :obj:`np.ndarray`
Model
"""
# create boundary data
recs = self.geometry.rec.copy()
recs.data[:] = dobs.T[:]
# assign source location to source object with custom wavelet
if hasattr(self, "wav"):
self.wav.coordinates.data[0, :] = self.geometry.src_positions[isrc, :]
# source wavefield
if hasattr(self, "src_wavefield"):
u0 = self.src_wavefield[isrc]
else:
u0 = solver.forward(
save=True, src=None if not hasattr(self, "wav") else self.wav
)[1]
# adjoint modelling (reverse wavefield plus imaging condition)
model = solver.jacobian_adjoint(
rec=recs, u=u0, checkpointing=self.checkpointing
)[0]
return model
def _bornadj_allshots(self, dobs: NDArray) -> NDArray:
"""Adjoint Born modelling for all shots
Parameters
----------
dobs : :obj:`np.ndarray`
Observed data to inject
Returns
-------
model : :obj:`np.ndarray`
Model
"""
# create geometry for single source
geometry = AcquisitionGeometry(
self.model,
self.geometry.rec_positions,
self.geometry.src_positions[0, :],
self.geometry.t0,
self.geometry.tn,
f0=self.geometry.f0,
src_type=self.geometry.src_type,
)
nsrc = self.geometry.src_positions.shape[0]
mtot = np.zeros(self.model.shape, dtype=np.float32)
solver = AcousticWaveSolver(self.model, geometry, space_order=self.space_order)
for isrc in range(nsrc):
solver.geometry.src_positions = self.geometry.src_positions[isrc, :]
m = self._bornadj_oneshot(solver, isrc, dobs[isrc])
mtot += self._crop_model(m.data, self.model.nbl)
return mtot
@reshaped
def _matvec(self, x: NDArray) -> NDArray:
y = self._born_allshots(x)
return y
@reshaped
def _rmatvec(self, x: NDArray) -> NDArray:
y = self._bornadj_allshots(x)
return y