fastoad.openmdao.variables module

Module for managing OpenMDAO variables

class fastoad.openmdao.variables.Variable(name, **kwargs)[source]

Bases: Hashable

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() and description().

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_openmdao_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

name

Name of the variable

metadata: Dict

Dictionary for metadata of the variable

classmethod read_variable_descriptions(file_parent: str, update_existing: bool = True)[source]

Reads variable descriptions in indicated folder or package, if it contains some.

The file variable_descriptions.txt is looked for. Nothing is done if it is not found (no error raised also).

Each line of the file should be formatted like:

my:variable||The description of my:variable, as long as needed, but on one line.
Parameters
  • file_parent – the folder path or the package name that should contain the file

  • update_existing – if True, previous descriptions will be updated. if False, previous descriptions will be erased.

classmethod update_variable_descriptions(variable_descriptions: Union[Mapping[str, str], Iterable[Tuple[str, str]]])[source]

Updates description of variables.

Parameters

variable_descriptions – dict-like object with variable names as keys and descriptions as values

classmethod get_openmdao_keys()[source]
Returns

the keys that are used in OpenMDAO variables

property value

value of the variable

property units

units associated to value (or None if not found)

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

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.

names()List[str][source]
Returns

names of variables

metadata_keys()List[str][source]
Returns

the metadata keys that are common to all variables in the list

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.

update(other_var_list: fastoad.openmdao.variables.VariableList, add_variables: bool = True)[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, unknown variables are also added

to_ivc()openmdao.core.indepvarcomp.IndepVarComp[source]
Returns

an OpenMDAO IndepVarComp instance with all variables from current 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_openmdao_keys(). Values in Series “value” are floats or lists (numpy arrays are converted).

Returns

a pandas DataFrame instance with all variables from current list

classmethod from_dict(var_dict: Union[Mapping[str, dict], Iterable[Tuple[str, dict]]])fastoad.openmdao.variables.VariableList[source]

Creates a VariableList instance from a dict-like object.

Parameters

var_dict

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_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_openmdao_keys().

Parameters

df – a DataFrame instance

Returns

a VariableList instance

classmethod from_problem(problem: openmdao.core.problem.Problem, use_initial_values: bool = False, get_promoted_names: bool = True, promoted_only: bool = True)fastoad.openmdao.variables.VariableList[source]

Creates a VariableList instance containing inputs and outputs of 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.

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, promoted names will be returned instead of absolute ones (if no promotion, absolute name will be returned)

  • promoted_only – if True, only promoted variable names will be returned

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