-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduct.F90
2457 lines (2060 loc) · 71.5 KB
/
duct.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******************************************************************************|
!C duct.f, the duct-flow solvers for diablo. VERSION 0.9
!C These solvers were written by ? and ? (spring 2001).
!C******************************************************************************|
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
SUBROUTINE INIT_DUCT
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
!C Initialize any constants here
! INCLUDE 'header_duct'
use ntypes
use Domain
use Grid
use Fft_var
use TIME_STEP_VAR
use run_variable
use mg_vari, only : INIT_FLAG, bc
use mpi_var
implicit none
INTEGER J, K,N
PI=4.D0*ATAN(1.D0)
! At YMIN Location
!c write(*,*) 'U_BC_YMIN: ',U_BC_YMIN
IF (W_BC_YMIN.EQ.0) THEN
JSTART=2
ELSE IF (W_BC_YMIN.EQ.1) THEN
JSTART=1
ELSE IF (W_BC_YMIN.EQ.7) THEN
JSTART=1
ELSE
JSTART=2
END IF
! At ZMIN Location
!c write(*,*) 'U_BC_ZMIN: ',U_BC_ZMIN
IF (U_BC_ZMIN.EQ.0) THEN
ZSTART=2
ELSE IF (U_BC_ZMIN.EQ.1) THEN
ZSTART=1
ELSE
ZSTART=2
END IF
! Now, set the indexing for the scalar equations
! At YMIN Location
DO N=1,N_TH
IF (TH_BC_YMIN(N).EQ.0) THEN
JSTART_TH(N)=2
ELSE IF (TH_BC_YMIN(N).EQ.1) THEN
JSTART_TH(N)=1
ELSE IF (TH_BC_YMIN(N).EQ.7) THEN
JSTART_TH(N)=1
ELSE
JSTART_TH(N)=2
END IF
END DO
! At ZMIN Location
DO N=1,N_TH
IF (TH_BC_ZMIN(N).EQ.0) THEN
ZSTART_TH(N)=2
ELSE IF (TH_BC_ZMIN(N).EQ.1) THEN
ZSTART_TH(N)=1
ELSE
ZSTART_TH(N)=2
END IF
END DO
! At YMAX Location
!c write(*,*) 'U_BC_YMAX: ',U_BC_YMAX
IF (U_BC_YMAX.EQ.0) THEN
JEND=NY-1
ELSE IF (U_BC_YMAX.EQ.1) THEN
JEND=NY
ELSE
JEND=NY-1
END IF
! At ZMAX Location
!c write(*,*) 'U_BC_ZMAX: ',U_BC_ZMAX
IF (U_BC_ZMAX.EQ.0) THEN
ZEND=NZ-1
ELSE IF (U_BC_ZMAX.EQ.1) THEN
ZEND=NZ
ELSE
ZEND=NZ-1
END IF
! Set the upper and lower limits of timestepping of the scalar equations
! At YMAX Location
DO N=1,N_TH
IF(TH_BC_YMAX(N).EQ.0)THEN
JEND_TH(N)=NY-1
ELSE IF (TH_BC_YMAX(N).EQ.1) THEN
JEND_TH(N)=NY
ELSE IF (TH_BC_YMAX(N).EQ.5) THEN
JEND_TH(N)=NY
ELSE IF (TH_BC_YMAX(N).EQ.7) THEN
JEND_TH(N)=NY
ELSE
JEND_TH(N)=NY-1
END IF
END DO
! At ZMAX Location
DO N=1,N_TH
IF (TH_BC_ZMAX(N).EQ.0) THEN
ZEND_TH(N)=NZ-1
ELSE IF (TH_BC_ZMAX(N).EQ.1) THEN
ZEND_TH(N)=NZ
ELSE IF (TH_BC_ZMAX(N).EQ.5) THEN
ZEND_TH(N)=NZ
ELSE
ZEND_TH(N)=NZ-1
END IF
END DO
IF (rank .eq. 0 ) then
WRITE(6,*) '###################################################'
WRITE(6,*)'Boundary condition for U'
WRITE(6,*)'U_BC_YMIN',U_BC_YMIN,'U_BC_YMAX',U_BC_YMAX
WRITE(6,*)'U_BC_ZMIN',U_BC_ZMIN,'U_BC_ZMAX',U_BC_ZMAX
WRITE(6,*) '###################################################'
WRITE(6,*)'U_BC_YMIN_C1',U_BC_YMIN_C1,'U_BC_YMAX_C1',U_BC_YMAX_C1
WRITE(6,*)'U_BC_ZMIN_C1',U_BC_ZMIN_C1,'U_BC_ZMAX_C1',U_BC_ZMAX_C1
WRITE(6,*)'Boundary condition for W'
WRITE(6,*)'W_BC_YMIN',W_BC_YMIN,'W_BC_YMAX', W_BC_YMAX
WRITE(6,*)'W_BC_ZMIN',W_BC_ZMIN,'W_BC_ZMAX', W_BC_ZMAX
WRITE(6,*) '###################################################'
WRITE(6,*)'W_BC_YMIN_C1',W_BC_YMIN_C1,'W_BC_YMAX_C1',W_BC_YMAX_C1
WRITE(6,*)'W_BC_ZMIN_C1',W_BC_ZMIN_C1,'W_BC_ZMAX_C1',W_BC_ZMAX_C1
WRITE(6,*)'Boundary condition for V'
WRITE(6,*)'V_BC_YMIN',V_BC_YMIN,'V_BC_YMAX',V_BC_YMAX
WRITE(6,*)'V_BC_ZMIN',V_BC_ZMIN,'V_BC_ZMAX',V_BC_ZMAX
WRITE(6,*) '###################################################'
WRITE(6,*)'V_BC_YMIN_C1',V_BC_YMIN_C1,'V_BC_YMAX_C1',V_BC_YMAX_C1
WRITE(6,*)'V_BC_ZMIN_C1',V_BC_ZMIN_C1,'V_BC_ZMAX_C1',V_BC_ZMAX_C1
WRITE(6,*)'#####################################################'
WRITE(6,*) 'JSATRT', JSTART, 'JEND', JEND
WRITE(6,*) 'ZSATRT', ZSTART, 'ZEND', ZEND
WRITE(6,*)'####################################################'
DO N=1,N_TH
WRITE(6,*)'Boundary condition for TH'
WRITE(6,*)'TH_BC_YMIN',TH_BC_YMIN(N),'TH_BC_YMAX', TH_BC_YMAX(N)
WRITE(6,*)'TH_BC_ZMIN',TH_BC_ZMIN(N),'TH_BC_ZMAX', TH_BC_ZMAX(N)
WRITE(6,*) '###################################################'
WRITE(6,*)'TH_BC_YMIN_C1',TH_BC_YMIN_C1(N), &
'TH_BC_YMAX_C1',TH_BC_YMAX_C1(N)
WRITE(6,*)'TH_BC_ZMIN_C1',TH_BC_ZMIN_C1(N), &
'TH_BC_ZMAX_C1',TH_BC_ZMAX_C1(N)
WRITE(6,*)'#####################################################'
WRITE(6,*) 'JSATRT_TH', JSTART_TH(N), 'JEND_TH', JEND_TH(N)
WRITE(6,*) 'ZSATRT_TH', ZSTART_TH(N), 'ZEND_TH', ZEND_TH(N)
WRITE(6,*)'#####################################################'
ENDDO
ENDIF
RETURN
END
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
SUBROUTINE RK_DUCT_1
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
!C Main time-stepping algorithm for the duct-flow case.
!C INPUTS (in Fourier space): CUi, P, and (if k>1) CFi at (k-1) (for i=1,2,3)
!C OUTPUTS (in Fourier space): CUi, P, and (if k<3) CFi at (k)
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
RETURN
END
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
SUBROUTINE RK_DUCT_2
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
!C Alternative time-stepping algorithm for the duct-flow case.
!C INPUTS (in Fourier space): CUi, P, and (if k>1) CFi at (k-1) (for i=1,2,3)
!C OUTPUTS (in Fourier space): CUi, P, and (if k<3) CFi at (k)
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
RETURN
END
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
SUBROUTINE RK_DUCT_3
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
!C Alternative time-stepping algorithm for the duct-flow case with ADI.
!C INPUTS (in Fourier space): CUi, P, and (if k>1) CFi at (k-1) (for i=1,2,3)
!C OUTPUTS (in Fourier space): CUi, P, and (if k<3) CFi at (k)
!C----*|--.---------.---------.---------.---------.---------.---------.-|-------|
use ntypes
use Domain
use Grid
use Fft_var
use TIME_STEP_VAR
use run_variable
use ADI_var
use mg_vari, only : INIT_FLAG
use omp_lib
use les_chan_var
use mpi_var
implicit none
! INCLUDE 'header_duct'
INTEGER I,J,K,N
REAL(r8) TEMP1, TEMP2, TEMP3, TEMP4, TEMP5, UBULK, &
MEAN_U1,D
!C Communicate the information between ghost cells
!c CALL GHOST_CHAN_MPI
!C Define the constants that are used in the time-stepping
!C For reference, see Numerical Renaissance
TEMP1=NU * H_BAR(RK_STEP) / 2.0
TEMP2=H_BAR(RK_STEP) / 2.0
TEMP3=ZETA_BAR(RK_STEP) * H_BAR(RK_STEP)
TEMP4=H_BAR(RK_STEP)
TEMP5=BETA_BAR(RK_STEP) * H_BAR(RK_STEP)
!C First, we will compute the explicit RHS terms and store in Ri
!C Note, Momentum equation and hence the RHS is evaluated at the
!C corresponding velocity points.
CR1X(:,:,:) = (0.d0,0.d0)
CR2X(:,:,:) = (0.d0,0.d0)
CR3X(:,:,:) = (0.d0,0.d0)
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CR1X(I,K,J)=CU1X(I,K,J)
END DO
END DO
END DO
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CR2X(I,K,J)=CU2X(I,K,J)
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CR3X(I,K,J)=CU3X(I,K,J)
END DO
END DO
END DO
!C Add the R-K term from the rk-1 step
IF (RK_STEP .GT. 1) THEN
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CR1X(I,K,J)=CR1X(I,K,J)+TEMP3*CF1X(I,K,J)
END DO
END DO
END DO
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CR2X(I,K,J)=CR2X(I,K,J)+TEMP3*CF2X(I,K,J)
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CR3X(I,K,J)=CR3X(I,K,J)+TEMP3*CF3X(I,K,J)
END DO
END DO
END DO
END IF
!C Take the y-derivative of the pressure at GY points in Fourier space
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CS1X(I,K,J)=(CPX(I,K,J) - CPX(I,K,J-1)) / DY(J)
END DO
END DO
END DO
!C Take the z-derivative of the pressure at GZ points in Fourier space
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CS2X(I,K,J)=(CPX(I,K,J) - CPX(I,K-1,J)) / DZ(K)
END DO
END DO
END DO
!C Add the pressure gradient to the RHS as explicit Euler
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CR1X(I,K,J)=CR1X(I,K,J)-TEMP4*(CIKXP(I)*CPX(I,K,J))
END DO
END DO
END DO
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CR2X(I,K,J)=CR2X(I,K,J)-TEMP4*CS1X(I,K,J)
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CR3X(I,K,J)=CR3X(I,K,J)-TEMP4*CS2X(I,K,J)
END DO
END DO
END DO
!C Here, add the constant, forcing pressure gradient
!C There are several ways of doing this
IF (F_TYPE.EQ.1) THEN
!C Add forcing for a constant pressure gradient
DO J=JSTART,JEND
DO K=ZSTART,ZEND
CR1X(0,K,J)=CR1X(0,K,J)-TEMP4*PX0
END DO
END DO
ELSE IF (F_TYPE.EQ.0) THEN
!C Add the mean pressure gradient to keep Ubulk constant
!C This section needs to be parallelized
ELSE IF (F_TYPE.EQ.2) THEN
!C If oscillatory pressure gradient
DO J=JSTART,JEND
DO K=ZSTART,ZEND
CR1X(0,K,J)=CR1X(0,K,J)-TEMP4*(PX0 + &
AMP_OMEGA0*cos(OMEGA0*TIME))
END DO
END DO
ELSE IF (F_TYPE.EQ.4) THEN
!C If oscillatory pressure gradient
DO J=JSTART,JEND
DO K=ZSTART,ZEND
CR1X(0,K,J)=CR1X(0,K,J)-TEMP4*(PX0 &
+AMP_OMEGA0*cos(OMEGA0*TIME))* cos(ANG_BETA)
END DO
END DO
ELSE IF (F_TYPE .EQ. 5) THEN
!C End if forcing type
END IF
!C Now compute the term R-K term Ai
!C Compile terms of Ai in CFi which will be saved for next time step
!C First, store the horizontal viscous terms in CFi
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CF1X(I,K,J)=-NU * KX2P(I) * CU1X(I,K,J)
END DO
END DO
END DO
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CF2X(I,K,J)=-NU * KX2P(I) * CU2X(I,K,J)
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CF3X(I,K,J)=-NU * KX2P(I) * CU3X(I,K,J)
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CF1X(I,K,J)=CF1X(I,K,J) + (f_0 + beta_f*GZF(K))*0.50d0*(CU3X(I,K+1,J)+CU3X(I,K,J))
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CF3X(I,K,J)=CF3X(I,K,J) - (f_0 + beta_f*GZ(K))*0.50d0*(CU1X(I,K,J)+CU1X(I,K-1,J))
END DO
END DO
END DO
! Do for each scalar
DO N=1,N_TH
! If a scalar contributes to the denisty, RI_TAU is not equal to zero and
! add the buoyancy term as explicit R-K. Don't add the 0,0 mode which
! corresponds to a plane average. The plane averaged density balances
! the hydrostratic pressure component.
IF ((F_TYPE .EQ. 4).OR.(F_TYPE.EQ.5)) THEN
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
! Use second order interpolation
CF2X(I,K,J)=CF2X(I,K,J) - RI_TAU(N)*cos(ANG_BETA)* &
(CTHX(I,K,J,N)*DYF(J-1)+CTHX(I,K,J-1,N)*DYF(J))/(2.d0*DY(J))
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CF1X(I,K,J)=CF1X(I,K,J)-RI_TAU(N)*sin(ANG_BETA)*CTHX(I,K,J,N)
END DO
END DO
END DO
ELSE
IF (DEV_BACK_TH) THEN
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
! Use second order interpolation
CF2X(I,K,J)=CF2X(I,K,J)+RI_TAU(N)* &
(CTHX(I,K,J,N)*DYF(J-1)+CTHX(I,K,J-1,N)*DYF(J))/(2.d0*DY(J))
END DO
END DO
END DO
ELSE
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
! Use second order interpolation
CF2X(I,K,J)=CF2X(I,K,J)+RI_TAU(N)* &
(CTHX(I,K,J,N)*DYF(J-1)+CTHX(I,K,J-1,N)*DYF(J))/(2.d0*DY(J))
END DO
END DO
END DO
ENDIF
END IF
! Now, compute the RHS vector for the scalar equations
! Since TH is defined at horizontal velocity points, the
! scalar update equation will be very similar to the horizontal
! velocity update.
! We will store the RHS scalar terms in CRTH, RTH
! The k-1 term for the R-K stepping is saved in FTH, CFTH
CRTHX(:,:,:,N) = (0.d0,0.d0)
! First, build the RHS vector, use CRTH
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NX2P
CRTHX(I,K,J,N)=CTHX(I,K,J,N)
ENDDO
END DO
END DO
! Add term from k-2 step to free up CFTH variable
IF (RK_STEP .GT. 1) THEN
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NX2P
CRTHX(I,K,J,N)=CRTHX(I,K,J,N)+TEMP3*CFTHX(I,K,J,N)
END DO
END DO
END DO
END IF
! Now compute the explicit R-K term Ai
! Compile terms of Ai in CFi which will be saved for next time step
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NX2P
CFTHX(I,K,J,N)=-(NU/PR(N)) * KX2P(I) * CTHX(I,K,J,N)
END DO
END DO
END DO
!C Need to be forced by background temperature gradient
IF (DEV_BACK_TH) THEN
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NX2P
CFTHX(I,K,J,N)=CFTHX(I,K,J,N) - 0.5*(CU2X(I,K,J+1)+CU2X(I,K,J))* &
(THBAR(j+1,n)-THBAR(j-1,n))/(2.*DYF(j))
END DO
END DO
END DO
END IF
! End of loop for passive scalars (N_TH)
END DO
! IF (IBM) THEN
! CALL sink_momentum
! ENDIF
IF (LES.AND.((.NOT.CREATE_NEW_FLOW).OR.(TIME_STEP.GT.10))) THEN
!C If we have created new flow with random perturbations, wait for a
!C spinup before applying the subgrid model for stability purposes
!C In the process, Ui is converted to physical space
! write(*,*) 'calling les'
call les_chan
!C Add the subgrid scale scalar flux to the scalar equations
IF (N_TH .gt. 0) then
DO N=1,N_TH
! call les_chan_th(N)
ENDDO
ENDIF
! write(*,*) 'Done with les'
!C convert to physical space.
! CALL REAL_FOURIER_TRANS_U1 (.false.)
! CALL REAL_FOURIER_TRANS_U2 (.false.)
! CALL REAL_FOURIER_TRANS_U3 (.false.)
! Transform THETA to physical space for computation of nonlinear terms
! Here pass the first location in memory of the array for scalar n
IF (N_TH .gt. 0) then
CALL REAL_FOURIER_TRANS_TH (.false.)
ENDIF
ELSE
!C If the subgrid model hasn't been called, then it is necessary to
!C convert to physical space.
CALL REAL_FOURIER_TRANS_U1 (.false.)
CALL REAL_FOURIER_TRANS_U2 (.false.)
CALL REAL_FOURIER_TRANS_U3 (.false.)
! Transform THETA to physical space for computation of nonlinear terms
! Here pass the first location in memory of the array for scalar n
IF (N_TH .gt. 0) then
CALL REAL_FOURIER_TRANS_TH (.false.)
ENDIF
END IF
!c IF ( F_TYPE .EQ. 1 ) THEN
S1X(:,:,:) =0.d0
!C U1*U3
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NXP
S1X(I,K,J)=U3X(I,K,J)*((DZF(K)*U1X(I,K,J) &
+DZF(K-1)*U1X(I,K-1,J))/(2.*DZ(K)))
!C S1(I,K,J)=U1(I,K,J)*((DZ(K)*U3(I,K,J)
!C & +DZ(K+1)*U3(I,K+1,J))/(2.D0*DZF(K)))
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CF3X(I,K,J)=CF3X(I,K,J) - CIKXP(I) * CS1X(I,K,J)
END DO
END DO
END DO
!C U1*U1
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NXP
S1X(I,K,J)=U1X(I,K,J)*U1X(I,K,J)
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CF1X(I,K,J)=CF1X(I,K,J) - CIKXP(I) * CS1X(I,K,J)
END DO
END DO
END DO
!C U1*U2
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NXP
S1X(I,K,J)=((DYF(J)*U1X(I,K,J) &
+DYF(J-1)*U1X(I,K,J-1))/(2.*DY(J))) &
*U2X(I,K,J)
!c S1(I,K,J)=((DY(J+1)*U2(I,K,J+1)
!c & +DY(J)*U2(I,K,J))/(2.D0*DYF(J)))
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CF2X(I,K,J)=CF2X(I,K,J) - CIKXP(I) * CS1X(I,K,J)
END DO
END DO
END DO
! Add the vertical (y) derivative and spanwise (z) derivative term explicitly
!C d(U1*U2)/dY + d(U1*U3)/dZ
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NXP
S1X(I,K,J) = (U2X(I,K,J+1)*((U1X(I,K,J+1)*DYF(J+1)+U1X(I,K,J)* & !C d(U1*U2)/dY
DYF(J))/(2.d0*DY(J+1))) - U2X(I,K,J)*((U1X(I,K,J)*DYF(J)+ &
U1X(I,K,J-1)*DYF(J-1))/(2.d0*DY(J))))/DYF(J) &
+ & ! d(U1*U3)/dZ
(U3X(I,K+1,J)*((U1X(I,K+1,J)*DZF(K+1)+U1X(I,K,J) &
*DZF(K))/(2.D0*DZ(K+1)))-U3X(I,K,J)*((U1X(I,K,J) &
*DZF(K)+U1X(I,K-1,J)*DZF(K-1))/(2.D0*DZ(K)))) &
/DZF(K)
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CF1X(I,K,J)=CF1X(I,K,J) - CS1X(I,K,J)
END DO
END DO
END DO
! !C d(U1*U3)/dZ
!
! DO J=JSTART,JEND
! DO K=ZSTART,ZEND
! DO I=0,NXP
! !c S1(I,K,J)= ((U1(I,K+1,J)*U3(I,K+1,J) + U1(I,K,J)*U3(I,K+1,J)
! !c & - U1(I,K,J)*U3(I,K,J) - U1(I,K-1,J)*U3(I,K,J))/(2.d0*DZF(K)))
! S1X(I,K,J) = (U3X(I,K+1,J)*((U1X(I,K+1,J)*DZF(K+1)+U1X(I,K,J) &
! *DZF(K))/(2.D0*DZ(K+1)))-U3X(I,K,J)*((U1X(I,K,J) &
! *DZF(K)+U1X(I,K-1,J)*DZF(K-1))/(2.D0*DZ(K)))) &
! /DZF(K)
! END DO
! END DO
! END DO
!
! CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
! varp(:,:,:) = 0.d0
! DO I=0,NXM
! varp(I,:,:)=S1Z(I,:,:)
! ENDDO
! CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
! DO I=0,NKX
! CS1Z(I,:,:)=cvarp(I,:,:)
! ENDDO
! CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
! CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
!
! DO J=JSTART,JEND
! DO K=ZSTART,ZEND
! DO I=0,NX2P
! CF1X(I,K,J)=CF1X(I,K,J) - CS1X(I,K,J)
! END DO
! END DO
! END DO
! d(U2*U2)/dY + d(U2*U3)/dZ
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NXP
S1X(I,K,J)= & ! d(U2*U2)/dY
(0.25d0*(U2X(I,K,J)+U2X(I,K,J+1))**2.d0 &
-0.25d0*(U2X(I,K,J)+U2X(I,K,J-1))**2.d0)/DY(J) &
+ & ! d(U2*U3)/dZ
( (U2X(I,K+1,J)*DZF(K+1)+ U2X(I,K,J)*DZF(K))/(2.*DZ(K+1))* &
(U3X(I,K+1,J)*DYF(J)+U3X(I,K+1,J-1)*DYF(J-1))/(2.*DY(J)) &
- (U2X(I,K-1,J)*DZF(K-1) + U2X(I,K,J)*DZF(K))/(2.*DZ(K))* &
(U3X(I,K,J-1)*DYF(J-1)+U3X(I,K,J)*DYF(J))/(2.*DY(J)) ) &
/DZF(K)
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CF2X(I,K,J)=CF2X(I,K,J) - CS1X(I,K,J)
END DO
END DO
END DO
! !C d(U2*U3)/dZ
!
! DO J=2,NY
! DO K=ZSTART,ZEND
! DO I=0,NXP
!
! S1X(I,K,J)=( (U2X(I,K+1,J)*DZF(K+1)+ U2X(I,K,J)*DZF(K))/(2.*DZ(K+1))* &
! (U3X(I,K+1,J)*DYF(J)+U3X(I,K+1,J-1)*DYF(J-1))/(2.*DY(J)) &
! - (U2X(I,K-1,J)*DZF(K-1) + U2X(I,K,J)*DZF(K))/(2.*DZ(K))* &
! (U3X(I,K,J-1)*DYF(J-1)+U3X(I,K,J)*DYF(J))/(2.*DY(J)) ) &
! /DZF(K)
!
! END DO
! END DO
! END DO
!
!
!
!
! CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
! varp(:,:,:) = 0.d0
! DO I=0,NXM
! varp(I,:,:)=S1Z(I,:,:)
! ENDDO
! CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
! DO I=0,NKX
! CS1Z(I,:,:)=cvarp(I,:,:)
! ENDDO
! CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
! CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
!
! DO J=2,NY
! DO K=ZSTART,ZEND
! DO I=0,NX2P
! CF2X(I,K,J)=CF2X(I,K,J) - CS1X(I,K,J)
! END DO
! END DO
! END DO
! !CCC4
! d(U2*U3)/dY + d(U3*U3)/dZ
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NXP
S1X(I,K,J)=( ((DYF(J+1)*U3X(I,K,J+1) & ! d(U2*U3)/dY
+DYF(J)*U3X(I,K,J))/(2.*DY(J+1))) &
*((DZF(K)*U2X(I,K,J+1) &
+DZF(K-1)*U2X(I,K-1,J+1))/(2.*DZ(K))) &
-((DYF(J)*U3X(I,K,J) &
+DYF(J-1)*U3X(I,K,J-1))/(2.*DY(J))) &
*((DZF(K)*U2X(I,K,J) &
+DZF(K-1)*U2X(I,K-1,J))/(2.*DZ(K))) )/DYF(J) &
+ & ! d(U3*U3)/dZ
(0.25d0*(U3X(I,K,J)+U3X(I,K+1,J))**2.d0 &
-0.25d0*(U3X(I,K,J)+U3X(I,K-1,J))**2.d0)/DZ(K)
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CF3X(I,K,J)=CF3X(I,K,J) - CS1X(I,K,J)
END DO
END DO
END DO
!C d(U3*U3)/dZ
!
! DO J=JSTART,JEND
! DO K=2,NZ
! DO I=0,NXP
! S1X(I,K,J)= &
! (0.25d0*(U3X(I,K,J)+U3X(I,K+1,J))**2.d0 &
! -0.25d0*(U3X(I,K,J)+U3X(I,K-1,J))**2.d0)/DZ(K)
! END DO
! END DO
! END DO
!
! CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
! varp(:,:,:) = 0.d0
! DO I=0,NXM
! varp(I,:,:)=S1Z(I,:,:)
! ENDDO
! CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
! DO I=0,NKX
! CS1Z(I,:,:)=cvarp(I,:,:)
! ENDDO
! CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
! CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
!
! DO J=JSTART,JEND
! DO K=2,NZ
! DO I=0,NX2P
! CF3X(I,K,J)=CF3X(I,K,J) - CS1X(I,K,J)
! END DO
! END DO
! END DO
!c ENDIF
!C -- At this point, we are done computing the nonlinear terms --
!C Finally, Add CFi to CRi
DO J=JSTART,JEND
DO K=ZSTART,ZEND
DO I=0,NX2P
CR1X(I,K,J)=CR1X(I,K,J) + TEMP5 * CF1X(I,K,J)
END DO
END DO
END DO
DO J=2,NY
DO K=ZSTART,ZEND
DO I=0,NX2P
CR2X(I,K,J)=CR2X(I,K,J) + TEMP5 * CF2X(I,K,J)
END DO
END DO
END DO
DO J=JSTART,JEND
DO K=2,NZ
DO I=0,NX2P
CR3X(I,K,J)=CR3X(I,K,J) + TEMP5 * CF3X(I,K,J)
END DO
END DO
END DO
!C Convert RHS terms to physical space
!C made a change in FFT_X_TO_PHYSICAL(CR2,R2,0,NY+1,0,NZ+1)
!C
CALL REAL_FOURIER_TRANS_R1 (.false.)
CALL REAL_FOURIER_TRANS_R2 (.false.)
CALL REAL_FOURIER_TRANS_R3 (.false.)
!C Now, build the explicit RHS terms for the passive scalar(s)
DO N=1,N_TH
! Do for each scalar:
! Compute the nonlinear terms that are present in the explicit term A
! U1*TH
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NXP
S1X(I,K,J)=THX(I,K,J,N)*U1X(I,K,J)
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NX2P
CFTHX(I,K,J,N)=CFTHX(I,K,J,N) - CIKXP(I) * CS1X(I,K,J)
END DO
END DO
END DO
! d(U3*TH)/dz + d(U2TH)/dy
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NXP
S1X(I,K,J)=((THX(I,K+1,J,N)*U3X(I,K+1,J) + THX(I,K,J,N)*U3X(I,K+1,J) & ! d(U3*TH)/dz
-THX(I,K,J,N)*U3X(I,K,J)-THX(I,K-1,J,N)*U3X(I,K,J))/(2.d0*DZF(K))) &
+ & ! d(U2TH)/dy
((THX(I,K,J+1,N)*U2X(I,K,J+1) + THX(I,K,J,N)*U2X(I,K,J+1) &
-THX(I,K,J,N)*U2X(I,K,J)-THX(I,K,J-1,N)*U2X(I,K,J))/(2.d0*DYF(J)))
END DO
END DO
END DO
CALL MPI_TRANSPOSE_REAL_X_TO_Z(S1X,S1Z)
varp(:,:,:) = 0.d0
DO I=0,NXM
varp(I,:,:)=S1Z(I,:,:)
ENDDO
CALL FFT_X_TO_FOURIER_OP(varp,cvarp,0,NY+1,0,NZP)
DO I=0,NKX
CS1Z(I,:,:)=cvarp(I,:,:)
ENDDO
CS1Z(NKX+1:NX2V-1,:,:)=(0.0,0.0)
CALL MPI_TRANSPOSE_COMPLEX_Z_TO_X(CS1Z,CS1X)
DO J=JSTART_TH(N),JEND_TH(N)
DO K=ZSTART_TH(N),ZEND_TH(N)
DO I=0,NX2P
CFTHX(I,K,J,N)=CFTHX(I,K,J,N) - CS1X(I,K,J)
END DO
END DO
END DO
! ! U2*TH
! DO J=JSTART_TH(N),JEND_TH(N)
! DO K=ZSTART_TH(N),ZEND_TH(N)
! DO I=0,NXM