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

Feature request: add virtual-packet-logging option to run_tardis() #1498

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 16 additions & 2 deletions tardis/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@


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
Expand All @@ -19,6 +23,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
Expand All @@ -36,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
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)
Expand Down
49 changes: 47 additions & 2 deletions tardis/montecarlo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -502,13 +546,14 @@ 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.

Parameters
----------
config : tardis.io.config_reader.Configuration
virtual_packet_logging : bool

Returns
-------
Expand Down Expand Up @@ -552,5 +597,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),
)
12 changes: 10 additions & 2 deletions tardis/simulation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -502,13 +505,16 @@ 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.

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
Expand Down Expand Up @@ -541,7 +547,9 @@ 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(
Expand Down