Skip to content

Commit

Permalink
Merge branch 'master' into move_coloring_working
Browse files Browse the repository at this point in the history
  • Loading branch information
swryan committed Mar 5, 2024
2 parents 337794a + fa88683 commit 87b4ada
Show file tree
Hide file tree
Showing 17 changed files with 419 additions and 260 deletions.
2 changes: 1 addition & 1 deletion openmdao/api.py
Expand Up @@ -121,7 +121,7 @@

# Reports System
from openmdao.utils.reports_system import register_report, unregister_report, get_reports_dir, \
list_reports, clear_reports
list_reports, clear_reports, set_reports_dir

import os

Expand Down
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
3 changes: 0 additions & 3 deletions openmdao/core/driver.py
Expand Up @@ -196,9 +196,6 @@ def __init__(self, **kwargs):

self._has_scaling = False

# Want to allow the setting of hooks on Drivers
_setup_hooks(self)

def _get_inst_id(self):
if self._problem is None:
return None
Expand Down
2 changes: 1 addition & 1 deletion openmdao/core/explicitcomponent.py
Expand Up @@ -68,7 +68,7 @@ def _configure(self):
"""
new_jacvec_prod = getattr(self, 'compute_jacvec_product', None)

if self.matrix_free is _UNDEFINED:
if self.matrix_free == _UNDEFINED:
self.matrix_free = overrides_method('compute_jacvec_product', self, ExplicitComponent)

if self.matrix_free:
Expand Down
2 changes: 1 addition & 1 deletion openmdao/core/implicitcomponent.py
Expand Up @@ -76,7 +76,7 @@ def _configure(self):
if self._has_solve_nl is _UNDEFINED:
self._has_solve_nl = overrides_method('solve_nonlinear', self, ImplicitComponent)

if self.matrix_free is _UNDEFINED:
if self.matrix_free == _UNDEFINED:
self.matrix_free = overrides_method('apply_linear', self, ImplicitComponent)

if self.matrix_free:
Expand Down
63 changes: 62 additions & 1 deletion openmdao/core/problem.py
Expand Up @@ -2005,6 +2005,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 @@ -2045,7 +2106,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 @@ -1290,8 +1290,8 @@ def test_alias_constraints(self):
assert_near_equal(totals['a3', 'widths']['abs error'][0], 0.0, 1e-6)
assert_near_equal(totals['a4', '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 @@ -526,7 +526,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 @@ -635,7 +635,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 @@ -1667,7 +1667,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 @@ -1706,7 +1706,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 @@ -1719,7 +1719,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 @@ -1749,7 +1749,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 @@ -1823,11 +1823,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 @@ -1860,7 +1860,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 @@ -1960,7 +1960,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 @@ -1975,7 +1975,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 @@ -1984,7 +1984,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 @@ -2003,7 +2003,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
Expand Up @@ -1081,10 +1081,10 @@
"source": [
"### *Listing Problem Variables*\n",
"\n",
"The `Problem` class has a method `list_problem_vars` which prints out the values and metadata for design, constraint, and objective variables.\n",
"The `Problem` class has a method `list_driver_vars` which prints out the values and metadata for design, constraint, and objective variables.\n",
"\n",
"```{eval-rst}\n",
" .. automethod:: openmdao.core.problem.Problem.list_problem_vars\n",
" .. automethod:: openmdao.core.problem.Problem.list_driver_vars\n",
" :noindex:\n",
"```\n",
"\n",
Expand Down Expand Up @@ -1154,16 +1154,16 @@
"metadata": {},
"outputs": [],
"source": [
"prob.list_problem_vars(print_arrays=True,\n",
" desvar_opts=['lower', 'upper', 'ref', 'ref0',\n",
" 'indices', 'adder', 'scaler',\n",
" 'parallel_deriv_color', 'min', 'max'],\n",
" cons_opts=['lower', 'upper', 'equals', 'ref', 'ref0',\n",
" 'indices', 'adder', 'scaler', 'linear', 'min', 'max'],\n",
" objs_opts=['ref', 'ref0',\n",
" 'indices', 'adder', 'scaler',\n",
" 'parallel_deriv_color',\n",
" 'cache_linear_solution'])"
"prob.list_driver_vars(print_arrays=True,\n",
" desvar_opts=['lower', 'upper', 'ref', 'ref0',\n",
" 'indices', 'adder', 'scaler',\n",
" 'parallel_deriv_color', 'min', 'max'],\n",
" cons_opts=['lower', 'upper', 'equals', 'ref', 'ref0',\n",
" 'indices', 'adder', 'scaler', 'linear', 'min', 'max'],\n",
" objs_opts=['ref', 'ref0',\n",
" 'indices', 'adder', 'scaler',\n",
" 'parallel_deriv_color',\n",
" 'cache_linear_solution'])"
]
}
],
Expand All @@ -1187,7 +1187,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
"version": "3.11.4"
},
"orphan": true
},
Expand Down
Expand Up @@ -92,8 +92,18 @@
"source": [
"## Setting the directory for the reports\n",
"\n",
"If the user wishes to have the reports directory placed into a different directory other than the default current working directory, the user can set the environment variable, `OPENMDAO_REPORTS_DIR` to the name of that directory. The directory does not have to exist beforehand. It will be created by the reporting system. As an example, if `OPENMDAO_REPORTS_DIR` is set to \"my_reports\", then, if the `Problem` name is `problem1`, then the reports will be written to the \"my_reports/problem1\" directory.\n",
"If the user wishes to have the reports directory placed into a different directory other than the default current working directory, the user has two options for doing so.\n",
"\n",
"1. This option can be set programmatically using the `set_reports_dir` function from `openmdao.api`.\n",
"Note that this is a global OpenMDAO setting and therefore it cannot take different values for different processors if running multiple problems simultaneously under MPI.\n",
"\n",
"2. The default value may be set using the environment variable `OPENMDAO_REPORTS_DIR` to the name of that directory. The directory does not have to exist beforehand. It will be created by the reporting system. As an example, if `OPENMDAO_REPORTS_DIR` is set to \"my_reports\", then, if the `Problem` name is `problem1`, then the reports will be written to the \"my_reports/problem1\" directory.Note that `OPENMDAO_REPORTS_DIR` is read during the import of openmdao, so setting `os.environ['OPENMDAO_REPORTS_DIR'] = './my_reports_dir'` after OpenMDAO has been imported **will have no effect**."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Support for subproblems\n",
"\n",
"The reporting system supports subproblems. For example, if a model has two Problems and they have the names `problem1` and `problem2`, the reporting system will put the reports into the subfolders, `problem1` and `problem2` under the top level reports directory, which defaults to `./reports` or is specified by the value of `OPENMDAO_REPORTS_DIR`.\n",
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 87b4ada

Please sign in to comment.