Package: ansys-units
OverviewPyAnsys Units provides a Pythonic interface for units, unit systems, and unit
conversions. Its features enable seamless setup and usage of physical
quantities, enabling you to perform these tasks:Instantiate physical quantities from a unit string, list of dimensions, or
quantity map.Perform unit conversions and arithmetic operations between quantity objects or
real values.Create custom units and unit systems.Documentation and issuesDocumentation for the latest stable release of PyAnsys Units is hosted atPyAnsys Units documentation.In the upper right corner of the documentation’s title bar, there is an option for
switching from viewing the documentation for the latest stable release to viewing
the documentation for the development version or previously released versions.On thePyAnsys Units Issuespage, you can
create issues to report bugs, and request new features. On thePyAnsys Units Discussionspage or theDiscussionspage on the Ansys Developer portal, you can post questions, share ideas, and get community feedback.To reach the project support team, emailpyansys.core@ansys.com.InstallationTheansys.unitspackage supports Python 3.9 through Python 3.11 on Windows
and Linux.Install the latest release fromPyPIwith this command:pip install ansys-unitsIf you plan on doing localdevelopmentof PyAnsys Units with Git, install the latest release with
these commands:git clone https://github.com/ansys/pyansys-units.git
cd pyansys-units
pip install pip -U
pip install -e .Getting startedBasic usagePyAnsys Units supports defining quantities and their units in a intuitive way.
Start by importing theansys.unitspackage:importansys.unitsasansunitsYou can instantiate quantities with one of four methods:# Using unit stringsvolume=ansunits.Quantity(value=1,units="m^3")volume.value# 1.0volume.units.name# "m^3"# Using Unit instancesureg=ansunits.UnitRegistry()mass=ansunits.Quantity(value=1,units=ureg.kg)volume.value# 1.0volume.units.name# "kg"# Using base dimensionsdims=ansunits.BaseDimensionsdimensions=ansunits.Dimensions({dims.LENGTH:1,dims.TIME:-2})acceleration=ansunits.Quantity(value=3,dimensions=dimensions)acceleration.value# 3.0acceleration.units.name# "m s^-2"# Using the quantity maptorque=ansunits.Quantity(5,quantity_map={"Torque":1})torque.value# 5.0torque.units.name# "N m"torque.si_units# "kg m^2 s^-2"You can instantiate unit systems with one of two methods:# Use a pre-defined unit systemsi=ansunits.UnitSystem(unit_sys="SI")si.base_units# ['kg', 'm', 's', 'K', 'delta_K', 'radian', 'mol', 'cd', 'A', 'sr']# Custom unit systems are defined by passing selected base units. Any unit# type that is not given will be filled with the SI equivalent.ureg=ansunits.UnitRegistry()dims=ansunits.BaseDimensionssys=ansunits.UnitSystem(base_units={dims.MASS:ureg.slug,dims.LENGTH:ureg.ft,dims.TEMPERATURE:ureg.R,dims.TEMPERATURE_DIFFERENCE:ureg.delta_R,dims.CHEMICAL_AMOUNT:ureg.slugmol,})sys.base_units# ['slug', 'ft', 's', 'R', 'delta_R', 'radian', 'slugmol', 'cd', 'A', 'sr']ExamplesPerform arithmetic operations:importansys.unitsasansunitsdeg=ansunits.Quantity(90,"degree")math.sin(deg)# 1.0v1=ansunits.Quantity(10.0,"m s^-1")v2=ansunits.Quantity(5.0,"m s^-1")v3=v1-v2v3.value# 5.0vpow=v1**2vpow.value# 100.0vpow.units# "m^2 s^-2"Directly convert values to another set of units:importansys.unitsasansunitsflbs=ansunits.Quantity(1,"lb ft^-1 s^-1")flbs.value# 1pas=flbs.to("Pa s")pas.value# 1.4881639435695542pas.units.name# 'Pa s'Use a custom unit system to perform conversions:importansys.unitsasansunitsureg=ansunits.UnitRegistry()dims=ansunits.BaseDimensionssys=ansunits.UnitSystem(base_units={dims.MASS:ureg.slug,dims.LENGTH:ureg.ft,dims.TEMPERATURE:ureg.R,dims.TEMPERATURE_DIFFERENCE:ureg.delta_R,dims.CHEMICAL_AMOUNT:ureg.slugmol,})v=ansunits.Quantity(10,"kg m s^2")v2=sys.convert(v)v2.value# 2.2480894309971045v2.units.name# 'slug ft s^2'LicensePyAnsys Units is licensed under the MIT license. For more information, see theLICENSEfile.