forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advance_xp2_xpyp_module.F90
4865 lines (3973 loc) · 212 KB
/
advance_xp2_xpyp_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_xp2_xpyp_module
! Description:
! Contains the subroutine advance_xp2_xpyp and ancillary functions.
!-----------------------------------------------------------------------
implicit none
public :: advance_xp2_xpyp, &
update_xp2_mc
private :: xp2_xpyp_lhs, &
xp2_xpyp_solve, &
xp2_xpyp_uv_rhs, &
xp2_xpyp_rhs, &
xp2_xpyp_implicit_stats, &
term_tp, &
term_dp1_lhs, &
term_dp1_rhs, &
term_pr1, &
term_pr2
private ! Set default scope
! Private named constants to avoid string comparisons
integer, parameter, private :: &
xp2_xpyp_rtp2 = 1, & ! Named constant for rtp2 solves
xp2_xpyp_thlp2 = 2, & ! Named constant for thlp2 solves
xp2_xpyp_rtpthlp = 3, & ! Named constant for rtpthlp solves
xp2_xpyp_up2_vp2 = 4, & ! Named constant for up2_vp2 solves
xp2_xpyp_vp2 = 6, & ! Named constant for vp2 solves
xp2_xpyp_up2 = 5, & ! Named constant for up2 solves
xp2_xpyp_scalars = 7, & ! Named constant for scalar solves
xp2_xpyp_sclrp2 = 8, & ! Named constant for sclrp2 solves
xp2_xpyp_sclrprtp = 9, & ! Named constant for sclrprtp solves
xp2_xpyp_sclrpthlp = 10, & ! Named constant for sclrpthlp solves
xp2_xpyp_single_lhs = 11 ! Named constant for single lhs solve
contains
!=============================================================================
subroutine advance_xp2_xpyp( tau_zm, wm_zm, rtm, wprtp, thlm, & ! In
wpthlp, wpthvp, um, vm, wp2, wp2_zt, & ! In
wp3, upwp, vpwp, sigma_sqd_w, Skw_zm, & ! In
wprtp2, wpthlp2, wprtpthlp, & ! In
Kh_zt, rtp2_forcing, thlp2_forcing, & ! In
rtpthlp_forcing, rho_ds_zm, rho_ds_zt, & ! In
invrs_rho_ds_zm, thv_ds_zm, cloud_frac, & ! In
Lscale, wp3_on_wp2, wp3_on_wp2_zt, & ! In
pdf_implicit_coefs_terms, & ! In
l_iter, dt, & ! In
sclrm, wpsclrp, & ! In
wpsclrp2, wpsclrprtp, wpsclrpthlp, & ! In
wp2_splat, & ! In
l_predict_upwp_vpwp, & ! In
l_min_xp2_from_corr_wx, & ! In
l_C2_cloud_frac, & ! In
l_upwind_xpyp_ta, & ! In
l_single_C2_Skw, & ! In
rtp2, thlp2, rtpthlp, up2, vp2, & ! Inout
sclrp2, sclrprtp, sclrpthlp) ! Inout
! Description:
! Prognose scalar variances, scalar covariances, and horizontal turbulence components.
! References:
! https://arxiv.org/pdf/1711.03675v1.pdf#nameddest=url:xpyp_eqns
!
! https://arxiv.org/pdf/1711.03675v1.pdf#nameddest=url:up2_vp2_eqns
!
! Eqn. 13, 14, 15 on p. 3545 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 4:
! /Steady-state solution for the variances/
!-----------------------------------------------------------------------
use constants_clubb, only: &
w_tol_sqd, & ! Constant(s)
rt_tol, &
thl_tol, &
max_mag_correlation_flux, &
cloud_frac_min, &
fstderr, &
one, &
two_thirds, &
one_half, &
one_third, &
zero, &
zero_threshold
use model_flags, only: &
l_hole_fill, & ! logical constants
l_explicit_turbulent_adv_xpyp
use parameters_tunable, only: &
C2rt, & ! Variable(s)
C2thl, &
C2rtthl, &
c_K2, &
nu2_vert_res_dep, &
c_K9, &
nu9_vert_res_dep, &
C4, &
C14, &
C5, &
C2, &
C2b, &
C2c
use parameters_model, only: &
sclr_dim, & ! Variable(s)
sclr_tol
use grid_class, only: &
gr, & ! Variable(s)
zm2zt, & ! Procedure(s)
zt2zm
use pdf_closure_module, only: &
iiPDF_ADG1, & ! Variable(s)
iiPDF_new, &
iiPDF_new_hybrid, &
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 clip_explicit, only: &
clip_covar, & ! Procedure(s)
clip_variance, &
clip_variance_level, &
clip_sclrp2, &
clip_sclrprtp, &
clip_sclrpthlp
use sponge_layer_damping, only: &
up2_vp2_sponge_damp_settings, & ! Variable(s)
up2_vp2_sponge_damp_profile, &
sponge_damp_xp2 ! Procedure(s)
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_end_update, &
stat_modify, &
stat_update_var
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constants
use stats_variables, only: &
stats_zm, & ! Variable(s)
irtp2_cl, &
iup2_sdmp, &
ivp2_sdmp, &
iup2_cl, &
ivp2_cl, &
l_stats_samp
use array_index, only: &
iisclr_rt, &
iisclr_thl
use mean_adv, only: &
term_ma_zm_lhs_all ! Procedure(s)
use diffusion, only: &
diffusion_zm_lhs_all ! Procedure(s)
implicit none
! Intrinsic functions
intrinsic :: &
exp, sqrt, min
! Constant parameters
logical, parameter :: &
l_clip_large_rtp2 = .true. ! Clip rtp2 to be < rtm^2 * coef
real( kind = core_rknd ), parameter :: &
rtp2_clip_coef = one_half ! Coefficient appled the clipping threshold on rtp2 [-]
! Input variables
real( kind = core_rknd ), intent(in), dimension(gr%nz) :: &
tau_zm, & ! Time-scale tau on momentum levels [s]
wm_zm, & ! w-wind component on momentum levels [m/s]
rtm, & ! Total water mixing ratio (t-levs) [kg/kg]
wprtp, & ! <w'r_t'> (momentum levels) [(m/s)(kg/kg)]
thlm, & ! Liquid potential temp. (t-levs) [K]
wpthlp, & ! <w'th_l'> (momentum levels) [(m K)/s]
wpthvp, & ! <w'th_v'> (momentum levels) [(m K)/s]
um, & ! u wind (thermodynamic levels) [m/s]
vm, & ! v wind (thermodynamic levels) [m/s]
wp2, & ! <w'^2> (momentum levels) [m^2/s^2]
wp2_zt, & ! <w'^2> interpolated to thermo. levels [m^2/s^2]
wp3, & ! <w'^3> (thermodynamic levels) [m^3/s^3]
upwp, & ! <u'w'> (momentum levels) [m^2/s^2]
vpwp, & ! <v'w'> (momentum levels) [m^2/s^2]
sigma_sqd_w, & ! sigma_sqd_w (momentum levels) [-]
Skw_zm, & ! Skewness of w on momentum levels [-]
wprtp2, & ! <w'r_t'^2> (thermodynamic levels) [m/s (kg/kg)^2]
wpthlp2, & ! <w'th_l'^2> (thermodynamic levels) [m/s K^2]
wprtpthlp, & ! <w'r_t'th_l'> (thermodynamic levels) [m/s (kg/kg) K]
Kh_zt, & ! Eddy diffusivity on thermo. levels [m^2/s]
rtp2_forcing, & ! <r_t'^2> forcing (momentum levels) [(kg/kg)^2/s]
thlp2_forcing, & ! <th_l'^2> forcing (momentum levels) [K^2/s]
rtpthlp_forcing, & ! <r_t'th_l'> forcing (momentum levels) [(kg/kg)K/s]
rho_ds_zm, & ! Dry, static density on momentum levs. [kg/m^3]
rho_ds_zt, & ! Dry, static density on thermo. levels [kg/m^3]
invrs_rho_ds_zm, & ! Inv. dry, static density @ mom. levs. [m^3/kg]
thv_ds_zm, & ! Dry, base-state theta_v on mom. levs. [K]
cloud_frac, & ! Cloud fraction (thermodynamic levels) [-]
Lscale, & ! Mixing length [m]
wp3_on_wp2, & ! Smoothed version of <w'^3>/<w'^2> zm [m/s]
wp3_on_wp2_zt ! Smoothed version of <w'^3>/<w'^2> zt [m/s]
type(implicit_coefs_terms), intent(in) :: &
pdf_implicit_coefs_terms ! Implicit coefs / explicit terms [units vary]
logical, intent(in) :: l_iter ! Whether variances are prognostic
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep [s]
! Passive scalar input
real( kind = core_rknd ), intent(in), dimension(gr%nz, sclr_dim) :: &
sclrm, & ! Mean value; pass. scalar (t-levs.) [{sclr units}]
wpsclrp, & ! <w'sclr'> (momentum levels) [m/s{sclr units}]
wpsclrp2, & ! <w'sclr'^2> (thermodynamic levels) [m/s{sclr units}^2]
wpsclrprtp, & ! <w'sclr'r_t'> (thermo. levels) [m/s{sclr units)kg/kg]
wpsclrpthlp ! <w'sclr'th_l'> (thermo. levels) [m/s{sclr units}K]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
wp2_splat ! Gustiness tendency for wp2 equation
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_min_xp2_from_corr_wx, & ! Flag to base the threshold minimum value of xp2 (rtp2 and thlp2)
! on keeping the overall correlation of w and x within the limits
! of -max_mag_correlation_flux to max_mag_correlation_flux.
l_C2_cloud_frac, & ! Flag to use cloud fraction to adjust the value of the turbulent
! dissipation coefficient, C2.
l_upwind_xpyp_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 rtp2, thlp2,
! up2, vp2, sclrp2, rtpthlp, sclrprtp, & sclrpthlp.
l_single_C2_Skw ! Use a single Skewness dependent C2 for rtp2, thlp2, and rtpthlp
! Input/Output variables
! An attribute of (inout) is also needed to import the value of the variances
! at the surface. Brian. 12/18/05.
real( kind = core_rknd ), intent(inout), dimension(gr%nz) :: &
rtp2, & ! <r_t'^2> [(kg/kg)^2]
thlp2, & ! <th_l'^2> [K^2]
rtpthlp, & ! <r_t'th_l'> [(kg K)/kg]
up2, & ! <u'^2> [m^2/s^2]
vp2 ! <v'^2> [m^2/s^2]
! Passive scalar output
real( kind = core_rknd ), intent(inout), dimension(gr%nz, sclr_dim) :: &
sclrp2, sclrprtp, sclrpthlp
! Local Variables
real( kind = core_rknd ), dimension(gr%nz) :: &
C2sclr_1d, C2rt_1d, C2thl_1d, C2rtthl_1d, &
C4_C14_1d ! Parameters C4 and C14 combined for simplicity
real( kind = core_rknd ) :: &
threshold ! Minimum value for variances [units vary]
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs ! Tridiagonal matrix
real( kind = core_rknd ), dimension(gr%nz,2) :: &
uv_rhs, &! RHS vectors of tridiagonal system for up2/vp2
uv_solution ! Solution to the tridiagonal system for up2/vp2
! Eddy Diffusion for Variances and Covariances.
real( kind = core_rknd ), dimension(gr%nz) :: &
Kw2, & ! For rtp2, thlp2, rtpthlp, and passive scalars [m^2/s]
Kw9 ! For up2 and vp2 [m^2/s]
real( kind = core_rknd ), dimension(gr%nz) :: &
rtpthlp_chnge ! Net change in r_t'th_l' due to clipping [(kg/kg) K]
real( kind = core_rknd ), dimension(gr%nz,sclr_dim) :: &
sclrprtp_chnge, & ! Net change in sclr'r_t' due to clipping [units vary]
sclrpthlp_chnge ! Net change in sclr'th_l' due to clipping [units vary]
! Turbulent advection terms
! Implicit (LHS) turbulent advection terms
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs_ta_wprtp2, & ! For <w'rt'^2>
lhs_ta_wpthlp2, & ! For <w'thl'^2>
lhs_ta_wprtpthlp, & ! For <w'rt'thl'>
lhs_ta_wpup2, & ! For <w'u'^2>
lhs_ta_wpvp2 ! For <w'v'^2>
! Explicit (RHS) turbulent advection terms
real( kind = core_rknd ), dimension(gr%nz) :: &
rhs_ta_wprtp2, & ! For <w'rt'^2>
rhs_ta_wpthlp2, & ! For <w'thl'^2>
rhs_ta_wprtpthlp, & ! For <w'rt'thl'>
rhs_ta_wpup2, & ! For <w'u'^2>
rhs_ta_wpvp2 ! For <w'v'^2>
! Implicit (LHS) turbulent advection terms for scalars
real( kind = core_rknd ), dimension(3,gr%nz,sclr_dim) :: &
lhs_ta_wpsclrp2, & ! For <w'sclr'^2>
lhs_ta_wprtpsclrp, & ! For <w'rt'sclr'>
lhs_ta_wpthlpsclrp ! For <w'thl'sclr'>
! Explicit (RHS) turbulent advection terms for scalars
real( kind = core_rknd ), dimension(gr%nz,sclr_dim) :: &
rhs_ta_wpsclrp2, & ! For <w'sclr'^2>
rhs_ta_wprtpsclrp, & ! For <w'sclr'rt'>
rhs_ta_wpthlpsclrp ! For <w'sclr'thl'>
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs_diff, & ! Diffusion contributions to lhs, dissipation term 2
lhs_diff_uv, & ! Diffusion contributions to lhs for <w'u'^2> and <w'v'^2>
lhs_ma ! Mean advection contributions to lhs
logical :: l_scalar_calc, l_first_clip_ts, l_last_clip_ts
! Minimum value of cloud fraction to multiply C2 by to calculate the
! adjusted value of C2.
real( kind = core_rknd ), parameter :: &
min_cloud_frac_mult = 0.10_core_rknd
! Loop indices
integer :: i, k
!---------------------------- Begin Code ----------------------------------
if ( clubb_at_least_debug_level( 0 ) ) then
! Assertion check for C5
if ( C5 > one .or. C5 < zero ) then
write(fstderr,*) "The C5 variable is outside the valid range"
err_code = clubb_fatal_error
return
end if
end if
if ( l_single_C2_Skw ) then
! Use a single value of C2 for all equations.
C2rt_1d = C2b + ( C2 - C2b ) * exp( -one_half * ( Skw_zm / C2c )**2 )
if ( l_C2_cloud_frac ) then
do k = 1, gr%nz, 1
if ( cloud_frac(k) >= cloud_frac_min ) then
C2rt_1d(k) &
= C2rt_1d(k) * max( min_cloud_frac_mult, cloud_frac(k) )
endif ! cloud_frac(k) >= cloud_frac_min
enddo ! k = 1, gr%nz, 1
endif ! l_C2_cloud_frac
C2thl_1d = C2rt_1d
C2rtthl_1d = C2rt_1d
C2sclr_1d = C2rt_1d
else ! .not. l_single_C2_Skw
! Use 3 different values of C2 for rtp2, thlp2, rtpthlp.
if ( l_C2_cloud_frac ) then
do k = 1, gr%nz, 1
if ( cloud_frac(k) >= cloud_frac_min ) then
C2rt_1d(k) = C2rt * max( min_cloud_frac_mult, cloud_frac(k) )
C2thl_1d(k) = C2thl * max( min_cloud_frac_mult, cloud_frac(k) )
C2rtthl_1d(k) = C2rtthl &
* max( min_cloud_frac_mult, cloud_frac(k) )
else ! cloud_frac(k) < cloud_frac_min
C2rt_1d(k) = C2rt
C2thl_1d(k) = C2thl
C2rtthl_1d(k) = C2rtthl
endif ! cloud_frac(k) >= cloud_frac_min
enddo ! k = 1, gr%nz, 1
else
C2rt_1d = C2rt
C2thl_1d = C2thl
C2rtthl_1d = C2rtthl
endif ! l_C2_cloud_frac
C2sclr_1d = C2rt ! Use rt value for now
endif ! l_single_C2_Skw
! Combine C4 and C14 for simplicity
C4_C14_1d(1:gr%nz) = ( two_thirds * C4 ) + ( one_third * C14 )
! Are we solving for passive scalars as well?
if ( sclr_dim > 0 ) then
l_scalar_calc = .true.
else
l_scalar_calc = .false.
end if
! Define the Coefficent of Eddy Diffusivity for the variances
! and covariances.
do k = 1, gr%nz, 1
! Kw2 is used for variances and covariances rtp2, thlp2, rtpthlp, and
! passive scalars. The variances and covariances are located on the
! momentum levels. Kw2 is located on the thermodynamic levels.
! Kw2 = c_K2 * Kh_zt
Kw2(k) = c_K2 * Kh_zt(k)
! Kw9 is used for variances up2 and vp2. The variances are located on
! the momentum levels. Kw9 is located on the thermodynamic levels.
! Kw9 = c_K9 * Kh_zt
Kw9(k) = c_K9 * Kh_zt(k)
enddo
! Calculate all the explicit and implicit turbulent advection terms
call calc_xp2_xpyp_ta_terms( wprtp, wprtp2, wpthlp, wpthlp2, wprtpthlp, & ! In
rtp2, thlp2, rtpthlp, upwp, vpwp, up2, vp2, wp2, & ! In
wp2_zt, wpsclrp, wpsclrp2, wpsclrprtp, wpsclrpthlp, & ! In
sclrp2, sclrprtp, sclrpthlp, & ! In
rho_ds_zt, invrs_rho_ds_zm, rho_ds_zm, & ! In
wp3_on_wp2, wp3_on_wp2_zt, sigma_sqd_w, & ! In
pdf_implicit_coefs_terms, l_scalar_calc, & ! In
l_upwind_xpyp_ta, & ! In
lhs_ta_wprtp2, lhs_ta_wpthlp2, lhs_ta_wprtpthlp, & ! Out
lhs_ta_wpup2, lhs_ta_wpvp2, lhs_ta_wpsclrp2, & ! Out
lhs_ta_wprtpsclrp, lhs_ta_wpthlpsclrp, & ! Out
rhs_ta_wprtp2, rhs_ta_wpthlp2, rhs_ta_wprtpthlp, & ! Out
rhs_ta_wpup2, rhs_ta_wpvp2, rhs_ta_wpsclrp2, & ! Out
rhs_ta_wprtpsclrp, rhs_ta_wpthlpsclrp ) ! Out
! Calculate LHS eddy diffusion term: dissipation term 2 (dp2). This is the
! diffusion term for all LHS matrices except <w'u'^2> and <w'v'^2>
call diffusion_zm_lhs_all( Kw2(:), nu2_vert_res_dep(:), & ! In
gr%invrs_dzt(:), gr%invrs_dzm(:), & ! In
lhs_diff(:,:) ) ! Out
! Calculate LHS mean advection (ma) term, this term is equal for all LHS matrices
call term_ma_zm_lhs_all( wm_zm(:), gr%invrs_dzm(:), & ! In
lhs_ma(:,:) ) ! Out
if ( ( C2rt == C2thl .and. C2rt == C2rtthl ) .and. &
( l_explicit_turbulent_adv_xpyp .or. &
.not. l_explicit_turbulent_adv_xpyp .and. iiPDF_type == iiPDF_ADG1 ) ) then
! All left hand side matricies are equal for rtp2, thlp2, rtpthlp, and scalars.
! Thus only one solve is neccesary, using combined right hand sides
call solve_xp2_xpyp_with_single_lhs( C2rt_1d, tau_zm, rtm, thlm, wprtp, wpthlp, & ! In
rtp2_forcing, thlp2_forcing, rtpthlp_forcing, & ! In
sclrm, wpsclrp, & ! In
lhs_ta_wprtp2, lhs_ma, lhs_diff, & ! In
rhs_ta_wprtp2, rhs_ta_wpthlp2, & ! In
rhs_ta_wprtpthlp, rhs_ta_wpsclrp2, & ! In
rhs_ta_wprtpsclrp, rhs_ta_wpthlpsclrp, & ! In
dt, l_iter, l_scalar_calc, & ! In
rtp2, thlp2, rtpthlp, & ! Out
sclrp2, sclrprtp, sclrpthlp ) ! Out
else
! Left hand sides are potentially different, this requires multiple solves
call solve_xp2_xpyp_with_multiple_lhs( C2rt_1d, C2thl_1d, C2rtthl_1d, C2sclr_1d, & ! In
tau_zm, rtm, thlm, wprtp, wpthlp, & ! In
rtp2_forcing, thlp2_forcing, rtpthlp_forcing, & ! In
sclrm, wpsclrp, & ! In
lhs_ta_wprtp2, lhs_ta_wpthlp2, & ! In
lhs_ta_wprtpthlp, lhs_ta_wpsclrp2, & ! In
lhs_ta_wprtpsclrp, lhs_ta_wpthlpsclrp, & ! In
lhs_ma, lhs_diff, & ! In
rhs_ta_wprtp2, rhs_ta_wpthlp2, & ! In
rhs_ta_wprtpthlp, rhs_ta_wpsclrp2, & ! In
rhs_ta_wprtpsclrp, rhs_ta_wpthlpsclrp, & ! In
dt, l_iter, l_scalar_calc, & ! In
rtp2, thlp2, rtpthlp, & ! Out
sclrp2, sclrprtp, sclrpthlp ) ! Out
end if
if ( l_stats_samp ) then
call xp2_xpyp_implicit_stats( xp2_xpyp_rtp2, rtp2 ) ! Intent(in)
call xp2_xpyp_implicit_stats( xp2_xpyp_thlp2, thlp2 ) ! Intent(in)
call xp2_xpyp_implicit_stats( xp2_xpyp_rtpthlp, rtpthlp ) ! Intent(in)
end if
!!!!!***** u'^2 / v'^2 *****!!!!!
! Calculate LHS eddy diffusion term: dissipation term 2 (dp2), for <w'u'^2> and <w'v'^2>
call diffusion_zm_lhs_all( Kw9(:), nu9_vert_res_dep(:), & ! In
gr%invrs_dzt(:), gr%invrs_dzm(:), & ! In
lhs_diff_uv(:,:) ) ! Out
if ( iiPDF_type == iiPDF_new_hybrid ) then
! Different LHS required for up2 and vp2.
! Solve for up2
! Implicit contributions to term up2
call xp2_xpyp_lhs( dt, l_iter, tau_zm, C4_C14_1d, & ! In
lhs_ta_wpup2, lhs_ma, lhs_diff_uv, & ! In
lhs ) ! Out
! Explicit contributions to up2
call xp2_xpyp_uv_rhs( xp2_xpyp_up2, dt, l_iter, & ! In
wp2, wp2_zt, wpthvp, & ! In
Lscale, C4_C14_1d, tau_zm, & ! In
um, vm, upwp, vpwp, up2, vp2, & ! In
thv_ds_zm, C4, C5, C14, wp2_splat, & ! In
lhs_ta_wpup2, rhs_ta_wpup2, & ! In
uv_rhs(:,1) ) ! Out
! Solve the tridiagonal system
call xp2_xpyp_solve( xp2_xpyp_up2_vp2, 1, & ! Intent(in)
uv_rhs, lhs, & ! Intent(inout)
uv_solution ) ! Intent(out)
up2(1:gr%nz) = uv_solution(1:gr%nz,1)
if ( l_stats_samp ) then
call xp2_xpyp_implicit_stats( xp2_xpyp_up2, up2 ) ! Intent(in)
endif
! Solve for vp2
! Implicit contributions to term vp2
call xp2_xpyp_lhs( dt, l_iter, tau_zm, C4_C14_1d, & ! In
lhs_ta_wpvp2, lhs_ma, lhs_diff_uv, & ! In
lhs ) ! Out
! Explicit contributions to vp2
call xp2_xpyp_uv_rhs( xp2_xpyp_vp2, dt, l_iter, & ! In
wp2, wp2_zt, wpthvp, & ! In
Lscale, C4_C14_1d, tau_zm, & ! In
vm, um, vpwp, upwp, vp2, up2, & ! In
thv_ds_zm, C4, C5, C14, wp2_splat, & ! In
lhs_ta_wpvp2, rhs_ta_wpvp2, & ! In
uv_rhs(:,1) ) ! Out
! Solve the tridiagonal system
call xp2_xpyp_solve( xp2_xpyp_up2_vp2, 1, & ! Intent(in)
uv_rhs, lhs, & ! Intent(inout)
uv_solution ) ! Intent(out)
vp2(1:gr%nz) = uv_solution(1:gr%nz,1)
if ( l_stats_samp ) then
call xp2_xpyp_implicit_stats( xp2_xpyp_vp2, vp2 ) ! Intent(in)
endif
else ! ADG1 and other types
! ADG1 allows up2 and vp2 to use the same LHS.
! Implicit contributions to term up2/vp2
call xp2_xpyp_lhs( dt, l_iter, tau_zm, C4_C14_1d, & ! In
lhs_ta_wpup2, lhs_ma, lhs_diff_uv, & ! In
lhs ) ! Out
! Explicit contributions to up2
call xp2_xpyp_uv_rhs( xp2_xpyp_up2, dt, l_iter, & ! In
wp2, wp2_zt, wpthvp, & ! In
Lscale, C4_C14_1d, tau_zm, & ! In
um, vm, upwp, vpwp, up2, vp2, & ! In
thv_ds_zm, C4, C5, C14, wp2_splat, & ! In
lhs_ta_wpup2, rhs_ta_wpup2, & ! In
uv_rhs(:,1) ) ! Out
! Explicit contributions to vp2
call xp2_xpyp_uv_rhs( xp2_xpyp_vp2, dt, l_iter, & ! In
wp2, wp2_zt, wpthvp, & ! In
Lscale, C4_C14_1d, tau_zm, & ! In
vm, um, vpwp, upwp, vp2, up2, & ! In
thv_ds_zm, C4, C5, C14, wp2_splat, & ! In
lhs_ta_wpup2, rhs_ta_wpvp2, & ! In
uv_rhs(:,2) ) ! Out
! Solve the tridiagonal system
call xp2_xpyp_solve( xp2_xpyp_up2_vp2, 2, & ! Intent(in)
uv_rhs, lhs, & ! Intent(inout)
uv_solution ) ! Intent(out)
up2(1:gr%nz) = uv_solution(1:gr%nz,1)
vp2(1:gr%nz) = uv_solution(1:gr%nz,2)
if ( l_stats_samp ) then
call xp2_xpyp_implicit_stats( xp2_xpyp_up2, up2 ) ! Intent(in)
call xp2_xpyp_implicit_stats( xp2_xpyp_vp2, vp2 ) ! Intent(in)
endif
endif ! iiPDF_type
! Apply the positive definite scheme to variances
if ( l_hole_fill ) then
call pos_definite_variances( xp2_xpyp_rtp2, dt, rt_tol**2, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
rtp2 ) ! Intent(inout)
call pos_definite_variances( xp2_xpyp_thlp2, dt, thl_tol**2, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
thlp2 ) ! Intent(inout)
call pos_definite_variances( xp2_xpyp_up2, dt, w_tol_sqd, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
up2 ) ! Intent(inout)
call pos_definite_variances( xp2_xpyp_vp2, dt, w_tol_sqd, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
vp2 ) ! Intent(inout)
endif
! Clipping for r_t'^2
!threshold = zero_threshold
!
!where ( wp2 >= w_tol_sqd ) &
! threshold = rt_tol*rt_tol
! The value of rtp2 is not allowed to become smaller than the threshold
! value of rt_tol^2. Additionally, that threshold value may be boosted at
! any grid level in order to keep the overall correlation of w and rt
! between the values of -max_mag_correlation_flux and
! max_mag_correlation_flux by boosting rtp2 rather than by limiting the
! magnitude of wprtp.
if ( l_min_xp2_from_corr_wx ) then
! The overall correlation of w and rt is:
!
! corr_w_rt = wprtp / ( sqrt( wp2 ) * sqrt( rtp2 ) ).
!
! Squaring both sides, the equation becomes:
!
! corr_w_rt^2 = wprtp^2 / ( wp2 * rtp2 ).
!
! Using max_mag_correlation_flux for the correlation and then solving for
! the minimum of rtp2, the equation becomes:
!
! rtp2|_min = wprtp^2 / ( wp2 * max_mag_correlation_flux^2 ).
do k = 1, gr%nz, 1
threshold &
= max( rt_tol**2, &
wprtp(k)**2 / ( wp2(k) * max_mag_correlation_flux**2 ) )
call clip_variance_level( xp2_xpyp_rtp2, dt, threshold, k, & ! In
rtp2(k) ) ! In/out
enddo ! k = 1, gr%nz, 1
else
! Consider only the minimum tolerance threshold value for rtp2.
threshold = rt_tol**2
call clip_variance( xp2_xpyp_rtp2, dt, threshold, & ! Intent(in)
rtp2 ) ! Intent(inout)
endif ! l_min_xp2_from_corr_wx
! Special clipping on the variance of rt to prevent a large variance at
! higher altitudes. This is done because we don't want the PDF to extend
! into the negative, and found that for latin hypercube sampling a large
! variance aloft leads to negative samples of total water.
! -dschanen 8 Dec 2010
if ( l_clip_large_rtp2 ) then
! This overwrites stats clipping data from clip_variance
if ( l_stats_samp ) then
call stat_modify( irtp2_cl, -rtp2 / dt, stats_zm )
endif
do k = 1, gr%nz
threshold = max( rt_tol**2, rtp2_clip_coef * zt2zm( rtm, k )**2 )
if ( rtp2(k) > threshold ) then
rtp2(k) = threshold
end if
end do ! k = 1..gr%nz
if ( l_stats_samp ) then
call stat_modify( irtp2_cl, rtp2 / dt, stats_zm )
endif
end if ! l_clip_large_rtp2
! Clipping for th_l'^2
!threshold = zero_threshold
!
!where ( wp2 >= w_tol_sqd ) &
! threshold = thl_tol*thl_tol
! The value of thlp2 is not allowed to become smaller than the threshold
! value of thl_tol^2. Additionally, that threshold value may be boosted at
! any grid level in order to keep the overall correlation of w and theta-l
! between the values of -max_mag_correlation_flux and
! max_mag_correlation_flux by boosting thlp2 rather than by limiting the
! magnitude of wpthlp.
if ( l_min_xp2_from_corr_wx ) then
! The overall correlation of w and theta-l is:
!
! corr_w_thl = wpthlp / ( sqrt( wp2 ) * sqrt( thlp2 ) ).
!
! Squaring both sides, the equation becomes:
!
! corr_w_thl^2 = wpthlp^2 / ( wp2 * thlp2 ).
!
! Using max_mag_correlation_flux for the correlation and then solving for
! the minimum of thlp2, the equation becomes:
!
! thlp2|_min = wpthlp^2 / ( wp2 * max_mag_correlation_flux^2 ).
do k = 1, gr%nz, 1
threshold &
= max( thl_tol**2, &
wpthlp(k)**2 / ( wp2(k) * max_mag_correlation_flux**2 ) )
call clip_variance_level( xp2_xpyp_thlp2, dt, threshold, k, & ! In
thlp2(k) ) ! In/out
enddo ! k = 1, gr%nz, 1
else
! Consider only the minimum tolerance threshold value for thlp2.
threshold = thl_tol**2
call clip_variance( xp2_xpyp_thlp2, dt, threshold, & ! Intent(in)
thlp2 ) ! Intent(inout)
endif ! l_min_xp2_from_corr_wx
! Clipping for u'^2
! Clip negative values of up2
threshold = w_tol_sqd
call clip_variance( xp2_xpyp_up2, dt, threshold, & ! Intent(in)
up2 ) ! Intent(inout)
! Clip excessively large values of up2
if ( l_stats_samp ) then
! Store previous value in order to calculate clipping
call stat_modify( iup2_cl, -up2 / dt, & ! Intent(in)
stats_zm ) ! Intent(inout)
endif
where (up2 > 1000._core_rknd)
up2 = 1000._core_rknd
end where
if ( l_stats_samp ) then
! Store final value in order to calculate clipping
call stat_modify( iup2_cl, up2 / dt, & ! Intent(in)
stats_zm ) ! Intent(inout)
endif
! Clipping for v'^2
! Clip negative values of vp2
threshold = w_tol_sqd
call clip_variance( xp2_xpyp_vp2, dt, threshold, & ! Intent(in)
vp2 ) ! Intent(inout)
if ( l_stats_samp ) then
! Store previous value in order to calculate clipping
call stat_modify( ivp2_cl, -vp2 / dt, & ! Intent(in)
stats_zm ) ! Intent(inout)
endif
where (vp2 > 1000._core_rknd)
vp2 = 1000._core_rknd
end where
if ( l_stats_samp ) then
! Store final value in order to calculate clipping
call stat_modify( ivp2_cl, vp2 / dt, & ! Intent(in)
stats_zm ) ! Intent(inout)
endif
! When selected, apply sponge damping after up2 and vp2 have been advanced.
if ( up2_vp2_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( iup2_sdmp, up2 / dt, stats_zm )
call stat_begin_update( ivp2_sdmp, vp2 / dt, stats_zm )
endif
up2 = sponge_damp_xp2( dt, gr%zm, up2, w_tol_sqd, &
up2_vp2_sponge_damp_profile )
vp2 = sponge_damp_xp2( dt, gr%zm, vp2, w_tol_sqd, &
up2_vp2_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( iup2_sdmp, up2 / dt, stats_zm )
call stat_end_update( ivp2_sdmp, vp2 / dt, stats_zm )
endif
endif ! up2_vp2_sponge_damp_settings%l_sponge_damping
! Clipping for r_t'th_l'
! Clipping r_t'th_l' at each vertical level, based on the
! correlation of r_t and th_l at each vertical level, such that:
! corr_(r_t,th_l) = r_t'th_l' / [ sqrt(r_t'^2) * sqrt(th_l'^2) ];
! -1 <= corr_(r_t,th_l) <= 1.
! Since r_t'^2, th_l'^2, and r_t'th_l' are all computed in the
! same place, clipping for r_t'th_l' only has to be done once.
l_first_clip_ts = .true.
l_last_clip_ts = .true.
call clip_covar( xp2_xpyp_rtpthlp, l_first_clip_ts, & ! Intent(in)
l_last_clip_ts, dt, rtp2, thlp2, & ! Intent(in)
l_predict_upwp_vpwp, & ! Intent(in)
rtpthlp, rtpthlp_chnge ) ! Intent(inout)
if ( l_scalar_calc ) then
! Apply hole filling algorithm to the scalar variance terms
if ( l_hole_fill ) then
do i = 1, sclr_dim, 1
call pos_definite_variances( xp2_xpyp_sclrp2, dt, sclr_tol(i)**2, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
sclrp2(:,i) ) ! Intent(inout)
if ( i == iisclr_rt ) then
! Here again, we do this kluge here to make sclr'rt' == rt'^2
call pos_definite_variances( xp2_xpyp_sclrprtp, dt, sclr_tol(i)**2, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
sclrprtp(:,i) ) ! Intent(inout)
end if
if ( i == iisclr_thl ) then
! As with sclr'rt' above, but for sclr'thl'
call pos_definite_variances( xp2_xpyp_sclrpthlp, dt, sclr_tol(i)**2, & ! Intent(in)
rho_ds_zm, rho_ds_zt, & ! Intent(in)
sclrpthlp(:,i) ) ! Intent(inout)
end if
enddo
endif
! Clipping for sclr'^2
do i = 1, sclr_dim, 1
! threshold = zero_threshold
!
! where ( wp2 >= w_tol_sqd ) &
! threshold = sclr_tol(i)*sclr_tol(i)
threshold = sclr_tol(i)**2
call clip_variance( clip_sclrp2, dt, threshold, & ! Intent(in)
sclrp2(:,i) ) ! Intent(inout)
enddo
! Clipping for sclr'r_t'
! Clipping sclr'r_t' at each vertical level, based on the
! correlation of sclr and r_t at each vertical level, such that:
! corr_(sclr,r_t) = sclr'r_t' / [ sqrt(sclr'^2) * sqrt(r_t'^2) ];
! -1 <= corr_(sclr,r_t) <= 1.
! Since sclr'^2, r_t'^2, and sclr'r_t' are all computed in the
! same place, clipping for sclr'r_t' only has to be done once.
do i = 1, sclr_dim, 1
if ( i == iisclr_rt ) then
! Treat this like a variance if we're emulating rt
threshold = sclr_tol(i) * rt_tol
call clip_variance( clip_sclrprtp, dt, threshold, & ! Intent(in)
sclrprtp(:,i) ) ! Intent(inout)
else
l_first_clip_ts = .true.
l_last_clip_ts = .true.
call clip_covar( clip_sclrprtp, l_first_clip_ts, & ! Intent(in)
l_last_clip_ts, dt, sclrp2(:,i), rtp2(:), & ! Intent(in)
l_predict_upwp_vpwp, & ! Intent(in)
sclrprtp(:,i), sclrprtp_chnge(:,i) ) ! Intent(inout)
end if
enddo
! Clipping for sclr'th_l'
! Clipping sclr'th_l' at each vertical level, based on the
! correlation of sclr and th_l at each vertical level, such that:
! corr_(sclr,th_l) = sclr'th_l' / [ sqrt(sclr'^2) * sqrt(th_l'^2) ];
! -1 <= corr_(sclr,th_l) <= 1.
! Since sclr'^2, th_l'^2, and sclr'th_l' are all computed in the
! same place, clipping for sclr'th_l' only has to be done once.
do i = 1, sclr_dim, 1
if ( i == iisclr_thl ) then
! As above, but for thl
threshold = sclr_tol(i) * thl_tol
call clip_variance( clip_sclrpthlp, dt, threshold, & ! Intent(in)
sclrpthlp(:,i) ) ! Intent(inout)
else
l_first_clip_ts = .true.
l_last_clip_ts = .true.
call clip_covar( clip_sclrpthlp, l_first_clip_ts, & ! Intent(in)
l_last_clip_ts, dt, sclrp2(:,i), thlp2(:), & ! Intent(in)
l_predict_upwp_vpwp, & ! Intent(in)
sclrpthlp(:,i), sclrpthlp_chnge(:,i) ) ! Intent(inout)
end if
enddo
endif ! l_scalar_calc
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "Error in advance_xp2_xpyp"
write(fstderr,*) "Intent(in)"
write(fstderr,*) "tau_zm = ", tau_zm
write(fstderr,*) "wm_zm = ", wm_zm
write(fstderr,*) "rtm = ", rtm
write(fstderr,*) "wprtp = ", wprtp
write(fstderr,*) "thlm = ", thlm
write(fstderr,*) "wpthlp = ", wpthlp
write(fstderr,*) "wpthvp = ", wpthvp
write(fstderr,*) "um = ", um
write(fstderr,*) "vm = ", vm
write(fstderr,*) "wp2 = ", wp2
write(fstderr,*) "wp3 = ", wp3
write(fstderr,*) "upwp = ", upwp
write(fstderr,*) "vpwp = ", vpwp
write(fstderr,*) "sigma_sqd_w = ", sigma_sqd_w
write(fstderr,*) "Skw_zm = ", Skw_zm
write(fstderr,*) "Kh_zt = ", Kh_zt
write(fstderr,*) "rtp2_forcing = ", rtp2_forcing
write(fstderr,*) "thlp2_forcing = ", thlp2_forcing
write(fstderr,*) "rtpthlp_forcing = ", rtpthlp_forcing
write(fstderr,*) "rho_ds_zm = ", rho_ds_zm
write(fstderr,*) "rho_ds_zt = ", rho_ds_zt
write(fstderr,*) "invrs_rho_ds_zm = ", invrs_rho_ds_zm
write(fstderr,*) "thv_ds_zm = ", thv_ds_zm
write(fstderr,*) "wp2_zt = ", wp2_zt
do i = 1, sclr_dim
write(fstderr,*) "sclrm = ", i, sclrm(:,i)
write(fstderr,*) "wpsclrp = ", i, wpsclrp(:,i)
enddo
write(fstderr,*) "Intent(In/Out)"
write(fstderr,*) "rtp2 = ", rtp2
write(fstderr,*) "thlp2 = ", thlp2
write(fstderr,*) "rtpthlp = ", rtpthlp
write(fstderr,*) "up2 = ", up2
write(fstderr,*) "vp2 = ", vp2
do i = 1, sclr_dim
write(fstderr,*) "sclrp2 = ", i, sclrp2(:,i)
write(fstderr,*) "sclrprtp = ", i, sclrprtp(:,i)
write(fstderr,*) "sclrthlp = ", i, sclrpthlp(:,i)
enddo
endif
end if
return
end subroutine advance_xp2_xpyp
!============================================================================================
subroutine solve_xp2_xpyp_with_single_lhs( C2x, tau_zm, rtm, thlm, wprtp, wpthlp, &
rtp2_forcing, thlp2_forcing, rtpthlp_forcing, &
sclrm, wpsclrp, &
lhs_ta, lhs_ma, lhs_diff, &
rhs_ta_wprtp2, rhs_ta_wpthlp2, &
rhs_ta_wprtpthlp, rhs_ta_wpsclrp2, &
rhs_ta_wprtpsclrp, rhs_ta_wpthlpsclrp, &
dt, l_iter, l_scalar_calc, &
rtp2, thlp2, rtpthlp, &
sclrp2, sclrprtp, sclrpthlp )
! Description:
! This subroutine generates a single lhs matrix and multiple rhs matricies, then
! solves the system. This should only be used when the lhs matrices for
! <w'r_t'>, <w'th_l'>, <w'rt'thl'> ,<w'sclr'^2>, <w'sclr'rt'>, and <w'sclr'thl'>