forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip_explicit.F90
1106 lines (901 loc) · 37.5 KB
/
clip_explicit.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 clip_explicit
implicit none
private
public :: clip_covars_denom, &
clip_covar, &
clip_covar_level, &
clip_variance, &
clip_variance_level, &
clip_skewness, &
clip_skewness_core
! Named constants to avoid string comparisons
integer, parameter, public :: &
clip_rtp2 = 1, & ! Named constant for rtp2 clipping
clip_thlp2 = 2, & ! Named constant for thlp2 clipping
clip_rtpthlp = 3, & ! Named constant for rtpthlp clipping
clip_up2 = 5, & ! Named constant for up2 clipping
clip_vp2 = 6, & ! Named constant for vp2 clipping
! clip_scalar = 7, & ! Named constant for scalar clipping
clip_wprtp = 8, & ! Named constant for wprtp clipping
clip_wpthlp = 9, & ! Named constant for wpthlp clipping
clip_upwp = 10, & ! Named constant for upwp clipping
clip_vpwp = 11, & ! Named constant for vpwp clipping
clip_wp2 = 12, & ! Named constant for wp2 clipping
clip_wpsclrp = 13, & ! Named constant for wp scalar clipping
clip_sclrp2 = 14, & ! Named constant for sclrp2 clipping
clip_sclrprtp = 15, & ! Named constant for sclrprtp clipping
clip_sclrpthlp = 16, & ! Named constant for sclrpthlp clipping
clip_wphydrometp = 17 ! Named constant for wphydrometp clipping
contains
!=============================================================================
subroutine clip_covars_denom( dt, rtp2, thlp2, up2, vp2, wp2, &
sclrp2, wprtp_cl_num, wpthlp_cl_num, &
wpsclrp_cl_num, upwp_cl_num, vpwp_cl_num, &
wprtp, wpthlp, upwp, vpwp, wpsclrp )
! Description:
! Some of the covariances found in the CLUBB model code need to be clipped
! multiple times during each timestep to ensure that the correlation between
! the two relevant variables stays between -1 and 1 at all times during the
! model run. The covariances that need to be clipped multiple times are
! w'r_t', w'th_l', w'sclr', u'w', and v'w'. One of the times that each one
! of these covariances is clipped is immediately after each one is set.
! However, each covariance still needs to be clipped two more times during
! each timestep (once after advance_xp2_xpyp is called and once after
! advance_wp2_wp3 is called). This subroutine handles the times that the
! covariances are clipped away from the time that they are set. In other
! words, this subroutine clips the covariances after the denominator terms
! in the relevant correlation equation have been altered, ensuring that
! all correlations will remain between -1 and 1 at all times.
! References:
! None
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variable(s)
use parameters_model, only: &
sclr_dim ! Variable(s)
use model_flags, only: &
l_tke_aniso ! Logical
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Timestep [s]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
rtp2, & ! r_t'^2 [(kg/kg)^2]
thlp2, & ! theta_l'^2 [K^2]
up2, & ! u'^2 [m^2/s^2]
vp2, & ! v'^2 [m^2/s^2]
wp2 ! w'^2 [m^2/s^2]
real( kind = core_rknd ), dimension(gr%nz,sclr_dim), intent(in) :: &
sclrp2 ! sclr'^2 [{units vary}^2]
integer, intent(in) :: &
wprtp_cl_num, &
wpthlp_cl_num, &
wpsclrp_cl_num, &
upwp_cl_num, &
vpwp_cl_num
! Input/Output Variables
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
wprtp, & ! w'r_t' [(kg/kg) m/s]
wpthlp, & ! w'theta_l' [K m/s]
upwp, & ! u'w' [m^2/s^2]
vpwp ! v'w' [m^2/s^2]
real( kind = core_rknd ), dimension(gr%nz,sclr_dim), intent(inout) :: &
wpsclrp ! w'sclr' [units m/s]
! Local Variables
logical :: &
l_first_clip_ts, & ! First instance of clipping in a timestep.
l_last_clip_ts ! Last instance of clipping in a timestep.
real( kind = core_rknd ), dimension(gr%nz) :: &
wprtp_chnge, & ! Net change in w'r_t' due to clipping [(kg/kg) m/s]
wpthlp_chnge, & ! Net change in w'th_l' due to clipping [K m/s]
upwp_chnge, & ! Net change in u'w' due to clipping [m^2/s^2]
vpwp_chnge ! Net change in v'w' due to clipping [m^2/s^2]
real( kind = core_rknd ), dimension(gr%nz,sclr_dim) :: &
wpsclrp_chnge ! Net change in w'sclr' due to clipping [{units vary}]
integer :: i ! scalar array index.
! ---- Begin Code ----
!!! Clipping for w'r_t'
!
! Clipping w'r_t' at each vertical level, based on the
! correlation of w and r_t at each vertical level, such that:
! corr_(w,r_t) = w'r_t' / [ sqrt(w'^2) * sqrt(r_t'^2) ];
! -1 <= corr_(w,r_t) <= 1.
!
! Since w'^2, r_t'^2, and w'r_t' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for w'r_t'
! is done three times during each timestep (once after each variable has
! been updated).
!
! This subroutine handles the first and third instances of
! w'r_t' clipping.
! The first instance of w'r_t' clipping takes place after
! r_t'^2 is updated in advance_xp2_xpyp.
! The third instance of w'r_t' clipping takes place after
! w'^2 is updated in advance_wp2_wp3.
! Used within subroutine clip_covar.
if ( wprtp_cl_num == 1 ) then
l_first_clip_ts = .true.
l_last_clip_ts = .false.
elseif ( wprtp_cl_num == 2 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .false.
elseif ( wprtp_cl_num == 3 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .true.
endif
! Clip w'r_t'
call clip_covar( clip_wprtp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2, rtp2, & ! intent(in)
wprtp, wprtp_chnge ) ! intent(inout)
!!! Clipping for w'th_l'
!
! Clipping w'th_l' at each vertical level, based on the
! correlation of w and th_l at each vertical level, such that:
! corr_(w,th_l) = w'th_l' / [ sqrt(w'^2) * sqrt(th_l'^2) ];
! -1 <= corr_(w,th_l) <= 1.
!
! Since w'^2, th_l'^2, and w'th_l' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for w'th_l'
! is done three times during each timestep (once after each variable has
! been updated).
!
! This subroutine handles the first and third instances of
! w'th_l' clipping.
! The first instance of w'th_l' clipping takes place after
! th_l'^2 is updated in advance_xp2_xpyp.
! The third instance of w'th_l' clipping takes place after
! w'^2 is updated in advance_wp2_wp3.
! Used within subroutine clip_covar.
if ( wpthlp_cl_num == 1 ) then
l_first_clip_ts = .true.
l_last_clip_ts = .false.
elseif ( wpthlp_cl_num == 2 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .false.
elseif ( wpthlp_cl_num == 3 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .true.
endif
! Clip w'th_l'
call clip_covar( clip_wpthlp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2, thlp2, & ! intent(in)
wpthlp, wpthlp_chnge ) ! intent(inout)
!!! Clipping for w'sclr'
!
! Clipping w'sclr' at each vertical level, based on the
! correlation of w and sclr at each vertical level, such that:
! corr_(w,sclr) = w'sclr' / [ sqrt(w'^2) * sqrt(sclr'^2) ];
! -1 <= corr_(w,sclr) <= 1.
!
! Since w'^2, sclr'^2, and w'sclr' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for w'sclr'
! is done three times during each timestep (once after each variable has
! been updated).
!
! This subroutine handles the first and third instances of
! w'sclr' clipping.
! The first instance of w'sclr' clipping takes place after
! sclr'^2 is updated in advance_xp2_xpyp.
! The third instance of w'sclr' clipping takes place after
! w'^2 is updated in advance_wp2_wp3.
! Used within subroutine clip_covar.
if ( wpsclrp_cl_num == 1 ) then
l_first_clip_ts = .true.
l_last_clip_ts = .false.
elseif ( wpsclrp_cl_num == 2 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .false.
elseif ( wpsclrp_cl_num == 3 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .true.
endif
! Clip w'sclr'
do i = 1, sclr_dim, 1
call clip_covar( clip_wpsclrp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2(:), sclrp2(:,i), & ! intent(in)
wpsclrp(:,i), wpsclrp_chnge(:,i) ) ! intent(inout)
enddo
!!! Clipping for u'w'
!
! Clipping u'w' at each vertical level, based on the
! correlation of u and w at each vertical level, such that:
! corr_(u,w) = u'w' / [ sqrt(u'^2) * sqrt(w'^2) ];
! -1 <= corr_(u,w) <= 1.
!
! Since w'^2, u'^2, and u'w' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for u'w'
! is done three times during each timestep (once after each variable has
! been updated).
!
! This subroutine handles the first and second instances of
! u'w' clipping.
! The first instance of u'w' clipping takes place after
! u'^2 is updated in advance_xp2_xpyp.
! The second instance of u'w' clipping takes place after
! w'^2 is updated in advance_wp2_wp3.
! Used within subroutine clip_covar.
if ( upwp_cl_num == 1 ) then
l_first_clip_ts = .true.
l_last_clip_ts = .false.
elseif ( upwp_cl_num == 2 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .false.
elseif ( upwp_cl_num == 3 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .true.
endif
! Clip u'w'
if ( l_tke_aniso ) then
call clip_covar( clip_upwp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2, up2, & ! intent(in)
upwp, upwp_chnge ) ! intent(inout)
else
! In this case, up2 = wp2, and the variable `up2' does not interact
call clip_covar( clip_upwp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2, wp2, & ! intent(in)
upwp, upwp_chnge ) ! intent(inout)
end if
!!! Clipping for v'w'
!
! Clipping v'w' at each vertical level, based on the
! correlation of v and w at each vertical level, such that:
! corr_(v,w) = v'w' / [ sqrt(v'^2) * sqrt(w'^2) ];
! -1 <= corr_(v,w) <= 1.
!
! Since w'^2, v'^2, and v'w' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for v'w'
! is done three times during each timestep (once after each variable has
! been updated).
!
! This subroutine handles the first and second instances of
! v'w' clipping.
! The first instance of v'w' clipping takes place after
! v'^2 is updated in advance_xp2_xpyp.
! The second instance of v'w' clipping takes place after
! w'^2 is updated in advance_wp2_wp3.
! Used within subroutine clip_covar.
if ( vpwp_cl_num == 1 ) then
l_first_clip_ts = .true.
l_last_clip_ts = .false.
elseif ( vpwp_cl_num == 2 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .false.
elseif ( vpwp_cl_num == 3 ) then
l_first_clip_ts = .false.
l_last_clip_ts = .true.
endif
if ( l_tke_aniso ) then
call clip_covar( clip_vpwp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2, vp2, & ! intent(in)
vpwp, vpwp_chnge ) ! intent(inout)
else
! In this case, vp2 = wp2, and the variable `vp2' does not interact
call clip_covar( clip_vpwp, l_first_clip_ts, & ! intent(in)
l_last_clip_ts, dt, wp2, wp2, & ! intent(in)
vpwp, vpwp_chnge ) ! intent(inout)
end if
return
end subroutine clip_covars_denom
!=============================================================================
subroutine clip_covar( solve_type, l_first_clip_ts, &
l_last_clip_ts, dt, xp2, yp2, &
xpyp, xpyp_chnge )
! Description:
! Clipping the value of covariance x'y' based on the correlation between x
! and y.
!
! The correlation between variables x and y is:
!
! corr_(x,y) = x'y' / [ sqrt(x'^2) * sqrt(y'^2) ];
!
! where x'^2 is the variance of x, y'^2 is the variance of y, and x'y' is
! the covariance of x and y.
!
! The correlation of two variables must always have a value between -1
! and 1, such that:
!
! -1 <= corr_(x,y) <= 1.
!
! Therefore, there is an upper limit on x'y', such that:
!
! x'y' <= [ sqrt(x'^2) * sqrt(y'^2) ];
!
! and a lower limit on x'y', such that:
!
! x'y' >= -[ sqrt(x'^2) * sqrt(y'^2) ].
!
! The values of x'y', x'^2, and y'^2 are all found on momentum levels.
!
! The value of x'y' may need to be clipped whenever x'y', x'^2, or y'^2 is
! updated.
!
! The following covariances are found in the code:
!
! w'r_t', w'th_l', w'sclr', (computed in advance_xm_wpxp);
! r_t'th_l', sclr'r_t', sclr'th_l', (computed in advance_xp2_xpyp);
! u'w', v'w', w'edsclr' (computed in advance_windm_edsclrm);
! and w'hm' (computed in setup_pdf_parameters).
! References:
! None
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variable(s)
use constants_clubb, only: &
max_mag_correlation, & ! Constant(s)
max_mag_correlation_flux
use model_flags, only: &
l_predict_upwp_vpwp ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_modify, &
stat_end_update
use stats_variables, only: &
stats_zm, & ! Variable(s)
iwprtp_cl, &
iwpthlp_cl, &
irtpthlp_cl, &
iupwp_cl, &
ivpwp_cl, &
l_stats_samp
implicit none
! Input Variables
integer, intent(in) :: &
solve_type ! Variable being solved; used for STATS.
logical, intent(in) :: &
l_first_clip_ts, & ! First instance of clipping in a timestep.
l_last_clip_ts ! Last instance of clipping in a timestep.
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep; used here for STATS [s]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
xp2, & ! Variance of x, x'^2 (momentum levels) [{x units}^2]
yp2 ! Variance of y, y'^2 (momentum levels) [{y units}^2]
! Output Variable
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
xpyp ! Covariance of x and y, x'y' (momentum levels) [{x units}*{y units}]
real( kind = core_rknd ), dimension(gr%nz), intent(out) :: &
xpyp_chnge ! Net change in x'y' due to clipping [{x units}*{y units}]
! Local Variables
real( kind = core_rknd ) :: &
max_mag_corr, & ! Maximum magnitude of a correlation allowed
xpyp_bound
integer :: k ! Array index
integer :: &
ixpyp_cl
! ---- Begin Code ----
select case ( solve_type )
case ( clip_wprtp ) ! wprtp clipping budget term
ixpyp_cl = iwprtp_cl
case ( clip_wpthlp ) ! wpthlp clipping budget term
ixpyp_cl = iwpthlp_cl
case ( clip_rtpthlp ) ! rtpthlp clipping budget term
ixpyp_cl = irtpthlp_cl
case ( clip_upwp ) ! upwp clipping budget term
if ( l_predict_upwp_vpwp ) then
ixpyp_cl = iupwp_cl
else
ixpyp_cl = 0
endif ! l_predict_upwp_vpwp
case ( clip_vpwp ) ! vpwp clipping budget term
if ( l_predict_upwp_vpwp ) then
ixpyp_cl = ivpwp_cl
else
ixpyp_cl = 0
endif ! l_predict_upwp_vpwp
case default ! scalars (or upwp/vpwp) are involved
ixpyp_cl = 0
end select
if ( l_stats_samp ) then
if ( l_first_clip_ts ) then
call stat_begin_update( ixpyp_cl, xpyp / dt, stats_zm )
else
call stat_modify( ixpyp_cl, -xpyp / dt, stats_zm )
endif
endif
! When clipping for wprtp or wpthlp, use the special value for
! max_mag_correlation_flux. For all other correlations, use
! max_mag_correlation.
if ( ( solve_type == clip_wprtp ) .or. ( solve_type == clip_wpthlp ) ) then
max_mag_corr = max_mag_correlation_flux
else ! All other covariances
max_mag_corr = max_mag_correlation
endif ! solve_type
! The value of x'y' at the surface (or lower boundary) is a set value that
! is either specified or determined elsewhere in a surface subroutine. It
! is ensured elsewhere that the correlation between x and y at the surface
! (or lower boundary) is between -1 and 1. Thus, the covariance clipping
! code does not need to be invoked at the lower boundary. Likewise, the
! value of x'y' is set at the upper boundary, so the covariance clipping
! code does not need to be invoked at the upper boundary.
! Note that if clipping were applied at the lower boundary, momentum will
! not be conserved, therefore it should never be added.
do k = 2, gr%nz-1, 1
xpyp_bound = max_mag_corr * sqrt( xp2(k) * yp2(k) )
! Clipping for xpyp at an upper limit corresponding with a correlation
! between x and y of max_mag_corr.
if ( xpyp(k) > xpyp_bound ) then
xpyp_chnge(k) = xpyp_bound - xpyp(k)
xpyp(k) = xpyp_bound
! Clipping for xpyp at a lower limit corresponding with a correlation
! between x and y of -max_mag_corr.
else if ( xpyp(k) < -xpyp_bound ) then
xpyp_chnge(k) = -xpyp_bound - xpyp(k)
xpyp(k) = -xpyp_bound
else
xpyp_chnge(k) = 0.0_core_rknd
end if
enddo ! k = 2..gr%nz
! Since there is no covariance clipping at the upper or lower boundaries,
! the change in x'y' due to covariance clipping at those levels is 0.
xpyp_chnge(1) = 0.0_core_rknd
xpyp_chnge(gr%nz) = 0.0_core_rknd
if ( l_stats_samp ) then
if ( l_last_clip_ts ) then
call stat_end_update( ixpyp_cl, xpyp / dt, stats_zm )
else
call stat_modify( ixpyp_cl, xpyp / dt, stats_zm )
endif
endif
return
end subroutine clip_covar
!=============================================================================
subroutine clip_covar_level( solve_type, level, l_first_clip_ts, &
l_last_clip_ts, dt, xp2, yp2, &
xpyp, xpyp_chnge )
! Description:
! Clipping the value of covariance x'y' based on the correlation between x
! and y. This is all done at a single vertical level.
!
! The correlation between variables x and y is:
!
! corr_(x,y) = x'y' / [ sqrt(x'^2) * sqrt(y'^2) ];
!
! where x'^2 is the variance of x, y'^2 is the variance of y, and x'y' is
! the covariance of x and y.
!
! The correlation of two variables must always have a value between -1
! and 1, such that:
!
! -1 <= corr_(x,y) <= 1.
!
! Therefore, there is an upper limit on x'y', such that:
!
! x'y' <= [ sqrt(x'^2) * sqrt(y'^2) ];
!
! and a lower limit on x'y', such that:
!
! x'y' >= -[ sqrt(x'^2) * sqrt(y'^2) ].
!
! The values of x'y', x'^2, and y'^2 are all found on momentum levels.
!
! The value of x'y' may need to be clipped whenever x'y', x'^2, or y'^2 is
! updated.
!
! The following covariances are found in the code:
!
! w'r_t', w'th_l', w'sclr', (computed in advance_xm_wpxp);
! r_t'th_l', sclr'r_t', sclr'th_l', (computed in advance_xp2_xpyp);
! u'w', v'w', w'edsclr' (computed in advance_windm_edsclrm);
! and w'hm' (computed in setup_pdf_parameters).
! References:
! None
!-----------------------------------------------------------------------
use constants_clubb, only: &
max_mag_correlation, & ! Constant(s)
max_mag_correlation_flux, &
zero
use model_flags, only: &
l_predict_upwp_vpwp ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
use stats_type_utilities, only: &
stat_begin_update_pt, & ! Procedure(s)
stat_modify_pt, &
stat_end_update_pt
use stats_variables, only: &
stats_zm, & ! Variable(s)
iwprtp_cl, &
iwpthlp_cl, &
irtpthlp_cl, &
iupwp_cl, &
ivpwp_cl, &
l_stats_samp
implicit none
! Input Variables
integer, intent(in) :: &
solve_type, & ! Variable being solved; used for STATS
level ! Vertical level index
logical, intent(in) :: &
l_first_clip_ts, & ! First instance of clipping in a timestep.
l_last_clip_ts ! Last instance of clipping in a timestep.
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep; used here for STATS [s]
real( kind = core_rknd ), intent(in) :: &
xp2, & ! Variance of x, <x'^2> [{x units}^2]
yp2 ! Variance of y, <y'^2> [{y units}^2]
! Output Variable
real( kind = core_rknd ), intent(inout) :: &
xpyp ! Covariance of x and y, <x'y'> [{x units}*{y units}]
real( kind = core_rknd ), intent(out) :: &
xpyp_chnge ! Net change in <x'y'> due to clipping [{x units}*{y units}]
! Local Variables
real( kind = core_rknd ) :: &
max_mag_corr ! Maximum magnitude of a correlation allowed
integer :: &
ixpyp_cl ! Statistics index
select case ( solve_type )
case ( clip_wprtp ) ! wprtp clipping budget term
ixpyp_cl = iwprtp_cl
case ( clip_wpthlp ) ! wpthlp clipping budget term
ixpyp_cl = iwpthlp_cl
case ( clip_rtpthlp ) ! rtpthlp clipping budget term
ixpyp_cl = irtpthlp_cl
case ( clip_upwp ) ! upwp clipping budget term
if ( l_predict_upwp_vpwp ) then
ixpyp_cl = iupwp_cl
else
ixpyp_cl = 0
endif ! l_predict_upwp_vpwp
case ( clip_vpwp ) ! vpwp clipping budget term
if ( l_predict_upwp_vpwp ) then
ixpyp_cl = ivpwp_cl
else
ixpyp_cl = 0
endif ! l_predict_upwp_vpwp
case default ! scalars (or upwp/vpwp) are involved
ixpyp_cl = 0
end select
if ( l_stats_samp ) then
if ( l_first_clip_ts ) then
call stat_begin_update_pt( ixpyp_cl, level, &
xpyp / dt, stats_zm )
else
call stat_modify_pt( ixpyp_cl, level, &
-xpyp / dt, stats_zm )
endif
endif
! When clipping for wprtp or wpthlp, use the special value for
! max_mag_correlation_flux. For all other correlations, use
! max_mag_correlation.
if ( ( solve_type == clip_wprtp ) .or. ( solve_type == clip_wpthlp ) ) then
max_mag_corr = max_mag_correlation_flux
else ! All other covariances
max_mag_corr = max_mag_correlation
endif ! solve_type
! The value of x'y' at the surface (or lower boundary) is a set value that
! is either specified or determined elsewhere in a surface subroutine. It
! is ensured elsewhere that the correlation between x and y at the surface
! (or lower boundary) is between -1 and 1. Thus, the covariance clipping
! code does not need to be invoked at the lower boundary. Likewise, the
! value of x'y' is set at the upper boundary, so the covariance clipping
! code does not need to be invoked at the upper boundary.
! Note that if clipping were applied at the lower boundary, momentum will
! not be conserved, therefore it should never be added.
! Clipping for xpyp at an upper limit corresponding with a correlation
! between x and y of max_mag_corr.
if ( xpyp > max_mag_corr * sqrt( xp2 * yp2 ) ) then
xpyp_chnge = max_mag_corr * sqrt( xp2 * yp2 ) - xpyp
xpyp = max_mag_corr * sqrt( xp2 * yp2 )
! Clipping for xpyp at a lower limit corresponding with a correlation
! between x and y of -max_mag_corr.
elseif ( xpyp < -max_mag_corr * sqrt( xp2 * yp2 ) ) then
xpyp_chnge = -max_mag_corr * sqrt( xp2 * yp2 ) - xpyp
xpyp = -max_mag_corr * sqrt( xp2 * yp2 )
else
xpyp_chnge = zero
endif
if ( l_stats_samp ) then
if ( l_last_clip_ts ) then
call stat_end_update_pt( ixpyp_cl, level, &
xpyp / dt, stats_zm )
else
call stat_modify_pt( ixpyp_cl, level, &
xpyp / dt, stats_zm )
endif
endif
return
end subroutine clip_covar_level
!=============================================================================
subroutine clip_variance( solve_type, dt, threshold, &
xp2 )
! Description:
! Clipping the value of variance x'^2 based on a minimum threshold value.
! The threshold value must be greater than or equal to 0.
!
! The values of x'^2 are found on the momentum levels.
!
! The following variances are found in the code:
!
! r_t'^2, th_l'^2, u'^2, v'^2, sclr'^2, (computed in advance_xp2_xpyp);
! w'^2 (computed in advance_wp2_wp3).
! References:
! None
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_end_update
use stats_variables, only: &
stats_zm, & ! Variable(s)
iwp2_cl, &
irtp2_cl, &
ithlp2_cl, &
iup2_cl, &
ivp2_cl, &
l_stats_samp
implicit none
! Input Variables
integer, intent(in) :: &
solve_type ! Variable being solved; used for STATS.
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep; used here for STATS [s]
real( kind = core_rknd ), intent(in) :: &
threshold ! Minimum value of x'^2 [{x units}^2]
! Output Variable
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
xp2 ! Variance of x, x'^2 (momentum levels) [{x units}^2]
! Local Variables
integer :: k ! Array index
integer :: &
ixp2_cl
! ---- Begin Code ----
select case ( solve_type )
case ( clip_wp2 ) ! wp2 clipping budget term
ixp2_cl = iwp2_cl
case ( clip_rtp2 ) ! rtp2 clipping budget term
ixp2_cl = irtp2_cl
case ( clip_thlp2 ) ! thlp2 clipping budget term
ixp2_cl = ithlp2_cl
case ( clip_up2 ) ! up2 clipping budget term
ixp2_cl = iup2_cl
case ( clip_vp2 ) ! vp2 clipping budget term
ixp2_cl = ivp2_cl
case default ! scalars are involved
ixp2_cl = 0
end select
if ( l_stats_samp ) then
call stat_begin_update( ixp2_cl, xp2 / dt, stats_zm )
endif
! Limit the value of x'^2 at threshold.
! The value of x'^2 at the surface (or lower boundary) is a set value that
! is determined elsewhere in a surface subroutine. Thus, the variance
! clipping code does not need to be invoked at the lower boundary.
! Likewise, the value of x'^2 is set at the upper boundary, so the variance
! clipping code does not need to be invoked at the upper boundary.
!
! charlass on 09/11/2013: I changed the clipping so that also the surface
! level is clipped. I did this because we discovered that there are slightly
! negative values in thlp2(1) and rtp2(1) when running quarter_ss case with
! WRF-CLUBB (see wrf:ticket:51#comment:33)
do k = 1, gr%nz-1, 1
if ( xp2(k) < threshold ) then
xp2(k) = threshold
endif
enddo
if ( l_stats_samp ) then
call stat_end_update( ixp2_cl, xp2 / dt, stats_zm )
endif
return
end subroutine clip_variance
!=============================================================================
subroutine clip_variance_level( solve_type, dt, threshold, level, &
xp2 )
! Description:
! Clipping the value of variance x'^2 based on a minimum threshold value.
! The threshold value must be greater than or equal to 0. This clipping is
! done at a single vertical level.
!
! The values of x'^2 are found on the momentum levels.
!
! The following variances are found in the code:
!
! r_t'^2, th_l'^2, u'^2, v'^2, sclr'^2, (computed in advance_xp2_xpyp);
! w'^2 (computed in advance_wp2_wp3).
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
use stats_type_utilities, only: &
stat_begin_update_pt, & ! Procedure(s)
stat_end_update_pt
use stats_variables, only: &
stats_zm, & ! Variable(s)
iwp2_cl, &
irtp2_cl, &
ithlp2_cl, &
iup2_cl, &
ivp2_cl, &
l_stats_samp
implicit none
! Input Variables
integer, intent(in) :: &
solve_type ! Variable being solved; used for STATS.
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep; used here for STATS [s]
real( kind = core_rknd ), intent(in) :: &
threshold ! Minimum value of x'^2 [{x units}^2]
integer, intent(in) :: &
level ! Vertical level index
! Output Variable
real( kind = core_rknd ), intent(inout) :: &
xp2 ! Variance of x, x'^2 (momentum levels) [{x units}^2]
integer :: &
ixp2_cl
! ---- Begin Code ----
select case ( solve_type )
case ( clip_wp2 ) ! wp2 clipping budget term
ixp2_cl = iwp2_cl
case ( clip_rtp2 ) ! rtp2 clipping budget term
ixp2_cl = irtp2_cl
case ( clip_thlp2 ) ! thlp2 clipping budget term
ixp2_cl = ithlp2_cl
case ( clip_up2 ) ! up2 clipping budget term
ixp2_cl = iup2_cl
case ( clip_vp2 ) ! vp2 clipping budget term
ixp2_cl = ivp2_cl
case default ! scalars are involved
ixp2_cl = 0
end select
if ( l_stats_samp ) then
call stat_begin_update_pt( ixp2_cl, level, xp2 / dt, stats_zm )
endif
! Limit the value of x'^2 at threshold.
if ( xp2 < threshold ) then
xp2 = threshold
endif
if ( l_stats_samp ) then
call stat_end_update_pt( ixp2_cl, level, xp2 / dt, stats_zm )
endif
return
end subroutine clip_variance_level
!=============================================================================
subroutine clip_skewness( dt, sfc_elevation, wp2_zt, wp3 )
! Description:
! Clipping the value of w'^3 based on the skewness of w, Sk_w.
!
! Aditionally, to prevent possible crashes due to wp3 growing too large,
! abs(wp3) will be clipped to 100.
!
! The skewness of w is:
!
! Sk_w = w'^3 / (w'^2)^(3/2).
!
! The value of Sk_w is limited to a range between an upper limit and a lower
! limit. The values of the limits depend on whether the level altitude is
! within 100 meters of the surface.
!
! For altitudes less than or equal to 100 meters above ground level (AGL):
!
! -0.2_core_rknd*sqrt(2) <= Sk_w <= 0.2_core_rknd*sqrt(2);
!
! while for all altitudes greater than 100 meters AGL:
!
! -4.5_core_rknd <= Sk_w <= 4.5_core_rknd.
!
! Therefore, there is an upper limit on w'^3, such that:
!
! w'^3 <= threshold_magnitude * (w'^2)^(3/2);
!
! and a lower limit on w'^3, such that:
!
! w'^3 >= -threshold_magnitude * (w'^2)^(3/2).
!
! The values of w'^3 are found on the thermodynamic levels, while the values
! of w'^2 are found on the momentum levels. Therefore, the values of w'^2
! are interpolated to the thermodynamic levels before being used to
! calculate the upper and lower limits for w'^3.
! References:
! None
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_end_update
use stats_variables, only: &
stats_zt, & ! Variable(s)
iwp3_cl, &
l_stats_samp
implicit none
! External
intrinsic :: sign, sqrt, real
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep; used here for STATS [s]
real( kind = core_rknd ), intent(in) :: &
sfc_elevation ! Elevation of ground level [m AMSL]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
wp2_zt ! w'^2 interpolated to thermodyamic levels [m^2/s^2]
! Input/Output Variables
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &