{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "# Storing Simulations to HDF\n", "\n", "You can ask TARDIS to store the state of each iteration of the simulation you are running. We show examples of how this is done:" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Initialize the simulation with the `tardis_example.yml` configuration file." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tardis import run_tardis\n", "from tardis.io.atom_data.util 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')\n", "\n", "# We run the simulation\n", "simulation = run_tardis('tardis_example.yml')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "You can now use the `to_hdf` method, to save properties to a HDF file.\n", "\n", "#### Parameters \n", "\n", "`file_path`: Path where the HDF file should be stored. (Required) \n", "`path`: Path inside the HDF store to store the elements. (Optional) \n", "`name`: Name of the group inside HDF store, under which properties will be saved.(Optional)\n", "`overwrite`: If the HDF file already exists, do you overwrite the existing file (Optional, default `False`)\n", "\n", "
\n", "\n", "Note\n", " \n", "Throughout this notebook, we set ``overwrite=True`` so that the notebook can be run repeatedly if needed.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simulation.to_hdf('/tmp/full_example.hdf', overwrite=True)\n", "\n", "# The commented out code below shows an example of to_hdf with more parameters\n", "#simulation.to_hdf(file_path='/tmp/full_example.hdf', path='/', name='simulation')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Open the stored HDF file with pandas and print a list of its entries using the `keys()` method:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "data = pd.HDFStore('/tmp/full_example.hdf', overwrite=True)\n", "\n", "data.keys()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Access `model.density` under simulation, which is a one-dimensional array" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(data['/simulation/simulation_state/density'])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "Scalars are stored in a `scalars` `pandas.Series` for every module. For example to access `model.t_inner` under simulation, one would need to do the following." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(data['/simulation/simulation_state/scalars']['t_inner'])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Breakdown of the various to_hdf methods\n", "Every module in TARDIS has its own `to_hdf` method responsible to store its own data to an HDF file." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Plasma\n", "The following call will store every plasma property to `/tmp/plasma_output.hdf` under `/parent/plasma`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simulation.plasma.to_hdf('/tmp/plasma_output.hdf', path='parent', overwrite=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "import pandas\n", "\n", "plasma_data = pandas.HDFStore('/tmp/plasma_output.hdf')\n", "\n", "plasma_data.keys()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Plasma's `to_hdf` method can also accept a `collection` parameter which can specify which types of plasma properties will be stored. For example if we wanted to only store Input plasma properties, we would do the following:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from tardis.plasma.properties.base import Input\n", "simulation.plasma.to_hdf('/tmp/plasma_input_output.hdf', collection=[Input], overwrite=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "\n", "plasma_input_data = pandas.HDFStore('/tmp/plasma_input_output.hdf')\n", "\n", "plasma_input_data.keys()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Model\n", "The following call will store properties of the `SimulationState` to `/tmp/model_output.hdf` under `/simulation_state`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simulation.simulation_state.to_hdf('/tmp/model_output.hdf', overwrite=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "\n", "model_data = pandas.HDFStore('/tmp/model_output.hdf')\n", "\n", "model_data.keys()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### MontecarloTransport\n", "The following call will store properties of the `MontecarloTransport` to `/tmp/transport_output.hdf` under `/transport`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "simulation.transport.to_hdf('/tmp/transport_output.hdf', overwrite=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas\n", "\n", "transport_data = pandas.HDFStore('/tmp/transport_output.hdf')\n", "\n", "transport_data.keys()" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "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": 1 }