Skip to main content

Comparing 1-D and N-D extrapolations with the 2nd-order acoustic wave equation

In this notebook, a ‘Mulder-style’ immersed boundary using a 1-D extrapolation per cartesian direction is compared with an immersed boundary approach using a single N-D extrapolation.

# Load the signed distance function for the boundary geometry
import pickle
import matplotlib.pyplot as plt

def read_sdf(surface, dims):
    """Unpickle an sdf"""
    fname = '../tests/sdfs/' + surface + '_' + str(dims) + 'd.dat'
    with open(fname, 'rb') as f:
        sdf = pickle.load(f)
    return sdf

# Load the sinusoid 2D sdf
sdf = read_sdf('hi_res_sinusoid', 2)

plt.imshow(sdf.data.T, origin='lower')
plt.title("Boundary geometry SDF")
plt.colorbar()
plt.show()

The same boundary geometry is used for both runs to compare the results of the two approaches.

from schism import BoundaryGeometry

bg = BoundaryGeometry(sdf)
grid = bg.grid  # Gets the grid from the signed distance function
Operator `normals` ran in 0.01 s

The same TimeFunction can be used for both runs, the only requirement is that the data attribute is reset between.

import devito as dv

s_o = 4
p = dv.TimeFunction(name='p', grid=grid, space_order=s_o, time_order=2)

As this notebook will use both 1-D and N-D extrapolations, separate boundary conditions are needed for each, with the former using appropriate approximations.

from schism import BoundaryConditions

# N-dimensional boundary conditions
bc_list_nd = [dv.Eq(p, 0),  # Zero pressure on free surface
              dv.Eq(p.dx2+p.dy2, 0),  # Zero laplacian
              dv.Eq(p.dx4 + 2*p.dx2dy2 + p.dy4, 0)]  # Zero biharmonic

# 1D boundary conditions
bc_list_1d = [dv.Eq(p, 0),  # Zero pressure on free surface
              dv.Eq(p.dx2, 0),
              dv.Eq(p.dy2, 0),  # Zero second derivative
              dv.Eq(p.dx4, 0),
              dv.Eq(p.dy4, 0)]  # Zero fourth derivative

bcs_nd = BoundaryConditions(bc_list_nd)
bcs_1d = BoundaryConditions(bc_list_1d)

First running the implementation with 1D extrapolations.

from schism import Boundary

boundary_1d = Boundary(bcs_1d, bg, has_1D_basis=True, strategy='reduce')

derivs = (p.dx2, p.dy2)
subs_1d = boundary_1d.substitutions(derivs)
Generating stencils for Derivative(p(t, x, y), (x, 2))
Generating stencils for Derivative(p(t, x, y), (y, 2))

Before running the models, a boundary mask to damp reflections at the edges of the domain is set up. As in previous examples, this will be a simple sponge mask.

import numpy as np

damp = dv.Function(name='damp', grid=grid)

# Number of boundary layers
nbl = 10
taper = np.linspace(0, 0.3, nbl)

# Add the taper to the function's data
damp.data[:10] += taper[::-1, np.newaxis]
damp.data[-10:] += taper[:, np.newaxis]
damp.data[:, :10] += taper[np.newaxis, ::-1]
damp.data[:, -10:] += taper[np.newaxis, :]

plt.imshow(damp.data.T, origin='lower')
plt.title("Damping mask")
plt.colorbar()
plt.show()

Setting up the source term, a Ricker source as before.

from examples.seismic import TimeAxis, RickerSource

c = 1

t0 = 0.  # Simulation starts a t=0
tn = 700.  # Simulation last 0.7 seconds (700 ms)
dt = 0.5*grid.spacing[0]/c  # Time step from grid spacing

time_range = TimeAxis(start=t0, stop=tn, step=dt)

f0 = 0.016  # Source peak frequency is 16Hz (0.016 kHz)
src = RickerSource(name='src', grid=grid, f0=f0,
                   npoint=1, time_range=time_range)

src.coordinates.data[0, 0] = 500.
src.coordinates.data[0, 1] = 600.

Snapshotting using a second TimeFunction and a ConditionalDimension will be used to sample the wavefield every 100ms.

snap_interval = 100  # Snapshot interval in ms
timestep_interval = round(snap_interval/dt)

time_subsampled = dv.ConditionalDimension('t_sub', parent=grid.time_dim,
                                          factor=timestep_interval)

psave = dv.TimeFunction(name='psave', grid=grid, time_order=2,
                        save=int(tn/snap_interval)+1, time_dim=time_subsampled)

eq_save = dv.Eq(psave, p)

Setting up the equation with the modified stencils:

