-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhalo.py
1413 lines (1181 loc) · 53.9 KB
/
halo.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
from copy import copy
import cosmology
import defaults
import hod
import mass_function
import numpy
from scipy import integrate
from scipy import special
from scipy.interpolate import InterpolatedUnivariateSpline
"""Classes for describing a basic halo model.
Given an input HOD object and cosmological and halo parameters, a halo object
should be able to generate a non-linear power spectrum for dark matter,
galaxies or their cross-spectrum. It should also be able to return the halo
profile as a function of mass and radius and its Fourier transform as a
function of mass and wave-number.
"""
__author__ = ("Chris Morrison <[email protected]>"+
"Ryan Scranton <[email protected]>, ")
class Halo(object):
"""Basic halo model object.
Given an HOD object and cosmological and halo parameters, a halo object
should be able to generate a non-linear power spectrum for dark matter,
galaxies or their cross-spectrum. It should also be able to return
the halo profile as a function of mass and radius and its Fourier transform
as a function of mass and wave-number.
Attributes:
redshift: float redshift at which to compute the halo model
input_hod: HOD object from hod.py. Determines how galaxies populate
halos
cosmo_single_epoch: SingleEpoch object from cosmology.py
mass_func: MassFunction object from mass_function.py
halo_dict: dictionary of floats defining halo properties. (see
defaults.py for details)
"""
def __init__(self, redshift=0.0, input_hod=None, cosmo_single_epoch=None,
mass_func=None, halo_dict=None, extrapolate=False, **kws):
# Hard coded, but we shouldn't expect halos outside of this range.
self._k_min = defaults.default_limits['k_min']
self._k_max = defaults.default_limits['k_max']
ln_mass_min = numpy.log(1.0e9)
ln_mass_max = numpy.log(5.0e16)
self._ln_k_max = numpy.log(self._k_max)
self._ln_k_min = numpy.log(self._k_min)
self._ln_k_array = numpy.linspace(
self._ln_k_min, self._ln_k_max,
defaults.default_precision["halo_npoints"])
self._redshift = redshift
if cosmo_single_epoch is None:
cosmo_single_epoch = cosmology.SingleEpoch(redshift)
self.cosmo = cosmo_single_epoch
if halo_dict is None:
halo_dict = defaults.default_halo_dict
self.halo_dict = halo_dict
if mass_func is None:
mass_func = mass_function.MassFunction(
self._redshift, self.cosmo, self.halo_dict)
self.mass = mass_func
self.c0 = halo_dict["c0"]/(1.0 + self._redshift)
self.beta = halo_dict["beta"]
# If we hard-code to an NFW profile, then we can use an analytic
# form for the halo profile Fourier transform.
# self.alpha = -1.0*halo_dict.dpalpha
self.alpha = halo_dict["alpha"]
#self.cosmo = cosmology.SingleEpoch(self._redshift, cosmo_dict)
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self.rho_bar = self.cosmo.rho_bar()
self._h = self.cosmo._h
if input_hod is None:
input_hod = hod.HODZheng()
self.local_hod = input_hod
self._extrapolate = extrapolate
self._calculate_n_bar()
self._initialize_halo_splines()
self._initialized_y_spline = False
self._hold_ln_k = 1e10
self._initialized_h_m = False
self._initialized_h_g = False
self._initialized_pp_mm = False
self._initialized_pp_gm = False
self._initialized_pp_gg = False
self._initialized_gm_extrapolation = False
self._initialized_gg_extrapolation = False
def get_extrapolation(self):
"""
Return internal variable defining if the code extrapolates beyond k_min
and k_max.
"""
return self._extrapolate
def set_extrapolation(self, boolean):
"""
Set internal variable defining if the code extrapolates beyond k_min
and k_max.
Args:
boolean: boolean variable to set if extrapolation is performed.
"""
self._extrapolate = boolean
def get_cosmology(self):
"""
Return the internal cosmology dictionary.
"""
return self.cosmo.get_cosmology()
def get_cosmology_object(self):
"""
Return the internal cosmology object.
"""
return self.cosmo
def set_cosmology(self, cosmo_dict, redshift=None):
"""
Reset the internal cosmology to the values in cosmo_dict and
re-initialize the internal splines. Optimally reset the internal
redshift value.
Args:
cosmo_dict: dictionary of floats defining a cosmology. (see
defaults.py for details)
redshift: float redshift to compute halo model.
"""
if redshift==None:
redshift = self._redshift
self.cosmo_dict = cosmo_dict
self._redshift = redshift
self.cosmo = cosmology.SingleEpoch(redshift, cosmo_dict)
self.delta_v = self.halo_dict['delta_v']
if self.delta_v == -1:
self.delta_v = self.cosmo.delta_v()
self.rho_bar = self.cosmo.rho_bar()
self._h = self.cosmo._h
self.c0 = self.halo_dict["c0"]/(1.0 + redshift)
self.mass.set_cosmology_object(self.cosmo)
self._calculate_n_bar()
self._initialize_halo_splines()
self._initialized_y_spline = False
self._initialized_h_m = False
self._initialized_h_g = False
self._initialized_pp_mm = False
self._initialized_pp_gm = False
self._initialized_pp_gg = False
self._initialized_gm_extrapolation = False
self._initialized_gg_extrapolation = False
def get_hod(self, return_object=False):
return self.local_hod.get_hod()
def get_hod_object(self):
return self.local_hod
def set_hod(self, hod_dict):
self.local_hod.set_hod(hod_dict)
self._calculate_n_bar()
self._initialized_h_g = False
self._initialized_pp_gm = False
self._initialized_pp_gg = False
self._initialized_gm_extrapolation = False
self._initialized_gg_extrapolation = False
def set_hod_object(self, input_hod):
"""
Reset the internal HOD object to input_hod and re-initialize splines.
Args:
input_hod: a HOD object from hod.py defining the occupation
distribution of galaxies.
"""
self.local_hod = input_hod
self._calculate_n_bar()
self._initialized_h_g = False
self._initialized_pp_gm = False
self._initialized_pp_gg = False
self._initialized_gm_extrapolation = False
self._initialized_gg_extrapolation = False
def get_halo(self):
"""
Return the internal halo parameters.
"""
return self.halo_dict
def set_halo(self, halo_dict=None):
"""
Reset the internal halo properties to the values in halo_dict and
re-initialize the class splines.
Args:
halo_dict: dictionary of floats defining halo properties. (see
defaults.py for details)
"""
self.c0 = halo_dict["c0"]/(1.0 + self._redshift)
self.beta = halo_dict["beta"]
self.alpha = -1.0
self.mass.set_halo(halo_dict)
self._initialized_y_spline = False
self.set_hod_object(self.local_hod)
def get_mass(self):
"""
Return the internal object defining the mass function.
Returns:
MassFunction object
"""
return self.mass
def get_redshift(self):
"""
Return the internal redshift value.
Returns:
float redshift
"""
return self._redshift
def set_redshift(self, redshift):
"""
Reset the internal redshift to the value redshift and
re-initialize the class splines.
Args:
redshift: float value redshift at which to compute the halo model
"""
if redshift != self._redshift:
self.set_cosmology(self.cosmo.cosmo_dict, redshift)
def linear_power(self, k):
"""
Linear power spectrum from the cosmology module.
Args:
k [h/Mpc]: float array wave number
Returns:
float array linear power spectrum [Mpc/h]**3
"""
return self.cosmo.linear_power(k)
def power_mm(self, k):
"""
Non-Linear matter power spectrum derived from the halo model. This power
spectrum should be used for correlations that depend only on dark matter
(ie cosmo shear and magnification).
Args:
k [h/Mpc]: float array wave number
Returns:
float array non-linear matter power spectrum [Mpc/h]**3
"""
if not self._initialized_h_m:
self._initialize_h_m()
if not self._initialized_pp_mm:
self._initialize_pp_mm()
### first term in the this function is what is usually referred to as
### the 2_halo term (ie correltaions between different halos). The
### second term (_pp_mm) is referred to as the 1_halo or poisson term
### (ie correlations within a halo).
### Where statements so that we are able to input and return numpy
### as well as extrapolate where requested.
if self._extrapolate:
return numpy.where(
k < self._k_min, self.linear_power(k)*(
self._h_m(self._k_min)*self._h_m(self._k_min) +
self._pp_mm(self._k_min)/self.linear_power(self._k_min)),
numpy.where(
k < self._k_max,
(self.linear_power(k)*
self._h_m(k)*self._h_m(k) + self._pp_mm(k)),
self.linear_power(k)*(
self._h_m(self._k_max)*self._h_m(self._k_max) +
self._pp_mm(self._k_max)/
self.linear_power(self._k_max))))
else:
return numpy.where(
k < self._k_min, self.linear_power(k)*(
self._h_m(self._k_min)*self._h_m(self._k_min) +
self._pp_mm(self._k_min)/self.linear_power(self._k_min)),
numpy.where(k <= self._k_max,
self.linear_power(k)*self._h_m(k)*self._h_m(k) +
self._pp_mm(k), 0.0))
def power_gm(self, k):
"""
Non-Linear galaxy-matter cross power spectrum derived from the halo
model and input halo occupation distribution.This power spectrum should
be used for correlations that depend on the cross-correlation between
galaxies and dark matter.
(ie galaxy-galaxy shear and magnification).
Args:
k [h/Mpc]: float array wave number
Returns:
float array non-linear galaxy-matter power spectrum [Mpc/h]**3
"""
if not self._initialized_h_m:
self._initialize_h_m()
if not self._initialized_h_g:
self._initialize_h_g()
if not self._initialized_pp_gm:
self._initialize_pp_gm()
### Given the last 5 points of the spline, we extrapolate using the
### average splopes between these points.
if not self._initialized_gm_extrapolation and self._extrapolate:
k_array = numpy.exp(self._ln_k_array[-7:-1])
log_values = numpy.log(
self.linear_power(k_array)*
self._h_g(k_array)*self._h_m(k_array) + self._pp_gm(k_array))
self._log_slope_gm = numpy.mean(
(log_values[1:] - log_values[:-1])/
(self._ln_k_array[-6:-1] - self._ln_k_array[-7:-2]))
self._initialized_gm_extrapolation = True
### Where statements so that we are able to input and return numpy
### as well as extrapolate where requested.
if self._extrapolate:
return numpy.where(
k < self._k_min, self.linear_power(k)*(
self._h_g(self._k_min)*self._h_m(self._k_min) +
self._pp_gm(self._k_min)/self.linear_power(self._k_min)),
numpy.where(
k < self._k_max, (
self.linear_power(k)*
self._h_g(k)*self._h_m(k) + self._pp_gm(k)),
numpy.power(k/self._k_max, self._log_slope_gm)*(
self.linear_power(self._k_max)*
self._h_g(self._k_max)*self._h_m(self._k_max) +
self._pp_gm(self._k_max))))
else:
return numpy.where(
k < self._k_min, self.linear_power(k)*(
self._h_g(self._k_min)*self._h_m(self._k_min) +
self._pp_gm(self._k_min)/self.linear_power(self._k_min)),
numpy.where(k <= self._k_max,
self.linear_power(k)*self._h_g(k)*self._h_m(k) +
self._pp_gm(k), 0.0))
def power_mg(self, k):
"""
Non-Linear galaxy-matter cross power spectrum derived from the halo
model and input halo occupation distribution. This power spectrum
should be used for correlations between galaxies and matter.
themselves. (ie galaxy clustering/auto-correlations)
Args:
k [h/Mpc]: float array wave number
Returns:
float array non-linear galaxy-matter power spectrum [Mpc/h]**3
"""
return self.power_gm(k)
def power_gg(self, k):
"""
Non-Linear galaxy power spectrum derived from the halo
model and input halo occupation distribution.
Args:
k [h/Mpc]: float array wave number
Returns:
float array non-linear galaxy power spectrum [Mpc/h]**3
"""
if not self._initialized_h_g:
self._initialize_h_g()
if not self._initialized_pp_gg:
self._initialize_pp_gg()
### Given the last 5 points of the spline, we extrapolate using the
### average splopes between these points.
if not self._initialized_gg_extrapolation and self._extrapolate:
k_array = numpy.exp(self._ln_k_array[-7:-1])
log_values = numpy.log(
self.linear_power(k_array)*
self._h_g(k_array)*self._h_g(k_array) + self._pp_gg(k_array))
self._log_slope_gg = numpy.mean(
(log_values[1:] - log_values[:-1])/
(self._ln_k_array[-6:-1] - self._ln_k_array[-7:-2]))
self._initialized_gg_extrapolation = True
### Where statements so that we are able to input and return numpy
### as well as extrapolate where requested.
if self._extrapolate:
return numpy.where(
k < self._k_min, self.linear_power(k)*(
self._h_g(self._k_min)*self._h_g(self._k_min) +
self._pp_gg(self._k_min)/self.linear_power(self._k_min)),
numpy.where(
k < self._k_max, (
self.linear_power(k)*
self._h_g(k)*self._h_g(k) + self._pp_gg(k)),
numpy.power(k/self._k_max, self._log_slope_gg)*(
self.linear_power(self._k_max)*
self._h_g(self._k_max)*self._h_g(self._k_max) +
self._pp_gg(self._k_max))))
else:
return numpy.where(
k < self._k_min, self.linear_power(k)*(
self._h_g(self._k_min)*self._h_g(self._k_min) +
self._pp_gg(self._k_min)/self.linear_power(self._k_min)),
numpy.where(k <= self._k_max,
self.linear_power(k)*self._h_g(k)*self._h_g(k) +
self._pp_gg(k), 0.0))
def virial_radius(self, mass):
"""
Halo virial radius in Mpc as a function of mass in M_sun.
Args:
mass: float array of halo mass [M_solar/h]
Returns:
float array virial radius [Mpc/h]
"""
return numpy.exp(self._ln_r_v_spline(numpy.log(mass)))
def concentration(self, mass):
"""
Halo concentration as a function of mass in M_sun.
Functional form from Bullock et al.
Args:
mass: float array of halo mass [M_solar/h]
Returns:
float array halo concentration
"""
return numpy.exp(self._ln_concen_spline(numpy.log(mass)))
def halo_normalization(self, mass):
"""
Halo normalization in h^2*M_sun/Mpc^3 as a function of mass in M_sun.
Args:
mass: float array of halo mass [M_solar/h]
Returns:
float array halo normalization
"""
return numpy.exp(self._ln_halo_norm_spline(numpy.log(mass)))
def y(self, ln_k, mass):
"""
Wrapper function to return the fourier transform of the halo profile, y,
in both the general case and the specific case of alpha=-1.
Args:
ln_k: float value at which to compute the transform
mass: halo mass in M_solar/h at which to compute the transform
Returns:
a float value(array) of the halo fourier transform
"""
if self.alpha == -1.0:
return self.y_nfw(ln_k, mass)
return self.y_general(ln_k, mass)
def y_general(self, ln_k, mass):
if ln_k != self._hold_ln_k or not self._initialized_y_spline:
self._initialized_y_spline = False
self._initialize_y_spline(ln_k)
ln_mass = numpy.log(mass)
return numpy.where(numpy.logical_and(ln_mass >= self.mass.ln_mass_min,
ln_mass <= self.mass.ln_mass_max),
self._y_spline(ln_mass), 0.0)
def _initialize_y_spline(self, ln_k):
"""
Internal function to initialize the spline of y(M) for a given k value.
"""
self._y_array = numpy.empty_like(self.mass._ln_mass_array)
for idx, ln_mass in enumerate(self.mass._ln_mass_array):
mass = numpy.exp(ln_mass)
c = self.concentration(mass)
r_vir = self.virial_radius(mass)
norm = 1.0
if (numpy.fabs(numpy.sinc(numpy.exp(ln_k)*r_vir/(c*numpy.pi))) <=
1e-16):
norm = 1.0/self._y_integrand(1.0+numpy.pi/4.0, mass, ln_k, 1.0)
else:
norm = 1.0/self._y_integrand(1.0, mass, ln_k, 1.0)
tmp_y = integrate.romberg(
self._y_integrand, 1e-8, c, args=(mass, ln_k, norm),
vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"])/norm
self._y_array[idx] = (4.0*numpy.pi*tmp_y*(r_vir/c)**3*
self.halo_normalization(mass)/mass)
self._y_spline = InterpolatedUnivariateSpline(self.mass._ln_mass_array,
self._y_array)
self._hold_ln_k = ln_k
def _y_integrand(self, x, mass, ln_k, norm):
"""
Internal function for defingin
"""
k = numpy.exp(ln_k)
r_vir = self.virial_radius(mass)
c = self.concentration(mass)
r = x*r_vir/c
return (norm*x**2*self._halo_profile(x, mass)*
numpy.sinc(k*r/numpy.pi))
def _halo_profile(self, x, mass=None):
"""
Halo profile as a function of dimentionless units x where x=r/r_s and
is zero for x > c = r_virial/r_s. If other profiles are desired (e.g
such as accounting for baryonic gas mass) this profile, the function
_halo_normalization, the normalaization factors in _initialize_y_spline
should be modified.
Args:
x: unitless distance from center of halo. True distance is
x*r_vir/c where r_vir is the virial radius and c is the halo
concentration.
mass: mass of the halo in units M_solor/h
Returns:
float array of unormalized halo profile
"""
return x**self.alpha/(1.0 + x)**(3.0 + self.alpha)
def y_nfw(self, ln_k, mass):
"""Fourier transform of the halo profile.
Using an analytic expression for the Fourier transform from
White (2001). This is only valid for an NFW profile.
Args:
ln_k: float array natural log wave number [ln(Mpc/h)]
mass: float halo mass [M_solar/h]
Returns:
float array NFW profile Fourier transform
"""
k = numpy.exp(ln_k)
con = self.concentration(mass)
con_plus = 1.0 + con
z = k*self.virial_radius(mass)/con
si_z, ci_z = special.sici(z)
si_cz, ci_cz = special.sici(con_plus*z)
rho_km = (numpy.cos(z)*(ci_cz - ci_z) +
numpy.sin(z)*(si_cz - si_z) -
numpy.sin(con*z)/(con_plus*z))
mass_k = numpy.log(con_plus) - con/con_plus
return rho_km/mass_k
def write(self, output_file_name):
"""
Write out all halo model power spectra to a file.
Args:
output_file_name: string name of file to write to
"""
f = open(output_file_name, "w")
f.write("#ttype1 = k [Mpc/h]\n#ttype2 = linear_power [(Mpc/h)^3]\n"
"#ttype3 = power_mm\n#ttype4 = power_gg\n"
"#ttype5 = power_gm\n")
for ln_k in self._ln_k_array:
k = numpy.exp(ln_k)
f.write("%1.10f %1.10f %1.10f %1.10f %1.10f\n" % (
k, self.linear_power(k), self.power_mm(k),
self.power_gg(k), self.power_gm(k)))
f.close()
def write_halo(self, output_file_name, k=None):
"""
Write out halo properties as a functino of mass [Mpc/h].
Args:
output_file_name: string name of file to write to
"""
if k is None:
k = 0.01
ln_k = numpy.log(k)
f = open(output_file_name, "w")
f.write("#ttype1 = mass [M_solar/h]\n"
"#ttype2 = y(k, M), NFW Fourier Transform\n"
"#ttype3 = concentration\n#ttype4 = halo_norm\n"
"#ttype4 = halo_normalization"
"#ttype5 = virial_radius [M_solar/h]\n")
for nu in self.mass._nu_array:
mass = self.mass.mass(nu)
f.write("%1.10f %1.10f %1.10f %1.10f %1.10f\n" % (
mass, self.y(ln_k, mass), self.concentration(mass),
self.halo_normalization(mass), self.virial_radius(mass)))
f.close()
def write_power_components(self, output_file_name):
"""
Write out the individual components from the halo model [Mpc/h].
Args:
output_file_name: string name of file to write to
"""
f = open(output_file_name, "w")
f.write("#ttype1 = k [Mpc/h]\n#ttype2 = 2 halo dark matter component\n"
"#ttype3 = dark matter poisson component\n"
"#ttype4 = 2 halo galaxy component\n"
"#ttype5 = matter-galaxy poisson component\n"
"#ttype6 = galaxy-galaxy poisson component\n")
for ln_k in self._ln_k_array:
k = numpy.exp(ln_k)
f.write("%1.10f %1.10f %1.10f %1.10f %1.10f %1.10f\n" % (
k, self._h_m(k), self._pp_mm(k),
self._h_g(k), self._pp_gm(k), self._pp_gg(k)))
f.close()
def _h_m(self, k):
return numpy.where(
numpy.logical_and(k >= self._k_min, k <= self._k_max),
self._h_m_spline(numpy.log(k)), 0.0)
def _pp_mm(self, k):
return numpy.where(
numpy.logical_and(k >= self._k_min, k <= self._k_max),
self._pp_mm_spline(numpy.log(k)), 0.0)
def _pp_gm(self, k):
return numpy.where(
numpy.logical_and(k >= self._k_min, k <= self._k_max),
self._pp_gm_spline(numpy.log(k)), 0.0)
def _h_g(self, k):
return numpy.where(
numpy.logical_and(k >= self._k_min, k <= self._k_max),
self._h_g_spline(numpy.log(k)), 0.0)
def _pp_gg(self, k):
return numpy.where(
numpy.logical_and(k >= self._k_min, k <= self._k_max),
self._pp_gg_spline(numpy.log(k)), 0.0)
def _calculate_n_bar(self):
nu_min = self.mass.nu_min
if (self.local_hod.first_moment_zero > -1 and
self.local_hod.first_moment_zero >
numpy.exp(self.mass.ln_mass_min)):
nu_min = self.mass.nu(self.local_hod.first_moment_zero)
norm = 1.0
if (self.local_hod._safe_norm != -1 and
(self.local_hod._safe_norm > numpy.exp(self.mass.ln_mass_min) and
self.local_hod._safe_norm < numpy.exp(self.mass.ln_mass_max))):
ln_nu = numpy.log(self.mass.nu(self.local_hod._safe_norm))
inv_norm = self._nbar_integrand(ln_nu)
if inv_norm > 1e-16:
norm = 1.0/inv_norm
else:
norm = 1.0
self.n_bar_over_rho_bar = integrate.romberg(
self._nbar_integrand, numpy.log(nu_min),
numpy.log(self.mass.nu_max),
args=(norm,), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"])/norm
self.n_bar = self.n_bar_over_rho_bar*self.rho_bar
def _nbar_integrand(self, ln_nu, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
return (norm*nu*self.local_hod.first_moment(mass, z=self._redshift)*self.mass.f_nu(nu)/
mass)
def calculate_bias(self):
"""
Compute the average galaxy bias from the input HOD. The output value is
stored in the class variable bias.
Returns:
float bias
"""
nu_min = self.mass.nu_min
if (self.local_hod.first_moment_zero > -1 and
self.local_hod.first_moment_zero >
numpy.exp(self.mass.ln_mass_min)):
nu_min = self.mass.nu(self.local_hod.first_moment_zero)
norm = 1.0
if (self.local_hod._safe_norm != -1 and
(self.local_hod._safe_norm > numpy.exp(self.mass.ln_mass_min) and
self.local_hod._safe_norm < numpy.exp(self.mass.ln_mass_max))):
ln_nu = numpy.log(self.mass.nu(self.local_hod._safe_norm))
inv_norm = self._bias_integrand(ln_nu)
if inv_norm > 1e-16:
norm = 1.0/inv_norm
else:
norm = 1.0
bias = integrate.romberg(
self._bias_integrand, numpy.log(nu_min),
numpy.log(self.mass.nu_max),
args=(norm,), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"])
self.bias = bias/(norm*self.n_bar_over_rho_bar)
return self.bias
def _bias_integrand(self, ln_nu, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
return (norm*nu*self.local_hod.first_moment(mass, z=self._redshift)*self.mass.f_nu(nu)*
self.mass.bias_nu(nu)/mass)
def calculate_m_eff(self):
"""
Compute the effective Halo mass from the input HOD. The output value is
stored in the class variable m_eff.
Returns:
float m_eff
"""
nu_min = self.mass.nu_min
if (self.local_hod.first_moment_zero > -1 and
self.local_hod.first_moment_zero >
numpy.exp(self.mass.ln_mass_min)):
nu_min = self.mass.nu(self.local_hod.first_moment_zero)
norm = 1.0
if (self.local_hod._safe_norm != -1 and
(self.local_hod._safe_norm > numpy.exp(self.mass.ln_mass_min) and
self.local_hod._safe_norm < numpy.exp(self.mass.ln_mass_max))):
ln_nu = numpy.log(self.mass.nu(self.local_hod._safe_norm))
inv_norm = self._m_eff_integrand(ln_nu)
if inv_norm > 1e-16 and inv_norm is not numpy.nan:
norm = 1.0/inv_norm
else:
norm = 1.0
m_eff = integrate.romberg(
self._m_eff_integrand, numpy.log(nu_min),
numpy.log(self.mass.nu_max),
args=(norm,), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"])
self.m_eff = m_eff/(norm*self.n_bar_over_rho_bar)
return self.m_eff
def _m_eff_integrand(self, ln_nu, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
return (norm*nu*self.local_hod.first_moment(mass, z=self._redshift)*self.mass.f_nu(nu))
def calculate_f_sat(self):
"""
Compute the fraction of satellite galaxies relative to the total number.
This function requires the hod object to have a satellite_first_moment
method. Raises AttributeError if no such method is found. Stores the
result in the class variable f_sat.
Returns:
float f_sat
"""
nu_min = self.mass.nu_min
if (self.local_hod.second_moment_zero > -1 and
self.local_hod.second_moment_zero >
numpy.exp(self.mass.ln_mass_min)):
nu_min = self.mass.nu(self.local_hod.second_moment_zero)
norm = 1.0
if self.local_hod._safe_norm != -1:
ln_nu = numpy.log(self.mass.nu(self.local_hod._safe_norm))
inv_norm = self._f_sat_integrand(ln_nu)
if inv_norm > 1e-16:
norm = 1.0/inv_norm
else:
norm = 1.0
f_sat = integrate.romberg(
self._f_sat_integrand, numpy.log(nu_min),
numpy.log(self.mass.nu_max),
args=(norm,), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"])
self.f_sat = f_sat/(norm*self.n_bar_over_rho_bar)
return self.f_sat
def _f_sat_integrand(self, ln_nu, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
return (norm*nu*self.local_hod.satellite_first_moment(mass, z=self._redshift)*
self.mass.f_nu(nu)/mass)
def _initialize_halo_splines(self):
ln_r_v_array = numpy.zeros_like(self.mass._nu_array)
ln_concen_array = numpy.zeros_like(self.mass._nu_array)
ln_halo_norm_array = numpy.zeros_like(self.mass._nu_array)
for idx in xrange(self.mass._nu_array.size):
mass = numpy.exp(self.mass._ln_mass_array[idx])
ln_r_v_array[idx] = numpy.log(self._virial_radius(mass))
ln_concen_array[idx] = numpy.log(self._concentration(mass))
ln_halo_norm_array[idx] = numpy.log(self._halo_normalization(mass))
self._ln_r_v_spline = InterpolatedUnivariateSpline(
self.mass._ln_mass_array, ln_r_v_array)
self._ln_concen_spline = InterpolatedUnivariateSpline(
self.mass._ln_mass_array, ln_concen_array)
self._ln_halo_norm_spline = InterpolatedUnivariateSpline(
self.mass._ln_mass_array, ln_halo_norm_array)
# def _a_c(self, mass):
# """Formation epoch definition from Wechsler et al. 2002
# """
# a_c = 0.1*numpy.log10(mass)-0.9
# if a_c > 0.01:
# return 0.1*numpy.log10(mass)-0.9
# elif a_c <=0.01:
# return 0.01
# def _concentration(self, mass):
# """Halo concentration as a function of halo mass.
# Functional form from Wechsler et al. 2002
# """
# return 4.1/(self._a_c(mass)*(1+self._redshift))
def _concentration(self, mass):
"""Halo concentration as a function of halo mass.
Functional form from Bullock et al.
"""
return self.c0*(mass/self.mass.m_star)**self.beta
def _halo_normalization(self, mass):
"""Halo normalization as a function of mass.
The halo density profile is normalized such that the integral of the
density profile out to the virial radius equals the halo mass. This
ends up being the ratio between the halo mass and the integral
int(0, concentration, x**(2+alpha)/(1 + x)**(3+alpha))
which is a hypergeometric function of alpha and the concentration.
"""
con = self._concentration(mass)
rho_s = (self.rho_bar*self.delta_v*con*con*con)/3.0
rho_norm = (con**(3.0 + self.alpha)*
special.hyp2f1(3.0+self.alpha, 3.0+self.alpha,
4.0+self.alpha, -1.0*con))/(3.0+self.alpha)
return rho_s/rho_norm
def _virial_radius(self, mass):
"""Halo virial radius as a function of mass."""
r3 = 3.0*mass/(4.0*numpy.pi*self.delta_v*self.rho_bar)
return r3**(1.0/3.0)
def _initialize_h_m(self):
h_m_array = numpy.zeros_like(self._ln_k_array)
for idx, ln_k in enumerate(self._ln_k_array):
norm = 1.0/self._h_m_integrand(0.0, ln_k, 1.0)
h_m = integrate.romberg(
self._h_m_integrand, numpy.log(self.mass.nu_min),
numpy.log(self.mass.nu_max), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"],
args=(ln_k, norm))
h_m_array[idx] = h_m/norm
self._h_m_spline = InterpolatedUnivariateSpline(
self._ln_k_array, h_m_array)
self._initialized_h_m = True
def _h_m_integrand(self, ln_nu, ln_k, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
return (norm*nu*self.mass.f_nu(nu)*self.mass.bias_nu(nu)*
self.y(ln_k, mass))
def _initialize_h_g(self):
h_g_array = numpy.zeros_like(self._ln_k_array)
### To make the romberg intergration more effecient we use an internal
### HOD variable to tell the code the limit below which the selected
### moment is zero.
nu_min = self.mass.nu_min
if (self.local_hod.first_moment_zero > -1 and
self.local_hod.first_moment_zero >
numpy.exp(self.mass.ln_mass_min)):
nu_min = self.mass.nu(self.local_hod.first_moment_zero)
for idx, ln_k in enumerate(self._ln_k_array):
norm = 1.0
if self.local_hod._safe_norm != -1:
ln_nu = numpy.log(self.mass.nu(self.local_hod._safe_norm))
inv_norm = self._h_g_integrand(ln_nu, ln_k)
if inv_norm > 1e-16:
norm = 1.0/inv_norm
else:
norm = 1.0
h_g = integrate.romberg(
self._h_g_integrand, numpy.log(nu_min),
numpy.log(self.mass.nu_max), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"],
args=(ln_k, norm))/norm
h_g_array[idx] = h_g/self.n_bar_over_rho_bar
self._h_g_spline = InterpolatedUnivariateSpline(self._ln_k_array,
h_g_array)
self._initialized_h_g = True
def _h_g_integrand(self, ln_nu, ln_k, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
return (norm*nu*self.mass.f_nu(nu)*self.mass.bias_nu(nu)*
self.y(ln_k, mass)*self.local_hod.first_moment(mass, z=self._redshift)/mass)
def _initialize_pp_mm(self):
pp_mm_array = numpy.zeros_like(self._ln_k_array)
for idx, ln_k in enumerate(self._ln_k_array):
norm = 1.0/self._pp_mm_integrand(0.0, ln_k, 1.0)
pp_mm = integrate.romberg(
self._pp_mm_integrand, numpy.log(self.mass.nu_min),
numpy.log(self.mass.nu_max), vec_func=True,
tol=defaults.default_precision["global_precision"],
rtol=defaults.default_precision["halo_precision"],
divmax=defaults.default_precision["divmax"],
args=(ln_k, norm))
pp_mm_array[idx] = pp_mm/(norm*self.rho_bar)
self._pp_mm_spline = InterpolatedUnivariateSpline(
self._ln_k_array, pp_mm_array)
self._initialized_pp_mm = True
def _pp_mm_integrand(self, ln_nu, ln_k, norm=1.0):
nu = numpy.exp(ln_nu)
mass = self.mass.mass(nu)
y = self.y(ln_k, mass)
return norm*nu*self.mass.f_nu(nu)*mass*y*y
def _initialize_pp_gg(self):
pp_gg_array = numpy.zeros_like(self._ln_k_array)
### To make the romberg intergration more effecient we use an internal
### HOD variable to tell the code the limit below which the selected