Listing Variables#

When working with a model, it may sometimes be helpful to examine the input and output variables. Several methods are provided for this purpose.

System.list_inputs(val=True, prom_name=True, units=False, shape=False, global_shape=False, desc=False, hierarchical=True, print_arrays=False, tags=None, print_tags=False, includes=None, excludes=None, is_indep_var=None, is_design_var=None, all_procs=False, out_stream=DEFAULT_OUT_STREAM, print_min=False, print_max=False, return_format='list')[source]

Write a list of input names and other optional information to a specified stream.

Parameters:
valbool, optional

When True, display/return input values. Default is True.

prom_namebool, optional

When True, display/return the promoted name of the variable. Default is True.

unitsbool, optional

When True, display/return units. Default is False.

shapebool, optional

When True, display/return the shape of the value. Default is False.

global_shapebool, optional

When True, display/return the global shape of the value. Default is False.

descbool, optional

When True, display/return description. Default is False.

hierarchicalbool, optional

When True, human readable output shows variables in hierarchical format.

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions Default is False.

tagsstr or list of strs

User defined tags that can be used to filter what gets listed. Only inputs with the given tags will be listed. Default is None, which means there will be no filtering based on tags.

print_tagsbool

When true, display tags in the columnar display.

includesNone, str, or iter of str

Collection of glob patterns for pathnames of variables to include. Default is None, which includes all input variables.

excludesNone, str, or iter of str

Collection of glob patterns for pathnames of variables to exclude. Default is None.

is_indep_varbool or None

If None (the default), do no additional filtering of the inputs. If True, list only inputs connected to an output tagged openmdao:indep_var. If False, list only inputs _not_ connected to outputs tagged openmdao:indep_var.

is_design_varbool or None

If None (the default), do no additional filtering of the inputs. If True, list only inputs connected to outputs that are driver design variables. If False, list only inputs _not_ connected to outputs that are driver design variables.

all_procsbool, optional

When True, display output on all ranks. Default is False, which will display output only from rank 0.

out_streamfile-like object

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

print_minbool

When true, if the input value is an array, print its smallest value.

print_maxbool

When true, if the input value is an array, print its largest value.

return_formatstr

Indicates the desired format of the return value. Can have value of ‘list’ or ‘dict’. If ‘list’, the return value is a list of (name, metadata) tuples. if ‘dict’, the return value is a dictionary mapping {name: metadata}.

Returns:
list of (name, metadata) or dict of {name: metadata}

List or dict of input names and other optional information about those inputs.

System.list_outputs(explicit=True, implicit=True, val=True, prom_name=True, residuals=False, residuals_tol=None, units=False, shape=False, global_shape=False, bounds=False, scaling=False, desc=False, hierarchical=True, print_arrays=False, tags=None, print_tags=False, includes=None, excludes=None, is_indep_var=None, is_design_var=None, all_procs=False, list_autoivcs=False, out_stream=DEFAULT_OUT_STREAM, print_min=False, print_max=False, return_format='list')[source]

Write a list of output names and other optional information to a specified stream.

Parameters:
explicitbool, optional

Include outputs from explicit components. Default is True.

implicitbool, optional

Include outputs from implicit components. Default is True.

valbool, optional

When True, display output values. Default is True.

prom_namebool, optional

When True, display the promoted name of the variable. Default is True.

residualsbool, optional

When True, display residual values. Default is False.

residuals_tolfloat, optional

If set, limits the output of list_outputs to only variables where the norm of the resids array is greater than the given ‘residuals_tol’. Default is None.

unitsbool, optional

When True, display units. Default is False.

shapebool, optional

When True, display/return the shape of the value. Default is False.

global_shapebool, optional

When True, display/return the global shape of the value. Default is False.

boundsbool, optional

When True, display/return bounds (lower and upper). Default is False.

scalingbool, optional

When True, display/return scaling (ref, ref0, and res_ref). Default is False.

descbool, optional

When True, display/return description. Default is False.

hierarchicalbool, optional

When True, human readable output shows variables in hierarchical format.

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions Default is False.

tagsstr or list of strs

User defined tags that can be used to filter what gets listed. Only outputs with the given tags will be listed. Default is None, which means there will be no filtering based on tags.

print_tagsbool

When true, display tags in the columnar display.

includesNone, str, or iter of str

Collection of glob patterns for pathnames of variables to include. Default is None, which includes all output variables.

excludesNone, str, or iter of str

Collection of glob patterns for pathnames of variables to exclude. Default is None.

is_indep_varbool or None

If None (the default), do no additional filtering of the inputs. If True, list only outputs tagged openmdao:indep_var. If False, list only outputs that are _not_ tagged openmdao:indep_var.

