From c460fc27c17a8416057aec208e9a8d9fd2d0a28e Mon Sep 17 00:00:00 2001 From: "Matthew W. Thompson" Date: Mon, 30 Jan 2023 15:25:46 -0600 Subject: [PATCH 1/2] Automatically convert `openmm.unit.Quantity` box vectors --- docs/releasehistory.md | 1 + openff/toolkit/tests/test_topology.py | 10 ++++++++++ openff/toolkit/topology/topology.py | 10 +++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/releasehistory.md b/docs/releasehistory.md index f5cfc15b1..3b6092bd1 100644 --- a/docs/releasehistory.md +++ b/docs/releasehistory.md @@ -11,6 +11,7 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w ### New features - [PR #1502](https://github.com/openforcefield/openff-toolkit/pull/1502): Adds Gasteiger charge computation using the RDKit backend. - [PR #1498](https://github.com/openforcefield/openff-toolkit/pull/1498): `Molecule.remap()` now supports partial mappings with the `partial` argument. +- [PR #1528](https://github.com/openforcefield/openff-toolkit/pull/1528): `Topology.box_vectors` are can now be set with `openmm.unit.Quantity`s, which are internally converted. ### Behavior changes - [PR #1498](https://github.com/openforcefield/openff-toolkit/pull/1498): New, more complete, and more descriptive errors for `Molecule.remap()`. diff --git a/openff/toolkit/tests/test_topology.py b/openff/toolkit/tests/test_topology.py index c9ba9d237..d5f4eecb1 100644 --- a/openff/toolkit/tests/test_topology.py +++ b/openff/toolkit/tests/test_topology.py @@ -204,6 +204,16 @@ def test_box_vectors(self): topology.box_vectors = good_vectors assert (topology.box_vectors == good_vectors * np.eye(3)).all() + def test_issue_1527(self): + """Test the error handling of setting box vectors with an OpenMM quantity.""" + import numpy + import openmm.unit + + topology = Topology() + topology.box_vectors = numpy.ones(3) * openmm.unit.nanometer + + assert isinstance(topology.box_vectors, unit.Quantity) + def test_is_periodic(self): """Test the getter and setter for is_periodic""" vacuum_top = Topology() diff --git a/openff/toolkit/topology/topology.py b/openff/toolkit/topology/topology.py index 0694e5f21..9a410095f 100644 --- a/openff/toolkit/topology/topology.py +++ b/openff/toolkit/topology/topology.py @@ -601,7 +601,15 @@ def box_vectors(self, box_vectors): self._box_vectors = None return if not hasattr(box_vectors, "units"): - raise InvalidBoxVectorsError("Given unitless box vectors") + + if hasattr(box_vectors, "unit"): + # this is probably an openmm.unit.Quantity; we should gracefully import OpenMM but + # the chances of this being an object with the two previous conditions met is low + box_vectors = ensure_quantity(box_vectors, "openff") + + else: + raise InvalidBoxVectorsError("Given unitless box vectors") + # Unit.compatible_units() returns False with itself, for some reason if (box_vectors.units != unit.nm) and ( box_vectors.units not in unit.nm.compatible_units() From 7c91984a86fa441b9778a16f978243d1b5cca6d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Feb 2023 01:06:03 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- openff/toolkit/topology/topology.py | 1 - 1 file changed, 1 deletion(-) diff --git a/openff/toolkit/topology/topology.py b/openff/toolkit/topology/topology.py index 9a410095f..f0f24fdea 100644 --- a/openff/toolkit/topology/topology.py +++ b/openff/toolkit/topology/topology.py @@ -601,7 +601,6 @@ def box_vectors(self, box_vectors): self._box_vectors = None return if not hasattr(box_vectors, "units"): - if hasattr(box_vectors, "unit"): # this is probably an openmm.unit.Quantity; we should gracefully import OpenMM but # the chances of this being an object with the two previous conditions met is low