From d4e1faf51cde78e62d0d5295f8078505def9ae07 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 12 Aug 2024 19:01:00 +0530 Subject: [PATCH 1/6] refactor packet_tracker Signed-off-by: Asish Kumar --- tardis/transport/montecarlo/packet_source.py | 4 +- .../transport/montecarlo/packet_trackers.py | 59 +++++++++---------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/tardis/transport/montecarlo/packet_source.py b/tardis/transport/montecarlo/packet_source.py index 9d0b3088522..44b00edc57d 100644 --- a/tardis/transport/montecarlo/packet_source.py +++ b/tardis/transport/montecarlo/packet_source.py @@ -145,6 +145,7 @@ class BlackBodySimpleSource(BasePacketSource, HDFWriterMixin): hdf_properties = ["radius", "temperature", "base_seed"] hdf_name = "black_body_simple_source" + l_coef = np.pi**4 / 90.0 @classmethod def from_simulation_state(cls, simulation_state, *args, **kwargs): @@ -210,7 +211,6 @@ def create_packet_nus(self, no_of_packets, l_samples=1000): numpy.ndarray """ l_array = np.cumsum(np.arange(1, l_samples, dtype=np.float64) ** -4) - l_coef = np.pi**4 / 90.0 # For testing purposes if self.legacy_mode_enabled: @@ -218,7 +218,7 @@ def create_packet_nus(self, no_of_packets, l_samples=1000): else: xis = self.rng.random((5, no_of_packets)) - l = l_array.searchsorted(xis[0] * l_coef) + 1.0 + l = l_array.searchsorted(xis[0] * self.l_coef) + 1.0 xis_prod = np.prod(xis[1:], 0) x = ne.evaluate("-log(xis_prod)/l") diff --git a/tardis/transport/montecarlo/packet_trackers.py b/tardis/transport/montecarlo/packet_trackers.py index 466fad27843..dc485bb82d7 100644 --- a/tardis/transport/montecarlo/packet_trackers.py +++ b/tardis/transport/montecarlo/packet_trackers.py @@ -68,13 +68,14 @@ def __init__(self, length): def extend_array(self, array, array_length): temp_array = np.empty( - array_length * self.extend_factor, dtype=array.dtype + array_length, dtype=array.dtype ) temp_array[:array_length] = array return temp_array def track(self, r_packet): if self.num_interactions >= self.length: + self.length = self.length * self.extend_factor self.status = self.extend_array(self.status, self.length) self.r = self.extend_array(self.r, self.length) self.nu = self.extend_array(self.nu, self.length) @@ -84,7 +85,6 @@ def track(self, r_packet): self.interaction_type = self.extend_array( self.interaction_type, self.length ) - self.length = self.length * self.extend_factor self.index = r_packet.index self.seed = r_packet.seed @@ -123,31 +123,34 @@ def rpacket_trackers_to_dataframe(rpacket_trackers): Dataframe containing properties of RPackets as columns like status, seed, r, nu, mu, energy, shell_id, interaction_type """ - len_df = sum([len(tracker.r) for tracker in rpacket_trackers]) - index_array = np.empty([2, len_df], dtype="int") - df_dtypes = np.dtype( - [ - ("status", np.int64), - ("seed", np.int64), - ("r", np.float64), - ("nu", np.float64), - ("mu", np.float64), - ("energy", np.float64), - ("shell_id", np.int64), - ("interaction_type", np.int64), - ] - ) + len_df = sum(len(tracker.r) for tracker in rpacket_trackers) + + index_array = np.empty((2, len_df), dtype="int") + df_dtypes = np.dtype([ + ("status", np.int64), + ("seed", np.int64), + ("r", np.float64), + ("nu", np.float64), + ("mu", np.float64), + ("energy", np.float64), + ("shell_id", np.int64), + ("interaction_type", np.int64), + ]) rpacket_tracker_ndarray = np.empty(len_df, df_dtypes) + cur_index = 0 for rpacket_tracker in rpacket_trackers: - prev_index = cur_index - cur_index = prev_index + len(rpacket_tracker.r) - for j, column_name in enumerate(df_dtypes.fields.keys()): - rpacket_tracker_ndarray[column_name][ - prev_index:cur_index - ] = getattr(rpacket_tracker, column_name) - index_array[0][prev_index:cur_index] = getattr(rpacket_tracker, "index") - index_array[1][prev_index:cur_index] = range(cur_index - prev_index) + length = len(rpacket_tracker.r) + next_index = cur_index + length + + for column_name in df_dtypes.names: + rpacket_tracker_ndarray[column_name][cur_index:next_index] = getattr(rpacket_tracker, column_name) + + index_array[0][cur_index:next_index] = rpacket_tracker.index + index_array[1][cur_index:next_index] = np.arange(length) + + cur_index = next_index + return pd.DataFrame( rpacket_tracker_ndarray, index=pd.MultiIndex.from_arrays(index_array, names=["index", "step"]), @@ -218,9 +221,7 @@ def generate_rpacket_tracker_list(no_of_packets, length): ------- A list containing RPacketTracker for each RPacket """ - rpacket_trackers = List() - for i in range(no_of_packets): - rpacket_trackers.append(RPacketTracker(length)) + rpacket_trackers = List([RPacketTracker(length) for _ in range(no_of_packets)]) return rpacket_trackers @@ -235,7 +236,5 @@ def generate_rpacket_last_interaction_tracker_list(no_of_packets): ------- A list containing RPacketLastInteractionTracker for each RPacket """ - rpacket_trackers = List() - for i in range(no_of_packets): - rpacket_trackers.append(RPacketLastInteractionTracker()) + rpacket_trackers = List([RPacketLastInteractionTracker() for _ in range(no_of_packets)]) return rpacket_trackers From bd2a7a460ddf36b18936ede36a03ee7fa023fc7c Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 13 Aug 2024 14:11:42 +0530 Subject: [PATCH 2/6] update generate_rpacket Signed-off-by: Asish Kumar --- tardis/transport/montecarlo/packet_trackers.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tardis/transport/montecarlo/packet_trackers.py b/tardis/transport/montecarlo/packet_trackers.py index b9ab4a208d1..b9aeb77d794 100644 --- a/tardis/transport/montecarlo/packet_trackers.py +++ b/tardis/transport/montecarlo/packet_trackers.py @@ -290,7 +290,8 @@ def generate_rpacket_tracker_list(no_of_packets, length): ------- A list containing RPacketTracker for each RPacket """ - rpacket_trackers = List([RPacketTracker(length) for _ in range(no_of_packets)]) + rPacketTracker = RPacketTracker(length) + rpacket_trackers = List([rPacketTracker for _ in range(no_of_packets)]) return rpacket_trackers @@ -305,5 +306,6 @@ def generate_rpacket_last_interaction_tracker_list(no_of_packets): ------- A list containing RPacketLastInteractionTracker for each RPacket """ - rpacket_trackers = List([RPacketLastInteractionTracker() for _ in range(no_of_packets)]) + rPacketLastInteractionTracker = RPacketLastInteractionTracker() + rpacket_trackers = List([rPacketLastInteractionTracker for _ in range(no_of_packets)]) return rpacket_trackers From deeb6ec0467ea2334e0353ad0ae55a690edd9e5e Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 13 Aug 2024 14:38:51 +0530 Subject: [PATCH 3/6] update generate_rpacket Signed-off-by: Asish Kumar --- tardis/transport/montecarlo/packet_trackers.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tardis/transport/montecarlo/packet_trackers.py b/tardis/transport/montecarlo/packet_trackers.py index b9aeb77d794..b9ab4a208d1 100644 --- a/tardis/transport/montecarlo/packet_trackers.py +++ b/tardis/transport/montecarlo/packet_trackers.py @@ -290,8 +290,7 @@ def generate_rpacket_tracker_list(no_of_packets, length): ------- A list containing RPacketTracker for each RPacket """ - rPacketTracker = RPacketTracker(length) - rpacket_trackers = List([rPacketTracker for _ in range(no_of_packets)]) + rpacket_trackers = List([RPacketTracker(length) for _ in range(no_of_packets)]) return rpacket_trackers @@ -306,6 +305,5 @@ def generate_rpacket_last_interaction_tracker_list(no_of_packets): ------- A list containing RPacketLastInteractionTracker for each RPacket """ - rPacketLastInteractionTracker = RPacketLastInteractionTracker() - rpacket_trackers = List([rPacketLastInteractionTracker for _ in range(no_of_packets)]) + rpacket_trackers = List([RPacketLastInteractionTracker() for _ in range(no_of_packets)]) return rpacket_trackers From d86fb39b41922c19e9de5a19d3909da668b421c1 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 13 Aug 2024 18:26:26 +0530 Subject: [PATCH 4/6] update generate_rpacket Signed-off-by: Asish Kumar --- tardis/transport/montecarlo/packet_trackers.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tardis/transport/montecarlo/packet_trackers.py b/tardis/transport/montecarlo/packet_trackers.py index b9ab4a208d1..7a6c642c0e9 100644 --- a/tardis/transport/montecarlo/packet_trackers.py +++ b/tardis/transport/montecarlo/packet_trackers.py @@ -290,7 +290,10 @@ def generate_rpacket_tracker_list(no_of_packets, length): ------- A list containing RPacketTracker for each RPacket """ - rpacket_trackers = List([RPacketTracker(length) for _ in range(no_of_packets)]) + rPacketTracker = RPacketTracker(length) + rpacket_trackers = List() + for _ in range(no_of_packets): + rpacket_trackers.append(rPacketTracker) return rpacket_trackers @@ -305,5 +308,8 @@ def generate_rpacket_last_interaction_tracker_list(no_of_packets): ------- A list containing RPacketLastInteractionTracker for each RPacket """ - rpacket_trackers = List([RPacketLastInteractionTracker() for _ in range(no_of_packets)]) + rpacket_trackers = List() + rPacketLastInteractionTracker = RPacketLastInteractionTracker() + for _ in range(no_of_packets): + rpacket_trackers.append(rPacketLastInteractionTracker) return rpacket_trackers From b2f9af5ef45f69d98fd7157c95029f6b1c6d0b5e Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Fri, 16 Aug 2024 16:46:32 +0530 Subject: [PATCH 5/6] change variable name in packet_tracker Signed-off-by: Asish Kumar --- tardis/transport/montecarlo/packet_trackers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tardis/transport/montecarlo/packet_trackers.py b/tardis/transport/montecarlo/packet_trackers.py index 7a6c642c0e9..ca07a524845 100644 --- a/tardis/transport/montecarlo/packet_trackers.py +++ b/tardis/transport/montecarlo/packet_trackers.py @@ -290,10 +290,10 @@ def generate_rpacket_tracker_list(no_of_packets, length): ------- A list containing RPacketTracker for each RPacket """ - rPacketTracker = RPacketTracker(length) + r_packet_tracker = RPacketTracker(length) rpacket_trackers = List() for _ in range(no_of_packets): - rpacket_trackers.append(rPacketTracker) + rpacket_trackers.append(r_packet_tracker) return rpacket_trackers @@ -309,7 +309,7 @@ def generate_rpacket_last_interaction_tracker_list(no_of_packets): A list containing RPacketLastInteractionTracker for each RPacket """ rpacket_trackers = List() - rPacketLastInteractionTracker = RPacketLastInteractionTracker() + r_packet_last_interaction_tracker = RPacketLastInteractionTracker() for _ in range(no_of_packets): - rpacket_trackers.append(rPacketLastInteractionTracker) + rpacket_trackers.append(r_packet_last_interaction_tracker) return rpacket_trackers From fd18ffd8953d85846bae67ec1d065222af550140 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Fri, 16 Aug 2024 16:55:13 +0530 Subject: [PATCH 6/6] revert to old changes because of increase in time Signed-off-by: Asish Kumar --- tardis/transport/montecarlo/packet_trackers.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tardis/transport/montecarlo/packet_trackers.py b/tardis/transport/montecarlo/packet_trackers.py index ca07a524845..423c38a7486 100644 --- a/tardis/transport/montecarlo/packet_trackers.py +++ b/tardis/transport/montecarlo/packet_trackers.py @@ -290,10 +290,9 @@ def generate_rpacket_tracker_list(no_of_packets, length): ------- A list containing RPacketTracker for each RPacket """ - r_packet_tracker = RPacketTracker(length) rpacket_trackers = List() for _ in range(no_of_packets): - rpacket_trackers.append(r_packet_tracker) + rpacket_trackers.append(RPacketTracker(length)) return rpacket_trackers @@ -309,7 +308,6 @@ def generate_rpacket_last_interaction_tracker_list(no_of_packets): A list containing RPacketLastInteractionTracker for each RPacket """ rpacket_trackers = List() - r_packet_last_interaction_tracker = RPacketLastInteractionTracker() for _ in range(no_of_packets): - rpacket_trackers.append(r_packet_last_interaction_tracker) + rpacket_trackers.append(RPacketLastInteractionTracker()) return rpacket_trackers