Using Solver Options

All solvers (both nonlinear and linear) have a number of options that you access via the options attribute that control its behavior:

Option DefaultAcceptable Values Acceptable Types Description
atol 1e-10N/A N/A absolute error tolerance
err_on_non_converge 0 [True, False] ['bool'] When True, AnalysisError will be raised if we don't converge.
iprint 1 N/A ['int'] whether to print output
maxiter 10 N/A ['int'] maximum number of iterations
rtol 1e-10N/A N/A relative error tolerance

Iteration Limit and Convergence Tolerances

Here is how you would change the iteration limit and convergence tolerances for NonlinearBlockGS.

import openmdao.api as om
from openmdao.test_suite.components.sellar import SellarDis1withDerivatives, SellarDis2withDerivatives
import numpy as np

prob = om.Problem()
model = prob.model

model.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2'])
model.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2'])

model.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                           z=np.array([0.0, 0.0]), x=0.0),
                    promotes=['obj', 'x', 'z', 'y1', 'y2'])

model.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1'), promotes=['con1', 'y1'])
model.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0'), promotes=['con2', 'y2'])

nlbgs = model.nonlinear_solver = om.NonlinearBlockGS()

nlbgs.options['maxiter'] = 20
nlbgs.options['atol'] = 1e-6
nlbgs.options['rtol'] = 1e-6

prob.setup()
prob.set_val('x', 1.)
prob.set_val('z', np.array([5.0, 2.0]))
prob.run_model()

print(prob.get_val('y1'))
print(prob.get_val('y2'))
NL: NLBGS Converged in 5 iterations
[25.5883027]
[12.05848818]

Displaying Solver Convergence Info

Solvers can all print out some information about their convergence history. If you want to control that printing behavior you can use the iprint option in the solver.

iprint = -1: Print nothing

from openmdao.test_suite.components.sellar import SellarDerivatives

prob = om.Problem()
prob.model = SellarDerivatives()

prob.setup()

newton = prob.model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
scipy = prob.model.linear_solver = om.ScipyKrylov()

newton.options['maxiter'] = 2

# use a real bad initial guess
prob.set_val('y1', 10000)
prob.set_val('y2', -26)

newton.options['iprint'] = -1
scipy.options['iprint'] = -1
prob.run_model()

iprint = 0: Print only errors or convergence failures.

from openmdao.test_suite.components.sellar import SellarDerivatives

prob = om.Problem()
prob.model = SellarDerivatives()

prob.setup()

newton = prob.model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
scipy = prob.model.linear_solver = om.ScipyKrylov()

newton.options['maxiter'] = 1

prob.set_val('y1', 10000)
prob.set_val('y2', -26)

newton.options['iprint'] = 0
scipy.options['iprint'] = 0

prob.run_model()
NL: NewtonSolver 'NL: Newton' on system '' failed to converge in 1 iterations.

iprint = 1: Print a convergence summary, as well as errors and convergence failures.

from openmdao.test_suite.components.sellar import SellarDerivatives

prob = om.Problem()
prob.model = SellarDerivatives()

prob.setup()

newton = prob.model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
scipy = prob.model.linear_solver = om.ScipyKrylov()

newton.options['maxiter'] = 20

prob.set_val('y1', 10000)
prob.set_val('y2', -26)

newton.options['iprint'] = 1
scipy.options['iprint'] = 0
prob.run_model()
NL: Newton Converged in 2 iterations

iprint = 2: Print the residual for every solver iteration.

from openmdao.test_suite.components.sellar import SellarDerivatives

prob = om.Problem()
prob.model = SellarDerivatives()

prob.setup()

newton = prob.model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
scipy = prob.model.linear_solver = om.ScipyKrylov()

newton.options['maxiter'] = 20

prob.set_val('y1', 10000)
prob.set_val('y2', -26)

