numra_spde/lib.rs
1// Allow some clippy lints that are prevalent in numerical code
2#![allow(clippy::assign_op_pattern)]
3#![allow(clippy::needless_range_loop)]
4#![allow(clippy::manual_abs_diff)]
5
6//! Stochastic Partial Differential Equation (SPDE) solvers for Numra.
7//!
8//! This crate provides solvers for SPDEs of the form:
9//!
10//! ```text
11//! ∂u/∂t = L[u] + σ(u) ξ(x,t)
12//! ```
13//!
14//! where L is a spatial differential operator, σ(u) is the diffusion coefficient,
15//! and ξ(x,t) is space-time white noise.
16//!
17//! # Method
18//!
19//! We use the Method of Lines (MOL) approach:
20//! 1. Discretize the spatial operator L using finite differences
21//! 2. Convert to a system of SDEs
22//! 3. Solve using SDE solvers from `numra-sde`
23//!
24//! # Example: Stochastic Heat Equation
25//!
26//! ```
27//! use numra_spde::{SpdeSystem, SpdeSolver, SpdeOptions, SpdeResult};
28//! use numra_pde::Grid1D;
29//! use numra_core::Scalar;
30//!
31//! // Stochastic heat equation: ∂T/∂t = α ∂²T/∂x² + σ dW(x,t)
32//! struct StochasticHeat {
33//! alpha: f64, // Thermal diffusivity
34//! sigma: f64, // Noise intensity
35//! }
36//!
37//! impl SpdeSystem<f64> for StochasticHeat {
38//! fn dim(&self) -> usize { 1 } // 1D spatial
39//!
40//! fn drift(&self, _t: f64, u: &[f64], du: &mut [f64], grid: &Grid1D<f64>) {
41//! let dx = grid.dx_uniform();
42//! let n = u.len();
43//! for i in 0..n {
44//! let u_left = if i == 0 { 0.0 } else { u[i - 1] }; // Dirichlet BC
45//! let u_right = if i == n - 1 { 0.0 } else { u[i + 1] }; // Dirichlet BC
46//! du[i] = self.alpha * (u_left - 2.0 * u[i] + u_right) / (dx * dx);
47//! }
48//! }
49//!
50//! fn diffusion(&self, _t: f64, u: &[f64], sigma: &mut [f64], _grid: &Grid1D<f64>) {
51//! for i in 0..u.len() {
52//! sigma[i] = self.sigma; // Additive noise
53//! }
54//! }
55//! }
56//! ```
57//!
58//! Author: Moussa Leblouba
59//! Date: 3 February 2026
60//! Modified: 2 May 2026
61
62pub use numra_core::Scalar;
63pub use numra_pde::Grid1D;
64
65mod noise;
66mod solver;
67mod system;
68
69pub use noise::{ColoredNoise, ColoredNoiseGenerator, SpaceTimeNoise, WhiteNoise};
70pub use solver::{
71 MolSdeSolver, SpdeEnsemble, SpdeMethod, SpdeOptions, SpdeResult, SpdeSolver, SpdeStats,
72};
73pub use system::{NoiseCorrelation, SpdeSystem};