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 ^ -1NOTAng ^ - 1). Brackets and parentheses are not supported, and any of the characters[]()will be ignored.components (defaultdict(list), optional) – Sets the
componentsattribute (see Attributes). Default is None.
Examples
Base units can be set:
>>> time_unit = Unit('s')
Compound units can be set with spaces separating
baseunits which are multiplied:>>> charge_unit = Unit('A s')
Compound units can be set with
/separatingbaseunits 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
componentsof theUnit, separated into two list (numeratoranddenominator) depending on which side of the fraction each component is on. If theUnitis abaseunit i.e. initialized usingUnit(), then thecomponentsonly has anumeratorand this is theUnitstring. If it a combinedUnit(created by either__mul__,__div__or__pow__) then theUnitobjects 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_factoris1000.as the system units are femtoseconds.- Type:
- property base: bool
Get whether the
Unitis abaseor compoundUnit- Returns:
If True,
Unitis abaseUnit(only has a single element in thecomponentsnumeratorlist)- Return type:
bool
- property conversion_factor: float
Calculates the factor by which a value with this
Unitshould be multiplied in order to express it in system units. This takes into account any orders of magnitude and compound units. Theconversion_factorfor aUnitcomposed 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
unitattributeunitattribute 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
ndarrayso that it contains aunitattributeunitattribute 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
arraywith data.offset (int, optional) – Offset of
arraydata inbuffer.strides (tuple of ints, optional) – Strides of data in memory.
order (str, optional) – Either
Cfor row-major orFfor column-major. Default isC.
- MDMC.common.units.create_units(codata_version: str) dict[Unit, float][source]
Creates a dict of
Unitbased 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
UnitNDArrayfrom anarrayor 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=Nonetounit_arrayresults in None being returned. This allows classes to have properties with units which can be either have avalueor be undefined.- Parameters:
- Returns:
A
UnitArrayobject satisfying the specified requirements.- Return type:
UnitArray