Skip to content

Commit

Permalink
Continuing to fix config references
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfullard committed Feb 28, 2024
1 parent 8acdcf0 commit a4b0faf
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 98 deletions.
5 changes: 3 additions & 2 deletions tardis/montecarlo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
)
from tardis.montecarlo.montecarlo_configuration import (
MonteCarloConfiguration,
configuration_initialize,
)
from tardis.montecarlo.montecarlo_numba import (
montecarlo_main_loop,
Expand Down Expand Up @@ -143,8 +144,8 @@ def initialize_transport_state(
transport_state._integrator = FormalIntegrator(
simulation_state, plasma, self
)
self.montecarlo_configuration.configuration_initialize(
transport=self, number_of_vpackets=no_of_virtual_packets
configuration_initialize(
self.montecarlo_configuration, self, no_of_virtual_packets
)

return transport_state
Expand Down
59 changes: 30 additions & 29 deletions tardis/montecarlo/montecarlo_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self):
self.SURVIVAL_PROBABILITY = 0.0
self.VPACKET_TAU_RUSSIAN = 10.0

self.INITIAL_TRACKING_ARRAY_LENGTH = None
self.INITIAL_TRACKING_ARRAY_LENGTH = 0
self.LEGACY_MODE_ENABLED = False

self.ENABLE_RPACKET_TRACKING = False
Expand All @@ -52,32 +52,33 @@ def __init__(self):
self.VPACKET_SPAWN_END_FREQUENCY = 1e200
self.ENABLE_VPACKET_TRACKING = False

def configuration_initialize(self, transport, number_of_vpackets):
if transport.line_interaction_type == "macroatom":
self.LINE_INTERACTION_TYPE = LineInteractionType.MACROATOM
elif transport.line_interaction_type == "downbranch":
self.LINE_INTERACTION_TYPE = LineInteractionType.DOWNBRANCH
elif transport.line_interaction_type == "scatter":
self.LINE_INTERACTION_TYPE = LineInteractionType.SCATTER
else:
raise ValueError(
f'Line interaction type must be one of "macroatom",'
f'"downbranch", or "scatter" but is '
f"{transport.line_interaction_type}"
)
self.NUMBER_OF_VPACKETS = number_of_vpackets
self.TEMPORARY_V_PACKET_BINS = number_of_vpackets
self.ENABLE_FULL_RELATIVITY = transport.enable_full_relativity
self.MONTECARLO_SEED = transport.packet_source.base_seed
self.VPACKET_SPAWN_START_FREQUENCY = (
transport.virtual_spectrum_spawn_range.end.to(
u.Hz, equivalencies=u.spectral()
).value
)
self.VPACKET_SPAWN_END_FREQUENCY = (
transport.virtual_spectrum_spawn_range.start.to(
u.Hz, equivalencies=u.spectral()
).value

def configuration_initialize(config, transport, number_of_vpackets):
if transport.line_interaction_type == "macroatom":
config.LINE_INTERACTION_TYPE = LineInteractionType.MACROATOM
elif transport.line_interaction_type == "downbranch":
config.LINE_INTERACTION_TYPE = LineInteractionType.DOWNBRANCH
elif transport.line_interaction_type == "scatter":
config.LINE_INTERACTION_TYPE = LineInteractionType.SCATTER
else:
raise ValueError(
f'Line interaction type must be one of "macroatom",'
f'"downbranch", or "scatter" but is '
f"{transport.line_interaction_type}"
)
self.ENABLE_VPACKET_TRACKING = transport.enable_vpacket_tracking
self.ENABLE_RPACKET_TRACKING = transport.enable_rpacket_tracking
config.NUMBER_OF_VPACKETS = number_of_vpackets
config.TEMPORARY_V_PACKET_BINS = number_of_vpackets
config.ENABLE_FULL_RELATIVITY = transport.enable_full_relativity
config.MONTECARLO_SEED = transport.packet_source.base_seed
config.VPACKET_SPAWN_START_FREQUENCY = (
transport.virtual_spectrum_spawn_range.end.to(
u.Hz, equivalencies=u.spectral()
).value
)
config.VPACKET_SPAWN_END_FREQUENCY = (
transport.virtual_spectrum_spawn_range.start.to(
u.Hz, equivalencies=u.spectral()
).value
)
config.ENABLE_VPACKET_TRACKING = transport.enable_vpacket_tracking
config.ENABLE_RPACKET_TRACKING = transport.enable_rpacket_tracking
60 changes: 19 additions & 41 deletions tardis/montecarlo/montecarlo_numba/interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ def determine_bf_macro_activation_idx(
# ionization energy is created
nu_threshold = opacity_state.photo_ion_nu_threshold_mins[continuum_id]
fraction_ionization = nu_threshold / nu
if (
np.random.random() < fraction_ionization
): # Create ionization energy (i-packet)
destination_level_idx = opacity_state.photo_ion_activation_idx[
continuum_id
]
if np.random.random() < fraction_ionization: # Create ionization energy (i-packet)
destination_level_idx = opacity_state.photo_ion_activation_idx[continuum_id]
else: # Create thermal energy (k-packet)
destination_level_idx = opacity_state.k_packet_idx
return destination_level_idx
Expand Down Expand Up @@ -169,9 +165,7 @@ def continuum_event(
r_packet.r, r_packet.mu, time_explosion, enable_full_relativity
)
comov_energy = r_packet.energy * old_doppler_factor
comov_nu = (
r_packet.nu * old_doppler_factor
) # make sure frequency should be updated
comov_nu = r_packet.nu * old_doppler_factor # make sure frequency should be updated
r_packet.energy = comov_energy * inverse_doppler_factor
r_packet.nu = comov_nu * inverse_doppler_factor

Expand Down Expand Up @@ -254,7 +248,6 @@ def macro_atom_event(
transition_id,
time_explosion,
opacity_state,
continuum_processes_enabled,
enable_full_relativity,
)
else:
Expand All @@ -272,9 +265,7 @@ def bf_cooling(r_packet, time_explosion, opacity_state):
time_explosion : float
opacity_state : tardis.montecarlo.montecarlo_numba.numba_interface.OpacityState
"""
fb_cooling_prob = opacity_state.p_fb_deactivation[
:, r_packet.current_shell_id
]
fb_cooling_prob = opacity_state.p_fb_deactivation[:, r_packet.current_shell_id]
p = fb_cooling_prob[0]
i = 0
zrand = np.random.random()
Expand Down Expand Up @@ -317,9 +308,7 @@ def get_current_line_id(nu, line_list):


@njit(**njit_dict_no_parallel)
def free_free_emission(
r_packet, time_explosion, opacity_state, full_relativity
):
def free_free_emission(r_packet, time_explosion, opacity_state, enable_full_relativity):
"""
Free-Free emission - set the frequency from electron-ion interaction
Expand All @@ -330,22 +319,20 @@ def free_free_emission(
opacity_state : tardis.montecarlo.montecarlo_numba.numba_interface.OpacityState
"""
inverse_doppler_factor = get_inverse_doppler_factor(
r_packet.r, r_packet.mu, time_explosion
r_packet.r, r_packet.mu, time_explosion, enable_full_relativity
)
comov_nu = sample_nu_free_free(opacity_state, r_packet.current_shell_id)
r_packet.nu = comov_nu * inverse_doppler_factor
current_line_id = get_current_line_id(comov_nu, opacity_state.line_list_nu)
r_packet.next_line_id = current_line_id

if full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(
r_packet, time_explosion, r_packet.mu
)
if enable_full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(r_packet, time_explosion, r_packet.mu)


@njit(**njit_dict_no_parallel)
def bound_free_emission(
r_packet, time_explosion, opacity_state, continuum_id, full_relativity
r_packet, time_explosion, opacity_state, continuum_id, enable_full_relativity
):
"""
Bound-Free emission - set the frequency from photo-ionization
Expand All @@ -358,7 +345,7 @@ def bound_free_emission(
continuum_id : int
"""
inverse_doppler_factor = get_inverse_doppler_factor(
r_packet.r, r_packet.mu, time_explosion
r_packet.r, r_packet.mu, time_explosion, enable_full_relativity
)

comov_nu = sample_nu_free_bound(
Expand All @@ -368,10 +355,8 @@ def bound_free_emission(
current_line_id = get_current_line_id(comov_nu, opacity_state.line_list_nu)
r_packet.next_line_id = current_line_id

if full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(
r_packet, time_explosion, r_packet.mu
)
if enable_full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(r_packet, time_explosion, r_packet.mu)


@njit(**njit_dict_no_parallel)
Expand Down Expand Up @@ -402,9 +387,7 @@ def thomson_scatter(r_packet, time_explosion, enable_full_relativity):
r_packet.nu = comov_nu * inverse_new_doppler_factor
r_packet.energy = comov_energy * inverse_new_doppler_factor
if enable_full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(
r_packet, time_explosion, r_packet.mu
)
r_packet.mu = angle_aberration_CMF_to_LF(r_packet, time_explosion, r_packet.mu)
temp_doppler_factor = get_doppler_factor(
r_packet.r, r_packet.mu, time_explosion, enable_full_relativity
)
Expand Down Expand Up @@ -435,7 +418,7 @@ def line_scatter(
r_packet.mu = get_random_mu()

inverse_new_doppler_factor = get_inverse_doppler_factor(
r_packet.r, r_packet.mu, time_explosion
r_packet.r, r_packet.mu, time_explosion, enable_full_relativity
)

comov_energy = r_packet.energy * old_doppler_factor
Expand All @@ -447,7 +430,6 @@ def line_scatter(
r_packet.next_line_id,
time_explosion,
opacity_state,
continuum_processes_enabled,
enable_full_relativity,
)
else: # includes both macro atom and downbranch - encoded in the transition probabilities
Expand All @@ -468,7 +450,7 @@ def line_scatter(

@njit(**njit_dict_no_parallel)
def line_emission(
r_packet, emission_line_id, time_explosion, opacity_state, full_relativity
r_packet, emission_line_id, time_explosion, opacity_state, enable_full_relativity
):
"""
Sets the frequency of the RPacket properly given the emission channel
Expand All @@ -486,15 +468,11 @@ def line_emission(
if emission_line_id != r_packet.next_line_id:
pass
inverse_doppler_factor = get_inverse_doppler_factor(
r_packet.r, r_packet.mu, time_explosion
)
r_packet.nu = (
opacity_state.line_list_nu[emission_line_id] * inverse_doppler_factor
r_packet.r, r_packet.mu, time_explosion, enable_full_relativity
)
r_packet.nu = opacity_state.line_list_nu[emission_line_id] * inverse_doppler_factor
r_packet.next_line_id = emission_line_id + 1
nu_line = opacity_state.line_list_nu[emission_line_id]

if full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(
r_packet, time_explosion, r_packet.mu
)
if enable_full_relativity:
r_packet.mu = angle_aberration_CMF_to_LF(r_packet, time_explosion, r_packet.mu)
34 changes: 27 additions & 7 deletions tardis/montecarlo/montecarlo_numba/single_packet_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,19 @@ def single_packet_loop(
set_packet_props_full_relativity(r_packet, numba_model)
else:
set_packet_props_partial_relativity(r_packet, numba_model)
r_packet.initialize_line_id(opacity_state, numba_model)
r_packet.initialize_line_id(
opacity_state,
numba_model,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)

trace_vpacket_volley(
r_packet,
vpacket_collection,
numba_radial_1d_geometry,
numba_model,
opacity_state,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)

if montecarlo_configuration.ENABLE_RPACKET_TRACKING:
Expand Down Expand Up @@ -150,7 +155,11 @@ def single_packet_loop(

if interaction_type == InteractionType.BOUNDARY:
move_r_packet(
r_packet, distance, numba_model.time_explosion, estimators
r_packet,
distance,
numba_model.time_explosion,
estimators,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)
move_packet_across_shell_boundary(
r_packet, delta_shell, len(numba_radial_1d_geometry.r_inner)
Expand All @@ -159,28 +168,33 @@ def single_packet_loop(
elif interaction_type == InteractionType.LINE:
r_packet.last_interaction_type = 2
move_r_packet(
r_packet, distance, numba_model.time_explosion, estimators
r_packet,
distance,
numba_model.time_explosion,
estimators,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)
line_scatter(
r_packet,
numba_model.time_explosion,
line_interaction_type,
opacity_state,
montecarlo_configuration.CONTINUUM_PROCESSES_ENABLED,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)
trace_vpacket_volley(
r_packet,
vpacket_collection,
numba_radial_1d_geometry,
numba_model,
opacity_state,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)

elif interaction_type == InteractionType.ESCATTERING:
r_packet.last_interaction_type = 1

move_r_packet(
r_packet, distance, numba_model.time_explosion, estimators
)
move_r_packet(r_packet, distance, numba_model.time_explosion, estimators)
thomson_scatter(r_packet, numba_model.time_explosion)

trace_vpacket_volley(
Expand All @@ -189,14 +203,19 @@ def single_packet_loop(
numba_radial_1d_geometry,
numba_model,
opacity_state,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)
elif (
montecarlo_configuration.CONTINUUM_PROCESSES_ENABLED
and interaction_type == InteractionType.CONTINUUM_PROCESS
):
r_packet.last_interaction_type = InteractionType.CONTINUUM_PROCESS
move_r_packet(
r_packet, distance, numba_model.time_explosion, estimators
r_packet,
distance,
numba_model.time_explosion,
estimators,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)
continuum_event(
r_packet,
Expand All @@ -214,6 +233,7 @@ def single_packet_loop(
numba_radial_1d_geometry,
numba_model,
opacity_state,
montecarlo_configuration.ENABLE_FULL_RELATIVITY,
)
else:
pass
Expand Down
9 changes: 7 additions & 2 deletions tardis/montecarlo/montecarlo_numba/tests/test_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ def test_line_scatter(
time_explosion = verysimple_numba_model.time_explosion

interaction.line_scatter(
packet, time_explosion, line_interaction_type, verysimple_opacity_state
packet,
time_explosion,
line_interaction_type,
verysimple_opacity_state,
False,
False,
)

assert np.abs(packet.mu - init_mu) > 1e-7
Expand Down Expand Up @@ -94,7 +99,7 @@ def test_line_emission(
time_explosion = verysimple_numba_model.time_explosion

interaction.line_emission(
packet, emission_line_id, time_explosion, verysimple_opacity_state
packet, emission_line_id, time_explosion, verysimple_opacity_state, False
)

assert packet.next_line_id == emission_line_id + 1
Expand Down
Loading

0 comments on commit a4b0faf

Please sign in to comment.