newton.options['iprint'] = 2
scipy.options['iprint'] = 1
prob.run_model()
NL: Newton 0 ; 1.95729619e+11 1
NL: Newton 1 ; 1.60660573e+13 82.0829129
NL: Newton 2 ; 0.118870379 6.07319319e-13
NL: Newton Converged

Controlling Solver Output in Large Models

When you have a large model with multiple solvers, it is easier to use a shortcut method that recurses over the entire model. The set_solver_print method on problem can be used to set the iprint to one of four specific values for all solvers in the model while specifically controlling depth (how many systems deep) and the solver type (linear, nonlinear, or both.)

To print everything, just call set_solver_print with a level of “2”.

import numpy as np
from openmdao.test_suite.components.double_sellar import SubSellar

prob = om.Problem()
model = prob.model

sub1 = model.add_subsystem('sub1', om.Group(), promotes_inputs=['z'])
sub2 = sub1.add_subsystem('sub2', om.Group(), promotes_inputs=['z'])
g1 = sub2.add_subsystem('g1', SubSellar(), promotes_inputs=['z'])
g2 = model.add_subsystem('g2', SubSellar())

model.connect('sub1.sub2.g1.y2', 'g2.x')
model.connect('g2.y2', 'sub1.sub2.g1.x')

model.nonlinear_solver = om.NewtonSolver()
model.linear_solver = om.ScipyKrylov()
model.nonlinear_solver.options['solve_subsystems'] = True
model.nonlinear_solver.options['max_sub_solves'] = 0

g1.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g1.linear_solver = om.LinearBlockGS()

g2.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g2.linear_solver = om.ScipyKrylov()
g2.linear_solver.precon = om.LinearBlockGS()
g2.linear_solver.precon.options['maxiter'] = 2

prob.set_solver_print(level=2)

