component.py

Defines the base class for a Component in OpenMDAO.

class openmdao.core.component.Component[source]

Bases: openmdao.core.system.System

Base class for a Component system. The Component can declare variables and operates on its params to produce unknowns, which can be explicit outputs or implicit states.

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.

add_output(name, val=<object object>, **kwargs)[source]

Add an output to this component.

Args:

name : string

Name of the variable output.

val : float or ndarray or object

Initial value for the output. While the value is overwritten during execution, it is useful for infering size.

add_param(name, val=<object object>, **kwargs)[source]

Add a param input to this component.

Args:

name : string

Name of the input.

val : float or ndarray or object

Initial value for the input.

add_state(name, val=<object object>, **kwargs)[source]

Add an implicit state to this component.

Args:

name : string

Name of the state.

val : float or ndarray

Initial value for the state.

alloc_jacobian()[source]

Creates a jacobian dictionary with the keys pre-populated and correct array sizes allocated. caches the result in the component, and returns that cache if it finds it.

Returns:

dict

pre-allocated jacobian dictionary

apply_linear(params, unknowns, dparams, dunknowns, dresids, mode)[source]

Multiplies incoming vector by the Jacobian (fwd mode) or the transpose Jacobian (rev mode). If the user doesn’t provide this method, then we just multiply by the cached jacobian.

Args:

params : VecWrapper

VecWrapper containing parameters. (p)

unknowns : VecWrapper

VecWrapper containing outputs and states. (u)

dparams : VecWrapper

VecWrapper containing either the incoming vector in forward mode or the outgoing result in reverse mode. (dp)

dunknowns : VecWrapper

In forward mode, this VecWrapper contains the incoming vector for the states. In reverse mode, it contains the outgoing vector for the states. (du)

dresids : VecWrapper

VecWrapper containing either the outgoing result in forward mode or the incoming vector in reverse mode. (dr)

mode : string

Derivative mode, can be ‘fwd’ or ‘rev’.

apply_nonlinear(params, unknowns, resids)[source]

Evaluates the residuals for this component. For explicit components, the residual is the output produced by the current params minus the previously calculated output. Thus, an explicit component must execute its solve nonlinear method. Implicit components should override this and calculate their residuals in place.

Args:

params : VecWrapper

VecWrapper containing parameters. (p)

unknowns : VecWrapper

VecWrapper containing outputs and states. (u)

resids : VecWrapper

VecWrapper containing residuals. (r)

complex_step_jacobian(params, unknowns, resids, total_derivs=False, fd_params=None, fd_states=None, fd_unknowns=None, poi_indices=None, qoi_indices=None, use_check=False, option_overrides=None)[source]

Return derivatives of all unknowns in this system w.r.t. all incoming params using complex step.

Args:

params : VecWrapper

VecWrapper containing parameters. (p)

unknowns : VecWrapper

VecWrapper containing outputs and states. (u)

resids : VecWrapper

VecWrapper containing residuals. (r)

total_derivs : bool, optional

Should always be False, as componentwise derivatives only need partials.

fd_params : list of strings, optional

List of parameter name strings with respect to which derivatives are desired. This is used by problem to limit the derivatives that are taken.

fd_unknowns : list of strings, optional

List of output or state name strings for derivatives to be calculated. This is used by problem to limit the derivatives that are taken.

fd_states : list of strings, optional

List of state name strings for derivatives to be taken with respect to. This is used by problem to limit the derivatives that are taken.

poi_indices: dict of list of integers, optional

Should be an empty list, as there is no subcomponent relevance reduction.

qoi_indices: dict of list of integers, optional

Should be an empty list, as there is no subcomponent relevance reduction.

use_check: bool

Set to True to use check_step_size, check_type, and check_form

option_overrides: dict

Dictionary of options that override the default values. The ‘check_form’, ‘check_step_size’, ‘check_step_calc’, and ‘check_type’ options are available. This is used by check_partial_derivatives.

Returns:

dict

Dictionary whose keys are tuples of the form (‘unknown’, ‘param’) and whose values are ndarrays containing the derivative for that tuple pair.

components(local=False, recurse=False, include_self=False)[source]
Returns:

iterator

Iterator over sub-Components.

dump(nest=0, out_stream=<open file '<stdout>', mode 'w'>, verbose=False, dvecs=False, sizes=False)[source]

Writes a formated dump of this Component to file.

Args:

nest : int, optional

Starting nesting level. Defaults to 0.

out_stream : an open file, optional

Where output is written. Defaults to sys.stdout.

verbose : bool, optional

If True, output additional info beyond just the tree structure. Default is False.

dvecs : bool, optional

If True, show contents of du and dp vectors instead of u and p (the default).

sizes : bool, optional

If True, show sizes of vectors and comms. Default is False.

linearize(params, unknowns, resids)[source]

Returns Jacobian. Returns None unless component overides this method and returns something. J should be a dictionary whose keys are tuples of the form (‘unknown’, ‘param’) and whose values are ndarrays.

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.

set_var_indices(name, val=<object object>, shape=None, src_indices=None)[source]

Sets the ‘src_indices’ metadata of an existing variable on this component, as well as its value, size, shape, and global size.

This only works for numpy array variables.

Args:

name : string

Name of the variable.

val : ndarray, optional

Initial value for the variable.

shape : tuple, optional

Specifies the shape of the ndarray value

src_indices : array of indices

An index array indicating which entries in the distributed version of this variable are present in this process.

setup_distrib()[source]

Override this in your Component to set specific indices that will be pulled from source variables to fill your parameters. This method should set the ‘src_indices’ metadata for any parameters or unknowns that require it.

solve_linear(dumat, drmat, vois, mode=None)[source]

Single linear solution applied to whatever input is sitting in the rhs vector.

Args:

dumat : dict of VecWrappers

In forward mode, each VecWrapper contains the incoming vector for the states. There is one vector per quantity of interest for this problem. In reverse mode, it contains the outgoing vector for the states. (du)

drmat : dict of VecWrappers

VecWrapper containing either the outgoing result in forward mode or the incoming vector in reverse mode. There is one vector per quantity of interest for this problem. (dr)

vois : list of strings

List of all quantities of interest to key into the mats.

mode : string

Derivative mode, can be ‘fwd’ or ‘rev’, but generally should be called without mode so that the user can set the mode in this system’s ln_solver.options.

solve_nonlinear(params, unknowns, resids)[source]

Runs the component. The user is required to define this function in all components.

Args:

params : VecWrapper, optional

VecWrapper containing parameters. (p)

unknowns : VecWrapper, optional

VecWrapper containing outputs and states. (u)

resids : VecWrapper, optional

VecWrapper containing residuals. (r)