diff --git a/tests/qm/test_qm.py b/tests/qm/test_qm.py index b43fddbbb..a90d3ed71 100644 --- a/tests/qm/test_qm.py +++ b/tests/qm/test_qm.py @@ -478,6 +478,70 @@ def callback(numbers_qm, charges_mm, xyz_qm, xyz_mm, cell=None, idx_mm=None): assert nrg == 42 +def test_qm_force_cleared_outside_cutoff(ala_mols): + """ + Make sure that an MM atom leaving the QM cutoff doesn't keep its previous + QM/MM force. + """ + + import openmm + + # Zero energy, with a unit x-force on every MM atom within the cutoff. + def callback(numbers_qm, charges_mm, xyz_qm, xyz_mm, cell=None, idx_mm=None): + forces_mm = np.zeros((len(xyz_mm), 3)) + forces_mm[:, 0] = 1.0 + return (0.0, np.zeros((len(xyz_qm), 3)).tolist(), forces_mm.tolist()) + + mols = ala_mols.clone() + + qm_mols, engine = sr.qm.create_engine( + mols, + mols[0], + callback, + callback=None, + cutoff="12 A", + switch_width=0, + ) + + d = qm_mols.dynamics( + timestep="1fs", + constraint="none", + qm_engine=engine, + cutoff_type="pme", + cutoff="7.5 A", + platform="reference", + ) + + context = d.context() + + group = next( + f.getForceGroup() + for f in context.getSystem().getForces() + if f.getName() == "QMForce" + ) + + nm = openmm.unit.nanometer + kj = openmm.unit.kilojoule_per_mole + + # The oxygen of the first water. + idx = mols[0].num_atoms() + water = [idx + i for i in range(mols[1].num_atoms())] + + inside = context.getState(getPositions=True).getPositions(asNumpy=True) + inside = inside.value_in_unit(nm) + outside = inside.copy() + outside[water, 1] += 1.0 + + def force_x(pos): + context.setPositions(pos * nm) + state = context.getState(getForces=True, groups={group}) + return state.getForces(asNumpy=True).value_in_unit(kj / nm)[idx, 0] + + assert force_x(outside) == 0.0 + assert force_x(inside) != 0.0 + assert force_x(outside) == 0.0 + + def _check_link_atom_forces(qm_mols, engine, atol): """ Compare the forces on the atoms around the residue 1 link atoms of the diff --git a/wrapper/Convert/SireOpenMM/pyqm.cpp b/wrapper/Convert/SireOpenMM/pyqm.cpp index 385775c0f..64e1a8f5a 100644 --- a/wrapper/Convert/SireOpenMM/pyqm.cpp +++ b/wrapper/Convert/SireOpenMM/pyqm.cpp @@ -26,6 +26,7 @@ * \*********************************************/ +#include #include #include @@ -609,6 +610,9 @@ double PyQMForceImpl::computeForce( const std::vector &positions, std::vector &forces) { + // OpenMM reuses this buffer between calls, so clear stale forces. + std::fill(forces.begin(), forces.end(), OpenMM::Vec3()); + // If this is the first step, then setup information for the neighbour list. if (this->step_count == 0) { @@ -1069,7 +1073,8 @@ double PyQMForceImpl::computeForce( { // r_hat points from the nearest QM atom to the MM atom. const Vector mm_vec(xyz_mm[i][0], xyz_mm[i][1], xyz_mm[i][2]); - const Vector r_hat = (mm_vec - nearest_qm_vecs[i]).normalise(); + const Vector r_hat = + (space.getMinimumImage(mm_vec, nearest_qm_vecs[i]) - nearest_qm_vecs[i]).normalise(); // dE_dcharges_mm is in kJ/mol/e (converted in Python); dsdr is in 1/Å; // multiply by 10 to convert kJ/mol/Å → kJ/mol/nm (OpenMM units). const double correction = diff --git a/wrapper/Convert/SireOpenMM/torchqm.cpp b/wrapper/Convert/SireOpenMM/torchqm.cpp index b8ceb5bf2..48da21036 100644 --- a/wrapper/Convert/SireOpenMM/torchqm.cpp +++ b/wrapper/Convert/SireOpenMM/torchqm.cpp @@ -33,6 +33,7 @@ #include #endif +#include #include #include "SireError/errors.h" @@ -411,6 +412,9 @@ double TorchQMForceImpl::computeForce( const std::vector &positions, std::vector &forces) { + // OpenMM reuses this buffer between calls, so clear stale forces. + std::fill(forces.begin(), forces.end(), OpenMM::Vec3()); + // Get the platform name from the context. const auto platform = context.getPlatform().getName(); @@ -998,7 +1002,8 @@ double TorchQMForceImpl::computeForce( { // r_hat points from the nearest QM atom to the MM atom. const Vector mm_vec(xyz_mm[3 * i], xyz_mm[3 * i + 1], xyz_mm[3 * i + 2]); - const Vector r_hat = (mm_vec - nearest_qm_vecs[i]).normalise(); + const Vector r_hat = + (space.getMinimumImage(mm_vec, nearest_qm_vecs[i]) - nearest_qm_vecs[i]).normalise(); // dE_dq is in Hartree/e; dsdr is in 1/Å; multiply by HARTREE_TO_KJ_MOL*10 // to convert to kJ/mol/nm, matching the units of forces_qm/forces_mm. const double correction = -dE_dq[i].item() * HARTREE_TO_KJ_MOL * 10.0 * charges_unscaled[i] * dsdr;