# Building Models with Solvers and Implicit Components¶

This tutorial will show you how to define implicit components and build models with them. We’ll use a nonlinear circuit analysis example problem.

## Circuit analysis¶

Consider a simple electrical circuit made up from two resistors, a diode, and a constant current source. Our goal is to solve for the steady-state voltages at node 1 and node 2. In order to find the voltages, we’ll employ Kirchoff’s current law, and solve for the voltages needed at each node to drive the net current to 0.

This means that the voltages at each node are state variables for the analysis. In other words, V1 and V2 are defined implicitly by the following residual equation:

$${R_{node_j}} = \sum_k I_{k}^{in} - \sum_k I_{k}^{out} = 0 .$$

To build this model we’re going to define three different components:

1. Resistor (Explicit)

2. Diode (Explicit)

3. Node (Implicit)

ExplicitComponents - Resistor and Diode

The Resistor and Diode components will each compute their current, given the voltages on either side. These calculations are analytic functions, so we’ll inherit from ExplicitComponent. These components will each declare some options to allow you to pass in the relevant physical constants, and to allow you to give some reasonable default values.

import openmdao.api as om

class Resistor(om.ExplicitComponent):
"""Computes current across a resistor using Ohm's law."""

def initialize(self):
self.options.declare('R', default=1., desc='Resistance in Ohms')

def setup(self):

def setup_partials(self):
self.declare_partials('I', 'V_in', method='fd')
self.declare_partials('I', 'V_out', method='fd')

def compute(self, inputs, outputs):
deltaV = inputs['V_in'] - inputs['V_out']
outputs['I'] = deltaV / self.options['R']

class Diode(om.ExplicitComponent):
"""Computes current across a diode using the Shockley diode equation."""

def initialize(self):
self.options.declare('Is', default=1e-15, desc='Saturation current in Amps')
self.options.declare('Vt', default=.025875, desc='Thermal voltage in Volts')

def setup(self):

def setup_partials(self):
self.declare_partials('I', 'V_in', method='fd')
self.declare_partials('I', 'V_out', method='fd')

def compute(self, inputs, outputs):
deltaV = inputs['V_in'] - inputs['V_out']
Is = self.options['Is']
Vt = self.options['Vt']
outputs['I'] = Is * (np.exp(deltaV / Vt) - 1)


Important

Since we’ve provided default values for the options, they won’t be required arguments when instantiating Resistor or Diode. Check out the Features section for more details on how to use [component options] (../../features/core_features/working_with_components/options.ipynb).

## ImplicitComponent - Node¶

The Node component inherits from ImplicitComponent, which has a different interface than ExplicitComponent. Rather than compute the values of its outputs, it computes residuals via the apply_nonlinear method. When those residuals have been driven to zero, the values of the outputs will be implicitly known. apply_nonlinear computes the residuals using values from inputs and outputs. Notice that we still define V as an output of the Node component, albeit one that is implicitly defined.

class Node(om.ImplicitComponent):
"""Computes voltage residual across a node based on incoming and outgoing current."""

def initialize(self):
self.options.declare('n_in', default=1, types=int, desc='number of connections with + assumed in')
self.options.declare('n_out', default=1, types=int, desc='number of current connections + assumed out')

def setup(self):

for i in range(self.options['n_in']):
i_name = 'I_in:{}'.format(i)

for i in range(self.options['n_out']):
i_name = 'I_out:{}'.format(i)

def setup_partials(self):
#note: we don't declare any partials wrt V here,
#      because the residual doesn't directly depend on it
self.declare_partials('V', 'I*', method='fd')

def apply_nonlinear(self, inputs, outputs, residuals):
residuals['V'] = 0.
for i_conn in range(self.options['n_in']):
residuals['V'] += inputs['I_in:{}'.format(i_conn)]
for i_conn in range(self.options['n_out']):
residuals['V'] -= inputs['I_out:{}'.format(i_conn)]


Every state variable must have exactly one corresponding residual which is defined in the apply_nonlinear method. The residuals equations in an implicit component are not analogous to the outputs equations in the compute method of an explicit component. Instead of defining an explicit equation for the output, residuals['example_output'] defines an equation for the residual associated with the output (state variable) example_output. In our example, residuals['V'] defines the equation of the residual associated with the state variable V. There will be no explicit equation defining V, instead, the residual equation sums the currents associated with V so the sum can be driven to zero.

