-
Notifications
You must be signed in to change notification settings - Fork 0
/
halofit_ppf.f90
2942 lines (2152 loc) · 80 KB
/
halofit_ppf.f90
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
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! The `halofit' code models the nonlinear evolution of cold matter
! cosmological power spectra. The full details of the way in which
! this is done are presented in Smith et al. (2002), MNRAS, ?, ?.
!
! The code `halofit' was written by R. E. Smith & J. A. Peacock.
! See http://www.astro.upenn.edu/~res,
!
! Subsequent updates as below
! Only tested for basic models with power law initial power spectra
! References for variant versions are
! halofit_original: astro-ph/0207664
! halofit_peacock: http://www.roe.ac.uk/~jap/haloes/
! halofit_bird: arXiv: 1109.4416
! halofit_takahashi: arXiv: 1208.2701
! halofit_mead: arXiv:1505.07833,1602.02154
! Adapted for F90 and CAMB, AL March 2005
!!BR09 Oct 09: generalized expressions for om(z) and ol(z) to include w
! RT12 Oct: update some fitting parameters in the code to enhance
! the power spectrum at small scales (arXiv:1208.2701)
!!JD 08/13: generalized expressions for om(z) and ol(z) to include
! w_0 and w_a
! SPB14 Feb: update the fitting parameters for neutrinos to work with RT12
! modifications
! AL Sept 14: added halofit_version parameter to change approximation used;
! separate halofit.f90 is no longer needed as equations.f90 defined fixed wa_ppf
! Jan 15: Suggested change from Simeon Bird to avoid issues with very large Omm and neutrinos
!AM Mar 16: Added in HMcode
!AM May 16: Fixed some small bugs and added better neutrino approximations
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
module NonLinear
use ModelParams
use transfer
use LambdaGeneral
implicit none
private
real, parameter :: Min_kh_nonlinear = 0.005
real(dl):: om_m,om_v,fnu,omm0, acur
integer, parameter :: halofit_original = 1, halofit_bird=2, halofit_peacock=3, halofit_takahashi=4
integer, parameter :: halofit_mead=5, halofit_halomodel=6
integer, parameter :: halofit_default = halofit_takahashi
integer :: halofit_version = halofit_default
public Min_kh_nonlinear, NonLinear_GetNonLinRatios, NonLinear_ReadParams
public halofit_version, halofit_default, halofit_original, halofit_bird, halofit_peacock, halofit_takahashi
public halofit_mead, halofit_halomodel
!!AM - Added these types for HMcode
INTEGER :: imead, ihm !!AM - added these for HMcode, need to be visible to all subroutines and functions
TYPE HM_cosmology
!Contains only things that do not need to be recalculated with each new z
REAL :: om_m, om_v, w, wa, f_nu, ns, h, Tcmb, Nnu
REAL, ALLOCATABLE :: r_sigma(:), sigma(:)
REAL, ALLOCATABLE :: growth(:), a_growth(:)
REAL, ALLOCATABLE :: k_plin(:), plin(:), plinc(:)
END TYPE HM_cosmology
TYPE HM_tables
!Stuff that needs to be recalculated for each new z
REAL, ALLOCATABLE :: c(:), rv(:), nu(:), sig(:), zc(:), m(:), rr(:), sigf(:)
REAL :: sigv, sigv100, c3, knl, rnl, neff, sig8z
INTEGER :: n
END TYPE HM_tables
!!AM - End of my additions
contains
subroutine NonLinear_ReadParams(Ini)
use IniFile
Type(TIniFile) :: Ini
halofit_version = Ini_Read_Int_File(Ini, 'halofit_version', halofit_default)
end subroutine NonLinear_ReadParams
subroutine NonLinear_GetNonLinRatios(CAMB_Pk)
!Fill the CAMB_Pk%nonlin_scaling array with sqrt(non-linear power/linear power)
!for each redshift and wavenumber
!This implementation uses Halofit
type(MatterPowerData) :: CAMB_Pk
integer itf
real(dl) a,plin,pq,ph,pnl,rk
real(dl) sig,rknl,rneff,rncur,d1,d2
real(dl) diff,xlogr1,xlogr2,rmid
integer i
IF(halofit_version==halofit_mead .OR. halofit_version==halofit_halomodel) THEN
!AM - Call HMcode here
CALL HMcode(CAMB_Pk)
ELSE
!!BR09 putting neutrinos into the matter as well, not sure if this is correct, but at least one will get a consisent omk.
omm0 = CP%omegac+CP%omegab+CP%omegan
fnu = CP%omegan/omm0
CAMB_Pk%nonlin_ratio = 1
do itf = 1, CAMB_Pk%num_z
! calculate nonlinear wavenumber (rknl), effective spectral index (rneff) and
! curvature (rncur) of the power spectrum at the desired redshift, using method
! described in Smith et al (2002).
a = 1/real(1+CAMB_Pk%Redshifts(itf),dl)
om_m = omega_m(a, omm0, CP%omegav, w_lam,wa_ppf)
om_v = omega_v(a, omm0, CP%omegav, w_lam,wa_ppf)
acur = a
xlogr1=-2.0
xlogr2=3.5
do
rmid=(xlogr2+xlogr1)/2.0
rmid=10**rmid
call wint(CAMB_Pk,itf,rmid,sig,d1,d2)
diff=sig-1.0
if (abs(diff).le.0.001) then
rknl=1./rmid
rneff=-3-d1
rncur=-d2
exit
elseif (diff.gt.0.001) then
xlogr1=log10(rmid)
elseif (diff.lt.-0.001) then
xlogr2=log10(rmid)
endif
if (xlogr2 < -1.9999) then
!is still linear, exit
goto 101
else if (xlogr2>3.4999) then
! Totally crazy non-linear
global_error_flag=349
write(*,*) 'Error in halofit'
goto 101
end if
end do
! now calculate power spectra for a logarithmic range of wavenumbers (rk)
do i=1, CAMB_PK%num_k
rk = exp(CAMB_Pk%log_kh(i))
if (rk > Min_kh_nonlinear) then
! linear power spectrum !! Remeber => plin = k^3 * P(k) * constant
! constant = 4*pi*V/(2*pi)^3
plin= MatterPowerData_k(CAMB_PK, rk, itf)*(rk**3/(2*pi**2))
! calculate nonlinear power according to halofit: pnl = pq + ph,
! where pq represents the quasi-linear (halo-halo) power and
! where ph is represents the self-correlation halo term.
call halofit(rk,rneff,rncur,rknl,plin,pnl,pq,ph) ! halo fitting formula
CAMB_Pk%nonlin_ratio(i,itf) = sqrt(pnl/plin)
end if
enddo
101 continue
end do
END IF
end subroutine NonLinear_GetNonLinRatios
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subroutine halofit(rk,rn,rncur,rknl,plin,pnl,pq,ph)
real(dl) gam,a,b,c,xmu,xnu,alpha,beta,f1,f2,f3
real(dl) rk,rn,plin,pnl,pq,ph,plinaa
real(dl) rknl,y,rncur
real(dl) f1a,f2a,f3a,f1b,f2b,f3b,frac
real(dl) extragam, peacock_fudge
if (halofit_version ==halofit_original .or. halofit_version ==halofit_bird &
.or. halofit_version == halofit_peacock) then
! halo model nonlinear fitting formula as described in
! Appendix C of Smith et al. (2002)
!SPB11: Standard halofit underestimates the power on the smallest scales by a
!factor of two. Add an extra correction from the simulations in Bird, Viel,
!Haehnelt 2011 which partially accounts for this.
if (halofit_version ==halofit_bird) then
extragam = 0.3159 -0.0765*rn -0.8350*rncur
gam=extragam+0.86485+0.2989*rn+0.1631*rncur
else
gam=0.86485+0.2989*rn+0.1631*rncur
end if
a=1.4861+1.83693*rn+1.67618*rn*rn+0.7940*rn*rn*rn+ &
0.1670756*rn*rn*rn*rn-0.620695*rncur
a=10**a
b=10**(0.9463+0.9466*rn+0.3084*rn*rn-0.940*rncur)
c=10**(-0.2807+0.6669*rn+0.3214*rn*rn-0.0793*rncur)
xmu=10**(-3.54419+0.19086*rn)
xnu=10**(0.95897+1.2857*rn)
alpha=1.38848+0.3701*rn-0.1452*rn*rn
beta=0.8291+0.9854*rn+0.3400*rn**2+fnu*(-6.4868+1.4373*rn**2)
elseif (halofit_version == halofit_takahashi) then
!RT12 Oct: the halofit in Smith+ 2003 predicts a smaller power
!than latest N-body simulations at small scales.
!Update the following fitting parameters of gam,a,b,c,xmu,xnu,
!alpha & beta from the simulations in Takahashi+ 2012.
!The improved halofit accurately provide the power spectra for WMAP
!cosmological models with constant w.
gam=0.1971-0.0843*rn+0.8460*rncur
a=1.5222+2.8553*rn+2.3706*rn*rn+0.9903*rn*rn*rn+ &
0.2250*rn*rn*rn*rn-0.6038*rncur+0.1749*om_v*(1.+w_lam+wa_ppf*(1-acur))
a=10**a
b=10**(-0.5642+0.5864*rn+0.5716*rn*rn-1.5474*rncur+ &
0.2279*om_v*(1.+w_lam+wa_ppf*(1-acur)))
c=10**(0.3698+2.0404*rn+0.8161*rn*rn+0.5869*rncur)
xmu=0.
xnu=10**(5.2105+3.6902*rn)
alpha=abs(6.0835+1.3373*rn-0.1959*rn*rn-5.5274*rncur)
beta=2.0379-0.7354*rn+0.3157*rn**2+1.2490*rn**3+ &
0.3980*rn**4-0.1682*rncur + fnu*(1.081 + 0.395*rn**2)
else
call MpiStop('Unknown halofit_version')
end if
if(abs(1-om_m).gt.0.01) then ! omega evolution
f1a=om_m**(-0.0732)
f2a=om_m**(-0.1423)
f3a=om_m**(0.0725)
f1b=om_m**(-0.0307)
f2b=om_m**(-0.0585)
f3b=om_m**(0.0743)
frac=om_v/(1.-om_m)
f1=frac*f1b + (1-frac)*f1a
f2=frac*f2b + (1-frac)*f2a
f3=frac*f3b + (1-frac)*f3a
else
f1=1.0
f2=1.
f3=1.
endif
y=(rk/rknl)
ph=a*y**(f1*3)/(1+b*y**(f2)+(f3*c*y)**(3-gam))
ph=ph/(1+xmu*y**(-1)+xnu*y**(-2))*(1+fnu*0.977)
plinaa=plin*(1+fnu*47.48*rk**2/(1+1.5*rk**2))
pq=plin*(1+plinaa)**beta/(1+plinaa*alpha)*exp(-y/4.0-y**2/8.0)
pnl=pq+ph
if (halofit_version == halofit_peacock) then
!From http://www.roe.ac.uk/~jap/haloes/
!(P-P_linear) -> (P-P_linear) * (1+2y^2)/(1+y^2), where y = k/10 h Mpc^(-1).
peacock_fudge = rk/10
pnl = plin + (pnl-plin)*(1+2*peacock_fudge**2)/(1+peacock_fudge**2)
end if
end subroutine halofit
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! The subroutine wint, finds the effective spectral quantities
! rknl, rneff & rncur. This it does by calculating the radius of
! the Gaussian filter at which the variance is unity = rknl.
! rneff is defined as the first derivative of the variance, calculated
! at the nonlinear wavenumber and similarly the rncur is the second
! derivative at the nonlinear wavenumber.
subroutine wint(CAMB_Pk,itf,r,sig,d1,d2)
integer, intent(in) :: itf
type(MatterPowerData) :: CAMB_Pk
real(dl) sum1,sum2,sum3,t,y,x,w1,w2,w3
real(dl) x2,rk, fac,r, sig, d1,d2, anorm
integer i,nint
nint=3000
sum1=0.d0
sum2=0.d0
sum3=0.d0
anorm = 1/(2*pi**2)
do i=1,nint
t=(i-0.5_dl)/nint
y=-1.d0+1.d0/t
rk=y
d2=MatterPowerData_k(CAMB_PK, rk, itf)*(rk**3*anorm)
x=y*r
x2=x*x
w1=exp(-x2)
w2=2*x2*w1
w3=4*x2*(1-x2)*w1
fac=d2/y/t/t
sum1=sum1+w1*fac
sum2=sum2+w2*fac
sum3=sum3+w3*fac
enddo
sum1=sum1/nint
sum2=sum2/nint
sum3=sum3/nint
sig=sqrt(sum1)
d1=-sum2/sum1
d2=-sum2*sum2/sum1/sum1 - sum3/sum1
end subroutine wint
!!JD 08/13 generalize to variable w
function omega_m(aa,om_m0,om_v0,wval,waval)
real(dl) omega_m,omega_t,om_m0,om_v0,aa,wval,waval,Qa2
Qa2= aa**(-1.0-3.0*(wval+waval))*dexp(-3.0*(1-aa)*waval)
omega_t=1.0+(om_m0+om_v0-1.0)/(1-om_m0-om_v0+om_v0*Qa2+om_m0/aa)
omega_m=omega_t*om_m0/(om_m0+om_v0*aa*Qa2)
end function omega_m
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
! evolution of omega lambda with expansion factor
function omega_v(aa,om_m0,om_v0,wval,waval)
real(dl) aa,omega_v,om_m0,om_v0,omega_t,wval,waval,Qa2
Qa2= aa**(-1.0-3.0*(wval+waval))*dexp(-3.0*(1-aa)*waval)
omega_t=1.0+(om_m0+om_v0-1.0)/(1-om_m0-om_v0+om_v0*Qa2+om_m0/aa)
omega_v=omega_t*om_v0*Qa2/(om_v0*Qa2+om_m0/aa)
end function omega_v
!!JD end generalize to variable w
!!AM Below is for HMcode
SUBROUTINE HMcode(CAMB_Pk)
!!AM - A CAMB derived type that I need
TYPE(MatterPowerData) :: CAMB_Pk
REAL :: z, k
REAL :: p1h, p2h, pfull, plin
INTEGER :: i, j, nk, nz
REAL, PARAMETER :: pi=3.141592654
TYPE(HM_cosmology) :: cosi
TYPE(HM_tables) :: lut
!HMcode developed by Alexander Mead ([email protected])
!Please contact me if you have any questions whatsoever
!If you use this in your work please cite the original paper: http://arxiv.org/abs/1505.07833
!If you use the extensions (w(a) and massive neutrinos) then please cite: http://arxiv.org/abs/1602.02154
!Also consider citing the source code at ASCL: http://ascl.net/1508.001
!Use imead to switch between the standard and accurate halo-model calcuation
!0 - Standard (this is just a vanilla halo model calculation with no accuracy tweaks)
!1 - Accurate from Mead et al. (2015; arXiv 1505.07833)
IF(halofit_version==halofit_halomodel) imead=0
IF(halofit_version==halofit_mead) imead=1
!Use ihm to switch between verbose (diagnostic) and non-verbose mode
!0 - Non-verbose
!1 - Verbose
ihm=0
IF(ihm==1) WRITE(*,*)
IF(ihm==1) WRITE(*,*) 'Running HMcode'
IF(ihm==1) WRITE(*,*)
!!AM - Translate from CAMB variables to my variables
nz=CAMB_PK%num_z
nk=CAMB_PK%num_k
!!AM - Assign cosmological parameters for the halo model calculation
CALL assign_HM_cosmology(cosi)
!Fill growth function table (only needs to be done once)
CALL fill_growtab(cosi)
!Loop over redshifts
DO j=1,nz
!Initialise the specific HM_cosmology (fill sigma(R) and P_lin HM_tables)
!Currently this needs to be done at each z (mainly because of scale-dependent growth with neutrinos)
!For non neutrino models this could only be done once, which would speed things up a bit
CALL initialise_HM_cosmology(j,cosi,CAMB_PK)
!Sets the current redshift from the table
z=CAMB_Pk%Redshifts(j)
!Initiliasation for the halomodel calcualtion (needs to be done for each z)
CALL halomod_init(z,lut,cosi)
!Loop over k values and calculate P(k)
DO i=1,nk
k=exp(CAMB_Pk%log_kh(i))
plin=p_lin(k,z,0,cosi)
CALL halomod(k,z,p1h,p2h,pfull,plin,lut,cosi)
CAMB_Pk%nonlin_ratio(i,j)=sqrt(pfull/plin)
END DO
END DO
END SUBROUTINE HMcode
FUNCTION Delta_v(z,lut,cosm)
!Funciton for the virialised overdensity
REAL :: Delta_v
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(imead==0) THEN
!Value that is normally used in halo model predictions
Delta_v=200.
ELSE IF(imead==1) THEN
!Mead et al. (2015; arXiv 1505.07833) value
Delta_v=418.*(omega_m_hm(z,cosm)**(-0.352))
!Mead et al. (2016; arXiv 1602.02154) neutrino addition
Delta_v=Delta_v*(1.+0.916*cosm%f_nu)
END IF
END FUNCTION Delta_v
FUNCTION delta_c(z,lut,cosm)
!Function for the linear collapse density
REAL :: delta_c
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(imead==0) THEN
delta_c=1.686
ELSE IF(imead==1) THEN
!Mead et al. (2015; arXiv 1505.07833) value
delta_c=1.59+0.0314*log(lut%sig8z)
!Mead et al. (2016; arXiv 1602.02154) neutrino addition
delta_c=delta_c*(1.+0.262*cosm%f_nu)
END IF
!Nakamura & Suto (1997) fitting formula for LCDM models
delta_c=delta_c*(1.+0.0123*log10(omega_m_hm(z,cosm)))
END FUNCTION delta_c
FUNCTION eta(z,lut,cosm)
!Function eta that puffs halo profiles
REAL :: eta
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(imead==0) THEN
eta=0.
ELSE IF(imead==1) THEN
!The first parameter here is 'eta_0' in Mead et al. (2015; arXiv 1505.07833)
eta=0.603-0.3*lut%sig8z
END IF
END FUNCTION eta
FUNCTION kstar(z,lut,cosm)
!Function k* that cuts off the 1-halo term at large scales
REAL :: kstar
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(imead==0) THEN
!Set to zero for the standard Poisson one-halo term
kstar=0.
ELSE IF(imead==1) THEN
!One-halo cut-off wavenumber
!Mead et al. (2015; arXiv 1505.07833) value
kstar=0.584*(lut%sigv)**(-1.)
END IF
END FUNCTION kstar
FUNCTION As(z,lut,cosm)
!Halo concentration pre-factor from Bullock et al. (2001) relation
REAL :: As
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(imead==0) THEN
!Set to 4 for the standard Bullock value
As=4.
ELSE IF(imead==1) THEN
!This is the 'A' halo-concentration parameter in Mead et al. (2015; arXiv 1505.07833)
As=3.13
END IF
END FUNCTION As
FUNCTION fdamp(z,lut,cosm)
!Linear power damping function from Mead et al. (2015; arXiv 1505.07833)
REAL ::fdamp
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
!Linear theory damping factor
IF(imead==0) THEN
!Set to 0 for the standard linear theory two halo term
fdamp=0.
ELSE IF(imead==1) THEN
!Mead et al. (2016; arXiv 1602.02154) value
fdamp=0.0095*lut%sigv100**1.37
END IF
!Catches extreme values of fdamp
IF(fdamp<1.e-3) fdamp=1.e-3
IF(fdamp>0.99) fdamp=0.99
END FUNCTION fdamp
FUNCTION alpha(z,lut,cosm)
!Two- to one-halo transition smoothing from Mead et al. (2015; arXiv 1505.07833)
REAL :: alpha
REAL, INTENT(IN) :: z
TYPE(HM_tables), INTENT(IN) :: lut
TYPE(HM_cosmology), INTENT(IN) :: cosm
IF(imead==0) THEN
!Set to 1 for the standard halomodel sum of one- and two-halo terms
alpha=1.
ELSE IF(imead==1) THEN
!This uses the top-hat defined neff (HALOFIT uses Gaussian filtered fields instead)
!Mead et al. (2016; arXiv 1602.02154) value
alpha=3.24*1.85**lut%neff
END IF
!Catches values of alpha that are crazy
IF(alpha>2.) alpha=2.
IF(alpha<0.5) alpha=0.5
END FUNCTION alpha
FUNCTION r_nl(lut)
!Calculates R_nl, defined by nu(R_nl)=1., nu=dc/sigma(R)
TYPE(HM_tables), INTENT(IN) :: lut
REAL :: r_nl
IF(lut%nu(1)>1.) THEN
!This catches some very strange values that appear for odd cosmological models
r_nl=lut%rr(1)
ELSE
r_nl=exp(find(log(1.),log(lut%nu),log(lut%rr),3,3))
END IF
END FUNCTION r_nl
SUBROUTINE halomod(k,z,p1h,p2h,pfull,plin,lut,cosm)
!Calcuates 1-halo and 2-halo terms and combines them to form the full halomodel power
REAL, INTENT(OUT) :: p1h, p2h, pfull
REAL, INTENT(IN) :: plin, k, z
REAL :: a
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
!Calls expressions for one- and two-halo terms and then combines
!to form the full power spectrum
IF(k==0.) THEN
p1h=0.
p2h=0.
ELSE
p1h=p_1h(k,z,lut,cosm)
p2h=p_2h(k,z,plin,lut,cosm)
END IF
a=alpha(z,lut,cosm)
pfull=(p2h**a+p1h**a)**(1./a)
END SUBROUTINE halomod
SUBROUTINE fill_table(min,max,arr,n,ilog)
!Fills array 'arr' in equally spaced intervals
INTEGER :: i
REAL, INTENT(IN) :: min, max
REAL :: a, b
REAL, ALLOCATABLE, INTENT(OUT) :: arr(:)
INTEGER, INTENT(IN) :: ilog, n
!ilog=0 does linear spacing
!ilog=1 does log spacing
!Allocate arrays
IF(ALLOCATED(arr)) DEALLOCATE(arr)
ALLOCATE(arr(n))
!This is probably unnecessary
arr=0.
!Decide on linear or log spacing
IF(ilog==0) THEN
a=min
b=max
ELSE IF(ilog==1) THEN
a=log(min)
b=log(max)
END IF
!Fill the array
IF(n==1) THEN
!This should not be necessary
arr(1)=a
ELSE IF(n>1) THEN
DO i=1,n
arr(i)=a+(b-a)*float(i-1)/float(n-1)
END DO
END IF
!If your are filling in log
IF(ilog==1) arr=exp(arr)
END SUBROUTINE fill_table
SUBROUTINE fill_plintab(iz,cosm,CAMB_PK)
!Fills internal HMcode HM_tables for the linear power spectrum at z=0
TYPE(MatterPowerData), INTENT(IN) :: CAMB_PK
INTEGER, INTENT(IN) :: iz
TYPE(HM_cosmology) :: cosm
INTEGER :: i, nk
INTEGER :: imeth
REAL :: z, g, k, kmin, kmax
REAL, PARAMETER :: pi=3.141592654
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: Filling linear power HM_tables'
!Fill arrays
IF(ALLOCATED(cosm%k_plin)) DEALLOCATE(cosm%k_plin)
IF(ALLOCATED(cosm%plin)) DEALLOCATE(cosm%plin)
IF(ALLOCATED(cosm%plinc)) DEALLOCATE(cosm%plinc)
imeth=2
IF(imeth==1) THEN
!Fill k-table with the same k points as in the CAMB calculation
!If a user has specified lots of points this could make the halo-model
!calculation chug
nk=CAMB_PK%num_k
ALLOCATE(cosm%k_plin(nk))
DO i=1,nk
cosm%k_plin(i)=exp(CAMB_Pk%log_kh(i))
END DO
ELSE IF(imeth==2) THEN
!Fill a k-table with an equal-log-spaced k range
!Note that the minimum should be such that the spectrum is accurately a power-law below this wavenumber
kmin=1e-3
kmax=1e2
nk=128
CALL fill_table(kmin,kmax,cosm%k_plin,nk,1)
END IF
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: k_min:', cosm%k_plin(1)
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: k_max:', cosm%k_plin(nk)
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: nk:', nk
ALLOCATE(cosm%plin(nk),cosm%plinc(nk))
!Fill power table
DO i=1,nk
!Take the power from the current redshift choice
cosm%plin(i)=MatterPowerData_k(CAMB_PK,DBLE(cosm%k_plin(i)),iz)*(cosm%k_plin(i)**3/(2.*pi**2))
cosm%plinc(i)=cosm%plin(i)*(Tcb_Tcbnu_ratio(cosm%k_plin(i),z,cosm))**2.
END DO
!Find the redshift
z=CAMB_Pk%Redshifts(iz)
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: z of input:', z
!Calculate the growth factor at the redshift of interest
g=grow(z,cosm)
!Grow the power to z=0
cosm%plin=cosm%plin/(g**2.)
cosm%plinc=cosm%plinc/(g**2.)
!Check sigma_8 value
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: Sigma_8:', sigma(8.,0.,0,cosm)
IF(ihm==1) WRITE(*,*) 'LINEAR POWER: Done'
IF(ihm==1) WRITE(*,*)
END SUBROUTINE fill_plintab
FUNCTION Tcb_Tcbnu_ratio(k,z,cosm)
!Calculates the ratio of T(k) for cold vs. all matter
!Uses approximations in Eisenstein & Hu (1999; arXiv 9710252)
!Note that this assumes that there are exactly 3 species of neutrinos with
!Nnu<=3 of these being massive, and with the mass split evenly between the number of massive species.
REAL :: Tcb_Tcbnu_ratio
REAL, INTENT(IN) :: k, z
REAL :: D, Dcb, Dcbnu, pcb, zeq, q, yfs
REAL :: BigT
TYPE(HM_cosmology) :: cosm
IF(cosm%f_nu==0.) THEN
Tcb_Tcbnu_ratio=1.
ELSE
!Growth exponent under the assumption that neutrinos are completely unclustered (equation 11)
pcb=(5.-sqrt(1.+24.*(1.-cosm%f_nu)))/4.
!Theta for temperature (BigT=T/2.7 K)
BigT=cosm%Tcmb/2.7
!The matter-radiation equality redshift
zeq=(2.5e4)*cosm%om_m*(cosm%h**2.)*(BigT**(-4.))
!The growth function normalised such that D=(1.+z_eq)/(1+z) at early times (when Omega_m \approx 1)
!For my purpose (just the ratio) seems to work better using the EdS growth function result, \propto a .
!In any case, can't use grow at the moment because that is normalised by default.
!D=(1.+zeq)*grow(z,cosm)
D=(1.+zeq)/(1.+z)
!Wave number relative to the horizon scale at equality (equation 5)
!Extra factor of h becauase all my k are in units of h/Mpc
q=k*cosm%h*BigT**2./(cosm%om_m*cosm%h**2.)
!Free streaming scale (equation 14)
!Note that Eisenstein & Hu (1999) only consider the case of 3 neutrinos
!with Nnu of these being massve with the mass split evenly between Nnu species.
yfs=17.2*cosm%f_nu*(1.+0.488*cosm%f_nu**(-7./6.))*(cosm%Nnu*q/cosm%f_nu)**2.
!These are (almost) the scale-dependent growth functions for each component in Eisenstein & Hu (1999)
!Some part is missing, but this cancels when they are divided by each other, which is all I need them for.
!Equations (12) and (13)
Dcb=(1.+(D/(1.+yfs))**0.7)**(pcb/0.7)
Dcbnu=((1.-cosm%f_nu)**(0.7/pcb)+(D/(1.+yfs))**0.7)**(pcb/0.7)
Tcb_Tcbnu_ratio=Dcb/Dcbnu
END IF
END FUNCTION Tcb_Tcbnu_ratio
SUBROUTINE assign_HM_cosmology(cosm)
!Assigns the internal HMcode cosmological parameters
TYPE(HM_cosmology) :: cosm
!Converts CAMB parameters to Meadfit parameters
cosm%om_m=CP%omegac+CP%omegab+CP%omegan
cosm%om_v=CP%omegav
cosm%w=w_lam
cosm%wa=wa_ppf
cosm%f_nu=CP%omegan/cosm%om_m
cosm%h=CP%H0/100.
cosm%Tcmb=CP%tcmb
cosm%Nnu=CP%Num_Nu_massive
!n_s is read in here. The non-linear CAMB module does not work if there is more than
!one value in this array, so explicitly setting '1' here is fine.
cosm%ns=CP%InitPower%an(1)
!Write out cosmological parameters if necessary
IF(ihm==1) WRITE(*,*) 'HM_cosmology: Om_m:', cosm%om_m
IF(ihm==1) WRITE(*,*) 'HM_cosmology: Om_v:', cosm%om_v
IF(ihm==1) WRITE(*,*) 'HM_cosmology: w_0:', cosm%w
IF(ihm==1) WRITE(*,*) 'HM_cosmology: w_a:', cosm%wa
IF(ihm==1) WRITE(*,*) 'HM_cosmology: f_nu:', cosm%f_nu
IF(ihm==1) WRITE(*,*) 'HM_cosmology: n_s:', cosm%ns
IF(ihm==1) WRITE(*,*) 'HM_cosmology: h:', cosm%h
IF(ihm==1) WRITE(*,*) 'HM_cosmology: T_cmb:', cosm%Tcmb
IF(ihm==1) WRITE(*,*) 'HM_cosmology: N_nu (massive):', cosm%Nnu
IF(ihm==1) WRITE(*,*)
END SUBROUTINE assign_HM_cosmology
SUBROUTINE initialise_HM_cosmology(iz,cosm,CAMB_PK)
!Sets up HM_tables of sigma, growth and linear power for the HM_cosmology
TYPE(MatterPowerData), INTENT(IN) :: CAMB_PK
TYPE(HM_cosmology) :: cosm
INTEGER, INTENT(IN) :: iz
!Fill linear power table and grows it to z=0
CALL fill_plintab(iz,cosm,CAMB_PK)
!Fill sigma(r) table
CALL fill_sigtab(cosm)
END SUBROUTINE initialise_HM_cosmology
SUBROUTINE allocate_LUT(lut)
!Allocates memory for the HMcode look-up HM_tables
TYPE(HM_tables) :: lut
INTEGER :: n
n=lut%n
ALLOCATE(lut%zc(n),lut%m(n),lut%c(n),lut%rv(n))
ALLOCATE(lut%nu(n),lut%rr(n),lut%sigf(n),lut%sig(n))
lut%zc=0.
lut%m=0.
lut%c=0.
lut%rv=0.
lut%nu=0.
lut%rr=0.
lut%sigf=0.
lut%sig=0.
END SUBROUTINE allocate_LUT
SUBROUTINE deallocate_LUT(lut)
!Deallocates HMcode look-up HM_tables
TYPE(HM_tables) :: lut
DEALLOCATE(lut%zc,lut%m,lut%c,lut%rv,lut%nu,lut%rr,lut%sigf,lut%sig)
END SUBROUTINE deallocate_LUT
SUBROUTINE halomod_init(z,lut,cosm)
!Halo-model initialisation routine
!Computes look-up HM_tables necessary for the halo model calculations
REAL, INTENT(IN) :: z
INTEGER :: i, n
REAL :: Dv, dc, f, m, mmin, mmax, nu, r, sig
TYPE(HM_cosmology) :: cosm
TYPE(HM_tables) :: lut
IF(ihm==1) WRITE(*,*) 'HALOMOD: Filling look-up HM_tables'
IF(ihm==1) WRITE(*,*) 'HALOMOD: HM_tables being filled at redshift:', z
!Find value of sigma_v, sig8, etc.
lut%sigv=sqrt(dispint(z,cosm))
IF(ihm==1) WRITE(*,*) 'HALOMOD: sigv [Mpc/h]:', lut%sigv
lut%sigv100=sigma_v(100.,z,cosm)
IF(ihm==1) WRITE(*,*) 'HALOMOD: sigv100 [Mpc/h]:', lut%sigv100
lut%sig8z=sigma(8.,z,0,cosm)
IF(ihm==1) WRITE(*,*) 'HALOMOD: sig8(z):', lut%sig8z
IF(ALLOCATED(lut%rr)) CALL deallocate_LUT(lut)
!Number of entries in look-up HM_tables. Could be played with to improve speed or accuracy
n=256
lut%n=n
CALL allocate_lut(lut)
!Sets the mass range for halo model calculation
!Default is 1e0 to 1e18, I cannot believe this would ever be insufficient
mmin=1.e0
mmax=1.e18
IF(ihm==1) WRITE(*,*) 'HALOMOD: M_min:', mmin
IF(ihm==1) WRITE(*,*) 'HALOMOD: M_max:', mmax
dc=delta_c(z,lut,cosm)
DO i=1,n
m=exp(log(mmin)+log(mmax/mmin)*float(i-1)/float(n-1))
r=radius_m(m,cosm)
sig=sigmac(r,z,cosm)
nu=dc/sig
lut%m(i)=m
lut%rr(i)=r
lut%sig(i)=sig
lut%nu(i)=nu
END DO
IF(ihm==1) WRITE(*,*) 'HALOMOD: m, r, nu, sig HM_tables filled'
!Fills up a table for sigma(fM) for Bullock c(m) relation
!This is the f=0.01 parameter in the Bullock realtion sigma(fM,z)
f=0.01**(1./3.)
DO i=1,lut%n
lut%sigf(i)=sigmac(lut%rr(i)*f,z,cosm)
END DO
IF(ihm==1) WRITE(*,*) 'HALOMOD: sigf HM_tables filled'
!Fill virial radius table using real radius table
Dv=Delta_v(z,lut,cosm)
lut%rv=lut%rr/(Dv**(1./3.))
IF(ihm==1) WRITE(*,*) 'HALOMOD: rv HM_tables filled'
IF(ihm==1) WRITE(*,*) 'HALOMOD: nu min:', lut%nu(1)
IF(ihm==1) WRITE(*,*) 'HALOMOD: nu max:', lut%nu(lut%n)
IF(ihm==1) WRITE(*,*) 'HALOMOD: R_v min [Mpc/h]:', lut%rv(1)
IF(ihm==1) WRITE(*,*) 'HALOMOD: R_v max [Mpc/h]:', lut%rv(lut%n)
IF(ihm==1) WRITE(*,*) 'HALOMOD: M min [Msun/h]:', lut%m(1)
IF(ihm==1) WRITE(*,*) 'HALOMOD: M max [Msun/h]:', lut%m(lut%n)
!Find non-linear radius and scale
lut%rnl=r_nl(lut)
lut%knl=1./lut%rnl
IF(ihm==1) WRITE(*,*) 'HALOMOD: r_nl [Mpc/h]:', lut%rnl
IF(ihm==1) WRITE(*,*) 'HALOMOD: k_nl [h/Mpc]:', lut%knl
!Calcuate the effective spectral index at the collapse scale
lut%neff=neff(lut,cosm)
IF(ihm==1) WRITE(*,*) 'HALOMOD: n_eff:', lut%neff
!Get the concentration for all the haloes
CALL conc_bull(z,lut,cosm)
IF(ihm==1) WRITE(*,*) 'HALOMOD: c HM_tables filled'
IF(ihm==1) WRITE(*,*) 'HALOMOD: c min [Msun/h]:', lut%c(lut%n)
IF(ihm==1) WRITE(*,*) 'HALOMOD: c max [Msun/h]:', lut%c(1)
IF(ihm==1) WRITE(*,*) 'HALOMOD: Done'
IF(ihm==1) WRITE(*,*)
IF(ihm==1) CALL write_parameters(z,lut,cosm)
!Switch off verbose mode if doing multiple z
ihm=0
END SUBROUTINE halomod_init
SUBROUTINE write_parameters(z,lut,cosm)
!This subroutine writes out the halomodel parameters at the current redshift
REAL, INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(ihm==1) WRITE(*,*) 'Parameters at your redshift'
IF(ihm==1) WRITE(*,*) '==========================='
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'z:', z
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'Dv:', Delta_v(z,lut,cosm)
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'dc:', delta_c(z,lut,cosm)
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'eta:', eta(z,lut,cosm)
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'k*:', kstar(z,lut,cosm)
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'A:', As(z,lut,cosm)
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'fdamp:', fdamp(z,lut,cosm)
IF(ihm==1) WRITE(*,fmt='(A10,F10.5)') 'alpha:', alpha(z,lut,cosm)
IF(ihm==1) WRITE(*,*)
END SUBROUTINE write_parameters
PURE FUNCTION radius_m(m,cosm)
!Calculates the co-moving radius that encloses a mass 'm' in the homogeneous Universe
REAL :: radius_m
REAL, INTENT(IN) :: m
TYPE(HM_cosmology), INTENT(IN) :: cosm
REAL, PARAMETER :: pi=3.141592654
radius_m=(3.*m/(4.*pi*cosmic_density(cosm)))**(1./3.)
END FUNCTION radius_m
FUNCTION neff(lut,cosm)
!Finds the effective spectral index at the collapse scale r_nl
!Where nu(r_nl)=1.
REAL :: neff
REAL :: ns
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
!Numerical differentiation to find effective index at collapse
neff=-3.-derivative_table(log(lut%rnl),log(lut%rr),log(lut%sig**2.),3,3)
!For some bizarre cosmological models r_nl is very small, so almost no collapse has occured
!In this case the n_eff calculation goes mad and needs to be fixed using this fudge.
ns=cosm%ns
IF(neff<ns-4.) neff=ns-4.
IF(neff>ns) neff=ns
END FUNCTION neff
SUBROUTINE conc_bull(z,lut,cosm)
!Calculates the Bullock et al. (2001) concentration-mass relation
REAL, INTENT(IN) :: z
TYPE(HM_cosmology) :: cosm, cos_lcdm
TYPE(HM_tables) :: lut
REAL :: A, zf, ainf, zinf, g_lcdm, g_wcdm, pow
INTEGER :: i
!Amplitude of relation (4. in Bullock et al. 2001)
A=As(z,lut,cosm)
!Fill the collapse time look-up table
CALL zcoll_bull(z,cosm,lut)