Skip to content

Commit

Permalink
Merge pull request #929 from openforcefield/from-openmm-positions-war…
Browse files Browse the repository at this point in the history
…ning

Warn if positions not given
  • Loading branch information
mattwthompson authored Mar 19, 2024
2 parents 547dd1e + 7a95d55 commit 2d62ab3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Please note that all releases prior to a version 1.0.0 are considered pre-releas
## Current development

* #933 Fixes #934 in which atom order was sometimes mangled in `Interchange.from_openmm`.
* #929 A warning is raised when positions are not passed to `Interchange.from_openmm`.
* #930 Adds `additional_forces` argument to `create_openmm_simulation`.

## 0.3.23 - 2024-03-07
Expand Down
5 changes: 3 additions & 2 deletions examples/experimental/openmm-import/protein-ligand.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
"import pathlib\n",
"import sys\n",
"import urllib.request\n",
"from typing import Dict, Tuple\n",
"from typing import Tuple\n",
"\n",
"import openmm\n",
"import openmm.app\n",
"import openmm.unit\n",
"from openff.units.openmm import ensure_quantity\n",
"\n",
"from openff.interchange import Interchange\n",
"from openff.interchange.drivers import get_openmm_energies\n",
"from openff.interchange.drivers.openmm import _get_openmm_energies, _process\n",
"from openff.interchange.interop.openmm import from_openmm\n",
Expand Down Expand Up @@ -116,7 +117,7 @@
"metadata": {},
"outputs": [],
"source": [
"converted_interchange = from_openmm(\n",
"converted_interchange = Interchange.from_openmm(\n",
" topology=protein.topology,\n",
" system=openmm_system,\n",
" positions=ensure_quantity(protein.getPositions(), \"openff\"),\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/experimental/openmmforcefields/gaff.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@
"metadata": {},
"outputs": [],
"source": [
"from openff.interchange.interop.openmm import from_openmm\n",
"from openff.interchange import Interchange\n",
"\n",
"imported = from_openmm(\n",
"imported = Interchange.from_openmm(\n",
" topology=topology.to_openmm(),\n",
" system=system_gaff,\n",
" positions=molecule.conformers[0].to_openmm(),\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from openff.interchange.exceptions import UnsupportedImportError
from openff.interchange.interop.openmm._import import from_openmm
from openff.interchange.warnings import MissingPositionsWarning


class TestUnsupportedCases:
Expand Down Expand Up @@ -52,3 +53,18 @@ def test_found_virtual_sites(self, monkeypatch, tip4p, water):
system=system,
topology=topology.to_openmm(),
)

def test_missing_positions_warning(self, monkeypatch, sage, water):
monkeypatch.setenv("INTERCHANGE_EXPERIMENTAL", "1")

topology = water.to_topology()
topology.box_vectors = Quantity([4, 4, 4], "nanometer")

with pytest.warns(
MissingPositionsWarning,
match="are you sure",
):
from_openmm(
system=sage.create_openmm_system(topology),
topology=topology.to_openmm(),
)
22 changes: 17 additions & 5 deletions openff/interchange/interop/openmm/_import/_import.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from typing import TYPE_CHECKING, Union
import warnings
from typing import TYPE_CHECKING, Optional, Union

from openff.models.types import ArrayQuantity
from openff.toolkit import Topology
from openff.toolkit import Quantity, Topology
from openff.utilities.utilities import has_package, requires_package

from openff.interchange._experimental import experimental
Expand All @@ -17,6 +18,7 @@
BasicElectrostaticsCollection,
)
from openff.interchange.interop.openmm._import.compat import _check_compatible_inputs
from openff.interchange.warnings import MissingPositionsWarning

if has_package("openmm"):
import openmm
Expand All @@ -34,10 +36,11 @@
@requires_package("openmm")
@experimental
def from_openmm(
*,
system: "openmm.System",
positions: Optional[Quantity] = None,
topology: Union["openmm.app.Topology", Topology, None] = None,
positions=None,
box_vectors=None,
box_vectors: Optional[Quantity] = None,
) -> "Interchange":
"""Create an Interchange object from OpenMM data."""
from openff.interchange import Interchange
Expand Down Expand Up @@ -88,7 +91,16 @@ def from_openmm(

interchange.topology = topology

if positions is not None:
if positions is None:

warnings.warn(
"Nothing was passed to the `positions` argument, are you sure "
"you don't want to pass positions?",
MissingPositionsWarning,
stacklevel=2,
)

else:

interchange.positions = positions

Expand Down
4 changes: 4 additions & 0 deletions openff/interchange/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ class InterchangeDeprecationWarning(UserWarning):

class SwitchingFunctionNotImplementedWarning(UserWarning):
"""Exporting to an engine that does not implement a switching function."""


class MissingPositionsWarning(UserWarning):
"""Warning for when positions are likely needed but missing."""

0 comments on commit 2d62ab3

Please sign in to comment.