-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrators.F90
1329 lines (1107 loc) · 62.3 KB
/
integrators.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
module integrators
implicit none
type dydt_i
procedure(dydt_i_tem), pointer, nopass :: f_i => null ()
end type dydt_i
! For adaptive step and implicit (might be overwritten)
integer*4 :: MAX_N_ITER = 100
real*8, parameter :: MAX_DT_FAC = 5., SAFE_LOW = 1e-300
real*8 :: BETA = 0.9, E_TOL = 1e-8
! Aux Constants
real*8, parameter :: C1_3 = 1/3., C2_3 = C1_3 * 2
real*8, parameter :: C1_5 = 1/5.
real*8, parameter :: C1_6 = 1/6., C5_6 = C1_6 * 5
real*8, parameter :: C1_7 = 1/7.
real*8, parameter :: C1_9 = 1/9., C2_9 = C1_9 * 2, C4_9 = C1_9 * 4, C8_9 = C1_9 * 8
real*8, parameter :: C1_12 = 1/12., C5_12 = C1_12 * 5
real*8, parameter :: C1_15 = 1/15., C4_15 = C1_15 * 4
real*8, parameter :: C1_18 = 1/18., C5_18 = C1_18 * 5
real*8, parameter :: C2_27 = 2/27.
real*8, parameter :: C5_36 = 5/36.
real*8, parameter :: C2_45 = 2/45.
real*8, parameter :: C1_48 = 1/48.
real*8, parameter :: C1_840 = 1/840., C41_840 = C1_840 * 41
real*8, parameter :: SQ3 = sqrt(3.)
real*8, parameter :: SQ15 = sqrt(15.)
real*8, parameter :: SQ3_6 = SQ3 * C1_6, SQ3_2 = SQ3_6 * 2
real*8, parameter :: SQ15_5 = SQ15 * 0.2, SQ15_10 = SQ15_5 * 2, SQ15_24 = SQ15/24.
real*8, parameter :: SQ15_30 = SQ15/30., SQ15_15 = SQ15_30 * 2
abstract interface
!---------------------------------------------------------------------------------------------
! ND -> ND
!---------------------------------------------------------------------------------------------
! f_i (t, y__) = der
real*8 function dydt_i_tem (t, y) result (der)
implicit none
real*8, intent(in) :: t
real*8, dimension(:), intent(in) :: y
end function dydt_i_tem
! f__ (t, y__) = (f_i (t, y__), ..., f_n (t, y__)) = der__
function dydt_tem (t, y) result (der)
implicit none
real*8, intent(in) :: t
real*8, dimension(:), intent(in) :: y
real*8, dimension(size (y)) :: der
! Here must be every f_i defined explicitly
end function dydt_tem
! in (t, y__, dt, f_i, ynew) -> ynew, dt
subroutine integ_tem_i (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, intent(out) :: ynew
real*8, dimension(:), intent(in) :: y
procedure(dydt_i_tem) :: dydt
end subroutine integ_tem_i
! in (t, y__, dt, f__, ynew__) -> ynew__
! Remember that, in this case,
! f__ == (f_1, ..., f_N) must be
! pre-defined explicitly
subroutine integ_tem (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
end subroutine integ_tem
! in (t, y__, dt, f__, osol, oerr, yaux__, ynew__) -> osol, oerr, yaux__, ynew__
! Remember that, in this case,
! f__ == (f_1, ..., f_N) must be
! pre-defined explicitly
subroutine embedded_tem (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
end subroutine embedded_tem
subroutine calc_rk (t, y, dt, dydt, kin, kout)
implicit none
real*8, intent(in) :: t, dt
procedure(dydt_tem) :: dydt
real*8, dimension(:, :), intent(in) :: kin
real*8, dimension(size (kin, 1)), intent(in) :: y
real*8, dimension(size (kin, 1), size (kin,2)), intent(out) :: kout
end subroutine calc_rk
!---------------------------------------------------------------------------------------------
! WRAPPERS
!---------------------------------------------------------------------------------------------
! Here dydt_vec is a pointer to an array of (f__1, ..., f__N)
! so this is kind of a wrapper.
! dydt_tem_w (t, y__, =>f__)
! CREATE F (t, y__) = (f__1 (t, y__), ..., f__N (t, y__)) = der__
! --> (f__1 (t, y__), ..., f__N (t, y__))
! --> (der_1, ..., der_N) = der__
function dydt_tem_w (t, y, dydt) result (der)
import :: dydt_i
implicit none
real*8, intent(in) :: t
real*8, dimension(:), intent(in) :: y
type(dydt_i), dimension(size (y)) :: dydt
real*8, dimension(size (y)) :: der
end function dydt_tem_w
! Here dydt_vec is a pointer to an array of (f__1, ..., f__N);
! so this is kind of a wrapper.
! integ_tem_w (t, y__, dt, =>f__, integ, ynew__) -->
! CREATE F (t, y__) = (f__1 (t, y__), ..., f__N (t, y__))
! --> integ (t, y__, dt, F__, ynew__) --> ynew__
subroutine integ_tem_w (t, y, dt, dydt, integ, ynew)
import :: dydt_i
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
type(dydt_i), dimension(size (y)) :: dydt
procedure(integ_tem) :: integ
real*8, dimension(size (y)), intent(out) :: ynew
end subroutine integ_tem_w
! Same as before, but for embedded_integrators
subroutine embedded_tem_w (t, y, dt_adap, dydt, e_tol, beta, dt_min, dt_used, integ, ynew)
import :: dydt_i
implicit none
real*8, intent(in) :: t, e_tol, beta, dt_min
real*8, intent(inout) :: dt_adap, dt_used
real*8, dimension(:), intent(in) :: y
type(dydt_i), dimension(size (y)) :: dydt
procedure(embedded_tem) :: integ
real*8, dimension(size (y)), intent(out) :: ynew
end subroutine embedded_tem_w
! Same as before, but for rk_adap
subroutine rk_adap_w (t, y, dt_adap, dydt, integ, p, e_tol, beta, dt_min, dt_used, ynew)
import :: dydt_i
integer*4, intent(in) :: p
real*8, intent(in) :: t, e_tol, beta, dt_min
procedure(integ_tem) :: integ
real*8, dimension(:), intent(in) :: y
type(dydt_i), dimension(size (y)) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, intent(inout) :: dt_adap, dt_used
end subroutine rk_adap_w
end interface
contains
!---------------------------------------------------------------------------------------------
! ND -> ND
!---------------------------------------------------------------------------------------------
!------------------------------------------ SOLVERS ------------------------------------------
!! Implicit Methods Solver
subroutine solve_1k_implicit (t, y, dt, dydt, kprev, cte, k)
implicit none
real*8, intent(in) :: t, dt, cte
real*8, dimension(:), intent(in) :: y
real*8, dimension(size (y)), intent(in) :: kprev
real*8, dimension(size (y)), intent(out) :: k
real*8, dimension(size (y)) :: kaux
procedure(dydt_tem) :: dydt
integer*4 :: i
k = dydt (t, y + dt * kprev)
do i = 1, MAX_N_ITER
kaux = k
k = dydt (t, y + dt * (kprev + cte * k))
if (maxval( abs ((kaux - k) / (kaux + SAFE_LOW))) .le. E_TOL) then
exit
end if
end do
end subroutine solve_1k_implicit
subroutine solve_rk_implicit (t, y, dt, dydt, solver, rk)
implicit none
real*8, intent(in) :: t, dt
procedure(dydt_tem) :: dydt
real*8, dimension(:, :), intent(inout) :: rk
real*8, dimension(size (rk,1), size (rk,2)) :: rkold
real*8, dimension(size (rk,1)), intent(in) :: y
procedure(calc_rk) :: solver
integer*4 :: i
do i = 1, MAX_N_ITER
rkold = rk
call solver (t, y, dt, dydt, rkold, rk)
if (maxval( abs ((rkold - rk) / (rkold + SAFE_LOW))) .le. E_TOL) then
exit
end if
end do
end subroutine solve_rk_implicit
!! Runge Kutta Methods Solver
subroutine get_rks (t, y, dt, dydt, m, rk)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
real*8, dimension(:,:), intent(in) :: m
real*8, dimension((size (m,1) - 1), size (y)), intent(out) :: rk ! In columns
real*8, dimension(size (y)) :: rkaux
procedure(dydt_tem) :: dydt
integer*4 :: i, j
do i = 1, size (m, 1) - 1 ! Rows
rkaux = 0.
do j = 1, i ! Cols (<i bc its inf triang)
rkaux = rkaux + m(1+j,i) * rk(j,:)
end do
rk(i, :) = dydt (t + m(1,i) * dt, y + dt * rkaux)
end do
end subroutine get_rks
subroutine solve_rk (t, y, dt, dydt, m, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
real*8, dimension(:,:), intent(in) :: m
real*8, dimension((size (m,1) - 1), size (y)) :: rk
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
integer*4 :: i
call get_rks (t, y, dt, dydt, m, rk)
do i = 1, size (y)
ynew(i) = y(i) + dt * dot_product ((/m(2:, size (m, 1))/), rk(:,i))
end do
end subroutine solve_rk
subroutine solve_rk_embed (t, y, dt, dydt, m, maux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
real*8, dimension(:,:), intent(in) :: m
real*8, dimension((size (m,1) - 1), size (y)) :: rk
real*8, dimension((size (m,1) - 1)), intent(in) :: maux
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: yaux, ynew
integer*4 :: i
call get_rks (t, y, dt, dydt, m, rk)
do i = 1, size (y)
yaux(i) = y(i) + dt * dot_product (maux, rk(:,i))
ynew(i) = y(i) + dt * dot_product ((/m(2:, size (m, 1))/), rk(:,i))
end do
end subroutine solve_rk_embed
!!
!! Embedded Methods Solver
recursive subroutine solve_embed (t, y, dt_adap, dydt, integ, e_tol, beta, dt_min, dt_used, ynew)
implicit none
real*8, intent(in) :: t, e_tol, beta, dt_min
real*8, intent(inout) :: dt_adap, dt_used
real*8, dimension(:), intent(in) :: y
integer*4, save :: osol, oaux, iter = 0
real*8, dimension(size (y)) :: yaux, yscal
procedure(dydt_tem) :: dydt
procedure(embedded_tem) :: integ
real*8, dimension(size (y)), intent(out) :: ynew
real*8 :: e_calc, ratio
iter = iter + 1
dt_adap = max (dt_adap, dt_min)
call integ (t, y, dt_adap, dydt, osol, oaux, yaux, ynew)
yscal = abs (y + dt_adap * dydt (t, y)) + SAFE_LOW
e_calc = max (maxval (abs ((ynew - yaux) / yscal )), SAFE_LOW)
ratio = e_tol / e_calc
if (ratio > 1.) then
dt_used = dt_adap
dt_adap = dt_adap * min (beta * ratio**(1. / osol), MAX_DT_FAC)
iter = 0
else
if (dt_adap .eq. dt_min) then
dt_used = dt_min
iter = 0
else
dt_adap = dt_adap * min (beta * ratio**(1. / oaux), MAX_DT_FAC)
if ((isnan (dt_adap)) .or. (dt_adap < dt_min) .or. (iter .eq. MAX_N_ITER)) then
dt_adap = dt_min
dt_used = dt_min
call integ (t, y, dt_adap, dydt, osol, oaux, yaux, ynew)
iter = 0
else
call solve_embed (t, y, dt_adap, dydt, integ, e_tol, beta, dt_min, dt_used, ynew)
end if
end if
end if
end subroutine solve_embed
!!
!---------------------------------------- INTEGRATORS ----------------------------------------
!! Runge Kutta Methods (implicit and explicit)
subroutine Euler1 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
ynew = y + dt * dydt (t, y)
end subroutine Euler1
subroutine Euler_back1 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1
real*8 :: aux
kaux = 0.
aux = 1.
call solve_1k_implicit (t + dt, y, dt, dydt, kaux, aux, k1)
ynew = y + dt * k1
end subroutine Euler_back1
subroutine Euler_center2 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1
real*8 :: aux
kaux = 0.
aux = 0.5
call solve_1k_implicit (t + dt * 0.5, y, dt, dydt, kaux, aux, k1)
ynew = y + dt * k1
end subroutine Euler_center2
subroutine Crank_Nicolson2 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2
real*8 :: aux
k1 = dydt (t, y)
kaux = k1 * 0.5
aux = 0.5
call solve_1k_implicit (t + dt, y, dt, dydt, kaux, aux, k2)
ynew = y + dt * (k1 + k2) * 0.5
end subroutine Crank_Nicolson2
subroutine Heun2 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2
k1 = dydt (t, y)
k2 = dydt (t + dt, y + dt * k1)
ynew = y + dt * (k1 + k2) * 0.5
end subroutine Heun2
subroutine midpoint2 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.5, y + dt * k1 * 0.5)
ynew = y + dt * k2
end subroutine midpoint2
subroutine strange2 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.75, y + dt * k1 * 0.75)
ynew = y + dt * (k1 + k2 * 2) * C1_3
end subroutine strange2
subroutine Ralston2 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2
k1 = dydt (t, y)
k2 = dydt (t + dt * C2_3, y + dt * k1 * C2_3)
ynew = y + dt * (k1 + k2 * 3) * 0.25
end subroutine Ralston2
subroutine Hammer_Hollingsworth2 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2
real*8 :: aux
k1 = dydt (t, y)
kaux = k1 * C1_3
aux = C1_3
call solve_1k_implicit (t + dt * C2_3, y, dt, dydt, kaux, aux, k2)
ynew = y + dt * (k1 + k2 * 3) * 0.25
end subroutine Hammer_Hollingsworth2
subroutine Kraaijevanger_Spijker2 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2
real*8 :: aux
kaux = 0.
aux = 0.5
call solve_1k_implicit (t + dt * 0.5, y, dt, dydt, kaux, aux, k1)
kaux = - k1 * 0.5
aux = 2.
call solve_1k_implicit (t + dt * 1.5, y, dt, dydt, kaux, aux, k2)
ynew = y + dt * (- k1 + k2 * 3) * 0.5
end subroutine Kraaijevanger_Spijker2
subroutine Qin_Zhang2 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2
real*8, parameter :: aux = 0.25
kaux = 0.
call solve_1k_implicit (t + dt * 0.25, y, dt, dydt, kaux, aux, k1)
kaux = k1 * 0.5
call solve_1k_implicit (t + dt * 0.75, y, dt, dydt, kaux, aux, k2)
ynew = y + dt * (k1 + k2) * 0.5
end subroutine Qin_Zhang2
subroutine Runge_Kutta3 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.5, y + dt * k1 * 0.5)
k3 = dydt (t + dt, y + dt * (- k1 + k2 * 2))
ynew = y + dt * (k1 + k2 * 4 + k3) * C1_6
end subroutine Runge_Kutta3
subroutine Heun3 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3
k1 = dydt (t, y)
k2 = dydt (t + dt * C1_3, y + dt * k1 * C1_3)
k3 = dydt (t + dt * C2_3, y + dt * k2 * C2_3)
ynew = y + dt * (k1 + k3 * 3) * 0.25
end subroutine Heun3
subroutine Ralston3 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.5, y + dt * k1 * 0.5)
k3 = dydt (t + dt * 0.75, y + dt * k2 * 0.75)
ynew = y + dt * (k1 * 2 + k2 * 3 + k3 * 4) * C1_9
end subroutine Ralston3
subroutine SSPRK3 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3
k1 = dydt (t, y)
k2 = dydt (t + dt, y + dt * k1)
k3 = dydt (t + dt * 0.5, y + dt * (k1 + k2) * 0.25)
ynew = y + dt * (k1 + k2 + k3 * 4) * C1_6
end subroutine SSPRK3
subroutine Crouzeix3 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2
real*8 :: aux
kaux = 0.
aux = 0.5 + SQ3_6
call solve_1k_implicit (t + dt * aux, y, dt, dydt, kaux, aux, k1)
kaux = - k1 * SQ3_6 * 2
call solve_1k_implicit (t + dt * (0.5 - SQ3_6), y, dt, dydt, kaux, aux, k2)
ynew = y + dt * (k1 + k2) * 0.5
end subroutine Crouzeix3
subroutine Runge_Kutta_implicit3 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2, k3, k4
real*8, parameter :: aux = 0.5
kaux = 0.
call solve_1k_implicit (t + dt * 0.5, y, dt, dydt, kaux, aux, k1)
kaux = k1 * C1_6
call solve_1k_implicit (t + dt * C2_3, y, dt, dydt, kaux, aux, k2)
kaux = (- k1 + k2) * 0.5
call solve_1k_implicit (t + dt * 0.5, y, dt, dydt, kaux, aux, k3)
kaux = ((k1 - k2) * 3 + k3) * 0.5
call solve_1k_implicit (t + dt, y, dt, dydt, kaux, aux, k4)
ynew = y + dt * (kaux + k4 * 0.5)
end subroutine Runge_Kutta_implicit3
subroutine Ralston4 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3, k4
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.4, y + dt * k1 * 0.4)
k3 = dydt (t + dt * 0.45573725, y + dt * (k1 * 0.29697761 + k2 * 0.15875964))
k4 = dydt (t + dt, y + dt * (k1 * 0.21810040 - k2 * 3.05096516 + k3 * 3.83286476))
ynew = y + dt * (k1 * 0.17476028 - k2 * 0.55148066 + k3 * 1.20553560 + k4 * 0.17118478)
end subroutine Ralston4
subroutine Lobatto4 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: kaux, k1, k2, k3
real*8, parameter :: aux = 0.25
k1 = dydt (t, y)
kaux = k1 * 0.25
call solve_1k_implicit (t + dt * 0.5, y, dt, dydt, kaux, aux, k2)
k3 = dydt (t + dt, y + dt * k2)
ynew = y + dt * (k1 + k2 * 4 + k3) * C1_6
end subroutine Lobatto4
subroutine Runge_Kutta4 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3, k4
real*8 :: aut
aut = dt * 0.5
k1 = dydt (t, y)
k2 = dydt (t + aut, y + dt * k1 * 0.5)
k3 = dydt (t + aut, y + dt * k2 * 0.5)
k4 = dydt (t + dt, y + dt * k3)
ynew = y + dt * (k1 + (k2 + k3) * 2 + k4) * C1_6
end subroutine Runge_Kutta4
subroutine Gauss_Legendre4 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y), 2) :: rk
rk = 0.
call solve_rk_implicit (t, y, dt, dydt, FunK, rk)
ynew = y + dt * (rk(:,1) + rk(:,2)) * 0.5
contains
subroutine FunK (t, y, dt, dydt, kin, kout)
implicit none
real*8, intent(in) :: t, dt
procedure(dydt_tem) :: dydt
real*8, dimension(:, :), intent(in) :: kin
real*8, dimension(size (kin, 1)), intent(in) :: y
real*8, dimension(size (kin, 1), size (kin,2)), intent(out) :: kout
kout(:,1) = dydt (t + dt * (0.5 - SQ3_6), y + dt * ( &
& kin(:,1) * 0.25 + &
& kin(:,2) * (0.25 - SQ3_6)))
kout(:,2) = dydt (t + dt * (0.5 + SQ3_6), y + dt * ( &
& kin(:,1) * (0.25 + SQ3_6) + &
& kin(:,2) * 0.25))
end subroutine FunK
end subroutine Gauss_Legendre4
subroutine Runge_Kutta_four_oct4 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3, k4, kaux
k1 = dydt (t, y)
kaux = k1 * C1_3
k2 = dydt (t + dt * C1_3, y + dt * kaux)
k3 = dydt (t + dt * C2_3, y + dt * (- kaux + k2))
k4 = dydt (t + dt, y + dt * (k1 - k2 + k3))
ynew = y + dt * (k1 + (k2 + k3) * 3 + k4) * 0.125
end subroutine Runge_Kutta_four_oct4
subroutine Runge_Kutta5 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6
real*8 :: aut
aut = dt * 0.25
k1 = dydt (t, y)
k2 = dydt (t + aut, y + dt * k1 * 0.25)
k3 = dydt (t + aut, y + dt * (k1 + k2) * 0.125)
k4 = dydt (t + dt * 0.5, y + dt * (- k2 * 0.5 + k3))
k5 = dydt (t + dt * 0.75, y + dt * (k1 + k4 * 3) * 3/16.)
k6 = dydt (t + dt, y + dt * (- k1 * 3 + k2 * 2 + (k3 - k4) * 12 + k5 * 8) * C1_7)
ynew = y + dt * ((k1 + k6) * 7 + (k3 + k5) * 32 + k4 * 12)/90.
end subroutine Runge_Kutta5
subroutine Gauss_Legendre6 (t, y, dt, dydt, ynew) ! Implicit
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y), 3) :: rk
rk = 0.
call solve_rk_implicit (t, y, dt, dydt, FunK, rk)
ynew = y + dt * ((rk(:,1) + rk(:,3)) * 5 + rk(:,2) * 8) * C1_18
contains
subroutine FunK (t, y, dt, dydt, kin, kout)
implicit none
real*8, intent(in) :: t, dt
procedure(dydt_tem) :: dydt
real*8, dimension(:, :), intent(in) :: kin
real*8, dimension(size (kin, 1)), intent(in) :: y
real*8, dimension(size (kin, 1), size (kin,2)), intent(out) :: kout
kout(:,1) = dydt (t + dt * (1. - SQ15_5) * 0.5, y + dt * (&
& kin(:,1) * C5_36 + &
& kin(:,2) * (C2_3 - SQ15_5) * C1_3 + &
& kin(:,3) * (C5_36 - SQ15_30)))
kout(:,2) = dydt (t + dt * 0.5, y + dt * (&
& kin(:,1) * (C5_36 + SQ15_24)+ &
& kin(:,2) * C2_9 + &
& kin(:,3) * (C5_36 - SQ15_24)))
kout(:,3) = dydt (t + dt * (1. + SQ15_5) * 0.5, y + dt * (&
& kin(:,1) * (C5_36 + SQ15_30) + &
& kin(:,2) * (C2_9 + SQ15_15) + &
& kin(:,3) * C5_36))
end subroutine FunK
end subroutine Gauss_Legendre6
subroutine Runge_Kutta6 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6, k7
k1 = dydt (t, y)
k2 = dydt (t + dt * C1_3, y + dt * k1 * C1_3)
k3 = dydt (t + dt * C2_3, y + dt * k2 * C2_3)
k4 = dydt (t + dt * C1_3, y + dt * (k1 + k2 * 4 - k3) * C1_12)
k5 = dydt (t + dt * C5_6, y + dt * (k1 * 25 - k2 * 110 + k3 * 35 + k4 * 90) * C1_48)
k6 = dydt (t + dt * C1_6, y + dt * (k1 * 3 + k4 * 10 + k5 * 2) * 0.05 + (-k2 * 11 - k3 * 3)/24.)
k7 = dydt (t + dt, y + dt * (- k1 * 30.75 + k2 * 495 + k3 * 53.75 - k4 * 590 + k5 * 32 + k6 * 400)/195.)
ynew = y + dt * ((k1 + k7) * 13/200. + (k3 + k4) * 11/40. + (k5 + k6) * 4/25.)
end subroutine Runge_Kutta6
subroutine Abbas6 (t, y, dt, dydt, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
real*8, dimension(size (y)), intent(out) :: ynew
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6, k7
real*8 :: aut
aut = dt * C1_3
k1 = dydt (t, y)
k2 = dydt (t + aut, y + dt * k1 * C1_3)
k3 = dydt (t + dt * C2_3, y + dt * k2 * C2_3)
k4 = dydt (t + aut, y + dt * (k1 + k2 * 4 - k3) * C1_12)
k5 = dydt (t + dt * C5_6, y + dt * (k1 * 25 - k2 * 110 + k3 * 35 + k4 * 90)/48.)
k6 = dydt (t + dt * C1_6, y + dt * (k1 * 0.15 - k2 * 0.55 - k3 * 0.125 + k4 * 0.5 + k5 * 0.1))
k7 = dydt (t + dt, y + dt * (- k1 * 195.75 + k2 * 495 + k3 * 53.75 - k4 * 590 + k5 * 32 + k6 * 400)/195.)
ynew = y + dt * ((k1 + k7) * 13 + (k3 + k4) * 55 + (k5 + k6) * 32) * 0.005
end subroutine Abbas6
!!
!! Embedded
subroutine Heun_Euler2_1 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2
osol = 2
oaux = 1
k1 = dydt (t, y)
k2 = dydt (t + dt, y + dt * k1)
ynew = y + dt * (k1 + k2) * 0.5
yaux = y + dt * k1
end subroutine Heun_Euler2_1
subroutine Fehlberg1_2 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3
osol = 1
oaux = 2
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.5, y + dt * k1 * 0.5)
ynew = y + dt * (k1 + k2 * 255) * 0.00390625
k3 = dydt (t + dt, ynew)
yaux = y + dt * (k1 + k2 * 500 + k3) * 0.001953125
end subroutine Fehlberg1_2
subroutine Bogacki_Shampine3_2 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4
osol = 3
oaux = 2
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.5, y + dt * k1 * 0.5)
k3 = dydt (t + dt * 0.75, y + dt * k2 * 0.75)
ynew = y + dt * (k1 * 2 + k2 * 3 + k3 * 4) * C1_9
k4 = dydt (t + dt, ynew)
yaux = y + dt * (k1 * 7/24. + k2 * 0.25 + k3 * C1_3 + k4 * 0.125)
end subroutine Bogacki_Shampine3_2
subroutine Zonneveld4_3 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, kaux
real*8 :: aut
aut = dt * 0.5
osol = 4
oaux = 3
k1 = dydt (t, y)
k2 = dydt (t + aut, y + dt * k1 * 0.5)
k3 = dydt (t + aut, y + dt * k2 * 0.5)
k4 = dydt (t + dt, y + dt * k3)
k5 = dydt (t + dt * 0.75, y + dt * (k1 * 5 + k2 * 7 + k3 * 13 - k4) * 0.03125)
kaux = (k2 + k3)
ynew = y + dt * (k1 + kaux * 2 + k4) * C1_6
yaux = y + dt * (- k1 * 3 + kaux * 14 + k4 * 13 - k5 * 32) * C1_6
end subroutine Zonneveld4_3
subroutine Merson4_3 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5
osol = 4
oaux = 3
k1 = dydt (t, y)
k2 = dydt (t + dt * C1_3, y + dt * k1 * C1_3)
k3 = dydt (t + dt * C1_3, y + dt * (k1 + k2) * C1_6)
k4 = dydt (t + dt * 0.5, y + dt * (k1 + k3 * 3) * 0.125)
k5 = dydt (t + dt, y + dt * (k1 - k3 * 3 + k4 * 4) * 0.5)
ynew = y + dt * (k1 + k4 * 4 + k5) * C1_6
yaux = y + dt * (k1 + k3 * 3 + k4 * 4 + k5 * 2) * 0.1
end subroutine Merson4_3
subroutine Fehlberg4_5 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6
osol = 4
oaux = 5
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.25, y + dt * k1 * 0.25)
k3 = dydt (t + dt * 0.375, y + dt * (k1 * 3 + k2 * 9) * 0.03125)
k4 = dydt (t + dt * 12/13., y + dt * (k1 * 1932 - k2 * 7200 + k3 * 7296)/2197.)
k5 = dydt (t + dt, y + dt * ((k1 * 8341 + k3 * 29440 - k4 * 845)/4104. - k2 * 8))
k6 = dydt (t + dt * 0.5, y + dt * ((- k1 * 1216 + k4 * 1859)/4104. + k2 * 2 - k3 * 3544/2565. - k5 * 0.275))
ynew = y + dt * ((k1 * 475 + k4 * 2197)/4104. + k3 * 1408/2565. - k5 * 0.2)
yaux = y + dt * ((k1 * 6688 + k4 * 28561 + k6 * 2052)/56430. + k3 * 6656/12825. - k5 * 0.18)
end subroutine Fehlberg4_5
subroutine Cash_Karp5_4 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6
osol = 5
oaux = 4
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.2, y + dt * k1 * 0.2)
k3 = dydt (t + dt * 0.3, y + dt * (k1 * 3 + k2 * 9) * 0.025)
k4 = dydt (t + dt * 0.6, y + dt * (k1 * 3 - k2 * 9 + k3 * 12) * 0.1)
k5 = dydt (t + dt, y + dt * (- k1 * 11 + k2 * 135 - k3 * 140 + k4 * 70)/54.)
k6 = dydt (t + dt * 0.875, y + dt * (k1 * 3262 + k2 * 37800 + k3 * 4600 + k4 * 44275 + k5 * 6831)/110592.)
ynew = y + dt * (k1 * 37/378. + k3 * 250/621. + k4 * 125/594. + k6 * 512/1771.)
yaux = y + dt * ((k1 * 5650 + k4 * 13525)/55296. + k3 * 18575/48384. + k5 * 277/14336. + k6 * 0.25)
end subroutine Cash_Karp5_4
subroutine Dormand_Prince5_4 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6, k7
osol = 5
oaux = 4
k1 = dydt (t, y)
k2 = dydt (t + dt * 0.2, y + dt * k1 * 0.2)
k3 = dydt (t + dt * 0.3, y + dt * (k1 * 0.075 + k2 * 0.225))
k4 = dydt (t + dt * 0.8, y + dt * (k1 * 44 - k2 * 168 + k3 * 160)/45.)
k5 = dydt (t + dt * C8_9, y + dt * (k1 * 19372 - k2 * 76080 + k3 * 64448 - k4 * 1908)/6561.)
k6 = dydt (t + dt, y + dt * ((k1 * 9017 - k2 * 34080 + k4 * 882)/3168. + k3 * 46732/5247. - k5 * 5103/18656.))
ynew = y + dt * ((k1 * 35 + k4 * 250)/384. + k3 * 500/1113. - k5 * 2187/6784. + k6 * 11/84.)
k7 = dydt (t + dt, ynew)
yaux = y + dt * ((k1 * 5179 + k4 * 35370)/57600. + k3 * 7571/16695. - k5 * 92097/339200. + k6 * 187/2100. + k7 * 0.025)
end subroutine Dormand_Prince5_4
subroutine Verner6_5 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none
real*8, intent(in) :: t, dt
real*8, dimension(:), intent(in) :: y
procedure(dydt_tem) :: dydt
integer*4, intent(out) :: osol, oaux
real*8, dimension(size (y)), intent(out) :: ynew, yaux
real*8, dimension(size (y)) :: k1, k2, k3, k4, k5, k6, k7, k8
osol = 6
oaux = 5
k1 = dydt (t, y)
k2 = dydt (t + dt * C1_6, y + dt * k1 * C1_6)
k3 = dydt (t + dt * C4_15, y + dt * (k1 * 4 + k2 * 16)/75.)
k4 = dydt (t + dt * C2_3, y + dt * (k1 * 5 - k2 * 16 + k3 * 15) * C1_6)
k5 = dydt (t + dt * C5_6, y + dt * ((- k1 * 165 - k3 * 425)/64. + (k2 * 880 + k4 * 85)/96.))
k6 = dydt (t + dt, y + dt * ((k1 * 612 + k5 * 88)/255. - k2 * 8 + (k3 * 4015 - k4 * 187)/612.))
k7 = dydt (t + dt * C1_15, y + dt * ((- k1 * 8263 + k2 * 24800)/15000. - k3 * 643/680. - k4 * 0.324 + k5 * 2484/10625.))
k8 = dydt (t + dt, y + dt * (k1 * 3501/1720. + (297275 * k3 - 367200 * k2)/52632. - k4 * 319/2322. + &
& k5 * 24068/84065. + k7 * 3850/26703.))
ynew = y + dt * (k1 * 0.075 + k3 * 875/2244. + (k4 * 3703 + k7 * 125)/11592. + k5 * 264/1955. + k8 * 43/616.)
yaux = y + dt * ((k1 * 13 + k4 * 50)/160. + (k3 * 2375 + k6 * 408)/5984. + k5 * 12/85.)
end subroutine Verner6_5
subroutine Fehlberg7_8 (t, y, dt, dydt, osol, oaux, yaux, ynew)
implicit none