nvalchemi.dynamics.hooks.FreezeAtomsHook#
- class nvalchemi.dynamics.hooks.FreezeAtomsHook(frequency=1, freeze_category=AtomCategory.SPECIAL.value, mask_key=None, zero_forces=True, zero_velocities=True, stage=DynamicsStage.BEFORE_PRE_UPDATE)[source]#
Freeze selected atoms during molecular dynamics simulation.
During dynamics, certain atoms may need to remain fixed in place, such as substrate atoms in surface simulations, boundary atoms in slab models, or anchor atoms in constrained optimization. This hook identifies atoms by their
atom_categoriesfield and constrains them by:Snapshotting and clearing state — At
BEFORE_PRE_UPDATE, the hook snapshots all atomic positions and clears frozen atoms’ velocities and forces before the integrator uses them.Restoring before compute — At
AFTER_PRE_UPDATE, the hook restores frozen atoms’ positions and clears their velocities so compute-preparation hooks and the model see the constrained geometry.Clearing new state — At
BEFORE_POST_UPDATE, the hook clears forces newly computed for frozen atoms and clears their velocities before the second integrator update.Restoring positions — At
AFTER_POST_UPDATE, the hook restores positions of frozen atoms usingtorch.where, effectively undoing any displacement applied by the integrator.Zeroing velocities — Velocities of frozen atoms are set to zero to prevent momentum accumulation.
Optionally exposing forces — When
zero_forces=False, raw model forces remain available atAFTER_COMPUTEfor reporting. However, they are still cleared atBEFORE_POST_UPDATEso they cannot propagate through the integrator.
The hook fires at five stages:
BEFORE_PRE_UPDATE(to snapshot positions and clear prior state),AFTER_PRE_UPDATE(to restore the constrained geometry before compute preparation),AFTER_COMPUTE(optionally clear model forces),BEFORE_POST_UPDATE(to clear newly computed forces), andAFTER_POST_UPDATE(to restore frozen positions and clear velocities). This multi-stage design enablestorch.compile(fullgraph=True)compatibility by avoiding data-dependent branching.- Parameters:
frequency (int, optional) – Apply constraints every
frequencysteps. Default1(every step). Setting this higher than 1 is not recommended as frozen atoms will drift between constraint applications.freeze_category (int, optional) – The
atom_categoriesvalue that identifies frozen atoms. Default isAtomCategory.SPECIAL.value(-1). Atoms withbatch.atom_categories == freeze_categorywill be frozen whenmask_keyis not provided.mask_key (str, optional) – Name of a boolean node-level batch field that identifies frozen atoms. When provided, this mask is used instead of
atom_categories. This supports workflow-owned constraints such as NEB endpoint and per-path fixed-atom masks. Default isNone.zero_forces (bool, optional) – Whether to zero forces immediately at
AFTER_COMPUTE. DefaultTrue. Set toFalseto inspect raw frozen-atom forces in anAFTER_COMPUTEhook. Note that forces are always cleared beforepost_update.zero_velocities (bool, optional) – Whether to clear frozen-atom velocities around integrator updates. Default
True. Set toFalsefor optimizers that do not use velocities, such as quasi-Newton methods.stage (Enum)
- frequency#
Constraint application frequency in steps.
- Type:
int
- freeze_category#
Category value identifying frozen atoms.
- Type:
int
- mask_key#
Optional node-level boolean batch field identifying frozen atoms.
- Type:
str | None
- zero_forces#
Whether forces are zeroed immediately after compute.
- Type:
bool
- zero_velocities#
Whether velocities are cleared around integrator updates.
- Type:
bool
- stage#
Primary stage, set to
BEFORE_PRE_UPDATEfor protocol compliance.- Type:
Examples
Freeze atoms marked as SPECIAL (default):
>>> from nvalchemi.dynamics.hooks import FreezeAtomsHook >>> hook = FreezeAtomsHook() >>> dynamics = DemoDynamics(model=model, n_steps=1000, hooks=[hook]) >>> dynamics.run(batch)
Freeze bulk atoms instead:
>>> from nvalchemi._typing import AtomCategory >>> hook = FreezeAtomsHook(freeze_category=AtomCategory.BULK.value)
Keep forces for analysis:
>>> hook = FreezeAtomsHook(zero_forces=False)
Notes
Fires at
BEFORE_PRE_UPDATE(snapshot positions and clear prior forces/velocities),AFTER_PRE_UPDATE(restore positions before compute preparation),AFTER_COMPUTE(optionally expose new forces),BEFORE_POST_UPDATE(clear new forces), andAFTER_POST_UPDATE(restore positions and clear velocities).Uses
torch.wherefor branchless GPU-vectorized restore, enablingtorch.compile(fullgraph=True)compatibility.All positions are snapshotted each step (not just frozen ones) to avoid shape-dependent logic.
When using with
WrapPeriodicHook, both hooks fire atAFTER_POST_UPDATE. Registration order determines execution order; register this hook before the periodic wrapping hook to ensure frozen positions are restored before wrapping is applied.