From 9fbca0dbcd9877146b5be1a9edc4c3c9bb9c387c Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Fri, 30 Oct 2026 14:53:23 +0000 Subject: [PATCH 1/2] Allow multiple QM/MM link atoms on the same QM atom. --- doc/source/changelog.rst | 4 +++ src/sire/qm/_utils.py | 44 +++++++++++++++++-------------- tests/qm/test_qm.py | 56 +++++++++++++++++++++++++++++++++------- 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst index bba8ce56c..9c178197b 100644 --- a/doc/source/changelog.rst +++ b/doc/source/changelog.rst @@ -128,6 +128,10 @@ organisation on `GitHub `__. * 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 `__ - September 2026 ---------------------------------------------------------------------------------------------- diff --git a/src/sire/qm/_utils.py b/src/sire/qm/_utils.py index bd00f0ae7..41e7075a4 100644 --- a/src/sire/qm/_utils.py +++ b/src/sire/qm/_utils.py @@ -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: @@ -370,34 +370,40 @@ 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) _warnings.warn( "Attempting to add a link atom for a non carbon-carbon " f"bond (atom index {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 = [] @@ -432,7 +438,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. @@ -451,7 +457,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. @@ -467,7 +473,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] diff --git a/tests/qm/test_qm.py b/tests/qm/test_qm.py index b43fddbbb..7b7b5febb 100644 --- a/tests/qm/test_qm.py +++ b/tests/qm/test_qm.py @@ -100,6 +100,15 @@ 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}, + ), + ), ], ) def test_link_atoms(ala_mols, selection, expected): @@ -212,6 +221,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. @@ -478,10 +502,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 @@ -508,10 +532,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() @@ -524,8 +551,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. @@ -565,15 +601,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") From 92bcb4a45a0a88fcfbb355b2e4b0e0d0472bd5d4 Mon Sep 17 00:00:00 2001 From: Lester Hedges Date: Fri, 30 Oct 2026 14:56:19 +0000 Subject: [PATCH 2/2] Name the MM1 atom in the non carbon-carbon link atom warning. --- src/sire/qm/_utils.py | 3 ++- tests/qm/test_qm.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/sire/qm/_utils.py b/src/sire/qm/_utils.py index 41e7075a4..acee63928 100644 --- a/src/sire/qm/_utils.py +++ b/src/sire/qm/_utils.py @@ -393,9 +393,10 @@ def _get_link_atoms(mols, qm_mol_to_atoms, map): # Warn if the link atom is not for a carbon-carbon bond. if elem != carbon or link_elem != carbon: + 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. diff --git a/tests/qm/test_qm.py b/tests/qm/test_qm.py index 7b7b5febb..36f3b2616 100644 --- a/tests/qm/test_qm.py +++ b/tests/qm/test_qm.py @@ -107,6 +107,7 @@ def callback(a, b, c, d, e=None, f=None): {6: 8, 10: 8}, {6: [4, 7], 10: [11, 12, 13]}, {6: 0.7522429261559697, 10: 0.7142857142857143}, + [[6, 10]], ), ), ], @@ -137,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(): """