ImplicitFuncComp#

ImplicitFuncComp is a component that provides a shortcut for building an ImplicitComponent based on a python function. That function takes inputs and states as arguments and returns residual values. The function may take some inputs that are non-differentiable and are assumed to be static during the computation of derivatives. These static values may be of any hashable type. All other arguments and return values must be either floats or numpy arrays. The mapping between a state argument and its residual output must be specified in the metadata when the output (state) is added by setting ‘resid’ to the name of the residual.

It may seem confusing to use add_output to specify state variables since the state variables are actually input arguments to the function, but in OpenMDAO’s view of the world, states are outputs so we use add_output to specify them. Also, using the metadata to specify which input arguments are actually states gives more flexibility in terms of how the function arguments are ordered. For example, if it’s desirable for a function to be passable to scipy.optimize.newton, then the function’s arguments can be ordered with the states first, followed by the inputs, in order to match the order expected by scipy.optimize.newton.

The add_output function is part of the Function Metadata API. You use this API to specify various metadata that OpenMDAO needs in order to properly configure a fully functional implicit component. You should read and understand the Function Metadata API before you continue with this section.

ImplicitFuncComp Options#

OptionDefaultAcceptable ValuesAcceptable TypesDescription
always_optFalse[True, False]['bool']If True, force nonlinear operations on this component to be included in the optimization loop even if this component is not relevant to the design variables and responses.
assembled_jac_typecsc['csc', 'dense']N/ALinear solver(s) in this group or implicit component, if using an assembled jacobian, will use this type.
default_shape(1,)N/A['tuple']Default shape for variables that do not set val to a non-scalar value or set shape, shape_by_conn, copy_shape, or compute_shape. Default is (1,).
derivs_methodN/A['jax', 'cs', 'fd', None]N/AThe method to use for computing derivatives
distributedFalse[True, False]['bool']If True, set all variables in this component as distributed across multiple processes
run_root_onlyFalse[True, False]['bool']If True, call compute, compute_partials, linearize, apply_linear, apply_nonlinear, solve_linear, solve_nonlinear, and compute_jacvec_product only on rank 0 and broadcast the results to the other ranks.
use_jitTrue[True, False]['bool']If True, attempt to use jit on compute_primal, assuming jax or some other AD package capable of jitting is active.

ImplicitFuncComp Constructor#

The call signature for the ImplicitFuncComp constructor is:

ImplicitFuncComp.__init__(apply_nonlinear, solve_nonlinear=None, linearize=None, solve_linear=None, **kwargs)[source]

Initialize attributes.

ImplicitFuncComp Example: A simple implicit function component#

The simplest implicit function component requires the definition of a function that takes inputs and states as arguments and returns residual values. This function maps to the apply_nonlinear method in the OpenMDAO component API. Here’s an example:

import openmdao.api as om
import openmdao.func_api as omf

def apply_nl(a, b, c, x):  # inputs a, b, c and state x
    R_x = a * x ** 2 + b * x + c
    return R_x

f = (omf.wrap(apply_nl)
        .add_output('x', resid='R_x', val=0.0)
        .declare_partials(of='*', wrt='*', method='cs')
        )

p = om.Problem()
p.model.add_subsystem('comp', om.ImplicitFuncComp(f))

p.model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False, iprint=0)

# need this since comp is implicit and doesn't have a solve_linear
p.model.linear_solver = om.DirectSolver()

p.setup()

p.set_val('comp.a', 2.)
p.set_val('comp.b', -8.)
p.set_val('comp.c', 6.)
p.run_model()