Skip to content

Quantities (pint)

vhirtham edited this page May 3, 2021 · 2 revisions

Quantities

Introduction

One aim of weldx is to be as specific about describing the physics of welding data as possible. An important aspect of this task is the use of (physical) quantities, i.e. the combination of numeric values with a unit.

The pint package

The weldx packages relies heavily on pint to work with quantities. One advantage of pint besides it's ease of use is the ongoing effort to further integrate it with other popular packages like numpy, sympy, pandas and xarray that are also used in weldx.

Read more about pint here.

Quickstart

If you only want to start working with units inside weldx, here is what you need to know:

First, import the weldx base quantity constructor Q_

from weldx import Q_

Now use Q_ to create quantities or add a unit to existing numpy-like arrays.

Here are some examples:

import numpy as np

a = Q_(5, "meter")
print(a)
t = Q_(2, "minute")
print(t)
I = Q_(np.arange(20),"A")
print(I)
5 meter
2 minute
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19] ampere

That's it, simply use the quantity objects like you would with other numpy-like numerical objects.

If you are worried about unit syntax, don't be: pint has very powerful string parsing for units (including prefixes), so in most cases it should be sufficient to provide units in a "natural" engineering notation even for complex cases:

Q_(200,"kVA / (nsecond * dl)")
200
kilovolt_ampere/(deciliter nanosecond)

How weldx uses pint

When importing the weldx package two things happen:

  • A default pint.UnitRegistry is created and imported as weldx.constants.WELDX_UNIT_REGISTRY. Since all pint quantities must have the same base UnitRegistry it is important to work with the UnitRegistry everywhere in weldx. All changes and modifications to the unit system are based and stored in this Registry (example: enabling use of "%" for units).
  • Based on the above UnitRegistry the Q_ object gets created. This should be the default method when creating new quantities throughout weldx.

Useful functions

Check equal dimensionality

from weldx.constants import WELDX_UNIT_REGISTRY as ureg
ureg.is_compatible_with("m*s","inch*min")
Clone this wiki locally