prob.setup()
prob.set_val('z', np.array([5.0, 2.0]))
prob.run_model()
+  
+  ============
+  sub1.sub2.g1
+  ============
+  NL: Newton 0 ; 27.6990975 1
+  |  
+  |  ============
+  |  sub1.sub2.g1
+  |  ============
+  |  LN: LNBGS 0 ; 27.6990975 1
+  |  LN: LNBGS 1 ; 4.08 0.147297218
+  |  LN: LNBGS 2 ; 0.408 0.0147297218
+  |  LN: LNBGS 3 ; 0.0408 0.00147297218
+  |  LN: LNBGS 4 ; 0.00408 0.000147297218
+  |  LN: LNBGS 5 ; 0.000408 1.47297218e-05
+  |  LN: LNBGS 6 ; 4.08e-05 1.47297218e-06
+  |  LN: LNBGS 7 ; 4.08e-06 1.47297218e-07
+  |  LN: LNBGS 8 ; 4.07999998e-07 1.47297217e-08
+  |  LN: LNBGS 9 ; 4.07999998e-08 1.47297217e-09
+  |  LN: LNBGS 10 ; 4.08000034e-09 1.4729723e-10
+  |  LN: LNBGSSolver 'LN: LNBGS' on system 'sub1.sub2.g1' failed to converge in 10 iterations.
+  |  LS: BCHK 0 ; 7.63720546 0.275720372
+  NL: Newton 1 ; 7.63720546 0.275720372
+  |  
+  |  ============
+  |  sub1.sub2.g1
+  |  ============
+  |  LN: LNBGS 0 ; 35.8626295 1
+  |  LN: LNBGS 1 ; 5.31210051 0.148123565
+  |  LN: LNBGS 2 ; 0.108228014 0.00301784938
+  |  LN: LNBGS 3 ; 0.00220502286 6.14852533e-05
+  |  LN: LNBGS 4 ; 4.49248361e-05 1.2526922e-06
+  |  LN: LNBGS 5 ; 9.15292506e-07 2.55221806e-08
+  |  LN: LNBGS 6 ; 1.86480453e-08 5.19985445e-10
+  |  LN: LNBGS 7 ; 3.79932752e-10 1.05941131e-11
+  |  LN: LNBGS Converged
+  |  LS: BCHK 0 ; 0.00229800815 0.000300896468
+  NL: Newton 2 ; 0.00229800815 8.29632863e-05
+  |  
+  |  ============
+  |  sub1.sub2.g1
+  |  ============
+  |  LN: LNBGS 0 ; 7.63037967 1
+  |  LN: LNBGS 1 ; 1.52607593 0.2
+  |  LN: LNBGS 2 ; 0.030168883 0.00395378531
+  |  LN: LNBGS 3 ; 0.000596406432 7.81620913e-05
+  |  LN: LNBGS 4 ; 1.1790315e-05 1.54518064e-06
+  |  LN: LNBGS 5 ; 2.3308187e-07 3.05465626e-08
+  |  LN: LNBGS 6 ; 4.60777837e-09 6.03872752e-10
+  |  LN: LNBGS 7 ; 9.10908321e-11 1.19379161e-11
+  |  LN: LNBGS Converged
+  |  LS: BCHK 0 ; 2.16279761e-10 9.41161853e-08
+  NL: Newton 3 ; 2.16279761e-10 7.80818802e-12
+  NL: Newton Converged
+  
+  ==
+  g2
+  ==
+  NL: Newton 0 ; 10.8584882 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 10.8584882 1
+  |  | precon: LNBGS 1 ; 1.08584882 0.1
+  |  | precon: LNBGS 2 ; 0.108584882 0.01
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 10.8584882 1
+  |  | precon: LNBGS 1 ; 1.08584882 0.1
+  |  | precon: LNBGS 2 ; 0.108584882 0.01
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 10.6413184 1
+  |  | precon: LNBGS 1 ; 1.06413184 0.1
+  |  | precon: LNBGS 2 ; 0.106413184 0.01
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  LN: SCIPY 0 ; 2.23428074e-16 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 1 ; 0 0
+  |  LS: BCHK 0 ; 2.674353 0.246291469
+  NL: Newton 1 ; 2.674353 0.246291469
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 12.66543 1
+  |  | precon: LNBGS 1 ; 1.56456798 0.123530585
+  |  | precon: LNBGS 2 ; 0.0472420672 0.00373000105
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 24.0487147 1
+  |  | precon: LNBGS 1 ; 2.59147581 0.107759431
+  |  | precon: LNBGS 2 ; 0.0782495077 0.00325379167
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 0.0782537311 0.0292608085
+  NL: Newton 2 ; 0.0782537311 0.00720668752
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 2.67028344 1
+  |  | precon: LNBGS 1 ; 0.532483179 0.199410733
+  |  | precon: LNBGS 2 ; 0.0157297699 0.00589067426
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 5.34048535 1
+  |  | precon: LNBGS 1 ; 1.06744048 0.199877055
+  |  | precon: LNBGS 2 ; 0.0315326265 0.00590444958
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 0.0315326478 0.402953922
+  NL: Newton 3 ; 0.0315326478 0.002903963
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.141316508 1
+  |  | precon: LNBGS 1 ; 0.00433457078 0.0306727844
+  |  | precon: LNBGS 2 ; 0.000128644017 0.000910325474
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.251101036 1
+  |  | precon: LNBGS 1 ; 0.00774063729 0.0308267836
+  |  | precon: LNBGS 2 ; 0.000229731322 0.000914895954
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.0310731855 1
+  |  | precon: LNBGS 1 ; 0.000914868055 0.0294423646
+  |  | precon: LNBGS 2 ; 2.71520083e-05 0.000873808328
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  LN: SCIPY 0 ; 9.75180612e-16 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 1 ; 1.77600351e-18 0.0018212047
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 2 ; 2.39177393e-17 0.0245264714
+  |  LS: BCHK 0 ; 2.73246842e-05 0.000866552163
+  NL: Newton 4 ; 2.73246842e-05 2.51643542e-06
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.0315869424 1
+  |  | precon: LNBGS 1 ; 0.000930692166 0.0294644589
+  |  | precon: LNBGS 2 ; 2.75844271e-05 0.000873285762
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.0631467356 1
+  |  | precon: LNBGS 1 ; 0.00185996617 0.0294546686
+  |  | precon: LNBGS 2 ; 5.51268219e-05 0.000872995593
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 5.51268219e-05 2.01747334
+  NL: Newton 5 ; 5.51268219e-05 5.07684138e-06
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 8.31582143e-05 1
+  |  | precon: LNBGS 1 ; 1.8496054e-06 0.0222420048
+  |  | precon: LNBGS 2 ; 5.48196761e-08 0.00065922142
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.000111245695 1
+  |  | precon: LNBGS 1 ; 2.06532962e-06 0.0185654791
+  |  | precon: LNBGS 2 ; 6.12134356e-08 0.000550254421
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 6.12134368e-08 0.00111041113
+  NL: Newton 6 ; 6.12134368e-08 5.63738119e-09
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 5.50043951e-05 1
+  |  | precon: LNBGS 1 ; 1.63025086e-06 0.0296385564
+  |  | precon: LNBGS 2 ; 4.83184329e-08 0.000878446764
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.000110070004 1
+  |  | precon: LNBGS 1 ; 3.26231788e-06 0.0296385734
+  |  | precon: LNBGS 2 ; 9.66906938e-08 0.000878447267
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 2.54594824e-07 1
+  |  | precon: LNBGS 1 ; 7.54770819e-09 0.0296459609
+  |  | precon: LNBGS 2 ; 2.23703872e-10 0.000878666223
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  LN: SCIPY 0 ; 1.95550592e-13 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 1 ; 3.43561559e-16 0.00175689348
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 2 ; 7.96739733e-25 4.07434069e-12
+  |  LS: BCHK 0 ; 2.6903145e-08 0.439497379
+  NL: Newton 7 ; 2.6903145e-08 2.47761426e-09
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 1.15002949e-07 1
+  |  | precon: LNBGS 1 ; 3.77987804e-09 0.0328676618
+  |  | precon: LNBGS 2 ; 1.12030478e-10 0.000974153088
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 2.03107809e-07 1
+  |  | precon: LNBGS 1 ; 6.57788963e-09 0.0323861976
+  |  | precon: LNBGS 2 ; 1.94959761e-10 0.000959883141
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 1.94958716e-10 0.00724668865
+  NL: Newton 8 ; 1.94958716e-10 1.79544991e-11
+  NL: Newton Converged
NL: Newton 0 ; 2.37397296 1
|  LN: SCIPY 0 ; 0.098359697 1
|  LN: SCIPY 1 ; 0.0693696179 0.705264656
|  LN: SCIPY 2 ; 0.0140631673 0.142976928
|  LN: SCIPY 3 ; 3.37376407e-17 3.4300269e-16
|  LS: BCHK 0 ; 0.00515047967 0.00216956121
NL: Newton 1 ; 0.00515047967 0.00216956121
|  LN: SCIPY 0 ; 43.4785647 1
|  LN: SCIPY 1 ; 31.5021075 0.72454341
|  LN: SCIPY 2 ; 6.15996484 0.141678201
|  LN: SCIPY 3 ; 5.56612352e-14 1.28019947e-15
|  LS: BCHK 0 ; 7.76418949e-08 1.5074692e-05
NL: Newton 2 ; 7.76418949e-08 3.27054672e-08
|  LN: SCIPY 0 ; 47073.4632 1
|  LN: SCIPY 1 ; 9397.23777 0.199629199
|  LN: SCIPY 2 ; 6363.98157 0.135192551
|  LN: SCIPY 3 ; 5.91164947e-12 1.25583483e-16
|  LN: SCIPY 4 ; 0 0
|  LS: BCHK 0 ; 0 0
NL: Newton 3 ; 0 0
NL: Newton Converged

