Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
tsoc
openmm
Commits
b7b4742f
"...ssh:/git@developer.sourcefind.cn:2222/tsoc/openmm.git" did not exist on "75c9f3d557ed99291c9371a4707903be8ada933f"
Commit
b7b4742f
authored
Jul 16, 2014
by
peastman
Browse files
Fixed a bug in unit conversion for sequences
parent
c9c5997b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
34 deletions
+29
-34
wrappers/python/simtk/unit/doctests.py
wrappers/python/simtk/unit/doctests.py
+7
-7
wrappers/python/simtk/unit/quantity.py
wrappers/python/simtk/unit/quantity.py
+22
-27
No files found.
wrappers/python/simtk/unit/doctests.py
View file @
b7b4742f
...
@@ -41,12 +41,12 @@ Quantity(value=1.0, unit=calorie)
...
@@ -41,12 +41,12 @@ Quantity(value=1.0, unit=calorie)
>>> 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.184
0000000000002
, unit=joule)
Quantity(value=4.184, unit=joule)
>>> j = 1.0*joules
>>> j = 1.0*joules
>>> j
>>> j
Quantity(value=1.0, unit=joule)
Quantity(value=1.0, unit=joule)
>>> j.in_units_of(calories)
>>> j.in_units_of(calories)
Quantity(value=0.239005736137667
29
, unit=calorie)
Quantity(value=0.239005736137667
3
, unit=calorie)
>>> j/joules
>>> j/joules
1.0
1.0
>>> print(j/calories)
>>> print(j/calories)
...
@@ -58,7 +58,7 @@ Quantity(value=0.23900573613766729, unit=calorie)
...
@@ -58,7 +58,7 @@ Quantity(value=0.23900573613766729, unit=calorie)
>>> c**2
>>> c**2
Quantity(value=1.0, unit=calorie**2)
Quantity(value=1.0, unit=calorie**2)
>>> (c**2).in_units_of(joule*joule)
>>> (c**2).in_units_of(joule*joule)
Quantity(value=17.505856
000000001
, unit=joule**2)
Quantity(value=17.505856, unit=joule**2)
>>> ScaledUnit(1000.0, kelvin, "kilokelvin", "kK")
>>> ScaledUnit(1000.0, kelvin, "kilokelvin", "kK")
ScaledUnit(factor=1000.0, master=kelvin, name='kilokelvin', symbol='kK')
ScaledUnit(factor=1000.0, master=kelvin, name='kilokelvin', symbol='kK')
...
@@ -252,12 +252,12 @@ Collections of numbers can also be used as values.
...
@@ -252,12 +252,12 @@ Collections of numbers can also be used as values.
>>> 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
...
@@ -772,7 +772,7 @@ Examples
...
@@ -772,7 +772,7 @@ Examples
Creating a new ScaledUnit:
Creating a new ScaledUnit:
>>> mC = milli * ScaledUnit(4.184, joule, "calorie", "cal")
>>> mC = milli * ScaledUnit(4.184, joule, "calorie", "cal")
>>> mC
>>> mC
ScaledUnit(factor=0.004184
0000000000002
, master=joule, name='millicalorie', symbol='mcal')
ScaledUnit(factor=0.004184, master=joule, name='millicalorie', symbol='mcal')
Creating a new Unit:
Creating a new Unit:
>>> ms = milli * second
>>> ms = milli * second
...
@@ -795,7 +795,7 @@ Examples
...
@@ -795,7 +795,7 @@ Examples
Formatting of Quantities
Formatting of Quantities
>>> x = 5.439999999 * picosecond
>>> x = 5.439999999 * picosecond
>>> x
>>> x
Quantity(value=5.439999999
0000003
, unit=picosecond)
Quantity(value=5.439999999, unit=picosecond)
>>> x.format("%.3f")
>>> x.format("%.3f")
'5.440 ps'
'5.440 ps'
...
...
wrappers/python/simtk/unit/quantity.py
View file @
b7b4742f
...
@@ -545,10 +545,7 @@ class Quantity(object):
...
@@ -545,10 +545,7 @@ class Quantity(object):
pass
pass
if
factor_is_identity
:
if
factor_is_identity
:
# No multiplication required
# No multiplication required
if
(
self
.
unit
is
new_unit
):
result
=
Quantity
(
copy
.
deepcopy
(
self
.
_value
),
new_unit
)
result
=
self
else
:
result
=
Quantity
(
self
.
_value
,
new_unit
)
else
:
else
:
try
:
try
:
# multiply operator, if it exists, is preferred
# multiply operator, if it exists, is preferred
...
@@ -558,18 +555,7 @@ class Quantity(object):
...
@@ -558,18 +555,7 @@ class Quantity(object):
value
=
factor
*
self
.
_value
# works for number, numpy.array, or vec3, e.g.
value
=
factor
*
self
.
_value
# works for number, numpy.array, or vec3, e.g.
result
=
Quantity
(
value
,
new_unit
)
result
=
Quantity
(
value
,
new_unit
)
except
TypeError
:
except
TypeError
:
# list * float fails with TypeError
value
=
copy
.
deepcopy
(
self
.
_value
)
# Presumably a list type
# deep copy
value
=
self
.
_value
[:]
# deep copy
# convert tuple to list
try
:
value
[
0
]
=
value
[
0
]
# tuple is immutable
except
TypeError
:
# convert immutable tuple to list
value
=
[]
for
i
in
self
.
_value
:
value
.
append
(
i
)
result
=
Quantity
(
self
.
_scale_sequence
(
value
,
factor
,
post_multiply
),
new_unit
)
result
=
Quantity
(
self
.
_scale_sequence
(
value
,
factor
,
post_multiply
),
new_unit
)
if
(
new_unit
.
is_dimensionless
()):
if
(
new_unit
.
is_dimensionless
()):
return
result
.
_value
return
result
.
_value
...
@@ -579,20 +565,29 @@ class Quantity(object):
...
@@ -579,20 +565,29 @@ class Quantity(object):
def
_scale_sequence
(
self
,
value
,
factor
,
post_multiply
):
def
_scale_sequence
(
self
,
value
,
factor
,
post_multiply
):
try
:
try
:
if
post_multiply
:
if
post_multiply
:
if
isinstance
(
self
.
_value
,
tuple
):
value
=
value
*
factor
value
=
tuple
([
x
*
factor
for
x
in
value
])
else
:
for
i
in
range
(
len
(
value
)):
value
[
i
]
=
value
[
i
]
*
factor
else
:
else
:
if
isinstance
(
self
.
_value
,
tuple
):
value
=
factor
*
value
value
=
tuple
([
factor
*
x
for
x
in
value
])
except
TypeError
:
try
:
if
post_multiply
:
if
isinstance
(
value
,
tuple
):
value
=
tuple
([
x
*
factor
for
x
in
value
])
else
:
for
i
in
range
(
len
(
value
)):
value
[
i
]
=
value
[
i
]
*
factor
else
:
if
isinstance
(
value
,
tuple
):
value
=
tuple
([
factor
*
x
for
x
in
value
])
else
:
for
i
in
range
(
len
(
value
)):
value
[
i
]
=
factor
*
value
[
i
]
except
TypeError
as
ex
:
if
isinstance
(
value
,
tuple
):
value
=
tuple
([
self
.
_scale_sequence
(
x
,
factor
,
post_multiply
)
for
x
in
value
])
else
:
else
:
for
i
in
range
(
len
(
value
)):
for
i
in
range
(
len
(
value
)):
value
[
i
]
=
factor
*
value
[
i
]
value
[
i
]
=
self
.
_scale_sequence
(
value
[
i
],
factor
,
post_multiply
)
except
TypeError
as
ex
:
for
i
in
range
(
len
(
value
)):
value
[
i
]
=
self
.
_scale_sequence
(
value
[
i
],
factor
,
post_multiply
)
return
value
return
value
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment