%load_ext autoreload
%autoreload 2
Dimensionality and Compatibility
The dimensional analysis of a quantity or a unit, no matter the form, can be performed invoking the method pyunitwizard.get_dimensionality()
:
import pyunitwizard as puw
puw.configure.load_library(['pint', 'openmm.unit'])
q = puw.quantity(1.4, 'kJ/mol', form='openmm.unit')
puw.get_dimensionality(q)
{'[L]': 2.0,
'[M]': 1.0,
'[T]': -2.0,
'[K]': 0,
'[mol]': -1.0,
'[A]': 0,
'[Cd]': 0}
Let’s see a second example:
q = puw.quantity('3.5N/(2.0nm**2)')
puw.get_dimensionality(q)
{'[L]': -1, '[M]': 1, '[T]': -2, '[K]': 0, '[mol]': 0, '[A]': 0, '[Cd]': 0}
Where dimensions correspond to the following fundamental quantities:
Fundamental Quantity |
Dimension |
---|---|
Length |
[L] |
Mass |
[M] |
Time |
[T] |
Temperature |
[K] |
Substance |
[mol] |
Electric Current |
[A] |
Luminous Intensity |
[Cd] |
In addition, PyUnitWizard can check the dimensional compatibility between quantities with pyunitwizard.compatibility()
, again, regardless their pythonic form:
q1 = puw.quantity(1.0, 'kJ/mol', form='openmm.unit')
q2 = puw.quantity(1.0, 'kcal/mol', form='pint')
puw.compatibility(q1, q2)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[5], line 4
1 q1 = puw.quantity(1.0, 'kJ/mol', form='openmm.unit')
2 q2 = puw.quantity(1.0, 'kcal/mol', form='pint')
----> 4 puw.compatibility(q1, q2)
AttributeError: module 'pyunitwizard' has no attribute 'compatibility'
q1 = puw.quantity(1.0, 'nm**3', form='pint')
q2 = puw.quantity(1.0, 'litre', form='pint')
puw.compatibility(q1, q2)
True
q1 = puw.quantity(1.0, 'degrees', form='pint')
q2 = puw.quantity(1.0, 'radians', form='pint')
puw.compatibility(q1, q2)
True
q1 = puw.quantity(1.0, 'degrees', form='openmm.unit')
q2 = puw.quantity(1.0, 'hertzs', form='pint')
puw.compatibility(q1, q2)
False
q1 = puw.quantity(1.0, 'meters/seconds', form='openmm.unit')
q2 = puw.quantity(1.0, 'newtons/meter**2', form='pint')
puw.compatibility(q1, q2)
False
With the help of the method pyunitwizard.check
we can also compare the dimensionality of a quantity or unit with a set of dimensions given by the user:
puw.check('meter', dimensionality={'[L]':1})
True
puw.check('1 meter', dimensionality={'[L]':1, '[T]':-1})
False
puw.check('1 meter/seconds', dimensionality={'[L]':1, '[T]':-1})
True