{
 "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\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Setting the Order of Subsystems in a Group\n",
    "",
    "\n",
    "By default, subsystems are executed in the same order in which they were added to\n",
    "their parent Group.  In order to change this order, use the `set_order` method.\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.core.group.Group.set_order\n",
    "        :noindex:\n",
    "```\n",
    "\n",
    "The list of names provided to `set_order` must contain every subsystem that has\n",
    "been added to the Group.\n",
    "\n",
    "```{Note}\n",
    "Use caution when setting the order of execution of your subsystems, whether\n",
    "by just calling `add_subsystem` in a specific order, or by later changing\n",
    "the order using `set_order`.  If you choose an order that doesn't follow\n",
    "the natural data flow order of your subsystems, your model may take longer\n",
    "to converge.\n",
    "```\n",
    "\n",
    "## Usage\n",
    "",
    "\n",
    "Change the execution order of components *C1* and *C3*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class ReportOrderComp(om.ExplicitComponent):\n",
    "    \"\"\"Adds name to list.\"\"\"\n",
    "\n",
    "    def __init__(self, order_list):\n",
    "        super().__init__()\n",
    "        self._order_list = order_list\n",
    "\n",
    "    def compute(self, inputs, outputs):\n",
    "        self._order_list.append(self.pathname)\n",
    "\n",
    "# this list will record the execution order of our C1, C2, and C3 components\n",
    "order_list = []\n",
    "\n",
    "prob = om.Problem()\n",
    "model = prob.model\n",
    "\n",
    "model.add_subsystem('C1', ReportOrderComp(order_list))\n",
    "model.add_subsystem('C2', ReportOrderComp(order_list))\n",
    "model.add_subsystem('C3', ReportOrderComp(order_list))\n",
    "\n",
    "prob.setup()\n",
    "prob.run_model()\n",
    "\n",
    "print(order_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(order_list == ['C1', 'C2', 'C3'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# reset the shared order list\n",
    "order_list[:] = []\n",
    "\n",
    "prob.setup()\n",
    "# now swap C2 and C1 in the order\n",
    "model.set_order(['C2', 'C1', 'C3'])\n",
    "\n",
    "# after changing the order, we must call setup again\n",
    "prob.setup()\n",
    "prob.run_model()\n",
    "\n",
    "print(order_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(order_list == ['C2', 'C1', 'C3'])"
   ]
  }
 ],
 "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.1"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
