strip_unit

mdcraft.lib.unit.strip_unit(quantity: Any, output_unit: str | 'unit.Unit' | U_ | None = None, /) tuple[Any, str | 'unit.Unit' | U_ | None][source]

Separates the unit from a physical quantity after, optionally, converting it to a different unit.

Parameters:
quantityany, positional-only

Physical quantity from which to strip the unit.

output_unitstr, openmm.unit.Unit, or pint.Unit, positional-only, optional

Unit to convert quantity to. If not specified, the original unit is returned.

Returns:
valueany

Value of the physical quantity.

unitstr, openmm.unit.Unit, or pint.Unit

Unit of the physical quantity.

Examples

This function supports a number of different input types. The following examples provide an exhaustive overview of the possible inputs and outputs. unit refers to the openmm.unit module, ureg refers to a pint.UnitRegistry instance, and Q_ refers to the pint.Quantity class attached to the ureg instance.

Strings containing quantities without units are typecasted to floating-point numbers and converted, if necessary, before being returned with the unaltered user-specified unit:

>>> strip_unit("9.80665")
(9.80665, None)
>>> strip_unit("9.80665", "m/s^2")
(9.80665, 'm/s^2')
>>> strip_unit("9.80665", unit.meter / unit.second**2)
(9.80665, Unit({BaseUnit(name="meter", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit("9.80665", ureg.meter / ureg.second**2)
(9.80665, <Unit('meter / second ** 2')>)

Strings containing quantities with units have their numeric parts typecasted to floating-point numbers and returned with the user-specified unit or a formatted string containing the original unit:

>>> strip_unit("9.80665 meter/second^2")
(9.80665, 'meter / second ** 2')
>>> strip_unit("9.80665 meters/second^2", "ft/s^2")
(32.17404855643044, 'ft/s^2')
>>> strip_unit("9.80665 m/s^2", unit.foot / unit.second**2)
(32.17404855643044, Unit({BaseUnit(name="foot", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit("9.80665 m/s^2", ureg.foot / ureg.second**2)
(32.17404855643044, <Unit('foot / second ** 2')>)

Dimensionless OpenMM quantities have their underlying values returned with None or an OpenMM or Pint unit if a unit was specified by the user:

>>> strip_unit(unit.Quantity(9.80665))
(9.80665, None)
>>> strip_unit(unit.Quantity(9.80665), "m/s^2")
(9.80665, Unit({BaseUnit(name="meter", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(unit.Quantity(9.80665), unit.meter / unit.second**2)
(9.80665, Unit({BaseUnit(name="meter", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(unit.Quantity(9.80665), ureg.meter / ureg.second**2)
(9.80665, <Unit('meter / second ** 2')>)

OpenMM quantities have their underlying values returned with the original OpenMM unit, a user-specified unit if it is an OpenMM or Pint unit, or an equivalent OpenMM unit if the user-specified unit is a string:

>>> strip_unit(9.80665 * unit.meter / unit.second**2)
(9.80665, Unit({BaseUnit(name="meter", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(9.80665 * unit.meter / unit.second**2, "ft/s^2")
(32.17404855643044, Unit({BaseUnit(name="foot", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(9.80665 * unit.meter / unit.second**2, unit.foot / unit.second**2)
(32.17404855643044, Unit({BaseUnit(name="foot", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(9.80665 * unit.meter / unit.second**2, ureg.foot / ureg.second**2)
(32.17404855643044, <Unit('foot / second ** 2')>)

Similarly, dimensionless Pint quantities have their underlying values returned with None or a Pint unit if a unit was specified by the user:

>>> strip_unit(Q_(9.80665))
(9.80665, None)
>>> strip_unit(Q_("9.80665"), "m/s^2")
(9.80665, 'meter / second ** 2')
>>> strip_unit(Q_("9.80665"), unit.meter / unit.second**2)
(9.80665, Unit({BaseUnit(name="meter", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(Q_("9.80665"), ureg.meter / ureg.second**2)
(9.80665, <Unit('meter / second ** 2')>)

Finally, Pint quantities have their underlying values returned with the original Pint unit, a user-specified unit if it is an OpenMM or Pint unit, or an equivalent Pint unit if the user-specified unit is a string:

>>> strip_unit(Q_("9.80665 m/s^2"))
(9.80665, <Unit('meter / second ** 2')>)
>>> strip_unit(Q_("9.80665 m/s^2"), "ft/s^2")
(32.17404855643044, <Unit('foot / second ** 2')>)
>>> strip_unit(Q_("9.80665 m/s^2"), unit.foot / unit.second**2)
(32.17404855643044, Unit({BaseUnit(name="foot", ...): 1.0, BaseUnit(name="second", ...): -2.0}))
>>> strip_unit(Q_("9.80665 m/s^2"), ureg.foot / ureg.second**2)
(32.17404855643044, <Unit('foot / second ** 2')>)