Setting Options in Jupyter Notebook

Setting Options in Jupyter Notebook

When working in a Jupyter notebook, you can quickly see and set options on the driver and systems in your model.

Here we demonstrate the ability to interactively set the driver options for the Sellar MDA model from the Basic User Guide.

import openmdao.api as om
from openmdao.test_suite.components.sellar_feature import SellarMDA

import numpy as np

# define Sellar MDA problem
prob = om.Problem(model=SellarMDA())

model = prob.model
model.add_design_var('z', lower=np.array([-10.0, 0.0]),
                          upper=np.array([10.0, 10.0]))
model.add_design_var('x', lower=0.0, upper=10.0)
model.add_objective('obj')
model.add_constraint('con1', upper=0.0)
model.add_constraint('con2', upper=0.0)

# set driver and show driver options
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options

Try changing some of the options above and then seeing how the options have changed by running the cell below:

from pprint import pprint
pprint(list(prob.driver.options.items()))
[('debug_print', []),
 ('optimizer', 'SLSQP'),
 ('tol', 1e-06),
 ('maxiter', 200),
 ('disp', True),
 ('singular_jac_behavior', 'warn'),
 ('singular_jac_tol', 1e-16)]

Similarly, we can set recording options:

prob.driver.recording_options

pprint(list(prob.driver.recording_options.items()))
[('record_desvars', True),
 ('record_responses', False),
 ('record_objectives', True),
 ('record_constraints', True),
 ('includes', []),
 ('excludes', []),
 ('record_derivatives', False),
 ('record_inputs', True),
 ('record_outputs', True),
 ('record_residuals', False)]
# run the optimization
prob.setup()
prob.run_driver()
=====
cycle
=====
NL: NLBGS Converged in 8 iterations
=====
cycle
=====
NL: NLBGS Converged in 1 iterations
=====
cycle
=====
NL: NLBGS Converged in 9 iterations
=====
cycle
=====
NL: NLBGS Converged in 9 iterations
=====
cycle
=====
NL: NLBGS Converged in 10 iterations
=====
cycle
=====
NL: NLBGS Converged in 9 iterations
=====
cycle
=====
NL: NLBGS Converged in 8 iterations
=====
cycle
=====
NL: NLBGS Converged in 7 iterations
=====
cycle
=====
NL: NLBGS Converged in 7 iterations
=====
cycle
=====
NL: NLBGS Converged in 6 iterations
=====
cycle
=====
NL: NLBGS Converged in 5 iterations
Optimization terminated successfully    (Exit mode 0)
            Current function value: [3.18339411]
            Iterations: 9
            Function evaluations: 10
            Gradient evaluations: 9
Optimization Complete
-----------------------------------
False

Note that options that are not editable, such as the guess_func function reference for a BalanceComp, will be shown but disabled:

def guess_function(inputs, outputs, residuals):
    outputs['x'] = np.sqrt(inputs['rhs:x'])

bal = om.BalanceComp('x', guess_func=guess_function)
bal.options