-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow_statistic.F90
2179 lines (1709 loc) · 55.7 KB
/
flow_statistic.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
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
SUBROUTINE SAVE_STATS_DUCT(FINAL)
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
! INCLUDE 'header_duct'
use ntypes
use Domain
use Grid
use Fft_var, only : pi, CIKX
use TIME_STEP_VAR
use run_variable
use variable_stat
use mpi_var
implicit none
LOGICAL FINAL, TKE_BUDGET, MEAN_KE_BUDGET,MEAN_BACKGROUND,SAVE_3D, &
ZX_PLANE, XY_PLANE, ZX_PLANE_2,XY_PLANE_comb, ZX_PLANE_comb, &
ZX_PLANE_comb_2, PLANE_3D, XY_PLANE_comb_2
CHARACTER*35 FNAME
integer i,j,k,n
real(r8) :: uc, ubulk
OPEN (11,file='signal_plane_data',form='formatted',status='old')
READ(11,*)
READ(11,*) XY_PLANE_comb,XY_PLANE_comb_2, ZX_PLANE_comb,ZX_PLANE_comb_2, PLANE_3D
close(11)
XY_PLANE = .false.
ZX_PLANE = .false.
ZX_PLANE_2 = .false.
! PLANE_3D = .true.
! ZX_PLANE_comb_2 = .true.
! XY_PLANE_comb = .true.
! ZX_PLANE_comb = .true.
SAVE_3D = .false.
TKE_BUDGET = .false.
MEAN_KE_BUDGET = .false.
MEAN_BACKGROUND = .false.
IF ( rank .eq. 0) THEN
WRITE(6,*) 'Saving flow statistics.'
! write(6,*) 'Allocate all the tmp arrays'
IF (SAVE_3D) THEN
write(6,*) 'Writing 3D data field'
ENDIF
ENDIF
! allocating stat variables
! call allocate_temps
!C Apply Boundary conditions to velocity field
! IF (USE_MPI) THEN
! CALL APPLY_BC_VEL_MPI
! ELSE
CALL APPLY_BC_VEL_LOWER
CALL APPLY_BC_VEL_UPPER
CALL APPLY_BC_VEL_LEFT
CALL APPLY_BC_VEL_RIGHT
! END IF
IF (RANK .eq. 0) THEN
! Compute and write out bulk velocity
! Integrat the instantaneous mean profile numerically at GY points
UBULK= 0.0d0
area = 0.0d0
DO J=2,NY
DO K=2,NZ
area = area + DZ(k)*DY(j)
UBULK=UBULK+0.25*(dble(CU3X(0,K,J))+dble(CU3X(0,K-1,J)) + &
dble(CU3X(0,K-1,J-1)) + dble(CU3X(0,K,J-1)) )*DY(j)*DZ(K)
ENDDO
ENDDO
UBULK=UBULK/area
! Write out UBULK
write(6,*) 'UBULK: ',UBULK
ENDIF
! Save CUi
do k=0,NZ+1
do i=0,NX2P
do j=0,NY+1
CR1X(i,k,j)=CU1X(i,k,j)
CR2X(i,k,j)=CU2X(i,k,j)
CR3X(i,k,j)=CU3X(i,k,j)
! THIS STEPs ARE REQURIED WHEN DERIVATIVES w.r.t X IS REQURIED LATER
CF1X(i,k,j)=CU1X(i,k,j)
CF2X(i,k,j)=CU2X(i,k,j)
CF3X(i,k,j)=CU3X(i,k,j)
end do
end do
end do
! write(6,*)'I am here flow_stst_0', rank
! TRANSFERRING MEAN TO ALL NODES
do k=0,NZ+1
do j=0,NY+1
p_mean(k,j)=CPX(0,k,j)
CALL MPI_BCAST_COMPLEX(p_mean(k,j),1,1)
enddo
enddo
do k=0,NZV-1
do j=0,NY+1
CALL MPI_BCAST_COMPLEX(CR1X(0,k,j),1,1)
CALL MPI_BCAST_COMPLEX(CR2X(0,k,j),1,1)
CALL MPI_BCAST_COMPLEX(CR3X(0,k,j),1,1)
enddo
enddo
! Convert to physical space
CALL REAL_FOURIER_TRANS_U1 (.false.)
CALL REAL_FOURIER_TRANS_U2 (.false.)
CALL REAL_FOURIER_TRANS_U3 (.false.)
CALL REAL_FOURIER_TRANS_P (.false.)
! Get the turbulent kinetic energy at each level
do k=0,NZ+1
do j=0,NY+1
urms(k,j)=0.d0
vrms(k,j)=0.d0
wrms(k,j)=0.d0
do i=0,min(NXP,NXP_L)
urms(k,j)=urms(k,j)+(U1X(i,k,j)-dble(CR1X(0,k,j)))**2
vrms(k,j)=vrms(k,j)+(U2X(i,k,j)-dble(CR2X(0,k,j)))**2
wrms(k,j)=wrms(k,j)+(U3X(i,k,j)-dble(CR3X(0,k,j)))**2
end do
! urms(k,j)=dsqrt(urms(k,j)/(dble(NX)))
! vrms(k,j)=dsqrt(vrms(k,j)/(dble(NX)))
! wrms(k,j)=dsqrt(wrms(k,j)/(dble(NX)))
end do
end do
uv(:,:)=0.
uw(:,:)=0.
wv(:,:)=0.
pv(:,:)=0.
pu(:,:)=0.
! Compute the Reynolds stress and turbulent flux
do k=0,NZ
do j=0,NY
uv(k,j)=0.
uw(k,j)=0.
wv(k,j)=0.
pv(k,j)=0.
pu(k,j)=0.
do i=0,min(NXP,NXP_L)
uv(k,j)=uv(k,j)+(U1X(i,k,j)-dble(CR1X(0,k,j))) &
*0.5*((U2X(i,k,j+1)-dble(CR2X(0,k,j+1))) + &
(U2X(i,k,j)-dble(CR2X(0,k,j))) )
wv(k,j)=wv(k,j)+ 0.5*((U3X(i,k+1,j)-dble(CR3X(0,k+1,j))) + &
(U3X(i,k,j)-dble(CR3X(0,k,j))) )* &
0.5*((U2X(i,k,j+1)-dble(CR2X(0,k,j+1))) + &
(U2X(i,k,j)-dble(CR2X(0,k,j))) )
uw(k,j)=uw(k,j)+(U1X(i,k,j)-dble(CR1X(0,k,j))) &
*0.5*((U3X(i,k+1,j)-dble(CR3X(0,k+1,j))) + &
(U3X(i,k,j)-dble(CR3X(0,k,j))) )
pu(k,j)=pv(k,j)+0.5*((U3X(i,k+1,j)-dble(CR3X(0,k+1,j))) + &
(U3X(i,k,j)-dble(CR3X(0,k,j))) ) &
*(PX(i,k,j)-dble(p_mean(k,j)))
pv(k,j)=pv(k,j)+ 0.5*((U2X(i,k,j+1)-dble(CR2X(0,k,j+1))) + &
(U2X(i,k,j)-dble(CR2X(0,k,j))) ) &
*(PX(i,k,j)-dble(p_mean(k,j)))
end do
uv(k,j)=uv(k,j)/(float(NX))
uw(k,j)=uw(k,j)/(float(NX))
wv(k,j)=wv(k,j)/(float(NX))
pu(k,j)=pv(k,j)/(float(NX))
pv(k,j)=pv(k,j)/(float(NX))
end do
end do
!Compute the mean velocity gradient
do k=1,NZ
do j=1,NY
dudy(k,j)=dble(CR2X(0,k,j)-CR2X(0,k,j-1))/(GYF(j)-GYF(j-1))
dwdy(k,j)=dble(CR3X(0,k,j)-CR3X(0,k,j-1))/(GYF(j)-GYF(j-1))
end do
end do
do k=1,NZ
dudy(k,1)=dudy(k,2)
enddo
do k=1,NZ
do j=1,NY
dudz(k,j)=dble(CR2X(0,k,j)-CR2X(0,k-1,j))/(GZF(k)-GZF(k-1))
dwdz(k,j)=dble(CR3X(0,k,j)-CR3X(0,k-1,j))/(GZF(k)-GZF(k-1))
end do
enddo
do j=1,NY
dwdz(1,j)=dwdz(2,j)
dudz(1,j)=dudz(2,j)
end do
! Calculate the mean square shear
do k=1,NZ
do j=1,NY
shear(k,j)=0.d0
do i=0,min(NXP,NXP_L)
shear(k,j)=shear(k,j) &
+((U1X(i,k,j+1)-U1X(i,k,j-1))/(2.d0*DYF(j)))**2.d0 &
+((U3X(i,k,j+1)-U3X(i,k,j-1))/(2.d0*DYF(j)))**2.d0 &
+((U1X(i,k+1,j)-U1X(i,k-1,j))/(2.d0*DZF(k)))**2.d0 &
+((U3X(i,k+1,j)-U3X(i,k-1,j))/(2.d0*DZF(k)))**2.d0
end do
end do
shear(k,j)=shear(k,j)/dble(NX)
end do
!c call tkebudget_chan_1
!C Call netcdf
!c call NETCDF_WRITE
! Do over the number of passive scalars
do n=1,N_TH
! Save CTHX
do k=0,NZ+1
do i=0,NX2P
do j=0,NY+1
CRTHX(i,k,j,n)=CTHX(i,k,j,n)
CFTHX(i,k,j,n)=CTHX(i,k,j,n)
end do
end do
end do
do k=0,NZV-1
do j=0,NY+1
CALL MPI_BCAST_COMPLEX(CRTHX(0,k,j,N),1,1)
enddo
enddo
end do ! done with passive scalar do loop
! Convert to physical space
if (N_TH .gt. 0) then
CALL REAL_FOURIER_TRANS_TH (.false.)
endif
! Do over the number of passive scalars
do n=1,N_TH
do j=0,NY+1
do k=0,NZ+1
thrms(k,j,n)=0.
do i=0,min(NXP,NXP_L)
thrms(k,j,n)=thrms(k,j,n) + (abs(THX(i,k,j,n) &
-dble(CRTHX(0,k,j,n))))**2.
end do
end do
end do
! Compute the Reynolds stress and mean velocity gradient
do j=1,NY
do k=1,NZ
thv(k,j,n)=0.
thw(k,j,n)=0.
do i=0,min(NXP,NXP_L)
thv(k,j,n)=thv(k,j,n)+(THX(i,k,j,n)-dble(CRTHX(0,k,j,n))) &
*(0.5*(U2X(i,k,j)+U2X(i,k,j+1) ) &
- 0.5*dble(CR2X(0,k,j)+CR2X(0,k,j+1)) )
thw(k,j,n)=thw(k,j,n)+(THX(i,k,j,n)-dble(CRTHX(0,k,j,n))) &
*(0.5*(U3X(i,k+1,j)+U3X(i,k,j) ) &
- 0.5*dble(CR3X(0,k+1,j)+CR3X(0,k,j)) )
end do
thv(k,j,n)=thv(k,j,n)/(float(NX))
thw(k,j,n)=thw(k,j,n)/(float(NX))
end do
end do
! Get the y-derivative of the mean scalar at GYF points
do k=1,NZ
do j=1,NY
dthdy(k,j,n)=dble(CRTHX(0,k,j+1,n)-CRTHX(0,k,j-1,n))/(2.*DYF(j))
end do
end do
do k=1,NZ
do j=1,NY
dthdz(k,j,n)=dble(CRTHX(0,k+1,j,n)-CRTHX(0,k-1,j,n))/(2.*DZF(K))
end do
end do
UBULK= 0.0d0
area = 0.0d0
DO J=2,NY
DO K=2,NZ
area = area + DY(j)*DZ(k)
UBULK=UBULK+0.25*(dble(CRTHX(0,K,J,n))+dble(CRTHX(0,K-1,J,n)) + &
dble(CRTHX(0,K-1,J-1,n)) + dble(CRTHX(0,K,J-1,n)) )*DY(j)*DZ(K)
ENDDO
ENDDO
UBULK=UBULK/area
! Write out UBULK
If (rank .eq. 0) then
! Write out UBULK
write(*,*) 'THBULK: ',UBULK, 'Ri', RI_TAU(N)
write(99,*) 'THBULK: ',UBULK, 'Ri', RI_TAU(N)
endif
! End do over number of passive scalars, n
end do
! IF (XY_PLANE_comb)THEN
! CALL MPI_COMBINE_XY (U1X,U1_XY,NXP+1,NY+2)
! CALL MPI_COMBINE_XY (U2X,U2_XY,NXP+1,NY+2)
! CALL MPI_COMBINE_XY (U3X,U3_XY,NXP+1,NY+2)
! CALL MPI_COMBINE_XY (THX,TH_XY,NXP+1,NY+2)
! ENDDO
if (rank .eq. 0) then
open(66,file='plane_data_yz/time_bulk.txt',form='formatted', &
status='unknown', position='append' )
write(6,*) 'TIME STEP = ', TIME_STEP, ' TIME= ', TIME/60.0,' dt = ', DELTA_T
write(66,565) TIME/(60.0d0*60.0d0),DELTA_T,UBULK
close(66)
565 format(f12.5,2f13.8)
k = time_step/SAVE_STATS_INT
call plane_parav_plane_yz(k)
end if
IF (XY_PLANE_comb)THEN
k = time_step/SAVE_STATS_INT
call plane_parav_plane_xy(k)
ENDIF
IF (XY_PLANE_comb_2)THEN
k = time_step/SAVE_STATS_INT
call plane_parav_plane_xy_2(k)
ENDIF
IF (ZX_PLANE_comb) THEN
k=time_step/SAVE_STATS_INT
call plane_parav_plane_zx(k)
ENDIF
IF (ZX_PLANE_comb_2) THEN
k=time_step/SAVE_STATS_INT
call plane_parav_plane_zx_2(k)
ENDIF
IF (PLANE_3D) THEN
k=time_step/SAVE_STATS_INT
call plane_parav_3D(k)
ENDIF
! IF (XY_PLANE) THEN
!!!!!!!!!!!!SAVING SPAN-WISE PLANE DATA!!!!!!!!!!!!!!!!!!!!!!!!!!!
! i = int((NZ+1)/2) !(NZ+1)/2
! k = time_step/SAVE_STATS_INT
! call plane_XY_binary(k,i)
! ENDIF
! IF (ZX_PLANE) THEN
!!!!!!!!!!!!SAVING SPAN-WISE PLANE DATA!!!!!!!!!!!!!!!!!!!!!!!!!!!
! j = NY !int((NY+1)/2) !(NZ+1)/2
! k = time_step/SAVE_STATS_INT
! call plane_ZX_binary(k,j)
! ENDIF
! IF (ZX_PLANE_2) THEN
! j = NY-16 !int((NY+1)/2) !(NZ+1)/2
! k = time_step/SAVE_STATS_INT
! call plane_ZX_binary_2(k,j)
! ENDIF
IF (SAVE_3D) THEN
k = time_step/SAVE_STATS_INT
call plane_3D_binary(k)
ENDIF
if ( MEAN_BACKGROUND ) then
call mean_background_potential_duct
endif
! Convert back to Fourier space
if (N_TH .gt. 0) then
CALL REAL_FOURIER_TRANS_TH (.true.)
endif
! write(6,*) 'I am here 1', rank
! CALL MPI_COMBINE_STATS(urms,NZ+2,NY+2)
! write(6,*) 'I am here 2', rank
! CALL MPI_COMBINE_STATS(vrms,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(wrms,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(uv,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(uw,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(wv,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(pu,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(pv,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(shear,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(dudz,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(dudy,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(dwdz,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(dwdy,NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(omega_x,NZ+2,NY+2)
! Get the bulk rms value
if(rank .eq. 0) then
do k=0,NZ+1
do j=0,NY+1
urms(k,j)=dsqrt(urms(k,j)/(dble(NX)))
vrms(k,j)=dsqrt(vrms(k,j)/(dble(NX)))
wrms(k,j)=dsqrt(wrms(k,j)/(dble(NX)))
end do
end do
urms_b=0.d0
vrms_b=0.d0
wrms_b=0.d0
area = 0.d0
do k=1,NZ
do j=1,NY
area = area + DY(j)*DZ(k)
urms_b=urms_b + 0.25d0*(urms(k,j)+urms(k,j-1) &
+ urms(k-1,j)+urms(k-1,j-1))*DY(j)*DZ(k)
vrms_b=vrms_b + 0.25d0*(vrms(k,j)+vrms(k,j-1) &
+ vrms(k-1,j)+vrms(k-1,j-1))*DY(j)*DZ(k)
wrms_b=wrms_b + 0.25d0*(wrms(k,j)+wrms(k,j-1) &
+ wrms(k-1,j)+wrms(k-1,j-1))*DY(j)*DZ(k)
end do
enddo
urms_b=urms_b/(area)
vrms_b=vrms_b/(area)
wrms_b=wrms_b/(area)
! Write out the bulk rms velocity
write(6,*) '<U_rms>: ',urms_b
write(6,*) '<V_rms>: ',vrms_b, area
write(6,*) '<W_rms>: ',wrms_b
endif
Do n =1,N_th
! CALL MPI_COMBINE_STATS(thrms(0,0,n),NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(thv(0,0,n),NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(thw(0,0,n),NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(dthdz(0,0,n),NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(dthdy(0,0,n),NZ+2,NY+2)
! CALL MPI_COMBINE_STATS(Rig,NZ+2,NY+2)
if (rank .eq. 0) then
do j=0,NY+1
do k=0,NZ+1
thrms(k,j,n)=sqrt(thrms(k,j,n)/float(NX))
end do
end do
endif
enddo
IF (MEAN_KE_BUDGET) THEN
call mean_energy_budget_duct
ENDIF
!!!!!!!!!!!!!TKE BUDGET!!!!!!!!!!!!!!!!!!!!!
IF (TKE_BUDGET) THEN
call tke_budget_duct
ENDIF
!C Convert velocity back to Fourier space
! C Convert velocity back to Fourier space
CALL REAL_FOURIER_TRANS_U1 (.true.)
CALL REAL_FOURIER_TRANS_U2 (.true.)
CALL REAL_FOURIER_TRANS_U3 (.true.)
CALL REAL_FOURIER_TRANS_P (.true.)
CF1X = (0.d0,0.d0)
CF2X = (0.d0,0.d0)
CF3X = (0.d0,0.d0)
! Saving files for paraview
if (rank .eq. 0 ) then
write(6,*) 'done save_stats chan'
k = time_step/SAVE_STATS_INT
call plane_parav_vel(k)
endif
! Deallocating stat variables
! call deallocate_temps
! CCCCCCCCCCCCCCCCCCCCCCCCCC
if (rank .eq. 0 ) then
write(*,*) 'Deallocate all the tmp arrays'
endif
RETURN
END
subroutine tke_budget_duct
! NOte, it is important to only run this routine after complete R-K
! time advancement since F1 is overwritten which is needed between R-K steps
! This subroutine should be called in SAVE_STATS_CHAN after computing
! plane averaged statistics, with the velocity in physical space, and
! CRi containing the velocity in Fourier space
use ntypes
use Domain
use Grid
use Fft_var, only : CIKXP
use TIME_STEP_VAR
use run_variable
use mg_vari, only : INIT_FLAG
use variable_stat
use mpi_var
use les_chan_var
implicit none
integer i,j,k,n
! CHARACTER*28 file_tke
if (rank .eq. 0) then
write(6,*) 'Calculating tke budget', rank
endif
! tke_mean(:,:) = 0.0d0
tke_2_1(:,:) = 0.0d0
tke_2_2(:,:) = 0.0d0
tke_3(:,:) = 0.0d0
tke_4(:,:) = 0.0d0
IF (TIME_STEP .eq. 0) THEN
tke_1(:,:) = 0.0d0
ELSE
do j=0,NY
do k=0,NZ
! tke_mean defined at GY points
! tke_mean_old(k,j)=tke_mean(k,j)
tke_mean(k,j)=0.5d0*( urms(k,j)**2.d0 &
+ 0.25*(vrms(k,j) + vrms(k,j+1))**2.d0 &
+ 0.25*(wrms(k,j) + wrms(k+1,j))**2.d0 )
tke_1(k,j)=(tke_mean(k,j)-tke_mean_old(k,j))/(TIME-TIME_old)
end do
end do
ENDIF
! time_old=TIME
! U2*dtke/dy
do j=1,NY-1
do k=1,NZ-1
tke_2_1(k,j)=-dble(CR2X(0,K,J))*(tke_mean(k,j+1)-tke_mean(k,j-1))/(2.0d0*DYF(j))
end do
end do
! U3*dtke/dz
do j=1,NY-1
do k=1,NZ-1
tke_2_2(k,j)=-dble(CR3X(0,K,J))*(tke_mean(k+1,j)-tke_mean(k-1,j))/(2.0d0*DZF(k))
tke_2(k,j) = tke_2_1(k,j) + tke_2_2(k,j)
end do
end do
! Get the production at GYF and GZF points (cell center)
do j=1,NY
do k=1,NZ
! tke_3(k,j)= -uv(k,j)*dUdy(k,j)-uw(k,j)*dUdz(k,j)
! -vv(k,j)*dVdy(k,j)-vw(k,j)*dVdz(k,j)
! -wv(k,j)*dWdy(k,j)-ww(k,j)*dWdy(k,j)
tke_3(k,j)=-uv(k,j)*dble(CR1X(0,k,j+1)-CR1X(0,k,j-1))/(2.0d0*DYF(j)) &
-uw(k,j)*dble(CR1X(0,k+1,j)-CR1X(0,k-1,j))/(2.0d0*DZF(k)) &
-0.25d0*(vrms(k,j)+vrms(k,j+1))**2.0*dble(CR2X(0,k,j+1)-CR2X(0,k,j))/DYF(j) &
-wv(k,j)*0.50d0*dble(CR2X(0,k+1,j+1)+CR2X(0,k+1,j)-CR2X(0,k-1,j+1)-CR2X(0,k-1,j))/(2.0d0*DZF(k)) &
-wv(k,j)*0.50d0*dble(CR3X(0,k+1,j+1)+CR3X(0,k,j+1)-CR3X(0,k+1,j-1)-CR3X(0,k,j-1))/(2.0d0*DYF(j)) &
-0.25d0*(wrms(k,j)+wrms(k+1,j))**2.0*dble(CR3X(0,k+1,j)-CR3X(0,k,j))/DZF(k)
end do
end do
! viscous diffusion NU*d2tke/dxidxi
do j=1,NY-1
do k=1,NZ-1
tke_4(k,j)= NU*( ( (tke_mean(K+1,J) - tke_mean(K,J)) / DZ(K+1) &
-(tke_mean(K,J) - tke_mean(K-1,J)) / DZ(K)) /DZF(K) &
+ ( (tke_mean(K,J+1) - tke_mean(K,J)) / DY(J+1) &
-(tke_mean(K,J) - tke_mean(K,J-1)) / DY(J)) /DYF(J) )
end do
end do
do n=1,N_TH
do j=1,NY
do k=1,NZ
! tke_5(k,j,n)=-RI_TAU(n)*thv(k,j,n)
tke_5(k,j,n)= RI_TAU(N)*thv(k,j,n)
end do
end do
end do
! Construct the resolved turbulent transport terms
! Convert the pressure to physical space for computation of the
! pressure transport
! Get the mean of the pressure
! do j=0,NY+1
! do k=0,NZ+1
! p_mean(k,j)=dble(CF1X(0,k,j))
! end do
! end do
transport(:,:) = 0.0d0;
do j=1,NY+1
do k=0,NZ+1
transport(k,j)=0.d0
do i=0,min(NXP,NXP_L)
transport(k,j)=transport(k,j) &
! Vertical Pressure Transport term:
+0.50d0*(U2X(I,K,J) -dble(CR2X(0,k,j)) )*(PX(I,K,J)+PX(I,K,J-1)-p_mean(k,J)-p_mean(k,J-1))
end do
transport(k,j)=transport(k,j)/dble(NX)
end do
end do
CALL MPI_COMBINE_STATS(transport,NZ+2,NY+2)
do j=1,NY
do k=1,NZ
tke_6_1_1(k,j)=- (transport(k,j+1)-transport(k,j))/DYF(j)
end do
end do
do j=0,NY+1
do k=1,NZ+1
transport(k,j)=0.d0
do i=0,min(NXP,NXP_L)
transport(k,j)=transport(k,j) &
! Horizontal Pressure Transport term:
+ 0.50d0*(U3X(I,K,J)-dble(CR3X(0,k,j)))*(PX(I,K,J)+PX(I,K-1,J)-p_mean(k,J)-p_mean(k-1,J))
end do
transport(k,j)=transport(k,j)/dble(NX)
end do
end do
CALL MPI_COMBINE_STATS(transport,NZ+2,NY+2)
do j=1,NY
do k=1,NZ
tke_6_1_2(k,j)=-(transport(k+1,j)-transport(k,j))/DZF(k)
tke_6_1(k,j)=tke_6_1_1(k,j) + tke_6_1_2(k,j)
end do
end do
! Turbulent transport terms:
! d(0.5*u_i^2.0*v)dy
do j=1,NY+1
do k=1,NZ
transport(k,j)=0.d0
do i=0,min(NXP,NXP_L)
transport(k,j)=transport(k,j) &
! u1^2*u2
+ 0.5d0*0.250d0*(U1X(I,K,J)+U1X(I,K,J-1)-dble(CR1X(0,k,J)+CR1X(0,k,J-1)))**2.d0 &
*(U2X(I,K,J)- dble(CR2X(0,k,J)) ) &
! U2^3
+ 0.5d0*(U2X(I,K,J)- dble(CR2X(0,k,J)))**3.d0 &
! U3^2*U2
+ 0.5d0*0.06250d0*(U3X(I,K+1,J)+U3X(I,K+1,J-1)+U3X(I,K,J)+U3X(I,K,J-1) &
-dble(CR3X(0,k+1,J)+CR3X(0,k+1,J-1)+CR3X(0,k,J)+CR3X(0,k,J-1)))**2.d0 &
*(U2X(I,K,J)- dble(CR2X(0,k,J)))
end do
transport(k,j)=transport(k,j)/dble(NX)
end do
end do
CALL MPI_COMBINE_STATS(transport,NZ+2,NY+2)
! Now, the vertical derivative of the transport term:
do j=1,NY
do k=1,NZ
tke_6_2_1(k,j)=-(transport(k,j+1)-transport(k,j))/DYF(j)
end do
end do
! d(0.5*u_i^2.0*w)dz
do j=1,NY
do k=1,NZ+1
transport(k,j)=0.d0
do i=0,min(NXP,NXP_L)
transport(k,j)=transport(k,j) &
! U1^2*U3
+ 0.5d0*0.250d0*(U1X(I,K,J)+U1X(I,K-1,J)-dble(CR1X(0,K,J)+CR1X(0,K-1,J)))**2.d0 &
*(U3X(I,K,J)- dble(CR3X(0,K,J)) ) &
! U3^3
+ 0.5d0*(U3X(I,K,J)- dble(CR3X(0,K,J)))**3.d0 &
! U2^2.0*U3
+ 0.5d0*0.06250d0*(U2X(I,K,J+1)+U2X(I,K-1,J+1)+U2X(I,K,J)+U2X(I,K-1,J) &
-dble(CR2X(0,K,J+1)+CR2X(0,K-1,J+1)+CR2X(0,K,J)+CR2X(0,K-1,J)))**2.d0 &
*(U3X(I,K,J)- dble(CR3X(0,K,J)))
end do
transport(k,j)=transport(k,j)/dble(NX)
end do
end do
CALL MPI_COMBINE_STATS(transport,NZ+2,NY+2)
! Now, the horizontal derivative of the transport term:
do j=1,NY
do k=1,NZ
tke_6_2_2(k,j)=-(transport(k+1,j)-transport(k,j))/DZF(k)
tke_6_2(k,j) = tke_6_2_1(k,j) + tke_6_2_2(k,j)
end do
end do
! Compute the turbulent dissipation rate, epsilon=nu*<du_i/dx_j du_i/dx_j>
do j=0,NY+1
do k=0,NZ+1
epsilon(k,j)=0.d0
end do
end do
! Store du/dx in CS1
do j=1,NY
do k=1,NZ
do i=0,NX2P
CS1X(i,k,j)=CIKXP(i)*CF1X(i,k,j) ! WE HAVE ALSO STORED CU1 IN CF1X
! CS1(i,k,j)=CIKXP(i)*CR1X(i,k,j)
end do
end do
end do
! Convert to physical space
CALL MPI_TRANSPOSE_COMPLEX_X_TO_Z(CS1X,CS1Z)
cvarp(:,:,:)=(0.d0,0.d0)
DO I=0,NKX
cvarp(I,:,:)=CS1Z(I,:,:)
ENDDO
CALL FFT_X_TO_PHYSICAL_OP(cvarp,varp,0,NY+1,0,NZP)
DO I=0,NXM
S1Z(I,:,:)=varp(I,:,:)
ENDDO
S1Z(NXM+1:NXV-1,:,:)=0.0
CALL MPI_TRANSPOSE_REAL_Z_TO_X(S1Z,S1X)
!C call fft_x_to_physical(CS1,S1X,0,NY+1,0,NZ+1)
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
! epsilon(k,j)=epsilon(k,j)+(S1X(i,k,j)**2.0)
epsilon(k,j)=epsilon(k,j) + S1X(i,k,j)**2.0
end do
end do
end do
! Store dv/dx in CS1
do j=1,NY
do k=1,NZ
do i=0,NX2P
CS1X(i,k,j)=CIKXP(i)*CF2X(i,k,j) ! WE HAVE ALSO STORED CU2 IN CF2X
! CS1(i,k,j)=CIKXP(i)*CR2X(i,k,j)
end do
end do
end do
! Convert to physical space
CALL MPI_TRANSPOSE_COMPLEX_X_TO_Z(CS1X,CS1Z)
cvarp(:,:,:)=(0.d0,0.d0)
DO I=0,NKX
cvarp(I,:,:)=CS1Z(I,:,:)
ENDDO
CALL FFT_X_TO_PHYSICAL_OP(cvarp,varp,0,NY+1,0,NZP)
DO I=0,NXM
S1Z(I,:,:)=varp(I,:,:)
ENDDO
S1Z(NXM+1:NXV-1,:,:)=0.0
CALL MPI_TRANSPOSE_REAL_Z_TO_X(S1Z,S1X)
!C call fft_x_to_physical(CS1,S1X,0,NY+1,0,NZ+1)
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
epsilon(k,j)=epsilon(k,j)+(S1X(i,k,j)**2.0)
end do
end do
end do
! Store dw/dx in CS1
do j=1,NY
do k=1,NZ
do i=0,NX2P
CS1X(i,k,j)=CIKXP(i)*CF3X(i,k,j) ! WE HAVE ALSO STORED CU3 IN CF3X
end do
end do
end do
! Convert to physical space
CALL MPI_TRANSPOSE_COMPLEX_X_TO_Z(CS1X,CS1Z)
cvarp(:,:,:)=(0.d0,0.d0)
DO I=0,NKX
cvarp(I,:,:)=CS1Z(I,:,:)
ENDDO
CALL FFT_X_TO_PHYSICAL_OP(cvarp,varp,0,NY+1,0,NZP)
DO I=0,NXM
S1Z(I,:,:)=varp(I,:,:)
ENDDO
S1Z(NXM+1:NXV-1,:,:)=0.0
CALL MPI_TRANSPOSE_REAL_Z_TO_X(S1Z,S1X)
!C call fft_x_to_physical(CS1,S1X,0,NY+1,0,NZ+1)
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
epsilon(k,j)=epsilon(k,j)+ (S1X(i,k,j)**2.0)
end do
end do
end do
! Compute du/dy note remove mean
do j=0,NY+1
do k=0,NZ+1
do i=0,min(NXP,NXP_L)
S2X(i,k,j)= U1X(i,k,j)-dble(CR1X(0,k,j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
S1X(i,k,j) = (S2X(i,k,j+1)-S2X(i,k,j-1))/(2.0d0*DYF(j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
epsilon(k,j)=epsilon(k,j)+(S1X(i,k,j)**2.0)
end do
end do
end do
! Compute du/dz
do j=0,NY+1
do k=0,NZ+1
do i=0,min(NXP,NXP_L)
S2X(i,k,j)= U1X(i,k,j)-dble(CR1X(0,k,j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
S1X(i,k,j) = (S2X(i,k+1,j)-S2X(i,k-1,j))/(2.0d0*DZF(k))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
epsilon(k,j)=epsilon(k,j)+ (S1X(i,k,j)**2.0)
end do
end do
end do
! Compute dv/dy note remove mean
do j=0,NY+1
do k=0,NZ+1
do i=0,min(NXP,NXP_L)
S2X(i,k,j)= U2X(i,k,j)-dble(CR2X(0,k,j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
S1X(i,k,j) = (S2X(i,k,j+1)-S2X(i,k,j))/DYF(j)
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
epsilon(k,j)=epsilon(k,j)+(S1X(i,k,j)**2.0)
end do
end do
end do
! Compute dw/dy, note remove mean
do j=0,NY+1
do k=0,NZ+1
do i=0,min(NXP,NXP_L)
S2X(i,k,j)= U3X(i,k,j)-dble(CR3X(0,k,j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
S1X(i,k,j)=0.50d0*(S2X(i,k+1,j+1)+S2X(i,k,j+1)-S2X(i,k+1,j-1)-S2X(i,k,j-1))/(2.0d0*DYF(j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
epsilon(k,j)=epsilon(k,j)+(S1X(i,k,j)**2.0)
end do
end do
end do
! Compute dv/dz, note remove mean
do j=0,NY+1
do k=0,NZ+1
do i=0,min(NXP,NXP_L)
S2X(i,k,j)= U2X(i,k,j)-dble(CR2X(0,k,j))
end do
end do
end do
do j=1,NY
do k=1,NZ
do i=0,min(NXP,NXP_L)
S1X(i,k,j) = 0.50d0*(S2X(i,k+1,j+1)+S2X(i,k+1,j)-S2X(i,k-1,j+1)-S2X(i,k-1,j))/(2.0d0*DZF(k))
end do