Post · 8 min read· pp. 38–44

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.

SellarMDAWithUnits class definitiondiagram

text
class SellarMDAWithUnits(om.Group):
    """
    Group containing the Sellar MDA.
    """

    class SellarDis1Units(om.ExplicitComponent):
        """
        Component containing Discipline 1 -- no derivatives version.
        """

        def setup(self):

            # Global Design Variable
            self.add_input('z', val=np.zeros(2), units='degC')

            # Local Design Variable
            self.add_input('x', val=0., units='degC')

            # Coupling parameter
            self.add_input('y2', val=1.0, units='degC')

            # Coupling output
            self.add_output('y1', val=1.0, units='degC')

        def setup_partials(self):
            # Finite difference all partials.
            self.declare_partials('*', '*', method='fd')

        def compute(self, inputs, outputs):
            """
            Evaluates the equation
            y1 = z1**2 + z2 + x1 - 0.2*y2
            """
            z1 = inputs['z'][0]
            z2 = inputs['z'][1]
            x1 = inputs['x']
            y2 = inputs['y2']

            outputs['y1'] = z1**2 + z2 + x1 - 0.2*y2

    class SellarDis2Units(om.ExplicitComponent):
        """
        Component containing Discipline 2 -- no derivatives version.
        """

        def setup(self):
            # Global Design Variable
            self.add_input('z', val=np.zeros(2), units='degC')

            # Coupling parameter
            self.add_input('y1', val=1.0, units='degC')

            # Coupling output
            self.add_output('y2', val=1.0, units='degC')

        def setup_partials(self):
            # Finite difference all partials.
            self.declare_partials('*', '*', method='fd')

        def compute(self, inputs, outputs):
            """
            Evaluates the equation
            y2 = y1**(.5) + z1 + z2
            """

            z1 = inputs['z'][0]
            z2 = inputs['z'][1]
            y1 = inputs['y1']

            # Note: this may cause some issues. However, y1 is constrained to be
            # above 3.16, so lets just let it converge, and the optimizer will
            # throw it out
            if y1.real < 0.0:
                y1 *= -1

            outputs['y2'] = y1**.5 + z1 + z2

    def setup(self):

        cycle = self.add_subsystem('cycle', om.Group(), promotes=['*'])
        cycle.add_subsystem('d1', self.SellarDis1Units(), promotes_inputs=['x', 'z', 'y2'],
                            promotes_outputs=['y1'])
        cycle.add_subsystem('d2', self.SellarDis2Units(), promotes_inputs=['z', 'y1'],
                            promotes_outputs=['y2'])

        self.set_input_defaults('x', 1.0, units='degC')
        self.set_input_defaults('z', np.array([5.0, 2.0]), units='degC')

        # Nonlinear Block Gauss Seidel is a gradient free solver
        cycle.nonlinear_solver = om.NonlinearBlockGS()

        self.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)',
                                                  z={'val': np.array([0.0, 0.0]), 'units': 'degC'},
                                                  x={'val': 0.0, 'units': 'degC'},
                                                  y1={'units': 'degC'},
                                                  y2={'units': 'degC'}),
                           promotes=['x', 'z', 'y1', 'y2', 'obj'])

        self.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1', y1={'units': 'degC'},
                                                   con1={'units': 'degC'}),
                           promotes=['con1', 'y1'])
        self.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0', y2={'units': 'degC'},
                                                   con2={'units': 'degC'}),
                           promotes=['con2', 'y2'])
python
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('basic_recording_example.sql')
# Attach a recorder to the problem
prob.add_recorder(recorder)

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

