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
14 changes: 8 additions & 6 deletions corelib/src/libs/SireCAS/lambdaschedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,18 +759,20 @@ void LambdaSchedule::addDecoupleStage(const QString &name, bool perturbed_is_dec
this->setEquation(name, "ghost/ghost", "*", this->initial());
this->setEquation(name, "ghost-14", "*", this->initial());

// kappa is fixed to 1.0
this->setEquation(name, "ghost/ghost", "kappa", 1.0);
this->setEquation(name, "ghost-14", "kappa", 1.0);
// stop subtracting the hard Coulomb as the charges in the
// NonbondedForce go to zero, so the intramolecular terms are kept
this->setEquation(name, "ghost/ghost", "kappa", 1.0 - this->lam());
this->setEquation(name, "ghost-14", "kappa", 1.0 - this->lam());
}
else
{
this->setEquation(name, "ghost/ghost", "*", this->final());
this->setEquation(name, "ghost-14", "*", this->final());

// kappa is fixed to 1.0
this->setEquation(name, "ghost/ghost", "kappa", 1.0);
this->setEquation(name, "ghost-14", "kappa", 1.0);
// stop subtracting the hard Coulomb as the charges in the
// NonbondedForce go to zero, so the intramolecular terms are kept
this->setEquation(name, "ghost/ghost", "kappa", this->lam());
this->setEquation(name, "ghost-14", "kappa", this->lam());
}
}

Expand Down
114 changes: 71 additions & 43 deletions src/sire/morph/_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,63 @@ def evaluate_xml_force(mols, xml, force):
nrg_coul_list = []
nrg_lj_list = []

def evaluate_bonds(bond_force):
"""Evaluate the energy of each bond in a CustomBondForce."""

# Get the energy terms.
terms = list(reversed(bond_force.get("energy").split(";")[1:-1]))

# The LJ term may be cut off, e.g. "coul_nrg+step(0.9-r)*lj_nrg".
import re

match = re.search(r"step\(([^-]+)-r\)", bond_force.get("energy").split(";")[0])
lj_cutoff = float(match.group(1)) if match else None

# Get the parameters for this force.
parameters = [p.get("name") for p in bond_force.find("PerBondParameters")]

# Get all the bond parameters.
bonds = bond_force.find("Bonds").findall("Bond")

# Get the list of atoms.
atoms = mols.atoms()

# Loop over all bonds.
for bond in bonds:
# Get the atoms involved in the bond.
atom_i = atoms[int(bond.get("p1"))]
atom_j = atoms[int(bond.get("p2"))]

# Set the parameters for this bond.
for k, param in enumerate(parameters):
setattr(module, param, float(bond.get(f"param{k + 1}")))

# Get the distance between the particles.
r = measure(atom_i, atom_j).to(nanometer)

# Evaluate the energy term by term.
for term in terms:
# Replace any instances of ^ with **.
term = term.replace("^", "**")

# Split the term into the result and the expression.
result, expression = term.split("=")

# Evaluate the expression.
setattr(module, result, eval(expression))

# Give energies units.
coul_nrg = module.coul_nrg * kJ_per_mol
lj_nrg = module.lj_nrg * kJ_per_mol

if lj_cutoff is not None and r > lj_cutoff:
lj_nrg = 0.0 * kJ_per_mol

# Append the results for this bond.
pairs.append((atom_i, atom_j))
nrg_coul_list.append(coul_nrg)
nrg_lj_list.append(lj_nrg)

# CustomNonbondedForce: ghost-ghost or ghost-nonghost.
if name != "Ghost14BondForce":
# Get the parameters for this force.
Expand All @@ -135,6 +192,8 @@ def evaluate_xml_force(mols, xml, force):
.find("Set2")
]

is_same_set = set(set1) == set(set2)

