anypinn.problems.pde
Boundary condition constraints for PDE problems.
BCValueFn: TypeAlias = Callable[[Tensor], Tensor]
module-attribute
A callable that maps boundary coordinates (n_pts, d) → target values (n_pts, out_dim).
PDEResidualFn: TypeAlias = Callable[[Tensor, FieldsRegistry, ParamsRegistry], Tensor]
module-attribute
A callable (x, fields, params) → residual tensor, expected to be zero at the solution.
BoundaryCondition
Pairs a boundary region sampler with a prescribed value function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampler
|
Callable[[int], Tensor]
|
Callable |
required |
value
|
BCValueFn
|
Callable |
required |
n_pts
|
int
|
Number of boundary points sampled per training step. |
100
|
Source code in src/anypinn/problems/pde.py
n_pts = n_pts
instance-attribute
sampler = sampler
instance-attribute
value = value
instance-attribute
__init__(sampler: Callable[[int], Tensor], value: BCValueFn, n_pts: int = 100)
DirichletBCConstraint
Bases: Constraint
Enforces the Dirichlet boundary condition: u(x_bc) = g(x_bc).
Minimizes weight * criterion(u(x_bc), g(x_bc)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bc
|
BoundaryCondition
|
Boundary condition (sampler + target value function). |
required |
field
|
Field
|
The neural field to enforce the condition on. |
required |
log_key
|
str
|
Key used when logging the loss value. |
'loss/bc_dirichlet'
|
weight
|
float
|
Loss term weight. |
1.0
|
Source code in src/anypinn/problems/pde.py
bc = bc
instance-attribute
field = field
instance-attribute
log_key = log_key
instance-attribute
weight = weight
instance-attribute
__init__(bc: BoundaryCondition, field: Field, log_key: str = 'loss/bc_dirichlet', weight: float = 1.0)
loss(batch: TrainingBatch, criterion: nn.Module, log: LogFn | None = None) -> Tensor
Source code in src/anypinn/problems/pde.py
NeumannBCConstraint
Bases: Constraint
Enforces the Neumann boundary condition: \(\partial u / \partial n (x_{bc}) = h(x_{bc})\).
For a rectangular domain face whose outward normal is axis-aligned with
dimension normal_dim, we have
\(\partial u / \partial n = \partial u / \partial x_{\mathrm{normal\_dim}}\).
Minimizes
weight * criterion(du_dn(x_bc), h(x_bc)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bc
|
BoundaryCondition
|
Boundary condition (sampler + target normal-derivative function). |
required |
field
|
Field
|
The neural field to enforce the condition on. |
required |
normal_dim
|
int
|
Index of the spatial dimension the boundary normal points along. |
required |
log_key
|
str
|
Key used when logging the loss value. |
'loss/bc_neumann'
|
weight
|
float
|
Loss term weight. |
1.0
|
Source code in src/anypinn/problems/pde.py
bc = bc
instance-attribute
field = field
instance-attribute
log_key = log_key
instance-attribute
normal_dim = normal_dim
instance-attribute
weight = weight
instance-attribute
__init__(bc: BoundaryCondition, field: Field, normal_dim: int, log_key: str = 'loss/bc_neumann', weight: float = 1.0)
Source code in src/anypinn/problems/pde.py
loss(batch: TrainingBatch, criterion: nn.Module, log: LogFn | None = None) -> Tensor
Source code in src/anypinn/problems/pde.py
PDEResidualConstraint
Bases: Constraint
Enforces a PDE interior residual: residual_fn(x, fields, params) ≈ 0.
Minimizes weight * criterion(residual_fn(x_coll, fields, params), 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
FieldsRegistry
|
Registry of neural fields the residual function operates on. Pass only the subset needed — other fields in the Problem are ignored. |
required |
params
|
ParamsRegistry
|
Registry of parameters the residual function uses. |
required |
residual_fn
|
PDEResidualFn
|
Callable (x, fields, params) → Tensor of residuals.
Should use |
required |
log_key
|
str
|
Key used when logging the loss value. |
'loss/pde_residual'
|
weight
|
float
|
Loss term weight. |
1.0
|
Source code in src/anypinn/problems/pde.py
fields = fields
instance-attribute
log_key = log_key
instance-attribute
params = params
instance-attribute
residual_fn = residual_fn
instance-attribute
weight = weight
instance-attribute
__init__(fields: FieldsRegistry, params: ParamsRegistry, residual_fn: PDEResidualFn, log_key: str = 'loss/pde_residual', weight: float = 1.0)
Source code in src/anypinn/problems/pde.py
loss(batch: TrainingBatch, criterion: nn.Module, log: LogFn | None = None) -> Tensor
Source code in src/anypinn/problems/pde.py
PeriodicBCConstraint
Bases: Constraint
Enforces periodic boundary conditions:
u(x_left, t) = u(x_right, t) and
∂u/∂x(x_left, t) = ∂u/∂x(x_right, t).
The two boundary samplers must produce paired points — identical coordinates in every dimension except the periodic one — so that the value- and derivative-matching losses are meaningful.
Minimizes
weight * [criterion(u_left, u_right) + criterion(du_left, du_right)].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bc_left
|
BoundaryCondition
|
Left boundary sampler (sampler + dummy value function). |
required |
bc_right
|
BoundaryCondition
|
Right boundary sampler (sampler + dummy value function). |
required |
field
|
Field
|
The neural field to enforce the condition on. |
required |
match_dim
|
int
|
Spatial dimension index for the derivative matching. |
0
|
log_key
|
str
|
Key used when logging the loss value. |
'loss/bc_periodic'
|
weight
|
float
|
Loss term weight. |
1.0
|