A quickstart for stochastic steppers¶
Run a periodic, reflected, bounded and vector problem with the same rollout interface. See the capability matrix for supported combinations. Runtime depends on the machine. The default runs in a few minutes or less. Set EXPONAX_SPDE_PRESET=smoke for a smaller demonstration. Figures are written only if SAVE_FIGURES is enabled.
Only the diffusion OU law is an exact statistical gate. The other outputs illustrate the API.
Four families and three spectra¶
For the reflected solution, the periodic grid is twice as long as the half domain and includes the reflection endpoints. The native Neumann stepper uses cell centres. The vector Burgers example starts from a gradient field and forces only gradients. Matérn weights decay with wavenumber, a band selects a shell, and explicit modes select integer wave indices.
The left panel compares three discretisations with independent noise draws.
n = 16 if SMOKE else 32
h = 0.025
x = jnp.arange(n) * (2 * jnp.pi / n)
u0 = jnp.cos(x)[None]
periodic = spde.StochasticDiffusion(1, 2 * np.pi, n, h, noise_spectrum=Matern(1.0))
half_x = jnp.linspace(0, np.pi, n // 2 + 1)
reflected = spde.StochasticDiffusion(
1, 2 * np.pi, n, h, noise_symmetry="even", noise_spectrum=Modes([(2,)])
)
mirror = spde.symmetry.extend(jnp.cos(half_x)[None], "even")
cell_x = (jnp.arange(n // 2) + 0.5) * (2 * jnp.pi / n)
bounded = spde.bounded.StochasticDiffusion(
1, np.pi, n // 2, h, boundary="neumann", noise_spectrum=Band(1.0, 3.0)
)
burgers = spde.StochasticVectorBurgers(
2, 2 * np.pi, n, h, noise_spectrum=Modes([(1, 1)])
)
velocity = jnp.stack(
[jnp.broadcast_to(-0.1 * jnp.sin(x[:, None] + x[None, :]), (n, n))] * 2
)
steps = 4 if SMOKE else 20
periodic_path = jax.jit(spde.stochastic_rollout(periodic, steps, include_init=True))(
u0, jax.random.key(34001)
)
mirror_path = jax.jit(spde.stochastic_rollout(reflected, steps))(
mirror, jax.random.key(34002)
)
bounded_path = jax.jit(spde.stochastic_rollout(bounded, steps))(
jnp.cos(cell_x)[None], jax.random.key(34003)
)
velocity_path = jax.jit(spde.stochastic_rollout(burgers, steps))(
velocity, jax.random.key(34004)
)
ensemble = jax.jit(
spde.stochastic_ensemble_rollout(periodic, steps, 4, stride=steps, batch_size=2)
)(u0, jax.random.key(34005))
print("DIAGNOSTIC | initial and terminal ensemble shape |", ensemble.shape)
fig, (ax, flow) = plt.subplots(1, 2, figsize=(9, 3.5), layout="constrained")
ax.plot(x, periodic_path[-1, 0], label="periodic")
ax.plot(half_x, spde.symmetry.restrict(mirror_path[-1], "even")[0], label="reflected")
ax.plot(cell_x, bounded_path[-1, 0], ".", label="bounded Neumann")
ax.set(xlabel="x", ylabel="field", title="Three diffusion discretisations")
ax.legend()
width = max(1, n // 12)
flow.quiver(
x[::width],
x[::width],
*np.asarray(velocity_path[-1, :, ::width, ::width]).transpose(0, 2, 1),
)
flow.set(xlabel="x", ylabel="y", title="Vector Burgers")
show(fig, "quickstart_families")
DIAGNOSTIC | initial and terminal ensemble shape | (4, 2, 1, 32)
Reuse one path at two step sizes¶
For additive noise the increment API supplies the stochastic convolution including the noise intensity. Combining two successive increments uses the semigroup on the earlier increment. This is also how coupled Richardson sampling reduces variance. The streamed structure factor reports mean mode power without retaining trajectories.
fine = spde.StochasticDiffusion(1, 2 * np.pi, n, h / 2, noise_spectrum=Matern(1.0))
draws = jax.vmap(fine.sample_increment)(jax.random.split(jax.random.key(34006), 2))
start = ex.fft(u0)
two_steps = fine.step_with_increment(
fine.step_with_increment(start, draws[0]), draws[1]
)
one_step = periodic.step_with_increment(start, fine.coarsen_increments(draws))
spectrum = jax.jit(spde.stochastic_structure_factor(periodic, steps, 4, batch_size=2))(
u0, jax.random.key(34007)
)
rich = spde.richardson_weak_extrapolation(
periodic,
fine,
u0,
steps,
jax.random.key(34008),
num_samples=16,
batch_size=4,
coupled=True,
observable=lambda u: jnp.mean(u**2),
)
print(
"DIAGNOSTIC | coupled OU replay maximum difference | "
f"{jnp.max(abs(two_steps - one_step)):.3g}"
)
print(f"DIAGNOSTIC | streamed power of mode 1 | {spectrum[0, 1]:.5g}")
print(
"DIAGNOSTIC | coupled Richardson mean square | "
f"{rich['mean_rich']:.5g} ± {rich['se_rich']:.2g} SE"
)
DIAGNOSTIC | coupled OU replay maximum difference | 1.39e-17 DIAGNOSTIC | streamed power of mode 1 | 0.97425 DIAGNOSTIC | coupled Richardson mean square | 0.49445 ± 0.0082 SE
Exact law: one diffusion step¶
From zero, the Fourier mode \(k\) has raw power \(N \sigma^2 q_k (\exp(2 L_k h)-1)/(2 L_k \Delta x)\), with \(L_k=- u k^2\). The factor \(N\) comes from the unnormalised transform of grid white noise. At mode 2, the two Gaussian coordinates are independent with equal variance. Its squared modulus therefore has coefficient of variation 1. Use 1,600 independent paths for a planned relative SE of 2.5%, also in smoke mode.
For the stochastic convolution and its mode representation, see Lord, Powell & Shardlow (2014).
sigma, nu, mode, alpha = 0.35, 0.2, 2, 1.0
model = spde.StochasticDiffusion(
1, 2 * np.pi, n, h, diffusivity=nu, sigma=sigma, noise_alpha=alpha
)
zero = ex.fft(jnp.zeros_like(u0))
def power(key):
return jnp.abs(model.step_fourier(zero, key=key)[0, mode]) ** 2
symbol = -nu * mode**2
q = (1 + mode**2) ** (-alpha)
target = n * sigma**2 * q * np.expm1(2 * symbol * h) / (2 * symbol * (2 * np.pi / n))
check_power("diffusion mode 2 power", observations(power, 34009), target)
PASS | diffusion mode 2 power | 0.09635306 ± 0.0024 SE | target 0.0978519 | 0.63 SE | relative SE 2.48%