{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Analysing Spectrum" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tardis.visualization import plot_util as pu\n", "from astropy import units as u" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Every simulation run requires [atomic data](io/configuration/components/atomic/atomic_data.rst) and a [configuration file](io/configuration/index.rst). \n", "\n", "## Atomic Data\n", "\n", "We recommend using the [kurucz_cd23_chianti_H_He.h5](https://github.com/tardis-sn/tardis-regression-data/raw/main/atom_data/kurucz_cd23_chianti_H_He.h5) dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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": "markdown", "metadata": {}, "source": [ "You can also obtain a copy of the atomic data from the [tardis-regression-data](https://github.com/tardis-sn/tardis-regression-data/tree/main/atom_data) repository.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example Configuration File\n", "\n", "The configuration file [tardis_example.yml](https://github.com/tardis-sn/tardis/tree/master/docs/tardis_example.yml) is used throughout this Quickstart.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!wget -q -nc https://raw.githubusercontent.com/tardis-sn/tardis/master/docs/tardis_example.yml" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat tardis_example.yml" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running the Simulation\n", "\n", "To run the simulation, import the `run_tardis` function and create the `sim` object." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:**\n", "\n", "Get more information about the [progress bars](io/output/progress_bars.rst), [logging configuration](io/optional/tutorial_logging_configuration.ipynb), and [convergence plots](io/visualization/tutorial_convergence_plot.ipynb).\n", "\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tardis import run_tardis\n", "\n", "sim = run_tardis(\n", " \"tardis_example.yml\",\n", " log_level=\"ERROR\"\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## HDF\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "TARDIS can save simulation data to HDF files for later analysis. The code below shows how to load a simulation from an HDF file. This is useful when you want to analyze simulation results without re-running the simulation.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import astropy.units as u\n", "# import pandas as pd\n", "\n", "# hdf_fpath = \"add_file_path_here\"\n", "# with pd.HDFStore(hdf_fpath, \"r\") as hdf:\n", "# sim = u.Quantity(hdf[\"/simulation\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate integrated spectrum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Note:**\n", "\n", "It takes about a minute to calculate. Please be patient while it runs.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "spectrum_integrated = sim.spectrum_solver.spectrum_integrated" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting the Spectrum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will plot the spectrum using matplotlib and plotly. The plots show the luminosity density as a function of wavelength.\n", "\n", "- $\\textbf{Wavelength } [\\overset{\\circ}{A}]$: The x-axis represents the wavelength in Angstroms.\n", "- $\\textbf{Luminosity density} [erg\\;s^{-1}\\;\\overset{\\circ}{A^{-1}}]$: The y-axis represents the luminosity density in erg per second per Angstrom." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use Matplotlib to create a static plot of the ***formal integral spectrum*** that was calculated in the previous step." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "# Create a new figure with specified dimensions\n", "plt.figure(figsize=(10, 6.5))\n", "\n", "spectrum_integrated.plot(label=\"Formal integral\") # Plot spectrum from formal integral solution\n", "\n", "xlabel = pu.axis_label_in_latex(\"Wavelength\", u.AA)\n", "ylabel = pu.axis_label_in_latex(\n", " \"Luminosity density\", u.Unit(\"erg/(s AA)\"), only_text=True\n", ")\n", "# Set title, labels, and template\n", "plt.xlim(500, 9000)\n", "plt.title(\"TARDIS example model spectrum\")\n", "plt.xlabel(xlabel)\n", "plt.ylabel(ylabel)\n", "plt.legend()\n", "plt.show()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plotly\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we use Plotly to create an interactive plot of the ***virtual packet spectrum*** generated by the TARDIS simulation.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import plotly.graph_objects as go\n", "\n", "# Create a new figure for plotting the spectrum\n", "fig = go.Figure()\n", "\n", "# Plot the wavelength spectrum\n", "fig.add_trace(\n", " go.Scatter(\n", " x=sim.spectrum_solver.spectrum_virtual_packets.wavelength,\n", " y=sim.spectrum_solver.spectrum_virtual_packets.luminosity_density_lambda,\n", " mode=\"lines\",\n", " name=\"Spectrum\"\n", " )\n", ")\n", "\n", "xlabel = pu.axis_label_in_latex(\"Wavelength\", u.AA)\n", "ylabel = pu.axis_label_in_latex(\n", " \"Luminosity density\", u.Unit(\"erg/(s AA)\"), only_text=True\n", ")\n", "# Set title, labels, and template\n", "fig.update_layout(\n", " title=\"TARDIS example model spectrum\",\n", " xaxis_title=xlabel,\n", " yaxis_title=ylabel,\n", " xaxis_range=[500, 9000],\n", " showlegend=True,\n", " template=\"plotly_white\"\n", ")\n", "\n", "# Display the plot\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "tardis", "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.12.4" } }, "nbformat": 4, "nbformat_minor": 2 }