{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output",
     "active-ipynb"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode  # noqa: F401\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ],
   "id": "d43c82b7"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Computing Total Derivatives\n",
    "\n",
    "Problem has a method, `compute_totals`, that allows you to compute the unscaled total derivative values for a model.\n",
    "\n",
    "If the model approximated its Jacobian, the method uses an approximation method."
   ],
   "id": "79b1b576"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.core.problem.Problem.compute_totals\n",
    "        :noindex:\n",
    "```"
   ],
   "id": "ff953ee4"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Usage\n",
    "\n",
    "Here is a simple example of using `compute_totals`:"
   ],
   "id": "5d26d0ea"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.notebook_utils import get_code\n",
    "from myst_nb import glue\n",
    "glue(\"code_src64\", get_code(\"openmdao.test_suite.components.paraboloid.Paraboloid\"), display=False)"
   ],
   "id": "7bece461"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ":::{Admonition} `Paraboloid` class definition \n",
    ":class: dropdown\n",
    "\n",
    "{glue:}`code_src64`\n",
    ":::"
   ],
   "id": "9f881d33"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.paraboloid import Paraboloid\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', Paraboloid())\n",
    "\n",
    "model.set_input_defaults('comp.x', 3.0)\n",
    "model.set_input_defaults('comp.y', -4.0)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "totals = prob.compute_totals(of=['comp.f_xy'], wrt=['comp.x', 'comp.y'])\n",
    "print(totals[('comp.f_xy', 'comp.x')][0][0])"
   ],
   "id": "0e00df55"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(totals[('comp.f_xy', 'comp.y')][0][0])"
   ],
   "id": "5fa1aea0"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "assert_near_equal(totals[('comp.f_xy', 'comp.x')][0][0], -4.0, tolerance=1e-8)\n",
    "assert_near_equal(totals[('comp.f_xy', 'comp.y')][0][0], 3.0, tolerance=1e-8)"
   ],
   "id": "6865b9d0"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "totals = prob.compute_totals(of=['comp.f_xy'], wrt=['comp.x', 'comp.y'], return_format='dict')\n",
    "print(totals['comp.f_xy']['comp.x'][0][0])"
   ],
   "id": "20d6a5de"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(totals['comp.f_xy']['comp.y'][0][0])"
   ],
   "id": "30453406"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "assert_near_equal(totals['comp.f_xy']['comp.x'][0][0], -4.0, tolerance=1e-8)\n",
    "assert_near_equal(totals['comp.f_xy']['comp.y'][0][0], 3.0, tolerance=1e-8)"
   ],
   "id": "fcd812b8"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, `compute_totals` returns the derivatives unscaled, but you can also request that they be scaled by the driver scale values declared when the des_vars, objectives, or constraints are added:"
   ],
   "id": "f06e9a6a"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.paraboloid import Paraboloid\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', Paraboloid())\n",
    "\n",
    "model.set_input_defaults('comp.x', 3.0)\n",
    "model.set_input_defaults('comp.y', -4.0)\n",
    "\n",
    "model.add_design_var('comp.x', 3.0, ref0=50.0)\n",
    "model.add_design_var('comp.y', -4.0)\n",
    "model.add_objective('comp.f_xy')\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "totals = prob.compute_totals(of=['comp.f_xy'], wrt=['comp.x', 'comp.y'], driver_scaling=True)\n",
    "print(totals[('comp.f_xy', 'comp.x')][0][0])"
   ],
   "id": "2cf8bb0f"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(totals[('comp.f_xy', 'comp.y')][0][0])"
   ],
   "id": "57fa7993"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "assert_near_equal(totals[('comp.f_xy', 'comp.y')][0][0], 3.0, tolerance=1e-8)"
   ],
   "id": "5a3028a2"
  }
 ],
 "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.1"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
