-
Notifications
You must be signed in to change notification settings - Fork 2
/
figure_6_PKS1510-089_fit_gammapy.py
453 lines (410 loc) · 12.2 KB
/
figure_6_PKS1510-089_fit_gammapy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# general modules
import logging
import pkg_resources
from pathlib import Path
import numpy as np
import astropy.units as u
from astropy.constants import k_B, m_e, c, G, M_sun
from astropy.coordinates import Distance
import matplotlib.pyplot as plt
from utils import time_function_call
# agnpy modules
from agnpy.spectra import BrokenPowerLaw
from agnpy.emission_regions import Blob
from agnpy.synchrotron import Synchrotron
from agnpy.compton import SynchrotronSelfCompton, ExternalCompton
from agnpy.targets import SSDisk, RingDustTorus
from agnpy.utils.plot import sed_x_label, sed_y_label
# gammapy modules
from gammapy.modeling.models import (
SpectralModel,
Parameter,
SPECTRAL_MODEL_REGISTRY,
SkyModel,
)
from gammapy.estimators import FluxPoints
from gammapy.datasets import FluxPointsDataset
from gammapy.modeling import Fit
# constants
mec2 = m_e.to("erg", equivalencies=u.mass_energy())
gamma_size = 400
gamma_to_integrate = np.logspace(0, 7, gamma_size)
class AgnpyEC(SpectralModel):
"""Wrapper of agnpy's non synchrotron, SSC and EC classes. The flux model
accounts for the Disk and DT's thermal SEDs.
A broken power law is assumed for the electron spectrum.
To limit the span of the parameters space, we fit the log10 of the parameters
whose range is expected to cover several orders of magnitudes (normalisation,
gammas, size and magnetic field of the blob).
"""
tag = "EC"
log10_k_e = Parameter("log10_k_e", -5, min=-20, max=2)
p1 = Parameter("p1", 2.1, min=1.0, max=5.0)
p2 = Parameter("p2", 3.1, min=1.0, max=5.0)
log10_gamma_b = Parameter("log10_gamma_b", 3, min=1, max=6)
log10_gamma_min = Parameter("log10_gamma_min", 1, min=0, max=4)
log10_gamma_max = Parameter("log10_gamma_max", 5, min=3, max=8)
# source general parameters
z = Parameter("z", 0.1, min=0.01, max=1)
d_L = Parameter("d_L", "1e27 cm", min=1e25, max=1e33)
# emission region parameters
delta_D = Parameter("delta_D", 10, min=1, max=40)
log10_B = Parameter("log10_B", 0.0, min=-3.0, max=1.0)
t_var = Parameter("t_var", "600 s", min=10, max=np.pi * 1e7)
mu_s = Parameter("mu_s", 0.9, min=0.0, max=1.0)
log10_r = Parameter("log10_r", 17.0, min=16.0, max=20.0)
# disk parameters
log10_L_disk = Parameter("log10_L_disk", 45.0, min=42.0, max=48.0)
log10_M_BH = Parameter("log10_M_BH", 42, min=32, max=45)
m_dot = Parameter("m_dot", "1e26 g s-1", min=1e24, max=1e30)
R_in = Parameter("R_in", "1e14 cm", min=1e12, max=1e16)
R_out = Parameter("R_out", "1e17 cm", min=1e12, max=1e19)
# DT parameters
xi_dt = Parameter("xi_dt", 0.6, min=0.0, max=1.0)
T_dt = Parameter("T_dt", "1e3 K", min=1e2, max=1e4)
R_dt = Parameter("R_dt", "2.5e18 cm", min=1.0e17, max=1.0e19)
@staticmethod
def evaluate(
energy,
log10_k_e,
p1,
p2,
log10_gamma_b,
log10_gamma_min,
log10_gamma_max,
z,
d_L,
delta_D,
log10_B,
t_var,
mu_s,
log10_r,
log10_L_disk,
log10_M_BH,
m_dot,
R_in,
R_out,
xi_dt,
T_dt,
R_dt,
):
# conversion
k_e = 10 ** log10_k_e * u.Unit("cm-3")
gamma_b = 10 ** log10_gamma_b
gamma_min = 10 ** log10_gamma_min
gamma_max = 10 ** log10_gamma_max
B = 10 ** log10_B * u.G
R_b = (c * t_var * delta_D / (1 + z)).to("cm")
r = 10 ** log10_r * u.cm
L_disk = 10 ** log10_L_disk * u.Unit("erg s-1")
M_BH = 10 ** log10_M_BH * u.Unit("g")
eps_dt = 2.7 * (k_B * T_dt / mec2).to_value("")
nu = energy.to("Hz", equivalencies=u.spectral())
# non-thermal components
sed_synch = Synchrotron.evaluate_sed_flux(
nu,
z,
d_L,
delta_D,
B,
R_b,
BrokenPowerLaw,
k_e,
p1,
p2,
gamma_b,
gamma_min,
gamma_max,
ssa=True,
gamma=gamma_to_integrate,
)
sed_ssc = SynchrotronSelfCompton.evaluate_sed_flux(
nu,
z,
d_L,
delta_D,
B,
R_b,
BrokenPowerLaw,
k_e,
p1,
p2,
gamma_b,
gamma_min,
gamma_max,
ssa=True,
gamma=gamma_to_integrate,
)
sed_ec_dt = ExternalCompton.evaluate_sed_flux_dt(
nu,
z,
d_L,
delta_D,
mu_s,
R_b,
L_disk,
xi_dt,
eps_dt,
R_dt,
r,
BrokenPowerLaw,
k_e,
p1,
p2,
gamma_b,
gamma_min,
gamma_max,
gamma=gamma_to_integrate,
)
# thermal components
sed_bb_disk = SSDisk.evaluate_multi_T_bb_norm_sed(
nu, z, L_disk, M_BH, m_dot, R_in, R_out, d_L
)
sed_bb_dt = RingDustTorus.evaluate_bb_norm_sed(
nu, z, xi_dt * L_disk, T_dt, R_dt, d_L
)
sed = sed_synch + sed_ssc + sed_ec_dt + sed_bb_disk + sed_bb_dt
return (sed / energy ** 2).to("1 / (cm2 eV s)")
SPECTRAL_MODEL_REGISTRY.append(AgnpyEC)
logging.info("reading PKS1510-089 SED from agnpy datas")
sed_path = pkg_resources.resource_filename(
"agnpy", "data/mwl_seds/PKS1510-089_2015b.ecsv"
)
flux_points = FluxPoints.read(sed_path)
# array of systematic errors, will just be summed in quadrature to the statistical error
# we assume
# - 30% on VHE gamma-ray instruments
# - 10% on HE gamma-ray instruments
# - 10% on X-ray instruments
# - 5% on lower-energy instruments
x = flux_points.table["e_ref"]
y = flux_points.table["e2dnde"]
y_err_stat = flux_points.table["e2dnde_errn"]
y_err_syst = np.zeros(len(x))
# define energy ranges
e_vhe = 100 * u.GeV
e_he = 0.1 * u.GeV
e_x_ray_max = 300 * u.keV
e_x_ray_min = 0.3 * u.keV
vhe_gamma = x >= e_vhe
he_gamma = (x >= e_he) * (x < e_vhe)
x_ray = (x >= e_x_ray_min) * (x < e_x_ray_max)
uv_to_radio = x < e_x_ray_min
# declare systematics
y_err_syst[vhe_gamma] = 0.30
y_err_syst[he_gamma] = 0.10
y_err_syst[x_ray] = 0.10
y_err_syst[uv_to_radio] = 0.05
y_err_syst = y * y_err_syst
# sum in quadrature the errors
flux_points.table["e2dnde_err"] = np.sqrt(y_err_stat ** 2 + y_err_syst ** 2)
flux_points = flux_points.to_sed_type("dnde")
# declare a model
agnpy_ec = AgnpyEC()
# initialise parameters
z = 0.361
d_L = Distance(z=z).to("cm")
# - blob
Gamma = 20
delta_D = 25
Beta = np.sqrt(1 - 1 / np.power(Gamma, 2)) # jet relativistic speed
mu_s = (1 - 1 / (Gamma * delta_D)) / Beta # viewing angle
B = 0.35 * u.G
# - disk
L_disk = 6.7e45 * u.Unit("erg s-1") # disk luminosity
M_BH = 5.71 * 1e7 * M_sun
eta = 1 / 12
m_dot = (L_disk / (eta * c ** 2)).to("g s-1")
R_g = ((G * M_BH) / c ** 2).to("cm")
R_in = 6 * R_g
R_out = 10000 * R_g
# - DT
xi_dt = 0.6 # fraction of disk luminosity reprocessed by the DT
T_dt = 1e3 * u.K
R_dt = 6.47 * 1e18 * u.cm
# - size and location of the emission region
t_var = 0.5 * u.d
r = 6e17 * u.cm
# instance of the model wrapping angpy functionalities
# - AGN parameters
# -- distances
agnpy_ec.z.quantity = z
agnpy_ec.z.frozen = True
agnpy_ec.d_L.quantity = d_L.cgs.value
agnpy_ec.d_L.frozen = True
# -- SS disk
agnpy_ec.log10_L_disk.quantity = np.log10(L_disk.to_value("erg s-1"))
agnpy_ec.log10_L_disk.frozen = True
agnpy_ec.log10_M_BH.quantity = np.log10(M_BH.to_value("g"))
agnpy_ec.log10_M_BH.frozen = True
agnpy_ec.m_dot.quantity = m_dot
agnpy_ec.m_dot.frozen = True
agnpy_ec.R_in.quantity = R_in
agnpy_ec.R_in.frozen = True
agnpy_ec.R_out.quantity = R_out
agnpy_ec.R_out.frozen = True
# -- Dust Torus
agnpy_ec.xi_dt.quantity = xi_dt
agnpy_ec.xi_dt.frozen = True
agnpy_ec.T_dt.quantity = T_dt
agnpy_ec.T_dt.frozen = True
agnpy_ec.R_dt.quantity = R_dt
agnpy_ec.R_dt.frozen = True
# - blob parameters
agnpy_ec.delta_D.quantity = delta_D
agnpy_ec.delta_D.frozen = True
agnpy_ec.log10_B.quantity = np.log10(B.to_value("G"))
agnpy_ec.mu_s.quantity = mu_s
agnpy_ec.mu_s.frozen = True
agnpy_ec.t_var.quantity = t_var
agnpy_ec.t_var.frozen = True
agnpy_ec.log10_r.quantity = np.log10(r.to_value("cm"))
agnpy_ec.log10_r.frozen = True
# - EED
agnpy_ec.log10_k_e.quantity = np.log10(0.05)
agnpy_ec.p1.quantity = 1.8
agnpy_ec.p2.quantity = 3.5
agnpy_ec.log10_gamma_b.quantity = np.log10(500)
agnpy_ec.log10_gamma_min.quantity = np.log10(1)
agnpy_ec.log10_gamma_min.frozen = True
agnpy_ec.log10_gamma_max.quantity = np.log10(3e4)
agnpy_ec.log10_gamma_max.frozen = True
# define model
model = SkyModel(name="PKS1510-089_EC", spectral_model=agnpy_ec)
dataset_ec = FluxPointsDataset(model, flux_points)
# do not use frequency point below 1e11 Hz, affected by non-blazar emission
E_min_fit = (1e11 * u.Hz).to("eV", equivalencies=u.spectral())
dataset_ec.mask_fit = dataset_ec.data.energy_ref > E_min_fit
logging.info("performing the fit")
# define the fitter
fitter = Fit([dataset_ec])
results = time_function_call(fitter.run, optimize_opts={"print_level": 1})
print(results)
print(agnpy_ec.parameters.to_table())
logging.info("plot the final model with the individual components")
# blob definition
k_e = 10 ** agnpy_ec.log10_k_e.value * u.Unit("cm-3")
p1 = agnpy_ec.p1.value
p2 = agnpy_ec.p2.value
gamma_b = 10 ** agnpy_ec.log10_gamma_b.value
gamma_min = 10 ** agnpy_ec.log10_gamma_min.value
gamma_max = 10 ** agnpy_ec.log10_gamma_max.value
B = 10 ** agnpy_ec.log10_B.value * u.G
r = 10 ** agnpy_ec.log10_r.value * u.cm
delta_D = agnpy_ec.delta_D.value
R_b = (
c * agnpy_ec.t_var.quantity * agnpy_ec.delta_D.quantity / (1 + agnpy_ec.z.quantity)
).to("cm")
parameters = {
"p1": p1,
"p2": p2,
"gamma_b": gamma_b,
"gamma_min": gamma_min,
"gamma_max": gamma_max,
}
spectrum_dict = {"type": "BrokenPowerLaw", "parameters": parameters}
blob = Blob(
R_b,
z,
delta_D,
Gamma,
B,
k_e,
spectrum_dict,
spectrum_norm_type="differential",
gamma_size=500,
)
print(blob)
print(f"jet power in particles: {blob.P_jet_e:.2e}")
print(f"jet power in B: {blob.P_jet_B:.2e}")
# Disk and DT definition
L_disk = 10 ** agnpy_ec.log10_L_disk.value * u.Unit("erg s-1")
M_BH = 10 ** agnpy_ec.log10_M_BH.value * u.Unit("g")
m_dot = agnpy_ec.m_dot.value * u.Unit("g s-1")
eta = (L_disk / (m_dot * c ** 2)).to_value("")
R_in = agnpy_ec.R_in.value * u.cm
R_out = agnpy_ec.R_out.value * u.cm
disk = SSDisk(M_BH, L_disk, eta, R_in, R_out)
dt = RingDustTorus(L_disk, xi_dt, T_dt, R_dt=R_dt)
# radiative processes
synch = Synchrotron(blob, ssa=True)
ssc = SynchrotronSelfCompton(blob, synch)
ec_dt = ExternalCompton(blob, dt, r)
# SEDs
nu = np.logspace(9, 27, 200) * u.Hz
synch_sed = synch.sed_flux(nu)
ssc_sed = ssc.sed_flux(nu)
ec_dt_sed = ec_dt.sed_flux(nu)
disk_bb_sed = disk.sed_flux(nu, z)
dt_bb_sed = dt.sed_flux(nu, z)
total_sed = synch_sed + ssc_sed + ec_dt_sed + disk_bb_sed + dt_bb_sed
# make figure 6
fig, ax = plt.subplots()
ax.loglog(
nu / (1 + z), total_sed, ls="-", lw=2.1, color="crimson", label="agnpy, total"
)
ax.loglog(
nu / (1 + z),
synch_sed,
ls="--",
lw=1.3,
color="goldenrod",
label="agnpy, synchrotron",
)
ax.loglog(
nu / (1 + z), ssc_sed, ls="--", lw=1.3, color="dodgerblue", label="agnpy, SSC"
)
ax.loglog(
nu / (1 + z),
ec_dt_sed,
ls="--",
lw=1.3,
color="lightseagreen",
label="agnpy, EC on DT",
)
ax.loglog(
nu / (1 + z),
disk_bb_sed,
ls="-.",
lw=1.3,
color="dimgray",
label="agnpy, disc black body",
)
ax.loglog(
nu / (1 + z),
dt_bb_sed,
ls=":",
lw=1.3,
color="dimgray",
label="agnpy, DT black body",
)
# systematic errors in gray
ax.errorbar(
x.to("Hz", equivalencies=u.spectral()).value,
y,
yerr=y_err_syst,
marker=",",
ls="",
color="gray",
label="",
)
# statistical errors in black
ax.errorbar(
x.to("Hz", equivalencies=u.spectral()).value,
y,
yerr=y_err_stat,
marker=".",
ls="",
color="k",
label="PKS 1510-089,\n Ahnen et al. (2017), period B",
)
ax.set_xlabel(sed_x_label)
ax.set_ylabel(sed_y_label)
ax.set_xlim([1e9, 1e29])
ax.set_ylim([10 ** (-13.5), 10 ** (-7.5)])
ax.legend(
loc="upper center", fontsize=11, ncol=2,
)
Path("figures").mkdir(exist_ok=True)
fig.savefig("figures/figure_6_gammapy_fit.png")
fig.savefig("figures/figure_6_gammapy_fit.pdf")