Skip to main content

1st-order acoustic sinusoid hill

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

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 diagonal 2D sdf
sdf = read_sdf('sinusoid_new_hi_res', 2)

# Load the staggered sdfs
sdf_x = read_sdf('sinusoid_new_hi_res_x', 2)
sdf_y = read_sdf('sinusoid_new_hi_res_y', 2)

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

# It is a good idea to make sure your signed distance functions are aligned
# Misaligned SDFs can result in considerable weirdness
plt.imshow(sdf_x.data.T-sdf.data.T, origin='lower')
plt.title("Alignment check x")
plt.colorbar()
plt.show()

plt.imshow(sdf_y.data.T-sdf.data.T, origin='lower')
plt.title("Alignment check y")
plt.colorbar()
plt.show()

from schism import BoundaryGeometry
import sympy as sp

x, y = sdf.grid.dimensions
h_x = x.spacing
h_y = y.spacing
zero = sp.core.numbers.Zero()
# Set cutoff for the velocity subgrids to zero
# This means that any velocity point on the interior can be updated
# This prevents non-physical ripple effects caused by over-extended extrapolations
# By default, this cutoff is 0.5 (half a grid spacing)
cutoff = {(h_x/2, zero): 0., (zero, h_y/2): 0.}

bg = BoundaryGeometry((sdf, sdf_x, sdf_y), cutoff=cutoff)
grid = bg.grid  # Gets the grid from the signed distance function
Operator `normals` ran in 0.01 s
import devito as dv

s_o = 4
p = dv.TimeFunction(name='p', grid=grid, space_order=s_o,
                    time_order=1, staggered=dv.NODE)
v = dv.VectorTimeFunction(name='v', grid=grid, space_order=s_o,
                          time_order=1)
from schism import BoundaryConditions

bc_list = [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
           dv.Eq(v[0].dx + v[1].dy, 0),  # Divergence of velocity equals zero
           dv.Eq(v[0].dx3 + v[1].dx2dy
                 + v[0].dxdy2 + v[1].dy3, 0)]  # Laplacian of divergence is zero

bcs = BoundaryConditions(bc_list)
from schism import Boundary

boundary = Boundary(bcs, bg)
x, y = grid.dimensions

pdx = p.dx(x0=x+x.spacing/2)
pdy = p.dy(x0=y+y.spacing/2)
vxdx = v[0].forward.dx(x0=x)
vydy = v[1].forward.dy(x0=y)

derivs = (pdx, pdy, vxdx, vydy)

subs = boundary.substitutions(derivs)
Generating stencils for Derivative(p(t, x, y), x)
Generating stencils for Derivative(p(t, x, y), y)
Generating stencils for Derivative(v_x(t + dt, x + h_x/2, y), x)
Generating stencils for Derivative(v_y(t + dt, x, y + h_y/2), y)
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()

from examples.seismic import TimeAxis, RickerSource

c = 1

t0 = 0.  # Simulation starts a t=0
tn = 600.  # Simulation last 0.55 seconds (550 ms)
dt = 0.5*5/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] = 500.
rho = 1
# Pressure update
eq_p = dv.Eq(p.forward, (1-damp)*(p + dt*rho*c**2*(subs[vxdx] + subs[vydy])))
# Velocity updates
eq_vx = dv.Eq(v[0].forward, v[0] + dt*subs[pdx]/rho)
eq_vy = dv.Eq(v[1].forward, v[1] + dt*subs[pdy]/rho)
src_term = src.inject(field=p.forward, expr=c*src*dt**2)
op = dv.Operator([eq_vx, eq_vy, eq_p] + src_term)
op(time=time_range.num-1, dt=dt)
Operator `Kernel` ran in 5.55 s
PerformanceSummary([(PerfKey(name='section0', rank=None),
                     PerfEntry(time=5.5486270000000015, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section1', rank=None),
                     PerfEntry(time=0.00019799999999999955, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])
plt.figure(figsize=(10, 10))
plt.imshow(p.data[1].T, origin='lower', cmap='seismic')
plt.colorbar()
plt.show()

plt.figure(figsize=(10, 10))
plt.imshow(v[0].data[1].T, origin='lower', cmap='seismic')
plt.colorbar()
plt.show()

plt.figure(figsize=(10, 10))
plt.imshow(v[1].data[1].T, origin='lower', cmap='seismic')
plt.colorbar()
plt.show()

assert np.isclose(np.linalg.norm(p.data), 10.720868)
assert np.isclose(np.linalg.norm(v[0].data), 5.3727317)
assert np.isclose(np.linalg.norm(v[1].data), 9.329413)
Back to top