To print everything for nonlinear solvers, and nothing for the linear solvers, first turn everything on, as shown above, and then call set_solver_print again to set a level of “-1” on just the linear solvers (using the type_ argument), so that we suppress everything, including the messages when the linear block Gauss-Seidel solver hits the maximum iteration limit. You can call the set_solver_print method multiple times to stack different solver print types in your model.

from openmdao.test_suite.components.double_sellar import SubSellar

prob = om.Problem()
model = prob.model

sub1 = model.add_subsystem('sub1', om.Group(), promotes_inputs=['z'])
sub2 = sub1.add_subsystem('sub2', om.Group(), promotes_inputs=['z'])
g1 = sub2.add_subsystem('g1', SubSellar(), promotes_inputs=['z'])
g2 = model.add_subsystem('g2', SubSellar())

model.connect('sub1.sub2.g1.y2', 'g2.x')
model.connect('g2.y2', 'sub1.sub2.g1.x')

model.nonlinear_solver = om.NewtonSolver()
model.linear_solver = om.ScipyKrylov()
model.nonlinear_solver.options['solve_subsystems'] = True
model.nonlinear_solver.options['max_sub_solves'] = 0

g1.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g1.linear_solver = om.LinearBlockGS()

g2.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g2.linear_solver = om.ScipyKrylov()
g2.linear_solver.precon = om.LinearBlockGS()
g2.linear_solver.precon.options['maxiter'] = 2

prob.set_solver_print(level=2)
prob.set_solver_print(level=-1, type_='LN')

prob.setup()
prob.set_val('z', np.array([5.0, 2.0]))
prob.run_model()
+  
+  ============
+  sub1.sub2.g1
+  ============
+  NL: Newton 0 ; 27.6990975 1
+  |  LS: BCHK 0 ; 7.63720546 0.275720372
+  NL: Newton 1 ; 7.63720546 0.275720372
+  |  LS: BCHK 0 ; 0.00229800815 0.000300896468
+  NL: Newton 2 ; 0.00229800815 8.29632863e-05
+  |  LS: BCHK 0 ; 2.16279761e-10 9.41161853e-08
+  NL: Newton 3 ; 2.16279761e-10 7.80818802e-12
+  NL: Newton Converged
+  
+  ==
+  g2
+  ==
+  NL: Newton 0 ; 10.8584882 1
+  |  LS: BCHK 0 ; 2.674353 0.246291469
+  NL: Newton 1 ; 2.674353 0.246291469
+  |  LS: BCHK 0 ; 0.0782537311 0.0292608085
+  NL: Newton 2 ; 0.0782537311 0.00720668752
+  |  LS: BCHK 0 ; 0.0315326478 0.402953922
+  NL: Newton 3 ; 0.0315326478 0.002903963
+  |  LS: BCHK 0 ; 2.73246842e-05 0.000866552163
+  NL: Newton 4 ; 2.73246842e-05 2.51643542e-06
+  |  LS: BCHK 0 ; 5.51268219e-05 2.01747334
+  NL: Newton 5 ; 5.51268219e-05 5.07684138e-06
+  |  LS: BCHK 0 ; 6.12134368e-08 0.00111041113
+  NL: Newton 6 ; 6.12134368e-08 5.63738119e-09
+  |  LS: BCHK 0 ; 2.6903145e-08 0.439497379
+  NL: Newton 7 ; 2.6903145e-08 2.47761426e-09
+  |  LS: BCHK 0 ; 1.94958716e-10 0.00724668865
+  NL: Newton 8 ; 1.94958716e-10 1.79544991e-11
+  NL: Newton Converged
NL: Newton 0 ; 2.37397296 1
|  LS: BCHK 0 ; 0.00515047967 0.00216956121
NL: Newton 1 ; 0.00515047967 0.00216956121
|  LS: BCHK 0 ; 7.76418949e-08 1.5074692e-05
NL: Newton 2 ; 7.76418949e-08 3.27054672e-08
|  LS: BCHK 0 ; 0 0
NL: Newton 3 ; 0 0
NL: Newton Converged

If we just want to print solver output for the first level of this multi-level model, we first turn off all printing, and then set a print level of “2” with a depth argument of “2” so that we only print the top solver and the solver in ‘g2’, but not the solver in ‘sub1.sub2.g1’.

from openmdao.test_suite.components.double_sellar import SubSellar

prob = om.Problem()
model = prob.model

sub1 = model.add_subsystem('sub1', om.Group(), promotes_inputs=['z'])
sub2 = sub1.add_subsystem('sub2', om.Group(), promotes_inputs=['z'])
g1 = sub2.add_subsystem('g1', SubSellar(), promotes_inputs=['z'])
g2 = model.add_subsystem('g2', SubSellar())

model.connect('sub1.sub2.g1.y2', 'g2.x')
model.connect('g2.y2', 'sub1.sub2.g1.x')

model.nonlinear_solver = om.NewtonSolver()
model.linear_solver = om.ScipyKrylov()
model.nonlinear_solver.options['solve_subsystems'] = True
model.nonlinear_solver.options['max_sub_solves'] = 0

g1.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g1.linear_solver = om.LinearBlockGS()

g2.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g2.linear_solver = om.ScipyKrylov()
g2.linear_solver.precon = om.LinearBlockGS()
g2.linear_solver.precon.options['maxiter'] = 2

prob.set_solver_print(level=0)
prob.set_solver_print(level=2, depth=2)

