Skip to content

Commit ef37ac8

Browse files
author
SimonBoothroyd
authored
Add replacements for pint dynamic classes. (#341)
1 parent 0d55e15 commit ef37ac8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+506
-451
lines changed

devtools/conda-envs/test_env.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies:
1919
- pandas
2020
- openmm
2121
- networkx
22-
- pint >=0.10.1,<0.15
22+
- pint >=0.16.1
2323
- packmol
2424
- pymbar >=3.0.5
2525
- mdtraj >=1.9.3

docs/layers/workflowlayer.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
.. |parameter_gradient_key| replace:: :py:class:`~openff.evaluator.forcefield.ParameterGradientKey`
2828

29-
.. |quantity| replace:: :py:class:`~pint.Quantity`
29+
.. |quantity| replace:: :py:class:`~openff.evaluator.utils.units.Quantity`
3030

3131
Workflow Layers
3232
===============

docs/observables/observables.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
.. |parameter_gradient| replace:: :py:class:`~openff.evaluator.forcefield.ParameterGradient`
1212
.. |parameter_gradient_key| replace:: :py:class:`~openff.evaluator.forcefield.ParameterGradientKey`
1313

14-
.. |quantity| replace:: :py:class:`~pint.Quantity`
15-
.. |measurement| replace:: :py:class:`~pint.Measurement`
14+
.. |quantity| replace:: :py:class:`~openff.evaluator.utils.units.Quantity`
15+
.. |measurement| replace:: :py:class:`~openff.evaluator.utils.units.Measurement`
1616

1717
.. |float| replace:: :py:class:`~float`
1818
.. |int| replace:: :py:class:`~int`
@@ -57,17 +57,17 @@ Supported Operations
5757

5858
.. rst-class:: spaced-list
5959

60-
- **+ and -**: |observable| objects can be summed with and subtracted from other |observable| objects, pint
60+
- **+ and -**: |observable| objects can be summed with and subtracted from other |observable| objects,
6161
|quantity| objects, floats or integers. When two |observable| objects are summed / subtracted, their gradients are
6262
combined by summing / subtracting also. When an |observable| is summed / subtracted with a |quantity|,
6363
|float| or |int| object it is assumed that these objects do not depend on any force field parameters.
6464

65-
- **\***: |observable| objects may be multiplied by other |observable| objects, pint |quantity| objects, and |float|
65+
- **\***: |observable| objects may be multiplied by other |observable| objects, |quantity| objects, and |float|
6666
or |int| objects. When two |observable| objects are multiplied their gradients are propagated using the product
6767
rule. When an |observable| is multiplied by a |quantity|, |float| or |int| object it is assumed that these
6868
objects do not depend on any force field parameters.
6969

70-
- **/**: |observable| objects may be divided by other |observable| objects, pint |quantity| objects, and |float| or
70+
- **/**: |observable| objects may be divided by other |observable| objects, |quantity| objects, and |float| or
7171
|int| objects. Gradients are propagated through the division using the quotient rule. When an |observable| is
7272
divided by a |quantity|, |float| or |int| object (or when these objects are divided by an |observable| object)
7373
it is assumed that these objects do not depend on any force field parameters.

docs/workflows/protocols.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The inputs and outputs of a protocol are defined using the custom |input_attribu
6969
# once it is executed.
7070
result = OutputAttribute(
7171
docstring="The sum of the values.",
72-
type_hint=typing.Union[int, float, pint.Measurement, pint.Quantity],
72+
type_hint=typing.Union[int, float, unit.Measurement, unit.Quantity],
7373
)
7474

7575
def _execute(self, directory, available_resources):
@@ -193,7 +193,7 @@ just wish to take the larger / smaller of the two inputs::
193193

194194
timestep = InputAttribute(
195195
docstring="The timestep to evolve the system by at each step.",
196-
type_hint=pint.Quantity,
196+
type_hint=unit.Quantity,
197197
merge_behavior=InequalityMergeBehaviour.SmallestValue,
198198
default_value=2.0 * unit.femtosecond,
199199
)

docs/workflows/replicators.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
.. |replicator_value| replace:: :py:class:`~openff.evaluator.workflow.utils.ReplicatorValue`
3434
.. |placeholder_id| replace:: :py:attr:`~openff.evaluator.workflow.schemas.ProtocolReplicator.placeholder_id`
3535

36-
.. |quantity| replace:: :py:class:`~pint.Quantity`
36+
.. |quantity| replace:: :py:class:`~openff.evaluator.utils.units.Quantity`
3737

3838
Replicators
3939
===========

openff/evaluator/__init__.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,11 @@
33
A physical property evaluation toolkit from the Open Forcefield Consortium.
44
"""
55

6-
import warnings
7-
8-
import pint
9-
106
from ._version import get_versions
117
from .plugins import register_default_plugins, register_external_plugins
8+
from .utils.units import DEFAULT_UNIT_REGISTRY
129

13-
unit = pint.UnitRegistry()
14-
unit.default_format = "~"
15-
pint.set_application_registry(unit)
16-
17-
with warnings.catch_warnings():
18-
warnings.simplefilter("ignore")
19-
pint.Quantity([])
10+
unit = DEFAULT_UNIT_REGISTRY
2011

2112
# Load the default plugins
2213
register_default_plugins()

openff/evaluator/attributes/attributes.py

+3-30
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from collections.abc import Iterable, Mapping
88
from enum import Enum, IntEnum, IntFlag
99

10-
import pint
11-
1210
from openff.evaluator import unit
1311
from openff.evaluator.attributes.typing import is_instance_of_type, is_supported_type
1412
from openff.evaluator.utils.serialization import TypedBaseModel
@@ -94,7 +92,7 @@ def validate(self, attribute_type=None):
9492
)
9593

9694
elif isinstance(attribute_value, Iterable) and not isinstance(
97-
attribute_value, pint.Quantity
95+
attribute_value, (unit.Quantity, unit.Measurement)
9896
):
9997

10098
iterable_values = (
@@ -188,8 +186,6 @@ def __getstate__(self):
188186

189187
def __setstate__(self, state):
190188

191-
import pint
192-
193189
attribute_names = self.get_attributes()
194190

195191
for name in attribute_names:
@@ -205,22 +201,6 @@ def __setstate__(self, state):
205201
elif attribute.optional and name not in state:
206202
state[name] = UNDEFINED
207203

208-
if isinstance(state[name], pint.Measurement) and not isinstance(
209-
state[name], unit.Measurement
210-
):
211-
212-
state[name] = unit.Measurement.__new__(
213-
unit.Measurement, state[name].value, state[name].error
214-
)
215-
216-
elif isinstance(state[name], pint.Quantity) and not isinstance(
217-
state[name], unit.Quantity
218-
):
219-
220-
state[name] = unit.Quantity.__new__(
221-
unit.Quantity, state[name].magnitude, state[name].units
222-
)
223-
224204
self._set_value(name, state[name])
225205

226206

@@ -276,14 +256,7 @@ def __init__(
276256
)
277257

278258
if hasattr(type_hint, "__qualname__"):
279-
280-
if type_hint.__qualname__ == "build_quantity_class.<locals>.Quantity":
281-
docstring = f"Quantity: {docstring}"
282-
elif type_hint.__qualname__ == "build_quantity_class.<locals>.Unit":
283-
docstring = f"Unit: {docstring}"
284-
else:
285-
docstring = f"{type_hint.__qualname__}: {docstring}"
286-
259+
docstring = f"{type_hint.__qualname__}: {docstring}"
287260
elif hasattr(type_hint, "__name__"):
288261
docstring = f"{type_hint.__name__}: {docstring}"
289262
else:
@@ -293,7 +266,7 @@ def __init__(
293266
self._default_value = default_value
294267

295268
if isinstance(
296-
default_value, (int, float, str, pint.Quantity, pint.Measurement, Enum)
269+
default_value, (int, float, str, unit.Quantity, unit.Measurement, Enum)
297270
) or (
298271
isinstance(default_value, (list, tuple, set, frozenset))
299272
and len(default_value) <= 4

openff/evaluator/backends/backends.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import re
66
from enum import Enum
77

8-
import pint
9-
108
from openff.evaluator import unit
119

1210

@@ -157,7 +155,7 @@ def __init__(
157155
assert self._per_thread_memory_limit is not None
158156

159157
assert (
160-
isinstance(self._per_thread_memory_limit, pint.Quantity)
158+
isinstance(self._per_thread_memory_limit, unit.Quantity)
161159
and unit.get_base_units(unit.byte)[-1]
162160
== unit.get_base_units(self._per_thread_memory_limit.units)[-1]
163161
)

openff/evaluator/data/units/__init__.py

Whitespace-only changes.
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Based on the default Pint constants definition file (0.16.1)
2+
# https://github.com/hgrecco/pint/tree/master/pint
3+
#
4+
# Based on the International System of Units
5+
# Language: english
6+
# Source: https://physics.nist.gov/cuu/Constants/
7+
# https://physics.nist.gov/PhysRefData/XrayTrans/Html/search.html
8+
# :copyright: 2013,2019 by Pint Authors, see https://github.com/hgrecco/pint/blob/0.16.1/AUTHORS for more details.
9+
10+
#### MATHEMATICAL CONSTANTS ####
11+
# As computed by Maxima with fpprec:50
12+
13+
pi = 3.1415926535897932384626433832795028841971693993751 = π # pi
14+
15+
#### DEFINED EXACT CONSTANTS ####
16+
# Recommended CODATA-2018 values
17+
18+
speed_of_light = 299792458 m/s = c = c_0 # since 1983
19+
planck_constant = 6.62607015e-34 J s = h # since May 2019
20+
elementary_charge = 1.602176634e-19 C = e # since May 2019
21+
avogadro_number = 6.02214076e23 # since May 2019
22+
boltzmann_constant = 1.380649e-23 J K^-1 = k = k_B # since May 2019
23+
standard_atmosphere = 1.01325e5 Pa = atm = atmosphere # since 1954
24+
25+
#### DERIVED EXACT CONSTANTS ####
26+
# Floating-point conversion may introduce inaccuracies
27+
28+
zeta = c / (cm/s) = ζ
29+
dirac_constant = h / (2 * π) = ħ = hbar = atomic_unit_of_action = a_u_action
30+
avogadro_constant = avogadro_number * mol^-1 = N_A
31+
molar_gas_constant = k * N_A = R
32+
33+
#### MEASURED CONSTANTS ####
34+
# Recommended CODATA-2018 values
35+
36+
rydberg_constant = 1.0973731568160e7 * m^-1 = R_∞ = R_inf # (21)
37+
atomic_mass_constant = 1.66053906660e-27 kg = m_u # (50)
38+
electron_mass = 9.1093837015e-31 kg = m_e = atomic_unit_of_mass = a_u_mass # (28)
39+
40+
#### DERIVED CONSTANTS ####
41+
42+
fine_structure_constant = (2 * h * R_inf / (m_e * c)) ** 0.5 = α = alpha
43+
vacuum_permeability = 2 * α * h / (e ** 2 * c) = µ_0 = mu_0 = mu0 = magnetic_constant
44+
vacuum_permittivity = e ** 2 / (2 * α * h * c) = ε_0 = epsilon_0 = eps_0 = eps0 = electric_constant

0 commit comments

Comments
 (0)