Skip to content

Commit 7a2276f

Browse files
committed
tests: Add test for acoustic wave equation
1 parent 14738d7 commit 7a2276f

1 file changed

Lines changed: 63 additions & 5 deletions

File tree

tests/test_specialization.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import sympy
66

77
from devito import (
8-
ConditionalDimension, Dimension, Eq, Function, Grid, Operator, SubDomain,
9-
TimeFunction, switchconfig
8+
ConditionalDimension, Dimension, Eq, Function, Grid, Operator, SparseTimeFunction,
9+
SubDomain, TimeFunction, solve, switchconfig
1010
)
1111
from devito.ir.iet.visitors import Specializer
1212

@@ -233,7 +233,7 @@ def test_basic_mpi(self, caplog, mode, override):
233233
('time_m', 'time_M'),
234234
('x_m', 'y_M'),
235235
('x_m', 'x_M', 'y_m', 'y_M')])
236-
def test_diffusion_like(self, specialize):
236+
def test_diffusion(self, specialize):
237237
grid = Grid(shape=(11, 11))
238238

239239
dt = 2.5e-5
@@ -253,6 +253,64 @@ def test_diffusion_like(self, specialize):
253253

254254
assert np.all(np.isclose(check, f.data))
255255

256-
# Diffusion-like test
257-
# Acoustic-like test (with and without source injection)
256+
@pytest.mark.parametrize('specialize',
257+
[('x_m',),
258+
('y_M',),
259+
('time_m',),
260+
('time_m', 'time_M'),
261+
('x_m', 'y_M'),
262+
('x_m', 'x_M', 'y_m', 'y_M')])
263+
@pytest.mark.parametrize('source', [False, True])
264+
def test_acoustic(self, specialize, source):
265+
# TODO: Add source injection switch
266+
grid = Grid(shape=(101, 101))
267+
268+
u = TimeFunction(name='u', grid=grid, space_order=4, time_order=2)
269+
270+
def gaussian(x, x0=0, sigma=1):
271+
return np.exp(-(x - x0)**2 / (2 * sigma**2))
272+
273+
# Initialise with Gaussian bump
274+
def gaussian_bump(x, y, x0=0, y0=0, sigma=1):
275+
return gaussian(x, x0=x0, sigma=sigma)*gaussian(y, x0=y0, sigma=sigma)
276+
277+
x = np.linspace(-grid.extent[0]/2, grid.extent[0]/2, grid.shape[0])
278+
y = np.linspace(-grid.extent[1]/2, grid.extent[1]/2, grid.shape[0])
279+
xx, yy = np.meshgrid(x, y, indexing='ij')
280+
281+
u.data[:] = gaussian_bump(xx, yy, sigma=0.05)
282+
283+
c = 1.
284+
m = 1/c**2 # Square slowness
285+
286+
dt = 0.6*min(grid.spacing)/c
287+
288+
pde = m * u.dt2 - u.laplace
289+
stencil = Eq(u.forward, solve(pde, u.forward))
290+
291+
if source:
292+
src = SparseTimeFunction(name='src', grid=grid, nt=51, npoint=1)
293+
src.coordinates.data[:] = np.array(grid.extent)/2
294+
src.data[:, 0] = np.sin(np.linspace(0, 2*np.pi, 51))
295+
296+
src_term = src.inject(field=u.forward, expr=src)
297+
op = Operator([stencil] + src_term)
298+
else:
299+
op = Operator(stencil)
300+
301+
op.apply(t_M=50, dt=dt)
302+
303+
# Save and reset result
304+
check = np.array(u.data)
305+
u.data[:] = gaussian_bump(xx, yy, sigma=0.05)
306+
307+
op.apply_specialize(t_M=50, dt=dt, specialize=specialize)
308+
309+
assert np.all(np.isclose(check, u.data))
310+
311+
if source:
312+
assert np.isclose(np.linalg.norm(u.data), 21.698477)
313+
else:
314+
assert np.isclose(np.linalg.norm(u.data), 10.793053)
315+
258316
# Elastic-like test (with and without source injection)

0 commit comments

Comments
 (0)