PyomoUnitsContainer
(class from pyomo.core.base.units_container
)
- class pyomo.core.base.units_container.PyomoUnitsContainer(pint_registry=NOTSET)[source]
Bases:
object
Class that is used to create and contain units in Pyomo.
This is the class that is used to create, contain, and interact with units in Pyomo. The module (
pyomo.core.base.units_container
) also contains a module level units containerunits
that is an instance of a PyomoUnitsContainer. This module instance should typically be used instead of creating your own instance of aPyomoUnitsContainer
. For an overview of the usage of this class, see the module documentation (pyomo.core.base.units_container
)This class is based on the “pint” module. Documentation for available units can be found at the following url: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
Note
Pre-defined units can be accessed through attributes on the PyomoUnitsContainer class; however, these attributes are created dynamically through the __getattr__ method, and are not present on the class until they are requested.
Methods
__init__
([pint_registry])Create a PyomoUnitsContainer instance.
convert
(src[, to_units])This method returns an expression that contains the explicit conversion from one unit to another.
convert_temp_C_to_K
(value_in_C)Convert a value in degrees Celsius to Kelvin Note that this method converts a numerical value only.
convert_temp_F_to_R
(value_in_F)Convert a value in degrees Fahrenheit to Rankine.
convert_temp_K_to_C
(value_in_K)Convert a value in Kelvin to degrees Celsius.
convert_temp_R_to_F
(value_in_R)Convert a value in Rankine to degrees Fahrenheit.
convert_value
(num_value[, from_units, to_units])This method performs explicit conversion of a numerical value from one unit to another, and returns the new value.
get_units
(expr)Return the Pyomo units corresponding to this expression (also performs validation and will raise an exception if units are not consistent).
load_definitions_from_file
(definition_file)Load new units definitions from a file
Load new units definitions from a string
set_pint_registry
(pint_registry)Attributes
pint_registry
Member Documentation
- convert(src, to_units=None)[source]
This method returns an expression that contains the explicit conversion from one unit to another.
- Parameters:
src (Pyomo expression) – The source value that will be converted. This could be a Pyomo Var, Pyomo Param, or a more complex expression.
to_units (Pyomo units expression) – The desired target units for the new expression
- Returns:
ret
- Return type:
Pyomo expression
- convert_temp_C_to_K(value_in_C)[source]
Convert a value in degrees Celsius to Kelvin Note that this method converts a numerical value only. If you need temperature conversions in expressions, please work in absolute temperatures only.
- convert_temp_F_to_R(value_in_F)[source]
Convert a value in degrees Fahrenheit to Rankine. Note that this method converts a numerical value only. If you need temperature conversions in expressions, please work in absolute temperatures only.
- convert_temp_K_to_C(value_in_K)[source]
Convert a value in Kelvin to degrees Celsius. Note that this method converts a numerical value only. If you need temperature conversions in expressions, please work in absolute temperatures only.
- convert_temp_R_to_F(value_in_R)[source]
Convert a value in Rankine to degrees Fahrenheit. Note that this method converts a numerical value only. If you need temperature conversions in expressions, please work in absolute temperatures only.
- convert_value(num_value, from_units=None, to_units=None)[source]
This method performs explicit conversion of a numerical value from one unit to another, and returns the new value.
The argument “num_value” must be a native numeric type (e.g. float). Note that this method returns a numerical value only, and not an expression with units.
- Parameters:
num_value (float or other native numeric type) – The value that will be converted
from_units (Pyomo units expression) – The units to convert from
to_units (Pyomo units expression) – The units to convert to
- Returns:
float
- Return type:
The converted value
- get_units(expr)[source]
Return the Pyomo units corresponding to this expression (also performs validation and will raise an exception if units are not consistent).
- Parameters:
expr (Pyomo expression) – The expression containing the desired units
- Returns:
Returns the units corresponding to the expression
- Return type:
Pyomo unit (expression)
- Raises:
- load_definitions_from_file(definition_file)[source]
Load new units definitions from a file
This method loads additional units definitions from a user specified definition file. An example of a definitions file can be found at: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
If we have a file called
my_additional_units.txt
with the following lines:USD = [currency]
Then we can add this to the container with:
>>> u.load_definitions_from_file('my_additional_units.txt') >>> print(u.USD) USD
- load_definitions_from_strings(definition_string_list)[source]
Load new units definitions from a string
This method loads additional units definitions from a list of strings (one for each line). An example of the definitions strings can be found at: https://github.com/hgrecco/pint/blob/master/pint/default_en.txt
For example, to add the currency dimension and US dollars as a unit, use
>>> u.load_definitions_from_strings(['USD = [currency]']) >>> print(u.USD) USD