Commit 74415dd9 authored by Lee-Ping Wang's avatar Lee-Ping Wang
Browse files

Merge branch 'master' of https://github.com/SimTk/openmm

parents 25c22045 6701dacc
...@@ -15,7 +15,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -15,7 +15,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
...@@ -40,14 +40,14 @@ __version__ = "0.6" ...@@ -40,14 +40,14 @@ __version__ = "0.6"
class BaseUnit(object): class BaseUnit(object):
''' '''
Physical unit expressed in exactly one BaseDimension. Physical unit expressed in exactly one BaseDimension.
For example, meter_base_unit could be a BaseUnit for the length dimension. For example, meter_base_unit could be a BaseUnit for the length dimension.
The BaseUnit class is used internally in the more general Unit class. The BaseUnit class is used internally in the more general Unit class.
''' '''
def __init__(self, base_dim, name, symbol): def __init__(self, base_dim, name, symbol):
"""Creates a new BaseUnit. """Creates a new BaseUnit.
Parameters Parameters
- self: The newly created BaseUnit. - self: The newly created BaseUnit.
- base_dim: (BaseDimension) The dimension of the new unit, e.g. 'mass' - base_dim: (BaseDimension) The dimension of the new unit, e.g. 'mass'
...@@ -62,7 +62,7 @@ class BaseUnit(object): ...@@ -62,7 +62,7 @@ class BaseUnit(object):
self._conversion_factor_to[self] = 1.0 self._conversion_factor_to[self] = 1.0
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 __lt__(self, other): def __lt__(self, other):
""" """
Comparison function that sorts BaseUnits by BaseDimension Comparison function that sorts BaseUnits by BaseDimension
...@@ -78,10 +78,10 @@ class BaseUnit(object): ...@@ -78,10 +78,10 @@ class BaseUnit(object):
Returns a dictionary of BaseDimension:exponent pairs, describing the dimension of this unit. Returns a dictionary of BaseDimension:exponent pairs, describing the dimension of this unit.
""" """
yield (self.dimension, 1) yield (self.dimension, 1)
def iter_base_units(self): def iter_base_units(self):
yield (self, 1) yield (self, 1)
def get_dimension_tuple(self): def get_dimension_tuple(self):
""" """
Returns a sorted tuple of (BaseDimension, exponent) pairs, that can be used as a dictionary key. Returns a sorted tuple of (BaseDimension, exponent) pairs, that can be used as a dictionary key.
...@@ -89,7 +89,7 @@ class BaseUnit(object): ...@@ -89,7 +89,7 @@ class BaseUnit(object):
l = list(self.iter_base_dimensions()) l = list(self.iter_base_dimensions())
l.sort() l.sort()
return tuple(l) return tuple(l)
def __str__(self): def __str__(self):
"""Returns a string with the name of this BaseUnit """Returns a string with the name of this BaseUnit
""" """
...@@ -97,40 +97,40 @@ class BaseUnit(object): ...@@ -97,40 +97,40 @@ class BaseUnit(object):
def __repr__(self): def __repr__(self):
return 'BaseUnit(base_dim=%s, name="%s", symbol="%s")' % (self.dimension, self.name, self.symbol) return 'BaseUnit(base_dim=%s, name="%s", symbol="%s")' % (self.dimension, self.name, self.symbol)
def define_conversion_factor_to(self, other, factor): def define_conversion_factor_to(self, other, factor):
""" """
Defines a conversion factor between two BaseUnits. Defines a conversion factor between two BaseUnits.
self * factor = other self * factor = other
Parameters: Parameters:
- self: (BaseUnit) 'From' unit in conversion. - self: (BaseUnit) 'From' unit in conversion.
- other: (BaseUnit) 'To' unit in conversion. - other: (BaseUnit) 'To' unit in conversion.
- factor: (float) Conversion factor. - factor: (float) Conversion factor.
After calling this method, both self and other will have stored After calling this method, both self and other will have stored
conversion factors for one another, plus all other BaseUnits which conversion factors for one another, plus all other BaseUnits which
self and other have previously defined. self and other have previously defined.
Both self and other must have the same dimension, otherwise a TypeError Both self and other must have the same dimension, otherwise a TypeError
will be raised. will be raised.
Returns None. Returns None.
""" """
if self.dimension != other.dimension: if self.dimension != other.dimension:
raise TypeError('Cannot define conversion for BaseUnits with different dimensions.') raise TypeError('Cannot define conversion for BaseUnits with different dimensions.')
assert(factor != 0) assert(factor != 0)
assert(not self is other) assert(not self is other)
# import all transitive conversions # import all transitive conversions
self._conversion_factor_to[other] = factor self._conversion_factor_to[other] = factor
self._conversion_factor_to_by_name[other.name] = factor self._conversion_factor_to_by_name[other.name] = factor
for (unit, cfac) in other._conversion_factor_to.items(): for (unit, cfac) in other._conversion_factor_to.items():
if unit is self: continue if unit is self: continue
if self._conversion_factor_to.has_key(unit): continue if self._conversion_factor_to.has_key(unit): continue
self._conversion_factor_to[unit] = factor * cfac self._conversion_factor_to[unit] = factor * cfac
unit._conversion_factor_to[self] = pow(factor * cfac, -1) unit._conversion_factor_to[self] = pow(factor * cfac, -1)
self._conversion_factor_to_by_name[unit.name] = factor * cfac self._conversion_factor_to_by_name[unit.name] = factor * cfac
unit._conversion_factor_to_by_name[self.name] = pow(factor * cfac, -1) unit._conversion_factor_to_by_name[self.name] = pow(factor * cfac, -1)
# and for the other guy # and for the other guy
invFac = pow(factor, -1.0) invFac = pow(factor, -1.0)
...@@ -146,13 +146,13 @@ class BaseUnit(object): ...@@ -146,13 +146,13 @@ class BaseUnit(object):
def conversion_factor_to(self, other): def conversion_factor_to(self, other):
"""Returns a conversion factor from this BaseUnit to another BaseUnit. """Returns a conversion factor from this BaseUnit to another BaseUnit.
It does not matter which existing BaseUnit you define the conversion factor to. It does not matter which existing BaseUnit you define the conversion factor to.
Conversions for all other known BaseUnits will be computed at the same time. Conversions for all other known BaseUnits will be computed at the same time.
Raises TypeError if dimension does not match. Raises TypeError if dimension does not match.
Raises LookupError if no conversion has been defined. (see define_conversion_factor_to). Raises LookupError if no conversion has been defined. (see define_conversion_factor_to).
""" """
if self is other: return 1.0 if self is other: return 1.0
if self.dimension != other.dimension: if self.dimension != other.dimension:
......
...@@ -11,7 +11,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -11,7 +11,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
......
This diff is collapsed.
...@@ -10,7 +10,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -10,7 +10,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
...@@ -34,7 +34,7 @@ import sys ...@@ -34,7 +34,7 @@ import sys
def eye(size): 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]
...@@ -50,11 +50,11 @@ def eye(size): ...@@ -50,11 +50,11 @@ def eye(size):
r.append(0) r.append(0)
result.append(r) result.append(r)
return MyMatrix(result) return MyMatrix(result)
def zeros(m, n=None): 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]
...@@ -82,7 +82,7 @@ class MyVector(object): ...@@ -82,7 +82,7 @@ class MyVector(object):
def __str__(self): def __str__(self):
return str(self.data) return str(self.data)
def __repr__(self): def __repr__(self):
return self.__class__.__name__ + "(" + repr(self.data) + ")" return self.__class__.__name__ + "(" + repr(self.data) + ")"
...@@ -91,20 +91,20 @@ class MyVector(object): ...@@ -91,20 +91,20 @@ class MyVector(object):
def __contains__(self, item): def __contains__(self, item):
return item in self.data return item in self.data
def __delitem__(self, key): def __delitem__(self, key):
del self.data[key] del self.data[key]
def __iter__(self): def __iter__(self):
for item in self.data: for item in self.data:
yield item yield item
def __len__(self): def __len__(self):
return len(self.data) return len(self.data)
def __setitem__(self, key, value): def __setitem__(self, key, value):
self.data[key] = value self.data[key] = value
def __rmul__(self, lhs): def __rmul__(self, lhs):
try: try:
len(lhs) len(lhs)
...@@ -119,7 +119,7 @@ class MyVector(object): ...@@ -119,7 +119,7 @@ class MyVector(object):
class MyMatrix(MyVector): 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]
...@@ -158,7 +158,7 @@ class MyMatrix(MyVector): ...@@ -158,7 +158,7 @@ class MyMatrix(MyVector):
""" """
def numRows(self): def numRows(self):
return len(self.data) return len(self.data)
def numCols(self): def numCols(self):
if len(self.data) == 0: if len(self.data) == 0:
return 0 return 0
...@@ -179,13 +179,13 @@ class MyMatrix(MyVector): ...@@ -179,13 +179,13 @@ class MyMatrix(MyVector):
start_char = " " start_char = " "
result += "]" result += "]"
return result return result
def __repr__(self): def __repr__(self):
return 'MyMatrix(' + MyVector.__repr__(self) + ')' return 'MyMatrix(' + MyVector.__repr__(self) + ')'
def is_square(self): def is_square(self):
return self.numRows() == self.numCols() return self.numRows() == self.numCols()
def __iter__(self): def __iter__(self):
for item in self.data: for item in self.data:
yield MyVector(item) yield MyVector(item)
...@@ -206,7 +206,7 @@ class MyMatrix(MyVector): ...@@ -206,7 +206,7 @@ class MyMatrix(MyVector):
def __mul__(self, rhs): def __mul__(self, rhs):
""" """
Matrix multiplication. Matrix multiplication.
>>> 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)
...@@ -218,7 +218,7 @@ class MyMatrix(MyVector): ...@@ -218,7 +218,7 @@ class MyMatrix(MyVector):
>>> print(a*b) >>> print(a*b)
[[19, 22] [[19, 22]
[43, 50]] [43, 50]]
""" """
m = self.numRows() m = self.numRows()
n = len(rhs[0]) n = len(rhs[0])
...@@ -235,7 +235,7 @@ class MyMatrix(MyVector): ...@@ -235,7 +235,7 @@ class MyMatrix(MyVector):
def __add__(self, rhs): def __add__(self, rhs):
""" """
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]]
...@@ -253,7 +253,7 @@ class MyMatrix(MyVector): ...@@ -253,7 +253,7 @@ class MyMatrix(MyVector):
def __sub__(self, rhs): def __sub__(self, rhs):
""" """
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]]
...@@ -270,7 +270,7 @@ class MyMatrix(MyVector): ...@@ -270,7 +270,7 @@ class MyMatrix(MyVector):
def __pos__(self): def __pos__(self):
return self return self
def __neg__(self): def __neg__(self):
m = self.numRows() m = self.numRows()
n = self.numCols() n = self.numCols()
...@@ -384,7 +384,7 @@ class MyMatrix(MyVector): ...@@ -384,7 +384,7 @@ class MyMatrix(MyVector):
for k in range(0,n): for k in range(0,n):
temp = a[k][indxr[l]] temp = a[k][indxr[l]]
a[k][indxr[l]] = a[k][indxc[l]] a[k][indxr[l]] = a[k][indxc[l]]
a[k][indxc[l]] = temp a[k][indxc[l]] = temp
return a return a
def transpose(self): def transpose(self):
...@@ -395,13 +395,13 @@ class MyMatrixTranspose(MyMatrix): ...@@ -395,13 +395,13 @@ class MyMatrixTranspose(MyMatrix):
def transpose(self): def transpose(self):
return MyMatrix(self.data) return MyMatrix(self.data)
def numRows(self): def numRows(self):
if len(self.data) == 0: if len(self.data) == 0:
return 0 return 0
else: else:
return len(self.data[0]) return len(self.data[0])
def numCols(self): def numCols(self):
return len(self.data) return len(self.data)
...@@ -461,7 +461,7 @@ class MyMatrixTranspose(MyMatrix): ...@@ -461,7 +461,7 @@ class MyMatrixTranspose(MyMatrix):
# run module directly for testing # run module directly for testing
if __name__=='__main__': if __name__=='__main__':
# Test the examples in the docstrings # Test the examples in the docstrings
import doctest, sys import doctest, sys
doctest.testmod(sys.modules[__name__]) doctest.testmod(sys.modules[__name__])
...@@ -11,7 +11,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -11,7 +11,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
...@@ -44,14 +44,14 @@ import sys ...@@ -44,14 +44,14 @@ import sys
class SiPrefix(object): class SiPrefix(object):
""" """
Unit prefix that can be multiplied by a unit to yield a new unit. Unit prefix that can be multiplied by a unit to yield a new unit.
e.g. millimeter = milli*meter e.g. millimeter = milli*meter
""" """
def __init__(self, prefix, factor, symbol): def __init__(self, prefix, factor, symbol):
self.prefix = prefix self.prefix = prefix
self.factor = factor self.factor = factor
self.symbol = symbol self.symbol = symbol
def __mul__(self, unit): def __mul__(self, unit):
""" """
SiPrefix * BaseUnit yields new BaseUnit SiPrefix * BaseUnit yields new BaseUnit
...@@ -136,7 +136,7 @@ si_prefixes = ( yotto ...@@ -136,7 +136,7 @@ si_prefixes = ( yotto
def define_prefixed_units(base_unit, module = sys.modules[__name__]): def define_prefixed_units(base_unit, module = sys.modules[__name__]):
""" """
Create attributes for prefixed units derived from a particular BaseUnit, e.g. "kilometer" from "meter_base_unit" Create attributes for prefixed units derived from a particular BaseUnit, e.g. "kilometer" from "meter_base_unit"
Parameters Parameters
- base_unit (BaseUnit) existing base unit to use as a basis for prefixed units - 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 simtk.unit module.
...@@ -146,7 +146,7 @@ def define_prefixed_units(base_unit, module = sys.modules[__name__]): ...@@ -146,7 +146,7 @@ def define_prefixed_units(base_unit, module = sys.modules[__name__]):
name = new_base_unit.name name = new_base_unit.name
new_unit = Unit({new_base_unit: 1.0}) new_unit = Unit({new_base_unit: 1.0})
# Create base_unit attribute, needed for creating UnitSystems # Create base_unit attribute, needed for creating UnitSystems
module.__dict__[name + '_base_unit'] = new_base_unit # e.g. "kilometer_base_unit" module.__dict__[name + '_base_unit'] = new_base_unit # e.g. "kilometer_base_unit"
# Create attribue in this module # Create attribue in this module
module.__dict__[name] = new_unit # e.g. "kilometer" module.__dict__[name] = new_unit # e.g. "kilometer"
# And plural version # And plural version
......
This diff is collapsed.
...@@ -13,7 +13,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -13,7 +13,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
......
This diff is collapsed.
...@@ -11,7 +11,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -11,7 +11,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
......
...@@ -13,7 +13,7 @@ Portions copyright (c) 2012 Stanford University and the Authors. ...@@ -13,7 +13,7 @@ Portions copyright (c) 2012 Stanford University and the Authors.
Authors: Christopher M. Bruns Authors: Christopher M. Bruns
Contributors: Peter Eastman Contributors: Peter Eastman
Permission is hereby granted, free of charge, to any person obtaining a Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"), copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, the rights to use, copy, modify, merge, publish, distribute, sublicense,
...@@ -49,7 +49,7 @@ from unit_definitions import * ...@@ -49,7 +49,7 @@ from unit_definitions import *
def sin(angle): def sin(angle):
""" """
Examples Examples
>>> sin(90*degrees) >>> sin(90*degrees)
1.0 1.0
""" """
...@@ -67,7 +67,7 @@ def sinh(angle): ...@@ -67,7 +67,7 @@ def sinh(angle):
def cos(angle): def cos(angle):
""" """
Examples Examples
>>> cos(180*degrees) >>> cos(180*degrees)
-1.0 -1.0
""" """
...@@ -102,10 +102,10 @@ def acos(x): ...@@ -102,10 +102,10 @@ def acos(x):
0.0 rad 0.0 rad
""" """
return math.acos(x) * radians return math.acos(x) * radians
def acosh(x): def acosh(x):
return math.acosh(x) * radians return math.acosh(x) * radians
def asin(x): def asin(x):
return math.asin(x) * radians return math.asin(x) * radians
...@@ -114,10 +114,10 @@ def asinh(x): ...@@ -114,10 +114,10 @@ def asinh(x):
def atan(x): def atan(x):
return math.atan(x) * radians return math.atan(x) * radians
def atanh(x): def atanh(x):
return math.atanh(x) * radians return math.atanh(x) * radians
def atan2(x, y): def atan2(x, y):
return math.atan2(x, y) * radians return math.atan2(x, y) * radians
......
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