This notebook is available at https://github.com/tardis-sn/tardis/tree/master/docs/io/optional/callback_example.ipynb
Using Custom Callbacks When Running TARDIS¶
[7]:
import warnings
warnings.filterwarnings('ignore')
from tardis import run_tardis
import tardis
tardis.logger.setLevel(0)
tardis.logging.captureWarnings(False)
The command run_tardis
allows users to provide a set of callbacks to the simulation. These callbacks are called at the end of each iteration. This example will show you how to create a custom callback and run a model with TARDIS. As an example, we create a custom callback that prints out some basic information about our model at every iteration. Specifically, we’ll print out a table of the inner velocities of each shell as well as the radiative temperature of each shell. The first thing to
note is that the callback function must have the simulation object as the first argument. This grants the user access to the state of the simulation at each iteration.
[2]:
def display_table(sim):
'''Display a table of velocities and
radiative temperatures at each iteration
'''
# We have direct access to the attributes of the simulation
columns = zip(sim.model.v_inner[::5].to('km/s'),
sim.model.t_rad[::5].to('K'))
print("Iteration:", sim.iterations_executed)
print(" {:<15} {:<15}".format('v_inner', 't_rad'))
format_string = " {0.value:<8.2f} {0.unit:<6s}\
{1.value:<8.2f} {1.unit:<6s}"
for velocity, temperature in columns:
print(format_string.format(velocity, temperature))
Now we give the callback to run_tardis
. run_tardis
offers the keyword argument simulation_callbacks
which takes a list of lists containing the callback as well as any optional arguments you wish to include with your callback. For this example our function requires no extra arguments and we only have a single callback, so we give run_tardis
a 2D list containing the callback as its only element.
[3]:
sim = run_tardis('tardis_example.yml',
simulation_callbacks=[[display_table]])
Iteration: 1
v_inner t_rad
11000.00 km / s 10171.21 K
13250.00 km / s 10306.11 K
15500.00 km / s 10174.38 K
17750.00 km / s 9910.44 K
Iteration: 2
v_inner t_rad
11000.00 km / s 11518.52 K
13250.00 km / s 11554.41 K
15500.00 km / s 11373.57 K
17750.00 km / s 11040.79 K
Iteration: 3
v_inner t_rad
11000.00 km / s 10501.13 K
13250.00 km / s 10869.94 K
15500.00 km / s 10558.27 K
17750.00 km / s 10185.65 K
Iteration: 4
v_inner t_rad
11000.00 km / s 11527.01 K
13250.00 km / s 11706.14 K
15500.00 km / s 11444.21 K
17750.00 km / s 11085.86 K
Iteration: 5
v_inner t_rad
11000.00 km / s 10659.38 K
13250.00 km / s 11038.24 K
15500.00 km / s 10790.70 K
17750.00 km / s 10450.67 K
Iteration: 6
v_inner t_rad
11000.00 km / s 11507.44 K
13250.00 km / s 11634.57 K
15500.00 km / s 11418.59 K
17750.00 km / s 10999.62 K
Iteration: 7
v_inner t_rad
11000.00 km / s 10679.65 K
13250.00 km / s 11048.27 K
15500.00 km / s 10825.71 K
17750.00 km / s 10506.32 K
Iteration: 8
v_inner t_rad
11000.00 km / s 11459.83 K
13250.00 km / s 11688.14 K
15500.00 km / s 11441.77 K
17750.00 km / s 11099.45 K
Iteration: 9
v_inner t_rad
11000.00 km / s 10666.22 K
13250.00 km / s 11000.29 K
15500.00 km / s 10822.88 K
17750.00 km / s 10384.18 K
Iteration: 10
v_inner t_rad
11000.00 km / s 11427.41 K
13250.00 km / s 11612.35 K
15500.00 km / s 11373.25 K
17750.00 km / s 11047.96 K
Iteration: 11
v_inner t_rad
11000.00 km / s 10741.34 K
13250.00 km / s 11230.52 K
15500.00 km / s 10870.70 K
17750.00 km / s 10538.17 K
Iteration: 12
v_inner t_rad
11000.00 km / s 11507.88 K
13250.00 km / s 11808.85 K
15500.00 km / s 11557.41 K
17750.00 km / s 11117.76 K
Iteration: 13
v_inner t_rad
11000.00 km / s 10727.22 K
13250.00 km / s 11171.76 K
15500.00 km / s 10971.72 K
17750.00 km / s 10574.92 K
Iteration: 14
v_inner t_rad
11000.00 km / s 11413.63 K
13250.00 km / s 11697.01 K
15500.00 km / s 11424.45 K
17750.00 km / s 11047.43 K
Iteration: 15
v_inner t_rad
11000.00 km / s 10712.62 K
13250.00 km / s 11125.89 K
15500.00 km / s 10816.39 K
17750.00 km / s 10421.69 K
Iteration: 16
v_inner t_rad
11000.00 km / s 11352.16 K
13250.00 km / s 11644.97 K
15500.00 km / s 11320.70 K
17750.00 km / s 10965.23 K
Iteration: 17
v_inner t_rad
11000.00 km / s 10738.80 K
13250.00 km / s 11210.27 K
15500.00 km / s 10911.46 K
17750.00 km / s 10545.07 K
Iteration: 18
v_inner t_rad
11000.00 km / s 11316.67 K
13250.00 km / s 11599.09 K
15500.00 km / s 11403.51 K
17750.00 km / s 10958.97 K
Iteration: 19
v_inner t_rad
11000.00 km / s 10862.51 K
13250.00 km / s 11194.46 K
15500.00 km / s 11065.62 K
17750.00 km / s 10599.85 K
Iteration: 20
v_inner t_rad
11000.00 km / s 10862.51 K
13250.00 km / s 11194.46 K
15500.00 km / s 11065.62 K
17750.00 km / s 10599.85 K
Running Callbacks with Extra Arguments¶
The callbacks provided to run_tardis
can also take extra arguments. As an example, we’ll make a callback that appends the value of the radiative temperature at each iteration to a list so we can watch how the model converges. The callback will take a list we want to append to as an argument. We’ll send both this new callback and our original display_table
callback to run_tardis
as an example of using multiple callbacks at once.
[4]:
def append_t_rad_to_table(sim, table):
'''append the array for the radiative temperature
at each iteration to a given table'''
table.append(sim.model.t_rad.copy())
In order to add our new callback, we just create another entry in our list of callbacks. Since append_t_rad_to_table
takes an extra argument, we will provide that argument in the inner list containing the callback.
[5]:
t_rad_table = [] # list to store t_rad at each iteration
callbacks = [[display_table],
[append_t_rad_to_table, t_rad_table]]
sim = run_tardis('tardis_example.yml',
simulation_callbacks=callbacks)
Iteration: 1
v_inner t_rad
11000.00 km / s 10171.21 K
13250.00 km / s 10306.11 K
15500.00 km / s 10174.38 K
17750.00 km / s 9910.44 K
Iteration: 2
v_inner t_rad
11000.00 km / s 11518.52 K
13250.00 km / s 11554.41 K
15500.00 km / s 11373.57 K
17750.00 km / s 11040.79 K
Iteration: 3
v_inner t_rad
11000.00 km / s 10501.13 K
13250.00 km / s 10869.94 K
15500.00 km / s 10558.27 K
17750.00 km / s 10185.65 K
Iteration: 4
v_inner t_rad
11000.00 km / s 11527.01 K
13250.00 km / s 11706.14 K
15500.00 km / s 11444.21 K
17750.00 km / s 11085.86 K
Iteration: 5
v_inner t_rad
11000.00 km / s 10659.38 K
13250.00 km / s 11038.24 K
15500.00 km / s 10790.70 K
17750.00 km / s 10450.67 K
Iteration: 6
v_inner t_rad
11000.00 km / s 11507.44 K
13250.00 km / s 11634.57 K
15500.00 km / s 11418.59 K
17750.00 km / s 10999.62 K
Iteration: 7
v_inner t_rad
11000.00 km / s 10679.65 K
13250.00 km / s 11048.27 K
15500.00 km / s 10825.71 K
17750.00 km / s 10506.32 K
Iteration: 8
v_inner t_rad
11000.00 km / s 11459.83 K
13250.00 km / s 11688.14 K
15500.00 km / s 11441.77 K
17750.00 km / s 11099.45 K
Iteration: 9
v_inner t_rad
11000.00 km / s 10666.22 K
13250.00 km / s 11000.29 K
15500.00 km / s 10822.88 K
17750.00 km / s 10384.18 K
Iteration: 10
v_inner t_rad
11000.00 km / s 11427.41 K
13250.00 km / s 11612.35 K
15500.00 km / s 11373.25 K
17750.00 km / s 11047.96 K
Iteration: 11
v_inner t_rad
11000.00 km / s 10741.34 K
13250.00 km / s 11230.52 K
15500.00 km / s 10870.70 K
17750.00 km / s 10538.17 K
Iteration: 12
v_inner t_rad
11000.00 km / s 11507.88 K
13250.00 km / s 11808.85 K
15500.00 km / s 11557.41 K
17750.00 km / s 11117.76 K
Iteration: 13
v_inner t_rad
11000.00 km / s 10727.22 K
13250.00 km / s 11171.76 K
15500.00 km / s 10971.72 K
17750.00 km / s 10574.92 K
Iteration: 14
v_inner t_rad
11000.00 km / s 11413.63 K
13250.00 km / s 11697.01 K
15500.00 km / s 11424.45 K
17750.00 km / s 11047.43 K
Iteration: 15
v_inner t_rad
11000.00 km / s 10712.62 K
13250.00 km / s 11125.89 K
15500.00 km / s 10816.39 K
17750.00 km / s 10421.69 K
Iteration: 16
v_inner t_rad
11000.00 km / s 11352.16 K
13250.00 km / s 11644.97 K
15500.00 km / s 11320.70 K
17750.00 km / s 10965.23 K
Iteration: 17
v_inner t_rad
11000.00 km / s 10738.80 K
13250.00 km / s 11210.27 K
15500.00 km / s 10911.46 K
17750.00 km / s 10545.07 K
Iteration: 18
v_inner t_rad
11000.00 km / s 11316.67 K
13250.00 km / s 11599.09 K
15500.00 km / s 11403.51 K
17750.00 km / s 10958.97 K
Iteration: 19
v_inner t_rad
11000.00 km / s 10862.51 K
13250.00 km / s 11194.46 K
15500.00 km / s 11065.62 K
17750.00 km / s 10599.85 K
Iteration: 20
v_inner t_rad
11000.00 km / s 10862.51 K
13250.00 km / s 11194.46 K
15500.00 km / s 11065.62 K
17750.00 km / s 10599.85 K
Now we can look at the way the radiative temperature changes in each shell every iteration.
[6]:
%pylab notebook
for t_rad in t_rad_table:
plot(t_rad)
ylabel(r'$T_{rad}\ [K]$')
xlabel('Iteration Number')
Populating the interactive namespace from numpy and matplotlib
[6]:
Text(0.5, 0, 'Iteration Number')