Skip to content

Commit

Permalink
Merge f5a0598 into 6b42683
Browse files Browse the repository at this point in the history
  • Loading branch information
robfalck committed Feb 26, 2024
2 parents 6b42683 + f5a0598 commit 3b68991
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 22 deletions.
2 changes: 1 addition & 1 deletion openmdao/components/submodel_comp.py
Expand Up @@ -305,7 +305,7 @@ def setup(self):
# NOTE to be looked at later. Trying to get variables from subsystems has been causing
# issues and is a goal for a future version
#
# driver_vars = p.list_problem_vars(out_stream=None,
# driver_vars = p.list_driver_vars(out_stream=None,
# desvar_opts = ['lower', 'upper', 'ref', 'ref0',
# 'indices', 'adder', 'scaler',
# 'parallel_deriv_color',
Expand Down
63 changes: 62 additions & 1 deletion openmdao/core/problem.py
Expand Up @@ -2019,6 +2019,67 @@ def list_problem_vars(self,
"""
Print all design variables and responses (objectives and constraints).
Parameters
----------
show_promoted_name : bool
If True, then show the promoted names of the variables.
print_arrays : bool, 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_scaling : bool, 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_opts : list 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_opts : list 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_opts : list 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_stream : file-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.
"""
warn_deprecation(msg='Method `list_problem_vars` has been renamed `list_driver_vars`.\n'
'Please update your code to use list_driver_vars to avoid this warning.')
self.list_driver_vars(show_promoted_name=show_promoted_name,
print_arrays=print_arrays,
driver_scaling=driver_scaling,
desvar_opts=desvar_opts,
cons_opts=cons_opts,
objs_opts=objs_opts,
out_stream=out_stream)

def list_driver_vars(self,
show_promoted_name=True,
print_arrays=False,
driver_scaling=True,
desvar_opts=[],
cons_opts=[],
objs_opts=[],
out_stream=_DEFAULT_OUT_STREAM
):
"""
Print all design variables and responses (objectives and constraints).
Parameters
----------
show_promoted_name : bool
Expand Down Expand Up @@ -2059,7 +2120,7 @@ def list_problem_vars(self,
and objectives.
"""
if self._metadata['setup_status'] < _SetupStatus.POST_FINAL_SETUP:
raise RuntimeError(f"{self.msginfo}: Problem.list_problem_vars() cannot be called "
raise RuntimeError(f"{self.msginfo}: Problem.list_driver_vars() cannot be called "
"before `Problem.run_model()`, `Problem.run_driver()`, or "
"`Problem.final_setup()`.")

Expand Down
4 changes: 2 additions & 2 deletions openmdao/core/tests/test_check_totals.py
Expand Up @@ -1289,8 +1289,8 @@ def test_alias_constraints(self):
assert_near_equal(totals['a3', 'p1.widths']['abs error'][0], 0.0, 1e-6)
assert_near_equal(totals['a4', 'p1.widths']['abs error'][0], 0.0, 1e-6)

l = prob.list_problem_vars(show_promoted_name=True, print_arrays=False,
cons_opts=['indices', 'alias'])
l = prob.list_driver_vars(show_promoted_name=True, print_arrays=False,
cons_opts=['indices', 'alias'])

# Rev mode

Expand Down
4 changes: 2 additions & 2 deletions openmdao/core/tests/test_driver.py
Expand Up @@ -527,7 +527,7 @@ def test_units_basic(self):
strout = StringIO()
sys.stdout = strout
try:
prob.list_problem_vars(desvar_opts=['units'], objs_opts=['units'], cons_opts=['units'])
prob.list_driver_vars(desvar_opts=['units'], objs_opts=['units'], cons_opts=['units'])
finally:
sys.stdout = stdout
output = strout.getvalue().split('\n')
Expand Down Expand Up @@ -636,7 +636,7 @@ def test_units_with_scaling(self):
strout = StringIO()
sys.stdout = strout
try:
prob.list_problem_vars(desvar_opts=['units'], objs_opts=['units'], cons_opts=['units'])
prob.list_driver_vars(desvar_opts=['units'], objs_opts=['units'], cons_opts=['units'])
finally:
sys.stdout = stdout
output = strout.getvalue().split('\n')
Expand Down
22 changes: 11 additions & 11 deletions openmdao/core/tests/test_problem.py
Expand Up @@ -1679,7 +1679,7 @@ def hook_func(prob):
hooks._unregister_hook('final_setup', class_name='Problem')
hooks.use_hooks = False

def test_list_problem_vars(self):
def test_list_driver_vars(self):
model = SellarDerivatives()
model.nonlinear_solver = om.NonlinearBlockGS()

Expand Down Expand Up @@ -1718,7 +1718,7 @@ def test_list_problem_vars(self):
strout = StringIO()
sys.stdout = strout
try:
prob.list_problem_vars(show_promoted_name=False)
prob.list_driver_vars(show_promoted_name=False)
finally:
sys.stdout = stdout
output = strout.getvalue().split('\n')
Expand All @@ -1731,7 +1731,7 @@ def test_list_problem_vars(self):
strout = StringIO()
sys.stdout = strout
try:
prob.list_problem_vars(
prob.list_driver_vars(
desvar_opts=['lower', 'upper', 'ref', 'ref0',
'indices', 'adder', 'scaler',
'parallel_deriv_color',
Expand Down Expand Up @@ -1761,7 +1761,7 @@ def test_list_problem_vars(self):
strout = StringIO()
sys.stdout = strout
try:
l = prob.list_problem_vars(print_arrays=True,
l = prob.list_driver_vars(print_arrays=True,
desvar_opts=['lower', 'upper', 'ref', 'ref0',
'indices', 'adder', 'scaler',
'parallel_deriv_color',
Expand Down Expand Up @@ -1835,11 +1835,11 @@ def test_list_problem_vars_before_final_setup(self):

prob.setup()

msg = f"Problem {prob._get_inst_id()}: Problem.list_problem_vars() cannot be called " \
msg = f"Problem {prob._get_inst_id()}: Problem.list_driver_vars() cannot be called " \
"before `Problem.run_model()`, `Problem.run_driver()`, or `Problem.final_setup()`."

with self.assertRaises(RuntimeError) as err:
prob.list_problem_vars()
prob.list_driver_vars()
self.assertEqual(str(err.exception), msg)

def test_list_problem_w_multi_constraints(self):
Expand Down Expand Up @@ -1872,7 +1872,7 @@ def test_list_problem_w_multi_constraints(self):
strout = StringIO()
sys.stdout = strout
try:
p.list_problem_vars()
p.list_driver_vars()
finally:
sys.stdout = stdout

Expand Down Expand Up @@ -1972,7 +1972,7 @@ def test_list_problem_vars_driver_scaling(self):
sys.stdout = strout

try:
prob.list_problem_vars()
prob.list_driver_vars()
finally:
sys.stdout = stdout
output = strout.getvalue().split('\n')
Expand All @@ -1987,7 +1987,7 @@ def test_list_problem_vars_driver_scaling(self):
sys.stdout = strout

try:
prob.list_problem_vars(driver_scaling=False)
prob.list_driver_vars(driver_scaling=False)
finally:
sys.stdout = stdout
output = strout.getvalue().split('\n')
Expand All @@ -1996,7 +1996,7 @@ def test_list_problem_vars_driver_scaling(self):
self.assertTrue('-20.' in output[14]) # con
self.assertTrue('3.18' in output[21]) # obj

def test_feature_list_problem_vars(self):
def test_feature_list_driver_vars(self):

prob = om.Problem(model=SellarDerivatives())
model = prob.model
Expand All @@ -2015,7 +2015,7 @@ def test_feature_list_problem_vars(self):
prob.setup()
prob.run_driver()

prob.list_problem_vars(print_arrays=True,
prob.list_driver_vars(print_arrays=True,
desvar_opts=['lower', 'upper', 'ref', 'ref0',
'indices', 'adder', 'scaler',
'parallel_deriv_color'],
Expand Down
27 changes: 22 additions & 5 deletions openmdao/drivers/tests/test_doe_driver.py
@@ -1,6 +1,8 @@
"""
Test DOE Driver and Generators.
"""
from ast import Str
from io import StringIO
import unittest

import os
Expand All @@ -16,8 +18,9 @@
from openmdao.test_suite.components.paraboloid_distributed import DistParab
from openmdao.test_suite.groups.parallel_groups import FanInGrouped

from openmdao.utils.assert_utils import assert_near_equal
from openmdao.utils.assert_utils import assert_near_equal, assert_warning
from openmdao.utils.general_utils import run_driver, printoptions
from openmdao.utils.om_warnings import OMDeprecationWarning
from openmdao.utils.testing_utils import use_tempdirs

from openmdao.utils.mpi import MPI
Expand Down Expand Up @@ -1562,7 +1565,7 @@ def test_derivative_no_recording(self):
@use_tempdirs
class TestDOEDriverListVars(unittest.TestCase):

def test_list_problem_vars(self):
def test_list_driver_vars(self):
# this passes if no exception is raised

prob = om.Problem()
Expand Down Expand Up @@ -1593,13 +1596,13 @@ def test_list_problem_vars(self):
prob.run_driver()
prob.cleanup()

prob.list_problem_vars()
prob.list_driver_vars()


@use_tempdirs
class TestDOEDriverListVars(unittest.TestCase):

def test_list_problem_vars(self):
def test_list_driver_vars(self):
# this passes if no exception is raised

prob = om.Problem()
Expand Down Expand Up @@ -1630,7 +1633,21 @@ def test_list_problem_vars(self):
prob.run_driver()
prob.cleanup()

prob.list_problem_vars()
f = StringIO()
prob.list_driver_vars(out_stream=f)
output = f.getvalue()

self.assertIn('x -1 0', output)
self.assertIn('y 3 0', output)
self.assertIn('f_xy 59 0', output)

expected_warning = 'Method `list_problem_vars` has been ' \
'renamed `list_driver_vars`.\nPlease update ' \
'your code to use list_driver_vars to avoid ' \
'this warning.'

with assert_warning(OMDeprecationWarning, expected_warning):
prob.list_problem_vars()


@unittest.skipUnless(MPI and PETScVector, "MPI and PETSc are required.")
Expand Down

0 comments on commit 3b68991

Please sign in to comment.