CRPropa3 icon indicating copy to clipboard operation
CRPropa3 copied to clipboard

Improved Photon Field Sampling

Open ehlertdo opened this issue 4 months ago • 2 comments

Hi CRPropa team,

Brief Description

Background

I was recently trying to use CRPropa with custom, tabulated photon fields relevant for AGN environments. I have processed the fields with CRPropa3-data to get the right format to use with CRPropa and, excluding some minor issues with converting the units correctly, this works generally fine. To check that everything works correctly I also created a tabulated CMB (simple black body) to compare with crp.CMB(). For programmatic reasons my maximum tabulated photon energy (4e10 eV) is very large compared to the typical CMB photon temperature (CRPropa current, epsMax_CMB=0.1 eV).

Problem

I noticed that in this case there is a problem with the sampling of photon energies when running CRPropa (e.g. for photopion) with fields where the maximum tabulated energy is much larger than any energies where the photon density is non-negligible. This gives the following error error: no photon found in sampleEps, please make sure that photon field provides photons for the interaction by adapting the energy range of the tabulated photon field.. By truncating the tabulated CMB files at lower energies, e.g. 0.1 eV as is the current default, the error can be avoided. However, ideally CRPropa would work correctly no matter the tabulated range of energies.

Solution

After some digging I noticed that CRPropa appears to use a linear sampling for the photon field energies when processing the interactions. At present, only the maximum interaction probability can be sampled logarithmically (see #368). The relevant line of code is in PhotopionProduction::sampleEps(...) where

double eps = epsMin + random.rand() * (epsMax - epsMin);

I have verified that by replacing this with a logarithmic sampling, using the inverse CDF of the reciprocal distribution,

double eps = epsMin * pow(epsMax /epsMin, random.rand());

the sampling works correctly even for the scenario mentioned above. To me it appears that this sampling is more robust and should be generally preferred. Perhaps a flag similar to PhotoPionProduction::setSampleLog(bool b) should be available to enable this behavior or the same flag could be used for both purposes.

Extra Material

Photon Field Density & Sampling

In the following I show results for three different implementations of the CMB in CRPropa to illustrate the problem and show that my suggested solution works correctly. The photon field densities of the three fields, accessed via .getMinimumPhotonEnergy(0) for each field are shown in the figure below. They are in good agreement. Also indicated are 100 random samples of the photon energies for the tabulated CMB field with large epsMax (green dotted field; field 3) for three different implementations. It is clear that the linear sampling currently implemented in CRPropa fails to properly sample the entire energy range and therefore fails to "find any photons" as the original error I received suggest. If logarithmic sampling is used, either via scipy.stats.reciproval.rvs() or by manually implementing the inverse CDF of the reciprocal distribution, the entire range is sampled and the interval with non-zero photon density is found (note that the latter two implementations are not exactly identical in the plot because I am using separate random numbers for each approach). eps_sampling

A Simple Propagation Exercise

The following plot shows the fraction of surviving cosmic-ray protons after propagating them for 10 Mpc in the default and tabulated CMB fields respectively with only photopion production enabled. The results for the tabulated CMB field are not in agreement with the other implementations if the vanilla CRPropa version (with linear sampling) is used (essentially no interaction; finishes in seconds while the other runs take ~5min). It can be brought into excellent agreement with the other approaches if one switches to the logarithmic sampling of photon energies (modifying the source code and reinstalling CRPropa). survival_fraction-1 From the above survival fraction one can estimate the average photopion interaction length for protons with the CMB. This is illustrated in the plot below. The results for all scenarios but the field with high epsMax and linear sampling are in good agreement with each other and literature sources such as the book by Dermer&Menon 2009 (Fig. 9.3 dash-dotted line). Note that points in the 1e3-1e4 Mpc range are due to some simple numerical approximations in the plotting process and should be at infinity. They occur because the number of particles in in a specific energy bin is lower than what would be expected for an ideal powerlaw distribution in ~50% of cases. This is not related to the problem presented here and should be ignored. interaction_length-1

ehlertdo avatar Mar 01 '24 16:03 ehlertdo