Skip to content

Commit

Permalink
added check to pyoptsparse to ensure that a full model solve_nonlinea…
Browse files Browse the repository at this point in the history
…r is done at the beginning of an optimization
  • Loading branch information
naylor-b committed Feb 22, 2024
1 parent 925c14e commit 5ea6dd9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 9 additions & 4 deletions openmdao/drivers/pyoptsparse_driver.py
Expand Up @@ -185,6 +185,8 @@ class pyOptSparseDriver(Driver):
Specifies sparsity of sub-jacobians of the total jacobian.
_user_termination_flag : bool
This is set to True when the user sends a signal to terminate the job.
_model_ran : bool
This is set to True after the full model has been run at least once.
"""

def __init__(self, **kwargs):
Expand Down Expand Up @@ -237,6 +239,7 @@ def __init__(self, **kwargs):
self._exc_info = None
self._total_jac_format = 'dict'
self._total_jac_sparsity = None
self._model_ran = False

self.cite = CITATIONS

Expand Down Expand Up @@ -336,6 +339,7 @@ def _setup_driver(self, problem):
' but the selected optimizer ({0}) does not support'
' multiple objectives.'.format(self.options['optimizer']))

self._model_ran = False
self._setup_tot_jac_sparsity()

def get_driver_objective_calls(self):
Expand Down Expand Up @@ -379,7 +383,6 @@ def run(self):
self.pyopt_solution = None
self._total_jac = None
self.iter_count = 0
fwd = problem._mode == 'fwd'
self._quantities = []

optimizer = self.options['optimizer']
Expand All @@ -397,14 +400,13 @@ def run(self):
model_ran = False
if optimizer in run_required or linear_constraints:
with RecordingDebugging(self._get_name(), self.iter_count, self) as rec:
# Initial Run - do without relevance to avoid breaking some tests that
# depend on the old behavior. TODO: possibly revisit this?
model.run_solve_nonlinear()
rec.abs = 0.0
rec.rel = 0.0
model_ran = True
self.iter_count += 1

self._model_ran = model_ran
self._coloring_info.run_model = not model_ran

comm = None if isinstance(problem.comm, FakeComm) else problem.comm
Expand Down Expand Up @@ -737,8 +739,11 @@ def _objfunc(self, dv_dict):
self.iter_count += 1
try:
self._in_user_function = True
with model._relevant.all_seeds_active():
# deactivate the relevance if we haven't run the full model yet, so that
# the full model will run at least once.
with model._relevant.all_seeds_active(active=self._model_ran):
model.run_solve_nonlinear()
self._model_ran = True

# Let the optimizer try to handle the error
except AnalysisError:
Expand Down
1 change: 0 additions & 1 deletion openmdao/drivers/scipy_optimizer.py
Expand Up @@ -285,7 +285,6 @@ def run(self):

# Initial Run
with RecordingDebugging(self._get_name(), self.iter_count, self) as rec:
# do the initial run without relevance. TODO: maybe revisit this?
model.run_solve_nonlinear()
self.iter_count += 1

Expand Down

0 comments on commit 5ea6dd9

Please sign in to comment.