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 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.
SellarImplicitDis1
class definition
class SellarImplicitDis1(om.ImplicitComponent):
"""
Component containing Discipline 1 -- no derivatives version.
"""
def __init__(self, units=None, scaling=None):
super().__init__()
self.execution_count = 0
self._units = units
self._do_scaling = scaling
def setup(self):
if self._units:
units = 'ft'
else:
units = None
if self._do_scaling is None:
ref = 1.
else:
ref = .1
# Global Design Variable
self.add_input('z', val=np.zeros(2), units=units)
# Local Design Variable
self.add_input('x', val=0., units=units)
# Coupling parameter
self.add_input('y2', val=1.0, units=units)
# Coupling output
self.add_output('y1', val=1.0, lower=-0.1, upper=1000, units=units, ref=ref)
def setup_partials(self):
# Derivatives
self.declare_partials('*', '*')
def apply_nonlinear(self, inputs, outputs, resids):
"""
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']
y1 = outputs['y1']
resids['y1'] = -(z1**2 + z2 + x1 - 0.2*y2 - y1)
def linearize(self, inputs, outputs, J):
"""
Jacobian for Sellar discipline 1.
"""
J['y1', 'y2'] = 0.2
J['y1', 'z'] = -np.array([[2.0 * inputs['z'][0], 1.0]])
J['y1', 'x'] = -1.0
J['y1', 'y1'] = 1.0
SellarImplicitDis2
class definition
class SellarImplicitDis2(om.ImplicitComponent):
"""
Component containing Discipline 2 -- implicit version.
"""
def __init__(self, units=None, scaling=None):
super().__init__()
self.execution_count = 0
self._units = units
self._do_scaling = scaling
def setup(self):
if self._units:
units = 'inch'
else:
units = None
if self._do_scaling is None:
ref = 1.0
else:
ref = .18
# Global Design Variable
self.add_input('z', val=np.zeros(2), units=units)
# Coupling parameter
self.add_input('y1', val=1.0, units=units)
# Coupling output
self.add_output('y2', val=1.0, lower=0.1, upper=1000., units=units, ref=ref)
def setup_partials(self):
# Derivatives
self.declare_partials('*', '*')
def apply_nonlinear(self, inputs, outputs, resids):
"""
Evaluates the equation
y2 = y1**(.5) + z1 + z2
"""
z1 = inputs['z'][0]
z2 = inputs['z'][1]
y1 = inputs['y1'].copy()
y2 = outputs['y2']
# 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
resids['y2'] = -(y1**.5 + z1 + z2 - y2)
def linearize(self, inputs, outputs, J):
"""
Jacobian for Sellar discipline 2.
"""
y1 = inputs['y1']
if y1.real < 0.0:
y1 *= -1
if y1.real < 1e-8:
y1 = 1e-8
J['y2', 'y1'] = -.5*y1**-.5
J['y2', 'z'] = -np.array([[1.0, 1.0]])
J['y2', 'y2'] = 1.0
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'
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,) | 1.0 | 0.0 | 1.0 | comp.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
Print Array Values#
The list_inputs()
and list_outputs()
methods both have a print_arrays
option. By default, this option is set to False and only the norm of the array will appear in the tabular display. The norm value is surrounded by vertical bars to indicate that it is a norm. When the option is set to True, the complete value of the array will also be a displayed below the row.
import numpy as np
import openmdao.api as om
class ArrayAdder(om.ExplicitComponent):
"""
Just a simple component that has array inputs and outputs
"""
def __init__(self, size):
super().__init__()
self.size = size
def setup(self):
self.add_input('x', val=np.zeros(self.size), units='inch')
self.add_output('y', val=np.zeros(self.size), units='ft')
def compute(self, inputs, outputs):
outputs['y'] = inputs['x'] + 10.0
size = 30
prob = om.Problem()
prob.model.add_subsystem('des_vars', om.IndepVarComp('x', np.ones(size), units='inch'),
promotes=['x'])
prob.model.add_subsystem('mult', ArrayAdder(size), promotes=['x', 'y'])
prob.setup()
prob['x'] = np.arange(size)
prob.run_driver()
prob.model.list_inputs(val=True,
units=True,
hierarchical=True,
print_arrays=True);
1 Input(s) in 'model'
varname val units prom_name
------- ------------------- ----- ---------
mult
x |92.493243| inch x
val:
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,
26., 27., 28., 29.])
prob.model.list_outputs(val=True,
implicit=False,
units=True,
shape=True,
bounds=True,
residuals=True,
scaling=True,
hierarchical=True,
print_arrays=True);
2 Explicit Output(s) in 'model'
varname val resids units shape lower upper ref ref0 res_ref prom_name
-------- -------------------- ------ ----- ----- ----- ----- --- ---- ------- ---------
des_vars
x |92.493243| |0.0| inch (30,) None None 1.0 0.0 1.0 x
val:
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19., 20., 21., 22., 23., 24., 25.,
26., 27., 28., 29.])
resids:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
mult
y |142.32006183| |0.0| ft (30,) None None 1.0 0.0 1.0 y
val:
array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22.,
23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35.,
36., 37., 38., 39.])
resids:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
You can control the format of the array values via numpy.set_printoptions
. OpenMDAO provides the printoptions
context manager to assist with this.
from openmdao.utils.general_utils import printoptions
with printoptions(edgeitems=3, infstr='inf',
linewidth=75, nanstr='nan', precision=8,
suppress=False, threshold=1000, formatter=None):
prob.model.list_outputs(val=True,
implicit=False,
units=True,
shape=True,
bounds=True,
residuals=True,
scaling=True,
hierarchical=False,
print_arrays=True)
2 Explicit Output(s) in 'model'
varname | val | resids | units | shape | lower | upper | ref | ref0 | res_ref | prom_name |
---|---|---|---|---|---|---|---|---|---|---|
des_vars.x | [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.] | [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] | inch | (30,) | 1.0 | 0.0 | 1.0 | x | ||
mult.y | [10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.] | [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] | ft | (30,) | 1.0 | 0.0 | 1.0 | y |
Print Minimum or Maximum Array Values#
When working with large arrays, it can be difficult to determine how the array is interacting with the upper and lower bounds by looking through the output of the entire contents. To provide a quick visual reference, the list_inputs()
and list_outputs()
methods have print_min
and print_max
options that output columns with the minimum and maximum values of the array.
prob.model.list_inputs(val=True,
units=True,
hierarchical=True,
print_min=True,
print_max=True);
1 Input(s) in 'model'
varname val units prom_name min max
------- ------------------- ----- --------- --- ----
mult
x |92.493243| inch x 0.0 29.0
prob.model.list_outputs(val=True,
implicit=False,
units=True,
shape=True,
bounds=True,
residuals=True,
scaling=True,
hierarchical=True,
print_min=True,
print_max=True);
2 Explicit Output(s) in 'model'
varname val resids units shape lower upper ref ref0 res_ref prom_name min max
-------- -------------------- ------ ----- ----- ----- ----- --- ---- ------- --------- ---- ----
des_vars
x |92.493243| |0.0| inch (30,) None None 1.0 0.0 1.0 x 0.0 29.0
mult
y |142.32006183| |0.0| ft (30,) None None 1.0 0.0 1.0 y 10.0 39.0
Note that it is normally required to run the model before list_inputs()
and list_outputs()
can be used. This is because the final setup that occurs just before execution determines the hierarchy and builds the data structures and connections. In some cases however, it can be useful to call these functions on a system prior to execution to assist in configuring your model. At configure
time, basic metadata about a system’s inputs and outputs is available.
See the documentation for the configure method for one such use case.
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.
DistribComp
class definition
class DistribComp(om.ExplicitComponent):
"""Simple Distributed Component."""
def initialize(self):
self.options.declare('size', types=int, default=1,
desc="Size of input and output vectors.")
def setup(self):
comm = self.comm
rank = comm.rank
size = self.options['size']
# if comm.size is 2 and size is 15, this results in
# 8 entries for proc 0 and 7 entries for proc 1
sizes, _ = evenly_distrib_idxs(comm.size, size)
mysize = sizes[rank]
self.add_input('invec', np.ones(mysize, float), distributed=True)
self.add_output('outvec', np.ones(mysize, float), distributed=True,)
def compute(self, inputs, outputs):
if self.comm.rank == 0:
outputs['outvec'] = inputs['invec'] * 2.0
else:
outputs['outvec'] = inputs['invec'] * -3.0
Summer
class definition
class Summer(om.ExplicitComponent):
"""Sums an input array."""
def initialize(self):
self.options.declare('size', types=int, default=1,
desc="Size of input and output vectors.")
def setup(self):
self.add_input('invec', np.ones(self.options['size'], float))
self.add_output('sum', 0.0, shape=1)
def compute(self, inputs, outputs):
outputs['sum'] = np.sum(inputs['invec'])
%%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]
varname | val | shape | global_shape | prom_name |
---|---|---|---|---|
C2.invec | [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] | (4,) | (15,) | invec |
[output:0]
varname | val | shape | global_shape | prom_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]
varname | val | shape | global_shape | prom_name |
---|---|---|---|---|
C2.outvec | [ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.] | (4,) | (15,) | outvec |
[output:1]
varname | val | shape | global_shape | prom_name |
---|---|---|---|---|
C2.outvec | [ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.] | (4,) | (15,) | outvec |
[output:2]
varname | val | shape | global_shape | prom_name |
---|---|---|---|---|
C2.outvec | [ 2. 2. 2. 2. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3. -3.] | (4,) | (15,) | outvec |
[output:3]
varname | val | shape | global_shape | prom_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.
SellarDerivatives
class definition
class SellarDerivatives(om.Group):
"""
Group containing the Sellar MDA. This version uses the disciplines with derivatives.
"""
def setup(self):
self.add_subsystem('d1', SellarDis1withDerivatives(), promotes=['x', 'z', 'y1', 'y2'])
self.add_subsystem('d2', SellarDis2withDerivatives(), promotes=['z', 'y1', 'y2'])
self.add_subsystem('obj_cmp', om.ExecComp('obj = x**2 + z[1] + y1 + exp(-y2)', obj=0.0,
x=0.0, z=np.array([0.0, 0.0]), y1=0.0, y2=0.0),
promotes=['obj', 'x', 'z', 'y1', 'y2'])
self.add_subsystem('con_cmp1', om.ExecComp('con1 = 3.16 - y1', con1=0.0, y1=0.0),
promotes=['con1', 'y1'])
self.add_subsystem('con_cmp2', om.ExecComp('con2 = y2 - 24.0', con2=0.0, y2=0.0),
promotes=['con2', 'y2'])
self.set_input_defaults('x', 1.0)
self.set_input_defaults('z', np.array([5.0, 2.0]))
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])})]}