Utilities#
Provides all the attrs-based utilities for the CSM base model and its subclasses.
- csm.models.utils.convert_if_allowable_type(value, target, allowable)#
Converts the type of
valueto thetargettype if it is one of anallowabletype. Ifvalueis already the correct type, or is not of anallowabletype for conversion, thevaluewill be returned unmodified.- Parameters:
value (Any) -- User input field value.
target (type) -- The type the
valueshould be converted to if it is not already of that type.allowable (type | tuple[type]) -- The allowable type(s) for conversion, i.e.,
(str, int)for a target type offloat.
- Returns:
Any -- The original
valueor a convertedvalueof typetarget.- Return type:
Any
- csm.models.utils.convert_float(value, *, target=<class 'float'>, allowable=(<class 'int'>, <class 'str'>))#
Converts the type of
valueto thetargettype if it is one of anallowabletype. Ifvalueis already the correct type, or is not of anallowabletype for conversion, thevaluewill be returned unmodified.- Parameters:
value (Any) -- User input field value.
target (type) -- The type the
valueshould be converted to if it is not already of that type.allowable (type | tuple[type]) -- The allowable type(s) for conversion, i.e.,
(str, int)for a target type offloat.
- Returns:
Any -- The original
valueor a convertedvalueof typetarget.- Return type:
Any
- csm.models.utils.create_field(obj, units='unitless', io_type='input', *, default=None, additional_validators=None, additional_converters=None, **kwargs)#
Creates an
obj-based field with pre-loaded defaults, conversions, validations, and metadata.- Parameters:
obj (type) -- A type. Currently only accepts
int,float, orbool.units (str, optional) -- OpenMDAO-compatible units. See <https://openmdao.org/newdocs/versions/latest/features/units.html> for more details. Defaults to "unitless".
io_type (str, optional) -- One of "input", "output", or "both" for how the attribute should be initialized within a WISDEM model. Typically
xx_mass` and ``xx_costattributes are both inputs and outputs. Defaults to "input".default (int | float | bool, optional) -- Value of the default, if not None.
additional_validators (list[callable], optional) -- A list of additional validator functions to attach to the
attrs.fieldinitialization. Defaults to Noneadditional_converters (list[callable], optional) -- A list of additional converter functions to attach to the
attrs.fieldinitialization. Defaults to Nonekwargs (dict[str, Any], optional) -- Additional parameterizations to pass to
attrs.field.
- Returns:
attrs.field -- Creates an
attrs.fieldobject for the attribute.- Raises:
NotImplementedError -- Raised if an unsupported type object is passed. Only
int,float, andboolare accepted at this time.- Return type:
field
- csm.models.utils.reuse(attribute, *, default=None, metadata=None, init=None)#
Reuses an existing
attrs.Attributeobject with an updated default value.Borrowed idea from python-attrs/attrs#1429 until the functionality is fully integrated.
- Parameters:
attribute (
attrs.Attribute) -- The attribute to modify.default (Any, optional) -- The new default value.
metadata (None, optional) -- Updated metadata dictionary to change attributes such as "units" or "io". Defaults to None.
init (bool | None) -- Custom value for the init attribute, if modification is desired. If None, then the existing value for the
attributewill be used. Defaults to False.
- Returns:
attrib -- The new attribute object used in class initialization.
- Return type:
attrib
- csm.models.utils.generate_parameterization(parameterized_kwargs)#
Validates the kwargs to be parameterized and creates the full set of values to be used for each argument.
- Parameters:
parameterized_kwargs (dict[str, int | float | bool]) --
Dictionary of independent variables with an iterable value consisting of an explicit set of values or range of values generated by
np.linspace. For both cases, the first value must be one of "inputs" or "range". Subsequent values should specified according to the case:- "inputs": all subsequent values will be used as inputs, e.g.,
{"tower_length": ("inputs", 90, 100)} will run 2 iterations, one with a 90m tower length and one with a 100 meter tower length.
- "range": subsequent values must be start, stop, num where stop is inclusive,
e.g., {"efficiency_max": ("range", 0.8, 1.0, 5)} will run 5 iterations of the model varying
efficiency_maxwith values 0.8, 0.85, 0.9, 0.95, and 1.0.
- Raises:
ValueError -- Raised if any of the keys of
parameterized_kwargsare not defined as a "range" or "inputs" style variable.ValueError -- Raised if fewer than 2 inputs are able to be generated by the parameterization type.
- Returns:
dict[str, list]] --
- Dictionary of each argument and the full set of values to parameterize
a model.
- Return type:
dict[str, list]
- csm.models.utils.get_dependent_attributes(parameter_graph, name)#
Returns a set of model attributes that depend on the value of
name.- Parameters:
parameter_graph (nx.DiGraph) -- The
CSMBase.parameter_graph.name (str) -- The name of a model attribute that a user inputs or can be calculated.
- Returns:
set[str] -- Set of model attribute names that rely on the value of
name.- Return type:
set[str]
- csm.models.utils.get_descendant_attributes(parameter_graph, name)#
Returns a set of model attributes
namerequires.- Parameters:
parameter_graph (nx.DiGraph) -- The
CSMBase.parameter_graph.name (str) -- The name of a model attribute that a user inputs or can be calculated.
- Returns:
set[str] -- Set of model attribute names that
namerelies on.- Return type:
set[str]