Skip to content

Commit

Permalink
Remove duplicate definition (#1144)
Browse files Browse the repository at this point in the history
* Remove duplicate definition

* Fix akma unit system and update units package to latest

* Remove unnecessary module
  • Loading branch information
swails committed Feb 15, 2021
1 parent 73bd1cc commit 05c5cd2
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 68 deletions.
11 changes: 1 addition & 10 deletions parmed/topologyobjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@
'ThreeParticleExtraPointFrame', 'OutOfPlaneExtraPointFrame', 'RBTorsionType',
'UnassignedAtomType', 'Link', 'DrudeAtom', 'DrudeAnisotropy']

# Create the AKMA unit system which is the unit system used by Amber and CHARMM
scale_factor = u.sqrt(1/u.kilocalories_per_mole * (u.daltons * u.angstroms**2))
scale_factor = scale_factor.value_in_unit(u.picoseconds)
akma_time_unit = u.BaseUnit(u.picosecond_base_unit.dimension, 'akma time', symbol='aks')
akma_time_unit.define_conversion_factor_to(u.picosecond_base_unit, scale_factor)
akma_unit_system = u.UnitSystem([u.angstrom_base_unit, u.dalton_base_unit, akma_time_unit,
u.elementary_charge_base_unit, u.kelvin_base_unit,
u.mole_base_unit, u.radian_base_unit])

def _strip_units(value, unit=None):
"""
Strips any units from the given value by casting them into the AKMA unit
Expand All @@ -44,7 +35,7 @@ def _strip_units(value, unit=None):
if unit is None:
if value.unit.is_compatible(u.degrees):
return value.value_in_unit(u.degrees)
return value.value_in_unit_system(akma_unit_system)
return value.value_in_unit_system(u.akma_unit_system)
else:
return value.value_in_unit(unit)
return value
Expand Down
2 changes: 1 addition & 1 deletion parmed/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

_time_scale = (daltons * angstroms**2 / kilocalories_per_mole).conversion_factor_to(picoseconds**2)
akma_time_base_unit = BaseUnit(time_dimension, "akma_time_unit", "akma-s")
akma_time_base_unit.define_conversion_factor_to(picosecond_base_unit, 1 / sqrt(_time_scale))
akma_time_base_unit.define_conversion_factor_to(picosecond_base_unit, sqrt(_time_scale))
del _time_scale

akma_unit_system = UnitSystem([
Expand Down
14 changes: 11 additions & 3 deletions parmed/unit/basedimension.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/env python
"""
Module simtk.unit.basedimension
Module openmm.unit.basedimension
BaseDimension class for use by units and quantities.
BaseDimensions are things like "length" and "mass".
Expand Down Expand Up @@ -61,8 +61,7 @@ def __init__(self, name):
"""Create a new BaseDimension.
Each new BaseDimension is assumed to be independent of all other BaseDimensions.
Use the existing BaseDimensions in simtk.dimension instead of creating
new ones.
Use the existing BaseDimensions instead of creating new ones.
"""
self.name = name
if not self.name in BaseDimension._index_by_name.keys():
Expand All @@ -80,6 +79,15 @@ def __lt__(self, other):
"""
return self._index < other._index

def __le__(self, other):
return self._index <= other._index

def __gt__(self, other):
return self._index > other._index

def __ge__(self, other):
return self._index >= other._index

def __hash__(self):
"""
Needed for using BaseDimensions as hash keys.
Expand Down
2 changes: 1 addition & 1 deletion parmed/unit/baseunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


"""
Module simtk.unit.baseunit
Module openmm.unit.baseunit
Contains BaseUnit class, which is a component of the Unit class.
Expand Down
14 changes: 6 additions & 8 deletions parmed/unit/constants.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/env python
"""
Module simtk.unit.constants
Module openmm.unit.constants
This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Biological Structures at Stanford, funded under the NIH Roadmap for
Medical Research, grant U54 GM072970. See https://simtk.org.
Portions copyright (c) 2012 Stanford University and the Authors.
Portions copyright (c) 2012-2020 Stanford University and the Authors.
Authors: Christopher M. Bruns
Contributors: Peter Eastman
Expand Down Expand Up @@ -40,14 +40,12 @@
### CONSTANTS ###
#################

# codata 2006
AVOGADRO_CONSTANT_NA = 6.02214179e23 / mole
BOLTZMANN_CONSTANT_kB = 1.3806504e-23 * joule / kelvin
# codata 2018
AVOGADRO_CONSTANT_NA = 6.02214076e23 / mole
BOLTZMANN_CONSTANT_kB = 1.380649e-23 * joule / kelvin
MOLAR_GAS_CONSTANT_R = AVOGADRO_CONSTANT_NA * BOLTZMANN_CONSTANT_kB

# From simtkcommon
SPEED_OF_LIGHT_C = 2.99792458e8 * meter / second
GRAVITATIONAL_CONSTANT_G = 6.6742e-11 * newton * meter**2 / kilogram**2
GRAVITATIONAL_CONSTANT_G = 6.6743e-11 * newton * meter**2 / kilogram**2

