SFI.langevin.noise module

Noise models for Langevin simulators.

This module provides a hierarchy of noise models that go beyond the simple diagonal diffusion D = σ I. In particular, it supports conserved noise needed by SPDE models such as Active Model B+, where the noise takes the form ∇·(σ η) and preserves the spatial integral of the field.

Class hierarchy

  • NoiseModel — abstract base;

  • WhiteNoise — i.i.d. per-site Gaussian (recovers current behaviour);

  • ConservedNoisesqrt(-σ² ∇²) ξ via FFT on a periodic grid;

  • CompositeNoise — different noise models on different field components.

Usage

Pass a NoiseModel instance as D= to OverdampedProcess (or any LangevinBase subclass). The simulator detects the noise model and delegates noise generation to it instead of the traditional sqrt(2D)·ξ path.

from SFI.langevin.noise import ConservedNoise

noise = ConservedNoise(sigma=0.3, grid_shape=(64, 64), dx=1.0)
proc  = OverdampedProcess(BASIS, D=noise)
proc.set_params(theta_F=theta)
proc.set_extras(extras_global=box_extras)
proc.initialize(X0)
coll = proc.simulate(dt=0.02, Nsteps=3000, key=key, oversampling=4)
class SFI.langevin.noise.CompositeNoise(*, components, n_fields)[source]

Bases: NoiseModel

Apply different noise models to different field components.

Useful when some fields have conserved dynamics (e.g. concentration) and others have non-conserved dynamics (e.g. velocity).

Parameters:
  • components (list of (NoiseModel, field_indices) pairs) – Each element specifies a noise model and the field indices it applies to. field_indices is a list of ints. Together the indices must cover range(n_fields) exactly once.

  • n_fields (int)

Example

>>> conserved = ConservedNoise(sigma=0.3, grid_shape=(64, 64), n_fields=1)
>>> white = WhiteNoise(sigma=0.1, n_fields=2)
>>> composite = CompositeNoise(
...     components=[(conserved, [0]), (white, [1, 2])],
...     n_fields=3,
... )
property dim: int
effective_D_per_site(extras)[source]

Return an approximate per-site diffusion matrix (d, d).

This is used by the inference pipeline as a pragmatic approximation when the noise is not white-in-space. For WhiteNoise(σ) this returns exactly σ·I. For ConservedNoise it returns the spatially-averaged effective variance per site.

Return type:

Array, shape (d, d)

Parameters:

extras (dict)

property n_fields: int

Number of field components per site.

property noise_kind: str

Short string tag for the noise type.

sample(key, x, extras)[source]

Draw one noise increment.

Parameters:
  • key (PRNG key)

  • x (Array, shape (P, d) or (d,)) – Current state (used only for shape; not accessed by white/conserved noise, but may be needed by multiplicative noise subclasses).

  • extras (dict) – Process extras (contains grid geometry, etc.).

Returns:

Noise increment already multiplied by sqrt(2) so the integrator applies x += dt*F + sqrt(dt) * sample(...).

Return type:

Array, same shape as x

class SFI.langevin.noise.ConservedNoise(sigma, *, grid_shape, dx=1.0, n_fields=1)[source]

Bases: NoiseModel

Conserved (divergence-form) noise on a periodic square grid.

Implements noise of the form

\[\eta(x, t) = \nabla \cdot \bigl(\sigma\, \vec{\Lambda}(x,t)\bigr)\]

where \(\vec{\Lambda}\) is spatiotemporal white vector noise. In Fourier space this is equivalent to multiplying each mode by \(|k|\):

\[\hat{\eta}_k = \sigma\,|k|\,\hat{\xi}_k\]

This noise conserves the spatial average of the field (\(\sum_i \phi_i\) is a constant of the noise process), as required by Model B / Active Model B+ dynamics.

Parameters:
  • sigma (float) – Noise amplitude (the \(\sigma\) in the equations above). This is the continuum amplitude; the grid discretisation is handled internally.

  • grid_shape (sequence of int) – Grid dimensions (Nx, Ny, ...) — must match the simulation grid.

  • dx (float or sequence of float) – Grid spacing (uniform or per-axis).

  • n_fields (int) – Number of field components per site.

Notes

The sample method uses real FFT (rfftn / irfftn) for efficiency. It draws white noise in real space, transforms to Fourier space, multiplies by \(\sigma\,|k|\,\sqrt{2/\Delta V}\) (where \(\Delta V = \prod \Delta x_\alpha\) is the cell volume), and transforms back.

