-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathbase.py
351 lines (320 loc) · 11.8 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from numba import prange, njit, jit, objmode
import logging
import numpy as np
from tardis.montecarlo.montecarlo_numba.r_packet import (
RPacket,
PacketStatus,
)
from tardis.montecarlo.montecarlo_numba.utils import MonteCarloException
from tardis.montecarlo.montecarlo_numba.numba_interface import (
PacketCollection,
VPacketCollection,
RPacketCollection,
NumbaModel,
numba_plasma_initialize,
Estimators,
configuration_initialize,
)
from tardis.montecarlo import (
montecarlo_configuration as montecarlo_configuration,
)
from tardis.montecarlo.montecarlo_numba.single_packet_loop import (
single_packet_loop,
)
from tardis.montecarlo.montecarlo_numba import njit_dict
from numba.typed import List
from tardis.util.base import update_iterations_pbar, update_packet_pbar
def montecarlo_radial1d(
model,
plasma,
iteration,
no_of_packets,
total_iterations,
show_progress_bars,
runner,
):
packet_collection = PacketCollection(
runner.input_r,
runner.input_nu,
runner.input_mu,
runner.input_energy,
runner._output_nu,
runner._output_energy,
)
numba_model = NumbaModel(
runner.r_inner_cgs,
runner.r_outer_cgs,
model.time_explosion.to("s").value,
)
numba_plasma = numba_plasma_initialize(plasma, runner.line_interaction_type)
estimators = Estimators(
runner.j_estimator,
runner.nu_bar_estimator,
runner.j_blue_estimator,
runner.Edotlu_estimator,
)
packet_seeds = montecarlo_configuration.packet_seeds
number_of_vpackets = montecarlo_configuration.number_of_vpackets
(
v_packets_energy_hist,
last_interaction_type,
last_interaction_in_nu,
last_line_interaction_in_id,
last_line_interaction_out_id,
virt_packet_nus,
virt_packet_energies,
virt_packet_initial_mus,
virt_packet_initial_rs,
virt_packet_last_interaction_in_nu,
virt_packet_last_interaction_type,
virt_packet_last_line_interaction_in_id,
virt_packet_last_line_interaction_out_id,
tracked_rpackets,
) = montecarlo_main_loop(
packet_collection,
numba_model,
numba_plasma,
estimators,
runner.spectrum_frequency.value,
number_of_vpackets,
packet_seeds,
iteration=iteration,
show_progress_bars=show_progress_bars,
no_of_packets=no_of_packets,
total_iterations=total_iterations,
)
runner._montecarlo_virtual_luminosity.value[:] = v_packets_energy_hist
runner.last_interaction_type = last_interaction_type
runner.last_interaction_in_nu = last_interaction_in_nu
runner.last_line_interaction_in_id = last_line_interaction_in_id
runner.last_line_interaction_out_id = last_line_interaction_out_id
if montecarlo_configuration.VPACKET_LOGGING and number_of_vpackets > 0:
runner.virt_packet_nus = np.concatenate(
np.array(virt_packet_nus)
).ravel()
runner.virt_packet_energies = np.concatenate(
np.array(virt_packet_energies)
).ravel()
runner.virt_packet_initial_mus = np.concatenate(
np.array(virt_packet_initial_mus)
).ravel()
runner.virt_packet_initial_rs = np.concatenate(
np.array(virt_packet_initial_rs)
).ravel()
runner.virt_packet_last_interaction_in_nu = np.concatenate(
np.array(virt_packet_last_interaction_in_nu)
).ravel()
runner.virt_packet_last_interaction_type = np.concatenate(
np.array(virt_packet_last_interaction_type)
).ravel()
runner.virt_packet_last_line_interaction_in_id = np.concatenate(
np.array(virt_packet_last_line_interaction_in_id)
).ravel()
runner.virt_packet_last_line_interaction_out_id = np.concatenate(
np.array(virt_packet_last_line_interaction_out_id)
).ravel()
update_iterations_pbar(1)
# Condition for Checking if RPacket Tracking is enabled
if montecarlo_configuration.RPACKET_TRACKING:
runner.rpacket_tracker = tracked_rpackets
@njit(**njit_dict)
def montecarlo_main_loop(
packet_collection,
numba_model,
numba_plasma,
estimators,
spectrum_frequency,
number_of_vpackets,
packet_seeds,
iteration,
show_progress_bars,
no_of_packets,
total_iterations,
):
"""
This is the main loop of the MonteCarlo routine that generates packets
and sends them through the ejecta.
Parameters
----------
packet_collection : PacketCollection
numba_model : NumbaModel
estimators : NumbaEstimators
spectrum_frequency : astropy.units.Quantity
frequency bins
number_of_vpackets : int
VPackets released per interaction
packet_seeds : numpy.array
"""
output_nus = np.empty_like(packet_collection.packets_output_nu)
last_interaction_types = (
np.ones_like(packet_collection.packets_output_nu, dtype=np.int64) * -1
)
output_energies = np.empty_like(packet_collection.packets_output_nu)
last_interaction_in_nus = np.empty_like(packet_collection.packets_output_nu)
last_line_interaction_in_ids = (
np.ones_like(packet_collection.packets_output_nu, dtype=np.int64) * -1
)
last_line_interaction_out_ids = (
np.ones_like(packet_collection.packets_output_nu, dtype=np.int64) * -1
)
v_packets_energy_hist = np.zeros_like(spectrum_frequency)
delta_nu = spectrum_frequency[1] - spectrum_frequency[0]
# Pre-allocate a list of vpacket collections for later storage
vpacket_collections = List()
for i in range(len(output_nus)):
vpacket_collections.append(
VPacketCollection(
i,
spectrum_frequency,
montecarlo_configuration.v_packet_spawn_start_frequency,
montecarlo_configuration.v_packet_spawn_end_frequency,
number_of_vpackets,
montecarlo_configuration.temporary_v_packet_bins,
)
)
# Configuring the Tracking for R_Packets
tracked_rpackets = List()
for i in range(len(output_nus)):
tracked_rpackets.append(RPacketCollection())
# Arrays for vpacket logging
virt_packet_nus = []
virt_packet_energies = []
virt_packet_initial_mus = []
virt_packet_initial_rs = []
virt_packet_last_interaction_in_nu = []
virt_packet_last_interaction_type = []
virt_packet_last_line_interaction_in_id = []
virt_packet_last_line_interaction_out_id = []
for i in prange(len(output_nus)):
if show_progress_bars:
with objmode:
update_amount = 1
update_packet_pbar(
update_amount,
current_iteration=iteration,
no_of_packets=no_of_packets,
total_iterations=total_iterations,
)
if montecarlo_configuration.single_packet_seed != -1:
seed = packet_seeds[montecarlo_configuration.single_packet_seed]
np.random.seed(seed)
else:
seed = packet_seeds[i]
np.random.seed(seed)
r_packet = RPacket(
numba_model.r_inner[0],
packet_collection.packets_input_mu[i],
packet_collection.packets_input_nu[i],
packet_collection.packets_input_energy[i],
seed,
i,
)
vpacket_collection = vpacket_collections[i]
rpacket_collection = tracked_rpackets[i]
single_packet_loop(
r_packet,
numba_model,
numba_plasma,
estimators,
vpacket_collection,
rpacket_collection,
)
output_nus[i] = r_packet.nu
last_interaction_in_nus[i] = r_packet.last_interaction_in_nu
last_line_interaction_in_ids[i] = r_packet.last_line_interaction_in_id
last_line_interaction_out_ids[i] = r_packet.last_line_interaction_out_id
if r_packet.status == PacketStatus.REABSORBED:
output_energies[i] = -r_packet.energy
last_interaction_types[i] = r_packet.last_interaction_type
elif r_packet.status == PacketStatus.EMITTED:
output_energies[i] = r_packet.energy
last_interaction_types[i] = r_packet.last_interaction_type
vpackets_nu = vpacket_collection.nus[: vpacket_collection.idx]
vpackets_energy = vpacket_collection.energies[: vpacket_collection.idx]
vpackets_initial_mu = vpacket_collection.initial_mus[
: vpacket_collection.idx
]
vpackets_initial_r = vpacket_collection.initial_rs[
: vpacket_collection.idx
]
v_packets_idx = np.floor(
(vpackets_nu - spectrum_frequency[0]) / delta_nu
).astype(np.int64)
# if we're only in a single-packet mode
# if montecarlo_configuration.single_packet_seed == -1:
# break
for j, idx in enumerate(v_packets_idx):
if (vpackets_nu[j] < spectrum_frequency[0]) or (
vpackets_nu[j] > spectrum_frequency[-1]
):
continue
v_packets_energy_hist[idx] += vpackets_energy[j]
if montecarlo_configuration.VPACKET_LOGGING:
for vpacket_collection in vpacket_collections:
vpackets_nu = vpacket_collection.nus[: vpacket_collection.idx]
vpackets_energy = vpacket_collection.energies[
: vpacket_collection.idx
]
vpackets_initial_mu = vpacket_collection.initial_mus[
: vpacket_collection.idx
]
vpackets_initial_r = vpacket_collection.initial_rs[
: vpacket_collection.idx
]
virt_packet_nus.append(np.ascontiguousarray(vpackets_nu))
virt_packet_energies.append(np.ascontiguousarray(vpackets_energy))
virt_packet_initial_mus.append(
np.ascontiguousarray(vpackets_initial_mu)
)
virt_packet_initial_rs.append(
np.ascontiguousarray(vpackets_initial_r)
)
virt_packet_last_interaction_in_nu.append(
np.ascontiguousarray(
vpacket_collection.last_interaction_in_nu[
: vpacket_collection.idx
]
)
)
virt_packet_last_interaction_type.append(
np.ascontiguousarray(
vpacket_collection.last_interaction_type[
: vpacket_collection.idx
]
)
)
virt_packet_last_line_interaction_in_id.append(
np.ascontiguousarray(
vpacket_collection.last_interaction_in_id[
: vpacket_collection.idx
]
)
)
virt_packet_last_line_interaction_out_id.append(
np.ascontiguousarray(
vpacket_collection.last_interaction_out_id[
: vpacket_collection.idx
]
)
)
if montecarlo_configuration.RPACKET_TRACKING:
for rpacket_collection in tracked_rpackets:
rpacket_collection.finalize_array()
packet_collection.packets_output_energy[:] = output_energies[:]
packet_collection.packets_output_nu[:] = output_nus[:]
return (
v_packets_energy_hist,
last_interaction_types,
last_interaction_in_nus,
last_line_interaction_in_ids,
last_line_interaction_out_ids,
virt_packet_nus,
virt_packet_energies,
virt_packet_initial_mus,
virt_packet_initial_rs,
virt_packet_last_interaction_in_nu,
virt_packet_last_interaction_type,
virt_packet_last_line_interaction_in_id,
virt_packet_last_line_interaction_out_id,
tracked_rpackets,
)