Skip to content

A Fourier neural operator trained on solution data

operator fno_poisson_2d.py

A Fourier neural operator learns the map from a forcing to the solution of Poisson's equation, from a dataset of examples. Once trained, a new forcing gives its solution in one forward pass.

Top row: the FNO's own output for three held-out forcings on the unit square. Bottom row: the exact solutions of the same problems. One signed colour scale for all six panels.

Top: the operator's output for three forcings it never saw. Bottom: the exact solutions.

The problem

\[-\Delta u = f\quad\text{on}\ [0,1]^2,\qquad u=0\ \text{on}\ \partial\Omega,\]

with \(f\) drawn from a truncated sine basis, so \(u\) is known in closed form. The operator \(\mathcal G_\theta: f\mapsto u\) is fitted to 256 pairs on a \(64\times64\) grid:

\[\min_\theta\ \frac{1}{N}\sum_{i=1}^{N}\lVert\mathcal G_\theta(f_i)-u_i\rVert^2 .\]

The jNO code

The arrays are attached to a domain as named tensors, so the loss references them directly:

forcing, solution = generate_poisson_data(SAMPLES, GRID, n_modes=5, alpha=1.5, seed=42)
d = build_domain_from_arrays(forcing, solution, GRID)
f, u_data = d.variable("_f"), d.variable("_u")

Each FNO layer transforms to Fourier space, mixes the lowest n_modes coefficients with a learned linear map and transforms back:

net = jno.nn(
    foundax.fno2d(
        in_features=1,
        hidden_channels=16,
        n_modes=6,
        d_vars=1,
        n_layers=2,
        n_steps=1,
        d_model=(GRID, GRID),
        norm="layer",
        linear_conv=True,
        key=KEY,
    )
)

The loss is a supervised regression, no PDE residual:

crux = jno.core([(u_data - net(f)).mse])
crux.solve(epochs=EPOCHS, batchsize=32)

Result

On eight held-out forcings from an unseen seed, the mean rel. \(L^2\) error is 0.0995 (per sample 0.043 to 0.193).

Full script

The whole script, as it runs in the tests
"""Fourier neural operator for -Δu = f on the unit square with u = 0 on the boundary, trained on (f, u) pairs:
a regression, no PDE residual."""

import foundax
import jax
import numpy as np
import optax
from poisson_data import build_domain_from_arrays, generate_poisson_data

import jno

run = jno.setup(__file__)
KEY, GRID, SAMPLES, EPOCHS = jax.random.PRNGKey(0), 64, 256, 600

forcing, solution = generate_poisson_data(SAMPLES, GRID, n_modes=5, alpha=1.5, seed=42)
d = build_domain_from_arrays(forcing, solution, GRID)
f, u_data = d.variable("_f"), d.variable("_u")

net = jno.nn(
    foundax.fno2d(
        in_features=1,
        hidden_channels=16,
        n_modes=6,
        d_vars=1,
        n_layers=2,
        n_steps=1,
        d_model=(GRID, GRID),
        norm="layer",
        linear_conv=True,
        key=KEY,
    )
)
net.optimizer(
    optax.chain(
        optax.clip_by_global_norm(1e-3),
        optax.adamw(optax.cosine_decay_schedule(5e-4, EPOCHS, alpha=1e-7 / 5e-4), weight_decay=1e-6),
    )
)

crux = jno.core([(u_data - net(f)).mse])
crux.solve(epochs=EPOCHS, batchsize=32)

# Eight held-out forcings from another seed: the operator's own output against the exact solutions.
N_TEST = 8
f_test, u_test = generate_poisson_data(N_TEST, GRID, n_modes=5, alpha=1.5, seed=123)
test = build_domain_from_arrays(f_test, u_test, GRID)
pred = np.asarray(crux.eval([net(test.variable("_f"))], domain=test)).reshape(N_TEST, -1)
truth = u_test.reshape(N_TEST, -1)

rel = np.linalg.norm(pred - truth, axis=1) / np.linalg.norm(truth, axis=1)
print(f"held-out per-sample rel-L2: {np.array2string(rel, precision=3)}")
print(f"held-out mean rel-L2 = {rel.mean():.4f}")
assert rel.mean() < 0.2, rel.mean()