Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Convergence Plots #1636

Merged
merged 40 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
00971e1
initial commit
atharva-2001 Jun 4, 2021
94d6241
updating class structure
atharva-2001 Jun 14, 2021
af99842
adding functions to create empty plots
atharva-2001 Jun 14, 2021
5989612
adding functions to update convergence plots
atharva-2001 Jun 14, 2021
563e2c6
fixing imports for convergence plots
atharva-2001 Jun 15, 2021
75b593a
changes to get update convergence plots from tardis/simulation/base.py
atharva-2001 Jun 15, 2021
c712b26
adding updated notebook
atharva-2001 Jun 15, 2021
f1f52ca
allowing user to customize plot layout from run_tardis
atharva-2001 Jun 15, 2021
c64d195
adding options to not show plots and change colorscale
atharva-2001 Jun 15, 2021
e70c266
adding docstrings
atharva-2001 Jun 15, 2021
0c40895
adding updated notebook
atharva-2001 Jun 15, 2021
453e08b
fix typo
atharva-2001 Jun 15, 2021
7752a6f
exporting convergence plots
atharva-2001 Jun 18, 2021
89bdc9a
layout changes
atharva-2001 Jun 18, 2021
7de1ff6
adding check to see if data is collected
atharva-2001 Jun 18, 2021
26d8fbb
Merge branch 'master' into convergence_plots
atharva-2001 Jun 18, 2021
be879b1
moving convergence plots notebook
atharva-2001 Jun 19, 2021
5dce0f5
adding tests for convergence class and transition_colors function
atharva-2001 Jun 21, 2021
f108175
fix typos and adding comments
atharva-2001 Jun 21, 2021
745a6b3
reformatted using black
atharva-2001 Jun 21, 2021
0bdd848
adding function to override default plot configuration
atharva-2001 Jun 21, 2021
37e8d13
showing how plots can be updated in the notebook
atharva-2001 Jun 21, 2021
a23df2c
fixing plot heights and tick labels
atharva-2001 Jun 23, 2021
77f65be
code refactor
atharva-2001 Jun 28, 2021
6ead63a
adding docstrings
atharva-2001 Jun 28, 2021
a4af64c
fixing axes and legend, converting units using astropy
atharva-2001 Jun 29, 2021
b652101
fixing hover data and making axes titles romanized/upright in certain…
atharva-2001 Jun 30, 2021
d8ca858
use same colorscale for both plasma and luminosity plots
atharva-2001 Jul 1, 2021
d26efd5
add docstrings and code comments
atharva-2001 Jul 1, 2021
9149ba3
Merge branch 'master' into convergence_plots
atharva-2001 Jul 1, 2021
718f0b9
add option to change colorscale, format using black, add updated note…
atharva-2001 Jul 1, 2021
7ec4ba5
remove unnecessary customizations, edit docstrings
atharva-2001 Jul 2, 2021
e205106
add documentation in the notebook
atharva-2001 Jul 2, 2021
f05fe03
test length of fig.data tuple after build
atharva-2001 Jul 2, 2021
ed23426
add tests for update and override function
atharva-2001 Jul 6, 2021
b4555a9
edit docstrings and code comments
atharva-2001 Jul 13, 2021
a2a60d9
renaming luminosity_plot to t_inner_luminosities_plot
atharva-2001 Jul 15, 2021
f624afc
minor changes in documentation and docstrings
atharva-2001 Jul 19, 2021
35c5693
Merge branch 'master' into convergence_plots
atharva-2001 Jul 23, 2021
2b08432
[build docs]
atharva-2001 Jul 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
855 changes: 855 additions & 0 deletions docs/io/visualization/convergence_plot.ipynb

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions tardis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ def run_tardis(
packet_source=None,
simulation_callbacks=[],
virtual_packet_logging=False,
show_cplots=True,
log_level=None,
specific=None,
**kwargs,
):
"""
Run TARDIS from a given config object.
Expand Down Expand Up @@ -48,6 +50,12 @@ def run_tardis(
If True, only show the log messages from a particular log level, set by `log_level`.
If False, the logger shows log messages belonging to the level set and all levels above it in severity.
The default value None means that the `specific` specified in the configuration file will be used.
show_cplots : bool, default: True, optional
Option to enable tardis convergence plots.
**kwargs : dict, optional
Optional keyword arguments including those
supported by :obj:`tardis.visualization.tools.convergence_plot.ConvergencePlots`.
Returns
-------
Expand All @@ -73,6 +81,9 @@ def run_tardis(
)
tardis_config = Configuration.from_config_dict(config)

if not isinstance(show_cplots, bool):
raise TypeError("Expected bool in show_cplots argument")

logging_state(log_level, tardis_config, specific)

if atom_data is not None:
Expand All @@ -89,6 +100,8 @@ def run_tardis(
packet_source=packet_source,
atom_data=atom_data,
virtual_packet_logging=virtual_packet_logging,
show_cplots=show_cplots,
**kwargs,
)
for cb in simulation_callbacks:
simulation.add_callback(*cb)
Expand Down
80 changes: 79 additions & 1 deletion tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas as pd
from astropy import units as u, constants as const
from collections import OrderedDict
from tardis import model

from tardis.montecarlo import MontecarloRunner
from tardis.model import Radial1DModel
Expand All @@ -12,6 +13,7 @@
from tardis.io.config_reader import ConfigurationError
from tardis.util.base import is_notebook
from tardis.montecarlo import montecarlo_configuration as mc_config_module
from tardis.visualization import ConvergencePlots
from IPython.display import display

# Adding logging support
Expand Down Expand Up @@ -97,6 +99,7 @@ class Simulation(PlasmaStateStorerMixin, HDFWriterMixin):
luminosity_nu_start : astropy.units.Quantity
luminosity_nu_end : astropy.units.Quantity
luminosity_requested : astropy.units.Quantity
cplots_kwargs: dict
nthreads : int
The number of threads to run montecarlo with
Expand Down Expand Up @@ -129,6 +132,8 @@ def __init__(
luminosity_requested,
convergence_strategy,
nthreads,
show_cplots,
cplots_kwargs,
):

super(Simulation, self).__init__(iterations, model.no_of_shells)
Expand All @@ -146,6 +151,7 @@ def __init__(
self.luminosity_nu_end = luminosity_nu_end
self.luminosity_requested = luminosity_requested
self.nthreads = nthreads

if convergence_strategy.type in ("damped"):
self.convergence_strategy = convergence_strategy
self.converged = False
Expand All @@ -162,6 +168,18 @@ def __init__(
f"- input is {convergence_strategy.type}"
)

if show_cplots:
self.cplots = ConvergencePlots(
iterations=self.iterations, **cplots_kwargs
)

if "export_cplots" in cplots_kwargs:
if not isinstance(cplots_kwargs["export_cplots"], bool):
raise TypeError("Expected bool in export_cplots argument")
self.export_cplots = cplots_kwargs["export_cplots"]
else:
self.export_cplots = False

self._callbacks = OrderedDict()
self._cb_next_id = 0

Expand Down Expand Up @@ -289,6 +307,22 @@ def advance_state(self):
else:
next_t_inner = self.model.t_inner

if hasattr(self, "cplots"):
self.cplots.fetch_data(
name="t_inner",
value=self.model.t_inner.value,
item_type="value",
)
self.cplots.fetch_data(
name="t_rad", value=self.model.t_rad, item_type="iterable"
)
self.cplots.fetch_data(
name="w", value=self.model.w, item_type="iterable"
)
self.cplots.fetch_data(
name="velocity", value=self.model.velocity, item_type="iterable"
)

self.log_plasma_state(
self.model.t_rad,
self.model.w,
Expand Down Expand Up @@ -343,6 +377,23 @@ def iterate(self, no_of_packets, no_of_virtual_packets=0, last_run=False):
reabsorbed_luminosity = self.runner.calculate_reabsorbed_luminosity(
self.luminosity_nu_start, self.luminosity_nu_end
)
if hasattr(self, "cplots"):
self.cplots.fetch_data(
name="Emitted",
value=emitted_luminosity.value,
item_type="value",
)
self.cplots.fetch_data(
name="Absorbed",
value=reabsorbed_luminosity.value,
item_type="value",
)
self.cplots.fetch_data(
name="Requested",
value=self.luminosity_requested.value,
item_type="value",
)

self.log_run_results(emitted_luminosity, reabsorbed_luminosity)
self.iterations_executed += 1

Expand All @@ -362,6 +413,8 @@ def run(self):
)
self.iterate(self.no_of_packets)
self.converged = self.advance_state()
if hasattr(self, "cplots"):
self.cplots.update()
self._call_back()
if self.converged:
if self.convergence_strategy.stop_if_converged:
Expand All @@ -379,6 +432,13 @@ def run(self):
)

self.reshape_plasma_state_store(self.iterations_executed)
if hasattr(self, "cplots"):
self.cplots.fetch_data(
name="t_inner",
value=self.model.t_inner.value,
item_type="value",
)
self.cplots.update(export_cplots=self.export_cplots, last=True)

logger.info(
f"Simulation finished in {self.iterations_executed:d} iterations "
Expand Down Expand Up @@ -518,7 +578,12 @@ def remove_callback(self, id):

@classmethod
def from_config(
cls, config, packet_source=None, virtual_packet_logging=False, **kwargs
cls,
config,
packet_source=None,
virtual_packet_logging=False,
show_cplots=True,
**kwargs,
):
"""
Create a new Simulation instance from a Configuration object.
Expand Down Expand Up @@ -564,6 +629,17 @@ def from_config(
virtual_packet_logging=virtual_packet_logging,
)

cplots_config_options = [
"plasma_plot_config",
"t_inner_luminosities_config",
"plasma_cmap",
"t_inner_luminosities_colors",
"export_cplots",
]
cplots_kwargs = {}
for item in set(cplots_config_options).intersection(kwargs.keys()):
cplots_kwargs[item] = kwargs[item]

luminosity_nu_start = config.supernova.luminosity_wavelength_end.to(
u.Hz, u.spectral()
)
Expand All @@ -587,6 +663,7 @@ def from_config(
model=model,
plasma=plasma,
runner=runner,
show_cplots=show_cplots,
no_of_packets=int(config.montecarlo.no_of_packets),
no_of_virtual_packets=int(config.montecarlo.no_of_virtual_packets),
luminosity_nu_start=luminosity_nu_start,
Expand All @@ -595,4 +672,5 @@ def from_config(
luminosity_requested=config.supernova.luminosity_requested.cgs,
convergence_strategy=config.montecarlo.convergence_strategy,
nthreads=config.montecarlo.nthreads,
cplots_kwargs=cplots_kwargs,
)
2 changes: 2 additions & 0 deletions tardis/visualization/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Visualization tools and widgets for TARDIS."""

from tardis.visualization.tools.convergence_plot import ConvergencePlots

from tardis.visualization.widgets.shell_info import (
shell_info_from_simulation,
shell_info_from_hdf,
Expand Down
Loading