Parameters and ParameterFrames in bluemira
Parameters and ParameterFrames are the mechanism bluemira uses to contain
metadata about a given value.
Each Parameter must have a unit associated with its value, and can have a
source, description and long_name.
ParameterFrames implicitly convert parameter values’ units to the
base units of bluemira.
from dataclasses import dataclass
from bluemira.base.parameter_frame import Parameter, ParameterFrame
Parameters and Units
First, I make a small ParameterFrame and compare the two methods of creating it.
@dataclass
class MyParameterFrame(ParameterFrame):
"""A ParameterFrame"""
R_0: Parameter[float]
A: Parameter[float]
# the unit "" is equivalent to "dimensionless"
mypf = MyParameterFrame.from_dict({
"A": {"value": 5, "unit": ""},
"R_0": {"value": 8, "unit": "m"},
})
mypf2 = MyParameterFrame.from_dict({
"A": {"value": 5, "unit": ""},
"R_0": {"value": 8, "unit": "m"},
})
print(mypf)
print(mypf2)
# Both frames equal
assert mypf == mypf2 # noqa: S101
Trying to set a unit with the wrong dimension
mydiffval = MyParameterFrame.from_dict({
"A": {"value": 6, "unit": "m"},
"R_0": {"value": 8, "unit": "m"},
})
try:
mypf.update_from_frame(mydiffval)
except ValueError as ve:
print(ve)
Changing a value of a parameter with a compatible but different unit
mypf.update_from_dict({"R_0": {"value": 6000, "unit": "mm"}})
print(mypf)
Accessing the value of a parameter in a different unit
print(mypf.R_0.value_as("cm")) # 600