# Instantiate your CaseReader
cr = om.CaseReader(prob.get_outputs_dir() / "basic_recording_example.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)
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 29.0742299 1
NL: NLBGS 2 ; 2.26505979 0.0779060975
NL: NLBGS 3 ; 0.0438762115 0.00150911001
NL: NLBGS 4 ; 0.000867730413 2.98453446e-05
NL: NLBGS 5 ; 1.715381e-05 5.90000492e-07
NL: NLBGS 6 ; 3.39109478e-07 1.16635756e-08
NL: NLBGS 7 ; 6.70377103e-09 2.30574328e-10
NL: NLBGS 8 ; 1.32523072e-10 4.55809395e-12
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 2.62038164e-12 1
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 19.1706591 1
NL: NLBGS 2 ; 1.13589624 0.0592518093
NL: NLBGS 3 ; 0.0406301437 0.00211939211
NL: NLBGS 4 ; 0.00140619452 7.33513916e-05
NL: NLBGS 5 ; 4.87225696e-05 2.54151771e-06
NL: NLBGS 6 ; 1.68809957e-06 8.80564178e-08
NL: NLBGS 7 ; 5.84879659e-08 3.05091054e-09
NL: NLBGS 8 ; 2.0264451e-09 1.05705552e-10
NL: NLBGS 9 ; 7.02107155e-11 3.66240488e-12
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 5.26036063 1
NL: NLBGS 2 ; 0.514550635 0.0978166083
NL: NLBGS 3 ; 0.0258623712 0.00491646354
NL: NLBGS 4 ; 0.00126421196 0.000240328003
NL: NLBGS 5 ; 6.18806612e-05 1.17635777e-05
NL: NLBGS 6 ; 3.02873598e-06 5.75765845e-07
NL: NLBGS 7 ; 1.48241325e-07 2.81808293e-08
NL: NLBGS 8 ; 7.25566271e-09 1.37930899e-09
NL: NLBGS 9 ; 3.55128357e-10 6.75102682e-11
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 1.07485557 1
NL: NLBGS 2 ; 0.0990937832 0.0921926498
NL: NLBGS 3 ; 0.00548653522 0.00510443949
NL: NLBGS 4 ; 0.000301840404 0.0002808195
NL: NLBGS 5 ; 1.66114932e-05 1.54546282e-05
NL: NLBGS 6 ; 9.14179752e-07 8.50514039e-07
NL: NLBGS 7 ; 5.03100774e-08 4.68063605e-08
NL: NLBGS 8 ; 2.76871581e-09 2.57589567e-09
NL: NLBGS 9 ; 1.52370715e-10 1.41759246e-10
NL: NLBGS 10 ; 8.38520075e-12 7.80123486e-12
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 0.158851234 1
NL: NLBGS 2 ; 0.0153545659 0.0966600355
NL: NLBGS 3 ; 0.00086251981 0.00542973314
NL: NLBGS 4 ; 4.84015909e-05 0.000304697607
NL: NLBGS 5 ; 2.71628246e-06 1.70995364e-05
NL: NLBGS 6 ; 1.52436453e-07 9.59617681e-07
NL: NLBGS 7 ; 8.55466141e-09 5.38532889e-08
NL: NLBGS 8 ; 4.80083649e-10 3.02222171e-09
NL: NLBGS 9 ; 2.69419589e-11 1.6960497e-10
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 0.0172187064 1
NL: NLBGS 2 ; 0.00168376512 0.0977869697
NL: NLBGS 3 ; 9.47059834e-05 0.00550017993
NL: NLBGS 4 ; 5.32629053e-06 0.00030933163
NL: NLBGS 5 ; 2.99553923e-07 1.73970051e-05
NL: NLBGS 6 ; 1.68470948e-08 9.78418145e-07
NL: NLBGS 7 ; 9.47491167e-10 5.50268495e-08
NL: NLBGS 8 ; 5.32877417e-11 3.09475871e-09
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 0.00175633487 1
NL: NLBGS 2 ; 0.000171891789 0.0978695987
NL: NLBGS 3 ; 9.66953306e-06 0.00550551789
NL: NLBGS 4 ; 5.43939908e-07 0.000309701707
NL: NLBGS 5 ; 3.05982525e-08 1.74216506e-05
NL: NLBGS 6 ; 1.72124338e-09 9.80020044e-07
NL: NLBGS 7 ; 9.68248369e-11 5.51289156e-08
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 0.000177747679 1
NL: NLBGS 2 ; 1.73975932e-05 0.097878033
NL: NLBGS 3 ; 9.78689694e-07 0.00550606173
NL: NLBGS 4 ; 5.50554547e-08 0.000309739373
NL: NLBGS 5 ; 3.09710328e-09 1.74241559e-05
NL: NLBGS 6 ; 1.74224984e-10 9.80181485e-07
NL: NLBGS 7 ; 9.80082451e-12 5.51389731e-08
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 1.79815195e-05 1
NL: NLBGS 2 ; 1.7600111e-06 0.0978788865
NL: NLBGS 3 ; 9.90083455e-08 0.00550611675
NL: NLBGS 4 ; 5.56965333e-09 0.000309743197
NL: NLBGS 5 ; 3.1331774e-10 1.74244307e-05
NL: NLBGS 6 ; 1.76257707e-11 9.80215869e-07
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 1.81929779e-06 1
NL: NLBGS 2 ; 1.78067619e-07 0.0978771152
NL: NLBGS 3 ; 1.00170861e-08 0.00550601784
NL: NLBGS 4 ; 5.63504899e-10 0.000309737582
NL: NLBGS 5 ; 3.16997672e-11 1.74241773e-05
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 1.83235039e-07 1
NL: NLBGS 2 ; 1.79382663e-08 0.0978975765
NL: NLBGS 3 ; 1.00910654e-09 0.00550717018
NL: NLBGS 4 ; 5.67669739e-11 0.000309804141
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 1.85444354e-08 1
NL: NLBGS 2 ; 1.81498585e-09 0.0978722628
NL: NLBGS 3 ; 1.02100677e-10 0.00550573122
NL: NLBGS 4 ; 5.74347468e-12 0.000309714185
NL: NLBGS Converged
myst-ansi
=====
cycle
=====
NL: NLBGS 1 ; 1.87205065e-09 1
NL: NLBGS 2 ; 1.83221756e-10 0.0978722215
NL: NLBGS 3 ; 1.03073754e-11 0.00550592761
NL: NLBGS Converged
myst-ansi
[-1.66145764e-10]
[273.15]
python
# list_outputs will list your model's outputs and return a list of them too
print(case.list_outputs())
myst-ansi
5 Explicit Output(s) in 'model'

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


0 Implicit Output(s) in 'model'


[('con_cmp1.con1', {'val': array([-1.66145764e-10]), 'io': 'output', 'prom_name': 'con1'}), ('con_cmp2.con2', {'val': array([-20.24472223]), 'io': 'output', 'prom_name': 'con2'}), ('cycle.d1.y1', {'val': array([3.16]), 'io': 'output', 'prom_name': 'y1'}), ('cycle.d2.y2', {'val': array([3.75527777]), 'io': 'output', 'prom_name': 'y2'}), ('obj_cmp.obj', {'val': array([3.18339395]), 'io': 'output', 'prom_name': 'obj'})]
python
# 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'])
myst-ansi
[3.18339395]
[1.36309645e-15]
[-1.66145764e-10]