Skip to content

Commit

Permalink
Merge pull request #34 from openforcefield/quantity-to-openmm
Browse files Browse the repository at this point in the history
Add `Quantity.to_openmm`
  • Loading branch information
mattwthompson authored Jun 3, 2022
2 parents 7f8fccd + e43884e commit 02be10d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
napoleon_google_docstring = True
napoleon_use_param = False
napoleon_use_ivar = True
napoleon_use_rtype = False
napoleon_preprocess_types = True

myst_enable_extensions = [
"deflist",
Expand Down
15 changes: 15 additions & 0 deletions openff/units/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
from openff.units import unit


class TestQuantity:
@skip_if_missing("openmm.unit")
def test_to_openmm_method(self):
"""
Test the basic behavior of `Quantity.to_openmm` as an API call. Testing of the underlying
behavior of the standalone `to_openmm` function is in opeff/units/tests/test_openmm.py.
"""
from openmm import unit as openmm_unit

quantity = unit.Quantity(0.5, "nanometer")
converted = quantity.to_openmm()

assert converted == openmm_unit.Quantity(0.5, openmm_unit.nanometer)


class TestPickle:
"""Test pickle-based serialization of Quantity, Unit, and Measurement objects
Expand Down
18 changes: 18 additions & 0 deletions openff/units/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

import uuid
import warnings
from typing import TYPE_CHECKING

import pint
from openff.utilities import requires_package
from pint.measurement import _Measurement
from pint.quantity import _Quantity
from pint.unit import _Unit

from openff.units.utilities import get_defaults_path

if TYPE_CHECKING:
from openmm.unit import Quantity as OpenMMQuantity

__all__ = [
"DEFAULT_UNIT_REGISTRY",
"Quantity",
Expand Down Expand Up @@ -63,6 +68,19 @@ def _dask_finalize(results, func, args, units):
values = func(results, *args)
return Quantity(values, units)

@requires_package("openmm")
def to_openmm(self) -> "OpenMMQuantity":
"""Convert the quantity to an ``openmm.unit.Quantity``.
Returns
-------
openmm_quantity : openmm.unit.quantity.Quantity
The OpenMM compatible quantity.
"""
from openff.units.openmm import to_openmm

return to_openmm(self)


class Measurement(_Measurement):
"""A value with associated units and uncertainty."""
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ ignore_missing_imports = True
[mypy-openmm]
ignore_missing_imports = True

[mypy-openmm.unit]
ignore_missing_imports = True

[mypy-openmm.app.element]
ignore_missing_imports = True

0 comments on commit 02be10d

Please sign in to comment.