Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tests/convert/test_openmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion wrapper/Convert/SireOpenMM/lambdalever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1714,10 +1714,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)
Expand Down
16 changes: 9 additions & 7 deletions wrapper/Convert/SireOpenMM/sire_to_openmm_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2859,17 +2859,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
Expand Down