Skip to content

Commit

Permalink
Fixed a bug where the SubModel was never set to complex step mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth-T-Moore committed Oct 4, 2023
1 parent 479a538 commit 7d1eae2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
29 changes: 28 additions & 1 deletion openmdao/components/tests/test_submodel_comp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import openmdao.api as om
from openmdao.utils.mpi import MPI
from openmdao.utils.assert_utils import assert_near_equal, assert_check_partials
from openmdao.utils.assert_utils import assert_near_equal, assert_check_partials, \
assert_check_totals
from openmdao.test_suite.groups.parallel_groups import FanIn, FanOut

try:
Expand Down Expand Up @@ -464,6 +465,28 @@ def test_subprob_solver_print(self):

self.assertTrue((3, 20, 'NL') in p.model.submodel._subprob.model._solver_print_cache)

def test_complex_step_across_submodel(self):
p = om.Problem()
subprob = om.Problem()
subprob.model.add_subsystem('comp', om.ExecComp('x = r*cos(theta)'), promotes=['*'])
submodel = om.SubmodelComp(problem=subprob)

submodel.add_input('r', name='new_r', val=20)
submodel.add_input('theta', name='new_theta', val=0.5)
submodel.add_output('x', name='new_x', val=100)

model = p.model
model.add_subsystem('submodel', submodel, promotes=['*'])
model.add_design_var('new_r')
model.add_design_var('new_theta')
model.add_objective('new_x')

p.setup(force_alloc_complex=True)
p.run_model()

totals = p.check_totals(method='cs')
assert_check_totals(totals, atol=1e-11, rtol=1e-11)


@unittest.skipUnless(MPI and PETScVector, "MPI and PETSc are required.")
class TestSubmodelCompMPI(unittest.TestCase):
Expand All @@ -485,3 +508,7 @@ def test_submodel_comp(self):
p.run_model()
cpd = p.check_partials(method='cs', out_stream=None)
assert_check_partials(cpd)


if __name__ == '__main__':
unittest.main()
44 changes: 23 additions & 21 deletions openmdao/core/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -4832,27 +4832,29 @@ def _set_complex_step_mode(self, active):
active : bool
Complex mode flag; set to True prior to commencing complex step.
"""
for sub in self.system_iter(include_self=True, recurse=True):
sub.under_complex_step = active
sub._inputs.set_complex_step_mode(active)
sub._outputs.set_complex_step_mode(active)
sub._residuals.set_complex_step_mode(active)

if sub._doutputs._alloc_complex:
sub._doutputs.set_complex_step_mode(active)
sub._dinputs.set_complex_step_mode(active)
sub._dresiduals.set_complex_step_mode(active)
if sub.nonlinear_solver:
sub.nonlinear_solver._set_complex_step_mode(active)

if sub.linear_solver:
sub.linear_solver._set_complex_step_mode(active)

if sub._owns_approx_jac:
sub._jacobian.set_complex_step_mode(active)

if sub._assembled_jac:
sub._assembled_jac.set_complex_step_mode(active)
self.under_complex_step = active
self._inputs.set_complex_step_mode(active)
self._outputs.set_complex_step_mode(active)
self._residuals.set_complex_step_mode(active)

if self._doutputs._alloc_complex:
self._doutputs.set_complex_step_mode(active)
self._dinputs.set_complex_step_mode(active)
self._dresiduals.set_complex_step_mode(active)
if self.nonlinear_solver:
self.nonlinear_solver._set_complex_step_mode(active)

if self.linear_solver:
self.linear_solver._set_complex_step_mode(active)

if self._owns_approx_jac:
self._jacobian.set_complex_step_mode(active)

if self._assembled_jac:
self._assembled_jac.set_complex_step_mode(active)

for sub in self.system_iter(include_self=False, recurse=True):
sub._set_complex_step_mode(active)

def cleanup(self):
"""
Expand Down

0 comments on commit 7d1eae2

Please sign in to comment.