Work with Units
Create components with unit-aware fields using the HasPerUnit mixin.
Create Components with Units
Section titled “Create Components with Units”>>> from typing import Annotated>>> from infrasys import Component>>> from r2x_core.units import HasPerUnit, Unit>>> class Generator(HasPerUnit, Component):... base_power: Annotated[float, Unit("MVA")]... active_power: Annotated[float, Unit("pu", base="base_power")]>>> gen = Generator(name="Gen1", base_power=100.0, active_power=0.95)>>> gen.base_power100.0>>> gen.active_power0.95Input Values in Natural Units
Section titled “Input Values in Natural Units”Automatically convert natural units to per-unit:
>>> from typing import Annotated>>> from infrasys import Component>>> from r2x_core.units import HasPerUnit, Unit>>> class Generator(HasPerUnit, Component):... base_power: Annotated[float, Unit("MVA")]... active_power: Annotated[float, Unit("pu", base="base_power")]>>> gen = Generator(... name="Gen2",... base_power=250.0,... active_power={"value": 200.0, "unit": "MVA"}... )>>> gen.active_power0.8Switch Display Modes
Section titled “Switch Display Modes”Change how values are displayed:
>>> from typing import Annotated>>> from infrasys import Component>>> from r2x_core.units import HasPerUnit, Unit, UnitSystem, set_unit_system>>> from r2x_core import System>>> class Generator(HasPerUnit, Component):... base_power: Annotated[float, Unit("MVA")]... active_power: Annotated[float, Unit("pu", base="base_power")]>>> set_unit_system(UnitSystem.DEVICE_BASE)>>> gen = Generator(name="Gen", base_power=150.0, active_power=0.90)>>> gen.active_power0.9>>> set_unit_system(UnitSystem.NATURAL_UNITS)>>> repr_str = repr(gen)>>> "MVA" in repr_strTrueSee Also
Section titled “See Also”- Working with units - Full unit documentation