Package openmdao.units

This package is based on the PhysicalQuantity package that is part of ScientificPython. It performs unit conversions.

This module provides a data type that represents a physical quantity together with its unit. It is possible to add and subtract these quantities if the units are compatible and a quantity can be converted to another compatible unit. Multiplication, subtraction, and raising to integer powers are allowed without restriction, and the result will have the correct unit. A quantity can be raised to a non-integer power only if the result can be represented by integer powers of the base units.

The module provides a basic set of predefined physical quanitites in its built-in library; however, it also supports generation of personal libararies which can be saved and reused.

This module is based on the PhysicalQuantities module in Scientific Python, by Konrad Hinsen. Modifications by Justin Gray.

class openmdao.units.units.NumberDict[source]

Bases: dict

Dictionary storing numerical values.

Constructor: NumberDict()

An instance of this class acts like an array of numbers with generalized (non-integer) indices. A value of zero is assumed for undefined entries. NumberDict instances support addition and subtraction with other NumberDict instances, and multiplication and division by scalars.

class openmdao.units.units.PhysicalQuantity(*args)[source]

Bases: object

Physical quantity with units

PhysicalQuantity instances allow addition, subtraction, multiplication, and division with each other as well as multiplication, division, and exponentiation with numbers. Addition and subtraction check that the units of the two operands are compatible and return the result in the units of the first operand.


Change the unit and adjust the value so that the combination is equivalent to the original one. The new unit must be compatible with the previous unit of the object.

@param unit: a unit @type unit: C{str} @raise TypeError: if the unit string is not a known unit or a unit incompatible with the current one.


Converts the values of the PQ to the target_unit.


Parsing Cosine.


Return unit (string) of physical quantity.


Return value (float) of physical quantity (no unit).


@returns: the same quantity converted to base units, i.e., SI units in most cases @rtype: L{PhysicalQuantity}


Express the quantity in different units. If one unit is specified, a new PhysicalQuantity object is returned that expresses the quantity in that unit. If several units are specified, the return value is a tuple of PhysicalObject instances with with one element per unit such that the sum of all quantities in the tuple equals the original quantity and all the values except for the last one are integers. This is used to convert to irregular unit systems like hour/minute/second.

@param units: one or several units @type units: C{str} or sequence of C{str} @returns: one or more physical quantities @rtype: L{PhysicalQuantity} or C{tuple} of L{PhysicalQuantity} @raises TypeError: if any of the specified units are not compatible with the original unit.


@param unit: a unit @type unit: C{str} @returns: C{True} if the specified unit is compatible with the one of the quantity. @rtype: C{bool}.


Parsing Sine.


Parsing Square Root


Parsing tangent.

class openmdao.units.units.PhysicalUnit(names, factor, powers, offset=0)[source]

Bases: object

Physical unit.

A physical unit is defined by a name (possibly composite), a scaling factor, and the exponentials of each of the SI base units that enter into it. Units can be multiplied, divided, and raised to integer powers.


@param other: another unit @type other: L{PhysicalUnit} @returns: the conversion factor from this unit to another unit @rtype: C{float} @raises TypeError: if the units are not compatible.


@param other: another unit @type other: L{PhysicalUnit} @returns: the conversion factor and offset from this unit to another unit @rtype: (C{float}, C{float}) @raises TypeError: if the units are not compatible.


Checks if this PQ is an Angle.


@param other: another unit @type other: L{PhysicalUnit} @returns: C{True} if the units are compatible, i.e., if the powers of the base units are the same @rtype: C{bool}.


Dimensionless PQ.


Looks like it’s parsing fractions.


Sets the name.

class openmdao.units.units.UnitsOnlyPQ(*args)[source]

Bases: openmdao.units.units.PhysicalQuantity

When you want to determine the units of a combined expression without worrying about possible problems with the actual values (divide by zero, etc.), replace the variables in the expression with instances of this class, evaluate the expression, and retrieve the units of the result.

WARNING: only the units of the resulting UnitsOnlyPQ object will be correct. The value may be incorrect, so don’t use it.

openmdao.units.units.add_offset_unit(name, baseunit, factor, offset, comment='')[source]

Adding Offset Unit.

openmdao.units.units.add_unit(name, unit, comment='')[source]

Adding Unit.

openmdao.units.units.convert_units(value, units, convunits)[source]

Return the given value (given in units) converted to convunits.


Imports a units library, replacing any existing definitions.


Update units in current library from filename which must contain a units section.

filename: string or file
Source of units configuration data.
OpenMDAO Home

Table Of Contents

Previous topic

Package openmdao.test

Next topic

Package openmdao.util

This Page