forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvance_xm_wpxp_module.F90
5137 lines (4228 loc) · 214 KB
/
advance_xm_wpxp_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_xm_wpxp_module
! Description:
! Contains the CLUBB advance_xm_wpxp_module scheme.
! References:
! None
!-----------------------------------------------------------------------
implicit none
private ! Default scope
public :: advance_xm_wpxp
private :: xm_wpxp_lhs, &
xm_wpxp_rhs, &
xm_wpxp_solve, &
xm_wpxp_clipping_and_stats, &
xm_term_ta_lhs, &
xm_term_ta_lhs_all, &
wpxp_term_tp_lhs, &
wpxp_term_tp_lhs_all, &
wpxp_terms_ac_pr2_lhs, &
wpxp_terms_ac_pr2_lhs_all, &
wpxp_term_pr1_lhs_all, &
wpxp_terms_bp_pr3_rhs, &
wpxp_terms_bp_pr3_rhs_all, &
xm_correction_wpxp_cl, &
damp_coefficient, &
diagnose_upxp, &
error_prints_xm_wpxp
! Parameter Constants
integer, parameter, private :: &
nsub = 2, & ! Number of subdiagonals in the LHS matrix
nsup = 2, & ! Number of superdiagonals in the LHS matrix
xm_wpxp_thlm = 1, & ! Named constant for thlm and wpthlp solving
xm_wpxp_rtm = 2, & ! Named constant for rtm and wprtp solving
xm_wpxp_scalar = 3, & ! Named constant for sclrm and wpsclrp solving
xm_wpxp_um = 4, & ! Named constant for optional um and upwp solving
xm_wpxp_vm = 5 ! Named constant for optional vm and vpwp solving
contains
!=============================================================================
subroutine advance_xm_wpxp( dt, sigma_sqd_w, wm_zm, wm_zt, wp2, &
Lscale, wp3_on_wp2, wp3_on_wp2_zt, Kh_zt, Kh_zm, &
tau_C6_zm, Skw_zm, wp2rtp, rtpthvp, rtm_forcing, &
wprtp_forcing, rtm_ref, wp2thlp, thlpthvp, &
thlm_forcing, wpthlp_forcing, thlm_ref, &
rho_ds_zm, rho_ds_zt, invrs_rho_ds_zm, &
invrs_rho_ds_zt, thv_ds_zm, rtp2, thlp2, &
w_1_zm, w_2_zm, varnce_w_1_zm, varnce_w_2_zm, &
mixt_frac_zm, l_implemented, em, wp2sclrp, &
sclrpthvp, sclrm_forcing, sclrp2, exner, rcm, &
p_in_Pa, thvm, Cx_fnc_Richardson, &
ice_supersat_frac, &
pdf_implicit_coefs_terms, &
um_forcing, vm_forcing, ug, vg, wpthvp, &
fcor, um_ref, vm_ref, up2, vp2, &
uprcp, vprcp, rc_coef, &
l_predict_upwp_vpwp, &
l_diffuse_rtm_and_thlm, &
l_stability_correct_Kh_N2_zm, &
l_upwind_wpxp_ta, &
l_upwind_xm_ma, &
l_uv_nudge, &
l_tke_aniso, &
l_use_C7_Richardson, &
l_brunt_vaisala_freq_moist, &
l_use_thvm_in_bv_freq, &
rtm, wprtp, thlm, wpthlp, &
sclrm, wpsclrp, um, upwp, vm, vpwp )
! Description:
! Advance the mean and flux terms by one timestep.
! References:
! https://arxiv.org/pdf/1711.03675v1.pdf#nameddest=url:wpxp_eqns
!
! Eqn. 16 & 17 on p. 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 5:
! /Implicit solutions for the means and fluxes/
!-----------------------------------------------------------------------
use parameters_tunable, only: &
C6rt, & ! Variable(s)
C6rtb, &
C6rtc, &
C6thl, &
C6thlb, &
C6thlc, &
C7, &
C7b, &
C7c, &
c_K6, &
C6rt_Lscale0, &
C6thl_Lscale0, &
C7_Lscale0, &
wpxp_L_thresh
use constants_clubb, only: &
fstderr, & ! Constant
rt_tol, &
thl_tol, &
w_tol, &
w_tol_sqd, &
thl_tol_mfl, &
rt_tol_mfl, &
max_mag_correlation, &
one, &
one_half, &
zero, &
zero_threshold, &
eps, &
ep1
use parameters_model, only: &
sclr_dim, & ! Variable(s)
ts_nudge
use grid_class, only: &
gr, & ! Variable(s)
ddzt ! Procedure(s)
use grid_class, only: &
zm2zt, & ! Procedure(s)
zt2zm
use model_flags, only: &
l_clip_semi_implicit, & ! Variable(s)
l_explicit_turbulent_adv_wpxp
use mono_flux_limiter, only: &
calc_turb_adv_range ! Procedure(s)
use pdf_closure_module, only: &
iiPDF_new, & ! Variable(s)
iiPDF_ADG1, &
iiPDF_type
use pdf_parameter_module, only: &
implicit_coefs_terms ! Variable Type
use turbulent_adv_pdf, only: &
sgn_turbulent_velocity ! Procedure(s)
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 ! Constants
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_end_update, &
stat_update_var
use stats_variables, only: &
stats_zt, & ! Variables
stats_zm, &
irtm_sdmp, &
ithlm_sdmp, &
ium_sdmp, &
ivm_sdmp, &
ium_ndg, &
ivm_ndg, &
ium_ref, &
ivm_ref, &
iC7_Skw_fnc, &
iC6rt_Skw_fnc, &
iC6thl_Skw_fnc, &
l_stats_samp
use sponge_layer_damping, only: &
rtm_sponge_damp_settings, &
thlm_sponge_damp_settings, &
uv_sponge_damp_settings, &
rtm_sponge_damp_profile, &
thlm_sponge_damp_profile, &
uv_sponge_damp_profile, &
sponge_damp_xm ! Procedure(s)
implicit none
! Parameter Constants
logical, parameter :: &
l_iter = .true. ! True when the means and fluxes are prognosed
! -------------------- Input Variables --------------------
real( kind = core_rknd ), intent(in) :: &
dt ! Timestep [s]
real( kind = core_rknd ), intent(in), dimension(gr%nz) :: &
sigma_sqd_w, & ! sigma_sqd_w on momentum levels [-]
wm_zm, & ! w wind component on momentum levels [m/s]
wm_zt, & ! w wind component on thermodynamic levels [m/s]
wp2, & ! w'^2 (momentum levels) [m^2/s^2]
Lscale, & ! Turbulent mixing length [m]
em, & ! Turbulent Kinetic Energy (TKE) [m^2/s^2]
wp3_on_wp2, & ! Smoothed wp3 / wp2 on momentum levels [m/s]
wp3_on_wp2_zt, & ! Smoothed wp3 / wp2 on thermo. levels [m/s]
Kh_zt, & ! Eddy diffusivity on thermodynamic levels [m^2/s]
Kh_zm, & ! Eddy diffusivity on momentum levels
tau_C6_zm, & ! Time-scale tau on momentum levels applied to C6 term [s]
Skw_zm, & ! Skewness of w on momentum levels [-]
wp2rtp, & ! <w'^2 r_t'> (thermodynamic levels) [m^2/s^2 kg/kg]
rtpthvp, & ! r_t'th_v' (momentum levels) [(kg/kg) K]
rtm_forcing, & ! r_t forcing (thermodynamic levels) [(kg/kg)/s]
wprtp_forcing, & ! <w'r_t'> forcing (momentum levels) [(kg/kg)/s^2]
rtm_ref, & ! rtm for nudging [kg/kg]
wp2thlp, & ! <w'^2 th_l'> (thermodynamic levels) [m^2/s^2 K]
thlpthvp, & ! th_l'th_v' (momentum levels) [K^2]
thlm_forcing, & ! th_l forcing (thermodynamic levels) [K/s]
wpthlp_forcing, & ! <w'th_l'> forcing (momentum levels) [K/s^2]
thlm_ref, & ! thlm for nudging [K]
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 @ moment. levs. [m^3/kg]
invrs_rho_ds_zt, & ! Inv. dry, static density @ thermo. levs. [m^3/kg]
thv_ds_zm, & ! Dry, base-state theta_v on moment. levs. [K]
! Added for clipping by Vince Larson 29 Sep 2007
rtp2, & ! r_t'^2 (momentum levels) [(kg/kg)^2]
thlp2, & ! th_l'^2 (momentum levels) [K^2]
! End of Vince Larson's addition.
w_1_zm, & ! Mean w (1st PDF component) [m/s]
w_2_zm, & ! Mean w (2nd PDF component) [m/s]
varnce_w_1_zm, & ! Variance of w (1st PDF component) [m^2/s^2]
varnce_w_2_zm, & ! Variance of w (2nd PDF component) [m^2/s^2]
mixt_frac_zm ! Weight of 1st PDF component (Sk_w dependent) [-]
logical, intent(in) :: &
l_implemented ! Flag for CLUBB being implemented in a larger model.
! Additional variables for passive scalars
real( kind = core_rknd ), intent(in), dimension(gr%nz,sclr_dim) :: &
wp2sclrp, & ! <w'^2 sclr'> (thermodynamic levels) [Units vary]
sclrpthvp, & ! <sclr' th_v'> (momentum levels) [Units vary]
sclrm_forcing, & ! sclrm forcing (thermodynamic levels) [Units vary]
sclrp2 ! For clipping Vince Larson [Units vary]
real( kind = core_rknd ), intent(in), dimension(gr%nz) :: &
exner, & ! Exner function [-]
rcm, & ! cloud water mixing ratio, r_c [kg/kg]
p_in_Pa, & ! Air pressure [Pa]
thvm, & ! Virutal potential temperature [K]
Cx_fnc_Richardson,& ! Cx_fnc computed from Richardson_num [-]
ice_supersat_frac
type(implicit_coefs_terms), intent(in) :: &
pdf_implicit_coefs_terms ! Implicit coefs / explicit terms [units vary]
! Variables used to predict <u> and <u'w'>, as well as <v> and <v'w'>.
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
um_forcing, & ! <u> forcing term (thermodynamic levels) [m/s^2]
vm_forcing, & ! <v> forcing term (thermodynamic levels) [m/s^2]
ug, & ! <u> geostrophic wind (thermodynamic levels) [m/s]
vg, & ! <v> geostrophic wind (thermodynamic levels) [m/s]
wpthvp ! <w'thv'> (momentum levels) [m/s K]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
uprcp, & ! < u' r_c' > [(m kg)/(s kg)]
vprcp, & ! < v' r_c' > [(m kg)/(s kg)]
rc_coef ! Coefficient on X'r_c' in X'th_v' equation [K/(kg/kg)]
real( kind = core_rknd ), intent(in) :: &
fcor ! Coriolis parameter [s^-1]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
um_ref, & ! Reference u wind component for nudging [m/s]
vm_ref, & ! Reference v wind component for nudging [m/s]
up2, & ! Variance of the u wind component [m^2/s^2]
vp2 ! Variance of the v wind component [m^2/s^2]
logical, intent(in) :: &
l_predict_upwp_vpwp, & ! Flag to predict <u'w'> and <v'w'> along with <u> and <v>
! alongside the advancement of <rt>, <w'rt'>, <thl>,
! <wpthlp>, <sclr>, and <w'sclr'> in subroutine
! advance_xm_wpxp. Otherwise, <u'w'> and <v'w'> are still
! approximated by eddy diffusivity when <u> and <v> are
! advanced in subroutine advance_windm_edsclrm.
l_diffuse_rtm_and_thlm, & ! This flag determines whether or not we want CLUBB to do
! diffusion on rtm and thlm
l_stability_correct_Kh_N2_zm, & ! This flag determines whether or not we want CLUBB to apply
! a stability correction
l_upwind_wpxp_ta, & ! This flag determines whether we want to use an upwind
! differencing approximation rather than a centered
! differencing for turbulent or mean advection terms.
! It affects wprtp, wpthlp, & wpsclrp.
l_upwind_xm_ma, & ! This flag determines whether we want to use an upwind
! differencing approximation rather than a centered
! differencing for turbulent or mean advection terms.
! It affects rtm, thlm, sclrm, um and vm.
l_uv_nudge, & ! For wind speed nudging
l_tke_aniso, & ! For anisotropic turbulent kinetic energy, i.e. TKE = 1/2
! (u'^2 + v'^2 + w'^2)
l_use_C7_Richardson, & ! Parameterize C7 based on Richardson number
l_brunt_vaisala_freq_moist, & ! Use a different formula for the Brunt-Vaisala frequency in
! saturated atmospheres (from Durran and Klemp, 1982)
l_use_thvm_in_bv_freq ! Use thvm in the calculation of Brunt-Vaisala frequency
! -------------------- Input/Output Variables --------------------
real( kind = core_rknd ), intent(inout), dimension(gr%nz) :: &
rtm, & ! r_t (total water mixing ratio) [kg/kg]
wprtp, & ! w'r_t' [(kg/kg) m/s]
thlm, & ! th_l (liquid water potential temperature) [K]
wpthlp ! w'th_l' [K m/s]
! Input/Output Variables
real( kind = core_rknd ), intent(inout), dimension(gr%nz,sclr_dim) :: &
sclrm, wpsclrp ! [Units vary]
! Variables used to predict <u> and <u'w'>, as well as <v> and <v'w'>.
real( kind = core_rknd ), intent(inout), dimension(gr%nz) :: &
um, & ! <u>: mean west-east horiz. velocity (thermo. levs.) [m/s]
upwp, & ! <u'w'>: momentum flux (momentum levels) [m^2/s^2]
vm, & ! <v>: mean south-north horiz. velocity (thermo. levs.) [m/s]
vpwp ! <v'w'>: momentum flux (momentum levels) [m^2/s^2]
! -------------------- Local Variables --------------------
real( kind = core_rknd ), dimension(gr%nz) :: &
C6rt_Skw_fnc, C6thl_Skw_fnc, C7_Skw_fnc
! Eddy Diffusion for wpthlp and wprtp.
real( kind = core_rknd ), dimension(gr%nz) :: Kw6 ! wpxp eddy diff. [m^2/s]
! Variables used as part of the monotonic turbulent advection scheme.
! Find the lowermost and uppermost grid levels that can have an effect
! on the central thermodynamic level during the course of a time step,
! due to the effects of turbulent advection only.
integer, dimension(gr%nz) :: &
low_lev_effect, & ! Index of the lowest level that has an effect.
high_lev_effect ! Index of the highest level that has an effect.
! Constant parameters as a function of Skw.
integer :: &
nrhs ! Number of RHS vectors
! Saved values of predictive fields, prior to being advanced, for use in
! print statements in case of fatal error.
real( kind = core_rknd ), dimension(gr%nz) :: &
rtm_old, & ! Saved value of r_t [kg/kg]
wprtp_old, & ! Saved value of w'r_t' [(kg/kg) m/s]
thlm_old, & ! Saved value of th_l [K]
wpthlp_old ! Saved value of w'th_l' [K m/s]
! Input/Output Variables
real( kind = core_rknd ), dimension(gr%nz,sclr_dim) :: &
sclrm_old, wpsclrp_old ! [Units vary]
! Variables used to predict <u> and <u'w'>, as well as <v> and <v'w'>.
real( kind = core_rknd ), dimension(gr%nz) :: &
um_old, & ! Saved value of <u> [m/s]
upwp_old, & ! Saved value of <u'w'> [m^2/s^2]
vm_old, & ! Saved value of <v> [m/s]
vpwp_old ! Saved value of <v'w'> [m^2/s^2]
! LHS/RHS terms
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs_diff_zm, & ! Diffusion term for w'x'
lhs_diff_zt, & ! Diffusion term for w'x'
lhs_ma_zt, & ! Mean advection contributions to lhs
lhs_ma_zm ! Mean advection contributions to lhs
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs_ta_wprtp, & ! w'r_t' turbulent advection contributions to lhs
lhs_ta_wpthlp, & ! w'thl' turbulent advection contributions to lhs
lhs_ta_wpup, & ! w'u' turbulent advection contributions to lhs
lhs_ta_wpvp ! w'v' turbulent advection contributions to lhs
real( kind = core_rknd ), dimension(3,gr%nz,sclr_dim) :: &
lhs_ta_wpsclrp ! w'sclr' turbulent advection contributions to lhs
real( kind = core_rknd ), dimension(gr%nz) :: &
rhs_ta_wprtp, & ! w'r_t' turbulent advection contributions to rhs
rhs_ta_wpthlp, & ! w'thl' turbulent advection contributions to rhs
rhs_ta_wpup, & ! w'u' turbulent advection contributions to rhs
rhs_ta_wpvp ! w'v' turbulent advection contributions to rhs
real( kind = core_rknd ), dimension(gr%nz,sclr_dim) :: &
rhs_ta_wpsclrp ! w'sclr' turbulent advection contributions to rhs
real( kind = core_rknd ), dimension(2,gr%nz) :: &
lhs_tp, & ! Turbulent production terms of w'x'
lhs_ta_xm ! Turbulent advection terms of xm
real( kind = core_rknd ), dimension(gr%nz) :: &
lhs_ac_pr2, & ! Accumulation of w'x' and w'x' pressure term 2
lhs_pr1_wprtp, & ! Pressure term 1 for w'r_t' for all grid levels
lhs_pr1_wpthlp, & ! Pressure term 1 for w'thl' for all grid levels
lhs_pr1_wpsclrp ! Pressure term 1 for w'sclr' for all grid levels
logical :: &
l_scalar_calc ! True if sclr_dim > 0
! -------------------- Begin Code --------------------
! Check whether the passive scalars are present.
if ( sclr_dim > 0 ) then
l_scalar_calc = .true.
else
l_scalar_calc = .false.
end if
if ( l_clip_semi_implicit &
.or. ( ( iiPDF_type == iiPDF_new ) &
.and. ( .not. l_explicit_turbulent_adv_wpxp ) ) ) then
nrhs = 1
else
if ( l_predict_upwp_vpwp ) then
nrhs = 4+sclr_dim
else
nrhs = 2+sclr_dim
endif
endif
! Save values of predictive fields to be printed in case of crash.
rtm_old = rtm
wprtp_old = wprtp
thlm_old = thlm
wpthlp_old = wpthlp
if ( sclr_dim > 0 ) then
sclrm_old = sclrm
wpsclrp_old = wpsclrp
endif ! sclr_dim > 0
if ( l_predict_upwp_vpwp ) then
um_old = um
upwp_old = upwp
vm_old = vm
vpwp_old = vpwp
endif ! l_predict_upwp_vpwp
! Compute C6 and C7 as a function of Skw
! The if...then is just here to save compute time
if ( abs(C6rt-C6rtb) > abs(C6rt+C6rtb)*eps/2 ) then
C6rt_Skw_fnc(1:gr%nz) = C6rtb + (C6rt-C6rtb) &
*EXP( -one_half * (Skw_zm(1:gr%nz)/C6rtc)**2 )
else
C6rt_Skw_fnc(1:gr%nz) = C6rtb
endif
if ( abs(C6thl-C6thlb) > abs(C6thl+C6thlb)*eps/2 ) then
C6thl_Skw_fnc(1:gr%nz) = C6thlb + (C6thl-C6thlb) &
*EXP( -one_half * (Skw_zm(1:gr%nz)/C6thlc)**2 )
else
C6thl_Skw_fnc(1:gr%nz) = C6thlb
endif
! Compute C7_Skw_fnc
if ( l_use_C7_Richardson ) then
! New formulation based on Richardson number
C7_Skw_fnc = Cx_fnc_Richardson
else
if ( abs(C7-C7b) > abs(C7+C7b)*eps/2 ) then
C7_Skw_fnc(1:gr%nz) = C7b + (C7-C7b) &
*EXP( -one_half * (Skw_zm(1:gr%nz)/C7c)**2 )
else
C7_Skw_fnc(1:gr%nz) = C7b
endif
! Damp C7 as a function of Lscale in stably stratified regions
C7_Skw_fnc = damp_coefficient( C7, C7_Skw_fnc, &
C7_Lscale0, wpxp_L_thresh, Lscale )
end if ! l_use_C7_Richardson
! Damp C6 as a function of Lscale in stably stratified regions
C6rt_Skw_fnc = damp_coefficient( C6rt, C6rt_Skw_fnc, &
C6rt_Lscale0, wpxp_L_thresh, Lscale )
C6thl_Skw_fnc = damp_coefficient( C6thl, C6thl_Skw_fnc, &
C6thl_Lscale0, wpxp_L_thresh, Lscale )
! C6rt_Skw_fnc = C6rt
! C6thl_Skw_fnc = C6thl
! C7_Skw_fnc = C7
if ( l_stats_samp ) then
call stat_update_var( iC7_Skw_fnc, C7_Skw_fnc, stats_zm )
call stat_update_var( iC6rt_Skw_fnc, C6rt_Skw_fnc, stats_zm )
call stat_update_var( iC6thl_Skw_fnc, C6thl_Skw_fnc, stats_zm )
end if
if ( clubb_at_least_debug_level( 0 ) ) then
! Assertion check for C7_Skw_fnc
if ( any( C7_Skw_fnc(:) > one ) .or. any( C7_Skw_fnc(:) < zero ) ) then
write(fstderr,*) "The C7_Skw_fnc variable is outside the valid range"
err_code = clubb_fatal_error
return
end if
end if
! Define the Coefficent of Eddy Diffusivity for the wpthlp and wprtp.
! Kw6 is used for wpthlp and wprtp, which are located on momentum levels.
! Kw6 is located on thermodynamic levels.
! Kw6 = c_K6 * Kh_zt
Kw6(1:gr%nz) = c_K6 * Kh_zt(1:gr%nz)
! Find the number of grid levels, both upwards and downwards, that can
! have an effect on the central thermodynamic level during the course of
! one time step due to turbulent advection. This is used as part of the
! monotonic turbulent advection scheme.
call calc_turb_adv_range( dt, w_1_zm, w_2_zm, varnce_w_1_zm, varnce_w_2_zm, & ! In
mixt_frac_zm, & ! In
low_lev_effect, high_lev_effect ) ! Out
! Calculate 1st pressure terms for w'r_t', w'thl', and w'sclr'.
call wpxp_term_pr1_lhs_all( C6rt_Skw_fnc, C6thl_Skw_fnc, C7_Skw_fnc, & ! Intent(in)
tau_C6_zm, l_scalar_calc, & ! Intent(in)
lhs_pr1_wprtp, lhs_pr1_wpthlp, lhs_pr1_wpsclrp ) ! Intent(out)
call calc_xm_wpxp_ta_terms( wprtp, wp2rtp, wpthlp, wp2thlp, wpsclrp, wp2sclrp, &
rho_ds_zt, invrs_rho_ds_zm, rho_ds_zm, &
sigma_sqd_w, wp3_on_wp2, wp3_on_wp2_zt, &
pdf_implicit_coefs_terms, &
l_explicit_turbulent_adv_wpxp, l_predict_upwp_vpwp, &
l_scalar_calc, &
l_upwind_wpxp_ta, &
lhs_ta_wprtp, lhs_ta_wpthlp, lhs_ta_wpup, &
lhs_ta_wpvp, lhs_ta_wpsclrp, &
rhs_ta_wprtp, rhs_ta_wpthlp, rhs_ta_wpup, &
rhs_ta_wpvp, rhs_ta_wpsclrp )
! Calculate various terms that are the same between all LHS matricies
call calc_xm_wpxp_lhs_terms( Kh_zm, wm_zm, wm_zt, wp2, & ! In
Kw6, C7_Skw_fnc, invrs_rho_ds_zt, & ! In
rho_ds_zm, l_implemented, em, & ! In
Lscale, thlm, exner, rtm, rcm, p_in_Pa, thvm, & ! In
ice_supersat_frac, & ! In
l_diffuse_rtm_and_thlm, & ! In
l_stability_correct_Kh_N2_zm, & ! In
l_upwind_xm_ma, & ! In
l_brunt_vaisala_freq_moist, & ! In
l_use_thvm_in_bv_freq, & ! In
lhs_diff_zm, lhs_diff_zt, lhs_ma_zt, lhs_ma_zm, & ! Out
lhs_tp, lhs_ta_xm, lhs_ac_pr2 ) ! Out
! Setup and decompose matrix for each variable.
if ( l_clip_semi_implicit &
.or. ( ( iiPDF_type == iiPDF_new ) &
.and. ( .not. l_explicit_turbulent_adv_wpxp ) ) ) then
! LHS matrices are unique, multiple band solves required
call solve_xm_wpxp_with_multiple_lhs( dt, l_iter, nrhs, wm_zt, wp2, & ! In
l_clip_semi_implicit, & ! In
rtpthvp, rtm_forcing, wprtp_forcing, thlpthvp, & ! In
thlm_forcing, wpthlp_forcing, rho_ds_zm, & ! In
rho_ds_zt, invrs_rho_ds_zm, invrs_rho_ds_zt, & ! In
thv_ds_zm, rtp2, thlp2, l_implemented, & ! In
sclrpthvp, sclrm_forcing, sclrp2, & ! In
low_lev_effect, high_lev_effect, C7_Skw_fnc, & ! In
lhs_diff_zm, lhs_diff_zt, lhs_ma_zt, lhs_ma_zm, & ! In
lhs_ta_wprtp, lhs_ta_wpthlp, lhs_ta_wpsclrp, & ! In
rhs_ta_wprtp, rhs_ta_wpthlp, rhs_ta_wpsclrp, & ! In
lhs_tp, lhs_ta_xm, lhs_ac_pr2, lhs_pr1_wprtp, & ! In
lhs_pr1_wpthlp, lhs_pr1_wpsclrp, & ! In
l_predict_upwp_vpwp, & ! In
l_diffuse_rtm_and_thlm, & ! In
l_upwind_xm_ma, & ! In
l_tke_aniso, & ! In
rtm, wprtp, thlm, wpthlp, sclrm, wpsclrp ) ! Out
else
! LHS matrices are equivalent, only one solve required
call solve_xm_wpxp_with_single_lhs( dt, l_iter, nrhs, wm_zt, wp2, tau_C6_zm, & ! In
rtpthvp, rtm_forcing, wprtp_forcing, thlpthvp, & ! In
thlm_forcing, wpthlp_forcing, rho_ds_zm, & ! In
rho_ds_zt, invrs_rho_ds_zm, invrs_rho_ds_zt, & ! In
thv_ds_zm, rtp2, thlp2, l_implemented, & ! In
sclrpthvp, sclrm_forcing, sclrp2, um_forcing, & ! In
vm_forcing, ug, vg, uprcp, vprcp, rc_coef, fcor, & ! In
up2, vp2, & ! In
low_lev_effect, high_lev_effect, & ! In
C6rt_Skw_fnc, C6thl_Skw_fnc, C7_Skw_fnc, & ! In
lhs_diff_zm, lhs_diff_zt, lhs_ma_zt, lhs_ma_zm, & ! In
lhs_ta_wprtp, & ! In
rhs_ta_wprtp, rhs_ta_wpthlp, rhs_ta_wpup, & ! In
rhs_ta_wpvp, rhs_ta_wpsclrp, & ! In
lhs_tp, lhs_ta_xm, lhs_ac_pr2, lhs_pr1_wprtp, & ! In
lhs_pr1_wpthlp, lhs_pr1_wpsclrp, & ! In
l_predict_upwp_vpwp, & ! In
l_diffuse_rtm_and_thlm, & ! In
l_upwind_xm_ma, & ! In
l_tke_aniso, & ! In
rtm, wprtp, thlm, wpthlp, & ! Out
sclrm, wpsclrp, um, upwp, vm,vpwp ) ! Out
endif ! l_clip_semi_implicit &
! .or. ( ( iiPDF_type == iiPDF_new ) &
! .and. ( .not. l_explicit_turbulent_adv_wpxp ) )
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
call error_prints_xm_wpxp( dt, sigma_sqd_w, wm_zm, wm_zt, wp2, &
Lscale, wp3_on_wp2, wp3_on_wp2_zt, &
Kh_zt, Kh_zm, tau_C6_zm, Skw_zm, &
wp2rtp, rtpthvp, rtm_forcing, &
wprtp_forcing, rtm_ref, wp2thlp, &
thlpthvp, thlm_forcing, &
wpthlp_forcing, thlm_ref, rho_ds_zm, &
rho_ds_zt, invrs_rho_ds_zm, &
invrs_rho_ds_zt, thv_ds_zm, rtp2, &
thlp2, w_1_zm, w_2_zm, &
varnce_w_1_zm, varnce_w_2_zm, &
mixt_frac_zm, l_implemented, em, &
wp2sclrp, sclrpthvp, sclrm_forcing, &
sclrp2, exner, rcm, p_in_Pa, thvm, &
Cx_fnc_Richardson, &
pdf_implicit_coefs_terms, &
um_forcing, vm_forcing, ug, vg, &
wpthvp, fcor, um_ref, vm_ref, up2, &
vp2, uprcp, vprcp, rc_coef, rtm, &
wprtp, thlm, wpthlp, sclrm, wpsclrp, &
um, upwp, vm, vpwp, rtm_old, &
wprtp_old, thlm_old, wpthlp_old, &
sclrm_old, wpsclrp_old, um_old, &
upwp_old, vm_old, vpwp_old, &
l_predict_upwp_vpwp )
end if
end if
if ( rtm_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( irtm_sdmp, rtm / dt, stats_zt )
endif
rtm(1:gr%nz) = sponge_damp_xm( dt, gr%zt, rtm_ref(1:gr%nz), &
rtm(1:gr%nz), rtm_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( irtm_sdmp, rtm / dt, stats_zt )
endif
endif ! rtm_sponge_damp_settings%l_sponge_damping
if ( thlm_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( ithlm_sdmp, thlm / dt, stats_zt )
endif
thlm(1:gr%nz) = sponge_damp_xm( dt, gr%zt, thlm_ref(1:gr%nz), &
thlm(1:gr%nz), thlm_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( ithlm_sdmp, thlm / dt, stats_zt )
endif
endif ! thlm_sponge_damp_settings%l_sponge_damping
if ( l_predict_upwp_vpwp ) then
if ( uv_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( ium_sdmp, um / dt, stats_zt )
call stat_begin_update( ivm_sdmp, vm / dt, stats_zt )
endif
um(1:gr%nz) = sponge_damp_xm( dt, gr%zt, um_ref(1:gr%nz), &
um(1:gr%nz), uv_sponge_damp_profile )
vm(1:gr%nz) = sponge_damp_xm( dt, gr%zt, vm_ref(1:gr%nz), &
vm(1:gr%nz), uv_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( ium_sdmp, um / dt, stats_zt )
call stat_end_update( ivm_sdmp, vm / dt, stats_zt )
endif
endif ! uv_sponge_damp_settings%l_sponge_damping
! Adjust um and vm if nudging is turned on.
if ( l_uv_nudge ) then
! Reflect nudging in budget
if ( l_stats_samp ) then
call stat_begin_update( ium_ndg, um / dt, stats_zt )
call stat_begin_update( ivm_ndg, vm / dt, stats_zt )
endif
um(1:gr%nz) &
= um(1:gr%nz) - ( ( um(1:gr%nz) - um_ref(1:gr%nz) ) * (dt/ts_nudge) )
vm(1:gr%nz) &
= vm(1:gr%nz) - ( ( vm(1:gr%nz) - vm_ref(1:gr%nz) ) * (dt/ts_nudge) )
! Reflect nudging in budget
if ( l_stats_samp ) then
call stat_end_update( ium_ndg, um / dt, stats_zt )
call stat_end_update( ivm_ndg, vm / dt, stats_zt )
endif
endif ! l_uv_nudge
if ( l_stats_samp ) then
call stat_update_var( ium_ref, um_ref, stats_zt )
call stat_update_var( ivm_ref, vm_ref, stats_zt )
endif
endif ! l_predict_upwp_vpwp
return
end subroutine advance_xm_wpxp
!======================================================================================
subroutine xm_wpxp_lhs( l_iter, dt, wpxp, wm_zt, C7_Skw_fnc, & ! In
invrs_rho_ds_zt, wpxp_upper_lim, wpxp_lower_lim, & ! In
l_implemented, lhs_diff_zm, lhs_diff_zt, & ! In
lhs_ma_zm, lhs_ma_zt, lhs_ta_wpxp, lhs_ta_xm, & ! In
lhs_tp, lhs_pr1, lhs_ac_pr2, & ! In
l_diffuse_rtm_and_thlm, & ! In
lhs ) ! Out
! Description:
! Compute LHS band diagonal matrix for xm and w'x'.
! This subroutine computes the implicit portion of
! the xm and w'x' equations.
!
!
! Notes:
!
! Boundary conditions:
! The turbulent flux (wpxp) use fixed-point boundary conditions at both the
! upper and lower boundaries. Therefore, anything set in the wpxp loop
! at both the upper and lower boundaries would be overwritten here.
! However, the wpxp loop does not extend to the boundary levels. An array
! with a value of 1 at the main diagonal on the left-hand side and with
! values of 0 at all other diagonals on the left-hand side will preserve the
! right-hand side value at that level. The value of xm at level k = 1,
! which is below the model surface, is preserved and then overwritten to
! match the new value of xm at level k = 2.
!
! xm(1) wpxp(1) ... wpxp(nzmax)
! [ 0.0 0.0 0.0 ]
! [ 0.0 0.0 0.0 ]
! [ 1.0 1.0 ... 1.0 ]
! [ 0.0 0.0 0.0 ]
! [ 0.0 0.0 0.0 ]
!
!
! LHS turbulent advection (ta) term:
! An "over-implicit" weighted time step is applied to this term.
! The weight of the implicit portion of this term is controlled by
! the factor gamma_over_implicit_ts (abbreviated "gamma" in the
! equation in order to balance a weight that is not equal to 1,
! such that:
! -y(t) * [ gamma * X(t+1) + ( 1 - gamma ) * X(t) ] + RHS;
! where X is the variable that is being solved for in a predictive
! equation (<w'x'> in this case), y(t) is the linearized portion of
! the term that gets treated implicitly, and RHS is the portion of
! the term that is always treated explicitly. A weight of greater
! than 1 can be applied to make the term more numerically stable.
!
!
! xm: Left-hand side (implicit xm portion of the code).
!
! Thermodynamic subdiagonal (lhs index: t_km1_tdiag)
! [ x xm(k-1,<t+1>) ]
! Momentum subdiagonal (lhs index: t_km1_mdiag)
! [ x wpxp(k-1,<t+1>) ]
! Thermodynamic main diagonal (lhs index: t_k_tdiag)
! [ x xm(k,<t+1>) ]
! Momentum superdiagonal (lhs index: t_k_mdiag)
! [ x wpxp(k,<t+1>) ]
! Thermodynamic superdiagonal (lhs index: t_kp1_tdiag)
! [ x xm(k+1,<t+1>) ]
!
!
! w'x': Left-hand side (implicit w'x' portion of the code).
!
! Momentum subdiagonal (lhs index: m_km1_mdiag)
! [ x wpxp(k-1,<t+1>) ]
! Thermodynamic subdiagonal (lhs index: m_k_tdiag)
! [ x xm(k,<t+1>) ]
! Momentum main diagonal (lhs index: m_k_mdiag)
! [ x wpxp(k,<t+1>) ]
! Thermodynamic superdiagonal (lhs index: m_kp1_tdiag)
! [ x xm(k+1,<t+1>) ]
! Momentum superdiagonal (lhs index: m_kp1_mdiag)
! [ x wpxp(k+1,<t+1>) ]
!
!----------------------------------------------------------------------------------
use grid_class, only: &
gr, & ! Variable(s)
zm2zt, & ! Procedure(s)
ddzt
use constants_clubb, only: &
gamma_over_implicit_ts, & ! Constant(s)
one, &
zero
use model_flags, only: &
l_clip_semi_implicit ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
use clip_semi_implicit, only: &
clip_semi_imp_lhs ! Procedure(s)
use stats_variables, only: &
ztscr01, & ! Variable(s)
ztscr02, &
ztscr03, &
ztscr04, &
ztscr05, &
zmscr01, &
zmscr02, &
zmscr03, &
zmscr04, &
zmscr05, &
zmscr06, &
zmscr07, &
zmscr08, &
zmscr09, &
zmscr10, &
zmscr11, &
zmscr12, &
zmscr13, &
zmscr14, &
zmscr15
use stats_variables, only: &
l_stats_samp, &
ithlm_ma, &
ithlm_ta, &
irtm_ma, &
irtm_ta, &
iwpthlp_ma, &
iwpthlp_ta, &
iwpthlp_tp, &
iwpthlp_ac, &
iwpthlp_pr1, &
iwpthlp_pr2, &
iwpthlp_dp1, &
iwpthlp_sicl, &
iwprtp_ma, &
iwprtp_ta, &
iwprtp_tp, &
iwprtp_ac, &
iwprtp_pr1, &
iwprtp_pr2, &
iwprtp_dp1, &
iwprtp_sicl
use advance_helper_module, only: &
set_boundary_conditions_lhs, & ! Procedure(s)
calc_stability_correction
implicit none
! External
intrinsic :: min, max
!------------------- Input Variables -------------------
real( kind = core_rknd ), intent(in) :: &
dt ! Timestep [s]
real( kind = core_rknd ), intent(in), dimension(gr%nz) :: &
wpxp, & ! w'x' (momentum levs) at timestep (t) [un vary]
wm_zt, & ! w wind component on thermo. levels [m/s]
C7_Skw_fnc, & ! C_7 parameter with Sk_w applied [-]
invrs_rho_ds_zt, & ! Inv. dry, static density at t-levs. [m^3/kg]
wpxp_upper_lim, & ! Keeps corrs. from becoming > 1 [un vary]
wpxp_lower_lim ! Keeps corrs. from becoming < -1 [un vary]
logical, intent(in) :: &
l_implemented, & ! Flag for CLUBB being implemented in a larger model.
l_iter
!------------------- Output Variable -------------------
real( kind = core_rknd ), intent(out), dimension(nsup+nsub+1,2*gr%nz) :: &
lhs ! Implicit contributions to wpxp/xm (band diag. matrix) (LAPACK)
!------------------- Local Variables -------------------
! Indices
integer :: k
integer :: k_xm, k_wpxp
logical :: l_upper_thresh, l_lower_thresh ! flags for clip_semi_imp_lhs
real( kind = core_rknd ), dimension(3,gr%nz), intent(in) :: &
lhs_diff_zm, & ! Diffusion term for w'x'
lhs_diff_zt, & ! Diffusion term for xm
lhs_ma_zt, & ! Mean advection contributions to lhs
lhs_ma_zm, & ! Mean advection contributions to lhs
lhs_ta_wpxp ! Turbulent advection contributions to lhs
real( kind = core_rknd ), dimension(2,gr%nz), intent(in) :: &
lhs_tp, & ! Turbulent production terms of w'x'
lhs_ta_xm ! Turbulent advection terms of xm
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
lhs_ac_pr2, & ! Accumulation of w'x' and w'x' pressure term 2
lhs_pr1 ! Pressure term 1 for w'x'
logical, intent(in) :: &
l_diffuse_rtm_and_thlm ! This flag determines whether or not we want CLUBB to do diffusion
! on rtm and thlm
real (kind = core_rknd) :: &
invrs_dt
!------------------- Begin Code -------------------
! Initializations/precalculations
invrs_dt = 1.0_core_rknd / dt
! Lower boundary for xm, lhs(:,1)
lhs(1,1) = 0.0_core_rknd
lhs(2,1) = 0.0_core_rknd
lhs(3,1) = 1.0_core_rknd
lhs(4,1) = 0.0_core_rknd
lhs(5,1) = 0.0_core_rknd
! Lower boundary for w'x', lhs(:,2)
lhs(1,2) = 0.0_core_rknd
lhs(2,2) = 0.0_core_rknd
lhs(3,2) = 1.0_core_rknd
lhs(4,2) = 0.0_core_rknd
lhs(5,2) = 0.0_core_rknd
! Combine xm and w'x' terms into LHS
do k = 2, gr%nz
k_xm = 2*k - 1 ! xm at odd index values
k_wpxp = 2*k ! w'x' at even index values
! ---- sum xm terms ----
lhs(1,k_xm) = zero
lhs(2,k_xm) = lhs_ta_xm(1,k)
lhs(3,k_xm) = invrs_dt
lhs(4,k_xm) = lhs_ta_xm(2,k)
lhs(5,k_xm) = zero
! ---- sum w'x' terms ----
lhs(1,k_wpxp) = lhs_ma_zm(1,k) + lhs_diff_zm(1,k) &
+ gamma_over_implicit_ts * lhs_ta_wpxp(1,k)
lhs(2,k_wpxp) = lhs_tp(1,k)
lhs(3,k_wpxp) = lhs_ma_zm(2,k) + lhs_diff_zm(2,k) + lhs_ac_pr2(k) &
+ gamma_over_implicit_ts * ( lhs_ta_wpxp(2,k) + lhs_pr1(k) )
lhs(4,k_wpxp) = lhs_tp(2,k)
lhs(5,k_wpxp) = lhs_ma_zm(3,k) + lhs_diff_zm(3,k) &
+ gamma_over_implicit_ts * lhs_ta_wpxp(3,k)
enddo
! Upper boundary for w'x', , lhs(:,2*gr%nz)
! These were set in the loop above for simplicity, so they must be set properly here
lhs(1,2*gr%nz) = 0.0_core_rknd
lhs(2,2*gr%nz) = 0.0_core_rknd
lhs(3,2*gr%nz) = 1.0_core_rknd
lhs(4,2*gr%nz) = 0.0_core_rknd
lhs(5,2*gr%nz) = 0.0_core_rknd
! LHS time tendency
if ( l_iter ) then
do k = 2, gr%nz-1
k_wpxp = 2*k
lhs(3,k_wpxp) = lhs(3,k_wpxp) + invrs_dt
end do
endif
! Calculate diffusion terms for all thermodynamic grid level
if ( l_diffuse_rtm_and_thlm ) then