is_design_varbool or None

If None (the default), do no additional filtering of the inputs. If True, list only inputs connected to outputs that are driver design variables. If False, list only inputs _not_ connected to outputs that are driver design variables.

all_procsbool, optional

When True, display output on all processors. Default is False.

list_autoivcsbool

If True, include auto_ivc outputs in the listing. Defaults to False.

out_streamfile-like

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

print_minbool

When true, if the output value is an array, print its smallest value.

print_maxbool

When true, if the output value is an array, print its largest value.

return_formatstr

Indicates the desired format of the return value. Can have value of ‘list’ or ‘dict’. If ‘list’, the return value is a list of (name, metadata) tuples. if ‘dict’, the return value is a dictionary mapping {name: metadata}.

Returns:
list of (name, metadata) or dict of {name: metadata}

List or dict of output names and other optional information about those outputs.

System.list_vars(val=True, prom_name=True, residuals=False, residuals_tol=None, units=False, shape=False, global_shape=False, bounds=False, scaling=False, desc=False, print_arrays=False, tags=None, print_tags=False, includes=None, excludes=None, is_indep_var=None, is_design_var=None, all_procs=False, list_autoivcs=False, out_stream=DEFAULT_OUT_STREAM, print_min=False, print_max=False, return_format='list')[source]

Write a list of inputs and outputs sorted by component in execution order.

Parameters:
valbool, optional

When True, display output values. Default is True.

prom_namebool, optional

When True, display the promoted name of the variable. Default is True.

residualsbool, optional

When True, display residual values. Default is False.

residuals_tolfloat, optional

If set, limits the output of list_outputs to only variables where the norm of the resids array is greater than the given ‘residuals_tol’. Default is None.

unitsbool, optional

When True, display units. Default is False.

shapebool, optional

When True, display/return the shape of the value. Default is False.

global_shapebool, optional

When True, display/return the global shape of the value. Default is False.

boundsbool, optional

When True, display/return bounds (lower and upper). Default is False.

scalingbool, optional

When True, display/return scaling (ref, ref0, and res_ref). Default is False.

descbool, optional

When True, display/return description. Default is False.

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions Default is False.

tagsstr or list of strs

User defined tags that can be used to filter what gets listed. Only outputs with the given tags will be listed. Default is None, which means there will be no filtering based on tags.

print_tagsbool

When true, display tags in the columnar display.

includesNone, str, or iter of str

Collection of glob patterns for pathnames of variables to include. Default is None, which includes all output variables.

excludesNone, str, or iter of str

Collection of glob patterns for pathnames of variables to exclude. Default is None.

is_indep_varbool or None

If None (the default), do no additional filtering of the inputs. If True, list only outputs tagged openmdao:indep_var. If False, list only outputs that are _not_ tagged openmdao:indep_var.

is_design_varbool or None

If None (the default), do no additional filtering of the inputs. If True, list only inputs connected to outputs that are driver design variables. If False, list only inputs _not_ connected to outputs that are driver design variables.

all_procsbool, optional

When True, display output on all processors. Default is False.

list_autoivcsbool

If True, include auto_ivc outputs in the listing. Defaults to False.

out_streamfile-like

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

print_minbool

When true, if the output value is an array, print its smallest value.

print_maxbool

When true, if the output value is an array, print its largest value.

return_formatstr

Indicates the desired format of the return value. Can have value of ‘list’ or ‘dict’. If ‘list’, the return value is a list of (name, metadata) tuples. if ‘dict’, the return value is a dictionary mapping {name: metadata}.

Returns:
list of (name, metadata) or dict of {name: metadata}

List or dict of output names and other optional information about those outputs.

Example#

In the following example, we create a model consisting of two instances of ImplicitComponent.

The implicit components are both instances of QuadraticComp, defined as shown here.

import openmdao.api as om


class QuadraticComp(om.ImplicitComponent):
    """
    A Simple Implicit Component representing a Quadratic Equation.

    R(a, b, c, x) = ax^2 + bx + c

    Solution via Quadratic Formula:
    x = (-b + sqrt(b^2 - 4ac)) / 2a
    """

    def setup(self):
        self.add_input('a', val=1., tags=['tag_a'])
        self.add_input('b', val=1.)
        self.add_input('c', val=1.)
        self.add_output('x', val=0., tags=['tag_x'])

    def setup_partials(self):
        self.declare_partials(of='*', wrt='*')

    def apply_nonlinear(self, inputs, outputs, residuals):
        a = inputs['a']
        b = inputs['b']
        c = inputs['c']
        x = outputs['x']
        residuals['x'] = a * x ** 2 + b * x + c

    def solve_nonlinear(self, inputs, outputs):
        a = inputs['a']
        b = inputs['b']
        c = inputs['c']
        outputs['x'] = (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)

These two components are placed in a Group with their common inputs promoted together.

import openmdao.api as om

group = om.Group()

sub = group.add_subsystem('sub', om.Group(), promotes_inputs=['a', 'b', 'c'])

sub.add_subsystem('comp1', QuadraticComp(), promotes_inputs=['a', 'b', 'c'])
sub.add_subsystem('comp2', QuadraticComp(), promotes_inputs=['a', 'b', 'c'])

global prob
prob = om.Problem(model=group)
prob.setup()

prob.set_val('a', 1.)
prob.set_val('b', -4.)
prob.set_val('c', 3.)
prob.run_model()

Usage#

List Inputs#

The list_inputs() method on a System will display all the inputs in execution order with their values. By default, the variable name and variable value are displayed. Also by default, the variables are displayed as part of the System hierarchy.

prob.model.list_inputs();
6 Input(s) in 'model'

varname  val    prom_name
-------  -----  ---------
sub
  comp1
    a    [1.]   a        
    b    [-4.]  b        
    c    [3.]   c        
  comp2
    a    [1.]   a        
    b    [-4.]  b        
    c    [3.]   c        

List Outputs#

The list_outputs() method will display all the outputs in execution order. There are many options to this method, which we will explore below. For this example, we will only display the value in addition to the name of the output variable.

prob.model.list_outputs();
0 Explicit Output(s) in 'model'


2 Implicit Output(s) in 'model'

varname  val   prom_name  
-------  ----  -----------
sub
  comp1
    x    [3.]  sub.comp1.x
  comp2
    x    [3.]  sub.comp2.x

List Implicit or Explicit Outputs#

Note that explicit and implicit outputs are listed separately. If you are only interested in seeing one or the other, you can exclude the ones you do not wish to see via the implicit and explicit arguments.

prob.model.list_outputs(implicit=False);
0 Explicit Output(s) in 'model'
prob.model.list_outputs(explicit=False);
2 Implicit Output(s) in 'model'

varname  val   prom_name  
-------  ----  -----------
sub
  comp1
    x    [3.]  sub.comp1.x
  comp2
    x    [3.]  sub.comp2.x

Get List via Return Value#

Both of these methods also return the information in the form of a list. You can disable the display of the information by setting the argument out_stream to None and then access the data instead via the return value.

# list inputs
inputs = prob.model.list_inputs(out_stream=None)

from pprint import pprint
pprint(sorted(inputs))
[('sub.comp1.a', {'prom_name': 'a', 'val': array([1.])}),
 ('sub.comp1.b', {'prom_name': 'b', 'val': array([-4.])}),
 ('sub.comp1.c', {'prom_name': 'c', 'val': array([3.])}),
 ('sub.comp2.a', {'prom_name': 'a', 'val': array([1.])}),
 ('sub.comp2.b', {'prom_name': 'b', 'val': array([-4.])}),
 ('sub.comp2.c', {'prom_name': 'c', 'val': array([3.])})]

List Names Only#

If you just want to see the names of the variables, you can disable the display of the values by setting the optional argument val to False.

prob.model.list_inputs(val=False);
6 Input(s) in 'model'

varname  prom_name
-------  ---------
sub
  comp1
    a    a        
    b    b        
    c    c        
  comp2
    a    a        
    b    b        
    c    c        

List Names and Promoted Name#

If you want the names of the variables and their promoted name within the model, you can enable the display of promoted names by setting the optional argument prom_name to True.

prob.model.list_outputs(prom_name=True);
0 Explicit Output(s) in 'model'


2 Implicit Output(s) in 'model'

varname  val   prom_name  
-------  ----  -----------
sub
  comp1
    x    [3.]  sub.comp1.x
  comp2
    x    [3.]  sub.comp2.x

List Variables Filtered by Name#

You can use the includes and excludes optional arguments to filter what variables are returned from System.list_inputs and System.list_outputs. Here are some short examples showing this feature.

prob.model.list_inputs(val=False, includes=['*comp2*',]);
3 Input(s) in 'model'

varname  prom_name
-------  ---------
sub
  comp2
    a    a        
    b    b        
    c    c        
prob.model.list_outputs(val=False, excludes=['*comp2*',]);
0 Explicit Output(s) in 'model'


1 Implicit Output(s) in 'model'

varname  prom_name  
-------  -----------
sub
  comp1
    x    sub.comp1.x

List Independent Variables and Design Variables#

The System.list_inputs method also provides a way to determine which inputs you are ultimately responsible for setting. The inputs report achieves this in a graphical format, but this method allows it to be done programmatically.

