exec_comp.py

Class definition for ExecComp, a component that evaluates an expression.

class openmdao.components.exec_comp.ExecComp(exprs, **kwargs)[source]

Bases: openmdao.core.component.Component

Given a list of assignment statements, this component creates input and output variables at construction time. All variables appearing on the left-hand side of an assignment are outputs, and the rest are inputs. Each variable is assumed to be of type float unless the initial value for that variable is supplied in **kwargs. Derivatives are calculated using complex step.

Args:

exprs: str or list of str

An assignment statement or iter of them. These express how the outputs are calculated based on the inputs.

**kwargs: dict of named args

Initial values of variables can be set by setting a named arg with the var name.

Options:

fd_options[‘force_fd’] : bool(False)

Set to True to finite difference this system.

fd_options[‘form’] : str(‘forward’)

Finite difference mode. (forward, backward, central) You can also set to ‘complex_step’ to peform the complex step method if your components support it.

fd_options[‘step_size’] : float(1e-06)

Default finite difference stepsize

fd_options[‘step_type’] : str(‘absolute’)

Set to absolute, relative

Notes

In order to create an ExecComp with array variables, or any other variable type that is not a float, you must use a keyword arg to set the initial value of each non-float variable. For example, let’s say we have an ExecComp that takes an array ‘x’ as input and outputs a float variable ‘y’ which is the sum of the entries in ‘x’.

>>> import numpy
>>> from openmdao.api import ExecComp
>>> excomp = ExecComp('y=numpy.sum(x)', x=numpy.ones(10,dtype=float))

In this example, ‘y’ would be assumed to be the default type of float and would be given an initial value of 0.0, while ‘x’ would be initialized with a size 100 float array.

linearize(params, unknowns, resids)[source]

Uses complex step method to calculate a Jacobian dict.

Args:

params : VecWrapper

VecWrapper containing parameters. (p)

unknowns : VecWrapper

VecWrapper containing outputs and states. (u)

resids : VecWrapper

VecWrapper containing residuals. (r)

Returns:

dict

Dictionary whose keys are tuples of the form (‘unknown’, ‘param’) and whose values are ndarrays.

solve_nonlinear(params, unknowns, resids)[source]

Executes this component’s assignment statemens.

Args:

params : VecWrapper, optional

VecWrapper containing parameters. (p)

unknowns : VecWrapper, optional

VecWrapper containing outputs and states. (u)

resids : VecWrapper, optional

VecWrapper containing residuals. (r)