Skip to content

Add alchemical free energies with ML intramolecular potentials - #559

Merged
lohedges merged 31 commits into
develfrom
feature_ml_fep
Dec 3, 2026
Merged

lohedges merged 31 commits into
develfrom
feature_ml_fep

Conversation

@lohedges

@lohedges lohedges commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

This PR adds interpolate="end_states" to sire.qm.create_engine and sire.qm.emle, so that the intramolecular energy of a perturbable molecule comes from an ML potential (e.g. ANI-2x, MACE, EMLE) at every λ, rather than the force field. This works for relative free energies with a merged molecule and for absolute free energies with a decoupled one. The intermolecular interactions stay MM (PME plus softcore), so the existing lambda schedules and softcore handle the alchemical transformation unchanged. Currently the QM region must be a whole perturbable molecule, so QM regions with link atoms, e.g. covalently bound ligands or mutated protein residues, aren't supported.

Usage

import sire as sr

# Mechanical embedding, with any GPU-resident callback.
mols, engine = sr.qm.create_engine(
    mols,
    mols[0],
    callback,
    mechanical_embedding=True,
    gpu_resident=True,
    interpolate="end_states",
)

# Or electrostatic embedding with an EMLE model, for both end states with their total charges.
mols, engine = sr.qm.emle(
    mols, mols[0], model, gpu_resident=True, interpolate="end_states"
)
schedule = sr.cas.LambdaSchedule.standard_morph()
sr.qm.add_embedding_stages(schedule)

d = mols.dynamics(qm_engine=engine, schedule=schedule, lambda_value=0.5)

How it works

  • Energy. The model is evaluated for both end states in one batch each step, on each state's real atoms (ghosts are masked). The energy is (1 − ml)·(E_vac,0 + embed·E_emb,0) + ml·(E_vac,1 + embed·E_emb,1), where ml and embed are new levers on the qmff force, so any schedule can shape them per stage. The energies of each state's isolated atoms are subtracted, so that the energy added to OpenMM's buffer is small enough to be accurate in single precision. These cancel between the legs of a cycle.
  • MM terms. The MM angles, torsions and intramolecular non-bonded pairs between real atoms are removed in each end state. Bonds are kept with zero force constant so that they can be constrained as in MM. Terms involving ghost atoms are kept.
  • Electrostatic embedding. Linearly mixing the embedding of two end states diverges when solvent overlaps appearing atoms, so the embedding is only switched on where the molecule is fully coupled. sr.qm.add_embedding_stages sets embed to zero in the existing stages and adds an embedding_off stage at the start and an embedding_on stage at the end, which switch between mechanical and electrostatic embedding. embed is zero by default, so an engine with electrostatic embedding needs these stages. Each end state's embedding uses that state's MM charges, which is exact because the embedding is only on in the embedding stages, where the charges are held at their end-state values. Any schedule that has embed > 0 in a stage where MM charges change would see the charges of the two end states mixed by ml, rather than the MM charges at that λ.
  • Callback contract. A GPU-resident callback for end states receives atomic numbers (2, N), MM charges (2, M), QM positions (2, N, 3), MM positions (2, M, 3), the cell and, if it accepts them, MM groups (2, M). It returns the in vacuo energies (2,), or the in vacuo energies followed by embedding rows, which are summed. This isn't specific to EMLE: any model that returns its embedding energy separately can be used.
  • Energy reuse. For energy-only evaluations with unchanged positions and box, e.g. the energies at other λ for MBAR or replica exchange, the callback recombines its cached end-state energies with the new lever values rather than running the model. With ANI-2x on ethane → methanol this cut each extra λ window from 6.0 ms to 0.4 ms, the same as without ML.

Other changes

  • sire.qm.create_engine and sire.qm.emle now raise an error if the QM atoms are in a perturbable molecule, which they previously corrupted (e.g. bond0 became bond00).
  • Constraints on the QM region no longer warn for end-state engines, and the ML molecule is constrained like an MM one rather than held rigid.
  • PyQMForce and TorchQMForce streams store the end-state data, and older streams still load.
  • The MM charges of end-state engines are switched by residue, as in Fix issue #554 #555.
  • Link atoms for QM atoms other than carbon, when the force field has no bond between that element and hydrogen, now use typical X–H bond lengths. They were previously scaled from a C–H bond by the ratio of LJ sigma values, which gave an S–H bond of 1.14 Å rather than 1.34 Å and caused large forces on the QM–MM bond.
  • A tutorial page on alchemical free energies with ML potentials.

Validation

Unit tests compare energies and forces against independent torch calculations for synthetic models, ANI-2x and ANI2xEMLE at several λ, and check constraints, energy reuse and short dynamics.

With SOMD2 (support for this will follow in a separate PR), the ethane → methanol relative hydration free energy and the absolute hydration free energies of both molecules were run with MM, ANI-2x with mechanical embedding and ANI-2x with EMLE embedding. Mechanical embedding agrees with MM in every leg. The EMLE result is about 3.5 kcal/mol more negative, which matches the embedding stages and the known over-solvation of EMLE with fixed-charge LJ and TIP3P. The EMLE thermodynamic cycle closes (−0.19 ± 0.34 kcal/mol).

@lohedges lohedges added the enhancement New feature or request label Dec 3, 2026
@lohedges
lohedges merged commit f3c068c into devel Dec 3, 2026
5 checks passed
@lohedges
lohedges deleted the feature_ml_fep branch December 3, 2026 12:22

This branch was previously deployed

1 inactive deployment
sire-build — 7b09923e Deployed Dec 3, 2026 by lohedges via build (3.13, linux) #487
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant