SNEWS2 / snewpy

A Python package for working with supernova neutrinos
https://snewpy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
26 stars 19 forks source link

Allow array as `t` argument for `SupernovaModel.get_*_spectra` #221

Closed Sheshuk closed 1 year ago

Sheshuk commented 1 year ago

What I want

As a user I need to build the neutrino spectrum vs. time. I want to be able to calculate this 2d spectra directly from SupernovaModel without constructing loops on the t variable:

#Loading the SN model
model = ccsn.Bollig_2016('models/Bollig_2016/s11.2c')
#time and energy bins
t = np.linspace(0,10,101)<<u.s
E = np.linspace(0,50,51)<<u.MeV

fluxes2d = model.get_initial_spectra(t,E)
assert fluxes2d[0].shape == (101, 51)

#backward compatibility: if we provide a scalar time we get 1D arrays.
#Let's take t[20] as scalar value
fluxes1d = model.get_initial_spectra(t[20],E)
assert fluxes1d[0].shape == (51)

#check that 1d and 2d results are compatible
for flavor in fluxes2d:
   assert np.allaclose(fluxes2d[flavor][20], fluxes1d)

What we have now

Currently SupernovaModel.get_initial_spectra and SupernovaModel.get_transformed_spectra can work only with a single t value. As far as I understand, the only limitation is in the implementration of PinchedModel, so it can be easily corrected.

This is in addition to the existing test_snowglobes.py tests, which should work without modification and provide crosscheck with the events table.

Outlook

This should make possible further optimization in snowglobes.generate_* functions - getting rid of the time loops there can speed up computation and make code cleaner.

After implementing #214 we can use NeutrinoFlux class as the return value.