Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
467 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
openmdao/core/tests/test_distrib_driver_debug_print_options.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import os | ||
import sys | ||
import unittest | ||
from six import StringIO | ||
|
||
from openmdao.api import Problem, Group, IndepVarComp, ExecComp, ParallelGroup | ||
from openmdao.utils.general_utils import set_pyoptsparse_opt | ||
from openmdao.utils.mpi import MPI | ||
|
||
try: | ||
from openmdao.vectors.petsc_vector import PETScVector | ||
except ImportError: | ||
PETScVector = None | ||
|
||
@unittest.skipIf(PETScVector is None or os.environ.get("TRAVIS"), | ||
"PETSc is required." if PETScVector is None | ||
else "Unreliable on Travis CI.") | ||
class DistributedDriverDebugPrintOptionsTest(unittest.TestCase): | ||
|
||
N_PROCS = 2 | ||
|
||
def test_distributed_driver_debug_print_options(self): | ||
|
||
# check that pyoptsparse is installed. if it is, try to use SLSQP. | ||
OPT, OPTIMIZER = set_pyoptsparse_opt('SLSQP') | ||
|
||
if OPTIMIZER: | ||
from openmdao.drivers.pyoptsparse_driver import pyOptSparseDriver | ||
|
||
class Mygroup(Group): | ||
|
||
def setup(self): | ||
self.add_subsystem('indep_var_comp', IndepVarComp('x'), promotes=['*']) | ||
self.add_subsystem('Cy', ExecComp('y=2*x'), promotes=['*']) | ||
self.add_subsystem('Cc', ExecComp('c=x+2'), promotes=['*']) | ||
|
||
self.add_design_var('x') | ||
self.add_constraint('c', lower=-3.) | ||
|
||
prob = Problem() | ||
|
||
prob.model.add_subsystem('par', ParallelGroup()) | ||
|
||
prob.model.par.add_subsystem('G1', Mygroup()) | ||
prob.model.par.add_subsystem('G2', Mygroup()) | ||
|
||
prob.model.add_subsystem('Obj', ExecComp('obj=y1+y2')) | ||
|
||
prob.model.connect('par.G1.y', 'Obj.y1') | ||
prob.model.connect('par.G2.y', 'Obj.y2') | ||
|
||
prob.model.add_objective('Obj.obj') | ||
|
||
prob.driver = pyOptSparseDriver() | ||
prob.driver.options['optimizer'] = 'SLSQP' | ||
prob.driver.options['print_results'] = False | ||
|
||
prob.driver.options['debug_print'] = ['desvars','ln_cons','nl_cons','objs'] | ||
|
||
prob.setup(vector_class=PETScVector) | ||
|
||
stdout = sys.stdout | ||
strout = StringIO() | ||
sys.stdout = strout | ||
sys.stdout = strout | ||
try: | ||
prob.run_driver() | ||
finally: | ||
sys.stdout = stdout | ||
|
||
output = strout.getvalue().split('\n') | ||
if MPI.COMM_WORLD.rank == 0: | ||
# Just make sure we have more than one. Not sure we will always have the same number | ||
# of iterations | ||
self.assertTrue(output.count("Design Vars") > 1, | ||
"Should be more than one design vars header printed") | ||
self.assertTrue(output.count("Nonlinear constraints") > 1, | ||
"Should be more than one nonlinear constraint header printed") | ||
self.assertTrue(output.count("Linear constraints") > 1, | ||
"Should be more than one linear constraint header printed") | ||
self.assertTrue(output.count("Objectives") > 1, | ||
"Should be more than one objective header printed") | ||
|
||
self.assertTrue(len([s for s in output if s.startswith('par.G1.indep_var_comp.x')]) > 1, | ||
"Should be more than one par.G1.indep_var_comp.x printed") | ||
self.assertTrue(len([s for s in output if s.startswith('par.G2.indep_var_comp.x')]) > 1, | ||
"Should be more than one par.G2.indep_var_comp.x printed") | ||
self.assertTrue(len([s for s in output if s.startswith('par.G1.Cc.c')]) > 1, | ||
"Should be more than one par.G1.Cc.c printed") | ||
self.assertTrue(len([s for s in output if s.startswith('par.G2.Cc.c')]) > 1, | ||
"Should be more than one par.G2.Cc.c printed") | ||
self.assertTrue(len([s for s in output if s.startswith('None')]) > 1, | ||
"Should be more than one None printed") | ||
self.assertTrue(len([s for s in output if s.startswith('Obj.obj')]) > 1, | ||
"Should be more than one Obj.obj printed") | ||
else: | ||
self.assertEqual(output, ['']) | ||
|
||
|
||
if __name__ == "__main__": | ||
from openmdao.utils.mpi import mpirun_tests | ||
mpirun_tests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...mdao/docs/feature_reference/working_with_models/debugging/debugging_drivers.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.. _debugging-drivers: | ||
|
||
********************* | ||
Driver Debug Printing | ||
********************* | ||
|
||
When working with a model, it may sometimes be helpful to print out the design variables, constraints, and | ||
objectives as the `Driver` iterates. OpenMDAO provides options on the `Driver` to let you do that. | ||
|
||
Usage | ||
----- | ||
|
||
This example shows how to use the `Driver` debug printing options. The `debug_print` option is a list of strings. | ||
The only valid strings are 'desvars','ln_cons','nl_cons',and 'objs'. | ||
|
||
|
||
.. embed-test:: | ||
openmdao.drivers.tests.test_scipy_optimizer.TestScipyOptimizerFeatures.test_debug_print_option | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.