You can interact with this notebook online: Launch notebook

How to Store Simulations to HDF

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:

Initialize the simulation with the tardis_example.yml configuration file.

[1]:
from tardis import run_tardis
from tardis.io.atom_data import download_atom_data

# We download the atomic data needed to run the simulation
download_atom_data('kurucz_cd23_chianti_H_He')

# We run the simulation
simulation = run_tardis('tardis_example.yml')
Atomic Data kurucz_cd23_chianti_H_He already exists in /home/runner/Downloads/tardis-data/kurucz_cd23_chianti_H_He.h5. Will not download - override with force_download=True.

You can now use the to_hdf method, to save properties to a HDF file.

Parameters

file_path: Path where the HDF file should be stored. (Required)
path: Path inside the HDF store to store the elements. (Optional)
name: Name of the group inside HDF store, under which properties will be saved.(Optional) overwrite: If the HDF file already exists, do you overwrite the existing file (Optional, default False)

Note

Throughout this notebook, we set overwrite=True so that the notebook can be run repeatedly if needed.

[2]:
simulation.to_hdf('/tmp/full_example.hdf', overwrite=True)

# The commented out code below shows an example of to_hdf with more parameters
#simulation.to_hdf(file_path='/tmp/full_example.hdf', path='/', name='simulation')

Open the stored HDF file with pandas and print a list of its entries using the keys() method:

[3]:
import pandas as pd

data = pd.HDFStore('/tmp/full_example.hdf', overwrite=True)

data.keys()
[3]:
['/simulation/iterations_electron_densities',
 '/simulation/iterations_t_inner',
 '/simulation/iterations_t_rad',
 '/simulation/iterations_w',
 '/simulation/transport/transport_state/emitted_packet_mask',
 '/simulation/transport/transport_state/j_blue_estimator',
 '/simulation/transport/transport_state/j_estimator',
 '/simulation/transport/transport_state/last_interaction_in_nu',
 '/simulation/transport/transport_state/last_interaction_in_r',
 '/simulation/transport/transport_state/last_interaction_type',
 '/simulation/transport/transport_state/last_line_interaction_in_id',
 '/simulation/transport/transport_state/last_line_interaction_out_id',
 '/simulation/transport/transport_state/last_line_interaction_shell_id',
 '/simulation/transport/transport_state/nu_bar_estimator',
 '/simulation/transport/transport_state/output_energy',
 '/simulation/transport/transport_state/output_nu',
 '/simulation/transport/transport_state/packet_luminosity',
 '/simulation/transport/transport_state/scalars',
 '/simulation/spectrum_solver/montecarlo_virtual_luminosity',
 '/simulation/spectrum_solver/spectrum_virtual_packets/_frequency',
 '/simulation/spectrum_solver/spectrum_virtual_packets/luminosity',
 '/simulation/spectrum_solver/spectrum_virtual_packets/luminosity_density_lambda',
 '/simulation/spectrum_solver/spectrum_virtual_packets/scalars',
 '/simulation/spectrum_solver/spectrum_virtual_packets/wavelength',
 '/simulation/spectrum_solver/spectrum_real_packets_reabsorbed/_frequency',
 '/simulation/spectrum_solver/spectrum_real_packets_reabsorbed/luminosity',
 '/simulation/spectrum_solver/spectrum_real_packets_reabsorbed/luminosity_density_lambda',
 '/simulation/spectrum_solver/spectrum_real_packets_reabsorbed/scalars',
 '/simulation/spectrum_solver/spectrum_real_packets_reabsorbed/wavelength',
 '/simulation/spectrum_solver/spectrum_real_packets/_frequency',
 '/simulation/spectrum_solver/spectrum_real_packets/luminosity',
 '/simulation/spectrum_solver/spectrum_real_packets/luminosity_density_lambda',
 '/simulation/spectrum_solver/spectrum_real_packets/scalars',
 '/simulation/spectrum_solver/spectrum_real_packets/wavelength',
 '/simulation/spectrum_solver/spectrum_integrated/_frequency',
 '/simulation/spectrum_solver/spectrum_integrated/luminosity',
 '/simulation/spectrum_solver/spectrum_integrated/luminosity_density_lambda',
 '/simulation/spectrum_solver/spectrum_integrated/scalars',
 '/simulation/spectrum_solver/spectrum_integrated/wavelength',
 '/simulation/simulation_state/abundance',
 '/simulation/simulation_state/density',
 '/simulation/simulation_state/dilution_factor',
 '/simulation/simulation_state/r_inner',
 '/simulation/simulation_state/scalars',
 '/simulation/simulation_state/t_radiative',
 '/simulation/simulation_state/v_inner',
 '/simulation/simulation_state/v_outer',
 '/simulation/plasma/beta_rad',
 '/simulation/plasma/beta_sobolev',
 '/simulation/plasma/continuum_interaction_species',
 '/simulation/plasma/dilute_planckian_radiation_field',
 '/simulation/plasma/electron_densities',
 '/simulation/plasma/excitation_energy',
 '/simulation/plasma/f_lu',
 '/simulation/plasma/g',
 '/simulation/plasma/g_electron',
 '/simulation/plasma/general_level_boltzmann_factor',
 '/simulation/plasma/ion_number_density',
 '/simulation/plasma/ionization_data',
 '/simulation/plasma/j_blues',
 '/simulation/plasma/level_boltzmann_factor',
 '/simulation/plasma/level_number_density',
 '/simulation/plasma/levels',
 '/simulation/plasma/lines',
 '/simulation/plasma/lines_lower_level_index',
 '/simulation/plasma/lines_upper_level_index',
 '/simulation/plasma/macro_atom_data',
 '/simulation/plasma/metastability',
 '/simulation/plasma/nlte_excitation_species',
 '/simulation/plasma/nlte_ionization_species',
 '/simulation/plasma/nu',
 '/simulation/plasma/number_density',
 '/simulation/plasma/partition_function',
 '/simulation/plasma/phi',
 '/simulation/plasma/scalars',
 '/simulation/plasma/selected_atoms',
 '/simulation/plasma/stimulated_emission_factor',
 '/simulation/plasma/t_electrons',
 '/simulation/plasma/t_rad',
 '/simulation/plasma/tau_sobolevs',
 '/simulation/plasma/transition_probabilities',
 '/simulation/plasma/w',
 '/simulation/plasma/wavelength_cm']

