exec_comp.py

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

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

units : dict, optional

A mapping of variable names to their units.

**kwargs : dict of named args

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

Options:

deriv_options[‘type’] : str(‘user’)

Derivative calculation type (‘user’, ‘fd’, ‘cs’) Default is ‘user’, where derivative is calculated from user-supplied derivatives. Set to ‘fd’ to finite difference this system. Set to ‘cs’ to perform the complex step if your components support it.

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

Finite difference mode. (forward, backward, central)

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

Default finite difference stepsize

deriv_options[‘step_calc’] : str(‘absolute’)

Set to absolute, relative

deriv_options[‘check_type’] : str(‘fd’)

Type of derivative check for check_partial_derivatives. Set to ‘fd’ to finite difference this system. Set to ‘cs’ to perform the complex step method if your components support it.

deriv_options[‘check_form’] : str(‘forward’)

Finite difference mode: (“forward”, “backward”, “central”) During check_partial_derivatives, the difference form that is used for the check.

deriv_options[‘check_step_calc’] : str(‘absolute’,)

Set to ‘absolute’ or ‘relative’. Default finite difference step calculation for the finite difference check in check_partial_derivatives.

deriv_options[‘check_step_size’] : float(1e-06)

Default finite difference stepsize for the finite difference check in check_partial_derivatives”

deriv_options[‘linearize’] : bool(False)

Set to True if you want linearize to be called even though you are using FD.

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)