{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Montecarlo Packet Visualization"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`RPacketPlotter` plots the `RPackets` that are generated by the [Montecarlo](https://tardis-sn.github.io/tardis/physics/montecarlo/index.html) method and creates an animated plot that contains the packet trajectories as they move away from the photosphere.\n",
    "The properties of individual RPackets are taken from the [rpacket_tracker](https://tardis-sn.github.io/tardis/io/output/rpacket_tracking.html).\n",
    "\n",
    "`RPacketPlotter` uses the properties (specifically, `mu` and `r`) present in the `rpacket_tracker` to calculate the coordinates of packets as they move through the ejecta. In the following section, the mathematical expression for getting the angle(θ) of packets with respect to the x-axis is shown, which can be used (along with radius `r`) to calculate the x and y coordinates of packets."
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting packet coordinates\n",
    "\n",
    "`RPacketPlotter` uses the properties (specifically, `mu` and `r`) present in the `rpacket_tracker` to calculate the coordinates of packets as they move through the ejecta. In the following section, the mathematical expression for getting the angle(θ) of packets with respect to the x-axis is shown, which can be used (along with radius `r`) to calculate the x and y coordinates of packets.\n",
    "<br><br>\n",
    "<img src=\"attachment:packet_diagram.jpg\" style=\"width:400px\">\n",
    "\n",
    "<br>The diagram above shows the packet trajectory as it starts from photosphere `P0` and continues to move along the subsequent points `P1`, `P2`, and so on.\n",
    "\n",
    "<div class=\"alert alert-info\">\n",
    "\n",
    "Note\n",
    "    \n",
    "Here `μ` represents the direction of packet propagation with respect to the radial line.\n",
    "    \n",
    "</div>\n",
    "\n",
    "To determine the polar coordinates of any arbitrary point, say `P2`, we need `r2` and `θ2`. `r2` is already present in the array obtained from the simulation. To determine `θ2`, we use the sine rule and apply it to the triangle `OP1P2`, where `O` is the center.\n",
    "\n",
    "$$\n",
    "\\frac{r_{2}}{\\sin(\\pi - \\mu_{1})} = \\frac{r_{1}}{\\sin(\\alpha)}\n",
    "$$\n",
    "\n",
    "Now, writing `α` in terms of `μ1` and `θ2`\n",
    "\n",
    "$$ \n",
    "α = μ_{1} - θ_{2}\n",
    "$$\n",
    "$$\n",
    "\\frac{r_{2}}{\\sin(\\pi - \\mu_{1})} = \\frac{r_{1}}{\\sin(μ_{1} - θ_{2})}\n",
    "$$\n",
    "\n",
    "Thus,\n",
    "\n",
    "$$ \n",
    "θ_{2} = -\\sin^{-1}(\\frac{r1}{r2}\\sin(\\mu_{1})) + \\mu_{1}\n",
    "$$\n",
    "\n",
    "Hence, for `i-th` point, `θ` will be:\n",
    "\n",
    "$$ \n",
    "θ_{i} = -\\sin^{-1}(\\frac{r_{i-1}}{r_{i}}\\sin(\\mu_{i-1})) + \\mu_{i-1}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running the simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tardis import run_tardis\n",
    "from tardis.io.configuration.config_reader import Configuration\n",
    "from tardis.io.atom_data import download_atom_data\n",
    "\n",
    "# We download the atomic data needed to run the simulation\n",
    "download_atom_data('kurucz_cd23_chianti_H_He')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reading the Configuration stored in `tardis_example.yml` into config\n",
    "\n",
    "config = Configuration.from_yaml(\"tardis_example.yml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# changing config file for enabling the rpacket_tracking\n",
    "\n",
    "config[\"montecarlo\"][\"tracking\"][\"track_rpacket\"]=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "sim = run_tardis(config, show_progress_bars=False)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotting Packets with RPacketPlotter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Importing the RPacketPlotter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tardis.visualization import RPacketPlotter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, we create an RPacketPlotter object `rpacket_plotter` that will be used to generate a plot.\n",
    "\n",
    "`no_of_packets` can be specified as a parameter to the `from_simulation` class method. By default, `15` packets will used to create a plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rpacket_plotter = RPacketPlotter.from_simulation(sim, no_of_packets=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the `rpacket_plotter` we use the `generate_plot` method to create a plot.\n",
    "\n",
    "Here the `theme` parameter can be defined. Currently, we have 2 themes, i.e. `light` and `dark`. By Default the `light` theme will be plotted."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Light Theme"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rpacket_plotter.generate_plot().show(renderer=\"notebook_connected\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dark Theme"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rpacket_plotter.generate_plot(theme=\"dark\").show(renderer=\"notebook_connected\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Animation and Other interactive features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Play` button at the bottom-left can be used to start the animation. The animation can be paused at any time using the `pause` button. Also, the `timeline slider` can be used to reach any point in the animation. A demo is shown below.\n",
    "<br><br>\n",
    "<img src=\"attachment:ezgif.com-gif-maker.gif\" style=\"width:800px\">\n",
    "<br><br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hovering over any packet will show its properties like its coordinates, interaction type, etc. The `zoom-in` feature can be used to view a particular part of the plot. The demo below shows these features.\n",
    "<br><br>\n",
    "<img src=\"attachment:ezgif.com-gif-maker%20%281%29.gif\" style=\"width:800px\">"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "6890d83cfa6c65bebc4bd449872c06a268510d8b8ae8c923ce6786e6c4796836"
  },
  "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}