An implicit component varies its outputs (state variables, in this case V) to drive the residual equation to zero. In our model, V does not show up directly in the residual equation. Instead, our explicit components Resistor and Diode create a dependence of the currents on V, so by using a solver on a higher level of the model hierarchy, we can vary V to have an effect on current, and we can drive the residuals to zero.

All implicit components must define the apply_nonlinear method, but it is not a requirement that every ImplicitComponent define the solve_nonlinear method. (The solve_nonlinear method provides a way to explicitly define an output within an implicit component.) In fact, for the Node component, it is not even possible to define a solve_nonlinear because V does not show up directly in the residual function. So the implicit function represented by instances of the Node component must be converged at a higher level in the model hierarchy.

There are cases where it is possible, and even advantageous, to define the solve_nonlinear method. For example, when a component is performing an engineering analysis with its own specialized nonlinear solver routines (e.g. CFD or FEM), then it makes sense to expose those to OpenMDAO via solve_nonlinear so OpenMDAO can make use of them. Just remember that apply_nonlinear must be defined, regardless of whether you also define solve_nonlinear.

Important

In this case, the residual equation is not a direct function of the state variable V. Often, however, the residual might be a direct function of one or more output variables. If that is the case, you can access the values via outputs['V']. See the ImplicitComponent documentation for an example of this.

## Building the Circuit Group and Solving It with NewtonSolver¶

We can combine the Resistor, Diode, and Node into the circuit pictured above using a Group. Adding components and connecting their variables is the same as what you’ve seen before in the Sellar - Two Discipline tutorial. What is new here is the additional use of the nonlinear NewtonSolver and linear DirectSolver to converge the system.

In previous tutorials, we used a gradient-free NonlinearBlockGaussSeidel solver, but that won’t work here. Just above, we discussed that the Node class does not, and in fact can not, define its own solve_nonlinear method. Hence, there would be no calculations for the GaussSeidel solver to iterate on. Instead we use the Newton solver at the Circuit level, which uses Jacobian information to compute group level updates for all the variables simultaneously. The Newton solver’s use of that Jacobian information is why we need to declare a linear solver in this case.

Important

OpenMDAO provides a library of linear solvers that are useful in different advanced scenarios. For many problems, especially problems built from components with mostly scalar variables, the DirectSolver will be both the most efficient and the easiest to use. We recommend you stick with DirectSolver unless you have a good reason to switch.

import numpy as np

class Circuit(om.Group):

def setup(self):

self.connect('n1.V', ['R1.V_in', 'R2.V_in'])
self.connect('R1.I', 'n1.I_out:0')
self.connect('R2.I', 'n1.I_out:1')

self.connect('n2.V', ['R2.V_out', 'D1.V_in'])
self.connect('R2.I', 'n2.I_in:0')
self.connect('D1.I', 'n2.I_out:0')

self.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
self.nonlinear_solver.options['iprint'] = 2
self.nonlinear_solver.options['maxiter'] = 20
self.linear_solver = om.DirectSolver()

p = om.Problem()
model = p.model

model.connect('source.I', 'circuit.I_in')
model.connect('ground.V', 'circuit.Vg')

p.setup()

# set some initial guesses
p['circuit.n1.V'] = 10.
p['circuit.n2.V'] = 1.

p.run_model()

print(p['circuit.n1.V'])
print(p['circuit.n2.V'])
print(p['circuit.R1.I'])
print(p['circuit.R2.I'])
print(p['circuit.D1.I'])

# sanity check: should sum to .1 Amps
print(p['circuit.R1.I'] + p['circuit.D1.I'])

=======
circuit
=======
NL: Newton 0 ; 59.9046598 1
NL: Newton 1 ; 22.3887968 0.373740489
NL: Newton 2 ; 8.23629948 0.13749013
NL: Newton 3 ; 3.02978539 0.0505767899
NL: Newton 4 ; 1.11437824 0.0186025301
NL: Newton 5 ; 0.409725119 0.00683962016
NL: Newton 6 ; 0.150492273 0.00251219644
NL: Newton 7 ; 0.0551239641 0.000920194929
NL: Newton 8 ; 0.0200403261 0.000334537016
NL: Newton 9 ; 0.00713760492 0.000119149411
NL: Newton 10 ; 0.00240281896 4.01107188e-05
NL: Newton 11 ; 0.000692025597 1.15521163e-05
NL: Newton 12 ; 0.000129140479 2.15576685e-06
NL: Newton 13 ; 7.60315074e-06 1.26920857e-07
NL: Newton 14 ; 3.10658301e-08 5.18587873e-10
NL: Newton 15 ; 1.12044922e-12 1.87038742e-14
NL: Newton Converged
[9.90804735]
[0.71278185]
[0.09908047]
[0.00091953]
[0.00091953]
[0.1]


