Skip to content

Allen–Cahn: a thin front that moves

PINN allen_cahn_2d.py

A disc of one phase sits in another, separated by a thin front, and surface tension makes the disc shrink. The front is about 0.04 wide, so a network has to represent a sharp transition and move it.

The trained network's field u(x, y, t) next to the exact one, both on the PiYG map from -1 (magenta) to 1 (green), for t from 0 to 1: a green disc on a magenta ground whose radius shrinks from 0.4 to 0.2, with a thin white ring where the field crosses zero. The two panels look the same.

The trained network (left) and the exact front (right), \(t=0\) to \(1\), on one colour scale.

The problem

The Allen–Cahn equation with a cubic reaction term, \(\varepsilon=0.03\), and a manufactured forcing \(f\):

\[u_t = \varepsilon^2\Delta u + u - u^3 + f,\qquad u^\* = \tanh\!\left(\frac{R(t)-r}{\sqrt2\,\varepsilon}\right),\quad R(t)=0.4-0.2\,t,\]

with \(r\) the distance from the centre. The exact \(u^\*\) also gives the initial and boundary values.

The jNO code

The forcing \(f=u^\*_t-\varepsilon^2\Delta u^\*-u^\*+u^{\*3}\) is derived by differentiating the exact expression, so there is no hand algebra:

d = jno.shape.rect(0, 0, 1, 1, size=0.025).domain(time=(0, T, 21))
x, y, t = d.variable("interior")
x0, y0, t0 = d.variable("initial")
xb, yb, tb = d.variable("boundary")

front = lambda x, y, t: tanh((0.4 - 0.2 * t - sqrt((x - 0.5) ** 2 + (y - 0.5) ** 2 + 1e-9)) / (2**0.5 * ε))
u_exact = front(x, y, t)  # +1 inside a circle whose radius shrinks from 0.4 to 0.2, -1 outside; the front is ~0.04 wide
ue = u_exact.scalar.bind(x=x, y=y, t=t)
f = ue.t - ε**2 * (ue.xx + ue.yy) - ue + ue**3  # makes u_exact a solution

A DeepONet reads the time in its branch and the position in its trunk:

net = jno.nn(
    foundax.deeponet(
        n_sensors=1, coord_dim=2, n_outputs=1, n_layers=4, basis_functions=64, hidden_dim=64, key=jax.random.PRNGKey(42)
    )
)
net.optimizer(optax.adam(optax.warmup_cosine_decay_schedule(0.0, 2e-3, 10, 12_000, 1e-5)))

The reaction term u - u**3 reads as on paper once the field is bound to its coordinates; the initial and boundary values are loss terms next to the residual:

u = net(t, jno.np.concat([x, y])).scalar.bind(x=x, y=y, t=t)
pde = u.t - ε**2 * (u.xx + u.yy) - u + u**3 - f
ini = u(x0, y0, t0) - front(x0, y0, t0)  # the same field at t = 0
bc = u(xb, yb, tb) - front(xb, yb, tb)  # and on the sides, where the front has not arrived

Result

After 12 000 epochs, on a space-time grid finer than the collocation points:

region rel. \(L^2\) error
whole domain \(1.1\times10^{-2}\)
within 0.06 of the front \(2.5\times10^{-2}\)

Full script

The whole script, as it runs in the tests
"""2-D Allen-Cahn equation, u_t = ε² Δu + u - u³ + f: a thin circular front that shrinks, a manufactured solution."""

import foundax
import jax
import numpy as np
import optax

import jno

run = jno.setup(__file__)
tanh, sqrt = jno.np.tanh, jno.np.sqrt
ε, T = 0.03, 1.0

d = jno.shape.rect(0, 0, 1, 1, size=0.025).domain(time=(0, T, 21))
x, y, t = d.variable("interior")
x0, y0, t0 = d.variable("initial")
xb, yb, tb = d.variable("boundary")

front = lambda x, y, t: tanh((0.4 - 0.2 * t - sqrt((x - 0.5) ** 2 + (y - 0.5) ** 2 + 1e-9)) / (2**0.5 * ε))
u_exact = front(x, y, t)  # +1 inside a circle whose radius shrinks from 0.4 to 0.2, -1 outside; the front is ~0.04 wide
ue = u_exact.scalar.bind(x=x, y=y, t=t)
f = ue.t - ε**2 * (ue.xx + ue.yy) - ue + ue**3  # makes u_exact a solution

net = jno.nn(
    foundax.deeponet(
        n_sensors=1, coord_dim=2, n_outputs=1, n_layers=4, basis_functions=64, hidden_dim=64, key=jax.random.PRNGKey(42)
    )
)
net.optimizer(optax.adam(optax.warmup_cosine_decay_schedule(0.0, 2e-3, 10, 12_000, 1e-5)))

u = net(t, jno.np.concat([x, y])).scalar.bind(x=x, y=y, t=t)
pde = u.t - ε**2 * (u.xx + u.yy) - u + u**3 - f
ini = u(x0, y0, t0) - front(x0, y0, t0)  # the same field at t = 0
bc = u(xb, yb, tb) - front(xb, yb, tb)  # and on the sides, where the front has not arrived

crux = jno.core([pde.mse, ini.mse, bc.mse])
crux.solve(12_000)

# the whole space-time field on a finer grid than the collocation points, against the exact front
N_T = 41
fine = jno.shape.rect(0, 0, 1, 1, size=0.0125).domain(time=(0, T, N_T))
xs, ys, u_h, u_ref = (np.asarray(v)[0, :, :, 0] for v in crux.eval([x, y, u, u_exact], domain=fine, min_consecutive=N_T))
rel_l2 = float(np.linalg.norm(u_h - u_ref) / np.linalg.norm(u_ref))
radius = 0.4 - 0.2 * np.linspace(0, T, N_T)[:, None]
near = np.abs(np.hypot(xs - 0.5, ys - 0.5) - radius) < 0.06  # within 0.06 of the front
rel_front = float(np.linalg.norm((u_h - u_ref)[near]) / np.linalg.norm(u_ref[near]))
print(f"Relative L2 error: {rel_l2:.4e} everywhere, {rel_front:.4e} within 0.06 of the front")
assert rel_l2 < 0.3, f"relative L2 error too large: {rel_l2:.3e}"  # reference: the tanh front above