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

Implement last interaction tracker #2716

Merged
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
38 changes: 38 additions & 0 deletions tardis/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from tardis.simulation import Simulation
from tardis.tests.fixtures.atom_data import *
from tardis.tests.fixtures.regression_data import regression_data
from tardis import run_tardis

# ensuring that regression_data is not removed by ruff
assert regression_data is not None
Expand Down Expand Up @@ -219,6 +220,14 @@ def config_verysimple(example_configuration_dir):
)


# Creating a new config object so as to not overwrite other config.
@pytest.fixture(scope="session")
def config_rpacket_tracking(example_configuration_dir):
return Configuration.from_yaml(
example_configuration_dir / "tardis_configv1_verysimple.yml"
)


@pytest.fixture(scope="function")
def config_montecarlo_1e5_verysimple(example_configuration_dir):
return Configuration.from_yaml(
Expand All @@ -243,3 +252,32 @@ def simulation_verysimple_vpacket_tracking(config_verysimple, atomic_dataset):
sim.last_no_of_packets = 4000
sim.run_final()
return sim


@pytest.fixture(scope="session")
def simulation_rpacket_tracking(config_rpacket_tracking, atomic_dataset):
"""
Creating a simulation object using a simple configuration

Parameters
----------
config_verysimple_rpacket_tracking : tardis.io.configuration.config_reader.Configuration
atomic_dataset : AtomData

Returns
-------
simulation object with track_rpacket enabled
"""
config_rpacket_tracking.montecarlo.iterations = 3
config_rpacket_tracking.montecarlo.no_of_packets = 4000
config_rpacket_tracking.montecarlo.last_no_of_packets = -1

config_rpacket_tracking.montecarlo.tracking.track_rpacket = True

atomic_data = deepcopy(atomic_dataset)
sim = run_tardis(
config_rpacket_tracking,
atom_data=atomic_data,
show_convergence_plots=False,
)
return sim
3 changes: 0 additions & 3 deletions tardis/transport/montecarlo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
}

