Core API Reference¶
The opifex.core package provides the fundamental abstractions and interfaces for the Opifex framework.
Problems¶
The Problem interface is defined as a Protocol, serving as the unified contract for all scientific machine learning problems.
Problem Protocol¶
opifex.core.problems.Problem
¶
PDE Problems¶
opifex.core.problems.PDEProblem
¶
PDEProblem(geometry: Geometry, equation: Callable, boundary_conditions: dict[str, Any] | list[Any], initial_conditions: dict[str, Any] | list[Any] | None = None, parameters: dict[str, float] | None = None, time_dependent: bool = False)
Bases: ABC
Base class for Partial Differential Equation problems.
This class provides the foundation for defining PDE problems that can be solved using Physics-Informed Neural Networks (PINNs) or Neural Operators.
Source code in opifex/core/problems.py
ODE Problems¶
opifex.core.problems.ODEProblem
¶
ODEProblem(time_span: tuple[float, float], equation: Callable, initial_conditions: dict[str, float | Array] | None = None, boundary_conditions: dict[str, Any] | None = None, parameters: dict[str, float] | None = None)
Bases: ABC
Base class for Ordinary Differential Equation problems.
Supports both initial value problems (IVPs) and boundary value problems (BVPs).
Source code in opifex/core/problems.py
Optimization Problems¶
opifex.core.problems.OptimizationProblem
¶
OptimizationProblem(dimension: int, bounds: list[tuple[float, float]] | None = None, constraints: list[Callable] | None = None, parameters: dict[str, Any] | None = None)
Bases: ABC
Base class for optimization problems.
Supports both constrained and unconstrained optimization, with support for learn-to-optimize (L2O) applications.
Source code in opifex/core/problems.py
Quantum Problems¶
opifex.core.problems.QuantumProblem
¶
QuantumProblem(molecular_system: MolecularSystem, method: str = 'neural_dft', convergence_threshold: float = 1e-08, max_iterations: int = 100, parameters: dict[str, Any] | None = None)
Bases: ABC
Base class for quantum mechanical problems.
This class provides the foundation for quantum mechanical calculations, including electronic structure, molecular dynamics, and quantum chemistry.
Source code in opifex/core/problems.py
get_geometry
¶
get_domain
¶
Get quantum mechanical domain.
Source code in opifex/core/problems.py
get_parameters
¶
Get quantum mechanical parameters.
compute_energy
abstractmethod
¶
opifex.core.problems.ElectronicStructureProblem
¶
ElectronicStructureProblem(molecular_system: MolecularSystem, functional_type: str = 'neural_xc', scf_method: str = 'neural_scf', grid_level: int = 3, neural_functional_path: str | None = None, boundary_conditions: list[Any] | None = None, constraints: list[Any] | None = None, **kwargs)
Bases: QuantumProblem
Electronic structure calculation problem for Neural DFT.
This class specifically handles electronic structure calculations using neural density functional theory, including neural exchange-correlation functionals and ML-accelerated SCF methods.
Source code in opifex/core/problems.py
scf_solver
property
¶
scf_solver: SCFSolver
The restricted Kohn-Sham SCF solver backing this problem.
Built lazily (and cached) from the problem's :class:MolecularSystem
and the requested functional, then reused for the energy and the
analytic forces so both come from one self-consistent solve.
get_parameters
¶
Get Neural DFT specific parameters.
Source code in opifex/core/problems.py
validate
¶
validate() -> bool
Validate Neural DFT problem definition.
Source code in opifex/core/problems.py
compute_energy
¶
Compute the converged Kohn-Sham total energy of the system.
Delegates to :meth:_energy_from_positions -- the same differentiable
routine that :meth:compute_forces differentiates -- so the energy and
the forces are mutually consistent (F = -dE/dR) and the public
quantum API is jit / grad / vmap compatible. A concrete
Python float is returned in eager execution; under JAX transforms
the traced array is returned instead (calling float() on a tracer
raises a TypeError subclass, which selects the array branch).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
density
|
Array | None
|
Ignored. The restricted Kohn-Sham SCF solves for the
self-consistent density itself; the argument is retained only
for interface parity with :class: |
None
|
Returns:
| Type | Description |
|---|---|
float | Array
|
Total Kohn-Sham energy in Hartree. |
Source code in opifex/core/problems.py
compute_forces
¶
Analytic nuclear forces :math:F = -\partial E/\partial R.
The exact implicit-differentiation forces of the converged Kohn-Sham
energy (:meth:~opifex.neural.quantum.dft.SCFSolver.compute_forces).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
density
|
Array | None
|
Ignored (the SCF solves for the density); see
:meth: |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Forces on nuclei in Hartree/Bohr [Shape: (n_atoms, 3)]. |
Source code in opifex/core/problems.py
setup_neural_functional
¶
Setup neural exchange-correlation functional.
Source code in opifex/core/problems.py
setup_scf_cycle
¶
Setup Self-Consistent Field cycle parameters.
Source code in opifex/core/problems.py
Factory Functions¶
Create an electronic-structure (Kohn-Sham DFT) problem instance.
The returned :class:ElectronicStructureProblem computes its energy and
analytic forces through the real restricted Kohn-Sham SCF solver
(:class:~opifex.neural.quantum.dft.SCFSolver); functional_type
selects the exchange-correlation functional (pbe_* requests use the PBE
GGA, all others use the LDA).
Boundary Conditions¶
Base Classes¶
Bases: ABC
Abstract base class for boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boundary
|
str
|
Boundary identifier (e.g., "left", "right", "top", "bottom") |
required |
time_dependent
|
bool
|
Whether condition varies with time |
False
|
spatial_dependent
|
bool
|
Whether condition varies with spatial position |
True
|
evaluate
abstractmethod
¶
Evaluate boundary condition at given position and time.
apply
¶
apply(params: Array, x: Array | None = None, t: float = 0.0, weight: float = 1.0, **kwargs: Any) -> Array
Apply this boundary condition to a parameter array.
The default is the identity: boundary conditions that do not mask the
raw parameter vector (e.g. :class:WavefunctionBC) leave it unchanged.
DirichletBC/NeumannBC/RobinBC override this to constrain the
relevant entries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Array
|
Parameter array to constrain. |
required |
x
|
Array | None
|
Optional spatial coordinates for function evaluation. |
None
|
t
|
float
|
Time for time-dependent conditions. |
0.0
|
weight
|
float
|
Scaling applied by overriding conditions. |
1.0
|
**kwargs
|
Any
|
Accepted for a uniform call signature across conditions. |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The (possibly constrained) parameter array. |
Initial condition specification for time-dependent problems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float | Callable[[Array], Array]
|
Constant value or function defining initial condition |
required |
dimension
|
int
|
Dimension of the solution field |
1
|
derivative_order
|
int
|
Order of time derivative (0=position, 1=velocity, etc.) |
0
|
name
|
str | None
|
Optional name for the initial condition |
None
|
Classical Boundary Conditions¶
Bases: BoundaryCondition
Dirichlet boundary condition: u = g on boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boundary
|
str
|
Boundary identifier |
required |
value
|
float | Callable[..., Array]
|
Constant value or function g(x) or g(x, t) for boundary |
required |
time_dependent
|
bool
|
Whether condition varies with time |
False
|
evaluate
¶
Evaluate Dirichlet condition at given position and time.
apply
¶
apply(params: Array, x: Array | None = None, t: float = 0.0, weight: float = 1.0, **kwargs: Any) -> Array
Apply Dirichlet boundary condition to parameters.
This method bridges the OOP boundary specification with the functional boundary application system, allowing BC objects to apply themselves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Array
|
Parameter array to constrain |
required |
x
|
Array | None
|
Optional spatial coordinates for function evaluation |
None
|
t
|
float
|
Time for time-dependent conditions |
0.0
|
weight
|
float
|
Constraint weight (0-1, default 1.0) |
1.0
|
**kwargs
|
Any
|
Additional arguments (left_boundary, right_boundary) |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
Parameters with Dirichlet boundary condition applied |
Examples:
Bases: BoundaryCondition
Neumann boundary condition: du/dn = g on boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boundary
|
str
|
Boundary identifier |
required |
value
|
float | Callable[..., Array]
|
Constant value or function g(x) or g(x, t) for normal derivative |
required |
time_dependent
|
bool
|
Whether condition varies with time |
False
|
evaluate
¶
Evaluate Neumann condition at given position and time.
apply
¶
apply(params: Array, x: Array | None = None, t: float = 0.0, weight: float = 1.0, **kwargs: Any) -> Array
Apply Neumann boundary condition to parameters.
This method bridges the OOP boundary specification with the functional boundary application system, allowing BC objects to apply themselves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Array
|
Parameter array to constrain |
required |
x
|
Array | None
|
Optional spatial coordinates (unused for Neumann, kept for interface consistency) |
None
|
t
|
float
|
Time for time-dependent conditions (unused for Neumann, kept for interface consistency) |
0.0
|
weight
|
float
|
Constraint weight (0-1, default 1.0) |
1.0
|
Returns:
| Type | Description |
|---|---|
Array
|
Parameters with Neumann boundary condition applied (zero derivative) |
Examples:
Bases: BoundaryCondition
Robin (mixed) boundary condition: alpha*u + beta*du/dn = gamma on boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boundary
|
str
|
Boundary identifier |
required |
alpha
|
float | Callable[..., float]
|
Coefficient for u term |
required |
beta
|
float | Callable[..., float]
|
Coefficient for du/dn term |
required |
gamma
|
float | Callable[..., Array]
|
Right-hand side function |
required |
time_dependent
|
bool
|
Whether condition varies with time |
False
|
evaluate
¶
Evaluate Robin condition coefficients and RHS at given position and time.
apply
¶
apply(params: Array, x: Array | None = None, t: float = 0.0, weight: float = 1.0, **kwargs: Any) -> Array
Apply Robin boundary condition to parameters.
This method bridges the OOP boundary specification with the functional boundary application system, allowing BC objects to apply themselves.
Robin condition: alpha*u + beta*du/dn = gamma on boundary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Array
|
Parameter array to constrain |
required |
x
|
Array | None
|
Optional spatial coordinates for function evaluation |
None
|
t
|
float
|
Time for time-dependent conditions |
0.0
|
weight
|
float
|
Constraint weight (0-1, default 1.0) |
1.0
|
Returns:
| Type | Description |
|---|---|
Array
|
Parameters with Robin boundary condition applied |
Examples:
Quantum Boundary Conditions & Constraints¶
Bases: BoundaryCondition
Quantum mechanical wavefunction boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
condition_type
|
str
|
Type of wavefunction condition ("vanishing", "normalization", "periodic", "boundary") |
required |
boundary
|
str
|
Boundary identifier for spatial boundaries |
'all'
|
value
|
complex | None
|
Value for boundary conditions (real values auto-converted to complex) |
None
|
norm_value
|
float | None
|
Normalization value for wavefunction |
None
|
Bases: Constraint
Electronic density constraints for quantum systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
constraint_type
|
str
|
Type of constraint ("conservation", "positivity", "particle_number") |
required |
n_electrons
|
int | None
|
Number of electrons for particle number conservation |
None
|
tolerance
|
float
|
Tolerance for constraint satisfaction |
1e-08
|
enforcement_method
|
str
|
Method for constraint enforcement |
'lagrange'
|
Bases: Constraint
Molecular symmetry constraints for quantum systems.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point_group
|
str | None
|
Point group symmetry (e.g., "C2v", "D2h") |
None
|
operations
|
Sequence[str] | None
|
List of symmetry operations |
None
|
symmetry_type
|
str
|
Type of symmetry ("point_group", "translational") |
'point_group'
|
lattice_vectors
|
Array | None
|
Lattice vectors for periodic systems |
None
|
enforce_in_loss
|
bool
|
Whether to enforce symmetry in loss function |
True
|
Collections¶
Collection and management of boundary conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boundary_conditions
|
Sequence[BoundaryCondition]
|
List of boundary conditions |
required |
get_boundary_condition
¶
get_boundary_condition(boundary: str) -> BoundaryCondition | None
Get boundary condition for specific boundary.
get_by_type
¶
get_by_type(condition_type: str) -> list[BoundaryCondition]
Get all boundary conditions of specific type.
remove_condition
¶
Remove boundary condition for specific boundary.
apply_all
¶
Apply all boundary conditions in collection to parameters.
This method sequentially applies each boundary condition in the collection, allowing multiple BCs to be enforced together.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Array
|
Parameter array to constrain |
required |
x
|
Array | None
|
Optional spatial coordinates for function evaluation |
None
|
t
|
float
|
Time for time-dependent conditions |
0.0
|
weight
|
float
|
Global constraint weight applied to all BCs (0-1, default 1.0) |
1.0
|
Returns:
| Type | Description |
|---|---|
Array
|
Parameters with all boundary conditions applied |
Examples: