Skip to content

DeepONet on a parametric Poisson family

operator deeponet_poisson_2d.py

One network that solves a whole family of problems: a DeepONet learns the map from a coefficient \(k\) to the solution field, for every \(k\) in a range at once. It never sees a solution, only the physics.

Top row: the DeepONet's own output for three held-out coefficients k = 0.7, 1.234 and 1.4 while it trains, epoch 0 to 2000. Bottom row: the finite-element reference for the same k. One colour scale for all six panels.

Top: the network's output for three coefficients it was never trained on. Bottom: the finite-element reference.

The problem

\[k\,\Delta u + 1 = 0\quad\text{on}\ [0,2]\times[0,1],\qquad u=0\ \text{on}\ \partial\Omega,\qquad k\sim U(0.5,\ 1.5).\]

The reference for any \(k\) is \(u=w/k\) with \(-\Delta w=1\), one finite-element solve.

The jNO code

Multiplying a domain by an integer replicates it, one copy per sample, and a tensor variable attaches one \(k\) to each copy:

d = N_SAMPLES * jno.shape.rect(0, 0, 2, 1, size=0.05).domain()  # one mesh, replicated per sample
x, y, _ = d.variable("interior")
k = d.variable("k", jax.random.uniform(KEY, (N_SAMPLES, 1, 1), minval=0.5, maxval=1.5))  # one k per sample

The branch net reads \(k\), the trunk net reads \((x,y)\). The factor \(x(2-x)\,y(1-y)\) makes the boundary condition exact for every sample, so the residual is the only loss:

net = jno.nn(
    foundax.deeponet(n_sensors=1, coord_dim=2, basis_functions=32, hidden_dim=128, activation=jax.numpy.tanh, key=KEY)
)
net.optimizer(optax.adam(optax.cosine_decay_schedule(1e-3, EPOCHS, alpha=1e-2)))
u = (net(k, jno.np.concat([x, y], axis=-1)) * x * (2 - x) * y * (1 - y)).scalar.bind(x=x, y=y)  # u = 0 on the boundary
crux = jno.core([(k * (u.xx + u.yy) + 1.0).mse])
crux.solve(epochs=EPOCHS, batchsize=32)

batchsize=32 takes 32 of the 50 samples per step, a minibatch in \(k\).

Result

The three held-out coefficients after training: the DeepONet's output (top) and the finite-element reference (bottom), same colour scale.

On a mesh five times finer than the training mesh, against finite elements:

\(k\) (held out) rel. \(L^2\) error
0.7 0.0144
1.234 0.0079
1.4 0.0073

Full script

The whole script, as it runs in the tests
"""DeepONet for a parametric Poisson family, k Δu + 1 = 0 on [0, 2] x [0, 1] with u = 0 on the boundary,
learned from the PDE residual alone: the network never sees a solution."""

import foundax
import jax
import numpy as np
import optax

import jno

run = jno.setup(__file__)
KEY, N_SAMPLES, EPOCHS = jax.random.PRNGKey(0), 50, 2_000

d = N_SAMPLES * jno.shape.rect(0, 0, 2, 1, size=0.05).domain()  # one mesh, replicated per sample
x, y, _ = d.variable("interior")
k = d.variable("k", jax.random.uniform(KEY, (N_SAMPLES, 1, 1), minval=0.5, maxval=1.5))  # one k per sample

net = jno.nn(
    foundax.deeponet(n_sensors=1, coord_dim=2, basis_functions=32, hidden_dim=128, activation=jax.numpy.tanh, key=KEY)
)
net.optimizer(optax.adam(optax.cosine_decay_schedule(1e-3, EPOCHS, alpha=1e-2)))

u = (net(k, jno.np.concat([x, y], axis=-1)) * x * (2 - x) * y * (1 - y)).scalar.bind(x=x, y=y)  # u = 0 on the boundary
crux = jno.core([(k * (u.xx + u.yy) + 1.0).mse])
crux.solve(epochs=EPOCHS, batchsize=32)

# Held-out k on a mesh five times finer. Reference: k Δu + 1 = 0 is -Δw = 1 with u = w / k.
K_TEST = (0.7, 1.234, 1.4)
td = len(K_TEST) * jno.shape.rect(0, 0, 2, 1, size=0.01).domain()
xt, yt, _ = td.variable("interior")
kt = td.variable("k", np.array(K_TEST, dtype=np.float32).reshape(-1, 1, 1))
u_net = net(kt, jno.np.concat([xt, yt], axis=-1)) * xt * (2 - xt) * yt * (1 - yt)
pred = np.asarray(crux.eval([u_net], domain=td)).reshape(len(K_TEST), -1)

r = jno.shape.rect(0, 0, 2, 1, size=0.01).domain()
xr, yr, _ = r.variable("interior", split=True)
xb, yb, _ = r.variable("boundary", split=True)
w = r.unknown()
v = w.test()
grad = lambda q: jno.np.jacobian(q, [xr, yr])
fem = jno.fem([jno.np.inner(grad(w), grad(v)) - v, w(xb, yb) - 0.0], quad_degree=3)
ref = np.asarray(fem.solve(linear=jno.solve.lu())).ravel() / np.array(K_TEST)[:, None]

rel_l2 = np.linalg.norm(pred - ref, axis=1) / np.linalg.norm(ref, axis=1)
print(f"held-out k = {K_TEST}: rel-L2 vs finite elements = {np.array2string(rel_l2, precision=4)}")
assert rel_l2.max() < 0.15, rel_l2