forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advance_wp2_wp3_module.F90
5943 lines (4745 loc) · 224 KB
/
advance_wp2_wp3_module.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
!------------------------------------------------------------------------
! $Id$
!===============================================================================
module advance_wp2_wp3_module
implicit none
private ! Default Scope
public :: advance_wp2_wp3
private :: wp23_solve, &
wp23_lhs, &
wp23_rhs, &
wp2_term_ta_lhs, &
wp2_terms_ac_pr2_lhs, &
wp2_term_dp1_lhs, &
wp2_term_pr1_lhs, &
wp2_terms_bp_pr2_rhs, &
wp2_term_dp1_rhs, &
wp2_term_pr3_rhs, &
wp2_term_pr1_rhs, &
wp3_term_ta_new_pdf_lhs, &
wp3_term_ta_ADG1_lhs, &
wp3_term_tp_lhs, &
wp3_terms_ac_pr2_lhs, &
wp3_term_pr1_lhs, &
wp3_term_ta_explicit_rhs, &
wp3_terms_bp1_pr2_rhs, &
wp3_term_pr1_rhs, &
wp3_term_bp2_rhs, &
wp2_term_ta_lhs_all, &
wp2_terms_ac_pr2_lhs_all, &
wp2_term_dp1_lhs_all, &
wp2_term_pr1_lhs_all, &
wp2_terms_bp_pr2_rhs_all, &
wp2_term_dp1_rhs_all, &
wp2_term_pr3_rhs_all, &
wp2_term_pr1_rhs_all, &
wp3_term_ta_new_pdf_lhs_all, &
wp3_term_ta_ADG1_lhs_all, &
wp3_term_tp_lhs_all, &
wp3_terms_ac_pr2_lhs_all, &
wp3_term_pr1_lhs_all, &
wp3_term_ta_explicit_rhs_all, &
wp3_terms_bp1_pr2_rhs_all, &
wp3_term_pr1_rhs_all, &
wp3_term_bp2_rhs_all
! Private named constants to avoid string comparisons
integer, parameter, private :: &
clip_wp2 = 12 ! Named constant for wp2 clipping.
! NOTE: This must be the same as the clip_wp2 declared in
! clip_explicit!
contains
!=============================================================================
subroutine advance_wp2_wp3( dt, sfc_elevation, sigma_sqd_w, wm_zm, & ! In
wm_zt, a3, a3_zt, wp3_on_wp2, wp4, & ! In
wpthvp, wp2thvp, um, vm, upwp, vpwp, & ! In
up2, vp2, Kh_zm, Kh_zt, tau_zm, tau_zt, & ! In
tau_C1_zm, Skw_zm, Skw_zt, rho_ds_zm, & ! In
rho_ds_zt, invrs_rho_ds_zm, & ! In
invrs_rho_ds_zt, radf, thv_ds_zm, & ! In
thv_ds_zt, mixt_frac, Cx_fnc_Richardson, & ! In
wp2_splat, wp3_splat, & ! intent(in)
pdf_implicit_coefs_terms, & ! In
wprtp, wpthlp, rtp2, thlp2, & ! In
wp2, wp3, wp3_zm, wp2_zt ) ! Inout
! Description:
! Advance w'^2 and w'^3 one timestep.
! References:
! https://arxiv.org/pdf/1711.03675v1.pdf#nameddest=url:wp2_wp3_eqns
!
! Eqn. 12 & 18 on p. 3545--3546 of
! ``A PDF-Based Model for Boundary Layer Clouds. Part I:
! Method and Model Description'' Golaz, et al. (2002)
! JAS, Vol. 59, pp. 3540--3551.
! See also
! ``Equations for CLUBB'', Section 6:
! /Implict solution for the vertical velocity moments/
!------------------------------------------------------------------------
use grid_class, only: &
gr, & ! Variable(s)
zt2zm, & ! Procedure(s)
zm2zt
use parameters_tunable, only: &
C11c, & ! Variable(s)
C11b, &
C11, &
C1c, &
C1b, &
C1, &
c_K1, &
c_K8
use sponge_layer_damping, only: &
wp2_sponge_damp_settings, & ! Variable(s)
wp3_sponge_damp_settings, &
wp2_sponge_damp_profile, &
wp3_sponge_damp_profile, &
sponge_damp_xp2, & ! Procedure(s)
sponge_damp_xp3
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_end_update, &
stat_update_var
use stats_variables, only: &
iC1_Skw_fnc, & ! Variable(s)
iC11_Skw_fnc, &
iwp2_sdmp, &
iwp3_sdmp, &
stats_zm, &
stats_zt, &
l_stats_samp
use constants_clubb, only: &
fstderr, & ! Variables
one, &
one_half, &
one_third, &
w_tol_sqd, &
eps
use pdf_parameter_module, only: &
implicit_coefs_terms ! Variable Type
use clubb_precision, only: &
core_rknd ! Variable(s)
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constant
use model_flags, only: &
l_damp_wp2_using_em, & ! Logical(s)
l_use_C11_Richardson
implicit none
intrinsic :: exp
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep [s]
real( kind = core_rknd ), intent(in) :: &
sfc_elevation ! Elevation of ground level [m AMSL]
real( kind = core_rknd ), intent(in), dimension(gr%nz) :: &
sigma_sqd_w, & ! sigma_sqd_w (momentum levels) [-]
wm_zm, & ! w wind component on momentum levels [m/s]
wm_zt, & ! w wind component on thermodynamic levels [m/s]
a3, & ! a_3 (momentum levels); See eqn. 25 in `Equations for CLUBB' [-]
a3_zt, & ! a_3 interpolated to thermodynamic levels [-]
wp3_on_wp2, & ! Smoothed version of wp3 / wp2 [m/s]
wp4, & ! w'^4 (momentum levels) [m^4/s^4]
wpthvp, & ! w'th_v' (momentum levels) [K m/s]
wp2thvp, & ! w'^2th_v' (thermodynamic levels) [K m^2/s^2]
um, & ! u wind component (thermodynamic levels) [m/s]
vm, & ! v wind component (thermodynamic levels) [m/s]
upwp, & ! u'w' (momentum levels) [m^2/s^2]
vpwp, & ! v'w' (momentum levels) [m^2/s^2]
up2, & ! u'^2 (momentum levels) [m^2/s^2]
vp2, & ! v'^2 (momentum levels) [m^2/s^2]
Kh_zm, & ! Eddy diffusivity on momentum levels [m^2/s]
Kh_zt, & ! Eddy diffusivity on thermodynamic levels [m^2/s]
tau_zm, & ! Time-scale tau on momentum levels [s]
tau_zt, & ! Time-scale tau on thermodynamic levels [s]
tau_C1_zm, & ! Tau values used for the C1 (dp1) term in wp2 [s]
Skw_zm, & ! Skewness of w on momentum levels [-]
Skw_zt, & ! Skewness of w on thermodynamic levels [-]
rho_ds_zm, & ! Dry, static density on momentum levels [kg/m^3]
rho_ds_zt, & ! Dry, static density on thermo. levels [kg/m^3]
invrs_rho_ds_zm, & ! Inv. dry, static density @ momentum levs. [m^3/kg]
invrs_rho_ds_zt, & ! Inv. dry, static density @ thermo. levs. [m^3/kg]
radf, & ! Buoyancy production at the CL top [m^2/s^3]
thv_ds_zm, & ! Dry, base-state theta_v on momentum levs. [K]
thv_ds_zt, & ! Dry, base-state theta_v on thermo. levs. [K]
mixt_frac, & ! Weight of 1st normal distribution [-]
wprtp, & ! Flux of total water mixing ratio [m/s kg/kg]
wpthlp, & ! Flux of liquid water potential temp. [m/s K]
rtp2, & ! Variance of rt (overall) [kg^2/kg^2]
thlp2, & ! Variance of thl (overall) [K^2]
Cx_fnc_Richardson, & ! Cx_fnc from Richardson_num [-]
wp2_splat, & ! Tendency of <w'2> due to vertical compression of eddies [m^2/s^3]
wp3_splat ! Tendency of <w'3> due to vertical compression of eddies [m^3/s^4]
type(implicit_coefs_terms), dimension(gr%nz), intent(in) :: &
pdf_implicit_coefs_terms ! Implicit coefs / explicit terms [units vary]
! Input/Output
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
wp2, & ! w'^2 (momentum levels) [m^2/s^2]
wp3, & ! w'^3 (thermodynamic levels) [m^3/s^3]
wp3_zm ! w'^3 interpolated to momentum levels [m^3/s^3]
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
wp2_zt ! w'^2 interpolated to thermodyamic levels [m^2/s^2]
! Local Variables
real( kind = core_rknd ), dimension(gr%nz) :: &
tauw3t ! Currently just tau_zt [s]
! Eddy Diffusion for w'^2 and w'^3.
real( kind = core_rknd ), dimension(gr%nz) :: Kw1 ! w'^2 coef. eddy diff. [m^2/s]
real( kind = core_rknd ), dimension(gr%nz) :: Kw8 ! w'^3 coef. eddy diff. [m^2/s]
! Internal variables for C11 function, Vince Larson 13 Mar 2005
! Brian added C1 function.
real( kind = core_rknd ), dimension(gr%nz) :: &
C1_Skw_fnc, & ! C_1 parameter with Sk_w applied [-]
C11_Skw_fnc, & ! C_11 parameter with Sk_w applied [-]
! End Vince Larson's addition.
C16_fnc ! C_16 parameter [-]
integer :: k ! Array indices
!-----------------------------------------------------------------------
! Define tauw
! tauw3t = tau_zt
! . / ( 1.
! . + 3.0_core_rknd * max(
! . min(1.-(mixt_frac-0.01_core_rknd)/(0.05_core_rknd-0.01_core_rknd)
! . ,1.)
! . ,0.)
! . + 3.0_core_rknd * max(
! . min(1.-(mixt_frac-0.99_core_rknd)/(0.95_core_rknd-0.99_core_rknd)
! . ,1.)
! . ,0.)
! . )
! do k=1,gr%nz
!
! Skw = abs( wp3(k)/max(wp2(k),1.e-8)**1.5_core_rknd )
! Skw = min( 5.0_core_rknd, Skw )
! tauw3t(k) = tau_zt(k) / ( 0.005_core_rknd*Skw**4 + one )
!
! end do
tauw3t = tau_zt
! Vince Larson added code to make C11 function of Skw. 13 Mar 2005
! If this code is used, C11 is no longer relevant, i.e. constants
! are hardwired.
if ( l_use_C11_Richardson ) then
C11_Skw_fnc = Cx_fnc_Richardson
else
! Calculate C_{1} and C_{11} as functions of skewness of w.
! The if..then here is only for computational efficiency -dschanen 2 Sept 08
if ( abs(C11-C11b) > abs(C11+C11b)*eps/2 ) then
C11_Skw_fnc(1:gr%nz) = &
C11b + (C11-C11b)*EXP( -one_half * (Skw_zt(1:gr%nz)/C11c)**2 )
else
C11_Skw_fnc(1:gr%nz) = C11b
end if
end if ! l_use_C11_Richardson
! The if..then here is only for computational efficiency -dschanen 2 Sept 08
if ( abs(C1-C1b) > abs(C1+C1b)*eps/2 ) then
C1_Skw_fnc(1:gr%nz) = &
C1b + (C1-C1b)*EXP( -one_half * (Skw_zm(1:gr%nz)/C1c)**2 )
else
C1_Skw_fnc(1:gr%nz) = C1b
end if
if ( l_damp_wp2_using_em ) then
! Insert 1/3 here to account for the fact that in the dissipation term,
! (2/3)*em = (2/3)*(1/2)*(wp2+up2+vp2). Then we can insert wp2, up2,
! and vp2 directly into the dissipation subroutines without prefixing them by (1/3).
C1_Skw_fnc(1:gr%nz) = one_third * C1_Skw_fnc(1:gr%nz)
end if
!C11_Skw_fnc = C11
!C1_Skw_fnc = C1
! Set C16_fnc based on Richardson_num
C16_fnc = Cx_fnc_Richardson
if ( clubb_at_least_debug_level( 0 ) ) then
! Assertion check for C11_Skw_fnc
if ( any( C11_Skw_fnc(:) > one ) .or. any( C11_Skw_fnc(:) < 0._core_rknd ) ) then
write(fstderr,*) "The C11_Skw_fnc is outside the valid range for this variable"
err_code = clubb_fatal_error
return
end if
if ( any( C16_fnc(:) > one ) .or. any( C16_fnc(:) < 0._core_rknd ) ) then
write(fstderr,*) "The C16_fnc is outside the valid range for this variable"
err_code = clubb_fatal_error
return
end if
end if
if ( l_stats_samp ) then
call stat_update_var( iC11_Skw_fnc, C11_Skw_fnc, stats_zt )
call stat_update_var( iC1_Skw_fnc, C1_Skw_fnc, stats_zm )
endif
! Define the Coefficent of Eddy Diffusivity for the wp2 and wp3.
do k = 1, gr%nz, 1
! Kw1 is used for wp2, which is located on momentum levels.
! Kw1 is located on thermodynamic levels.
! Kw1 = c_K1 * Kh_zt
Kw1(k) = c_K1 * Kh_zt(k)
! Kw8 is used for wp3, which is located on thermodynamic levels.
! Kw8 is located on momentum levels.
! Note: Kw8 is usually defined to be 1/2 of Kh_zm.
! Kw8 = c_K8 * Kh_zm
Kw8(k) = c_K8 * Kh_zm(k)
enddo
! Solve semi-implicitly
call wp23_solve( dt, sfc_elevation, sigma_sqd_w, wm_zm, & ! Intent(in)
wm_zt, a3, a3_zt, wp3_on_wp2, wp4, & ! Intent(in)
wpthvp, wp2thvp, um, vm, upwp, vpwp, & ! Intent(in)
up2, vp2, Kw1, Kw8, Kh_zt, Skw_zt, & ! Intent(in)
tau_zm, tauw3t, tau_C1_zm, C1_Skw_fnc, & ! Intent(in)
C11_Skw_fnc, C16_fnc, rho_ds_zm, & ! Intent(in)
rho_ds_zt, invrs_rho_ds_zm, & ! Intent(in)
invrs_rho_ds_zt, radf, & ! Intent(in)
thv_ds_zm, thv_ds_zt, & ! Intent(in)
wp2_splat, wp3_splat, & ! Intent(in)
pdf_implicit_coefs_terms, & ! Intent(in)
wprtp, wpthlp, rtp2, thlp2, & ! Intent(in)
wp2, wp3, wp3_zm, wp2_zt ) ! Intent(inout)
! When selected, apply sponge damping after wp2 and wp3 have been advanced.
if ( wp2_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( iwp2_sdmp, wp2 / dt, stats_zm )
endif
wp2 = sponge_damp_xp2( dt, gr%zm, wp2, w_tol_sqd, &
wp2_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( iwp2_sdmp, wp2 / dt, stats_zm )
endif
endif ! wp2_sponge_damp_settings%l_sponge_damping
if ( wp3_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( iwp3_sdmp, wp3 / dt, stats_zt )
endif
wp3 = sponge_damp_xp3( dt, gr%zt, wp3, wp3_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( iwp3_sdmp, wp3 / dt, stats_zt )
endif
endif ! wp3_sponge_damp_settings%l_sponge_damping
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "Error in advance_wp2_wp3"
write(fstderr,*) "Intent(in)"
write(fstderr,*) "gr%zt = ", gr%zt, new_line('c')
write(fstderr,*) "dt = ", dt, new_line('c')
write(fstderr,*) "sfc_elevation = ", sfc_elevation, new_line('c')
write(fstderr,*) "sigma_sqd_w = ", sigma_sqd_w, new_line('c')
write(fstderr,*) "wm_zm = ", wm_zm, new_line('c')
write(fstderr,*) "wm_zt = ", wm_zt, new_line('c')
write(fstderr,*) "wp4 = ", wp4, new_line('c')
write(fstderr,*) "wpthvp = ", wpthvp, new_line('c')
write(fstderr,*) "wp2thvp = ", wp2thvp, new_line('c')
write(fstderr,*) "um = ", um, new_line('c')
write(fstderr,*) "vm = ", vm, new_line('c')
write(fstderr,*) "upwp = ", upwp, new_line('c')
write(fstderr,*) "vpwp = ", vpwp, new_line('c')
write(fstderr,*) "up2 = ", up2, new_line('c')
write(fstderr,*) "vp2 = ", vp2, new_line('c')
write(fstderr,*) "Kh_zm = ", Kh_zm, new_line('c')
write(fstderr,*) "Kh_zt = ", Kh_zt, new_line('c')
write(fstderr,*) "tau_zm = ", tau_zm, new_line('c')
write(fstderr,*) "tau_zt = ", tau_zt, new_line('c')
write(fstderr,*) "Skw_zm = ", Skw_zm, new_line('c')
write(fstderr,*) "Skw_zt = ", Skw_zt, new_line('c')
write(fstderr,*) "mixt_frac = ", mixt_frac, new_line('c')
write(fstderr,*) "a3 = ", a3, new_line('c')
write(fstderr,*) "a3_zt = ", a3_zt, new_line('c')
write(fstderr,*) "wp3_on_wp2 = ", wp3_on_wp2, new_line('c')
write(fstderr,*) "tau_C1_zm = ", tau_C1_zm, new_line('c')
write(fstderr,*) "rho_ds_zm = ", rho_ds_zm, new_line('c')
write(fstderr,*) "rho_ds_zt = ", rho_ds_zt, new_line('c')
write(fstderr,*) "invrs_rho_ds_zm = ", invrs_rho_ds_zm, new_line('c')
write(fstderr,*) "invrs_rho_ds_zt = ", invrs_rho_ds_zt, new_line('c')
write(fstderr,*) "radf = ", radf, new_line('c')
write(fstderr,*) "thv_ds_zm = ", thv_ds_zm, new_line('c')
write(fstderr,*) "thv_ds_zt = ", thv_ds_zt, new_line('c')
write(fstderr,*) "Cx_fnc_Richardson = ", Cx_fnc_Richardson, new_line('c')
write(fstderr,*) "pdf_implicit_coefs_terms = ", pdf_implicit_coefs_terms
write(fstderr,*) new_line('c')
write(fstderr,*) "Intent(in/out)"
write(fstderr,*) "wp2_zt = ", wp2_zt, new_line('c')
write(fstderr,*) "wp3_zm = ", wp3_zm, new_line('c')
write(fstderr,*) "wp2 = ", wp2, new_line('c')
write(fstderr,*) "wp3 = ", wp3, new_line('c')
end if ! fatal error
end if
return
end subroutine advance_wp2_wp3
!=============================================================================
subroutine wp23_solve( dt, sfc_elevation, sigma_sqd_w, wm_zm, & ! Intent(in)
wm_zt, a3, a3_zt, wp3_on_wp2, wp4, & ! Intent(in)
wpthvp, wp2thvp, um, vm, upwp, vpwp, & ! Intent(in)
up2, vp2, Kw1, Kw8, Kh_zt, Skw_zt, & ! Intent(in)
tau1m, tauw3t, tau_C1_zm, C1_Skw_fnc, & ! Intent(in)
C11_Skw_fnc, C16_fnc, rho_ds_zm, & ! Intent(in)
rho_ds_zt, invrs_rho_ds_zm, & ! Intent(in)
invrs_rho_ds_zt, radf, & ! Intent(in)
thv_ds_zm, thv_ds_zt, & ! Intent(in)
wp2_splat, wp3_splat, & ! Intent(in)
pdf_implicit_coefs_terms, & ! Intent(in)
wprtp, wpthlp, rtp2, thlp2, & ! Intent(in)
wp2, wp3, wp3_zm, wp2_zt ) ! Intent(inout)
! Description:
! Decompose, and back substitute the matrix for wp2/wp3
! References:
! _Equations for CLUBB_ section 6.3
!------------------------------------------------------------------------
use grid_class, only: &
gr ! Variable(s)
use grid_class, only: &
zm2zt, & ! Function(s)
zt2zm, &
ddzt
use constants_clubb, only: &
w_tol_sqd, & ! Variables(s)
max_mag_correlation_flux, &
one, &
zero, &
zero_threshold, &
fstderr
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constants
use model_flags, only: &
l_tke_aniso, & ! Variable(s)
l_hole_fill, &
l_explicit_turbulent_adv_wp3, &
l_gmres, &
l_min_wp2_from_corr_wx
use clubb_precision, only: &
core_rknd ! Variable(s)
use lapack_wrap, only: &
band_solve, & ! Procedure(s)
band_solvex
use fill_holes, only: &
fill_holes_vertical
use clip_explicit, only: &
clip_variance, & ! Procedure(s)
clip_variance_level, &
clip_skewness
use pdf_closure_module, only: &
iiPDF_ADG1, & ! Variable(s)
iiPDF_new, &
iiPDF_type
use pdf_parameter_module, only: &
implicit_coefs_terms ! Variable Type
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_update_var, &
stat_update_var_pt, &
stat_end_update, &
stat_end_update_pt
use stats_variables, only: &
stats_zm, & ! Variable(s)
stats_zt, &
stats_sfc, &
l_stats_samp, &
icoef_wp4_implicit, &
iwp2_ta, &
iwp2_ma, &
iwp2_pd, &
iwp2_ac, &
iwp2_dp1, &
iwp2_dp2, &
iwp2_pr1, &
iwp2_pr2, &
iwp3_ta, &
iwp3_ma, &
iwp3_tp, &
iwp3_ac, &
iwp3_dp1, &
iwp3_pr1, &
iwp3_pr2, &
iwp3_pr3, &
iwp23_matrix_condt_num
use stats_variables, only: &
zmscr01, &
zmscr02, &
zmscr03, &
zmscr04, &
zmscr05, &
zmscr06, &
zmscr07, &
zmscr08, &
zmscr09, &
zmscr10, &
zmscr11, &
zmscr12, &
ztscr01, &
ztscr02
use stats_variables, only: &
ztscr03, &
ztscr04, &
ztscr05, &
ztscr06, &
ztscr07, &
ztscr08, &
ztscr09, &
ztscr10, &
ztscr11, &
ztscr12, &
ztscr13, &
ztscr14, &
ztscr15, &
ztscr16
implicit none
! External
intrinsic :: max, min, sqrt
! Parameter Constants
integer, parameter :: &
nrhs = 1 ! Number of RHS vectors
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Timestep [s]
real( kind = core_rknd ), intent(in) :: &
sfc_elevation ! Elevation of ground level [m AMSL]
real( kind = core_rknd ), intent(in), dimension(gr%nz) :: &
sigma_sqd_w, & ! sigma_sqd_w (momentum levels) [-]
wm_zm, & ! w wind component on momentum levels [m/s]
wm_zt, & ! w wind component on thermodynamic levels [m/s]
a3, & ! a_3 (momentum levels); See eqn. 25 in `Equations for CLUBB' [-]
a3_zt, & ! a_3 interpolated to thermodynamic levels [-]
wp3_on_wp2, & ! Smoothed version of wp3 / wp2 [m/s]
wp4, & ! w'^4 (momentum levels) [m^4/s^4]
wpthvp, & ! w'th_v' (momentum levels) [K m/s]
wp2thvp, & ! w'^2th_v' (thermodynamic levels) [K m^2/s^2]
um, & ! u wind component (thermodynamic levels) [m/s]
vm, & ! v wind component (thermodynamic levels) [m/s]
upwp, & ! u'w' (momentum levels) [m^2/s^2]
vpwp, & ! v'w' (momentum levels) [m^2/s^2]
up2, & ! u'^2 (momentum levels) [m^2/s^2]
vp2, & ! v'^2 (momentum levels) [m^2/s^2]
Kw1, & ! Coefficient of eddy diffusivity for w'^2 [m^2/s]
Kw8, & ! Coefficient of eddy diffusivity for w'^3 [m^2/s]
Kh_zt, & ! Eddy diffusivity on thermodynamic levels [m^2/s]
Skw_zt, & ! Skewness of w on thermodynamic levels [-]
tau1m, & ! Time-scale tau on momentum levels [s]
tauw3t, & ! Time-scale tau on thermodynamic levels [s]
tau_C1_zm, & ! Tau values used for the C1 (dp1) term in wp2 [s]
C1_Skw_fnc, & ! C_1 parameter with Sk_w applied [-]
C11_Skw_fnc, & ! C_11 parameter with Sk_w applied [-]
C16_fnc, & ! C_16 parameter [-]
rho_ds_zm, & ! Dry, static density on momentum levels [kg/m^3]
rho_ds_zt, & ! Dry, static density on thermo. levels [kg/m^3]
invrs_rho_ds_zm, & ! Inv. dry, static density @ momentum levs. [m^3/kg]
invrs_rho_ds_zt, & ! Inv. dry, static density @ thermo. levs. [m^3/kg]
radf, & ! Buoyancy production at CL top [m^2/s^3]
thv_ds_zm, & ! Dry, base-state theta_v on momentum levs. [K]
thv_ds_zt, & ! Dry, base-state theta_v on thermo. levs. [K]
wprtp, & ! Flux of total water mixing ratio [m/s kg/kg]
wpthlp, & ! Flux of liquid water potential temp. [m/s K]
rtp2, & ! Variance of rt (overall) [kg^2/kg^2]
thlp2, & ! Variance of thl (overall) [K^2]
wp2_splat, & ! Tendency of <w'2> due to vertical compression of eddies [m^2/s^3]
wp3_splat ! Tendency of <w'3> due to vertical compression of eddies [m^3/s^4]
type(implicit_coefs_terms), dimension(gr%nz), intent(in) :: &
pdf_implicit_coefs_terms ! Implicit coefs / explicit terms [units vary]
! Input/Output Variables
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
wp2, & ! w'^2 (momentum levels) [m^2/s^2]
wp3, & ! w'^3 (thermodynamic levels) [m^3/s^3]
wp3_zm ! w'^3 interpolated to momentum levels [m^3/s^3]
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
wp2_zt ! w'^2 interpolated to thermodyamic levels [m^2/s^2]
! Local Variables
real( kind = core_rknd ), dimension(5,2*gr%nz) :: &
lhs ! Implicit contributions to wp2/wp3 (band diag. matrix)
real( kind = core_rknd ), dimension(2*gr%nz) :: &
rhs ! RHS of band matrix
! real, target, dimension(2*gr%nz) ::
real( kind = core_rknd ), dimension(2*gr%nz) :: &
solut ! Solution to band diagonal system.
real( kind = core_rknd ), dimension(gr%nz) :: &
coef_wp4_implicit_zt, & ! <w'^4>|_zt=coef_wp4_implicit_zt*<w'^2>|_zt^2 [-]
coef_wp4_implicit ! <w'^4> = coef_wp4_implicit * <w'^2>^2 [-]
real( kind = core_rknd ), dimension(gr%nz) :: &
a1, & ! a_1 (momentum levels); See eqn. 23 in `Equations for CLUBB' [-]
a1_zt ! a_1 interpolated to thermodynamic levels [-]
! real, dimension(gr%nz) :: &
! wp2_n ! w'^2 at the previous timestep [m^2/s^2]
real( kind = core_rknd ) :: &
rcond ! Est. of the reciprocal of the condition #
real( kind = core_rknd ), dimension(gr%nz,5) :: &
wp3_pr3_lhs ! wp3_pr3 (implicit) contribution to lhs
real( kind = core_rknd ) :: &
threshold ! Minimum value for wp2 [m^2/s^2]
! Array indices
integer :: k, km1, kp1, k_wp2, k_wp3
! Set logical to true for Crank-Nicholson diffusion scheme
! or to false for completely implicit diffusion scheme.
! Note: Although Crank-Nicholson diffusion has usually been used for wp2
! and wp3 in the past, we found that using completely implicit
! diffusion stabilized the deep convective cases more while having
! almost no effect on the boundary layer cases. Brian; 1/4/2008.
! logical, parameter :: l_crank_nich_diff = .true.
logical, parameter :: l_crank_nich_diff = .false.
!-----------------------------------------------------------------------
!----- Begin Code -----
if ( .not. l_explicit_turbulent_adv_wp3 ) then
if ( iiPDF_type == iiPDF_new ) then
! Unpack coef_wp4_implicit from pdf_implicit_coefs_terms.
! Since PDF parameters and the resulting implicit coefficients and
! explicit terms are calculated on thermodynamic levels, the <w'^4>
! implicit coefficient needs to be unpacked as coef_wp4_implicit_zt.
coef_wp4_implicit_zt = pdf_implicit_coefs_terms%coef_wp4_implicit
! The values of <w'^4> are located on momentum levels. Interpolate
! coef_wp4_implicit_zt to momentum levels as coef_wp4_implicit. The
! discretization diagram is found in the description section of
! function wp3_term_ta_new_pdf_lhs below. These values are always
! positive.
coef_wp4_implicit = max( zt2zm( coef_wp4_implicit_zt ), &
zero_threshold )
! Set the value of coef_wp4_implicit to 0 at the lower boundary and at
! the upper boundary. This sets the value of <w'^4> to 0 at the lower
! and upper boundaries.
coef_wp4_implicit(1) = zero
coef_wp4_implicit(gr%nz) = zero
if ( l_stats_samp ) then
call stat_update_var( icoef_wp4_implicit, coef_wp4_implicit, &
stats_zm )
endif ! l_stats_samp
elseif ( iiPDF_type == iiPDF_ADG1 ) then
! Define a_1 and a_3 (both are located on momentum levels).
! They are variables that are both functions of sigma_sqd_w (where
! sigma_sqd_w is located on momentum levels).
a1 = one / ( one - sigma_sqd_w )
! Interpolate a_1 from momentum levels to thermodynamic levels. This
! will be used for the w'^3 turbulent advection (ta) term.
a1_zt = max( zm2zt( a1 ), zero_threshold ) ! Positive def. quantity
endif ! iiPDF_type
endif ! .not. l_explicit_turbulent_adv_wp3
! Compute the explicit portion of the w'^2 and w'^3 equations.
! Build the right-hand side vector.
call wp23_rhs( dt, wp2, wp3, a1, a1_zt, a3, a3_zt, wp3_on_wp2, & ! intent(in)
coef_wp4_implicit, wp4, wpthvp, wp2thvp, um, vm, & ! intent(in)
upwp, vpwp, up2, vp2, Kw1, Kw8, Kh_zt, & ! intent(in)
Skw_zt, tau1m, tauw3t, tau_C1_zm, C1_Skw_fnc, & ! intent(in)
C11_Skw_fnc, C16_fnc, rho_ds_zm, invrs_rho_ds_zt, radf, & ! intent(in)
thv_ds_zm, thv_ds_zt, wp2_splat, wp3_splat, & ! intent(in)
l_crank_nich_diff, & ! intent(in)
rhs ) ! intent(out)
if (l_gmres) then
call wp23_gmres( dt, wp2, wm_zm, wm_zt, a1, a1_zt, a3, a3_zt, &
wp3_on_wp2, coef_wp4_implicit, &
Kw1, Kw8, Skw_zt, tau1m, tauw3t, tau_C1_zm, &
C1_Skw_fnc, C11_Skw_fnc, C16_fnc, rho_ds_zm, &
rho_ds_zt, invrs_rho_ds_zm, &
invrs_rho_ds_zt, l_crank_nich_diff, nrhs, &
rhs, &
solut, wp3_pr3_lhs )
else
! Compute the implicit portion of the w'^2 and w'^3 equations.
! Build the left-hand side matrix.
call wp23_lhs( dt, wp2, wm_zm, wm_zt, a1, a1_zt, a3, a3_zt, &
wp3_on_wp2, coef_wp4_implicit, &
Kw1, Kw8, Skw_zt, tau1m, tauw3t, tau_C1_zm, C1_Skw_fnc, &
C11_Skw_fnc, C16_fnc, rho_ds_zm, rho_ds_zt, &
invrs_rho_ds_zm, invrs_rho_ds_zt, l_crank_nich_diff, &
lhs, wp3_pr3_lhs )
! Solve the system with LAPACK
if ( l_stats_samp .and. iwp23_matrix_condt_num > 0 ) then
! Perform LU decomp and solve system (LAPACK with diagnostics)
! Note that this can change the answer slightly
call band_solvex( "wp2_wp3", 2, 2, 2*gr%nz, nrhs, &
lhs, rhs, solut, rcond )
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "in wp23_solve calling band_solvex for wp2_wp3"
return
end if
end if
! Est. of the condition number of the w'^2/w^3 LHS matrix
call stat_update_var_pt( iwp23_matrix_condt_num, 1, one / rcond, stats_sfc )
else
! Perform LU decomp and solve system (LAPACK)
call band_solve( "wp2_wp3", 2, 2, 2*gr%nz, nrhs, &
lhs, rhs, solut )
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "in wp23_solve calling band_solve for wp2_wp3"
return
end if
end if
endif
endif ! l_gmres
! Copy result into output arrays and clip
do k = 1, gr%nz
km1 = max( k-1, 1 )
kp1 = min( k+1, gr%nz )
k_wp3 = 2*k - 1
k_wp2 = 2*k
! wp2_n(k) = wp2(k) ! For the positive definite scheme
wp2(k) = solut(k_wp2)
wp3(k) = solut(k_wp3)
end do
if ( l_stats_samp ) then
! Finalize implicit contributions for wp2
do k = 2, gr%nz-1
km1 = max( k-1, 1 )
kp1 = min( k+1, gr%nz )
! w'^2 term dp1 has both implicit and explicit components;
! call stat_end_update_pt.
call stat_end_update_pt( iwp2_dp1, k, &
zmscr01(k) * wp2(k), stats_zm )
! w'^2 term dp2 has both implicit and explicit components (if the
! Crank-Nicholson scheme is selected); call stat_end_update_pt.
! If Crank-Nicholson diffusion is not selected, then w'^3 term dp1 is
! completely implicit; call stat_update_var_pt.
if ( l_crank_nich_diff ) then
call stat_end_update_pt( iwp2_dp2, k, &
zmscr02(k) * wp2(km1) &
+ zmscr03(k) * wp2(k) &
+ zmscr04(k) * wp2(kp1), stats_zm )
else
call stat_update_var_pt( iwp2_dp2, k, &
zmscr02(k) * wp2(km1) &
+ zmscr03(k) * wp2(k) &
+ zmscr04(k) * wp2(kp1), stats_zm )
endif
! w'^2 term ta is completely implicit; call stat_update_var_pt.
call stat_update_var_pt( iwp2_ta, k, &
zmscr05(k) * wp3(k) &
+ zmscr06(k) * wp3(kp1), stats_zm )
! w'^2 term ma is completely implicit; call stat_update_var_pt.
call stat_update_var_pt( iwp2_ma, k, &
zmscr07(k) * wp2(km1) &
+ zmscr08(k) * wp2(k) &
+ zmscr09(k) * wp2(kp1), stats_zm )
! w'^2 term ac is completely implicit; call stat_update_var_pt.
call stat_update_var_pt( iwp2_ac, k, &
zmscr10(k) * wp2(k), stats_zm )
! w'^2 term pr1 has both implicit and explicit components;
! call stat_end_update_pt.
if ( l_tke_aniso ) then
call stat_end_update_pt( iwp2_pr1, k, &
zmscr12(k) * wp2(k), stats_zm )
endif
! w'^2 term pr2 has both implicit and explicit components;
! call stat_end_update_pt.
call stat_end_update_pt( iwp2_pr2, k, &
zmscr11(k) * wp2(k), stats_zm )
enddo
! Finalize implicit contributions for wp3
do k = 2, gr%nz-1, 1
km1 = max( k-1, 1 )
kp1 = min( k+1, gr%nz )
! w'^3 term pr1 has both implicit and explicit components;
! call stat_end_update_pt.
call stat_end_update_pt( iwp3_pr1, k, &
ztscr01(k) * wp3(k), stats_zt )
! w'^3 term dp1 has both implicit and explicit components (if the
! Crank-Nicholson scheme is selected); call stat_end_update_pt.
! If Crank-Nicholson diffusion is not selected, then w'^3 term dp1 is
! completely implicit; call stat_update_var_pt.
if ( l_crank_nich_diff ) then
call stat_end_update_pt( iwp3_dp1, k, &
ztscr02(k) * wp3(km1) &
+ ztscr03(k) * wp3(k) &
+ ztscr04(k) * wp3(kp1), stats_zt )
else
call stat_update_var_pt( iwp3_dp1, k, &
ztscr02(k) * wp3(km1) &
+ ztscr03(k) * wp3(k) &
+ ztscr04(k) * wp3(kp1), stats_zt )
endif
! w'^3 term ta has both implicit and explicit components;
! call stat_end_update_pt.
call stat_end_update_pt( iwp3_ta, k, &
ztscr05(k) * wp3(km1) &
+ ztscr06(k) * wp2(km1) &
+ ztscr07(k) * wp3(k) &
+ ztscr08(k) * wp2(k) &
+ ztscr09(k) * wp3(kp1), stats_zt )
! w'^3 term tp has both implicit and explicit components;
! call stat_end_update_pt.
call stat_end_update_pt( iwp3_tp, k, &
ztscr10(k) * wp2(km1) &
+ ztscr11(k) * wp2(k), stats_zt )
! w'^3 pressure term 3 (pr3) has both implicit and explicit components;
! call stat_end_update_pt
call stat_end_update_pt( iwp3_pr3, k, &
- wp3_pr3_lhs(k,5) * wp3(km1) &
- wp3_pr3_lhs(k,4) * wp2(km1) &
- wp3_pr3_lhs(k,3) * wp3(k) &
- wp3_pr3_lhs(k,2) * wp2(k) &
- wp3_pr3_lhs(k,1) * wp3(kp1), stats_zt )
! w'^3 term ma is completely implicit; call stat_update_var_pt.
call stat_update_var_pt( iwp3_ma, k, &
ztscr12(k) * wp3(km1) &
+ ztscr13(k) * wp3(k) &
+ ztscr14(k) * wp3(kp1), stats_zt )
! w'^3 term ac is completely implicit; call stat_update_var_pt.
call stat_update_var_pt( iwp3_ac, k, &
ztscr15(k) * wp3(k), stats_zt )
! w'^3 term pr2 has both implicit and explicit components;
! call stat_end_update_pt.
call stat_end_update_pt( iwp3_pr2, k, &
ztscr16(k) * wp3(k), stats_zt )
enddo
endif ! l_stats_samp
if ( l_stats_samp ) then
! Store previous value for effect of the positive definite scheme
call stat_begin_update( iwp2_pd, wp2 / dt, stats_zm )
endif
if ( l_hole_fill .and. any( wp2 < w_tol_sqd ) ) then
! Use a simple hole filling algorithm
call fill_holes_vertical( 2, w_tol_sqd, "zm", &
rho_ds_zt, rho_ds_zm, &
wp2 )
endif ! wp2
! Here we attempt to clip extreme values of wp2 to prevent a crash of the
! type found on the Climate Process Team ticket #49. Chris Golaz found that
! instability caused by large wp2 in CLUBB led unrealistic results in AM3.
! -dschanen 11 Apr 2011
where ( wp2 > 1000._core_rknd ) wp2 = 1000._core_rknd
if ( l_stats_samp ) then
! Store updated value for effect of the positive definite scheme
call stat_end_update( iwp2_pd, wp2 / dt, stats_zm )
endif
! Clip <w'^2> at a minimum threshold.
! The value of <w'^2> is not allowed to become smaller than the threshold
! value of w_tol^2. Additionally, that threshold value may be boosted at
! any grid level in order to keep the overall correlation of w and rt or
! the overall correlation of w and theta-l between the values of
! -max_mag_correlation_flux and max_mag_correlation_flux by boosting <w'^2>
! rather than by limiting the magnitude of <w'rt'> or <w'thl'>.
if ( l_min_wp2_from_corr_wx ) then
! The overall correlation of w and rt is:
!
! corr_w_rt = wprtp / ( sqrt( wp2 ) * sqrt( rtp2 ) );
!
! and the overall correlation of w and thl is:
!
! corr_w_thl = wpthlp / ( sqrt( wp2 ) * sqrt( thlp2 ) ).
!
! Squaring both sides, the equations becomes:
!
! corr_w_rt^2 = wprtp^2 / ( wp2 * rtp2 ); and
!
! corr_w_thl^2 = wpthlp^2 / ( wp2 * thlp2 ).
!
! Using max_mag_correlation_flux for the correlation and then solving for
! the minimum of wp2, the equation becomes:
!
! wp2|_min = max( wprtp^2 / ( rtp2 * max_mag_correlation_flux^2 ),
! wpthlp^2 / ( thlp2 * max_mag_correlation_flux^2 ) ).
do k = 1, gr%nz, 1
threshold &