prob.setup()
prob.set_val('z', np.array([5.0, 2.0]))
prob.run_model()
+  |  LN: LNBGSSolver 'LN: LNBGS' on system 'sub1.sub2.g1' failed to converge in 10 iterations.
+  
+  ==
+  g2
+  ==
+  NL: Newton 0 ; 10.8584882 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 10.8584882 1
+  |  | precon: LNBGS 1 ; 1.08584882 0.1
+  |  | precon: LNBGS 2 ; 0.108584882 0.01
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 10.8584882 1
+  |  | precon: LNBGS 1 ; 1.08584882 0.1
+  |  | precon: LNBGS 2 ; 0.108584882 0.01
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 10.6413184 1
+  |  | precon: LNBGS 1 ; 1.06413184 0.1
+  |  | precon: LNBGS 2 ; 0.106413184 0.01
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  LN: SCIPY 0 ; 2.23428074e-16 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 1 ; 0 0
+  |  LS: BCHK 0 ; 2.674353 0.246291469
+  NL: Newton 1 ; 2.674353 0.246291469
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 12.66543 1
+  |  | precon: LNBGS 1 ; 1.56456798 0.123530585
+  |  | precon: LNBGS 2 ; 0.0472420672 0.00373000105
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 24.0487147 1
+  |  | precon: LNBGS 1 ; 2.59147581 0.107759431
+  |  | precon: LNBGS 2 ; 0.0782495077 0.00325379167
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 0.0782537311 0.0292608085
+  NL: Newton 2 ; 0.0782537311 0.00720668752
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 2.67028344 1
+  |  | precon: LNBGS 1 ; 0.532483179 0.199410733
+  |  | precon: LNBGS 2 ; 0.0157297699 0.00589067426
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 5.34048535 1
+  |  | precon: LNBGS 1 ; 1.06744048 0.199877055
+  |  | precon: LNBGS 2 ; 0.0315326265 0.00590444958
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 0.0315326478 0.402953922
+  NL: Newton 3 ; 0.0315326478 0.002903963
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.141316508 1
+  |  | precon: LNBGS 1 ; 0.00433457078 0.0306727844
+  |  | precon: LNBGS 2 ; 0.000128644017 0.000910325474
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.251101036 1
+  |  | precon: LNBGS 1 ; 0.00774063729 0.0308267836
+  |  | precon: LNBGS 2 ; 0.000229731322 0.000914895954
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.0310731855 1
+  |  | precon: LNBGS 1 ; 0.000914868055 0.0294423646
+  |  | precon: LNBGS 2 ; 2.71520083e-05 0.000873808328
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  LN: SCIPY 0 ; 9.75180612e-16 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 1 ; 1.77600351e-18 0.0018212047
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 2 ; 2.39177393e-17 0.0245264714
+  |  LS: BCHK 0 ; 2.73246842e-05 0.000866552163
+  NL: Newton 4 ; 2.73246842e-05 2.51643542e-06
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.0315869424 1
+  |  | precon: LNBGS 1 ; 0.000930692166 0.0294644589
+  |  | precon: LNBGS 2 ; 2.75844271e-05 0.000873285762
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.0631467356 1
+  |  | precon: LNBGS 1 ; 0.00185996617 0.0294546686
+  |  | precon: LNBGS 2 ; 5.51268219e-05 0.000872995593
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 5.51268219e-05 2.01747334
+  NL: Newton 5 ; 5.51268219e-05 5.07684138e-06
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 8.31582143e-05 1
+  |  | precon: LNBGS 1 ; 1.8496054e-06 0.0222420048
+  |  | precon: LNBGS 2 ; 5.48196761e-08 0.00065922142
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.000111245695 1
+  |  | precon: LNBGS 1 ; 2.06532962e-06 0.0185654791
+  |  | precon: LNBGS 2 ; 6.12134356e-08 0.000550254421
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 6.12134368e-08 0.00111041113
+  NL: Newton 6 ; 6.12134368e-08 5.63738119e-09
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 5.50043951e-05 1
+  |  | precon: LNBGS 1 ; 1.63025086e-06 0.0296385564
+  |  | precon: LNBGS 2 ; 4.83184329e-08 0.000878446764
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0.000110070004 1
+  |  | precon: LNBGS 1 ; 3.26231788e-06 0.0296385734
+  |  | precon: LNBGS 2 ; 9.66906938e-08 0.000878447267
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 2.54594824e-07 1
+  |  | precon: LNBGS 1 ; 7.54770819e-09 0.0296459609
+  |  | precon: LNBGS 2 ; 2.23703872e-10 0.000878666223
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  LN: SCIPY 0 ; 1.95550592e-13 1
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 1 ; 3.43561559e-16 0.00175689348
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 2 ; 7.96739733e-25 4.07434069e-12
+  |  LS: BCHK 0 ; 2.6903145e-08 0.439497379
+  NL: Newton 7 ; 2.6903145e-08 2.47761426e-09
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 1.15002949e-07 1
+  |  | precon: LNBGS 1 ; 3.77987804e-09 0.0328676618
+  |  | precon: LNBGS 2 ; 1.12030478e-10 0.000974153088
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 2.03107809e-07 1
+  |  | precon: LNBGS 1 ; 6.57788963e-09 0.0323861976
+  |  | precon: LNBGS 2 ; 1.94959761e-10 0.000959883141
+  |  | precon:LN: LNBGSSolver 'LN: LNBGS' on system 'g2' failed to converge in 2 iterations.
+  |  | precon:
+  |  | precon:==
+  |  | precon:g2
+  |  | precon:==
+  |  | precon: LNBGS 0 ; 0 0
+  |  | precon:LN: LNBGS Converged
+  |  LN: SCIPY 0 ; 0 0
+  |  LS: BCHK 0 ; 1.94958716e-10 0.00724668865
+  NL: Newton 8 ; 1.94958716e-10 1.79544991e-11
+  NL: Newton Converged
NL: Newton 0 ; 2.37397296 1
|  LN: SCIPY 0 ; 0.098359697 1
|  LN: SCIPY 1 ; 0.0693696179 0.705264656
|  LN: SCIPY 2 ; 0.0140631673 0.142976928
|  LN: SCIPY 3 ; 3.37376407e-17 3.4300269e-16
|  LS: BCHK 0 ; 0.00515047967 0.00216956121
NL: Newton 1 ; 0.00515047967 0.00216956121
|  LN: SCIPY 0 ; 43.4785647 1
|  LN: SCIPY 1 ; 31.5021075 0.72454341
|  LN: SCIPY 2 ; 6.15996484 0.141678201
|  LN: SCIPY 3 ; 5.56612352e-14 1.28019947e-15
|  LS: BCHK 0 ; 7.76418949e-08 1.5074692e-05
NL: Newton 2 ; 7.76418949e-08 3.27054672e-08
|  LN: SCIPY 0 ; 47073.4632 1
|  LN: SCIPY 1 ; 9397.23777 0.199629199
|  LN: SCIPY 2 ; 6363.98157 0.135192551
|  LN: SCIPY 3 ; 5.91164947e-12 1.25583483e-16
|  LN: SCIPY 4 ; 0 0
|  LS: BCHK 0 ; 0 0
NL: Newton 3 ; 0 0
NL: Newton Converged

