{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "upset-transaction",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:45:01.285752Z",
     "iopub.status.busy": "2026-10-02T14:45:01.282064Z",
     "iopub.status.idle": "2026-10-02T14:45:01.293476Z",
     "shell.execute_reply": "2026-10-02T14:45:01.292547Z"
    },
    "papermill": {
     "duration": 0.016954,
     "end_time": "2026-10-02T14:45:01.294080+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:01.277126+00:00",
     "status": "completed"
    },
    "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]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "allied-means",
   "metadata": {
    "papermill": {
     "duration": 0.002517,
     "end_time": "2026-10-02T14:45:01.298674+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:01.296157+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# Moving Some Systems Outside of the Optimization Loop\n",
    "\n",
    "Sometimes a model contains systems that don't need to be executed during each iteration of an\n",
    "optimization loop.  If those systems are expensive to execute, leaving them out of the optimization\n",
    "loop can significantly improve performance.\n",
    "\n",
    "If the `Problem` option `group_by_pre_opt_post` is True, OpenMDAO will separate all of the \n",
    "Components in the model into `pre`, `optimization`, or `post` sets based on the dependency graph. \n",
    "Only those Components found in\n",
    "the `optimization` set will be executed during each iteration of the optimization, while the ones\n",
    "in the `pre` and `post` sets will only execute before or after the optimization loop.\n",
    "\n",
    "It's also possible, by setting the `always_opt` option on a component, to force that component\n",
    "to be included in the optimization loop regardless of its dependencies.  Note that depending\n",
    "on what its dependecies actually are, moving it to the optimization loop may also force other\n",
    "components to move there even if they would normally be in `pre` or `post`.\n",
    "\n",
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "73798c74",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:45:01.460964Z",
     "iopub.status.busy": "2026-10-02T14:45:01.460721Z",
     "iopub.status.idle": "2026-10-02T14:45:04.593141Z",
     "shell.execute_reply": "2026-10-02T14:45:04.592540Z"
    },
    "papermill": {
     "duration": 3.139235,
     "end_time": "2026-10-02T14:45:04.593790+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:01.454555+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1790952304.279482] [runnervm8df0l:9300 :0]        ib_iface.c:1269 UCX  ERROR mana_0: iface 0x558ac2716420 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",
      "[1790952304.279750] [runnervm8df0l:9300 :0]      ucp_worker.c:1412 UCX  ERROR uct_iface_open(ud_verbs/mana_0:1) failed: Input/output error\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[runnervm8df0l:09300] pml_ucx.c:313  Error: Failed to create UCP worker\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Problem: problem\n",
       "Driver:  ScipyOptimizeDriver\n",
       "  success     : True\n",
       "  iterations  : 3\n",
       "  runtime     : 2.5241E-01 s\n",
       "  model_evals : 4\n",
       "  model_time  : 1.4281E-01 s\n",
       "  deriv_evals : 2\n",
       "  deriv_time  : 1.2517E-01 s\n",
       "  exit_status : SUCCESS"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.exec_comp_for_test import ExecComp4Test\n",
    "\n",
    "size = 3\n",
    "\n",
    "prob = om.Problem(group_by_pre_opt_post=True)\n",
    "prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', disp=False)\n",
    "\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('pre1', ExecComp4Test('y=2.*x', x=np.ones(size), y=np.zeros(size)))\n",
    "model.add_subsystem('pre2', ExecComp4Test('y=3.*x', x=np.ones(size), y=np.zeros(size)))\n",
    "\n",
    "model.add_subsystem('iter1', ExecComp4Test('y=x1 + x2*4. + x3',\n",
    "                                            x1=np.ones(size), x2=np.ones(size),\n",
    "                                            x3=np.ones(size), y=np.zeros(size)))\n",
    "model.add_subsystem('iter2', ExecComp4Test('y=.5*x', x=np.ones(size), y=np.zeros(size)))\n",
    "model.add_subsystem('iter4', ExecComp4Test('y=7.*x', x=np.ones(size), y=np.zeros(size)))\n",
    "model.add_subsystem('iter3', ExecComp4Test('y=6.*x', x=np.ones(size), y=np.zeros(size)))\n",
    "\n",
    "model.add_subsystem('post1', ExecComp4Test('y=8.*x', x=np.ones(size), y=np.zeros(size)))\n",
    "model.add_subsystem('post2', ExecComp4Test('y=x1*9. + x2*5', x1=np.ones(size),\n",
    "                                            x2=np.ones(size), y=np.zeros(size)))\n",
    "\n",
    "# we don't want ExecComps to be colored because it makes the iter counting more complicated\n",
    "for comp in model.system_iter(typ=ExecComp4Test):\n",
    "    comp.options['do_coloring'] = False\n",
    "    comp.options['has_diag_partials'] = True\n",
    "\n",
    "\n",
    "model.connect('pre1.y', ['iter1.x1', 'post2.x1'])\n",
    "model.connect('pre2.y', 'iter1.x2')\n",
    "model.connect('iter1.y', ['iter2.x', 'iter4.x'])\n",
    "model.connect('iter2.y', 'post2.x2')\n",
    "model.connect('iter3.y', 'post1.x')\n",
    "model.connect('iter4.y', 'iter3.x')\n",
    "\n",
    "prob.model.add_design_var('iter1.x3', lower=0, upper=10)\n",
    "prob.model.add_constraint('iter2.y', upper=10.)\n",
    "prob.model.add_objective('iter3.y', index=0)\n",
    "\n",
    "prob.setup()\n",
    "prob.run_driver()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bea1bf63",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-10-02T14:45:04.633887Z",
     "iopub.status.busy": "2026-10-02T14:45:04.633398Z",
     "iopub.status.idle": "2026-10-02T14:45:04.807110Z",
     "shell.execute_reply": "2026-10-02T14:45:04.805748Z"
    },
    "papermill": {
     "duration": 0.21272,
     "end_time": "2026-10-02T14:45:04.808064+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:04.595344+00:00",
     "status": "completed"
    },
    "tags": [
     "remove-input",
     "remove-output",
     "allow-assert"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_check_totals\n",
    "\n",
    "assert prob.model.pre1.num_nl_solves == 1\n",
    "assert prob.model.pre2.num_nl_solves == 1\n",
    "\n",
    "assert prob.model.iter1.num_nl_solves == 3\n",
    "assert prob.model.iter2.num_nl_solves == 3\n",
    "assert prob.model.iter3.num_nl_solves == 3\n",
    "assert prob.model.iter4.num_nl_solves == 3\n",
    "\n",
    "assert prob.model.post1.num_nl_solves == 1\n",
    "assert prob.model.post2.num_nl_solves == 1\n",
    "\n",
    "data = prob.check_totals(out_stream=None)\n",
    "assert_check_totals(data)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "522e97b4",
   "metadata": {
    "papermill": {
     "duration": 0.075697,
     "end_time": "2026-10-02T14:45:04.885608+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:04.809911+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## Check pre/post via the command line\n",
    "\n",
    "Use the `openmdao list_pre_post` command to view the sets of components found in the `pre` and `post` sets.  Any components not shown will be found in the `optimization` set and will run during each iteration of the optimization loop.\n",
    "Running the command on a script containing the example model above would show the following:"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "17ac1495",
   "metadata": {
    "papermill": {
     "duration": 0.002079,
     "end_time": "2026-10-02T14:45:04.889318+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:04.887239+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "```\n",
    "    Pre-optimization systems:\n",
    "        pre1\n",
    "        pre2\n",
    "\n",
    "    Post-optimization systems:\n",
    "        post1\n",
    "        post2\n",
    "```"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "115f00b0",
   "metadata": {
    "papermill": {
     "duration": 0.001194,
     "end_time": "2026-10-02T14:45:04.891798+00:00",
     "exception": false,
     "start_time": "2026-10-02T14:45:04.890604+00:00",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "Note that the `pre` and `post` sets will only be shown if the `driver` is an optimizer and the `group_by_pre_opt_post` option of the `Problem` is True."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Edit Metadata",
  "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": 5.156667,
   "end_time": "2026-10-02T14:45:05.532483+00:00",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/openmdao_book/features/experimental/pre_opt_post.ipynb",
   "output_path": "/home/runner/work/OpenMDAO/OpenMDAO/openmdao/docs/_executed_book/features/experimental/pre_opt_post.ipynb",
   "parameters": {},
   "start_time": "2026-10-02T14:45:00.375816+00:00",
   "version": "2.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}