{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "24c7d770",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:15.261633Z",
     "iopub.status.busy": "2026-10-02T14:41:15.261384Z",
     "iopub.status.idle": "2026-10-02T14:41:15.266224Z",
     "shell.execute_reply": "2026-10-02T14:41:15.265532Z"
    },
    "papermill": {
     "duration": 0.009161,
     "end_time": "2026-10-02T14:41:15.267020+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:15.257859+00:00",
     "status": "completed"
    },
    "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",
   "id": "be46f639",
   "metadata": {
    "papermill": {
     "duration": 0.002777,
     "end_time": "2026-10-02T14:41:15.291648+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:15.288871+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# ExecComp\n",
    "\n",
    "\n",
    "`ExecComp` is a component that provides a shortcut for building an ExplicitComponent that\n",
    "represents a set of simple mathematical relationships between inputs and outputs. The ExecComp\n",
    "automatically takes care of all of the component API methods, so you just need to instantiate\n",
    "it with an equation or a list of equations.\n",
    "\n",
    "## ExecComp Options\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0dcf75a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:15.300993Z",
     "iopub.status.busy": "2026-10-02T14:41:15.300805Z",
     "iopub.status.idle": "2026-10-02T14:41:16.604109Z",
     "shell.execute_reply": "2026-10-02T14:41:16.603370Z"
    },
    "papermill": {
     "duration": 1.307112,
     "end_time": "2026-10-02T14:41:16.604851+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:15.297739+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<!DOCTYPE html>\n",
       "<html lang=\"en\">\n",
       "<head>\n",
       "    <style>\n",
       "        h2 {\n",
       "            text-align: center;\n",
       "        }\n",
       "    </style>\n",
       "</head>\n",
       "<body>\n",
       "    <h2></h2>\n",
       "        <table style=\"border: 1px solid #999; border-collapse: collapse;\">\n",
       "        <tr><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Option</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Default</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Acceptable Values</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Acceptable Types</th><th style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; background-color: #E9E9E9; text-align: left;\">Description</th></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">always_opt</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">False</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">If True, force nonlinear operations on this component to be included in the optimization loop even if this component is not relevant to the design variables and responses.</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">default_shape</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">(1,)</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;tuple&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Default shape for variables that do not set val to a non-scalar value or set shape, shape_by_conn, copy_shape, or compute_shape. Default is (1,).</td></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">derivs_method</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;jax&#x27;, &#x27;cs&#x27;, &#x27;fd&#x27;, None]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">The method to use for computing derivatives</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">do_coloring</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">True</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">If True (the default), compute the partial jacobian coloring for this component.</td></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">has_diag_partials</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">False</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">If True, treat all array/array partials as diagonal if both arrays have size &gt; 1. All arrays with size &gt; 1 must have the same flattened size or an exception will be raised.</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">run_root_only</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">False</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">If True, call compute, compute_partials, linearize, apply_linear, apply_nonlinear, solve_linear, solve_nonlinear, and compute_jacvec_product only on rank 0 and broadcast the results to the other ranks.</td></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">shape</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;int&#x27;, &#x27;tuple&#x27;, &#x27;list&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Shape to be assigned to all variables in this component. Default is None, which means shape may be provided for variables individually.</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">shape_by_conn</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">False</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">If True, shape all inputs and outputs based on their connection. Default is False.</td></tr>\n",
       "       <tr style=\"background-color: ghostwhite;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">units</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">N/A</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;str&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">Units to be assigned to all variables in this component. Default is None, which means units may be provided for variables individually.</td></tr>\n",
       "       <tr style=\"background-color: #F3F3F3;\"><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">use_jit</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">True</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[True, False]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">[&#x27;bool&#x27;]</td><td style=\"border: 1px solid #999; border-collapse: collapse; padding: 5px; text-align: left;\">If True, attempt to use jit on compute_primal, assuming jax or some other AD package capable of jitting is active.</td></tr>\n",
       "    </table>\n",
       "</body>\n",
       "</html>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "om.show_options_table(\"openmdao.components.exec_comp.ExecComp\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2830a40",
   "metadata": {
    "papermill": {
     "duration": 0.001695,
     "end_time": "2026-10-02T14:41:16.608507+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:16.606812+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Constructor\n",
    "\n",
    "The call signature for the `ExecComp` constructor is:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.exec_comp.ExecComp.__init__\n",
    "        :noindex:\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "179fc1bd",
   "metadata": {
    "papermill": {
     "duration": 0.001569,
     "end_time": "2026-10-02T14:41:16.611903+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:16.610334+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "The values of the `kwargs` can be `dicts` which define the initial value for the variables along with\n",
    "other metadata. For example,\n",
    "\n",
    "```\n",
    "    ExecComp('xdot=x/t', x={'units': 'ft'}, t={'units': 's'}, xdot={'units': 'ft/s')\n",
    "```\n",
    "\n",
    "Here is a list of the possible metadata that can be assigned to a variable in this way. The **Applies To** column indicates\n",
    "whether the metadata is appropriate for input variables, output variables, or both.\n",
    "\n",
    "```{eval-rst}\n",
    "================  ====================================================== ============================================================= ==============  ========\n",
    "Name              Description                                            Valid Types                                                   Applies To      Default\n",
    "================  ====================================================== ============================================================= ==============  ========\n",
    "value             Initial value in user-defined units                    float, list, tuple, ndarray                                   input & output  1\n",
    "shape             Variable shape, only needed if not an array            int, tuple, list, None                                        input & output  None\n",
    "shape_by_conn     Determine variable shape based on its connection       bool                                                          input & output  False\n",
    "copy_shape        Determine variable shape based on named variable       str                                                           input & output  None\n",
    "units             Units of variable                                      str, None                                                     input & output  None\n",
    "desc              Description of variable                                str                                                           input & output  \"\"\n",
    "res_units         Units of residuals                                     str, None                                                     output          None\n",
    "ref               Value of variable when scaled value is 1               float, ndarray                                                output          1\n",
    "ref0              Value of variable when scaled value is 0               float, ndarray                                                output          0\n",
    "res_ref           Value of residual when scaled value is 1               float, ndarray                                                output          1\n",
    "lower             Lower bound of variable                                float, list, tuple, ndarray, Iterable, None                   output          None\n",
    "upper             Lower bound of variable                                float, list, tuple, ndarray, Iterable, None                   output          None\n",
    "src_indices       Global indices of the variable                         int, list of ints, tuple of ints, int ndarray, Iterable, None input           None\n",
    "flat_src_indices  If True, src_indices are indices into flattened source bool                                                          input           None\n",
    "tags              Used to tag variables for later filtering              str, list of strs                                             input & output  None\n",
    "================  ====================================================== ============================================================= ==============  ========\n",
    "```\n",
    "\n",
    "These metadata are passed to the `Component` methods `add_input` and `add_output`.\n",
    "For more information about these metadata, see the documentation for the arguments to these methods on [Component](../../../_srcdocs/packages/core/component).\n",
    "\n",
    "## Registering User Functions\n",
    "\n",
    "To get your own functions added to the internal namespace of ExecComp so you can call them\n",
    "from within an ExecComp expression, you can use the `ExecComp.register` function.\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.components.exec_comp.ExecComp.register\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "Note that you're required, when registering a new function, to indicate whether that function\n",
    "is complex safe or not.\n",
    "\n",
    "\n",
    "## ExecComp Example: Simple\n",
    "\n",
    "For example, here is a simple component that takes the input and adds one to it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8f98c07f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:16.616314Z",
     "iopub.status.busy": "2026-10-02T14:41:16.615915Z",
     "iopub.status.idle": "2026-10-02T14:41:17.825467Z",
     "shell.execute_reply": "2026-10-02T14:41:17.824319Z"
    },
    "papermill": {
     "duration": 1.212602,
     "end_time": "2026-10-02T14:41:17.826088+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:16.613486+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1790952077.814865] [runnervm8df0l:6437 :0]        ib_iface.c:1269 UCX  ERROR mana_0: iface 0x555d76e72340 failed to create UD QP TX wr:256 sge:6 inl:64 resp:0 RX wr:4096 sge:1 resp:0 failed: Operation not supported\n",
      "[1790952077.815119] [runnervm8df0l:6437 :0]      ucp_worker.c:1412 UCX  ERROR uct_iface_open(ud_verbs/mana_0:1) failed: Input/output error\n",
      "[3.]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[runnervm8df0l:06437] pml_ucx.c:313  Error: Failed to create UCP worker\n"
     ]
    }
   ],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', om.ExecComp('y=x+1.'))\n",
    "\n",
    "model.set_input_defaults('comp.x', 2.0)\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('comp.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6d34e57e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:17.830916Z",
     "iopub.status.busy": "2026-10-02T14:41:17.830761Z",
     "iopub.status.idle": "2026-10-02T14:41:17.834775Z",
     "shell.execute_reply": "2026-10-02T14:41:17.834105Z"
    },
    "papermill": {
     "duration": 0.007108,
     "end_time": "2026-10-02T14:41:17.835255+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.828147+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "assert_near_equal(prob.get_val('comp.y'), 3.0, 0.00001)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d5a7e72",
   "metadata": {
    "papermill": {
     "duration": 0.001725,
     "end_time": "2026-10-02T14:41:17.838863+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.837138+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Multiple Outputs\n",
    "\n",
    "You can also create an ExecComp with multiple outputs by placing the expressions in a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e059071",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:17.842951Z",
     "iopub.status.busy": "2026-10-02T14:41:17.842826Z",
     "iopub.status.idle": "2026-10-02T14:41:17.848178Z",
     "shell.execute_reply": "2026-10-02T14:41:17.847468Z"
    },
    "papermill": {
     "duration": 0.008269,
     "end_time": "2026-10-02T14:41:17.848871+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.840602+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.]\n",
      "[1.]\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', om.ExecComp(['y1=x+1.', 'y2=x-1.']), promotes=['x'])\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('x', 2.0)\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('comp.y1'))\n",
    "print(prob.get_val('comp.y2'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "80133d73",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:17.882519Z",
     "iopub.status.busy": "2026-10-02T14:41:17.882388Z",
     "iopub.status.idle": "2026-10-02T14:41:17.885717Z",
     "shell.execute_reply": "2026-10-02T14:41:17.884975Z"
    },
    "papermill": {
     "duration": 0.035362,
     "end_time": "2026-10-02T14:41:17.886139+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.850777+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('comp.y1'), 3.0, 0.00001)\n",
    "assert_near_equal(prob.get_val('comp.y2'), 1.0, 0.00001)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a38a11f",
   "metadata": {
    "papermill": {
     "duration": 0.001701,
     "end_time": "2026-10-02T14:41:17.889624+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.887923+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Arrays\n",
    "\n",
    "You can declare an ExecComp with arrays for inputs and outputs, but when you do, you must also\n",
    "pass in a correctly-sized array as an argument to the ExecComp call, or set the 'shape' metadata\n",
    "for that variable as described earlier. If specifying the value directly, it can be the initial value\n",
    "in the case of unconnected inputs, or just an empty array with the correct size."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3bde6a90",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:17.894299Z",
     "iopub.status.busy": "2026-10-02T14:41:17.894115Z",
     "iopub.status.idle": "2026-10-02T14:41:17.899993Z",
     "shell.execute_reply": "2026-10-02T14:41:17.899237Z"
    },
    "papermill": {
     "duration": 0.009023,
     "end_time": "2026-10-02T14:41:17.900445+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.891422+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', om.ExecComp('y=x[1]',\n",
    "                                        x=np.array([1., 2., 3.]),\n",
    "                                        y=0.0))\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('comp.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d23f7f29",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:17.984600Z",
     "iopub.status.busy": "2026-10-02T14:41:17.984443Z",
     "iopub.status.idle": "2026-10-02T14:41:17.987718Z",
     "shell.execute_reply": "2026-10-02T14:41:17.987142Z"
    },
    "papermill": {
     "duration": 0.085968,
     "end_time": "2026-10-02T14:41:17.988218+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.902250+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('comp.y'), 2.0, 0.00001)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68cf712a",
   "metadata": {
    "papermill": {
     "duration": 0.001849,
     "end_time": "2026-10-02T14:41:17.991995+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.990146+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Math Functions\n",
    "\n",
    "Functions from the math library are available for use in the expression strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b6224b00",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:17.996527Z",
     "iopub.status.busy": "2026-10-02T14:41:17.996324Z",
     "iopub.status.idle": "2026-10-02T14:41:18.002871Z",
     "shell.execute_reply": "2026-10-02T14:41:18.002220Z"
    },
    "papermill": {
     "duration": 0.009539,
     "end_time": "2026-10-02T14:41:18.003320+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:17.993781+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', om.ExecComp('z = sin(x)**2 + cos(y)**2'))\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('comp.x', np.pi/2.0)\n",
    "prob.set_val('comp.y', np.pi/2.0)\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('comp.z'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "117c5718",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.007893Z",
     "iopub.status.busy": "2026-10-02T14:41:18.007724Z",
     "iopub.status.idle": "2026-10-02T14:41:18.011430Z",
     "shell.execute_reply": "2026-10-02T14:41:18.010774Z"
    },
    "papermill": {
     "duration": 0.006678,
     "end_time": "2026-10-02T14:41:18.011922+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.005244+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('comp.z'), 1.0, 0.00001)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac3e0608",
   "metadata": {
    "papermill": {
     "duration": 0.001845,
     "end_time": "2026-10-02T14:41:18.015646+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.013801+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Variable Properties\n",
    "\n",
    "You can also declare properties like 'units', 'upper', or 'lower' on the inputs and outputs. In this\n",
    "example we declare all our inputs to be inches to trigger conversion from a variable expressed in feet\n",
    "in one connection source."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bbf5ffb8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.075359Z",
     "iopub.status.busy": "2026-10-02T14:41:18.075186Z",
     "iopub.status.idle": "2026-10-02T14:41:18.081893Z",
     "shell.execute_reply": "2026-10-02T14:41:18.081078Z"
    },
    "papermill": {
     "duration": 0.009943,
     "end_time": "2026-10-02T14:41:18.082368+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.072425+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[24.]\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('comp', om.ExecComp('z=x+y',\n",
    "                                        x={'val': 0.0, 'units': 'inch'},\n",
    "                                        y={'val': 0.0, 'units': 'inch'},\n",
    "                                        z={'val': 0.0, 'units': 'inch'}))\n",
    "\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('comp.x', 12.0, units='inch')\n",
    "prob.set_val('comp.y', 1.0, units='ft')\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('comp.z'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e06e234a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.087057Z",
     "iopub.status.busy": "2026-10-02T14:41:18.086888Z",
     "iopub.status.idle": "2026-10-02T14:41:18.090048Z",
     "shell.execute_reply": "2026-10-02T14:41:18.089472Z"
    },
    "papermill": {
     "duration": 0.00609,
     "end_time": "2026-10-02T14:41:18.090521+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.084431+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('comp.z'), 24.0, 0.00001)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d9c71da",
   "metadata": {
    "papermill": {
     "duration": 0.002757,
     "end_time": "2026-10-02T14:41:18.096084+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.093327+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Diagonal Partials\n",
    "\n",
    "If all of your ExecComp's array inputs and array outputs are the same size and happen to have\n",
    "diagonal partials, you can make computation of derivatives for your ExecComp faster by specifying a\n",
    "`has_diag_partials=True` argument\n",
    "to `__init__` or via the component options. This will cause the ExecComp to solve for its partials\n",
    "by complex stepping all entries of an array input at once instead of looping over each entry individually."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a644af3c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.102287Z",
     "iopub.status.busy": "2026-10-02T14:41:18.102161Z",
     "iopub.status.idle": "2026-10-02T14:41:18.137780Z",
     "shell.execute_reply": "2026-10-02T14:41:18.136998Z"
    },
    "papermill": {
     "duration": 0.03935,
     "end_time": "2026-10-02T14:41:18.138267+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.098917+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3. -0. -0. -0. -0.]\n",
      " [-0.  3. -0. -0. -0.]\n",
      " [-0. -0.  3. -0. -0.]\n",
      " [-0. -0. -0.  3. -0.]\n",
      " [-0. -0. -0. -0.  3.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "p = om.Problem()\n",
    "model = p.model\n",
    "\n",
    "model.add_subsystem('comp', om.ExecComp('y=3.0*x + 2.5',\n",
    "                                        has_diag_partials=True,\n",
    "                                        x=np.ones(5), y=np.ones(5)))\n",
    "\n",
    "p.setup()\n",
    "\n",
    "p.set_val('comp.x', np.ones(5))\n",
    "\n",
    "p.run_model()\n",
    "\n",
    "J = p.compute_totals(of=['comp.y'], wrt=['comp.x'], return_format='array')\n",
    "\n",
    "print(J)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3b5a1489",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.205997Z",
     "iopub.status.busy": "2026-10-02T14:41:18.205736Z",
     "iopub.status.idle": "2026-10-02T14:41:18.208585Z",
     "shell.execute_reply": "2026-10-02T14:41:18.207896Z"
    },
    "papermill": {
     "duration": 0.006751,
     "end_time": "2026-10-02T14:41:18.209026+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.202275+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from numpy.testing import assert_almost_equal\n",
    "\n",
    "assert_almost_equal(J, np.eye(5)*3., decimal=6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a8b22fb",
   "metadata": {
    "papermill": {
     "duration": 0.001908,
     "end_time": "2026-10-02T14:41:18.212894+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.210986+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Options\n",
    "\n",
    "Other options that can apply to all the variables in the component are variable shape and units.\n",
    "These can also be set as a keyword argument in the constructor or via the component options. In the\n",
    "following example the variables all share the same shape, which is specified in the constructor, and\n",
    "common units that are specified by setting the option."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "19393ea2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.217466Z",
     "iopub.status.busy": "2026-10-02T14:41:18.217339Z",
     "iopub.status.idle": "2026-10-02T14:41:18.223094Z",
     "shell.execute_reply": "2026-10-02T14:41:18.222298Z"
    },
    "papermill": {
     "duration": 0.008799,
     "end_time": "2026-10-02T14:41:18.223584+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.214785+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2. 4.]\n"
     ]
    }
   ],
   "source": [
    "model = om.Group()\n",
    "\n",
    "xcomp = model.add_subsystem('comp', om.ExecComp('y=2*x', shape=(2,)))\n",
    "\n",
    "xcomp.options['units'] = 'm'\n",
    "\n",
    "prob = om.Problem(model)\n",
    "prob.setup()\n",
    "\n",
    "prob.set_val('comp.x', [100., 200.], units='cm')\n",
    "\n",
    "prob.run_model()\n",
    "\n",
    "print(prob.get_val('comp.y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "773a1d18",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.257405Z",
     "iopub.status.busy": "2026-10-02T14:41:18.257253Z",
     "iopub.status.idle": "2026-10-02T14:41:18.260299Z",
     "shell.execute_reply": "2026-10-02T14:41:18.259662Z"
    },
    "papermill": {
     "duration": 0.035174,
     "end_time": "2026-10-02T14:41:18.260799+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.225625+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(prob.get_val('comp.y'), [2., 4.], 0.00001)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89f661f8",
   "metadata": {
    "papermill": {
     "duration": 0.002786,
     "end_time": "2026-10-02T14:41:18.266440+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.263654+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: User function registration\n",
    "\n",
    "If the function is complex safe, then you don't need to do anything differently than you\n",
    "would for any other ExecComp."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2ae09b1d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.414790Z",
     "iopub.status.busy": "2026-10-02T14:41:18.414544Z",
     "iopub.status.idle": "2026-10-02T14:41:18.422359Z",
     "shell.execute_reply": "2026-10-02T14:41:18.421729Z"
    },
    "papermill": {
     "duration": 0.153752,
     "end_time": "2026-10-02T14:41:18.423205+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.269453+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.0\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    om.ExecComp.register(\"myfunc\", lambda x: x * x, complex_safe=True)\n",
    "except NameError:\n",
    "    pass\n",
    "p = om.Problem()\n",
    "comp = p.model.add_subsystem(\"comp\", om.ExecComp(\"y = 2 * myfunc(x)\"))\n",
    "\n",
    "p.setup()\n",
    "p.run_model()\n",
    "J = p.compute_totals(of=['comp.y'], wrt=['comp.x'])\n",
    "print(J['comp.y', 'comp.x'][0][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8b87d947",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.428872Z",
     "iopub.status.busy": "2026-10-02T14:41:18.428645Z",
     "iopub.status.idle": "2026-10-02T14:41:18.432371Z",
     "shell.execute_reply": "2026-10-02T14:41:18.431510Z"
    },
    "papermill": {
     "duration": 0.007567,
     "end_time": "2026-10-02T14:41:18.433067+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.425500+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(J['comp.y', 'comp.x'][0][0], 4., 1e-10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4250f625",
   "metadata": {
    "papermill": {
     "duration": 0.002441,
     "end_time": "2026-10-02T14:41:18.456039+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.453598+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Complex unsafe user function registration\n",
    "\n",
    "If the function isn't complex safe, then derivatives involving that function\n",
    "will have to be computed using finite difference instead of complex step.  The way to specify\n",
    "that `fd` should be used for a given derivative is to call `declare_partials`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "54da8ea4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.461175Z",
     "iopub.status.busy": "2026-10-02T14:41:18.461019Z",
     "iopub.status.idle": "2026-10-02T14:41:18.471137Z",
     "shell.execute_reply": "2026-10-02T14:41:18.470585Z"
    },
    "papermill": {
     "duration": 0.013521,
     "end_time": "2026-10-02T14:41:18.471816+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.458295+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.000001999848735\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    om.ExecComp.register(\"unsafe\", lambda x: x * x, complex_safe=False)\n",
    "except NameError:\n",
    "    pass\n",
    "p = om.Problem()\n",
    "comp = p.model.add_subsystem(\"comp\", om.ExecComp(\"y = 2 * unsafe(x)\"))\n",
    "\n",
    "# because our function is complex unsafe, we must declare that the partials\n",
    "# with respect to 'x' use 'fd' instead of 'cs'\n",
    "comp.declare_partials('*', 'x', method='fd')\n",
    "\n",
    "p.setup()\n",
    "p.run_model()\n",
    "J = p.compute_totals(of=['comp.y'], wrt=['comp.x'])\n",
    "print(J['comp.y', 'comp.x'][0][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "68ed8041",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.477303Z",
     "iopub.status.busy": "2026-10-02T14:41:18.477159Z",
     "iopub.status.idle": "2026-10-02T14:41:18.479887Z",
     "shell.execute_reply": "2026-10-02T14:41:18.479401Z"
    },
    "papermill": {
     "duration": 0.006062,
     "end_time": "2026-10-02T14:41:18.480354+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.474292+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(4.999621836532242e-07)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert_near_equal(J['comp.y', 'comp.x'][0][0], 4., 1e-5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fd83809",
   "metadata": {
    "papermill": {
     "duration": 0.002169,
     "end_time": "2026-10-02T14:41:18.484836+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.482667+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Adding Expressions\n",
    "\n",
    "You can add additional expressions to an `ExecComp` with the \"add_expr\" method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5b02f892",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.603421Z",
     "iopub.status.busy": "2026-10-02T14:41:18.603209Z",
     "iopub.status.idle": "2026-10-02T14:41:18.611272Z",
     "shell.execute_reply": "2026-10-02T14:41:18.610371Z"
    },
    "papermill": {
     "duration": 0.124912,
     "end_time": "2026-10-02T14:41:18.611823+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.486911+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8.7]\n",
      "[3.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "class ConfigGroup(om.Group):\n",
    "    def setup(self):\n",
    "        excomp = om.ExecComp('y=x',\n",
    "                             x={'val' : 3.0, 'units' : 'mm'},\n",
    "                             y={'shape' : (1, ), 'units' : 'cm'})\n",
    "\n",
    "        self.add_subsystem('excomp', excomp, promotes=['*'])\n",
    "\n",
    "    def configure(self):\n",
    "        self.excomp.add_expr('z = 2.9*x',\n",
    "                             z={'shape' : (1, ), 'units' : 's'})\n",
    "\n",
    "p = om.Problem()\n",
    "p.model.add_subsystem('sub', ConfigGroup(), promotes=['*'])\n",
    "p.setup()\n",
    "p.run_model()\n",
    "\n",
    "print(p.get_val('z'))\n",
    "print(p.get_val('y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0933f356",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.619204Z",
     "iopub.status.busy": "2026-10-02T14:41:18.618999Z",
     "iopub.status.idle": "2026-10-02T14:41:18.622120Z",
     "shell.execute_reply": "2026-10-02T14:41:18.621465Z"
    },
    "papermill": {
     "duration": 0.00853,
     "end_time": "2026-10-02T14:41:18.623072+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.614542+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert_almost_equal(p.get_val('z'), 8.7, 1e-8)\n",
    "assert_almost_equal(p.get_val('y'), 3.0, 1e-8)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35a4f13a",
   "metadata": {
    "papermill": {
     "duration": 0.002516,
     "end_time": "2026-10-02T14:41:18.647480+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.644964+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## ExecComp Example: Constants\n",
    "\n",
    "You can define variables in `ExecComp` to be constants.\n",
    "Here is a simple model in which no constants are used. Notice the names of the inputs that are printed in the two models."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b3d3a637",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.657601Z",
     "iopub.status.busy": "2026-10-02T14:41:18.657414Z",
     "iopub.status.idle": "2026-10-02T14:41:18.663106Z",
     "shell.execute_reply": "2026-10-02T14:41:18.662417Z"
    },
    "papermill": {
     "duration": 0.013083,
     "end_time": "2026-10-02T14:41:18.663621+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.650538+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C1.a', 'C1.b']\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem()\n",
    "C1 = prob.model.add_subsystem('C1', om.ExecComp('x = a + b'))\n",
    "prob.setup()\n",
    "prob.set_solver_print(level=0)\n",
    "prob.run_model()\n",
    "print(list(C1._inputs._names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d3e8c1fa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.669346Z",
     "iopub.status.busy": "2026-10-02T14:41:18.669075Z",
     "iopub.status.idle": "2026-10-02T14:41:18.672934Z",
     "shell.execute_reply": "2026-10-02T14:41:18.672392Z"
    },
    "papermill": {
     "duration": 0.007266,
     "end_time": "2026-10-02T14:41:18.673355+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.666089+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert('a' in C1._inputs)\n",
    "assert('b' in C1._inputs)\n",
    "assert('x' in C1._outputs)\n",
    "assert_near_equal(C1._outputs['x'], 2.0, 0.00001)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a5e5f96b",
   "metadata": {
    "papermill": {
     "duration": 0.031242,
     "end_time": "2026-10-02T14:41:18.707405+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.676163+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Here is the same model only with one of the variables as a constant."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b96df468",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.783514Z",
     "iopub.status.busy": "2026-10-02T14:41:18.783240Z",
     "iopub.status.idle": "2026-10-02T14:41:18.791086Z",
     "shell.execute_reply": "2026-10-02T14:41:18.790342Z"
    },
    "papermill": {
     "duration": 0.080808,
     "end_time": "2026-10-02T14:41:18.791710+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.710902+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C1.b']\n"
     ]
    }
   ],
   "source": [
    "prob = om.Problem()\n",
    "C1 = prob.model.add_subsystem('C1', om.ExecComp('x = a + b', a={'val': 6, 'constant':True}))\n",
    "prob.setup()\n",
    "prob.set_solver_print(level=0)\n",
    "prob.run_model()\n",
    "print(list(C1._inputs._names))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "8bc3c2eb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:41:18.801248Z",
     "iopub.status.busy": "2026-10-02T14:41:18.801044Z",
     "iopub.status.idle": "2026-10-02T14:41:18.804848Z",
     "shell.execute_reply": "2026-10-02T14:41:18.804196Z"
    },
    "papermill": {
     "duration": 0.010619,
     "end_time": "2026-10-02T14:41:18.806444+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:41:18.795825+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.0)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "assert('a' not in C1._inputs)\n",
    "assert('b' in C1._inputs)\n",
    "assert('x' in C1._outputs)\n",
    "assert_near_equal(C1._outputs['x'], 7.0, 0.00001)"
   ]
  }
 ],
 "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.13.14"
  },
  "orphan": true,
  "papermill": {
   "default_parameters": {},
   "duration": 4.943688,
   "end_time": "2026-10-02T14:41:19.528001+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/features/building_blocks/components/exec_comp.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/features/building_blocks/components/exec_comp.ipynb",
   "parameters": {},
   "start_time": "2026-10-02T14:41:14.584313+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}