The factor \(1/\sqrt{\Delta V}\) provides the correct continuum limit: the noise covariance \(\langle\eta_i\,\eta_j\rangle = -\sigma^2 \nabla^2 \delta_{ij} / \Delta V\) is independent of grid resolution when sigma is held fixed.

property dim: int
effective_D_per_site(extras)[source]

Return an approximate per-site diffusion matrix (d, d).

This is used by the inference pipeline as a pragmatic approximation when the noise is not white-in-space. For WhiteNoise(σ) this returns exactly σ·I. For ConservedNoise it returns the spatially-averaged effective variance per site.

Return type:

Array, shape (d, d)

Parameters:

extras (dict)

property grid_shape: Tuple[int, ...]
property n_fields: int

Number of field components per site.

property noise_kind: str

Short string tag for the noise type.

sample(key, x, extras)[source]

Draw one conserved-noise increment.

Steps: 1. Draw white noise ξ ~ N(0,1) on the grid, shape (Nx, Ny, …, d) 2. rFFT along spatial axes 3. Multiply by \(\sigma\,|k|\,\sqrt{2/\Delta V}\) (the _multiplier) 4. iFFT back to real space 5. Flatten back to (P, d)

The k=0 mode of _multiplier is zero, so sum_i η_i = 0 exactly.

Parameters:
  • key (Array)

  • x (Array)

  • extras (dict)

Return type:

Array

property sigma: float
class SFI.langevin.noise.NoiseModel(*, n_fields)[source]

Bases: ABC

Abstract base for noise models used by Langevin simulators.

Subclasses must implement sample() and effective_D_per_site(). The simulator calls sample(key, x, extras) once per Euler–Maruyama substep to obtain the noise increment (already scaled by sqrt(2) so that the step becomes x += dt*F + sqrt(dt)*sample(key, x, extras)).

Parameters:

n_fields (int) – Number of field components per grid site (= dim in the force contract). E.g. 1 for a scalar field, 2 for a 2-component system.

property dim: int
abstractmethod effective_D_per_site(extras)[source]

Return an approximate per-site diffusion matrix (d, d).

This is used by the inference pipeline as a pragmatic approximation when the noise is not white-in-space. For WhiteNoise(σ) this returns exactly σ·I. For ConservedNoise it returns the spatially-averaged effective variance per site.

Return type:

Array, shape (d, d)

Parameters:

extras (dict)

property n_fields: int

Number of field components per site.

property noise_kind: str

Short string tag for the noise type.

abstractmethod sample(key, x, extras)[source]

Draw one noise increment.

Parameters:
  • key (PRNG key)

  • x (Array, shape (P, d) or (d,)) – Current state (used only for shape; not accessed by white/conserved noise, but may be needed by multiplicative noise subclasses).

  • extras (dict) – Process extras (contains grid geometry, etc.).

Returns:

Noise increment already multiplied by sqrt(2) so the integrator applies x += dt*F + sqrt(dt) * sample(...).

Return type:

Array, same shape as x

class SFI.langevin.noise.WhiteNoise(sigma, *, n_fields=1)[source]

Bases: NoiseModel

Spatially uncorrelated Gaussian noise: B = sqrt(2σ) I.

Each grid site receives i.i.d. N(0, dt) noise per component. This recovers the current D = σ (scalar constant) behaviour.

Parameters:
  • sigma (float) – Scalar diffusion coefficient (the D in dx = F dt + sqrt(2D) dW).

  • n_fields (int) – Number of field components per site.

property dim: int
effective_D_per_site(extras)[source]

Return an approximate per-site diffusion matrix (d, d).

This is used by the inference pipeline as a pragmatic approximation when the noise is not white-in-space. For WhiteNoise(σ) this returns exactly σ·I. For ConservedNoise it returns the spatially-averaged effective variance per site.

Return type:

Array, shape (d, d)

Parameters:

extras (dict)

property n_fields: int

Number of field components per site.

property noise_kind: str

Short string tag for the noise type.

sample(key, x, extras)[source]

Draw one noise increment.

Parameters:
  • key (PRNG key)

  • x (Array, shape (P, d) or (d,)) – Current state (used only for shape; not accessed by white/conserved noise, but may be needed by multiplicative noise subclasses).

  • extras (dict) – Process extras (contains grid geometry, etc.).

Returns:

Noise increment already multiplied by sqrt(2) so the integrator applies x += dt*F + sqrt(dt) * sample(...).

Return type:

Array, same shape as x

property sigma: float