forked from ManuelBuenAbad/snr_ghosts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
astro.py
1350 lines (1024 loc) · 44.6 KB
/
astro.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This is a module for the basics of the astrophysical expressions
"""
from __future__ import division
import numpy as np
from numpy import pi, sqrt, exp, power, log, log10
from scipy.integrate import trapz
from scipy.special import erf, lambertw
from inspect import getargspec
import os
# related to the map handling
import healpy as hp
from astropy_healpix import HEALPix
from astropy.coordinates import SkyCoord
# from astropy.io import fits
from astropy.coordinates import Galactic
# from astropy import units as u
import tools as tl
import constants as ct
import particle as pt
import ska as sk
# Dark matter and galactic functions:
def rho_NFW(r, rs, rhos):
"""
The density profile [GeV/cm**3] of an NFW halo.
Parameters
----------
r : the distance from the center [kpc]
rs : the NFW r_s parameter [kpc]
rhos : the NFW rho_s parameter [GeV/cm**3]
"""
res = rhos/(r/rs)/(1.+r/rs)**2
return res
def rho_MW(r, DM_profile="NFW"):
"""
The density [GeV/cm**3] of the Milky Way.
Parameters
----------
x : distance from the center [kpc]
"""
if DM_profile == "NFW":
rs = ct._rs_of_MW_
rho_at_sun = ct._rho_local_DM_
rsun = ct._Sun_to_gal_center_
rhos = rho_at_sun * (rsun/rs) * (1. + rsun/rs)**2
res = rho_NFW(r, rs, rhos)
try:
res = res.astype(float)
except:
pass
elif DM_profile == "Burkert":
x = r / ct._r_H_
res = ct._rho_H_ / (1. + x) / (1. + x**2)
else:
raise Exception(
"The DM profile can only be NFW or Burkert. Please check the input.")
return res
def theta_gal_ctr(l, b, output_radians=True):
"""
Computes the theta angle [radians/degrees] that a point of galactic coordinates (l, b) makes with the galactic center.
Parameters
----------
l : galactic longitude [deg]
b : galactic latitude [deg]
output_radians : whether the result is given in radians (default: True)
"""
theta = np.arccos(np.cos(l*pi/180.)*np.cos(b*pi/180.))
if output_radians:
return theta
else:
return theta*180./pi
def r_to_gal(x, th, r0=ct._Sun_to_gal_center_):
"""
The radius [kpc] of a point to the galactic center.
Parameters
----------
x : distance of a point on the line of sight [kpc]
th : angle between galaxy center and light source [radian]
r0 : distance from the Sun to the galaxy center [kpc] (default: 8.1)
"""
r2 = x**2 + r0**2 - 2.*x*r0*np.cos(th)
r = sqrt(r2)
try:
r = r.astype(float)
except:
pass
return r
#########################################
# Light-related functions:
def flux_density_to_psd(nu, Snu, Omega):
"""
Convert flux density S_nu to phase space density f. Returns Energy [eV] and f = S_nu/Omega * 2*pi^2/E^3.
Parameters
----------
nu : array of the frequency [GHz]
Snu : array of S_nu [Jy]
Omega : the solid angle the source subtends [sr]
"""
Snu_in_eV3 = Snu * ct._Jy_over_eV3_
E = ct._h_ * nu * ct._GHz_over_eV_
res = Snu_in_eV3 / Omega * 2. * np.pi**2 / E**3
return (E, res)
def psd_to_flux_density(E, f, Omega):
"""
Converting phase space density f to flux density S_nu. Returns frequency nu [GHz] and spectral irradiance S_nu = E^3/2./pi^2*f*Omega [Jy].
Parameters
----------
E : array of energy [eV]
f : p.s.d
Omega : the angle the source subtends [sr]
"""
Snu = E**3 / 2. / np.pi**2 * f * Omega / ct._Jy_over_eV3_
nu = E / ct._GHz_over_eV_ / ct._h_
return (nu, Snu)
def flux(nu, Snu):
"""
Integrate S_nu w.r.t. nu to get S [eV^4]. Returns the flux (irradiance) [eV^4].
Parameters
----------
nu : the range of the frequency to be integrated over [GHz]
Snu : the flux density [Jy]
"""
# fix unit
nu_in_eV = nu * ct._GHz_over_eV_
Snu_in_eV3 = Snu * ct._Jy_over_eV3_
res = trapz(Snu_in_eV3, nu_in_eV)
return res
def irrad(distance, lumin):
"""
Returns the flux density (spectral irradiance) [Jy] of a source.
Parameters
----------
distance : distance to source [kpc]
lumin : spectral luminosity of source [erg * s^-1 * Hz^-1]
"""
area = 4.*pi * \
(distance*ct._kpc_over_cm_)**2. # [cm^2] sphere covering the source
irrad = (lumin/area) / ct._Jy_over_cgs_irrad_ # [Jy]
return irrad
def lumin(distance, irrad):
"""
Returns the spectral luminosity [erg * s^-1 * Hz^-1] of a source.
Parameters
----------
distance : distance to source [kpc]
irrad : flux density (spectral irradiance) of source [Jy]
"""
area = 4.*pi * \
(distance*ct._kpc_over_cm_)**2. # [cm^2] sphere covering the source
lumin = (irrad*ct._Jy_over_cgs_irrad_)*area # [erg * s^-1 * Hz^-1]
return lumin
def S_cygA(nu):
"""
The flux density [Jy] S_nu of Cygnus A.
Parameters
----------
nu : frequency, can be scalar or array [GHz]
"""
log_res = []
nu_lst, flg_scalar = tl.treat_as_arr(nu) # scalar --> array trick
nu_lst *= 1.e3 # converting to MHz
for nui in nu_lst:
if nui < 2000.:
a = 4.695
b = 0.085
c = -0.178
else:
a = 7.161
b = -1.244
c = 0.
log_res.append(a + b * np.log10(nui) + c * np.log10(nui)**2)
log_res = np.asarray(log_res)
res = 10.**(log_res)
if flg_scalar:
res = np.squeeze(res)
return res
#########################################
# Source model functions:
def FreeErrorMssg(model):
"""
A custom error message for different free-expansion evolutions.
"""
if model in pars_early.keys():
return "For model=={}, we need the arguments: {}.".format(model, ['t']+pars_early[model])
else:
return "Currently, this function can only support model=='eff' (Bietenholz et al., arXiv:2011.11737) or model=='thy'/'thy-alt' (Weiler et al., 1986ApJ...301..790W). model={} is not supported.".format(str(model))
def AdiabaticErrorMssg():
"""
A custom error message for the adiabatic evolution.
"""
return "L_adiab needs the arguments: ['t_ref', 'L_ref', 'gamma']."
# adiabatic and spectral indices
def gamma_from_alpha(alpha):
"""
Adiabatic index from spectral index, Sedov-Taylor expansion.
Parameters
----------
alpha : spectral index
"""
gamma = (4./5)*(2.*alpha + 1.)
return gamma
def alpha_from_gamma(gamma):
"""
Spectral index from adiabatic index, Sedov-Taylor expansion.
Parameters
----------
gamma : adiabatic index
"""
alpha = ((5./4)*gamma - 1.)/2.
return alpha
def nu_factor(nu, nu_pivot, alpha):
"""
Frequency rescaling factor, depending on the spectral index.
Parameters
----------
nu : frequency [GHz]
nu_pivot : pivot (i.e. reference) frequency [GHz]
alpha : spectral index
"""
return (nu/nu_pivot)**-alpha
# SNR light-curves
def L_eff(t, L_peak, t_peak):
"""
Returns the early evolution (i.e. during free expansion) of the SNR radio spectral luminosity [erg * s^-1 * Hz^-1] as a function of time after explosion [years] and frequency [GHz], according to the effective model from Bietenholz et al., arXiv:2011.11737. NOTE: frequency dependence prefactor, usually (nu/nu_pivot)^-alpha, is factorized out.
Parameters
----------
t : time after explosion [years]
L_peak : peak spectral luminosity [erg * s^-1 * Hz^-1]
t_peak : peak time [days]
"""
t_days = t*365. # [years] -> [days]
return L_peak * exp(-1.5 * (t_peak/t_days - 1.)) * (t_days/t_peak)**-1.5
def L_thy(t, L_norm, beta, K2, delta, tau_factor=1.):
"""
Returns the early evolution (i.e. during free expansion) of the SNR radio spectral luminosity [erg * s^-1 * Hz^-1] as a function of time after explosion [years] and frequency [GHz], according to the theoretical model from Weiler et al., 1986ApJ...301..790W. NOTE: frequency dependence prefactor, usually (nu/nu_pivot)^-alpha, is factorized out.
Parameters
----------
t : time after explosion [years]
L_norm : normalization spectral luminosity [erg * s^-1 * Hz^-1]
beta : post-peak free expansion evolution [defined in the exponent with a minus sign]
K2 : optical depth normalization
delta : optical depth time evolution [defined in the exponent with a minus sign]
tau_factor : frequency-dependent power for opacity tau; in Weiler's it's (nu/5.)**-2.1 (default: 1.)
"""
# In Weiler's, factor = (nu/5)^-alpha, while tau_factor = (nu/5.)**-2.1
t_days = t*365. # [years] -> [days]
tau = tau_factor * K2 * t_days**(-delta)
return L_norm * t_days**(-beta) * exp(-tau)
def L_free(t, model='eff', **kwargs):
"""
Returns the free expansion evolution of the SNR radio spectral luminosity [erg * s^-1 * Hz^-1] as a function of time after explosion [years] and frequency [GHz]. NOTE: frequency dependence prefactor, usually (nu/nu_pivot)^-alpha, is factorized out.
Parameters
----------
t : time after explosion [years]
model : model for the free expansion evolution, either 'eff' or 'thy' (default: 'eff')
"""
# Checking kwargs:
# subset of kwargs that is in pars_early[model]
pars_inter = set(pars_early[model]).intersection(kwargs.keys())
if pars_inter != set(pars_early[model]):
raise KeyError("The kwargs passed do not contain all the arguments for the model requested. For model={} these are: {}".format(
model, pars_early[model]))
# reading off the arguments of the early evolution functions
if model == 'eff':
Learly_pars = getargspec(L_eff)[0]
elif model == 'thy':
Learly_pars = getargspec(L_thy)[0]
# building the kwargs for the free expansion lightcurve
free_kwargs = {}
for par in Learly_pars:
if par in kwargs.keys():
free_kwargs[par] = kwargs[par]
try:
if model == 'eff':
Lf = L_eff(t, **free_kwargs)
elif model == 'thy':
# in principle, L_norm could be absent from free_kwargs; instead there being 'K1' and 'distance'. An old version of this code allowed for that. We have decided to restrict it.
Lf = L_thy(t, **free_kwargs)
except TypeError:
raise TypeError(FreeErrorMssg(model=model))
return Lf
def L_adiab(t, t_ref, L_ref, gamma):
"""
Returns the adiabatic (Sedov-Taylor) evolution of the SNR radio spectral luminosity [erg * s^-1 * Hz^-1] as a function of time after explosion [years] and frequency [GHz]. NOTE: frequency dependence prefactor, usually (nu/nu_pivot)^-alpha, is factorized out.
Parameters
----------
t : time after explosion [years]
t_ref : reference time after explosion [years]
L_ref : spectral luminosity at reference time [erg * s^-1 * Hz^-1]
gamma : adiabatic expansion evolution [defined in the exponent with a minus sign]
"""
return L_ref * (t/t_ref)**(-gamma)
def sandwich_logeqn(Lpk, tpk, L0, t0, gamma, tt):
"""
Returns the equation to minimize in order to find the transition time between the effective free expansion and the adiabatic expansion.
Parameters
----------
Lpk : peak spectral luminosity [erg * s^-1 * Hz^-1]
tpk : peak time [days]
L0 : SNR spectral luminosity today [erg * s^-1 * Hz^-1]
t0 : SNR age [years]
gamma : adiabatic index
tt : free-adiabatic phases transition time [years]
"""
tpk_yr = tpk/365.
eqn = log10(Lpk/L0) + (1.5)*(1. - tpk_yr/tt)*log10(np.e) + \
(gamma-1.5)*log10(tt) + 1.5*log10(tpk_yr) - gamma*log10(t0)
return eqn
def tage_compute(Lpk, tpk, t_trans, L_today, gamma):
"""
Returns the age [years] of a SNR given Lpk, tpk, t_trans, L_today, and gamma.
Parameters
----------
Lpk : peak spectral luminosity [erg * s^-1 * Hz^-1]
tpk : peak time [days]
t_trans : free-adiabatic phases transition time [years]
L_today : SNR spectral luminosity today [erg * s^-1 * Hz^-1]
gamma : adiabatic index
"""
def Lf_fn(tt): return L_eff(tt, Lpk, tpk)
L_trans = Lf_fn(t_trans) # L_trans
t_age = (L_today/L_trans)**(-1/gamma) * t_trans # t_age
return t_age
def L_source(t, model='eff', output_pars=False, **kwargs):
"""
Returns the SNR radio luminosity [erg * s^-1 * Hz^-1] as a function of time after explosion [years] and frequency [GHz]. NOTE: frequency dependence prefactor, usually (nu/nu_pivot)^-alpha, is factorized out.
Parameters
----------
t : time after explosion [years]
model : model for the free expansion evolution, either 'eff' or 'thy' (default: 'eff')
output_pars : whether to output a dictionary with the parameters used
"""
if not 'gamma' in kwargs.keys():
raise KeyError(
"'gamma' is not in kwargs. Please pass 'gamma' as an argument.")
gamma = kwargs['gamma']
# list of lightcurve parameters that are known (i.e. present in kwargs)
known = {}
# list of lightcurve parameters that are to be deduced (i.e. are missing in kwargs)
to_deduce = []
for par in pars_lightcurve[model]:
if par in kwargs.keys():
known[par] = kwargs[par]
else:
to_deduce.append(par)
if (model == 'thy') and ('tau_factor' in kwargs.keys()): # special case
known['tau_factor'] = kwargs['tau_factor']
if len(to_deduce) > 1: # too many missing parameters
raise ValueError(
"to_deduce={} is too large. Please include more parameters in kwargs.".format(to_deduce))
try:
to_deduce = to_deduce[0] # extracting the only parameter to be deduced
# the parameters required in kwargs
required = pars_required[(model, to_deduce)]
if not set(required).issubset(set(known.keys())):
raise ValueError("known={} is too short. It should be a subset of the required parameters, which are {}. Please include more parameters in kwargs.".format(
known.keys(), required))
except IndexError:
to_deduce = None # no parameter to deduce!
if model == 'eff':
if (to_deduce == 'L_today') or (to_deduce == None): # based on peak info, deduce L_today
t_today = known['t_age'] # age [years]
t_trans = known['t_trans'] # transition time [years]
# computing transition luminosity
L_trans = L_free(t_trans, model=model, **known)
adiab_kwargs = {'L_ref': L_trans, 't_ref': t_trans, 'gamma': gamma}
L_today = L_adiab(t_today, **adiab_kwargs)
known.update({'L_today': L_today})
elif to_deduce == 'L_peak': # based on t_peak and today's info, deduce L_peak
t_age = known['t_age'] # age [years]
t_trans = known['t_trans'] # transition time [years]
L_today = known['L_today'] # spectral luminosity [today]
adiab_kwargs = {'L_ref': L_today, 't_ref': t_age, 'gamma': gamma}
eff_kwargs = {}
eff_pars = getargspec(L_eff)[0]
for par, val in known.items():
if par in eff_pars:
eff_kwargs[par] = val
# computing transition luminosity
L_trans = L_adiab(t_trans, **adiab_kwargs)
def LogLeff_fn(LogLpk): return log10(
L_eff(t_trans, L_peak=10**LogLpk, **eff_kwargs)) - log10(L_trans)
L_peak = tl.zeros(LogLeff_fn, log10(L_arr_default))
L_peak = np.squeeze(10.**L_peak)
known.update({'L_peak': L_peak})
elif to_deduce == 't_trans': # based on t_age and both today's and peak info, deduce t_trans
t_peak = known['t_peak']
t_age = known['t_age'] # age
L_peak = known['L_peak']
L_today = known['L_today'] # spectral luminosity today
adiab_kwargs = {'L_ref': L_today, 't_ref': t_age, 'gamma': gamma}
def fn(Ltt): return sandwich_logeqn(L_peak, t_peak, L_today,
t_age, gamma, 10.**Ltt) # function to minimize
try:
# NOTE: added 'np.squeeze'
Lt_cross = np.squeeze(tl.zeros(fn, log10(t_arr_default)))
t_cross = 10.**Lt_cross
try:
t_trans = max(t_cross)
except:
t_trans = t_cross
except ValueError:
t_trans = 1.e11 # made-up, very large value
known.update({'t_trans': t_trans})
elif to_deduce == 't_age': # based on t_trans and both today's and peak info, deduce t_age
t_peak = known['t_peak']
t_trans = known['t_trans'] # transition time
L_peak = known['L_peak']
L_today = known['L_today'] # spectral luminosity today
# simplified free expansion function
def Lf_fn(tt): return L_free(tt, model=model, **known)
L_trans = Lf_fn(t_trans) # L_trans
adiab_kwargs = {'L_ref': L_trans, 't_ref': t_trans, 'gamma': gamma}
t_age = tage_compute(L_peak, t_peak, t_trans,
L_today, gamma) # age
known.update({'t_age': t_age})
else:
raise ValueError(
"to_deduce={}, known={} is currently not supported.".format(to_deduce, known))
elif model == 'thy':
if (to_deduce == 'L_today') or (to_deduce == None):
t_today = known['t_age'] # age [years]
t_trans = known['t_trans'] # transition time [years]
# computing transition luminosity
L_trans = L_free(t_trans, model=model, **known)
adiab_kwargs = {'L_ref': L_trans, 't_ref': t_trans, 'gamma': gamma}
L_today = L_adiab(t_today, **adiab_kwargs)
known.update({'L_today': L_today})
elif to_deduce == 'L_norm':
t_age = known['t_age'] # age [years]
t_trans = known['t_trans'] # transition time [years]
L_today = known['L_today'] # spectral luminosity [today]
adiab_kwargs = {'L_ref': L_today, 't_ref': t_age, 'gamma': gamma}
thy_kwargs = {}
thy_pars = getargspec(L_thy)[0]
for par, val in known.items():
if par in thy_pars:
thy_kwargs[par] = val
# computing transition luminosity
L_trans = L_adiab(t_trans, **adiab_kwargs)
def LogLthy_fn(LogLnorm): return log10(
L_thy(t_trans, L_norm=10**LogLnorm, **thy_kwargs)) - log10(L_trans)
L_norm = tl.zeros(LogLthy_fn, log(L_arr_default))
L_norm = np.squeeze(10.**L_norm)
known.update({'L_norm': L_norm})
if output_pars:
return_pars = {}
return_pars.update(known)
return_pars.update(adiab_kwargs)
Lf = L_free(t, model=model, **known) # computing early-times luminosity
La = L_adiab(t, **adiab_kwargs) # computing adiabatic luminosity
if not 'use_free_expansion' in kwargs.keys():
use_free_expansion = 1.
else:
if kwargs['use_free_expansion'] is False:
use_free_expansion = 0.
else:
use_free_expansion = 1.
Lum = Lf*np.heaviside(t_trans - t, 1.) * \
use_free_expansion + La*np.heaviside(t - t_trans, 0.)
if output_pars:
return Lum, return_pars
else:
return Lum
# SNR light-curve analytic formulas, assuming the Bietenholz effective model.
def Snu_supp(gamma, frac_tpk, frac_tt):
"""
Fractional suppression of spectral irradiance (i.e. S0/S_peak = L0/L_peak).
Parameters
----------
gamma : adiabatic index
frac_tpk : ratio of peak day to SNR age
frac_tt : ratio of transition time to SNR age
"""
first = exp(1.5 - 1.5*frac_tpk/frac_tt)
second = (frac_tt/frac_tpk)**-1.5
third = (1./frac_tt)**-gamma
return first*second*third
def ftt(gamma, frac_tpk, sup):
"""
Fractional transition time (i.e. t_trans/t_age).
Parameters
----------
gamma : adiabatic index
frac_tpk : ratio of peak day to SNR age
sup : spectral irradiance suppression
"""
const = 1./(2.*gamma/3. - 1.)
arg = const * exp(const) * frac_tpk**(const+1.) * sup**(-2.*const/3.)
num = const*frac_tpk
den = lambertw(arg)
return np.real(num/den)
def dimless_free(frac_tpk, tau):
"""
Dimensionless free expansion lightcurve.
Parameters
----------
frac_tpk : ratio of peak day to SNR age
tau : ratio of time to SNR age
"""
return exp(1.5 - 1.5*frac_tpk/tau) * (tau/frac_tpk)**-1.5
def dimless_adiab(gamma, sup, tau):
"""
Dimensionless adiabatic expansion lightcurve.
Parameters
----------
gamma : adiabatic index
sup : spectral irradiance suppression
tau : ratio of time to SNR age
"""
return sup*(tau)**-gamma
def dimless_lum(gamma, frac_tpk, sup, tau):
"""
Dimensionless full lightcurve.
Parameters
----------
gamma : adiabatic index
frac_tpk : ratio of peak day to SNR age
sup : spectral irradiance suppression
tau : ratio of time to SNR age
"""
frac_tt = ftt(gamma, frac_tpk, sup)
free = dimless_free(frac_tpk, tau)
adiab = dimless_adiab(gamma, sup, tau)
return free*np.heaviside(frac_tt-tau, 0.) + adiab*np.heaviside(tau-frac_tt, 1.)
###############################
# SNR evolution analytic models
###############################
# ---------------------------------
# Phenomenological evolution model
def R_pheno(t, Rst=3.8, tst=360., eta1=1., eta2=0.4):
"""
SNR radius [pc] as a simple phenomenological broken power law. The default values are taken from Tables 2 & 3 of the Truelove-McKee '99 paper for n=0 ejecta, with M_ej = 1 M_sun, E_sn = 1.e51 erg, and n0 = 0.2 cm^-3.
Parameters
----------
t : SNR age [years]
Rst : radius [pc] at the start of the Sedov-Taylor (adiabatic) expansion phase (default: 3.8)
tst : age [years] at the start of the Sedov-Taylor (adiabatic) expansion phase (default 360.)
eta1 : the power scaling R~t^eta1 during the Ejecta-Dominated phase (default: 1.)
eta2 : the power scaling R`t^eta2 during the Sedov-Taylor expansion phase (default: 2/5 = 0.4)
"""
ed = Rst * (t/tst)**eta1 * np.heaviside(tst-t, 0.)
ad = Rst * (t/tst)**eta2 * np.heaviside(t-tst, 1.)
return ed+ad
def pheno_age(R, Rst=3.8, tst=360., eta1=1., eta2=0.4):
"""
SNR age [years] as deduced from a phenomenological broken power-law function of the SNR blast radius [pc]. The default values are taken from Tables 2 & 3 of the Truelove-McKee '99 paper for n=0 ejecta, with M_ej = 1 M_sun, E_sn = 1.e51 erg, and n0 = 0.2 cm^-3.
Parameters
----------
R : SNR radius [pc]
Rst : radius [pc] at the start of the Sedov-Taylor (adiabatic) expansion phase (default: 3.8)
tst : age [years] at the start of the Sedov-Taylor (adiabatic) expansion phase (default 360.)
eta1 : the power scaling R~t^eta1 during the Ejecta-Dominated phase (default: 1.)
eta2 : the power scaling R`t^eta2 during the Sedov-Taylor expansion phase (default: 2/5 = 0.4)
"""
def logDelRt(t, r): return log10(
R_pheno(t, Rst, tst, eta1=eta1, eta2=eta2)) - log10(r)
R_arr, _ = tl.treat_as_arr(R)
age = np.array([tl.zeros(logDelRt, t_arr_default, r) for r in R_arr])
age = np.squeeze(age)
return age
# -------------------------
# Physical evolution model
def ED_fn(t, t_bench, R_bench, model):
"""
Blast radius [pc] as a function of time for the Ejecta-Dominated era. Based on Truelove & McKee 1999 (TM99).
Parameters
----------
t : time [years]
t_bench : benchmark time [years]
R_bench : benchmark radius [pc]
model : model of the expansion ('TM99-simple'/'TM99-0' for a simplified version of TM99 or for the case with n=0 ejecta.)
"""
tstar = t/t_bench
if model == 'TM99-simple':
# power law R~t
Rstar = tstar
elif model == 'TM99-0':
# TM99, Table 5
Rstar = 2.01*tstar*(1. + 1.72 * tstar**(3./2.))**(-2./3.)
return R_bench*Rstar
def ST_fn(t, t_bench, R_bench, model):
"""
Blast radius [pc] as a function of time for the Sedov-Taylor era. Based on Truelove & McKee 1999 (TM99).
Parameters
----------
t : time [years]
t_bench : benchmark time [years]
R_bench : benchmark radius [pc]
model : model of the expansion ('TM99-simple'/'TM99-0' for a simplified version of TM99 or for the case with n=0 ejecta.)
"""
tstar = t/t_bench
if model == 'TM99-simple':
# power law R~t^(2/5)
Rstar = tstar**(2./5.)
elif model == 'TM99-0':
# TM99, Table 5
arg = (1.42*tstar - 0.254)
# regularizing:
arg = np.where(arg <= 0., 1.e-100, arg)
Rstar = arg**(2./5.)
return R_bench*Rstar
def Rb_TM99(t, t_bench, R_bench, model):
"""
Blast radius [pc] as a function of SNR age [years]. Based on Truelove & McKee 1999 (TM99).
Parameters
----------
t : time [years]
t_bench : benchmark time [years]
R_bench : benchmark radius [pc]
model : model of the expansion ('TM99-simple'/'TM99-0' for a simplified version of TM99 or for the case with n=0 ejecta.)
"""
# broken function:
if model == 'TM99-simple':
two_phase = np.minimum.reduce(
[ED_fn(t, t_bench, R_bench, model), ST_fn(t, t_bench, R_bench, model)])
elif model == 'TM99-0':
two_phase = np.maximum.reduce(
[ED_fn(t, t_bench, R_bench, model), ST_fn(t, t_bench, R_bench, model)])
return two_phase
def physics_age(R, model='estimate', M_ej=1., E_sn=1., rho0=1.):
"""
SNR age [years] as deduced from a physically-motivated a function of the SNR blast radius [pc]. Using either a simple model, or formulas by Truelove & McKee 1999 (TM99).
Parameters
----------
R : SNR radius today [pc]
model : 'estimate'/'TM99-simple'/TM99-0'': whether the simple one-phase 'estimate' model is used, or instead the two-phase Truelove-McKee model (ED-ST, or Ejecta-Dominated -- Sedov-Taylor), either in a simplified form, or for n=0 (uniform) ejecta profile.
M_ej : Mass of the ejecta [M_sun] (default: 1.)
E_sn : Energy of the SNR [1.e51 ergs] (default: 1.)
rho0 : Mass density of surrounding medium [m_proton/cm^3] (default: 1.)
"""
if not model in ['estimate', 'TM99-simple', 'TM99-0']:
raise ValueError("model={} is currently unsupported.".format(model))
if model == 'estimate':
# https://chandra.harvard.edu/edu/formal/age_snr/pencil_paper.pdf
R_m = R*(ct._kpc_over_m_/1000.) # [m]
vol_m3 = (4./3.) * pi * R_m**3. # [m^3]
m_proton = 0.93827208816*ct._GeV_over_g_ * 1.e-3 # [kg]
density = (m_proton*rho0)*1.e6 # [kg/m^3]
mass = density*vol_m3 # [kg]
E_sn_J = (E_sn*1.e51)*(1.e-7) # [J]
ke = 0.25*E_sn_J # [J]
vel = sqrt(2.*ke / mass) # [m/s]
age = (R_m/vel)/ct._year_over_s_ # [years]
return age
elif model in ['TM99-simple', 'TM99-0']:
# Article: https://iopscience.iop.org/article/10.1086/313176
# Erratum: https://iopscience.iop.org/article/10.1086/313385
# Revisited: arXiv:2109.03612
n0 = rho0/1. # number density [cm^-3]
# Characteristic scales: Table 2
R_ch = 3.07 * M_ej**(1./3.) * n0**(-1./3.) # [pc]
t_ch = 423. * E_sn**(-1./2.) * M_ej**(5./6.) * n0**(-1./3.) # [years]
# Sedov-Taylor scales: Table 3 (rounding up, for various ejecta power-law indices)
if model == 'TM99-simple':
tstar_ST = 0.4
Rstar_ST = 0.7
elif model == 'TM99-0':
tstar_ST = 0.4950
Rstar_ST = 0.7276
# Sedov-Taylor radius and age
t_ST = tstar_ST*t_ch
R_ST = Rstar_ST*R_ch
if model == 'TM99-simple':
t_bench = t_ST
R_bench = R_ST
elif model == 'TM99-0':
t_bench = t_ch
R_bench = R_ch
# defining the evolution in the two phases
def LogDelRb(t, r):
"""
Function whose zeros we need to find in order to solve for the time [years] as a function of the radius [pc].
"""
return log10(Rb_TM99(t, t_bench, R_bench, model)) - log10(r)
R_arr, _ = tl.treat_as_arr(R)
age = np.array([tl.zeros(LogDelRb, t_arr_default, r) for r in R_arr])
age = np.squeeze(age)
return age
#########################################
# Noise and signal
def bg_408_temp(l, b, size=None, average=False, verbose=True, load_on_the_fly=False):
"""
Reads the background brightness temperature at 408 MHz at galactic coordinates l and b.
Parameters
----------
l : longitude [deg]
b : latitude [deg]
size : angular size [sr] of the region of interest (default: None)
average : whether the brightness temperature is averaged over the size of the region of interest (default: False)
verbose: control of warning output. Switch to False only if you know what you are doing.
load_on_the_fly: flag to load the map on the fly. It's for debugging purpose. Switch to True only if you know what you are doing.
"""
if load_on_the_fly:
if verbose:
print('You requested to load Haslem every time bg_408_temp is called. This is highly time-consuming. You should use this only for debugging purpose.')
# initialize the haslem map
try:
path = os.path.dirname(os.path.abspath(__file__))+'/data/'
path = os.path.join(path, 'lambda_haslam408_dsds.fits')
global map_allsky_408
map_allsky_408 = hp.read_map(path)
except FileNotFoundError:
raise Exception(
'Haslam map (lambda_haslam408_dsds.fits) is not found.')
healp = HEALPix(nside=512, order='ring', frame=Galactic())
pos_coords = SkyCoord(frame="galactic", l=l, b=b, unit='deg')
pos_pix = healp.skycoord_to_healpix(pos_coords)
vec = hp.pix2vec(nside=512, ipix=pos_pix)
if size is not None:
size, is_scalar = tl.treat_as_arr(size) # scalar --> array trick
bg_T408 = []
if average:
for size_val in size:
radial_angle = ct.solid_angle_to_angle(
size_val)/2. # need to divide by 2 to get radius
new_pos_pix = hp.query_disc(
nside=512, vec=vec, radius=radial_angle)
bg_T408.append(np.average(map_allsky_408[new_pos_pix]))
else:
# this should be used only for debugging
# i.e. manually switch average == False
print('Warning: you are setting average flag to False')
for size_val in size:
new_pos_pix = pos_pix
bg_T408.append(np.average(map_allsky_408[new_pos_pix]))
if is_scalar:
bg_T408 = np.squeeze(bg_T408)
else:
bg_T408 = np.array(bg_T408)
else:
new_pos_pix = pos_pix
bg_T408 = np.average(map_allsky_408[new_pos_pix])
# query the background temperature for the echo
return bg_T408
def T_atm(nu):
"""Compute the atmospheric temperature [K].
NOTE: There is an unphysical break at the boundary.
:param nu: Frequency [GHz]
"""
nu, is_scalar = tl.treat_as_arr(nu)
nu_low = nu[nu <= ct._nu_min_ska_mid_]
nu_mid = nu[nu > ct._nu_min_ska_mid_]
res_low = Tatm_low_fn(nu_low)
res_mid = Tatm_mid_fn(nu_mid)
res = np.concatenate((res_low, res_mid))
if is_scalar:
res = np.squeeze(res)
return res
def Tatm_mid_fn(nu):
"""The atmospheric temperature for SKA1-Mid frequency
:param nu: frequency [GHz]
"""