-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra_functions.f
executable file
·3137 lines (2866 loc) · 120 KB
/
extra_functions.f
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
! Part of Dflow3d a 3D Navier Stokes solver with variable density for
! simulations of near field dredge plume mixing
! Copyright (C) 2012 Lynyrd de Wit
! This program 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.
! This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
subroutine chkdt
USE nlist
implicit none
! include 'param.txt'
! include 'common.txt'
include 'mpif.h'
integer ierr
real dr2,dz2,df,df2,kcoeff,tmp1,tmp2,tmp3,Courant,dtmp,dtold
double precision Tadapt,Uav,Vav,localsum,globalsum,du,duu,wsettlingmax,wsettlingmin
real Wmaxx,tmp11,tmp12,tmp13,tmp4,tmp11global,tmp12global,tmp13global
real dtnew,tmp10,tmp10global,Umaxx,Vmaxx
double precision localsum_Vol_V,globalsum_Vol_V
! IF (nobst>0.and.bp(1)%forever.eq.0.and.time_np+dt.gt.bp(1)%t0.and.counter<10) THEN
! counter=counter+1
! Courant=0.1
! IF (rank.eq.0) THEN
! write(*,*),'Placement bedplume, CFL=0.1 for 10 time steps ',counter,dt
! ENDIF
! ELSE
Courant = CFL
! ENDIF
IF (oPRHO.eq.1) THEN
dt_old = 1.e18 !1st order estimate P^n+1=P^n
ELSE
dt_old=dt !2nd order estimate P^n+1=P^n+dt/dt_old*(P^n - P^n-1)
ENDIF
dtold=dt
IF (istep.le.10) THEN
dt = dt_ini
IF (n_dtavg>0) THEN
dt_series=dt_max
ENDIF
ELSE
dt = dt_max
ENDIF
IF (istep.le.1000) THEN
IF (Uoutflow.eq.2) Uoutflow=999 !first 1000 time steps don't use convective outflow
ELSE
IF (Uoutflow.eq.999) Uoutflow=2 !after 10 time steps use convective outflow
ENDIF
! write(*,*),'dt,rank voor',dt,rank
IF (CNdiffz.eq.1.or.CNdiffz.eq.2) THEN
dz2 = 1.e9*dz*dz ! no dt restriction for vertical diff with CN implicit scheme
ELSE
dz2 = dz * dz
ENDIF
IF (nfrac>0) THEN
wsettlingmax=MAXVAL(frac(1:nfrac)%ws) !positive downward
wsettlingmin=MIN(0.,MINVAL(frac(1:nfrac)%ws)) !find air rise velocity (otherwise zero)
ELSE
wsettlingmax=0.
wsettlingmin=0.
ENDIF
!!!!! IF (CNdiffz.eq.11) THEN !AB2-CN, use stability limits viscosity from Wesseling for AB2-CN + standard CFL advection
!!!!! do k=1,kmax
!!!!! do j=1,jmax
!!!!! do i=1,imax
!!!!! df = Rp(i)*(phiv(j)-phiv(j-1))
!!!!! df2 = df * df
!!!!! dr2 = dr(i) * dr(i)
!!!!! kcoeff = ekm(i,j,k) /rnew(i,j,k)
!!!!! Wmaxx = MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin),
!!!!! & abs(Wnew(i,j,k-1)-wsettlingmax),abs(Wnew(i,j,k-1)-wsettlingmin))
!!!!! Umaxx = MAX(abs(Unew(i,j,k)),abs(Unew(i-1,j,k)))
!!!!! Vmaxx = MAX(abs(Vnew(i,j,k)),abs(Vnew(i,j-1,k)))
!!!!!!!! tmp1 = ( Umaxx / dr(i) ) +
!!!!!!!! & ( Vmaxx / df ) +
!!!!!!!! & ( Wmaxx / dz )
!!!!! tmp2 = ( 1.0/dr2 + 1.0/df2 + 1.0/dz2 )
!!!!!! tmp3 = 1.0 / ( 0.001 * tmp2 * kcoeff + 3.*tmp1 + 1.e-12 ) !19-11-2021: included dt<1000*dx^2/(kcoeff) as additional time step restriction for CN-implicit as tests for very high viscosity and no flow gave unstable results; theoretically it is not needed for stability + factor 3 for CFL advection criterium as AB2 needs <0.3 and CFL user-input can be as high as 0.9
!!!!!
!!!!!!!! ! switched off courant advection criterium 17-1-2022 as Wesseling doesn't have this
!!!!!!!! tmp3 = Courant / ( 3.*tmp1 + 1.e-12 )
!!!!! tmp11 = Courant*1.333333*kcoeff/(Umaxx**2+Vmaxx**2+Wmaxx**2+1.e-12)
!!!!! tmp12 = Courant*0.25/(kcoeff*(1./dr2+1./df2+1./dz2))
!!!!! tmp13 = Courant*(3.*kcoeff)**0.333333*(Umaxx**4/dr2+Vmaxx**4/df2+Wmaxx**4/dz2+1.e-12)**-0.333333
!!!!!
!!!!!!!! dt = min( dt , tmp3 , max(tmp11,min(tmp12,tmp13)) )
!!!!! dt = min( dt , max(tmp11,min(tmp12,tmp13)) )
!!!!! enddo
!!!!! enddo
!!!!! enddo
!!!!! dtmp=dt
!!!!! call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
!!!!! ELSEIF (CNdiffz.eq.12) THEN !AB2-CN, use stability limits viscosity from Wesseling for AB2-CN + standard CFL advection + diffusion limit from Chang 1991
!!!!! do k=1,kmax
!!!!! do j=1,jmax
!!!!! do i=1,imax
!!!!! df = Rp(i)*(phiv(j)-phiv(j-1))
!!!!! df2 = df * df
!!!!! dr2 = dr(i) * dr(i)
!!!!! kcoeff = ekm(i,j,k) /rnew(i,j,k)
!!!!! Wmaxx = MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin),
!!!!! & abs(Wnew(i,j,k-1)-wsettlingmax),abs(Wnew(i,j,k-1)-wsettlingmin))
!!!!! Umaxx = MAX(abs(Unew(i,j,k)),abs(Unew(i-1,j,k)))
!!!!! Vmaxx = MAX(abs(Vnew(i,j,k)),abs(Vnew(i,j-1,k)))
!!!!!!!! tmp1 = ( Umaxx / dr(i) ) +
!!!!!!!! & ( Vmaxx / df ) +
!!!!!!!! & ( Wmaxx / dz )
!!!!! tmp2 = ( 1.0/dr2 + 1.0/df2 + 1.0/dz2 )
!!!!!!!! tmp3 = Courant / ( CNdiff_factor * 0.75* tmp2 * kcoeff + 3.*tmp1 + 1.e-12 ) !19-11-2021: included dt<0.75*dx^2/(kcoeff)/CNdiff_factor from Chang 1991 + factor 3 for CFL advection criterium as AB2 needs <0.3 and CFL user-input can be as high as 0.9
!!!!! tmp3 = Courant / ( CNdiff_factor * 0.75* tmp2 * kcoeff + 1.e-12 ) !19-11-2021: included dt<0.75*dx^2/(kcoeff)/CNdiff_factor from Chang 1991; 17-1-2022 removed advection tmp1 criterium
!!!!!! tmp3 = Courant / ( 3.*tmp1 + 1.e-12 )
!!!!! tmp11 = Courant*1.333333*kcoeff/(Umaxx**2+Vmaxx**2+Wmaxx**2+1.e-12)
!!!!! tmp12 = Courant*0.25/(kcoeff*(1./dr2+1./df2+1./dz2))
!!!!! tmp13 = Courant*(3.*kcoeff)**0.333333*(Umaxx**4/dr2+Vmaxx**4/df2+Wmaxx**4/dz2+1.e-12)**-0.333333
!!!!!
!!!!! dt = min( dt , tmp3 , max(tmp11,min(tmp12,tmp13)) )
!!!!! enddo
!!!!! enddo
!!!!! enddo
!!!!! dtmp=dt
!!!!! call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
IF (CNdiffz.eq.11) THEN !CN-diff 3D implicit; theoretically unconditionally stable
! advection CFL dt criterium and diffusion dt criterium relaxed by factor CNdiff_dtfactor
! when CNdiff_dtfactor is chosen very large then no dt limit for diffusion (theoretically unconditionally stable)
! don't use Wesseling AB2-CN dt limits, because analyses of Wesseling AB2-CN dt limits reveal some problems:
! * at low viscosity, high velocity Wesseling AB2-CN dt gives much lower dt as advection CFL; why, that is not logical because even explicit diffusion scheme would give higher dt in this range of conditions and implicit diffusion is more stable
! * at high viscosity, all velocities Wesseling AB2-CN dt gives much higher dt as advection CFL; --> this seems dangerous
! do k=1,kmax
! do j=1,jmax
! do i=1,imax
! df = Rp(i)*(phiv(j)-phiv(j-1))
! tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) +
! & ( abs(Vnew(i,j,k)) / df ) +
! & ( MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin)) / dz )
! tmp3 = 1.0 / ( tmp1 + 1.e-12 )
! tmp3 =Courant *tmp3
! dt = min( dt , tmp3 )
! !dtmp = dt
! enddo
! enddo
! enddo
! dtmp=dt
! call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
do k=1,kmax
do j=1,jmax
do i=1,imax
df = Rp(i)*(phiv(j)-phiv(j-1))
df2 = df * df
dr2 = dr(i) * dr(i)
kcoeff = ekm(i,j,k) /rnew(i,j,k)
tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) +
& ( abs(Vnew(i,j,k)) / df ) +
& ( MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin)) / dz )
! tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) !2D TVD tests show that taking MAX of 3 dirs is not sufficient for stable results without overshoot/undershoot
! tmp1 = MAX(tmp1,( abs(Vnew(i,j,k)) / df ))
! tmp1 = MAX(tmp1,( abs(Wnew(i,j,k)) / dz ))
tmp2 = ( 1.0/dr2 + 1.0/df2 + 1.0/dz2 )
tmp3 = 1.0 / ( 2./CNdiff_dtfactor * tmp2 * kcoeff + tmp1 + 1.e-12 ) !included dt<CNdiff_dtfactor*dx^2/(2*kcoeff) --> Courant is extra safety factor for diffusion dt
tmp3 =Courant *tmp3
dt = min( dt , tmp3 )
!dtmp = dt
enddo
enddo
enddo
dtmp=dt
call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
ELSEIF (CNdiffz.eq.12) THEN !dt based on advection CFL and diffusion criterium relaxed by factor CNdiff_factor
do k=1,kmax
do j=1,jmax
do i=1,imax
df = Rp(i)*(phiv(j)-phiv(j-1))
df2 = df * df
dr2 = dr(i) * dr(i)
kcoeff = ekm(i,j,k) /rnew(i,j,k)
tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) +
& ( abs(Vnew(i,j,k)) / df ) +
& ( MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin)) / dz )
! tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) !2D TVD tests show that taking MAX of 3 dirs is not sufficient for stable results without overshoot/undershoot
! tmp1 = MAX(tmp1,( abs(Vnew(i,j,k)) / df ))
! tmp1 = MAX(tmp1,( abs(Wnew(i,j,k)) / dz ))
tmp2 = ( 1.0/dr2 + 1.0/df2 + 1.0/dz2 )
tmp3 = 1.0 / ( CNdiff_factor * 0.75 * tmp2 * kcoeff + tmp1 + 1.e-12 ) !included dt<0.75*dx^2/(kcoeff)/CNdiff_factor from Chang 1991 --> Courant is extra safety factor for diffusion dt
tmp3 =Courant *tmp3
dt = min( dt , tmp3 )
!dtmp = dt
enddo
enddo
enddo
dtmp=dt
call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
ELSEIF (CNdiffz.eq.31) THEN !fully 3D CN-diff implicit; theoretically unconditionally stable
do k=1,kmax
do j=1,jmax
do i=1,imax
df = Rp(i)*(phiv(j)-phiv(j-1))
df2 = df * df
dr2 = dr(i) * dr(i)
kcoeff = ekm(i,j,k) /rnew(i,j,k)
tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) +
& ( abs(Vnew(i,j,k)) / df ) +
& ( MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin)) / dz )
! tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) !2D TVD tests show that taking MAX of 3 dirs is not sufficient for stable results without overshoot/undershoot
! tmp1 = MAX(tmp1,( abs(Vnew(i,j,k)) / df ))
! tmp1 = MAX(tmp1,( abs(Wnew(i,j,k)) / dz ))
tmp2 = ( 1.0/dr2 + 1.0/df2 + 1.0/dz2 )
tmp3 = 1.0 / ( 2./CNdiff_dtfactor * tmp2 * kcoeff + tmp1 + 1.e-12 ) !included dt<CNdiff_dtfactor*dx^2/(2*kcoeff) --> Courant is extra safety factor for diffusion dt
tmp3 =Courant *tmp3
dt = min( dt , tmp3 )
enddo
enddo
enddo
dtmp=dt
call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
ELSE
do k=1,kmax
do j=1,jmax
do i=1,imax
df = Rp(i)*(phiv(j)-phiv(j-1))
df2 = df * df
dr2 = dr(i) * dr(i)
kcoeff = ekm(i,j,k) /rnew(i,j,k)
tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) +
& ( abs(Vnew(i,j,k)) / df ) +
& ( MAX(abs(Wnew(i,j,k)-wsettlingmax),abs(Wnew(i,j,k)-wsettlingmin)) / dz )
! tmp1 = ( abs(Unew(i,j,k)) / ( Rp(i+1)-Rp(i) ) ) !2D TVD tests show that taking MAX of 3 dirs is not sufficient for stable results without overshoot/undershoot
! tmp1 = MAX(tmp1,( abs(Vnew(i,j,k)) / df ))
! tmp1 = MAX(tmp1,( abs(Wnew(i,j,k)) / dz ))
tmp2 = ( 1.0/dr2 + 1.0/df2 + 1.0/dz2 )
tmp3 = 1.0 / ( 2.0 * tmp2 * kcoeff + tmp1 + 1.e-12 ) !19-11-2021: adjusted with factor 2 on tmp2, because stability explicit diffusion operator is dt<dx^2/(2*kcoeff)
tmp3 =Courant *tmp3
dt = min( dt , tmp3 )
!dtmp = dt
enddo
enddo
enddo
dtmp=dt
call mpi_allreduce(dtmp,dt,1,mpi_double_precision,mpi_min,mpi_comm_world,ierr)
ENDIF
IF (n_dtavg>0) THEN
tel_dt=tel_dt+1
!IF (tel_dt>100) tel_dt=1
!dt_series(tel_dt)=dt
!IF (dt.lt.dt_factor_avg * SUM(dt_series)/100.) THEN
! dt_series=dt/dt_factor_avg
!ENDIF
!dt = MIN(dt,dt_factor_avg * SUM(dt_series)/100.) !use minimum of avg_dt * factor or instantaneous dt
IF (tel_dt>n_dtavg) tel_dt=1
dt_series(tel_dt)=dt
dtnew = MINVAL(dt_series)
IF (dtnew/dtold>1.01) THEN !dtnew is growing
dt=MIN(0.5*(dtnew+dtold),dtold*1.1,dt) !dampen growth, but should always obey timestep restrictions present time step
dt_series(tel_dt)=dt
ELSE
dt=dtnew
ENDIF
ENDIF
dt=MIN(dt,dtold*1.1) !change in time step never more than +10% in one timestep for extra stability
if (isnan(dt).or.dt<1.e-12) then
call output_nc('flow3D_',999999999,time_np)
rnew=rold
cnew=cold
unew=uold
vnew=vold
wnew=wold
call output_nc('flow3D_',999999998,time_np) !output file with rcuvw from previous timestep (hopefully not crashed)
endif
if (isnan(dt)) stop 'ERROR, QUITING TUDFLOW3D: "dt" is a NaN'
if (dt<1.e-12) stop 'ERROR, QUITING TUDFLOW3D: "dt" is smaller than 1e-12'
if (time_int.eq.'AB2'.or.time_int.eq.'AB3') then
if (dt<dt_max) then
stop 'ERROR, QUITING TUDFLOW3D: "dt" is adjusted in AB2 or AB3, sign of instability try again with smaller dt'
endif
endif
!! if AB2 or AB3 timestep must be fixed, if unfortunately initially a too large timestep has been chosen
!! then a 25% reduction must lead to stable simulation from this point on (one change in timestep can be
!! stable, but subsequent changes in timestep are not stable for AB2 or AB3)
! if (time_int.eq.'AB2'.or.time_int.eq.'AB3') then
! if (dt<dt_max) then
! WRITE(*,'(a,a,a,f12.9,a,f12.9,a)') ' WARNING: Using ',time_int,' with dt too large, dt changed from : '
! & ,dt_max,' s, to : ',0.75*dt_max,' s'
! dt_max=0.75*dt_max
! dt=dt_max
! endif
! endif
if (periodicx.eq.1.and.ABS(dpdx).lt.1.e-12) THEN ! determination correct dpdx and dpdy based on wanted u or v
if (surf_layer>1.e-12) then !two different driving forces in the vertical are used
localsum=0.
localsum_Vol_V=0.
do k=1,ksurf_bc !kmax
do j=1,jmax
do i=1,imax
localsum=localsum+Unew(i,j,k)*(Rp(i+1)-Rp(i))*(phiv(j)-phiv(j-1))*Ru(i)*dz*fc_global(i,j+jmax*rank,k)
localsum_Vol_V=localsum_Vol_V+Vol_V(i,j+rank*jmax)*fc_global(i,j+jmax*rank,k)
enddo
enddo
enddo
call mpi_allreduce(localsum,globalsum,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(localsum_Vol_V,globalsum_Vol_V,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
Uav = globalsum/(globalsum_Vol_V+1.e-12)
if (istep>1) THEN
du = Uav-Uavold
else
du = 0.
endif
Uavold = Uav
! IF ((MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2)>1.e-18) THEN
! Tadapt = 100.*depth/sqrt(MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2+1.e-18)
! ELSE
Tadapt = 1000.*dt
! ENDIF
duu = Tadapt*du/dt !30 seconds worked
Uav = Uav + duu
dpdx1 = dpdx1 + ABS(U_b-Uav)*(U_b-Uav)/depth*dt/Tadapt
!dpdx1 = dpdx1 + MIN(ABS(ABS(U_b)*(U_b-Uav)/depth*dt/Tadapt),0.1*ABS(dpdx1))*SIGN(1.,ABS(U_b)*(U_b-Uav)/depth*dt/Tadapt)
do i=1,imax
do j=1,jmax
do k=1,ksurf_bc
Ppropx(i,j,k) = dpdx1*rhU(i,j,k)*fc_global(i,j+jmax*rank,k) !rnew !with variable density important to use rnew and not rho_b
enddo
enddo
enddo
! Ppropx(:,:,1:ksurf_bc) = dpdx1*rhU(:,:,1:ksurf_bc) !rnew !with variable density important to use rnew and not rho_b
Uav = Uav - duu
if (rank.eq.0) then
if (mod(istep,10) .eq.0) then
write(*,'(a,e11.4,a,f6.2,a,f6.2,a)') ' # bottom-layer dpdx: ',dpdx1,' Pa/m/(kg/m3); Uav: ',Uav,'; U_b:',U_b,' m/s #'
endif
endif
localsum=0.
localsum_Vol_V=0.
do k=ksurf_bc+1,kmax !kmax
do j=1,jmax
do i=1,imax
localsum=localsum+Unew(i,j,k)*(Rp(i+1)-Rp(i))*(phiv(j)-phiv(j-1))*Ru(i)*dz*fc_global(i,j+jmax*rank,k)
localsum_Vol_V=localsum_Vol_V+Vol_V(i,j+rank*jmax)*fc_global(i,j+jmax*rank,k)
enddo
enddo
enddo
call mpi_allreduce(localsum,globalsum,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(localsum_Vol_V,globalsum_Vol_V,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
Uav = globalsum/(globalsum_Vol_V+1.e-12)
if (istep>1) THEN
du = Uav-U3avold
else
du = 0.
endif
U3avold = Uav
! IF ((MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2)>1.e-18) THEN
! Tadapt = 100.*depth/sqrt(MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2+1.e-18)
! ELSE
Tadapt = 1000.*dt
! ENDIF
duu = Tadapt*du/dt !30 seconds worked
Uav = Uav + duu
dpdx3 = dpdx3 + ABS(U_b3-Uav)*(U_b3-Uav)/depth*dt/Tadapt
!dpdx1 = dpdx1 + MIN(ABS(ABS(U_b)*(U_b-Uav)/depth*dt/Tadapt),0.1*ABS(dpdx1))*SIGN(1.,ABS(U_b)*(U_b-Uav)/depth*dt/Tadapt)
do i=1,imax
do j=1,jmax
do k=ksurf_bc+1,kmax
Ppropx(i,j,k) = dpdx3*rhU(i,j,k)*fc_global(i,j+jmax*rank,k) !rnew !with variable density important to use rnew and not rho_b
enddo
enddo
enddo
! Ppropx(:,:,ksurf_bc+1:kmax) = dpdx3*rhU(:,:,ksurf_bc+1:kmax) !rnew !with variable density important to use rnew and not rho_b
Uav = Uav - duu
if (rank.eq.0) then
if (mod(istep,10) .eq.0) then
write(*,'(a,e11.4,a,f6.2,a,f6.2,a)') ' # top-layer dpdx: ',dpdx3,' Pa/m/(kg/m3); Uav: ',Uav,'; U_b3:',U_b3,' m/s #'
endif
endif
else ! one driving force over whole vertical
localsum=0.
localsum_Vol_V=0.
do k=1,kmax
do j=1,jmax
do i=1,imax
localsum=localsum+Unew(i,j,k)*(Rp(i+1)-Rp(i))*(phiv(j)-phiv(j-1))*Ru(i)*dz*fc_global(i,j+jmax*rank,k)
localsum_Vol_V=localsum_Vol_V+Vol_V(i,j+rank*jmax)*fc_global(i,j+jmax*rank,k)
enddo
enddo
enddo
call mpi_allreduce(localsum,globalsum,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(localsum_Vol_V,globalsum_Vol_V,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
Uav = globalsum/(globalsum_Vol_V+1.e-12)
if (istep>1) THEN
du = Uav-Uavold
else
du = 0.
endif
Uavold = Uav
! IF ((U_b**2+V_b**2)>1.e-18) THEN
! Tadapt = 100.*depth/sqrt(U_b**2+V_b**2+1.e-18)
! ELSE
Tadapt = 1000.*dt
! ENDIF
duu = Tadapt*du/dt !30 seconds worked
Uav = Uav + duu
dpdx1 = dpdx1 + ABS(U_b-Uav)*(U_b-Uav)/depth*dt/Tadapt
do i=1,imax
do j=1,jmax
do k=1,kmax
Ppropx(i,j,k) = dpdx1*rhU(i,j,k)*fc_global(i,j+jmax*rank,k) !rnew !with variable density important to use rnew and not rho_b
enddo
enddo
enddo
! Ppropx = dpdx1*rhU !rnew !with variable density important to use rnew and not rho_b
Uav = Uav - duu
if (rank.eq.0) then
if (mod(istep,10) .eq.0) then
write(*,'(a,e11.4,a,f6.2,a,f6.2,a)') ' # dpdx: ',dpdx1,' Pa/m/(kg/m3); Uav: ',Uav,'; U_b:',U_b,' m/s #'
!write(*,*),du,duu,ABS(U_b)*(U_b-(Uav+duu))/depth*dt/Tadapt
endif
endif
endif
endif
if (periodicy.eq.1.and.ABS(dpdy).lt.1.e-12) THEN ! test determination correct dpdx and dpdy based on wanted u or v
if (surf_layer>1.e-12) then !two different driving forces in the vertical are used
localsum=0.
localsum_Vol_V=0.
do k=1,ksurf_bc !kmax
do j=1,jmax
do i=1,imax
localsum=localsum+Vnew(i,j,k)*(Ru(i)-Ru(i-1))*(phip(j+1)-phip(j))*Rp(i)*dz*fc_global(i,j+jmax*rank,k)
localsum_Vol_V=localsum_Vol_V+Vol_V(i,j+rank*jmax)*fc_global(i,j+jmax*rank,k)
enddo
enddo
enddo
call mpi_allreduce(localsum,globalsum,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(localsum_Vol_V,globalsum_Vol_V,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
Vav = globalsum/(globalsum_Vol_V+1.e-12)
if (istep>1) THEN
du = Vav-Vavold
else
du = 0.
endif
Vavold = Vav
! IF ((MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2)>1.e-18) THEN
! Tadapt = 100.*depth/sqrt(MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2+1.e-18)
! ELSE
Tadapt = 1000.*dt
! ENDIF
duu = Tadapt*du/dt !30 seconds worked
Vav = Vav + duu
dpdy1 = dpdy1 + ABS(V_b-Vav)*(V_b-Vav)/depth*dt/Tadapt
do i=1,imax
do j=1,jmax
do k=1,ksurf_bc
Ppropy(i,j,k) = dpdy1*rhV(i,j,k)*fc_global(i,j+jmax*rank,k) !rnew !with variable density important to use rnew and not rho_b
enddo
enddo
enddo
!Ppropy(:,:,1:ksurf_bc) = dpdy1*rhV(:,:,1:ksurf_bc) !rnew !with variable density important to use rnew and not rho_b
Vav = Vav - duu
if (rank.eq.0) then
if (mod(istep,10) .eq.0) then
write(*,'(a,e11.4,a,f6.2,a,f6.2,a)') ' # bottom-layer dpdy: ',dpdy1,' Pa/m/(kg/m3); Vav: ',Vav,'; V_b:',V_b,' m/s #'
endif
endif
localsum=0.
localsum_Vol_V=0.
do k=ksurf_bc+1,kmax !kmax
do j=1,jmax
do i=1,imax
localsum=localsum+Vnew(i,j,k)*(Ru(i)-Ru(i-1))*(phip(j+1)-phip(j))*Rp(i)*dz*fc_global(i,j+jmax*rank,k)
localsum_Vol_V=localsum_Vol_V+Vol_V(i,j+rank*jmax)*fc_global(i,j+jmax*rank,k)
enddo
enddo
enddo
call mpi_allreduce(localsum,globalsum,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(localsum_Vol_V,globalsum_Vol_V,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
Vav = globalsum/(globalsum_Vol_V+1.e-12)
if (istep>1) THEN
du = Vav-V3avold
else
du = 0.
endif
V3avold = Vav
! IF ((MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2)>1.e-18) THEN
! Tadapt = 100.*depth/sqrt(MAX(U_b,U_b3)**2+MAX(V_b,V_b3)**2+1.e-18)
! ELSE
Tadapt = 1000.*dt
! ENDIF
duu = Tadapt*du/dt !30 seconds worked
Vav = Vav + duu
dpdy3 = dpdy3 + ABS(V_b3-Vav)*(V_b3-Vav)/depth*dt/Tadapt
do i=1,imax
do j=1,jmax
do k=ksurf_bc+1,kmax
Ppropy(i,j,k) = dpdy3*rhV(i,j,k)*fc_global(i,j+jmax*rank,k) !rnew !with variable density important to use rnew and not rho_b
enddo
enddo
enddo
!Ppropy(:,:,ksurf_bc+1:kmax) = dpdy3*rhV(:,:,ksurf_bc+1:kmax) !rnew !with variable density important to use rnew and not rho_b
Vav = Vav - duu
if (rank.eq.0) then
if (mod(istep,10) .eq.0) then
write(*,'(a,e11.4,a,f6.2,a,f6.2,a)') ' # top-layer dpdy: ',dpdy3,' Pa/m/(kg/m3); Vav: ',Vav,'; V_b3:',V_b3,' m/s #'
endif
endif
else ! one driving force over whole vertical
localsum=0.
localsum_Vol_V=0.
do k=1,kmax
do j=1,jmax
do i=1,imax
localsum=localsum+Vnew(i,j,k)*(Ru(i)-Ru(i-1))*(phip(j+1)-phip(j))*Rp(i)*dz*fc_global(i,j+jmax*rank,k)
localsum_Vol_V=localsum_Vol_V+Vol_V(i,j+rank*jmax)*fc_global(i,j+jmax*rank,k)
enddo
enddo
enddo
call mpi_allreduce(localsum,globalsum,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(localsum_Vol_V,globalsum_Vol_V,1,mpi_double_precision,mpi_sum,mpi_comm_world,ierr)
Vav = globalsum/(globalsum_Vol_V+1.e-12)
if (istep>1) THEN
du = Vav-Vavold
else
du = 0.
endif
Vavold = Vav
! IF ((U_b**2+V_b**2)>1.e-18) THEN
! Tadapt = 100.*depth/sqrt(U_b**2+V_b**2+1.e-18)
! ELSE
Tadapt = 1000.*dt
! ENDIF
duu = Tadapt*du/dt !30 seconds worked
Vav = Vav + duu
dpdy1 = dpdy1 + ABS(V_b-Vav)*(V_b-Vav)/depth*dt/Tadapt
do i=1,imax
do j=1,jmax
do k=1,kmax
Ppropy(i,j,k) = dpdy1*rhV(i,j,k)*fc_global(i,j+jmax*rank,k) !rnew !with variable density important to use rnew and not rho_b
enddo
enddo
enddo
!Ppropy = dpdy1*rhV !rnew !with variable density important to use rnew and not rho_b
Vav = Vav - duu
if (rank.eq.0) then
if (mod(istep,10) .eq.0) then
write(*,'(a,e11.4,a,f6.2,a,f6.2,a)') ' # dpdy: ',dpdy1,' Pa/m/(kg/m3); Vav: ',Vav,'; V_b:',V_b,' m/s #'
endif
endif
endif
endif
end
subroutine chkdiv
USE nlist
implicit none
! include 'param.txt'
! include 'common.txt'
include 'mpif.h'
real div1,divmax1,divbar1,divmax_tot1,divbar_tot1,rhoip,rhoim,rhojp,rhojm,rhokp,rhokm
real div2,divmax2,divbar2,divmax_tot2,divbar_tot2
real div3,divmax3,divbar3,divmax_tot3,divbar_tot3,rrri,rrrj,rrrk,rrrim,rrrjm,rrrkm
integer ierr,n,inout,n2
real xTSHD(1:4),yTSHD(1:4),phi,xx,yy,kg_sus,kg_sed,kg_sus_tot,kg_sed_tot
real wnew3(0:i1,0:j1,0:k1),sum_c_ws,ctot,ws(nfrac)
divbar1 = 0.0
divmax1 = 0.0
divbar2 = 0.0
divmax2 = 0.0
divbar3 = 0.0
divmax3 = 0.0
wnew3=wnew
IF (slipvel.eq.2) THEN
do j=1,jmax
do i=1,imax
do k=kbed(i,j)+1,kmax !1,kmax ! all cells below kbed no correction needed as dwdt=dwdt
sum_c_ws=0.
do n=1,nfrac
ws(n)=-frac(n)%ws !ws defined positive downwards
sum_c_ws=sum_c_ws+ws(n)*(cW(n,i,j,k)*frac(n)%rho/rhW(i,j,k)-cW(n,i,j,k))
enddo
wnew3(i,j,k)=wnew3(i,j,k)-sum_c_ws !go from mixture velocity (centre of mass velocity) to velocity of volume centre
enddo
enddo
enddo
ELSE
do j=1,jmax
do i=1,imax
do k=kbed(i,j)+1,kmax !1,kmax ! all cells below kbed no correction needed as dwdt=dwdt
sum_c_ws=0.
ctot=0.
do n=1,nfrac
ctot=cW(n,i,j,k)*frac(n)%dfloc/frac(n)%dpart*0.5*(rhocorr_air_z(n,k)+rhocorr_air_z(n,k+1))+ctot
enddo
ctot=MIN(ctot,1.) ! limit on 1, see also Winterwerp 1999 p.46, because Cfloc can be >1
do n=1,nfrac
ws(n)=-frac(n)%ws*(1.-ctot)**(frac(n)%n) !ws defined positive downwards
sum_c_ws=sum_c_ws+ws(n)*(cW(n,i,j,k)*frac(n)%rho/rhW(i,j,k)-cW(n,i,j,k))
enddo
wnew3(i,j,k)=wnew3(i,j,k)-sum_c_ws !go from mixture velocity (centre of mass velocity) to velocity of volume centre
enddo
enddo
enddo
ENDIF
do k=2,kmax-1
do j=2,jmax-1
do i=2,imax-1
div1 =
1 ( Ru(i)*dUdt(i,j,k) - Ru(i-1)*dUdt(i-1,j,k) ) / ( Rp(i)*dr(i) )
+ +
2 ( dVdt(i,j,k) - dVdt(i,j-1,k) ) / ( Rp(i)*(phiv(j)-phiv(j-1)) )
+ +
3 ( dWdt(i,j,k) - dWdt(i,j,k-1) ) / ( dz )
! + + (3.*drdt(i,j,k)-4.*rnew(i,j,k)+rold(i,j,k))/(2.*dt)
+ + (3.*drdt(i,j,k)-4.*rnew(i,j,k)+rold(i,j,k))/((3.*time_np-4.*time_n+time_nm))
div2= ( Ru(i)*Unew(i,j,k) - Ru(i-1)*Unew(i-1,j,k) ) / ( Rp(i)*dr(i) )
+ +
2 ( Vnew(i,j,k) - Vnew(i,j-1,k) ) / ( Rp(i)*(phiv(j)-phiv(j-1)) )
+ +
3 ( Wnew(i,j,k) - Wnew(i,j,k-1) ) / ( dz )
div3= ( Ru(i)*Unew(i,j,k) - Ru(i-1)*Unew(i-1,j,k) ) / ( Rp(i)*dr(i) )
+ +
2 ( Vnew(i,j,k) - Vnew(i,j-1,k) ) / ( Rp(i)*(phiv(j)-phiv(j-1)) )
+ +
3 ( Wnew3(i,j,k) - Wnew3(i,j,k-1) ) / ( dz )
DO n2=1,nbedplume !correct div2 for adding or subtracting volume:
IF ((bp(n2)%forever.eq.1.and.time_np.gt.bp(n2)%t0.and.time_np.lt.bp(n2)%t_end.and.bp(n2)%Q.ne.0.)) THEN
! rotation ship for ambient side current
if (LOA<0.) then
phi=0. !don't rotate grid
else
if ((U_TSHD-U_b).eq.0) then
phi=atan2(V_b,1.e-12)
else
phi=atan2(V_b,(U_TSHD-U_b))
endif
endif
xx=Rp(i)*cos_u(j)-schuif_x
yy=Rp(i)*sin_u(j)
IF (k.le.FLOOR(bp(n2)%height/dz).and.k.ge.CEILING(bp(n2)%zbottom/dz)) THEN ! obstacle:
xTSHD(1:4)=bp(n2)%x*cos(phi)-bp(n2)%y*sin(phi)
yTSHD(1:4)=bp(n2)%x*sin(phi)+bp(n2)%y*cos(phi)
if (bp(n2)%radius.gt.0.) then
inout=0
IF (((xx-xTSHD(1))**2+(yy-yTSHD(1))**2).lt.(bp(n2)%radius)**2) THEN
inout=1
ENDIF
else
CALL PNPOLY (xx,yy, xTSHD(1:4), yTSHD(1:4), 4, inout )
endif
ELSE
inout=0
ENDIF
if (inout.eq.1) then
div2=div2-bp(n2)%Q*fc_global(i,j+jmax*rank,k)/bp(n2)%volncells
div3=div3-bp(n2)%Q*fc_global(i,j+jmax*rank,k)/bp(n2)%volncells
endif
ENDIF
ENDDO ! bedplume loop
divbar1 = divbar1 + div1
div1 = abs(div1)
divmax1 = max( divmax1 , div1 )
divbar2 = divbar2 + div2
div2 = abs(div2)
divmax2 = max( divmax2 , div2 )
divbar3 = divbar3 + div3
div3 = abs(div3)
divmax3 = max( divmax3 , div3 )
enddo
enddo
enddo
call mpi_allreduce(divbar1,divbar_tot1,1,mpi_real8,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(divmax1,divmax_tot1,1,mpi_real8,mpi_max,mpi_comm_world,ierr)
call mpi_allreduce(divbar2,divbar_tot2,1,mpi_real8,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(divmax2,divmax_tot2,1,mpi_real8,mpi_max,mpi_comm_world,ierr)
call mpi_allreduce(divbar3,divbar_tot3,1,mpi_real8,mpi_sum,mpi_comm_world,ierr)
call mpi_allreduce(divmax3,divmax_tot3,1,mpi_real8,mpi_max,mpi_comm_world,ierr)
if (jmax.gt.2) then
if (rank.eq.0) write(6,100)divbar_tot1,divmax_tot1
100 format('Mass loss/gain dr/dt+div(ru)=0 (solved in continuity_solver=1) : Tot = ',e13.6,
+ ' Max = ',e13.6)
if (rank.eq.0) write(6,101)divbar_tot3,divmax_tot3
101 format('Div(u) volume centre velocity (solved in continuity_solver=36) : Tot = ',e13.6,
+ ' Max = ',e13.6)
if (rank.eq.0) write(6,102)divbar_tot2,divmax_tot2
102 format('Div(u) mass centre velocity (solved in continuity_solver=35) : Tot = ',e13.6,
+ ' Max = ',e13.6)
else
if (rank.eq.0) then
write(*,*),'No check on continuity because < 3 grid cells per CPU in lateral direction'
endif
endif
end
! MODULE work_array
! REAL, DIMENSION(:,:,:), ALLOCATABLE :: Uavg,Vavg,Wavg,Ravg,Pavg,muavg
! REAL, DIMENSION(:,:,:), ALLOCATABLE :: sigU2,sigV2,sigW2,sigR2,sigUV,sigUW,sigVW
! REAL, DIMENSION(:,:,:,:), ALLOCATABLE :: sigC2,Cavg,sigUC,sigVC,sigWC,Cmax,Cmin
! INTEGER stat_count
! REAL stat_time_count
!! REAL, DIMENSION(:,:,:), ALLOCATABLE :: fUavg,sigfU2
! END MODULE
subroutine statistics
! USE work_array
USE nlist
implicit none
! include 'param.txt'
! include 'common.txt'
!REAL uu,vv,ww,uudt,vvdt,wwdt
REAL uu(1:imax,1:jmax,1:kmax),vv(1:imax,1:jmax,1:kmax),ww(1:imax,1:jmax,1:kmax)
REAL uudt(1:imax,1:jmax,1:kmax),vvdt(1:imax,1:jmax,1:kmax),wwdt(1:imax,1:jmax,1:kmax)
REAL tau_flow_temp(1:imax,1:jmax),tau_frac_temp(1:nfrac,1:imax,1:jmax)
INTEGER n
stat_count=stat_count+1
stat_time_count=stat_time_count+dt
! below is 3x faster than elementwise within filling AVG and sig2 arrays within do-loop: now RMS takes 10% instead of 30% CPU time!
do i=1,imax
do j=1,jmax
do k=1,kmax
uu(i,j,k)=0.5*(Unew(i,j,k)+Unew(i-1,j,k))*cos_u(j)-0.5*(Vnew(i,j,k)+Vnew(i,j-1,k))*sin_u(j)
vv(i,j,k)=0.5*(Vnew(i,j,k)+Vnew(i,j-1,k))*cos_u(j)+0.5*(Unew(i,j,k)+Unew(i-1,j,k))*sin_u(j)
ww(i,j,k)=0.5*(Wnew(i,j,k)+Wnew(i,j,k-1))
do n=1,nfrac
Cmax(n,i,j,k) = MAX(Cmax(n,i,j,k),Cnew(n,i,j,k))
Cmin(n,i,j,k) = MIN(Cmin(n,i,j,k),Cnew(n,i,j,k))
enddo
enddo
enddo
enddo
tau_flow_temp=sqrt((0.5*(tau_fl_Unew(1:imax,1:jmax)+tau_fl_Unew(0:imax-1,1:jmax)))**2
& +(0.5*(tau_fl_Vnew(1:imax,1:jmax)+tau_fl_Vnew(1:imax,0:jmax-1)))**2)
uudt=uu*dt
vvdt=vv*dt
wwdt=ww*dt
sigU2 = sigU2 + uu*uudt
sigV2 = sigV2 + vv*vvdt
sigW2 = sigW2 + ww*wwdt
sigR2 = sigR2 + Rnew(1:imax,1:jmax,1:kmax)*Rnew(1:imax,1:jmax,1:kmax)*dt
sigUV = sigUV + uu*vvdt
sigUW = sigUW + uu*wwdt
sigVW = sigVW + vv*wwdt
sig_tau_flow2 = sig_tau_flow2+tau_flow_temp*tau_flow_temp*dt
Uavg = Uavg + uudt
Vavg = Vavg + vvdt
Wavg = Wavg + wwdt
Umax = MAX(Umax,ABS(uu))
Vmax = MAX(Vmax,ABS(vv))
Wmax = MAX(Wmax,ABS(ww))
! Umin = MIN(Umax,ABS(uu))
! Vmin = MIN(Vmax,ABS(vv))
! Wmin = MIN(Wmax,ABS(ww))
Uhormax = MAX(Uhormax,SQRT(uu**2+vv**2))
U3dmax = MAX(U3dmax,SQRT(uu**2+vv**2+ww**2))
Ravg = Ravg + Rnew(1:imax,1:jmax,1:kmax)*dt
Pavg = Pavg + (p(1:imax,1:jmax,1:kmax)+pold(1:imax,1:jmax,1:kmax))*dt
muavg = muavg + ekm(1:imax,1:jmax,1:kmax)*dt
Cavg = Cavg + Cnew(1:nfrac,1:imax,1:jmax,1:kmax)*dt
tau_flow_avg = tau_flow_avg+tau_flow_temp*dt
do n=1,nfrac
sigC2(n,:,:,:) = sigC2(n,:,:,:) + Cnew(n,1:imax,1:jmax,1:kmax)*Cnew(n,1:imax,1:jmax,1:kmax)*dt
sigUC(n,:,:,:) = sigUC(n,:,:,:) + Cnew(n,1:imax,1:jmax,1:kmax)*uudt
sigVC(n,:,:,:) = sigVC(n,:,:,:) + Cnew(n,1:imax,1:jmax,1:kmax)*vvdt
sigWC(n,:,:,:) = sigWC(n,:,:,:) + Cnew(n,1:imax,1:jmax,1:kmax)*wwdt
enddo
if (nfrac>0) then
tau_flow_temp=ust_sl_new(1:imax,1:jmax)*ust_sl_new(1:imax,1:jmax)*rho_b
sig_tau_sl2 = sig_tau_sl2+tau_flow_temp*tau_flow_temp*dt
tau_sl_avg = tau_sl_avg+tau_flow_temp*dt
tau_flow_temp=ust_bl_new(1:imax,1:jmax)*ust_bl_new(1:imax,1:jmax)*rho_b
sig_tau_bl2 = sig_tau_bl2+tau_flow_temp*tau_flow_temp*dt
tau_bl_avg = tau_bl_avg+tau_flow_temp*dt
tau_flow_temp=ust_mud_new(1:imax,1:jmax)*ust_mud_new(1:imax,1:jmax)*rho_b
sig_tau_mud2 = sig_tau_mud2+tau_flow_temp*tau_flow_temp*dt
tau_mud_avg = tau_mud_avg+tau_flow_temp*dt
tau_frac_temp=ust_frac_new(1:nfrac,1:imax,1:jmax)*ust_frac_new(1:nfrac,1:imax,1:jmax)*rho_b
sig_tau_frac2 = sig_tau_frac2+tau_frac_temp*tau_frac_temp*dt
tau_frac_avg = tau_frac_avg+tau_frac_temp*dt
endif
!!! Favre average results in differences of <1 promille for Umean and <1% for U' between Favre and Reynolds avg
!!! for CO2 plume simulation (Wang et al. 2008), this is not important for dredge plumes, thus only Reynolds avg is calculated from now on
! fUavg(i,j,k) = fUavg(i,j,k) + uu*R(i,j,k)
! sigfU2(i,j,k) = sigfU2(i,j,k) + uu*uu*R(i,j,k)
end
MODULE history_array
use nlist
IMPLICIT NONE
INTEGER,PARAMETER :: tdim=200000 ! length of history array in the time
INTEGER :: nhispoint
type history
integer :: i,j,k
REAL :: x,y,z
REAL*8, DIMENSION(tdim) :: U,V,W,P,RHO,t
REAL*8, DIMENSION(20,tdim) :: C ! 20 fractions in history, must be enough..
end type history
TYPE(history), DIMENSION(:), ALLOCATABLE :: his
END MODULE
subroutine init_his
USE history_array
USE error_functions
USE nlist
implicit none
! include 'param.txt'
! include 'common.txt'
integer ios,n
type history_init
integer :: i,j,k
end type history_init
TYPE(history_init), DIMENSION(100) :: hist
NAMELIST /histories/hist
hist(:)%i=0
OPEN(1,FILE=hisfile,IOSTAT=ios,ACTION='read')
IF (ios/=0) CALL writeerror(100)
READ (UNIT=1,NML=histories,IOSTAT=ios)
nhispoint=0
DO WHILE (hist(nhispoint+1)%i.NE.0)
nhispoint=nhispoint+1
END DO
!write(*,*)'hist1,2,3,4',hist(1)%i,hist(2)%i,hist(3)%i,hist(4)%i
!write(*,*)'nhispoint',nhispoint
CLOSE(1)
ALLOCATE(his(nhispoint))
!write(*,*)'his,2,3,4',his(1)%i,his(2)%i,his(3)%i,his(4)%i
DO n=1,nhispoint
his(n)%i=hist(n)%i
his(n)%j=hist(n)%j
his(n)%k=hist(n)%k
IF (his(n)%i>imax) CALL writeerror(110)
IF (his(n)%i<0) CALL writeerror(111)
IF (his(n)%j>jmax*px) CALL writeerror(120)
IF (his(n)%j<0) CALL writeerror(121)
IF (his(n)%k>kmax) CALL writeerror(130)
IF (his(n)%k<0) CALL writeerror(131)
his(n)%x=Rp(his(n)%i)*cos_u(his(n)%j-rank*jmax)-schuif_x
his(n)%y=Rp(his(n)%i)*sin_u(his(n)%j-rank*jmax)
his(n)%z=his(n)%k*dz-0.5*dz
ENDDO
END SUBROUTINE init_his
! subroutine appendhis(rank,istep,time,Unew,Vnew,Wnew,P,Cnew,Rnew)
subroutine appendhis
USE history_array
USE nlist
implicit none
! include 'param.txt'
!include 'common.txt'
! REAL Unew(0:i1,0:j1,0:k1),Vnew(0:i1,0:j1,0:k1),Wnew(0:i1,0:j1,0:k1)
! REAL Rnew(0:i1,0:j1,0:k1),Cnew(0:i1,0:j1,0:k1),P(0:i1,0:j1,0:k1)
! REAL time
integer n,r,nf
IF (istep.lt.200000) THEN
his(1)%t(istep)=time_np !Unew is already updated, therefore it is output at time_np
DO n=1,nhispoint
r=INT((his(n)%j-1)/jmax)
IF (rank.eq.r) THEN
i=his(n)%i
j=his(n)%j-rank*jmax
k=his(n)%k
his(n)%U(istep)=Unew(i,j,k)*cos_u(j)-Vnew(i,j,k)*sin_v(j)
his(n)%V(istep)=Vnew(i,j,k)*cos_v(j)+Unew(i,j,k)*sin_u(j)
his(n)%W(istep)=Wnew(i,j,k)
his(n)%P(istep)=Pold(i,j,k)+P(i,j,k)
his(n)%RHO(istep)=Rnew(i,j,k)
DO nf=1,MIN(20,nfrac)
his(n)%C(nf,istep)=Cnew(nf,i,j,k)
ENDDO
ENDIF
ENDDO
ENDIF
end SUBROUTINE
! subroutine finalize_his(rank,istep)
subroutine finalize_his
USE history_array
USE nlist
USE netcdf
implicit none
! include 'param.txt'
include 'mpif.h'
! real UU(1:200000)
integer ierr,n,r,tag,status(MPI_STATUS_SIZE),nf
INTEGER (8) :: ps
REAL :: Uhis(1:nhispoint,1:istep),Vhis(1:nhispoint,1:istep),Whis(1:nhispoint,1:istep)
REAL :: Phis(1:nhispoint,1:istep),Chis(1:nfrac,1:nhispoint,1:istep),RHOhis(1:nhispoint,1:istep)
REAL :: xhis(1:nhispoint),yhis(1:nhispoint),zhis(1:nhispoint)
INTEGER :: ihis(1:nhispoint),jhis(1:nhispoint),khis(1:nhispoint)
REAL :: this(1:istep)
! We are writing 3D, 2D and 1D data, a nhispoint x istep grid.
integer, parameter :: NDIMS1 = 2
integer, parameter :: NDIMS2 = 1
integer, parameter :: NDIMS3 = 3
integer, parameter :: NDIMS4 = 1
! integer, parameter :: NX = imax, NY = jmax, NZ = kmax
! When we create netCDF files, variables and dimensions, we get back
! an ID for each one.
integer :: ncid, varid1,varid2,varid3, varid4, varid5, varid6, varid7, varid8
integer :: varid9,varid10,varid11,varid12,varid13
integer :: dimids1(NDIMS1), dimids2(NDIMS2),dimids3(NDIMS3),dimids4(NDIMS4)
integer :: nhis_dimid,time_dimid,nfrac_dimid,istep2
character(1024) :: gitversion
character(1024) :: url
character(1024) :: date_make
include 'version.inc'
IF (rank>0) THEN
DO n=1,nhispoint
r=INT((his(n)%j-1)/jmax)
IF (rank.eq.r) THEN
tag=INT(r*n)
call mpi_sendrecv_replace(his(n)%U ,200000,MPI_REAL8,0,10,0,10, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%V ,200000,MPI_REAL8,0,11,0,11, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%W ,200000,MPI_REAL8,0,12,0,12, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%P ,200000,MPI_REAL8,0,13,0,13, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%C,20*200000,MPI_REAL8,0,14,0,14, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%RHO ,200000,MPI_REAL8,0,15,0,15, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%x ,1,MPI_REAL8,0,16,0,16, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%y ,1,MPI_REAL8,0,17,0,17, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%z ,1,MPI_REAL8,0,18,0,18, MPI_COMM_WORLD,status,ierr)
ENDIF
ENDDO
ELSE
DO n=1,nhispoint
r=INT((his(n)%j-1)/jmax)
IF (rank.ne.r) THEN
call mpi_sendrecv_replace(his(n)%U ,200000,MPI_REAL8,r,10,r,10, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%V ,200000,MPI_REAL8,r,11,r,11, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%W ,200000,MPI_REAL8,r,12,r,12, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%P ,200000,MPI_REAL8,r,13,r,13, MPI_COMM_WORLD,status,ierr)
call mpi_sendrecv_replace(his(n)%C ,20*200000,MPI_REAL8,r,14,r,14, MPI_COMM_WORLD,status,ierr)