Adding the implementation of the FlavorScheme and FlavorMatrix#324
Conversation
|
Edit: Updated the conversion matrix User interfaceHere is the result of the interface I ended up with. Probably it's not minimal, but should be quite convenient when we implement the flavor transformations - the flavor conversions are done easily. Flavor schemes:Working with the flavor schemes is simple: each one is an IntEnum in It's easy to define a new flavor scheme using this function: from snewpy.flavor import FlavorScheme #base class for all flavors
ExtraSterileFavors = FlavorScheme.from_lepton_names(name='ExtraSteriles', leptons=['E','MU','TAU','S1','S2','S3'])
print(list(ExtraSterileFavors))prints: I made three schemes: from snewpy.flavor import TwoFlavor, ThreeFlavor, FourFlavorConversion matricesConversion matrices are defined using operators M2_to_3 = TwoFlavor>>ThreeFlavor
M4_to_2 = TwoFlavor<<FourFlavor
print(M2_to_3)prints: and they can be multiplied by each other: (ThreeFlavor<<TwoFlavor) @ (TwoFlavor<<FourFlavor)gives a matrix (obviously,we're losing the information about nu_mu/nu_tau): Flux ContainersContainer classes from Containers can be multiplied by the flavor matrices, in this manner:flux_oscillated = M @ flux Containers can also be converted to the different flavor schemes:#no need to know what was the flavor scheme of the flux
flux_3f = flux>>ThreeFlavorAccessing and slicing of the flux containersUser can directly access the desired components of the flux: #this works in all schemes,where we have NU_E
flux_nue = flux["NU_E"]
#this will fail on all schemes without NU_X
flux_nux = flux["NU_X"]
#this works only if the flux.flavor_scheme==TwoFlavor, otherwise raises
flux_nue = flux[TwoFlavor.NU_E] |
|
A more real example of making a plot with the flux from the model: import matplotlib.pyplot as plt
from snewpy.models import ccsn
import astropy.units as u
import numpy as np
#get the model
m = ccsn.Bollig_2016(progenitor_mass=27*u.Msun)
T = np.linspace(0,1, 201)<<u.s
E = np.linspace(0,100, 101)<<u.MeV
flux = m.get_flux(T,E, distance=10*u.kpc)
#integrate over energy
flux_vs_time_2f = flux.integrate('energy')
flux_vs_time_3f = flux_vs_time_2f>>ThreeFlavor
#function for plotting
def plot_time(flux, label='', **kwargs):
for flv in flux.flavor:
plt.plot(flux.time, flux.array[flv].squeeze(), label=label+flv.to_tex(), **kwargs)
plt.plot(flux.time, flux.array.sum(axis=0).squeeze(),label=label+'total', color='k', **kwargs)
plot_time(flux_vs_time_2f, lw=1, label='TwoFlavor ')
plot_time(flux_vs_time_3f, ls=':', lw=2, label='ThreeFlavor ')
plt.legend(ncols=2)
plt.show()Edit: updated the image |
|
Thanks Andrey, I'll check it out. It seems that NU_X means MU and TAU. Is that correct? It needs to mean MU or TAU. |
Sorry, I'm mixing our definition all the time. So it should be |
JostMigenda
left a comment
There was a problem hiding this comment.
The implementation is looking quite good; I have a couple of mostly minor comments.
Regarding the high-level API design questions, let’s continue in #309.
More compact flavor matrix definition Co-authored-by: Jost Migenda <jost.migenda@kcl.ac.uk>
JostMigenda
left a comment
There was a problem hiding this comment.
As agreed during yesterday’s SNEWS telecon, this + #335 looks good and is ready to be merged into the release_v2.0 branch. Thanks for the nice work!
Like we discussed, we’ll keep the TwoFlavor scheme so we can use the TwoFlavor>>ThreeFlavor matrices in the FlavorTransformations; once those are ready, we’ll see if there’s a bit more cleanup we can do.

This is a suggestion for #309
FlavorSchemebase class and buildingTwoFlavor, ThreeFlavor, FourFlavorenums as it's childrenFlavorMatrixclass, which contains:flavors1,flavors2: children ofFlavorSchemeflavor schemes - it means that matrix transforms fromflavor1space toflavors2array: np.ndarraythe matrix itselfFlavorShemes, and make them easily accessibleFlavorSchemeinside theflux.Containerclass (before it stored an array offlavorvalues, but we need to keep the full scheme, to be able to operate on it)FlavorMatrix @ FlavorMatrix -> FlavorMatrixFlavorMatrix @ flux.Container -> flux.ContainerSee the description below