Skip to content

Commit 24502d3

Browse files
committed
tests: Add elastic test for specialization
1 parent 7a2276f commit 24502d3

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

devito/ir/iet/visitors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,10 @@ def visit_Operator(self, o, **kwargs):
16041604
state.pop('soname', None)
16051605
state.pop('_soname', None) # Clear cached soname so it is recomputed
16061606

1607+
# Tag operator to indicate that it's specialized
1608+
name = state['name']
1609+
state['name'] = f"{name}Specialized"
1610+
16071611
newargs, newkwargs = o.__getnewargs_ex__()
16081612
newop = o.__class__(*newargs, **newkwargs)
16091613

tests/test_specialization.py

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from devito import (
88
ConditionalDimension, Dimension, Eq, Function, Grid, Operator, SparseTimeFunction,
9-
SubDomain, TimeFunction, solve, switchconfig
9+
SubDomain, TensorTimeFunction, TimeFunction, VectorTimeFunction, diag, div, grad,
10+
solve, switchconfig
1011
)
1112
from devito.ir.iet.visitors import Specializer
1213

@@ -262,7 +263,6 @@ def test_diffusion(self, specialize):
262263
('x_m', 'x_M', 'y_m', 'y_M')])
263264
@pytest.mark.parametrize('source', [False, True])
264265
def test_acoustic(self, specialize, source):
265-
# TODO: Add source injection switch
266266
grid = Grid(shape=(101, 101))
267267

268268
u = TimeFunction(name='u', grid=grid, space_order=4, time_order=2)
@@ -313,4 +313,63 @@ def gaussian_bump(x, y, x0=0, y0=0, sigma=1):
313313
else:
314314
assert np.isclose(np.linalg.norm(u.data), 10.793053)
315315

316-
# Elastic-like test (with and without source injection)
316+
@pytest.mark.parametrize('specialize',
317+
[('x_m',),
318+
('y_M',),
319+
('time_m',),
320+
('time_m', 'time_M'),
321+
('x_m', 'y_M'),
322+
('x_m', 'x_M', 'y_m', 'y_M'),
323+
('o_x', 'o_y', 'p_src_m', 'p_src_M')])
324+
def test_elastic(self, specialize):
325+
grid = Grid(shape=(101, 101))
326+
327+
tau = TensorTimeFunction(name='tau', grid=grid, space_order=4)
328+
v = VectorTimeFunction(name='v', grid=grid, space_order=4)
329+
330+
vp = 1.25
331+
vs = 0.75
332+
density = 1.
333+
334+
dt = 0.6*min(grid.spacing)/vp
335+
336+
mu = vs**2 * density
337+
l = (vp**2 * density - 2*mu)
338+
b = 1/density
339+
340+
pde_v = v.dt - b * div(tau)
341+
pde_tau = tau.dt - l * diag(div(v.forward)) \
342+
- mu * (grad(v.forward) + grad(v.forward).transpose(inner=False))
343+
344+
u_v = Eq(v.forward, solve(pde_v, v.forward))
345+
u_t = Eq(tau.forward, solve(pde_tau, tau.forward))
346+
347+
src = SparseTimeFunction(name='src', grid=grid, nt=51, npoint=1)
348+
src.coordinates.data[:] = np.array(grid.extent)/2
349+
src.data[:, 0] = np.sin(np.linspace(0, 2*np.pi, 51))
350+
351+
src_xx = src.inject(field=tau[0, 0].forward, expr=src)
352+
src_yy = src.inject(field=tau[1, 1].forward, expr=src)
353+
354+
op = Operator([u_v] + [u_t] + src_xx + src_yy)
355+
356+
op.apply(t_M=50, dt=dt)
357+
358+
# Save and reset result
359+
fields = (tau[0, 0], tau[0, 1], tau[1, 1], v[0], v[1])
360+
361+
checks = {field.name: np.array(field.data) for field in fields}
362+
363+
for field in fields:
364+
field.data[:] = 0
365+
366+
op.apply_specialize(t_M=50, dt=dt, specialize=specialize)
367+
368+
for field in fields:
369+
check = checks[field.name]
370+
assert np.all(np.isclose(check, field.data))
371+
372+
norms = (1.333946, 0.4931774, 1.333946, 1.1619346, 1.1619346)
373+
374+
for field, norm in zip(fields, norms, strict=True):
375+
assert np.isclose(np.linalg.norm(field.data), norm)

0 commit comments

Comments
 (0)