eq = dv.Eq(p.forward, 2*p-p.backward+dt**2*c**2*(subs_1d[p.dx2]+subs_1d[p.dy2])+damp*p.dt)

Setting up a source injection term:

src_term = src.inject(field=p.forward, expr=c*src*dt**2)

Now the operator is created, remembering to include the snapshotting equation.

op = dv.Operator([eq] + [eq_save] + src_term)
op(time=time_range.num-1, dt=dt)
Operator `Kernel` ran in 0.31 s
PerformanceSummary([(PerfKey(name='section0', rank=None),
                     PerfEntry(time=0.21727, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section1', rank=None),
                     PerfEntry(time=0.000668, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section2', rank=None),
                     PerfEntry(time=0.08304100000000009, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])

The snapshots are saved for later plotting and the wavefield is reset for a run with N-D extrapolations.

# Save the data for later
wavefield_1d = psave.data.copy()

p.data[:] = 0

Generating new modified stencils using the N-D boundary conditions:

boundary_nd = Boundary(bcs_nd, bg)
subs_nd = boundary_nd.substitutions(derivs)
Generating stencils for Derivative(p(t, x, y), (x, 2))
Generating stencils for Derivative(p(t, x, y), (y, 2))

Now the equation is reformed using the updated stencils.

eq = dv.Eq(p.forward, 2*p-p.backward+dt**2*c**2*(subs_nd[p.dx2]+subs_nd[p.dy2])+damp*p.dt)

An operator is created and run.

op = dv.Operator([eq] + [eq_save] + src_term)
op(time=time_range.num-1, dt=dt)
Operator `Kernel` ran in 0.11 s
PerformanceSummary([(PerfKey(name='section0', rank=None),
                     PerfEntry(time=0.08620299999999999, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section1', rank=None),
                     PerfEntry(time=0.000525, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section2', rank=None),
                     PerfEntry(time=0.021623000000000017, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])
# Save the data for later
wavefield_nd = psave.data.copy()

p.data[:] = 0
# Set up vacuum layer for comparison
cv = dv.Function(name='cv', grid=grid)
cv.data[sdf.data > 0] = 1
eq = dv.Eq(p.forward, 2*p-p.backward+dt**2*cv**2*(p.dx2+p.dy2)+damp*p.dt)
op = dv.Operator([eq] + [eq_save] + src_term)
op(time=time_range.num-1, dt=dt)
Operator `Kernel` ran in 0.04 s
PerformanceSummary([(PerfKey(name='section0', rank=None),
                     PerfEntry(time=0.019966999999999988, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section1', rank=None),
                     PerfEntry(time=0.000428, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section2', rank=None),
                     PerfEntry(time=0.016510000000000007, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])

A side-by-side comparison of a vacuum layer approach, an immersed boundary based on 1D extrapolations (Mulder’s approach), and an immersed boundary based on N-dimensional extrapolations is shown at 600ms.

extent = (0, 1000, 0, 1000)
vmax_v = np.amax(np.abs(psave.data[6]))
vmax_1 = np.amax(np.abs(wavefield_1d[6]))
vmax_n = np.amax(np.abs(wavefield_nd[6]))

fig, ax = plt.subplots(1, 3, figsize=(15, 10), tight_layout=True, sharey=True)
ax[0].imshow(psave.data[6].T, origin='lower', extent=extent, cmap='seismic', vmax=vmax_v, vmin=-vmax_v)
ax[0].title.set_text("Vacuum layer")
ax[1].imshow(wavefield_1d[6].T, origin='lower', extent=extent, cmap='seismic', vmax=vmax_1, vmin=-vmax_1)
ax[1].title.set_text("Immersed boundary\n(axially-aligned extrapolations)")
ax[2].imshow(wavefield_nd[6].T, origin='lower', extent=extent, cmap='seismic', vmax=vmax_n, vmin=-vmax_n)
ax[2].title.set_text("Immersed boundary\n(N-dimensional extrapolations)")

for i in range(3):
    # Plot the surface
    ax[i].contour(np.linspace(0, grid.extent[0], grid.shape[0]),
                  np.linspace(0, grid.extent[1], grid.shape[1]), sdf.data.T, [0],
                  antialiased=True, linewidths=0.5)
    # Plot the source
    ax[i].plot(500, 600, 'r*', markersize=10)

plt.savefig("1d_nd_comparison")
plt.show()

assert np.isclose(np.linalg.norm(wavefield_1d), 91.436806)
assert np.isclose(np.linalg.norm(wavefield_nd), 91.413246)
assert np.isclose(np.linalg.norm(psave.data), 91.42825)
Back to top