Skip to content

Commit

Permalink
Changed the positron fraction and updated with a function to extract …
Browse files Browse the repository at this point in the history
…packet info
  • Loading branch information
Knights-Templars committed Aug 26, 2024
1 parent 76115ad commit 906935e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 14 deletions.
33 changes: 25 additions & 8 deletions tardis/energy_input/gamma_ray_packet_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,22 @@ def create_packets(
nus_cmf = np.zeros(number_of_packets)
statuses = np.ones(number_of_packets, dtype=np.int64) * 3

# sample packets from the gamma-ray lines
# sample packets from the gamma-ray lines only (include X-rays!)
sampled_packets_df_gamma = decays_per_isotope[

Check warning on line 744 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L744

Added line #L744 was not covered by tests
decays_per_isotope["radiation"] == "g"
]
# sample packets from dataframe, returning a dataframe where each row is
# a sampled packet

# sample packets from the gamma-ray lines at time = t0 : which is the start of the gamma-ray simulation
#sampled_packets_df_t0 = sampled_packets_df_gamma[sampled_packets_df_gamma.index.get_level_values("time") == self.times[0] * (u.s).to(u.d)]
# sample same number of packets as the gamma-ray lines at time = 0
# sampled_packets_df_gamma_t0 = sampled_packets_df_t0.sample(
# n=number_of_packets,
# weights="decay_energy_erg",
# replace=True,
# random_state=np.random.RandomState(self.base_seed),
# )

# sample packets from the time evolving dataframe
sampled_packets_df = sampled_packets_df_gamma.sample(

Check warning on line 759 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L759

Added line #L759 was not covered by tests
n=number_of_packets,
weights="decay_energy_erg",
Expand All @@ -765,17 +775,21 @@ def create_packets(
sampled_packets_df["inner_velocity"] = self.inner_velocities[shells]
sampled_packets_df["outer_velocity"] = self.outer_velocities[shells]

# sample radii at time = 0
# sample radii at time = 0
initial_radii = self.create_packet_radii(sampled_packets_df)
# sample decay times
sampled_times = sampled_packets_df.index.get_level_values("time") * (

Check warning on line 781 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L781

Added line #L781 was not covered by tests
u.d
).to(u.s)
# TODO: Rewrite this without for loop. This is expensive
# get the time step index of the packets
decay_time_indices = []
for i in range(number_of_packets):
decay_time_indices.append(get_index(sampled_times[i], self.times))

Check warning on line 788 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L786-L788

Added lines #L786 - L788 were not covered by tests

# scale radius by packet decay time. This could be replaced with
# Geometry object calculations. Note that this also adds a random
# unit vector multiplication for 3D. May not be needed.
locations = (
initial_radii.values
* self.effective_times[decay_time_indices]
Expand Down Expand Up @@ -843,21 +857,24 @@ def calculate_positron_fraction(self, isotopes, number_of_packets):

# Find the positron fraction from the zeroth shell of the dataframe
shell_number_0 = self.isotope_decay_df[

Check warning on line 859 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L859

Added line #L859 was not covered by tests
self.isotope_decay_df.index.get_level_values("shell_number") == 0
self.isotope_decay_df.index.get_level_values("shell_number") == 0
]

gamma_decay_df = shell_number_0[shell_number_0["radiation"] == "g"]

Check warning on line 863 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L863

Added line #L863 was not covered by tests

positrons_decay_df = shell_number_0[shell_number_0["radiation"] == "bp"]

Check warning on line 865 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L865

Added line #L865 was not covered by tests
# Find the total energy released from positrons per isotope from the dataframe
positron_energy_per_isotope = positrons_decay_df.groupby("isotope")[

Check warning on line 867 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L867

Added line #L867 was not covered by tests
"energy_per_channel_keV"
].sum()
# Find the total energy released per isotope from the dataframe
total_energy_per_isotope = shell_number_0.groupby("isotope")[
# Find the total energy released from gamma-ray per isotope from the dataframe
gamma_energy_per_isotope = gamma_decay_df.groupby("isotope")[

Check warning on line 871 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L871

Added line #L871 was not covered by tests
"energy_per_channel_keV"
].sum()
# TODO: Possibly move this for loop
for i, isotope in enumerate(isotopes):
isotope_positron_fraction[i] = (

Check warning on line 876 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L875-L876

Added lines #L875 - L876 were not covered by tests
positron_energy_per_isotope[isotope]
/ total_energy_per_isotope[isotope]
/ gamma_energy_per_isotope[isotope]
)
return isotope_positron_fraction

Check warning on line 880 in tardis/energy_input/gamma_ray_packet_source.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/gamma_ray_packet_source.py#L880

Added line #L880 was not covered by tests
65 changes: 59 additions & 6 deletions tardis/energy_input/main_gamma_ray_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
iron_group_fraction_per_shell,
)
from tardis.energy_input.GXPacket import GXPacket
from tardis.energy_input.util import get_index
from tardis.energy_input.util import make_isotope_string_tardis_like

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -207,8 +208,6 @@ def run_gamma_ray_loop(
for i in range(num_decays)
]

# return packets

energy_bins = np.logspace(2, 3.8, spectrum_bins)
energy_out = np.zeros((len(energy_bins - 1), time_steps))
energy_deposited = np.zeros((number_of_shells, time_steps))
Expand All @@ -222,8 +221,8 @@ def run_gamma_ray_loop(
total_rf_energy = 0

Check warning on line 221 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L220-L221

Added lines #L220 - L221 were not covered by tests

for p in packets:
total_cmf_energy += p.energy_cmf
total_rf_energy += p.energy_rf
total_cmf_energy += p.energy_cmf
total_rf_energy += p.energy_rf

Check warning on line 225 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L223-L225

Added lines #L223 - L225 were not covered by tests

logger.info(f"Total CMF energy is {total_cmf_energy}")
logger.info(f"Total RF energy is {total_rf_energy}")

Check warning on line 228 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L227-L228

Added lines #L227 - L228 were not covered by tests
Expand Down Expand Up @@ -279,8 +278,62 @@ def run_gamma_ray_loop(
positron_energy = pd.DataFrame(

Check warning on line 278 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L278

Added line #L278 was not covered by tests
data=energy_deposited_positron, columns=times[:-1]
)
print(positron_energy.sum().sum())

total_deposited_energy = (positron_energy + deposited_energy) / dt_array

Check warning on line 282 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L282

Added line #L282 was not covered by tests

return escape_energy, packets_df_escaped, total_deposited_energy
return escape_energy, packets_df_escaped, deposited_energy, total_deposited_energy,

Check warning on line 284 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L284

Added line #L284 was not covered by tests



def get_packet_properties(number_of_shells, times, time_steps, packets):

"""
Function to get the properties of the packets.
Parameters
----------
packets : list
List of packets.
Returns
-------
packets_nu_cmf_array : np.ndarray
Array of packets in cmf.
packets_nu_rf_array : np.ndarray
Array of packets in rf.
packets_energy_cmf_array : np.ndarray
Array of packets energy in cmf.
packets_energy_rf_array : np.ndarray
Array of packets energy in rf.
packets_positron_energy_array : np.ndarray
Array of packets positron energy.
"""

#collect the properties of the packets
shell_number = []
time_current = []

Check warning on line 314 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L313-L314

Added lines #L313 - L314 were not covered by tests

# Bin the frequency of the packets in shell and time

packets_nu_cmf_array = np.zeros((number_of_shells, time_steps))
packets_nu_rf_array = np.zeros((number_of_shells, time_steps))
packets_energy_cmf_array = np.zeros((number_of_shells, time_steps))
packets_energy_rf_array = np.zeros((number_of_shells, time_steps))
packets_positron_energy_array = np.zeros((number_of_shells, time_steps))

Check warning on line 322 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L318-L322

Added lines #L318 - L322 were not covered by tests


for p in packets:
time_index = get_index(p.time_current, times)
shell_number.append(p.shell)
time_current.append(p.time_current)
packets_nu_cmf_array[p.shell, time_index] += p.nu_cmf
packets_nu_rf_array[p.shell, time_index] += p.nu_rf
packets_energy_cmf_array[p.shell, time_index] += p.energy_cmf
packets_energy_rf_array[p.shell, time_index] += p.energy_rf
packets_positron_energy_array[p.shell, time_index] += p.positron_energy

Check warning on line 333 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L325-L333

Added lines #L325 - L333 were not covered by tests


return packets_nu_cmf_array, packets_nu_rf_array, packets_energy_cmf_array, packets_energy_rf_array, packets_positron_energy_array

Check warning on line 336 in tardis/energy_input/main_gamma_ray_loop.py

View check run for this annotation

Codecov / codecov/patch

tardis/energy_input/main_gamma_ray_loop.py#L336

Added line #L336 was not covered by tests



0 comments on commit 906935e

Please sign in to comment.