Access model.density under simulation, which is a one-dimensional array

[4]:
print(data['/simulation/simulation_state/density'])
0     7.542804e-14
1     5.728475e-14
2     4.396074e-14
3     3.406287e-14
4     2.663135e-14
5     2.099596e-14
6     1.668287e-14
7     1.335311e-14
8     1.076154e-14
9     8.729085e-15
10    7.123652e-15
11    5.846921e-15
12    4.825093e-15
13    4.002324e-15
14    3.336039e-15
15    2.793540e-15
16    2.349550e-15
17    1.984397e-15
18    1.682677e-15
19    1.432260e-15
dtype: float64

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.

[5]:
print(data['/simulation/simulation_state/scalars']['t_inner'])
10651.315137263384

Breakdown of the various to_hdf methods

Every module in TARDIS has its own to_hdf method responsible to store its own data to an HDF file.

Plasma

The following call will store every plasma property to /tmp/plasma_output.hdf under /parent/plasma

[6]:
simulation.plasma.to_hdf('/tmp/plasma_output.hdf', path='parent', overwrite=True)
[7]:
import pandas

plasma_data = pandas.HDFStore('/tmp/plasma_output.hdf')

plasma_data.keys()
[7]:
['/parent/plasma/beta_rad',
 '/parent/plasma/beta_sobolev',
 '/parent/plasma/continuum_interaction_species',
 '/parent/plasma/dilute_planckian_radiation_field',
 '/parent/plasma/electron_densities',
 '/parent/plasma/excitation_energy',
 '/parent/plasma/f_lu',
 '/parent/plasma/g',
 '/parent/plasma/g_electron',
 '/parent/plasma/general_level_boltzmann_factor',
 '/parent/plasma/ion_number_density',
 '/parent/plasma/ionization_data',
 '/parent/plasma/j_blues',
 '/parent/plasma/level_boltzmann_factor',
 '/parent/plasma/level_number_density',
 '/parent/plasma/levels',
 '/parent/plasma/lines',
 '/parent/plasma/lines_lower_level_index',
 '/parent/plasma/lines_upper_level_index',
 '/parent/plasma/macro_atom_data',
 '/parent/plasma/metastability',
 '/parent/plasma/nlte_excitation_species',
 '/parent/plasma/nlte_ionization_species',
 '/parent/plasma/nu',
 '/parent/plasma/number_density',
 '/parent/plasma/partition_function',
 '/parent/plasma/phi',
 '/parent/plasma/scalars',
 '/parent/plasma/selected_atoms',
 '/parent/plasma/stimulated_emission_factor',
 '/parent/plasma/t_electrons',
 '/parent/plasma/t_rad',
 '/parent/plasma/tau_sobolevs',
 '/parent/plasma/transition_probabilities',
 '/parent/plasma/w',
 '/parent/plasma/wavelength_cm']

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:

[8]:
from tardis.plasma.properties.base import Input
simulation.plasma.to_hdf('/tmp/plasma_input_output.hdf', collection=[Input], overwrite=True)
[9]:
import pandas

plasma_input_data = pandas.HDFStore('/tmp/plasma_input_output.hdf')

plasma_input_data.keys()
[9]:
['/plasma/continuum_interaction_species',
 '/plasma/dilute_planckian_radiation_field',
 '/plasma/j_blues',
 '/plasma/nlte_excitation_species',
 '/plasma/nlte_ionization_species',
 '/plasma/number_density',
 '/plasma/scalars']

Model

The following call will store properties of the SimulationState to /tmp/model_output.hdf under /simulation_state.

[10]:
simulation.simulation_state.to_hdf('/tmp/model_output.hdf', overwrite=True)
[11]:
import pandas

model_data = pandas.HDFStore('/tmp/model_output.hdf')

model_data.keys()
[11]:
['/simulation_state/abundance',
 '/simulation_state/density',
 '/simulation_state/dilution_factor',
 '/simulation_state/r_inner',
 '/simulation_state/scalars',
 '/simulation_state/t_radiative',
 '/simulation_state/v_inner',
 '/simulation_state/v_outer']

MontecarloTransport

The following call will store properties of the MontecarloTransport to /tmp/transport_output.hdf under /transport.

[12]:
simulation.transport.to_hdf('/tmp/transport_output.hdf', overwrite=True)
[13]:
import pandas

transport_data = pandas.HDFStore('/tmp/transport_output.hdf')

transport_data.keys()
[13]:
['/transport/transport_state/emitted_packet_mask',
 '/transport/transport_state/j_blue_estimator',
 '/transport/transport_state/j_estimator',
 '/transport/transport_state/last_interaction_in_nu',
 '/transport/transport_state/last_interaction_in_r',
 '/transport/transport_state/last_interaction_type',
 '/transport/transport_state/last_line_interaction_in_id',
 '/transport/transport_state/last_line_interaction_out_id',
 '/transport/transport_state/last_line_interaction_shell_id',
 '/transport/transport_state/nu_bar_estimator',
 '/transport/transport_state/output_energy',
 '/transport/transport_state/output_nu',
 '/transport/transport_state/packet_luminosity',
 '/transport/transport_state/scalars']