The set_solver_print method can also be called on Systems. For instance, if we want to print detailed output from group ‘g2’ down, we can first call set_solver_print on the problem or the top level model with a level of “-1”, and then call it on group ‘g2’ with a level of “2”.

from openmdao.test_suite.components.double_sellar import SubSellar

prob = om.Problem()
model = prob.model

model.add_subsystem('pz', om.IndepVarComp('z', np.array([5.0, 2.0])))

sub1 = model.add_subsystem('sub1', om.Group())
sub2 = sub1.add_subsystem('sub2', om.Group())
g1 = sub2.add_subsystem('g1', SubSellar())
g2 = model.add_subsystem('g2', SubSellar())

model.connect('sub1.sub2.g1.y2', 'g2.x')
model.connect('g2.y2', 'sub1.sub2.g1.x')

model.nonlinear_solver = om.NewtonSolver()
model.linear_solver = om.ScipyKrylov()
model.nonlinear_solver.options['solve_subsystems'] = True
model.nonlinear_solver.options['max_sub_solves'] = 0

g1.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g1.linear_solver = om.LinearBlockGS()

g2.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)
g2.linear_solver = om.ScipyKrylov()
g2.linear_solver.precon = om.LinearBlockGS()
g2.linear_solver.precon.options['maxiter'] = 2

prob.set_solver_print(level=-1, type_='all')
g2.set_solver_print(level=2, type_='NL')

prob.setup()
prob.run_model()
+  
+  ==
+  g2
+  ==
+  NL: Newton 0 ; 0.295012438 1
+  |  LS: BCHK 0 ; 0.0110545084 0.0374713301
+  NL: Newton 1 ; 0.0110545084 0.0374713301
+  |  LS: BCHK 0 ; 0.00687558034 0.621970698
+  NL: Newton 2 ; 0.00687558034 0.0233060694
+  |  LS: BCHK 0 ; 0.000704373693 0.102445708
+  NL: Newton 3 ; 0.000704373693 0.00238760677
+  |  LS: BCHK 0 ; 0.000159452227 0.226374478
+  NL: Newton 4 ; 0.000159452227 0.000540493236
+  |  LS: BCHK 0 ; 1.48224681e-06 0.00929586772
+  NL: Newton 5 ; 1.48224681e-06 5.02435363e-06
+  |  LS: BCHK 0 ; 2.17279174e-06 1.46587715
+  NL: Newton 6 ; 2.17279174e-06 7.36508519e-06
+  |  LS: BCHK 0 ; 1.14602217e-08 0.00527442253
+  NL: Newton 7 ; 1.14602217e-08 3.88465712e-08
+  |  LS: BCHK 0 ; 2.30427312e-08 2.01067063
+  NL: Newton 8 ; 2.30427312e-08 7.81076599e-08
+  |  LS: BCHK 0 ; 4.8361706e-10 0.0209878359
+  NL: Newton 9 ; 4.8361706e-10 1.63931075e-09
+  |  LS: BCHK 0 ; 6.21009466e-10 1.28409338
+  NL: Newton 10 ; 6.21009466e-10 2.10502808e-09
+  NL: NewtonSolver 'NL: Newton' on system 'g2' failed to converge in 10 iterations.