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
4 changes: 4 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ organisation on `GitHub <https://github.com/openbiosim/sire>`__.
* Fixed ``sire.restraints.boresch_search()`` not rejecting candidates whose r1-r2-r3 or
l1-l2-l3 anchor angles are near collinear, which leaves the phiA or phiC dihedral undefined.

* Allow QM atoms with more than one bond to the MM region, adding a link atom for each
QM-MM bond. A warning is raised, since multiple link atoms on the same QM atom are not
recommended.

`2026.2.0 <https://github.com/openbiosim/sire/compare/2026.1.0...2026.2.0>`__ - September 2026
----------------------------------------------------------------------------------------------

Expand Down
47 changes: 27 additions & 20 deletions src/sire/qm/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ def _get_link_atoms(mols, qm_mol_to_atoms, map):
editor.connect(bond.atom0(), bond.atom1())
connectivity = editor.commit()

# Dictionary to store the MM1 atoms.
mm1_atoms = {}
# List of (QM, MM1) atom index pairs, one for each link atom.
mm1_atoms = []

# Loop over the QM atoms and find any MM atoms that are bonded to them.
for atom in qm_atoms:
Expand All @@ -370,34 +370,41 @@ def _get_link_atoms(mols, qm_mol_to_atoms, map):

# Store the list of MM atoms.
if len(mm_atoms) > 0:
abs_idx = mols.atoms().find(atom)

# If the element is hydrogen, raise an exception.
if elem == hydrogen:
raise Exception(
"Attempting replace a hydrogen with a link atom "
f"(atom index {abs_idx})!"
)

# Warn if multiple link atoms will be added to this QM atom.
if len(mm_atoms) > 1:
raise Exception(f"QM atom {idx} has more than one MM bond!")
else:
_warnings.warn(
f"QM atom (index {abs_idx}) is bonded to {len(mm_atoms)} "
"MM atoms. Adding multiple link atoms to the same QM atom "
"is not recommended!"
)

for mm_atom in mm_atoms:
# Get the element of the cut atom.
link_elem = qm_mol[mm_atoms[0]].property(elem_prop)

# If the element is hydrogen, raise an exception.
if elem == hydrogen:
abs_idx = mols.atoms().find(atom)
raise Exception(
"Attempting replace a hydrogen with a link atom "
f"(atom index {abs_idx})!"
)
link_elem = qm_mol[mm_atom].property(elem_prop)

# Warn if the link atom is not for a carbon-carbon bond.
if elem != carbon or link_elem != carbon:
abs_idx = mols.atoms().find(atom)
mm1_abs_idx = mols.atoms().find(qm_mol.atoms()[mm_atom])
_warnings.warn(
"Attempting to add a link atom for a non carbon-carbon "
f"bond (atom index {abs_idx})!"
f"bond (atom indices {abs_idx} and {mm1_abs_idx})!"
)

# Store the link (MM1) atom.
mm1_atoms[idx] = mm_atoms[0]
mm1_atoms.append((idx, mm_atom))

# Now work out the MM atoms that are bonded to the link atoms. (MM2 atoms.)
mm2_atoms = {}
for qm_idx, mm1_idx in mm1_atoms.items():
for qm_idx, mm1_idx in mm1_atoms:
if mm1_idx not in mm2_atoms:
bonds = connectivity.get_bonds(mm1_idx)
mm_bonds = []
Expand Down Expand Up @@ -432,7 +439,7 @@ def _get_link_atoms(mols, qm_mol_to_atoms, map):

# Convert MM1 to QM atom dictionary to absolute indices.
mm1_to_qm_local = {}
for k, v in mm1_atoms.items():
for k, v in mm1_atoms:
qm_idx = mols.atoms().find(qm_mol.atoms()[k])
link_idx = mols.atoms().find(qm_mol.atoms()[v])
# Make sure that we haven't assigned this link atom already.
Expand All @@ -451,7 +458,7 @@ def _get_link_atoms(mols, qm_mol_to_atoms, map):
mm1_to_mm2_local[link_idx] = mm_idx

# Store the MM1 atom indices.
mm1_indices.append(list(mm1_atoms.values()))
mm1_indices.append([mm1_idx for _, mm1_idx in mm1_atoms])

# Now work out the QM-MM1 bond distances based on the equilibrium
# MM bond lengths.
Expand All @@ -467,7 +474,7 @@ def _get_link_atoms(mols, qm_mol_to_atoms, map):
r = _CAS.Symbol("r")

