fastoad.openmdao.variables module¶
Module for managing OpenMDAO variables
-
class
fastoad.openmdao.variables.
Variable
(*args, **kwds)[source]¶ Bases:
collections.abc.Hashable
,typing.Generic
A class for storing data of OpenMDAO variables.
Instantiation is expected to be done through keyword arguments only.
Beside the mandatory parameter ‘name, kwargs is expected to have keys ‘value’, ‘units’ and ‘desc’, that are accessible respectively through properties
name()
,value()
,units()
anddescription()
.Other keys are possible. They match the definition of OpenMDAO’s method
Component.add_output()
described here.These keys can be listed with class method
get_authorized_keys()
. Any other key in kwargs will be silently ignored.Special behaviour:
description()
will return the content of kwargs[‘desc’] unless these 2 conditions are met:kwargs[‘desc’] is None or ‘desc’ key is missing
a description exists in FAST-OAD internal data for the variable name
Then, the internal description will be returned by
description()
- Parameters
kwargs – the attributes of the variable, as keyword arguments
-
property
description
¶ description of the variable (or None if not found)
-
property
is_input
¶ I/O status of the variable.
True if variable is a problem input
False if it is an output
None if information not found
-
metadata
: Dict¶ Dictionary for metadata of the variable
-
name
¶ Name of the variable
-
property
units
¶ units associated to value (or None if not found)
-
property
value
¶ value of the variable
-
class
fastoad.openmdao.variables.
VariableList
(iterable=(), /)[source]¶ Bases:
list
Class for storing OpenMDAO variables
A list of Variable instances, but items can also be accessed through variable names.
There are 2 ways for adding a variable:
# Assuming these Python variables are ready var_1 = Variable('var/1', value=0.) metadata_2 = {'value': 1., 'units': 'm'} # ... a VariableList instance can be populated like this vars = VariableList() vars.append(var_1) # Adds directly a Variable instance vars['var/2'] = metadata_2 # Adds the variable with given name and given metadata
After that, following equalities are True:
print( var_1 in vars ) print( 'var/1' in vars.names() ) print( 'var/2' in vars.names() )
Note
Adding a Variable instance that has a name that is already in the VariableList instance will replace the previous Variable instance instead of adding a new one.
-
append
(var: fastoad.openmdao.variables.Variable) → None[source]¶ Append var to the end of the list, unless its name is already used. In that case, var will replace the previous Variable instance with the same name.
-
classmethod
from_dataframe
(df: pandas.core.frame.DataFrame) → fastoad.openmdao.variables.VariableList[source]¶ Creates a VariableList instance from a pandas DataFrame instance.
The DataFrame instance is expected to have column names “name” + some keys among the ones given by
Variable.get_authorized_keys()
.- Parameters
df – a DataFrame instance
- Returns
a VariableList instance
-
classmethod
from_ivc
(ivc: openmdao.core.indepvarcomp.IndepVarComp) → fastoad.openmdao.variables.VariableList[source]¶ Creates a VariableList instance from an OpenMDAO IndepVarComp instance
- Parameters
ivc – an IndepVarComp instance
- Returns
a VariableList instance
-
classmethod
from_problem
(problem: openmdao.core.problem.Problem, use_initial_values: bool = False, get_promoted_names=True) → fastoad.openmdao.variables.VariableList[source]¶ Creates a VariableList instance containing inputs and outputs of a an OpenMDAO Problem.
Warning
problem.setup() must have been run.
The inputs (is_input=True) correspond to the variables of IndepVarComp components and all the unconnected variables.
If variables are promoted, the promoted name will be used. Otherwise (and if promoted_only is False), the absolute name will be used.
Note
Variables from _auto_ivc are ignored.
- Parameters
problem – OpenMDAO Problem instance to inspect
use_initial_values – if True, returned instance will contain values before computation
get_promoted_names – if True, only promoted variable names will be returned
- Returns
VariableList instance
-
classmethod
from_system
(system: openmdao.core.system.System) → fastoad.openmdao.variables.VariableList[source]¶ Creates a VariableList instance containing inputs and outputs of a an OpenMDAO System. The inputs (is_input=True) correspond to the variables of IndepVarComp components and all the unconnected variables.
Warning: setup() must NOT have been called.
In the case of a group, if variables are promoted, the promoted name will be used. Otherwise, the absolute name will be used.
- Parameters
system – OpenMDAO Component instance to inspect
- Returns
VariableList instance
-
classmethod
from_unconnected_inputs
(problem: openmdao.core.problem.Problem, with_optional_inputs: bool = False) → fastoad.openmdao.variables.VariableList[source]¶ Creates a VariableList instance containing unconnected inputs of an OpenMDAO Problem.
Warning
problem.setup() must have been run.
If optional_inputs is False, only inputs that have numpy.nan as default value (hence considered as mandatory) will be in returned instance. Otherwise, all unconnected inputs will be in returned instance.
- Parameters
problem – OpenMDAO Problem instance to inspect
with_optional_inputs – If True, returned instance will contain all unconnected inputs. Otherwise, it will contain only mandatory ones.
- Returns
VariableList instance
-
metadata_keys
() → List[str][source]¶ - Returns
the metadata keys that are common to all variables in the list
-
to_dataframe
() → pandas.core.frame.DataFrame[source]¶ Creates a DataFrame instance from a VariableList instance.
Column names are “name” + the keys returned by
Variable.get_authorized_keys()
. Values in Series “value” are floats or lists (numpy arrays are converted).- Returns
a pandas DataFrame instance with all variables from current list
-
to_ivc
() → openmdao.core.indepvarcomp.IndepVarComp[source]¶ - Returns
an OpenMDAO IndepVarComp instance with all variables from current list
-
update
(other_var_list: fastoad.openmdao.variables.VariableList, add_variables: bool = False)[source]¶ Uses variables in other_var_list to update the current VariableList instance.
- For each Variable instance in other_var_list:
if a Variable instance with same name exists, it is replaced by the one in other_var_list
if not, Variable instance from other_var_list will be added only if add_variables==True
- Parameters
other_var_list – source for new Variable data
add_variables – if True, variables can be added instead of just updated
-