Skip to content

Recover a source through a finite-difference solve

FDM inverse_source_fdm.py

A heat source of known shape but unknown strength warms a plate held cold on its edges. From the measured steady temperature, how strong is the source? The finite-difference solve is differentiable, so it goes straight into the loss and the source strength is fitted by gradient descent.

An animation of the first 30 gradient steps. Left: the true source, a single smooth bump peaking at 19.7. Right: the recovered source s f on the same colour scale, starting at 2.5 times the truth (peak near 49) and shrinking onto the true source within about twenty steps.

Left: the true source. Right: the estimate \(s_k f\) at step \(k\), on the same scale.

The problem

\[-\Delta u = s\,f,\qquad u=0\ \text{on}\ \partial\Omega,\qquad f = 2\pi^2\sin\pi x\sin\pi y,\]
\[\min_s\ \lVert u(s) - u^\text{obs}\rVert^2,\]

with the data computed at the true \(s=1\) and the fit started at \(s=2.5\).

The jNO code

The forward problem is a function of the amplitude. Called with a number it returns the solution; called with a parameter it returns a node of the graph that jno.core re-runs at every step:

u = d.unknown()
ui = u.bind(x=x, y=y)
f = 2 * π**2 * jno.np.sin(π * x) * jno.np.sin(π * y)
forward = lambda s: jno.fdm([-ui.xx - ui.yy - s * f, u(xb, yb) - 0.0]).solve()

observed = np.asarray(forward(1.0)).reshape(-1)  # the data: the solve at the true amplitude

The gradient flows through the solve by implicit differentiation, the same mechanism fem.solve() uses:

s = jno.np.parameter((1,), name="s").initialize(jax.nn.initializers.constant(2.5)).optimizer(optax.sgd(1.0))
crux = jno.core([(forward(s) - observed).mse])  # forward(s) is a trace node; the domain is inferred
for _ in range(150):
    crux.solve(1, log=False)

Result

On a mesh of size 0.025 (1931 nodes):

step \(\lvert s-1\rvert\)
1 0.78
10 \(2.2\times10^{-3}\)
15 \(8\times10^{-5}\)
150 \(10^{-12}\)

Full script

The whole script, as it runs in the tests
"""Recover a source amplitude through a finite-difference solve:  -Δu = s f,  u = 0 on the boundary."""

import os
from pathlib import Path

import jax
import numpy as np
import optax

import jno

jax.config.update("jax_enable_x64", True)
run = jno.setup(__file__)
π = jno.np.pi

d = jno.shape.rect(0, 0, 1, 1, size=0.025).domain()
x, y, _ = d.variable("interior", split=True)
xb, yb, _ = d.variable("boundary", split=True)
u = d.unknown()
ui = u.bind(x=x, y=y)
f = 2 * π**2 * jno.np.sin(π * x) * jno.np.sin(π * y)
forward = lambda s: jno.fdm([-ui.xx - ui.yy - s * f, u(xb, yb) - 0.0]).solve()

observed = np.asarray(forward(1.0)).reshape(-1)  # the data: the solve at the true amplitude

s = jno.np.parameter((1,), name="s").initialize(jax.nn.initializers.constant(2.5)).optimizer(optax.sgd(1.0))
crux = jno.core([(forward(s) - observed).mse])  # forward(s) is a trace node; the domain is inferred
path = [2.5]
for _ in range(150):
    crux.solve(1, log=False)
    path.append(float(np.ravel(crux.eval([s]))[0]))

rec = path[-1]
print(f"Recovered source amplitude s = {rec:.4f}  (true 1.0)")
assert abs(rec - 1.0) < 1e-2