zblz / naima

Derivation of non-thermal particle distributions through MCMC spectral fitting
http://naima.readthedocs.io
BSD 3-Clause "New" or "Revised" License
45 stars 53 forks source link

ValueError: need at least one array to concatenate #225

Open haoxianglin opened 2 years ago

haoxianglin commented 2 years ago

Hi,

I got this error message whenever the values of Eemin and Eemax of naima.models.Synchrotron is too close (encountered in some extreme parameter choices in GRB study). An example code to reproduce this error is as below:

from naima.models import ExponentialCutoffBrokenPowerLaw, Synchrotron
import astropy.units as u
from astropy.constants import c, m_e

mec2 = (m_e * c**2).to(u.MeV)
Eemin = (1e6 * mec2).to(u.eV)
Eebrk = (1.005e6 * mec2).to(u.eV)
Eemax = (1.01e6 * mec2).to(u.eV)

electrons = ExponentialCutoffBrokenPowerLaw(
    amplitude=1 * 1/u.eV,
    e_0=Eemin,
    e_break=Eebrk,
    alpha_1=2,
    alpha_2=3,
    e_cutoff=Eemax,
    beta=2,
)

SYN = Synchrotron(
    electrons,
    B=0.1 * u.G,
    Eemax=Eemax,
    Eemin=Eemin,
    nEed=100,
)

SYN.flux(1*u.keV, distance=0 * u.cm)

I believe it arises from the zero length of the Lorentz factor sequence in the source code:

log10gmin = np.log10(self.Eemin / mec2).value
log10gmax = np.log10(self.Eemax / mec2).value
return np.logspace(
            log10gmin, log10gmax, int(self.nEed * (log10gmax - log10gmin))
        )

For example, if Eemax = 1.01*Eemin, and by default nEed = 100, then self.nEed * (log10gmax - log10gmin) = 100*log10(1.01) = 0.432, and int(0.432) = 0, which creates the error.

While increasing nEed solves the problem, I wonder if it is better to avoid this by restricting the length of sequence to be at least 2, e.g.

log10gmin = np.log10(self.Eemin / mec2).value
log10gmax = np.log10(self.Eemax / mec2).value
return np.logspace(
            log10gmin, log10gmax, max(2, int(self.nEed * (log10gmax - log10gmin)))
        )