{
 "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": [
    "# Accessing Subsystems Within a Group\n",
    "",
    "\n",
    "To access a `Component` or another `Group` within a `Group`, just access the attribute with the name\n",
    "of the subsystem.\n",
    "\n",
    "\n",
    "Usage\n",
    "",
    "\n",
    "The class `BranchGroup`, seen here, is used in the example that follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "\n",
    "\n",
    "class BranchGroup(om.Group):\n",
    "\n",
    "    def setup(self):\n",
    "        b1 = self.add_subsystem('Branch1', om.Group())\n",
    "        g1 = b1.add_subsystem('G1', om.Group())\n",
    "        g2 = g1.add_subsystem('G2', om.Group())\n",
    "        g2.add_subsystem('comp1', om.ExecComp('b=2.0*a', a=3.0, b=6.0))\n",
    "\n",
    "        b2 = self.add_subsystem('Branch2', om.Group())\n",
    "        g3 = b2.add_subsystem('G3', om.Group())\n",
    "        g3.add_subsystem('comp2', om.ExecComp('b=3.0*a', a=4.0, b=12.0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows accessing components that are two nested branches from the top."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = om.Problem(model=BranchGroup())\n",
    "p.setup()\n",
    "\n",
    "c1 = p.model.Branch1.G1.G2.comp1\n",
    "print(c1.pathname)\n",
    "\n",
    "c2 = p.model.Branch2.G3.comp2\n",
    "print(c2.pathname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert(c1.pathname == 'Branch1.G1.G2.comp1')\n",
    "assert(c2.pathname == 'Branch2.G3.comp2')"
   ]
  }
 ],
 "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
}
