units
Module for all unit definitions and operations
This includes defining units used in MDMC, converting units, and subclassing
data strucures (float, array
) so that they have a unit
attribute. This style follows that of the Atomic Simulation Environment.
As members of units.py are set dynamically, pylintrc excludes member checking for units.py using the generated-members keyword. Care must be taken to ensure members exist when importing from units.py, as these will not be linted.
- class MDMC.common.units.Unit(string: str, components: defaultdict[list] = None)[source]
A class for defining unit strings
It possesses additional
*
and/
operands so that combined units can be returned.Note
NON-INTEGER POWER OPERATIONS ARE CURRENTLY NOT IMPEMENTED
- Parameters:
string (str) – The unit, which can contain
/
to specify divisors and^
to specify powers. It can contain int which are used to specify order of magnitude (e.g.10^6 Pa
). It can also contain negative powers, but there must not be a space between the negative sign and the number (e.g.Ang ^ -1
NOTAng ^ - 1
). Brackets and parentheses are not supported, and any of the characters[]()
will be ignored.components (defaultdict(list), optional) – Sets the
components
attribute (see Attributes). Default is None.
Examples
Base units can be set:
>>> time_unit = Unit('s')
Compound units can be set with spaces separating
base
units which are multiplied:>>> charge_unit = Unit('A s')
Compound units can be set with
/
separatingbase
units which are divided:>>> velocity_unit = Unit('m / s')
Units raised to a power can be set with
^
:>>> volume_unit = Unit('Ang ^ 3')
Compound units can be set with a combination of these operands:
>>> force_unit = Unit('kg m / s ^ 2')
To set an inverse unit, the power operation can be applied to a
Unit
:>>> frequency = Unit('s') ** -1
Or set with
^
within the string:>>> frequency = Unit('s ^ -1')
Or with
/
within the string:>>> frequency = Unit('1 / s')
Orders of magnitude can also be included:
>>> pressure = Unit('10 ^ 6 Pa')
- components
Contains the
components
of theUnit
, separated into two list (numerator
anddenominator
) depending on which side of the fraction each component is on. If theUnit
is abase
unit i.e. initialized usingUnit()
, then thecomponents
only has anumerator
and this is theUnit
string. If it a combinedUnit
(created by either__mul__
,__div__
or__pow__
) then theUnit
objects which combined to form it make up thecomponents
.- Type:
defaultdict(list)
- conversion_factor
The factor by which to multiply a value in order to express it in system units. For example
Unit('ps').conversion_factor
is1000.
as the system units are femtoseconds.- Type:
- property base: bool
Get whether the
Unit
is abase
or compoundUnit
- Returns:
If True,
Unit
is abase
Unit
(only has a single element in thecomponents
numerator
list)- Return type:
bool
- property conversion_factor: float
Calculates the factor by which a value with this
Unit
should be multiplied in order to express it in system units. This takes into account any orders of magnitude and compound units. Theconversion_factor
for aUnit
composed only of system units is therefore always 1.- Returns:
The conversion factor to system units for the relevant property.
- Return type:
float
- class MDMC.common.units.UnitFloat(value: float, unit: Unit | str)[source]
Subclasses float so that it contains a
unit
attributeunit
attribute is returned when __repr__ or __str__ are called.- Parameters:
Note
As both
__repr__
and__deepcopy__
rely on the float being real, this class is not compatible with complex numbers. This should be immaterial as no quantity which possesses units is complex.
- class MDMC.common.units.UnitNDArray(shape, unit, dtype=<class 'float'>, buffer=None, offset=0, strides=None, order=None)[source]
Subclasses
ndarray
so that it contains aunit
attributeunit
attribute is returned when__repr__
or__str__
are called- Parameters:
shape (tuple of ints) – Shape of created
array
.dtype (data-type, optional) – Any object that can be interpreted as a NumPy data type.
buffer (object exposing NumPy buffer interface, optional) – Used to fill the
array
with data.offset (int, optional) – Offset of
array
data inbuffer
.strides (tuple of ints, optional) – Strides of data in memory.
order (str, optional) – Either
C
for row-major orF
for column-major. Default isC
.
- MDMC.common.units.create_units(codata_version: str) dict[Unit, float] [source]
Creates a dict of
Unit
based on the CODATA version.- Parameters:
codata_version (str) – The CODATA version to be used
- Returns:
Contains (
Unit
: conversion factor) pairs- Return type:
dict
- MDMC.common.units.unit_array(obj, unit: Unit | str, dtype=None) UnitNDArray [source]
Helper function for creating a
UnitNDArray
from anarray
or any nested sequenceThis mimics the manner in which NumPy creates arrays (although is in Python not C), except several arguments are excluded.
Also, unlike
np.array(None)
, passingobj=None
tounit_array
results in None being returned. This allows classes to have properties with units which can be either have avalue
or be undefined.- Parameters:
- Returns:
A
UnitArray
object satisfying the specified requirements.- Return type:
UnitArray