OM Tools Developer API¶
- class omtools.core.variable.Variable(*args, **kwargs)¶
The
Variable
class is a base type for nodes in a Directed Acyclic Graph (DAG) that represents the computation to be performed during model evaluation.Each
Variable
object stores a function that constructs an OpenMDAOComponent
object corresponding to the computation that theVariable
object represents.- __add__(other)¶
Represent addition between two expressions
- __init__(*args, **kwargs)¶
Initialize self. See help(type(self)) for accurate signature.
- __mul__(other)¶
Represent multiplication between two expressions
- __pow__(other)¶
Represent raising an expression to a real number
- __radd__(other)¶
Represent addition between two expressions
- __rmul__(other)¶
Represent multiplication between two expressions
- __rsub__(other)¶
Represent subtraction between two expressions
- __rtruediv__(other)¶
Represent division between two expressions
- __sub__(other)¶
Represent subtraction between two expressions
- __truediv__(other)¶
Represent division between two expressions
- __weakref__¶
list of weak references to the object (if defined)
- add_dependency_node(dependency)¶
Add a dependency node to the DAG representing the computation that OpenMDAO will perform during model evaluation. Each
Variable
object represents some computation that returns a value. The number of dependency nodes of anVariable
object is the number of arguments required to return a value. for example, inc = a + b
, the objectc
has two dependencies:a
andb
.- Parameters
- predecesor: Variable
An Variable object upon which this Variable object depends
- compute_dag_cost() → int¶
Compute cost of traversing all nodes in the DAG starting at
self
; ignores cycles, so it is guaranteed to compute finite values of cost and terminate; required for sorting branches before performingtopological_sort
- Parameters
- branch: set[Variable]
Set of all Variable objects traversed so far in current branch. If a dependency is in
branch
, thencompute_dag_cost
will terminate and return a value. Ifself
is a leaf node, thencompute_dag_cost
will terminate.
- Returns
- int
Cost of traversing DAG starting with
self
- get_dependency_index(candidate) → Optional[int]¶
Get index of dependency in
self.dependencies
. Used for removing indirect dependencies that woud otherwise affect the cost of branches in the DAG, which would affect execution order, even with the sme constraints on execution order.- Parameters
- candidate: Variable
The candidate dependency node
- Returns
- Optional[int]
If
dependency
is a dependency ofself
, then the index ofdependency
inself.dependencies
is returned. Otherwise,None
is returned.
- incr_times_visited()¶
Increment number of times a node is visited during
topological_sort
. This is necessary fortopological_sort
to determine execution order for expressions.
- print_dag(depth=- 1, indent='')¶
Print the graph starting at this node (debugging tool)
- register_nodes(nodes: dict)¶
Register all nodes in DAG.
- Parameters
- nodes: dict[Variable]
Dictionary of nodes registered so far
- remove_dependency_by_index(index)¶
Remove dependency node, given its index. does nothing if
index
is out of range. SeeVariable.remove_dependency
.- Parameters
- index: int
Index within
self.dependencies
where the node to be removed might be
- remove_dependency_node(candidate)¶
Remove dependency node. Does nothing if candidate is not a dependency. Used for removing indirect dependencies and preventing cycles from forming in DAG.
- Parameters
- candidate: Variable
Node to remove from
self.dependencies
- sort_dependency_branches(reverse_branch_sorting=False)¶
Sort dependencies by DAG cost so that branches with higher cost (
Variable._dag_cost
) appear before shorter branches (“critical path” sorting). User can set flag to force branches with lower cost to appear before branches with hgiher cost (“low hanging fruit” sorting).- Parameters
- reverse_branch_sorting: bool = False
Flag to set sorting preference. Default is “critical path”. Reverse branch sorting to produce “low hanging fruit” sorting.
- class omtools.core.explicit_output.ExplicitOutput(*args, **kwargs)¶
Class for creating an explicit output
- define(expr: omtools.core.variable.Variable)¶
Define expression (in terms of
self
) that computes value for this output. This method defines a cyclic relationship, which requires an iterative solver to converge using OpenMDAO.- Parameters
- expr: Variable
The expression to compute iteratively until convergence
- initialize(name: str, shape: Tuple[int] = (1), val=1)¶
Initialize explicit output
- Parameters
- name: str
Name of variable to compute explicitly
- shape: Tuple[int]
Shape of variable to compute explicitly
- val: Number or ndarray
Initial value of variable to compute explicitly
- class omtools.core.implicit_output.ImplicitOutput(*args, **kwargs)¶
Class for creating an implicit output
- define_residual(residual_expr: omtools.core.variable.Variable)¶
Define the residual that must equal zero for this output to be computed
- Parameters
- residual_expr: Variable
Residual expression
- define_residual_bracketed(residual_expr: omtools.core.variable.Variable, x1=0.0, x2=1.0)¶
Define the residual that must equal zero for this output to be computed
- Parameters
- residual_expr: Variable
Residual expression
- initialize(group, name: str, shape: Tuple[int] = (1), val=1)¶
Initialize implicit output
- Parameters
- name: str
Name of variable to compute implicitly
- shape: Tuple[int]
Shape of variable to compute implicitly
- val: Number or ndarray
Initial value of variable to compute implicitly
- class omtools.core.indep.Indep(*args, **kwargs)¶
Container for creating a value that is constant during model evaluation; i.e. independent variable, or design variable
- dependencies: list¶
- dependents: list¶
- initialize(name, shape=(1), val=1, dv=False)¶
Initialize independent variable
- Parameters
- name: str
Name of independent variable (constant during model evaluation)
- shape: Tuple[int]
Shape of independent variable (constant during model evaluation)
- val: Number or ndarray
Value of independent variable (constant during model evaluation)
- dv: bool
Flag to set independent variable as a design variable, allowing the optimizer to modify the value
- class omtools.core.input.Input(*args, **kwargs)¶
Class for declaring an input variable
- dependencies: list¶
- dependents: list¶
- initialize(name: str, shape: Tuple[int] = (1), val=1)¶
Initialize subsystem input
- Parameters
- name: str
Name of variable computed by another
System
object- shape: Tuple[int]
Shape of variable computed by another
System
object- val: Number or ndarray
Default value of variable computed by another
System
object, takes effect if there is no connection to anotherSystem
object’s output
- class omtools.core.output.Output(*args, **kwargs)¶
Base class for outputs; used to prevent circular imports
- class omtools.core.subsystem.Subsystem(*args, **kwargs)¶
Class for declaring an input variable
- omtools.core.group._post_setup(func: Callable) → Callable¶
This function replaces
Group.setup
with a new method that callsGroup.setup
and performs the necessary steps to determine execution order and construct and add the appropriate subsystems.The new method is the core of the
omtools
package. This function analyzes the Directed Acyclic Graph (DAG), sorts expressions, and directs OpenMDAO to add the correspondingComponent
objects.This function ensures an execution order that is free of unnecessary feedback regardless of the order in which the user registers outputs.
- omtools.core.graph.remove_indirect_dependencies(node: omtools.core.variable.Variable)¶
Remove the dependencies that do not constrin execution order. That is, if C depends on B and A, and B depends on A, then the execution order must be A, B, C, even without the dependence of C on A.
- Parameters
- node: Variable
The node to treat as “root”. In
omtools.group
,Group._root
is treated as the “root” node.
- omtools.core.graph.topological_sort(node: omtools.core.variable.Variable) → List[omtools.core.variable.Variable]¶
Perform a topological sort on the Directed Acyclic Graph (DAG). If any cycles are detected when traversing the graph,
topological_sort
will not terminate, and it will cause a memory overflow.This version of a topological sort is modified so that a node will not be added to the sorted list until the node has been visited as many times as its in-degree; i.e. the number of dependents.
- Parameters
- node: Variable
The node to treat as “root”. In
omtools.group
,Group._root
is treated as the “root” node.
- Returns
- list[Variable]
List of
Variable
objects sorted from root to leaf. When overridingomtools.Group.setup
, the first node will beGroup._root
, and the last will be anInput
,ExplicitOutput
,ImplicitOutput
, orIndepVar
.