예제 #1
0
    def test_synchrotron(self):
        import naima

        particle_distribution = naima.models.LogParabola(
            amplitude=2e33 / u.eV, e_0=10 * u.TeV, alpha=1.3, beta=0.5
        )
        radiative_model = naima.radiative.Synchrotron(particle_distribution, B=2 * u.G)

        model = NaimaSpectralModel(radiative_model)
        for p in model.parameters:
            assert p._type == "spectral"

        val_at_2TeV = 1.0565840392550432e-24 * u.Unit("cm-2 s-1 TeV-1")
        integral_1_10TeV = 4.449186e-13 * u.Unit("cm-2 s-1")
        eflux_1_10TeV = 4.594121e-13 * u.Unit("TeV cm-2 s-1")

        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV)
        assert_quantity_allclose(
            model.integral(energy_min=self.energy_min, energy_max=self.energy_max),
            integral_1_10TeV,
            rtol=1e-5,
        )
        assert_quantity_allclose(
            model.energy_flux(energy_min=self.energy_min, energy_max=self.energy_max),
            eflux_1_10TeV,
            rtol=1e-5,
        )
        val = model(self.e_array)
        assert val.shape == self.e_array.shape

        model.B.value = 3  # update B
        val_at_2TeV = 5.1985064062296e-16 * u.Unit("cm-2 s-1 TeV-1")
        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV)
예제 #2
0
    def test_ic(self):
        import naima

        particle_distribution = naima.models.ExponentialCutoffBrokenPowerLaw(
            amplitude=2e33 / u.eV,
            e_0=10 * u.TeV,
            alpha_1=2.5,
            alpha_2=2.7,
            e_break=900 * u.GeV,
            e_cutoff=10 * u.TeV,
        )
        radiative_model = naima.radiative.InverseCompton(
            particle_distribution, seed_photon_fields=["CMB"]
        )

        model = NaimaSpectralModel(radiative_model)

        val_at_2TeV = 4.347836316893546e-12 * u.Unit("cm-2 s-1 TeV-1")
        integral_1_10TeV = 1.595813e-11 * u.Unit("cm-2 s-1")
        eflux_1_10TeV = 2.851283e-11 * u.Unit("TeV cm-2 s-1")

        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV)
        assert_quantity_allclose(
            model.integral(emin=self.emin, emax=self.emax), integral_1_10TeV, rtol=1e-5
        )
        assert_quantity_allclose(
            model.energy_flux(emin=self.emin, emax=self.emax), eflux_1_10TeV, rtol=1e-5
        )
        val = model(self.e_array)
        assert val.shape == self.e_array.shape
예제 #3
0
    def test_pion_decay(self):
        import naima

        particle_distribution = naima.models.PowerLaw(
            amplitude=2e33 / u.eV, e_0=10 * u.TeV, alpha=2.5
        )
        radiative_model = naima.radiative.PionDecay(
            particle_distribution, nh=1 * u.cm ** -3
        )
        model = NaimaSpectralModel(radiative_model)
        for p in model.parameters:
            assert p._type == "spectral"

        val_at_2TeV = 9.725347355450884e-14 * u.Unit("cm-2 s-1 TeV-1")
        integral_1_10TeV = 3.530537143620737e-13 * u.Unit("cm-2 s-1")
        eflux_1_10TeV = 7.643559573105779e-13 * u.Unit("TeV cm-2 s-1")

        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV)
        assert_quantity_allclose(
            model.integral(energy_min=self.energy_min, energy_max=self.energy_max),
            integral_1_10TeV,
        )
        assert_quantity_allclose(
            model.energy_flux(energy_min=self.energy_min, energy_max=self.energy_max),
            eflux_1_10TeV,
        )
        val = model(self.e_array)
        assert val.shape == self.e_array.shape

        model.amplitude.error = 0.1 * model.amplitude.value

        out = model.evaluate_error(1 * u.TeV)
        assert_allclose(out.data, [5.266068e-13, 5.266068e-14], rtol=1e-3)
예제 #4
0
    def test_pion_decay(self):
        import naima

        particle_distribution = naima.models.PowerLaw(amplitude=2e33 / u.eV,
                                                      e_0=10 * u.TeV,
                                                      alpha=2.5)
        radiative_model = naima.radiative.PionDecay(particle_distribution,
                                                    nh=1 * u.cm**-3)
        model = NaimaSpectralModel(radiative_model)

        val_at_2TeV = 9.725347355450884e-14 * u.Unit("cm-2 s-1 TeV-1")
        integral_1_10TeV = 3.530537143620737e-13 * u.Unit("cm-2 s-1")
        eflux_1_10TeV = 7.643559573105779e-13 * u.Unit("TeV cm-2 s-1")

        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV)
        assert_quantity_allclose(
            model.integral(emin=self.emin, emax=self.emax), integral_1_10TeV)
        assert_quantity_allclose(
            model.energy_flux(emin=self.emin, emax=self.emax), eflux_1_10TeV)
        val = model(self.e_array)
        assert val.shape == self.e_array.shape
예제 #5
0
    def test_synchrotron(self):
        import naima

        particle_distribution = naima.models.LogParabola(amplitude=2e33 / u.eV,
                                                         e_0=10 * u.TeV,
                                                         alpha=1.3,
                                                         beta=0.5)
        radiative_model = naima.radiative.Synchrotron(particle_distribution,
                                                      B=2 * u.G)

        model = NaimaSpectralModel(radiative_model)

        val_at_2TeV = 1.0565840392550432e-24 * u.Unit("cm-2 s-1 TeV-1")
        integral_1_10TeV = 4.4491861907713736e-13 * u.Unit("cm-2 s-1")
        eflux_1_10TeV = 4.594120986691428e-13 * u.Unit("TeV cm-2 s-1")

        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV)
        assert_quantity_allclose(
            model.integral(emin=self.emin, emax=self.emax), integral_1_10TeV)
        assert_quantity_allclose(
            model.energy_flux(emin=self.emin, emax=self.emax), eflux_1_10TeV)
        val = model(self.e_array)
        assert val.shape == self.e_array.shape
