parmed.unit.quantity module

Module simtk.unit.quantity

Physical quantities with units, intended to produce similar functionality to Boost.Units package in C++ (but with a runtime cost). Uses similar API as Scientific.Physics.PhysicalQuantities but different internals to satisfy our local requirements. In particular, there is no underlying set of ‘canonical’ base units, whereas in Scientific.Physics.PhysicalQuantities all units are secretly in terms of SI units. Also, it is easier to add new fundamental dimensions to simtk.dimensions. You might want to make new dimensions for, say, “currency” or “information”.

Some features of this implementation:
  • Quantities are a combination of a value and a unit. The value part can be any python type, including numbers, lists, numpy arrays, and anything else. The unit part must be a simtk.unit.Unit.

  • Operations like adding incompatible units raises an error.

  • Multiplying or dividing units/quantities creates new units.

  • Users can create new Units and Dimensions, but most of the useful ones are predefined.

  • Conversion factors between units are applied transitively, so all possible conversions are available.

  • I want dimensioned Quantities that are compatible with numpy arrays, but do not necessarily require the python numpy package. In other words, Quantities can be based on either numpy arrays or on built in python types.

  • Units are NOT necessarily stored in terms of SI units internally. This is very important for me, because one important application area for us is at the molecular scale. Using SI units internally can lead to exponent overflow in commonly used molecular force calculations. Internally, all unit systems are equally fundamental in SimTK.

Two possible enhancements that have not been implemented are
  1. Include uncertainties with propagation of errors

  2. Incorporate offsets for celsius <-> kelvin conversion

This is part of the OpenMM molecular simulation toolkit originating from Simbios, the NIH National Center for Physics-Based Simulation of Biological Structures at Stanford, funded under the NIH Roadmap for Medical Research, grant U54 GM072970. See https://simtk.org.

Portions copyright (c) 2012 Stanford University and the Authors. Authors: Christopher M. Bruns Contributors: Peter Eastman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class parmed.unit.quantity.Quantity(value=None, unit=None)[source]

Bases: object

Physical quantity, such as 1.3 meters per second.

Quantities contain both a value, such as 1.3; and a unit, such as ‘meters per second’.

Supported value types include:

1 - numbers (float, int, long) 2 - lists of numbers, e.g. [1,2,3] 3 - tuples of numbers, e.g. (1,2,3)

Note - unit conversions will cause tuples to be converted to lists

4 - lists of tuples of numbers, lists of lists of … etc. of numbers 5 - numpy.arrays

Methods

in_unit_system(system)

Returns a new Quantity equal to this one, expressed in a particular unit system.

in_units_of(other_unit)

Returns an equal Quantity expressed in different units.

max(*args, **kwargs)

Computes the maximum value of the sequence, with the result having the same unit as the current sequence.

mean(*args, **kwargs)

Computes the mean of a sequence, with the result having the same unit as the current sequence.

min(*args, **kwargs)

Computes the minimum value of the sequence, with the result having the same unit as the current sequence.

reduce_unit([guide_unit])

Combine similar component units and scale, to form an equal Quantity in simpler units.

reshape(shape[, order])

Same as numpy.ndarray.reshape, except the result is a Quantity with the same units as the current object rather than a plain numpy.ndarray

sqrt()

Returns square root of a Quantity.

std(*args, **kwargs)

Computes the square root of the variance of a sequence, with the result having the same unit as the current sequence.

sum(*args, **kwargs)

Computes the sum of a sequence, with the result having the same unit as the current sequence.

value_in_unit(unit)

Returns underlying value, in the specified units.

value_in_unit_system(system)

Returns the underlying value type, after conversion to a particular unit system.

append

count

extend

format

index

insert

pop

remove

append(item)[source]
count(item)[source]
extend(rhs)[source]
format(format_spec)[source]
in_unit_system(system)[source]

Returns a new Quantity equal to this one, expressed in a particular unit system.

in_units_of(other_unit)[source]

Returns an equal Quantity expressed in different units.

If the units are the same as those in self, a reference to self is returned. Raises a TypeError if the new unit is not compatible with the original unit.

The post_multiply argument is used in case the multiplication operation is not commutative.

i.e. result = factor * value when post_multiply is False and result = value * factor when post_multiply is True

index(item)[source]
insert(index, item)[source]
max(*args, **kwargs)[source]

Computes the maximum value of the sequence, with the result having the same unit as the current sequence.

If the value is not iterable, it raises a TypeError

This function can take as arguments any arguments recognized by numpy.max. If arguments are passed to a non-numpy array, a TypeError is raised

mean(*args, **kwargs)[source]

Computes the mean of a sequence, with the result having the same unit as the current sequence.

If the value is not iterable, it raises a TypeError

This function can take as arguments any arguments recognized by numpy.mean. If arguments are passed to a non-numpy array, a TypeError is raised

min(*args, **kwargs)[source]

Computes the minimum value of the sequence, with the result having the same unit as the current sequence.

If the value is not iterable, it raises a TypeError

This function can take as arguments any arguments recognized by numpy.min. If arguments are passed to a non-numpy array, a TypeError is raised

pop(*args)[source]
reduce_unit(guide_unit=None)[source]

Combine similar component units and scale, to form an equal Quantity in simpler units.

Returns underlying value type if unit is dimensionless.

remove(item)[source]
reshape(shape, order='C')[source]

Same as numpy.ndarray.reshape, except the result is a Quantity with the same units as the current object rather than a plain numpy.ndarray

sqrt()[source]

Returns square root of a Quantity.

Raises ArithmeticError if component exponents are not even. This behavior can be changed if you present a reasonable real life case to me.

std(*args, **kwargs)[source]

Computes the square root of the variance of a sequence, with the result having the same unit as the current sequence.

If the value is not iterable, it raises a TypeError

This function can take as arguments any arguments recognized by numpy.std. If arguments are passed to a non-numpy array, a TypeError is raised

sum(*args, **kwargs)[source]

Computes the sum of a sequence, with the result having the same unit as the current sequence.

If the value is not iterable, it raises a TypeError (same behavior as if you tried to iterate over, for instance, an integer).

This function can take as arguments any arguments recognized by numpy.sum. If arguments are passed to a non-numpy array, a TypeError is raised

value_in_unit(unit)[source]

Returns underlying value, in the specified units.

value_in_unit_system(system)[source]

Returns the underlying value type, after conversion to a particular unit system.

parmed.unit.quantity.is_dimensionless(x)[source]
parmed.unit.quantity.is_quantity(x)[source]

Returns True if x is a Quantity, False otherwise.