kups.potential.mliap.direct
¶
Factory for graph-based MLIAPs whose models output gradients directly.
Bridges a torch- or JAX-side model_fn that returns a PotentialOut
(energy + gradients + hessians) into a kUPS
Potential via
DirectPotential.
This module covers the "direct" branch only: the model produces the gradients (forces, virials, …) itself. For energy-only models that should be differentiated via JAX autodiff, construct PotentialFromEnergy directly — see tojax for that pattern.
Example
from kups.potential.mliap.direct import make_direct_mliap_potential
def my_forces_fn(inp: DirectMliapInput) -> WithPatch[PotentialOut[Array, EmptyType], IdPatch]:
energy, forces = model(inp.graph)
return WithPatch(PotentialOut(energy, -forces, EMPTY), IdPatch())
potential = make_direct_mliap_potential(my_forces_fn, ...)
DirectMliapFn
¶
Bases: Protocol
Protocol for a direct MLIAP model function.
Returns a PotentialOut that bundles energy, gradients and (optionally)
hessians for one graph input. Conventional Gradients payloads:
Array: position gradients only (∂E/∂r).PositionsAndCell: position + cell gradients (forces + stress).EmptyType: no gradients — but in that case the autodiff path (PotentialFromEnergy) is more natural; this module is for the gradient-producing case.
Source code in src/kups/potential/mliap/direct.py
filter_pullback(geometry, cotangents, gradient)
¶
Map direct (∂E/∂r, ∂E/∂h|_r) outputs to the DOF gradient ∂E/∂u.
Carrier- and codomain-agnostic: the gradient lens's set is the map
u → pose, and pulling the physical cotangent back through it with one
jax.vjp yields ∂E/∂u for any codomain U, with the cell-factor and
expm chain rule (and the atoms-ride-the-cell coupling) falling out of the
vjp. Targeting (positions, cell.vectors) makes the cotangent the raw
(n, 3) position gradient plus the raw (n_systems, 3, 3) ∂E/∂h.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometry
|
Geometry
|
The carrier's geometric view (e.g. |
required |
cotangents
|
PositionsAndCell
|
Direct outputs — |
required |
gradient
|
Lens[Geometry, U]
|
Relaxation filter |
required |
Returns:
| Type | Description |
|---|---|
U
|
|
Source code in src/kups/potential/mliap/direct.py
make_direct_mliap_potential(model_fn, particles_view, systems_view, neighborlist_view, model_view, *, patch_idx_view=None, out_cache_lens=None)
¶
Wrap a direct-gradient model_fn into a kUPS Potential.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_fn
|
DirectMliapFn[Model, Gradients, Hessians, P, S, Ptch]
|
Direct MLIAP function — see DirectMliapFn. |
required |
particles_view
|
View[State, Table[ParticleId, P]]
|
View to extract particles from state. |
required |
systems_view
|
View[State, Table[SystemId, S]]
|
View to extract systems (cell) from state. |
required |
neighborlist_view
|
View[State, NeighborList[Literal[2]]]
|
View to extract a cutoff-bound neighbor list from state. |
required |
model_view
|
View[State, Model]
|
View to extract model from state. |
required |
patch_idx_view
|
View[State, PotentialOut[Gradients, Hessians]] | None
|
View for cached output indices (optional). |
None
|
out_cache_lens
|
Lens[State, PotentialOut[Gradients, Hessians]] | None
|
Lens for output cache (optional). |
None
|
Returns:
| Type | Description |
|---|---|
Potential[State, Gradients, Hessians, Patch[State]]
|
Configured kUPS |