Skip to main content

TTI sinusoid hill

This notebook demonstrates the application of immersed boundaries to a set of pseudo-acoustic wave equations featuring TTI. This is based on the Fletcher et al. 2009 TTI formulation.

# 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()

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
import devito as dv

s_o = 4
p = dv.TimeFunction(name='p', grid=grid, space_order=s_o, time_order=2)
q = dv.TimeFunction(name='q', grid=grid, space_order=s_o, time_order=2)
from schism import BoundaryConditions

bc_list = [dv.Eq(p, 0),
           dv.Eq(p.dx2+p.dy2, 0),
           dv.Eq(p.dx4 + 2*p.dx2dy2 + p.dy4, 0),
           dv.Eq(q, 0),
           dv.Eq(q.dx2+q.dy2, 0),
           dv.Eq(q.dx4 + 2*q.dx2dy2 + q.dy4, 0)]

bcs = BoundaryConditions(bc_list)
from schism import Boundary

boundary = Boundary(bcs, bg)
derivs = (p.dx2, p.dy2, p.dxdy, q.dx2, q.dy2, q.dxdy)
subs = boundary.substitutions(derivs)
Generating stencils for Derivative(p(t, x, y), (x, 2))
Derivative(p(t, x, y), (x, 2)) [(x, 2)]
Derivative(p(t, x, y), (y, 2)) [(y, 2)]
Derivative(p(t, x, y), (y, 4)) [(y, 4)]
Derivative(p(t, x, y), (x, 2), (y, 2)) [(x, 2), (y, 2)]
Derivative(p(t, x, y), (x, 4)) [(x, 4)]
Generating stencils for Derivative(p(t, x, y), (y, 2))
Derivative(p(t, x, y), (x, 2)) [(x, 2)]
Derivative(p(t, x, y), (y, 2)) [(y, 2)]
Derivative(p(t, x, y), (y, 4)) [(y, 4)]
Derivative(p(t, x, y), (x, 2), (y, 2)) [(x, 2), (y, 2)]
Derivative(p(t, x, y), (x, 4)) [(x, 4)]
Generating stencils for Derivative(p(t, x, y), x, y)
Derivative(p(t, x, y), (x, 2)) [(x, 2)]
Derivative(p(t, x, y), (y, 2)) [(y, 2)]
Derivative(p(t, x, y), (y, 4)) [(y, 4)]
Derivative(p(t, x, y), (x, 2), (y, 2)) [(x, 2), (y, 2)]
Derivative(p(t, x, y), (x, 4)) [(x, 4)]
Generating stencils for Derivative(q(t, x, y), (x, 2))
Derivative(q(t, x, y), (y, 4)) [(y, 4)]
Derivative(q(t, x, y), (x, 4)) [(x, 4)]
Derivative(q(t, x, y), (x, 2), (y, 2)) [(x, 2), (y, 2)]
Derivative(q(t, x, y), (y, 2)) [(y, 2)]
Derivative(q(t, x, y), (x, 2)) [(x, 2)]
Generating stencils for Derivative(q(t, x, y), (y, 2))
Derivative(q(t, x, y), (y, 4)) [(y, 4)]
Derivative(q(t, x, y), (x, 4)) [(x, 4)]
Derivative(q(t, x, y), (x, 2), (y, 2)) [(x, 2), (y, 2)]
Derivative(q(t, x, y), (y, 2)) [(y, 2)]
Derivative(q(t, x, y), (x, 2)) [(x, 2)]
Generating stencils for Derivative(q(t, x, y), x, y)
Derivative(q(t, x, y), (y, 4)) [(y, 4)]
Derivative(q(t, x, y), (x, 4)) [(x, 4)]
Derivative(q(t, x, y), (x, 2), (y, 2)) [(x, 2), (y, 2)]
Derivative(q(t, x, y), (y, 2)) [(y, 2)]
Derivative(q(t, x, y), (x, 2)) [(x, 2)]
from examples.seismic import TimeAxis, RickerSource
import numpy as np

v_py = dv.Function(name='v_py', grid=grid)
v_py.data[:] = 1.

t0 = 0.  # Simulation starts a t=0
tn = 550.  # Simulation last 0.6 seconds (600 ms)
dt = 0.5*np.amin(grid.spacing)/np.amax(v_py.data)  # Time step from grid spacing

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

