Skip to content

Full-waveform inversion: recover a wave speed

FEM inverse_wave_speed_2d.py

A wave rings through a medium whose wave speed is unknown, and a receiver records the displacement over time, a seismogram. From that record alone, how fast is the medium? The gradient flows back through the whole time integration of the wave equation; this is full-waveform inversion, the tool of seismic imaging and elastography, in its smallest form.

An animation over 220 Adam iterations. Left: the membrane at t = 0.5 as observed (true c² = 2) and as computed with the speed recovered so far, in PuOr, a single negative bump. Right: the receiver's displacement history, the observed one dashed and the recovered one in orange, which starts at a too slow oscillation and overshoots slightly before settling onto the data.

Left: the wave at \(t=0.5\), observed and computed with the current estimate. Right: the receiver's record, observed (dashed) and computed.

The problem

\[u_{tt} = c^2\,\Delta u,\qquad u = 0 \text{ on } \partial\Omega,\qquad u(0)=\sin(\pi x)\sin(\pi y),\quad u_t(0)=0,\]
\[\min_{c^2}\ \lVert u(c^2) - u^\text{obs}\rVert^2,\]

with the data computed at \(c^2=2\) and the fit started at \(c^2=1\).

The jNO code

The speed enters the weak form as a parameter. With the true value passed in, the same problem produces the data:

c2 = jno.np.parameter((1,), name="c2")
fem = jno.fem(
    [
        ub.tt * vb + c2 * jno.np.inner(grad(u), grad(v)),
        u(*b) - 0.0,
        u(x0, y0) - jno.np.sin(π * x0) * jno.np.sin(π * y0),
        u.bind(x=x0, y=y0, t=t0).t - 0.0,
    ]
)
u_obs = np.asarray(fem.solve(c2=jnp.array([c2_true])))  # the seismogram: the wave at the true speed

fem.solve() marches the second-order form with the energy-conserving trapezoidal rule. Its trajectory is a node of the graph, so the misfit differentiates through every time step back to \(c^2\):

c2.initialize(jax.nn.initializers.constant(c2_guess)).optimizer(optax.adam(5e-2))
crux = jno.core([(fem.solve() - u_obs).mse])
for _ in range(110):
    crux.solve(2, log=False)

Result

A wrong speed rings at the wrong frequency, so the misfit has a clean gradient. Adam runs through the truth to 2.14, comes back to 1.95 and spirals in: after 220 iterations \(c^2=2.0000\) against the true 2.0.

Full script

The whole script, as it runs in the tests
"""Full-waveform inversion: the wave speed from a seismogram, through a second-order solve.

u_tt = c² Δu,   u = 0 on the boundary,   u(0) = sin(πx) sin(πy),   u_t(0) = 0
"""

import os
from pathlib import Path

import jax
import jax.numpy as jnp
import numpy as np
import optax

import jno

jax.config.update("jax_enable_x64", True)  # second-order soft modes need float64
run = jno.setup(__file__)
π = jno.np.pi
c2_true, c2_guess = 2.0, 1.0  # the speed² to recover, and a deliberately wrong start

d = jno.shape.rect(0, 0, 1, 1, size=0.05).domain(time=(0.0, 1.5, 60))
x, y, t = d.variable("interior", split=True)
b = d.variable("boundary", split=True)
x0, y0, t0 = d.variable("initial", split=True)
u = d.unknown()
v = u.test()
ub, vb = u.bind(x=x, y=y, t=t), v.bind(x=x, y=y, t=t)
grad = lambda w: jno.np.jacobian(w, [x, y])

c2 = jno.np.parameter((1,), name="c2")
fem = jno.fem(
    [
        ub.tt * vb + c2 * jno.np.inner(grad(u), grad(v)),
        u(*b) - 0.0,
        u(x0, y0) - jno.np.sin(π * x0) * jno.np.sin(π * y0),
        u.bind(x=x0, y=y0, t=t0).t - 0.0,
    ]
)
u_obs = np.asarray(fem.solve(c2=jnp.array([c2_true])))  # the seismogram: the wave at the true speed

c2.initialize(jax.nn.initializers.constant(c2_guess)).optimizer(optax.adam(5e-2))
crux = jno.core([(fem.solve() - u_obs).mse])
path = [c2_guess]
for _ in range(110):
    crux.solve(2, log=False)
    path.append(float(np.ravel(crux.eval([c2]))[0]))

rec = path[-1]
print(
    f"Recovered c² = {rec:.4f}  (truth {c2_true}),  started at {c2_guess}:  rel. error {abs(rec - c2_true) / c2_true:.2%}"
)
assert abs(rec - c2_true) / c2_true < 0.02