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
47ab0a62
Commit
47ab0a62
authored
Mar 08, 2011
by
Peter Eastman
Browse files
Optimizations to unit conversion
parent
5adaa15e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
4 deletions
+8
-4
wrappers/python/simtk/unit/basedimension.py
wrappers/python/simtk/unit/basedimension.py
+3
-2
wrappers/python/simtk/unit/unit.py
wrappers/python/simtk/unit/unit.py
+5
-2
No files found.
wrappers/python/simtk/unit/basedimension.py
View file @
47ab0a62
...
@@ -42,6 +42,7 @@ class BaseDimension(object):
...
@@ -42,6 +42,7 @@ class BaseDimension(object):
if
not
self
.
name
in
BaseDimension
.
_index_by_name
.
keys
():
if
not
self
.
name
in
BaseDimension
.
_index_by_name
.
keys
():
BaseDimension
.
_index_by_name
[
name
]
=
BaseDimension
.
_next_unused_index
BaseDimension
.
_index_by_name
[
name
]
=
BaseDimension
.
_next_unused_index
BaseDimension
.
_next_unused_index
+=
1
BaseDimension
.
_next_unused_index
+=
1
self
.
_index
=
BaseDimension
.
_index_by_name
[
name
]
def
__cmp__
(
self
,
other
):
def
__cmp__
(
self
,
other
):
"""
"""
...
@@ -51,13 +52,13 @@ class BaseDimension(object):
...
@@ -51,13 +52,13 @@ class BaseDimension(object):
Returns 0 if self == other, -1 if self < other, and 1 if self > other.
Returns 0 if self == other, -1 if self < other, and 1 if self > other.
"""
"""
return
cmp
(
BaseDimension
.
_index_by_name
[
self
.
name
],
BaseDimension
.
_index_by_name
[
other
.
name
]
)
return
cmp
(
self
.
_index
,
other
.
_index
)
def
__hash__
(
self
):
def
__hash__
(
self
):
"""
"""
Needed for using BaseDimensions as hash keys.
Needed for using BaseDimensions as hash keys.
"""
"""
return
hash
(
BaseDimension
.
_index_by_name
[
self
.
name
])
return
self
.
_index
def
__repr__
(
self
):
def
__repr__
(
self
):
return
'BaseDimension("%s")'
%
self
.
name
return
'BaseDimension("%s")'
%
self
.
name
...
...
wrappers/python/simtk/unit/unit.py
View file @
47ab0a62
...
@@ -89,7 +89,6 @@ class Unit(object):
...
@@ -89,7 +89,6 @@ class Unit(object):
"""
"""
Yields (BaseDimension, exponent) tuples comprising this unit.
Yields (BaseDimension, exponent) tuples comprising this unit.
"""
"""
result
=
{}
# There might be two units with the same dimension? No.
# There might be two units with the same dimension? No.
for
dimension
in
sorted
(
self
.
_all_base_units
.
iterkeys
()):
for
dimension
in
sorted
(
self
.
_all_base_units
.
iterkeys
()):
exponent
=
0
exponent
=
0
...
@@ -105,7 +104,6 @@ class Unit(object):
...
@@ -105,7 +104,6 @@ class Unit(object):
There might be multiple BaseUnits with the same dimension.
There might be multiple BaseUnits with the same dimension.
"""
"""
result
=
{}
for
dimension
in
sorted
(
self
.
_all_base_units
.
iterkeys
()):
for
dimension
in
sorted
(
self
.
_all_base_units
.
iterkeys
()):
for
base_unit
in
sorted
(
self
.
_all_base_units
[
dimension
].
iterkeys
()):
for
base_unit
in
sorted
(
self
.
_all_base_units
[
dimension
].
iterkeys
()):
exponent
=
self
.
_all_base_units
[
dimension
][
base_unit
]
exponent
=
self
.
_all_base_units
[
dimension
][
base_unit
]
...
@@ -526,6 +524,7 @@ class ScaledUnit(object):
...
@@ -526,6 +524,7 @@ class ScaledUnit(object):
class
UnitSystem
(
object
):
class
UnitSystem
(
object
):
def
__init__
(
self
,
units
):
def
__init__
(
self
,
units
):
self
.
units
=
units
self
.
units
=
units
self
.
_unit_conversion_cache
=
{}
# Create a set of base units to be used for dimension conversion
# Create a set of base units to be used for dimension conversion
base_units
=
{}
base_units
=
{}
for
unit
in
self
.
units
:
for
unit
in
self
.
units
:
...
@@ -577,6 +576,9 @@ class UnitSystem(object):
...
@@ -577,6 +576,9 @@ class UnitSystem(object):
def
express_unit
(
self
,
old_unit
):
def
express_unit
(
self
,
old_unit
):
"""
"""
"""
"""
unit_name
=
old_unit
.
get_name
()
if
unit_name
in
self
.
_unit_conversion_cache
:
return
self
.
_unit_conversion_cache
[
unit_name
]
# First express unit in terms of base dimensions found in this unit system
# First express unit in terms of base dimensions found in this unit system
# (plus other dimensions not found)
# (plus other dimensions not found)
m
=
len
(
self
.
dimensions
)
m
=
len
(
self
.
dimensions
)
...
@@ -606,6 +608,7 @@ class UnitSystem(object):
...
@@ -606,6 +608,7 @@ class UnitSystem(object):
found_dims
[
dim
]
=
base_unit
found_dims
[
dim
]
=
base_unit
exponent
=
other_dims
[
dim
]
exponent
=
other_dims
[
dim
]
new_unit
*=
Unit
({
base_unit
:
exponent
})
new_unit
*=
Unit
({
base_unit
:
exponent
})
self
.
_unit_conversion_cache
[
unit_name
]
=
new_unit
return
new_unit
return
new_unit
def
is_unit
(
x
):
def
is_unit
(
x
):
...
...
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