# Loop over the link atoms.
for qm_idx, mm1_idx in mm1_atoms.items():
for qm_idx, mm1_idx in mm1_atoms:
# Get the QM and MM1 atoms.
qm_atom = qm_mol[qm_idx]
mm1_atom = qm_mol[mm1_idx]
Expand Down
62 changes: 52 additions & 10 deletions tests/qm/test_qm.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def callback(a, b, c, d, e=None, f=None):
{14: 0.7565543071161049},
),
),
(
# QM atom 8 is bonded to MM1 atoms 6 and 10.
"atomidx 8,9 or atomidx 14:22",
(
{6: 8, 10: 8},
{6: [4, 7], 10: [11, 12, 13]},
{6: 0.7522429261559697, 10: 0.7142857142857143},
[[6, 10]],
),
),
],
)
def test_link_atoms(ala_mols, selection, expected):
Expand Down Expand Up @@ -128,6 +138,11 @@ def test_link_atoms(ala_mols, selection, expected):
assert mm1_to_mm2 == expected[1]
assert bond_scale_factors == expected[2]

if len(expected) > 3:
assert [
sorted(idx.value() for idx in idxs) for idxs in mm1_indices
] == expected[3]


def test_link_atoms_non_carbon():
"""
Expand Down Expand Up @@ -212,6 +227,21 @@ def test_link_atoms_unsupported(ala_mols, selection, match):
_get_link_atoms(ala_mols, qm_mol_to_atoms, _create_map({}))


def test_link_atoms_multiple_warning(ala_mols):
"""
Make sure that a warning is raised when a QM atom has multiple link atoms.
"""

from sire.base import create_map as _create_map
from sire.qm._utils import _create_qm_mol_to_atoms, _get_link_atoms

qm_atoms = ala_mols[0]["atomidx 8,9 or atomidx 14:22"].atoms()
qm_mol_to_atoms = _create_qm_mol_to_atoms(qm_atoms)

with pytest.warns(UserWarning, match="multiple link atoms"):
_get_link_atoms(ala_mols, qm_mol_to_atoms, _create_map({}))


def test_charge_redistribution():
"""
Make sure that charge redistribution works correctly.
Expand Down Expand Up @@ -478,10 +508,10 @@ def callback(numbers_qm, charges_mm, xyz_qm, xyz_mm, cell=None, idx_mm=None):
assert nrg == 42


def _check_link_atom_forces(qm_mols, engine, atol):
def _check_link_atom_forces(qm_mols, engine, atol, indices=None):
"""
Compare the forces on the atoms around the residue 1 link atoms of the
alanine dipeptide to central finite differences of the total energy.
Compare the forces on the atoms around the link atoms of the alanine
dipeptide to central finite differences of the total energy.
"""

import openmm
Expand All @@ -508,10 +538,13 @@ def energy(pos):
context.setPositions(pos * nm)
return context.getState(getEnergy=True).getPotentialEnergy().value_in_unit(kj)

# MM1 atoms 4 and 16 are bonded to QM1 atoms 6 and 14, with MM2 atoms
# 1, 5 and 17, 18. Atom 10 is a QM control.
# By default, check the residue 1 link atoms: MM1 atoms 4 and 16 are bonded
# to QM1 atoms 6 and 14, with MM2 atoms 1, 5 and 17, 18. Atom 10 is a QM control.
if indices is None:
indices = [1, 4, 5, 6, 10, 14, 16, 17, 18]

delta = 1e-4
for idx in [1, 4, 5, 6, 10, 14, 16, 17, 18]:
for idx in indices:
fd_force = np.zeros(3)
for k in range(3):
pos = positions.copy()
Expand All @@ -524,8 +557,17 @@ def energy(pos):
assert np.allclose(forces[idx], fd_force, atol=atol)


@pytest.mark.parametrize(
"selection, indices",
[
("residx 1", None),
# QM1 atom 8 is bonded to MM1 atoms 6 and 10, with MM2 atoms 4, 7 and
# 11, 12, 13. Atom 14 is a QM control.
("atomidx 8,9 or atomidx 14:22", [4, 6, 7, 8, 10, 11, 12, 13, 14]),
],
)
@pytest.mark.parametrize("mechanical_embedding", [False, True])
def test_link_atom_forces(ala_mols, mechanical_embedding):
def test_link_atom_forces(ala_mols, selection, indices, mechanical_embedding):
"""
Make sure that the forces on the QM1, MM1 and MM2 atoms around each link
atom are the negative gradient of the QM energy.
Expand Down Expand Up @@ -565,15 +607,15 @@ def callback(numbers_qm, charges_mm, xyz_qm, xyz_mm, cell=None, idx_mm=None):

qm_mols, engine = sr.qm.create_engine(
mols,
mols[0]["residx 1"],
mols[0][selection],
callback,
mechanical_embedding=mechanical_embedding,
)

_check_link_atom_forces(qm_mols, engine, atol=0.1)
_check_link_atom_forces(qm_mols, engine, atol=0.1, indices=indices)

# Make sure that both link atoms were added to the QM region.
assert num_rows[-1] == mols[0]["residx 1"].num_atoms() + 2
assert num_rows[-1] == mols[0][selection].num_atoms() + 2


@pytest.mark.skipif(not has_emle, reason="emle-engine is not installed")
Expand Down
Loading