PDE Module API Reference¶
Complete API reference for the PDE matrix generation module.
Main Classes¶
PDEMatrixGenerator¶
- class matrix_toolkit.pde.PDEMatrixGenerator(equation_type: str, config: PDEConfig)[source]¶
Bases:
objectMain interface for generating PDE matrices
Examples
>>> from matrix_toolkit.pde import PDEMatrixGenerator, PDEConfig >>> >>> # 2D Poisson equation >>> config = PDEConfig( ... dimension=2, ... mesh_size=64, ... backend='scipy', ... format='csr' ... ) >>> >>> gen = PDEMatrixGenerator('poisson', config) >>> A = gen.generate() >>> print(A.shape) # (4096, 4096)
Supported Equation Types:
Classical:
poisson,heat,wave,helmholtz,biharmonicFluid:
stokes,navier_stokes,burgers,advection_diffusionSolid:
elasticityEM:
maxwellReaction:
reaction_diffusion,fisher_kpp,gray_scottQuantum:
schrodinger,klein_gordon
- __init__(equation_type: str, config: PDEConfig)[source]¶
Initialize PDE matrix generator
- Parameters:
equation_type – Type of equation (‘poisson’, ‘heat’, ‘wave’, etc.)
config – PDE configuration
- generate(n_matrices: int = 1, randomize: bool = None) Any | List[Any][source]¶
Generate PDE matrix/matrices
- Parameters:
n_matrices – Number of matrices to generate
randomize – Whether to randomize parameters (overrides config)
- Returns:
Single matrix if n_matrices=1, otherwise list of matrices
- generate_batch(n_matrices: int, vary_params: List[str] = None, **param_ranges) List[Any][source]¶
Generate batch of matrices with varying parameters
- Parameters:
n_matrices – Number of matrices to generate
vary_params – Which parameters to vary
**param_ranges – Parameter ranges for randomization
- Returns:
List of matrices
Examples
>>> gen.generate_batch( ... n_matrices=10, ... vary_params=['diffusion_coeff'], ... diffusion_coeff=(0.1, 10.0) ... )
PDEConfig¶
- class matrix_toolkit.pde.PDEConfig(dimension: int = 2, domain: Tuple[float, ...]=(0.0, 1.0), mesh_size: int | ~typing.Tuple[int, ...]=32, mesh_type: str = 'uniform', discretization: DiscretizationType = DiscretizationType.FINITE_DIFFERENCE, stencil_order: StencilType = StencilType.SECOND_ORDER, boundary_condition: BoundaryCondition | List[BoundaryCondition] = BoundaryCondition.DIRICHLET, coefficients: Dict[str, ~typing.Any]=<factory>, random_params: bool = False, random_seed: int | None = None, param_ranges: Dict[str, ~typing.Tuple[float, float]]=<factory>, backend: str = 'scipy', format: str = 'csr', dtype: str = 'float64', include_rhs: bool = False, include_exact_solution: bool = False, normalize: bool = False)[source]¶
Bases:
objectConfiguration for PDE matrix generation
- discretization: DiscretizationType = 'fd'¶
- stencil_order: StencilType = 2¶
- boundary_condition: BoundaryCondition | List[BoundaryCondition] = 'dirichlet'¶
- __init__(dimension: int = 2, domain: Tuple[float, ...]=(0.0, 1.0), mesh_size: int | ~typing.Tuple[int, ...]=32, mesh_type: str = 'uniform', discretization: DiscretizationType = DiscretizationType.FINITE_DIFFERENCE, stencil_order: StencilType = StencilType.SECOND_ORDER, boundary_condition: BoundaryCondition | List[BoundaryCondition] = BoundaryCondition.DIRICHLET, coefficients: Dict[str, ~typing.Any]=<factory>, random_params: bool = False, random_seed: int | None = None, param_ranges: Dict[str, ~typing.Tuple[float, float]]=<factory>, backend: str = 'scipy', format: str = 'csr', dtype: str = 'float64', include_rhs: bool = False, include_exact_solution: bool = False, normalize: bool = False) None¶
Equation Classes¶
Classical PDEs¶
- class matrix_toolkit.pde.PoissonEquation(config: PDEConfig)[source]¶
Bases:
BasePDEEquationThe Poisson equation:
\[-\Delta u = f\]In 1D:
\[-\]rac{d^2 u}{dx^2} = f
In 2D:
\[-\left(\]rac{partial^2 u}{partial x^2} + rac{partial^2 u}{partial y^2} ight) = f
In 3D:
\[-\left(\]rac{partial^2 u}{partial x^2} + rac{partial^2 u}{partial y^2} + rac{partial^2 u}{partial z^2} ight) = f
- Examples:
>>> config = PDEConfig(dimension=2, mesh_size=64) >>> eq = PoissonEquation(config) >>> A = eq.generate_matrix()
- class matrix_toolkit.pde.HeatEquation(config: PDEConfig)[source]¶
Bases:
BasePDEEquationThe heat/diffusion equation:
\[\]rac{partial u}{partial t} = lpha abla^2 u
Time discretization using the theta-method:
:math:` heta = 0`: Forward Euler (explicit)
:math:` heta = 0.5`: Crank-Nicolson
:math:` heta = 1`: Backward Euler (implicit)
- Examples:
>>> config = PDEConfig( ... dimension=2, ... mesh_size=32, ... coefficients={'dt': 0.01, 'theta': 0.5} ... ) >>> eq = HeatEquation(config) >>> M = eq.generate_matrix() # System matrix
- class matrix_toolkit.pde.WaveEquation(config: PDEConfig)[source]¶
Bases:
BasePDEEquationThe wave equation:
\[\]rac{partial^2 u}{partial t^2} = c^2 abla^2 u
Discretized in time using finite differences:
\[\]rac{u^{n+1} - 2u^n + u^{n-1}}{Delta t^2} = c^2 abla^2 u^n
- Examples:
>>> config = PDEConfig( ... dimension=2, ... mesh_size=64, ... coefficients={'wave_speed': 1.0, 'dt': 0.01} ... ) >>> eq = WaveEquation(config) >>> M = eq.generate_matrix()
- class matrix_toolkit.pde.ConvectionDiffusionEquation(config: PDEConfig)[source]¶
Bases:
BasePDEEquationThe convection-diffusion equation:
\[-\]arepsilon abla^2 u + mathbf{v} cdot abla u = f
- Examples:
>>> config = PDEConfig( ... dimension=2, ... mesh_size=64, ... coefficients={'velocity': (1.0, 0.5), 'diffusion': 0.01} ... ) >>> eq = ConvectionDiffusionEquation(config) >>> A = eq.generate_matrix()
- class matrix_toolkit.pde.HelmholtzEquation(config: PDEConfig)[source]¶
Bases:
BasePDEEquationThe Helmholtz equation:
\[-\Delta u - k^2 u = f\]This equation arises in wave propagation, acoustics, and electromagnetics.
Examples
>>> config = PDEConfig( ... dimension=2, ... mesh_size=64, ... coefficients={'wavenumber': 10.0} ... ) >>> eq = HelmholtzEquation(config) >>> A = eq.generate_matrix()
Fluid Mechanics¶
Solid Mechanics¶
Electromagnetics¶
Reaction-Diffusion¶
Quantum Mechanics¶
Matrix Generators¶
1D Generators¶
- matrix_toolkit.pde.dim1d.generate_1d_laplacian(n: int, h: float = None, boundary: BoundaryCondition = BoundaryCondition.DIRICHLET, stencil_order: StencilType = StencilType.SECOND_ORDER) spmatrix[source]¶
Generate 1D Laplacian matrix: -d²u/dx²
- Parameters:
n – Number of interior grid points
h – Grid spacing (default: 1/(n+1))
boundary – Boundary condition type
stencil_order – Finite difference accuracy order
- Returns:
Sparse matrix in CSR format
Examples
>>> A = generate_1d_laplacian(100) >>> A.shape (100, 100)
- matrix_toolkit.pde.dim1d.generate_1d_convection_diffusion(n: int, velocity: float = 1.0, diffusion: float = 1.0, h: float = None, boundary: BoundaryCondition = BoundaryCondition.DIRICHLET) spmatrix[source]¶
Generate 1D convection-diffusion matrix: -ε d²u/dx² + v du/dx
- Parameters:
n – Number of grid points
velocity – Convection velocity (v)
diffusion – Diffusion coefficient (ε)
h – Grid spacing
boundary – Boundary condition
- Returns:
Sparse CSR matrix
- matrix_toolkit.pde.dim1d.generate_1d_heat(n: int, dt: float, diffusion: float = 1.0, h: float = None, theta: float = 0.5) spmatrix[source]¶
Generate 1D heat equation matrix: du/dt = ε d²u/dx²
Uses theta-method: implicit (θ=1), explicit (θ=0), Crank-Nicolson (θ=0.5)
- Parameters:
n – Number of grid points
dt – Time step
diffusion – Thermal diffusivity
h – Spatial grid spacing
theta – Implicitness parameter
- Returns:
M u^{n+1} = … u^n
- Return type:
System matrix M for
- matrix_toolkit.pde.dim1d.generate_1d_wave(n: int, dt: float, wave_speed: float = 1.0, h: float = None) spmatrix[source]¶
Generate 1D wave equation matrix: d²u/dt² = c² d²u/dx²
- Parameters:
n – Number of grid points
dt – Time step
wave_speed – Wave speed c
h – Grid spacing
- Returns:
System matrix M
2D Generators¶
- matrix_toolkit.pde.dim2d.generate_2d_laplacian(nx: int, ny: int, hx: float = None, hy: float = None, boundary: BoundaryCondition = BoundaryCondition.DIRICHLET) spmatrix[source]¶
Generate 2D Laplacian matrix: -(∂²u/∂x² + ∂²u/∂y²)
- Uses standard 5-point stencil:
- 1/(h²) * [-1 ]
[-1 4 -1] [ -1 ]
- Parameters:
nx – Number of grid points in x-direction
ny – Number of grid points in y-direction
hx – Grid spacing in x (default: 1/(nx+1))
hy – Grid spacing in y (default: 1/(ny+1))
boundary – Boundary condition
- Returns:
Sparse CSR matrix of size (nx*ny, nx*ny)
Examples
>>> A = generate_2d_laplacian(32, 32) >>> A.shape (1024, 1024)
- matrix_toolkit.pde.dim2d.generate_2d_convection_diffusion(nx: int, ny: int, velocity: Tuple[float, float] = (1.0, 1.0), diffusion: float = 1.0, hx: float = None, hy: float = None, boundary: BoundaryCondition = BoundaryCondition.DIRICHLET) spmatrix[source]¶
Generate 2D convection-diffusion matrix: -ε(∂²u/∂x² + ∂²u/∂y²) + vx ∂u/∂x + vy ∂u/∂y
- Parameters:
nx – Grid points in each direction
ny – Grid points in each direction
velocity – Convection velocity (vx, vy)
diffusion – Diffusion coefficient ε
hx – Grid spacings
hy – Grid spacings
boundary – Boundary condition
- Returns:
Sparse CSR matrix
- matrix_toolkit.pde.dim2d.generate_2d_heat(nx: int, ny: int, dt: float, diffusion: float = 1.0, hx: float = None, hy: float = None, theta: float = 0.5) spmatrix[source]¶
Generate 2D heat equation matrix: ∂u/∂t = ε(∂²u/∂x² + ∂²u/∂y²)
- Parameters:
nx – Grid points
ny – Grid points
dt – Time step
diffusion – Thermal diffusivity
hx – Grid spacings
hy – Grid spacings
theta – Implicitness parameter
- Returns:
System matrix M
- matrix_toolkit.pde.dim2d.generate_2d_wave(nx: int, ny: int, dt: float, wave_speed: float = 1.0, hx: float = None, hy: float = None) spmatrix[source]¶
Generate 2D wave equation matrix: ∂²u/∂t² = c²(∂²u/∂x² + ∂²u/∂y²)
- Parameters:
nx – Grid points
ny – Grid points
dt – Time step
wave_speed – Wave speed c
hx – Grid spacings
hy – Grid spacings
- Returns:
System matrix M
- matrix_toolkit.pde.dim2d.generate_2d_helmholtz(nx: int, ny: int, wavenumber: float, hx: float = None, hy: float = None) spmatrix[source]¶
Generate 2D Helmholtz matrix: -Δu - k²u
- Parameters:
nx – Grid points
ny – Grid points
wavenumber – Wave number k
hx – Grid spacings
hy – Grid spacings
- Returns:
Sparse CSR matrix
3D Generators¶
- matrix_toolkit.pde.dim3d.generate_3d_laplacian(nx: int, ny: int, nz: int, hx: float = None, hy: float = None, hz: float = None, boundary: BoundaryCondition = BoundaryCondition.DIRICHLET) spmatrix[source]¶
Generate 3D Laplacian matrix: -(∂²u/∂x² + ∂²u/∂y² + ∂²u/∂z²)
Uses standard 7-point stencil.
- Parameters:
nx – Grid points in each direction
ny – Grid points in each direction
nz – Grid points in each direction
hx – Grid spacings
hy – Grid spacings
hz – Grid spacings
boundary – Boundary condition
- Returns:
Sparse CSR matrix of size (nx*ny*nz, nx*ny*nz)
Examples
>>> A = generate_3d_laplacian(16, 16, 16) >>> A.shape (4096, 4096) >>> A.nnz # 7-point stencil 28672
- matrix_toolkit.pde.dim3d.generate_3d_convection_diffusion(nx: int, ny: int, nz: int, velocity: Tuple[float, float, float] = (1.0, 1.0, 1.0), diffusion: float = 1.0, hx: float = None, hy: float = None, hz: float = None) spmatrix[source]¶
Generate 3D convection-diffusion matrix
-ε∇²u + v·∇u
- Parameters:
nx – Grid points
ny – Grid points
nz – Grid points
velocity – Convection velocity (vx, vy, vz)
diffusion – Diffusion coefficient
hx – Grid spacings
hy – Grid spacings
hz – Grid spacings
- Returns:
Sparse CSR matrix
- matrix_toolkit.pde.dim3d.generate_3d_heat(nx: int, ny: int, nz: int, dt: float, diffusion: float = 1.0, hx: float = None, hy: float = None, hz: float = None, theta: float = 0.5) spmatrix[source]¶
Generate 3D heat equation matrix: ∂u/∂t = ε∇²u
- Parameters:
nx – Grid points
ny – Grid points
nz – Grid points
dt – Time step
diffusion – Thermal diffusivity
hx – Grid spacings
hy – Grid spacings
hz – Grid spacings
theta – Implicitness parameter
- Returns:
System matrix M
- matrix_toolkit.pde.dim3d.generate_3d_wave(nx: int, ny: int, nz: int, dt: float, wave_speed: float = 1.0, hx: float = None, hy: float = None, hz: float = None) spmatrix[source]¶
Generate 3D wave equation matrix: ∂²u/∂t² = c²∇²u
- Parameters:
nx – Grid points
ny – Grid points
nz – Grid points
dt – Time step
wave_speed – Wave speed c
hx – Grid spacings
hy – Grid spacings
hz – Grid spacings
- Returns:
System matrix M
- matrix_toolkit.pde.dim3d.generate_3d_helmholtz(nx: int, ny: int, nz: int, wavenumber: float, hx: float = None, hy: float = None, hz: float = None) spmatrix[source]¶
Generate 3D Helmholtz matrix: -∇²u - k²u
- Parameters:
nx – Grid points
ny – Grid points
nz – Grid points
wavenumber – Wave number k
hx – Grid spacings
hy – Grid spacings
hz – Grid spacings
- Returns:
Sparse CSR matrix
- matrix_toolkit.pde.dim3d.generate_3d_biharmonic(nx: int, ny: int, nz: int, hx: float = None, hy: float = None, hz: float = None) spmatrix[source]¶
Generate 3D biharmonic matrix: ∇⁴u = ∇²∇²u
- Parameters:
nx – Grid points
ny – Grid points
nz – Grid points
hx – Grid spacings
hy – Grid spacings
hz – Grid spacings
- Returns:
Sparse CSR matrix
Random Parameter Generation¶
RandomParameterGenerator¶
- class matrix_toolkit.pde.random_params.RandomParameterGenerator[source]¶
Bases:
objectGenerate random parameters for PDE problems
Examples
>>> gen = RandomParameterGenerator() >>> gen.add_parameter('diffusion', 'uniform', low=0.1, high=10.0) >>> gen.add_parameter('velocity', 'normal', mean=1.0, std=0.5) >>> params = gen.sample(n=100)
LatinHypercubeSampler¶
- class matrix_toolkit.pde.random_params.LatinHypercubeSampler[source]¶
Bases:
objectLatin Hypercube Sampling for better space coverage
Examples
>>> sampler = LatinHypercubeSampler() >>> sampler.add_parameter('diffusion', low=0.1, high=10.0) >>> sampler.add_parameter('velocity', low=-1.0, high=1.0) >>> samples = sampler.sample(n=50)
ParameterDistribution¶
Enumerations¶
BoundaryCondition¶
- class matrix_toolkit.pde.config.BoundaryCondition(value)[source]¶
Boundary condition types
Available boundary conditions:
DIRICHLET: u = 0 on boundaryNEUMANN: ∂u/∂n = 0 on boundaryPERIODIC: Periodic boundariesROBIN: αu + β∂u/∂n = 0MIXED: Different conditions on different boundaries
- DIRICHLET = 'dirichlet'¶
- NEUMANN = 'neumann'¶
- PERIODIC = 'periodic'¶
- ROBIN = 'robin'¶
- MIXED = 'mixed'¶
DiscretizationType¶
StencilType¶
Quick Reference Table¶
Equation |
String ID |
Dimensions |
Key Coefficients |
|---|---|---|---|
Poisson |
|
1D, 2D, 3D |
|
Heat |
|
1D, 2D, 3D |
|
Wave |
|
1D, 2D, 3D |
|
Helmholtz |
|
1D, 2D, 3D |
|
Stokes |
|
2D, 3D |
|
Navier-Stokes |
|
2D, 3D |
|
Burgers |
|
1D |
|
Elasticity |
|
2D, 3D |
|
Maxwell |
|
3D |
|
Schrödinger |
|
1D, 2D, 3D |
|
Klein-Gordon |
|
1D, 2D, 3D |
|
Usage Examples¶
Basic Usage¶
from matrix_toolkit.pde import PDEMatrixGenerator, PDEConfig
# Simple Poisson
config = PDEConfig(dimension=2, mesh_size=64)
gen = PDEMatrixGenerator('poisson', config)
A = gen.generate()
With Parameters¶
# Navier-Stokes with parameters
config = PDEConfig(
dimension=2,
mesh_size=64,
coefficients={
'viscosity': 0.01,
'dt': 0.01,
'velocity_x0': 1.0,
'velocity_y0': 0.0
}
)
gen = PDEMatrixGenerator('navier_stokes', config)
A = gen.generate()
Batch Generation¶
# Generate 100 matrices with random parameters
config = PDEConfig(
dimension=2,
mesh_size=32,
random_params=True,
param_ranges={'viscosity': (0.001, 0.1)}
)
gen = PDEMatrixGenerator('stokes', config)
matrices = gen.generate(n_matrices=100)
See Also¶
PDE Matrix Generation - User guide
../examples/pde_examples - Examples
../tutorials/pde_tutorial - Tutorial