Physical parameters
Parameters representing physical quantities, numerical values accompanied by a unit, are represented by PhysicalQuantity
. One main feature is that any necessary conversions from the specified unit to the declared unit are handled automatically by the framework.
We can declare a parameter by setting its unit:
class Diode(Configurable):
threshold = PhysicalQuantity(unit='V') # Declared in units of Volt.
Specifying it in the HOCON file as millivolt:
threshold: "10 [mV]" # The space between the value and the unit is optional (so "10[mV]" would equally do).
The configured instance will obtain the value as declared in units of Volt:
diode = Diode(config)
diode.threshold # 0.01
Note that the unit is only used for loading the parameter and applying the appropriate conversions
while the parameter's value is internally treated as a float
with the declared unit assumed
implicitly.
Alternative unit declaration
Using a string for unit declaration is in fact a shorthand. Behind the scenes there runs a unit engine which is based on a specific python package. Different engines are intended to be supported in the future, for now only the default based on pint is running.
We can select an engine as follows:
from hanna.physics import units
from hanna.physics.units import Pint, NumericalUnits, Units
units.engine = Pint # Select the pint package as the active engine.
units.engine = NumericalUnits # Select the numericalunits package as active engine.
units.engine = Units # Select the units package as active engine.
Select units can also be done directly through the active engine:
class Diode(Configurable):
threshold = PhysicalQuantity(unit=units.V)
This falls back to the active engine's unit objects, so we could equally do:
import pint
ur = pint.UnitRegistry()
class Diode(Configurable):
threshold = PhysicalQuantity(unit=ur.V)
Modifying units
If for some reason we want to change the declaration of an existing parameter we can modify the unit in the following way:
Diode.threshold.unit = 'mV'