Swift–Hohenberg pattern formation
spectral swift_hohenberg_spectral.py
Tiny random noise grows into fingerprint-like labyrinths of rolls: the Swift–Hohenberg equation, a prototype for convection rolls, animal coat patterns and crystal grain boundaries. The rolls are full of defects, half-integer disclinations that glide and annihilate in pairs, so the labyrinth slowly straightens.

The computed \(u\) on a \(384\times384\) Fourier grid, 22 critical wavelengths across. The defect cores are found afterwards from the computed field, by image analysis.
The problem
on a doubly periodic square. The linear growth rate \(\sigma(k)=r-(1-k^2)^2\) amplifies only modes near the ring \(|\mathbf k|=1\). The equation is the gradient flow of the Lyapunov energy
and a defect-free 1-D roll has \(F/L^2=-r^2/6\) and rms amplitude \(\sqrt{2r/3}\).
The jNO code
The fourth-order partials go into the equation as they read, and etdrk4() integrates the whole linear operator
exactly in Fourier space. jno.sp(dealias=2) forms the cubic term on a grid padded to twice the size:
u = U.bind(x=x, y=y, t=t, scheme=jno.sp(dealias=2))
lap = u.xx + u.yy
biharm = u.xxxx + 2 * u.xxyy + u.yyyy
ts = np.linspace(0.0, T, FRAMES)
uf = np.asarray(
jno.spectral(
[
u.t + (1.0 - R_PARAM) * u + 2 * lap + biharm + u**3,
U(x0, y0) - jax.numpy.asarray(u0.reshape(-1, 1)),
U(xl, yl) - U(xr, yr),
U(xb, yb) - U(xt, yt),
]
).solve(time=jno.solve.etdrk4(), save_ts=ts)
).reshape(FRAMES, N, N)
The energy is evaluated through the unknown's own Fourier basis: \((1+\Delta)\) is the factor \(1-|\mathbf k|^2\) on the coefficients, and the integral is the basis's quadrature:
def energy(frame, c):
one_plus_lap = np.real(np.asarray(basis.values((1.0 - K2) * c))).ravel()
u_ = frame.ravel()
return float(np.asarray(basis.integrate(0.5 * one_plus_lap**2 - 0.5 * R_PARAM * u_**2 + 0.25 * u_**4)))
Result
![Left: Lyapunov free-energy density F[u]/L^2 decreasing monotonically from +1.48e-3 to -0.0113 toward the 1-D roll ground state -0.0131. Middle: total disclination cores falling from 138 at t = 20 to 22 at t = 180 with exact N_{+1/2} = N_{-1/2} balance. Right: azimuthal power spectrum P(k) peaking at k_c = 1 with a cubic 3*k_c harmonic.](../swift_hohenberg_spectral_energy.png)
| check | computed | theory |
|---|---|---|
| energy density \(F/L^2\) | non-increasing, \(1.48\times10^{-3}\to-0.0113\) | above the 1-D roll's \(-0.0131\) |
| disclinations | \(138\) at \(t=20\) \(\to\) \(22\) at \(t=180\) | \(N_{+1/2}=N_{-1/2}\) at every frame |
| dominant wavenumber | \(1.0000\) | \(k_c=1\) |
| rms amplitude | \(0.411\) | 1-D roll \(0.432\) |
| power at \(k\ge4.5\) | \(5.9\times10^{-13}\) |
Full script
The whole script, as it runs in the tests
"""Swift-Hohenberg defect labyrinths on a periodic box: 4th-order ETDRK4, cubic dealiasing, and disclination tracking.
u_t = r u - (1 + Delta)^2 u - u^3 .
Tiny random noise amplifies modes on the critical ring |k| = 1 (wavelength 2 pi) and saturates into fingerprint-like
labyrinthine rolls populated by +-1/2 nematic disclinations that glide and annihilate in pairs. Checks: the Lyapunov
free energy F[u] decreases monotonically, net topological charge vanishes at every instant (Poincare-Hopf on T^2),
more than two-thirds of the initial defect pairs annihilate by t = 180, and the dealiased k >= 4.5 tail is < 1e-10.
"""
import jax
import numpy as np
import jno
jax.config.update("jax_enable_x64", True)
run = jno.setup(__file__)
N_WAVES = 22
L, N = N_WAVES * 2 * np.pi, 384
R_PARAM = 0.28
T, STEPS, FRAMES = 180.0, 900, 91
d = jno.shape.rect(0.0, 0.0, L, L).domain(time=(0.0, T, STEPS + 1))
U = d.unknown(space=("fourier", "fourier"), order=(N, N), name="u")
x, y, t = d.variable("interior", split=True)
x0, y0, _ = d.variable("initial", split=True)
(xl, yl, _), (xr, yr, _) = d.variable("left", split=True), d.variable("right", split=True)
(xb, yb, _), (xt, yt, _) = d.variable("bottom", split=True), d.variable("top", split=True)
basis = U.basis # the Fourier basis: coefficients, wavenumbers, values, integrals
kx, ky = (np.asarray(basis.wavenumbers(axis)) for axis in (0, 1))
K2 = kx[:, None] ** 2 + ky[None, :] ** 2 # |k|^2 on the coefficient grid
# Small random seed (std = 0.02): white noise kept only on 0 < |k| < 2.5, all orientations around the critical ring.
noise = np.random.default_rng(42).normal(size=N * N)
band = (K2 > 0) & (K2 < 2.5**2)
u0 = np.real(np.asarray(basis.values(band * np.asarray(basis.coeffs(noise))))).ravel()
u0 = 0.02 * u0 / u0.std()
# Explicit 4th-order partials fold -(1 + Delta)^2 into ETDRK4's exact linear propagator; dealias=2 pads u^3 to 768^2.
u = U.bind(x=x, y=y, t=t, scheme=jno.sp(dealias=2))
lap = u.xx + u.yy
biharm = u.xxxx + 2 * u.xxyy + u.yyyy
ts = np.linspace(0.0, T, FRAMES)
uf = np.asarray(
jno.spectral(
[
u.t + (1.0 - R_PARAM) * u + 2 * lap + biharm + u**3,
U(x0, y0) - jax.numpy.asarray(u0.reshape(-1, 1)),
U(xl, yl) - U(xr, yr),
U(xb, yb) - U(xt, yt),
]
).solve(time=jno.solve.etdrk4(), save_ts=ts)
).reshape(FRAMES, N, N)
# Lyapunov free energy F[u] = int [ 1/2 ((1 + Delta) u)^2 - (r/2) u^2 + (1/4) u^4 ] dA, through the basis:
# (1 + Delta) is the factor 1 - |k|^2 on the coefficients, the integral is the basis's own quadrature.
coeffs = [np.asarray(basis.coeffs(frame.ravel())) for frame in uf]
def energy(frame, c):
one_plus_lap = np.real(np.asarray(basis.values((1.0 - K2) * c))).ravel()
u_ = frame.ravel()
return float(np.asarray(basis.integrate(0.5 * one_plus_lap**2 - 0.5 * R_PARAM * u_**2 + 0.25 * u_**4)))
F = np.array([energy(frame, c) for frame, c in zip(uf, coeffs)])
F_density = F / L**2
rms_hist = np.sqrt([float(np.asarray(basis.mean(frame.ravel() ** 2))) for frame in uf])
amp_rms = float(np.sqrt(2 * R_PARAM / 3))
i_sat = int(np.argmax(rms_hist > 0.70 * amp_rms))
# Azimuthally averaged power spectrum |c|^2, binned by |k|, at saturation onset and at the end.
dk = 2 * np.pi / L
k_bin = np.rint(np.sqrt(K2) / dk).astype(int).ravel()
p_rad_sat, p_rad_end = (np.bincount(k_bin, weights=np.abs(coeffs[m]).ravel() ** 2) for m in (i_sat, -1))
k_vals = np.arange(len(p_rad_end)) * dk
k_peak = float(k_vals[int(np.argmax(p_rad_end))])
tail_frac = float(np.abs(coeffs[-1])[K2 >= 4.5**2].__pow__(2).sum() / (np.abs(coeffs[-1]) ** 2).sum())
f_roll = -(R_PARAM**2) / 6
print(
f"Swift-Hohenberg {N}^2 (dealias=2), r={R_PARAM}, {STEPS} steps to t={T}: "
f"F/L^2 {F_density[0]:.3e}->{F_density[-1]:.4f} (1-D roll {f_roll:.4f}), "
f"k_peak={k_peak:.4f}, rms(u)={rms_hist[-1]:.3f} (roll {amp_rms:.3f}), tail(k>=4.5)={tail_frac:.2e}"
)
assert np.all(np.diff(F) <= 1e-10), f"Lyapunov energy increased: max dF={np.diff(F).max():.3e}"
assert f_roll - 1e-3 < F_density[-1] < 0.75 * f_roll
assert abs(k_peak - 1.0) <= 1.5 * dk
assert abs(rms_hist[-1] - amp_rms) / amp_rms < 0.10
assert tail_frac < 1e-10