{
 "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  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modifying Solver Options\n",
    "\n",
    "In OpenMDAO, implicit outputs can have bounds and scaling attached to them. These are defined when `add_output` is called on an implicit component.\n",
    "\n",
    "In complex models, this means that a user may need to tweak bounds and scaling in multiple files. The bounds and scaling of a variable may be dependent upon the application of the model. These bounds may need to be adjusted in a file that is generally outside of the expertise of the user.\n",
    "\n",
    "OpenMDAO provides a method `System.set_output_solver_options` that allows users to set bounds throughout their model from a single script or notebook."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.core.system.System.set_output_solver_options\n",
    "        :noindex:\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example of Setting Solver Output Options\n",
    "\n",
    "Here is an example of how to make use of the `System.set_output_solver_options` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Implicit component in which bounds and scaling are set on outputs\n",
    "import openmdao.api as om\n",
    "\n",
    "class ScalingExample3(om.ImplicitComponent):\n",
    "\n",
    "    def setup(self):\n",
    "        self.add_input('x1', val=100.0)\n",
    "        self.add_input('x2', val=5000.0)\n",
    "        self.add_output('y1', val=200., ref=1e2, res_ref=1e5, ref0=1.0, lower=2., upper=3)\n",
    "        self.add_output('y2', val=6000., ref=1e3, res_ref=1e-5)\n",
    "\n",
    "    def apply_nonlinear(self, inputs, outputs, residuals):\n",
    "        x1 = inputs['x1']\n",
    "        x2 = inputs['x2']\n",
    "        y1 = outputs['y1']\n",
    "        y2 = outputs['y2']\n",
    "\n",
    "        residuals['y1'] = 1e5 * (x1 - y1) / y1\n",
    "        residuals['y2'] = 1e-5 * (x2 - y2) / y2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Script that makes use of this implicit component\n",
    "prob = om.Problem()\n",
    "\n",
    "# ScalingExample3 sets values for ref, res_ref, ref0, lower, and upper\n",
    "comp = prob.model.add_subsystem('comp', ScalingExample3())\n",
    "\n",
    "prob.setup()\n",
    "prob.run_model() # Run model with the bounds and scaling set in the component"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "with prob.model._scaled_context_all():\n",
    "    assert_near_equal(prob.get_val('comp.y1'), 2.0, 1e-2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Now override a value from this cell (could also be from a separate script) and re-run the model\n",
    "prob.model.set_output_solver_options(name='comp.y1',ref=1e3)\n",
    "prob.setup()\n",
    "prob.run_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "with prob.model._scaled_context_all():\n",
    "    assert_near_equal(prob.get_val('comp.y1'), 0.2, 1e-2)"
   ]
  }
 ],
 "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
}
