-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmass_flux.F90
1077 lines (889 loc) · 39.3 KB
/
mass_flux.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
module edmf
use physics_utils, only: rtype, rtype8, itype
use physconst, only: rgas => rair, cp => cpair, ggr => gravit, &
lcond => latvap, lice => latice, eps => zvir
use spmd_utils, only: masterproc
implicit none
private
public :: integrate_mf, calc_mf_vertflux, compute_tmpi3
public :: mf_readnl, mf_nup, mf_L0, mf_ent0, do_edmf, do_condensation, do_mf_diag, do_wthv_mf
real(rtype) :: mf_L0 = 0._rtype ! Default in namelist_defaults_cam.xml: 50 m
real(rtype) :: mf_ent0 = 0._rtype ! Default in namelist_defaults_cam.xml: 0.2
integer :: mf_nup = 0 ! Default in namelist_defaults_cam.xml: 10 plumes
real(rtype) :: mf_a = 0._rtype ! Default in namelist_defaults_cam.xml: 1
real(rtype) :: mf_b = 0._rtype ! Default in namelist_defaults_cam.xml: 0.5
real(rtype) :: mf_c = 0._rtype ! Default in namelist_defaults_cam.xml: 0.5
logical, protected :: do_edmf = .false.
logical, protected :: do_condensation = .false.
logical, protected :: do_mf_diag = .false.
logical, protected :: do_wthv_mf = .false.
contains
! TODO: Consider an "edmf_init" routine to initialize physical constants
! =============================================================================== !
! Eddy-diffusivity mass-flux routine !
! =============================================================================== !
subroutine mf_readnl(nlfile)
! =============================================================================== !
! MF namelists !
! =============================================================================== !
use namelist_utils, only: find_group_name
use cam_abortutils, only: endrun
use mpishorthand ! in spmd_utils.F90: mpiint, mpii8, mpichar, mpilog, mpipk, mpic16, mpir8, mpir4, mpicom, mpimax
character(len=*), intent(in) :: nlfile ! filepath for file containing namelist input
integer :: iunit, read_status
namelist /shoc_mf_nl/ mf_L0, mf_ent0, mf_nup, mf_a, mf_b, mf_c, do_edmf, do_condensation, do_mf_diag, do_wthv_mf
! Read namelist to determine if SHOC history should be called
if (masterproc) then
!iunit = getunit()
open( newunit=iunit, file=trim(nlfile), status='old' )
call find_group_name(iunit, 'shoc_mf_nl', status=read_status)
if (read_status == 0) then
read(iunit, nml=shoc_mf_nl, iostat=read_status)
if (read_status /= 0) then
call endrun('mf_readnl: error reading namelist')
end if
end if
close(iunit)
!close(unit=iunit)
!call freeunit(iunit)
end if
#ifdef SPMD
! Broadcast namelist variables
call mpibcast(mf_L0, 1, mpir8, 0, mpicom)
call mpibcast(mf_ent0, 1, mpir8, 0, mpicom)
call mpibcast(mf_nup, 1, mpiint, 0, mpicom)
call mpibcast(mf_a, 1, mpir8, 0, mpicom)
call mpibcast(mf_b, 1, mpir8, 0, mpicom)
call mpibcast(mf_c, 1, mpir8, 0, mpicom)
call mpibcast(do_edmf, 1, mpilog, 0, mpicom)
call mpibcast(do_condensation, 1, mpilog, 0, mpicom)
call mpibcast(do_mf_diag, 1, mpilog, 0, mpicom)
call mpibcast(do_wthv_mf, 1, mpilog, 0, mpicom)
#endif
!if ((.not. do_edmf) .and. do_mf_diag ) then
! call endrun('shoc_mf_nl: Error - cannot turn on do_mf_diag without also turning on do_edmf')
!end if
end subroutine mf_readnl
subroutine integrate_mf(shcol, nz, nzi, dt, & ! input
rho_zt_in, rho_zi_in, & ! input
zt_in, zi_in, dz_zt_in, p_in, thv_zi_in, & ! input
u_in, v_in, thl_in, thv_in, qt_in, & ! input
ust, wthl, wqt, qc_in, & ! input
pblh, pblh_wthl, wthv_sec_in, & ! input
dry_a_out, moist_a_out, & ! output: updraft properties for diagnostics
dry_w_out, moist_w_out, & ! output: updraft properties for diagnostics
dry_qt_out, moist_qt_out, & ! output: updraft properties for diagnostics
dry_thl_out, moist_thl_out, & ! output: updraft properties for diagnostics
dry_u_out, moist_u_out, & ! output: updraft properties for diagnostics
dry_v_out, moist_v_out, & ! output: updraft properties for diagnostics
moist_qc_out, & ! output: updraft properties for diagnostics
ae_out, aw_out, & ! output: variables needed for diffusion solver
awthv_out, & ! output: variable needed for total wthv
awthl_out, awqt_out, & ! output: variables needed for diffusion solver
awql_out, awqi_out, & ! output: variables needed for diffusion solver
awu_out, awv_out, & ! output: variables needed for diffusion solver
freq_dry, freq_moist, plumeheight, & ! output
plume_dry_height, ztop_B, cfl) ! output: frequency of plume activation (2D)
! ================================================================================= !
! Original author: Marcin Kurowski, JPL
! Modified heavily by Maria Chinita and Mikael Witte, UCLA/JPL/NPS for implementation in E3SM/SCREAM
! email: maria.j.chinita.candeias@jpl.nasa.gov
!
! Variables needed for solver:
! ae = sum_i (1-a_i)
! aw = sum (a_i w_i)
! awthl = sum(a_i w_i*thl_i)
! awqt = sum(a_i w_i*qt_i)
! awql,awqi,awu,awv similar to above except for different variables - not currently coupled to SHOC diffusion solver
!
!
! - mass flux variables are computed on edges (i.e. momentum grid):
! upa,upw,upqt,... 1:nzi
! dry_a,moist_a,dry_w,moist_w, ... 1:nzi
! ================================================================================= !
! ============================================================================== !
! INPUTS
! physics controls
integer, intent(in) :: shcol,nz,nzi
real(rtype), dimension(shcol,nz), intent(in) :: zt_in, dz_zt_in, rho_zt_in
real(rtype), dimension(shcol,nzi), intent(in) :: zi_in, p_in, thv_zi_in, rho_zi_in
real(rtype), dimension(shcol,nz), intent(in) :: u_in,v_in,thl_in,qt_in,qc_in,thv_in, wthv_sec_in ! all on thermodynamic/midpoint levels
real(rtype), dimension(shcol), intent(in) :: ust, wthl, wqt
real(rtype), dimension(shcol), intent(in) :: pblh, pblh_wthl
!time step [s]
real(rtype), intent(in) :: dt
! ============================================================================== !
! OUTPUTS
! updraft properties
real(rtype),dimension(shcol,nzi), intent(out) :: dry_a_out, moist_a_out, &
dry_w_out, moist_w_out, &
dry_qt_out, moist_qt_out, &
dry_thl_out, moist_thl_out, &
dry_u_out, moist_u_out, &
dry_v_out, moist_v_out, &
moist_qc_out
! variables needed for diffusion solver
real(rtype),dimension(shcol,nzi), intent(out) :: ae_out, aw_out, &
awthv_out, awthl_out, &
awqt_out, awql_out, &
awqi_out, awu_out, &
awv_out, cfl
! plume activation frequency
real(rtype),dimension(shcol), intent(out) :: freq_dry, freq_moist
! plume height from one plume test
real(rtype), dimension(shcol), intent(out) :: plumeheight, plume_dry_height, ztop_B
! ============================================================================== !
! INTERNAL VARIABLES
! flipped variables (i.e. here index 1 is at surface)
real(rtype), dimension(shcol,nz) :: zt, dz_zt, rho_zt
real(rtype), dimension(shcol,nzi) :: zi, p, thv_zi, rho_zi
real(rtype), dimension(shcol,nz) :: u, v, thl, qt, qc, thv, wthv_sec
! flipped updraft properties (i.e. index 1 is at surface)
real(rtype), dimension(shcol,nzi) :: dry_a, moist_a, &
dry_w, moist_w, &
dry_qt, moist_qt, &
dry_thl, moist_thl, &
dry_u, moist_u, &
dry_v, moist_v, &
moist_qc, &
dry_th, moist_th, &
ae, aw, &
awu, awv, &
awthv, awthl, &
awqt, awql, &
awqv, awth, &
awqi, awqc
! updraft properties
real(rtype), dimension(nzi,mf_nup) :: upw, upa, &
upthl, upthv, &
upth, &
upqt, upqc, &
upql, upqv, &
upqi, ups, &
upu, upv
! entrainment variables
real(rtype), dimension(nz,mf_nup) :: entf, ent
integer, dimension(nz,mf_nup) :: enti
real(rtype), dimension(nz) :: ent_oneplume
! other variables
integer :: k,j,i,index_top
real :: cfl_zt
real(rtype) :: wthv, wstar, qstar, thstar, &
sigmaw, sigmaqt, sigmath, z0, &
wmin, wmax, wlv, wtv, &
wp, wstar_aux
real(rtype) :: pbj, b, qtn, thln, &
thvn, thn, qcn, qln, &
qin, un, vn, wn2, &
entexp, entexpu, entw, iexh
real(rtype) :: plumeheight_aux
real(rtype), dimension(shcol) :: plume_top_height
! internal surface cont
real(rtype) :: dzt(nz)
! Dynamic L0 and ztop
real(rtype) :: dynamic_L0, ztop
! w parameters
! virtual mass coefficients for w-eqn after Suselj etal 2019
real(rtype),parameter :: wa = 1._rtype, &
wb = 1.5_rtype
! parameters defining initial conditions for updrafts
real(rtype),parameter :: pwmin = 1.5_rtype, &
pwmax = 3._rtype
! min values to avoid singularities
real(rtype),parameter :: wstarmin = 1.e-3_rtype, &
pblhmin = 100._rtype
! fixed entrainment rate (to debug only)
real(rtype),parameter :: fixent = 1.e-3_rtype
logical ::check
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!! BEGIN CODE !!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Flip vertical coordinates and all input variables
do k=1,nz
! thermodynamic grid variables
zt(:,k) = zt_in(:,nz-k+1)
dz_zt(:,k) = dz_zt_in(:,nz-k+1)
u(:,k) = u_in(:,nz-k+1)
v(:,k) = v_in(:,nz-k+1)
thl(:,k) = thl_in(:,nz-k+1)
thv(:,k) = thv_in(:,nz-k+1)
thv_zi(:,k) = thv_zi_in(:,nz-k+1)
qt(:,k) = qt_in(:,nz-k+1)
qc(:,k) = qc_in(:,nz-k+1)
wthv_sec(:,k) = wthv_sec_in(:,nz-k+1)
rho_zt(:,k) = rho_zt_in(:,nz-k+1)
rho_zi(:,k) = rho_zi_in(:,nz-k+1)
! momentum altitude grid
zi(:,k) = zi_in(:,nzi-k+1)
p(:,k) = p_in(:,nzi-k+1)
enddo
zi(:,nzi) = zi_in(:,1)
p(:,nzi) = p_in(:,1)
rho_zi(:,nzi) = rho_zi_in(:,1)
! INITIALIZE OUTPUT VARIABLES
! set updraft properties to zero
dry_a = 0._rtype
moist_a = 0._rtype
dry_w = 0._rtype
moist_w = 0._rtype
dry_qt = 0._rtype
moist_qt = 0._rtype
dry_thl = 0._rtype
moist_thl = 0._rtype
dry_u = 0._rtype
moist_u = 0._rtype
dry_v = 0._rtype
moist_v = 0._rtype
moist_qc = 0._rtype
moist_th = 0._rtype
dry_th = 0._rtype
! outputs - variables needed for solver
aw = 0._rtype
awthv = 0._rtype
awthl = 0._rtype
awqt = 0._rtype
awqc = 0._rtype
awqv = 0._rtype
awql = 0._rtype
awqi = 0._rtype
awu = 0._rtype
awv = 0._rtype
awth = 0._rtype
! this is the environmental area - by default 1.
ae = 1._rtype
! CFL number
cfl = 0._rtype
! START MAIN COMPUTATION
! NOTE: SHOC does not invert the vertical coordinate, which by default is ordered from lowest to highest pressure
! (i.e. top of atmosphere to bottom) so surface-based do loops are performed in reverse (i.e. from nz to 1)
do j=1,shcol
! zero out plume properties
upw = 0._rtype
upthl = 0._rtype
upthv = 0._rtype
upqt = 0._rtype
upa = 0._rtype
upu = 0._rtype
upv = 0._rtype
upqc = 0._rtype
ent = 0._rtype
upth = 0._rtype
upql = 0._rtype
upqi = 0._rtype
upqv = 0._rtype
! pblh variable is very off from the correct height according to eye estimation of the profiles
! let's calculate pblh here as
!the level of the minimum of qt gradient. Loop starts at 3 so we don't pick near
!surface values
! Using our PBL height:
pbj = max(pblh(j),pblhmin)
wthv = wthl(j)+eps*thv(j,1)*wqt(j)
! if surface buoyancy is positive then do mass-flux, otherwise not
if (wthv>0.0) then
dzt = dz_zt(j,:)
! calculate mixed layer height using wthv_sec profile (its minimum/inflection point)
ztop_B(j) = 0._rtype
do k=1,nz-1
if (zt(j,k) < 100._rtype) cycle
if (wthv_sec(j,k) < 0 .and. wthv_sec(j,k-1)>wthv_sec(j,k) .and. wthv_sec(j,k+1)>wthv_sec(j,k) ) then
ztop_B(j) = zt(j,k)
exit
endif
enddo
if (plume_dry_height(j) > 0._rtype .and. plume_dry_height(j) > pblhmin) then
pbj = plume_dry_height(j)
else
! For the first time step we don't have a plume_dry_height value
pbj = max(ztop_B(j),pblhmin)
endif
wstar = max( wstarmin, (ggr/thv(j,1)*wthv*pbj)**(1._rtype/3._rtype) )
qstar = wqt(j) / wstar
thstar = wthl(j) / wstar
sigmaw = 0.572_rtype * wstar
sigmaqt = 2.89_rtype * abs(qstar)
sigmath = 2.89_rtype * abs(thstar)
wmin = sigmaw * pwmin
wmax = sigmaw * pwmax
ent_oneplume = fixent
plumeheight_aux = 0._rtype
call oneplume( nz, nzi, zi(j,:), dzt, ent_oneplume, p(j,:), qt(j,:), thl(j,:), thv(j,:), &
thv_zi(j,:), wmax, wmin, sigmaw, sigmaqt, sigmath, wa, wb, &
do_condensation, plumeheight_aux)
plumeheight(j) = plumeheight_aux
! compute entrainment coefficient
! get dz/L0
ztop = max(pblh_wthl(j),ztop_B(j),pblhmin)
dynamic_L0 = mf_a*(ztop**mf_b)
do i=1,mf_nup
do k=1,nz
!entf(k,i) = dzt(k) / mf_L0
entf(k,i) = dzt(k) / dynamic_L0
enddo
enddo
call Poisson( nz, mf_nup, entf, enti, 69._rtype)
! entrainment: Ent=Ent0/dz*P(dz/L0)
do i=1,mf_nup
do k=1,nz
ent(k,i) = real( enti(k,i))*mf_ent0/dzt(k)
enddo
enddo
do i=1,mf_nup
! wlv = w_min_i
wlv = wmin + (wmax-wmin) / (real(mf_nup)) * (real(i)-1._rtype)
! wtv = w_max_i
wtv = wmin + (wmax-wmin) / (real(mf_nup)) * real(i)
! Surface vertical velocity of updraft i: w_i
upw(1,i) = 0.5_rtype * (wlv+wtv)
! Surface area of updraft i: a_i
upa(1,i) = 0.5_rtype * erf( wtv/(sqrt(2._rtype)*sigmaw) ) &
- 0.5_rtype * erf( wlv/(sqrt(2._rtype)*sigmaw) )
upu(1, i) = u(j,1)
upv(1, i) = v(j,1)
upqt(1,i) = qt(j,1) + 0.32_rtype * upw(1,i) * sigmaqt/sigmaw
upthv(1,i) = thv(j,1) + 0.58_rtype * upw(1,i) * sigmath/sigmaw
upthl(1,i) = upthv(1,i) / (1._rtype+eps*upqt(1,i))
upqv(1,i) = upqt(1,i)
if (do_condensation) then
iexh = (1.e5_rtype / p(j,1))**(rgas/cp)
call condensation_mf(upqt(1,i), upthl(1,i), p(j,1), iexh, &
thvn, qcn, thn, qln, qin)
upthv(1,i) = thvn
upqc(1,i) = qcn
upql(1,i) = qln
upqi(1,i) = qin
upth(1,i) = thn
else
upqc(1,i) = 0._rtype
upql(1,i) = 0._rtype
upqi(1,i) = 0._rtype
!upthv(1,i) = upthl(1,i)*(1._rtype+eps*upqt(1,i))
upth(1,i) = upthl(1,i)
end if
enddo
! Integrate updrafts
do i=1,mf_nup
do k=2,nzi
entexp = exp(-ent(k-1,i)*dzt(k-1))
entexpu = exp(-ent(k-1,i)*dzt(k-1)/3._rtype)
qtn = qt(j,k-1) *(1._rtype-entexp ) + upqt (k-1,i)*entexp
thln = thl(j,k-1)*(1._rtype-entexp ) + upthl(k-1,i)*entexp
un = u(j,k-1) *(1._rtype-entexpu) + upu (k-1,i)*entexpu
vn = v(j,k-1) *(1._rtype-entexpu) + upv (k-1,i)*entexpu
iexh = (1.e5_rtype / p(j,k))**(rgas/cp)
! Condensation within updrafts, input/output at full levels:
if (do_condensation) then
call condensation_mf(qtn, thln, p(j,k), iexh, &
thvn, qcn, thn, qln, qin)
else
thvn = thln*(1._rtype+eps*qtn)
thn = thln
qcn = 0._rtype
qin = 0._rtype
qln = 0._rtype
end if
! To avoid singularities w equation has to be computed diferently if wp==0
b=ggr*(0.5_rtype*(thvn+upthv(k-1,i))/thv(j,k-1)-1._rtype)
wp = wb*ent(k-1,i)
if (wp==0._rtype) then
wn2 = upw(k-1,i)**2._rtype+2._rtype*wa*b*dzt(k-1)
else
entw = exp(-2._rtype*wp*dzt(k-1))
wn2 = entw*upw(k-1,i)**2._rtype+wa*b/(wb*ent(k-1,i))*(1._rtype-entw)
end if
if (wn2>0._rtype) then
upw(k,i) = sqrt(wn2)
upthv(k,i) = thvn
upthl(k,i) = thln
upqt(k,i) = qtn
upqc(k,i) = qcn
upu(k,i) = un
upv(k,i) = vn
upa(k,i) = upa(k-1,i)
upth(k,i) = thn
upql(k,i) = qln
upqi(k,i) = qin
upqv(k,i) = qtn - qcn
else
exit
end if
enddo
enddo
! writing updraft properties for output
! all variables, except areas (moist_a and dry_a) are now multipled by the area
do k=1,nzi
! first sum over all i-updrafts
do i=1,mf_nup
if (upqc(k,i)>0._rtype) then
moist_a(j,k) = moist_a(j,k) + upa(k,i)
moist_w(j,k) = moist_w(j,k) + upa(k,i)*upw(k,i)
moist_qt(j,k) = moist_qt(j,k) + upa(k,i)*upqt(k,i)
moist_thl(j,k) = moist_thl(j,k) + upa(k,i)*upthl(k,i)
moist_th(j,k) = moist_th(j,k) + upa(k,i)*upth(k,i)
moist_u(j,k) = moist_u(j,k) + upa(k,i)*upu(k,i)
moist_v(j,k) = moist_v(j,k) + upa(k,i)*upv(k,i)
moist_qc(j,k) = moist_qc(j,k) + upa(k,i)*upqc(k,i)
else
dry_a(j,k) = dry_a(j,k) + upa(k,i)
dry_w(j,k) = dry_w(j,k) + upa(k,i)*upw(k,i)
dry_qt(j,k) = dry_qt(j,k) + upa(k,i)*upqt(k,i)
dry_thl(j,k) = dry_thl(j,k) + upa(k,i)*upthl(k,i)
dry_th(j,k) = dry_th(j,k) + upa(k,i)*upth(k,i)
dry_u(j,k) = dry_u(j,k) + upa(k,i)*upu(k,i)
dry_v(j,k) = dry_v(j,k) + upa(k,i)*upv(k,i)
endif
enddo
if ( dry_a(j,k) > 0._rtype ) then
dry_w(j,k) = dry_w(j,k) / dry_a(j,k)
dry_qt(j,k) = dry_qt(j,k) / dry_a(j,k)
dry_thl(j,k) = dry_thl(j,k) / dry_a(j,k)
dry_th(j,k) = dry_th(j,k) / dry_a(j,k)
dry_u(j,k) = dry_u(j,k) / dry_a(j,k)
dry_v(j,k) = dry_v(j,k) / dry_a(j,k)
else
dry_w(j,k) = 0._rtype
dry_qt(j,k) = 0._rtype
dry_thl(j,k) = 0._rtype
dry_th(j,k) = 0._rtype
dry_u(j,k) = 0._rtype
dry_v(j,k) = 0._rtype
endif
if ( moist_a(j,k) > 0._rtype ) then
moist_w(j,k) = moist_w(j,k) / moist_a(j,k)
moist_qt(j,k) = moist_qt(j,k) / moist_a(j,k)
moist_thl(j,k) = moist_thl(j,k) / moist_a(j,k)
moist_th(j,k) = moist_th(j,k) / moist_a(j,k)
moist_u(j,k) = moist_u(j,k) / moist_a(j,k)
moist_v(j,k) = moist_v(j,k) / moist_a(j,k)
moist_qc(j,k) = moist_qc(j,k) / moist_a(j,k)
else
moist_w(j,k) = 0._rtype
moist_qt(j,k) = 0._rtype
moist_thl(j,k) = 0._rtype
moist_th(j,k) = 0._rtype
moist_u(j,k) = 0._rtype
moist_v(j,k) = 0._rtype
moist_qc(j,k) = 0._rtype
endif
enddo
do k=1,nzi
do i=1,mf_nup
ae (j,k) = ae (j,k) - upa(k,i)
aw (j,k) = aw (j,k) + upa(k,i)*upw(k,i)
awu (j,k) = awu (j,k) + upa(k,i)*upw(k,i)*upu(k,i)
awv (j,k) = awv (j,k) + upa(k,i)*upw(k,i)*upv(k,i)
awthv(j,k)= awthv(j,k)+ upa(k,i)*upw(k,i)*upthv(k,i)
awthl(j,k)= awthl(j,k)+ upa(k,i)*upw(k,i)*upthl(k,i) !*cpair/iexh
awth(j,k) = awth(j,k) + upa(k,i)*upw(k,i)*upth(k,i) !*cpair/iexh
awqt(j,k) = awqt(j,k) + upa(k,i)*upw(k,i)*upqt(k,i)
awqc(j,k) = awqc(j,k) + upa(k,i)*upw(k,i)*upqc(k,i)
awqv(j,k) = awqv(j,k) + upa(k,i)*upw(k,i)*upqv(k,i)
awql(j,k) = awql(j,k) + upa(k,i)*upw(k,i)*upql(k,i)
awqi(j,k) = awqi(j,k) + upa(k,i)*upw(k,i)*upqi(k,i)
enddo
enddo
! Find highest vertical level where the plume ensemble is dry (i.e., moist_qc = 0)
check = .true.
plume_dry_height(j) = 0._rtype
index_top = 1
do k=1,nzi
if (check .and. aw(j,k) > 0._rtype .and. moist_qc(j,k) .EQ. 0._rtype) then
plume_dry_height(j) = zi(j,k)
index_top = k
else
check = .false.
endif
enddo
!! Highest vertical level reached by the moist plumes
!(analyzed from the top of the dry CBL given by plume_dry_height)
check = .true.
plume_top_height(j) = 0._rtype
do k=index_top+1,nzi
if (check .and. aw(j,k) > 0._rtype .and. moist_qc(j,k) > 0._rtype) then
plume_top_height(j) = zi(j,k)
else
check = .false.
endif
enddo
! Check CLF condition on mass-flux (aw)
do k=1,nz
cfl_zt = (2._rtype/dt)*rho_zt(j,k)*dz_zt(j,k)
if (zi(i,k) < ztop*1.5_rtype) then
if (aw(j,k) > (cfl_zt/rho_zi(j,k)) ) then
print*,'WARNING: aw > CFL'
print*,'aw(j,k) = ',aw(j,k)
print*,'CFL = ',cfl_zt/rho_zi(j,k)
print*,'k index = ',k
endif
endif
cfl(i,k) = cfl_zt/rho_zi(j,k)
enddo
end if ! ( wthv > 0.0 )
if (ANY(dry_a (j,:)>0._rtype)) freq_dry(j) = 1._rtype
if (ANY(moist_a(j,:)>0._rtype)) freq_moist(j) = 1._rtype
end do ! j=1,shcol
! flip output variables so index 1 = model top (i.e. lowest pressure)
do k=1,nzi
dry_a_out(:,nzi-k+1) = dry_a(:,k)
dry_w_out(:,nzi-k+1) = dry_w(:,k)
dry_qt_out(:,nzi-k+1) = dry_qt(:,k)
dry_thl_out(:,nzi-k+1) = dry_thl(:,k)
dry_u_out(:,nzi-k+1) = dry_u(:,k)
dry_v_out(:,nzi-k+1) = dry_v(:,k)
moist_a_out(:,nzi-k+1) = moist_a(:,k)
moist_w_out(:,nzi-k+1) = moist_w(:,k)
moist_qt_out(:,nzi-k+1) = moist_qt(:,k)
moist_thl_out(:,nzi-k+1) = moist_thl(:,k)
moist_u_out(:,nzi-k+1) = moist_u(:,k)
moist_v_out(:,nzi-k+1) = moist_v(:,k)
moist_qc_out(:,nzi-k+1) = moist_qc(:,k)
ae_out(:,nzi-k+1) = ae(:,k)
aw_out(:,nzi-k+1) = aw(:,k)
awthv_out(:,nzi-k+1) = awthv(:,k)
awthl_out(:,nzi-k+1) = awthl(:,k)
awqt_out(:,nzi-k+1) = awqt(:,k)
awql_out(:,nzi-k+1) = awql(:,k)
awqi_out(:,nzi-k+1) = awqi(:,k)
awu_out(:,nzi-k+1) = awu(:,k)
awv_out(:,nzi-k+1) = awv(:,k)
end do
end subroutine integrate_mf
subroutine oneplume( nz, nzi, zi, dzt, ent, p, qt, thl, thv, &
thv_zi, wmax, wmin, sigmaw, sigmaqt, sigmath, wa, wb, &
do_condensation, plumeheight )
!**********************************************************************
! Calculate a single plume with zero entrainment
! to be used for a dynamic mixing length calculation
! By Rachel Storer
!**********************************************************************
integer, intent(in) :: nz, nzi
real(rtype), intent(in) :: wmax, wmin, sigmaw, sigmaqt, sigmath, wa, wb
real(rtype), dimension(nz), intent(in) :: dzt, qt, thl, thv, ent
real(rtype), dimension(nzi), intent(in) :: zi, p, thv_zi
logical, intent(in) :: do_condensation
real(rtype), intent(inout) :: plumeheight
!local variables
integer :: k
real(rtype) :: thvn, qtn, thln, qcn, thn, qln, qin, wn2
real(rtype) :: iexh, entexp, entexpu, wp, entw
real(rtype) :: ztop
real(rtype), dimension(nzi) :: upw, upa, upqt, upthv, upthl, upth, &
upqc, upql, upqi, b, thvflx
thvflx = 0._rtype
b = 0._rtype
upw = 0._rtype
upthl = 0._rtype
upthv = 0._rtype
upqt = 0._rtype
upa = 0._rtype
upqc = 0._rtype
upth = 0._rtype
upql = 0._rtype
upqi = 0._rtype
upw(1) = 0.5_rtype * (wmax+wmin)
upa(1) = 0.5_rtype * erf( wmax/(sqrt(2.5_rtype)*sigmaw) ) &
- 0.5_rtype * erf( wmin/(sqrt(2._rtype)*sigmaw) )
upqt(1) = qt(1) + 0.32_rtype * upw(1) * sigmaqt/sigmaw
upthv(1) = thv(1) + 0.58_rtype * upw(1) * sigmath/sigmaw
upthl(1) = upthv(1) / (1._rtype+eps*upqt(1))
upth(1) = upthl(1)
! get cloud, lowest momentum level
if (do_condensation) then
iexh = (1.e5_rtype / p(1))**(rgas/cp)
call condensation_mf(upqt(1), upthl(1), p(1), iexh, &
thvn, qcn, thn, qln, qin)
upthv(1) = thvn
upqc(1) = qcn
upql(1) = qln
upqi(1) = qin
upth(1) = thn
else
! assume no cldliq
upthv(1) = upthl(1)*(1._rtype+eps*upqt(1))
upth(1) = upthl(1)
end if
do k=2,nzi
entexp = exp(-ent(k-1)*dzt(k-1))
entexpu = exp(-ent(k-1)*dzt(k-1)/3._rtype)
! integrate updraft
qtn = qt(k-1) *(1._rtype-entexp ) + upqt (k-1)*entexp
thln = thl(k-1)*(1._rtype-entexp ) + upthl(k-1)*entexp
! get cloud, momentum levels
if (do_condensation) then
iexh = (1.e5_rtype / p(k))**(rgas/cp)
call condensation_mf(qtn, thln, p(k), iexh, &
thvn, qcn, thn, qln, qin)
else
thvn = thln*(1._rtype+eps*qtn)
thn = thln ! THIS NEEEDS TO BE FIXED!! CALCULATED CORRECTLY
qcn = 0._rtype
qin = 0._rtype
qln = 0._rtype
end if
! get buoyancy
b(k)=ggr*(0.5_rtype*(thvn+upthv(k-1))/thv(k-1)-1._rtype)
wp = wb*ent(k-1)
if (wp==0._rtype) then
wn2 = upw(k-1)**2._rtype+2._rtype*wa*b(k)*dzt(k-1)
else
entw = exp(-2._rtype*wp*dzt(k-1))
wn2 = entw*upw(k-1)**2._rtype+wa*b(k)/(wb*ent(k-1))*(1._rtype-entw)
endif
if (wn2>0._rtype) then
upw(k) = sqrt(wn2)
upthv(k) = thvn
upthl(k) = thln
upqt(k) = qtn
upqc(k) = qcn
upa(k) = upa(k-1)
upql(k) = qln
upqi(k) = qin
upth(k) = thn
plumeheight = zi(k)
else
exit
end if
enddo
end subroutine oneplume
subroutine condensation_mf( qt, thl, p, iex, thv, qc, th, ql, qi)
!
! zero or one condensation for edmf: calculates thv and qc
!
use wv_saturation, only : qsat
real(rtype),intent(in) :: qt,thl,p,iex
real(rtype),intent(out):: thv,qc,th,ql,qi
!local variables
integer :: niter,i
real(rtype) :: diff,t,qs,qcold,es,wf
! max number of iterations
niter=50
! minimum difference
diff=2.e-5_rtype
qc=0._rtype
t=thl/iex
!by definition:
! T = Th*Exner, Exner=(p/p0)^(R/cp) (1)
! Thl = Th - L/cp*ql/Exner (2)
!so:
! Th = Thl + L/cp*ql/Exner (3)
! T = Th*Exner=(Thl+L/cp*ql/Exner)*Exner (4)
! = Thl*Exner + L/cp*ql
do i=1,niter
wf = get_watf(t)
t = thl/iex+get_alhl(wf)/cp*qc !as in (4)
! qsat, p is in pascal (check!)
call qsat(t,p,es,qs)
qcold = qc
qc = max(0.5_rtype*qc+0.5_rtype*(qt-qs),0._rtype)
if (abs(qc-qcold)<diff) exit
enddo
wf = get_watf(t)
t = thl/iex+get_alhl(wf)/cp*qc
call qsat(t,p,es,qs)
qc = max(qt-qs,0._rtype)
thv = (thl+get_alhl(wf)/cp*iex*qc)*(1.+eps*(qt-qc)-qc)
th = t*iex
qi = qc*(1.-wf)
ql = qc*wf
contains
function get_watf(t)
real(rtype) :: t,get_watf,tc
real(rtype), parameter :: &
tmax=-10._rtype, &
tmin=-40._rtype
tc=t-273.16_rtype
if (tc>tmax) then
get_watf=1._rtype
else if (tc<tmin) then
get_watf=0._rtype
else
get_watf=(tc-tmin)/(tmax-tmin);
end if
end function get_watf
function get_alhl(wf)
!latent heat of the mixture based on water fraction
real(rtype) :: get_alhl,wf
get_alhl = wf*lcond+(1._rtype-wf)*(lcond+lice)
end function get_alhl
end subroutine condensation_mf
subroutine calc_mf_vertflux(shcol,nlev,nlevi,aw,awvar,var,var_zi,varflx)
implicit none
! INPUT VARIABLES
! number of SHOC columns
integer, intent(in) :: shcol
! number of midpoint levels
integer, intent(in) :: nlev
! number of interface levels
integer, intent(in) :: nlevi
! Sum plume (a_i*w_i) [m/s]
real(rtype), intent(in) :: aw(shcol,nlevi)
! Sum plume vertical flux of generic variable var (a_i*w_i*var_i) [units vary]
real(rtype), intent(in) :: awvar(shcol,nlevi)
! Input variable on thermo/full grid [units vary]
real(rtype), intent(in) :: var_zi(shcol,nlevi) ! NOTE: var is interpolated to zi, so has dim nzi
real(rtype), intent(in) :: var(shcol,nlev)
! OUTPUT VARIABLE
real(rtype), intent(out) :: varflx(shcol,nlevi)
! INTERNAL VARIABLES
integer :: i,k
! diagnose MF fluxes
varflx(:shcol,1) = 0._rtype
do k=2,nlev
do i=1,shcol
varflx(i,k)= awvar(i,k) - aw(i,k)*0.5*(var(i,k-1)+var(i,k)) ! centered differences
!varflx(i,k)= awvar(i,k) - aw(i,k)*var(i,k) ! upwind scheme (in reference to the surface)
!varflx(i,k)= awvar(i,k) - aw(i,k)*var(i,k-1) ! downwind scheme (in reference to the surface)
!varflx(i,k)= awvar(i,k) - aw(i,k)*var_zi(i,k)
end do
end do
varflx(:shcol,nlevi) = 0._rtype
end subroutine calc_mf_vertflux
subroutine compute_tmpi3(nlevi, shcol, dtime, rho_zi, tmpi3)
!intent-ins
integer, intent(in) :: nlevi, shcol
!time step [s]
real(rtype), intent(in) :: dtime
!air density at interfaces [kg/m3]
real(rtype), intent(in) :: rho_zi(shcol,nlevi)
!intent-out
real(rtype), intent(out) :: tmpi3(shcol,nlevi)
!local vars
integer :: i, k
tmpi3(:,1) = 0._rtype
! eqn: tmpi3 = dt*g*rho
do k = 2, nlevi
do i = 1, shcol
tmpi3(i,k) = dtime * ggr*rho_zi(i,k)
enddo
enddo
end subroutine compute_tmpi3
subroutine Poisson(nz,nup,mu,POI,seed)
use time_manager, only: is_first_step
implicit none
integer, intent(in) :: nz,nup
real(rtype),dimension(nz,nup),intent(in) :: MU
integer, dimension(nz,nup), intent(out) :: POI
real(rtype), intent(in) :: seed
integer :: seed_len,i,j
integer,allocatable:: the_seed(:)
!!
integer, dimension(nz,nup) :: poi_aux
if (is_first_step()) then
call random_seed(SIZE=seed_len)
allocate(the_seed(seed_len))
the_seed(1) = int((seed - int(seed)) * 1000000000._rtype)
if (seed_len > 1) the_seed(2:) = the_seed(1)
call random_seed(put=the_seed)
end if
do i=1,nz
do j=1,nup
poi(i,j) = int(poidev(mu(i,j)))
enddo
enddo
end subroutine Poisson
FUNCTION poidev(xm)
IMPLICIT NONE
REAL(rtype), INTENT(IN) :: xm
REAL(rtype) :: poidev
REAL(rtype), PARAMETER :: PI=3.141592653589793238462643383279502884197_rtype
!Returns as a floating-point number an integer value that is a random deviate drawn from a
!Poisson distribution of mean xm, using ran1 as a source of uniform random deviates.
REAL(rtype) :: em,harvest,t,y
REAL(rtype), SAVE :: alxm,g,oldm=-1.0_rtype,sq
!Begin code
!oldm is a flag for whether xm has changed since last call.
if (xm < 12.0) then !Use direct method.
if (xm /= oldm) then
oldm=xm
g=exp(-xm) !If xm is new, compute the exponential.
end if
em=-1
t=1.0_rtype
do
em=em+1.0_rtype !Instead of adding exponential deviates it is
!equivalent to multiply uniform deviates.
!We never actually have to take the log;
!merely compare to the pre-computed exponential.
call random_number(harvest)
t=t*harvest
if (t <= g) exit
end do
else ! Use rejection method.
if (xm /= oldm) then !If xm has changed since the last call, then precompute
!some functions that occur below.
oldm=xm
sq=sqrt(2.0_rtype*xm)
alxm=log(xm)
g=xm*alxm-gammln_s(xm+1.0_rtype) ! The function gammln is the natural log of the
! gamma function, as given in §6.1.
end if
do