Skip to content

Diagnostics and ensembles

The rollout functions follow keyed stochastic paths. The strong-order checks use coupled fine and coarse paths with the same underlying noise. The weak extrapolation guide gives the Richardson estimator and its assumptions. The energy functionals use grid-compatible spectral quadrature.

exponax_spde.stochastic_rollout

stochastic_rollout(
    stepper: Module,
    num_steps: int,
    *,
    include_init: bool = True,
    stride: int = 1
) -> Callable[
    [Float[Array, "1 * N"], PRNGKeyArray],
    Float[Array, "T 1 *N"],
]

Return a function that rolls a stochastic stepper forward.

The returned function has signature::

trajectory = rollout_fn(u_init, key)

The trajectory contains steps stride, 2*stride, ... up to num_steps. It contains num_steps // stride states, plus step zero if include_init=True. Unlike this stochastic utility, upstream exponax.rollout has no stride argument.

The function is fully JIT-compatible because it uses jax.lax.scan internally rather than a Python-level loop.

Parameters:

Name Type Description Default
stepper Module

A stochastic stepper whose __call__ accepts (u, key=key) as a keyword-only key argument.

required
num_steps int

Number of time steps T to take.

required
include_init bool

Whether to prepend the initial condition to the returned trajectory array.

True
stride int

Positive number of time steps between saved states. The number of steps must be a multiple of the stride.

1

Returns:

Type Description
Callable[[Float[Array, '1 * N'], PRNGKeyArray], Float[Array, 'T 1 *N']]

Callable taking the initial field and key and returning the saved trajectory.

Example
rollout_fn = stochastic_rollout(stepper, 500, include_init=True)
trajectory = jax.jit(rollout_fn)(u_0, jax.random.PRNGKey(0))
trajectory.shape  # (501, 1, N)

exponax_spde.stochastic_ensemble_rollout

stochastic_ensemble_rollout(
    stepper: Module,
    num_steps: int,
    num_samples: int,
    *,
    include_init: bool = True,
    batch_size: int | None = None,
    stride: int = 1
) -> Callable[
    [Float[Array, "1 * N"], PRNGKeyArray],
    Float[Array, "M T 1 *N"],
]

Return a batched rollout function producing M independent paths.

The returned function has signature::

ensemble = ensemble_fn(u_init, key)

where ensemble has shape (M, S, 1, *N) and S is num_steps // stride + int(include_init). The saved states use the same keys as the corresponding states with stride=1.

Parameters:

Name Type Description Default
stepper Module

Forwarded to stochastic_rollout.

required
num_steps int

Number of time steps T to take.

required
num_samples int

Number of Monte-Carlo trajectories M.

required
include_init bool

Whether to prepend the initial condition.

True
batch_size int | None

Number of paths to advance together. None maps over the full sample axis.

None
stride int

Positive number of time steps between saved states. The number of steps must be a multiple of the stride.

1

Returns:

Type Description
Callable[[Float[Array, '1 * N'], PRNGKeyArray], Float[Array, 'M T 1 *N']]

Callable taking the initial field and key and returning one trajectory per path.

Example
ens_fn = stochastic_ensemble_rollout(stepper, 200, num_samples=256)
ensemble = jax.jit(ens_fn)(u_0, jax.random.PRNGKey(0))
ensemble.shape  # (256, 201, 1, N)

exponax_spde.structure_factor

structure_factor(
    ensemble: Float[Array, "M T 1 *N"],
    *,
    burn_in_fraction: float = 0.5
) -> Float[Array, "1 * K"]

Compute the empirical structure factor of a periodic trajectory ensemble.

Average \(|\hat u_k|^2\) over samples and post-burn-in times, using exponax coefficient extraction units. The stationary heat-equation law follows by dividing the additive increment power by \(1-e^{2hL_k}\) for \(L_k<0\).

Parameters:

Name Type Description Default
ensemble Float[Array, 'M T 1 *N']

Channel-first trajectories, with sample and time axes first.

required
burn_in_fraction float

Fraction of saved time steps discarded before averaging.

0.5

Returns:

Type Description
Float[Array, '1 * K']

One array of spectral powers in channel-first rFFT layout.

exponax_spde.stochastic_structure_factor

stochastic_structure_factor(
    stepper: Module,
    num_steps: int,
    num_samples: int,
    *,
    burn_in_fraction: float = 0.5,
    batch_size: int | None = None
) -> Callable[
    [Float[Array, "1 * N"], PRNGKeyArray],
    Float[Array, "1 * K"],
]

Return a rollout function that accumulates the empirical structure factor.

With the same initial state and key, its spectrum agrees up to rounding with that of structure_factor applied to stochastic_ensemble_rollout with include_init=False and the same burn-in fraction. The returned function fn(u_init, key) has shape (1, *K) in real-FFT coefficient extraction units.

The scan carries the current fields and spectral sums, so peak memory is O(num_samples × N^d), independent of the number of steps. The step and sample counts must be positive, and 0 <= burn_in_fraction < 1. Set batch_size to limit the number of paths advanced and transformed together. The scan still carries one field per sample.

Parameters:

Name Type Description Default
stepper Module

Stochastic stepper on a periodic grid.

required
num_steps int

Number of time advances per path.

