Skip to content

Viscous Burgers: moving the collocation points

PINN burgers_viscous_1d.py

A smooth wave steepens into a shock only about 0.01 wide: the classic hard case for a PINN. The network learns the solution on the space-time rectangle, and the question is where to put the collocation points. Fixed uniform points are compared with three adaptive samplers that move the points to where the residual is large.

Four columns, one per strategy (fixed, RAD, RAR-D, R3). Each column has two panels on the x-t rectangle, x to the right and t upward: above, the network's u(x, t) in red and blue with the collocation points as small dark dots; below, the absolute error against the exact solution, pale where it is small and dark where it is large. The fixed points stay evenly spread. For the three adaptive strategies the dots gather along the vertical line x = 0 for t above about 0.3, where the shock forms; the error is largest along that line.

One column per strategy. Top: the network's \(u(x,t)\) with the collocation points it holds at that epoch. Bottom: the error against the exact solution.

The problem

The viscous Burgers equation on \((x,t)\in[-1,1]\times[0,1]\):

\[u_t + u\,u_x = \nu\,u_{xx},\qquad \nu=\frac{0.01}{\pi},\qquad u(x,0)=-\sin\pi x,\qquad u(\pm1,t)=0 .\]

The exact solution comes from the Cole–Hopf transform. Time is the second coordinate of a 2-D domain, so a collocation point is a point of the \(x\)–\(t\) plane and a sampler can move it in both.

The jNO code

The four strategies are jNO's resampling options: none, RAD, RAR-D and R3, each replacing points from epoch 1000 on:

strategies = {  # the settings of the examples in docs/ml/resampling.md
    "fixed": None,  # the same uniform points the whole time
    "RAD": jno.sampler.rad(resample_every=100, resample_fraction=0.1, start_epoch=1000, k=10),
    "RAR-D": jno.sampler.rard(resample_every=100, resample_fraction=0.1, start_epoch=1000, power=2.0),
    "R3": jno.sampler.r3(resample_every=100, start_epoch=1000, threshold_mode="mean"),
}

One function builds the problem for a given strategy, so the runs differ only in how the points move. The factor (1 - x**2) makes \(u(\pm1,t)=0\) exact, and .scalar.bind(x=x, t=t) lets u.t, u.x and u.xx read like the equation:

def burgers(strategy):
    """The same problem, network and initialisation every time; only the way the collocation points move differs."""
    d = jno.shape.rect(-1.0, 0.0, 1.0, 1.0).domain()  # the (x, t) plane: t is the second coordinate
    x, t, _ = d.variable("interior", sample=(POINTS, None), resampling_strategy=strategy)
    x0, t0, _ = d.variable("bottom", sample=(500, None))  # t = 0

    net = jno.nn(foundax.mlp(in_features=2, hidden_dims=64, num_layers=5, key=jax.random.PRNGKey(0)))
    net.optimizer(optax.adam(optax.warmup_cosine_decay_schedule(0.0, 2e-3, 10, EPOCHS, 1e-5)))

    u = (net(x, t) * (1 - x**2)).scalar.bind(x=x, t=t)  # hard u(-1, t) = u(1, t) = 0
    pde = u.t + u * u.x - ν * u.xx
    ini = u(x0, t0) + sin(π * x0)  # u(x, 0) = -sin(πx)
    return jno.core([pde.mse, ini.mse]), u, x, t

Result

Per strategy, a row: the final network u(x, t) with its points, and the absolute error against the Cole-Hopf solution, which is concentrated in a thin vertical strip at x = 0: thinnest for RAD, about as thin for the fixed points and R3, and a wide dark band for RAR-D, whose error also spreads into the smooth part on either side. The figure is the final state of the animation's run.

Rel. \(L^2\) errors after 15 000 epochs, one run each:

strategy away from the shock within 0.1 of the shock points in the shock strip
fixed \(1.5\times10^{-3}\) \(3.9\times10^{-2}\) 6 %
RAD \(3.8\times10^{-3}\) \(2.0\times10^{-2}\) 37 %
RAR-D \(4.1\times10^{-2}\) \(5.7\times10^{-1}\) 23 %
R3 \(4.2\times10^{-3}\) \(4.2\times10^{-2}\) 20 %

