{ "cells": [ { "cell_type": "markdown", "id": "f57cd4fe", "metadata": {}, "source": [ "# Tracking the Properties of Real Packets" ] }, { "cell_type": "markdown", "id": "c103617c", "metadata": {}, "source": [ "**TARDIS** has the functionality to track the properties of the *RPackets* that are generated when running the Simulation. The `rpacket_tracker` can track all the interactions a packet undergoes & thus keeps a track of the various properties, a packet may have.
The `rpacket_tracker` tracks the properties of all the rpackets in the *All the Iterations of the Simulation*. It generates a `pandas.DataFrame` that contains the the properties of all the interactions that a particular `RPacket` undergoes as it is propagated throughout the run of the simulation. This happens for all the packets of each respective `Iteration` and are stored along with the `iteration number`. A sample of the *RPacket Tracking DataFrame* can be seen in the end of this tutorial." ] }, { "cell_type": "markdown", "id": "ce6214b9", "metadata": {}, "source": [ "\n", " \n", " The properties that are tracked are as follows :\n", "
    \n", "
  1. index - Index of the Packet
  2. \n", "
  3. seed - Seed of the Packet
  4. \n", "
  5. status - Current Status for the Packet Interaction
  6. \n", "
  7. r - Radius of the Current Shell
  8. \n", "
  9. nu - Packet's Frequency
  10. \n", "
  11. mu - Propagation Direction of the Packet (cosine of the angle the packet’s path makes with the radial direction)
  12. \n", "
  13. energy - Energy of the Packet
  14. \n", "
  15. shell_id - Current Shell Id where the Packet is present
  16. \n", "
\n", " \n", "" ] }, { "cell_type": "markdown", "id": "4b0de6ca", "metadata": {}, "source": [ "
\n", "\n", "Warning\n", "\n", "Turning on the `tracking` option in the config has consequences on the time of the simulation run. Please keep this into consideration as the time for processing the output is high and can lead to long simulation times for simpler runs as well.
Properties can be accessed through the `DataFrame` indexing easily.\n", "
" ] }, { "cell_type": "markdown", "id": "1686d9f1", "metadata": {}, "source": [ "## How to Setup the Tracking for the RPackets?" ] }, { "cell_type": "markdown", "id": "29e14475", "metadata": {}, "source": [ "**TARDIS**' `rpacket_tracker` is configured via the `YAML` file. This functionality of tracking the packets is turned **off**, by default. This is due to the fact that using this property, leads to longer execution time for the Simulation. An example configuration can be seen below for setting up the *tracking*:\n", "\n", "```yaml\n", "... \n", "montecarlo:\n", "...\n", "tracking:\n", " track_rpacket: true\n", "```" ] }, { "cell_type": "markdown", "id": "13b6420b", "metadata": {}, "source": [ "The `montecarlo` section of the **YAML** file has a `tracking` sub section which holds the configuration properties for the `track_rpacket` & the `initial_array_length` (discussed later in the tutorial)." ] }, { "cell_type": "markdown", "id": "2634c571", "metadata": {}, "source": [ "Let us see, the new `rpacket_tracker` in action." ] }, { "cell_type": "code", "execution_count": null, "id": "a0e975b6", "metadata": {}, "outputs": [], "source": [ "from tardis.io.config_reader import Configuration" ] }, { "cell_type": "code", "execution_count": null, "id": "adbf5f75", "metadata": {}, "outputs": [], "source": [ "# Reading the Configuration stored in `tardis_config_packet_tracking.yml` into config\n", "\n", "config = Configuration.from_yaml(\"tardis_tracking_example.yml\")" ] }, { "cell_type": "code", "execution_count": null, "id": "975766e9", "metadata": {}, "outputs": [], "source": [ "# Checking the `tracking` section via the Schema\n", "\n", "config[\"montecarlo\"][\"tracking\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "b00bc2ca", "metadata": {}, "outputs": [], "source": [ "# Setting `r_packet_tracking` to True to turn on the Tracking \n", "\n", "config[\"montecarlo\"][\"tracking\"][\"track_rpacket\"] = True" ] }, { "cell_type": "code", "execution_count": null, "id": "3ece2c10", "metadata": {}, "outputs": [], "source": [ "config[\"montecarlo\"][\"tracking\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "b25271d6", "metadata": {}, "outputs": [], "source": [ "from tardis import run_tardis" ] }, { "cell_type": "code", "execution_count": null, "id": "f9e51fd3", "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Running the simulation from the config\n", "\n", "sim = run_tardis(config, log_level=\"Debug\", show_convergence_plots=False, show_progress_bars=False)" ] }, { "cell_type": "markdown", "id": "532bfafc", "metadata": {}, "source": [ "Now, the `tracked` properties can be accessed via the `rpacket_tracker` attribute of the `sim.runner` object. " ] }, { "cell_type": "code", "execution_count": null, "id": "f8b3424f", "metadata": {}, "outputs": [], "source": [ "type(sim.runner.rpacket_tracker)" ] }, { "cell_type": "markdown", "id": "4771d92a", "metadata": {}, "source": [ "It can be seen from the above code, that the `sim.runner.rpacket_tracker` is an instance of the `pandas.DataFrame` object. The `RPacketCollection` class has the following structure for the properties : {More information in the **TARDIS API** for `RPacketCollection` class}" ] }, { "cell_type": "markdown", "id": "ce587807", "metadata": {}, "source": [ "```python\n", "# Basic structure for the RPacketCollection Class\n", "class RPacketCollection:\n", " # Properties\n", " index\n", " seed\n", " status\n", " r\n", " nu\n", " mu\n", " energy\n", " shell_id\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "a3ea2f54", "metadata": {}, "outputs": [], "source": [ "len(sim.runner.rpacket_tracker)" ] }, { "cell_type": "markdown", "id": "7b3ee39f", "metadata": {}, "source": [ "The generated DataFrame can be accessed with `sim.runner.rpacket_tracker`. The DataFrame for this particular *simulation configuration* can be seen as follows :" ] }, { "cell_type": "code", "execution_count": null, "id": "dbec15ec", "metadata": {}, "outputs": [], "source": [ "sim.runner.rpacket_tracker" ] }, { "cell_type": "markdown", "id": "411f2ef9", "metadata": {}, "source": [ "To access these different properties, we may consider the following examples for the `rpacket_tracker`:\n", "
In this Example, we are trying to access the properties of the packet at index `10`.
In a similar way, we can check for any property for any packet in the range of packets for the last iteration." ] }, { "cell_type": "markdown", "id": "a4772b00", "metadata": {}, "source": [ "- Accessing the `index` property for the packets :" ] }, { "cell_type": "code", "execution_count": null, "id": "de7b8877", "metadata": {}, "outputs": [], "source": [ "sim.runner.rpacket_tracker[\"Packet Index\"]" ] }, { "cell_type": "markdown", "id": "d81fbbf7", "metadata": {}, "source": [ "- Accessing the `seed` property for the packets :" ] }, { "cell_type": "code", "execution_count": null, "id": "39e2dbd2", "metadata": {}, "outputs": [], "source": [ "sim.runner.rpacket_tracker[\"Packet Seed\"].unique()" ] }, { "cell_type": "markdown", "id": "7afe2110", "metadata": {}, "source": [ "- Accessing the `status` property for the packets :" ] }, { "cell_type": "code", "execution_count": null, "id": "e82427ea", "metadata": {}, "outputs": [], "source": [ "sim.runner.rpacket_tracker[\"Packet Status\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "8e66c2f7", "metadata": {}, "outputs": [], "source": [ "sim.runner.rpacket_tracker.loc[sim.runner.rpacket_tracker[\"Iteration\"] == 1]" ] }, { "cell_type": "code", "execution_count": null, "id": "543507be", "metadata": {}, "outputs": [], "source": [ "sim.runner.rpacket_tracker.loc[sim.runner.rpacket_tracker[\"Iteration\"] == 9]" ] }, { "cell_type": "markdown", "id": "ea308a55", "metadata": {}, "source": [ "Thus, all other properties {`r`, `nu`, `mu`, `energy`, `shell_id`} can be accessed accordingly." ] }, { "cell_type": "markdown", "id": "9136fba1", "metadata": {}, "source": [ "
\n", "\n", "Warning\n", "\n", "If we try to access `sim.runner.rpacket_tracker` property when we have the `track_rpacket` property in the `tracking` subsection of `montecarlo` config, turned off as follows `config[\"montecarlo\"][\"tracking\"][\"track_rpacket\"] = False`, it will return `None`. Error will be raised if we try to access the properties i.e. `seed`, `index`, etc.\n", "
" ] }, { "cell_type": "markdown", "id": "afa3c7f4", "metadata": {}, "source": [ "
\n", "\n", "Note\n", " \n", "When we initialise the `RPacketCollection()` class, the properties arrays {`index`, `seed`, `status`, etc} are allocated certain length based on the `initial_array_length` parameter that can be set via the `initial_array_length` property under `montecarlo -> tracking` section of the configuration. The default size of the array is `10`. This variable is important as the number of interactions a packet may have is variable, thus we need to allocate space dynamically. This variable is used to compute the size and expand the array such that the properties are able to hold these values for the packet interaction. Higher number, allocates more space initially leading to lesser times the arrays expands and vice versa. It can be set in the following manner `config[\"montecarlo\"][\"tracking\"][\"initial_array_length\"] = {value}`.\n", "
" ] } ], "metadata": { "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.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }