exec_comp.py

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

class openmdao.components.exec_comp.ExecComp(exprs, inits=None, **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 or inits. 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.

inits: dict, optional

A mapping of names to initial values, primarily for variables with names that are not valid python names, e.g., a:b:c.

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

If a variable has an initial value that is anything other than 0.0, either because it has a different type than float or just because its initial value is nonzero, you must use a keyword arg or the ‘inits’ dict to set the initial value. 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 the default initial value of 0.0, while ‘x’ would be initialized with a size 10 float array of ones.

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)