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"
......
This diff is collapsed.
...@@ -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