explicitcomponent.py

explicitcomponent.py#

Define the ExplicitComponent class.

class openmdao.core.explicitcomponent.ExplicitComponent(**kwargs)[source]

Bases: Component

Class to inherit from when all output variables are explicit.

Parameters:
**kwargsdict of keyword arguments

Keyword arguments that will be mapped into the Component options.

Attributes:
_has_compute_partialsbool

If True, the instance overrides compute_partials.

_vjp_hashint or None

Hash value for the last set of inputs to the compute_primal function.

_vjp_funfunction or None

The vector-Jacobian product function.

Methods

abs_meta_iter(iotype[, local, cont, discrete])

Iterate over absolute variable names and their metadata for this System.

add_constraint(name[, lower, upper, equals, ...])

Add a constraint variable to this system.

add_design_var(name[, lower, upper, ref, ...])

Add a design variable to this system.

add_discrete_input(name, val[, desc, tags, ...])

Add a discrete input variable to the component.

add_discrete_output(name, val[, desc, tags, ...])

Add an output variable to the component.

add_input(name[, val, shape, units, desc, ...])

Add an input variable to the component.

add_objective(name[, ref, ref0, index, ...])

Add a response variable to this system.

add_output(name[, val, shape, units, ...])

Add an output variable to the component.

add_recorder(recorder[, recurse])

Add a recorder to the system.

add_response(name, type_[, lower, upper, ...])

Add a response variable to this system.

best_partial_deriv_direction()

Return the best direction for partial deriv calculations based on input and output sizes.

check_config(logger)

Perform optional error checks.

check_partials([out_stream, compact_print, ...])

Check partial derivatives comprehensively for this component.

check_sparsity([method, max_nz, out_stream])

Check the sparsity of the computed jacobian against the declared sparsity.

cleanup()

Clean up resources prior to exit.

comm_info_iter()

Yield comm size for this system and all subsystems.

compute(inputs, outputs[, discrete_inputs, ...])

Compute outputs given inputs.

compute_fd_jac(jac[, method])

Force the use of finite difference to compute a jacobian.

compute_fd_sparsity([method, num_full_jacs, ...])

Use finite difference to compute a sparsity matrix.

compute_jacvec_product(inputs, d_inputs, ...)

Compute jac-vector product.

compute_partials(inputs, partials[, ...])

Compute sub-jacobian parts.

compute_sparsity([direction, num_iters, ...])

Compute the sparsity of the partial jacobian.

convert2units(name, val, units)

Convert the given value to the specified units.

convert_from_units(name, val, units)

Convert the given value from the specified units to those of the named variable.

convert_units(name, val, units_from, units_to)

Wrap the utility convert_units and give a good error message.

declare_coloring([wrt, method, form, step, ...])

Set options for deriv coloring of a set of wrt vars matching the given pattern(s).

declare_partials(of, wrt[, dependent, rows, ...])

Declare information about this component's subjacobians.

dist_size_iter(io, top_comm)

Yield names and distributed ranges of all local and remote variables in this system.

get_coloring_fname(mode)

Return the full pathname to a coloring file.

get_conn_graph()

Return the model connection graph.

get_constraints([recurse, get_sizes, ...])

Get the Constraint settings from this system.

get_declare_partials_calls([sparsity])

Return a string containing declare_partials() calls based on the subjac sparsity.

get_design_vars([recurse, get_sizes, ...])

Get the DesignVariable settings from this system.

get_io_metadata([iotypes, metadata_keys, ...])

Retrieve metadata for a filtered list of variables.

get_linear_vectors()

Return the linear inputs, outputs, and residuals vectors.

get_nonlinear_vectors()

Return the inputs, outputs, and residuals vectors.

get_objectives([recurse, get_sizes, ...])

Get the Objective settings from this system.

get_outputs_dir(*subdirs[, mkdir])

Get the path under which all output files of this system are to be placed.

get_promotions([inprom, outprom])

Return all promotions for the given promoted variable(s).

get_reports_dir()

Get the path to the directory where the report files should go.

get_responses([recurse, get_sizes, use_prom_ivc])

Get the response variable settings from this system.

get_self_statics()

Override this in derived classes if compute_primal references static values.

get_source(name)

Return the source variable connected to the given named variable.

get_val(name[, units, indices, get_remote, ...])

