{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "f57cd4fe", "metadata": {}, "source": [ "# Tracking the Properties of Real Packets" ] }, { "attachments": {}, "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.
Currently, the `rpacket_tracker` tracks the properties of all the rpackets in the *Last Iteration of the Simulation*. It generates a `List` that contains the individual instances of `RPacketCollection`{`Numba JITClass`}, for storing all the interaction properties as listed below." ] }, { "attachments": {}, "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", "
  17. interaction_type - Last Interaction type of the packet
  18. \n", "
\n", " \n", "" ] }, { "attachments": {}, "cell_type": "markdown", "id": "4b0de6ca", "metadata": {}, "source": [ "The data can be obtained in two ways i.e. `rpacket_tracker` and `rpacket_tracker_df`. The `rpacket_tracker` stores all the data for the interaction of the packets in a `list`, so it needs to accessed with a `list index` for each property for a particular `rpacket`. `rpacket_tracker_df` stores the data in a dataframe. Examples for the same are shown as follows. " ] }, { "attachments": {}, "cell_type": "markdown", "id": "1686d9f1", "metadata": {}, "source": [ "## How to Setup the Tracking for the RPackets?" ] }, { "attachments": {}, "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 that fact that using this property, may slow down the 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", "```" ] }, { "attachments": {}, "cell_type": "markdown", "id": "13b6420b", "metadata": {}, "source": [ "The `montecarlo` section of the **YAML** file now has a `tracking` sub section which holds the configuration properties for the `track_rpacket` & the `initial_array_length` (discussed later in the tutorial)." ] }, { "attachments": {}, "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.configuration.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_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\n", "from tardis.io.atom_data.util import download_atom_data" ] }, { "cell_type": "code", "execution_count": null, "id": "7d8471c3", "metadata": {}, "outputs": [], "source": [ "download_atom_data('kurucz_cd23_chianti_H_He')" ] }, { "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, show_convergence_plots=False, show_progress_bars=False)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "532bfafc", "metadata": {}, "source": [ "Now, the `tracked` properties can be accessed via the `rpacket_tracker` attribute of the `sim.transport` object. " ] }, { "cell_type": "code", "execution_count": null, "id": "f8b3424f", "metadata": {}, "outputs": [], "source": [ "type(sim.transport.transport_state.rpacket_tracker)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "4771d92a", "metadata": {}, "source": [ "It can be seen from the above code, that the `sim.transport.rpacket_tracker` is an instance of the `List` specifically *Numba Typed List*. The `RPacketCollection` class has the following structure for the properties : {More information in the **TARDIS API** for `RPacketCollection` class}" ] }, { "attachments": {}, "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", " interaction_type\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "a3ea2f54", "metadata": {}, "outputs": [], "source": [ "len(sim.transport.transport_state.rpacket_tracker)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "0f8566f4", "metadata": {}, "source": [ "### Accessing properties through `rpacket_tracker`" ] }, { "attachments": {}, "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." ] }, { "attachments": {}, "cell_type": "markdown", "id": "a4772b00", "metadata": {}, "source": [ "#### Accessing the `index` property for the packet with index = 10 :" ] }, { "cell_type": "code", "execution_count": null, "id": "de7b8877", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker[10].index" ] }, { "attachments": {}, "cell_type": "markdown", "id": "d81fbbf7", "metadata": {}, "source": [ "#### Accessing the `seed` property for the packet with index = 10 :" ] }, { "cell_type": "code", "execution_count": null, "id": "39e2dbd2", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker[10].seed" ] }, { "attachments": {}, "cell_type": "markdown", "id": "7afe2110", "metadata": {}, "source": [ "#### Accessing the `status` property for the packet with index = 10 :" ] }, { "cell_type": "code", "execution_count": null, "id": "e82427ea", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker[10].status" ] }, { "attachments": {}, "cell_type": "markdown", "id": "ea308a55", "metadata": {}, "source": [ "Thus, all other properties (`r`, `nu`, `mu`, `energy`, `shell_id`,`interaction_type`) can be accessed accordingly." ] }, { "attachments": {}, "cell_type": "markdown", "id": "c83dd906", "metadata": {}, "source": [ "We can also see the total number of interactions of index `10` packet under went, with the following example:" ] }, { "cell_type": "code", "execution_count": null, "id": "090b1517", "metadata": {}, "outputs": [], "source": [ "len(sim.transport.transport_state.rpacket_tracker[10].shell_id)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "9136fba1", "metadata": {}, "source": [ "
\n", "\n", "Warning\n", "\n", "If we try to access `sim.transport.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", "
" ] }, { "attachments": {}, "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", "
" ] }, { "attachments": {}, "cell_type": "markdown", "id": "2da1215e", "metadata": {}, "source": [ "### Accessing properties with `rpacket_tracker_df`" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6eaee13d", "metadata": {}, "source": [ "Also, the `tracked` properties can be accessed via the `rpacket_tracker_df` attribute of the `sim.transport` object. " ] }, { "cell_type": "code", "execution_count": null, "id": "5fa074c2", "metadata": {}, "outputs": [], "source": [ "type(sim.transport.transport_state.rpacket_tracker_df)" ] }, { "attachments": {}, "cell_type": "markdown", "id": "9a39309b", "metadata": {}, "source": [ "Here, all the properties of the rpackets discussed above are stored as a `Pandas.Dataframe` object. This makes accessing the properties of any rpacket more convenient and in a more organized way." ] }, { "attachments": {}, "cell_type": "markdown", "id": "93e78750", "metadata": {}, "source": [ "\n", " \n", " The Basic structure of the Dataframe is as follows:

\n", " Index Columns:\n", "
    \n", "
  1. index - Indicates the index number of the packet.
  2. \n", "
  3. step - It is the step number that any particular packet is on, as it moves through the ejecta.
  4. \n", "
\n", " Properties:

\n", " These following properties are present as columns in the Dataframe. These are the same properties that were present in the rpacket_tracker\n", "
    \n", "
  1. status
  2. \n", "
  3. seed
  4. \n", "
  5. r
  6. \n", "
  7. nu
  8. \n", "
  9. mu
  10. \n", "
  11. energy
  12. \n", "
  13. shell_id
  14. \n", "
  15. interaction_type
  16. \n", "
\n", " \n", "" ] }, { "attachments": {}, "cell_type": "markdown", "id": "54dce761", "metadata": {}, "source": [ "The entire dataframe, containing all the properties mentioned above, can be fetched as:" ] }, { "cell_type": "code", "execution_count": null, "id": "0ec47b2f", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker_df" ] }, { "attachments": {}, "cell_type": "markdown", "id": "81f1d718", "metadata": {}, "source": [ "To access these different properties in the dataframe, we may consider the following examples for the `rpacket_tracker_df`:\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." ] }, { "attachments": {}, "cell_type": "markdown", "id": "eb8df3d9", "metadata": {}, "source": [ "#### Accessing all the properties for the packet with index = 10:" ] }, { "cell_type": "code", "execution_count": null, "id": "612c8234", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker_df.loc[10]" ] }, { "attachments": {}, "cell_type": "markdown", "id": "75d86f95", "metadata": {}, "source": [ "#### Accessing the `energy` property for the packet with index = 10:\n" ] }, { "cell_type": "code", "execution_count": null, "id": "2c84adb1", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker_df.loc[10][\"energy\"]" ] }, { "attachments": {}, "cell_type": "markdown", "id": "1beffb1f", "metadata": {}, "source": [ "The above command returns a Pandas Series for the packet `#10` across different steps. To access any property at a particular step (for example at step `5`), the following commands can be used." ] }, { "cell_type": "code", "execution_count": null, "id": "1d7f0bb4", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker_df.loc[10,5][\"energy\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "1cf17109", "metadata": {}, "outputs": [], "source": [ "sim.transport.transport_state.rpacket_tracker_df.loc[10][\"energy\"][5]" ] }, { "attachments": {}, "cell_type": "markdown", "id": "6921f480", "metadata": {}, "source": [ "Thus, all other properties (`status`, `seed`, `r`, `nu`, `mu`, `shell_id`,`interaction_type`) can be accessed accordingly." ] } ], "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.11.5" }, "vscode": { "interpreter": { "hash": "6890d83cfa6c65bebc4bd449872c06a268510d8b8ae8c923ce6786e6c4796836" } } }, "nbformat": 4, "nbformat_minor": 5 }