from tardis.transport.montecarlo.r_packet import RPacket
from tardis.transport.montecarlo.montecarlo_main_loop import (
montecarlo_main_loop,
)
from tardis.transport.montecarlo.packet_collections import (
PacketCollection,
)
13 changes: 7 additions & 6 deletions tardis/transport/montecarlo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
from tardis import constants as const
from tardis.io.logger import montecarlo_tracking as mc_tracker
from tardis.io.util import HDFWriterMixin
from tardis.transport.montecarlo import (
from tardis.transport.montecarlo.montecarlo_main_loop import (
montecarlo_main_loop,
)
from tardis.transport.montecarlo.configuration import montecarlo_globals
from tardis.transport.montecarlo.configuration.base import (
MonteCarloConfiguration,
configuration_initialize,
Expand Down Expand Up @@ -210,11 +209,13 @@

update_iterations_pbar(1)
refresh_packet_pbar()
# Condition for Checking if RPacket Tracking is enabled
if self.enable_rpacket_tracking:
transport_state.rpacket_tracker = rpacket_trackers

if self.transport_state.rpacket_tracker is not None:
transport_state.rpacket_tracker = rpacket_trackers

Check warning on line 213 in tardis/transport/montecarlo/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/base.py#L213

Added line #L213 was not covered by tests

# Need to change the implementation of rpacket_trackers_to_dataframe
# Such that it also takes of the case of
# RPacketLastInteractionTracker
if self.enable_rpacket_tracking:

Check warning on line 218 in tardis/transport/montecarlo/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/base.py#L218

Added line #L218 was not covered by tests
self.transport_state.rpacket_tracker_df = (
rpacket_trackers_to_dataframe(
self.transport_state.rpacket_tracker
Expand Down
4 changes: 4 additions & 0 deletions tardis/transport/montecarlo/configuration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np

from tardis.transport.montecarlo.configuration import montecarlo_globals
from tardis.transport.montecarlo import montecarlo_main_loop
from tardis.transport.montecarlo.numba_interface import (
LineInteractionType,
)
Expand Down Expand Up @@ -80,3 +81,6 @@
montecarlo_globals.ENABLE_RPACKET_TRACKING = (
transport.enable_rpacket_tracking
)
montecarlo_main_loop.ENABLE_RPACKET_TRACKING = (

Check warning on line 84 in tardis/transport/montecarlo/configuration/base.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/configuration/base.py#L84

Added line #L84 was not covered by tests
transport.enable_rpacket_tracking
)
27 changes: 19 additions & 8 deletions tardis/transport/montecarlo/montecarlo_main_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
consolidate_vpacket_tracker,
initialize_last_interaction_tracker,
)
from tardis.transport.montecarlo.packet_trackers import RPacketTracker
import tardis.transport.montecarlo.montecarlo_main_loop as montecarlo_loop
from tardis.transport.montecarlo.packet_trackers import (
RPacketTracker,
RPacketLastInteractionTracker,
)
from tardis.transport.montecarlo.r_packet import (
PacketStatus,
RPacket,
Expand All @@ -20,6 +24,8 @@
)
from tardis.util.base import update_packet_pbar

ENABLE_RPACKET_TRACKING = False


@njit(**njit_dict)
def montecarlo_main_loop(
Expand Down Expand Up @@ -73,6 +79,17 @@
vpacket_collections = List()
# Configuring the Tracking for R_Packets
rpacket_trackers = List()
if ENABLE_RPACKET_TRACKING:
for i in range(no_of_packets):
rpacket_trackers.append(

Check warning on line 84 in tardis/transport/montecarlo/montecarlo_main_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/montecarlo_main_loop.py#L82-L84

Added lines #L82 - L84 were not covered by tests
RPacketTracker(
montecarlo_configuration.INITIAL_TRACKING_ARRAY_LENGTH
)
)
else:
for i in range(no_of_packets):
rpacket_trackers.append(RPacketLastInteractionTracker())

Check warning on line 91 in tardis/transport/montecarlo/montecarlo_main_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/montecarlo_main_loop.py#L90-L91

Added lines #L90 - L91 were not covered by tests

for i in range(no_of_packets):
vpacket_collections.append(
VPacketCollection(
Expand All @@ -84,11 +101,6 @@
montecarlo_configuration.TEMPORARY_V_PACKET_BINS,
)
)
rpacket_trackers.append(
RPacketTracker(
montecarlo_configuration.INITIAL_TRACKING_ARRAY_LENGTH
)
)

# Get the ID of the main thread and the number of threads
main_thread_id = get_thread_id()
Expand Down Expand Up @@ -128,7 +140,6 @@

# Get the local v_packet_collection for this thread
vpacket_collection = vpacket_collections[i]

# RPacket Tracker for this thread
rpacket_tracker = rpacket_trackers[i]

Expand Down Expand Up @@ -186,7 +197,7 @@
1,
)

if montecarlo_globals.ENABLE_RPACKET_TRACKING:
if ENABLE_RPACKET_TRACKING:

Check warning on line 200 in tardis/transport/montecarlo/montecarlo_main_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/montecarlo_main_loop.py#L200

Added line #L200 was not covered by tests
for rpacket_tracker in rpacket_trackers:
rpacket_tracker.finalize_array()

Expand Down
49 changes: 48 additions & 1 deletion tardis/transport/montecarlo/packet_trackers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
r : float
Radius of the shell where the RPacket is present
nu : float
Luminosity of the RPacket
Frequency of the RPacket
mu : float
Cosine of the angle made by the direction of movement of the RPacket from its original direction
energy : float
Expand Down Expand Up @@ -156,3 +156,50 @@
index=pd.MultiIndex.from_arrays(index_array, names=["index", "step"]),
columns=df_dtypes.names,
)


rpacket_last_interaction_tracker_spec = [
("index", int64),
("r", float64),
("nu", float64),
("energy", float64),
("shell_id", int64),
("interaction_type", int64),
]


@jitclass(rpacket_last_interaction_tracker_spec)
class RPacketLastInteractionTracker(object):
"""
Numba JITCLASS for storing the last interaction the RPacket undergoes.
Parameters
----------
index : int
Index position of each RPacket
r : float
Radius of the shell where the RPacket is present
nu : float
Frequency of the RPacket
energy : float
Energy possessed by the RPacket
shell_id : int
Current Shell No in which the last interaction happened
interaction_type: int
Type of interaction the rpacket undergoes
"""

def __init__(self):
self.index = -1
self.r = -1.0
self.nu = 0.0
self.energy = 0.0
self.shell_id = -1
self.interaction_type = -1

Check warning on line 197 in tardis/transport/montecarlo/packet_trackers.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/packet_trackers.py#L192-L197

Added lines #L192 - L197 were not covered by tests

def track(self, r_packet):
self.index = r_packet.index
self.r = r_packet.r
self.nu = r_packet.nu
self.energy = r_packet.energy
self.shell_id = r_packet.current_shell_id
self.interaction_type = r_packet.last_interaction_type

Check warning on line 205 in tardis/transport/montecarlo/packet_trackers.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/packet_trackers.py#L200-L205

Added lines #L200 - L205 were not covered by tests
22 changes: 19 additions & 3 deletions tardis/transport/montecarlo/single_packet_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
get_doppler_factor,
get_inverse_doppler_factor,
)
from tardis.transport.montecarlo.r_packet import RPacket
from tardis.transport.montecarlo.configuration import montecarlo_globals
from tardis.transport.montecarlo.estimators.radfield_estimator_calcs import (
update_bound_free_estimators,
Expand Down Expand Up @@ -83,8 +84,7 @@
montecarlo_configuration.SURVIVAL_PROBABILITY,
)

if montecarlo_globals.ENABLE_RPACKET_TRACKING:
rpacket_tracker.track(r_packet)
rpacket_tracker.track(r_packet)

Check warning on line 87 in tardis/transport/montecarlo/single_packet_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/single_packet_loop.py#L87

Added line #L87 was not covered by tests

# this part of the code is temporary and will be better incorporated
while r_packet.status == PacketStatus.IN_PROCESS:
Expand Down Expand Up @@ -257,9 +257,25 @@
)
else:
pass
if montecarlo_globals.ENABLE_RPACKET_TRACKING:
if interaction_type != InteractionType.BOUNDARY:

Check warning on line 260 in tardis/transport/montecarlo/single_packet_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/single_packet_loop.py#L260

Added line #L260 was not covered by tests
rpacket_tracker.track(r_packet)

# Registering the final boundary interaction.
# Only for RPacketTracker
andrewfullard marked this conversation as resolved.
Show resolved Hide resolved
# This is required by the RPacketPlotter tool
if montecarlo_globals.ENABLE_RPACKET_TRACKING:
temp_r_packet = RPacket(

Check warning on line 267 in tardis/transport/montecarlo/single_packet_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/single_packet_loop.py#L266-L267

Added lines #L266 - L267 were not covered by tests
andrewfullard marked this conversation as resolved.
Show resolved Hide resolved
r_packet.r,
r_packet.mu,
r_packet.nu,
r_packet.energy,
r_packet.seed,
r_packet.index,
)
temp_r_packet.current_shell_id = r_packet.current_shell_id
temp_r_packet.status = r_packet.status
rpacket_tracker.track(temp_r_packet)

Check warning on line 277 in tardis/transport/montecarlo/single_packet_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/transport/montecarlo/single_packet_loop.py#L275-L277

Added lines #L275 - L277 were not covered by tests


@njit
def set_packet_props_partial_relativity(r_packet, time_explosion):
Expand Down
73 changes: 0 additions & 73 deletions tardis/transport/montecarlo/tests/test_r_packet.py

This file was deleted.

Loading
Loading