diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index d5206e225..d2a29466d 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -227,6 +227,10 @@ organisation on `GitHub `__. cuPotential chooses its own PME parameters and tolerance. The plugin library is added to the RPATH, so ``LD_LIBRARY_PATH`` doesn't need to be set. +* Fixed exceptions between to and from ghost atoms, e.g. 1-4 pairs through a shared + anchor, keeping a hard Coulomb interaction at intermediate lambda. Overlapping + vanishing and appearing groups could collapse onto each other and crash. + `2026.2.0 `__ - September 2026 ---------------------------------------------------------------------------------------------- diff --git a/tests/convert/test_openmm.py b/tests/convert/test_openmm.py index c5335ccea..05ce770a6 100644 --- a/tests/convert/test_openmm.py +++ b/tests/convert/test_openmm.py @@ -571,3 +571,48 @@ def get_ghost14_params(d): for key in pairs: assert params0[key][2] < params_mid[key][2] < params1[key][2] assert params1[key][2] > 0.1 + + +@pytest.mark.skipif( + "openmm" not in sr.convert.supported_formats(), + reason="openmm support is not available", +) +@pytest.mark.parametrize("fixture", ["merged_zan_ose", "ghost_14_bug"]) +def test_openmm_to_from_ghost_exceptions(fixture, openmm_platform, request): + """ + To and from ghost atoms never interact, so exceptions between them, e.g. 1-4 + pairs through a shared anchor, must have no Coulomb at any lambda. + """ + from openmm import NonbondedForce + + mols = sr.morph.link_to_reference(request.getfixturevalue(fixture)) + mol = mols.molecules("property is_perturbable")[0] + + elem0 = mol.property("element0") + elem1 = mol.property("element1") + from_ghosts = {i for i in range(mol.num_atoms()) if elem1[i].symbol() == "Xx"} + to_ghosts = {i for i in range(mol.num_atoms()) if elem0[i].symbol() == "Xx"} + + d = mol.dynamics(platform=openmm_platform) + + def nonbonded(d): + return [ + f + for f in d.context().getSystem().getForces() + if isinstance(f, NonbondedForce) + ][0] + + nb = nonbonded(d) + to_from = {frozenset((a, b)) for a in from_ghosts for b in to_ghosts} + pairs = [ + k + for k in range(nb.getNumExceptions()) + if frozenset(nb.getExceptionParameters(k)[:2]) in to_from + ] + assert len(pairs) > 0 + + for lam in (0.0, 0.5, 1.0): + d.set_lambda(lam) + nb = nonbonded(d) + for k in pairs: + assert nb.getExceptionParameters(k)[2]._value == 0.0 diff --git a/wrapper/Convert/SireOpenMM/lambdalever.cpp b/wrapper/Convert/SireOpenMM/lambdalever.cpp index f7af40fdf..32395fa68 100644 --- a/wrapper/Convert/SireOpenMM/lambdalever.cpp +++ b/wrapper/Convert/SireOpenMM/lambdalever.cpp @@ -1798,10 +1798,11 @@ double LambdaLever::setLambda(OpenMM::Context &context, // is this interaction between an to/from ghost atom? const auto to_from_ghost = (from_ghost0 and to_ghost1) or (from_ghost1 and to_ghost0); + // to/from ghosts never interact, so have no Coulomb cljff->setExceptionParameters( boost::get<0>(idxs[j]), boost::get<0>(p), boost::get<1>(p), - boost::get<2>(p) * scale, 1e-9, 1e-9); + to_from_ghost ? 0.0 : boost::get<2>(p) * scale, 1e-9, 1e-9); // exclude 14s for to/from ghost interactions if (not to_from_ghost and ghost_14ff != 0) diff --git a/wrapper/Convert/SireOpenMM/sire_to_openmm_system.cpp b/wrapper/Convert/SireOpenMM/sire_to_openmm_system.cpp index dac76fde4..23b6f3b3b 100644 --- a/wrapper/Convert/SireOpenMM/sire_to_openmm_system.cpp +++ b/wrapper/Convert/SireOpenMM/sire_to_openmm_system.cpp @@ -3048,17 +3048,19 @@ OpenMMMetaData SireOpenMM::sire_to_openmm_system(OpenMM::System &system, is_ring_breaking = false; is_ring_making = false; + // Whether this is a to/from ghost interaction. + auto to_from_ghost = (from_ghost_idxs.contains(atom0) and to_ghost_idxs.contains(atom1)) or + (from_ghost_idxs.contains(atom1) and to_ghost_idxs.contains(atom0)); + // don't include the LJ term, as this is calculated // elsewhere - note that we need to use 1e-9 to // make sure that OpenMM doesn't eagerly remove - // this, and cause "changed excluded atoms" warnings + // this, and cause "changed excluded atoms" warnings. + // To/from ghosts never interact, and have no ghost-14 + // term to soften their Coulomb, so it is zeroed. idx = cljff->addException(boost::get<0>(p), boost::get<1>(p), - boost::get<2>(p), 1e-9, - 1e-9, true); - - // Whether this is a to/from ghost interaction. - auto to_from_ghost = (from_ghost_idxs.contains(atom0) and to_ghost_idxs.contains(atom1)) or - (from_ghost_idxs.contains(atom1) and to_ghost_idxs.contains(atom0)); + to_from_ghost ? 0.0 : boost::get<2>(p), + 1e-9, 1e-9, true); // Whether a ghost-14 slot is needed is decided here, once, // for the lifetime of the OpenMM::CustomBondForce - bonds