Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #882 #896

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w
### Bugfixes

- [PR #891](https://github.com/openforcefield/openforcefield/pull/891): Calls to `Molecule.from_openeye` no longer mutate the input OE molecule.
- [PR #896](https://github.com/openforcefield/openforcefield/pull/896): Ensure the vdW switch width is correctly set and enabled.

### Improved documentation and warnings
- [PR #862](https://github.com/openforcefield/openforcefield/pull/862): Clarify that `System` objects produced by the toolkit are OpenMM `System`s in anticipation of forthcoming OpenFF `System`s. Fixes [Issue #618](https://github.com/openforcefield/openforcefield/issues/618).
Expand Down
55 changes: 55 additions & 0 deletions openff/toolkit/tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,61 @@ def test_add_param_str(self):
assert vdw_handler.get_parameter({"smirks": "[*:1]"})[0].id == "n99"
assert vdw_handler.get_parameter({"smirks": "[#1:1]"})[0].id == "n00"

@pytest.mark.parametrize(
"switch_width, expected",
[
(10.0 * unit.angstrom, False),
(9.0 * unit.angstrom, False),
(8.0 * unit.angstrom, True),
],
)
@pytest.mark.parametrize(
"method",
["PME", "cutoff"],
)
def test_switch_width(self, switch_width, expected, method):
"""Test that create_force works on a vdWHandler which has a switch width
specified.
"""

from simtk import openmm

# Create a dummy topology containing only argon and give it a set of
# box vectors.
topology = Molecule.from_smiles("[Ar]").to_topology()
topology.box_vectors = unit.Quantity(numpy.eye(3) * 20 * unit.angstrom)

# create a VdW handler with only parameters for argon.
vdw_handler = vdWHandler(
version=0.3,
cutoff=9.0 * unit.angstrom,
switch_width=switch_width,
method=method,
)
vdw_handler.add_parameter(
{
"smirks": "[#18:1]",
"epsilon": 1.0 * unit.kilojoules_per_mole,
"sigma": 1.0 * unit.angstrom,
}
)

omm_sys = openmm.System()
vdw_handler.create_force(omm_sys, topology)

nonbonded_force = [
force
for force in omm_sys.getForces()
if isinstance(force, openmm.NonbondedForce)
][0]

assert numpy.isclose(
nonbonded_force.getSwitchingDistance().value_in_unit(unit.angstrom),
switch_width.value_in_unit(unit.angstrom),
)

assert nonbonded_force.getUseSwitchingFunction() == expected


class TestvdWType:
"""
Expand Down
10 changes: 8 additions & 2 deletions openff/toolkit/typing/engines/smirnoff/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3603,7 +3603,7 @@ def check_handler_compatibility(self, other_handler):
"""
float_attrs_to_compare = ["scale12", "scale13", "scale14", "scale15"]
string_attrs_to_compare = ["potential", "combining_rules", "method"]
unit_attrs_to_compare = ["cutoff"]
unit_attrs_to_compare = ["cutoff", "switch_width"]

self._check_attributes_are_equal(
other_handler,
Expand All @@ -3625,9 +3625,12 @@ def create_force(self, system, topology, **kwargs):
# "must be provided")
else:
force.setNonbondedMethod(openmm.NonbondedForce.LJPME)
force.setCutoffDistance(9.0 * unit.angstrom)
force.setCutoffDistance(self.cutoff)
force.setEwaldErrorTolerance(1.0e-4)

force.setSwitchingDistance(self.switch_width)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonBoothroyd I think this is the only substantive question remaining before I can make this merge-ready. The use of "width" makes me think that this is the more correct interpretation, since the switching function applies between this distance and the cutoff. But since you're more on the physics side, I'll defer this decision to you.

Suggested change
force.setSwitchingDistance(self.switch_width)
force.setSwitchingDistance(self.cutoff - self.switch_width)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The width would usually refer to the distance over which it is tapered, e.g. an angstrom or two or some such (e.g. switched between 8 and 10 angstroms) if that helps answer this question.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@j-wags is correct here: For OpenMM, you need to use force.setSwitchingDistance(self.cutoff - self.switch_width).

force.setUseSwitchingFunction(self.switch_width < self.cutoff)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that having a larger switch_width than the cutoff should raise an error -- Silently disabling the switching it would likely be the more surprising behavior.


# If method is cutoff, then we currently support openMM's PME for periodic system and NoCutoff for nonperiodic
elif self.method == "cutoff":
# If we're given a nonperiodic box, we always set NoCutoff. Later we'll add support for CutoffNonPeriodic
Expand All @@ -3638,6 +3641,9 @@ def create_force(self, system, topology, **kwargs):
force.setUseDispersionCorrection(True)
force.setCutoffDistance(self.cutoff)

force.setSwitchingDistance(self.switch_width)
force.setUseSwitchingFunction(self.switch_width < self.cutoff)

# Iterate over all defined Lennard-Jones types, allowing later matches to override earlier ones.
atom_matches = self.find_matches(topology)

Expand Down