relevance.py

relevance.py#

Class definitions for Relevance and related classes.

class openmdao.utils.relevance.Relevance(model, fwd_meta, rev_meta, rel_array_cache)[source]

Bases: object

Class that computes relevance based on a data flow graph.

It determines current relevance based on the current set of forward and reverse seed variables. Initial relevance is determined by starting at a given seed and traversing the data flow graph in the specified direction to find all relevant variables and systems. That information is then represented as a boolean array where True means the variable or system is relevant to the seed. Relevance with respect to groups of seeds, for example, one forward seed vs. all reverse seeds, is determined by combining the boolean relevance arrays for the individual seeds in the following manner: (fwd_array1 | fwd_array2 | …) & (rev_array1 | rev_array2 | …). In other words, the union of the fwd arrays is intersected with the union of the rev arrays.

The full set of fwd and rev seeds must be set at initialization time. At any point after that, the set of active seeds can be changed using the set_seeds method, but those seeds must be subsets of the full set of seeds.

Parameters:
model<Group>

The top level group in the system hierarchy.

fwd_metadict

Dictionary of design variable metadata. Keys don’t matter.

rev_metadict

Dictionary of response variable metadata. Keys don’t matter.

rel_array_cachedict

Cache of relevance arrays stored by array hash.

Attributes:
_graph<nx.DirectedGraph>

Dependency graph. Dataflow graph containing both variables and systems.

_var2idxdict

dict of all variables in the graph mapped to the row index into the variable relevance array.

_sys2idxdict

dict of all systems in the graph mapped to the row index into the system relevance array.

_seed_varsdict

Maps direction to currently active seed variable names.

_all_seed_varsdict

Maps direction to all seed variable names.

_activebool or None

If True, relevance is active. If False, relevance is inactive. If None, relevance is uninitialized.

_seed_var_mapdict

Nested dict of the form {fwdseed(s): {revseed(s): var_array, …}}. Keys that contain multiple seeds are sorted tuples of seed names.

_seed_sys_mapdict

Nested dict of the form {fwdseed(s): {revseed(s): sys_array, …}}. Keys that contain multiple seeds are sorted tuples of seed names.

_single_seed2relvarsdict

Dict of the form {‘fwd’: {seed: var_array}, ‘rev’: …} where each seed is a key and var_array is the variable relevance array for the given seed.

_single_seed2relsysdict

Dict of the form {‘fwd’: {seed: sys_array}, ‘rev’: …} where each seed is a key and var_array is the system relevance array for the given seed.

_nonlinear_setsdict

Dict of the form {‘pre’: pre_rel_array, ‘iter’: iter_rel_array, ‘post’: post_rel_array}.

_current_rel_varrayndarray

Array representing the variable relevance for the currently active seeds.

_current_rel_sarrayndarray

Array representing the system relevance for the currently active seeds.

_rel_array_cachedict

Cache of relevance arrays stored by array hash.

_no_dv_responseslist

List of responses that have no relevant design variables.

_redundant_adjoint_systemsset or None

Set of systems that may benefit from caching RHS arrays and solutions to avoid some linear solves.

_seed_cachedict

Maps seed variable names to the source of the seed.

_rel_array_cachedict

Cache of relevance arrays stored by array hash.

emptybool

If True, relevance is empty and no relevance checking will be performed.

__init__(model, fwd_meta, rev_meta, rel_array_cache)[source]

Initialize all attributes.

active(active)[source]

Context manager for temporarily deactivating relevance.

Note that if this relevance object is already inactive, this context manager will have no effect, i.e., calling this with active=True will not activate an inactive relevance object, but calling it with active=False will deactivate an active relevance object.

The only way to activate an otherwise inactive relevance object is to use the all_seeds_active, seeds_active, or nonlinear_active context managers and this will only work if _active is None or True.

Parameters:
activebool

If True, activate relevance. If False, deactivate relevance.

Yields:
None
all_seeds_active()[source]

Context manager where all seeds are active.

If _active is False, this will have no effect.

Yields:
None
any_relevant(names)[source]

Return True if any of the given variables are relevant.

Parameters:
namesiter of str

Iterator over variable names.

Returns:
bool

True if any of the given variables are relevant.

filter(systems, relevant=True)[source]

Filter the given iterator of systems to only include those that are relevant.

Parameters:
systemsiter of Systems

Iterator over systems.

relevantbool

If True, return only relevant systems. If False, return only irrelevant systems.

Yields:
System

Relevant system.

get_redundant_adjoint_systems()[source]

Find any systems that depend on responses that depend on other responses.

If any are found, it may be worthwhile to cache RHS arrays and solutions in order to avoid some linear solves.

Returns:
dict

Mapping of systems to the set of adjoints that can cause unnecessary linear solves.

is_relevant(name)[source]

Return True if the given variable is relevant.

Parameters:
namestr

Name of the variable.

Returns:
bool

True if the given variable is relevant.

is_relevant_system(name)[source]

Return True if the given named system is relevant.

Returns False if system has no subsystems with outputs.

Parameters:
namestr

Name of the System.

Returns:
bool

True if the given system is relevant.

iter_seed_pair_relevance(fwd_seeds=None, rev_seeds=None, inputs=False, outputs=False)[source]

Yield all relevant variables for each pair of seeds.

Parameters:
fwd_seedsiter of str or None

Iterator over forward seed variable names. If None use current registered seeds.

rev_seedsiter of str or None

Iterator over reverse seed variable names. If None use current registered seeds.

inputsbool

If True, include inputs.

outputsbool

If True, include outputs.

Yields:
set

Set of names of relevant variables.

list_relevance(relevant=True, type='system')[source]

Return a list of relevant variables and systems for the given seeds.

Parameters:
relevantbool

If True, return only relevant variables and systems. If False, return only irrelevant variables and systems.

typestr

If ‘system’, return only system names. If ‘var’, return only variable names.

Returns:
list of str

List of (ir)relevant variables or systems.

nonlinear_active(name, active=True)[source]

Context manager for activating a subset of systems using ‘pre’, ‘post’, or ‘iter’.

Parameters:
namestr

Name of the set to activate.

activebool

If False, relevance is temporarily deactivated.

Yields:
None
rel_vars_iter(rel_array, relevant=True)[source]

Return an iterator of relevant variable names.

Parameters:
rel_arrayndarray

Boolean relevance array. True means name is relevant.

relevantbool

If True, return only relevant names. If False, return only irrelevant names.

Yields:
str

Name of the relevant variable.

relevant_vars(name, direction, inputs=True, outputs=True)[source]

Return a set of variables relevant to the given dv/response in the given direction.

Parameters:
namestr

Name of the variable of interest.

directionstr

Direction of the search for relevant variables. ‘fwd’ or ‘rev’.

inputsbool

If True, include inputs.

outputsbool

If True, include outputs.

Returns:
set

Set of the relevant variables.

seeds_active(fwd_seeds=None, rev_seeds=None)[source]

Context manager where the specified seeds are active.

If _active is False, this will have no effect.

Parameters:
fwd_seedsiter of str or None

Iterator over forward seed variable names. If None use current active seeds.

rev_seedsiter of str or None

Iterator over reverse seed variable names. If None use current active seeds.

Yields:
None
openmdao.utils.relevance.get_relevance(model, of, wrt)[source]

Return a Relevance object for the given design vars, and responses.

Parameters:
model<Group>

The top level group in the system hierarchy.

ofdict

Dictionary of ‘of’ variables. Keys don’t matter.

wrtdict

Dictionary of ‘wrt’ variables. Keys don’t matter.

Returns:
Relevance

Relevance object.