-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmod_timeStep.f90
1269 lines (947 loc) · 39.5 KB
/
mod_timeStep.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
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! This file is part of NSCouette, a HPC code for DNS of Taylor-Couette flow !
! !
! Copyright (C) 2016 Marc Avila, Bjoern Hof, Jose Manuel Lopez, !
! Markus Rampp, Liang Shi !
! !
! NSCouette is free software: you can redistribute it and/or modify !
! it under the terms of the GNU General Public License as published by !
! the Free Software Foundation, either version 3 of the License, or !
! (at your option) any later version. !
! !
! NSCouette is distributed in the hope that it will be useful, !
! but WITHOUT ANY WARRANTY; without even the implied warranty of !
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the !
! GNU General Public License for more details. !
! !
! You should have received a copy of the GNU General Public License !
! along with NSCouette. If not, see <http://www.gnu.org/licenses/>. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
module mod_timestep
! Time splitting scheme based on a predictor-corrector method
! Influence matrices are used to impose the boundary conditions
! For further details see documentation of openpipeflow.org
! Main idea:
! 1) Compute predictor of velocity field
! 2) Corrector step: recompute non linear terms and iterate up to velocity field converges
! whithin the tolerance requested in mod_params.f90
! Note that all boundary conditions are imposed on the velocity field and thus
! no artificial boundary conditions for the pressure are needed.
! Linear equations
! (A*x=b)
! are solved by LU decomposition method and in spectral space
USE mod_inOut
IMPLICIT NONE
private
! Variables for linear equations A*x = b, L is laplacian operator
COMPLEX(KIND=8), allocatable :: b(:)
! Velocity u_old,u,u _new and nonlinear term udu
TYPE (vec_mpi) :: u_hat_mp_old, u_hat_mp_int
TYPE (vec_mpi) :: udu_hat_mp_old
COMPLEX(KIND=8), allocatable, dimension(:,:) :: u_plus_old,u_minus_old
COMPLEX(KIND=8), allocatable, dimension(:,:) :: u_plus_new,u_minus_new
COMPLEX(KIND=8), allocatable, dimension(:,:) :: udu_plus_old,udu_minus_old
INTEGER(KIND=4), PARAMETER :: iwidth=(n_s-1)/2, idiag=iwidth+1
!Laplacian matrices for the RHS of the NS equations
REAL(KIND=8), dimension(:,:,:), allocatable :: Lap_uz, Lap_up, Lap_um
#ifdef TE_CODE
REAL(KIND=8), dimension(:,:,:), allocatable :: Lap_T
#endif /* TE_CODE */
!Variables to check convergence
REAL(kind=8) :: corr_dt, dterr
!Influence matrices and correction step
REAL(KIND=8), dimension(:,:,:), allocatable :: u_functions
REAL(kind=8), dimension(:,:,:), allocatable :: inf_matrix
REAL(kind=8) :: BRe(8),BIm(8), aR(8),aI(8)
! see http://software.intel.com/en-us/forums/topic/293641
!DEC$ OPTIONS /NOWARN
type LUdcmp
sequence
real(kind=8), allocatable :: LU(:,:)
integer(kind=4), allocatable :: ipiv(:)
integer(kind=4) :: n,k,lda,ldb,info
end type LUdcmp
!DEC$ END OPTIONS
type(LUdcmp), dimension(:), allocatable :: LU_p,LU_uz,LU_up,LU_um
#ifdef TE_CODE
type(LUdcmp), dimension(:), allocatable :: LU_T
!-----------variables to adjust axial so that net mass flux is zero
REAL(kind=8), dimension(:), allocatable :: adj_flux(:)
REAL(kind=8) :: c_adj
#endif /* TE_CODE */
public :: pre_timeStep,post_timeStep,predictor,corrector,check_convergence,new_tstep
CONTAINS
SUBROUTINE pre_timeStep(init)
USE mod_fdInit
IMPLICIT NONE
LOGICAL, INTENT(in), OPTIONAL :: init
REAL(KIND=8) :: bA(n_s,m_r),bL(n_s,m_r)
REAL(KIND=8) :: inv_Mw_dr(m_r,m_r)
INTEGER(KIND=4) :: i,j,k,h
Complex(kind=8) :: aux1(m_r),aux2(m_r),aux3(m_r),auxp(m_r),auxm(m_r),x(m_r)
logical :: initialization=.false.
call perfon('pretstp')
if (present(init)) initialization=init
! Initialise variables
if (initialization) then
!allocate variables
ierr=u_hat_mp_old%alloc(m_r,mp_f)
ierr=u_hat_mp_int%alloc(m_r,mp_f)
ierr=udu_hat_mp_old%alloc(m_r,mp_f)
allocate(u_plus_old(m_r,mp_f))
allocate(u_minus_old(m_r,mp_f))
allocate(u_plus_new(m_r,mp_f))
allocate(u_minus_new(m_r,mp_f))
allocate(udu_plus_old(m_r,mp_f))
allocate(udu_minus_old(m_r,mp_f))
allocate(LU_p(mp_f),LU_uz(mp_f),LU_up(mp_f),LU_um(mp_f))
allocate(Lap_uz(n_s,m_r,mp_f), Lap_up(n_s,m_r,mp_f), Lap_um(n_s,m_r,mp_f))
#ifdef TE_CODE
allocate(LU_T(mp_f),Lap_T(n_s,m_r,mp_f))
allocate(adj_flux(m_r))
#endif /* TE_CODE */
allocate(inf_matrix(8,8,mp_f))
allocate(b(m_r))
allocate(bMw_dr(n_s,m_r),bMw_drr(n_s,m_r))
allocate(dr1((n_s-1)/2+1,0:(n_s-1)/2),dr2((n_s-1)/2+1,0:(n_s-1)/2))
allocate(intrdr(m_r))
allocate(row_inv_Mw_dr(m_r))
corr_dt = 0.0d0
dterr = 0.0d0
new_dt = .false.
! Radial derivatives
Call fd_Matrix(n_s-1,m_r-1,r,bMw_dr,bMw_drr,dr1,dr2,intrdr)
! Precompute the inverse of bMw_dr (-> row_inv_Mw_dr in output_energy)
bA(:,:)=bMw_dr
do h=1,idiag
bA(idiag-h+1,h) = 0.d0
enddo
bA(idiag,1) = 1.d0
call bmatinv(m_r,(n_s-1)/2,bA,inv_Mw_dr)
row_inv_Mw_dr(:)=inv_Mw_dr(m_r,:)
end if
if(.not. allocated(u_functions)) allocate( u_functions(m_r,mp_f,12) )
! Calculate Laplacian differentiation Matrix
! and precompute the LU decompositions
!$OMP PARALLEL DO &
!$OMP SCHEDULE (RUNTIME) &
#if !defined(__GFORTRAN__) && !defined(__XLF__) /* workaround for gcc bug 59488 and related xlf bug */
!$OMP DEFAULT(NONE) &
#endif
!$OMP PRIVATE(h,i,j,k,bA,bL,BRe,BIm,aux1,aux2,aux3,auxp,auxm,x) &
!$OMP SHARED(mp_f,m_r,dt,init,bMw_dr,bMw_drr,fk_mp,LU_p,LU_uz,LU_up,LU_um,&
!$OMP Lap_up,Lap_uz,Lap_um,u_functions,inf_matrix,r&
#ifdef TE_CODE
!$OMP ,LU_T,Lap_T,adj_flux,c_adj,Pr,intrdr)
#else
!$OMP )
#endif /* TE_CODE */
DO k=1,mp_f
DO j=1,m_r
bL(:,j)=0.d0
h = iwidth + 1 - j
do i = max(1,j-iwidth), min(m_r,j+iwidth)
bL(h+i,j) = bMw_dr(h+i,j)/r(i) + bMw_drr(h+i,j)
enddo
bL(idiag,j) = bL(idiag,j) - (fk_mp%th(j,k)/r(j))**2 - (fk_mp%z(j,k))**2
END DO
if (init) then
!pressure
bA(:,:) = bL(:,:)
! Neumann B.C.
do h=1,idiag
bA(idiag-h+1,h) = bMw_dr(idiag-h+1,h) ! A(1,:) = Mw_dr(1,:)
bA(idiag+h-1,m_r-h+1) = bMw_dr(idiag+h-1,m_r-h+1)! A(m_r,:) = Mw_dr(m_r,:)
enddo
! Replace the Neumann BC by Dirichlet at r = r_o
if (abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon) then
do h=1,idiag
bA(idiag+h-1,m_r-h+1) = 0.d0 ! A(m_r,:) = 0.d0
enddo
bA(idiag,m_r) = 1.d0 ! A(m_r,m_r) = 1.d0
end if
call luDecmp(m_r,(n_s-1)/2,bA,LU_p(k))
end if
#ifdef TE_CODE
!RHS equation
Lap_T(:,:,k) = (1d0-d_implicit)*bL(:,:)
Lap_T(idiag,:,k) = Pr/dt + Lap_T(idiag,:,k)
!LHS equation
bA(:,:) = -(d_implicit)*bL(:,:)
bA(idiag,:) = Pr/dt + bA(idiag,:)
!Dirichlet boundary conditions
do h=1,idiag
bA(idiag-h+1,h) = 0.d0 ! A(1,:) = 0.d0
bA(idiag+h-1,m_r-h+1) = 0.d0 ! A(m_r,:) = 0.d0
enddo
bA(idiag,1) = 1.d0 ! A(1,1) = 1.d0
bA(idiag,m_r) = 1.d0 ! A(m_r,m_r) = 1.d0
call luDecmp(m_r,(n_s-1)/2,bA,LU_T(k))
#endif /* TE_CODE */
!predictor_v
!u_z
!RHS equation
Lap_uz(:,:,k) = (1d0-d_implicit)*bL(:,:)
Lap_uz(idiag,:,k) = 1.0D0/dt + Lap_uz(idiag,:,k)
!LHS equation
bA(:,:) = -(d_implicit)*bL(:,:)
bA(idiag,:) = 1.D0/dt + bA(idiag,:)
!Dirichlet boundary conditions
do h=1,idiag
bA(idiag-h+1,h) = 0.d0 ! A(1,:) = 0.d0
bA(idiag+h-1,m_r-h+1) = 0.d0 ! A(m_r,:) = 0.d0
enddo
bA(idiag,1) = 1.d0 ! A(1,1) = 1.d0
bA(idiag,m_r) = 1.d0 ! A(m_r,m_r) = 1.d0
call luDecmp(m_r,(n_s-1)/2,bA,LU_uz(k))
#ifdef TE_CODE
!Compute function to correct the axial flow so that the net mass flux is zero
if ((abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon)) then
aux1(:) = dcmplx(1d0,0d0)
aux1(1) = dcmplx(0d0,0d0)
aux1(m_r) = dcmplx(0d0,0d0)
call luSolve(LU_uz(k),aux1(:), x)
adj_flux = dreal(x)
c_adj = dot_product(adj_flux, intrdr)
end if
#endif /* TE_CODE */
!u_+
bA(:,:) = bL(:,:)
do j=1,m_r
bA(idiag,j) = bA(idiag,j) - (1+2*fk_mp%th(j,k))/r(j)**2
enddo
!RHS equation
Lap_up(:,:,k) = (1d0-d_implicit)*bA(:,:)
Lap_up(idiag,:,k) = 1.0D0/dt + Lap_up(idiag,:,k)
!LHS equation
bA(:,:) = -(d_implicit)*bA(:,:)
bA(idiag,:) = 1.0D0/dt + bA(idiag,:)
!Dirichlet boundary conditions
do h=1,idiag
bA(idiag-h+1,h) = 0.d0 ! A(1,:) = 0.d0
bA(idiag+h-1,m_r-h+1) = 0.d0 ! A(m_r,:) = 0.d0
enddo
bA(idiag,1) = 1.d0 ! A(1,1) = 1.d0
bA(idiag,m_r) = 1.d0 ! A(m_r,m_r) = 1.d0
call luDecmp(m_r,(n_s-1)/2,bA,LU_up(k))
!u_-
bA(:,:) = bL(:,:)
do j=1,m_r
bA(idiag,j) = bA(idiag,j) + (-1+2*fk_mp%th(j,k))/r(j)**2
enddo
!RHS equation
Lap_um(:,:,k) = (1d0-d_implicit)*bA(:,:)
Lap_um(idiag,:,k) = 1.0D0/dt + Lap_um(idiag,:,k)
!LHS equation
bA(:,:) = -(d_implicit)*bA(:,:)
bA(idiag,:) = 1.0D0/dt + bA(idiag,:)
!Dirichlet boundary conditions
do h=1,idiag
bA(idiag-h+1,h) = 0.d0 ! A(1,:) = 0.d0
bA(idiag+h-1,m_r-h+1) = 0.d0 ! A(m_r,:) = 0.d0
enddo
bA(idiag,1) = 1.d0 ! A(1,1) = 1.d0
bA(idiag,m_r) = 1.d0 ! A(m_r,m_r) = 1.d0
call luDecmp(m_r,(n_s-1)/2,bA,LU_um(k))
!Get influence matrices
if (.not.(abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon)) then
aux1(:) = (0d0,0d0)
aux2(:) = (0d0,0d0)
aux3(:) = (0d0,0d0)
!Get u function when u+=1 is the boundary condition
!inner cylinder
aux1(1) = (1d0,0d0)
call luSolve(LU_up(k),aux1(:), x)
u_functions(:,k,1) = DREAL(x)
call evalBC( k, x, aux2, aux3, BRe, BIm)
inf_matrix(:,1,k) = BRe(:)
aux1(1) = (0d0,0d0)
!outer cylinder
aux1(m_r) = (1d0,0d0)
call luSolve(LU_up(k),aux1(:), x)
u_functions(:,k,7) = DREAL(x)
call evalBC( k, x, aux2, aux3, BRe, BIm)
inf_matrix(:,5,k) = BRe(:)
aux1(m_r) = (0d0,0d0)
!Get u function when u-=1 is the boundary condition
!inner cylinder
aux2(1) = (1d0,0d0)
call luSolve(LU_um(k),aux2(:), x)
u_functions(:,k,2) = DREAL(x)
call evalBC( k, aux1, x ,aux3 , BRe ,BIm )
inf_matrix(:,2,k) = BRe(:)
aux2(1) = (0d0,0d0)
!outer cylinder
aux2(m_r) = (1d0,0d0)
call luSolve(LU_um(k),aux2(:), x)
u_functions(:,k,8) = DREAL(x)
call evalBC( k, aux1, x ,aux3 , BRe ,BIm )
inf_matrix(:,6,k) = BRe(:)
aux2(m_r) = (0d0,0d0)
!Get u function when uz=i is the boundary condition
!inner cylinder
aux3(1) = (0d0,1d0)
call luSolve(LU_uz(k),aux3(:), x)
u_functions(:,k,3) = DIMAG(x)
call evalBC( k, aux1, aux2, x, BRe, BIm)
inf_matrix(:,3,k) = BRe(:)
aux3(1) = (0d0,0d0)
!outer cylinder
aux3(m_r) = (0d0,1d0)
call luSolve(LU_uz(k),aux3(:), x)
u_functions(:,k,9) = DIMAG(x)
call evalBC( k, aux1, aux2, x, BRe, BIm)
inf_matrix(:,7,k) = BRe(:)
aux3(m_r) = (0d0,0d0)
!Get functions corresponding to the pressure
!Inner cylinder
aux1(1) = (-1d0,0d0)
call luSolve(LU_p(k),aux1(:), x)
!Obtain gradient of pressure
aux1(:) = DZGBMV(bMw_dr,x(:),iwidth,iwidth) !radial derivative
aux2(:)= ii*fk_mp%th(:,k)/r(:)*x(:) !azimuthal derivative
aux3(:) = ii*fk_mp%z(:,k)*x(:) !axial derivative
!change of variables to decouple radial and azimuthal equations
auxp = aux1 + ii*aux2
auxm = aux1 - ii*aux2
u_functions(:,k,4) = DREAL(auxp)
u_functions(:,k,5) = DREAL(auxm)
u_functions(:,k,6) = DIMAG(aux3)
aux1(1)=(0d0,0d0)
call evalBC( k, auxp,auxm,aux3, BRe,BIm)
inf_matrix(:,4,k) = BRe(:)
!Outer cylinder
aux1(m_r) = (-1d0,0d0)
call luSolve(LU_p(k),aux1(:), x)
!Obtain gradient of pressure
aux1(:) = DZGBMV(bMw_dr,x(:),iwidth,iwidth)!radial derivative
aux2(:)= ii*fk_mp%th(:,k)/r(:)*x(:)!azimuthal derivative
aux3(:) = ii*fk_mp%z(:,k)*x(:) !axial derivative
!change of variables to decouple radial and azimuthal equations
auxp = aux1 + ii*aux2
auxm = aux1 - ii*aux2
u_functions(:,k,10) = DREAL(auxp)
u_functions(:,k,11) = DREAL(auxm)
u_functions(:,k,12) = DIMAG(aux3)
call evalBC( k, auxp,auxm,aux3, BRe,BIm)
inf_matrix(:,8,k) = BRe(:)
!Invert influence matrix
call mat_inv(8,inf_matrix(:,:,k),8)
endif
END DO
!$OMP END PARALLEL DO
call perfoff()
END SUBROUTINE pre_timeStep
!--------------------------------------------------------------------
SUBROUTINE post_timeStep()
IMPLICIT NONE
!deallocate variables
ierr=u_hat_mp_old%dealloc()
ierr=u_hat_mp_int%dealloc()
ierr=udu_hat_mp_old%dealloc()
deallocate(u_plus_old,u_minus_old)
deallocate(u_plus_new,u_minus_new)
deallocate(udu_plus_old,udu_minus_old)
deallocate(LU_p,LU_uz,LU_up,LU_um)
deallocate(Lap_uz,Lap_up,Lap_um)
deallocate(inf_matrix)
#ifdef TE_CODE
deallocate(LU_T,Lap_T)
deallocate(adj_flux)
#endif /* TE_CODE */
deallocate(bMw_dr,bMw_drr,dr1,dr2)
deallocate(row_inv_Mw_dr)
deallocate(b,r,th,z)
ierr=u_hat_mp%dealloc()
ierr=udu_hat_mp%dealloc()
ierr=fk_mp%dealloc()
deallocate(p_hat_mp)
END SUBROUTINE post_timeStep
Subroutine predictor()
IMPLICIT NONE
Integer(kind=4) :: i, p, k,j
COMPLEX(KIND=8) :: u_plus(m_r),u_minus(m_r)
COMPLEX(KIND=8) :: udu_plus(m_r),udu_minus(m_r)
COMPLEX(KIND=8) :: rhs_p(m_r),rhs_m(m_r),rhs_z(m_r)
COMPLEX(kind=8) :: rhs_r(m_r),rhs_th(m_r),dotp(m_r)
call perfon('predic')
!$OMP PARALLEL DO &
!$OMP SCHEDULE (RUNTIME) &
#if !defined(__GFORTRAN__) && !defined(__XLF__) /* workaround for gcc bug 59488 and related xlf bug */
!$OMP DEFAULT(NONE) &
#endif
!$OMP PRIVATE(i,p,k,j,b,dotp,aR,aI,BRe,BIm,rhs_p,rhs_m,rhs_z,rhs_r,rhs_th, &
!$OMP u_plus,u_minus,udu_plus,udu_minus) &
!$OMP SHARED(mp_f,m_r,Re_i,Re_o,LU_p,Lu_up,Lu_um,Lu_uz,u_hat_mp,udu_hat_mp,&
!$OMP fk_mp,p_hat_mp,bMw_dr,u_plus_old,u_minus_old, &
!$OMP udu_hat_mp_old,u_hat_mp_old,inf_matrix,u_functions,Lap_uz,Lap_up,Lap_um,dr1,dr2,r&
#ifdef TE_CODE
!$OMP ,LU_T,Lap_T)
#else
!$OMP )
#endif /* TE_CODE */
do k=1,mp_f
! Do change of variables to decouple equations for u_r & u_th
u_plus(:) = u_hat_mp%r(:,k) + ii*u_hat_mp%th(:,k)
u_minus(:) = u_hat_mp%r(:,k) - ii*u_hat_mp%th(:,k)
udu_plus(:) = udu_hat_mp%r(:,k) + ii*udu_hat_mp%th(:,k)
udu_minus(:) = udu_hat_mp%r(:,k) - ii*udu_hat_mp%th(:,k)
! Save velocities and nonlinear terms at step n
u_plus_old(:,k) = u_plus(:)
u_minus_old(:,k) = u_minus(:)
udu_hat_mp_old%r(:,k) = udu_hat_mp%r(:,k)
udu_hat_mp_old%th(:,k) = udu_hat_mp%th(:,k)
udu_hat_mp_old%z(:,k) = udu_hat_mp%z(:,k)
u_hat_mp_old%r(:,k) = u_hat_mp%r(:,k)
u_hat_mp_old%th(:,k) = u_hat_mp%th(:,k)
u_hat_mp_old%z(:,k) = u_hat_mp%z(:,k)
#ifdef TE_CODE
udu_hat_mp_old%T(:,k) = udu_hat_mp%T(:,k)
u_hat_mp_old%T(:,k) = u_hat_mp%T(:,k)
!Compute predictor of temperature
!Laplacian of temperature at step n
rhs_p(:)=DZGBMV(Lap_T(:,:,k),u_hat_mp_old%T(:,k),iwidth,iwidth)
!Adding non-linear terms
rhs_p(:)= rhs_p(:) + udu_hat_mp%T(:,k)
!Set boundary conditions to zero
rhs_p(1) = dcmplx(0d0,0d0)
rhs_p(m_r) = dcmplx(0d0,0d0)
! Boundary conditions for mode 0 (prescribed temperature at the cylinders)
IF (ABS(fk_mp%th(1,k)) <= epsilon .AND. ABS(fk_mp%z(1,k)) <= epsilon) THEN
rhs_p(1) = dcmplx(0.5d0,0d0)
rhs_p(m_r) = dcmplx(-0.5d0,0d0)
end IF
call luSolve(LU_T(k),rhs_p(:),u_hat_mp%T(:,k))
#endif /* TE_CODE */
!Get RHS of radial momentum equation
!First compute product of Laplacian and velocity
rhs_p(:)=DZGBMV(Lap_up(:,:,k),u_plus(:),iwidth,iwidth)
rhs_m(:)=DZGBMV(Lap_um(:,:,k),u_minus(:),iwidth,iwidth)
rhs_z(:)=DZGBMV(Lap_uz(:,:,k),u_hat_mp%z(:,k),iwidth,iwidth)
!Adding non-linear terms
rhs_p(:)=rhs_p(:)+udu_plus(:)
rhs_m(:)=rhs_m(:)+udu_minus(:)
rhs_z(:)=rhs_z(:)+udu_hat_mp%z(:,k)
!Get pressure (project RHS)
rhs_r(:)= 0.5D0 * (rhs_p(:) + rhs_m(:))
rhs_th(:)= -0.5D0*ii*(rhs_p(:) - rhs_m(:))
!set boundary conditions to zero
b(1)= (0d0,0d0)
b(m_r)= (0d0,0d0)
!Boundary condition for mode zero
dotp(:) = DZGBMV(bMw_dr,rhs_r(:),iwidth,iwidth)
DO j=2,m_r-1
b(j) = dotp(j) &
+ rhs_r(j)/r(j) &
+ii*fk_mp%th(j,k)*rhs_th(j)/r(j)&
+ii*fk_mp%z(j,k)*rhs_z(j)
END DO
if (abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon) then
p_hat_mp(:,k) = (0d0,0d0)
else
! solve system using LU decomposition precomputed in pre_timeStep()
call luSolve(LU_p(k),b,p_hat_mp(:,k))
end if
!Compute pressure gradient and substract from rhs (written directly as rhs_p and rhs_m)
dotp(:)=DZGBMV(bMw_dr,p_hat_mp(:,k),iwidth,iwidth)
rhs_p(:)=rhs_p(:)-dotp(:)+fk_mp%th(:,k)*p_hat_mp(:,k)/r(:)
rhs_m(:)=rhs_m(:)-dotp(:)-fk_mp%th(:,k)*p_hat_mp(:,k)/r(:)
rhs_z(:)=rhs_z(:)-ii*fk_mp%z(:,k)*p_hat_mp(:,k)
!Set boundary conditions to zero
rhs_p(1)=(0d0,0d0)
rhs_p(m_r)=(0d0,0d0)
rhs_m(1)=(0d0,0d0)
rhs_m(m_r)=(0d0,0d0)
rhs_z(1)=(0d0,0d0)
rhs_z(m_r)=(0d0,0d0)
!boundary conditions for mode zero
IF (ABS(fk_mp%th(1,k)) <= epsilon .AND. ABS(fk_mp%z(1,k)) <= epsilon) THEN
rhs_p(1) = ii*Re_i
rhs_p(m_r) = ii*Re_o
rhs_m(1) = -ii*Re_i
rhs_m(m_r) = -ii*Re_o
end IF
!Solve equations
call luSolve(LU_up(k),rhs_p(:),u_plus(:))
call luSolve(LU_um(k),rhs_m(:),u_minus(:))
call luSolve(LU_uz(k),rhs_z(:),u_hat_mp%z(:,k))
!correct boundary conditions with influence matrix
if (.not.(abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon)) then
call evalBC( k, u_plus(:),u_minus(:),u_hat_mp%z(:,k),BRe,BIm)
aR(:) = -matmul(inf_matrix(:,:,k),BRe(:))
aI(:) = -matmul(inf_matrix(:,:,k),BIm(:))
do i = 1, 2
j = (i-1)*4
p = (i-1)*6
u_plus(:) = dcmplx(real(u_plus(:)) + &
aR(j+1)*u_functions(:,k,p+1)+ aR(j+4)*u_functions(:,k,p+4),&
dimag(u_plus(:)) + aI(j+1)*u_functions(:,k,p+1) + &
aI(j+4)*u_functions(:,k,p+4))
u_minus(:) = dcmplx(real(u_minus(:)) + &
aR(j+2)*u_functions(:,k,p+2)+ aR(j+4)*u_functions(:,k,p+5),&
dimag(u_minus(:)) + aI(j+2)*u_functions(:,k,p+2) + &
aI(j+4)*u_functions(:,k,p+5))
u_hat_mp%z(:,k) = dcmplx(real(u_hat_mp%z(:,k)) - &
aI(j+3)*u_functions(:,k,p+3) - aI(j+4)*u_functions(:,k,p+6),&
dimag(u_hat_mp%z(:,k)) + aR(j+3)*u_functions(:,k,p+3) + &
aR(j+4)*u_functions(:,k,p+6))
end do
endif
!undo change of variables
u_hat_mp%r(:,k) = 0.5D0 *(u_plus(:) + u_minus(:))
u_hat_mp%th(:,k) = -0.5D0*ii*(u_plus(:) - u_minus(:))
end do
!$OMP END PARALLEL DO
!free divergence conditions for mode 0
if(myid .eq. root) then
u_hat_mp%r(:,1)=dcmplx(0d0,0d0)
u_hat_mp%th(:,1)=dcmplx(dreal(u_hat_mp%th(:,1)),0d0)
u_hat_mp%z(:,1)=dcmplx(dreal(u_hat_mp%z(:,1)),0d0)
#ifdef TE_CODE
!Impose net zero mass flux
call adjust_axialflux()
#endif
end if
call perfoff()
end subroutine predictor
Subroutine corrector()
IMPLICIT NONE
Integer(kind=4) :: i, p, k,j
COMPLEX(KIND=8) :: u_plus(m_r),u_minus(m_r)
COMPLEX(KIND=8) :: udu_plus(m_r),udu_minus(m_r)
COMPLEX(KIND=8) :: rhs_p(m_r),rhs_m(m_r),rhs_z(m_r)
COMPLEX(kind=8) :: rhs_r(m_r),rhs_th(m_r),dotp(m_r)
real(kind=8) :: corr
call perfon('correc')
!$OMP PARALLEL DO &
!$OMP SCHEDULE (RUNTIME) &
#if !defined(__GFORTRAN__) && !defined(__XLF__) /* workaround for gcc bug 59488 and related xlf bug */
!$OMP DEFAULT(NONE) &
#endif
!$OMP PRIVATE(i,p,k,j,b,dotp,aR,aI,BRe,BIm,rhs_p,rhs_m,rhs_z,rhs_r,rhs_th, &
!$OMP u_plus,u_minus,udu_plus,udu_minus) &
!$OMP SHARED(mp_f,m_r,LU_p,Lu_up,Lu_um,Lu_uz,u_hat_mp,udu_hat_mp,fk_mp,p_hat_mp, &
!$OMP bMw_dr,udu_hat_mp_old,u_hat_mp_int,r,&
!$OMP inf_matrix,u_functions,Lap_uz,Lap_up,Lap_um,dr1,dr2,u_plus_old,u_minus_old,u_hat_mp_old,Re_i,Re_o&
#ifdef TE_CODE
!$OMP ,LU_T,Lap_T)
#else
!$OMP )
#endif /* TE_CODE */
do k=1,mp_f
u_hat_mp_int%r(:,k) = u_hat_mp%r(:,k)
u_hat_mp_int%th(:,k) = u_hat_mp%th(:,k)
u_hat_mp_int%z(:,k) = u_hat_mp%z(:,k)
!multiply b by time-stepping coefficient (c NLq+1 + (1-c) NLq)
udu_hat_mp%r(:,k) = d_implicit*udu_hat_mp%r(:,k) + (1d0-d_implicit)* udu_hat_mp_old%r(:,k)
udu_hat_mp%th(:,k) = d_implicit*udu_hat_mp%th(:,k)+ (1d0-d_implicit)* udu_hat_mp_old%th(:,k)
udu_hat_mp%z(:,k) = d_implicit*udu_hat_mp%z(:,k) + (1d0-d_implicit)* udu_hat_mp_old%z(:,k)
#ifdef TE_CODE
udu_hat_mp%T(:,k) = d_implicit*udu_hat_mp%T(:,k) + (1d0-d_implicit)* udu_hat_mp_old%T(:,k)
#endif
!Change of variables
udu_plus(:) = udu_hat_mp%r(:,k) + ii*udu_hat_mp%th(:,k)
udu_minus(:) = udu_hat_mp%r(:,k) - ii*udu_hat_mp%th(:,k)
#ifdef TE_CODE
!Correct the temperature
!Laplacian of temperature at step n
rhs_p(:)=DZGBMV(Lap_T(:,:,k),u_hat_mp_old%T(:,k),iwidth,iwidth)
!Adding non-linear terms
rhs_p(:)=rhs_p(:)+udu_hat_mp%T(:,k)
!Set boundary conditions to zero
rhs_p(1) = dcmplx(0d0,0d0)
rhs_p(m_r) = dcmplx(0d0,0d0)
! Boundary conditions for mode 0 (prescribed temperature at the cylinders)
IF (ABS(fk_mp%th(1,k)) <= epsilon .AND. ABS(fk_mp%z(1,k)) <= epsilon) THEN
rhs_p(1) = dcmplx(0.5d0,0d0)
rhs_p(m_r) = dcmplx(-0.5d0,0d0)
end IF
call luSolve(LU_T(k),rhs_p(:),u_hat_mp%T(:,k))
#endif /* TE_CODE */
rhs_p(:)=DZGBMV(Lap_up(:,:,k),u_plus_old(:,k),iwidth,iwidth)
rhs_m(:)=DZGBMV(Lap_um(:,:,k),u_minus_old(:,k),iwidth,iwidth)
rhs_z(:)=DZGBMV(Lap_uz(:,:,k),u_hat_mp_old%z(:,k),iwidth,iwidth)
!Adding non-linear terms
rhs_p(:)=rhs_p(:)+udu_plus(:)
rhs_m(:)=rhs_m(:)+udu_minus(:)
rhs_z(:)=rhs_z(:)+udu_hat_mp%z(:,k)
!Get pressure (project RHS)
rhs_r(:)= 0.5D0 * (rhs_p(:) + rhs_m(:))
rhs_th(:)= -0.5D0*ii*(rhs_p(:) - rhs_m(:))
!set boundary conditions to zero
b(1)= (0d0,0d0)
b(m_r)= (0d0,0d0)
!Boundary condition for mode zero
dotp(:) = DZGBMV(bMw_dr,rhs_r(:),iwidth,iwidth)
!call bmatvec(bMw_dr,rhs_r,dotp(:))
DO j=2,m_r-1
b(j) = dotp(j) &
+ rhs_r(j)/r(j) &
+ii*fk_mp%th(j,k)*rhs_th(j)/r(j)&
+ii*fk_mp%z(j,k)*rhs_z(j)
END DO
if (abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon) then
p_hat_mp(:,k) = (0d0,0d0)
else
!solve system using LU decomposition precomputed in pre_timeStep()
call luSolve(LU_p(k),b,p_hat_mp(:,k))
end if
!Compute pressure gradient and substract from rhs (written directly as rhs_p and rhs_m
dotp(:)=DZGBMV(bMw_dr,p_hat_mp(:,k),iwidth,iwidth)
rhs_p(:)=rhs_p(:)-dotp(:)+fk_mp%th(:,k)*p_hat_mp(:,k)/r(:)
rhs_m(:)=rhs_m(:)-dotp(:)-fk_mp%th(:,k)*p_hat_mp(:,k)/r(:)
rhs_z(:)=rhs_z(:)-ii*fk_mp%z(:,k)*p_hat_mp(:,k)
!Set boundary conditions to zero
rhs_p(1)=(0d0,0d0)
rhs_p(m_r)=(0d0,0d0)
rhs_m(1)=(0d0,0d0)
rhs_m(m_r)=(0d0,0d0)
rhs_z(1)=(0d0,0d0)
rhs_z(m_r)=(0d0,0d0)
!boundary conditions for mode zero
IF (ABS(fk_mp%th(1,k)) <= epsilon .AND. ABS(fk_mp%z(1,k)) <= epsilon) THEN
rhs_p(1) = ii*Re_i
rhs_p(m_r) = ii*Re_o
rhs_m(1) = -ii*Re_i
rhs_m(m_r) = -ii*Re_o
end IF
!Solve equations
call luSolve(LU_up(k),rhs_p(:),u_plus(:))
call luSolve(LU_um(k),rhs_m(:),u_minus(:))
call luSolve(LU_uz(k),rhs_z(:),u_hat_mp%z(:,k))
!correct boundary conditions with influence matrix
if (.not.(abs(fk_mp%th(1,k)) <= epsilon .and. abs(fk_mp%z(1,k)) <= epsilon)) then
call evalBC(k, u_plus(:),u_minus(:),u_hat_mp%z(:,k),BRe,BIm)
!call evalBC(2, k, u_plus(:,k),u_minus(:,k),u_hat_mp%z(:,k),BRe,BIm)
aR(:) = -matmul(inf_matrix(:,:,k),BRe(:))
aI(:) = -matmul(inf_matrix(:,:,k),BIm(:))
do i = 1, 2
j = (i-1)*4
p = (i-1)*6
u_plus(:) = dcmplx(real(u_plus(:)) + &
aR(j+1)*u_functions(:,k,p+1)+ aR(j+4)*u_functions(:,k,p+4),&
dimag(u_plus(:)) + aI(j+1)*u_functions(:,k,p+1) + &
aI(j+4)*u_functions(:,k,p+4))
u_minus(:) = dcmplx(real(u_minus(:)) + &
aR(j+2)*u_functions(:,k,p+2)+ aR(j+4)*u_functions(:,k,p+5),&
dimag(u_minus(:)) + aI(j+2)*u_functions(:,k,p+2) + &
aI(j+4)*u_functions(:,k,p+5))
u_hat_mp%z(:,k) = dcmplx(real(u_hat_mp%z(:,k)) - &
aI(j+3)*u_functions(:,k,p+3) - aI(j+4)*u_functions(:,k,p+6),&
dimag(u_hat_mp%z(:,k)) + aR(j+3)*u_functions(:,k,p+3) + &
aR(j+4)*u_functions(:,k,p+6))
end do
endif
!undo change of variables
u_hat_mp%r(:,k) = 0.5D0 *(u_plus(:) + u_minus(:))
u_hat_mp%th(:,k) = -0.5D0*ii*(u_plus(:) - u_minus(:))
end do
!$OMP END PARALLEL DO
!free divergence conditions for mode 0
if(myid .eq. root) then
u_hat_mp%r(:,1)=dcmplx(0d0,0d0)
u_hat_mp%th(:,1)=dcmplx(dreal(u_hat_mp%th(:,1)),0d0)
u_hat_mp%z(:,1)=dcmplx(dreal(u_hat_mp%z(:,1)),0d0)
#ifdef TE_CODE
!Impose net zero mass flux
call adjust_axialflux()
#endif
end if
! Compute error norm
call measurecorr(u_hat_mp%r,u_hat_mp%th,u_hat_mp%z, u_hat_mp_int%r,u_hat_mp_int%th,u_hat_mp_int%z,corr)
dterr = max(dterr, corr) ! TODO: double check if dterr is ever greater than zero
call perfoff()
end Subroutine corrector
subroutine measurecorr(c1,c2,c3, c1_,c2_,c3_,corr)
! compute the square root of the relative maximum distance (infinity norm)
! between two three-component vectors, u^j=(c1,c2,c3) and u^j-1=(c1_,c2_,c3_)
! i.e. corr := SQRT( || u^j - u^j-1 ||inf / || u^j ||inf )
implicit none
complex(kind=8), dimension(m_r,mp_f), intent(in) :: c1,c2,c3,c1_,c2_,c3_
real(kind=8), intent(out) :: corr
real(kind=8), dimension(2) :: d, d_
integer(kind=4) :: nh
call perfon('mcorr')
d(:) = -1d9
!$OMP PARALLEL &
!$OMP DEFAULT(NONE) &
!$OMP PRIVATE(nh) &
!$OMP SHARED(mp_f,d,c1,c2,c3,c1_,c2_,c3_)
!$OMP DO SCHEDULE (STATIC) &
!$OMP REDUCTION(max:d)
do nh = 1,mp_f
d(1) = max(d(1), &
maxval( dabs2(c1(:,nh)) ), &
maxval( dabs2(c2(:,nh)) ), &
maxval( dabs2(c3(:,nh)) ) )
d(2) = max(d(2), &
maxval( dabs2(c1(:,nh) - c1_(:,nh)) ), &
maxval( dabs2(c2(:,nh) - c2_(:,nh)) ), &
maxval( dabs2(c3(:,nh) - c3_(:,nh)) ))
end do
!$OMP END DO
!$OMP END PARALLEL
call mpi_allreduce(d, d_, 2, MPI_REAL8, MPI_MAX, COMM, ierr)
d = d_
if (d(1) .ne. 0) then
corr = dsqrt(d(2)/d(1))
else
corr = 0d0
endif
call perfoff()
return
contains
elemental function dabs2(x)
!computes abs(x)**2 of a complex number
complex(kind=8), intent(in) :: x
real(kind=8) :: dabs2
dabs2 = dreal(x)**2 + dimag(x)**2
end function dabs2
end subroutine measurecorr
subroutine check_convergence()
! check for convergence via the 2-norm of the correction
implicit none
real(kind=8), save :: lasterr
real(kind=8) :: d
! Measure correction of dt
d = max(dabs(re_i-re_o), 1.0d0)
if (iter .eq. 1) then
if (dterr .eq. 0.0d0) then ! prevent divison by zero in rare case of zero error norm
corr_dt = 0.0d0
else
corr_dt = dt * dsqrt(tolerance_dterr*d/dterr)
end if
lasterr = 1.0d99
end if
! Abort simulation if dt is very small
if (dt .lt. 1d-9 .and. i_time .gt. 30) then
if (myid .eq. root) print*, 'check_convergence: Decreasing time step (dt --> 0)!'
call MPI_ABORT(comm,102,ierr)
! Abort simulation if number of iterations is large
else if (iter .gt. 10) then
if (myid .eq. root) print*, 'check_convergence: Too many corrector iterations!'
call MPI_ABORT(comm,103,ierr)
! Abort simulation if error is increasing or change correction time?
else if (dterr .gt. lasterr) then
if (myid .eq. root) print*, 'check_convergence: Increasing error norm!'
if (dterr .gt. 2d0*tolerance_dterr*d) CALL MPI_ABORT(comm,104,ierr)
if (dterr .lt. 2d0*tolerance_dterr*d) corr_dt = dt/(1d1*Courant)
iter = 0
else if (dterr .gt. tolerance_dterr) then
lasterr = dterr
iter = iter + 1
else
if ((myid .eq. root) .and. (modulo(i_time, print_time_screen) .eq. 0)) then
if (variable_dt) then
print*, ' step=', i_time, ' dt=', dt
else
print*, ' step=', i_time, ' dt=', dt, ' its=', iter
end if
end if
iter = 0
end if
dterr = 0d0