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
64 changes: 64 additions & 0 deletions tests/qm/test_qm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion wrapper/Convert/SireOpenMM/pyqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
\*********************************************/

#include <algorithm>
#include <limits>
#include <mutex>

Expand Down Expand Up @@ -609,6 +610,9 @@ double PyQMForceImpl::computeForce(
const std::vector<OpenMM::Vec3> &positions,
std::vector<OpenMM::Vec3> &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)
{
Expand Down Expand Up @@ -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 =
Expand Down
7 changes: 6 additions & 1 deletion wrapper/Convert/SireOpenMM/torchqm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <torch/csrc/autograd/autograd.h>
#endif

#include <algorithm>
#include <limits>

#include "SireError/errors.h"
Expand Down Expand Up @@ -411,6 +412,9 @@ double TorchQMForceImpl::computeForce(
const std::vector<OpenMM::Vec3> &positions,
std::vector<OpenMM::Vec3> &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();

Expand Down Expand Up @@ -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<float>() * HARTREE_TO_KJ_MOL * 10.0 * charges_unscaled[i] * dsdr;
Expand Down