{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output",
     "active-ipynb"
    ]
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    from openmdao.utils.notebook_utils import notebook_mode\n",
    "except ImportError:\n",
    "    !python -m pip install openmdao[notebooks]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.assert_utils import assert_near_equal\n",
    "\n",
    "import os\n",
    "if os.path.exists('cases.sql'):\n",
    "    os.remove('cases.sql')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Case Reader\n",
    "\n",
    "The `CaseReader` object is provided to read case recordings, regardless of which case recorder was used. \n",
    "\n",
    "Currently, OpenMDAO only implements `SqliteCaseRecorder`, therefore all the examples will make use of this recorder. Other types of case recorders are expected to be supported in the future."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CaseReader Constructor\n",
    "\n",
    "The call signature for the `CaseReader` constructor is:\n",
    "\n",
    "```{eval-rst}\n",
    "    .. automethod:: openmdao.recorders.sqlite_reader.SqliteCaseReader.__init__\n",
    "        :noindex:\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Determining What Sources and Variables Were Recorded\n",
    "\n",
    "The `CaseReader` object provides methods to determine which objects in the original problem were sources\n",
    "for for the recorded cases and what variables they recorded. Sources can include the problem, driver, components and solvers.\n",
    "\n",
    "The `list_sources` method provides a list of the names of objects that are the sources of recorded data\n",
    "in the file.\n",
    "\n",
    "```{eval-rst}\n",
    ".. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.list_sources\n",
    "    :noindex:\n",
    "```\n",
    "\n",
    "The complementary `list_source_vars` method will provide a list of the input and output variables recorded\n",
    "for a given source.\n",
    "\n",
    "```{eval-rst}\n",
    ".. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.list_source_vars\n",
    "    :noindex:\n",
    "```\n",
    "\n",
    "Here is an example of their usage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "from openmdao.utils.notebook_utils import get_code\n",
    "from myst_nb import glue\n",
    "glue(\"code_src86\", get_code(\"openmdao.test_suite.components.sellar_feature.SellarMDA\"), display=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ":::{Admonition} `SellarMDA` class definition \n",
    ":class: dropdown\n",
    "\n",
    "{glue:}`code_src86`\n",
    ":::"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.sellar_feature import SellarMDA\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "# define Sellar MDA problem\n",
    "prob = om.Problem(model=SellarMDA())\n",
    "\n",
    "model = prob.model\n",
    "model.add_design_var('z', lower=np.array([-10.0, 0.0]),\n",
    "                          upper=np.array([10.0, 10.0]))\n",
    "model.add_design_var('x', lower=0.0, upper=10.0)\n",
    "model.add_objective('obj')\n",
    "model.add_constraint('con1', upper=0.0)\n",
    "model.add_constraint('con2', upper=0.0)\n",
    "\n",
    "prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-9, disp=False)\n",
    "\n",
    "# add recorder to the driver, model and solver\n",
    "recorder = om.SqliteRecorder('cases.sql')\n",
    "\n",
    "prob.driver.add_recorder(recorder)\n",
    "model.add_recorder(recorder)\n",
    "model.nonlinear_solver.add_recorder(recorder)\n",
    "\n",
    "# run the problem\n",
    "prob.setup()\n",
    "prob.set_solver_print(0)\n",
    "prob.run_driver()\n",
    "prob.cleanup()\n",
    "\n",
    "cr = om.CaseReader('cases.sql')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sources = cr.list_sources()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert sorted(sources) == ['driver', 'root', 'root.nonlinear_solver']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "driver_vars = cr.list_source_vars('driver')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert driver_vars['inputs'] == []\n",
    "assert set(driver_vars['outputs']) == {'con1', 'con2', 'obj', 'x', 'z'}\n",
    "assert driver_vars['residuals'] == []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_vars = cr.list_source_vars('root')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert set(model_vars['inputs']) == {'x', 'y1', 'y2', 'z'}\n",
    "assert set(model_vars['outputs']) == {'con1', 'con2', 'obj', 'x', 'y1', 'y2', 'z'}\n",
    "assert set(model_vars['residuals']) == {'con1', 'con2', 'obj', 'x', 'y1', 'y2', 'z'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "solver_vars = cr.list_source_vars('root.nonlinear_solver')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert set(solver_vars['inputs']) == {'x', 'y1', 'y2', 'z'}\n",
    "assert set(solver_vars['outputs']) == {'con1', 'con2', 'obj', 'x', 'y1', 'y2', 'z'}\n",
    "assert solver_vars['residuals'] == []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case Names\n",
    "\n",
    "The `CaseReader` provides access to `Case` objects, each of which encapsulates a data point recorded by\n",
    "one of the sources.\n",
    "\n",
    "`Case` objects are uniquely identified in a case recorder file by their case names. A case name is a string.\n",
    "As an example, here is a case name:\n",
    "\n",
    "    'rank0:ScipyOptimize_SLSQP|1|root._solve_nonlinear|1'\n",
    "\n",
    "The first part of the case name indicates which rank or process that the case was recorded from. \n",
    "The remainder of the case name shows the hierarchical path to the object that was recorded along \n",
    "with the iteration counts for each object along the path. It follows a pattern of repeated pairs of\n",
    "\n",
    "    - object name ( problem, driver, system, or solver )\n",
    "    - iteration count\n",
    "\n",
    "These are separated by the `|` character.\n",
    "\n",
    "So in the given example, the case is:\n",
    "\n",
    "    - from rank 0\n",
    "    - the first iteration of the driver, `ScipyOptimize_SLSQP`\n",
    "    - the first execution of the `root` system which is the top-level model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Getting Names of the Cases\n",
    "\n",
    "The `list_cases` method returns the names of the cases in the order in which\n",
    "the cases were executed. You can optionally request cases only from a specific `source`.\n",
    "\n",
    "```{eval-rst}\n",
    ".. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.list_cases\n",
    "    :noindex:\n",
    "```\n",
    "\n",
    "There are two optional arguments to the `list_cases` method that affect what is returned.\n",
    "\n",
    "    - recurse: causes the returned value to include child cases.\n",
    "\n",
    "    - flat: works in conjunction with the `recurse` argument to determine if the returned\n",
    "      results are in the form of a list or nested dict. If recurse=True, flat=False, and there\n",
    "      are child cases, then the returned value is a nested ordered dict. Otherwise, it is a list.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## Accessing Cases\n",
    "\n",
    "Getting information from the cases is a two-step process. First, you need to get access to the Case object and then you can call a variety of methods on the Case object to get values from it. The second step is described on the [Getting Data from a Case](case_reader_data.ipynb) page.\n",
    "\n",
    "There are two methods used to get a specific `Case`:\n",
    "\n",
    "    - get_cases\n",
    "    - get_case"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Accessing Cases Using get_cases Method\n",
    "\n",
    "The `get_cases` method provides a quick and easy way to iterate over all the cases.\n",
    "\n",
    "```{eval-rst}\n",
    ".. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.get_cases\n",
    "    :noindex:\n",
    "```\n",
    "This method is similar to the `list_cases` method in that it has the two optional arguments\n",
    "`recurse` and `flat` to control what is returned as described above.\n",
    "\n",
    "Here is an example of its usage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.sellar_feature import SellarMDA\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "prob = om.Problem(model=SellarMDA())\n",
    "\n",
    "model = prob.model\n",
    "model.add_design_var('z', lower=np.array([-10.0, 0.0]),\n",
    "                          upper=np.array([10.0, 10.0]))\n",
    "model.add_design_var('x', lower=0.0, upper=10.0)\n",
    "model.add_objective('obj')\n",
    "model.add_constraint('con1', upper=0.0)\n",
    "model.add_constraint('con2', upper=0.0)\n",
    "\n",
    "driver = prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-5)\n",
    "driver.add_recorder(om.SqliteRecorder('cases.sql'))\n",
    "\n",
    "prob.setup()\n",
    "prob.set_solver_print(0)\n",
    "prob.run_driver()\n",
    "prob.cleanup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "cr = om.CaseReader('cases.sql')\n",
    "cases = cr.get_cases()\n",
    "for case in cases:\n",
    "    print(case.name, sorted(case.outputs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert len(cases) == driver.iter_count\n",
    "for i, case in enumerate(cases):\n",
    "    assert case.name == f\"rank0:ScipyOptimize_SLSQP|{i}\", f\"unexpected case name: {case.name}\"\n",
    "    assert sorted(case.outputs) == ['con1', 'con2', 'obj', 'x', 'z']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### Accessing Cases Using get_case Method\n",
    "\n",
    "The `get_case` method returns a `Case` object given a case name.\n",
    "```{eval-rst}\n",
    ".. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.get_case\n",
    "    :noindex:\n",
    "```\n",
    "You can use the `get_case` method to get a specific case from the list of case names\n",
    "returned by `list_cases` as shown here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "    case_names = cr.list_cases()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert len(case_names) == driver.iter_count\n",
    "for i, case_name in enumerate(case_names):\n",
    "    assert case_name == f\"rank0:ScipyOptimize_SLSQP|{i}\", f\"unexpected case name: {case_name}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# access a Case by name (e.g. first case)\n",
    "case = cr.get_case(\"rank0:ScipyOptimize_SLSQP|0\")\n",
    "print(case.name, sorted(case.outputs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert case.name == \"rank0:ScipyOptimize_SLSQP|0\", f\"unexpected case name: {case.name}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# access a Case by index (e.g. first case)\n",
    "case = cr.get_case(0)\n",
    "print(case.name, sorted(case.outputs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert case.name == \"rank0:ScipyOptimize_SLSQP|0\", f\"unexpected case name: {case.name}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# access a Case by index (e.g. last case)\n",
    "case = cr.get_case(-1)\n",
    "print(case.name, sorted(case.outputs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-input",
     "remove-output"
    ]
   },
   "outputs": [],
   "source": [
    "assert case.name == f\"rank0:ScipyOptimize_SLSQP|{driver.iter_count-1}\", f\"unexpected case name: {case.name}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# get each case by looping over case names\n",
    "for name in case_names:\n",
    "    case = cr.get_case(name)\n",
    "    print(case.name, sorted(case.outputs))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Processing a Nested Dictionary of Its Child Cases\n",
    "\n",
    "The following example demonstrates selecting a case from a case list and processing a nested\n",
    "dictionary of its child cases."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openmdao.api as om\n",
    "from openmdao.test_suite.components.sellar_feature import SellarMDA\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "# define Sellar MDA problem\n",
    "prob = om.Problem(model=SellarMDA())\n",
    "\n",
    "model = prob.model\n",
    "model.add_design_var('z', lower=np.array([-10.0, 0.0]),\n",
    "                          upper=np.array([10.0, 10.0]))\n",
    "model.add_design_var('x', lower=0.0, upper=10.0)\n",
    "model.add_objective('obj')\n",
    "model.add_constraint('con1', upper=0.0)\n",
    "model.add_constraint('con2', upper=0.0)\n",
    "\n",
    "prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-5)\n",
    "\n",
    "# add recorder to the driver, model and solver\n",
    "recorder = om.SqliteRecorder('cases.sql')\n",
    "\n",
    "prob.driver.add_recorder(recorder)\n",
    "model.add_recorder(recorder)\n",
    "model.nonlinear_solver.add_recorder(recorder)\n",
    "\n",
    "# run the problem\n",
    "prob.setup()\n",
    "prob.set_solver_print(0)\n",
    "prob.run_driver()\n",
    "prob.cleanup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cr = om.CaseReader('cases.sql')\n",
    "\n",
    "# get the last driver case\n",
    "driver_cases = cr.list_cases('driver')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# get a recursive dict of child cases of the last driver case\n",
    "last_driver_case = driver_cases[-1]\n",
    "cases = cr.get_cases(last_driver_case, recurse=True, flat=False)\n",
    "\n",
    "# display selected information from nested dict of cases\n",
    "def print_cases(cases, indent=0):\n",
    "    for case, children in cases.items():\n",
    "        print(indent*' ', case.source, '-', case.name.split('.')[-1], sorted(case.outputs))\n",
    "        if children:\n",
    "            print_cases(children, indent+2)\n",
    "            \n",
    "print_cases(cases)"
   ]
  }
 ],
 "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": 5
}