## Modifying Solver Settings in Your Run Script¶

In the above run script, we set some initial guess values: prob['n1.V']=10 and prob['n2.V']=1. If you try to play around with those initial guesses a bit, you will see that convergence is really sensitive to the initial guess you used for n2.V.Below we provide a second run script that uses the same Circuit group we defined previously, but which additionally modifies some solver settings and initial guesses. If we set the initial guess for prob['n2.V']=1e-3, then the model starts out with a massive residual. It also converges much more slowly, so although we gave it more than twice the number of iterations, it doesn’t even get close to a converged answer.

from openmdao.test_suite.scripts.circuit_analysis import Circuit

p = om.Problem()
model = p.model

model.connect('source.I', 'circuit.I_in')
model.connect('ground.V', 'circuit.Vg')

p.setup()

# you can change the NewtonSolver settings in circuit after setup is called
newton = p.model.circuit.nonlinear_solver
newton.options['maxiter'] = 50

# set some initial guesses
p['circuit.n1.V'] = 10.
p['circuit.n2.V'] = 1e-3

p.run_model()

print(p['circuit.n1.V'])
print(p['circuit.n2.V'])

# sanity check: should sum to .1 Amps
print(p['circuit.R1.I'] + p['circuit.D1.I'])

=======
circuit
=======
NL: Newton 0 ; 2.53337743 1
NL: Newton 1 ; 6.97216645e+152 2.75212306e+152
NL: Newton 2 ; 2.56496626e+152 1.01246906e+152
NL: Newton 3 ; 9.43616587e+151 3.72473748e+151
NL: Newton 4 ; 3.47143851e+151 1.37028082e+151
NL: Newton 5 ; 1.27709554e+151 5.04107884e+150
NL: Newton 6 ; 4.69826271e+150 1.8545451e+150
NL: Newton 7 ; 1.72842766e+150 6.822622e+149
NL: Newton 8 ; 6.35865288e+149 2.50995087e+149
NL: Newton 9 ; 2.33926287e+149 9.23377165e+148
NL: Newton 10 ; 8.60583345e+148 3.39698039e+148
NL: Newton 11 ; 3.16597037e+148 1.24970339e+148
NL: Newton 12 ; 1.16471792e+148 4.59749069e+147
NL: Newton 13 ; 4.28484056e+147 1.69135499e+147
NL: Newton 14 ; 1.57633521e+147 6.22226752e+146
NL: Newton 15 ; 5.79912522e+146 2.28908853e+146
NL: Newton 16 ; 2.13342017e+146 8.42124882e+145
NL: Newton 17 ; 7.84856585e+145 3.09806417e+145
NL: Newton 18 ; 2.88738181e+145 1.13973614e+145
NL: Newton 19 ; 1.06222893e+145 4.19293595e+144
NL: Newton 20 ; 3.90779737e+144 1.54252474e+144
NL: Newton 21 ; 1.43762609e+144 5.67474106e+143
NL: Newton 22 ; 5.28883302e+143 2.08766091e+143
NL: Newton 23 ; 1.94569053e+143 7.68022367e+142
NL: Newton 24 ; 7.15793376e+142 2.82545099e+142
NL: Newton 25 ; 2.63330755e+142 1.03944541e+142
NL: Newton 26 ; 9.6875843e+141 3.82397987e+141
NL: Newton 27 ; 3.56393196e+141 1.40679076e+141
NL: Newton 28 ; 1.31112263e+141 5.17539399e+140
NL: Newton 29 ; 4.82344381e+140 1.90395784e+140
NL: Newton 30 ; 1.7744801e+140 7.00440479e+139
NL: Newton 31 ; 6.52807361e+139 2.57682631e+139
NL: Newton 32 ; 2.40159048e+139 9.47979741e+138
NL: Newton 33 ; 8.83512836e+138 3.48748996e+138
NL: Newton 34 ; 3.25032488e+138 1.28300065e+138
NL: Newton 35 ; 1.19575081e+138 4.71998682e+137
NL: Newton 36 ; 4.39900639e+137 1.73641967e+137

