forked from cmbant/CAMB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equations_ppf.f90
2931 lines (2427 loc) · 96 KB
/
equations_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
! Equations module for dark energy with constant equation of state parameter w
! allowing for perturbations based on a quintessence model
! by Antony Lewis (http://cosmologist.info/)
! Dec 2003, fixed (fatal) bug in tensor neutrino setup
! Changes to tight coupling approximation
! June 2004, fixed problem with large scale polarized tensors; support for vector modes
! Generate vector modes on their own. The power spectrum is taken from the scalar parameters.
! August 2004, fixed reionization term in lensing potential
! Nov 2004, change massive neutrino l_max to be consistent with massless if light
! Apr 2005, added DoLateRadTruncation option
! June 2006, added support for arbitary neutrino mass splittings
! Nov 2006, tweak to high_precision transfer function accuracy at lowish k
! June 2011, improved radiation approximations from arXiv: 1104.2933; Some 2nd order tight coupling terms
! merged fderivs and derivs so flat and non-flat use same equations; more precomputed arrays
! optimized neutrino sampling, and reorganised neutrino integration functions
! Feb 2012, updated PPF version but now only simple case for w, w_a (no anisotropic stresses etc)
! Feb 2013: fixed various issues with accuracy at larger neutrino masses
! Oct 2013: fix PPF, consistent with updated equations_cross
! Mar 2014: fixes for tensors with massive neutrinos
module LambdaGeneral
use precision
use ModelParams
implicit none
real(dl) :: w_lam = -1_dl !p/rho for the dark energy (assumed constant)
! w_lam is now w0
!comoving sound speed. Always exactly 1 for quintessence
!(otherwise assumed constant, though this is almost certainly unrealistic)
real(dl) :: cs2_lam = 1_dl
!cs2_lam now is ce^2
logical :: use_tabulated_w = .false.
real(dl) :: wa_ppf = 0._dl
real(dl) :: c_Gamma_ppf = 0.4_dl
integer, parameter :: nwmax = 5000, nde = 2000
integer :: nw_ppf
real(dl) w_ppf(nwmax), a_ppf(nwmax), ddw_ppf(nwmax)
real(dl) rde(nde),ade(nde),ddrde(nde)
real(dl), parameter :: amin = 1.d-9
logical :: is_cosmological_constant
private nde,ddw_ppf,rde,ade,ddrde,amin
contains
subroutine DarkEnergy_ReadParams(Ini)
use IniFile
Type(TIniFile) :: Ini
character(LEN=Ini_max_string_len) wafile
integer i
if (Ini_HasKey_File(Ini,'usew0wa')) then
stop 'input variables changed from usew0wa: now use_tabulated_w or w, wa'
end if
use_tabulated_w = Ini_Read_Logical_File(Ini,'use_tabulated_w',.false.)
if(.not. use_tabulated_w)then
w_lam = Ini_Read_Double_File(Ini,'w', -1.d0)
wa_ppf = Ini_Read_Double_File(Ini,'wa', 0.d0)
if (Feedback >0) write(*,'("(w0, wa) = (", f8.5,", ", f8.5, ")")') w_lam,wa_ppf
else
wafile = Ini_Read_String_File(Ini,'wafile')
open(unit=10,file=wafile,status='old')
nw_ppf=0
do i=1,nwmax+1
read(10,*,end=100)a_ppf(i),w_ppf(i)
a_ppf(i)=dlog(a_ppf(i))
nw_ppf=nw_ppf+1
enddo
write(*,'("Note: ", a, " has more than ", I8, " data points")') trim(wafile), nwmax
write(*,*)'Increase nwmax in LambdaGeneral'
stop
100 close(10)
write(*,'("read in ", I8, " (a, w) data points from ", a)') nw_ppf, trim(wafile)
call setddwa
call interpolrde
endif
cs2_lam = Ini_Read_Double_File(Ini,'cs2_lam',1.d0)
call setcgammappf
end subroutine DarkEnergy_ReadParams
subroutine setddwa
real(dl), parameter :: wlo=1.d30, whi=1.d30
call spline(a_ppf,w_ppf,nw_ppf,wlo,whi,ddw_ppf) !a_ppf is lna here
end subroutine setddwa
function w_de(a)
real(dl) :: w_de, al
real(dl), intent(IN) :: a
if(.not. use_tabulated_w) then
w_de=w_lam+wa_ppf*(1._dl-a)
else
al=dlog(a)
if(al.lt.a_ppf(1)) then
w_de=w_ppf(1) !if a < minimum a from wa.dat
elseif(al.gt.a_ppf(nw_ppf)) then
w_de=w_ppf(nw_ppf) !if a > maximus a from wa.dat
else
call cubicsplint(a_ppf,w_ppf,ddw_ppf,nw_ppf,al,w_de)
endif
endif
end function w_de ! equation of state of the PPF DE
function drdlna_de(al)
real(dl) :: drdlna_de, a
real(dl), intent(IN) :: al
a=dexp(al)
drdlna_de=3._dl*(1._dl+w_de(a))
end function drdlna_de
subroutine interpolrde
real(dl), parameter :: rlo=1.d30, rhi=1.d30
real(dl) :: atol, almin, al, rombint, fint
integer :: i
external rombint
atol=1.d-5
almin=dlog(amin)
do i=1,nde
al=almin-almin/(nde-1)*(i-1) !interpolate between amin and today
fint=rombint(drdlna_de, al, 0._dl, atol)+4._dl*al
ade(i)=al
rde(i)=dexp(fint) !rho_de*a^4 normalize to its value at today
enddo
call spline(ade,rde,nde,rlo,rhi,ddrde)
end subroutine interpolrde
function grho_de(a) !8 pi G a^4 rho_de
real(dl) :: grho_de, al, fint
real(dl), intent(IN) :: a
if(.not. use_tabulated_w) then
grho_de=grhov*a**(1._dl-3.*w_lam-3.*wa_ppf)*exp(-3.*wa_ppf*(1._dl-a))
else
if(a.eq.0.d0)then
grho_de=0.d0 !assume rho_de*a^4-->0, when a-->0, OK if w_de always <0.
else
al=dlog(a)
if(al.lt.ade(1))then
fint=rde(1)*(a/amin)**(1.-3.*w_de(amin)) !if a<amin, assume here w=w_de(amin)
else !if amin is small enough, this extrapolation will be unnecessary.
call cubicsplint(ade,rde,ddrde,nde,al,fint)
endif
grho_de=grhov*fint
endif
endif
end function grho_de
!-------------------------------------------------------------------
SUBROUTINE cubicsplint(xa,ya,y2a,n,x,y)
INTEGER n
real(dl)x,y,xa(n),y2a(n),ya(n)
INTEGER k,khi,klo
real(dl)a,b,h
klo=1
khi=n
1 if (khi-klo.gt.1) then
k=(khi+klo)/2
if(xa(k).gt.x)then
khi=k
else
klo=k
endif
goto 1
endif
h=xa(khi)-xa(klo)
if (h.eq.0.) stop 'bad xa input in splint'
a=(xa(khi)-x)/h
b=(x-xa(klo))/h
y=a*ya(klo)+b*ya(khi)+&
((a**3-a)*y2a(klo)+(b**3-b)*y2a(khi))*(h**2)/6.d0
END SUBROUTINE cubicsplint
!--------------------------------------------------------------------
subroutine setcgammappf
c_Gamma_ppf=0.4d0*sqrt(cs2_lam)
end subroutine setcgammappf
end module LambdaGeneral
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
!Return OmegaK - modify this if you add extra fluid components
function GetOmegak()
use precision
use ModelParams
real(dl) GetOmegak
GetOmegak = 1 - (CP%omegab+CP%omegac+CP%omegav+CP%omegan)
end function GetOmegak
subroutine init_background
use LambdaGeneral
!This is only called once per model, and is a good point to do any extra initialization.
!It is called before first call to dtauda, but after
!massive neutrinos are initialized and after GetOmegak
is_cosmological_constant = .not. use_tabulated_w .and. w_lam==-1_dl .and. wa_ppf==0._dl
end subroutine init_background
!Background evolution
function dtauda(a)
!get d tau / d a
use precision
use ModelParams
use MassiveNu
use LambdaGeneral
implicit none
real(dl) dtauda
real(dl), intent(IN) :: a
real(dl) rhonu,grhoa2, a2
integer nu_i
a2=a**2
! 8*pi*G*rho*a**4.
grhoa2=grhok*a2+(grhoc+grhob)*a+grhog+grhornomass
if (is_cosmological_constant) then
grhoa2=grhoa2+grhov*a2**2
else
grhoa2=grhoa2+ grho_de(a)
end if
if (CP%Num_Nu_massive /= 0) then
!Get massive neutrino density relative to massless
do nu_i = 1, CP%nu_mass_eigenstates
call Nu_rho(a*nu_masses(nu_i),rhonu)
grhoa2=grhoa2+rhonu*grhormass(nu_i)
end do
end if
dtauda=sqrt(3/grhoa2)
end function dtauda
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
!Gauge-dependent perturbation equations
module GaugeInterface
use precision
use ModelParams
use MassiveNu
use LambdaGeneral
use Errors
use Transfer
implicit none
public
!Description of this file. Change if you make modifications.
character(LEN=*), parameter :: Eqns_name = 'equations_ppf-Jan15'
integer, parameter :: basic_num_eqns = 5
logical :: DoTensorNeutrinos = .true.
logical :: DoLateRadTruncation = .true.
!if true, use smooth approx to radition perturbations after decoupling on
!small scales, saving evolution of irrelevant osciallatory multipole equations
logical, parameter :: second_order_tightcoupling = .true.
real(dl) :: Magnetic = 0._dl
!Vector mode anisotropic stress in units of rho_gamma
real(dl) :: vec_sig0 = 1._dl
!Vector mode shear
integer, parameter :: max_l_evolve = 256 !Maximum l we are ever likely to propagate
!Note higher values increase size of Evolution vars, hence memory
!Supported scalar initial condition flags
integer, parameter :: initial_adiabatic=1, initial_iso_CDM=2, &
initial_iso_baryon=3, initial_iso_neutrino=4, initial_iso_neutrino_vel=5, initial_vector = 0
integer, parameter :: initial_nummodes = initial_iso_neutrino_vel
type EvolutionVars
real(dl) q, q2
real(dl) k_buf,k2_buf ! set in initial
integer w_ix !Index of two quintessence equations
integer r_ix !Index of the massless neutrino hierarchy
integer g_ix !Index of the photon neutrino hierarchy
integer q_ix !index into q_evolve array that gives the value q
logical TransferOnly
! nvar - number of scalar (tensor) equations for this k
integer nvar,nvart, nvarv
!Max_l for the various hierarchies
integer lmaxg,lmaxnr,lmaxnu,lmaxgpol,MaxlNeeded
integer lmaxnrt, lmaxnut, lmaxt, lmaxpolt, MaxlNeededt
logical EvolveTensorMassiveNu(max_nu)
integer lmaxnrv, lmaxv, lmaxpolv
integer polind !index into scalar array of polarization hierarchy
!array indices for massive neutrino equations
integer nu_ix(max_nu), nu_pert_ix
integer nq(max_nu), lmaxnu_pert
logical has_nu_relativistic
!Initial values for massive neutrino v*3 variables calculated when switching
!to non-relativistic approx
real(dl) G11(max_nu),G30(max_nu)
!True when using non-relativistic approximation
logical MassiveNuApprox(max_nu)
real(dl) MassiveNuApproxTime(max_nu)
!True when truncating at l=2,3 when k*tau>>1 (see arXiv:1104.2933)
logical high_ktau_neutrino_approx
!Massive neutrino scheme being used at the moment
integer NuMethod
!True when using tight-coupling approximation (required for stability at early times)
logical TightCoupling, TensTightCoupling
real(dl) TightSwitchoffTime
!Numer of scalar equations we are propagating
integer ScalEqsToPropagate
integer TensEqsToPropagate
!beta > l for closed models
integer FirstZerolForBeta
!Tensor vars
real(dl) aux_buf
real(dl) pig, pigdot !For tight coupling
real(dl) poltruncfac
!PPF parameters
real(dl) dgrho_e_ppf, dgq_e_ppf
logical no_nu_multpoles, no_phot_multpoles
integer lmaxnu_tau(max_nu) !lmax for massive neutinos at time being integrated
logical nu_nonrelativistic(max_nu)
real(dl) denlk(max_l_evolve),denlk2(max_l_evolve), polfack(max_l_evolve)
real(dl) Kf(max_l_evolve)
integer E_ix, B_ix !tensor polarization indices
real(dl) denlkt(4,max_l_evolve),Kft(max_l_evolve)
real, pointer :: OutputTransfer(:) => null()
end type EvolutionVars
!precalculated arrays
real(dl) polfac(max_l_evolve),denl(max_l_evolve),vecfac(max_l_evolve),vecfacpol(max_l_evolve)
real(dl), parameter :: ep0=1.0d-2
integer, parameter :: lmaxnu_high_ktau=4 !Jan2015, increased from 3 to fix mpk for mnu~6eV
real(dl) epsw
real(dl) nu_tau_notmassless(nqmax0+1,max_nu), nu_tau_nonrelativistic(max_nu),nu_tau_massive(max_nu)
contains
subroutine GaugeInterface_ScalEv(EV,y,tau,tauend,tol1,ind,c,w)
type(EvolutionVars) EV
real(dl) c(24),w(EV%nvar,9), y(EV%nvar), tol1, tau, tauend
integer ind
call dverk(EV,EV%ScalEqsToPropagate,derivs,tau,y,tauend,tol1,ind,c,EV%nvar,w)
if (ind==-3) then
call GlobalError('Dverk error -3: the subroutine was unable to satisfy the error ' &
//'requirement with a particular step-size that is less than or * ' &
//'equal to hmin, which may mean that tol is too small' &
//'--- but most likely you''ve messed up the y array indexing; ' &
//'compiling with bounds checking may (or may not) help find the problem.',error_evolution)
end if
end subroutine GaugeInterface_ScalEv
function next_nu_nq(nq) result (next_nq)
integer, intent(in) :: nq
integer q, next_nq
if (nq==0) then
next_nq=1
else
q = nu_q(nq)
if (q>=10) then
next_nq = nqmax
else
next_nq = nq+1
end if
end if
end function next_nu_nq
recursive subroutine GaugeInterface_EvolveScal(EV,tau,y,tauend,tol1,ind,c,w)
use ThermoData
type(EvolutionVars) EV, EVout
real(dl) c(24),w(EV%nvar,9), y(EV%nvar), yout(EV%nvar), tol1, tau, tauend
integer ind, nu_i
real(dl) cs2, opacity, dopacity
real(dl) tau_switch_ktau, tau_switch_nu_massless, tau_switch_nu_massive, next_switch
real(dl) tau_switch_no_nu_multpoles, tau_switch_no_phot_multpoles,tau_switch_nu_nonrel
real(dl) noSwitch, smallTime
noSwitch= CP%tau0+1
smallTime = min(tau, 1/EV%k_buf)/100
tau_switch_ktau = noSwitch
tau_switch_no_nu_multpoles= noSwitch
tau_switch_no_phot_multpoles= noSwitch
!Massive neutrino switches
tau_switch_nu_massless = noSwitch
tau_switch_nu_nonrel = noSwitch
tau_switch_nu_massive= noSwitch
!Evolve equations from tau to tauend, performing switches in equations if necessary.
if (.not. EV%high_ktau_neutrino_approx .and. .not. EV%no_nu_multpoles ) then
tau_switch_ktau= max(20, EV%lmaxnr-4)/EV%k_buf
end if
if (CP%Num_Nu_massive /= 0) then
do nu_i = 1, CP%Nu_mass_eigenstates
if (EV%nq(nu_i) /= nqmax) then
tau_switch_nu_massless = min(tau_switch_nu_massless,nu_tau_notmassless(next_nu_nq(EV%nq(nu_i)),nu_i))
else if (.not. EV%nu_nonrelativistic(nu_i)) then
tau_switch_nu_nonrel = min(nu_tau_nonrelativistic(nu_i),tau_switch_nu_nonrel)
else if (EV%NuMethod==Nu_trunc .and..not. EV%MassiveNuApprox(nu_i)) then
tau_switch_nu_massive = min(tau_switch_nu_massive,EV%MassiveNuApproxTime(nu_i))
end if
end do
end if
if (DoLateRadTruncation) then
if (.not. EV%no_nu_multpoles) & !!.and. .not. EV%has_nu_relativistic .and. tau_switch_nu_massless ==noSwitch) &
tau_switch_no_nu_multpoles=max(15/EV%k_buf*AccuracyBoost,min(taurend,matter_verydom_tau))
if (.not. EV%no_phot_multpoles .and. (.not.CP%WantCls .or. EV%k_buf>0.03*AccuracyBoost)) &
tau_switch_no_phot_multpoles =max(15/EV%k_buf,taurend)*AccuracyBoost
end if
next_switch = min(tau_switch_ktau, tau_switch_nu_massless,EV%TightSwitchoffTime, tau_switch_nu_massive, &
tau_switch_no_nu_multpoles, tau_switch_no_phot_multpoles, tau_switch_nu_nonrel,noSwitch)
if (next_switch < tauend) then
if (next_switch > tau+smallTime) then
call GaugeInterface_ScalEv(EV, y, tau,next_switch,tol1,ind,c,w)
if (global_error_flag/=0) return
end if
EVout=EV
if (next_switch == EV%TightSwitchoffTime) then
!TightCoupling
EVout%TightCoupling=.false.
EVout%TightSwitchoffTime = noSwitch
call SetupScalarArrayIndices(EVout)
call CopyScalarVariableArray(y,yout, EV, EVout)
EV=EVout
y=yout
ind=1
!Set up variables with their tight coupling values
y(EV%g_ix+2) = EV%pig
call thermo(tau,cs2,opacity,dopacity)
if (second_order_tightcoupling) then
! Francis-Yan Cyr-Racine November 2010
y(EV%g_ix+3) = (3._dl/7._dl)*y(EV%g_ix+2)*(EV%k_buf/opacity)*(1._dl+dopacity/opacity**2) + &
(3._dl/7._dl)*EV%pigdot*(EV%k_buf/opacity**2)*(-1._dl)
y(EV%polind+2) = EV%pig/4 + EV%pigdot*(1._dl/opacity)*(-5._dl/8._dl- &
(25._dl/16._dl)*dopacity/opacity**2) + &
EV%pig*(EV%k_buf/opacity)**2*(-5._dl/56._dl)
y(EV%polind+3) = (3._dl/7._dl)*(EV%k_buf/opacity)*y(EV%polind+2)*(1._dl + &
dopacity/opacity**2) + (3._dl/7._dl)*(EV%k_buf/opacity**2)*((EV%pigdot/4._dl)* &
(1._dl+(5._dl/2._dl)*dopacity/opacity**2))*(-1._dl)
else
y(EV%g_ix+3) = 3./7*y(EV%g_ix+2)*EV%k_buf/opacity
y(EV%polind+2) = EV%pig/4
y(EV%polind+3) =y(EV%g_ix+3)/4
end if
else if (next_switch==tau_switch_ktau) then
!k tau >> 1, evolve massless neutrino effective fluid up to l=2
EVout%high_ktau_neutrino_approx=.true.
EV%nq(1:CP%Nu_mass_eigenstates) = nqmax
call SetupScalarArrayIndices(EVout)
call CopyScalarVariableArray(y,yout, EV, EVout)
y=yout
EV=EVout
else if (next_switch == tau_switch_nu_massless) then
!Mass starts to become important, start evolving next momentum mode
do nu_i = 1, CP%Nu_mass_eigenstates
if (EV%nq(nu_i) /= nqmax .and. &
next_switch == nu_tau_notmassless(next_nu_nq(EV%nq(nu_i)),nu_i)) then
EVOut%nq(nu_i) = next_nu_nq(EV%nq(nu_i))
call SetupScalarArrayIndices(EVout)
call CopyScalarVariableArray(y,yout, EV, EVout)
EV=EVout
y=yout
exit
end if
end do
else if (next_switch == tau_switch_nu_nonrel) then
!Neutrino becomes non-relativistic, don't need high L
do nu_i = 1, CP%Nu_mass_eigenstates
if (.not. EV%nu_nonrelativistic(nu_i) .and. next_switch==nu_tau_nonrelativistic(nu_i) ) then
EVout%nu_nonrelativistic(nu_i)=.true.
call SetupScalarArrayIndices(EVout)
call CopyScalarVariableArray(y,yout, EV, EVout)
EV=EVout
y=yout
exit
end if
end do
else if (next_switch == tau_switch_nu_massive) then
!Very non-relativistic neutrinos, switch to truncated velocity-weight hierarchy
do nu_i = 1, CP%Nu_mass_eigenstates
if (.not. EV%MassiveNuApprox(nu_i) .and. next_switch== EV%MassiveNuApproxTime(nu_i) ) then
call SwitchToMassiveNuApprox(EV,y, nu_i)
exit
end if
end do
else if (next_switch==tau_switch_no_nu_multpoles) then
!Turn off neutrino hierarchies at late time where slow and not needed.
ind=1
EVout%no_nu_multpoles=.true.
EVOut%nq(1:CP%Nu_mass_eigenstates ) = nqmax
call SetupScalarArrayIndices(EVout)
call CopyScalarVariableArray(y,yout, EV, EVout)
y=yout
EV=EVout
else if (next_switch==tau_switch_no_phot_multpoles) then
!Turn off photon hierarchies at late time where slow and not needed.
ind=1
EVout%no_phot_multpoles=.true.
call SetupScalarArrayIndices(EVout)
call CopyScalarVariableArray(y,yout, EV, EVout)
y=yout
EV=EVout
end if
call GaugeInterface_EvolveScal(EV,tau,y,tauend,tol1,ind,c,w)
return
end if
call GaugeInterface_ScalEv(EV,y,tau,tauend,tol1,ind,c,w)
end subroutine GaugeInterface_EvolveScal
subroutine GaugeInterface_EvolveTens(EV,tau,y,tauend,tol1,ind,c,w)
use ThermoData
type(EvolutionVars) EV, EVOut
real(dl) c(24),w(EV%nvart,9), y(EV%nvart),yout(EV%nvart), tol1, tau, tauend
integer ind
real(dl) opacity, cs2
if (EV%TensTightCoupling .and. tauend > EV%TightSwitchoffTime) then
if (EV%TightSwitchoffTime > tau) then
call dverk(EV,EV%TensEqsToPropagate, derivst,tau,y,EV%TightSwitchoffTime,tol1,ind,c,EV%nvart,w)
end if
EVOut=EV
EVOut%TensTightCoupling = .false.
call SetupTensorArrayIndices(EVout)
call CopyTensorVariableArray(y,yout,Ev, Evout)
Ev = EvOut
y=yout
call thermo(tau,cs2,opacity)
y(EV%g_ix+2)= 32._dl/45._dl*EV%k_buf/opacity*y(3)
y(EV%E_ix+2) = y(EV%g_ix+2)/4
end if
call dverk(EV,EV%TensEqsToPropagate, derivst,tau,y,tauend,tol1,ind,c,EV%nvart,w)
end subroutine GaugeInterface_EvolveTens
function DeltaTimeMaxed(a1,a2, tol) result(t)
real(dl) a1,a2,t
real(dl), optional :: tol
if (a1>1._dl) then
t=0
elseif (a2 > 1._dl) then
t = DeltaTime(a1,1.01_dl, tol)
else
t= DeltaTime(a1,a2, tol)
end if
end function DeltaTimeMaxed
subroutine GaugeInterface_Init
!Precompute various arrays and other things independent of wavenumber
integer j, nu_i
real(dl) a_nonrel, a_mass,a_massive, time, nu_mass
epsw = 100/CP%tau0
if (CP%WantScalars) then
do j=2,max_l_evolve
polfac(j)=real((j+3)*(j-1),dl)/(j+1)
end do
end if
if (CP%WantVectors) then
do j=2,max_l_evolve
vecfac(j)=real((j+2),dl)/(j+1)
vecfacpol(j)=real((j+3)*j,dl)*(j-1)*vecfac(j)/(j+1)**2
end do
end if
do j=1,max_l_evolve
denl(j)=1._dl/(2*j+1)
end do
do nu_i=1, CP%Nu_Mass_eigenstates
nu_mass = max(0.1_dl,nu_masses(nu_i))
a_mass = 1.e-1_dl/nu_mass/lAccuracyBoost
!if (HighAccuracyDefault) a_mass=a_mass/4
time=DeltaTime(0._dl,nu_q(1)*a_mass)
nu_tau_notmassless(1, nu_i) = time
do j=2,nqmax
!times when each momentum mode becomes signficantly nonrelativistic
time= time + DeltaTimeMaxed(nu_q(j-1)*a_mass,nu_q(j)*a_mass, 0.01_dl)
nu_tau_notmassless(j, nu_i) = time
end do
a_nonrel = 2.5d0/nu_mass*AccuracyBoost !!!Feb13tweak
nu_tau_nonrelativistic(nu_i) =DeltaTimeMaxed(0._dl,a_nonrel)
a_massive = 17.d0/nu_mass*AccuracyBoost
nu_tau_massive(nu_i) =nu_tau_nonrelativistic(nu_i) + DeltaTimeMaxed(a_nonrel,a_massive)
end do
end subroutine GaugeInterface_Init
subroutine SetupScalarArrayIndices(EV, max_num_eqns)
!Set up array indices after the lmax have been decided
use MassiveNu
!Set the numer of equations in each hierarchy, and get total number of equations for this k
type(EvolutionVars) EV
integer, intent(out), optional :: max_num_eqns
integer neq, maxeq, nu_i
neq=basic_num_eqns
maxeq=neq
if (.not. EV%no_phot_multpoles) then
!Photon multipoles
EV%g_ix=basic_num_eqns+1
if (EV%TightCoupling) then
neq=neq+2
else
neq = neq+ (EV%lmaxg+1)
!Polarization multipoles
EV%polind = neq -1 !polind+2 is L=2, for polarizationthe first calculated
neq=neq + EV%lmaxgpol-1
end if
end if
if (.not. EV%no_nu_multpoles) then
!Massless neutrino multipoles
EV%r_ix= neq+1
if (EV%high_ktau_neutrino_approx) then
neq=neq + 3
else
neq=neq + (EV%lmaxnr+1)
end if
end if
maxeq = maxeq + (EV%lmaxg+1)+(EV%lmaxnr+1)+EV%lmaxgpol-1
!Dark energy
if (.not. is_cosmological_constant) then
EV%w_ix = neq+1
neq=neq+1 !ppf
maxeq=maxeq+1
else
EV%w_ix=0
end if
!Massive neutrinos
if (CP%Num_Nu_massive /= 0) then
EV%has_nu_relativistic = any(EV%nq(1:CP%Nu_Mass_eigenstates)/=nqmax)
if (EV%has_nu_relativistic) then
EV%lmaxnu_pert=EV%lmaxnu
EV%nu_pert_ix=neq+1
neq = neq+ EV%lmaxnu_pert+1
maxeq=maxeq+ EV%lmaxnu_pert+1
else
EV%lmaxnu_pert=0
end if
do nu_i=1, CP%Nu_Mass_eigenstates
if (EV%high_ktau_neutrino_approx) then
EV%lmaxnu_tau(nu_i) = lmaxnu_high_ktau *lAccuracyBoost
if (CP%Transfer%accurate_massive_neutrinos) EV%lmaxnu_tau(nu_i) = EV%lmaxnu_tau(nu_i) *3
else
EV%lmaxnu_tau(nu_i) =max(min(nint(0.8_dl*EV%q*nu_tau_nonrelativistic(nu_i)*lAccuracyBoost),EV%lmaxnu),3)
!!!Feb13tweak
if (EV%nu_nonrelativistic(nu_i)) EV%lmaxnu_tau(nu_i)=min(EV%lmaxnu_tau(nu_i),nint(4*lAccuracyBoost))
end if
if (nu_masses(nu_i) > 5000 .and. CP%Transfer%high_precision) EV%lmaxnu_tau(nu_i) = EV%lmaxnu_tau(nu_i)*2 !megadamping
EV%lmaxnu_tau(nu_i)=min(EV%lmaxnu,EV%lmaxnu_tau(nu_i))
EV%nu_ix(nu_i)=neq+1
if (EV%MassiveNuApprox(nu_i)) then
neq = neq+4
else
neq = neq+ EV%nq(nu_i)*(EV%lmaxnu_tau(nu_i)+1)
endif
maxeq = maxeq + nqmax*(EV%lmaxnu+1)
end do
else
EV%has_nu_relativistic = .false.
end if
EV%ScalEqsToPropagate = neq
if (present(max_num_eqns)) then
max_num_eqns=maxeq
end if
end subroutine SetupScalarArrayIndices
subroutine CopyScalarVariableArray(y,yout, EV, EVout)
type(EvolutionVars) EV, EVOut
real(dl), intent(in) :: y(EV%nvar)
real(dl), intent(out) :: yout(EVout%nvar)
integer lmax,i, nq
integer nnueq,nu_i, ix_off, ix_off2, ind, ind2
real(dl) q, pert_scale
yout=0
yout(1:basic_num_eqns) = y(1:basic_num_eqns)
if (.not. is_cosmological_constant) then
yout(EVout%w_ix)=y(EV%w_ix)
end if
if (.not. EV%no_phot_multpoles .and. .not. EVout%no_phot_multpoles) then
if (EV%TightCoupling .or. EVOut%TightCoupling) then
lmax=1
else
lmax = min(EV%lmaxg,EVout%lmaxg)
end if
yout(EVout%g_ix:EVout%g_ix+lmax)=y(EV%g_ix:EV%g_ix+lmax)
if (.not. EV%TightCoupling .and. .not. EVOut%TightCoupling) then
lmax = min(EV%lmaxgpol,EVout%lmaxgpol)
yout(EVout%polind+2:EVout%polind+lmax)=y(EV%polind+2:EV%polind+lmax)
end if
end if
if (.not. EV%no_nu_multpoles .and. .not. EVout%no_nu_multpoles) then
if (EV%high_ktau_neutrino_approx .or. EVout%high_ktau_neutrino_approx) then
lmax=2
else
lmax = min(EV%lmaxnr,EVout%lmaxnr)
end if
yout(EVout%r_ix:EVout%r_ix+lmax)=y(EV%r_ix:EV%r_ix+lmax)
end if
if (CP%Num_Nu_massive /= 0) then
do nu_i=1,CP%Nu_mass_eigenstates
ix_off=EV%nu_ix(nu_i)
ix_off2=EVOut%nu_ix(nu_i)
if (EV%MassiveNuApprox(nu_i) .and. EVout%MassiveNuApprox(nu_i)) then
nnueq=4
yout(ix_off2:ix_off2+nnueq-1)=y(ix_off:ix_off+nnueq-1)
else if (.not. EV%MassiveNuApprox(nu_i) .and. .not. EVout%MassiveNuApprox(nu_i)) then
lmax=min(EV%lmaxnu_tau(nu_i),EVOut%lmaxnu_tau(nu_i))
nq = min(EV%nq(nu_i), EVOut%nq(nu_i))
do i=1,nq
ind= ix_off + (i-1)*(EV%lmaxnu_tau(nu_i)+1)
ind2=ix_off2+ (i-1)*(EVOut%lmaxnu_tau(nu_i)+1)
yout(ind2:ind2+lmax) = y(ind:ind+lmax)
end do
do i=nq+1, EVOut%nq(nu_i)
lmax = min(EVOut%lmaxnu_tau(nu_i), EV%lmaxnr)
ind2=ix_off2+ (i-1)*(EVOut%lmaxnu_tau(nu_i)+1)
yout(ind2:ind2+lmax) = y(EV%r_ix:EV%r_ix+lmax)
!Add leading correction for the mass
q=nu_q(i)
pert_scale=(nu_masses(nu_i)/q)**2/2
lmax = min(lmax,EV%lmaxnu_pert)
yout(ind2:ind2+lmax) = yout(ind2:ind2+lmax) &
+ y(EV%nu_pert_ix:EV%nu_pert_ix+lmax)*pert_scale
end do
end if
end do
if (EVOut%has_nu_relativistic .and. EV%has_nu_relativistic) then
lmax = min(EVOut%lmaxnu_pert, EV%lmaxnu_pert)
yout(EVout%nu_pert_ix:EVout%nu_pert_ix+lmax)= y(EV%nu_pert_ix:EV%nu_pert_ix+lmax)
end if
end if
end subroutine CopyScalarVariableArray
subroutine SetupTensorArrayIndices(EV, maxeq)
type(EvolutionVars) EV
integer nu_i, neq
integer, optional, intent(out) :: maxeq
neq=3
EV%g_ix = neq-1 !EV%g_ix+2 is quadrupole
if (.not. EV%TensTightCoupling) then
EV%E_ix = EV%g_ix + (EV%lmaxt-1)
EV%B_ix = EV%E_ix + (EV%lmaxpolt-1)
neq = neq+ (EV%lmaxt-1)+(EV%lmaxpolt-1)*2
end if
if (present(maxeq)) then
maxeq =3 + (EV%lmaxt-1)+(EV%lmaxpolt-1)*2
end if
EV%r_ix = neq -1
if (DoTensorNeutrinos) then
neq = neq + EV%lmaxnrt-1
if (present(maxeq)) maxeq = maxeq+EV%lmaxnrt-1
if (CP%Num_Nu_massive /= 0 ) then
do nu_i=1, CP%nu_mass_eigenstates
EV%EvolveTensorMassiveNu(nu_i) = nu_tau_nonrelativistic(nu_i) < 0.8*tau_maxvis*AccuracyBoost
if (EV%EvolveTensorMassiveNu(nu_i)) then
EV%nu_ix(nu_i)=neq-1
neq = neq+ nqmax*(EV%lmaxnut-1)
if (present(maxeq)) maxeq = maxeq + nqmax*(EV%lmaxnut-1)
end if
end do
end if
end if
EV%TensEqsToPropagate = neq
end subroutine SetupTensorArrayIndices
subroutine CopyTensorVariableArray(y,yout, EV, EVout)
type(EvolutionVars) EV, EVOut
real(dl), intent(in) :: y(EV%nvart)
real(dl), intent(out) :: yout(EVout%nvart)
integer lmaxpolt, lmaxt, nu_i, ind, ind2, i
yout=0
yout(1:3) = y(1:3)
if (.not. EVOut%TensTightCoupling .and. .not.EV%TensTightCoupling) then
lmaxt = min(EVOut%lmaxt,EV%lmaxt)
yout(EVout%g_ix+2:EVout%g_ix+lmaxt)=y(EV%g_ix+2:EV%g_ix+lmaxt)
lmaxpolt = min(EV%lmaxpolt, EVOut%lmaxpolt)
yout(EVout%E_ix+2:EVout%E_ix+lmaxpolt)=y(EV%E_ix+2:EV%E_ix+lmaxpolt)
yout(EVout%B_ix+2:EVout%B_ix+lmaxpolt)=y(EV%B_ix+2:EV%B_ix+lmaxpolt)
end if
if (DoTensorNeutrinos) then
lmaxt=min(EV%lmaxnrt,EVOut%lmaxnrt)
yout(EVout%r_ix+2:EVout%r_ix+lmaxt)=y(EV%r_ix+2:EV%r_ix+lmaxt)
do nu_i =1, CP%nu_mass_eigenstates
if (EV%EvolveTensorMassiveNu(nu_i)) then
lmaxt=min(EV%lmaxnut,EVOut%lmaxnut)
do i=1,nqmax
ind= EV%nu_ix(nu_i) + (i-1)*(EV%lmaxnut-1)
ind2=EVOut%nu_ix(nu_i)+ (i-1)*(EVOut%lmaxnut-1)
yout(ind2+2:ind2+lmaxt) = y(ind+2:ind+lmaxt)
end do
end if
end do
end if
end subroutine CopyTensorVariableArray
subroutine GetNumEqns(EV)
use MassiveNu
!Set the numer of equations in each hierarchy, and get total number of equations for this k
type(EvolutionVars) EV
real(dl) scal, max_nu_mass
integer nu_i,q_rel,j
if (CP%Num_Nu_massive == 0) then
EV%lmaxnu=0
max_nu_mass=0
else
max_nu_mass = maxval(nu_masses(1:CP%Nu_mass_eigenstates))
do nu_i = 1, CP%Nu_mass_eigenstates
!Start with momentum modes for which t_k ~ time at which mode becomes non-relativistic
q_rel=0
do j=1, nqmax
!two different q's here EV%q ~k
if (nu_q(j) > nu_masses(nu_i)*adotrad/EV%q) exit
q_rel = q_rel + 1
end do
if (q_rel>= nqmax-2 .or. CP%WantTensors) then
EV%nq(nu_i)=nqmax
else
EV%nq(nu_i)=q_rel
end if
!q_rel = nint(nu_masses(nu_i)*adotrad/EV%q) !two dffierent q's here EV%q ~k
!EV%nq(nu_i)=max(0,min(nqmax0,q_rel)) !number of momentum modes to evolve intitially
EV%nu_nonrelativistic(nu_i) = .false.
end do
EV%NuMethod = CP%MassiveNuMethod
if (EV%NuMethod == Nu_Best) EV%NuMethod = Nu_Trunc
!l_max for massive neutrinos
if (CP%Transfer%high_precision) then
EV%lmaxnu=nint(25*lAccuracyBoost)
else
EV%lmaxnu=max(3,nint(10*lAccuracyBoost))
if (max_nu_mass>700) EV%lmaxnu=max(3,nint(15*lAccuracyBoost)) !Feb13 tweak
endif
end if
if (CP%closed) then
EV%FirstZerolForBeta = nint(EV%q*CP%r)
else
EV%FirstZerolForBeta=l0max !a large number
end if
EV%high_ktau_neutrino_approx = .false.
if (CP%WantScalars) then
EV%TightCoupling=.true.
EV%no_phot_multpoles =.false.
EV%no_nu_multpoles =.false.
EV%MassiveNuApprox=.false.
if (HighAccuracyDefault .and. CP%AccuratePolarization) then
EV%lmaxg = max(nint(11*lAccuracyBoost),3)
else
EV%lmaxg = max(nint(8*lAccuracyBoost),3)
end if
EV%lmaxnr = max(nint(14*lAccuracyBoost),3)
if (max_nu_mass>700 .and. HighAccuracyDefault) EV%lmaxnr = max(nint(32*lAccuracyBoost),3) !Feb13 tweak
EV%lmaxgpol = EV%lmaxg
if (.not.CP%AccuratePolarization) EV%lmaxgpol=max(nint(4*lAccuracyBoost),3)
if (EV%q < 0.05) then
!Large scales need fewer equations
scal = 1
if (CP%AccuratePolarization) scal = 4 !But need more to get polarization right
EV%lmaxgpol=max(3,nint(min(8,nint(scal* 150* EV%q))*lAccuracyBoost))
EV%lmaxnr=max(3,nint(min(7,nint(sqrt(scal)* 150 * EV%q))*lAccuracyBoost))
EV%lmaxg=max(3,nint(min(8,nint(sqrt(scal) *300 * EV%q))*lAccuracyBoost))
if (CP%AccurateReionization) then
EV%lmaxg=EV%lmaxg*4
EV%lmaxgpol=EV%lmaxgpol*2
end if
end if
if (EV%TransferOnly) then
EV%lmaxgpol = min(EV%lmaxgpol,nint(5*lAccuracyBoost))
EV%lmaxg = min(EV%lmaxg,nint(6*lAccuracyBoost))
end if
if (CP%Transfer%high_precision) then
if (HighAccuracyDefault) then
EV%lmaxnr=max(nint(45*lAccuracyBoost),3)
else
EV%lmaxnr=max(nint(30*lAccuracyBoost),3)
endif
if (EV%q > 0.04 .and. EV%q < 0.5) then !baryon oscillation scales
EV%lmaxg=max(EV%lmaxg,10)
end if
end if
if (CP%closed) then
EV%lmaxnu=min(EV%lmaxnu, EV%FirstZerolForBeta-1)
EV%lmaxnr=min(EV%lmaxnr, EV%FirstZerolForBeta-1)
EV%lmaxg=min(EV%lmaxg, EV%FirstZerolForBeta-1)
EV%lmaxgpol=min(EV%lmaxgpol, EV%FirstZerolForBeta-1)
end if
EV%poltruncfac=real(EV%lmaxgpol,dl)/max(1,(EV%lmaxgpol-2))
EV%MaxlNeeded=max(EV%lmaxg,EV%lmaxnr,EV%lmaxgpol,EV%lmaxnu)
if (EV%MaxlNeeded > max_l_evolve) stop 'Need to increase max_l_evolve'
call SetupScalarArrayIndices(EV,EV%nvar)
if (CP%closed) EV%nvar=EV%nvar+1 !so can reference lmax+1 with zero coefficient
EV%lmaxt=0
else
EV%nvar=0
end if
if (CP%WantTensors) then
EV%TensTightCoupling = .true.
EV%lmaxt=max(3,nint(8*lAccuracyBoost))
EV%lmaxpolt = max(3,nint(4*lAccuracyBoost))
! if (EV%q < 1e-3) EV%lmaxpolt=EV%lmaxpolt+1
if (DoTensorNeutrinos) then
EV%lmaxnrt=nint(6*lAccuracyBoost)
EV%lmaxnut=EV%lmaxnrt
else
EV%lmaxnut=0
EV%lmaxnrt=0
end if
if (CP%closed) then
EV%lmaxt=min(EV%FirstZerolForBeta-1,EV%lmaxt)
EV%lmaxpolt=min(EV%FirstZerolForBeta-1,EV%lmaxpolt)
EV%lmaxnrt=min(EV%FirstZerolForBeta-1,EV%lmaxnrt)
EV%lmaxnut=min(EV%FirstZerolForBeta-1,EV%lmaxnut)
end if