PDE Layer (pypielm.pde)¶
PDE building blocks: operators, collocation, and constraints.
Public surface:
from pypielm.pde import (
# Operators
gradient, laplacian, divergence, advection_term, AnalyticLaplacian,
# Domain + samplers
BoxDomain, UniformSampler, LHSSampler, AdaptiveSampler, GridSampler,
# Conditions
DirichletBC, NeumannBC, InitialCondition, PeriodicBC,
)
- pypielm.pde.gradient(u, x)[source]¶
Compute the gradient ∇u w.r.t. x via autograd.
\[\nabla u = \left(\frac{\partial u}{\partial x_1}, \ldots, \frac{\partial u}{\partial x_d}\right)\]
- pypielm.pde.laplacian(u, x)[source]¶
Compute the Laplacian Δu = Σᵢ ∂²u/∂xᵢ² via autograd.
\[\Delta u = \sum_{i=1}^{d} \frac{\partial^2 u}{\partial x_i^2}\]
- pypielm.pde.divergence(flux, x)[source]¶
Compute the divergence ∇ · F of a vector field F.
\[\nabla \cdot \mathbf{F} = \sum_{i=1}^{d} \frac{\partial F_i}{\partial x_i}\]
- pypielm.pde.advection_term(u, v, x)[source]¶
Compute the advection term v · ∇u.
\[\mathbf{v} \cdot \nabla u = \sum_{i=1}^{d} v_i \frac{\partial u}{\partial x_i}\]
- class pypielm.pde.AnalyticLaplacian(feature_map=None, input_dim=None)[source]¶
Fast Laplacian operator using precomputed analytic second derivatives.
Avoids the O(N · H · d) autograd overhead by using the analytic
d2()(orlaplacian()) method of the feature map directly.When
feature_mapis provided and exposes alaplacianmethod, that is used directly. Otherwise thed2()method is summed over all spatial dimensions.\[\Delta [H \boldsymbol{\beta}](x) = \left(\sum_{i=1}^{d} \mathbf{H}^{(2,i)}(x)\right) \boldsymbol{\beta}\]where \(\mathbf{H}^{(2,i)}\) is the second partial derivative of H w.r.t. coordinate \(x_i\).
- Parameters:
feature_map (
RandomFeatureMap|None) – ARandomFeatureMapor compatible feature map instance (must exposed2orlaplacian).input_dim (
int|None) – Number of spatial dimensions to sum over. Inferred fromfeature_map.input_dimiffeature_mapis provided.
- class pypielm.pde.BoxDomain(lb, ub)[source]¶
Axis-aligned bounding box in R^d.
- Parameters:
Example:
domain = BoxDomain(lb=[0.0, 0.0], ub=[1.0, 1.0])
- class pypielm.pde.UniformSampler(domain, n_points=1000, seed=42)[source]¶
Sample collocation points uniformly at random within a
BoxDomain.- Parameters:
- class pypielm.pde.LHSSampler(domain, n_points=1000, seed=42)[source]¶
Latin Hypercube Sampling (LHS) within a
BoxDomain.LHS ensures better space-filling than pure uniform sampling. Uses
scipy.stats.qmc.LatinHypercubewhen available; falls back to a stratified uniform sampler otherwise.- Parameters:
- class pypielm.pde.AdaptiveSampler(domain, residual_fn, n_points=1000, refine_ratio=0.5, n_candidates=10000, seed=42)[source]¶
Residual-guided adaptive collocation sampler.
Samples a large candidate set, evaluates the provided
residual_fn, and returns points concentrated in high-residual regions (toprefine_ratiofraction by residual magnitude) padded with uniform samples.- Parameters:
domain (
BoxDomain) – The spatial domain.residual_fn (
Callable[[Tensor],Tensor]) – Callablef(X: Tensor) → Tensorreturning scalar residuals of shape(N,)or(N, 1)for input of shape(N, d).n_points (
int) – Number of collocation points to return.refine_ratio (
float) – Fraction of returned points that are residual-guided (the rest are uniform samples).n_candidates (
int) – Number of candidates to evaluate before selecting.seed (
int) – Random seed.
- class pypielm.pde.GridSampler(domain, nx=64, ny=64)[source]¶
Structured Cartesian grid sampler (1D, 2D, or higher).
Useful for finite-difference baselines and structured visualisation.
- Parameters:
- class pypielm.pde.DirichletBC(boundary_fn, points, weight=1.0)[source]¶
Hard Dirichlet boundary condition: u(x) = g(x) on ∂Ω.
- Parameters:
- assemble(feature_map)[source]¶
Evaluate BC and return the linear system block.
- Parameters:
feature_map (
RandomFeatureMap) – The model’s hidden-layer feature map.- Return type:
- Returns:
WeightedLinearSystemwithH = feature_map(points),y = boundary_fn(points),weight = self.weight.
- class pypielm.pde.NeumannBC(flux_fn, normal, points, weight=1.0)[source]¶
Neumann boundary condition: ∂u/∂n = h(x) on ∂Ω.
The feature-matrix contribution uses the outward unit normal
nto form the directional derivative: ∂H/∂n = Σᵢ nᵢ · (∂H/∂xᵢ).- Parameters:
- class pypielm.pde.InitialCondition(ic_fn, points, weight=1.0)[source]¶
Initial condition: u(x, t=0) = u₀(x).
- Parameters:
- class pypielm.pde.PeriodicBC(axis, points_left, points_right, weight=1.0)[source]¶
Periodic boundary condition along a specified axis.
Pairs boundary points
x_leftandx_rightand enforces u(x_left) = u(x_right) by adding the penalty rowsH(x_left) - H(x_right)with targety = 0to the linear system.- Parameters:
axis (
int) – Axis along which periodicity is imposed (informational only; the caller is responsible for pairing points correctly).points_left (
Tensor) – Left boundary points, shape(N_bc, d).points_right (
Tensor) – Right boundary points, shape(N_bc, d).weight (
float) – Observation precision for the pairing rows.
Differential Operators¶
PDE differential operators (autograd + analytic fast paths).
Public API:
from pypielm.pde.operators import (
gradient, laplacian, divergence, advection_term, AnalyticLaplacian
)
- pypielm.pde.operators.gradient(u, x)[source]¶
Compute the gradient ∇u w.r.t. x via autograd.
\[\nabla u = \left(\frac{\partial u}{\partial x_1}, \ldots, \frac{\partial u}{\partial x_d}\right)\]
- pypielm.pde.operators.laplacian(u, x)[source]¶
Compute the Laplacian Δu = Σᵢ ∂²u/∂xᵢ² via autograd.
\[\Delta u = \sum_{i=1}^{d} \frac{\partial^2 u}{\partial x_i^2}\]
- pypielm.pde.operators.divergence(flux, x)[source]¶
Compute the divergence ∇ · F of a vector field F.
\[\nabla \cdot \mathbf{F} = \sum_{i=1}^{d} \frac{\partial F_i}{\partial x_i}\]
- pypielm.pde.operators.advection_term(u, v, x)[source]¶
Compute the advection term v · ∇u.
\[\mathbf{v} \cdot \nabla u = \sum_{i=1}^{d} v_i \frac{\partial u}{\partial x_i}\]
- class pypielm.pde.operators.AnalyticLaplacian(feature_map=None, input_dim=None)[source]¶
Bases:
objectFast Laplacian operator using precomputed analytic second derivatives.
Avoids the O(N · H · d) autograd overhead by using the analytic
d2()(orlaplacian()) method of the feature map directly.When
feature_mapis provided and exposes alaplacianmethod, that is used directly. Otherwise thed2()method is summed over all spatial dimensions.\[\Delta [H \boldsymbol{\beta}](x) = \left(\sum_{i=1}^{d} \mathbf{H}^{(2,i)}(x)\right) \boldsymbol{\beta}\]where \(\mathbf{H}^{(2,i)}\) is the second partial derivative of H w.r.t. coordinate \(x_i\).
- Parameters:
feature_map (
RandomFeatureMap|None) – ARandomFeatureMapor compatible feature map instance (must exposed2orlaplacian).input_dim (
int|None) – Number of spatial dimensions to sum over. Inferred fromfeature_map.input_dimiffeature_mapis provided.
Collocation Samplers¶
Collocation point samplers and domain descriptors.
Public API:
from pypielm.pde.collocation import (
BoxDomain, UniformSampler, LHSSampler, AdaptiveSampler, GridSampler
)
- class pypielm.pde.collocation.BoxDomain(lb, ub)[source]¶
Bases:
objectAxis-aligned bounding box in R^d.
- Parameters:
Example:
domain = BoxDomain(lb=[0.0, 0.0], ub=[1.0, 1.0])
- class pypielm.pde.collocation.UnionDomain(domains)[source]¶
Bases:
objectUnion of multiple
BoxDomainobjects.
- class pypielm.pde.collocation.UniformSampler(domain, n_points=1000, seed=42)[source]¶
Bases:
objectSample collocation points uniformly at random within a
BoxDomain.- Parameters:
- class pypielm.pde.collocation.LHSSampler(domain, n_points=1000, seed=42)[source]¶
Bases:
objectLatin Hypercube Sampling (LHS) within a
BoxDomain.LHS ensures better space-filling than pure uniform sampling. Uses
scipy.stats.qmc.LatinHypercubewhen available; falls back to a stratified uniform sampler otherwise.- Parameters:
- class pypielm.pde.collocation.AdaptiveSampler(domain, residual_fn, n_points=1000, refine_ratio=0.5, n_candidates=10000, seed=42)[source]¶
Bases:
objectResidual-guided adaptive collocation sampler.
Samples a large candidate set, evaluates the provided
residual_fn, and returns points concentrated in high-residual regions (toprefine_ratiofraction by residual magnitude) padded with uniform samples.- Parameters:
domain (
BoxDomain) – The spatial domain.residual_fn (
Callable[[Tensor],Tensor]) – Callablef(X: Tensor) → Tensorreturning scalar residuals of shape(N,)or(N, 1)for input of shape(N, d).n_points (
int) – Number of collocation points to return.refine_ratio (
float) – Fraction of returned points that are residual-guided (the rest are uniform samples).n_candidates (
int) – Number of candidates to evaluate before selecting.seed (
int) – Random seed.
Boundary & Initial Conditions¶
Boundary and initial condition helpers.
Each condition class evaluates the constraint at given boundary/IC points and
returns a WeightedLinearSystem tuple
(H_bc, y_bc, weight) that can be stacked into the global linear system
assembled during model training.
Public API:
from pypielm.pde.constraints import (
DirichletBC, NeumannBC, InitialCondition, PeriodicBC
)
- class pypielm.pde.constraints.DirichletBC(boundary_fn, points, weight=1.0)[source]¶
Bases:
objectHard Dirichlet boundary condition: u(x) = g(x) on ∂Ω.
- Parameters:
- assemble(feature_map)[source]¶
Evaluate BC and return the linear system block.
- Parameters:
feature_map (
RandomFeatureMap) – The model’s hidden-layer feature map.- Return type:
- Returns:
WeightedLinearSystemwithH = feature_map(points),y = boundary_fn(points),weight = self.weight.
- class pypielm.pde.constraints.NeumannBC(flux_fn, normal, points, weight=1.0)[source]¶
Bases:
objectNeumann boundary condition: ∂u/∂n = h(x) on ∂Ω.
The feature-matrix contribution uses the outward unit normal
nto form the directional derivative: ∂H/∂n = Σᵢ nᵢ · (∂H/∂xᵢ).- Parameters:
- class pypielm.pde.constraints.InitialCondition(ic_fn, points, weight=1.0)[source]¶
Bases:
objectInitial condition: u(x, t=0) = u₀(x).
- Parameters:
- class pypielm.pde.constraints.PeriodicBC(axis, points_left, points_right, weight=1.0)[source]¶
Bases:
objectPeriodic boundary condition along a specified axis.
Pairs boundary points
x_leftandx_rightand enforces u(x_left) = u(x_right) by adding the penalty rowsH(x_left) - H(x_right)with targety = 0to the linear system.- Parameters:
axis (
int) – Axis along which periodicity is imposed (informational only; the caller is responsible for pairing points correctly).points_left (
Tensor) – Left boundary points, shape(N_bc, d).points_right (
Tensor) – Right boundary points, shape(N_bc, d).weight (
float) – Observation precision for the pairing rows.