예제 #6
0
    def test_bad_init(self):
        import naima

        particle_distribution = naima.models.PowerLaw(amplitude=2e33 / u.eV,
                                                      e_0=10 * u.TeV,
                                                      alpha=2.5)
        radiative_model = naima.radiative.PionDecay(particle_distribution,
                                                    nh=1 * u.cm**-3)
        model = NaimaSpectralModel(radiative_model)

        with pytest.raises(NotImplementedError):
            NaimaSpectralModel.from_dict(model.to_dict())
        with pytest.raises(NotImplementedError):
            NaimaSpectralModel.from_parameters(model.parameters)
예제 #7
0
def test_flux_estimator_naima_model():
    import naima
    ECPL = naima.models.ExponentialCutoffPowerLaw(1e36 * u.Unit("1/eV"),
                                                  1 * u.TeV, 2.1, 13 * u.TeV)
    IC = naima.models.InverseCompton(ECPL, seed_photon_fields=["CMB"])
    naima_model = NaimaSpectralModel(IC)

    model = SkyModel(spectral_model=naima_model, name="test")

    estimator = FluxEstimator(source="test",
                              selection_optional=[],
                              reoptimize=True)

    scale_model = estimator.get_scale_model(Models([model]))

    assert_allclose(scale_model.norm.min, np.nan)
    assert_allclose(scale_model.norm.max, np.nan)
예제 #8
0
    def test_ssc(self):
        import naima

        ECBPL = naima.models.ExponentialCutoffBrokenPowerLaw(
            amplitude=3.699e36 / u.eV,
            e_0=1 * u.TeV,
            e_break=0.265 * u.TeV,
            alpha_1=1.5,
            alpha_2=3.233,
            e_cutoff=1863 * u.TeV,
            beta=2.0,
        )

        radiative_model = naima.radiative.InverseCompton(
            ECBPL,
            seed_photon_fields=[
                "CMB",
                ["FIR", 70 * u.K, 0.5 * u.eV / u.cm**3],
                ["NIR", 5000 * u.K, 1 * u.eV / u.cm**3],
            ],
            Eemax=50 * u.PeV,
            Eemin=0.1 * u.GeV,
        )
        B = 125 * u.uG
        radius = 2.1 * u.pc
        nested_models = {"SSC": {"B": B, "radius": radius}}
        model = NaimaSpectralModel(radiative_model,
                                   nested_models=nested_models)
        assert_quantity_allclose(model.B.quantity, B)
        assert_quantity_allclose(model.radius.quantity, radius)
        val_at_2TeV = 1.6703761561806372e-11 * u.Unit("cm-2 s-1 TeV-1")
        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV, rtol=1e-5)

        model.parameters["B"].value = 100
        val_at_2TeV = 1.441331153167876e-11 * u.Unit("cm-2 s-1 TeV-1")
        value = model(self.energy)
        assert_quantity_allclose(value, val_at_2TeV, rtol=1e-5)
import naima
from gammapy.modeling.models import NaimaSpectralModel
import astropy.units as u
import matplotlib.pyplot as plt

particle_distribution = naima.models.ExponentialCutoffPowerLaw(1e30 / u.eV, 10 * u.TeV, 3.0, 30 * u.TeV)
radiative_model = naima.radiative.InverseCompton(
    particle_distribution,
    seed_photon_fields=[
        "CMB",
        ["FIR", 26.5 * u.K, 0.415 * u.eV / u.cm ** 3],
    ],
    Eemin=100 * u.GeV,
)

model = NaimaSpectralModel(radiative_model, distance=1.5 * u.kpc)

opts = {
    "energy_range" : [10 * u.GeV, 80 * u.TeV],
    "energy_power" : 2,
    "flux_unit" : "erg-1 cm-2 s-1",
}

# Plot the total inverse Compton emission
model.plot(label='IC (total)', **opts)

# Plot the separate contributions from each seed photon field
for seed, ls in zip(['CMB','FIR'], ['-','--']):
    model = NaimaSpectralModel(radiative_model, seed=seed, distance=1.5 * u.kpc)
    model.plot(label="IC ({})".format(seed), ls=ls, color="gray", **opts)
예제 #10
0
# electron distribution with an `InverseCompton` radiative model, in the presence of multiple seed photon fields.

from astropy import units as u
import matplotlib.pyplot as plt
import naima
from gammapy.modeling.models import Models, NaimaSpectralModel, SkyModel

particle_distribution = naima.models.ExponentialCutoffPowerLaw(
    1e30 / u.eV, 10 * u.TeV, 3.0, 30 * u.TeV)
radiative_model = naima.radiative.InverseCompton(
    particle_distribution,
    seed_photon_fields=["CMB", ["FIR", 26.5 * u.K, 0.415 * u.eV / u.cm**3]],
    Eemin=100 * u.GeV,
)

model = NaimaSpectralModel(radiative_model, distance=1.5 * u.kpc)

opts = {
    "energy_range": [10 * u.GeV, 80 * u.TeV],
    "energy_power": 2,
    "flux_unit": "erg-1 cm-2 s-1",
}

# Plot the total inverse Compton emission
model.plot(label="IC (total)", **opts)

# Plot the separate contributions from each seed photon field
for seed, ls in zip(["CMB", "FIR"], ["-", "--"]):
    model = NaimaSpectralModel(radiative_model,
                               seed=seed,
                               distance=1.5 * u.kpc)