{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ee8e8873",
   "metadata": {},
   "source": [
    "# Using the Real-time Optimization and Analysis Progress Plots\n",
    "\n",
    "The real-time optimization and analysis progress plots feature allows the user to see, in real-time, the progress of an optimization or analysis run.\n",
    "\n",
    "There are two different plots for optimization and analysis. These will be explained below."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c48021c2",
   "metadata": {},
   "source": [
    "## Using the rtplot command\n",
    "\n",
    "Plotting is done using the `opendmao rtplot` subcommand. The command can be used for both optimization and analysis runs. The command detects what type of `Driver` is being used and generates the appropriate plot. The plotting is done using a browser-based application.\n",
    "\n",
    "In addition to plotting in real-time, as a script runs, the command can be used for plotting after a run by having the command read a case recorder file. These examples will explain the 4 different situations:\n",
    "\n",
    "### Real-time plotting of optimization run\n",
    "```\n",
    "openmdao rtplot path_to_optimization_script.py\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. \n",
    "\n",
    "### Real-time plotting of analysis run\n",
    "Similar to the optimiztion example, if the script is an analysis run, use a command of this form.\n",
    "\n",
    "```\n",
    "openmdao rtplot path_to_analysis_script.py\n",
    "```\n",
    "\n",
    "### Post run plotting of optimization run\n",
    "If an optimization run has been completed and the user would like to see a post-run plot, use a command like this. \n",
    "\n",
    "```\n",
    "openmdao rtplot name_of_optimization_script_out/cases.sql\n",
    "```\n",
    "\n",
    "### Post run plotting of analysis run\n",
    "Similary, if an analysis run is complete, use this form of the command. \n",
    "\n",
    "```\n",
    "openmdao rtplot name_of_analysis_script_out/cases.sql\n",
    "```\n"
   ]
  },
  {
   "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": "2ac29bfc",
   "metadata": {},
   "source": []
  },
  {
   "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."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15e5b104",
   "metadata": {},
   "source": [
    "# Real-time Analysis Progress Plot User Interface\n",
    "\n",
    "Here is an example of an analysis plot.\n",
    "\n",
    "![real_time_optimization_progress_plot](images/real_time_analysis_progress_plot.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "175075b7",
   "metadata": {},
   "source": [
    "On the diagonal of the visualization on the right, histograms show the sampled variable distributions. \n",
    "\n",
    "Below the diagonal, are scatter plots with pairs of variables along the x and y axes of the plots. The dots in the plot represent one run of the analysis. The x and y location are determined by the sampled variable pairs. The color of the dot represents the value of the response variable.\n",
    "\n",
    "The user can modify what response variable is being visualized using the menu on the left. \n",
    "\n",
    "They can also modify which sampled variables are shown using the checkboxes on the left. Initially only up to four are plotted because of space limitations. But more can be added as desired by the user. \n",
    "\n",
    "There is also a section on the left showing non-scalar samples variables. In this version of OpenMDAO, these cannot be plotted in this visualization but are shown for completeness. \n",
    "\n",
    "The visualization also includes some basic run information in the box in the upper left. \n",
    "\n",
    "In the upper left, is a box with basic information about the run. \n",
    "\n",
    "Below that, is where the user can select what is being plotted. The "
   ]
  }
 ],
 "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
}
