-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_phys.py
More file actions
1454 lines (1088 loc) · 57.7 KB
/
model_phys.py
File metadata and controls
1454 lines (1088 loc) · 57.7 KB
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 file contain a subclass of the model.py module and Cluster class. It
is dedicated to the computing of the physical properties of clusters.
"""
#==================================================
# Requested imports
#==================================================
import numpy as np
from scipy.optimize import brentq
import scipy.interpolate as interpolate
import scipy.ndimage as ndimage
import os
import astropy.units as u
from astropy import constants as const
from ClusterModel import model_tools
from ClusterModel.ClusterTools import cluster_global
from ClusterModel.ClusterTools import cluster_profile
from ClusterModel.ClusterTools import cluster_spectra
from ClusterModel.ClusterTools import cluster_szspec
from ClusterModel.ClusterTools import cluster_xspec
from ClusterModel.ClusterTools import cluster_hadronic_emission_kafexhiu2014 as K14
from ClusterModel.ClusterTools import cluster_electron_loss
from ClusterModel.ClusterTools import cluster_electron_emission
#==================================================
# Physics class
#==================================================
class Physics(object):
""" Physics class
This class searves as a parser to the main Cluster class, to
include the subclass Physics in this other file.
Attributes
----------
The attributes are the same as the Cluster class, see model.py
Methods
----------
* Methods related to the physical description of the cluster
- get_pressure_gas_profile(self, radius=np.logspace(1,5,1000)*u.kpc): compute the electron
gas pressure profile.
- get_density_gas_profile(self, radius=np.logspace(0,4,1000)*u.kpc): compute the electron
gas density profile.
- get_temperature_gas_profile(self, radius=np.logspace(0,4,1000)*u.kpc): compute the gas
temperature profile (same as electron or ions)
- get_entropy_gas_profile(self, radius=np.logspace(0,4,1000)*u.kpc) : compute the entropy profile
- get_hse_mass_profile(self, radius=np.logspace(0,4,1000)*u.kpc) : compute the hydrostatic
mass profile
- get_overdensity_contrast_profile(self, radius=np.logspace(0,4,1000)*u.kpc): compute the
overdensity contrast.
- get_mdelta_from_profile(self, delta=500, Rmin=10*u.kpc, Rmax=1e4*u.kpc): compute the mass
and radius corresponding to a given overdensity contrast, e.g. M500 or R500, from the hydrostatic
mass profile and a mean hydrostatic mass bias.
- get_gas_mass_profile(self, radius=np.logspace(0,4,1000)*u.kpc): compute the gas mass profile
- get_fgas_profile(self, radius=np.logspace(0,4,1000)*u.kpc): compute the gas fraction profile
- get_thermal_energy_profile(self, radius=np.logspace(0,4,1000)*u.kpc): compute the thermal
energy profile
- get_magfield_profile(self, radius=np.logspace(0,4,1000)*u.kpc): get the magnetic field profile
- get_normed_density_crp_profile(self, radius=np.logspace(0,4,1000)*u.kpc): get the radial
part of the cosmic ray protons distribution, f(r), in dN/dEdV = A f(r) f(E)
- get_normed_crp_spectrum(self, energy=np.logspace(-2,7,1000)*u.GeV): get the spectral part
of the cosmic ray proton distribution, f(E), in dN/dEdV = A f(r) f(E)
- _get_crp_normalization(self): compute the normalization of the cosmic ray proton distribution,
A, in dN/dEdV = A f(r) f(E)
- get_crp_2d(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc): compute
the CRp distribution on a 2d grid (energy vs radius) as dN/dEdV
- get_density_crp_profile(self, radius=np.logspace(0,4,1000)*u.kpc, Emin=None, Emax=None,
Energy_density=False): compute the cosmic ray proton density profile integrating over the energy
between Emin and Emax.
- get_crp_spectrum(self, energy=np.logspace(-2,7,1000)*u.GeV, Rmax=None): compute the cosmic ray proton
spectrum integrating over the volume up to Rmax
- get_crp_to_thermal_energy_profile(self, radius=np.logspace(0,4,1000)*u.kpc, Emin=None, Emax=None):
compute the cosmic ray proton energy (between Emin and Emax) to thermal energy profile.
* Methods related to the physical processes in the cluster
- get_rate_gamma(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
compute the gamma ray emission rate dN/dEdVdt versus energy and radius
- get_rate_cre(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
compute the CRe production rate dN/dEdVdt versus energy and radius
- get_rate_neutrino(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc, flavor='all'):
compute the neutrino production rate dN/dEdVdt versus energy and radius
- get_cre_2d(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
compute the CRe population assuming equilibrium dN/dEdV versus energy and radius
- get_density_cre_profile(self, radius=np.logspace(0,4,100)*u.kpc,Emin=None, Emax=None, Energy_density=False):
compute the cosmic ray electron density profile integrating over the energy between Emin and Emax.
- get_cre_spectrum(self, energy=np.logspace(-2,7,100)*u.GeV, Rmax=None): compute the cosmic ray electron
spectrum integrating over the volume up to Rmax
- get_rate_synchrotron(self, frequency=np.logspace(-3,3,100)*u.GHz, radius=np.logspace(0,4,100)*u.kpc):
compute the synchrotron emission per unit volume given the electron population and magnetic field
- get_rate_ic(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
compute the inverse compton emission per unit volume given the electron population and CMB(z)
- get_rate_sz(self, frequency=np.logspace(1,3,100)*u.GHz, radius=np.logspace(0,4,100)*u.kpc, Compton_only=False):
Compute the SZ emission per cm3
- make_xspec_table(self, Emin=0.1*u.keV, Emax=2.4*u.keV,Tmin=0.1*u.keV, Tmax=50.0*u.keV, nbin=100,
nH=0.0/u.cm**2, file_HI=None): compute a temperature versus counts/Sx table to be interpolated
when getting profiles and maps
- _itpl_xspec_table(self, xspecfile, Tinput): interpolate xspec tables to chosen temperature
- get_rate_xray(self, radius=np.logspace(0,4,100)*u.kpc, output_type='C'): compute
the X-ray emission per unit volume
"""
#==================================================
# Get the gas electron pressure profile
#==================================================
def get_pressure_gas_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the thermal electron pressure profile.
Parameters
----------
- radius (quantity) : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- p_r (quantity): the electron pressure profile in unit of keV cm-3
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# get profile
p_r = self._get_generic_profile(radius, self._pressure_gas_model)
p_r[radius > self._R_truncation] *= 0
return radius, p_r.to('keV cm-3')
#==================================================
# Get the gas electron density profile
#==================================================
def get_density_gas_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the thermal electron density profile.
Parameters
----------
- radius (quantity) : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- n_r (quantity): the electron density profile in unit of cm-3
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# get profile
n_r = self._get_generic_profile(radius, self._density_gas_model)
n_r[radius > self._R_truncation] *= 0
return radius, n_r.to('cm-3')
#==================================================
# Get the gas electron temperature profile
#==================================================
def get_temperature_gas_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the thermal temperature profile.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- T_r (quantity): the temperature profile in unit of keV
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# Compute n and P
radius, n_r = self.get_density_gas_profile(radius=radius)
radius, P_r = self.get_pressure_gas_profile(radius=radius)
# Get Temperature
n_r[n_r <= 0] = np.nan
T_r = P_r / n_r
# Apply truncation
T_r[radius > self._R_truncation] = np.nan
return radius, T_r.to('keV')
#==================================================
# Get the gas entropy profile
#==================================================
def get_entropy_gas_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the entropy profile.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- K_r (quantity): the entropy profile in unit of keV cm2
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# Compute n and P
radius, n_r = self.get_density_gas_profile(radius=radius)
radius, P_r = self.get_pressure_gas_profile(radius=radius)
# Get K
n_r[n_r <= 0] = np.nan
K_r = P_r / n_r**(5.0/3)
# Apply truncation
K_r[radius > self._R_truncation] = np.nan
return radius, K_r.to('keV cm2')
#==================================================
# Get the hydrostatic mass profile
#==================================================
def get_hse_mass_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the hydrostatic mass profile using exact analytical expressions.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- Mhse_r (quantity): the hydrostatic mass profile in unit of Msun
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
#---------- Mean molecular weights
mu_gas, mu_e, mu_p, mu_alpha = cluster_global.mean_molecular_weight(Y=self._helium_mass_fraction,
Z=self._metallicity_sol*self._abundance)
#---------- Get the electron density profile
radius, n_r = self.get_density_gas_profile(radius=radius)
#---------- Get dP/dr
dpdr_r = self._get_generic_profile(radius, self._pressure_gas_model, derivative=True)
dpdr_r[radius > self._R_truncation] *= 0
#---------- Compute the mass
n_r[n_r <= 0] = np.nan
Mhse_r = -radius**2 / n_r * dpdr_r / (mu_gas*const.m_p*const.G)
Mhse_r[radius > self._R_truncation] = np.nan
return radius, Mhse_r.to('Msun')
#==================================================
# Get the overdensity contrast profile
#==================================================
def get_overdensity_contrast_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the overdensity contrast profile.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- delta_r: the overdensity contrast profile
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# Compute delta from the mass profile
r, mhse = self.get_hse_mass_profile(radius)
delta_r = mhse/(1.0-self._hse_bias) / (4.0/3.0*np.pi*radius**3 * self._cosmo.critical_density(self._redshift))
return radius, delta_r.to_value('')*u.adu
#==================================================
# Get the R500 profile
#==================================================
def get_mdelta_from_profile(self, delta=500, Rmin=10*u.kpc, Rmax=1e4*u.kpc):
"""
Get R_delta and M_delta from the overdensity profile, given HSE equilibrium and HSE bias.
Parameters
----------
- delta : the overdensity considered, e.g. 2500, 500, 200
- Rmin (quantity): the minimal range to search for Rdelta
- Rmax (quantity): the maximal range to search for Rdelta
Outputs
----------
- Rdelta (quantity): the radius within which delta times the critical density is enclose, e.g. R500
- Mdelta (quantity): the mass corresponding to Rdelta
"""
# defines the function where to search for roots
def overdensity_delta_difference(rkpc):
rod, od = self.get_overdensity_contrast_profile(rkpc*u.kpc)
func = od.to_value('adu') - delta
func[np.isnan(od)] = -1.0 # Undefined overdensity (e.g. if truncation) should not be the root
return func
# Search the root
Rdelta = brentq(overdensity_delta_difference, Rmin.to_value('kpc'), Rmax.to_value('kpc'))
# In case the root is >= R_truncation, Rdelta was not reached
if Rdelta >= self._R_truncation.to_value('kpc'):
if not self._silent: print('The truncation was reached before R'+str(delta))
Rdelta = np.nan
# Get Mdelta as well
Mdelta = cluster_global.Rdelta_to_Mdelta(Rdelta, self._redshift, delta=delta, cosmo=self._cosmo)
return Rdelta*u.kpc, Mdelta*u.Msun
#==================================================
# Compute Mgas
#==================================================
def get_gas_mass_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the gas mass profile.
Parameters
----------
- radius (quantity) : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- Mgas_r (quantity): the gas mass profile
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
#---------- Mean molecular weights
mu_gas, mu_e, mu_p, mu_alpha = cluster_global.mean_molecular_weight(Y=self._helium_mass_fraction,
Z=self._metallicity_sol*self._abundance)
#---------- Integrate the mass
I_n_gas_r = np.zeros(len(radius))
for i in range(len(radius)):
rmin = np.amin([self._Rmin.to_value('kpc'), radius.to_value('kpc')[i]/10.0])*u.kpc # make sure we go well bellow rmax
rad = model_tools.sampling_array(rmin, radius[i], NptPd=self._Npt_per_decade_integ, unit=True)
# To avoid ringing at Rtrunc, insert it if we are above
if np.amax(rad) > self._R_truncation:
rad = rad.insert(0, self._R_truncation)
rad.sort()
rad, n_r = self.get_density_gas_profile(radius=rad)
I_n_gas_r[i] = model_tools.trapz_loglog(4*np.pi*rad**2*n_r, rad)
Mgas_r = mu_e*const.m_p * I_n_gas_r
return radius, Mgas_r.to('Msun')
#==================================================
# Compute fgas
#==================================================
def get_fgas_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the gas fraction profile.
Parameters
----------
- radius : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- fgas_r (quantity): the gas mass profile
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# Compute fgas from Mgas and Mhse
r, mgas = self.get_gas_mass_profile(radius)
r, mhse = self.get_hse_mass_profile(radius)
fgas_r = mgas.to_value('Msun') / mhse.to_value('Msun') * (1.0 - self._hse_bias)
return radius, fgas_r*u.adu
#==================================================
# Compute thermal energy
#==================================================
def get_thermal_energy_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Compute the thermal energy profile
Parameters
----------
- radius (quantity) : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- Uth (quantity) : the thermal energy, in GeV
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
#---------- Mean molecular weights
mu_gas, mu_e, mu_p, mu_alpha = cluster_global.mean_molecular_weight(Y=self._helium_mass_fraction,
Z=self._metallicity_sol*self._abundance)
#---------- Integrate the pressure in 3d
Uth_r = np.zeros(len(radius))*u.erg
for i in range(len(radius)):
rmin = np.amin([self._Rmin.to_value('kpc'), radius.to_value('kpc')[i]/10.0])*u.kpc # make sure we go well bellow rmax
rad = model_tools.sampling_array(rmin, radius[i], NptPd=self._Npt_per_decade_integ, unit=True)
# To avoid ringing at Rtrunc, insert it if we are above
if np.amax(rad) > self._R_truncation:
rad = rad.insert(0, self._R_truncation)
rad.sort()
rad, p_r = self.get_pressure_gas_profile(radius=rad)
Uth_r[i] = model_tools.trapz_loglog((3.0/2.0)*(mu_e/mu_gas) * 4*np.pi*rad**2 * p_r, rad)
return radius, Uth_r.to('erg')
#==================================================
# Get the gas electron density profile
#==================================================
def get_magfield_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the magnetic field profile.
Parameters
----------
- radius (quantity) : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- B_r (quantity): the magnetic field profile in unit of uG
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# get profile
B_r = self._get_generic_profile(radius, self._magfield_model)
B_r[radius > self._R_truncation] *= 0
return radius, B_r.to('uG')
#==================================================
# Get normalized CR density profile
#==================================================
def get_normed_density_crp_profile(self, radius=np.logspace(0,4,100)*u.kpc):
"""
Get the normalized cosmic ray proton density profile.
Parameters
----------
- radius (quantity) : the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- n_r (quantity): the normalized density profile, unitless
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# get profile
n_r = self._get_generic_profile(radius, self._density_crp_model)
n_r[radius > self._R_truncation] *= 0
return radius, n_r.to('adu')
#==================================================
# Get normalized CR density profile
#==================================================
def get_normed_crp_spectrum(self, energy=np.logspace(-2,7,100)*u.GeV):
"""
Get the normalized cosmic ray proton spectrum.
Parameters
----------
- energy (quantity) : the physical energy of CR protons
Outputs
----------
- radius (quantity): the 3d radius in unit of kpc
- S_E (quantity): the normalized spectrum profile, unitless
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
# get spectrum
S_E = self._get_generic_spectrum(energy, self._spectrum_crp_model)
S_E[energy > self._Epmax] *= 0
S_E[energy < self._Epmin] *= 0
return energy, S_E*u.adu
#==================================================
# Get the CR proton normalization
#==================================================
def _get_crp_normalization(self):
"""
Compute the normalization of the cosmic ray proton distribution:
dN/dE/dV = Norm f(E) f(r)
with f(E) the spectral form and f(r) the spatial form
Norm is in unit of GeV-1 cm-3
Parameters
----------
Outputs
----------
- Norm (quantity): in unit of GeV-1 cm-3
"""
Rcut = self._X_cr_E['R_norm']
# Get the thermal energy
rad_uth, U_th = self.get_thermal_energy_profile(Rcut)
# Get the spatial form volume for CRp
rad = model_tools.sampling_array(self._Rmin, Rcut, NptPd=self._Npt_per_decade_integ, unit=True)
rad, f_cr_r = self.get_normed_density_crp_profile(rad)
Vcr = model_tools.trapz_loglog(4*np.pi*rad**2 * f_cr_r.to_value('adu'), rad)
# Get the energy enclosed in the spectrum
eng = model_tools.sampling_array(self._Epmin, self._Epmax, NptPd=self._Npt_per_decade_integ, unit=True)
eng, f_cr_E = self.get_normed_crp_spectrum(eng)
Ienergy = model_tools.trapz_loglog(eng * f_cr_E.to_value('adu'), eng)
# Compute the normalization
Norm = self._X_cr_E['X'] * U_th / Vcr / Ienergy
return Norm.to('GeV-1 cm-3')
#==================================================
# Get the CR proton 2d distribution
#==================================================
def get_crp_2d(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
"""
Compute the cosmic ray proton 2d distribution.
Parameters
----------
- energy (quantity) : the physical energy of CR protons
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- spectrum (quantity): in unit of GeV-1 cm-3, with spectrum[i_energy, i_radius]
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
radius = model_tools.check_qarray(radius, unit='kpc')
# Get the normalization
norm = self._get_crp_normalization()
# Integrate over the considered volume
rad, f_r = self.get_normed_density_crp_profile(radius)
f_r2 = model_tools.replicate_array(f_r.to_value('adu'), len(energy), T=False)
# Get the energy form
eng, f_E = self.get_normed_crp_spectrum(energy)
f_E2 = model_tools.replicate_array(f_E.to_value('adu'), len(radius), T=True)
# compute the spectrum
spectrum = norm * f_E2 * f_r2
return spectrum.to('GeV-1 cm-3')
#==================================================
# Get the CR proton density profile
#==================================================
def get_density_crp_profile(self, radius=np.logspace(0,4,100)*u.kpc,
Emin=None, Emax=None, Energy_density=False):
"""
Compute the cosmic ray proton density profile, integrating energies
between Emin and Emax.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
- Emin (quantity): the lower bound for energy integration
- Emax (quantity): the upper bound for energy integration
- Energy_density (bool): if True, then the energy density is computed. Otherwise,
the number density is computed.
Outputs
----------
- density (quantity): in unit of cm-3 or GeV cm-3
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# Define energy
if Emin is None:
Emin = self._Epmin
if Emax is None:
Emax = self._Epmax
# Integrate over the spectrum
eng = model_tools.sampling_array(Emin, Emax, NptPd=self._Npt_per_decade_integ, unit=True)
dN_dEdV = self.get_crp_2d(eng, radius)
if Energy_density:
profile = (model_tools.trapz_loglog(np.vstack(eng.to_value('GeV'))*u.GeV * dN_dEdV, eng, axis=0)).to('GeV cm-3')
else:
profile = (model_tools.trapz_loglog(dN_dEdV, eng, axis=0)).to('cm-3')
return radius, profile
#==================================================
# Get the CR proton spectrum
#==================================================
def get_crp_spectrum(self, energy=np.logspace(-2,7,100)*u.GeV, Rmax=None):
"""
Compute the cosmic ray proton spectrum, integrating radius
between 0 and Rmax.
Parameters
----------
- energy (quantity) : the physical energy of CR protons
- Rmax (quantity): the radius within with the spectrum is computed
(default is R500)
Outputs
----------
- spectrum (quantity): in unit of GeV-1
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
# define the maximum radius
if Rmax is None:
Rmax = self._R500
# Define the radius for integration
rmin = np.amin([self._Rmin.to_value('kpc'), Rmax.to_value('kpc')/10])*u.kpc #In case of small Rmax, make sure we go low enough
rad = model_tools.sampling_array(rmin, Rmax, NptPd=self._Npt_per_decade_integ, unit=True)
# To improve precision around R_truncation in integration
if np.amax(rad) > self._R_truncation:
rad = rad.insert(0, self._R_truncation)
rad.sort()
# Get the differential spectrum/profile
dN_dEdV = self.get_crp_2d(energy, rad)
# Integrate
spectrum = model_tools.trapz_loglog(4*np.pi*rad**2 * dN_dEdV, rad)
return energy, spectrum.to('GeV-1')
#==================================================
# Get the CR to thermal energy profile
#==================================================
def get_crp_to_thermal_energy_profile(self, radius=np.logspace(0,4,100)*u.kpc,
Emin=None, Emax=None):
"""
Compute the X_cr_E profile, i.e. the cosmic ray to thermal energy enclosed within R
profile.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
- Emin (quantity): the lower bound for energy integration
- Emax (quantity): the upper bound for energy integration
Outputs
----------
- x_r (np.ndarray): the profile
"""
# In case the input is not an array
radius = model_tools.check_qarray(radius, unit='kpc')
# Define energy (going below/above Epmin/Epmax only introduces more integration error because of discountinuity)
if Emin is None or Emin < self._Epmin:
Emin = self._Epmin
if Emax is None or Emax > self._Epmax:
Emax = self._Epmax
# Thermal energy
r_uth, Uth_r = self.get_thermal_energy_profile(radius)
# Integrate CR energy density profile
Ucr_r = np.zeros(len(radius))*u.GeV
for i in range(len(radius)):
rmin = np.amin([self._Rmin.to_value('kpc'), radius.to_value('kpc')[i]/10.0])*u.kpc # make sure we go well bellow rmax
rad = model_tools.sampling_array(rmin, radius[i], NptPd=self._Npt_per_decade_integ, unit=True)
# To avoid ringing at Rtrunc, insert it if we are above
if np.amax(rad) > self._R_truncation:
rad = rad.insert(0, self._R_truncation)
rad.sort()
rad, e_cr = self.get_density_crp_profile(rad, Emin=Emin, Emax=Emax, Energy_density=True)
Ucr_r[i] = model_tools.trapz_loglog(4*np.pi*rad**2 * e_cr, rad)
# X(<R)
x_r = Ucr_r.to_value('GeV') / Uth_r.to_value('GeV')
return radius, x_r*u.adu
#==================================================
# Get the gamma ray production rate
#==================================================
def get_rate_gamma(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
"""
Compute the gamma ray production rate as dN/dEdVdt = f(E, r)
Parameters
----------
- energy (quantity) : the physical energy of gamma rays
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- dN_dEdVdt (np.ndarray): the differntial production rate
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
radius = model_tools.check_qarray(radius, unit='kpc')
# Get the thermal proton density profile
mu_gas, mu_e, mu_p, mu_alpha = cluster_global.mean_molecular_weight(Y=self._helium_mass_fraction,
Z=self._metallicity_sol*self._abundance)
rad, n_e = self.get_density_gas_profile(radius)
n_H = n_e * mu_e/mu_p
# Parse the CRp distribution: returns call function[rad, energy] amd returns f[rad, energy]
def Jp(rad, eng): return self.get_crp_2d(eng*u.GeV, rad*u.kpc).value.T
# Define the model
model = K14.PPmodel(Jp,
Y0=self._helium_mass_fraction,
Z0=self._metallicity_sol,
abundance=self._abundance,
hiEmodel=self._pp_interaction_model,
Epmin=self._Epmin,
Epmax=self._Epmax,
NptEpPd=self._Npt_per_decade_integ)
# Extract the spectrum
dN_dEdVdt = model.gamma_spectrum(energy, radius, n_H).T
return dN_dEdVdt.to('GeV-1 cm-3 s-1')
#==================================================
# Get the gamma ray production rate
#==================================================
def get_rate_cre(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
"""
Compute the cosmic ray electron production rate as dN/dEdVdt = f(E, r)
Note
----------
At high energy, wit few point per decade, some "lines" may appear in the spectrum
due to numerical issues.
Parameters
----------
- energy (quantity) : the physical energy of gamma rays
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
Outputs
----------
- dN_dEdVdt (np.ndarray): the differntial production rate
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
radius = model_tools.check_qarray(radius, unit='kpc')
# Get the thermal proton density profile
mu_gas, mu_e, mu_p, mu_alpha = cluster_global.mean_molecular_weight(Y=self._helium_mass_fraction,
Z=self._metallicity_sol*self._abundance)
rad, n_e = self.get_density_gas_profile(radius)
n_H = n_e * mu_e/mu_p
# Parse the CRp distribution: returns call function[rad, energy] amd returns f[rad, energy]
def Jp(rad, eng): return self.get_crp_2d(eng*u.GeV, rad*u.kpc).value.T
# Define the model
model = K14.PPmodel(Jp,
Y0=self._helium_mass_fraction,
Z0=self._metallicity_sol,
abundance=self._abundance,
hiEmodel=self._pp_interaction_model,
Epmin=self._Epmin,
Epmax=self._Epmax,
NptEpPd=self._Npt_per_decade_integ)
# Extract the spectrum
dN_dEdVdt = model.electron_spectrum(energy, radius, n_H).T
return dN_dEdVdt.to('GeV-1 cm-3 s-1')
#==================================================
# Get the gamma ray production rate
#==================================================
def get_rate_neutrino(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc, flavor='all'):
"""
Compute the cosmic ray electron production rate as dN/dEdVdt = f(E, r)
Note
----------
At high energy, wit few point per decade, some "lines" may appear in the spectrum
due to numerical issues.
Parameters
----------
- energy (quantity) : the physical energy of gamma rays
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
- flavor (str): either 'all', 'numu' or 'nue'
Outputs
----------
- dN_dEdVdt (np.ndarray): the differntial production rate
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
radius = model_tools.check_qarray(radius, unit='kpc')
# Get the thermal proton density profile
mu_gas, mu_e, mu_p, mu_alpha = cluster_global.mean_molecular_weight(Y=self._helium_mass_fraction,
Z=self._metallicity_sol*self._abundance)
rad, n_e = self.get_density_gas_profile(radius)
n_H = n_e * mu_e/mu_p
# Parse the CRp distribution: returns call function[rad, energy] amd returns f[rad, energy]
def Jp(rad, eng): return self.get_crp_2d(eng*u.GeV, rad*u.kpc).to_value('GeV-1 cm-3').T
# Define the model
model = K14.PPmodel(Jp,
Y0=self._helium_mass_fraction,
Z0=self._metallicity_sol,
abundance=self._abundance,
hiEmodel=self._pp_interaction_model,
Epmin=self._Epmin,
Epmax=self._Epmax,
NptEpPd=self._Npt_per_decade_integ)
# Extract the spectrum
if flavor == 'all':
dN_dEdVdt1 = model.neutrino_spectrum(energy, radius, n_H, flavor='numu').T
dN_dEdVdt2 = model.neutrino_spectrum(energy, radius, n_H, flavor='nue').T
dN_dEdVdt = dN_dEdVdt1 + dN_dEdVdt2
elif flavor == 'numu':
dN_dEdVdt = model.neutrino_spectrum(energy, radius, n_H, flavor='numu').T
elif flavor == 'nue':
dN_dEdVdt = model.neutrino_spectrum(energy, radius, n_H, flavor='nue').T
else :
raise ValueError('Only all, numu and nue flavor are available.')
return dN_dEdVdt.to('GeV-1 cm-3 s-1')
#==================================================
# Get the electron spectrum
#==================================================
def get_cre_2d(self, energy=np.logspace(-2,7,100)*u.GeV, radius=np.logspace(0,4,100)*u.kpc):
"""
Compute the electron spectrum as dN/dEdV = f(E, r)
The steady state solution is used: dN/dEdV(E,r) = 1/L(E,r) * \int_E^\infty Q(E) dE
See e.g. Sarrazin (1999) eq 38.
Parameters
----------
- radius (quantity): the physical 3d radius in units homogeneous to kpc, as a 1d array
- energy (quantity) : the physical energy of electrons
Outputs
----------
- dNg_dEdV (np.ndarray): the differntial production rate
"""
# In case the input is not an array
energy = model_tools.check_qarray(energy, unit='GeV')
radius = model_tools.check_qarray(radius, unit='kpc')
# Get the necessary quantity
radius, n_e = self.get_density_gas_profile(radius)
radius, B = self.get_magfield_profile(radius)
# Compute the losses
dEdt = cluster_electron_loss.dEdt_tot(energy, radius=radius, n_e=n_e, B=B, redshift=self._redshift)
# Get the injection rate between the and max possible, i.e. Epmax
emin = np.amax([(const.m_e*const.c**2).to_value('GeV'),
np.amin(energy.to_value('GeV'))])*u.GeV # min of CRe energy requested, or m_e c^2
emax = self._Epmax
eng = model_tools.sampling_array(emin, emax, NptPd=self._Npt_per_decade_integ, unit=True)
dN_dEdVdt = self.get_rate_cre(eng, radius) # This is the time consumming part
eng_grid = model_tools.replicate_array(eng, len(radius), T=True)
# Integrated cumulatively over the energy
dN_dEdVdt_integrated = np.zeros((len(energy),len(radius))) * u.cm**-3*u.s**-1
for i in range(len(energy)):
# Set out of limit values to 0
dN_dEdVdt_i = dN_dEdVdt.copy()
dN_dEdVdt_i[eng_grid < energy[i]] *= 0
# Compute integral
dN_dEdVdt_integrated[i,:] = model_tools.trapz_loglog(dN_dEdVdt_i, eng, axis=0)