-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprofiles.py
3911 lines (3067 loc) · 130 KB
/
profiles.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
####################### potential well classes ##########################
# Arthur Fangzhou Jiang (2016, HUJI) --- original version
# Arthur Fangzhou Jiang (2019, HUJI and UCSC) --- revisions
#
# - Dekel+ profile added some time earlier than 2019
# - support of velocity dispersion profile and thus dynamical friction
# (DF) for Dekel+ profile
# - improvement of DF implementation:
# 1. now compute the common part of the Chandrasekhar formula only once
# for each profile class (as opposed to three times in each of the
# force component .FR, .Fphi, and .Fz);
# 2. removed satelite-profile-dependent CoulombLogChoice, and now
# CoulombLogChoices only depend on satellite mass m and host
# potential
# Sheridan Beckwith Green (2020, Yale University) --- revisions
#
# - Added Green profile, which is an NFW profile multiplied by the
# transfer function from Green and van den Bosch (2019)
#########################################################################
import config as cfg # for global variables
import cosmo as co # for cosmology related functions
import warnings
import numpy as np
from scipy.optimize import brentq
from scipy.interpolate import InterpolatedUnivariateSpline
from scipy.integrate import quad,odeint
from scipy.special import erf,gamma,gammainc,gammaincc
#---variables for NFW mass definition change interpolation
x_interpolator = None
x_interpolator_min = None
x_interpolator_max = None
def gamma_lower(a,x):
"""
Non-normalized lower incomplete gamma function
integrate t^(a-1) exp(-t) from t=0 to t=x
Syntax:
gamma_lower(a,x)
"""
return gamma(a)*gammainc(a,x)
def gamma_upper(a,x):
"""
Non-normalized upper incomplete gamma function
integrate t^(a-1) exp(-t) from t=x to t=infty
Syntax:
gamma_upper(a,x)
"""
return gamma(a)*gammaincc(a,x)
#########################################################################
#---
class NFW(object):
"""
Class that implements the Navarro, Frenk, & White (1997) profile:
rho(R,z) = rho_crit * delta_char / [(r/r_s) * (1+r/r_s)^2]
= rho_0 / [(r/r_s) * (1+r/r_s)^2]
in a cylindrical frame (R,phi,z), where
r = sqrt(R^2 + z^2)
r_s: scale radius, at which d ln rho(r) / d ln(r) = -2
rho_crit: critical density of the Universe
delta_char = Delta_halo / 3 * c^3 / f(c), where c = R_vir / r_s
is the concentration parameter
Syntax:
halo = NFW(M,c,Delta=200.,z=0.,sf=1.)
where
M: halo mass [M_sun], where halo is defined as spherical
overdensity of Delta times critical density (float)
c: halo concentration (float)
Delta: average overdensity of the halo, in multiples of the
critical density of the Universe (float)
(default 200.)
z: redshift (float) (default 0.)
sf: Suppression factor used for reducing the overall
density of the halo while preserving its shape, used
when a disk is added in order to preserve total mass
of the system
Attributes:
.Mh: halo mass [M_sun]
.ch: halo concentration
.Deltah: spherical overdensity wrt instantaneous critical density
.z: redshift
.rhoc: critical density [M_sun kpc^-3]
.rhoh: average density of halo [M_sun kpc^-3]
.rh: halo radius within which density is Delta times rhoc [kpc]
.rs: scale radius [kpc]
.rmax: radius at which maximum circular velocity is reached [kpc]
.Vmax: maximum circular velocity [kpc/Gyr]
.s001: logarithmic density slope at 0.01 halo radius
Methods:
.rho(R,z=0.): density [M_sun kpc^-3] at radius r=sqrt(R^2+z^2)
.s(R,z=0.): logarithmic density slope at radius r=sqrt(R^2+z^2)
.M(R,z=0.): mass [M_sun] enclosed in radius r=sqrt(R^2+z^2)
.rhobar(R,z=0.): mean density [M_sun kpc^-3] within radius
r=sqrt(R^2+z^2)
.tdyn(R,z=0.): dyn. time [Gyr] within radius r = sqrt(R^2+z^2)
.Phi(R,z=0.): potential [(kpc/Gyr)^2] at radius r=sqrt(R^2+z^2)
.fgrav(R,z): grav. acceleration [(kpc/Gyr)^2 kpc^-1] at (R,z)
.Vcirc(R,z=0.): circ. vel. [kpc/Gyr] at radius r=sqrt(R^2+z^2)
.sigma(R,z=0.): vel. disp. [kpc/Gyr] at radius r=sqrt(R^2+z^2)
.d2Phidr2(R,z=0.): second radial derivative of potential [1/Gyr^2]
at radius r=sqrt(R^2+r^2)
HISTORY: Arthur Fangzhou Jiang (2016-10-24, HUJI)
Arthur Fangzhou Jiang (2016-10-30, HUJI)
Arthur Fangzhou Jiang (2019-08-24, HUJI)
"""
def __init__(self,M,c,Delta=200.,z=0.,sf=1.):
"""
Initialize NFW profile.
Syntax:
halo = NFW(M,c,Delta=200.,z=0.,sf=1.)
where
M: halo mass [M_sun] (float),
c: halo concentration (float),
Delta: spherical overdensity with respect to the critical
density of the universe (default is 200.)
z: redshift (float)
sf: Suppression factor used for reducing the overall
density of the halo while preserving its shape, used
when a disk is added in order to preserve total mass
of the system
"""
# input attributes
self.Mh = M
self.ch = c
self.Deltah = Delta
self.z = z
self.sf = sf
#
# derived attributes
self.rhoc = co.rhoc(z,cfg.h,cfg.Om,cfg.OL)
self.rhoh = self.Deltah * self.rhoc
self.rh = (3.*self.Mh / (cfg.FourPi*self.rhoh))**(1./3.)
self.rs = self.rh / self.ch
self.rmax = self.rs * 2.163
self.rho0 = self.sf*self.rhoc*self.Deltah/3.*self.ch**3./self.f(self.ch)
self.Phi0 = -cfg.FourPiG*self.rho0*self.rs**2.
self.Vmax = self.Vcirc(self.rmax)
self.s001 = self.s(0.01*self.rh)
def f(self,x):
"""
Auxiliary method for NFW profile: f(x) = ln(1+x) - x/(1+x)
Syntax:
.f(x)
where
x: dimensionless radius r/r_s (float or array)
"""
return np.log(1.+x) - x/(1.+x)
def rho(self,R,z=0.):
"""
Density [M_sun kpc^-3] at radius r = sqrt(R^2 + z^2).
Syntax:
.rho(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
return self.rho0 / (x * (1.+x)**2.)
def s(self,R,z=0.):
"""
Logarithmic density slope
- d ln rho / d ln r
at radius r = sqrt(R^2 + z^2).
Syntax:
.s(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
return 1. + 2*x / (1.+x)
def M(self,R,z=0.):
"""
Mass [M_sun] within radius r = sqrt(R^2 + z^2).
Syntax:
.M(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
return cfg.FourPi*self.rho0*self.rs**3. * self.f(x)
def rhobar(self,R,z=0.):
"""
Average density [M_sun kpc^-3] within radius r = sqrt(R^2 + z^2).
Syntax:
.rhobar(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
return 3.*self.rho0 * self.f(x)/x**3.
def tdyn(self,R,z=0.):
"""
Dynamical time [Gyr] within radius r = sqrt(R^2 + z^2).
Syntax:
.tdyn(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
return np.sqrt(cfg.ThreePiOverSixteenG / self.rhobar(R,z))
def Phi(self,R,z=0.):
"""
Potential [(kpc/Gyr)^2] at radius r = sqrt(R^2 + z^2).
Syntax:
.Phi(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
return self.Phi0 * np.log(1.+x)/x
def otherMassDefinition(self,Delta=200.):
"""
Computes the mass, radius, and concentration of the fixed,
physical halo under a new spherical overdensity definition.
Since rho0 is fixed, determines the cnew=rnew/rs that solves:
rho0 = [Delta * rhoc / 3] * (rnew/rs)**3 / f(rnew/rs)**3
Implementation based on Benedikt Diemer's COLOSSUS code.
Syntax:
.otherMassDefinition(Delta=200.)
where
Delta: Spherical overdensity in units of the critical
overdensity at the redshift that the halo was
initialized at (float).
Return:
Mnew: Mass within new overdensity (Msun, float)
rnew: Radius corresponding to new overdensity (kpc, float)
cnew: Concentration relative to new overdensity.
"""
global x_interpolator
global x_interpolator_min
global x_interpolator_max
if x_interpolator is None:
table_x = np.logspace(4.0, -4.0, 1000)
table_y = self.f(table_x) * 3.0 / table_x**3
x_interpolator = InterpolatedUnivariateSpline(table_y,
table_x, k=3)
knots = x_interpolator.get_knots()
x_interpolator_min = knots[0]
x_interpolator_max = knots[-1]
dens_threshold = Delta * self.rhoc
y = dens_threshold / self.rho0
if(y < x_interpolator_min):
raise Exception("Requested overdensity %.2e cannot be evaluated\
for scale density %.2e, out of range." \
% (y, x_interpolator_min))
elif(y > x_interpolator_max):
raise Exception("Requested overdensity %.2e cannot be evaluated\
for scale density %.2e, out of range." \
% (y, x_interpolator_max))
cnew = x_interpolator(y)
rnew = cnew * self.rs
Mnew = self.M(rnew)
return Mnew, rnew, cnew
def fgrav(self,R,z):
"""
gravitational acceleration [(kpc/Gyr)^2 kpc^-1] at location (R,z)
[- d Phi(R,z) / d R, 0, - d Phi(R,z) / d z]
Syntax:
.fgrav(R,z)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
Note that unlike the other methods, where z is optional with a
default of 0, here z must be specified.
Return:
R-component of gravitational acceleration
phi-component of gravitational acceleration
z-component of gravitational acceleration
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
fac = self.Phi0 * (self.f(x)/x) / r**2.
return fac*R, fac*0., fac*z
def Vcirc(self,R,z=0.):
"""
Circular velocity [kpc/Gyr] at radius r = sqrt(R^2 + z^2).
Syntax:
.Vcirc(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
return np.sqrt(r*-self.fgrav(r,0.)[0])
def sigma(self,R,z=0.):
"""
Velocity dispersion [kpc/Gyr] at radius r = sqrt(R^2 + z^2),
assuming isotropic velicity dispersion tensor, and following the
Zentner & Bullock (2003) fitting function:
sigma(x) = V_max 1.4393 x^0.345 / (1 + 1.1756 x^0.725)
where x = r/r_s.
Syntax:
.sigma(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
return self.Vmax*1.4393*x**0.354/(1.+1.1756*x**0.725)
def sigma_accurate(self,R,z=0.,beta=0.):
"""
Velocity dispersion [kpc/Gyr].
Syntax:
.sigma(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
beta: anisotropy parameter (default=0., i.e., isotropic)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
if isinstance(x,list) or isinstance(x,np.ndarray):
I = []
for xx in x:
II = quad(self.dIdx_sigma, xx, np.inf,args=(beta,))[0]
I.append(II)
I = np.array(I)
else:
I = quad(self.dIdx_sigma, x, np.inf,args=(beta,))[0]
f = self.f(x)
sigmasqr = -self.Phi0 / x**(2.*beta-1) *(1.+x)**2 * I
return np.sqrt(sigmasqr)
def dIdx_sigma(self,x,beta):
"""
Integrand for the integral in the velocity dispersion.
"""
f = self.f(x)
return x**(2.*beta-3.) * f / (1.+x)**2
def dlnsigmasqrdlnr_accurate(self,R,z=0.,beta=0.):
"""
d ln sigma^2 / d ln r
"""
r = np.sqrt(R**2.+z**2.)
r1 = r * (1.+cfg.eps)
r2 = r * (1.-cfg.eps)
y1 = np.log(self.sigma_accurate(r1))
y2 = np.log(self.sigma_accurate(r2))
return (y1-y2)/(r1-r2)
def d2Phidr2(self,R,z=0.):
"""
Second radial derivative of the gravitational potential [1/Gyr^2]
computed at r = sqrt(R^2 + z^2).
Syntax:
.d2Phidr2(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
f = (2.*np.log(1. + x) - x*(2. + 3.*x)/(1. + x)**2.) / r**3.
return self.Phi0 * self.rs * f
class Burkert(object):
"""
Class that implements the Burkert (1995) profile:
rho(R,z) = rho_0 / [(1+x)(1+x^2)], x = r/r_s
in a cylindrical frame (R,phi,z), where
r = sqrt(R^2 + z^2)
r_s: scale radius, at which d ln rho(r) / d ln(r) = -2
rho_crit: critical density of the Universe
delta_char = Delta_halo / 3 * c^3 / f(c), where c = R_vir / r_s
is the concentration parameter
Syntax:
halo = Burkert(M,c,Delta=200.,z=0.)
where
M: halo mass [M_sun], where halo is defined as spherical
overdensity of Delta times critical density (float)
c: Burkert concentration, R_vir/r_s (float)
Delta: average overdensity of the halo, in multiples of the
critical density of the Universe (float)
(default 200.)
z: redshift (float) (default 0.)
Attributes:
.Mh: halo mass [M_sun]
.ch: halo concentration
.Deltah: spherical overdensity wrt instantaneous critical density
.z: redshift
.rhoc: critical density [M_sun kpc^-3]
.rhoh: average density of halo [M_sun kpc^-3]
.rh: halo radius within which density is Delta times rhoc [kpc]
.rs: scale radius [kpc]
.rmax: radius at which maximum circular velocity is reached [kpc]
.rho0: central density [M_sun kpc^-3]
.Vmax: maximum circualr velocity [kpc/Gyr]
.s001: logarithmic density slope at 0.01 halo radius
Methods:
.rho(R,z=0.): density [M_sun kpc^-3] at radius r=sqrt(R^2+z^2)
.s(R,z=0.): logarithmic density slope at radius r=sqrt(R^2+z^2)
.M(R,z=0.): mass [M_sun] enclosed in radius r=sqrt(R^2+z^2)
.rhobar(R,z=0.): mean density [M_sun kpc^-3] within radius
r=sqrt(R^2+z^2)
.tdyn(R,z=0.): dyn. time [Gyr] within radius r = sqrt(R^2+z^2)
.Phi(R,z=0.): potential [(kpc/Gyr)^2] at radius r=sqrt(R^2+z^2)
.fgrav(R,z): grav. acceleration [(kpc/Gyr)^2 kpc^-1] at (R,z)
.Vcirc(R,z=0.): circ. vel. [kpc/Gyr] at radius r=sqrt(R^2+z^2)
.sigma(R,z=0.): vel. disp. [kpc/Gyr] at radius r=sqrt(R^2+z^2)
HISTORY: Arthur Fangzhou Jiang (2020-07-29, Caltech)
"""
def __init__(self,M,c,Delta=200.,z=0.):
"""
Initialize Burkert profile.
Syntax:
halo = Burkert(M,c,Delta=200.,z=0.)
where
M: halo mass [M_sun] (float),
c: halo concentration (float),
Delta: spherical overdensity with respect to the critical
density of the universe (default is 200.)
z: redshift (float)
"""
# input attributes
self.Mh = M
self.ch = c
self.Deltah = Delta
self.z = z
#
# derived attributes
self.rhoc = co.rhoc(z,cfg.h,cfg.Om,cfg.OL)
self.rhoh = self.Deltah * self.rhoc
self.rh = (3.*self.Mh / (cfg.FourPi*self.rhoh))**(1./3.)
self.rs = self.rh / self.ch
self.rho0=self.Mh/cfg.TwoPi/self.rh**3/self.f(self.ch)*self.ch**3
self.rmax = 3.24 * self.rs
self.Vmax = self.Vcirc(self.rmax)
self.s001 = self.s(0.01*self.rh)
def f(self,x):
"""
Auxiliary method for NFW profile:
f(x) = 0.5 ln(1+x^2) + ln(1+x) - arctan(x)
Syntax:
.f(x)
where
x: dimensionless radius r/r_s (float or array)
"""
return 0.5*np.log(1.+x**2)+np.log(1.+x)-np.arctan(x)
def rho(self,R,z=0.):
"""
Density [M_sun kpc^-3] at radius r = sqrt(R^2 + z^2).
Syntax:
.rho(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
return self.rho0 / ((1.+x) * (1.+x**2))
def s(self,R,z=0.):
"""
Logarithmic density slope
- d ln rho / d ln r
at radius r = sqrt(R^2 + z^2).
Syntax:
.s(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
xsqr = x**2
return x / (1.+x) + 2*xsqr / (1.+xsqr)
def M(self,R,z=0.):
"""
Mass [M_sun] within radius r = sqrt(R^2 + z^2).
Syntax:
.M(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
return cfg.TwoPi*self.rho0*self.rs**3 * self.f(x)
def rhobar(self,R,z=0.):
"""
Average density [M_sun kpc^-3] within radius r = sqrt(R^2 + z^2).
Syntax:
.rhobar(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
return self.M(r)/(cfg.FourPiOverThree*r**3)
def tdyn(self,R,z=0.):
"""
Dynamical time [Gyr] within radius r = sqrt(R^2 + z^2).
Syntax:
.tdyn(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
return np.sqrt(cfg.ThreePiOverSixteenG / self.rhobar(R,z))
def Phi(self,R,z=0.):
"""
Potential [(kpc/Gyr)^2] at radius r = sqrt(R^2 + z^2).
Syntax:
.Phi(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
return - np.pi*cfg.G*self.rho0*self.rs**2 /x * (-np.pi+\
2.*(1.+x)*np.arctan(1./x)+2.*(1.+x)*np.log(1.+x)+\
(1.-x)*np.log(1.+x**2))
def fgrav(self,R,z):
"""
gravitational acceleration [(kpc/Gyr)^2 kpc^-1] at location (R,z)
[- d Phi(R,z) / d R, 0, - d Phi(R,z) / d z]
Syntax:
.fgrav(R,z)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
Note that unlike the other methods, where z is optional with a
default of 0, here z must be specified.
Return:
R-component of gravitational acceleration
phi-component of gravitational acceleration
z-component of gravitational acceleration
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
fac = np.pi*cfg.G*self.rho0*self.rs/x**2 * \
(np.pi-2.*np.arctan(1./x)-2.*np.log(1.+x)-np.log(1.+x**2))/r
return fac*R, fac*0., fac*z
def Vcirc(self,R,z=0.):
"""
Circular velocity [kpc/Gyr] at radius r = sqrt(R^2 + z^2).
Syntax:
.Vcirc(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
return np.sqrt(r*-self.fgrav(r,0.)[0])
def sigma(self,R,z=0.):
"""
Velocity dispersion [kpc/Gyr] assuming isotropic velicity
dispersion tensor ...
Syntax:
.sigma(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
return self.Vmax * 0.299*np.exp(x**0.17) / (1.+0.286*x**0.797)
def sigma_accurate(self,R,z=0.,beta=0.):
"""
Velocity dispersion [kpc/Gyr].
Syntax:
.sigma(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
beta: anisotropy parameter (default=0., i.e., isotropic)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
if isinstance(x,list) or isinstance(x,np.ndarray):
I = []
for xx in x:
II = quad(self.dIdx_sigma, xx, np.inf,args=(beta,))[0]
I.append(II)
I = np.array(I)
else:
I = quad(self.dIdx_sigma, x, np.inf,args=(beta,))[0]
sigmasqr = cfg.TwoPiG*self.rho0*(1.+x)*(1.+x**2)*self.rs**2 / \
x**(2.*beta) * I
return np.sqrt(sigmasqr)
def dIdx_sigma(self,x,beta):
"""
Integrand for the integral in the velocity dispersion of Burkert.
"""
return (0.5*np.log(1.+x**2)+np.log(1.+x)-np.arctan(x))* \
x**(2.*beta-2.) / ((1.+x)* (1.+x**2))
class coreNFW(object):
"""
Class that implements the "coreNFW" profile (Read+2016):
M(r) = M_NFW(r) g(y)
rho(r) = rho_NFW(r) g(y) + [1-g(y)^2] M_NFW(r) / (4 pi r^2 r_c)
in a cylindrical frame (R,phi,z), where
r = sqrt(R^2 + z^2)
y = r / r_c with r_c a core radius, usually smaller than r_s
g(y) = tanh(y)
Syntax:
halo = coreNFW(M,c,rc,Delta=200.,z=0.)
where
M: halo mass [M_sun], where halo is defined as spherical
overdensity of Delta times critical density (float)
c: NFW halo concentration (float)
rc: core radius [kpc]
Delta: average overdensity of the halo, in multiples of the
critical density of the Universe (float)
(default 200.)
z: redshift (float) (default 0.)
Attributes:
.Mh: halo mass [M_sun]
.ch: halo concentration
.Deltah: spherical overdensity wrt instantaneous critical density
.z: redshift
.rhoc: critical density [M_sun kpc^-3]
.rhoh: average density of halo [M_sun kpc^-3]
.rh: halo radius within which density is Delta times rhoc [kpc]
.rs: scale radius [kpc]
.rc: core radius [kpc]
.rmax: radius at which maximum circular velocity is reached [kpc]
.Vmax: maximum circular velocity [kpc/Gyr]
.s001: logarithmic density slope at 0.01 halo radius
Methods:
.rho(R,z=0.): density [M_sun kpc^-3] at radius r=sqrt(R^2+z^2)
.s(R,z=0.): logarithmic density slope at radius r=sqrt(R^2+z^2)
.M(R,z=0.): mass [M_sun] enclosed in radius r=sqrt(R^2+z^2)
.rhobar(R,z=0.): mean density [M_sun kpc^-3] within radius
r=sqrt(R^2+z^2)
.tdyn(R,z=0.): dyn. time [Gyr] within radius r = sqrt(R^2+z^2)
.Phi(R,z=0.): potential [(kpc/Gyr)^2] at radius r=sqrt(R^2+z^2)
.fgrav(R,z): grav. acceleration [(kpc/Gyr)^2 kpc^-1] at (R,z)
.Vcirc(R,z=0.): circ. vel. [kpc/Gyr] at radius r=sqrt(R^2+z^2)
.sigma(R,z=0.): vel. disp. [kpc/Gyr] at radius r=sqrt(R^2+z^2)
HISTORY: Arthur Fangzhou Jiang (2021-03-11, Caltech)
"""
def __init__(self,M,c,rc,Delta=200.,z=0.):
"""
Initialize coreNFW profile.
Syntax:
halo = coreNFW(M,c,rc,Delta=200.,z=0.)
where
M: halo mass [M_sun] (float),
c: halo concentration (float),
rc: core radius [kpc]
Delta: spherical overdensity with respect to the critical
density of the universe (default is 200.)
z: redshift (float)
"""
# input attributes
self.Mh = M
self.ch = c
self.rc = rc
self.Deltah = Delta
self.z = z
#
# derived attributes
self.rhoc = co.rhoc(z,cfg.h,cfg.Om,cfg.OL)
self.rhoh = self.Deltah * self.rhoc
self.rh = (3.*self.Mh / (cfg.FourPi*self.rhoh))**(1./3.)
self.rs = self.rh / self.ch
self.xc = self.rc / self.rs
self.rmax = self.rs * 2.163 # accurate only if r_c < r_s
self.rho0 = self.rhoc*self.Deltah/3.*self.ch**3./self.f(self.ch)
self.Phi0 = -cfg.FourPiG*self.rho0*self.rs**2.
self.Vmax = self.Vcirc(self.rmax) # accurate only if r_c < r_s
self.s001 = self.s(0.01*self.rh)
def f(self,x):
"""
Auxiliary method for NFW profile: f(x) = ln(1+x) - x/(1+x)
Syntax:
.f(x)
where
x: dimensionless radius r/r_s (float or array)
"""
return np.log(1.+x) - x/(1.+x)
def g(self,y):
"""
Auxiliary method for coreNFW profile: f(y) = tanh(y)
Syntax:
.g(y)
where
y: dimensionless radius r/r_c (float or array)
"""
return np.tanh(y)
def rho(self,R,z=0.):
"""
Density [M_sun kpc^-3] at radius r = sqrt(R^2 + z^2).
Syntax:
.rho(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r / self.rs
y = r / self.rc
f = self.f(x)
g = self.g(y)
return self.rho0*(g/(x *(1.+x)**2)+(1.-g**2)*f/(self.xc*x**2.))
def s(self,R,z=0.):
"""
Logarithmic density slope
- d ln rho / d ln r
at radius r = sqrt(R^2 + z^2).
Syntax:
.s(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
r1 = r*(1.+cfg.eps)
r2 = r*(1.-cfg.eps)
rho1 = self.rho(r1)
rho2 = self.rho(r2)
return - np.log(rho1/rho2) / np.log(r1/r2)
def M(self,R,z=0.):
"""
Mass [M_sun] within radius r = sqrt(R^2 + z^2).
Syntax:
.M(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
x = r/self.rs
y = r/self.rc
return cfg.FourPi*self.rho0*self.rs**3. * self.f(x) * self.g(y)
def rhobar(self,R,z=0.):
"""
Average density [M_sun kpc^-3] within radius r = sqrt(R^2 + z^2).
Syntax:
.rhobar(R,z=0.)
where
R: R-coordinate [kpc] (float or array)
z: z-coordinate [kpc] (float or array)
(default=0., i.e., if z is not specified otherwise, the
first argument R is also the halo-centric radius r)
"""
r = np.sqrt(R**2.+z**2.)
return self.M(r)/(cfg.FourPiOverThree*r**3)
def tdyn(self,R,z=0.):
"""
Dynamical time [Gyr] within radius r = sqrt(R^2 + z^2).
Syntax:
.tdyn(R,z=0.)
where