-
Notifications
You must be signed in to change notification settings - Fork 230
/
Copy pathoperators.py
272 lines (226 loc) · 9.44 KB
/
operators.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
from devito import Eq, Operator, Function, TimeFunction, Inc, solve, sign
from devito.symbolics import retrieve_functions, INT
from examples.seismic import PointSource, Receiver
def freesurface(model, eq):
"""
Generate the stencil that mirrors the field as a free surface modeling for
the acoustic wave equation.
Parameters
----------
model : Model
Physical model.
eq : Eq
Time-stepping stencil (time update) to mirror at the freesurface.
"""
lhs, rhs = eq.evaluate.args
# Get vertical dimension and corresponding subdimension
zfs = model.grid.subdomains['fsdomain'].dimensions[-1]
z = zfs.parent
# Functions present in the stencil
funcs = retrieve_functions(rhs)
mapper = {}
# Antisymmetric mirror at negative indices
# TODO: Make a proper "mirror_indices" tool function
for f in funcs:
zind = f.indices[-1]
if (zind - z).as_coeff_Mul()[0] < 0:
s = sign(zind.subs({z: zfs, z.spacing: 1}))
mapper.update({f: s * f.subs({zind: INT(abs(zind))})})
return Eq(lhs, rhs.subs(mapper), subdomain=model.grid.subdomains['fsdomain'])
def laplacian(field, model, kernel):
"""
Spatial discretization for the isotropic acoustic wave equation. For a 4th
order in time formulation, the 4th order time derivative is replaced by a
double laplacian:
H = (laplacian + s**2/12 laplacian(1/m*laplacian))
Parameters
----------
field : TimeFunction
The computed solution.
model : Model
Physical model.
"""
if kernel not in ['OT2', 'OT4']:
raise ValueError("Unrecognized kernel")
s = model.grid.time_dim.spacing
biharmonic = field.biharmonic(1/model.m) if kernel == 'OT4' else 0
return field.laplace + s**2/12 * biharmonic
def iso_stencil(field, model, kernel, **kwargs):
"""
Stencil for the acoustic isotropic wave-equation:
u.dt2 - H + damp*u.dt = 0.
Parameters
----------
field : TimeFunction
The computed solution.
model : Model
Physical model.
kernel : str, optional
Type of discretization, 'OT2' or 'OT4'.
q : TimeFunction, Function or float
Full-space/time source of the wave-equation.
forward : bool, optional
Whether to propagate forward (True) or backward (False) in time.
"""
# Forward or backward
forward = kwargs.get('forward', True)
# Define time step to be updated
unext = field.forward if forward else field.backward
udt = field.dt if forward else field.dt.T
# Get the spacial FD
lap = laplacian(field, model, kernel)
# Get source
q = kwargs.get('q', 0)
# Define PDE and update rule
eq_time = solve(model.m * field.dt2 - lap - q + model.damp * udt, unext)
# Time-stepping stencil.
eqns = [Eq(unext, eq_time, subdomain=model.grid.subdomains['physdomain'])]
# Add free surface
if model.fs:
eqns.append(freesurface(model, Eq(unext, eq_time)))
return eqns
def ForwardOperator(model, geometry, space_order=4,
save=False, kernel='OT2', **kwargs):
"""
Construct a forward modelling operator in an acoustic medium.
Parameters
----------
model : Model
Object containing the physical parameters.
geometry : AcquisitionGeometry
Geometry object that contains the source (SparseTimeFunction) and
receivers (SparseTimeFunction) and their position.
space_order : int, optional
Space discretization order.
save : int or Buffer, optional
Saving flag, True saves all time steps. False saves three timesteps.
Defaults to False.
kernel : str, optional
Type of discretization, 'OT2' or 'OT4'.
"""
m = model.m
# Create symbols for forward wavefield, source and receivers
u = TimeFunction(name='u', grid=model.grid,
save=geometry.nt if save else None,
time_order=2, space_order=space_order)
src = PointSource(name='src', grid=geometry.grid, time_range=geometry.time_axis,
npoint=geometry.nsrc)
rec = Receiver(name='rec', grid=geometry.grid, time_range=geometry.time_axis,
npoint=geometry.nrec)
s = model.grid.stepping_dim.spacing
eqn = iso_stencil(u, model, kernel)
# Construct expression to inject source values
src_term = src.inject(field=u.forward, expr=src * s**2 / m)
# Create interpolation expression for receivers
rec_term = rec.interpolate(expr=u)
# Substitute spacing terms to reduce flops
return Operator(eqn + src_term + rec_term, subs=model.spacing_map,
name='Forward', **kwargs)
def AdjointOperator(model, geometry, space_order=4,
kernel='OT2', **kwargs):
"""
Construct an adjoint modelling operator in an acoustic media.
Parameters
----------
model : Model
Object containing the physical parameters.
geometry : AcquisitionGeometry
Geometry object that contains the source (SparseTimeFunction) and
receivers (SparseTimeFunction) and their position.
space_order : int, optional
Space discretization order.
kernel : str, optional
Type of discretization, 'OT2' or 'OT4'.
"""
m = model.m
v = TimeFunction(name='v', grid=model.grid, save=None,
time_order=2, space_order=space_order)
srca = PointSource(name='srca', grid=model.grid, time_range=geometry.time_axis,
npoint=geometry.nsrc)
rec = Receiver(name='rec', grid=model.grid, time_range=geometry.time_axis,
npoint=geometry.nrec)
s = model.grid.stepping_dim.spacing
eqn = iso_stencil(v, model, kernel, forward=False)
# Construct expression to inject receiver values
receivers = rec.inject(field=v.backward, expr=rec * s**2 / m)
# Create interpolation expression for the adjoint-source
source_a = srca.interpolate(expr=v)
# Substitute spacing terms to reduce flops
return Operator(eqn + receivers + source_a, subs=model.spacing_map,
name='Adjoint', **kwargs)
def GradientOperator(model, geometry, space_order=4, save=True,
kernel='OT2', **kwargs):
"""
Construct a gradient operator in an acoustic media.
Parameters
----------
model : Model
Object containing the physical parameters.
geometry : AcquisitionGeometry
Geometry object that contains the source (SparseTimeFunction) and
receivers (SparseTimeFunction) and their position.
space_order : int, optional
Space discretization order.
save : int or Buffer, optional
Option to store the entire (unrolled) wavefield.
kernel : str, optional
Type of discretization, centered or shifted.
"""
m = model.m
# Gradient symbol and wavefield symbols
grad = Function(name='grad', grid=model.grid)
u = TimeFunction(name='u', grid=model.grid, save=geometry.nt if save
else None, time_order=2, space_order=space_order)
v = TimeFunction(name='v', grid=model.grid, save=None,
time_order=2, space_order=space_order)
rec = Receiver(name='rec', grid=model.grid, time_range=geometry.time_axis,
npoint=geometry.nrec)
s = model.grid.stepping_dim.spacing
eqn = iso_stencil(v, model, kernel, forward=False)
if kernel == 'OT2':
gradient_update = Inc(grad, - u * v.dt2)
elif kernel == 'OT4':
gradient_update = Inc(grad, - u * v.dt2 - s**2 / 12.0 * u.biharmonic(m**(-2)) * v)
# Add expression for receiver injection
receivers = rec.inject(field=v.backward, expr=rec * s**2 / m)
# Substitute spacing terms to reduce flops
return Operator(eqn + receivers + [gradient_update], subs=model.spacing_map,
name='Gradient', **kwargs)
def BornOperator(model, geometry, space_order=4,
kernel='OT2', **kwargs):
"""
Construct an Linearized Born operator in an acoustic media.
Parameters
----------
model : Model
Object containing the physical parameters.
geometry : AcquisitionGeometry
Geometry object that contains the source (SparseTimeFunction) and
receivers (SparseTimeFunction) and their position.
space_order : int, optional
Space discretization order.
kernel : str, optional
Type of discretization, centered or shifted.
"""
m = model.m
# Create source and receiver symbols
src = Receiver(name='src', grid=model.grid, time_range=geometry.time_axis,
npoint=geometry.nsrc)
rec = Receiver(name='rec', grid=model.grid, time_range=geometry.time_axis,
npoint=geometry.nrec)
# Create wavefields and a dm field
u = TimeFunction(name="u", grid=model.grid, save=None,
time_order=2, space_order=space_order)
U = TimeFunction(name="U", grid=model.grid, save=None,
time_order=2, space_order=space_order)
dm = Function(name="dm", grid=model.grid, space_order=0)
s = model.grid.stepping_dim.spacing
eqn1 = iso_stencil(u, model, kernel)
eqn2 = iso_stencil(U, model, kernel, q=-dm*u.dt2)
# Add source term expression for u
source = src.inject(field=u.forward, expr=src * s**2 / m)
# Create receiver interpolation expression from U
receivers = rec.interpolate(expr=U)
# Substitute spacing terms to reduce flops
return Operator(eqn1 + source + eqn2 + receivers, subs=model.spacing_map,
name='Born', **kwargs)