How to call function between optimization iterations in pyOpt?
<p>I was wondering if there is a way that I can pass pyOpt a function that
should be called at the end of each iteration? </p>

<p>The reason I need something like this is that I am running a FEA
simulation in each function evaluation, and I would like to output the
FEA results (displacements, stresses) to an ExodusII file after each
optimization iteration.  I originally placed my writeExodus function at
the end of the "function evaluation" function, my problem with this is
that a new "pseudo time-step" gets written to my exodus file each time
the function is evaluated rather than only at the end of each iteration,
so this obviously would lead to extra unncessary output to the exodus
file for numerical differentiation (finite difference, complex step) and
for optimizers that make multiple function evaluations per iteration
(i.e. GCMMA when checking if approximation is conservative).</p>

<p>So, is there a way I can tell pyOpt to execute a function (i.e. my
exodusWrite function) at the end of each iteration?  Or alternatively,
is there anyway I can track the optimizer iterations in pyOpt so that
inside of my "function evaluation" function I can keep track of the
optimizer iterations and only write the exodus output when the iteration number changes?</p>

<p>you could either put your function into a component that you put at the end of your model. Since it won't have any connections, you'll want to <a href="http://openmdao.readthedocs.io/en/1.7.2/srcdocs/packages/core/group.html?highlight=set_order#openmdao.core.group.Group.set_order" rel="nofollow">set the run order</a> manually for your group. </p>

<p>Alternatively, you could just hack the pyopt_sparse driver code to manually call your function. You would just add a call to your method of choice at the end if <a href="https://github.com/OpenMDAO/OpenMDAO/blob/master/openmdao/drivers/pyoptsparse_driver.py#L423" rel="nofollow">this call</a> and it would then get called any time pyopt_sparse asks for an objective evaluation</p>

python optimization openmdao