From 2f7cf5434201cc6f5b26d3875818d521aa39c366 Mon Sep 17 00:00:00 2001 From: yuyizh Date: Fri, 19 Mar 2021 14:20:46 +0800 Subject: [PATCH 1/3] add new parameter to initialize virtual-packet-logging --- tardis/base.py | 4 ++-- tardis/montecarlo/base.py | 4 ++-- tardis/simulation/base.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tardis/base.py b/tardis/base.py index 0df0aa6af03..3b9ef9ca4e3 100644 --- a/tardis/base.py +++ b/tardis/base.py @@ -2,7 +2,7 @@ def run_tardis( - config, atom_data=None, packet_source=None, simulation_callbacks=[] + config, atom_data=None, packet_source=None, simulation_callbacks=[], virtual_packet_logging=False ): """ This function is one of the core functions to run TARDIS from a given @@ -36,7 +36,7 @@ def run_tardis( tardis_config = Configuration.from_config_dict(config) simulation = Simulation.from_config( - tardis_config, packet_source=packet_source, atom_data=atom_data + tardis_config, packet_source=packet_source, atom_data=atom_data, virtual_packet_logging=virtual_packet_logging ) for cb in simulation_callbacks: simulation.add_callback(*cb) diff --git a/tardis/montecarlo/base.py b/tardis/montecarlo/base.py index ad5a8effeb5..2fd6ee7e1d7 100644 --- a/tardis/montecarlo/base.py +++ b/tardis/montecarlo/base.py @@ -502,7 +502,7 @@ def calculate_f_lambda(self, wavelength): pass @classmethod - def from_config(cls, config, packet_source=None): + def from_config(cls, config, packet_source=None, virtual_packet_logging=False): """ Create a new MontecarloRunner instance from a Configuration object. @@ -552,5 +552,5 @@ def from_config(cls, config, packet_source=None): debug_packets=config.montecarlo.debug_packets, logger_buffer=config.montecarlo.logger_buffer, single_packet_seed=config.montecarlo.single_packet_seed, - virtual_packet_logging=config.spectrum.virtual.virtual_packet_logging, + virtual_packet_logging=(config.spectrum.virtual.virtual_packet_logging | virtual_packet_logging), ) diff --git a/tardis/simulation/base.py b/tardis/simulation/base.py index 666a16600a8..0f7f1feddf0 100644 --- a/tardis/simulation/base.py +++ b/tardis/simulation/base.py @@ -502,7 +502,7 @@ def remove_callback(self, id): return False @classmethod - def from_config(cls, config, packet_source=None, **kwargs): + def from_config(cls, config, packet_source=None, virtual_packet_logging=False, **kwargs): """ Create a new Simulation instance from a Configuration object. @@ -541,7 +541,7 @@ def from_config(cls, config, packet_source=None, **kwargs): runner = kwargs["runner"] else: runner = MontecarloRunner.from_config( - config, packet_source=packet_source + config, packet_source=packet_source, virtual_packet_logging=virtual_packet_logging ) luminosity_nu_start = config.supernova.luminosity_wavelength_end.to( From 7b79ea57ad32712962ec5787d0de7e0398b160ef Mon Sep 17 00:00:00 2001 From: yuyizh Date: Sat, 20 Mar 2021 14:00:33 +0800 Subject: [PATCH 2/3] add docstring --- tardis/base.py | 7 ++++++ tardis/montecarlo/base.py | 45 +++++++++++++++++++++++++++++++++++++++ tardis/simulation/base.py | 4 ++++ 3 files changed, 56 insertions(+) diff --git a/tardis/base.py b/tardis/base.py index 3b9ef9ca4e3..d4d44da34c7 100644 --- a/tardis/base.py +++ b/tardis/base.py @@ -19,6 +19,13 @@ def run_tardis( the atomic data. Atomic data to use for this TARDIS simulation. If set to None, the atomic data will be loaded according to keywords set in the configuration [default=None] + virtual_packet_logging : bool + option to enable virtual packet logging + [default=False] + + Returns + ------- + Simulation """ from tardis.io.config_reader import Configuration from tardis.io.atom_data.base import AtomData diff --git a/tardis/montecarlo/base.py b/tardis/montecarlo/base.py index 2fd6ee7e1d7..e88237adc14 100644 --- a/tardis/montecarlo/base.py +++ b/tardis/montecarlo/base.py @@ -425,7 +425,18 @@ def montecarlo_virtual_luminosity(self): def calculate_emitted_luminosity( self, luminosity_nu_start, luminosity_nu_end ): + """ + Calculate emitted luminosity. + + Parameters + ---------- + luminosity_nu_start : astropy.units.Quantity + luminosity_nu_end : astropy.units.Quantity + Returns + ------- + astropy.units.Quantity + """ luminosity_wavelength_filter = ( self.emitted_packet_nu > luminosity_nu_start ) & (self.emitted_packet_nu < luminosity_nu_end) @@ -439,7 +450,18 @@ def calculate_emitted_luminosity( def calculate_reabsorbed_luminosity( self, luminosity_nu_start, luminosity_nu_end ): + """ + Calculate reabsorbed luminosity. + Parameters + ---------- + luminosity_nu_start : astropy.units.Quantity + luminosity_nu_end : astropy.units.Quantity + + Returns + ------- + astropy.units.Quantity + """ luminosity_wavelength_filter = ( self.reabsorbed_packet_nu > luminosity_nu_start ) & (self.reabsorbed_packet_nu < luminosity_nu_end) @@ -484,6 +506,17 @@ def calculate_radiationfield_properties(self): return t_rad * u.K, w def calculate_luminosity_inner(self, model): + """ + Calculate inner luminosity. + + Parameters + ---------- + model : model.Radial1DModel + + Returns + ------- + astropy.units.Quantity + """ return ( 4 * np.pi @@ -493,6 +526,17 @@ def calculate_luminosity_inner(self, model): ).to("erg/s") def calculate_time_of_simulation(self, model): + """ + Calculate time of montecarlo simulation. + + Parameters + ---------- + model : model.Radial1DModel + + Returns + ------- + float + """ return 1.0 * u.erg / self.calculate_luminosity_inner(model) def calculate_f_nu(self, frequency): @@ -509,6 +553,7 @@ def from_config(cls, config, packet_source=None, virtual_packet_logging=False): Parameters ---------- config : tardis.io.config_reader.Configuration + virtual_packet_logging : bool Returns ------- diff --git a/tardis/simulation/base.py b/tardis/simulation/base.py index 0f7f1feddf0..b8136a2ccfe 100644 --- a/tardis/simulation/base.py +++ b/tardis/simulation/base.py @@ -349,6 +349,9 @@ def iterate(self, no_of_packets, no_of_virtual_packets=0, last_run=False): self.iterations_executed += 1 def run(self): + """ + run the simulation + """ start_time = time.time() while self.iterations_executed < self.iterations - 1: self.store_plasma_state( @@ -509,6 +512,7 @@ def from_config(cls, config, packet_source=None, virtual_packet_logging=False, * Parameters ---------- config : tardis.io.config_reader.Configuration + **kwargs Allow overriding some structures, such as model, plasma, atomic data and the runner, instead of creating them from the configuration From 219c63966ec82113397651ce58ea8db88bf41dd2 Mon Sep 17 00:00:00 2001 From: yuyizh Date: Sat, 20 Mar 2021 14:15:44 +0800 Subject: [PATCH 3/3] reformat files with black --- tardis/base.py | 11 +++++++++-- tardis/simulation/base.py | 8 ++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tardis/base.py b/tardis/base.py index d4d44da34c7..55fcd9d766b 100644 --- a/tardis/base.py +++ b/tardis/base.py @@ -2,7 +2,11 @@ def run_tardis( - config, atom_data=None, packet_source=None, simulation_callbacks=[], virtual_packet_logging=False + config, + atom_data=None, + packet_source=None, + simulation_callbacks=[], + virtual_packet_logging=False, ): """ This function is one of the core functions to run TARDIS from a given @@ -43,7 +47,10 @@ def run_tardis( tardis_config = Configuration.from_config_dict(config) simulation = Simulation.from_config( - tardis_config, packet_source=packet_source, atom_data=atom_data, virtual_packet_logging=virtual_packet_logging + tardis_config, + packet_source=packet_source, + atom_data=atom_data, + virtual_packet_logging=virtual_packet_logging, ) for cb in simulation_callbacks: simulation.add_callback(*cb) diff --git a/tardis/simulation/base.py b/tardis/simulation/base.py index b8136a2ccfe..ae9f7aa7bc3 100644 --- a/tardis/simulation/base.py +++ b/tardis/simulation/base.py @@ -505,7 +505,9 @@ def remove_callback(self, id): return False @classmethod - def from_config(cls, config, packet_source=None, virtual_packet_logging=False, **kwargs): + def from_config( + cls, config, packet_source=None, virtual_packet_logging=False, **kwargs + ): """ Create a new Simulation instance from a Configuration object. @@ -545,7 +547,9 @@ def from_config(cls, config, packet_source=None, virtual_packet_logging=False, * runner = kwargs["runner"] else: runner = MontecarloRunner.from_config( - config, packet_source=packet_source, virtual_packet_logging=virtual_packet_logging + config, + packet_source=packet_source, + virtual_packet_logging=virtual_packet_logging, ) luminosity_nu_start = config.supernova.luminosity_wavelength_end.to(