Managing Physical Units#
In GDM, all physical quantities are wrapped in pint objects (https://pypi.org/project/Pint/).
Users import physical quantities when building new models or updating model parameters.
Units are validated for all physical quantities.
All quantities are imported from
gdm.quantities
.
Creating Quantities#
from gdm.quantities import ReactivePower
power = ReactivePower(800, "kilovar")
print(power)
800 kilovar
The to
method can be used for unit conversion
print(power.to("var"))
800000.0 var
The quantitiy models can be used with list and numpy arrays
powers = ReactivePower([400, 200, 100], "kilovar")
print(powers)
print(powers.to("var"))
[400 200 100] kilovar
[400000.0 200000.0 100000.0] var
In certain cases, additional validation may be performed. e.g. PV systems can only provide active power hence represented by PossitiveActivePower
from gdm.quantities import PositiveActivePower
try:
PositiveActivePower(-700, "kilowatt")
except Exception as e:
print(e)
Active power (-700, kilowatt) must be positive.
Validation Limitations#
Warning
Unit validation does not occur until the quantity is added to a data model. In the example below, the rated_voltage
object is built with incorrect units. It is only validated when we instantiate the DistributionBus
component.
from gdm.distribution.enums import LimitType, Phase, VoltageTypes
from gdm.distribution.components import DistributionSubstation
from gdm.distribution.common.limitset import VoltageLimitSet
from gdm.distribution.components import DistributionFeeder
from gdm.distribution.components import DistributionBus
from gdm.quantities import PositiveVoltage
from infrasys import Location
rated_voltage = PositiveVoltage(400, "ampere")
/opt/hostedtoolcache/Python/3.12.10/x64/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py:502: UserWarning: Ellipsis is not a Python type (it may be an instance of an object), Pydantic will allow any object with no validation since we cannot even enforce that the input is an instance of the given type. To get rid of this error wrap the type with `pydantic.SkipValidation`.
warn(
bus = DistributionBus(
voltage_type=VoltageTypes.LINE_TO_LINE,
phases=[Phase.A, Phase.B, Phase.C],
rated_voltage=rated_voltage,
name="DistBus1",
substation=DistributionSubstation.example(),
feeder=DistributionFeeder.example(),
voltagelimits=[
VoltageLimitSet(
limit_type=LimitType.MIN, value=PositiveVoltage(400 * 0.9, "volt")
),
VoltageLimitSet(
limit_type=LimitType.MAX, value=PositiveVoltage(400 * 1.1, "volt")
),
],
coordinate=Location(x=20.0, y=30.0),
)
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
Cell In[6], line 1
----> 1 bus = DistributionBus(
2 voltage_type=VoltageTypes.LINE_TO_LINE,
3 phases=[Phase.A, Phase.B, Phase.C],
4 rated_voltage=rated_voltage,
5 name="DistBus1",
6 substation=DistributionSubstation.example(),
7 feeder=DistributionFeeder.example(),
8 voltagelimits=[
9 VoltageLimitSet(
10 limit_type=LimitType.MIN, value=PositiveVoltage(400 * 0.9, "volt")
11 ),
12 VoltageLimitSet(
13 limit_type=LimitType.MAX, value=PositiveVoltage(400 * 1.1, "volt")
14 ),
15 ],
16 coordinate=Location(x=20.0, y=30.0),
17 )
File /opt/hostedtoolcache/Python/3.12.10/x64/lib/python3.12/site-packages/pydantic/main.py:214, in BaseModel.__init__(self, **data)
212 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
213 __tracebackhide__ = True
--> 214 validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
215 if self is not validated_self:
216 warnings.warn(
217 'A custom validator is returning a value other than `self`.\n'
218 "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
219 'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
220 stacklevel=2,
221 )
ValidationError: 1 validation error for DistributionBus
rated_voltage
Assertion failed, Unit must be compatible with volt [type=assertion_error, input_value=<Quantity(400, 'ampere')>, input_type=PositiveVoltage]
For further information visit https://errors.pydantic.dev/2.10/v/assertion_error