Accessing Recorded Metadata

In addition to the cases themselves, a CaseReader may also record certain metadata about the model and its constituent systems and solvers.

Problem Metadata

By default, a case recorder will save metadata about the model to assist in later visualization and debugging. This information is made available via the problem_metadata attribute of a CaseReader.

import openmdao.api as om
from openmdao.test_suite.components.sellar import SellarDerivatives

prob = om.Problem(SellarDerivatives())

recorder = om.SqliteRecorder("cases.sql")
prob.driver.add_recorder(recorder)

prob.setup()
prob.run_driver()
NL: NLBGS Converged in 8 iterations
prob.cleanup()

cr = om.CaseReader("cases.sql")

# access list of connections stored in metadata
connections = sorted(cr.problem_metadata['connections_list'], key=lambda x: (x['tgt'], x['src']))

print('\n'.join([conn['src']+'\t'+conn['tgt'] for conn in connections]))
d1.y1	con_cmp1.y1
d2.y2	con_cmp2.y2
_auto_ivc.v1	d1.x
d2.y2	d1.y2
_auto_ivc.v0	d1.z
d1.y1	d2.y1
_auto_ivc.v0	d2.z
_auto_ivc.v1	obj_cmp.x
d1.y1	obj_cmp.y1
d2.y2	obj_cmp.y2
_auto_ivc.v0	obj_cmp.z
# access the model tree stored in metadata
print(set(cr.problem_metadata['tree'].keys()))
{'linear_solver', 'subsystem_type', 'children', 'name', 'options', 'type', 'nonlinear_solver', 'expressions', 'class', 'is_parallel', 'nonlinear_solver_options', 'linear_solver_options', 'component_type'}
print(cr.problem_metadata['tree']['name'])
root
print(sorted([child["name"] for child in cr.problem_metadata['tree']["children"]]))
['_auto_ivc', 'con_cmp1', 'con_cmp2', 'd1', 'd2', 'obj_cmp']

System Options

All case recorders record the component options and scaling factors for all systems in the model. These values are accessible using code such as this, where cr is a case reader object.

  • cr.list_model_options()

This function creates a dictionary of the latest model options available that were set in the model.

import openmdao.api as om
from openmdao.test_suite.components.sellar import SellarDerivatives

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

prob.setup()

# create recorder and attach to driver and model
recorder = om.SqliteRecorder("cases.sql")
prob.driver.add_recorder(recorder)
prob.model.add_recorder(recorder)

prob.run_model()
NL: NLBGS Converged in 8 iterations
prob.cleanup()

cr = om.CaseReader("cases.sql")

# options for all the systems in the model
options = cr.list_model_options()

print(sorted(options.keys()))
Run Number: 0
    Subsystem: root
        assembled_jac_type : csc

        nonlinear_solver : <class 'openmdao.solvers.nonlinear.nonlinear_block_gs.NonlinearBlockGS'>

        nl_atol : None

        nl_maxiter : None

        linear_solver : <class 'openmdao.solvers.linear.scipy_iter_solver.ScipyKrylov'>

        ln_atol : None

        ln_maxiter : None
['root']
# options for system 'root'
print(options['root']['ln_maxiter'])
None

Solver Metadata

Solvers record the solver options as metadata. Note that, because more than one solver’s metadata may be recorded, each solver’s metadata must be accessed through its absolute name within solver_metadata, as shown in the example below.

import openmdao.api as om
from openmdao.test_suite.components.sellar import SellarDerivatives

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

# create recorder
recorder = om.SqliteRecorder("cases.sql")

# add recorder to the nonlinear solver for the model
prob.model.nonlinear_solver = om.NonlinearBlockGS()
prob.model.nonlinear_solver.add_recorder(recorder)

# add recorder to the nonlinear solver for Component 'd1'
d1 = prob.model.d1
d1.nonlinear_solver = om.NonlinearBlockGS()
d1.nonlinear_solver.options['maxiter'] = 5
d1.nonlinear_solver.add_recorder(recorder)

prob.run_model()
NL: NLBGS Converged in 8 iterations
prob.cleanup()

cr = om.CaseReader("cases.sql")

metadata = cr.solver_metadata

print(sorted(metadata.keys()))
['d1.NonlinearBlockGS', 'root.NonlinearBlockGS']
print(metadata['d1.NonlinearBlockGS']['solver_options']['maxiter'])
5
print(metadata['root.NonlinearBlockGS']['solver_options']['maxiter'])
10