{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ed8eb91",
   "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]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55d304ad",
   "metadata": {},
   "source": [
    "# Picking Forward or Reverse Total Derivative Solve\n",
    "\n",
    "Analytic total derivatives can be calculated in either forward or reverse mode. In forward mode, OpenMDAO computes total derivatives with one linear solve per design variable. In reverse mode, it uses one linear solve per response (i.e. objective or nonlinear constraint). So the choice of forward or reverse is problem-dependent.\n",
    "\n",
    "In OpenMDAO, the default derivative direction is ‘auto’. When mode is ‘auto’, OpenMDAO will choose either forward or reverse mode based on the relative sizes of the design variables vs. the size of the objectives and nonlinear constraints, i.e., it will choose the one that results in the lowest number of linear solves. In general it’s best to leave mode on ‘auto’, but if you want to set the derivative direction explicitly for some reason, you can pass the mode argument to your problem setup function as follows:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36dd6b5f",
   "metadata": {},
   "source": [
    "    prob.setup(check=True, mode='fwd')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5df9ca69",
   "metadata": {},
   "source": [
    "or"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd9013c6",
   "metadata": {},
   "source": [
    "    prob.setup(check=True, mode='rev')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5b86b15",
   "metadata": {},
   "source": [
    "Pass ‘fwd’ to indicate forward mode and ‘rev’ to indicate reverse mode.\n",
    "\n",
    "If you choose a mode that is not optimal based on the sizes in your problem, you will see a warning like this in the output from your setup call."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db21a2d4",
   "metadata": {},
   "source": [
    "    RuntimeWarning: Inefficient choice of derivative mode.  You chose 'rev' for a problem with 100 design variables and 200 response variables (objectives and nonlinear constraints)."
   ]
  }
 ],
 "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": 5
}
