-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodpk_odeint.f90
More file actions
1461 lines (1147 loc) · 41.3 KB
/
modpk_odeint.f90
File metadata and controls
1461 lines (1147 loc) · 41.3 KB
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
MODULE modpk_odeint
!Module that controls the numerical integration of the equations of motion for
!both the background and the modes. Has various cosmology checks implemented
!in addition to numerical checking.
use modpkparams, only : dp
use camb_interface, only : pk_bad
use modpk_icsampling, only : ic_sampling, ic_flags
use dvode_f90_m, only : vode_opts, set_normal_opts, dvode_f90, get_stats, &
set_intermediate_opts
use modpk_io, only : out_opt
use csv_file, only : csv_write
use modpk_errorhandling, only : raise, run_outcome
implicit none
interface odeint
module procedure odeint_r
module procedure odeint_c
end interface
public :: odeint
contains
subroutine odeint_r(ystart,x1,x2,eps,h1,hmin,derivs,rkqs_r)
use ode_path
use internals
use modpk_observables
use modpkparams
use potential
use modpk_utils, only : reallocate_rv, reallocate_rm, bderivs_dvode
implicit none
real(dp), DIMENSION(:), INTENT(INOUT) :: ystart
real(dp), INTENT(IN) :: x1,x2,eps,h1,hmin
!MULTIFIELD
real(dp), DIMENSION(num_inflaton) :: phi, dphi
!END MULTIFIELD
INTERFACE
SUBROUTINE derivs(x,y,dydx)
use modpkparams
IMPLICIT NONE
real(dp), INTENT(IN) :: x
real(dp), DIMENSION(:), INTENT(IN) :: y
real(dp), DIMENSION(:), INTENT(OUT) :: dydx
END SUBROUTINE derivs
SUBROUTINE rkqs_r(y,dydx,x,htry,eps,yscal,hdid,hnext,derivs)
use modpkparams
IMPLICIT NONE
real(dp), DIMENSION(:), INTENT(INOUT) :: y
real(dp), DIMENSION(:), INTENT(IN) :: dydx,yscal
real(dp), INTENT(INOUT) :: x
real(dp), INTENT(IN) :: htry,eps
real(dp), INTENT(OUT) :: hdid,hnext
INTERFACE
SUBROUTINE derivs(x,y,dydx)
use modpkparams
IMPLICIT NONE
real(dp), INTENT(IN) :: x
real(dp), DIMENSION(:), INTENT(IN) :: y
real(dp), DIMENSION(:), INTENT(OUT) :: dydx
END SUBROUTINE derivs
END INTERFACE
END SUBROUTINE rkqs_r
END INTERFACE
real(dp), PARAMETER :: TINY=1.0e-30_dp
INTEGER, PARAMETER :: MAXSTP=nsteps
INTEGER*4 :: nstp,i
real(dp) :: h,hdid,hnext,x,xsav
real(dp), DIMENSION(SIZE(ystart)) :: dydx, y, yscal
real(dp) :: z, scalefac
real(dp) :: infl_efolds, infl_efolds_start
logical :: infl_checking
logical :: leave
!For DVODE integrator
integer :: neq, istats(31)
integer :: itask, istate
real(dp) :: rtol, rstats(22), nefold_out, dN_step
real(dp), dimension(:), allocatable :: atol
type (vode_opts) :: ode_integrator_opt
!Inits for checking whether in
!extended inflation period (for IC scan)
infl_checking = .false.
infl_efolds_start = 0e0_dp
ode_underflow=.FALSE.
infl_ended=.FALSE.
x=x1
h=SIGN(h1,x2-x1)
nok=0
nbad=0
kount=0
y(:)=ystart(:)
NULLIFY(xp,yp)
IF (save_steps) THEN
xsav=x-2.0e0_dp*dxsav
ALLOCATE(xp(256))
ALLOCATE(yp(SIZE(ystart),SIZE(xp)))
END IF
if (tech_opt%use_dvode_integrator) then
!Options for first call to dvode integrator
call initialize_dvode()
end if
DO nstp=1,MAXSTP
if (any(isnan(y))) then
print*, "MODECODE: E-fold",x
print*, "MODECODE: nstp",nstp
print*, "MODECODE: y", y
call raise%fatal_code(&
"y has a NaN value in odeint_r.",&
__FILE__, __LINE__)
end if
!Calc bundle exp_scalar by integrating tr(d2Vdphi2)
if (nstp==1) then
field_bundle%N=0e0_dp
field_bundle%dlogThetadN=0e0_dp
field_bundle%exp_scalar=1e0_dp
end if
call field_bundle%calc_exp_scalar(y(1:num_inflaton),x)
!Record the background trajectory
if (out_opt%save_traj) call print_traj()
CALL derivs(x,y,dydx)
!If get bad deriv, then override this error when IC sampling
if (pk_bad /= run_outcome%success) return
IF (save_steps .AND. (ABS(x-xsav) > ABS(dxsav))) &
CALL save_a_step
if (tech_opt%use_dvode_integrator) then
call dvode_f90(bderivs_dvode,neq,y,x,nefold_out, &
itask,istate,ode_integrator_opt)
call get_stats(rstats,istats)
if (istate<0) then
print*, "MODECODE istate=", istate
call raise%fatal_code(&
"The dvode_f90 integrator threw an error. &
Check the documentation there.",&
__FILE__, __LINE__)
end if
else
yscal(:)=ABS(y(:))+ABS(h*dydx(:))+TINY
IF ((x+h-x2)*(x+h-x1) > 0.0) h = x2 - x
CALL rkqs_r(y,dydx,x,h,eps,yscal,hdid,hnext,derivs)
IF (hdid == h) THEN
nok=nok+1
ELSE
nbad=nbad+1
END IF
end if
call check_for_eternal_inflation()
!MULTIFIELD
phi = y(1:num_inflaton)
dphi = y(num_inflaton+1 : 2*num_inflaton)
call check_inflation_started_properly()
!END MULTIFIELD
call check_inflation_ended_properly(leave)
if (leave) then
!Record the background trajectory
if (out_opt%save_traj) call print_traj()
return
end if
if (abs(x2-x)<1e-10) then
print*, "MODECODE: y=",y
print*, "MODECODE: Efolds=",x
call raise%fatal_code(&
"Reached the end of the integration in N. &
Could try to increase the max number of steps, &
but more likely that the integrator is taking steps &
that are too small. Potentially stiff problem.", &
__FILE__, __LINE__)
end if
IF (ode_underflow) RETURN
if ( .not. tech_opt%use_dvode_integrator) then
IF (ABS(hnext) < hmin) THEN
call raise%fatal_code(&
'stepsize smaller than minimum in odeint_r', &
__FILE__, __LINE__)
END IF
end if
!Set up next N-step
if (tech_opt%use_dvode_integrator) then
if (itask/=2) nefold_out = min(x + dN_step, x2)
!Increase accuracy requirements when not in SR
if (tech_opt%accuracy_setting>0) then
if (getEps(phi,dphi)>0.2e0_dp) then
rtol=1e-12_dp
atol=1e-12_dp
istate=3
end if
end if
else
h=hnext
end if
END DO
!It got to the end without going through enough inflation to even be called
!"slowroll_start"
if (getEps(y(1:num_inflaton),y(num_inflaton+1:2*num_inflaton))>1.0e0_dp) then
pk_bad = run_outcome%infl_didnt_start
call raise%warning(&
"N-integration finished with eps>1.0 and &
without inflating or only transient periods of inflation.")
return
else
PRINT*, 'MODECODE: nsteps', nstp, MAXSTP
PRINT*, "MODECODE: E-fold", x
print*, "MODECODE: Step size", h
print*, "MODECODE: epsilon=", getEps(y(1:num_inflaton),y(num_inflaton+1:2*num_inflaton))
print*, "MODECODE: V=", pot(y(1:num_inflaton))
PRINT*, "MODECODE: y=", y
ode_underflow=.TRUE.
call raise%warning(&
'Too many steps in odeint_r.', __FILE__, __LINE__)
end if
CONTAINS
! (C) Copr. 1986-92 Numerical Recipes Software, adapted.
SUBROUTINE save_a_step
kount=kount+1
IF (kount > SIZE(xp)) THEN
xp=>reallocate_rv(xp,2*SIZE(xp))
yp=>reallocate_rm(yp,SIZE(yp,1), SIZE(xp))
END IF
xp(kount)=x
yp(:,kount)=y(:)
xsav=x
END SUBROUTINE save_a_step
subroutine initialize_dvode()
neq = size(y)
if (allocated(atol)) deallocate(atol)
allocate(atol(neq))
!Relative tolerance
!Absolute tolerance
if (tech_opt%accuracy_setting==2) then
rtol = 1.e-10_dp
atol = 1.0e-14_dp
else if (tech_opt%accuracy_setting==1) then
rtol = 1.e-6_dp
atol = 1.0e-6_dp
else if (tech_opt%accuracy_setting==0) then
rtol = 1.e-5_dp
atol = 1.0e-5_dp
else if (tech_opt%accuracy_setting==-1) then
rtol = tech_opt%dvode_rtol_back
atol = tech_opt%dvode_atol_back(1:neq)
else
print*, "MODECODE: accuracy_setting =", tech_opt%accuracy_setting
call raise%fatal_code(&
"This accuracy_setting is not supported in initialize_dvode.",&
__FILE__, __LINE__)
end if
istate = 1 !Set =1 for 1st call to integrator
itask = 1 !Indicates normal usage, see dvode_f90_m.f90 for other values
!itask = 2 !Take only one time-step and output
if (itask /=2) then
!Integrate until nefold_out
!dN_step = sign(0.001e0_dp,x2-x1)
!dN_step = sign(0.01e0_dp,x2-x1)
dN_step = sign(0.001e0_dp,x2-x1)
nefold_out = x + dN_step
else
!Take only one step
nefold_out = Nefold_max
end if
!Force initial step-size guess very small
ode_integrator_opt = set_intermediate_opts(dense_j=.true.,&
abserr_vector=atol,&
relerr=rtol,&
user_supplied_jacobian=.false., &
mxstep=50000,&
H0=1e-9_dp)
end subroutine initialize_dvode
subroutine print_traj()
integer :: ii
character(1024) :: cname
logical :: adv
!Write the column header
if (out_opt%first_trajout) then
!First column
call csv_write(&
out_opt%trajout,&
'N', &
advance=.false.)
!Next num_inflaton columns
do ii=1,num_inflaton
write(cname, "(A3,I4.4)") "phi", ii
call csv_write(&
out_opt%trajout,&
trim(cname), &
advance=.false.)
end do
do ii=1,num_inflaton
write(cname, "(A4,I4.4)") "dphi", ii
call csv_write(&
out_opt%trajout,&
trim(cname), &
advance=.false.)
end do
call csv_write(&
out_opt%trajout,&
(/character(len=10) ::&
'V', 'eps','H','eta'/), &
advance=.false.)
do ii=1,num_inflaton
write(cname, "(A2,I4.4)") "dV", ii
if (ii==num_inflaton) then
adv=.true.
else
adv=.false.
end if
call csv_write(&
out_opt%trajout,&
trim(cname), &
advance=adv)
end do
out_opt%first_trajout = .false.
end if
!Write the trajectory
call csv_write(&
out_opt%trajout,&
x, &
advance=.false.)
call csv_write(&
out_opt%trajout,&
y(:), &
advance=.false.)
call csv_write(&
out_opt%trajout,&
pot(y(1:num_inflaton)),&
advance=.false.)
call csv_write(&
out_opt%trajout,&
getEps(y(1:num_inflaton),y(num_inflaton+1:2*num_inflaton)), &
advance=.false.)
call csv_write(&
out_opt%trajout,&
getH(y(1:num_inflaton),y(num_inflaton+1:2*num_inflaton)), &
advance=.false.)
call csv_write(&
out_opt%trajout,&
geteta(y(1:num_inflaton),y(num_inflaton+1:2*num_inflaton)), &
advance=.false.)
call csv_write(&
out_opt%trajout,&
dVdphi(y(1:num_inflaton)), &
advance=.true.)
!call csv_write(&
! out_opt%trajout,&
! d2Vdphi2(y(1:num_inflaton)), &
! advance=.true.)
end subroutine print_traj
subroutine check_for_eternal_inflation
IF ((x-x2)*(x2-x1) > 0.0e0_dp) THEN
WRITE(*, *) 'MODECODE: x, x1, x2 :', x, x1, x2
WRITE(*,*) 'MODECODE: vparams: ', (vparams(i,:),i=1,size(vparams,1))
IF (.NOT.instreheat) WRITE(*,*) 'MODECODE: N_pivot: ', N_pivot
call raise%fatal_cosmo(&
"This could be a model for which inflation does not end. &
Either adjust phi_init or use slowroll_infl_end for a potential &
for which inflation does not end by breakdown of slowroll.", &
__FILE__, __LINE__)
END IF
end subroutine check_for_eternal_inflation
subroutine check_inflation_started_properly()
IF(getEps(phi,dphi) .LT. 1 .AND. .NOT.(slowroll_start)) then
if (ic_sampling==ic_flags%slowroll_samp .or. &
ic_sampling==ic_flags%iso_N .or.&
ic_sampling==ic_flags%reg_samp) then
slowroll_start=.true.
else
!If scan ICs, say inflating iff eps<1 for "extended" period,
!3 efolds - protects against transient inflation epochs, i.e.,
!at traj turn-around or chance starting with dphi=0
if (.not. infl_checking) then
infl_checking = .true.
infl_efolds_start = x
else
infl_efolds = x - infl_efolds_start
if (infl_efolds > 3.0) then
slowroll_start=.true.
end if
end if
end if
else if (infl_checking) then
infl_checking=.false.
endif
end subroutine check_inflation_started_properly
subroutine check_inflation_ended_properly(leave)
logical, intent(inout) :: leave
leave = .false.
IF(ode_infl_end) THEN
IF (slowroll_infl_end) THEN
IF(getEps(phi, dphi) .GT. 1 .AND. slowroll_start) THEN
infl_ended = .TRUE.
ystart(:) = y(:)
IF (save_steps) CALL save_a_step
leave = .true.
RETURN
ENDIF
ELSE
IF(getEps(phi, dphi) .GT. 1 .AND. slowroll_start) THEN
PRINT*,'MODECODE: epsilon =', getEps(phi, dphi)
call raise%fatal_cosmo(&
'You asked for a no-slowroll-breakdown model, but inflation &
already ended via slowroll violation before your phi_end was &
reached. Please take another look at your inputs.',&
__FILE__, __LINE__)
ENDIF
!MULTIFIELD
IF (size(phi) .eq. 1) THEN
IF (phidot_sign(1).GT.0..AND.(phi(1).GT.(phi_infl_end(1)+0.1))) THEN
infl_ended=.TRUE.
ystart(:)=y(:)
IF (save_steps) CALL save_a_step
leave=.true.
RETURN
ENDIF
IF (phidot_sign(1).LT.0..AND.(phi(1).LT.(phi_infl_end(1)-0.1))) THEN
infl_ended=.TRUE.
ystart(:)=y(:)
IF (save_steps) CALL save_a_step
leave=.true.
RETURN
ENDIF
ELSE
! for multifield, determine the total field distance travelled
if(alternate_infl_end(phi,dphi)) then
infl_ended = .true.
ystart(:) = y(:)
IF (save_steps) CALL save_a_step
leave=.true.
RETURN
end if
END IF
!END MULTIFIELD
ENDIF
ENDIF
end subroutine check_inflation_ended_properly
END SUBROUTINE odeint_r
! Only called for ptb mode eqns
SUBROUTINE odeint_c(ystart, x1, x2, eps, h1, hmin, derivs, qderivs, rkqs_c)
USE ode_path
USE internals
USE modpk_observables
USE modpkparams
USE potential, only : tensorpower, getH, getEps, zpower,&
powerspectrum
use modpk_utils, only : reallocate_rv, reallocate_rm, mode_derivs_dvode, &
qderivs_dvode
IMPLICIT NONE
COMPLEX(KIND=DP), DIMENSION(:), INTENT(INOUT) :: ystart
real(dp), INTENT(IN) :: x1,x2,eps,h1,hmin
real(dp) :: eps_adjust
real(dp), DIMENSION(num_inflaton) :: phi, delphi ! the classical field phi and dphi are real
COMPLEX(KIND=DP), DIMENSION(size(ystart)) :: ytmp
LOGICAL :: use_q, compute_zpower
INTERFACE
SUBROUTINE derivs(x, y, dydx)
USE modpkparams
IMPLICIT NONE
real(dp), INTENT(IN) :: x
COMPLEX(KIND=DP), DIMENSION(:), INTENT(IN) :: y
COMPLEX(KIND=DP), DIMENSION(:), INTENT(OUT) :: dydx
END SUBROUTINE derivs
SUBROUTINE qderivs(x, y, dydx)
USE modpkparams
IMPLICIT NONE
real(dp), INTENT(IN) :: x
COMPLEX(KIND=DP), DIMENSION(:), INTENT(IN) :: y
COMPLEX(KIND=DP), DIMENSION(:), INTENT(OUT) :: dydx
END SUBROUTINE qderivs
SUBROUTINE rkqs_c(y,dydx,x,htry,eps,yscal,hdid,hnext,derivs)
USE modpkparams
IMPLICIT NONE
COMPLEX(KIND=DP), DIMENSION(:), INTENT(INOUT) :: y
COMPLEX(KIND=DP), DIMENSION(:), INTENT(IN) :: dydx,yscal
real(dp), INTENT(INOUT) :: x
real(dp), INTENT(IN) :: htry,eps
real(dp), INTENT(OUT) :: hdid,hnext
INTERFACE
SUBROUTINE derivs(x, y, dydx)
USE modpkparams
IMPLICIT NONE
real(dp), INTENT(IN) :: x
COMPLEX(KIND=DP), DIMENSION(:), INTENT(IN) :: y
COMPLEX(KIND=DP), DIMENSION(:), INTENT(OUT) :: dydx
END SUBROUTINE derivs
END INTERFACE
END SUBROUTINE rkqs_c
END INTERFACE
real(dp), PARAMETER :: TINY=1.0e-40_dp
INTEGER, PARAMETER :: MAXSTP=nsteps
INTEGER*4 :: nstp,i
real(dp) :: h,hdid,hnext,x,xsav
COMPLEX(KIND=DP), DIMENSION(size(ystart)) :: dydx, y, yscal
real(dp) :: scalefac, hubble, a_switch, dotphi
complex(dp), dimension(num_inflaton**2) :: psi, dpsi
complex(dp), dimension(num_inflaton**2) :: qij, dqij
real(dp) :: nk_sum, Nprime(num_inflaton), Nprimeprime(num_inflaton,num_inflaton)
integer :: ii, jj, kk
!For DVODE integrator
real(dp), dimension(size(ystart)*2) :: yreal, dyrealdx
integer :: neq, istats(31)
integer :: itask, istate
real(dp) :: rtol, rstats(22), nefold_out, dN_step
real(dp), dimension(:), allocatable :: atol, atol_real, atol_compl
type (vode_opts) :: ode_integrator_opt
character(1024) :: cname
ode_underflow=.FALSE.
infl_ended=.FALSE.
x=x1
h=SIGN(h1,x2-x1)
nok=0
nbad=0
kount=0
y(:)=ystart(:)
NULLIFY(xp,yp)
IF (save_steps) THEN
xsav=x-2.e0_dp*dxsav
ALLOCATE(xp(256))
!MULTIFIELD
ALLOCATE(yp(2*SIZE(ystart),SIZE(xp))) !! store real and imiganary seperately
!END MULTIFIELD
END IF
use_q = .false.
compute_zpower = .true.
eps_adjust = eps
if (tech_opt%use_dvode_integrator) then
!Options for first call to dvode integrator
call initialize_dvode_MODES()
end if
DO nstp=1,MAXSTP
if (any(isnan(real(y))) .or. any(isnan(aimag(y)))) then
print*, "MODECODE: E-fold",x
print*, "MODECODE: nstp",nstp
print*, "MODECODE: y", y
call raise%fatal_code(&
"y has a NaN value in odeint_c.",&
__FILE__, __LINE__)
end if
IF (use_q) THEN
! super-h use Q
CALL qderivs(x, y, dydx)
ELSE
! sub-h use psi
CALL derivs(x, y, dydx)
END IF
IF (save_steps .AND. (ABS(x-xsav) > ABS(dxsav))) &
CALL save_a_step
if (tech_opt%use_dvode_integrator) then
!Cmplx --> real
yreal(1:neq/2) = real(y)
yreal(neq/2+1:neq) = aimag(y)
if (use_q) then
call dvode_f90(qderivs_dvode,neq,yreal,x,nefold_out, &
itask,istate,ode_integrator_opt)
else
call dvode_f90(mode_derivs_dvode,neq,yreal,x,nefold_out, &
itask,istate,ode_integrator_opt)
end if
call get_stats(rstats,istats)
if (istate<0) then
print*, "MODECODE istate=", istate
call raise%fatal_code(&
"The dvode_f90 integrator threw an error. &
Check the documentation there.",&
__FILE__, __LINE__)
end if
!Set complex y from real y's
y = cmplx(yreal(1:neq/2),yreal(neq/2+1:neq), kind=dp)
else
! for yscal, evaluate real and imaginary parts separately,
! and then assemble them into complex format
!"Trick" to get constant fractional errors except very near
!zero-crossings. (Numerical Recipes)
yscal(:)=cmplx(ABS(real(y(:),kind=dp))+ABS(h*real(dydx(:),kind=dp))+TINY, &
ABS(real(y(:)*(0,-1),kind=dp))+ABS(h*real(dydx(:)*(0,-1),kind=dp))+TINY)
IF ((x+h-x2)*(x+h-x1) > 0.0) h=x2-x
IF (use_q) THEN
CALL rkqs_c(y,dydx,x,h,eps_adjust,yscal,hdid,hnext,qderivs)
ELSE
CALL rkqs_c(y,dydx,x,h,eps_adjust,yscal,hdid,hnext,derivs)
END IF
IF (hdid == h) THEN
nok=nok+1
ELSE
nbad=nbad+1
END IF
end if
!MULTIFIELD
phi = real(y(1:num_inflaton),kind=dp)
delphi = real(y(num_inflaton+1 : 2*num_inflaton),kind=dp)
dotphi = sqrt(dot_product(delphi, delphi))
if (out_opt%modes) call print_modes()
scalefac = a_init*exp(x)
!Increase accuracy requirements when not in SR
if (tech_opt%accuracy_setting>0) then
if (getEps(phi,delphi)>0.2e0_dp) then
eps_adjust=1e-10_dp
if (tech_opt%accuracy_setting==2) then
if (getEps(phi,delphi)>0.9e0_dp) then
eps_adjust=1e-16_dp
end if
end if
end if
else
eps_adjust = eps
end if
!END MULTIFIELD
IF(getEps(phi,delphi) .LT. 1 .AND. .NOT.(slowroll_start)) slowroll_start=.true.
IF(ode_ps_output) THEN
! if k<aH/eval_ps, then k<<aH
if(k .lt. a_init*exp(x)*getH(phi, delphi)/eval_ps) &
call evaluate_powerspectra()
END IF
call check_inflation_ended_properly_MODES()
call check_for_eternal_inflation_MODES()
IF(ode_infl_end) THEN
IF (infl_ended) THEN
IF (use_q) THEN
ytmp(:) = y(:)
! bckgrd
ystart(1:2*num_inflaton) = y(1:2*num_inflaton)
! ptbs
ystart(index_ptb_y:index_ptb_vel_y-1) = &
ytmp(index_ptb_y:index_ptb_vel_y-1)*scalefac/a_switch
ystart(index_ptb_vel_y:index_tensor_y-1) = &
ytmp(index_ptb_vel_y:index_tensor_y-1)&
*scalefac/a_switch + ystart(index_ptb_y:index_ptb_vel_y-1)
! tensors
ystart(index_tensor_y) =&
ytmp(index_tensor_y)*scalefac/a_switch
ystart(index_tensor_y+1) =&
ytmp(index_tensor_y+1)*scalefac/a_switch&
+ ystart(index_tensor_y)
ELSE
ystart(:) = y(:)
END IF
IF (save_steps) CALL save_a_step
!For outputting field values
if (out_opt%fields_end_infl) then
!Make header column
if (out_opt%first_fields_end_out) then
!First column
call csv_write(out_opt%fields_end_out,&
'k',&
advance=.false.)
!Next num_inflaton columns
do ii=1,num_inflaton
write(cname, "(A3,I4.4)") "phi_piv", ii
if (ii==num_inflaton) then
call csv_write(&
out_opt%fields_end_out,&
trim(cname), &
advance=.true.)
else
call csv_write(&
out_opt%fields_end_out,&
trim(cname), &
advance=.false.)
end if
end do
out_opt%first_fields_end_out = .false.
end if
!Write data
call csv_write(out_opt%fields_end_out,&
(/k, phi/),&
advance=.true.)
end if
RETURN
END IF
ENDIF
!switch to the Q variable for super-horizon evolution
!only apply the switch on y(1:4*num_inflaton+2)
IF (k .LT. a_init*exp(x)*getH(phi, delphi)/useq_ps &
.and. (.not. use_q)) THEN
call switch_to_qvar()
end if
IF (ode_underflow) RETURN
if (.not. tech_opt%use_dvode_integrator) then
IF (ABS(hnext) < hmin) THEN
call raise%fatal_code(&
'stepsize smaller than minimum in odeint_c', &
__FILE__, __LINE__)
end if
end if
!Set up next N-step
if (tech_opt%use_dvode_integrator) then
if (itask/=2) nefold_out = min(x + dN_step, x2)
else
h=hnext
end if
end do
ode_underflow=.TRUE.
print*, 'MODECODE: N =', x
print*, 'MODECODE: stepsize, h =', h
print*, 'MODECODE: background, y =', y(1:num_inflaton)
print*, 'MODECODE: accuracy =', eps_adjust, eps
print*, "MODECODE: epsilon", getEps(phi,delphi)
call raise%fatal_code(&
'Too many steps in odeint_c. Probably due to numerical accuracy or &
stiffness in the problem.', &
__FILE__, __LINE__)
contains
subroutine print_modes()
character(1024) :: cname
integer :: ii
!Make column headers
if (out_opt%first_modeout) then
out_opt%first_modeout = .false.
!First column --- reals
call csv_write(out_opt%modeout(1),&
'N',&
advance=.false.)
call csv_write(out_opt%modeout(3),&
'N',&
advance=.false.)
!Next num_inflaton columns
do ii=index_ptb_y,index_ptb_vel_y-1
write(cname, "(A8,I4.4)") "Re[mode]", ii
if (ii==index_ptb_vel_y-1) then
call csv_write(&
out_opt%modeout(1),&
trim(cname), &
advance=.true.)
else
call csv_write(&
out_opt%modeout(1),&
trim(cname), &
advance=.false.)
end if
write(cname, "(A9,I4.4)") "Re[qmode]", ii
if (ii==index_ptb_vel_y-1) then
call csv_write(&
out_opt%modeout(3),&
trim(cname), &
advance=.true.)
else
call csv_write(&
out_opt%modeout(3),&
trim(cname), &
advance=.false.)
end if
end do
!First column --- imags
call csv_write(out_opt%modeout(2),&
'N',&
advance=.false.)
call csv_write(out_opt%modeout(4),&
'N',&
advance=.false.)
!Next num_inflaton columns
do ii=index_ptb_y,index_ptb_vel_y-1
write(cname, "(A8,I4.4)") "Im[mode]", ii
if (ii==index_ptb_vel_y-1) then
call csv_write(&
out_opt%modeout(2),&
trim(cname), &
advance=.true.)
else
call csv_write(&
out_opt%modeout(2),&
trim(cname), &
advance=.false.)
end if
write(cname, "(A9,I4.4)") "Im[qmode]", ii
if (ii==index_ptb_vel_y-1) then
call csv_write(&
out_opt%modeout(4),&
trim(cname), &
advance=.true.)
else
call csv_write(&
out_opt%modeout(4),&
trim(cname), &
advance=.false.)
end if
end do
end if
if (.not. use_q) then
write(out_opt%modeout(1),'(100E30.22)') &
x - (n_tot - N_pivot), &
real(y(index_ptb_y:index_ptb_vel_y-1))/sqrt(2*k)
write(out_opt%modeout(2),'(100E30.22)') &
x - (n_tot - N_pivot),&
aimag(y(index_ptb_y:index_ptb_vel_y-1))/sqrt(2*k)
else
write(out_opt%modeout(3),'(100E30.22)') &
x - (n_tot - N_pivot), &
real(y(index_ptb_y:index_ptb_vel_y-1))/sqrt(2*k)
write(out_opt%modeout(4),'(100E30.22)') &
x - (n_tot - N_pivot),&
aimag(y(index_ptb_y:index_ptb_vel_y-1))/sqrt(2*k)
end if
end subroutine print_modes
SUBROUTINE save_a_step
USE modpkparams
IMPLICIT NONE
COMPLEX(KIND=DP), DIMENSION(size(ystart)) :: ytmp
COMPLEX(KIND=DP), DIMENSION(num_inflaton**2) :: ptb_tmp, dptb_tmp
kount=kount+1
IF (kount > SIZE(xp)) THEN
xp=>reallocate_rv(xp,2*SIZE(xp))
yp=>reallocate_rm(yp,SIZE(yp,1), SIZE(xp))
END IF
xp(kount) = x
IF (use_q) THEN ! convert from (a_switch*Q) to v
ytmp(:) = y(:)
ptb_tmp =ytmp(index_ptb_y:index_ptb_vel_y-1)
dptb_tmp =ytmp(index_ptb_vel_y:index_tensor_y-1)