Consider the following simple example using the Sellar model, where we intentionally have not added the variable x as a design variable:

import numpy as np
import openmdao.api as om

from openmdao.test_suite.components.sellar_feature import SellarMDA


model = SellarMDA()

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)

prob = om.Problem(model)

prob.setup()
prob.final_setup();

The is_indep_var argument provides inputs that the user can ultimately change, though some of them maybe be overridden by the Driver as design variables:

indeps = model.list_inputs(is_indep_var=True, prom_name=True)
5 Input(s) in 'model'

varname  val    prom_name
-------  -----  ---------
cycle
  d1
    z    |0.0|  z        
    x    [0.]   x        
  d2
    z    |0.0|  z        
obj_cmp
  x      [0.]   x        
  z      |0.0|  z        

We can also get a list of design variables using the is_design_var argument:

desvars = model.list_inputs(is_design_var=True, prom_name=True)
3 Input(s) in 'model'

varname  val    prom_name
-------  -----  ---------
cycle
  d1
    z    |0.0|  z        
  d2
    z    |0.0|  z        
obj_cmp
  z      |0.0|  z        

Combining these two arguments will show those variables that should be set by the user and whose values will not be overridden by the Driver:

nonDV_indeps = model.list_inputs(is_indep_var=True, is_design_var=False, prom_name=True)
2 Input(s) in 'model'

varname  val   prom_name
-------  ----  ---------
cycle
  d1
    x    [0.]  x        
obj_cmp
  x      [0.]  x        

List Variables Filtered by Tags#

When you add inputs and outputs to components, you can optionally set tags on the variables. These tags can then be used to filter what variables are printed and returned by the System.list_inputs and System.list_outputs methods. Each of those methods has an optional argument tags for that purpose.

Here is a simple example to show you how this works. Imagine that a model-builder builds a model with some set of variables they expect other non-model-builder users to vary. They want to classify the inputs into two sets: “beginner” and “advanced”. The model-builder would like to write some functions that query the model for the set of basic and advanced inputs and do some stuff with those lists (like make fancy formatted outputs or something).

import openmdao.api as om

class ActuatorDiscWithTags(om.ExplicitComponent):
    """Simple wind turbine model based on actuator disc theory"""

    def setup(self):

        # Inputs
        self.add_input('a', 0.5, desc="Induced Velocity Factor", tags="advanced")
        self.add_input('Area', 10.0, units="m**2", desc="Rotor disc area", tags="basic")
        self.add_input('rho', 1.225, units="kg/m**3", desc="air density", tags="advanced")
        self.add_input('Vu', 10.0, units="m/s",
                       desc="Freestream air velocity, upstream of rotor", tags="basic")

        # Outputs
        self.add_output('Vr', 0.0, units="m/s",
                        desc="Air velocity at rotor exit plane")
        self.add_output('Vd', 0.0, units="m/s",
                        desc="Slipstream air velocity, downstream of rotor")
        self.add_output('Ct', 0.0, desc="Thrust Coefficient")
        self.add_output('thrust', 0.0, units="N",
                        desc="Thrust produced by the rotor")
        self.add_output('Cp', 0.0, desc="Power Coefficient")
        self.add_output('power', 0.0, units="W", desc="Power produced by the rotor")

    def setup_partials(self):
        self.declare_partials('Vr', ['a', 'Vu'])
        self.declare_partials('Vd', 'a')
        self.declare_partials('Ct', 'a')
        self.declare_partials('thrust', ['a', 'Area', 'rho', 'Vu'])
        self.declare_partials('Cp', 'a')
        self.declare_partials('power', ['a', 'Area', 'rho', 'Vu'])

    def compute(self, inputs, outputs):
        """ Considering the entire rotor as a single disc that extracts
        velocity uniformly from the incoming flow and converts it to
        power."""

        a = inputs['a']
        Vu = inputs['Vu']

        qA = .5 * inputs['rho'] * inputs['Area'] * Vu ** 2

        outputs['Vd'] = Vd = Vu * (1 - 2 * a)
        outputs['Vr'] = .5 * (Vu + Vd)

        outputs['Ct'] = Ct = 4 * a * (1 - a)
        outputs['thrust'] = Ct * qA

        outputs['Cp'] = Cp = Ct * (1 - a)
        outputs['power'] = Cp * qA * Vu

    def compute_partials(self, inputs, J):
        """ Jacobian of partial derivatives."""

        a = inputs['a']
        Vu = inputs['Vu']
        Area = inputs['Area']
        rho = inputs['rho']

        # pre-compute commonly needed quantities
        a_times_area = a * Area
        one_minus_a = 1.0 - a
        a_area_rho_vu = a_times_area * rho * Vu

        J['Vr', 'a'] = -Vu
        J['Vr', 'Vu'] = one_minus_a

        J['Vd', 'a'] = -2.0 * Vu

        J['Ct', 'a'] = 4.0 - 8.0 * a

        J['thrust', 'a'] = .5 * rho * Vu**2 * Area * J['Ct', 'a']
        J['thrust', 'Area'] = 2.0 * Vu**2 * a * rho * one_minus_a
        J['thrust', 'rho'] = 2.0 * a_times_area * Vu ** 2 * (one_minus_a)
        J['thrust', 'Vu'] = 4.0 * a_area_rho_vu * (one_minus_a)

        J['Cp', 'a'] = 4.0 * a * (2.0 * a - 2.0) + 4.0 * (one_minus_a)**2

        J['power', 'a'] = 2.0 * Area * Vu**3 * a * rho * (
        2.0 * a - 2.0) + 2.0 * Area * Vu**3 * rho * one_minus_a ** 2
        J['power', 'Area'] = 2.0 * Vu**3 * a * rho * one_minus_a ** 2
        J['power', 'rho'] = 2.0 * a_times_area * Vu ** 3 * (one_minus_a)**2
        J['power', 'Vu'] = 6.0 * Area * Vu**2 * a * rho * one_minus_a**2


