-
Notifications
You must be signed in to change notification settings - Fork 5
/
bl_ysu.F90
1697 lines (1638 loc) · 60.2 KB
/
bl_ysu.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
#define NEED_B4B_DURING_CCPP_TESTING 1
!=================================================================================================================
module bl_ysu
use ccpp_kind_types,only: kind_phys
implicit none
private
public:: bl_ysu_run, &
bl_ysu_init, &
bl_ysu_finalize
contains
!=================================================================================================================
!>\section arg_table_bl_ysu_init
!!\html\include bl_ysu_init.html
!!
subroutine bl_ysu_init(errmsg,errflg)
!=================================================================================================================
!--- output arguments:
character(len=*),intent(out):: errmsg
integer,intent(out):: errflg
!-----------------------------------------------------------------------------------------------------------------
errmsg = 'bl_ysu_init OK'
errflg = 0
end subroutine bl_ysu_init
!=================================================================================================================
!>\section arg_table_bl_ysu_finalize
!!\html\include bl_ysu_finalize.html
!!
subroutine bl_ysu_finalize(errmsg,errflg)
!=================================================================================================================
!--- output arguments:
character(len=*),intent(out):: errmsg
integer,intent(out):: errflg
!-----------------------------------------------------------------------------------------------------------------
errmsg = 'bl_ysu_finalize OK'
errflg = 0
end subroutine bl_ysu_finalize
!=================================================================================================================
!>\section arg_table_bl_ysu_run
!!\html\include bl_ysu_run.html
!!
subroutine bl_ysu_run(ux,vx,tx,qvx,qcx,qix,nmix,qmix,p2d,p2di,pi2d, &
f_qc,f_qi, &
utnp,vtnp,ttnp,qvtnp,qctnp,qitnp,qmixtnp, &
cp,g,rovcp,rd,rovg,ep1,ep2,karman,xlv,rv, &
dz8w2d,psfcpa, &
znt,ust,hpbl,dusfc,dvsfc,dtsfc,dqsfc,psim,psih, &
xland,hfx,qfx,wspd,br, &
dt,kpbl1d, &
exch_hx,exch_mx, &
wstar,delta, &
u10,v10, &
uox,vox, &
rthraten, &
ysu_topdown_pblmix, &
ctopo,ctopo2, &
a_u,a_v,a_t,a_q,a_e, &
b_u,b_v,b_t,b_q,b_e, &
sfk,vlk,dlu,dlg,frcurb, &
flag_bep, &
its,ite,kte,kme, &
errmsg,errflg &
)
!-------------------------------------------------------------------------------
implicit none
!-------------------------------------------------------------------------------
!
! this code is a revised vertical diffusion package ("ysupbl")
! with a nonlocal turbulent mixing in the pbl after "mrfpbl".
! the ysupbl (hong et al. 2006) is based on the study of noh
! et al.(2003) and accumulated realism of the behavior of the
! troen and mahrt (1986) concept implemented by hong and pan(1996).
! the major ingredient of the ysupbl is the inclusion of an explicit
! treatment of the entrainment processes at the entrainment layer.
! this routine uses an implicit approach for vertical flux
! divergence and does not require "miter" timesteps.
! it includes vertical diffusion in the stable atmosphere
! and moist vertical diffusion in clouds.
!
! mrfpbl:
! coded by song-you hong (ncep), implemented by jimy dudhia (ncar)
! fall 1996
!
! ysupbl:
! coded by song-you hong (yonsei university) and implemented by
! song-you hong (yonsei university) and jimy dudhia (ncar)
! summer 2002
!
! further modifications :
! an enhanced stable layer mixing, april 2008
! ==> increase pbl height when sfc is stable (hong 2010)
! pressure-level diffusion, april 2009
! ==> negligible differences
! implicit forcing for momentum with clean up, july 2009
! ==> prevents model blowup when sfc layer is too low
! incresea of lamda, maximum (30, 0.1 x del z) feb 2010
! ==> prevents model blowup when delz is extremely large
! revised prandtl number at surface, peggy lemone, feb 2010
! ==> increase kh, decrease mixing due to counter-gradient term
! revised thermal, shin et al. mon. wea. rev. , songyou hong, aug 2011
! ==> reduce the thermal strength when z1 < 0.1 h
! revised prandtl number for free convection, dudhia, mar 2012
! ==> pr0 = 1 + bke (=0.272) when neutral, kh is reduced
! minimum kzo = 0.01, lo = min (30m,delz), hong, mar 2012
! ==> weaker mixing when stable, and les resolution in vertical
! gz1oz0 is removed, and psim psih are ln(z1/z0)-psim,h, hong, mar 2012
! ==> consider thermal z0 when differs from mechanical z0
! a bug fix in wscale computation in stable bl, sukanta basu, jun 2012
! ==> wscale becomes small with height, and less mixing in stable bl
! revision in background diffusion (kzo), jan 2016
! ==> kzo = 0.1 for momentum and = 0.01 for mass to account for
! internal wave mixing of large et al. (1994), songyou hong, feb 2016
! ==> alleviate superious excessive mixing when delz is large
! add multilayer urban canopy models of BEP and BEP+BEM, jan 2021
!
! references:
!
! hendricks, knievel, and wang (2020), j. appl. meteor. clim.
! hong (2010) quart. j. roy. met. soc
! hong, noh, and dudhia (2006), mon. wea. rev.
! hong and pan (1996), mon. wea. rev.
! noh, chun, hong, and raasch (2003), boundary layer met.
! troen and mahrt (1986), boundary layer met.
!
!-------------------------------------------------------------------------------
!
real(kind=kind_phys),parameter :: xkzminm = 0.1,xkzminh = 0.01
real(kind=kind_phys),parameter :: xkzmin = 0.01,xkzmax = 1000.,rimin = -100.
real(kind=kind_phys),parameter :: rlam = 30.,prmin = 0.25,prmax = 4.
real(kind=kind_phys),parameter :: brcr_ub = 0.0,brcr_sb = 0.25,cori = 1.e-4
real(kind=kind_phys),parameter :: afac = 6.8,bfac = 6.8,pfac = 2.0,pfac_q = 2.0
real(kind=kind_phys),parameter :: phifac = 8.,sfcfrac = 0.1
real(kind=kind_phys),parameter :: d1 = 0.02, d2 = 0.05, d3 = 0.001
real(kind=kind_phys),parameter :: h1 = 0.33333335, h2 = 0.6666667
real(kind=kind_phys),parameter :: zfmin = 1.e-8,aphi5 = 5.,aphi16 = 16.
real(kind=kind_phys),parameter :: tmin=1.e-2
real(kind=kind_phys),parameter :: gamcrt = 3.,gamcrq = 2.e-3
real(kind=kind_phys),parameter :: xka = 2.4e-5
integer,parameter :: imvdif = 1
real(kind=kind_phys),parameter :: rcl = 1.0
integer,parameter :: kts=1, kms=1
!
integer, intent(in ) :: its,ite,kte,kme
logical, intent(in) :: ysu_topdown_pblmix
!
integer, intent(in) :: nmix
!
real(kind=kind_phys), intent(in ) :: dt,cp,g,rovcp,rovg,rd,xlv,rv
!
real(kind=kind_phys), intent(in ) :: ep1,ep2,karman
!
logical, intent(in ) :: f_qc, f_qi
!
real(kind=kind_phys), dimension( its:,: ) , &
intent(in) :: dz8w2d, &
pi2d
!
real(kind=kind_phys), dimension( its:,: ) , &
intent(in ) :: tx, &
qvx, &
qcx, &
qix
!
real(kind=kind_phys), dimension( its:,:,: ) , &
intent(in ) :: qmix
!
real(kind=kind_phys), dimension( its:,: ) , &
intent(out ) :: utnp, &
vtnp, &
ttnp, &
qvtnp, &
qctnp, &
qitnp
!
real(kind=kind_phys), dimension( its:,:,: ) , &
intent(out ) :: qmixtnp
!
real(kind=kind_phys), dimension( its:,: ) , &
intent(in ) :: p2di
!
real(kind=kind_phys), dimension( its:,: ) , &
intent(in ) :: p2d
!
real(kind=kind_phys), dimension( its: ) , &
intent(out ) :: hpbl
!
real(kind=kind_phys), dimension( its: ) , &
intent(out ), optional :: dusfc, &
dvsfc, &
dtsfc, &
dqsfc
!
real(kind=kind_phys), dimension( its: ) , &
intent(in ) :: ust, &
znt
real(kind=kind_phys), dimension( its: ) , &
intent(in ) :: xland, &
hfx, &
qfx
!
real(kind=kind_phys), dimension( its: ), intent(in ) :: wspd
real(kind=kind_phys), dimension( its: ), intent(in ) :: br
!
real(kind=kind_phys), dimension( its: ), intent(in ) :: psim, &
psih
!
real(kind=kind_phys), dimension( its: ), intent(in ) :: psfcpa
integer, dimension( its: ), intent(out ) :: kpbl1d
!
real(kind=kind_phys), dimension( its:,: ) , &
intent(in ) :: ux, &
vx, &
rthraten
real(kind=kind_phys), dimension( its: ) , &
optional , &
intent(in ) :: ctopo, &
ctopo2
!
logical, intent(in ) :: flag_bep
real(kind=kind_phys), dimension( its:,: ) , &
optional , &
intent(in ) :: a_u, &
a_v, &
a_t, &
a_q, &
a_e, &
b_u, &
b_v, &
b_t, &
b_q, &
b_e, &
sfk, &
vlk, &
dlu, &
dlg
real(kind=kind_phys), dimension( its: ) , &
optional , &
intent(in ) :: frcurb
!
character(len=*), intent(out) :: errmsg
integer, intent(out) :: errflg
!
! local vars
!
real(kind=kind_phys), dimension( its:ite ) :: hol
real(kind=kind_phys), dimension( its:ite, kms:kme ) :: zq
!
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: &
thx,thvx,thlix, &
del, &
dza, &
dzq, &
xkzom, &
xkzoh, &
za
!
real(kind=kind_phys), dimension( its:ite ) :: &
rhox, &
govrth, &
zl1,thermal, &
wscale, &
hgamt,hgamq, &
brdn,brup, &
phim,phih, &
prpbl, &
wspd1,thermalli
!
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: xkzh,xkzm,xkzq, &
f1,f2, &
r1,r2, &
ad,au, &
cu, &
al, &
zfac, &
rhox2, &
hgamt2, &
ad1,adm,adv
!
!jdf added exch_hx
!
real(kind=kind_phys), dimension( its:ite, kts:kte ) , &
intent(out ) :: exch_hx, &
exch_mx
!
real(kind=kind_phys), dimension( its:ite ) , &
intent(inout) :: u10, &
v10
real(kind=kind_phys), dimension( its:ite ), optional , &
intent(in ) :: uox, &
vox
real(kind=kind_phys), dimension( its:ite ) :: uoxl, &
voxl
real(kind=kind_phys), dimension( its:ite ) :: &
brcr, &
sflux, &
zol1, &
brcr_sbro
!
real(kind=kind_phys), dimension( its:ite, kts:kte) :: r3,f3
integer, dimension( its:ite ) :: kpbl,kpblold
!
logical, dimension( its:ite ) :: pblflg, &
sfcflg, &
stable, &
cloudflg
logical :: definebrup
!
integer :: n,i,k,l,ic,is,kk
integer :: klpbl
!
!
real(kind=kind_phys) :: dt2,rdt,spdk2,fm,fh,hol1,gamfac,vpert,prnum,prnum0
real(kind=kind_phys) :: ss,ri,qmean,tmean,alph,chi,zk,rl2,dk,sri
real(kind=kind_phys) :: brint,dtodsd,dtodsu,rdz,dsdzt,dsdzq,dsdz2,rlamdz
real(kind=kind_phys) :: utend,vtend,ttend,qtend
real(kind=kind_phys) :: dtstep,govrthv
real(kind=kind_phys) :: cont, conq, conw, conwrc
!
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: wscalek,wscalek2
real(kind=kind_phys), dimension( its:ite ), intent(out) :: wstar, &
delta
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: xkzml,xkzhl, &
zfacent,entfac
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: qcxl, &
qixl
real(kind=kind_phys), dimension( its:ite ) :: ust3, &
wstar3, &
wstar3_2, &
hgamu,hgamv, &
wm2, we, &
bfxpbl, &
hfxpbl,qfxpbl, &
ufxpbl,vfxpbl, &
dthvx
real(kind=kind_phys) :: prnumfac,bfx0,hfx0,qfx0,delb,dux,dvx, &
dsdzu,dsdzv,wm3,dthx,dqx,wspd10,ross,tem1,dsig,tvcon,conpr, &
prfac,prfac2,phim8z,radsum,tmp1,templ,rvls,temps,ent_eff, &
rcldb,bruptmp,radflux,vconvlim,vconvnew,fluxc,vconvc,vconv
!topo-corr
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: fric, &
tke_ysu,&
el_ysu,&
shear_ysu,&
buoy_ysu
real(kind=kind_phys), dimension( its:ite) :: pblh_ysu,&
vconvfx
!
real(kind=kind_phys) :: bepswitch
real(kind=kind_phys), dimension( its:ite, kts:kte ) :: &
a_u2d,a_v2d,a_t2d,a_q2d,a_e2d,b_u2d,b_v2d,b_t2d,b_q2d,b_e2d, &
sfk2d,vlk2d,dlu2d,dlg2d
real(kind=kind_phys), dimension( its:ite ) :: &
frc_urb1d
real(kind=kind_phys), dimension( kts:kte ) :: thvx_1d,tke_1d,dzq_1d
real(kind=kind_phys), dimension( kts:kte+1) :: zq_1d
!
!-------------------------------------------------------------------------------
!
klpbl = kte
!
cont=cp/g
conq=xlv/g
conw=1./g
conwrc = conw*sqrt(rcl)
conpr = bfac*karman*sfcfrac
!
! k-start index for tracer diffusion
!
if(f_qc) then
do k = kts,kte
do i = its,ite
qcxl(i,k) = qcx(i,k)
enddo
enddo
else
do k = kts,kte
do i = its,ite
qcxl(i,k) = 0.
enddo
enddo
endif
!
if(f_qi) then
do k = kts,kte
do i = its,ite
qixl(i,k) = qix(i,k)
enddo
enddo
else
do k = kts,kte
do i = its,ite
qixl(i,k) = 0.
enddo
enddo
endif
!
do k = kts,kte
do i = its,ite
thx(i,k) = tx(i,k)/pi2d(i,k)
thlix(i,k) = (tx(i,k)-xlv*qcxl(i,k)/cp-2.834E6*qixl(i,k)/cp)/pi2d(i,k)
enddo
enddo
!
do k = kts,kte
do i = its,ite
tvcon = (1.+ep1*qvx(i,k))
thvx(i,k) = thx(i,k)*tvcon
enddo
enddo
!
if ( present(uox) .and. present(vox) ) then
do i =its,ite
uoxl(i) = uox(i)
voxl(i) = vox(i)
enddo
else
do i =its,ite
uoxl(i) = 0
voxl(i) = 0
enddo
endif
!
do i = its,ite
tvcon = (1.+ep1*qvx(i,1))
rhox(i) = psfcpa(i)/(rd*tx(i,1)*tvcon)
govrth(i) = g/thx(i,1)
enddo
!
if(present(a_u) .and. present(a_v) .and. present(a_t) .and. &
present(a_q) .and. present(a_t) .and. present(a_e) .and. &
present(b_u) .and. present(b_v) .and. present(b_t) .and. &
present(b_q) .and. present(b_e) .and. present(dlg) .and. &
present(dlu) .and. present(sfk) .and. present(vlk) .and. &
present(frcurb) .and. flag_bep) then
bepswitch=1.0
do k = kts, kte
do i = its,ite
a_u2d(i,k) = a_u(i,k)
a_v2d(i,k) = a_v(i,k)
a_t2d(i,k) = a_t(i,k)
a_q2d(i,k) = a_q(i,k)
a_e2d(i,k) = a_e(i,k)
b_u2d(i,k) = b_u(i,k)
b_v2d(i,k) = b_v(i,k)
b_t2d(i,k) = b_t(i,k)
b_q2d(i,k) = b_q(i,k)
b_e2d(i,k) = b_e(i,k)
dlg2d(i,k) = dlg(i,k)
dlu2d(i,k) = dlu(i,k)
vlk2d(i,k) = vlk(i,k)
sfk2d(i,k) = sfk(i,k)
enddo
enddo
do i = its, ite
frc_urb1d(i) = frcurb(i)
enddo
else
bepswitch=0.0
do k = kts, kte
do i = its,ite
a_u2d(i,k) = 0.0
a_v2d(i,k) = 0.0
a_t2d(i,k) = 0.0
a_q2d(i,k) = 0.0
a_e2d(i,k) = 0.0
b_u2d(i,k) = 0.0
b_v2d(i,k) = 0.0
b_t2d(i,k) = 0.0
b_q2d(i,k) = 0.0
b_e2d(i,k) = 0.0
dlg2d(i,k) = 0.0
dlu2d(i,k) = 0.0
vlk2d(i,k) = 1.0
sfk2d(i,k) = 1.0
enddo
enddo
do i = its, ite
frc_urb1d(i) = 0.0
enddo
endif
!
!-----compute the height of full- and half-sigma levels above ground
! level, and the layer thicknesses.
!
do i = its,ite
zq(i,1) = 0.
enddo
!
do k = kts,kte
do i = its,ite
zq(i,k+1) = dz8w2d(i,k)+zq(i,k)
tvcon = (1.+ep1*qvx(i,k))
rhox2(i,k) = p2d(i,k)/(rd*tx(i,k)*tvcon)
enddo
enddo
!
do k = kts,kte
do i = its,ite
za(i,k) = 0.5*(zq(i,k)+zq(i,k+1))
dzq(i,k) = zq(i,k+1)-zq(i,k)
del(i,k) = p2di(i,k)-p2di(i,k+1)
enddo
enddo
!
do i = its,ite
dza(i,1) = za(i,1)
enddo
!
do k = kts+1,kte
do i = its,ite
dza(i,k) = za(i,k)-za(i,k-1)
enddo
enddo
!
!-----initialize output and local exchange coefficents:
do k = kts,kte
do i = its,ite
exch_hx(i,k) = 0.
exch_mx(i,k) = 0.
xkzh(i,k) = 0.
xkzhl(i,k) = 0.
xkzm(i,k) = 0.
xkzml(i,k) = 0.
xkzq(i,k) = 0.
enddo
enddo
!
do i = its,ite
wspd1(i) = sqrt( (ux(i,1)-uoxl(i))*(ux(i,1)-uoxl(i)) + (vx(i,1)-voxl(i))*(vx(i,1)-voxl(i)) )+1.e-9
enddo
!
!---- compute vertical diffusion
!
! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
! compute preliminary variables
!
dtstep = dt
dt2 = 2.*dtstep
rdt = 1./dt2
!
do i = its,ite
bfxpbl(i) = 0.0
hfxpbl(i) = 0.0
qfxpbl(i) = 0.0
ufxpbl(i) = 0.0
vfxpbl(i) = 0.0
hgamu(i) = 0.0
hgamv(i) = 0.0
delta(i) = 0.0
wstar3_2(i) = 0.0
enddo
!
do k = kts,klpbl
do i = its,ite
wscalek(i,k) = 0.0
wscalek2(i,k) = 0.0
enddo
enddo
!
do k = kts,klpbl
do i = its,ite
zfac(i,k) = 0.0
enddo
enddo
do k = kts,klpbl-1
do i = its,ite
xkzom(i,k) = xkzminm
xkzoh(i,k) = xkzminh
enddo
enddo
!
do i = its,ite
if(present(dusfc)) dusfc(i) = 0.
if(present(dvsfc)) dvsfc(i) = 0.
if(present(dtsfc)) dtsfc(i) = 0.
if(present(dqsfc)) dqsfc(i) = 0.
enddo
!
do i = its,ite
hgamt(i) = 0.
hgamq(i) = 0.
wscale(i) = 0.
we(i) = 0.
kpbl(i) = 1
hpbl(i) = zq(i,1)
zl1(i) = za(i,1)
thermal(i)= thvx(i,1)
thermalli(i) = thlix(i,1)
pblflg(i) = .true.
sfcflg(i) = .true.
sflux(i) = hfx(i)/rhox(i)/cp + qfx(i)/rhox(i)*ep1*thx(i,1)
if(br(i).gt.0.0) sfcflg(i) = .false.
enddo
!
! compute the first guess of pbl height
!
do i = its,ite
stable(i) = .false.
brup(i) = br(i)
brcr(i) = brcr_ub
enddo
!
do k = 2,klpbl
do i = its,ite
if(.not.stable(i))then
brdn(i) = brup(i)
spdk2 = max(ux(i,k)**2+vx(i,k)**2,1.)
brup(i) = (thvx(i,k)-thermal(i))*(g*za(i,k)/thvx(i,1))/spdk2
kpbl(i) = k
stable(i) = brup(i).gt.brcr(i)
endif
enddo
enddo
!
do i = its,ite
k = kpbl(i)
if(brdn(i).ge.brcr(i))then
brint = 0.
elseif(brup(i).le.brcr(i))then
brint = 1.
else
brint = (brcr(i)-brdn(i))/(brup(i)-brdn(i))
endif
hpbl(i) = za(i,k-1)+brint*(za(i,k)-za(i,k-1))
if(hpbl(i).lt.zq(i,2)) kpbl(i) = 1
if(kpbl(i).le.1) pblflg(i) = .false.
enddo
!
do i = its,ite
fm = psim(i)
fh = psih(i)
zol1(i) = max(br(i)*fm*fm/fh,rimin)
if(sfcflg(i))then
zol1(i) = min(zol1(i),-zfmin)
else
zol1(i) = max(zol1(i),zfmin)
endif
hol1 = zol1(i)*hpbl(i)/zl1(i)*sfcfrac
if(sfcflg(i))then
phim(i) = (1.-aphi16*hol1)**(-1./4.)
phih(i) = (1.-aphi16*hol1)**(-1./2.)
bfx0 = max(sflux(i),0.)
hfx0 = max(hfx(i)/rhox(i)/cp,0.)
qfx0 = max(ep1*thx(i,1)*qfx(i)/rhox(i),0.)
wstar3(i) = (govrth(i)*bfx0*hpbl(i))
wstar(i) = (wstar3(i))**h1
else
phim(i) = (1.+aphi5*hol1)
phih(i) = phim(i)
wstar(i) = 0.
wstar3(i) = 0.
endif
ust3(i) = ust(i)**3.
wscale(i) = (ust3(i)+phifac*karman*wstar3(i)*0.5)**h1
wscale(i) = min(wscale(i),ust(i)*aphi16)
wscale(i) = max(wscale(i),ust(i)/aphi5)
enddo
!
! compute the surface variables for pbl height estimation
! under unstable conditions
!
do i = its,ite
if(sfcflg(i).and.sflux(i).gt.0.0)then
gamfac = bfac/rhox(i)/wscale(i)
hgamt(i) = min(gamfac*hfx(i)/cp,gamcrt)
hgamq(i) = min(gamfac*qfx(i),gamcrq)
vpert = (hgamt(i)+ep1*thx(i,1)*hgamq(i))/bfac*afac
thermal(i) = thermal(i)+max(vpert,0.)*min(za(i,1)/(sfcfrac*hpbl(i)),1.0)
thermalli(i)= thermalli(i)+max(vpert,0.)*min(za(i,1)/(sfcfrac*hpbl(i)),1.0)
hgamt(i) = max(hgamt(i),0.0)
hgamq(i) = max(hgamq(i),0.0)
brint = -15.9*ust(i)*ust(i)/wspd(i)*wstar3(i)/(wscale(i)**4.)
hgamu(i) = brint*ux(i,1)
hgamv(i) = brint*vx(i,1)
else
pblflg(i) = .false.
endif
enddo
!
! enhance the pbl height by considering the thermal
!
do i = its,ite
if(pblflg(i))then
kpbl(i) = 1
hpbl(i) = zq(i,1)
endif
enddo
!
do i = its,ite
if(pblflg(i))then
stable(i) = .false.
brup(i) = br(i)
brcr(i) = brcr_ub
endif
enddo
!
do k = 2,klpbl
do i = its,ite
if(.not.stable(i).and.pblflg(i))then
brdn(i) = brup(i)
spdk2 = max(ux(i,k)**2+vx(i,k)**2,1.)
brup(i) = (thvx(i,k)-thermal(i))*(g*za(i,k)/thvx(i,1))/spdk2
kpbl(i) = k
stable(i) = brup(i).gt.brcr(i)
endif
enddo
enddo
!
! enhance pbl by theta-li
!
if (ysu_topdown_pblmix)then
do i = its,ite
kpblold(i) = kpbl(i)
definebrup=.false.
do k = kpblold(i), kte-1
spdk2 = max(ux(i,k)**2+vx(i,k)**2,1.)
bruptmp = (thlix(i,k)-thermalli(i))*(g*za(i,k)/thlix(i,1))/spdk2
stable(i) = bruptmp.ge.brcr(i)
if (definebrup) then
kpbl(i) = k
brup(i) = bruptmp
definebrup=.false.
endif
if (.not.stable(i)) then !overwrite brup brdn values
brdn(i)=bruptmp
definebrup=.true.
pblflg(i)=.true.
endif
enddo
enddo
endif
do i = its,ite
if(pblflg(i)) then
k = kpbl(i)
if(brdn(i).ge.brcr(i))then
brint = 0.
elseif(brup(i).le.brcr(i))then
brint = 1.
else
brint = (brcr(i)-brdn(i))/(brup(i)-brdn(i))
endif
hpbl(i) = za(i,k-1)+brint*(za(i,k)-za(i,k-1))
if(hpbl(i).lt.zq(i,2)) kpbl(i) = 1
if(kpbl(i).le.1) pblflg(i) = .false.
endif
enddo
!
! stable boundary layer
!
do i = its,ite
if((.not.sfcflg(i)).and.hpbl(i).lt.zq(i,2)) then
brup(i) = br(i)
stable(i) = .false.
else
stable(i) = .true.
endif
enddo
!
do i = its,ite
if((.not.stable(i)).and.((xland(i)-1.5).ge.0))then
wspd10 = u10(i)*u10(i) + v10(i)*v10(i)
wspd10 = sqrt(wspd10)
ross = wspd10 / (cori*znt(i))
brcr_sbro(i) = min(0.16*(1.e-7*ross)**(-0.18),.3)
endif
enddo
!
do i = its,ite
if(.not.stable(i))then
if((xland(i)-1.5).ge.0)then
brcr(i) = brcr_sbro(i)
else
brcr(i) = brcr_sb
endif
endif
enddo
!
do k = 2,klpbl
do i = its,ite
if(.not.stable(i))then
brdn(i) = brup(i)
spdk2 = max(ux(i,k)**2+vx(i,k)**2,1.)
brup(i) = (thvx(i,k)-thermal(i))*(g*za(i,k)/thvx(i,1))/spdk2
kpbl(i) = k
stable(i) = brup(i).gt.brcr(i)
endif
enddo
enddo
!
do i = its,ite
if((.not.sfcflg(i)).and.hpbl(i).lt.zq(i,2)) then
k = kpbl(i)
if(brdn(i).ge.brcr(i))then
brint = 0.
elseif(brup(i).le.brcr(i))then
brint = 1.
else
brint = (brcr(i)-brdn(i))/(brup(i)-brdn(i))
endif
hpbl(i) = za(i,k-1)+brint*(za(i,k)-za(i,k-1))
if(hpbl(i).lt.zq(i,2)) kpbl(i) = 1
if(kpbl(i).le.1) pblflg(i) = .false.
endif
enddo
!
! estimate the entrainment parameters
!
do i = its,ite
cloudflg(i)=.false.
if(pblflg(i)) then
k = kpbl(i) - 1
wm3 = wstar3(i) + 5. * ust3(i)
wm2(i) = wm3**h2
bfxpbl(i) = -0.15*thvx(i,1)/g*wm3/hpbl(i)
dthvx(i) = max(thvx(i,k+1)-thvx(i,k),tmin)
we(i) = max(bfxpbl(i)/dthvx(i),-sqrt(wm2(i)))
if((qcxl(i,k)+qixl(i,k)).gt.0.01e-3.and.ysu_topdown_pblmix)then
if ( kpbl(i) .ge. 2) then
cloudflg(i)=.true.
templ=thlix(i,k)*(p2di(i,k+1)/100000)**rovcp
!rvls is ws at full level
rvls=100.*6.112*EXP(17.67*(templ-273.16)/(templ-29.65))*(ep2/p2di(i,k+1))
temps=templ + ((qvx(i,k)+qcxl(i,k))-rvls)/(cp/xlv + &
ep2*xlv*rvls/(rd*templ**2))
rvls=100.*6.112*EXP(17.67*(temps-273.15)/(temps-29.65))*(ep2/p2di(i,k+1))
rcldb=max((qvx(i,k)+qcxl(i,k))-rvls,0.)
!entrainment efficiency
dthvx(i) = (thlix(i,k+2)+thx(i,k+2)*ep1*(qvx(i,k+2)+qcxl(i,k+2))) &
- (thlix(i,k) + thx(i,k) *ep1*(qvx(i,k) +qcxl(i,k)))
dthvx(i) = max(dthvx(i),0.1)
tmp1 = xlv/cp * rcldb/(pi2d(i,k)*dthvx(i))
ent_eff = 0.2 * 8. * tmp1 +0.2
radsum=0.
do kk = 1,kpbl(i)-1
radflux=rthraten(i,kk)*pi2d(i,kk) !converts theta/s to temp/s
radflux=radflux*cp/g*(p2di(i,kk)-p2di(i,kk+1)) ! converts temp/s to W/m^2
if (radflux < 0.0 ) radsum=abs(radflux)+radsum
enddo
radsum=max(radsum,0.0)
!recompute entrainment from sfc thermals
bfx0 = max(max(sflux(i),0.0)-radsum/rhox2(i,k)/cp,0.)
bfx0 = max(sflux(i),0.0)
wm3 = (govrth(i)*bfx0*hpbl(i))+5. * ust3(i)
wm2(i) = wm3**h2
bfxpbl(i) = -0.15*thvx(i,1)/g*wm3/hpbl(i)
dthvx(i) = max(thvx(i,k+1)-thvx(i,k),tmin)
we(i) = max(bfxpbl(i)/dthvx(i),-sqrt(wm2(i)))
!entrainment from PBL top thermals
bfx0 = max(radsum/rhox2(i,k)/cp-max(sflux(i),0.0),0.)
bfx0 = max(radsum/rhox2(i,k)/cp,0.)
wm3 = (g/thvx(i,k)*bfx0*hpbl(i)) ! this is wstar3(i)
wm2(i) = wm2(i)+wm3**h2
bfxpbl(i) = - ent_eff * bfx0
dthvx(i) = max(thvx(i,k+1)-thvx(i,k),0.1)
we(i) = we(i) + max(bfxpbl(i)/dthvx(i),-sqrt(wm3**h2))
!wstar3_2
bfx0 = max(radsum/rhox2(i,k)/cp,0.)
wstar3_2(i) = (g/thvx(i,k)*bfx0*hpbl(i))
!recompute hgamt
wscale(i) = (ust3(i)+phifac*karman*(wstar3(i)+wstar3_2(i))*0.5)**h1
wscale(i) = min(wscale(i),ust(i)*aphi16)
wscale(i) = max(wscale(i),ust(i)/aphi5)
gamfac = bfac/rhox(i)/wscale(i)
hgamt(i) = min(gamfac*hfx(i)/cp,gamcrt)
hgamq(i) = min(gamfac*qfx(i),gamcrq)
gamfac = bfac/rhox2(i,k)/wscale(i)
hgamt2(i,k) = min(gamfac*radsum/cp,gamcrt)
hgamt(i) = max(hgamt(i),0.0) + max(hgamt2(i,k),0.0)
brint = -15.9*ust(i)*ust(i)/wspd(i)*(wstar3(i)+wstar3_2(i))/(wscale(i)**4.)
hgamu(i) = brint*ux(i,1)
hgamv(i) = brint*vx(i,1)
endif
endif
prpbl(i) = 1.0
dthx = max(thx(i,k+1)-thx(i,k),tmin)
dqx = min(qvx(i,k+1)-qvx(i,k),0.0)
hfxpbl(i) = we(i)*dthx
qfxpbl(i) = we(i)*dqx
!
dux = ux(i,k+1)-ux(i,k)
dvx = vx(i,k+1)-vx(i,k)
if(dux.gt.tmin) then
ufxpbl(i) = max(prpbl(i)*we(i)*dux,-ust(i)*ust(i))
elseif(dux.lt.-tmin) then
ufxpbl(i) = min(prpbl(i)*we(i)*dux,ust(i)*ust(i))
else
ufxpbl(i) = 0.0
endif
if(dvx.gt.tmin) then
vfxpbl(i) = max(prpbl(i)*we(i)*dvx,-ust(i)*ust(i))
elseif(dvx.lt.-tmin) then
vfxpbl(i) = min(prpbl(i)*we(i)*dvx,ust(i)*ust(i))
else
vfxpbl(i) = 0.0
endif
delb = govrth(i)*d3*hpbl(i)
delta(i) = min(d1*hpbl(i) + d2*wm2(i)/delb,100.)
endif
enddo
!
do k = kts,klpbl
do i = its,ite
if(pblflg(i).and.k.ge.kpbl(i))then
entfac(i,k) = ((zq(i,k+1)-hpbl(i))/delta(i))**2.
else
entfac(i,k) = 1.e30
endif
enddo
enddo
!
! compute diffusion coefficients below pbl
!
do k = kts,klpbl
do i = its,ite
if(k.lt.kpbl(i)) then
zfac(i,k) = min(max((1.-(zq(i,k+1)-zl1(i))/(hpbl(i)-zl1(i))),zfmin),1.)
zfacent(i,k) = (1.-zfac(i,k))**3.
wscalek(i,k) = (ust3(i)+phifac*karman*wstar3(i)*(1.-zfac(i,k)))**h1
wscalek2(i,k) = (phifac*karman*wstar3_2(i)*(zfac(i,k)))**h1
if(sfcflg(i)) then
prfac = conpr
prfac2 = 15.9*(wstar3(i)+wstar3_2(i))/ust3(i)/(1.+4.*karman*(wstar3(i)+wstar3_2(i))/ust3(i))
prnumfac = -3.*(max(zq(i,k+1)-sfcfrac*hpbl(i),0.))**2./hpbl(i)**2.
else
prfac = 0.
prfac2 = 0.
prnumfac = 0.
phim8z = 1.+aphi5*zol1(i)*zq(i,k+1)/zl1(i)
wscalek(i,k) = ust(i)/phim8z
wscalek(i,k) = max(wscalek(i,k),0.001)
endif
prnum0 = (phih(i)/phim(i)+prfac)
prnum0 = max(min(prnum0,prmax),prmin)
xkzm(i,k) = wscalek(i,k) *karman* zq(i,k+1) * zfac(i,k)**pfac+ &
wscalek2(i,k)*karman*(hpbl(i)-zq(i,k+1))*(1-zfac(i,k))**pfac
!Do not include xkzm at kpbl-1 since it changes entrainment
if (k.eq.kpbl(i)-1.and.cloudflg(i).and.we(i).lt.0.0) then
xkzm(i,k) = 0.0
endif
prnum = 1. + (prnum0-1.)*exp(prnumfac)
xkzq(i,k) = xkzm(i,k)/prnum*zfac(i,k)**(pfac_q-pfac)
prnum0 = prnum0/(1.+prfac2*karman*sfcfrac)
prnum = 1. + (prnum0-1.)*exp(prnumfac)
xkzh(i,k) = xkzm(i,k)/prnum
xkzm(i,k) = xkzm(i,k)+xkzom(i,k)
xkzh(i,k) = xkzh(i,k)+xkzoh(i,k)
xkzq(i,k) = xkzq(i,k)+xkzoh(i,k)
xkzm(i,k) = min(xkzm(i,k),xkzmax)
xkzh(i,k) = min(xkzh(i,k),xkzmax)
xkzq(i,k) = min(xkzq(i,k),xkzmax)
endif
enddo
enddo
!
! compute diffusion coefficients over pbl (free atmosphere)
!
do k = kts,kte-1
do i = its,ite
if(k.ge.kpbl(i)) then
ss = ((ux(i,k+1)-ux(i,k))*(ux(i,k+1)-ux(i,k)) &
+(vx(i,k+1)-vx(i,k))*(vx(i,k+1)-vx(i,k))) &
/(dza(i,k+1)*dza(i,k+1))+1.e-9
govrthv = g/(0.5*(thvx(i,k+1)+thvx(i,k)))
ri = govrthv*(thvx(i,k+1)-thvx(i,k))/(ss*dza(i,k+1))
if(imvdif.eq.1)then
if((qcxl(i,k)+qixl(i,k)).gt.0.01e-3.and. &
(qcxl(i,k+1)+qixl(i,k+1)).gt.0.01e-3)then
! in cloud
qmean = 0.5*(qvx(i,k)+qvx(i,k+1))
tmean = 0.5*(tx(i,k)+tx(i,k+1))
alph = xlv*qmean/rd/tmean
chi = xlv*xlv*qmean/cp/rv/tmean/tmean
ri = (1.+alph)*(ri-g*g/ss/tmean/cp*((chi-alph)/(1.+chi)))