Get an output/input/residual variable.

get_var_dup_info(name, io)

Return information about how the given variable is duplicated across MPI processes.

get_var_sizes(name, io)

Return the sizes of the given variable on all procs.

has_vectors()

Check if the system vectors have been initialized.

initialize()

Perform any one-time initialization run at instantiation.

is_explicit([is_comp])

Return True if this is an explicit component.

list_inputs([val, prom_name, units, shape, ...])

Write a list of input names and other optional information to a specified stream.

list_options([include_default, ...])

Write a list of output names and other optional information to a specified stream.

list_outputs([explicit, implicit, val, ...])

Write a list of output names and other optional information to a specified stream.

list_vars([val, prom_name, residuals, ...])

Write a list of inputs and outputs sorted by component in execution order.

load_case(case)

Pull all input and output variables from a Case into this System.

load_model_options()

Load the relevant model options from Problem._metadata['model_options'].

override_method(name, method)

Dynamically add a method to this component instance.

record_iteration()

Record an iteration of the current System.

run_apply_linear(mode[, scope_out, scope_in])

Compute jac-vec product.

run_apply_nonlinear()

Compute residuals.

run_linearize([sub_do_ln])

Compute jacobian / factorization.

run_solve_linear(mode)

Apply inverse jac product.

run_solve_nonlinear()

Compute outputs.

run_validation()

Run validate method on all systems below this system.

set_check_partial_options(wrt[, method, ...])

Set options that will be used for checking partial derivatives.

set_constraint_options(name[, ref, ref0, ...])

Set options for constraints in the model.

set_design_var_options(name[, lower, upper, ...])

Set options for design vars in the model.

set_objective_options(name[, ref, ref0, ...])

Set options for objectives in the model.

set_output_solver_options(name[, lower, ...])

Set solver output options.

set_solver_print([level, depth, type_, ...])

Control printing for solvers and subsolvers in the model.

set_val(name, val[, units, indices])

Set an input or output variable.

setup()

Declare inputs and outputs.

setup_partials()

Declare partials.

sparsity_matches_fd([direction, outstream])

Compare the sparsity computed by this system vs.

subjac_sparsity_iter(sparsity[, wrt_matches])

Iterate over sparsity for each subjac in the jacobian.

system_iter([include_self, recurse, typ, ...])

Yield a generator of local subsystems of this system.

total_local_size(io)

Return the total local size of the given variable.

use_fixed_coloring([coloring, recurse])

Use a precomputed coloring for this System.

uses_approx()

Return True if the system uses approximations to compute derivatives.

validate(inputs, outputs[, discrete_inputs, ...])

Check any final input / output values after a run.

__init__(**kwargs)[source]

Store some bound methods so we can detect runtime overrides.

add_output(name, val=1.0, shape=None, units=None, res_units=None, desc='', lower=None, upper=None, ref=1.0, ref0=0.0, res_ref=None, tags=None, shape_by_conn=False, copy_shape=None, compute_shape=None, units_by_conn=False, compute_units=None, copy_units=None, distributed=None, primal_name=None)[source]

Add an output variable to the component.

For ExplicitComponent, res_ref defaults to the value in res unless otherwise specified.

Parameters:
namestr

Name of the variable in this component’s namespace.

valfloat or list or tuple or ndarray

The initial value of the variable being added in user-defined units. Default is 1.0.

shapeint or tuple or list or None

Shape of this variable, only required if val is not an array. Default is None.

unitsstr or None

Units in which the output variables will be provided to the component during execution. Default is None, which means it has no units.

res_unitsstr or None

Units in which the residuals of this output will be given to the user when requested. Default is None, which means it has no units.

descstr

Description of the variable.

lowerfloat or list or tuple or ndarray or None

Lower bound(s) in user-defined units. It can be (1) a float, (2) an array_like consistent with the shape arg (if given), or (3) an array_like matching the shape of val, if val is array_like. A value of None means this output has no lower bound. Default is None.

upperfloat or list or tuple or ndarray or None

Upper bound(s) in user-defined units. It can be (1) a float, (2) an array_like consistent with the shape arg (if given), or (3) an array_like matching the shape of val, if val is array_like. A value of None means this output has no upper bound. Default is None.

reffloat

Scaling parameter. The value in the user-defined units of this output variable when the scaled value is 1. Default is 1.