# Get the exclusions.
exclusions = [
(int(e.get("p1")), int(e.get("p2")))
Expand Down Expand Up @@ -171,8 +230,9 @@ def evaluate_xml_force(mols, xml, force):
# Get the index from set2.
j = set2[y]

# Ignore self-interaction.
if i == j:
# Ignore self-interaction, and count each pair once when
# the two sets are the same.
if i == j or (is_same_set and j < i):
continue

# Check if this pair is excluded.
Expand Down Expand Up @@ -215,49 +275,17 @@ def evaluate_xml_force(mols, xml, force):
nrg_coul_list.append(coul_nrg)
nrg_lj_list.append(lj_nrg)

# Intramolecular ghost/ghost pairs are computed without a cutoff
# in a separate CustomBondForce.
if name == "GhostGhostNonbondedForce":
for bond_force in tree.find("Forces"):
if bond_force.get("name") == "GhostGhostBondForce":
evaluate_bonds(bond_force)
break

# CustomBondForce: ghost-14.
else:
# Get the parameters for this force.
parameters = [p.get("name") for p in force.find("PerBondParameters")]

# Get all the bond parameters.
bonds = force.find("Bonds").findall("Bond")

# Get the list of atoms.
atoms = mols.atoms()

# Loop over all bonds.
for bond in bonds:
# Get the atoms involved in the bond.
atom_i = atoms[int(bond.get("p1"))]
atom_j = atoms[int(bond.get("p2"))]

# Set the parameters for this bond.
for k, param in enumerate(parameters):
setattr(module, param, float(bond.get(f"param{k + 1}")))

# Get the distance between the particles.
r = measure(atom_i, atom_j).to(nanometer)

# Evaluate the energy term by term.
for term in terms:
# Replace any instances of ^ with **.
term = term.replace("^", "**")

# Split the term into the result and the expression.
result, expression = term.split("=")

# Evaluate the expression.
setattr(module, result, eval(expression))

# Give energies units.
coul_nrg = module.coul_nrg * kJ_per_mol
lj_nrg = module.lj_nrg * kJ_per_mol

# Append the results for this bond.
pairs.append((atom_i, atom_j))
nrg_coul_list.append(coul_nrg)
nrg_lj_list.append(lj_nrg)
evaluate_bonds(force)

# Return the results.
return pairs, nrg_coul_list, nrg_lj_list
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,16 @@ def cyclopropyl_constraint_bug():
return sr.load_test_files("ejm_31_jmc_28.bss")


@pytest.fixture(scope="session")
def macrocycle_ghost_constraints():
return sr.load_test_files("macrocycle_ghost_constraints.bss")


@pytest.fixture(scope="session")
def ring_break_constraints():
return sr.load_test_files("ring_break_constraints.bss")


@pytest.fixture(scope="session")
def zero_lj_mols():
return sr.load_test_files("zero_lj.prm7", "zero_lj.rst7")
Expand Down
110 changes: 96 additions & 14 deletions tests/convert/test_openmm_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ def test_cyclopropyl_anchor_bond_constraint(
cyclopropyl_constraint_bug, openmm_platform
):
"""
Regression test for an unperturbed anchor bond (to two alternate
heavy-atom substituents) not being constrained under
"h-bonds-not-heavy-perturbed", unlike SOMD1.
Heavy-atom bonds at the alchemical junction aren't constrained under
"h-bonds-not-heavy-perturbed", since constraining them destabilises
perturbable molecules with large ghost regions. (SOMD1 constrains them.)
"""
mols = sr.morph.link_to_reference(cyclopropyl_constraint_bug)
mol = mols[0]
Expand Down Expand Up @@ -503,21 +503,49 @@ def get_anchor_constraints(d):

return found

found = get_anchor_constraints(d)
assert get_anchor_constraints(d) == {}

assert set(found.keys()) == substituent_idxs
d.set_lambda(1.0)

# both anchor bonds should have the same, static length, matching
# SOMD1's value for this generic aromatic-C/sp3-C bond
for length in found.values():
assert length == pytest.approx(1.51234, abs=1e-3)
assert get_anchor_constraints(d) == {}

# the constraint should remain static (not lambda-dependent), since the
# bond's own parameters don't change between end states
d.set_lambda(1.0)
found_at_1 = get_anchor_constraints(d)

assert found_at_1 == found
@pytest.mark.skipif(
"openmm" not in sr.convert.supported_formats(),
reason="openmm support is not available",
)
@pytest.mark.slow
@pytest.mark.parametrize("lambda_value", [0.0, 1.0])
def test_macrocycle_ghost_constraints(
macrocycle_ghost_constraints, openmm_platform, lambda_value
):
"""
A partially mapped macrocycle (OpenFE bayer_macrocycles brd4, 61a~25e),
solvated, with large ghost regions. This blew up within a few steps when
heavy-atom bonds at the alchemical junction were constrained.
"""
if lambda_value == 0.0:
mols = sr.morph.link_to_reference(macrocycle_ghost_constraints)
else:
mols = sr.morph.link_to_perturbed(macrocycle_ghost_constraints)

mols = (
mols.minimisation(lambda_value=lambda_value, platform=openmm_platform)
.run()
.commit()
)

d = mols.dynamics(
timestep="2 fs",
lambda_value=lambda_value,
constraint="h-bonds",
perturbable_constraint="h-bonds-not-heavy-perturbed",
platform=openmm_platform,
)

d.run("1 ps", save_frequency=0, auto_fix_minimise=False)

assert d.current_potential_energy().value() < 0


@pytest.mark.skipif(
Expand Down Expand Up @@ -591,3 +619,57 @@ def test_asymmetric_constraints(merged_ethane_methanol):

# Check the minimised potential energies are the same. (Post constraint projection.)
assert isclose(nrg_forwards, nrg_backwards, rel_tol=1e-4)


@pytest.mark.skipif(
"openmm" not in sr.convert.supported_formats(),
reason="openmm support is not available",
)
@pytest.mark.slow
@pytest.mark.parametrize("lambda_value", [0.0, 1.0])
def test_ring_break_constraints(ring_break_constraints, openmm_platform, lambda_value):
"""
The bond broken by a ring-breaking perturbation (OpenFE scaffold_hopping
chk1, 1~17) is never constrained. One of its atoms becomes a hydrogen, so it
used to be constrained at lambda = 1, where it no longer exists, which blew up.
"""
pert = ring_break_constraints.molecules("property is_perturbable")[0]

bonds = [
{
tuple(sorted((b.atom0().value(), b.atom1().value())))
for b in pert.property(f"connectivity{i}").get_bonds()
}
for i in (0, 1)
]
broken = bonds[0] ^ bonds[1]
assert len(broken) == 1

if lambda_value == 0.0:
mols = sr.morph.link_to_reference(ring_break_constraints)
else:
mols = sr.morph.link_to_perturbed(ring_break_constraints)

mols = (
mols.minimisation(lambda_value=lambda_value, platform=openmm_platform)
.run()
.commit()
)

d = mols.dynamics(
timestep="1 fs",
lambda_value=lambda_value,
constraint="h-bonds",
perturbable_constraint="h-bonds-not-heavy-perturbed",
platform=openmm_platform,
)

constrained = {
tuple(sorted((c[0][0].index().value(), c[0][1].index().value())))
for c in d.get_constraints()
if c[0][0].molecule().number() == pert.number()
}

assert not (broken & constrained)

d.run("1 ps", save_frequency=0, auto_fix_minimise=False)
Loading