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
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: collections.defaultdict[list] = None)
A class for defining unit strings
It possesses additional
/operands so that combined units can be returned.
NON-INTEGER POWER OPERATIONS ARE CURRENTLY NOT IMPEMENTED
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 ^ -1NOT
Ang ^ - 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.
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
baseunits 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
>>> frequency = Unit('s') ** -1
Or set with
^within the string:
>>> frequency = Unit('s ^ -1')
/within the string:
>>> frequency = Unit('1 / s')
Orders of magnitude can also be included:
>>> pressure = Unit('10 ^ 6 Pa')
Unit, separated into two list (
denominator) depending on which side of the fraction each component is on. If the
baseunit i.e. initialized using
Unit(), then the
componentsonly has a
numeratorand this is the
Unitstring. If it a combined
Unit(created by either
__pow__) then the
Unitobjects which combined to form it make up the
The factor by which to multiply a value in order to express it in system units. For example
1000.as the system units are femtoseconds.
- property base: bool
Get whether the
Unit(only has a single element in the
- Return type
- 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. The
Unitcomposed only of system units is therefore always 1.
The conversion factor to system units for the relevant property.
- Return type
- class MDMC.common.units.UnitFloat(value: float, unit: Union[MDMC.common.units.Unit, str])
Subclasses float so that it contains a
unitattribute is returned when __repr__ or __str__ are called.
__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)
ndarrayso that it contains a
unitattribute is returned when
shape (tuple of ints) – Shape of created
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
offset (int, optional) – Offset of
strides (tuple of ints, optional) – Strides of data in memory.
order (str, optional) – Either
Cfor row-major or
Ffor column-major. Default is
- MDMC.common.units.create_units(codata_version: str) dict[MDMC.common.units.Unit, float]
Creates a dict of
Unitbased on the CODATA version.
codata_version (str) – The CODATA version to be used
Unit: conversion factor) pairs
- Return type
- MDMC.common.units.unit_array(obj, unit: Union[MDMC.common.units.Unit, str], dtype=None) MDMC.common.units.UnitNDArray
Helper function for creating a
arrayor any nested sequence
This mimics the manner in which NumPy creates arrays (although is in Python not C), except several arguments are excluded.
unit_arrayresults in None being returned. This allows classes to have properties with units which can be either have a
valueor be undefined.
UnitArrayobject satisfying the specified requirements.
- Return type