"platforms/cpu/src/CpuConstantPotentialForceFvec.cpp" did not exist on "15811b7c56b65a5e94e4c7b212100b37b4de331f"
Commit d648613b authored by Peter Eastman's avatar Peter Eastman
Browse files

Changes to support Python 3

parent d1793e5a
...@@ -69,6 +69,11 @@ def buildKeywordDictionary(major_version_num=MAJOR_VERSION_NUM, ...@@ -69,6 +69,11 @@ def buildKeywordDictionary(major_version_num=MAJOR_VERSION_NUM,
build_info=BUILD_INFO): build_info=BUILD_INFO):
from distutils.core import Extension from distutils.core import Extension
setupKeywords = {} setupKeywords = {}
try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
from distutils.command.build_py import build_py
setupKeywords["cmdclass"] = {'build_py': build_py}
setupKeywords["name"] = "OpenMM" setupKeywords["name"] = "OpenMM"
setupKeywords["version"] = "%s.%s.%s" % (major_version_num, setupKeywords["version"] = "%s.%s.%s" % (major_version_num,
minor_version_num, minor_version_num,
...@@ -158,11 +163,11 @@ def buildKeywordDictionary(major_version_num=MAJOR_VERSION_NUM, ...@@ -158,11 +163,11 @@ def buildKeywordDictionary(major_version_num=MAJOR_VERSION_NUM,
outputString = '' outputString = ''
firstTab = 40 firstTab = 40
secondTab = 60 secondTab = 60
for key in sorted( setupKeywords.iterkeys() ): for key in sorted(iter(setupKeywords)):
value = setupKeywords[key] value = setupKeywords[key]
outputString += key.rjust(firstTab) + str( value ).rjust(secondTab) + "\n" outputString += key.rjust(firstTab) + str( value ).rjust(secondTab) + "\n"
print "%s" % outputString sys.stdout.write("%s" % outputString)
return setupKeywords return setupKeywords
...@@ -170,8 +175,6 @@ def buildKeywordDictionary(major_version_num=MAJOR_VERSION_NUM, ...@@ -170,8 +175,6 @@ def buildKeywordDictionary(major_version_num=MAJOR_VERSION_NUM,
def main(): def main():
if sys.version_info < (2, 6): if sys.version_info < (2, 6):
reportError("OpenMM requires Python 2.6 or better.") reportError("OpenMM requires Python 2.6 or better.")
if sys.version_info >= (3,):
reportError("OpenMM has not been tested with Python 3.0 or higher.")
if platform.system() == 'Darwin': if platform.system() == 'Darwin':
macVersion = [int(x) for x in platform.mac_ver()[0].split('.')] macVersion = [int(x) for x in platform.mac_ver()[0].split('.')]
if tuple(macVersion) < (10, 5): if tuple(macVersion) < (10, 5):
......
...@@ -44,15 +44,15 @@ class BaseDimension(object): ...@@ -44,15 +44,15 @@ class BaseDimension(object):
BaseDimension._next_unused_index += 1 BaseDimension._next_unused_index += 1
self._index = BaseDimension._index_by_name[name] self._index = BaseDimension._index_by_name[name]
def __cmp__(self, other): def __lt__(self, other):
""" """
The implicit order of BaseDimensions is the order in which they were created. The implicit order of BaseDimensions is the order in which they were created.
This method is used for using BaseDimensions as hash keys, and also affects This method is used for using BaseDimensions as hash keys, and also affects
the order in which units appear in multi-dimensional Quantities. the order in which units appear in multi-dimensional Quantities.
Returns 0 if self == other, -1 if self < other, and 1 if self > other. Returns True if self < other, False otherwise.
""" """
return cmp(self._index, other._index) return self._index < other._index
def __hash__(self): def __hash__(self):
""" """
......
...@@ -37,15 +37,15 @@ class BaseUnit(object): ...@@ -37,15 +37,15 @@ class BaseUnit(object):
self._conversion_factor_to_by_name = {} self._conversion_factor_to_by_name = {}
self._conversion_factor_to_by_name[self.name] = 1.0 self._conversion_factor_to_by_name[self.name] = 1.0
def __cmp__(self, other): def __lt__(self, other):
""" """
Comparison function that sorts BaseUnits by BaseDimension Comparison function that sorts BaseUnits by BaseDimension
""" """
# First sort on dimension # First sort on dimension
c = cmp(self.dimension, other.dimension) if self.dimension != other.dimension:
if c != 0: return c return self.dimension < other.dimension
# Second on conversion factor # Second on conversion factor
return cmp(self.conversion_factor_to(other), 1.0) return self.conversion_factor_to(other) < 1.0
def iter_base_dimensions(self): def iter_base_dimensions(self):
""" """
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
Module simtk.unit.constants Module simtk.unit.constants
""" """
from __future__ import division
__author__ = "Christopher M. Bruns" __author__ = "Christopher M. Bruns"
__version__ = "0.5" __version__ = "0.5"
......
...@@ -36,9 +36,9 @@ False ...@@ -36,9 +36,9 @@ False
>>> c = 1.0*calories >>> c = 1.0*calories
>>> c >>> c
Quantity(value=1.0, unit=calorie) Quantity(value=1.0, unit=calorie)
>>> print calorie.conversion_factor_to(joule) >>> print(calorie.conversion_factor_to(joule))
4.184 4.184
>>> print joule.conversion_factor_to(calorie) >>> print(joule.conversion_factor_to(calorie))
0.239005736138 0.239005736138
>>> c.in_units_of(joules) >>> c.in_units_of(joules)
Quantity(value=4.1840000000000002, unit=joule) Quantity(value=4.1840000000000002, unit=joule)
...@@ -49,9 +49,9 @@ Quantity(value=1.0, unit=joule) ...@@ -49,9 +49,9 @@ Quantity(value=1.0, unit=joule)
Quantity(value=0.23900573613766729, unit=calorie) Quantity(value=0.23900573613766729, unit=calorie)
>>> j/joules >>> j/joules
1.0 1.0
>>> print j/calories >>> print(j/calories)
0.239005736138 0.239005736138
>>> print c/joules >>> print(c/joules)
4.184 4.184
>>> c/calories >>> c/calories
1.0 1.0
...@@ -75,33 +75,33 @@ False ...@@ -75,33 +75,33 @@ False
Examples Examples
>>> print meter / second >>> print(meter / second)
meter/second meter/second
>>> print meter / meter >>> print(meter / meter)
dimensionless dimensionless
Heterogeneous units are not reduced unless they are in a quantity. Heterogeneous units are not reduced unless they are in a quantity.
>>> print meter / centimeter >>> print(meter / centimeter)
meter/centimeter meter/centimeter
Examples Examples
>>> meters_per_second = Unit({meter_base_unit: 1.0, second_base_unit: -1.0}) >>> meters_per_second = Unit({meter_base_unit: 1.0, second_base_unit: -1.0})
>>> print meters_per_second >>> print(meters_per_second)
meter/second meter/second
>>> us = UnitSystem([ScaledUnit(1.0, coulomb/second, "ampere", "A"), second_base_unit]) >>> us = UnitSystem([ScaledUnit(1.0, coulomb/second, "ampere", "A"), second_base_unit])
>>> print us.express_unit(second) >>> print(us.express_unit(second))
second second
>>> print us.express_unit(coulomb/second) >>> print(us.express_unit(coulomb/second))
ampere ampere
>>> print us.express_unit(coulomb) >>> print(us.express_unit(coulomb))
second*ampere second*ampere
>>> print us.express_unit(meter/second) >>> print(us.express_unit(meter/second))
meter/second meter/second
>>> us = UnitSystem([ScaledUnit(1.0, coulomb/second, "ampere", "A"), second_base_unit]) >>> us = UnitSystem([ScaledUnit(1.0, coulomb/second, "ampere", "A"), second_base_unit])
>>> print us >>> print(us)
UnitSystem([ampere, second]) UnitSystem([ampere, second])
Examples Examples
...@@ -111,7 +111,7 @@ False ...@@ -111,7 +111,7 @@ False
>>> (meter/meter).is_dimensionless() >>> (meter/meter).is_dimensionless()
True True
>>> print (meter*meter).sqrt() >>> print((meter*meter).sqrt())
meter meter
>>> meter.sqrt() >>> meter.sqrt()
Traceback (most recent call last): Traceback (most recent call last):
...@@ -121,13 +121,13 @@ ArithmeticError: Exponents in Unit.sqrt() must be even. ...@@ -121,13 +121,13 @@ ArithmeticError: Exponents in Unit.sqrt() must be even.
Traceback (most recent call last): Traceback (most recent call last):
... ...
ArithmeticError: Exponents in Unit.sqrt() must be even. ArithmeticError: Exponents in Unit.sqrt() must be even.
>>> print (meter*meter/second/second).sqrt() >>> print((meter*meter/second/second).sqrt())
meter/second meter/second
Mixture of BaseUnits and ScaledUnits should cause no trouble: Mixture of BaseUnits and ScaledUnits should cause no trouble:
>>> print sqrt(kilogram*joule) >>> print(sqrt(kilogram*joule))
kilogram*meter/second kilogram*meter/second
>>> print sqrt(kilogram*calorie) >>> print(sqrt(kilogram*calorie))
kilogram*meter/second kilogram*meter/second
Examples Examples
...@@ -146,20 +146,20 @@ Examples ...@@ -146,20 +146,20 @@ Examples
Examples Examples
>>> print angstrom.in_unit_system(si_unit_system) >>> print(angstrom.in_unit_system(si_unit_system))
meter meter
>>> print angstrom.in_unit_system(cgs_unit_system) >>> print(angstrom.in_unit_system(cgs_unit_system))
centimeter centimeter
>>> print angstrom.in_unit_system(md_unit_system) >>> print(angstrom.in_unit_system(md_unit_system))
nanometer nanometer
>>> u = meter/second**2 >>> u = meter/second**2
>>> print u >>> print(u)
meter/(second**2) meter/(second**2)
>>> print u.in_unit_system(si_unit_system) >>> print(u.in_unit_system(si_unit_system))
meter/(second**2) meter/(second**2)
>>> print u.in_unit_system(cgs_unit_system) >>> print(u.in_unit_system(cgs_unit_system))
centimeter/(second**2) centimeter/(second**2)
>>> print u.in_unit_system(md_unit_system) >>> print(u.in_unit_system(md_unit_system))
nanometer/(picosecond**2) nanometer/(picosecond**2)
Examples Examples
...@@ -180,99 +180,99 @@ Examples ...@@ -180,99 +180,99 @@ Examples
>>> meter.conversion_factor_to(centimeter) >>> meter.conversion_factor_to(centimeter)
100.0 100.0
>>> print (md_kilocalorie/mole/angstrom).conversion_factor_to(md_kilojoule/mole/nanometer) >>> print((md_kilocalorie/mole/angstrom).conversion_factor_to(md_kilojoule/mole/nanometer))
41.84 41.84
Examples Examples
>>> print meter >>> print(meter)
meter meter
>>> print meter * second * second * kilogram >>> print(meter * second * second * kilogram)
kilogram*meter*second**2 kilogram*meter*second**2
>>> print meter / second / second / kilogram >>> print(meter / second / second / kilogram)
meter/(kilogram*second**2) meter/(kilogram*second**2)
Examples Examples
>>> print meter**3 >>> print(meter**3)
meter**3 meter**3
>>> print meter**3 >>> print(meter**3)
meter**3 meter**3
>>> meter.get_conversion_factor_to_base_units() >>> meter.get_conversion_factor_to_base_units()
1.0 1.0
Simple ScaledUnit in calorie Simple ScaledUnit in calorie
>>> print calorie.get_conversion_factor_to_base_units() >>> print(calorie.get_conversion_factor_to_base_units())
4.184 4.184
Compound ScaledUnit in md_kilocalorie Compound ScaledUnit in md_kilocalorie
>>> print md_kilocalorie.get_conversion_factor_to_base_units() >>> print(md_kilocalorie.get_conversion_factor_to_base_units())
4.184 4.184
calorie in a more complex unit calorie in a more complex unit
>>> print (md_kilocalorie/mole/angstrom).get_conversion_factor_to_base_units() >>> print((md_kilocalorie/mole/angstrom).get_conversion_factor_to_base_units())
4.184 4.184
Examples Examples
Create simple Quantities with either the multiply operator or the Quantity constructor. Create simple Quantities with either the multiply operator or the Quantity constructor.
>>> print 5 * centimeters >>> print(5 * centimeters)
5 cm 5 cm
>>> print Quantity(value=5, unit=centimeter) >>> print(Quantity(value=5, unit=centimeter))
5 cm 5 cm
>>> print Quantity(5, centimeter) >>> print(Quantity(5, centimeter))
5 cm 5 cm
Extract the underlying value using either division or the value_in_unit() method. Extract the underlying value using either division or the value_in_unit() method.
>>> i = 5 * centimeters >>> i = 5 * centimeters
>>> print i / millimeters >>> print(i / millimeters)
50.0 50.0
>>> print i.value_in_unit(millimeters) >>> print(i.value_in_unit(millimeters))
50.0 50.0
Collections of numbers can also be used as values. Collections of numbers can also be used as values.
>>> s = [1,2,3] * centimeters >>> s = [1,2,3] * centimeters
>>> print s >>> print(s)
[1, 2, 3] cm [1, 2, 3] cm
>>> print s / millimeters >>> print(s / millimeters)
[10.0, 20.0, 30.0] [10.0, 20.0, 30.0]
>>> s2 = [[1,2,3],[4,5,6]] * centimeters >>> s2 = [[1,2,3],[4,5,6]] * centimeters
>>> print s2 >>> print(s2)
[[1, 2, 3], [4, 5, 6]] cm [[1, 2, 3], [4, 5, 6]] cm
>>> print s2 / millimeters >>> print(s2 / millimeters)
[[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]] [[10.0, 20.0, 30.0], [40.0, 50.0, 60.0]]
>>> s3 = [(1,2,3),(4,5,6)] * centimeters >>> s3 = [(1,2,3),(4,5,6)] * centimeters
>>> print s3 >>> print(s3)
[(1, 2, 3), (4, 5, 6)] cm [(1, 2, 3), (4, 5, 6)] cm
>>> print s3 / millimeters >>> print(s3 / millimeters)
[(10.0, 20.0, 30.0), (40.0, 50.0, 60.0)] [(10.0, 20.0, 30.0), (40.0, 50.0, 60.0)]
>>> s4 = ((1,2,3),(4,5,6)) * centimeters >>> s4 = ((1,2,3),(4,5,6)) * centimeters
>>> print s4 >>> print(s4)
((1, 2, 3), (4, 5, 6)) cm ((1, 2, 3), (4, 5, 6)) cm
>>> print s4 / millimeters >>> print(s4 / millimeters)
[(10.0, 20.0, 30.0), (40.0, 50.0, 60.0)] [(10.0, 20.0, 30.0), (40.0, 50.0, 60.0)]
>>> t = (1,2,3) * centimeters >>> t = (1,2,3) * centimeters
>>> print t >>> print(t)
(1, 2, 3) cm (1, 2, 3) cm
>>> print t / millimeters >>> print(t / millimeters)
[10.0, 20.0, 30.0] [10.0, 20.0, 30.0]
Numpy examples are commented out because not all systems have numpy installed Numpy examples are commented out because not all systems have numpy installed
# >>> import numpy # >>> import numpy
# >>> # >>>
# >>> a = Quantity(numpy.array([1,2,3]), centimeters) # >>> a = Quantity(numpy.array([1,2,3]), centimeters)
# >>> print a # >>> print(a)
# [1 2 3] cm # [1 2 3] cm
# >>> print a / millimeters # >>> print(a / millimeters)
# [ 10. 20. 30.] # [ 10. 20. 30.]
# >>> # >>>
# >>> a2 = Quantity(numpy.array([[1,2,3],[4,5,6]]), centimeters) # >>> a2 = Quantity(numpy.array([[1,2,3],[4,5,6]]), centimeters)
# >>> print a2 # >>> print(a2)
# [[1 2 3] # [[1 2 3]
# [4 5 6]] cm # [4 5 6]] cm
# >>> print a2 / millimeters # >>> print(a2 / millimeters)
# [[ 10. 20. 30.] # [[ 10. 20. 30.]
# [ 40. 50. 60.]] # [ 40. 50. 60.]]
...@@ -280,13 +280,13 @@ Addition, subtraction, multiplication, division, and powers of Quantities ...@@ -280,13 +280,13 @@ Addition, subtraction, multiplication, division, and powers of Quantities
exhibit correct dimensional analysis and unit conversion. exhibit correct dimensional analysis and unit conversion.
>>> x = 1.3 * meters >>> x = 1.3 * meters
>>> y = 75.2 * centimeters >>> y = 75.2 * centimeters
>>> print x + y >>> print(x + y)
2.052 m 2.052 m
>>> print x - y >>> print(x - y)
0.548 m 0.548 m
>>> print x/y >>> print(x/y)
1.72872340426 1.72872340426
>>> print x*y >>> print(x*y)
0.9776 m**2 0.9776 m**2
The following examples are derived from the C++ Boost.Units examples at The following examples are derived from the C++ Boost.Units examples at
...@@ -294,61 +294,61 @@ http://www.boost.org/doc/libs/1_37_0/doc/html/boost_units/Examples.html ...@@ -294,61 +294,61 @@ http://www.boost.org/doc/libs/1_37_0/doc/html/boost_units/Examples.html
>>> >>>
>>> l = 2.0 * meters >>> l = 2.0 * meters
>>> >>>
>>> print l + 2.0 * nanometers >>> print(l + 2.0 * nanometers)
2.000000002 m 2.000000002 m
>>> print 2.0 * nanometers + l >>> print(2.0 * nanometers + l)
2000000002.0 nm 2000000002.0 nm
>>> >>>
>>> print l >>> print(l)
2.0 m 2.0 m
>>> print l+l >>> print(l+l)
4.0 m 4.0 m
>>> print l-l >>> print(l-l)
0.0 m 0.0 m
>>> print l*l >>> print(l*l)
4.0 m**2 4.0 m**2
>>> print l/l >>> print(l/l)
1.0 1.0
>>> print l * meter >>> print(l * meter)
2.0 m**2 2.0 m**2
>>> print kilograms * (l/seconds) * (l/seconds) >>> print(kilograms * (l/seconds) * (l/seconds))
4.0 kg m**2/(s**2) 4.0 kg m**2/(s**2)
>>> print kilograms * (l/seconds)**2 >>> print(kilograms * (l/seconds)**2)
4.0 kg m**2/(s**2) 4.0 kg m**2/(s**2)
>>> print l ** 3 >>> print(l ** 3)
8.0 m**3 8.0 m**3
>>> print l ** (3.0/2.0) >>> print(l ** (3.0/2.0))
2.82842712475 m**1.5 2.82842712475 m**1.5
>>> print l ** 0.5 >>> print(l ** 0.5)
1.41421356237 m**0.5 1.41421356237 m**0.5
>>> print l ** (2.0/3.0) >>> print(l ** (2.0/3.0))
1.58740105197 m**0.666667 1.58740105197 m**0.666667
>>> # complex example >>> # complex example
>>> l = (3.0 + 4.0j) * meters >>> l = (3.0 + 4.0j) * meters
>>> print l >>> print(l)
(3+4j) m (3+4j) m
>>> print l+l >>> print(l+l)
(6+8j) m (6+8j) m
>>> print l-l >>> print(l-l)
0j m 0j m
>>> print l*l >>> print(l*l)
(-7+24j) m**2 (-7+24j) m**2
>>> # Numerical error yields tiny imaginary component of l/l on linux CentOS5 >>> # Numerical error yields tiny imaginary component of l/l on linux CentOS5
>>> err = abs(l/l - 1) >>> err = abs(l/l - 1)
>>> assert err < 1e-8 >>> assert err < 1e-8
>>> print l * meter >>> print(l * meter)
(3+4j) m**2 (3+4j) m**2
>>> print kilograms * (l/seconds) * (l/seconds) >>> print(kilograms * (l/seconds) * (l/seconds))
(-7+24j) kg m**2/(s**2) (-7+24j) kg m**2/(s**2)
>>> print kilograms * (l/seconds)**2 >>> print(kilograms * (l/seconds)**2)
(-7+24j) kg m**2/(s**2) (-7+24j) kg m**2/(s**2)
>>> print l ** 3 >>> print(l ** 3)
(-117+44j) m**3 (-117+44j) m**3
>>> print l ** (3.0/2.0) >>> print(l ** (3.0/2.0))
(2+11j) m**1.5 (2+11j) m**1.5
>>> print l ** 0.5 >>> print(l ** 0.5)
(2+1j) m**0.5 (2+1j) m**0.5
>>> print l ** (2.0/3.0) >>> print(l ** (2.0/3.0))
(2.38285471252+1.69466313833j) m**0.666667 (2.38285471252+1.69466313833j) m**0.666667
>>> # kitchen sink example >>> # kitchen sink example
... s1 = 2.0 ... s1 = 2.0
...@@ -358,57 +358,57 @@ http://www.boost.org/doc/libs/1_37_0/doc/html/boost_units/Examples.html ...@@ -358,57 +358,57 @@ http://www.boost.org/doc/libs/1_37_0/doc/html/boost_units/Examples.html
>>> u2 = u1 * meter >>> u2 = u1 * meter
>>> q1 = 1.0*u1 >>> q1 = 1.0*u1
>>> q2 = 2.0*u2 >>> q2 = 2.0*u2
>>> print s1 >>> print(s1)
2.0 2.0
>>> print x1 >>> print(x1)
2 2
>>> print x2 >>> print(x2)
1.33333333333 1.33333333333
>>> print u1 >>> print(u1)
kilogram*meter/(second**2) kilogram*meter/(second**2)
>>> print u2 >>> print(u2)
kilogram*meter**2/(second**2) kilogram*meter**2/(second**2)
>>> print q1 >>> print(q1)
1.0 kg m/(s**2) 1.0 kg m/(s**2)
>>> print q2 >>> print(q2)
2.0 kg m**2/(s**2) 2.0 kg m**2/(s**2)
>>> print u1*s1 >>> print(u1*s1)
2.0 kg m/(s**2) 2.0 kg m/(s**2)
>>> print s1*u1 >>> print(s1*u1)
2.0 kg m/(s**2) 2.0 kg m/(s**2)
>>> print u1/s1 >>> print(u1/s1)
0.5 kg m/(s**2) 0.5 kg m/(s**2)
>>> print s1/u1 >>> print(s1/u1)
2.0 s**2/(kg m) 2.0 s**2/(kg m)
>>> print u1*u1 >>> print(u1*u1)
kilogram**2*meter**2/(second**4) kilogram**2*meter**2/(second**4)
>>> print u1/u1 >>> print(u1/u1)
dimensionless dimensionless
>>> print u1*u2 >>> print(u1*u2)
kilogram**2*meter**3/(second**4) kilogram**2*meter**3/(second**4)
>>> print u1/u2 >>> print(u1/u2)
/meter /meter
>>> print u1**x1 >>> print(u1**x1)
kilogram**2*meter**2/(second**4) kilogram**2*meter**2/(second**4)
>>> print u1**(1.0/x1) >>> print(u1**(1.0/x1))
kilogram**0.5*meter**0.5/second kilogram**0.5*meter**0.5/second
>>> print u1**x2 >>> print(u1**x2)
kilogram**1.33333*meter**1.33333/(second**2.66667) kilogram**1.33333*meter**1.33333/(second**2.66667)
>>> print u1**(1.0/x2) >>> print(u1**(1.0/x2))
kilogram**0.75*meter**0.75/(second**1.5) kilogram**0.75*meter**0.75/(second**1.5)
>>> l1 = 1.0*meters >>> l1 = 1.0*meters
>>> l2 = 2.0*meters >>> l2 = 2.0*meters
>>> print l1 == l2 >>> print(l1 == l2)
False False
>>> print l1 != l2 >>> print(l1 != l2)
True True
>>> print l1 <= l2 >>> print(l1 <= l2)
True True
>>> print l1 < l2 >>> print(l1 < l2)
True True
>>> print l1 >= l2 >>> print(l1 >= l2)
False False
>>> print l1 > l2 >>> print(l1 > l2)
False False
>>> >>>
>>> def work(f, dx): >>> def work(f, dx):
...@@ -418,15 +418,15 @@ False ...@@ -418,15 +418,15 @@ False
>>> dx = 1.0 * meter >>> dx = 1.0 * meter
>>> E = work(F, dx) >>> E = work(F, dx)
>>> >>>
>>> print "F = ", F >>> print("F = ", F)
F = 1.0 kg m/(s**2) F = 1.0 kg m/(s**2)
>>> print "dx = ", dx >>> print("dx = ", dx)
dx = 1.0 m dx = 1.0 m
>>> >>>
>>> def idealGasLaw(P, V, T): >>> def idealGasLaw(P, V, T):
... R = MOLAR_GAS_CONSTANT_R ... R = MOLAR_GAS_CONSTANT_R
... print "P * V = ", P * V ... print("P * V = ", P * V)
... print "R * T = ", R * T ... print("R * T = ", R * T)
... return (P * V / (R * T)).in_units_of(mole) ... return (P * V / (R * T)).in_units_of(mole)
... ...
>>> T = (273.0 + 37.0) * kelvin >>> T = (273.0 + 37.0) * kelvin
...@@ -438,82 +438,82 @@ P * V = 5.3053601125e-14 m**3 Pa ...@@ -438,82 +438,82 @@ P * V = 5.3053601125e-14 m**3 Pa
R * T = 2577.48646608 J/mol R * T = 2577.48646608 J/mol
>>> R = MOLAR_GAS_CONSTANT_R >>> R = MOLAR_GAS_CONSTANT_R
>>> >>>
>>> print "r = ", r >>> print("r = ", r)
r = 5e-07 m r = 5e-07 m
>>> print "P = ", P >>> print("P = ", P)
P = 101325.0 Pa P = 101325.0 Pa
>>> print "V = ", V >>> print("V = ", V)
V = 5.23598333333e-19 m**3 V = 5.23598333333e-19 m**3
>>> print "T = ", T >>> print("T = ", T)
T = 310.0 K T = 310.0 K
>>> print "n = ", n >>> print("n = ", n)
n = 2.05834644811e-17 mol n = 2.05834644811e-17 mol
>>> print "R = ", R >>> print("R = ", R)
R = 8.31447247122 J/(K mol) R = 8.31447247122 J/(K mol)
>>> print "E = ", E >>> print("E = ", E)
E = 1.0 kg m**2/(s**2) E = 1.0 kg m**2/(s**2)
>>> print "is_quantity(V) = ", is_quantity(V) >>> print("is_quantity(V) = ", is_quantity(V))
is_quantity(V) = True is_quantity(V) = True
>>> print (1.0*radians) / degrees >>> print((1.0*radians) / degrees)
57.2957795131 57.2957795131
>>> print (1.0*radians).in_units_of(degrees) >>> print((1.0*radians).in_units_of(degrees))
57.2957795131 deg 57.2957795131 deg
>>> print (1.0*angstroms).in_units_of(nanometers) >>> print((1.0*angstroms).in_units_of(nanometers))
0.1 nm 0.1 nm
>>> >>>
>>> print (90*degrees)/radians >>> print((90*degrees)/radians)
1.57079632679 1.57079632679
>>> print sin(90*degrees) >>> print(sin(90*degrees))
1.0 1.0
>>> x = 90 * degrees >>> x = 90 * degrees
>>> x += 0.3 * radians >>> x += 0.3 * radians
>>> print x >>> print(x)
107.188733854 deg 107.188733854 deg
>>> print 1 * nanometers > 1 * angstroms >>> print(1 * nanometers > 1 * angstroms)
True True
>>> print 1 * nanometers > 1 * degrees >>> print(1 * nanometers > 1 * degrees)
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: Unit "degree" is not compatible with Unit "nanometer". TypeError: Unit "degree" is not compatible with Unit "nanometer".
>>> >>>
>>> x = 1.5 * nanometers >>> x = 1.5 * nanometers
>>> print x / meters >>> print(x / meters)
1.5e-09 1.5e-09
>>> x = 1.5 * angstroms >>> x = 1.5 * angstroms
>>> print x / meters >>> print(x / meters)
1.5e-10 1.5e-10
>>> print x / nanometers >>> print(x / nanometers)
0.15 0.15
Examples Examples
>>> print is_quantity(meters) >>> print(is_quantity(meters))
False False
>>> print is_quantity(2.3*meters) >>> print(is_quantity(2.3*meters))
True True
>>> print is_quantity(2.3) >>> print(is_quantity(2.3))
False False
Examples Examples
>>> x = 100.0 * millimeter >>> x = 100.0 * millimeter
>>> print x.value_in_unit_system(si_unit_system) >>> print(x.value_in_unit_system(si_unit_system))
0.1 0.1
>>> print x.value_in_unit_system(cgs_unit_system) >>> print(x.value_in_unit_system(cgs_unit_system))
10.0 10.0
>>> print x.value_in_unit_system(md_unit_system) >>> print(x.value_in_unit_system(md_unit_system))
100000000.0 100000000.0
>>> >>>
>>> y = 20 * millimeters / millisecond**2 >>> y = 20 * millimeters / millisecond**2
>>> print y.value_in_unit_system(si_unit_system) >>> print(y.value_in_unit_system(si_unit_system))
20000.0 20000.0
>>> print y.value_in_unit_system(cgs_unit_system) >>> print(y.value_in_unit_system(cgs_unit_system))
2000000.0 2000000.0
>>> print y.value_in_unit_system(md_unit_system) >>> print(y.value_in_unit_system(md_unit_system))
2e-11 2e-11
>>> eps = Quantity(1.0, md_kilocalorie/mole) >>> eps = Quantity(1.0, md_kilocalorie/mole)
>>> epsQ = eps.value_in_unit_system(md_unit_system) >>> epsQ = eps.value_in_unit_system(md_unit_system)
>>> print epsQ >>> print(epsQ)
4.184 4.184
Dimensionless quantities return their unmodified values. Dimensionless quantities return their unmodified values.
...@@ -523,32 +523,32 @@ Dimensionless quantities return their unmodified values. ...@@ -523,32 +523,32 @@ Dimensionless quantities return their unmodified values.
Examples Examples
>>> x = 2.3*meters >>> x = 2.3*meters
>>> print x.value_in_unit(centimeters) >>> print(x.value_in_unit(centimeters))
230.0 230.0
Examples Examples
>>> print bool(2.3*meters) >>> print(bool(2.3*meters))
True True
>>> print bool(0*meters) >>> print(bool(0*meters))
False False
Examples Examples
>>> print -(2.3*meters) >>> print(-(2.3*meters))
-2.3 m -2.3 m
>>> print -(-2.3*meters) >>> print(-(-2.3*meters))
2.3 m 2.3 m
Examples Examples
>>> print +(2.3*meters) >>> print(+(2.3*meters))
2.3 m 2.3 m
Examples Examples
>>> print abs(-2.3*meters) >>> print(abs(-2.3*meters))
2.3 m 2.3 m
>>> (9.0*meter*meter).sqrt() >>> (9.0*meter*meter).sqrt()
...@@ -572,22 +572,22 @@ Quantity(value=2.0454828280872954, unit=kilogram*meter/second) ...@@ -572,22 +572,22 @@ Quantity(value=2.0454828280872954, unit=kilogram*meter/second)
Examples Examples
>>> print (2.3*meters)**2 >>> print((2.3*meters)**2)
5.29 m**2 5.29 m**2
Examples Examples
>>> x = 4.2 * centimeters >>> x = 4.2 * centimeters
>>> print 8.4 / x >>> print(8.4 / x)
2.0 /cm 2.0 /cm
Examples Examples
>>> x = 4.3 * meters >>> x = 4.3 * meters
>>> print x/centimeters >>> print(x/centimeters)
430.0 430.0
>>> print x/seconds >>> print(x/seconds)
4.3 m/s 4.3 m/s
>>> x = [1,2,3]*centimeter >>> x = [1,2,3]*centimeter
>>> x/millimeter >>> x/millimeter
...@@ -597,14 +597,14 @@ Examples ...@@ -597,14 +597,14 @@ Examples
Examples Examples
>>> x = 1.2*meters >>> x = 1.2*meters
>>> print 5*x >>> print(5*x)
6.0 m 6.0 m
Examples Examples
>>> x = 1.2*meters >>> x = 1.2*meters
>>> y = 72*centimeters >>> y = 72*centimeters
>>> print x*y >>> print(x*y)
0.864 m**2 0.864 m**2
>>> x = [1,2,3]*centimeter >>> x = [1,2,3]*centimeter
>>> x >>> x
...@@ -613,7 +613,7 @@ Examples ...@@ -613,7 +613,7 @@ Examples
Quantity(value=[100.0, 200.0, 300.0], unit=centimeter**2) Quantity(value=[100.0, 200.0, 300.0], unit=centimeter**2)
>>> u = nanometer**2/angstrom**2 >>> u = nanometer**2/angstrom**2
>>> print u >>> print(u)
nanometer**2/(angstrom**2) nanometer**2/(angstrom**2)
>>> q = Quantity(2.0, u) >>> q = Quantity(2.0, u)
>>> q >>> q
...@@ -632,23 +632,23 @@ Examples ...@@ -632,23 +632,23 @@ Examples
Examples Examples
>>> print 1.2 * meters - 72 * centimeters >>> print(1.2 * meters - 72 * centimeters)
0.48 m 0.48 m
Examples Examples
>>> print 1.2 * meters + 72 * centimeters >>> print(1.2 * meters + 72 * centimeters)
1.92 m 1.92 m
Examples Examples
>>> print repr(1.2*meter) >>> print(repr(1.2*meter))
Quantity(value=1.2, unit=meter) Quantity(value=1.2, unit=meter)
Examples Examples
>>> print 5.0 * nanometers >>> print(5.0 * nanometers)
5.0 nm 5.0 nm
Examples Examples
...@@ -689,13 +689,13 @@ Examples ...@@ -689,13 +689,13 @@ Examples
>>> x = 2.3*meters >>> x = 2.3*meters
>>> y = x.in_units_of(centimeters) >>> y = x.in_units_of(centimeters)
>>> print y >>> print(y)
230.0 cm 230.0 cm
>>> x = 2.3*meters >>> x = 2.3*meters
>>> print x.in_units_of(centimeters) >>> print(x.in_units_of(centimeters))
230.0 cm 230.0 cm
>>> print x.in_units_of(seconds) >>> print(x.in_units_of(seconds))
Traceback (most recent call last): Traceback (most recent call last):
... ...
TypeError: Unit "meter" is not compatible with Unit "second". TypeError: Unit "meter" is not compatible with Unit "second".
...@@ -703,34 +703,34 @@ Examples ...@@ -703,34 +703,34 @@ Examples
Examples Examples
>>> x = 100.0 * millimeter >>> x = 100.0 * millimeter
>>> print x >>> print(x)
100.0 mm 100.0 mm
>>> print x.in_unit_system(si_unit_system) >>> print(x.in_unit_system(si_unit_system))
0.1 m 0.1 m
>>> print x.in_unit_system(cgs_unit_system) >>> print(x.in_unit_system(cgs_unit_system))
10.0 cm 10.0 cm
>>> print x.in_unit_system(md_unit_system) >>> print(x.in_unit_system(md_unit_system))
100000000.0 nm 100000000.0 nm
>>> y = 20 * millimeters / millisecond**2 >>> y = 20 * millimeters / millisecond**2
>>> print y >>> print(y)
20 mm/(ms**2) 20 mm/(ms**2)
>>> print y.in_unit_system(si_unit_system) >>> print(y.in_unit_system(si_unit_system))
20000.0 m/(s**2) 20000.0 m/(s**2)
>>> print y.in_unit_system(cgs_unit_system) >>> print(y.in_unit_system(cgs_unit_system))
2000000.0 cm/(s**2) 2000000.0 cm/(s**2)
>>> print y.in_unit_system(md_unit_system) >>> print(y.in_unit_system(md_unit_system))
2e-11 nm/(ps**2) 2e-11 nm/(ps**2)
Sometimes mixed internal units have caused trouble: Sometimes mixed internal units have caused trouble:
>>> q = 1.0 * md_kilocalorie/mole/angstrom >>> q = 1.0 * md_kilocalorie/mole/angstrom
>>> print q.in_units_of(md_kilojoule/mole/nanometer) >>> print(q.in_units_of(md_kilojoule/mole/nanometer))
41.84 kJ/(nm mol) 41.84 kJ/(nm mol)
Examples Examples
>>> class Foo: >>> class Foo:
... def bar(self): ... def bar(self):
... print "bar" ... print("bar")
... ...
>>> x = Foo() >>> x = Foo()
>>> x.bar() >>> x.bar()
...@@ -741,24 +741,24 @@ Examples ...@@ -741,24 +741,24 @@ Examples
Examples Examples
>>> print meters * centimeters >>> print(meters * centimeters)
centimeter*meter centimeter*meter
>>> print meters * meters >>> print(meters * meters)
meter**2 meter**2
>>> print meter * meter >>> print(meter * meter )
meter**2 meter**2
Examples Examples
>>> print meter / 2 >>> print(meter / 2)
0.5 m 0.5 m
Examples Examples
>>> define_prefixed_units(kelvin_base_unit, sys.modules["__main__"]) >>> define_prefixed_units(kelvin_base_unit, sys.modules["__main__"])
>>> from __main__ import millikelvin >>> from __main__ import millikelvin
>>> print 5.0 * millikelvin >>> print(5.0 * millikelvin)
5.0 mK 5.0 mK
...@@ -821,17 +821,17 @@ Examples ...@@ -821,17 +821,17 @@ Examples
>>> V = 2.4 * nanometer**3 >>> V = 2.4 * nanometer**3
>>> beta = 4.e-4 * mole/joule >>> beta = 4.e-4 * mole/joule
>>> x1 = beta*p1*V >>> x1 = beta*p1*V
>>> # print x1 >>> # print(x1)
... y1 = x1 * AVOGADRO_CONSTANT_NA ... y1 = x1 * AVOGADRO_CONSTANT_NA
>>> print y1 >>> print(y1)
0.0585785776197 0.0585785776197
# Wrong answer is 5.85785776197e+25 # Wrong answer is 5.85785776197e+25
>>> x2 = beta*p2*V >>> x2 = beta*p2*V
>>> # print x2 >>> # print(x2)
... y2 = x2 * AVOGADRO_CONSTANT_NA ... y2 = x2 * AVOGADRO_CONSTANT_NA
>>> print y2 >>> print(y2)
0.0585785776197 0.0585785776197
>>> assert( abs(y1 - y2) < 0.01) >>> assert( abs(y1 - y2) < 0.01)
...@@ -863,6 +863,8 @@ Examples ...@@ -863,6 +863,8 @@ Examples
""" """
from __future__ import print_function
__author__ = "Christopher M. Bruns" __author__ = "Christopher M. Bruns"
__version__ = "0.5" __version__ = "0.5"
......
...@@ -8,7 +8,7 @@ def eye(size): ...@@ -8,7 +8,7 @@ def eye(size):
""" """
Returns identity matrix. Returns identity matrix.
>>> print eye(3) >>> print(eye(3))
[[1, 0, 0] [[1, 0, 0]
[0, 1, 0] [0, 1, 0]
[0, 0, 1]] [0, 0, 1]]
...@@ -28,7 +28,7 @@ def zeros(m, n=None): ...@@ -28,7 +28,7 @@ def zeros(m, n=None):
""" """
Returns matrix of zeroes Returns matrix of zeroes
>>> print zeros(3) >>> print(zeros(3))
[[0, 0, 0] [[0, 0, 0]
[0, 0, 0] [0, 0, 0]
[0, 0, 0]] [0, 0, 0]]
...@@ -94,13 +94,13 @@ class MyMatrix(MyVector): ...@@ -94,13 +94,13 @@ class MyMatrix(MyVector):
Pure python linear algebra matrix for internal matrix inversion in UnitSystem. Pure python linear algebra matrix for internal matrix inversion in UnitSystem.
>>> m = MyMatrix([[1,0,],[0,1,]]) >>> m = MyMatrix([[1,0,],[0,1,]])
>>> print m >>> print(m)
[[1, 0] [[1, 0]
[0, 1]] [0, 1]]
>>> print ~m >>> print(~m)
[[1.0, 0.0] [[1.0, 0.0]
[0.0, 1.0]] [0.0, 1.0]]
>>> print eye(5) >>> print(eye(5))
[[1, 0, 0, 0, 0] [[1, 0, 0, 0, 0]
[0, 1, 0, 0, 0] [0, 1, 0, 0, 0]
[0, 0, 1, 0, 0] [0, 0, 1, 0, 0]
...@@ -111,18 +111,18 @@ class MyMatrix(MyVector): ...@@ -111,18 +111,18 @@ class MyMatrix(MyVector):
1 1
>>> m[1:4] >>> m[1:4]
MyMatrixTranspose([[0, 0, 0],[1, 0, 0],[0, 1, 0],[0, 0, 1],[0, 0, 0]]) MyMatrixTranspose([[0, 0, 0],[1, 0, 0],[0, 1, 0],[0, 0, 1],[0, 0, 0]])
>>> print m[1:4] >>> print(m[1:4])
[[0, 0, 0] [[0, 0, 0]
[1, 0, 0] [1, 0, 0]
[0, 1, 0] [0, 1, 0]
[0, 0, 1] [0, 0, 1]
[0, 0, 0]] [0, 0, 0]]
>>> print m[1:4][0:2] >>> print(m[1:4][0:2])
[[0, 1] [[0, 1]
[0, 0] [0, 0]
[0, 0]] [0, 0]]
>>> m[1:4][0:2] = [[9,8],[7,6],[5,4]] >>> m[1:4][0:2] = [[9,8],[7,6],[5,4]]
>>> print m >>> print(m)
[[1, 0, 0, 0, 0] [[1, 0, 0, 0, 0]
[9, 8, 0, 0, 0] [9, 8, 0, 0, 0]
[7, 6, 1, 0, 0] [7, 6, 1, 0, 0]
...@@ -182,13 +182,13 @@ class MyMatrix(MyVector): ...@@ -182,13 +182,13 @@ class MyMatrix(MyVector):
>>> a = MyMatrix([[1,2],[3,4]]) >>> a = MyMatrix([[1,2],[3,4]])
>>> b = MyMatrix([[5,6],[7,8]]) >>> b = MyMatrix([[5,6],[7,8]])
>>> print a >>> print(a)
[[1, 2] [[1, 2]
[3, 4]] [3, 4]]
>>> print b >>> print(b)
[[5, 6] [[5, 6]
[7, 8]] [7, 8]]
>>> print a*b >>> print(a*b)
[[19, 22] [[19, 22]
[43, 50]] [43, 50]]
...@@ -209,7 +209,7 @@ class MyMatrix(MyVector): ...@@ -209,7 +209,7 @@ class MyMatrix(MyVector):
""" """
Matrix addition. Matrix addition.
>>> print MyMatrix([[1, 2],[3, 4]]) + MyMatrix([[5, 6],[7, 8]]) >>> print(MyMatrix([[1, 2],[3, 4]]) + MyMatrix([[5, 6],[7, 8]]))
[[6, 8] [[6, 8]
[10, 12]] [10, 12]]
""" """
...@@ -227,7 +227,7 @@ class MyMatrix(MyVector): ...@@ -227,7 +227,7 @@ class MyMatrix(MyVector):
""" """
Matrix subtraction. Matrix subtraction.
>>> print MyMatrix([[1, 2],[3, 4]]) - MyMatrix([[5, 6],[7, 8]]) >>> print(MyMatrix([[1, 2],[3, 4]]) - MyMatrix([[5, 6],[7, 8]]))
[[-4, -4] [[-4, -4]
[-4, -4]] [-4, -4]]
""" """
...@@ -256,32 +256,32 @@ class MyMatrix(MyVector): ...@@ -256,32 +256,32 @@ class MyMatrix(MyVector):
def __invert__(self): def __invert__(self):
""" """
>>> m = MyMatrix([[1,1],[0,1]]) >>> m = MyMatrix([[1,1],[0,1]])
>>> print m >>> print(m)
[[1, 1] [[1, 1]
[0, 1]] [0, 1]]
>>> print ~m >>> print(~m)
[[1.0, -1.0] [[1.0, -1.0]
[0.0, 1.0]] [0.0, 1.0]]
>>> print m*~m >>> print(m*~m)
[[1.0, 0.0] [[1.0, 0.0]
[0.0, 1.0]] [0.0, 1.0]]
>>> print ~m*m >>> print(~m*m)
[[1.0, 0.0] [[1.0, 0.0]
[0.0, 1.0]] [0.0, 1.0]]
>>> m = MyMatrix([[1,0,0],[0,0,1],[0,-1,0]]) >>> m = MyMatrix([[1,0,0],[0,0,1],[0,-1,0]])
>>> print m >>> print(m)
[[1, 0, 0] [[1, 0, 0]
[0, 0, 1] [0, 0, 1]
[0, -1, 0]] [0, -1, 0]]
>>> print ~m >>> print(~m)
[[1.0, 0.0, 0.0] [[1.0, 0.0, 0.0]
[0.0, 0.0, -1.0] [0.0, 0.0, -1.0]
[0.0, 1.0, 0.0]] [0.0, 1.0, 0.0]]
>>> print m*~m >>> print(m*~m)
[[1.0, 0.0, 0.0] [[1.0, 0.0, 0.0]
[0.0, 1.0, 0.0] [0.0, 1.0, 0.0]
[0.0, 0.0, 1.0]] [0.0, 0.0, 1.0]]
>>> print ~m*m >>> print(~m*m)
[[1.0, 0.0, 0.0] [[1.0, 0.0, 0.0]
[0.0, 1.0, 0.0] [0.0, 1.0, 0.0]
[0.0, 0.0, 1.0]] [0.0, 0.0, 1.0]]
......
...@@ -39,6 +39,8 @@ Two possible enhancements that have not been implemented are ...@@ -39,6 +39,8 @@ Two possible enhancements that have not been implemented are
2) Incorporate offsets for celsius <-> kelvin conversion 2) Incorporate offsets for celsius <-> kelvin conversion
""" """
from __future__ import division
__author__ = "Christopher M. Bruns" __author__ = "Christopher M. Bruns"
__version__ = "0.5" __version__ = "0.5"
...@@ -232,25 +234,23 @@ class Quantity(object): ...@@ -232,25 +234,23 @@ class Quantity(object):
""" """
if not is_quantity(other): if not is_quantity(other):
return False return False
else: if not self.unit.is_compatible(other.unit):
return NotImplemented # punt to cmp return False
return self.value_in_unit(other.unit) == other._value
def __ne__(self, other): def __ne__(self, other):
""" """
""" """
if not is_quantity(other): return not self.__eq__(other)
return True
else:
return NotImplemented # punt to cmp
def __cmp__(self, other): def __lt__(self, other):
"""Compares two quantities. """Compares two quantities.
Raises TypeError if the Quantities are of different dimension (e.g. length vs. mass) Raises TypeError if the Quantities are of different dimension (e.g. length vs. mass)
Returns -1 if self < other, 0 if self == other, and 1 if self > other. Returns True if self < other, False otherwise.
""" """
return cmp(self._value, (other.value_in_unit(self.unit))) return self._value < other.value_in_unit(self.unit)
def __ge__(self, other): def __ge__(self, other):
return self._value >= (other.value_in_unit(self.unit)) return self._value >= (other.value_in_unit(self.unit))
...@@ -363,7 +363,7 @@ class Quantity(object): ...@@ -363,7 +363,7 @@ class Quantity(object):
return self._change_units_with_factor(self.unit, other, post_multiply=True) return self._change_units_with_factor(self.unit, other, post_multiply=True)
# return Quantity(other * self._value, self.unit) # return Quantity(other * self._value, self.unit)
def __div__(self, other): def __truediv__(self, other):
"""Divide a Quantity by another object """Divide a Quantity by another object
Returns a new Quantity, unless the resulting unit type is dimensionless, Returns a new Quantity, unless the resulting unit type is dimensionless,
...@@ -383,16 +383,16 @@ class Quantity(object): ...@@ -383,16 +383,16 @@ class Quantity(object):
return self * pow(other, -1.0) return self * pow(other, -1.0)
# return Quantity(self._value / other, self.unit) # return Quantity(self._value / other, self.unit)
def __rdiv__(self, other): def __rtruediv__(self, other):
"""Divide a scalar by a quantity. """Divide a scalar by a quantity.
Returns a new Quantity. The resulting units are the inverse of the self argument units. Returns a new Quantity. The resulting units are the inverse of the self argument units.
""" """
if is_unit(other): if is_unit(other):
# print "R unit / quantity" # print "R unit / quantity"
raise NotImplementedError('programmer is surprised __rdiv__ was called instead of __div__') raise NotImplementedError('programmer is surprised __rtruediv__ was called instead of __truediv__')
elif is_quantity(other): elif is_quantity(other):
raise NotImplementedError('programmer is surprised __rdiv__ was called instead of __div__') raise NotImplementedError('programmer is surprised __rtruediv__ was called instead of __truediv__')
else: else:
# print "R scalar / quantity" # print "R scalar / quantity"
return other * pow(self, -1.0) return other * pow(self, -1.0)
......
...@@ -5,6 +5,8 @@ Module simtk.unit ...@@ -5,6 +5,8 @@ Module simtk.unit
Contains classes Unit and ScaledUnit. Contains classes Unit and ScaledUnit.
""" """
from __future__ import division
__author__ = "Christopher M. Bruns" __author__ = "Christopher M. Bruns"
__version__ = "0.5" __version__ = "0.5"
...@@ -149,17 +151,16 @@ class Unit(object): ...@@ -149,17 +151,16 @@ class Unit(object):
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
def __cmp__(self, other): def __lt__(self, other):
"""Compare two Units. """Compare two Units.
Raises a TypeError if the units have different dimensions. Raises a TypeError if the units have different dimensions.
Returns 0 if the Units are equal, -1 if the first Unit is smaller, Returns True if self < other, False otherwise.
and returns 1 if the first Unit is larger.
""" """
if not self.is_compatible(other): if not self.is_compatible(other):
raise TypeError('Unit "%s" is not compatible with Unit "%s".', (self, other)) raise TypeError('Unit "%s" is not compatible with Unit "%s".', (self, other))
return cmp(self.conversion_factor_to(other), 1.0) return self.conversion_factor_to(other) < 1.0
def __hash__(self): def __hash__(self):
""" """
...@@ -175,7 +176,7 @@ class Unit(object): ...@@ -175,7 +176,7 @@ class Unit(object):
# def __mul__(self, other): # def __mul__(self, other):
# See unit_operators.py for Unit.__mul__ operator # See unit_operators.py for Unit.__mul__ operator
def __div__(self, other): def __truediv__(self, other):
"""Divide a Unit by another object. """Divide a Unit by another object.
Returns a composite Unit if other is another Unit. Returns a composite Unit if other is another Unit.
...@@ -186,8 +187,8 @@ class Unit(object): ...@@ -186,8 +187,8 @@ class Unit(object):
""" """
return self * pow(other, -1) return self * pow(other, -1)
# def __rdiv__(self, other): # def __rtruediv__(self, other):
# Because rdiv returns a Quantity, look in quantity.py for definition of Unit.__rdiv__ # Because rtruediv returns a Quantity, look in quantity.py for definition of Unit.__rtruediv__
_pow_cache = {} _pow_cache = {}
...@@ -522,6 +523,11 @@ class ScaledUnit(object): ...@@ -522,6 +523,11 @@ class ScaledUnit(object):
else: else:
other_u = Unit({other: 1.0}) other_u = Unit({other: 1.0})
return self.factor * Unit(u).conversion_factor_to(other_u) return self.factor * Unit(u).conversion_factor_to(other_u)
def __lt__(self, other):
"""Compare two ScaledUnits.
"""
return hash(self) < hash(other)
def __str__(self): def __str__(self):
"""Returns a string with the name of this ScaledUnit """Returns a string with the name of this ScaledUnit
...@@ -569,7 +575,7 @@ class UnitSystem(object): ...@@ -569,7 +575,7 @@ class UnitSystem(object):
to_base_units[m][n] = power to_base_units[m][n] = power
try: try:
self.from_base_units = ~to_base_units self.from_base_units = ~to_base_units
except ArithmeticError, e: except ArithmeticError as e:
# for compatibility between python 2.5 and python 3.0, # for compatibility between python 2.5 and python 3.0,
# try replacing line above with the following two lines: # try replacing line above with the following two lines:
# except ArithmeticError: # except ArithmeticError:
......
...@@ -4,6 +4,8 @@ Module simtk.unit.unit_definitions ...@@ -4,6 +4,8 @@ Module simtk.unit.unit_definitions
""" """
from __future__ import division
__author__ = "Christopher M. Bruns" __author__ = "Christopher M. Bruns"
__version__ = "0.6" __version__ = "0.6"
......
...@@ -5,6 +5,8 @@ Module simtk.unit.math ...@@ -5,6 +5,8 @@ Module simtk.unit.math
Arithmetic methods on Quantities and Units Arithmetic methods on Quantities and Units
""" """
from __future__ import division
__author__ = "Christopher M. Bruns" __author__ = "Christopher M. Bruns"
__version__ = "0.5" __version__ = "0.5"
...@@ -69,7 +71,7 @@ def acos(x): ...@@ -69,7 +71,7 @@ def acos(x):
""" """
>>> acos(1.0) >>> acos(1.0)
Quantity(value=0.0, unit=radian) Quantity(value=0.0, unit=radian)
>>> print acos(1.0) >>> print(acos(1.0))
0.0 rad 0.0 rad
""" """
return math.acos(x) * radians return math.acos(x) * radians
...@@ -100,7 +102,7 @@ def sqrt(val): ...@@ -100,7 +102,7 @@ def sqrt(val):
""" """
>>> sqrt(9.0) >>> sqrt(9.0)
3.0 3.0
>>> print sqrt(meter*meter) >>> print(sqrt(meter*meter))
meter meter
>>> sqrt(9.0*meter*meter) >>> sqrt(9.0*meter*meter)
Quantity(value=3.0, unit=meter) Quantity(value=3.0, unit=meter)
......
...@@ -36,14 +36,14 @@ def _unit_class_rdiv(self, other): ...@@ -36,14 +36,14 @@ def _unit_class_rdiv(self, other):
of the inverse of self. of the inverse of self.
""" """
if is_unit(other): if is_unit(other):
raise NotImplementedError('programmer is surprised __rdiv__ was called instead of __div__') raise NotImplementedError('programmer is surprised __rtruediv__ was called instead of __truediv__')
else: else:
# print "R scalar / unit" # print "R scalar / unit"
unit = pow(self, -1.0) unit = pow(self, -1.0)
value = other value = other
return Quantity(value, unit).reduce_unit(self) return Quantity(value, unit).reduce_unit(self)
Unit.__rdiv__ = _unit_class_rdiv Unit.__rtruediv__ = _unit_class_rdiv
def _unit_class_mul(self, other): def _unit_class_mul(self, other):
......
...@@ -188,7 +188,7 @@ class SwigInputBuilder: ...@@ -188,7 +188,7 @@ class SwigInputBuilder:
def writeGlobalConstants(self): def writeGlobalConstants(self):
self.fOut.write("/* Global Constants */\n\n") self.fOut.write("/* Global Constants */\n\n")
node = (x for x in findNodes(self.doc.getroot(), "compounddef", kind="namespace") if x.findtext("compoundname") == "OpenMM").next() node = next((x for x in findNodes(self.doc.getroot(), "compounddef", kind="namespace") if x.findtext("compoundname") == "OpenMM"))
for section in findNodes(node, "sectiondef", kind="var"): for section in findNodes(node, "sectiondef", kind="var"):
for memberNode in findNodes(section, "memberdef", kind="variable", mutable="no", prot="public", static="yes"): for memberNode in findNodes(section, "memberdef", kind="variable", mutable="no", prot="public", static="yes"):
vDef = stripOpenmmPrefix(getText("definition", memberNode)) vDef = stripOpenmmPrefix(getText("definition", memberNode))
......
...@@ -250,9 +250,9 @@ def stripUnits(args): ...@@ -250,9 +250,9 @@ def stripUnits(args):
newArgList=[] newArgList=[]
for arg in args: for arg in args:
if unit.is_quantity(arg): if unit.is_quantity(arg):
# JDC: Ugly workaround for OpenMM using 'bar' for fundamental pressure unit. # JDC: Ugly workaround for OpenMM using 'bar' for fundamental pressure unit.
if arg.unit.is_compatible(unit.bar): if arg.unit.is_compatible(unit.bar):
arg = arg / unit.bar arg = arg / unit.bar
else: else:
arg=arg.value_in_unit_system(unit.md_unit_system) arg=arg.value_in_unit_system(unit.md_unit_system)
# JDC: End workaround. # JDC: End workaround.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment