Engine oracles for saddle and path searches¶
Choose an oracle¶
A GP-accelerated nudged elastic band (GP-NEB) or dimer search needs an energy and Cartesian forces at each requested geometry. Python adapters let the search use an engine instance or calculator from your script.
Adapter |
Input supplied by your script |
|---|---|
|
ASE atoms and a calculator |
|
A live LAMMPS instance with its potential configured |
|
An OpenMM Context with a System and Integrator |
|
A function returning GROMACS-unit energy and forces |
|
An oracle and PLUMED bias instructions |
The adapters live in gpridge.integrations. Install the selected
engine’s Python interface in the environment where GPRidge runs. Engine
imports occur when their adapters are constructed, so the small analytic
example needs only the base package and NumPy.
Check the oracle convention¶
Positions use Å, energies use eV and forces use eV/Å. A gradient is
dE/dR; a force is its negative. Atom i occupies components 3i,
3i+1 and 3i+2 in a flat coordinate array, or row i in an (N, 3)
array. Cells contain the three lattice vectors as rows.
Run the supplied two-atom spring example:
python -m gpridge.integrations.examples.harmonic_oracle
It prints these values and exits successfully when they agree with the analytic spring:
energy_eV=0.0625
gradient_atom0_eV_per_Angstrom=0.0 0.0 0.0
gradient_atom1_eV_per_Angstrom=0.5 0.0 0.0
Atom 0 is fixed at the origin. Atom 1 is at 1.25 Å and the spring has
rest length 1 Šand stiffness 2 eV/Ų. Its 0.25 Šextension gives
E = 0.5 * 2 * 0.25² = 0.0625 eV and dE/dx = 0.5 eV/Å.
Each adapter’s evaluate(positions) returns an Evaluation with
energy_eV, forces_eV_per_A and gradient_eV_per_A. Fixed atoms return
to their reference positions before evaluation, and their force rows are
zero. The adapter owns a fixed cell and periodicity; a conflicting cell
raises GeometryError. The caller’s input positions remain unchanged.
Connect an ASE calculator¶
This example wraps ASE’s effective medium theory (EMT) calculator for a Cu pair. It checks an energy and force evaluation; the dimer tutorial supplies a potential with a saddle for a complete search.
from ase import Atoms
from ase.calculators.emt import EMT
from gpridge.integrations import ASEOracle
atoms = Atoms("Cu2", positions=[[0, 0, 0], [2.5, 0, 0]])
with ASEOracle.from_calculator(atoms, EMT()) as oracle:
result = oracle.evaluate(atoms.positions)
print(result.energy_eV, result.forces_eV_per_A)
The energy is in eV and the two force rows are in eV/Å. The adapter takes
a snapshot of the atoms at construction. FixAtoms constraints select
whole frozen atoms. Other constraint types require a different coordinate
representation and are rejected.
For a GP-NEB or GPDimer driver, pass oracle.as_calculator() as its
oracle argument. Apply the same FixAtoms constraint to the ASE atoms
used by the search. The calculator shares one engine evaluation between
energy and force requests at the same geometry and checks the atom order,
cell, periodicity and open state on every call.
Reuse a LAMMPS instance¶
Configure the LAMMPS potential before constructing the adapter. The
atom_ids sequence maps GPRidge’s atom order to LAMMPS tags; supply the
same order in atomic_numbers and reference positions. The sequence
covers every atom in the instance. Tags must fit a signed 32-bit integer.
The following code continues a script with a configured instance and
an atomic_numbers sequence in its atom-ID order:
from gpridge.integrations import LammpsOracle
oracle = LammpsOracle.from_instance(
instance,
atom_ids=list(range(1, len(atomic_numbers) + 1)),
atomic_numbers=atomic_numbers,
units="metal",
)
result = oracle.evaluate(oracle.reference_positions_A)
print(result.energy_eV, result.gradient_eV_per_A)
oracle.close()
instance.close()
Each evaluation scatters coordinates in that order, runs run 0 post no,
and gathers the potential energy and forces. Select the instance’s actual
unit style: metal uses Å and eV, while real uses Å and kcal/mol. The
adapter converts real energies and forces to eV and eV/Å. Other unit
styles are rejected. LAMMPS subset scatter/gather requires compatible
atom tags, so use the standard tag-integer build rather than BIGBIG.
The caller owns the instance by default. Set owns=True when closing the
adapter should also close the LAMMPS instance. The adapter records the
cell and periodic axes at construction; retain that box while it is used.
Reuse an OpenMM context¶
Create an OpenMM System, Integrator and Context with the intended
potential and initial positions. Supply the particle order as atomic
numbers. This fragment continues that setup:
from gpridge.integrations import OpenMMOracle
with OpenMMOracle.from_context(context, atomic_numbers) as oracle:
result = oracle.evaluate(oracle.reference_positions_A)
print(result.energy_eV, result.forces_eV_per_A)
The adapter converts Å to nm before Context.setPositions, recomputes
virtual-site positions with computeVirtualSites, then reads energy and
forces with getState(energy=True, forces=True). OpenMM’s kJ/mol and
kJ/mol/nm outputs become eV and eV/Å. Particle masses are read in daltons.
A zero-mass particle joins the frozen-atom mask; a virtual site’s position
follows its parents and OpenMM distributes its forces to those parents.
Holonomic constraints remain part of the caller’s coordinate treatment;
this path evaluates the supplied Cartesian positions. The caller retains
the context for the adapter’s lifetime.
Convert GROMACS quantities¶
GromacsQuantityOracle wraps an evaluator supplied by your script. The
evaluator receives positions and an optional cell in nm and returns an
energy in kJ/mol and forces in kJ/mol/nm. A workflow using
mdrun -rerun must
request force output and extract the forces alongside the energy.
from gpridge.integrations import GromacsQuantityOracle
oracle = GromacsQuantityOracle(
evaluate,
atomic_numbers=atomic_numbers,
reference_positions_A=reference_angstrom,
)
result = oracle.evaluate(positions_angstrom)
print(result.energy_eV, result.gradient_eV_per_A)
Here evaluate is your GROMACS-unit function, and both position arrays
use the same atom order. Add cell_A and pbc for a periodic calculation.
The adapter provides quantity conversion; the script supplies the force
evaluation and manages its GROMACS input and output files.
Add a static PLUMED restraint¶
PlumedBias adds bias energy and force to an existing oracle. For example,
this restraint acts on the distance between atoms 1 and 2 in PLUMED’s
one-based indexing. masses_amu contains one mass per atom; positions and
restraint parameters use Å and eV.
from gpridge.integrations import PlumedBias
with PlumedBias.from_input(
oracle,
["d: DISTANCE ATOMS=1,2", "RESTRAINT ARG=d AT=1.0 KAPPA=1.0"],
masses_amu=masses_amu,
timestep_ps=0.0,
kT_eV=0.0,
input_units="host",
) as biased:
result = biased.evaluate(positions_angstrom)
print(result.energy_eV, result.gradient_eV_per_A)
The wrapper converts its host buffers in eV, Å and picoseconds through
PLUMED’s unit interface. input_units”host”= also declares those units
for action parameters, so the example uses a 1 Å target and stiffness
1 eV/Ų. The default input_units”plumed”= reads action parameters in
PLUMED’s nm, kJ/mol and ps convention. Caller-supplied UNITS actions are
rejected so that the selected convention applies throughout the input. Fixed-atom force components
are cleared after adding the bias. Closing this wrapper finalizes its
owned PLUMED kernel; the wrapped oracle stays open by default.
On a cluster, load PLUMED and its matching Python module before starting
the workflow. An EasyBuild installation can place the binding under
$EBROOTPLUMED/lib/plumed/python. When that directory contains the binding
for the loaded Python version, add it to the job’s PYTHONPATH. Use the
existing installation rather than building another environment in shared
scratch. The HPC guide covers toolchains and allocation storage.
Use a static bias when searching a fixed potential energy surface. A
history-dependent bias changes that surface as it evolves. The default
advance_step=False keeps the construction step; advance_step=True
increments it after each successful evaluation.
Errors and ownership¶
Use one thread at a time per oracle. close() is idempotent, and a
context manager closes the oracle when its block ends. Evaluations after
closing raise LifecycleError. Failed engine calls raise
EngineEvaluationError with the engine’s message. An engine’s non-finite
energy remains non-finite for the search’s rejection handling.
as_neb_oracle() returns a Python function yielding energy and a flat
Cartesian gradient. The write_oracle and write_force helpers fill
NumPy buffers for callers that adapt C callbacks. Keep pointer-backed
arrays and callbacks alive throughout a native call; see the
C API for buffer sizes and ownership.