{
 "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": [
    "# ImplicitFuncComp\n",
    "\n",
    "`ImplicitFuncComp` is a component that provides a shortcut for building an ImplicitComponent based on a python function. That function takes inputs and states as arguments and returns residual values. The function may take some inputs that are non-differentiable and are assumed to be static during the computation of derivatives.  These static values may be of any hashable type.  All other arguments and return values must be either floats or numpy arrays. The mapping between a state argument and its residual output must be specified in the metadata when the output (state) is added by setting 'resid' to the name of the residual.\n",
    "\n",
    "It may seem confusing to use `add_output` to specify state variables since the state variables\n",
    "are actually input arguments to the function, but in OpenMDAO's view of the world, states are outputs so we use `add_output` to specify them.  Also, using the metadata to specify which input arguments are actually states gives more flexibility in terms of how the function arguments are ordered. For example, if it's desirable for a function to be passable to `scipy.optimize.newton`, then the function's arguments can be ordered with the states first, followed by the inputs, in order to match the order expected by `scipy.optimize.newton`.\n",
    "\n",
    "The `add_output` function is part of the [Function Metadata API](../func_api.ipynb).  You use this API to specify various metadata that OpenMDAO needs in order to properly configure a fully functional implicit component. You should read and understand the [Function Metadata API](../func_api.ipynb) before you continue with this section.\n",
    "\n",
    "## ImplicitFuncComp Options\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "def func(a):\n",
    "    y = a * 2.\n",
    "    return y\n",
    "om.show_options_table(om.ImplicitFuncComp(func))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ImplicitFuncComp Constructor\n",
    "\n",
    "The call signature for the `ImplicitFuncComp` constructor is:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.implicit_func_comp.ImplicitFuncComp.__init__\n",
    "        :noindex:\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ImplicitFuncComp Example: A simple implicit function component\n",
    "\n",
    "The simplest implicit function component requires the definition of a function that takes\n",
    "inputs and states as arguments and returns residual values.  This function maps to the `apply_nonlinear`\n",
    "method in the OpenMDAO component API. Here's an example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "import openmdao.func_api as omf\n",
    "\n",
    "def apply_nl(a, b, c, x):  # inputs a, b, c and state x\n",
    "    R_x = a * x ** 2 + b * x + c\n",
    "    return R_x\n",
    "\n",
    "f = (omf.wrap(apply_nl)\n",
    "        .add_output('x', resid='R_x', val=0.0)\n",
    "        .declare_partials(of='*', wrt='*', method='cs')\n",
    "        )\n",
    "\n",
    "p = om.Problem()\n",
    "p.model.add_subsystem('comp', om.ImplicitFuncComp(f))\n",
    "\n",
    "p.model.nonlinear_solver = om.NewtonSolver(solve_subsystems=False, iprint=0)\n",
    "\n",
    "# need this since comp is implicit and doesn't have a solve_linear\n",
    "p.model.linear_solver = om.DirectSolver()\n",
    "\n",
    "p.setup()\n",
    "\n",
    "p.set_val('comp.a', 2.)\n",
    "p.set_val('comp.b', -8.)\n",
    "p.set_val('comp.c', 6.)\n",
    "p.run_model()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_check_partials, assert_check_totals\n",
    "\n",
    "assert_check_partials(p.check_partials(includes=['comp'], out_stream=None), atol=1e-5)\n",
    "assert_check_totals(p.check_totals(of=['comp.x'], wrt=['comp.a', 'comp.b', 'comp.c'], out_stream=None))"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "interpreter": {
   "hash": "245bb6672fbc289f90037d9f00b5ee20de7d921e65d14dbc4c07ab973781223d"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.12"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
