{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ee8e8873",
   "metadata": {},
   "source": [
    "# Using the Real-time Optimization Progress Plot\n",
    "\n",
    "The real-time optimization progress plot feature allows the user to see, in real-time, the progress of an optimization run by plotting design variables, constraints, and the objective.\n",
    "\n",
    "There two different ways that the plot can be generated."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c48021c2",
   "metadata": {},
   "source": [
    "## Running the plot as part of an optimization run\n",
    "\n",
    "Likely the most common way users will use this feature is to run the optimization script is using the `opendmao rtplot` subcommand.\n",
    "\n",
    "```\n",
    "openmdao rtplot path_to_script.py\n",
    "```\n",
    "\n",
    "This will run the optimization script and also automatically launch the plotting app in a browser window at the appropriate time during the optimization run. \n",
    "\n",
    "The plot will then be periodically be updated as the results of driver iterations complete. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5acdecaf",
   "metadata": {},
   "source": [
    "```{important}\n",
    "Plotting of the driver iterations will only work if the script has a case recorder attached to the `Driver`.\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10ba0143",
   "metadata": {},
   "source": [
    "## Running the plot separate from an optimization run\n",
    "\n",
    "This is used when you want to directly point the plotting application at a case recorder file that contains driver iteration information. \n",
    "\n",
    "For example, let's say you have an optimization script named `optimize_system.py`. In that script, a case recorder is attached to the `Driver` like this:\n",
    "\n",
    "```\n",
    "prob.driver.add_recorder(SqliteRecorder(\"system_driver_recording.sql\"))\n",
    "```\n",
    "if you run that script:\n",
    "\n",
    "```\n",
    "python optimize_system.py\n",
    "```\n",
    "\n",
    "The result will be a case recorder file in \n",
    "\n",
    "```\n",
    "optimize_system_out/system_driver_recording.sql\n",
    "\n",
    "```\n",
    "\n",
    "Then to see a plot of the optimization progress, or see the results after the script is complete:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ac29bfc",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c1860288",
   "metadata": {},
   "source": [
    "```\n",
    "openmdao realtime_opt_plot optimize_system_out/system_driver_recording.sql\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83cdd5bc",
   "metadata": {},
   "source": [
    "## Real-time Optimization Progress Plot User Interface\n",
    "\n",
    "The plot is very easy to use. Here is an example of what it can look like\n",
    "\n",
    "![real_time_optimization_progress_plot](images/real_time_optimization_progress_plot.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2c5dbc4",
   "metadata": {},
   "source": [
    "Initially, only the objective is plotted. Additional variables can be plotted by clicking on the button containing their name in the list on the right. In the example above all of them have already been selected and plotted. \n",
    "\n",
    "The objective axis is always on the left. Additional axes are created on the right as the user selects other variables to plot. The colors of the line, button, and axis label match.\n",
    "\n",
    "The variables buttons also include information about the units and, if the variable is a vector, the shape of the vector. \n",
    "\n",
    "For a design variable that is a vector, like z in the plot, the plot shows the range of all the values in the vector, with a solid fill between the min and the max values. \n",
    "\n",
    "For constraints, if they are vectors, the plot shows the worst case constraint among the values of the vector, using the infinity norm function, which returns the maximum absolute value among all elements in the vector. \n",
    "\n",
    "The optimization plot is created by running a small, local server using the Bokeh plotting library. To stop the server, the user can click on the Quit Application button in the upper right of the plot page."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.4"
  },
  "orphan": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