f0 = 0.02  # 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.
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()

import sympy as sp

ep = dv.Function(name='epsilon', grid=grid)
de = dv.Function(name='delta', grid=grid)
th = dv.Function(name='theta', grid=grid)
th.data[:] = np.radians(45.)
de.data[:] = 0.1
ep.data[:] = 0.25
sig = 0.3  # 0.5  # Sigma parameter to control pseudoshear

v_pn = v_py*dv.sqrt(1+2*de)
v_px = v_py*dv.sqrt(1+2*ep)
v_sy = dv.sqrt(v_py**2*(ep-de)/sig)


# Define H1 and H2
def h1(f, th):
    """Return the H1 operator"""
    term1 = sp.sin(th)**2*f.dx2
    term2 = sp.cos(th)**2*f.dy2
    term3 = sp.sin(2*th)*f.dxdy
    return term1 + term2 + term3

def h2(f, th):
    """Return the H2 operator"""
    return f.dx2 + f.dy2 - h1(f, th)

# Define RHS expressions
rhs_p = v_px**2*h2(p, th) + v_py**2*h1(q, th) + v_sy**2*h1(p, th) - v_sy**2*h1(q, th)
rhs_q = v_pn**2*h2(p, th) + v_py**2*h1(q, th) - v_sy**2*h2(p, th) + v_sy**2*h2(q, th)

eq_p = dv.Eq(p.forward,
             2*p - p.backward
             + dt**2*rhs_p.subs(subs) + damp*p.dt)

eq_q = dv.Eq(q.forward,
             2*q - q.backward
             + dt**2*rhs_q.subs(subs) + damp*q.dt)
src_p = src.inject(field=p.forward, expr=src*dt**2)
src_q = src.inject(field=q.forward, expr=src*dt**2)
op = dv.Operator([eq_p, eq_q] + src_p + src_q)
op(time=time_range.num-1, dt=dt)
Operator `Kernel` ran in 0.18 s
PerformanceSummary([(PerfKey(name='section0', rank=None),
                     PerfEntry(time=0.000268, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section1', rank=None),
                     PerfEntry(time=0.15896100000000002, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section2', rank=None),
                     PerfEntry(time=0.013487, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])
plt.imshow(p.data[1].T, origin='lower', cmap='seismic')
plt.colorbar()
plt.show()

# Save the data for later
wavefield = p.data.copy()

p.data[:] = 0
q.data[:] = 0
v_py.data[sdf.data <= 0] = 0

eq_p = dv.Eq(p.forward,
             2*p - p.backward
             + dt**2*rhs_p + damp*p.dt)

eq_q = dv.Eq(q.forward,
             2*q - q.backward
             + dt**2*rhs_q + damp*q.dt)

op = dv.Operator([eq_p, eq_q] + src_p + src_q)
op(time=time_range.num-1, dt=dt)
Operator `Kernel` ran in 0.04 s
PerformanceSummary([(PerfKey(name='section0', rank=None),
                     PerfEntry(time=0.000156, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section1', rank=None),
                     PerfEntry(time=0.019294999999999986, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),
                    (PerfKey(name='section2', rank=None),
                     PerfEntry(time=0.013810999999999999, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])
extent = (0, 1000, 0, 1000)
vmax_v = np.amax(np.abs(p.data[-1]))
vmax_i = np.amax(np.abs(wavefield[-1]))

fig, ax = plt.subplots(1, 2, figsize=(10, 10), tight_layout=True, sharey=True)
ax[0].imshow(p.data[-1].T, origin='lower', extent=extent, cmap='seismic', vmax=vmax_v, vmin=-vmax_v)
ax[0].title.set_text("Vacuum layer")
ax[1].imshow(wavefield[-1].T, origin='lower', extent=extent, cmap='seismic', vmax=vmax_i, vmin=-vmax_i)
ax[1].title.set_text("Immersed boundary")

for i in range(2):
    # 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("tti_vacuum_comparison")
plt.show()

assert np.isclose(np.linalg.norm(p.data), 230.2496)
assert np.isclose(np.linalg.norm(q.data), 205.35196)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[16], line 1
----> 1 assert np.isclose(np.linalg.norm(p.data), 230.2496)
      2 assert np.isclose(np.linalg.norm(q.data), 205.35196)

AssertionError: 
Back to top