ref0float

Scaling parameter. The value in the user-defined units of this output variable when the scaled value is 0. Default is 0.

res_reffloat

Scaling parameter. The value in the user-defined res_units of this output’s residual when the scaled value is 1. Default is None, which means residual scaling matches output scaling.

tagsstr or list of strs

User defined tags that can be used to filter what gets listed when calling list_inputs and list_outputs and also when listing results from case recorders.

shape_by_connbool

If True, shape this output to match its connected input(s).

copy_shapestr or None

If a str, that str is the name of a variable. Shape this output to match that of the named variable.

compute_shapefunction or None

If a function, that function is called to determine the shape of this output.

units_by_connbool

If True, units are computed by the connected input(s).

compute_unitsfunction or None

If a function, that function is called to determine the units of this output.

copy_unitsstr or None

If a str, that str is the name of a variable. Units this output to match that of the named variable.

distributedbool

If True, this variable is a distributed variable, so it can have different sizes/values across MPI processes.

primal_namestr or None

Valid python name to represent the variable in compute_primal if ‘name’ is not a valid python name.

Returns:
dict

Metadata for added variable.

compute(inputs, outputs, discrete_inputs=None, discrete_outputs=None)[source]

Compute outputs given inputs. The model is assumed to be in an unscaled state.

An inherited component may choose to either override this function or to define a compute_primal function.

Parameters:
inputsVector

Unscaled, dimensional input variables read via inputs[key].

outputsVector

Unscaled, dimensional output variables read via outputs[key].

discrete_inputsdict-like or None

If not None, dict-like object containing discrete input values.

discrete_outputsdict-like or None

If not None, dict-like object containing discrete output values.

compute_fd_sparsity(method='fd', num_full_jacs=2, perturb_size=1e-09)[source]

Use finite difference to compute a sparsity matrix.

Parameters:
methodstr

The type of finite difference to perform. Valid options are ‘fd’ for forward difference, or ‘cs’ for complex step.

num_full_jacsint

Number of times to repeat jacobian computation using random perturbations.

perturb_sizefloat

Size of the random perturbation.

Returns:
coo_matrix

The sparsity matrix.

compute_jacvec_product(inputs, d_inputs, d_outputs, mode, discrete_inputs=None)[source]

Compute jac-vector product. The model is assumed to be in an unscaled state.

If mode is:

‘fwd’: d_inputs |-> d_outputs

‘rev’: d_outputs |-> d_inputs

Parameters:
inputsVector

Unscaled, dimensional input variables read via inputs[key].

d_inputsVector

See inputs; product must be computed only if var_name in d_inputs.

d_outputsVector

See outputs; product must be computed only if var_name in d_outputs.

modestr

Either ‘fwd’ or ‘rev’.

discrete_inputsdict or None

If not None, dict containing discrete input values.

compute_partials(inputs, partials, discrete_inputs=None)[source]

Compute sub-jacobian parts. The model is assumed to be in an unscaled state.

Parameters:
inputsVector

Unscaled, dimensional input variables read via inputs[key].

partialsJacobian

Sub-jac components written to partials[output_name, input_name]..

discrete_inputsdict or None

If not None, dict containing discrete input values.

is_explicit(is_comp=True)[source]

Return True if this is an explicit component.

Parameters:
is_compbool

If True, return True if this is an explicit component. If False, return True if this is an explicit component or group.

Returns:
bool

True if this is an explicit component.

property linear_solver

Get the linear solver for this system.

property nonlinear_solver

Get the nonlinear solver for this system.

override_method(name, method)[source]

Dynamically add a method to this component instance.

This allows users to create an ExplicitComponent that has a compute_partials or compute_jacvec_product that isn’t defined statically, but instead is dynamically created during setup. The motivating use case is the omjlcomps library, where the compute_partials or compute_jacvec_product methods are implemented in the Julia programming language (see omjlcomps.JuliaExplicitComp in byuflowlab/OpenMDAO.jl).

Parameters:
namestr

The name of the method to add. Must be either ‘compute_partials’ or ‘compute_jacvec_product’.

methodfunction

The function to add as a method. Will be converted to a MethodType if necessary.

Raises:
ValueError

If name is not ‘compute_partials’ or ‘compute_jacvec_product’.