Quantity#
- class ansys.units.quantity.Quantity(value: int | float | None = None, units: Unit | str | None = None, quantity_table: dict | None = None, dimensions: Dimensions | None = None, copy_from: Quantity | None = None)#
Bases:
object
A class representing a physical quantity’s value and associated units.
A Quantity object can be instantiated from a NumPy array or list only if the
NumPy
package is installed.The value argument is not required when using
copy_from
. A value can be given to override the value from the copy.Implicit conversion from Quantity to float is only allowed if the operand is of quantity type angle or dimensionless.
- Parameters:
- value
int
,float
,list
,np.array
Real value of the quantity.
- units
str
,Unit
,optional
Initializes the quantity’s units using a string or
Unit
instance.- quantity_table
dict
[str
,int
],optional
Initializes the quantity’s units using the quantity table.
- dimensions
Dimensions
,optional
Initializes the quantity’s units in SI using a
Dimensions
instance.- copy_from
Quantity
,optional
An existing
Quantity
instance.
- value
- Attributes:
value
Value in contained units.
units
The quantity’s units.
dimensions
The quantity’s dimensions.
is_dimensionless
True if the quantity is dimensionless.
- compatible_units() set[str] #
Get all units with the same dimensions.
- Returns:
set
A set of unit objects.
- convert(system: UnitSystem) Quantity #
Convert a quantity into the unit system.
- Parameters:
- system
UnitSystem
Unit system to convert to.
- system
- Returns:
Quantity
Quantity object converted into the unit system.
Examples
>>> ur = UnitRegistry() >>> speed_si = Quantity(value=5, units= ur.m / ur.s) >>> bt = UnitSystem(system="BT") >>> speed_bt = speed_si.convert(bt)
- property dimensions#
The quantity’s dimensions.
- property is_dimensionless#
True if the quantity is dimensionless.
- classmethod preferred_units(units: list[Unit | str], remove: bool = False) None #
Add or remove preferred units.
Quantities are automatically converted to preferred units when the quantity is initialized. Conversion is always carried out if the base units are consistent with the preferred units.
Each preferred unit must have unique dimensions. To override units with the same dimensions, the original must first be removed.
- to(to_units: Unit | str) Quantity #
Perform quantity conversions.
- Parameters:
- to_units
Unit
orstr
Desired unit to convert to.
- to_units
- Returns:
Quantity
Quantity instance in the desired units.
Examples
>>> speed_si = Quantity(value=5, units="m s^-1") >>> speed_bt = speed_si.to("ft s^-1")
- validate_matching_dimensions(other)#
Validates dimensions of quantities.
- property value#
Value in contained units.
- ansys.units.quantity.get_si_value(quantity: Quantity) float #
Returns a quantity’s value in SI units.
- exception ansys.units.quantity.ExcessiveParameters#
Bases:
ValueError
Raised when excessive parameters are provided.
- exception ansys.units.quantity.IncompatibleDimensions(from_unit, to_unit)#
Bases:
ValueError
Raised when dimensions are incompatible.
- exception ansys.units.quantity.IncompatibleQuantities(q1, q2)#
Bases:
ValueError
Raised when quantities are incompatible.
- exception ansys.units.quantity.IncompatibleValue(value)#
Bases:
ValueError
Raised when an incompatible value is provided.
- exception ansys.units.quantity.InsufficientArguments#
Bases:
ValueError
Raised when insufficient arguments are provided.
- exception ansys.units.quantity.InvalidFloatUsage#
Bases:
FloatingPointError
Raised when float is unsupported for given type of quantity.
- exception ansys.units.quantity.NumPyRequired#
Bases:
ModuleNotFoundError
Raised when NumPy is unavailable.
- exception ansys.units.quantity.RequiresUniqueDimensions(unit, other_unit)#
Bases:
ValueError
Raised when two units with the same dimensions are added to the chosen units.