# run module directly for testing
if __name__=='__main__':
Expand Down
4 changes: 2 additions & 2 deletions parmed/unit/prefix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/env python
"""
Module simtk.unit.prefix
Module openmm.unit.prefix
This is part of the OpenMM molecular simulation toolkit originating from
Simbios, the NIH National Center for Physics-Based Simulation of
Expand Down Expand Up @@ -140,7 +140,7 @@ def define_prefixed_units(base_unit, module = sys.modules[__name__]):
Parameters
- base_unit (BaseUnit) existing base unit to use as a basis for prefixed units
- module (Module) module which will contain the new attributes. Defaults to simtk.unit module.
- module (Module) module which will contain the new attributes. Defaults to openmm.unit module.
"""
for prefix in si_prefixes:
new_base_unit = prefix * base_unit
Expand Down
45 changes: 36 additions & 9 deletions parmed/unit/quantity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/env python
"""
Module simtk.unit.quantity
Module openmm.unit.quantity
Physical quantities with units, intended to produce similar functionality
to Boost.Units package in C++ (but with a runtime cost).
Expand All @@ -9,14 +9,14 @@
In particular, there is no underlying set of 'canonical' base
units, whereas in Scientific.Physics.PhysicalQuantities all
units are secretly in terms of SI units. Also, it is easier
to add new fundamental dimensions to simtk.dimensions. You
to add new fundamental dimensions to basedimension. You
might want to make new dimensions for, say, "currency" or
"information".
Some features of this implementation:
* Quantities are a combination of a value and a unit. The value
part can be any python type, including numbers, lists, numpy
arrays, and anything else. The unit part must be a simtk.unit.Unit.
arrays, and anything else. The unit part must be a openmm.unit.Unit.
* Operations like adding incompatible units raises an error.
* Multiplying or dividing units/quantities creates new units.
* Users can create new Units and Dimensions, but most of the useful
Expand Down Expand Up @@ -100,7 +100,7 @@ def __init__(self, value=None, unit=None):
Parameters
- value: (any type, usually a number) Measure of this quantity
- unit: (Unit) the physical unit, e.g. simtk.unit.meters.
- unit: (Unit) the physical unit, e.g. openmm.unit.meters.
"""
# When no unit is specified, bend over backwards to handle all one-argument possibilities
if unit is None: # one argument version, copied from UList
Expand All @@ -112,13 +112,13 @@ def __init__(self, value=None, unit=None):
# Ulist of a Quantity is just the Quantity itself
unit = value.unit
value = value._value
elif isinstance(value, str):
elif _is_string(value):
unit = dimensionless
else:
# Is value a container?
is_container = True
try:
i = iter(value)
_ = iter(value)
except TypeError:
is_container = False
if is_container:
Expand Down Expand Up @@ -267,7 +267,7 @@ def __eq__(self, other):
def __ne__(self, other):
"""
"""
return not self.__eq__(other)
return not self == other

def __lt__(self, other):
"""Compares two quantities.
Expand All @@ -287,6 +287,8 @@ def __le__(self, other):
def __lt__(self, other):
return self._value < (other.value_in_unit(self.unit))

__hash__ = None

_reduce_cache = {}

def reduce_unit(self, guide_unit=None):
Expand Down Expand Up @@ -597,6 +599,11 @@ def __neg__(self):
"""
return Quantity(-(self._value), self.unit)

def __nonzero__(self):
"""Returns True if value underlying Quantity is zero, False otherwise.
"""
return bool(self._value)

def __bool__(self):
return bool(self._value)

Expand Down Expand Up @@ -732,8 +739,8 @@ def __setitem__(self, key, value):
# Delegate slices to one-at-a time ___setitem___
if isinstance(key, slice): # slice
indices = key.indices(len(self))
for i in range(*indices):
self[i] = value[i]
for value_idx, self_idx in enumerate(range(*indices)):
self[self_idx] = value[value_idx]
else: # single index
# Check unit compatibility
if self.unit.is_dimensionless() and is_dimensionless(value):
Expand Down Expand Up @@ -776,6 +783,7 @@ def pop(self, *args):
# list.sort with no arguments will delegate correctly
# list.sort with a comparison function cannot be done correctly


def is_quantity(x):
"""
Returns True if x is a Quantity, False otherwise.
Expand All @@ -793,6 +801,25 @@ def is_dimensionless(x):
# everything else in the universe is dimensionless
return True

# Strings can cause trouble
# as can any container that has infinite levels of containment
def _is_string(x):
# step 1) String is always a container
# and its contents are themselves containers.
if isinstance(x, str):
return True
try:
first_item = next(iter(x))
inner_item = next(iter(first_item))
if first_item is inner_item:
return True
else:
return False
except TypeError:
return False
except StopIteration:
return False

# run module directly for testing
if __name__=='__main__':
# Test the examples in the docstrings
Expand Down
2 changes: 1 addition & 1 deletion parmed/unit/standard_dimensions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/env python
"""
Module simtk.unit.standard_dimensions
Module openmm.unit.standard_dimensions
Definition of principal dimensions: mass, length, time, etc.
Expand Down

0 comments on commit 05c5cd2

Please sign in to comment.