NL: Newton 37 ; 1.61833528e+137 6.3880544e+136
NL: Newton 38 ; 5.95363784e+136 2.35007929e+136
NL: Newton 39 ; 2.19026328e+136 8.64562563e+135
NL: Newton 40 ; 8.05768403e+135 3.18060938e+135
NL: Newton 41 ; 2.96431357e+135 1.17010341e+135
NL: Newton 42 ; 1.09053109e+135 4.30465307e+134
NL: Newton 43 ; 4.01191721e+134 1.58362397e+134
NL: Newton 44 ; 1.47593038e+134 5.82593957e+133
NL: Newton 45 ; 5.42974936e+133 2.14328481e+133
NL: Newton 46 ; 1.99753175e+133 7.88485652e+132
NL: Newton 47 ; 7.34865064e+132 2.90073266e+132
NL: Newton 48 ; 2.70346973e+132 1.06714053e+132
NL: Newton 49 ; 9.9457015e+131 3.92586647e+131
NL: Newton 50 ; 3.65888981e+131 1.44427347e+131
NL: NewtonSolver 'NL: Newton' on system 'circuit' failed to converge in 50 iterations.
[9.98744708]
[8.73215484]
[0.09987447]


Important

You actually can get this model to converge. But you have to set the options for maxiter=400 and rtol=1e-100. (The rtol value needs to be very low to prevent premature termination.)

## Tweaking Newton Solver Settings to Get More Robust Convergence¶

The NewtonSolver has a lot of features that allow you to modify its behavior to handle more challenging problems. We’re going to look at two of the most important ones here:

1. Line searches

2. The solve_subsystems option

If we use both of these in combination, we can dramatically improve the robustness of the solver for this problem. The linesearch attribute makes sure that the solver doesn’t take too big of a step. The solve_subsystems option allows the Resistor and Diode components (the two ExplicitComponents) to help the convergence by updating their own output values given their inputs. When you use NewtonSolver on models with a lot of ExplicitComponents, you may find that turning on solve_subsystems helps convergence, but you need to be careful about the execution order when you try this.

Important

For this case, we used the ArmijoGoldsteinLS, which basically limits step sizes so that the residual always goes down. For many problems you might want to use BoundsEnforceLS instead, which only activates the line search to enforce upper and lower bounds on the outputs in the model.

p = om.Problem()
model = p.model

model.connect('source.I', 'circuit.I_in')
model.connect('ground.V', 'circuit.Vg')

p.setup()

# you can change the NewtonSolver settings in circuit after setup is called
newton = p.model.circuit.nonlinear_solver
newton.options['iprint'] = 2
newton.options['maxiter'] = 10
newton.options['solve_subsystems'] = True
newton.linesearch = om.ArmijoGoldsteinLS()
newton.linesearch.options['maxiter'] = 10
newton.linesearch.options['iprint'] = 2

# set some initial guesses
p['circuit.n1.V'] = 10.
p['circuit.n2.V'] = 1e-3

p.run_model()

print(p['circuit.n1.V'])
print(p['circuit.n2.V'])
print(p['circuit.R1.I'])
print(p['circuit.R2.I'])
print(p['circuit.D1.I'])

# sanity check: should sum to .1 Amps
print(p['circuit.R1.I'] + p['circuit.D1.I'])

=======
circuit
=======
NL: Newton 0 ; 0.00141407214 1

|  LS: AG 1 ; 6.97072428e+152 1
|  LS: AG 2 ; 8.51199023e+68 0.5
|  LS: AG 3 ; 9.40605951e+26 0.25
|  LS: AG 4 ; 988771.693 0.125
|  LS: AG 5 ; 0.00130322117 0.0625
NL: Newton 1 ; 0.00130322117 0.921608691
|  LS: AG 1 ; 5578243.07 1
|  LS: AG 2 ; 13.3717913 0.5
|  LS: AG 3 ; 0.0197991803 0.25
|  LS: AG 4 ; 0.000828009765 0.125
NL: Newton 2 ; 0.000828009765 0.585549875
NL: Newton 3 ; 7.03445167e-06 0.00497460594
NL: Newton 4 ; 2.66239247e-08 1.88278405e-05
NL: Newton 5 ; 8.96307984e-13 6.33848839e-10
NL: Newton Converged
[9.90804735]
[0.71278185]
[0.09908047]
[0.00091953]
[0.00091953]
[0.1]


Important

This tutorial used finite difference to approximate the partial derivatives for all the components. Check out this example if you want to see the same problem solved with analytic derivatives.