Skip to content

Commit

Permalink
Merge e11c190 into 697a423
Browse files Browse the repository at this point in the history
  • Loading branch information
naylor-b committed Jun 20, 2019
2 parents 697a423 + e11c190 commit 553bc60
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
20 changes: 10 additions & 10 deletions openmdao/core/tests/test_feature_cache_linear_solution.py
Expand Up @@ -90,27 +90,27 @@ def solve_linear(self, d_outputs, d_residuals, mode):
d_residuals['states'] = gmres(self.state_jac, d_outputs['states'], x0=d_residuals['states'], atol='legacy')[0]

p = om.Problem()
p.driver = om.ScipyOptimizeDriver()
p.driver.options['optimizer'] = 'SLSQP'

indeps = p.model.add_subsystem('indeps', om.IndepVarComp(), promotes_outputs=['a', 'b', 'c'])
indeps.add_output('a', 1.)
indeps.add_output('b', 4.)
indeps.add_output('c', 1.)
p.model.add_subsystem('quad', QuadraticComp(), promotes_inputs=['a', 'b', 'c'], promotes_outputs=['states'])
p.model.add_subsystem('obj', om.ExecComp('y = (x[1]-x[0])**2', x=np.ones(2)))
p.model.connect('states', 'obj.x')

p.model.add_design_var('a', cache_linear_solution=True)
p.model.add_constraint('states', upper=10)

p.model.add_objective('obj.y')

p.setup(mode='fwd')
p.run_model()

assert_rel_error(self, p['states'], [-0.26794919, -4.], 1e-6)

derivs = p.compute_totals(of=['states'], wrt=['a'])
assert_rel_error(self, derivs['states', 'a'], [[-0.02072594],[4.]], 1e-6)
p.run_driver()

p['a'] = 4
derivs = p.compute_totals(of=['states'], wrt=['a'])
assert_rel_error(self, derivs['states', 'a'], [[-0.02072594],[4.]], 1e-6)
print(p['a'], p['b'], p['c'])
print(p['states'])
assert_rel_error(self, p['obj.y'], 0.25029766, 1e-4)


if __name__ == "__main__":
Expand Down
Expand Up @@ -6,38 +6,35 @@ Restarting Linear Solutions for Expensive Linear Solves
.. note that the openmdao solvers will use a restart if they can
.. note that components that implement their own solve linear are responsible for their own restart!
.. note that components that implement their own solve linear are responsible for their own restart!
.. Tell them where to find the restart vector in reverse and forward modes
When using iterative linear solvers, it is often desirable to use the converged solution from a previous linear solve as the initial guess for the current one.
There is some memory cost associated with this feature, because the solution for each quantity of interest will be saved separately.
However, the benefit is reduced computational cost for the subsequent linear solves.
When using iterative linear solvers, it is often desirable to use the converged solution from a previous linear solve as the initial guess for the current one.
There is some memory cost associated with this feature, because the solution for each quantity of interest will be saved separately.
However, the benefit is reduced computational cost for the subsequent linear solves.

.. note::

This feature should not be used when using the :ref:`DirectSolver<directsolver>` at the top level of your model.
It won't offer any computational savings in that situation.
.. note::

To use this feature, provide :code:`cache_linear_solution=True` as an argument to :ref:`add_design_var()<feature_add_design_var>`,
:ref:`add_objective()<feature_add_objective>`, or :ref:`add_constraint()<feature_add_constraint>`.
This feature should not be used when using the :ref:`DirectSolver<directsolver>` at the top level of your model.
It won't offer any computational savings in that situation.

If you are using one of the OpenMDAO iterative solvers (:ref:`ScipyKrylov<scipyiterativesolver>`, :ref:`PETScKrylov<petscKrylov>`,
:ref:`LinearBlockGS<linearblockgs>`, or :ref:`LinearBlockJac<linearblockjac>`), then simply adding that argument is enough to activate this feature.
To use this feature, provide :code:`cache_linear_solution=True` as an argument to :ref:`add_design_var()<feature_add_design_var>`,
:ref:`add_objective()<feature_add_objective>`, or :ref:`add_constraint()<feature_add_constraint>`.

If you have implemented the :code:`solve_linear()` method for an :ref:`ImplicitComponent<comp-type-3-implicitcomp>`,
then you will need to make sure to use the provided guess solution in your implementation.
The cached solution will be put into the solution vector for you to use as an initial guess.
Note that you will override those values with the final solution.
In :code:`fwd` mode, the guess will be in the :code:`d_outputs` vector.
In :code:`rev` mode, the guess will be in the :code:`d_residuals` vector.
If you are using one of the OpenMDAO iterative solvers (:ref:`ScipyKrylov<scipyiterativesolver>`, :ref:`PETScKrylov<petscKrylov>`,
:ref:`LinearBlockGS<linearblockgs>`, or :ref:`LinearBlockJac<linearblockjac>`), then simply adding that argument is enough to activate this feature.

Below is a toy example problem that illustrates how the restart vectors should work.
The restart is passed in via the :code:`x0` argument to gmres.
If you have implemented the :code:`solve_linear()` method for an :ref:`ImplicitComponent<comp-type-3-implicitcomp>`,
then you will need to make sure to use the provided guess solution in your implementation.
The cached solution will be put into the solution vector for you to use as an initial guess.
Note that you will override those values with the final solution.
In :code:`fwd` mode, the guess will be in the :code:`d_outputs` vector.
In :code:`rev` mode, the guess will be in the :code:`d_residuals` vector.

Below is a toy example problem that illustrates how the restart vectors should work.
The restart is passed in via the :code:`x0` argument to gmres.

.. embed-code::
openmdao.core.tests.test_feature_cache_linear_solution.CacheLinearTestCase.test_feature_cache_linear
:layout: code, output




0 comments on commit 553bc60

Please sign in to comment.