# build the model
prob = om.Problem()
indeps = prob.model.add_subsystem('indeps', om.IndepVarComp(), promotes=['*'])
indeps.add_output('a', .5, tags="advanced")
indeps.add_output('Area', 10.0, units='m**2', tags="basic")
indeps.add_output('rho', 1.225, units='kg/m**3', tags="advanced")
indeps.add_output('Vu', 10.0, units='m/s', tags="basic")

prob.model.add_subsystem('a_disk', ActuatorDiscWithTags(),
                        promotes_inputs=['a', 'Area', 'rho', 'Vu'])

# setup the optimization
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'

prob.model.add_design_var('a', lower=0., upper=1.)
prob.model.add_objective('a_disk.Cp', scaler=-1)  # negated to maximize the objective

prob.setup()
prob.run_driver();
Optimization terminated successfully    (Exit mode 0)
            Current function value: -0.5925925906659251
            Iterations: 5
            Function evaluations: 6
            Gradient evaluations: 5
Optimization Complete
-----------------------------------
prob.model.list_inputs(tags='basic', units=True, shape=True);
2 Input(s) in 'model'

varname  val    units  shape  prom_name
-------  -----  -----  -----  ---------
a_disk
  Area   [10.]  m**2   (1,)   Area     
  Vu     [10.]  m/s    (1,)   Vu       
prob.model.list_inputs(tags=['basic','advanced'], units=True, shape=True);
4 Input(s) in 'model'

varname  val           units    shape  prom_name
-------  ------------  -------  -----  ---------
a_disk
  a      [0.33335528]  None     (1,)   a        
  Area   [10.]         m**2     (1,)   Area     
  rho    [1.225]       kg/m**3  (1,)   rho      
  Vu     [10.]         m/s      (1,)   Vu       
prob.model.list_outputs(tags='basic', units=False, shape=False);
2 Explicit Output(s) in 'model'

varname  val    prom_name
-------  -----  ---------
indeps
  Area   [10.]  Area     
  Vu     [10.]  Vu       


0 Implicit Output(s) in 'model'
prob.model.list_outputs(tags=['basic','advanced'], units=False, shape=False);
4 Explicit Output(s) in 'model'

varname  val           prom_name
-------  ------------  ---------
indeps
  a      [0.33335528]  a        
  Area   [10.]         Area     
  rho    [1.225]       rho      
  Vu     [10.]         Vu       


0 Implicit Output(s) in 'model'

Notice that if you only have one tag, you can set the argument tags to a string. If you have more than one tag, you use a list of strings.

This example showed how to add tags when using the add_input and add_output methods. You can also add tags to IndepVarComp and ExecComp variables using code like this:

comp = om.IndepVarComp('indep_var', tags='tag1')
ec = om.ExecComp('y=x+z+1.',
                 x={'val': 1.0, 'units': 'm', 'tags': 'tagx'},
                 y={'units': 'm', 'tags': ['tagy','tagm']},
                 z={'val': 2.0, 'tags': 'tagz'})

Note

Note that outputs of IndepVarComp are always tagged with openmdao:indep_var.

List Residuals Above a Tolerance#

In some cases, it might be convenient to only list variables whose residuals are above a given tolerance. The list_outputs method provides the optional argument residuals_tol for this purpose.

