{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Changing Model Settings After Setup\n",
    "\n",
    "After Problem `setup` has been called, the entire model hierarchy has been instantiated and\n",
    "[setup and configure](configure_method) have been called on all Groups and Components.\n",
    "However, you may still want to make some changes to your model configuration.\n",
    "\n",
    "OpenMDAO allows you to do a limited number of things after the Problem `setup` has been called, but before\n",
    "you have called `run_model` or `run_driver`. These allowed actions include the following:\n",
    "\n",
    "- [Set initial conditions for unconnected inputs or states](../running_your_models/set_get)\n",
    "- [Assign linear and nonlinear solvers](../../building_blocks/solvers/solvers)\n",
    "- Change solver settings\n",
    "- Assign Dense or Sparse Jacobians\n",
    "- [Set execution order](set_order.ipynb)\n",
    "- Assign case recorders\n",
    "\n",
    "\n",
    "Here, we instantiate a hierarchy of Groups, and then change the solver to one that can solve this problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class ImplSimple(om.ImplicitComponent):\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_input('a', val=1.)\n",
    "        self.add_output('x', val=0.)\n",
    "\n",
    "    def apply_nonlinear(self, inputs, outputs, residuals):\n",
    "        residuals['x'] = np.exp(outputs['x']) - \\\n",
    "            inputs['a']**2 * outputs['x']**2\n",
    "\n",
    "    def linearize(self, inputs, outputs, jacobian):\n",
    "        jacobian['x', 'x'] = np.exp(outputs['x']) - \\\n",
    "            2 * inputs['a']**2 * outputs['x']\n",
    "        jacobian['x', 'a'] = -2 * inputs['a'] * outputs['x']**2\n",
    "\n",
    "class Sub(om.Group):\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_subsystem('comp', ImplSimple())\n",
    "\n",
    "        # This will not solve it\n",
    "        self.nonlinear_solver = om.NonlinearBlockGS()\n",
    "\n",
    "    def configure(self):\n",
    "        # This will not solve it either.\n",
    "        self.nonlinear_solver = om.NonlinearBlockGS()\n",
    "\n",
    "class Super(om.Group):\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_subsystem('sub', Sub())\n",
    "\n",
    "top = om.Problem(model=Super())\n",
    "\n",
    "top.setup()\n",
    "\n",
    "# This will solve it.\n",
    "top.model.sub.nonlinear_solver = om.NewtonSolver(solve_subsystems=False)\n",
    "top.model.sub.linear_solver = om.ScipyKrylov()\n",
    "\n",
    "print(isinstance(top.model.sub.nonlinear_solver, om.NewtonSolver))\n",
    "print(isinstance(top.model.sub.linear_solver, om.ScipyKrylov))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(isinstance(top.model.sub.nonlinear_solver, om.NewtonSolver) == True)\n",
    "assert(isinstance(top.model.sub.linear_solver, om.ScipyKrylov) == True)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
