Skip to main content

Pythonic interface for units, unit systems, and unit conversions.

Project description

PyAnsys PyPI Python GH-CI codecov MIT Black pre-commit.ci status

Overview

PyAnsys Units is a Python library designed for managing physical quantities, which are combinations of numerical values and corresponding units of measurement. This package facilitates arithmetic operations and conversions between various units.

With a modular design, PyAnsys Units offers the flexibility to extend or modify its extensive list of physical units and unit systems without altering the source code. It seamlessly integrates with NumPy mathematical operations.

PyAnsys Units comes bundled with a comprehensive set of physical units, prefixes, and constants and boasts complete test coverage.

Documentation and issues

Documentation for the latest stable release of PyAnsys Units is hosted at PyAnsys 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 the PyAnsys Units Issues page, you can create issues to report bugs, and request new features. On the PyAnsys Units Discussions page or the Discussions page on the Ansys Developer portal, you can post questions, share ideas, and get community feedback.

To reach the project support team, email pyansys.core@ansys.com.

Installation

The ansys.units package supports Python 3.10 through Python 3.13 on Windows and Linux.

Install the latest release from PyPI with this command:

pip install ansys-units

If you plan on doing local development of 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 started

PyAnsys Units supports flexible instantiation of Quantity objects:

import ansys.units as ansunits

# Using unit strings

volume = ansunits.Quantity(value=1, units="m^3")

volume.value  # 1.0
volume.units.name  # "m^3"

# Using Unit instances

ureg = ansunits.UnitRegistry()

mass = ansunits.Quantity(value=1, units=ureg.kg)

volume.value  # 1.0
volume.units.name  # "kg"

# Using base dimensions

dims = ansunits.BaseDimensions
dimensions = ansunits.Dimensions({dims.LENGTH: 1, dims.TIME: -2})

acceleration = ansunits.Quantity(value=3, dimensions=dimensions)

acceleration.value  # 3.0
acceleration.units.name  # "m s^-2"

# Using the quantity map

torque = ansunits.Quantity(5, quantity_map={"Torque": 1})

torque.value  # 5.0
torque.units.name  # "N m"
torque.si_units  # "kg m^2 s^-2"

With NumPy installed, you can instantiate a Quantity using either a list of floats or a NumPy array:

from ansys.units import Quantity
import numpy as np

length_array_quantity = Quantity(value=[1.0, 6.0, 7.0], units="m")
length_array_quantity[1]  # Quantity (6.0, "m")
time = Quantity(value=2, units="s")
speed = length_array_quantity / time
speed  # Quantity ([0.5 3. 3.5], "m s^-1")

You can instantiate unit systems with one of two methods:

# Use a pre-defined unit system

si = ansunits.UnitSystem(system="SI")

si.base_units  # ['kg', 'm', 's', 'K', 'delta_K', 'radian', 'mol', 'cd', 'A', 'sr']

# Define a custom unit system from a dictionary of base units. Any unspecified
# unit will default to the SI equivalent.

ureg = ansunits.UnitRegistry()
dims = ansunits.BaseDimensions

sys = 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']

Examples

Perform arithmetic operations:

import ansys.units as ansunits

deg = ansunits.Quantity(90, "degree")
math.sin(deg)  # 1.0

v1 = ansunits.Quantity(10.0, "m s^-1")
v2 = ansunits.Quantity(5.0, "m s^-1")

v3 = v1 - v2
v3.value  # 5.0

vpow = v1**2
vpow.value  # 100.0
vpow.units  # "m^2 s^-2"

Directly convert values to another set of units:

import ansys.units as ansunits

flbs = ansunits.Quantity(1, "lb ft^-1 s^-1")
flbs.value  # 1

pas = flbs.to("Pa s")
pas.value  # 1.4881639435695542
pas.units.name  # 'Pa s'

Use a custom unit system to perform conversions:

import ansys.units as ansunits

ureg = ansunits.UnitRegistry()
dims = ansunits.BaseDimensions

sys = 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.2480894309971045
v2.units.name  # 'slug ft s^2'

License

PyAnsys Units is licensed under the MIT license. For more information, see the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansys_units-0.8.1.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansys_units-0.8.1-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

Details for the file ansys_units-0.8.1.tar.gz.

File metadata

  • Download URL: ansys_units-0.8.1.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ansys_units-0.8.1.tar.gz
Algorithm Hash digest
SHA256 776bd18ff94187679babb8d52ac002d236a8641fd65e9c7835e902daf287f755
MD5 0175f831d2f50b8ea26e75ff18c89661
BLAKE2b-256 6849dc64242a10fe051f017d7684038ee955d1f47452903e72331d14a6bdc1a8

See more details on using hashes here.

Provenance

The following attestation bundles were made for ansys_units-0.8.1.tar.gz:

Publisher: ci_cd.yml on ansys/pyansys-units

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ansys_units-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: ansys_units-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 42.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ansys_units-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc74164ad93c6acf32c351af9cd0a3bd0435b90112ab210044351b466d155893
MD5 252e3658bb816ae52442b446b9108723
BLAKE2b-256 ff6f19148525bf69e3cfcaf1c749a44c7b25d380fa4d3f6e950fce3f4785ca56

See more details on using hashes here.

Provenance

The following attestation bundles were made for ansys_units-0.8.1-py3-none-any.whl:

Publisher: ci_cd.yml on ansys/pyansys-units

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page