Near the shock RAD halves the error of the fixed points and R3 is level with them; RAR-D, in this run, is 14 times further from the exact solution there and 28 times away from it. Away from the shock the fixed points are the most accurate, because moving points to the shock leaves fewer for the smooth part.

Full script

The whole script, as it runs in the tests
"""Viscous Burgers equation u_t + u u_x = ν u_xx on the space-time rectangle, trained with four point strategies."""

import foundax
import jax
import numpy as np
import optax

import jno

run = jno.setup(__file__)
π, sin = jno.np.pi, jno.np.sin
ν, EPOCHS, POINTS = 0.01 / np.pi, 15_000, 4_000

strategies = {  # the settings of the examples in docs/ml/resampling.md
    "fixed": None,  # the same uniform points the whole time
    "RAD": jno.sampler.rad(resample_every=100, resample_fraction=0.1, start_epoch=1000, k=10),
    "RAR-D": jno.sampler.rard(resample_every=100, resample_fraction=0.1, start_epoch=1000, power=2.0),
    "R3": jno.sampler.r3(resample_every=100, start_epoch=1000, threshold_mode="mean"),
}


def burgers(strategy):
    """The same problem, network and initialisation every time; only the way the collocation points move differs."""
    d = jno.shape.rect(-1.0, 0.0, 1.0, 1.0).domain()  # the (x, t) plane: t is the second coordinate
    x, t, _ = d.variable("interior", sample=(POINTS, None), resampling_strategy=strategy)
    x0, t0, _ = d.variable("bottom", sample=(500, None))  # t = 0

    net = jno.nn(foundax.mlp(in_features=2, hidden_dims=64, num_layers=5, key=jax.random.PRNGKey(0)))
    net.optimizer(optax.adam(optax.warmup_cosine_decay_schedule(0.0, 2e-3, 10, EPOCHS, 1e-5)))

    u = (net(x, t) * (1 - x**2)).scalar.bind(x=x, t=t)  # hard u(-1, t) = u(1, t) = 0
    pde = u.t + u * u.x - ν * u.xx
    ini = u(x0, t0) + sin(π * x0)  # u(x, 0) = -sin(πx)
    return jno.core([pde.mse, ini.mse]), u, x, t




runs = {}
for name, strategy in strategies.items():
    runs[name] = crux, u, x, t = burgers(strategy)
    crux.solve(EPOCHS, min_consecutive=None)


def cole_hopf(x, t, nu, n=100):
    """The exact solution: the Cole-Hopf transform turns Burgers into the heat equation, whose kernel is integrated
    by Gauss-Hermite quadrature."""
    z, w = np.polynomial.hermite.hermgauss(n)
    y = x[..., None] - 2 * np.sqrt(nu * t[..., None]) * z
    log_kernel = -np.cos(np.pi * y) / (2 * np.pi * nu)
    e = np.exp(log_kernel - log_kernel.max(-1, keepdims=True)) * w
    return -(np.sin(np.pi * y) * e).sum(-1) / e.sum(-1)


fine = jno.shape.rect(-1.0, 0.0, 1.0, 1.0, size=0.01).domain()  # nodes to evaluate every network on
errors = {}
for name, (crux, u, x, t) in runs.items():
    xs, ts, u_h = (np.asarray(v).ravel() for v in crux.eval([x, t, u], domain=fine))
    u_ref = cole_hopf(xs, np.maximum(ts, 1e-9), ν)
    near = (np.abs(xs) < 0.1) & (ts > 0.3)  # the shock, once it has formed
    rel = lambda m: float(np.linalg.norm((u_h - u_ref)[m]) / np.linalg.norm(u_ref[m]))
    errors[name] = rel(~near), rel(near)
    print(f"{name:>5}: relative L2 error {errors[name][0]:.3e} away from the shock, {errors[name][1]:.3e} within 0.1 of it")
    assert errors[name][0] < 0.1, f"{name}: error away from the shock too large: {errors[name][0]:.3e}"  # Cole-Hopf