required
num_samples int

Number of independent paths.

required
burn_in_fraction float

Fraction of the steps omitted from the average.

0.5
batch_size int | None

Number of paths to advance together, or None for all paths.

None

Returns:

Type Description
Callable[[Float[Array, '1 * N'], PRNGKeyArray], Float[Array, '1 * K']]

Callable taking the initial field and key and returning spectral power.

exponax_spde.richardson_weak_extrapolation

richardson_weak_extrapolation(
    stepper_coarse: Module,
    stepper_fine: Module,
    u0: Float[Array, "1 * N"],
    num_steps_coarse: int,
    key: PRNGKeyArray,
    *,
    num_samples: int = 512,
    batch_size: int | None = None,
    weak_order: float = 1.0,
    coupled: bool = False,
    observable: Callable[[Array], Array] | None = None
) -> dict[str, Array]

Extrapolate the expected value of a terminal observable.

Take num_steps_coarse coarse steps of size h and twice as many fine steps of size h/2 from the same physical initial field u0. For a common leading weak-bias term proportional to hp, where p is weak_order, the weighted estimate (2p * mean_fine - mean_coarse) / (2**p - 1) cancels that term. Pass a finite, positive weak_order.

The default samples independent ensembles from key. Use coupled=True with compatible stochastic steppers to share the driving noise. The experimental protocol requires both steppers to expose dt and implement sample_increment(key), step_with_increment(u_hat, increment) and coarsen_increments(increments). Use steppers for the same model. The fine stepper coarsens each consecutive pair of fine increments. With concrete time steps, the coarse step must be twice the fine step. Concrete grid and noise settings must match wherever both expose them.

num_samples counts paths per ensemble, or pairs in coupled mode, and must be at least two. batch_size bounds the number of paths or pairs advanced at once. An optional pure JAX observable maps each terminal physical field to a scalar or fixed-shape array. The default uses the physical field itself.

The result contains mean_coarse, mean_fine, mean_rich, var_coarse and var_fine (population variances), plus se_coarse, se_fine, se_rich and se_difference (for the fine-minus-coarse mean). Standard errors use sample variances with ddof=1. In coupled mode, the last two use the paired samples to account for covariance. All results are componentwise.

Parameters:

Name Type Description Default
stepper_coarse Module

Stepper for the coarse time grid.

required
stepper_fine Module

Stepper for the fine time grid.

required
u0 Float[Array, '1 * N']

Initial physical field shared by all paths.

required
num_steps_coarse int

Number of coarse advances.

required
key PRNGKeyArray

JAX key for the coupled or independent ensemble.

required
num_samples int

Number of samples or sample pairs.

512
batch_size int | None

Maximum number of path advances evaluated together.

None
weak_order float

Positive power of the leading time-step bias.

1.0
coupled bool

Whether to reuse fine increments on the coarse grid.

False
observable Callable[[Array], Array] | None

Pure JAX function of the terminal field, or None to use the field.

None

Returns:

Type Description
dict[str, Array]

Dictionary of coarse, fine and extrapolated statistics and standard errors.

exponax_spde.functionals.quadratic_energy

quadratic_energy(u, weights=None, *, boundary=None)

Return half the squared \(L^2\) norm per unit volume.

Parameters:

Name Type Description Default
u Array

Channel-first field.

required
weights Array | float | None

Weights for squared spectral coefficients.

None
boundary Boundary

None for a periodic grid, otherwise select the bounded transform.

None

Returns:

Name Type Description
energy Array

The weighted quadratic energy density.

exponax_spde.functionals.ginzburg_landau

ginzburg_landau(stepper, u)

Integrate the Allen–Cahn Ginzburg–Landau energy.

The energy density is \(\nu|\nabla u|^2/2+\lambda(u^4-2u^2+1)/4\). The quartic term uses the Galerkin projection when a mask is available.

Parameters:

Name Type Description Default
stepper object

Periodic or bounded Allen–Cahn stepper defining the grid and drift.

required
u Array

Channel-first field.

required

Returns:

Name Type Description
energy Array

The scalar energy on the domain.

exponax_spde.functionals.van_der_waals

van_der_waals(stepper, u)

Integrate the Cahn–Hilliard free energy in the \(H^{-1}\) convention.

The gradient coefficient is \(\nu\gamma/2\). The potential is \(\nu(c_1u^2/2+c_3u^4/4)\) with the stepper's coefficients. The potential uses the Galerkin projection for the quartic term when a mask is available.

Parameters:

Name Type Description Default
stepper object

Periodic or bounded Cahn–Hilliard stepper defining the grid and drift.

required
u Array

Channel-first field.

required

Returns:

Name Type Description
energy Array

The scalar free energy on the domain.

exponax_spde.functionals.swift_hohenberg

swift_hohenberg(stepper, u)

Integrate the Swift–Hohenberg energy.

The quadratic energy uses \((k_c+\Delta)^2-r\). The polynomial potential uses the Galerkin-projected field when a mask is available.

Parameters:

Name Type Description Default
stepper object

Periodic or bounded Swift–Hohenberg stepper defining the grid and drift.

required
u Array

Channel-first field.

required

Returns:

Name Type Description
energy Array

The scalar energy on the domain.