fastoad.openmdao.variables.variable_list module
Class for managing a list of OpenMDAO variables.
- class fastoad.openmdao.variables.variable_list.VariableList(iterable=(), /)[source]
Bases:
listClass for storing OpenMDAO variables.
A list of
Variableinstances, but items can also be accessed through variable names. It also has utilities to be converted from/to some other data structures (python dict, OpenMDAO IndepVarComp, pandas DataFrame)See documentation of
Variableto see how to manipulate each element.There are several ways for adding variables:
# 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_A = VariableList() vars_A.append(var_1) # Adds directly a Variable instance vars_A['var/2'] = metadata_2 # Adds the variable with given name and given metadata
Note
Adding a Variable instance with a name that is already in the VariableList instance will replace the previous Variable instance instead of adding a new one.
# It is also possible to instantiate a VariableList instance from another VariableList # instance or a simple list of Variable instances vars_B = VariableList(vars_A) vars_C = VariableList([var_1]) # An existing VariableList instance can also receive the content of another VariableList # instance. vars_C.update(vars_A) # variables in vars_A will overwrite variables with same # name in vars_C
After that, following equalities are True:
print( var_1 in vars_A ) print( 'var/1' in vars_A.names() ) print( 'var/2' in vars_A.names() )
- metadata_keys() List[str][source]
- Returns:
the metadata keys that are common to all variables in the list
- append(var: Variable) None[source]
Appends 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.
- add_var(name, **kwargs)[source]
Adds, or replace, the named variable with given attributes
- Parameters:
name –
kwargs –
- update(other_var_list: list, 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 (special case: if one in other_var_list has an empty description, the original description is kept)
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() IndepVarComp[source]
- Returns:
an OpenMDAO IndepVarComp instance with all variables from current list
- to_dataframe() 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: Mapping[str, dict] | Iterable[Tuple[str, dict]]) VariableList[source]
Creates a VariableList instance from a dict-like object.
- Parameters:
var_dict –
- Returns:
a VariableList instance
- classmethod from_ivc(ivc: IndepVarComp) VariableList[source]
Creates a VariableList instance from an OpenMDAO IndepVarComp instance
- Parameters:
ivc – an IndepVarComp instance
- Returns:
a VariableList instance
- classmethod from_dataframe(df: DataFrame) 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: Problem, use_initial_values: bool = False, get_promoted_names: bool = True, promoted_only: bool = True, io_status: str = 'all') VariableList[source]
Creates a VariableList instance containing inputs and outputs of an OpenMDAO Problem.
The inputs (is_input=True) correspond to the variables of IndepVarComp components and all the unconnected input variables.
Note
Variables from _auto_ivc are ignored.
- Parameters:
problem – OpenMDAO Problem instance to inspect
use_initial_values – if True, or if problem has not been run, 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
io_status – to choose with type of variable we return (“all”, “inputs, “outputs”)
- Returns:
VariableList instance
- classmethod from_unconnected_inputs(problem: Problem, with_optional_inputs: bool = False) 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
- clear()
Remove all items from list.
- copy()
Return a shallow copy of the list.
- count(value, /)
Return number of occurrences of value.
- extend(iterable, /)
Extend list by appending elements from the iterable.
- index(value, start=0, stop=9223372036854775807, /)
Return first index of value.
Raises ValueError if the value is not present.
- insert(index, object, /)
Insert object before index.
- pop(index=-1, /)
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
- remove(value, /)
Remove first occurrence of value.
Raises ValueError if the value is not present.
- reverse()
Reverse IN PLACE.
- sort(*, key=None, reverse=False)
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.