forked from KuangLab-Harvard/SAM_SRCv6.11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbuffer.f90
649 lines (566 loc) · 16 KB
/
hbuffer.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
module hbuffer
use grid
use params
implicit none
integer hbuf_length, HBUF_MAX_LENGTH
parameter(HBUF_MAX_LENGTH = 1000)
real(8) hbuf(HBUF_MAX_LENGTH*nzm+30)
character(8) namelist(HBUF_MAX_LENGTH)
character(80) deflist(HBUF_MAX_LENGTH)
character(10) unitlist(HBUF_MAX_LENGTH)
integer status(HBUF_MAX_LENGTH)
integer average_type(HBUF_MAX_LENGTH)
integer,save:: statfileindex
character *4,save:: timechar
integer:: nstep_eof
CONTAINS
!------------------------------------------------------------
subroutine hbuf_average(n)
! Average the profiles in buffer
use vars
implicit none
integer l, n
real(8) coef
coef=1./dble(n)
do l = 1,hbuf_length*nzm
hbuf(l) = hbuf(l) *coef
end do
end subroutine hbuf_average
!------------------------------------------------------------
subroutine hbuf_avg_put(name, f, dimx1,dimx2,dimy1,dimy2,dimz,factor)
! Write to buffer an averaged 3D field (not to file yet)
implicit none
integer dimx1, dimx2, dimy1, dimy2, dimz
real f(dimx1:dimx2, dimy1:dimy2, dimz), fz(nzm), factor
character *(*) name
integer l, begin, k
logical flag
flag=.false.
do l = 1,hbuf_length
if(.not.(lgt(name,namelist(l)).or.llt(name,namelist(l)))) then
flag=.true.
if(status(l).gt.0) then
status(l) = 999
call averageXY(f,dimx1,dimx2,dimy1,dimy2,dimz,fz)
begin = (l-1)*nzm
do k = 1,nzm
hbuf(begin+k) = hbuf(begin+k) + fz(k) * factor
end do
endif
endif
end do
if(.not.flag.and.masterproc) print*,name
end subroutine hbuf_avg_put
!------------------------------------------------------------
subroutine hbuf_flush
! Flush the buffer
use vars
implicit none
integer l,k,n
do l=1,hbuf_length*nzm
hbuf(l) = 0.
end do
do n=1,ncondavg
do k=1,nzm
condavg_factor(k,n) = 0.
end do
end do
s_acld=0.
s_acldl=0.
s_acldm=0.
s_acldh=0.
s_acldcold=0.
s_acldisccp=0.
s_acldlisccp=0.
s_acldmisccp=0.
s_acldhisccp=0.
s_acldmodis=0.
s_acldlmodis=0.
s_acldmmodis=0.
s_acldhmodis=0.
s_acldliqmodis=0.
s_acldicemodis=0.
s_acldmisr=0.
s_relmodis=0.
s_reimodis=0.
s_lwpmodis=0.
s_iwpmodis=0.
s_tbisccp=0.
s_tbclrisccp=0.
s_cldtauisccp=0.
s_cldalbisccp=0.
s_cldtaumodis=0.
s_cldtaulmodis=0.
s_cldtauimodis=0.
s_ptopisccp=0.
s_ptopmodis=0.
s_ztopmisr=0.
s_ar=0.
s_arthr=0.
w_max=0.
u_max=0.
s_flnt=0.
s_flntoa=0.
s_flntoac=0.
s_flns=0.
s_flnsc=0.
s_flds=0.
s_fsnt=0.
s_fsntoa=0.
s_fsntoac=0.
s_fsns=0.
s_fsnsc=0.
s_fsds=0.
s_solin=0.
lhobs=0.
shobs=0.
s_sst = 0.
z_inv=0.
z2_inv=0.
z_ct=0.
z_cb=z(nzm)
z_ctmn=0.
z_cbmn=0.
z2_cb=0.
z2_ct=0.
cwpmean=0.
cwp2=0.
prec2=0.
precmax=0.
precmean=0.
precmean=0.
ncmn=0.
nrmn=0.
ncloudy=0.
nrainy=0.
end subroutine hbuf_flush
!------------------------------------------------------------
subroutine hbuf_init
! Read list of vertical profile names to store in file
use grid, only: case, masterproc
use tracers, only: tracers_hbuf_init
implicit none
character *8 nm
character *80 def
character *10 un
integer stat,count,type,i,trcount
character*3 filestatus
integer n,m
logical duplicate_entries
integer, external :: lenstr
open(66,file=trim(rundatadir)//'/lst',&
status='old',form='formatted')
! first determine number of entries:
hbuf_length = 0
111 continue
read(66,err=111,end=222,fmt=*) stat,type,nm
if(stat.gt.0) hbuf_length = hbuf_length+1
goto 111
222 continue
if(hbuf_length.gt.HBUF_MAX_LENGTH) then
print *,'Error: hbuf_length > HBUF_MAX_LENGTH.'
call task_abort()
endif
! fill the buffers:
rewind(66)
count = 0
333 continue
read(66,err=333,end=444,fmt=*) stat,type,nm,def,un
if(stat.gt.0) then
count = count + 1
namelist(count) = nm
deflist(count) = def
unitlist(count) = un
status(count) = stat
average_type(count) = type
endif
goto 333
444 continue
trcount=0
call hbuf_conditionals_init(count,trcount)
hbuf_length = hbuf_length+trcount
trcount=0
if(dotracers) call tracers_hbuf_init(namelist,deflist,unitlist,status,average_type,count,trcount)
hbuf_length = hbuf_length+trcount
trcount=0
if(docloud.or.dosmoke) call hbuf_micro_init(namelist,deflist,unitlist,status,average_type,count,trcount)
hbuf_length = hbuf_length+trcount
trcount=0
if(dosgs) call hbuf_sgs_init(namelist,deflist,unitlist,status,average_type,count,trcount)
hbuf_length = hbuf_length+trcount
! check if there are dublicate entries in the stat list:
duplicate_entries = .false.
do n = 1,count-1
do m = n+1,count
if (trim(namelist(n)).eq.trim(namelist(m))) then
duplicate_entries=.true.
if(masterproc) then
print*,'Error: Multiple definition of '//namelist(n)// ' variable in stat list'
end if
end if
end do
end do
! Halt simulation if duplicate entries appear in namelist.
if(duplicate_entries) call task_abort()
if(masterproc) then
print *,'Number of statistics profiles:', hbuf_length
print *,'Statistics profiles to save:'
write(*,'(8(a,3x))')(namelist(i),i=1,hbuf_length)
! make sure that the stat file doesn't exist if a new run to prevent
! accidental overwrite
filestatus='old'
if(nrestart.eq.0.or.nrestart.eq.2) then
filestatus='new'
end if
if(dosepstat) then
! write stat to different files to avoid large file (Qiyu 2024)
statfileindex=0
write(timechar,'(i4)') statfileindex
open (55,file='./OUT_STAT/'// &
case(1:lenstr(case))//'_'// &
caseid(1:lenstr(caseid))//'_'// &
timechar(5-lenstr(timechar):4)// &
'.stat', &
status=filestatus,form='unformatted')
else
open (55,file='./OUT_STAT/'// &
case(1:lenstr(case))//'_'// &
caseid(1:lenstr(caseid))//'.stat', &
status=filestatus,form='unformatted')
end if
close(55)
end if
close (66)
call hbuf_flush()
end subroutine hbuf_init
!-----------------------------------------------------------------
subroutine hbuf_put(name, f, factor)
! Write to buffer (not to file yet)
use grid, only: masterproc
implicit none
real f(nzm), factor
character *(*) name
integer l, begin, k
logical flag
flag=.false.
do l = 1,hbuf_length
if(.not.(lgt(name,namelist(l)).or.llt(name,namelist(l)))) then
flag=.true.
if(status(l).gt.0) then
status(l) = 999
begin = (l-1)*nzm
do k = 1,nzm
hbuf(begin+k) = hbuf(begin+k) + f(k)*factor
end do
endif
endif
end do
if(.not.flag.and.masterproc) print*,'name ', name,' is missing in "lst" file.'
end subroutine hbuf_put
!----------------------------------------------------------------
subroutine hbuf_write(n)
! Write buffer to file
use vars
implicit none
integer l, k, n, ntape, length
real(8) coef,aver,factor
real tmp(nzm),tmp1(nzm)
real(8) hbuf1(HBUF_MAX_LENGTH*nzm+100)
real(4) hbuf_real4(HBUF_MAX_LENGTH*nzm+100),dummy
integer nbuf
real cloud_f(nzm,ncondavg), tmp_f(nzm,ncondavg) !bloss
integer ncond !bloss
integer nsteplast
integer, external :: lenstr
data ntape/56/
aver=1./dble(n)
factor=1./dble(nx*ny)
! Kuang Ensemble run: turn on mpi to calculate outpu (Song Qiyu, 2022)
if(dompiensemble) dompi = .true.
if(dompi) then
! average condavg_factor across domains. This will sum the
! weighting of all of the conditional statistics across the
! processors and allow us to normalize the statistics on each
! processor accordingly. In the end, these normalized statistics
! on each processor will sum to give the value of the conditional
! statistic.
! sum across processors
call task_sum_real(condavg_factor,cloud_f,nzm*ncondavg)
else
cloud_f(1:nzm,1:ncondavg) = condavg_factor(1:nzm,1:ncondavg)
end if
! create normalization/weighting factor for conditional statistics
! Here, cloud_f holds the sum of condavg_factor across all processors.
condavg_factor(:,:) = 0.
do ncond = 1,ncondavg
do k=1,nzm
if(ABS(cloud_f(k,ncond)).gt.EPSILON(1.)) then
condavg_factor(k,ncond)=float(nsubdomains)*float(n)/cloud_f(k,ncond)
end if
end do
end do
! compute each processor's component of the total conditional average.
length = 0
do l = 1,hbuf_length
if(status(l).eq. 999) then
length = length+1
do ncond = 1,ncondavg
if(average_type(l).eq.ncond) then
do k=1,nzm
hbuf((l-1)*nzm+k) = hbuf((l-1)*nzm+k)*condavg_factor(k,ncond)
end do
do k=1,nzm
! insert a missing value if there are no samples of this
! conditional statistic at this level.
if(ABS(cloud_f(k,ncond)).lt.EPSILON(1.)) hbuf((l-1)*nzm+k) = -9999.
end do
endif
end do
endif
end do
! Get statistics buffer from different processes, add them together
! and average
if(dompi) then
tmp1(1)=w_max
tmp1(2)=u_max
tmp1(3)=precmax
tmp1(4)=z_ct
call task_max_real(tmp1,tmp,4)
w_max=tmp(1)
u_max=tmp(2)
precmax=tmp(3)
z_ct=tmp(4)
tmp1(1)=z_cb
call task_min_real(tmp1,tmp,1)
z_cb=tmp(1)
k=hbuf_length*nzm
hbuf(k+1)=s_acld
hbuf(k+2)=s_acldl
hbuf(k+3)=s_acldm
hbuf(k+4)=s_acldh
hbuf(k+5)=s_acldcold
hbuf(k+6)=s_ar
hbuf(k+7)=s_flns
hbuf(k+8)=s_flnt
hbuf(k+9)=s_flntoa
hbuf(k+10)=s_flnsc
hbuf(k+11)=s_flntoac
hbuf(k+12)=s_flds
hbuf(k+13)=s_fsns
hbuf(k+14)=s_fsnt
hbuf(k+15)=s_fsntoa
hbuf(k+16)=s_fsnsc
hbuf(k+17)=s_fsntoac
hbuf(k+18)=s_fsds
hbuf(k+19)=s_solin
hbuf(k+20)=s_acldisccp
hbuf(k+21)=s_acldlisccp
hbuf(k+22)=s_acldmisccp
hbuf(k+23)=s_acldhisccp
hbuf(k+24)=s_sst
hbuf(k+25)=s_acldmodis
hbuf(k+26)=s_acldlmodis
hbuf(k+27)=s_acldmmodis
hbuf(k+28)=s_acldhmodis
hbuf(k+29)=s_acldmisr
hbuf(k+30)=s_relmodis
hbuf(k+31)=s_reimodis
hbuf(k+32)=s_lwpmodis
hbuf(k+33)=s_iwpmodis
hbuf(k+34)=s_tbisccp
hbuf(k+35)=s_tbclrisccp
hbuf(k+36)=s_acldliqmodis
hbuf(k+37)=s_acldicemodis
hbuf(k+38)=s_cldtaumodis
hbuf(k+39)=s_cldtaulmodis
hbuf(k+40)=s_cldtauimodis
hbuf(k+41)=s_cldtauisccp
hbuf(k+42)=s_cldalbisccp
hbuf(k+43)=s_ptopisccp
hbuf(k+44)=s_ptopmodis
hbuf(k+45)=s_ztopmisr
hbuf(k+46)=z_inv
hbuf(k+47)=z2_inv
hbuf(k+48)=ncloudy
hbuf(k+49)=z_cbmn
hbuf(k+50)=z2_cb
hbuf(k+51)=z_ctmn
hbuf(k+52)=z2_ct
hbuf(k+53)=cwpmean
hbuf(k+54)=cwp2
hbuf(k+55)=precmean
hbuf(k+56)=prec2
hbuf(k+57)=ncmn
hbuf(k+58)=nrainy
hbuf(k+59)=nrmn
hbuf(k+60)=s_arthr
nbuf = k+60
call task_sum_real8(hbuf,hbuf1,nbuf)
coef = 1./dble(nsubdomains)
hbuf(1:nbuf) = hbuf1(1:nbuf)*coef
s_acld=hbuf(k+1)
s_acldl=hbuf(k+2)
s_acldm=hbuf(k+3)
s_acldh=hbuf(k+4)
s_acldcold=hbuf(k+5)
s_ar=hbuf(k+6)
s_flns=hbuf(k+7)
s_flnt=hbuf(k+8)
s_flntoa=hbuf(k+9)
s_flnsc=hbuf(k+10)
s_flntoac=hbuf(k+11)
s_flds=hbuf(k+12)
s_fsns=hbuf(k+13)
s_fsnt=hbuf(k+14)
s_fsntoa=hbuf(k+15)
s_fsnsc=hbuf(k+16)
s_fsntoac=hbuf(k+17)
s_fsds=hbuf(k+18)
s_solin=hbuf(k+19)
s_acldisccp=hbuf(k+20)
s_acldlisccp=hbuf(k+21)
s_acldmisccp=hbuf(k+22)
s_acldhisccp=hbuf(k+23)
s_sst=hbuf(k+24)
s_acldmodis=hbuf(k+25)
s_acldlmodis=hbuf(k+26)
s_acldmmodis=hbuf(k+27)
s_acldhmodis=hbuf(k+28)
s_acldmisr=hbuf(k+29)
s_relmodis=hbuf(k+30)
s_reimodis=hbuf(k+31)
s_lwpmodis=hbuf(k+32)
s_iwpmodis=hbuf(k+33)
s_tbisccp=hbuf(k+34)
s_tbclrisccp=hbuf(k+35)
s_acldliqmodis=hbuf(k+36)
s_acldicemodis=hbuf(k+37)
s_cldtaumodis=hbuf(k+38)
s_cldtaulmodis=hbuf(k+39)
s_cldtauimodis=hbuf(k+40)
s_cldtauisccp=hbuf(k+41)
s_cldalbisccp=hbuf(k+42)
s_ptopisccp=hbuf(k+43)
s_ptopmodis=hbuf(k+44)
s_ztopmisr=hbuf(k+45)
z_inv=hbuf(k+46)
z2_inv=hbuf(k+47)
if(hbuf(k+48).gt.0) then
coef=1./hbuf(k+48)
else
coef=1.
end if
z_cbmn=hbuf(k+49)*coef
z2_cb=hbuf(k+50)*coef
z_ctmn=hbuf(k+51)*coef
z2_ct=hbuf(k+52)*coef
cwpmean=hbuf(k+53)
cwp2=hbuf(k+54)
precmean=hbuf(k+55)
prec2=hbuf(k+56)
ncmn=hbuf(k+57)*coef
if(hbuf(k+58).gt.0) then
coef=1./hbuf(k+58)
else
coef=1.
end if
nrmn=hbuf(k+59)*coef
s_arthr=hbuf(k+60)
z2_inv=z2_inv*factor*aver-(z_inv*factor*aver)**2
z2_cb=z2_cb*factor-(z_cbmn*factor)**2
z2_ct=z2_ct*factor-(z_ctmn*factor)**2
cwp2=cwp2*factor*aver-(cwpmean*factor*aver)**2
prec2=prec2*factor*aver-(precmean*factor*aver)**2
endif
! Kuang Ensemble run: turn off mpi after calculating buffer values (Song Qiyu, 2022)
if(dompiensemble) dompi = .false.
if(masterproc) then
if(dosepstat) then
! write stat to different files to avoid large file (Qiyu 2024)
statfileindex=(nstep-1)/nstep_sepstat
write(timechar,'(i4)') statfileindex
open (ntape,file='./OUT_STAT/'// &
case(1:lenstr(case))//'_'// &
caseid(1:lenstr(caseid))//'_'// &
timechar(5-lenstr(timechar):4)// &
'.stat', &
status='unknown',form='unformatted')
nstep_eof = nstat+statfileindex*nstep_sepstat
else
statfileindex = 0
open (ntape,file='./OUT_STAT/'// &
case(1:lenstr(case))//'_'// &
caseid(1:lenstr(caseid))//'.stat', &
status='unknown',form='unformatted')
nstep_eof = nstat
end if
if(nstep.ne.nstep_eof) then
do while(.true.)
read(ntape, end=222)
read(ntape) dummy,dummy,nsteplast
if(nsteplast.ge.nstep) then
backspace(ntape)
backspace(ntape) ! these two lines added because of
read(ntape) ! a bug in gfrotran compiler
print*,'stat file at nstep ',nsteplast
goto 222 ! yeh, I know, it's bad ....
end if
read(ntape)
do l = 1,hbuf_length
if(status(l).eq. 999) then
read(ntape)
read(ntape)
read(ntape)
read(ntape)
end if
end do
end do
222 continue
backspace(ntape)
backspace(ntape)
read(ntape)
endif
print *,'Writting history file ',caseid
write(ntape) caseid, version
write(ntape) &
real(day-nstat*dt/2./86400.,4),real(dt,4),nstep,nx,ny,nz,nzm, &
real(dx,4),real(dy,4),real(dz,4),real(adz,4), &
real(z,4),real(pres,4),real(s_sst*aver*factor+t00,4),real(pres0,4), &
real(s_acld*aver*factor,4),real(s_ar*aver*factor,4), &
real(s_acldcold*aver*factor,4),real(w_max,4),real(u_max,4),-1._4,&
real(s_flns*aver*factor,4),real(s_flnt*aver*factor,4),real(s_flntoa*aver*factor,4),&
real(s_flnsc*aver*factor,4),real(s_flntoac*aver*factor,4),real(s_flds*aver*factor,4), &
real(s_fsns*aver*factor,4),real(s_fsnt*aver*factor,4),real(s_fsntoa*aver*factor,4), &
real(s_fsnsc*aver*factor,4),real(s_fsntoac*aver*factor,4), &
real(s_fsds*aver*factor,4),real(s_solin*aver*factor,4), &
real(sstobs,4),real(lhobs*aver,4),real(shobs*aver,4), &
real(s_acldl*aver*factor,4),real(s_acldm*aver*factor,4),real(s_acldh*aver*factor,4), &
real(s_acldisccp,4),real(s_acldlisccp,4),real(s_acldmisccp,4),real(s_acldhisccp,4), &
real(s_acldmodis,4),real(s_acldlmodis,4),real(s_acldmmodis,4), &
real(s_acldhmodis,4),real(s_acldmisr,4), &
real(s_relmodis,4), real(s_reimodis,4), real(s_lwpmodis,4), &
real(s_iwpmodis,4), real(s_tbisccp,4), real(s_tbclrisccp,4), &
real(s_acldliqmodis,4), real(s_acldicemodis,4), real(s_cldtauisccp,4), &
real(s_cldalbisccp,4), real(s_ptopisccp,4), &
real(s_cldtaumodis,4), real(s_cldtaulmodis,4), real(s_cldtauimodis,4), &
real(s_ptopmodis,4), real(s_ztopmisr,4), &
real(z_inv*aver*factor,4), real(z2_inv,4), &
real(z_ctmn*factor,4), real(z2_ct,4), real(z_ct,4), &
real(z_cbmn*factor,4), real(z2_cb,4), real(z_cb,4), &
real(cwpmean*aver*factor,4), real(cwp2,4), &
real(precmean*aver*factor,4), real(prec2,4), real(precmax,4), &
ncmn, nrmn, real(s_arthr*aver*factor,4)
write(ntape) length
hbuf_real4(1:hbuf_length*nzm) = hbuf(1:hbuf_length*nzm)
do l = 1,hbuf_length
if(status(l).eq. 999) then
write(ntape) namelist(l)
write(ntape) deflist(l)
write(ntape) unitlist(l)
write(ntape) (hbuf_real4((l-1)*nzm+k),k=1,nzm)
end if
end do
close (ntape)
end if
end subroutine hbuf_write
end module hbuffer