From db3eafc4d624afcbae4bfdc463a301c68bc61a0c Mon Sep 17 00:00:00 2001 From: swryan Date: Fri, 15 Mar 2024 14:55:47 -0400 Subject: [PATCH] return value from deprecated list_problem_vars() --- openmdao/components/tests/test_exec_comp.py | 5 +-- openmdao/core/problem.py | 14 ++++----- openmdao/core/tests/test_problem.py | 35 ++++++++++++++++++--- openmdao/drivers/tests/test_doe_driver.py | 13 ++------ 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/openmdao/components/tests/test_exec_comp.py b/openmdao/components/tests/test_exec_comp.py index 0054234dfc..6a38eb9831 100644 --- a/openmdao/components/tests/test_exec_comp.py +++ b/openmdao/components/tests/test_exec_comp.py @@ -22,7 +22,7 @@ from openmdao.utils.assert_utils import assert_near_equal, assert_check_partials, assert_warning from openmdao.utils.general_utils import env_truthy from openmdao.utils.testing_utils import force_check_partials -from openmdao.utils.om_warnings import OMDeprecationWarning, SetupWarning +from openmdao.utils.om_warnings import SetupWarning _ufunc_test_data = { 'min': { @@ -1975,7 +1975,8 @@ def test_exec_comp_jac(self, f): for comp in cpd: for (var, wrt) in cpd[comp]: - np.testing.assert_almost_equal(cpd[comp][var, wrt]['abs error'][0], 0, decimal=4) + np.testing.assert_almost_equal(cpd[comp][var, wrt]['abs error'][0], 0, decimal=4, + verbose=True, err_msg=f"{test_data['args']=}") if __name__ == "__main__": diff --git a/openmdao/core/problem.py b/openmdao/core/problem.py index d96db0aedd..d59babc145 100644 --- a/openmdao/core/problem.py +++ b/openmdao/core/problem.py @@ -2030,13 +2030,13 @@ def list_problem_vars(self, """ 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) + return 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, diff --git a/openmdao/core/tests/test_problem.py b/openmdao/core/tests/test_problem.py index eb2cd7a86d..c1a493f49b 100644 --- a/openmdao/core/tests/test_problem.py +++ b/openmdao/core/tests/test_problem.py @@ -6,7 +6,6 @@ from io import StringIO import numpy as np -from collections import defaultdict import openmdao.api as om from openmdao.core.problem import _default_prob_name @@ -17,7 +16,7 @@ from openmdao.utils.assert_utils import assert_near_equal, assert_warning, assert_check_totals import openmdao.utils.hooks as hooks from openmdao.utils.units import convert_units -from openmdao.utils.om_warnings import DerivativesWarning +from openmdao.utils.om_warnings import DerivativesWarning, OMDeprecationWarning from openmdao.utils.testing_utils import use_tempdirs from openmdao.utils.tests.test_hooks import hooks_active @@ -1690,7 +1689,7 @@ def test_list_driver_vars(self): strout = StringIO() sys.stdout = strout try: - prob.list_problem_vars() + prob.list_driver_vars() finally: sys.stdout = stdout output = strout.getvalue().split('\n') @@ -1806,7 +1805,33 @@ def test_list_driver_vars(self): self.assertEqual(l['objectives'][0][1]['scaler'], None) self.assertEqual(l['objectives'][0][1]['adder'], None) - def test_list_problem_vars_before_final_setup(self): + def test_list_problem_vars_deprecated(self): + model = SellarDerivatives() + model.nonlinear_solver = om.NonlinearBlockGS() + + prob = om.Problem(model) + + model.add_design_var('z', lower=np.array([-10.0, 0.0]), upper=np.array([10.0, 10.0])) + model.add_design_var('x', lower=0.0, upper=10.0) + model.add_objective('obj') + model.add_constraint('con1', upper=0.0) + model.add_constraint('con2', upper=0.0) + + prob.setup() + prob.run_driver() + + 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_vars = prob.list_problem_vars(out_stream=None) + + # make sure the deprecated function still returns tha data + self.assertEqual(set(prob_vars.keys()), {'constraints', 'design_vars', 'objectives'}) + + def test_list_driver_vars_before_final_setup(self): prob = om.Problem() prob.model.add_subsystem('parab', Paraboloid(), promotes_inputs=['x', 'y']) prob.model.add_subsystem('const', om.ExecComp('g = x + y'), promotes_inputs=['x', 'y']) @@ -1935,7 +1960,7 @@ def test_constraint_slice_with_negative_as_indices(self): p.run_model() - def test_list_problem_vars_driver_scaling(self): + def test_list_driver_vars_driver_scaling(self): model = SellarDerivatives() model.nonlinear_solver = om.NonlinearBlockGS() diff --git a/openmdao/drivers/tests/test_doe_driver.py b/openmdao/drivers/tests/test_doe_driver.py index 9a0b04900b..85fbca78c5 100644 --- a/openmdao/drivers/tests/test_doe_driver.py +++ b/openmdao/drivers/tests/test_doe_driver.py @@ -17,9 +17,8 @@ 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, assert_warning -from openmdao.utils.general_utils import run_driver, printoptions -from openmdao.utils.om_warnings import OMDeprecationWarning +from openmdao.utils.assert_utils import assert_near_equal +from openmdao.utils.general_utils import run_driver from openmdao.utils.testing_utils import use_tempdirs from openmdao.utils.mpi import MPI @@ -1640,14 +1639,6 @@ def test_list_driver_vars(self): 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.") @use_tempdirs