You can interact with this notebook online: Launch notebook

Workflow to solve v_inner boundary

The source code for this workflow can be found at: https://github.com/tardis-sn/tardis/blob/master/tardis/workflows/v_inner_solver.py.

This workflow demonstrates how to use the TARDIS modules to perform targeted tasks. This workflow is built on top of the SimpleTARDISWorkflow to solve the v_inner boundary, in addition to the remaining radiative properties.

[1]:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from astropy import units as u
[2]:
from tardis.workflows.v_inner_solver import InnerVelocitySolverWorkflow
from tardis.io.configuration.config_reader import Configuration
/home/runner/work/tardis/tardis/tardis/__init__.py:17: UserWarning: Astropy is already imported externally. Astropy should be imported after TARDIS.
  warnings.warn(
Initializing tabulator and plotly panel extensions for widgets to work
Auto-detected Sphinx build environment
Auto-detected Sphinx build environment
[3]:
config = Configuration.from_yaml('../tardis_example.yml')

This code modifies the TARDIS example configuration to include convergence information for the inner boundary velocity solver.

[4]:
config.montecarlo.convergence_strategy['v_inner_boundary'] = {
    'damping_constant' : 0.5,
    'threshold' : 0.01,
    'type' : 'damped',
    'store_iteration_properties' : True
    }

config.montecarlo.convergence_strategy.stop_if_converged = True
config.model.structure.velocity.start = 5000 * u.km/u.s # Decrease start velocity from 11000 km/s in example file, to search over a wider range
config.model.structure.velocity.num = 50 # Increase number of shells from 20 in example file, to provide more granularity

workflow = InnerVelocitySolverWorkflow(
    config, tau=2.0/3,
    mean_optical_depth="rosseland"
)
[5]:
workflow.run()
Shell No. t_rad next_t_rad w next_w
0 1.44e+04 K 1.59e+04 K 0.0531 0.482
5 1.44e+04 K 1.67e+04 K 0.0407 0.208
10 1.43e+04 K 1.68e+04 K 0.0323 0.124
15 1.42e+04 K 1.64e+04 K 0.0262 0.0907
20 1.42e+04 K 1.58e+04 K 0.0217 0.0738
25 1.41e+04 K 1.52e+04 K 0.0183 0.0648
Shell No. t_rad next_t_rad w next_w
0 1.59e+04 K 1.25e+04 K 0.482 0.46
5 1.67e+04 K 1.38e+04 K 0.208 0.158
10 1.68e+04 K 1.4e+04 K 0.124 0.0932
15 1.64e+04 K 1.38e+04 K 0.0907 0.0658
20 1.58e+04 K 1.35e+04 K 0.0738 0.0538
25 1.52e+04 K 1.29e+04 K 0.0648 0.049
Shell No. t_rad next_t_rad w next_w
0 1.25e+04 K 1.14e+04 K 0.46 0.511
5 1.38e+04 K 1.26e+04 K 0.158 0.195
10 1.4e+04 K 1.28e+04 K 0.0932 0.11
15 1.38e+04 K 1.27e+04 K 0.0658 0.0777
20 1.35e+04 K 1.24e+04 K 0.0538 0.0619
25 1.29e+04 K 1.2e+04 K 0.049 0.0541
Shell No. t_rad next_t_rad w next_w
0 1.14e+04 K 1.12e+04 K 0.511 0.507
5 1.26e+04 K 1.19e+04 K 0.195 0.22
10 1.28e+04 K 1.21e+04 K 0.11 0.132
15 1.27e+04 K 1.21e+04 K 0.0777 0.0902
20 1.24e+04 K 1.17e+04 K 0.0619 0.075
25 1.2e+04 K 1.14e+04 K 0.0541 0.0653
Shell No. t_rad next_t_rad w next_w
0 1.12e+04 K 1.08e+04 K 0.507 0.539
5 1.19e+04 K 1.15e+04 K 0.22 0.232
10 1.21e+04 K 1.16e+04 K 0.132 0.144
15 1.21e+04 K 1.15e+04 K 0.0902 0.103
20 1.17e+04 K 1.11e+04 K 0.075 0.0866
25 1.14e+04 K 1.08e+04 K 0.0653 0.0752
Shell No. t_rad next_t_rad w next_w
0 1.08e+04 K 1.09e+04 K 0.539 0.506
5 1.15e+04 K 1.12e+04 K 0.232 0.24
10 1.16e+04 K 1.13e+04 K 0.144 0.148
15 1.15e+04 K 1.12e+04 K 0.103 0.11
20 1.11e+04 K 1.09e+04 K 0.0866 0.0885
25 1.08e+04 K 1.06e+04 K 0.0752 0.0787
Shell No. t_rad next_t_rad w next_w
0 1.45e+04 K 1.08e+04 K 0.0563 0.562
5 1.12e+04 K 1.12e+04 K 0.264 0.252
10 1.13e+04 K 1.12e+04 K 0.159 0.158
15 1.12e+04 K 1.11e+04 K 0.116 0.115
20 1.1e+04 K 1.08e+04 K 0.0925 0.0935
25 1.07e+04 K 1.05e+04 K 0.0801 0.0804
30 1.03e+04 K 1.02e+04 K 0.0731 0.0717
Shell No. t_rad next_t_rad w next_w
0 1.08e+04 K 1.09e+04 K 0.562 0.584
5 1.12e+04 K 1.12e+04 K 0.252 0.263
10 1.12e+04 K 1.13e+04 K 0.158 0.159
15 1.11e+04 K 1.12e+04 K 0.115 0.115
20 1.08e+04 K 1.09e+04 K 0.0935 0.0954
25 1.05e+04 K 1.06e+04 K 0.0804 0.0828
30 1.02e+04 K 1.03e+04 K 0.0717 0.0733
Shell No. t_rad next_t_rad w next_w
0 1.09e+04 K 1.09e+04 K 0.584 0.57
5 1.12e+04 K 1.13e+04 K 0.263 0.259
10 1.13e+04 K 1.13e+04 K 0.159 0.161
15 1.12e+04 K 1.11e+04 K 0.115 0.12
20 1.09e+04 K 1.08e+04 K 0.0954 0.0986
25 1.06e+04 K 1.06e+04 K 0.0828 0.0806
30 1.03e+04 K 1.03e+04 K 0.0733 0.0726
Shell No. t_rad next_t_rad w next_w
0 1.09e+04 K 1.09e+04 K 0.57 0.576
5 1.13e+04 K 1.13e+04 K 0.259 0.255
10 1.13e+04 K 1.13e+04 K 0.161 0.158
15 1.11e+04 K 1.11e+04 K 0.12 0.118
20 1.08e+04 K 1.09e+04 K 0.0986 0.0952
25 1.06e+04 K 1.07e+04 K 0.0806 0.0799
30 1.03e+04 K 1.04e+04 K 0.0726 0.0715

Plot the spectrum

[6]:
spectrum = workflow.spectrum_solver.spectrum_real_packets
spectrum_virtual = workflow.spectrum_solver.spectrum_virtual_packets
spectrum_integrated = workflow.spectrum_solver.spectrum_integrated
[7]:
%matplotlib inline
plt.figure(figsize=(10, 6.5))

spectrum.plot(label="Normal packets")
spectrum_virtual.plot(label="Virtual packets")
spectrum_integrated.plot(label='Formal integral')

plt.xlim(500, 9000)
plt.title("TARDIS example model spectrum")
plt.xlabel(r"Wavelength [$\AA$]")
plt.ylabel(r"Luminosity density [erg/s/$\AA$]")
plt.legend()
plt.show()
../_images/workflows_v_inner_solver_workflow_11_0.svg

Plot the convergence process

[8]:
%matplotlib inline

# extract plasma states
t_rad = workflow.iterations_t_rad
t_inner = workflow.iterations_t_inner
w = workflow.iterations_w
taus = workflow.iterations_mean_optical_depth
v_inner = workflow.iterations_v_inner_boundary

# remove all nans, or rows where all values are nan
t_rad = t_rad[~np.all(np.isnan(t_rad), axis=1)]
t_inner = t_inner[~np.isnan(t_inner)]
w = w[~np.all(np.isnan(w), axis=1)]
taus = taus[~np.all(np.isnan(taus), axis=1)]
v_inner = v_inner[~np.isnan(v_inner)]

# initialize figure
fig,axes =  plt.subplots(2,2,figsize=(12,8))
plt.subplots_adjust(wspace=0.4,hspace=0.3)

# get the raw velocity grid
vel = workflow.simulation_state.geometry.v_inner

# pick a colormap for the iterations
cmap = plt.get_cmap('plasma',taus.shape[0])

# plot v inner change
v_inner_plot = axes[0,0]
v_inner_plot.plot(v_inner,marker="o",color="b")
v_inner_plot.set_xlabel("Iterations", fontsize=14)
v_inner_plot.set_ylabel("v_inner (cm/s)", fontsize=14)
v_inner_plot.grid(alpha=0.3)
v_inner_plot.tick_params(axis='y', colors='blue')
v_inner_plot.yaxis.label.set_color('blue')

# plot t inner change in same subplot
t_inner_plot = axes[0][0].twinx()
t_inner_plot.plot(t_inner,marker="s",color="r")
t_inner_plot.set_ylabel("t_inner (K)", fontsize=14)
t_inner_plot.tick_params(axis='y', colors='red')
t_inner_plot.yaxis.label.set_color('red')

# plot the tau change
tau_plot = axes[0][1]
for i, tau in enumerate(taus):
    tau_plot.plot(vel[-len(tau):], tau, color=cmap(i/taus.shape[0]),label=f"itr {i+1}",alpha=0.7)
tau_plot.scatter(workflow.simulation_state.v_inner_boundary.value, np.log(2.0 / 3.0), color="k",marker="o")
tau_plot.axhline(np.log(2.0 / 3.0), color='black', linestyle='--')
tau_plot.axvline(workflow.simulation_state.v_inner_boundary.value, color='k', linestyle='--')
tau_plot.set_xlabel("Velocity (cm/s)", fontsize=14)
tau_plot.set_ylabel("log(tau)", fontsize=14)
tau_plot.grid(alpha=0.3)
tau_plot.set_title("1 Loop of all together - without resetting plasma", fontsize=14)

# plot t radiative change
t_rad_plot = axes[1][0]
for i in range(len(taus)):
    t_rad_plot.plot(vel[-len(t_rad[i]):], t_rad[i],color=cmap(i/taus.shape[0]),label=f"itr {i+1}",alpha=0.7)
t_rad_plot.set_xlabel("Velocity (cm/s)", fontsize=14)
t_rad_plot.set_ylabel("t_rad", fontsize=14)
t_rad_plot.grid(alpha=0.3)

# plot dilution factor change
w_plot = axes[1][1]
for i in range(len(taus)):
    w_plot.plot(vel[-len(w[i]):], w[i],color=cmap(i/taus.shape[0]),label=f"itr {i+1}",alpha=0.7)
w_plot.set_xlabel("Velocity (cm/s)", fontsize=14)
w_plot.set_ylabel("dilution_factor", fontsize=14)
w_plot.grid(alpha=0.3)

# add colorbar for iteration number
norm = mpl.colors.Normalize(vmin=1, vmax=len(taus))
sm = mpl.cm.ScalarMappable(cmap=cmap, norm=norm)
cbar = fig.colorbar(sm, ax=axes, orientation='vertical', fraction=0.025, pad=0.02)
cbar.set_label('Iteration Number', fontsize=12)
../_images/workflows_v_inner_solver_workflow_13_0.svg