import openmdao.api as om
from openmdao.test_suite.components.sellar import SellarImplicitDis1, SellarImplicitDis2
prob = om.Problem()
model = prob.model

model.add_subsystem('p1', om.IndepVarComp('x', 1.0))
model.add_subsystem('d1', SellarImplicitDis1())
model.add_subsystem('d2', SellarImplicitDis2())
model.connect('d1.y1', 'd2.y1')
model.connect('d2.y2', 'd1.y2')

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

prob.setup()
prob.set_solver_print(level=-1)

prob.run_model()

outputs = model.list_outputs(residuals_tol=0.01, residuals=True)
0 Explicit Output(s) in 'model'


1 Implicit Output(s) in 'model'

varname  val          resids       prom_name
-------  -----------  -----------  ---------
d2
  y2     [0.2323774]  [0.0167747]  d2.y2    
print(outputs)
[('d2.y2', {'val': array([0.2323774]), 'prom_name': 'd2.y2', 'resids': array([0.0167747])})]

List Additional Variable Metadata#

The list_inputs() and list_outputs() methods have many options to also display units, shape, bounds (lower and upper), and scaling (res, res0, and res_ref) for the variables.

import openmdao.api as om

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

model.add_subsystem('p1', om.IndepVarComp('x', 12.0,
                                          lower=1.0, upper=100.0,
                                          ref=1.1, ref0=2.1,
                                          units='inch',
                                          ))
model.add_subsystem('p2', om.IndepVarComp('y', 1.0,
                                          lower=2.0, upper=200.0,
                                          ref=1.2, res_ref=2.2,
                                          units='ft',
                                          ))
model.add_subsystem('comp', om.ExecComp('z=x+y',
                                        x={'val': 0.0, 'units': 'inch'},
                                        y={'val': 0.0, 'units': 'inch'},
                                        z={'val': 0.0, 'units': 'inch'}))
model.connect('p1.x', 'comp.x')
model.connect('p2.y', 'comp.y')

prob.setup()
prob.set_solver_print(level=0)
prob.run_model()

inputs = prob.model.list_inputs(units=True)
2 Input(s) in 'model'

varname  val    units  prom_name
-------  -----  -----  ---------
comp
  x      [12.]  inch   comp.x   
  y      [12.]  inch   comp.y   
print(inputs)
[('comp.x', {'units': 'inch', 'prom_name': 'comp.x', 'val': array([12.])}), ('comp.y', {'units': 'inch', 'prom_name': 'comp.y', 'val': array([12.])})]
outputs = prob.model.list_outputs(implicit=False,
                                  val=True,
                                  units=True,
                                  shape=True,
                                  bounds=True,
                                  residuals=True,
                                  scaling=True,
                                  hierarchical=False,
                                  print_arrays=False)
3 Explicit Output(s) in 'model'

varnamevalresidsunitsshapelowerupperrefref0res_refprom_name
p1.x[12.][0.]inch(1,)[1.][100.]1.12.11.1p1.x
p2.y[1.][0.]ft(1,)[2.][200.]1.20.02.2p2.y
comp.z[24.][0.]inch(1,)1.00.01.0comp.z
from pprint import pprint
pprint(sorted(outputs))
[('comp.z',
  {'lower': None,
   'prom_name': 'comp.z',
   'ref': 1.0,
   'ref0': 0.0,
   'res_ref': 1.0,
   'resids': array([0.]),
   'shape': (1,),
   'units': 'inch',
   'upper': None,
   'val': array([24.])}),
 ('p1.x',
  {'lower': array([1.]),
   'prom_name': 'p1.x',
   'ref': 1.1,
   'ref0': 2.1,
   'res_ref': 1.1,
   'resids': array([0.]),
   'shape': (1,),
   'units': 'inch',
   'upper': array([100.]),
   'val': array([12.])}),
 ('p2.y',
  {'lower': array([2.]),
   'prom_name': 'p2.y',
   'ref': 1.2,
   'ref0': 0.0,
   'res_ref': 2.2,
   'resids': array([0.]),
   'shape': (1,),
   'units': 'ft',
   'upper': array([200.]),
   'val': array([1.])})]
prob.model.list_outputs(implicit=False,
                        val=True,
                        units=True,
                        shape=True,
                        bounds=True,
                        residuals=True,
                        scaling=True,
                        hierarchical=True,
                        print_arrays=False);
3 Explicit Output(s) in 'model'

varname  val    resids  units  shape  lower  upper   ref  ref0  res_ref  prom_name
-------  -----  ------  -----  -----  -----  ------  ---  ----  -------  ---------
p1
  x      [12.]  [0.]    inch   (1,)   [1.]   [100.]  1.1  2.1   1.1      p1.x     
