{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial: fitting a FSRQ broad-band SED using angpy and sherpa\n", "\n", "In order to perform a fit of the broad-band SED of a jetted AGN, `agnpy` includes a `sherpa` wrapper.\n", "The wrapper defines a custom [model.RegriddableModel1D](https://sherpa.readthedocs.io/en/latest/model_classes/api/sherpa.models.model.RegriddableModel1D.html), representing the emission due to a combination of radiative processes. The model can be used to fit flux points.\n", "\n", "Several combination of radiative processes can be considered to model the broad-band emission of jetted AGN. For simplicity, we provide wrappers for the two scenarios most-commonly considered:\n", "\n", " * `SycnhrotronSelfComptonModel`, representing the sum of synchrotron and synchrotron self Compton (SSC) radiation. This scenario is commonly considered to model BL Lac sources;\n", "\n", " * `ExternalComptonModel`, representing the sum of synchrotron and synchrotron self Compton radiation along with an external Compton (EC) component. EC scattering can be computed considering a list of target photon fields. This scenario is commonly considered to model flat spectrum radio quasars (FSRQs).\n", "\n", "In this tutorial, we will show how to use the `ExternalComptonModel` to fit the broad-band SED of PKS1510-089, measured during its gamma-ray flaring activity in 2015 [(Ahnen et al. 2017)](https://ui.adsabs.harvard.edu/abs/2017A%26A...603A..29A/abstract). We select the MWL SED corresponding to the period identified in the paper as \"Period B\" (MJD 57164-57166).\n", "\n", "[sherpa](https://sherpa.readthedocs.io/en/latest/index.html) is required to run this notebook." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import numpy, astropy and matplotlib for basic functionalities\n", "import numpy as np\n", "import astropy.units as u\n", "from astropy.constants import c, G, M_sun\n", "import matplotlib.pyplot as plt\n", "import pkg_resources\n", "\n", "# import agnpy classes\n", "from agnpy.spectra import LogParabola\n", "from agnpy.fit import ExternalComptonModel, load_sherpa_flux_points\n", "from agnpy.utils.plot import load_mpl_rc, sed_y_label\n", "\n", "load_mpl_rc()\n", "\n", "# import sherpa classes\n", "from sherpa.fit import Fit\n", "from sherpa.stats import Chi2\n", "from sherpa.optmethods import LevMar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `sherpa` wrapper of `agnpy`\n", "\n", "\n", "The `ExternalComptonModel` wraps the `agnpy` functions to compute synchrotron, SSC, and EC radiation. It returns a `model.RegriddableModel1D`. To initialise the model, the electron distribution and a list of targets for EC has to be specified, the remaining parameters (the ones of the emission region and of the line / thermal emitters) will be initialised automatically and can be modified at a later stage.\n", "\n", "The `ExternalComptonModel` class provides both the `sherpa` and `gammapy` wrappers. You should specify, through the `backend` argument, which package you want to use.\n", "\n", "In this case we choose to consider only EC on the DT radiation." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# electron energy distribution\n", "n_e = LogParabola(\n", " k=1 * u.Unit(\"cm-3\"), p=2.0, q=0.2, gamma_0=1e2, gamma_min=1, gamma_max=3e4\n", ")\n", "\n", "# initialise the sherpa model, consider only EC on the DT radiation\n", "ec_model = ExternalComptonModel(n_e, [\"dt\"], ssa=True, backend=\"sherpa\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us set appropriate parameters for the emission region. The size of the blob, $R_{\\rm b}$, is set by the variability timescale, $t_{\\rm var}$, via\n", "\n", "\\begin{equation}\n", "R_{\\rm b} = \\frac{c \\delta_{\\rm D} t_{\\rm var}}{1 + z},\n", "\\end{equation}\n", "\n", "where $c$ is the speed of light, $\\delta_{\\rm D}$ the Doppler factor, and $z$ the redshift.\n", "\n", "The parameters of the accretion disk and the DT are taken from [(Ahnen et al. 2017)](https://ui.adsabs.harvard.edu/abs/2017A%26A...603A..29A/abstract)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "z = 0.361\n", "\n", "# - blob parameters\n", "Gamma = 20\n", "delta_D = 25\n", "Beta = np.sqrt(1 - 1 / np.power(Gamma, 2)) # jet relativistic speed\n", "mu_s = (1 - 1 / (Gamma * delta_D)) / Beta # viewing angle\n", "B = 0.35 * u.G\n", "# size and location of the emission region\n", "t_var = 0.5 * u.d\n", "r = 6e17 * u.cm\n", "\n", "# - disk parameters\n", "L_disk = 6.7e45 * u.Unit(\"erg s-1\") # disk luminosity\n", "M_BH = 5.71 * 1e7 * M_sun\n", "eta = 1 / 12\n", "m_dot = (L_disk / (eta * c ** 2)).to(\"g s-1\")\n", "R_g = ((G * M_BH) / c ** 2).to(\"cm\")\n", "R_in = 6 * R_g\n", "R_out = 3e4 * R_g\n", "\n", "# - DT parameters\n", "xi_dt = 0.6\n", "T_dt = 2e3 * u.K\n", "# Ghisellini and Tavecchio's Formula\n", "# relating the DT radius to the disk luminosity (check the reference)\n", "R_dt = 2.5 * 1e18 * np.sqrt(L_disk.to_value(\"erg s-1\") / 1e45) * u.cm" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# emission regions parameters\n", "ec_model.z = z\n", "ec_model.delta_D = delta_D\n", "ec_model.log10_B = np.log10(0.35)\n", "ec_model.mu_s = mu_s\n", "ec_model.t_var = t_var.to_value(\"s\")\n", "ec_model.t_var.freeze()\n", "ec_model.log10_r = np.log10(r.to_value(\"cm\"))\n", "\n", "# we freeze the reference energy of the log parabola\n", "ec_model.log10_gamma_0.freeze()\n", "\n", "# target parameters\n", "# - disk\n", "ec_model.log10_L_disk = np.log10(L_disk.to_value(\"erg s-1\"))\n", "ec_model.M_BH = M_BH.to_value(\"g\")\n", "ec_model.m_dot = m_dot.to_value(\"g s-1\")\n", "ec_model.R_in = R_in.to_value(\"cm\")\n", "ec_model.R_out = R_out.to_value(\"cm\")\n", "# - DT\n", "ec_model.xi_dt = xi_dt\n", "ec_model.T_dt = T_dt.to_value(\"K\")\n", "ec_model.R_dt = R_dt.to_value(\"cm\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| Component | Parameter | Thawed | Value | Min | Max | Units |
|---|---|---|---|---|---|---|
| ec | log10_k | 0.0 | -10.0 | 10.0 | ||
| p | 2.0 | 1.0 | 5.0 | |||
| q | 0.2 | 0.001 | 1.0 | |||
| log10_gamma_0 | 2.0 | 2.0 | 6.0 | |||
| log10_gamma_min | 0.0 | 0.0 | 4.0 | |||
| log10_gamma_max | 4.477121254719663 | 4.0 | 8.0 | |||
| z | 0.361 | 0.001 | 10.0 | |||
| delta_D | 25.0 | 1.0 | 100.0 | |||
| log10_B | -0.4559319556497244 | -4.0 | 2.0 | |||
| t_var | 43200.0 | 10.0 | 31415926.535897933 | s | ||
| mu_s | 0.9992498439462307 | 0.0 | 1.0 | |||
| log10_r | 17.778151250383644 | 16.0 | 22.0 | |||
| log10_L_disk | 45.82607480270082 | 42.0 | 48.0 | |||
| M_BH | 1.135382036168587e+41 | 1e+32 | 1e+45 | g | ||
| m_dot | 8.945706450671092e+25 | 1e+24 | 1e+30 | g s-1 | ||
| R_in | 50589173803597.266 | 1000000000000.0 | 1e+16 | cm | ||
| R_out | 2.5294586901798634e+17 | 1000000000000.0 | 1e+19 | cm | ||
| xi_dt | 0.6 | 0.0 | 1.0 | |||
| T_dt | 2000.0 | 100.0 | 10000.0 | K | ||
| R_dt | 6.471089552772392e+18 | 1e+17 | 1e+20 | cm |