{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc7e3027",
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output",
     "active-ipynb"
    ]
   },
   "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",
   "id": "25b7f5f2",
   "metadata": {},
   "source": [
    "# How to know if a System is under FD or CS\n",
    "\n",
    "All Systems (Components and Groups) have two flags that indicate whether the System is running under finite difference or complex step.  The `under_finite_difference` flag is True if the System is being finite differenced and the `under_complex_step` flag is True if the System is being complex stepped.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "404246bb",
   "metadata": {},
   "source": [
    "## Usage\n",
    "\n",
    "\n",
    "First we'll show how to detect when a component is being finite differenced:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e807dfd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class MyFDPartialComp(om.ExplicitComponent):\n",
    "    def setup(self):\n",
    "        self.num_fd_computes = 0\n",
    "\n",
    "        self.add_input('x')\n",
    "        self.add_output('y')\n",
    "\n",
    "    def setup_partials(self):\n",
    "        self.declare_partials('y', 'x', method='fd')\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = 1.5 * inputs['x']\n",
    "        if self.under_finite_difference:\n",
    "            self.num_fd_computes += 1\n",
    "            print(f\"{self.pathname} is being finite differenced!\")\n",
    "\n",
    "        \n",
    "p = om.Problem()\n",
    "p.model.add_subsystem('comp', MyFDPartialComp())\n",
    "\n",
    "p.setup()\n",
    "p.run_model()\n",
    "\n",
    "# there shouldn't be any finite difference computes yet\n",
    "print(\"Num fd calls = \", p.model.comp.num_fd_computes)\n",
    "\n",
    "totals = p.compute_totals(['comp.y'], ['comp.x'])\n",
    "\n",
    "# since we're doing forward difference, there should be 1 call to compute under fd\n",
    "print(\"Num fd calls =\", p.model.comp.num_fd_computes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f6b9c38",
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output",
     "active-ipynb"
    ]
   },
   "outputs": [],
   "source": [
    "assert p.model.comp.num_fd_computes == 1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa12919",
   "metadata": {},
   "source": [
    "Now we'll do the same thing for a complex stepped component:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11d0b2cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "\n",
    "class MyCSPartialComp(om.ExplicitComponent):\n",
    "    def setup(self):\n",
    "        self.num_cs_computes = 0\n",
    "\n",
    "        self.add_input('x')\n",
    "        self.add_output('y')\n",
    "\n",
    "    def setup_partials(self):\n",
    "        self.declare_partials('y', 'x', method='cs')\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        outputs['y'] = 1.5 * inputs['x']\n",
    "        if self.under_complex_step:\n",
    "            self.num_cs_computes += 1\n",
    "            print(f\"{self.pathname} is being complex stepped!\")\n",
    "\n",
    "        \n",
    "p = om.Problem()\n",
    "p.model.add_subsystem('comp', MyCSPartialComp())\n",
    "\n",
    "p.setup()\n",
    "p.run_model()\n",
    "\n",
    "# there shouldn't be any complex step computes yet\n",
    "print(\"Num cs calls =\", p.model.comp.num_cs_computes)\n",
    "\n",
    "\n",
    "totals = p.compute_totals(['comp.y'], ['comp.x'])\n",
    "\n",
    "# there should be 1 call to compute under cs\n",
    "print(\"Num cs calls =\", p.model.comp.num_cs_computes)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9096b61",
   "metadata": {
    "tags": [
     "remove-input",
     "active-ipynb",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert p.model.comp.num_cs_computes == 1\n"
   ]
  }
 ],
 "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
}