p2
  y      [1.]   [0.]    ft     (1,)   [2.]   [200.]  1.2  0.0   2.2      p2.y     
comp
  z      [24.]  [0.]    inch   (1,)   None   None    1.0  0.0   1.0      comp.z   

List Global Shape#

When working with Distributed Variables, it may also be useful to display the global shape of a variable as well as the shape on the current processor. Note that this information is not available until after the model has been completely set up.

Note

This feature requires MPI, and may not be able to be run on Colab or Binder.

%%px

import numpy as np
import openmdao.api as om
from openmdao.test_suite.components.distributed_components import DistribComp, Summer
from openmdao.utils.array_utils import get_evenly_distributed_size

size = 15

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

indep = model.add_subsystem("indep", om.IndepVarComp())
indep.add_output('x', np.ones(get_evenly_distributed_size(prob.comm, size)), distributed=True)
model.add_subsystem("C2", DistribComp(size=size))
model.add_subsystem("C3", Summer(size=size))

model.connect('indep.x', 'C2.invec')
model.connect('C2.outvec', 'C3.invec', src_indices=om.slicer[:])

prob.setup()
prob.final_setup()

model.C2.list_inputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True);
model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True);
[stdout:0] 1 Input(s) in 'C2'
1 Explicit Output(s) in 'C2'
0 Implicit Output(s) in 'C2'
[output:0]

varnamevalshapeglobal_shapeprom_name
C2.invec[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.](4,)(15,)invec
[output:0]

varnamevalshapeglobal_shapeprom_name
C2.outvec[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.](4,)(15,)outvec

Note that the shape of the invec and outvec variables for the distributed C2 component can be different on each processor. Use the all_procs argument to display on all processors

%%px

prob.run_model()

model.C2.list_outputs(hierarchical=False, shape=True, global_shape=True, print_arrays=True, all_procs=True);
[stdout:0] 1 Explicit Output(s) in 'C2'
0 Implicit Output(s) in 'C2'
[stdout:1] 1 Explicit Output(s) in 'C2'
0 Implicit Output(s) in 'C2'
[stdout:2] 1 Explicit Output(s) in 'C2'
0 Implicit Output(s) in 'C2'
[stdout:3] 1 Explicit Output(s) in 'C2'
0 Implicit Output(s) in 'C2'
[output:0]

varnamevalshapeglobal_shapeprom_name
C2.outvec[ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.](4,)(15,)outvec
[output:1]

varnamevalshapeglobal_shapeprom_name
C2.outvec[ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.](4,)(15,)outvec
[output:2]

varnamevalshapeglobal_shapeprom_name
C2.outvec[ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.](4,)(15,)outvec
[output:3]

varnamevalshapeglobal_shapeprom_name
C2.outvec[ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.](3,)(15,)outvec

Listing Problem Variables#

The Problem class has a method list_driver_vars which prints out the values and metadata for design, constraint, and objective variables.

Problem.list_driver_vars(show_promoted_name=True, print_arrays=False, driver_scaling=True, desvar_opts=[], cons_opts=[], objs_opts=[], out_stream=DEFAULT_OUT_STREAM)[source]

Print all design variables and responses (objectives and constraints).

Parameters:
show_promoted_namebool

If True, then show the promoted names of the variables.

print_arraysbool, optional

When False, in the columnar display, just display norm of any ndarrays with size > 1. The norm is surrounded by vertical bars to indicate that it is a norm. When True, also display full values of the ndarray below the row. Format is affected by the values set with numpy.set_printoptions. Default is False.

driver_scalingbool, optional

When True, return values that are scaled according to either the adder and scaler or the ref and ref0 values that were specified when add_design_var, add_objective, and add_constraint were called on the model. Default is True.

desvar_optslist of str

List of optional columns to be displayed in the desvars table. Allowed values are: [‘lower’, ‘upper’, ‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘parallel_deriv_color’, ‘cache_linear_solution’, ‘units’, ‘min’, ‘max’].

cons_optslist of str

List of optional columns to be displayed in the cons table. Allowed values are: [‘lower’, ‘upper’, ‘equals’, ‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘linear’, ‘parallel_deriv_color’, ‘cache_linear_solution’, ‘units’, ‘min’, ‘max’].

objs_optslist of str

List of optional columns to be displayed in the objs table. Allowed values are: [‘ref’, ‘ref0’, ‘indices’, ‘adder’, ‘scaler’, ‘units’, ‘parallel_deriv_color’, ‘cache_linear_solution’].

out_streamfile-like object

Where to send human readable output. Default is sys.stdout. Set to None to suppress.

Returns:
dict

