-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhalofit.txt
3455 lines (2756 loc) · 117 KB
/
halofit.txt
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. (2003), MNRAS, 341, 4
!
! 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
! halofit_casarini: arXiv:0810.0190, arXiv:1601.07230
! 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
!AL Jun16: put in partial openmp for HMcode (needs restructure to do properly)
!AM Sep 16: Attempted fix of strange bug. No more modules with unallocated arrays as inputs
!LC Oct 16: extended Halofit from w=const. models to w=w(a) with PKequal
!AM May 17: Made the baryon feedback parameters more obvious in HMcode
!AL Jul 17: fixed undefined z calling Tcb_Tcbnu_ratio
!AM Jul 17: sped-up HMcode integration routines
!AM May 18: Fixed bug in Dolag correction to c(M) power
!AM Jul 19: Upgraded accuracy and bug fix for massive-neutrino models
!AL Jul 19: Speedups, use linear interpolation for pk; find index using fixed spacing; precompute growth(z)
!AL Sep 19: Propagate errors rather than stop, decrease jmax for integration time out (prevent very slow error)
!AM Sep 20: Added HMcode-2020 model
!AM Jan 23: Fixed HMcode-2020 feedback low-k predictions
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
module NonLinear
use results
use DarkEnergyInterface
use classes
use Transfer
use constants
use config
implicit none
private
integer, parameter :: halofit_original=1, halofit_bird=2, halofit_peacock=3, halofit_takahashi=4
integer, parameter :: halofit_casarini=7
integer, parameter :: halofit_mead2016=5, halofit_halomodel=6, halofit_mead2015=8, halofit_mead2020=9
integer, parameter :: halofit_mead2020_feedback=10
integer, parameter :: halofit_mead=halofit_mead2016 ! AM Kept for backwards compatability
integer, parameter :: halofit_default=halofit_mead2020
logical :: HM_verbose = .false.
type, extends(TNonLinearModel) :: THalofit
integer :: halofit_version = halofit_default
!!TT - These are the baryon parameters of HMCode
real(dl) :: HMcode_A_baryon=3.13_dl
real(dl) :: HMcode_eta_baryon=0.603_dl
real(dl) :: HMcode_logT_AGN=7.8_dl
real(dl) :: Zc=1.0_dl
real(dl) :: fo=1.0_dl
!!AM - Added these types for HMcode
integer, private :: imead !!AM - added these for HMcode, need to be visible to all subroutines and functions
real(dl), private :: om_m,om_v,fnu,omm0, acur, w_hf, wa_hf
real(dl), private :: om_c, om_b
contains
procedure :: ReadParams => THalofit_ReadParams
procedure :: GetNonLinRatios => THalofit_GetNonLinRatios
procedure :: halofit
procedure :: HMcode
procedure, nopass :: PythonClass => THalofit_PythonClass
procedure, nopass :: SelfPointer => THalofit_SelfPointer
procedure, private :: Delta_v
procedure, private :: delta_c
procedure, private :: eta
procedure, private :: kstar
procedure, private :: As
procedure, private :: conc_bull
procedure, private :: fdamp
procedure, private :: p_1h
procedure, private :: p_2h
procedure, private :: alpha
procedure, private :: halomod
procedure, private :: halomod_init
procedure, private :: write_parameters
procedure, private :: zcoll_bull
end type
public THalofit, HM_verbose
public halofit_default, halofit_original, halofit_bird, halofit_peacock, halofit_takahashi
public halofit_mead2016, halofit_mead2015, halofit_mead2020, halofit_halomodel, halofit_casarini
public halofit_mead2020_feedback
public halofit_mead ! AM for backwards compatability
TYPE HM_cosmology
!Contains only things that do not need to be recalculated with each new z
REAL(dl) :: om_m, om_c, om_b, om_nu, om_v, w, wa, f_nu, ns, h, Tcmb, Nnu
REAL(dl), ALLOCATABLE :: log_r_sigma(:), log_sigma(:)
REAL(dl), ALLOCATABLE :: a_growth(:), growth(:), agrow(:)
REAL(dl), ALLOCATABLE :: log_k_plin(:), log_plin(:), log_plinc(:)
REAL(dl), ALLOCATABLE :: log_k_wiggle(:), pk_wiggle(:)
real(dl) :: kmax
real(dl) :: gnorm
INTEGER :: nk, ng, nsig
real(dl) :: grow2_z, this_z !cached value at redshift being calculated
!AM - Added feedback parameters below at fixed fiducial (DMONLY) values
REAL(dl) :: A_baryon=3.13
REAL(dl) :: eta_baryon=0.603
REAL(dl) :: logT_AGN=7.8
END TYPE HM_cosmology
TYPE HM_tables
!Stuff that needs to be recalculated for each new z
REAL(dl), ALLOCATABLE :: c(:), rv(:), nu(:), sig(:), zc(:), m(:), rr(:), sigf(:)
REAL(dl) :: sigv, sigv100, knl, rnl, neff, sig8z, z, dc, sig8z_cold
INTEGER :: n
END TYPE HM_tables
!!AM - End of my additions
! HMcode parameters
REAL(dl), PARAMETER :: zc_Dolag=10._dl ! Halo collapse redshift for Dolag
REAL(dl), PARAMETER :: fdamp_min=1e-3_dl ! Minimum value of fdamp
REAL(dl), PARAMETER :: fdamp_max=0.99_dl ! Maximum value of fdamp
REAL(dl), PARAMETER :: alpha_min=0.5_dl ! Minimum value of alpha transition
REAL(dl), PARAMETER :: alpha_max=2._dl ! Maximum value of alpha transition
REAL(dl), PARAMETER :: ks_limit=7._dl ! Limit for (k/ks)^2 in one-halo term
REAL(dl), PARAMETER :: pi_HM=const_pi ! Lovely pi
! HMcode linear P(k) numerical parameters
! AM: Jul 19: Updated nk_pk_interpolation from 128 to 512
! AM: Dec 20: Calculation time and accuracy are especially sensive to these parameters
LOGICAL, PARAMETER :: rebin_pk=.TRUE. ! Should the linear P(k) be rebinned?
REAL(dl), PARAMETER :: kmin_pk_interpolation=1d-3 ! Minimum wavenumber if rebinning [h/Mpc]
REAL(dl), PARAMETER :: kmax_pk_interpolation=1d2 ! Maximum wavenumber if rebinning [h/Mpc]
INTEGER, PARAMETER :: nk_pk_interpolation=512 ! Number of points in k if rebining
LOGICAL, PARAMETER :: plin_extrap=.FALSE. ! Extrapolate at high-k via thoery or simple power law
INTEGER, PARAMETER :: iorder_pk_interpolation=1 ! Polynomial order for P(k) interpolation
INTEGER, PARAMETER :: ifind_pk_interpolation=1 ! Finding scheme for P(k) interpolation (if rebin_pk=True)
INTEGER, PARAMETER :: imeth_pk_interpolation=1 ! Method for P(k) interpolation
! HMcode dewiggle numerical parameters
REAL, PARAMETER :: kmin_wiggle=5e-3 ! Minimum wavenumber to calulate wiggle [Mpc/h]
REAL, PARAMETER :: kmax_wiggle=5. ! Maximum wavenumber to calulate wiggle [Mpc/h]
INTEGER, PARAMETER :: nk_wiggle=512 ! Number of k points to store wiggle
INTEGER, PARAMETER :: iorder_wiggle=3 ! Order for wiggle interpolation
INTEGER, PARAMETER :: ifind_wiggle=3 ! 3 - Mid-point finding scheme for wiggle interpolation
INTEGER, PARAMETER :: imeth_wiggle=2 ! 2- Lagrange polynomial interpolation
REAL, PARAMETER :: wiggle_sigma=0.25 ! Smoothing width if using Gaussian smoothing
REAL, PARAMETER :: knorm_nowiggle=0.03 ! Wavenumber at which to force linear and nowiggle to be identical [Mpc/h]
! Linear growth integral numerical parameters (LCDM only; only used in Dolag correction)
! AM: Jul 19: Updated acc_growint from 1e-3 to 1e-4
! AM: Sep 20: Changed cold_growth = .FALSE. to be in line with my code
REAL(dl), PARAMETER :: acc_growth_integration=1e-4 ! Accuracy for growth function integral
INTEGER, PARAMETER :: iorder_growth_integration=3 ! Polynomial order for growth integral
LOGICAL, PARAMETER :: cold_growth=.FALSE. ! Should growth be of cold or all matter?
! Linear growth factor tabulation and interpolation numerical parameters
! AM: TODO: Change finding scheme to assume linear spacing may save time
REAL(dl), PARAMETER :: amin_growth_interpolation=1e-3 ! Minimum scale factor for growth interpolation
REAL(dl), PARAMETER :: amax_growth_interpolation=1. ! Maximum scale factor for growth interpolation
INTEGER, PARAMETER :: n_growth_interpolation=64 ! Number of entries for growth look-up table
INTEGER, PARAMETER :: iorder_growth_interpolation=3 ! Polynomial order for growth function interpolation
INTEGER, PARAMETER :: ifind_growth_interpolation=3 ! Finding scheme for growth function interpolation
INTEGER, PARAMETER :: imeth_growth_interpolation=2 ! Method for growth function interpolation
! Growth function ODE numerical parameters
! AM: Jul 19: Updated acc_growth_ODE from 1e-3 to 1e-4
! AM: Sep 20: Changed aini from 1e-3 to 1e-4
REAL(dl), PARAMETER :: aini_growth_ODE=1e-4 ! Initial scale factor for growth ODE
REAL(dl), PARAMETER :: afin_growth_ODE=1. ! Final scale factor for growth ODE
REAL(dl), PARAMETER :: acc_growth_ODE=1e-4 ! Accuracy for growth integral or ODE
INTEGER, PARAMETER :: imeth_growth_ODE=3 ! Method for growth function ODE solving
INTEGER, PARAMETER :: iorder_growth_ODE_interpolation=3 ! Polynomial order for growth function ODE interpolation
INTEGER, PARAMETER :: ifind_growth_ODE_interpolation=3 ! Finding scheme for growth function ODE interpolation
INTEGER, PARAMETER :: imeth_growth_ODE_interpolation=2 ! Method growth function ODE interpolation
! Linear growth function inversion numerical parameters (used for c(M) only)
INTEGER, PARAMETER :: iorder_growth_inversion=3 ! Polynomial order for growth function ODE inversion
INTEGER, PARAMETER :: ifind_growth_inversion=3 ! Finding scheme for growth function ODE inversion
INTEGER, PARAMETER :: imeth_growth_inversion=2 ! Method growth function ODE inversion
! Accumulated growth parameters
INTEGER, PARAMETER :: iorder_integration_agrow=3 ! Polynomial order for accumulated growth integration
INTEGER, PARAMETER :: iorder_agrowth_interpolation=3 ! Polynomial order for accumlated growth interpolation
INTEGER, PARAMETER :: ifind_agrowth_interpolation=3 ! Finding scheme for accumulated growth interpolation
INTEGER, PARAMETER :: imeth_agrowth_interpolation=2 ! Method for accumulated growth interpolation
! HMcode numerical parameters for sigma(R) tabulation and interpolation
REAL(dl), PARAMETER :: rmin_sigma_interpolation=1e-4 ! Minimum scale for sigma(R) look-up tables [Mpc/h]
REAL(dl), PARAMETER :: rmax_sigma_interpolation=1e3 ! Maximum scale for sigma(R) look-up tables [Mpc/h]
INTEGER, PARAMETER :: n_sigma_interpolation=64 ! Number of points in look-up tables
INTEGER, PARAMETER :: iorder_sigma_interpolation=3 ! Polynomial order for sigma(R) interpolation
INTEGER, PARAMETER :: ifind_sigma_interpolation=3 ! Finding scheme for sigma(R) interpolation
INTEGER, PARAMETER :: imeth_sigma_interpolation=2 ! Method sigma(R) interpolation
! HMcode numerical parameters for sigma(R) integration (dominates run time as of Jul 2019)
! AM: Jul 19: Upgraded acc_sigma from 1e-3 to 3e-4
! AM: Sep 20: Upgraded acc_sigma from 3e-4 to 1e-4 to fix problems for some cosmologies
REAL(dl), PARAMETER :: acc_sigma_integration=1e-4 ! Relative accuracy of numerical integration
REAL(dl), PARAMETER :: alpha_sigma_integration=3. ! Exponent to speed up integration
INTEGER, PARAMETER :: iorder_sigma_integration=3 ! Polynomail order for numerical integration
! HMcode numerical parameters for sigmaV(R) integration
! AM: Jul 19: Upgraded acc_sigmaV from 1e-3 to 1e-4
REAL(dl), PARAMETER :: acc_sigmaV_integration=1e-4 ! Relative accuracy of numerical integration
REAL(dl), PARAMETER :: alpha_sigmaV_integration=3. ! Exponent to speed up integration
INTEGER, PARAMETER :: iorder_sigmaV_integration=3 ! Polynomial order for numerical integration
! HMcode numerical parameters for neff(R) integration
REAL(dl), PARAMETER :: acc_neff_integration=1e-4 ! Relative accuracy of numerical integration
REAL(dl), PARAMETER :: alpha_neff_integration=2. ! Exponent to speed up integration
INTEGER, PARAMETER :: iorder_neff_integration=3 ! Polynomial order for numerical integration
! HMcode numerical parameters for cold transfer function approximation
! AM: Sep 20: Care here, before EdS_Tcold_growth=.TRUE.
LOGICAL, PARAMETER :: EdS_Tcold_growth=.FALSE. ! Should the EdS growth function (incorrectly) be used?
! HMcode numerical parameters for one-halo term
INTEGER, PARAMETER :: iorder_integration_1h=1 ! Should be linear order (i.e., trapezium rule)
contains
function THalofit_PythonClass()
character(LEN=:), allocatable :: THalofit_PythonClass
THalofit_PythonClass = 'Halofit'
end function THalofit_PythonClass
subroutine THalofit_SelfPointer(cptr,P)
use iso_c_binding
Type(c_ptr) :: cptr
Type (THalofit), pointer :: PType
class (TPythonInterfacedClass), pointer :: P
call c_f_pointer(cptr, PType)
P => PType
end subroutine THalofit_SelfPointer
subroutine THalofit_ReadParams(this,Ini)
use IniObjects
class(THalofit) :: this
class(TIniFile), intent(in) :: Ini
this%halofit_version = Ini%Read_Int('halofit_version', halofit_default)
IF(this%halofit_version == halofit_mead2020_feedback) THEN
this%HMcode_logT_AGN = Ini%Read_Double('HMcode_logT_AGN', 7.8_dl)
END IF
this%halofit_version = Ini%Read_Int('halofit_version', halofit_default)
IF(this%halofit_version == halofit_default) THEN
this%Zc = Ini%Read_Double('Zc', 1.0_dl)
END IF
this%halofit_version = Ini%Read_Int('halofit_version', halofit_default)
IF(this%halofit_version == halofit_default) THEN
this%fo = Ini%Read_Double('fo', 1.0_dl)
END IF
end subroutine THalofit_ReadParams
subroutine THalofit_GetNonLinRatios(this,State,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
class(THalofit) :: this
class(TCAMBdata) :: State
type(MatterPowerData), target :: 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, h2
integer i
!$ if (ThreadNum /=0) call OMP_SET_NUM_THREADS(ThreadNum)
select type (State)
class is (CAMBdata)
associate(Params => State%CP)
IF(this%halofit_version==halofit_mead2016 .OR. &
this%halofit_version==halofit_halomodel .OR. &
this%halofit_version==halofit_mead2015 .OR. &
this%halofit_version==halofit_mead2020 .OR. &
this%halofit_version==halofit_mead2020_feedback) THEN
CALL this%HMcode(State,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.
h2 = (Params%H0/100)**2
this%omm0 = (Params%omch2+Params%ombh2+Params%omnuh2)/h2
this%fnu = Params%omnuh2/h2/this%omm0
CAMB_Pk%nonlin_ratio = 1
do itf = 1, CAMB_Pk%num_z
call Params%DarkEnergy%Effective_w_wa(this%w_hf, this%wa_hf)
if (this%halofit_version == halofit_casarini) then
! calculate equivalent w-constant models (w_hf,0) for w_lam+wa_ppf(1-a) models
! [Casarini+ (2009,2016)].
call PKequal(State,CAMB_Pk%Redshifts(itf),this%w_hf,this%wa_hf,this%w_hf,this%wa_hf)
endif
! 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)
this%om_m = omega_m(a, this%omm0, State%omega_de, this%w_hf, this%wa_hf)
this%om_v = omega_v(a, this%omm0, State%omega_de, this%w_hf, this%wa_hf)
this%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 (xlogr1>3.4999) then
! Totally crazy non-linear
call GlobalError('Error in halofit (xlogr1>3.4999)', error_nonlinear)
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 > this%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*const_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 this%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 associate
end select
end subroutine THalofit_GetNonLinRatios
!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subroutine halofit(this,rk,rn,rncur,rknl,plin,pnl,pq,ph)
class(THalofit) :: this
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 (this%halofit_version ==halofit_original .or. this%halofit_version ==halofit_bird &
.or. this%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 (this%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+this%fnu*(-6.4868+1.4373*rn**2)
elseif (this%halofit_version == halofit_takahashi .or. this%halofit_version == halofit_casarini) 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.
!LC16 Jun: Casarini+ 2009,2016 extended constant w prediction for w(a).
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*this%om_v*(1.+this%w_hf+this%wa_hf*(1-this%acur))
a=10**a
b=10**(-0.5642+0.5864*rn+0.5716*rn*rn-1.5474*rncur+ &
0.2279*this%om_v*(1.+this%w_hf+this%wa_hf*(1-this%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 + this%fnu*(1.081 + 0.395*rn**2)
else
call MpiStop('Unknown halofit_version')
end if
if(abs(1-this%om_m).gt.0.01) then ! omega evolution
f1a=this%om_m**(-0.0732)
f2a=this%om_m**(-0.1423)
f3a=this%om_m**(0.0725)
f1b=this%om_m**(-0.0307)
f2b=this%om_m**(-0.0585)
f3b=this%om_m**(0.0743)
frac=this%om_v/(1.-this%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+this%fnu*0.977)
plinaa=plin*(1+this%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 (this%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
integer index_cache
index_cache = 1
nint=3000
sum1=0.d0
sum2=0.d0
sum3=0.d0
anorm = 1/(2*const_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, index_cache)*(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(this,State,CAMB_Pk)
!!AM - A CAMB derived type that I need
class(THalofit) :: this
Class(CAMBdata) :: State
TYPE(MatterPowerData) :: CAMB_Pk
REAL(dl) :: z, k, y
REAL(dl) :: p1h, p2h, pfull, plin
REAL(dl), ALLOCATABLE :: p_den(:,:), p_num(:,:)
INTEGER :: i, j, ii, nk, nz
REAL :: t1, t2
TYPE(HM_cosmology) :: cosi
TYPE(HM_tables) :: lut
REAL(dl), PARAMETER :: pi=pi_HM
LOGICAL, PARAMETER :: timing_test = .FALSE.
!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
IF (timing_test) CALL CPU_TIME(t1)
!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. (2016; arXiv 1602.02154)
!2 - Accurate from Mead et al. (2015; arXiv 1505.07833)
!3 - Accurate from Mead et al. (2020; arXiv 2009.01858)
!4 - Denominator for feedback reaction model from Mead et al. (2020; arXiv 2009.01858)
!5 - Numerator for feedback reaction from Mead et al. (2020; arXiv 2009.01858)
IF(this%halofit_version==halofit_halomodel) this%imead=0
IF(this%halofit_version==halofit_mead2016) this%imead=1
IF(this%halofit_version==halofit_mead2015) this%imead=2
IF(this%halofit_version==halofit_mead2020) this%imead=3
HM_verbose = (FeedbackLevel>1)
IF(HM_verbose) WRITE(*,*)
IF(HM_verbose) WRITE(*,*) 'HMcode: Running HMcode'
IF(HM_verbose) WRITE(*,*)
!!AM - Translate from CAMB variables to my variables
nz=CAMB_PK%num_z
nk=CAMB_PK%num_k
IF(this%halofit_version==halofit_mead2020_feedback) THEN
ALLOCATE(p_den(nk,nz), p_num(nk,nz))
END IF
!!AM - Assign cosmological parameters for the halo model calculation
CALL assign_HM_cosmology(this,State,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-massive-neutrino models this could only be done once, which would speed things up a bit
CALL initialise_HM_cosmology(this,j,cosi,CAMB_PK)
!Sets the current redshift from the table
z=CAMB_Pk%Redshifts(j)
IF(this%halofit_version==halofit_mead2020_feedback) THEN
! Loop over numerator, denominator and HMcode to make feedback response model
DO ii = 1, 3
IF(ii==1) this%imead=3 ! HMcode 2020
IF(ii==2) this%imead=4 ! Denominator for response
IF(ii==3) this%imead=5 ! Numerator for response
!Initiliasation for the halomodel calculation (needs to be done for each z)
CALL this%halomod_init(z,lut,cosi)
if (global_error_flag/=0) return
!Loop over k values and calculate P(k)
!$OMP PARALLEL DO DEFAULT(SHARED), private(k,plin,pfull,p1h,p2h)
DO i=1,nk
k=exp(CAMB_Pk%log_kh(i))
plin=p_lin(k,z,0,cosi)
CALL this%halomod(k,p1h,p2h,pfull,plin,lut,cosi)
IF(this%imead==3) THEN
CAMB_Pk%nonlin_ratio(i,j)=sqrt(pfull/plin)
ELSE IF(this%imead==4) THEN
p_den(i,j)=pfull
ELSE IF(this%imead==5) THEN
p_num(i,j)=pfull
END IF
END DO
!$OMP END PARALLEL DO
END DO
ELSE
!Initiliasation for the halomodel calculation (needs to be done for each z)
CALL this%halomod_init(z,lut,cosi)
if (global_error_flag/=0) return
!Loop over k values and calculate P(k)
!$OMP PARALLEL DO DEFAULT(SHARED), private(k,plin,pfull,p1h,p2h)
DO i=1,nk
k=exp(CAMB_Pk%log_kh(i))
plin=p_lin(k,z,0,cosi)
CALL this%halomod(k,p1h,p2h,pfull,plin,lut,cosi)
CAMB_Pk%nonlin_ratio(i,j)=sqrt(pfull/plin)
END DO
!$OMP END PARALLEL DO
END IF
END DO
DO i=1,nz
z=CAMB_Pk%Redshifts(i)
IF(z<this%Zc) THEN
y=(1-this%fo)*z/this%Zc+this%fo
ELSE IF(z>=this%Zc) THEN
y=1
END IF
CAMB_Pk%nonlin_ratio(:,i) = CAMB_Pk%nonlin_ratio(:,i) * y
END DO
! Make the non-linear correction from the response for HMcode 2020
IF(this%halofit_version==halofit_mead2020_feedback) THEN
CAMB_Pk%nonlin_ratio=CAMB_Pk%nonlin_ratio*sqrt(p_num/p_den)
END IF
IF (timing_test) THEN
CALL CPU_TIME(t2)
WRITE(*, *) 'HMcode number of k:', nk
WRITE(*, *) 'HMcode number of z:', nz
WRITE(*, *) 'HMcode run time [s]:', t2-t1
STOP 'HMcode timing test complete'
END IF
END SUBROUTINE HMcode
FUNCTION Delta_v(this,z,cosm)
class(THalofit) :: this
!Function for the virialised overdensity
REAL(dl) :: Delta_v
REAL(dl), INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
IF(this%imead==1 .OR. this%imead==2) THEN
!Mead et al. (2015; arXiv 1505.07833) value
Delta_v=418*Omega_m_hm(z,cosm)**(-0.352_dl)
!Mead et al. (2016; arXiv 1602.02154) neutrino addition
IF(this%imead==1) Delta_v=Delta_v*(1+0.916_dl*cosm%f_nu)
ELSE IF(this%imead==0 .OR. this%imead==3 .OR. this%imead==4 .OR. this%imead==5) THEN
Delta_v=Dv_Mead(z, cosm)
END IF
END FUNCTION Delta_v
FUNCTION delta_c(this,z,lut,cosm)
class(THalofit) :: this
!Function for the linear collapse density
REAL(dl) :: delta_c
REAL(dl), INTENT(IN) :: z
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
IF(this%imead==1 .or. this%imead==2) THEN
!Mead et al. (2015; arXiv 1505.07833) value
delta_c=1.59+0.0314*log(lut%sig8z)
IF(this%imead==1) THEN
delta_c=delta_c*(1.+0.262*cosm%f_nu) !Mead et al. (2016; arXiv 1602.02154) neutrino addition
delta_c=delta_c*(1.+0.0123*log10(Omega_m_hm(z,cosm))) !Nakamura & Suto (1997) fitting formula for LCDM
END IF
ELSE IF(this%imead==0 .OR. this%imead==3 .OR. this%imead==4 .OR. this%imead==5) THEN
delta_c=dc_Mead(z, cosm)
END IF
END FUNCTION delta_c
FUNCTION eta(this,lut,cosm)
class(THalofit) :: this
!Function eta that puffs halo profiles
REAL(dl) :: eta
TYPE(HM_cosmology), INTENT(IN) :: cosm
TYPE(HM_tables), INTENT(IN) :: lut
REAL(dl) :: eta0
IF(this%imead==0 .OR. this%imead==4 .OR. this%imead==5) THEN
eta=0.
ELSE IF(this%imead==1 .or. this%imead==2) THEN
!The first parameter here is 'eta_0' in Mead et al. (2015; arXiv 1505.07833)
!eta=0.603-0.3*lut%sig8z
!AM - made baryon feedback parameter obvious
eta0=cosm%eta_baryon
!eta0=1.03-0.11*cosm%A_baryon !Original one-parameter relation from 1505.07833
!eta0=0.98-0.12*cosm%A_baryon !Updated one-parameter relation: Section 4.1.2 of 1707.06627
eta=eta0-0.3*lut%sig8z
ELSE IF(this%imead==3) THEN
eta=0.1281*lut%sig8z_cold**(-0.3644)
END IF
END FUNCTION eta
FUNCTION kstar(this,lut)
class(THalofit) :: this
!Function k* that cuts off the 1-halo term at large scales
REAL(dl) :: kstar
TYPE(HM_tables), INTENT(IN) :: lut
IF(this%imead==0) THEN
!Set to zero for the standard Poisson one-halo term
kstar=0.
ELSE IF(this%imead==1 .or. this%imead==2) THEN
!One-halo cut-off wavenumber
!Mead et al. (2015; arXiv 1505.07833) value
kstar=0.584*(lut%sigv)**(-1.)
ELSE IF(this%imead==3 .OR. this%imead==4 .OR. this%imead==5) THEN
kstar=0.05618*lut%sig8z_cold**(-1.013)
END IF
END FUNCTION kstar
FUNCTION As(this,lut,cosm)
class(THalofit) :: this
!Halo concentration pre-factor from Bullock et al. (2001) relation
TYPE(HM_tables), INTENT(IN) :: lut
TYPE(HM_cosmology), INTENT(IN) :: cosm
REAL(dl) :: As
REAL(dl) :: B0, Bz, theta
IF(this%imead==0 .OR. this%imead==4) THEN
!Set to 4 for the standard Bullock value
As=4.
ELSE IF(this%imead==5) THEN
theta=cosm%logT_AGN-7.8
B0=3.44-0.496*theta
Bz=-0.0671-0.0371*theta
As=B0*10**(lut%z*Bz)
ELSE IF(this%imead==1 .or. this%imead==2) THEN
!This is the 'A' halo-concentration parameter in Mead et al. (2015; arXiv 1505.07833)
!As=3.13
!AM - added for easy modification of feedback parameter
As=cosm%A_baryon
ELSE IF(this%imead==3) THEN
As=5.196
END IF
END FUNCTION As
FUNCTION fdamp(this,lut)
class(THalofit) :: this
!Linear power damping function from Mead et al. (2015; arXiv 1505.07833)
REAL(dl) ::fdamp
TYPE(HM_tables), INTENT(IN) :: lut
!Linear theory damping factor
IF(this%imead==0 .OR. this%imead==4 .OR. this%imead==5) THEN
!Set to 0 for the standard linear theory two halo term
fdamp=0.
ELSE IF(this%imead==1) THEN
!Mead et al. (2016; arXiv 1602.02154) value
fdamp=0.0095*lut%sigv100**1.37
ELSE IF(this%imead==2) THEN
!Mead et al. (2015) value
fdamp=0.188*lut%sig8z**4.29
ELSE IF(this%imead==3) THEN
fdamp=0.2696*lut%sig8z_cold**0.9403
END IF
!Catches extreme values of fdamp
IF(fdamp<fdamp_min) fdamp=fdamp_min
IF(fdamp>fdamp_max) fdamp=fdamp_max
END FUNCTION fdamp
FUNCTION alpha(this,lut)
class(THalofit) :: this
!Two- to one-halo transition smoothing from Mead et al. (2015; arXiv 1505.07833)
REAL(dl) :: alpha
TYPE(HM_tables), INTENT(IN) :: lut
IF(this%imead==0 .OR. this%imead==4 .OR. this%imead==5) THEN
!Set to 1 for the standard halomodel sum of one- and two-halo terms
alpha=1.
ELSE IF(this%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
ELSE IF(this%imead==2) THEN
!Mead et al. (2015) value
alpha=2.93*1.77**lut%neff
ELSE IF (this%imead==3) THEN
alpha=1.875*(1.603)**lut%neff
END IF
!Catches values of alpha that are crazy
IF(alpha>alpha_max) alpha=alpha_max
IF(alpha<alpha_min) alpha=alpha_min
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(dl) :: r_nl
INTEGER, PARAMETER :: iorder=3
INTEGER, PARAMETER :: ifind=3
INTEGER, PARAMETER :: imeth=2
IF(lut%nu(1)>1.) THEN
!This catches some very strange values that appear for odd cosmological models
!This is a terrible fudge, but I cannot think of a better solution
r_nl=lut%rr(1)
ELSE
r_nl=exp(find(log(1.d0),log(lut%nu),log(lut%rr),lut%n,iorder,ifind,imeth))
END IF
END FUNCTION r_nl
SUBROUTINE halomod(this,k,p1h,p2h,pfull,plin,lut,cosm)
class(THalofit) :: this
!Calcuates 1-halo and 2-halo terms and combines them to form the full halomodel power
REAL(dl), INTENT(OUT) :: p1h, p2h, pfull
REAL(dl), INTENT(IN) :: plin, k
REAL(dl) :: 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=this%p_1h(k,lut,cosm)
p2h=this%p_2h(k,plin,lut,cosm)
END IF
IF (this%imead==1 .OR. this%imead==2 .OR. this%imead==3) THEN
a=this%alpha(lut)
pfull=(p2h**a+p1h**a)**(1./a)
ELSE
pfull=p2h+p1h
END IF
END SUBROUTINE halomod
SUBROUTINE fill_table(min,max,arr,n)
!Fills array 'arr' in equally spaced intervals
INTEGER :: i
REAL(dl), INTENT(IN) :: min, max
REAL(dl), ALLOCATABLE :: arr(:)
INTEGER, INTENT(IN) :: n
!Allocate the array, and deallocate it if it is full
IF(ALLOCATED(arr)) DEALLOCATE(arr)
ALLOCATE(arr(n))
arr=0
IF(n==1) THEN
arr(1)=min
ELSE IF(n>1) THEN
DO i=1,n
arr(i)=min+(max-min)*real(i-1,dl)/(n-1)
END DO
END IF
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
REAL(dl) :: z, g
REAL(dl), ALLOCATABLE :: k(:), Pk(:), Pkc(:)
REAL(dl), PARAMETER :: pi=pi_HM
REAL(dl), PARAMETER :: kmin=kmin_pk_interpolation
REAL(dl), PARAMETER :: kmax=kmax_pk_interpolation
INTEGER :: nk=nk_pk_interpolation, index_cache
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: Filling linear power HM_tables'
!Fill arrays
IF(ALLOCATED(cosm%log_k_plin)) DEALLOCATE(cosm%log_k_plin)
IF(ALLOCATED(cosm%log_plin)) DEALLOCATE(cosm%log_plin)
IF(ALLOCATED(cosm%log_plinc)) DEALLOCATE(cosm%log_plinc)
IF(rebin_pk) THEN
!Fill a k-table with an equal-log-spaced k range
!Note that the minimum should be such that the linear spectrum is accurately a power-law below this wavenumber
cosm%nk=nk
CALL fill_table(log(kmin),log(kmax),cosm%log_k_plin,nk)
ELSE
if (ifind_pk_interpolation==1) error stop 'ifind_pk_interpolation=1 assumes rebin_pk'
!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
cosm%nk=nk
ALLOCATE(cosm%log_k_plin(nk))
cosm%log_k_plin=CAMB_Pk%log_kh
END IF
ALLOCATE(k(nk))
k=exp(cosm%log_k_plin)
cosm%kmax = k(nk)
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: k_min:', k(1)
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: k_max:', k(nk)
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: nk:', nk
ALLOCATE(Pk(nk),Pkc(nk))
!Find the redshift
z=CAMB_Pk%Redshifts(iz)
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: z of input:', z
index_cache = 1
!Fill power table, both cold- and all-matter
!$OMP PARALLEL DO DEFAULT(SHARED), FIRSTPRIVATE(index_cache)
DO i=1,nk
!Take the power from the current redshift choice
Pk(i)=MatterPowerData_k(CAMB_PK,k(i),iz, index_cache)*(k(i)**3/(2*pi**2))
Pkc(i)=Pk(i)*Tcb_Tcbnu_ratio(k(i),z,cosm)**2
END DO
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: Delta2_min:', Pk(1)
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: Delta2_max:', Pk(nk)
!Calculate the growth factor at the redshift of interest
g=grow(z,cosm)
cosm%grow2_z = g**2
cosm%this_z = z
ALLOCATE(cosm%log_plin(nk),cosm%log_plinc(nk))
!Grow the power to z=0
cosm%log_plin=log(Pk/(g**2))
cosm%log_plinc=log(Pkc/(g**2))
!Check sigma_8 value
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: sigma_8:', sigma_integral(8.d0,0.d0,0,cosm)
IF(HM_verbose) WRITE(*,*) 'LINEAR POWER: Done'
IF(HM_verbose) WRITE(*,*)
END SUBROUTINE fill_plintab