Basic Recording Example

Recording Terminology

  • Case: A Case stores a snapshot of all the variable values, metadata, and options of a model, or a sub-set of a model, at a particular point in time

  • Case Recorder: An OpenMDAO module used to store a snapshot of a model before, during, or after execution in an SQL file.

  • Sources: The OpenMDAO object responsible for recording the case. Can be Problem, Driver or a specific System or Solver identified by pathname.

Basic Recording Example

Below is a basic example of how to create a recorder, attach it to a Problem, save the information, and retrieve the data from the recorder. list_outputs is a quick way to show all of your outputs and their values at the time the case was recorded, and should you need to isolate a single value OpenMDAO provides two ways to retrieve them. To view all the design variables, constraints, and objectives, you can use their methods like the example below.

from openmdao.test_suite.components.sellar_feature import SellarMDAWithUnits
import numpy as np
import openmdao.api as om

# build the model
prob = om.Problem(model=SellarMDAWithUnits())

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)

# setup the optimization
driver = prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-9, disp=False)

# Create a recorder variable
recorder = om.SqliteRecorder('cases.sql')
# Attach a recorder to the problem
prob.add_recorder(recorder)

prob.setup()
prob.set_solver_print(0)
prob.run_driver()
prob.record("after_run_driver")

# Instantiate your CaseReader
cr = om.CaseReader("cases.sql")
# Isolate "problem" as your source
driver_cases = cr.list_cases('problem', out_stream=None)
# Get the first case from the recorder
case = cr.get_case('after_run_driver')

# These options will give outputs as the model sees them
# Gets value but will not convert units
const = case['con1']
print(const)

# get_val can convert your result's units if desired
const_K = case.get_val("con1", units='K')

print(const_K)
[-1.68550507e-10]
[273.15]
# list_outputs will list your model's outputs and return a list of them too
print(case.list_outputs())
5 Explicit Output(s) in 'model'

varname   val              
--------  -----------------
cycle
  d1
    y1    [3.16]           
  d2
    y2    [3.75527777]     
obj_cmp
  obj     [3.18339395]     
con_cmp1
  con1    [-1.68550507e-10]
con_cmp2
  con2    [-20.24472223]   


0 Implicit Output(s) in 'model'


[('con_cmp1.con1', {'val': array([-1.68550507e-10]), 'value': array([-1.68550507e-10])}), ('con_cmp2.con2', {'val': array([-20.24472223]), 'value': array([-20.24472223])}), ('cycle.d1.y1', {'val': array([3.16]), 'value': array([3.16])}), ('cycle.d2.y2', {'val': array([3.75527777]), 'value': array([3.75527777])}), ('obj_cmp.obj', {'val': array([3.18339395]), 'value': array([3.18339395])})]
# This code below will find all the objectives, design variables, and constraints that the
# problem source contains
objectives = case.get_objectives()
print(objectives['obj'])

design_vars = case.get_design_vars()
print(design_vars['x'])

constraints = case.get_constraints()
print(constraints['con1'])
[3.18339395]
[0.]
[-1.68550507e-10]