Name, size, val, and other requested parameters of design variables, constraints, and objectives.

You can optionally print out a variety of metadata. In this example, all the metadata is printed. The print_arrays option is also set to true so that full array values are printed and min and max are used so that the array’s lowest and highest values are shown.

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

prob = om.Problem(model=SellarDerivatives())
model = prob.model
model.nonlinear_solver = om.NonlinearBlockGS()
model.linear_solver = om.ScipyKrylov()

prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['tol'] = 1e-9

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)

prob.setup()
prob.run_driver();
NL: NLBGS Converged in 8 iterations
NL: NLBGS Converged in 1 iterations
NL: NLBGS Converged in 9 iterations
NL: NLBGS Converged in 10 iterations
NL: NLBGS Converged in 10 iterations
NL: NLBGS Converged in 9 iterations
NL: NLBGS Converged in 6 iterations
Optimization terminated successfully    (Exit mode 0)
            Current function value: 3.183393951728078
            Iterations: 6
            Function evaluations: 6
            Gradient evaluations: 6
Optimization Complete
-----------------------------------
prob.list_driver_vars(print_arrays=True,
                      desvar_opts=['lower', 'upper', 'ref', 'ref0',
                                   'indices', 'adder', 'scaler',
                                   'parallel_deriv_color', 'min', 'max'],
                      cons_opts=['lower', 'upper', 'equals', 'ref', 'ref0',
                                 'indices', 'adder', 'scaler', 'linear', 'min', 'max'],
                      objs_opts=['ref', 'ref0',
                                 'indices', 'adder', 'scaler',
                                 'parallel_deriv_color',
                                 'cache_linear_solution'])
----------------
Design Variables
----------------
name  val               size  lower   upper          ref   ref0  indices  adder  scaler  parallel_deriv_color  min  max         
----  ----------------  ----  ------  -------------  ----  ----  -------  -----  ------  --------------------  ---  ---------- 
z     |1.97763888|      2     |10.0|  |14.14213562|  None  None  None     None   None    None                  0.0  1.97763888  
      val:
      array([1.97763888, 0.        ])

      lower:
      array([-10.,   0.])

      upper:
      array([10., 10.])

x     [3.56410563e-15]  1     0.0     10.0           None  None  None     None   None    None                  0.0  0.0         

-----------
Constraints
-----------
name  val                size  lower   upper  equals  ref   ref0  indices  adder  scaler  linear  min           max           
----  -----------------  ----  ------  -----  ------  ----  ----  -------  -----  ------  ------  ------------  ------------ 
con1  [-8.86162255e-11]  1     -1e+30  0.0    None    None  None  None     None   None    False   -0.0          -0.0          
con2  [-20.24472223]     1     -1e+30  0.0    None    None  None  None     None   None    False   -20.24472223  -20.24472223  

----------
Objectives
----------
name  val           size  ref   ref0  indices  adder  scaler  parallel_deriv_color  cache_linear_solution  
----  ------------  ----  ----  ----  -------  -----  ------  --------------------  --------------------- 
obj   [3.18339395]  1     None  None  None     None   None    None                  False                  
{'design_vars': [('z',
   {'adder': None,
    'scaler': None,
    'name': 'z',
    'upper': array([10., 10.]),
    'lower': array([-10.,   0.]),
    'ref': None,
    'ref0': None,
    'indices': None,
    'parallel_deriv_color': None,
    'size': 2,
    'val': array([1.97763888, 0.        ])}),
  ('x',
   {'adder': None,
    'scaler': None,
    'name': 'x',
    'upper': 10.0,
    'lower': 0.0,
    'ref': None,
    'ref0': None,
    'indices': None,
    'parallel_deriv_color': None,
    'size': 1,
    'val': array([3.56410563e-15])})],
 'constraints': [('con1',
   {'name': 'con1',
    'lower': -1e+30,
    'upper': 0.0,
    'equals': None,
    'linear': False,
    'indices': None,
    'scaler': None,
    'adder': None,
    'ref': None,
    'ref0': None,
    'size': 1,
    'val': array([-8.86162255e-11])}),
  ('con2',
   {'name': 'con2',
    'lower': -1e+30,
    'upper': 0.0,
    'equals': None,
    'linear': False,
    'indices': None,
    'scaler': None,
    'adder': None,
    'ref': None,
    'ref0': None,
    'size': 1,
    'val': array([-20.24472223])})],
 'objectives': [('obj',
   {'name': 'obj',
    'indices': None,
    'scaler': None,
    'adder': None,
    'ref': None,
    'ref0': None,
    'cache_linear_solution': False,
    'parallel_deriv_color': None,
    'size': 1,
    'val': array([3.18339395])})]}