diff --git a/openmdao/components/submodel_comp.py b/openmdao/components/submodel_comp.py index f8535eb4d6..f00687a6b7 100644 --- a/openmdao/components/submodel_comp.py +++ b/openmdao/components/submodel_comp.py @@ -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', diff --git a/openmdao/core/problem.py b/openmdao/core/problem.py index 78d1334b69..f3a9bab066 100644 --- a/openmdao/core/problem.py +++ b/openmdao/core/problem.py @@ -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 @@ -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()`.") diff --git a/openmdao/core/tests/test_check_totals.py b/openmdao/core/tests/test_check_totals.py index d91d9ef20a..5a53268520 100644 --- a/openmdao/core/tests/test_check_totals.py +++ b/openmdao/core/tests/test_check_totals.py @@ -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 diff --git a/openmdao/core/tests/test_driver.py b/openmdao/core/tests/test_driver.py index 6c4cbb5ddc..bf18658dc9 100644 --- a/openmdao/core/tests/test_driver.py +++ b/openmdao/core/tests/test_driver.py @@ -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') @@ -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') diff --git a/openmdao/core/tests/test_problem.py b/openmdao/core/tests/test_problem.py index c223327b37..022f0e6326 100644 --- a/openmdao/core/tests/test_problem.py +++ b/openmdao/core/tests/test_problem.py @@ -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() @@ -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') @@ -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', @@ -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', @@ -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): @@ -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 @@ -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') @@ -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') @@ -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 @@ -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'], diff --git a/openmdao/drivers/tests/test_doe_driver.py b/openmdao/drivers/tests/test_doe_driver.py index 36ead070c9..9c82b544a8 100644 --- a/openmdao/drivers/tests/test_doe_driver.py +++ b/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 @@ -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 @@ -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() @@ -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() @@ -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.")