forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mono_flux_limiter.F90
1848 lines (1497 loc) · 75 KB
/
mono_flux_limiter.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 mono_flux_limiter
implicit none
private ! Default Scope
public :: monotonic_turbulent_flux_limit, &
calc_turb_adv_range
private :: mfl_xm_lhs, &
mfl_xm_rhs, &
mfl_xm_solve, &
mean_vert_vel_up_down
! Private named constants to avoid string comparisons
! NOTE: These values must match the values for xm_wpxp_thlm
! and xm_wpxp_rtm given in advance_xm_wpxp_module!
integer, parameter, private :: &
mono_flux_thlm = 1, & ! Named constant for thlm mono_flux calls
mono_flux_rtm = 2, & ! Named constant for rtm mono_flux calls
mono_flux_um = 4, & ! Named constant for um mono_flux calls
mono_flux_vm = 5 ! Named constant for vm mono_flux calls
contains
!=============================================================================
subroutine monotonic_turbulent_flux_limit( solve_type, dt, xm_old, &
xp2, wm_zt, xm_forcing, &
rho_ds_zm, rho_ds_zt, &
invrs_rho_ds_zm, invrs_rho_ds_zt, &
xp2_threshold, l_implemented, &
low_lev_effect, high_lev_effect, &
xm, xm_tol, wpxp )
! Description:
! Limits the value of w'x' and corrects the value of xm when the xm turbulent
! advection term is not monotonic. A monotonic turbulent advection scheme
! will not create new extrema for variable x, based only on turbulent
! advection (not considering mean advection and xm forcings).
!
! Montonic turbulent advection
! ----------------------------
!
! A monotonic turbulent advection scheme does not allow new extrema for
! variable x to be created (by means of turbulent advection). In a
! monotonic turbulent advection scheme, when only the effects of turbulent
! advection are considered (neglecting forcings and mean advection), the
! value of variable x at a given point should not increase above the
! greatest value of variable x at nearby points, nor decrease below the
! smallest value of variable x at nearby points. Nearby points are points
! that are close enough to the given point so that the value of variable x
! at the given point is effected by the values of variable x at the nearby
! points by means of transfer by turbulent winds during a time step. Again,
! a monotonic scheme insures that advection only transfers around values of
! variable x and does not create new extrema for variable x. A monotonic
! turbulent advection scheme is useful because the turbulent advection term
! (w'x') may go numerically unstable, resulting in large instabilities in
! the mean field (xm). A monotonic turbulent advection scheme will limit
! the change in xm, and also in w'x'.
!
! The following example illustrates the concept of monotonic turbulent
! advection. Three successive vertical grid levels are shown (k-1, k, and
! k+1). Three point values of theta-l are listed at every vertical grid
! level. All three vertical levels have a mean theta-l (thlm) of 288.0 K.
! A circulation is occuring (in the direction of the arrows) in the vertical
! (w wind component) and in the horizontal (u and/or v wind components),
! such that the mean value of vertical velocity (wmm) is 0, but there is a
! turbulent component such that w'^2 > 0.
!
! level = k+1 || --- 287.0 K --- 288.0 K --- 289.0 K --- || thlm = 288.0
! || / \--------------------->| ||
! || | | || wmm = 0; wp2 > 0
! || |<---------------------\ / ||
! level = k || --- 288.0 K --- 288.0 K --- 288.0 K --- || thlm = 288.0
! || |<---------------------/ \ ||
! || | | || wmm = 0; wp2 > 0
! || \ /--------------------->| ||
! level = k-1 || --- 287.5 K --- 288.0 K --- 288.5 K --- || thlm = 288.0
!
! Neglecting any contributions from thlm forcings (effects of radiation,
! microphysics, large-scale horizontal advection, etc.), the values of
! theta-l as shown will be altered by only turbulent advection. As a side
! note, the contribution of mean advection will be 0 since wmm = 0. The
! diagram shows that the value of theta-l at the point on the right at level
! k will increase. However, the values of theta-l at the other two points
! at level k will remain the same. Thus, the value of thlm at level k will
! become greater than 288.0 K. In the same manner, the values of thlm at
! the other two vertical levels (k-1 and k+1) will become smaller than
! 288.0 K. However, the monotonic turbulent advection scheme insures that
! any theta-l point value cannot become smaller than the smallest theta-l
! point value (287.0 K) or larger than the largest theta-l point value
! (289.0 K). Since all theta-l point values must fall between 287.0 K and
! 289.0 K, the level averages of theta-l (thlm) must fall between 287.0 K
! and 289.0 K. Thus, any values of the turbulent flux, w'th_l', that would
! cause thlm to rise above 289.0 K or fall below 287.0 K, not considering
! the effect of other terms on thlm (such as forcings), are faulty and need
! to be limited appropriately. The values of thlm also need to be corrected
! appropriately.
!
! Formula for the limitation of w'x' and xm
! -----------------------------------------
!
! The equation for change in the mean field, xm, over time is:
!
! d(xm)/dt = -w*d(xm)/dz - (1/rho_ds) * d( rho_ds * w'x' )/dz + xm_forcing;
!
! where w*d(xm)/dz is the mean advection component,
! (1/rho_ds) * d( rho_ds * w'x' )/dz is the turbulent advection component,
! and xm_forcing is the xm forcing component. The d(xm)/dt time tendency
! component is discretized as:
!
! xm(k,<t+1>)/dt = xm(k,<t>)/dt - w*d(xm)/dz
! - (1/rho_ds) * d( rho_ds * w'x' )/dz + xm_forcing.
!
! The value of xm after it has been advanced to timestep (t+1) must be in an
! appropriate range based on the values of xm at timestep (t), the amount of
! xm forcings applied over the ensuing time step, and the amount of mean
! advection applied over the ensuing time step. This is exactly the same
! thing as saying that the value of xm(k,<t+1>), with the contribution of
! turbulent advection included, must fall into a certain range based on the
! value of xm(k,<t+1>) without the contribution of the turbulent advection
! component over the last time step. The following inequality is used to
! limit the value of xm(k,<t+1>):
!
! MIN{ xm(k-1,<t>) + dt*xm_forcing(k-1) - dt*wm_zt(k-1)*d(xm)/dz|_(k-1)
! - x_max_dev_low(k-1,<t>),
! xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - x_max_dev_low(k,<t>),
! xm(k+1,<t>) + dt*xm_forcing(k+1) - dt*wm_zt(k+1)*d(xm)/dz|_(k+1)
! - x_max_dev_low(k+1,<t>) }
! <= xm(k,<t+1>) <=
! MAX{ xm(k-1,<t>) + dt*xm_forcing(k-1) - dt*wm_zt(k-1)*d(xm)/dz|_(k-1)
! + x_max_dev_high(k-1,<t>),
! xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! + x_max_dev_high(k,<t>),
! xm(k+1,<t>) + dt*xm_forcing(k+1) - dt*wm_zt(k+1)*d(xm)/dz|_(k+1)
! + x_max_dev_high(k+1,<t>) };
!
! where x_max_dev_low is the absolute value of the deviation from the mean
! of the smallest point value of variable x at the given vertical level and
! timestep; and where x_max_dev_high is the deviation from the mean of the
! largest point value of variable x at the given vertical level and
! timestep. For example, at vertical level (k+1) and timestep (t):
!
! x_max_dev_low(k+1,<t>) = | MIN( x(k+1,<t>) ) - xm(k+1,<t>) |;
! x_max_dev_high(k+1,<t>) = MAX( x(k+1,<t>) ) - xm(k+1,<t>).
!
! The inequality shown above only takes into account values from the central
! level, one-level-below the central level, and one-level-above the central
! level. This is the minimal amount of vertical levels that can have their
! values taken into consideration. Any vertical level that can have it's
! properties advect to the given level during the course of a single time
! step can be taken into consideration. However, only three levels will be
! considered in this example for the sake of simplicity.
!
! The inequality will be written in more simple terms:
!
! xm_lower_lim_allowable(k) <= xm(k,<t+1>) <= xm_upper_lim_allowable(k).
!
! The inequality can now be related to the turbulent flux, w'x'(k,<t+1>),
! through a substitution that is made for xm(k,<t+1>), such that:
!
! xm(k,<t+1>) = xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - dt * (1/rho_ds) * d( rho_ds * w'x' )/dz|_(k).
!
! The inequality becomes:
!
! xm_lower_lim_allowable(k)
! <=
! xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - dt * (1/rho_ds) * d( rho_ds * w'x' )/dz|_(k)
! <=
! xm_upper_lim_allowable(k).
!
! The inequality is rearranged, and the turbulent advection term,
! d(w'x')/dz, is discretized:
!
! xm_lower_lim_allowable(k)
! - [ xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k) ]
! <=
! - dt * (1/rho_ds_zt(k))
! * invrs_dzt(k)
! * [ rho_ds_zm(k) * w'x'(k,<t+1>)
! - rho_ds_zm(k-1) * w'x'(k-1,<t+1>) ]
! <=
! xm_upper_lim_allowable(k)
! - [ xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k) ];
!
! where invrs_dzt(k) = 1 / ( zm(k) - zm(k-1) ).
!
! Multiplying the inequality by -rho_ds_zt(k)/(dz*invrs_dzt(k)):
!
! rho_ds_zt(k)/(dz*invrs_dzt(k))
! * [ xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - xm_lower_lim_allowable(k) ]
! >=
! rho_ds_zm(k) * w'x'(k,<t+1>) - rho_ds_zm(k-1) * w'x'(k-1,<t+1>)
! >=
! rho_ds_zt(k)/(dz*invrs_dzt(k))
! * [ xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - xm_upper_lim_allowable(k) ].
!
! Note: The inequality symbols have been flipped due to multiplication
! involving a (-) sign.
!
! Adding rho_ds_zm(k-1) * w'x'(k-1,<t+1>) to the inequality:
!
! rho_ds_zt(k)/(dz*invrs_dzt(k))
! * [ xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - xm_lower_lim_allowable(k) ]
! + rho_ds_zm(k-1) * w'x'(k-1,<t+1>)
! >= rho_ds_zm(k) * w'x'(k,<t+1>) >=
! rho_ds_zt(k)/(dz*invrs_dzt(k))
! * [ xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - xm_upper_lim_allowable(k) ]
! + rho_ds_zm(k-1) * w'x'(k-1,<t+1>).
!
! The inequality is then rearranged to be based around w'x'(k,<t+1>):
!
! (1/rho_ds_zm(k))
! * [ rho_ds_zt(k)/(dt*invrs_dzt(k))
! * { xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - xm_lower_lim_allowable(k) }
! + rho_ds_zm(k-1) * w'x'(k-1,<t+1>) ]
! >= w'x'(k,<t+1>) >=
! (1/rho_ds_zm(k))
! * [ rho_ds_zt(k)/(dt*invrs_dzt(k))
! * { xm(k,<t>) + dt*xm_forcing(k) - dt*wm_zt(k)*d(xm)/dz|_(k)
! - xm_upper_lim_allowable(k) }
! + rho_ds_zm(k-1) * w'x'(k-1,<t+1>) ].
!
! The values of w'x' are found on the momentum levels, while the values of
! xm are found on the thermodynamic levels. Additionally, the values of
! rho_ds_zm are found on the momentum levels, and the values of rho_ds_zt
! are found on the thermodynamic levels. The inequality is applied to
! w'x'(k,<t+1>) from vertical levels 2 through the second-highest level
! (gr%nz-1). The value of w'x' at level 1 is a set surface (or lowest
! level) flux. The value of w'x' at the highest level is also a set value,
! and therefore is not altered.
!
! Approximating maximum and minimum values of x at any given vertical level
! -------------------------------------------------------------------------
!
! The CLUBB code provides means, variances, and covariances for certain
! variables at all vertical levels. However, there is no way to find the
! maximum or minimum point value of any variable on any vertical level.
! Without that information, x_max_dev_low and x_max_dev_high can't be found,
! and the inequality above is useless. However, there is a way to
! approximate the maximum and minimum point values at any given vertical
! level. The maximum and minimum point values can be approximated through
! the use of the variance, x'^2.
!
! Just as the mean value of x, which is xm, and the turbulent flux of x,
! which is w'x', are known, so is the variance of x, which is x'^2. The
! standard deviation of x is the square root of the variance of x. The
! distribution of x along the horizontal plane (at vertical level k) is
! approximated to be the sum of two normal (or Gaussian) distributions.
! Most of the values in a normal distribution are found within 2 standard
! deviations from the mean. Thus, the maximum point value of x along the
! horizontal plance at any vertical level can be approximated as:
! xm + 2*sqrt(x'^2). Likewise, the minimum value of x along the horizontal
! plane at any vertical level can be approximated as: xm - 2*sqrt(x'^2).
!
! The values of x'^2 are found on the momentum levels. The values of xm
! are found on the thermodynamic levels. Thus, the values of x'^2 are
! interpolated to the thermodynamic levels in order to find the maximum
! and minimum point values of variable x.
!
! The one downfall of this method is that instabilities can arise in the
! model where unphysically large values of x'^2 are produced. Thus, this
! allows for an unphysically large deviation of xm from its values at the
! previous time step due to turbulent advection. Thus, for purposes of
! determining the maximum and minimum point values of x, a upper limit
! is placed on x'^2, in order to limit the standard deviation of x. This
! limit is only applied in this subroutine, and is not applied to x'^2
! elsewhere in the model code.
! References:
!-----------------------------------------------------------------------
use grid_class, only: &
gr, & ! Variable(s)
zm2zt ! Procedure(s)
use constants_clubb, only: &
zero_threshold, &
eps, &
fstderr
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constant
use clubb_precision, only: &
core_rknd ! Variable(s)
use fill_holes, only: &
vertical_integral ! Procedure(s)
use stats_type_utilities, only: &
stat_begin_update, & ! Procedure(s)
stat_end_update, &
stat_update_var
use stats_variables, only: &
stats_zm, & ! Variable(s)
stats_zt, &
iwprtp_mfl, &
irtm_mfl, &
iwpthlp_mfl, &
ithlm_mfl, &
iupwp_mfl, &
ium_mfl, &
ivpwp_mfl, &
ivm_mfl, &
ithlm_old, &
ithlm_without_ta, &
ithlm_mfl_min, &
ithlm_mfl_max, &
irtm_old, &
irtm_without_ta, &
irtm_mfl_min, &
irtm_mfl_max, &
ithlm_enter_mfl, &
ithlm_exit_mfl, &
irtm_enter_mfl, &
irtm_exit_mfl, &
iwpthlp_mfl_min, &
iwpthlp_mfl_max, &
iwpthlp_entermfl, &
iwpthlp_exit_mfl, &
iwprtp_mfl_min, &
iwprtp_mfl_max, &
iwprtp_enter_mfl, &
iwprtp_exit_mfl, &
l_stats_samp
implicit none
! Constant Parameters
! Flag for using a semi-implicit, tridiagonal method to solve for xm(t+1)
! when xm(t+1) needs to be changed.
logical, parameter :: l_mfl_xm_imp_adj = .true.
! Input Variables
integer, intent(in) :: &
solve_type ! Variables being solved for.
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep length [s]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
xm_old, & ! xm at previous time step (thermo. levs.) [units vary]
xp2, & ! x'^2 (momentum levels) [units vary]
wm_zt, & ! w wind component on thermodynamic levels [m/s]
xm_forcing, & ! xm forcings (thermodynamic levels) [units vary]
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]
real( kind = core_rknd ), intent(in) :: &
xp2_threshold, & ! Lower limit of x'^2 [units vary]
xm_tol ! Lower limit of maxdev [units vary]
logical, intent(in) :: &
l_implemented ! Flag for CLUBB being implemented in a larger model.
integer, dimension(gr%nz), intent(in) :: &
low_lev_effect, & ! Index of lowest level that has an effect (for lev. k)
high_lev_effect ! Index of highest level that has an effect (for lev. k)
! Input/Output Variables
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
xm, & ! xm at current time step (thermodynamic levels) [units vary]
wpxp ! w'x' (momentum levels) [units vary]
! Local Variables
real( kind = core_rknd ), dimension(gr%nz) :: &
xp2_zt, & ! x'^2 interpolated to thermodynamic levels [units vary]
xm_enter_mfl, & ! xm as it enters the MFL [units vary]
xm_without_ta, & ! Value of xm without turb. adv. contrib. [units vary]
wpxp_net_adjust, & ! Net amount of adjustment needed on w'x' [units vary]
dxm_dt_mfl_adjust ! Rate of change of adjustment to xm [units vary]
real( kind = core_rknd ), dimension(gr%nz) :: &
min_x_allowable_lev, & ! Smallest usuable value of x at lev k [units vary]
max_x_allowable_lev, & ! Largest usuable value of x at lev k [units vary]
min_x_allowable, & ! Smallest usuable x within k +/- num_levs [units vary]
max_x_allowable, & ! Largest usuable x within k +/- num_levs [units vary]
wpxp_mfl_max, & ! Upper limit on w'x'(k) [units vary]
wpxp_mfl_min ! Lower limit on w'x'(k) [units vary]
real( kind = core_rknd ) :: &
max_xp2, & ! Maximum allowable x'^2 [units vary]
stnd_dev_x, & ! Standard deviation of x [units vary]
max_dev, & ! Determines approximate upper/lower limit of x [units vary]
m_adv_term, & ! Contribution of mean advection to d(xm)/dt [units vary]
xm_density_weighted, & ! Density weighted xm at domain top [units vary]
xm_adj_coef, & ! Coeffecient to eliminate spikes at domain top [units vary]
xm_vert_integral, & ! Vertical integral of xm [units_vary]
dz ! zm grid spacing at top of domain [m]
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs_mfl_xm ! Left hand side of tridiagonal matrix
real( kind = core_rknd ), dimension(gr%nz) :: &
rhs_mfl_xm ! Right hand side of tridiagonal matrix equation
integer :: &
k, km1 ! Array indices
! integer, parameter :: &
! num_levs = 10 ! Number of levels above and below level k to look for
! ! maxima and minima of variable x.
integer :: &
low_lev, & ! Lowest level (from level k) to look for x minima and maxima
high_lev ! Highest level (from level k) to look for x minima and maxima
integer :: &
iwpxp_mfl, &
ixm_mfl
!--- Begin Code ---
! Default Initialization required due to G95 compiler warning
max_xp2 = 0.0_core_rknd
dz = 0.0_core_rknd
select case( solve_type )
case ( mono_flux_rtm ) ! rtm/wprtp
iwpxp_mfl = iwprtp_mfl
ixm_mfl = irtm_mfl
max_xp2 = 5.0e-6_core_rknd
case ( mono_flux_thlm ) ! thlm/wpthlp
iwpxp_mfl = iwpthlp_mfl
ixm_mfl = ithlm_mfl
max_xp2 = 5.0_core_rknd
case ( mono_flux_um ) ! um/upwp
iwpxp_mfl = iupwp_mfl
ixm_mfl = ium_mfl
max_xp2 = 10.0_core_rknd
case ( mono_flux_vm ) ! vm/vpwp
iwpxp_mfl = ivpwp_mfl
ixm_mfl = ivm_mfl
max_xp2 = 10.0_core_rknd
case default ! passive scalars are involved
iwpxp_mfl = 0
ixm_mfl = 0
max_xp2 = 5.0_core_rknd
end select
if ( l_stats_samp ) then
call stat_begin_update( iwpxp_mfl, wpxp / dt, stats_zm )
call stat_begin_update( ixm_mfl, xm / dt, stats_zt )
endif
if ( l_stats_samp .and. solve_type == mono_flux_thlm ) then
call stat_update_var( ithlm_enter_mfl, xm, stats_zt )
call stat_update_var( ithlm_old, xm_old, stats_zt )
call stat_update_var( iwpthlp_entermfl, xm, stats_zm )
elseif ( l_stats_samp .and. solve_type == mono_flux_rtm ) then
call stat_update_var( irtm_enter_mfl, xm, stats_zt )
call stat_update_var( irtm_old, xm_old, stats_zt )
call stat_update_var( iwprtp_enter_mfl, xm, stats_zm )
endif
! Initialize arrays.
wpxp_net_adjust = 0.0_core_rknd
dxm_dt_mfl_adjust = 0.0_core_rknd
! Store the value of xm as it enters the mfl
xm_enter_mfl = xm
! Interpolate x'^2 to thermodynamic levels.
xp2_zt = max( zm2zt( xp2 ), xp2_threshold )
! Place an upper limit on xp2_zt.
! For purposes of this subroutine, an upper limit has been placed on the
! variance, x'^2. This does not effect the value of x'^2 anywhere else in
! the model code. The upper limit is a reasonable upper limit. This is
! done to prevent unphysically large standard deviations caused by numerical
! instabilities in the x'^2 profile.
xp2_zt = min( xp2_zt, max_xp2 )
! Find the maximum and minimum usuable values of variable x at each
! vertical level. Start from level 2, which is the first level above
! the ground (or above the model surface). This computation needs to be
! performed for all vertical levels above the ground (or model surface).
do k = 2, gr%nz, 1
km1 = max( k-1, 1 )
!kp1 = min( k+1, gr%nz )
! Standard deviation is the square root of the variance.
stnd_dev_x = sqrt( xp2_zt(k) )
! Most values are found within +/- 2 standard deviations from the mean.
! Use +/- 2 standard deviations from the mean as the maximum/minimum
! values.
! max_dev = 2.0_core_rknd*stnd_dev_x
! Set a minimum on max_dev
max_dev = max(2.0_core_rknd * stnd_dev_x, xm_tol)
! Calculate the contribution of the mean advection term:
! m_adv_term = -wm_zt(k)*d(xm)/dz|_(k).
! Note: mean advection is not applied to xm at level gr%nz.
!if ( .not. l_implemented .and. k < gr%nz ) then
! tmp(1:3) = term_ma_zt_lhs( wm_zt(k), gr%invrs_dzt(k), k )
! m_adv_term = - tmp(1) * xm(kp1) &
! - tmp(2) * xm(k) &
! - tmp(3) * xm(km1)
!else
! m_adv_term = 0.0_core_rknd
!endif
! Shut off to avoid using new, possibly corrupt mean advection term
m_adv_term = 0.0_core_rknd
! Find the value of xm without the contribution from the turbulent
! advection term.
! Note: the contribution of xm_forcing at level gr%nz should be 0.
xm_without_ta(k) = xm_old(k) + dt*xm_forcing(k) &
+ dt*m_adv_term
! Find the minimum usuable value of variable x at each vertical level.
if ( solve_type /= mono_flux_um .and. solve_type /= mono_flux_vm ) then
! Since variable x must be one of theta_l, r_t, or a scalar, all of
! which are positive definite quantities, the value must be >= 0.
min_x_allowable_lev(k) &
= max( xm_without_ta(k) - max_dev, zero_threshold )
else ! solve_type == mono_flux_um .or. solve_type == mono_flux_vm
! Variable x must be one of u or v.
min_x_allowable_lev(k) = xm_without_ta(k) - max_dev
endif ! solve_type /= mono_flux_um .and. solve_type /= mono_flux_vm
! Find the maximum usuable value of variable x at each vertical level.
max_x_allowable_lev(k) = xm_without_ta(k) + max_dev
enddo
! Boundary condition on xm_without_ta
k = 1
xm_without_ta(k) = xm(k)
min_x_allowable_lev(k) = min_x_allowable_lev(k+1)
max_x_allowable_lev(k) = max_x_allowable_lev(k+1)
! Find the maximum and minimum usuable values of x that can effect the value
! of x at level k. Then, find the upper and lower limits of w'x'. Reset
! the value of w'x' if it is outside of those limits, and store the amount
! of adjustment that was needed to w'x'.
! The values of w'x' at level 1 and at level gr%nz are set values and
! are not altered.
do k = 2, gr%nz-1, 1
km1 = max( k-1, 1 )
low_lev = max( low_lev_effect(k), 2 )
high_lev = min( high_lev_effect(k), gr%nz )
!low_lev = max( k-num_levs, 2 )
!high_lev = min( k+num_levs, gr%nz )
! Find the smallest value of all relevant level minima for variable x.
min_x_allowable(k) = minval( min_x_allowable_lev(low_lev:high_lev) )
! Find the largest value of all relevant level maxima for variable x.
max_x_allowable(k) = maxval( max_x_allowable_lev(low_lev:high_lev) )
! Find the upper limit for w'x' for a monotonic turbulent flux.
wpxp_mfl_max(k) &
= invrs_rho_ds_zm(k) &
* ( ( rho_ds_zt(k) / (dt*gr%invrs_dzt(k)) ) &
* ( xm_without_ta(k) - min_x_allowable(k) ) &
+ rho_ds_zm(km1) * wpxp(km1) )
! Find the lower limit for w'x' for a monotonic turbulent flux.
wpxp_mfl_min(k) &
= invrs_rho_ds_zm(k) &
* ( ( rho_ds_zt(k) / (dt*gr%invrs_dzt(k)) ) &
* ( xm_without_ta(k) - max_x_allowable(k) ) &
+ rho_ds_zm(km1) * wpxp(km1) )
if ( wpxp(k) > wpxp_mfl_max(k) ) then
! This block of print statements can be uncommented for debugging.
!print *, "k = ", k
!print *, "wpxp too large (mfl)"
!print *, "xm(t) = ", xm_old(k)
!print *, "xm(t+1) entering mfl = ", xm(k)
!print *, "xm(t+1) without ta = ", xm_without_ta(k)
!print *, "max x allowable = ", max_x_allowable(k)
!print *, "min x allowable = ", min_x_allowable(k)
!print *, "1/rho_ds_zm(k) = ", invrs_rho_ds_zm(k)
!print *, "rho_ds_zt(k) = ", rho_ds_zt(k)
!print *, "rho_ds_zt(k)*(delta_zt/dt) = ", &
! real( rho_ds_zt(k) / (dt*gr%invrs_dzt(k)) )
!print *, "xm without ta - min x allow = ", &
! xm_without_ta(k) - min_x_allowable(k)
!print *, "rho_ds_zm(km1) = ", rho_ds_zm(km1)
!print *, "wpxp(km1) = ", wpxp(km1)
!print *, "rho_ds_zm(km1) * wpxp(km1) = ", rho_ds_zm(km1) * wpxp(km1)
!print *, "wpxp upper lim = ", wpxp_mfl_max(k)
!print *, "wpxp before adjustment = ", wpxp(k)
! Determine the net amount of adjustment needed for w'x'.
wpxp_net_adjust(k) = wpxp_mfl_max(k) - wpxp(k)
! Reset the value of w'x' to the upper limit allowed by the
! monotonic flux limiter.
wpxp(k) = wpxp_mfl_max(k)
elseif ( wpxp(k) < wpxp_mfl_min(k) ) then
! This block of print statements can be uncommented for debugging.
!print *, "k = ", k
!print *, "wpxp too small (mfl)"
!print *, "xm(t) = ", xm_old(k)
!print *, "xm(t+1) entering mfl = ", xm(k)
!print *, "xm(t+1) without ta = ", xm_without_ta(k)
!print *, "max x allowable = ", max_x_allowable(k)
!print *, "min x allowable = ", min_x_allowable(k)
!print *, "1/rho_ds_zm(k) = ", invrs_rho_ds_zm(k)
!print *, "rho_ds_zt(k) = ", rho_ds_zt(k)
!print *, "rho_ds_zt(k)*(delta_zt/dt) = ", &
! real( rho_ds_zt(k) / (dt*gr%invrs_dzt(k)) )
!print *, "xm without ta - max x allow = ", &
! xm_without_ta(k) - max_x_allowable(k)
!print *, "rho_ds_zm(km1) = ", rho_ds_zm(km1)
!print *, "wpxp(km1) = ", wpxp(km1)
!print *, "rho_ds_zm(km1) * wpxp(km1) = ", rho_ds_zm(km1) * wpxp(km1)
!print *, "wpxp lower lim = ", wpxp_mfl_min(k)
!print *, "wpxp before adjustment = ", wpxp(k)
! Determine the net amount of adjustment needed for w'x'.
wpxp_net_adjust(k) = wpxp_mfl_min(k) - wpxp(k)
! Reset the value of w'x' to the lower limit allowed by the
! monotonic flux limiter.
wpxp(k) = wpxp_mfl_min(k)
! This block of code can be uncommented for debugging.
!else
!
! ! wpxp(k) is okay.
! if ( wpxp_net_adjust(km1) /= 0.0_core_rknd ) then
! print *, "k = ", k
! print *, "wpxp is in an acceptable range (mfl)"
! print *, "xm(t) = ", xm_old(k)
! print *, "xm(t+1) entering mfl = ", xm(k)
! print *, "xm(t+1) without ta = ", xm_without_ta(k)
! print *, "max x allowable = ", max_x_allowable(k)
! print *, "min x allowable = ", min_x_allowable(k)
! print *, "1/rho_ds_zm(k) = ", invrs_rho_ds_zm(k)
! print *, "rho_ds_zt(k) = ", rho_ds_zt(k)
! print *, "rho_ds_zt(k)*(delta_zt/dt) = ", &
! real( rho_ds_zt(k) / (dt*gr%invrs_dzt(k)) )
! print *, "xm without ta - min x allow = ", &
! xm_without_ta(k) - min_x_allowable(k)
! print *, "xm without ta - max x allow = ", &
! xm_without_ta(k) - max_x_allowable(k)
! print *, "rho_ds_zm(km1) = ", rho_ds_zm(km1)
! print *, "wpxp(km1) = ", wpxp(km1)
! print *, "rho_ds_zm(km1) * wpxp(km1) = ", &
! rho_ds_zm(km1) * wpxp(km1)
! print *, "wpxp upper lim = ", wpxp_mfl_max(k)
! print *, "wpxp lower lim = ", wpxp_mfl_min(k)
! print *, "wpxp (stays the same) = ", wpxp(k)
! endif
!
endif
enddo
! Boundary conditions
min_x_allowable(1) = 0._core_rknd
max_x_allowable(1) = 0._core_rknd
min_x_allowable(gr%nz) = 0._core_rknd
max_x_allowable(gr%nz) = 0._core_rknd
wpxp_mfl_min(1) = 0._core_rknd
wpxp_mfl_max(1) = 0._core_rknd
wpxp_mfl_min(gr%nz) = 0._core_rknd
wpxp_mfl_max(gr%nz) = 0._core_rknd
if ( l_stats_samp .and. solve_type == mono_flux_thlm ) then
call stat_update_var( ithlm_without_ta, xm_without_ta, stats_zt )
call stat_update_var( ithlm_mfl_min, min_x_allowable, stats_zt )
call stat_update_var( ithlm_mfl_max, max_x_allowable, stats_zt )
call stat_update_var( iwpthlp_mfl_min, wpxp_mfl_min, stats_zm )
call stat_update_var( iwpthlp_mfl_max, wpxp_mfl_max, stats_zm )
elseif ( l_stats_samp .and. solve_type == mono_flux_rtm ) then
call stat_update_var( irtm_without_ta, xm_without_ta, stats_zt )
call stat_update_var( irtm_mfl_min, min_x_allowable, stats_zt )
call stat_update_var( irtm_mfl_max, max_x_allowable, stats_zt )
call stat_update_var( iwprtp_mfl_min, wpxp_mfl_min, stats_zm )
call stat_update_var( iwprtp_mfl_max, wpxp_mfl_max, stats_zm )
endif
if ( any( abs(wpxp_net_adjust(:)) > eps ) ) then
! Reset the value of xm to compensate for the change to w'x'.
if ( l_mfl_xm_imp_adj ) then
! A tridiagonal matrix is used to semi-implicitly re-solve for the
! values of xm at timestep index (t+1).
! Set up the left-hand side of the tridiagonal matrix equation.
call mfl_xm_lhs( dt, wm_zt, l_implemented, &
lhs_mfl_xm )
! Set up the right-hand side of tridiagonal matrix equation.
call mfl_xm_rhs( dt, xm_old, wpxp, xm_forcing, &
rho_ds_zm, invrs_rho_ds_zt, &
rhs_mfl_xm )
! Solve the tridiagonal matrix equation.
call mfl_xm_solve( solve_type, lhs_mfl_xm, rhs_mfl_xm, &
xm )
! Check for errors
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) return
end if
else ! l_mfl_xm_imp_adj = .false.
! An explicit adjustment is made to the values of xm at timestep
! index (t+1), which is based upon the array of the amounts of w'x'
! adjustments.
do k = 2, gr%nz, 1
km1 = max( k-1, 1 )
! The rate of change of the adjustment to xm due to the monotonic
! flux limiter.
dxm_dt_mfl_adjust(k) &
= - invrs_rho_ds_zt(k) &
* gr%invrs_dzt(k) &
* ( rho_ds_zm(k) * wpxp_net_adjust(k) &
- rho_ds_zm(km1) * wpxp_net_adjust(km1) )
! The net change to xm due to the monotonic flux limiter is the
! rate of change multiplied by the time step length. Add the
! product to xm to find the new xm resulting from the monotonic
! flux limiter.
xm(k) = xm(k) + dxm_dt_mfl_adjust(k) * dt
enddo
! Boundary condition on xm
xm(1) = xm(2)
endif ! l_mfl_xm_imp_adj
! This code can be uncommented for debugging.
!do k = 1, gr%nz, 1
! print *, "k = ", k, "xm(t) = ", xm_old(k), "new xm(t+1) = ", xm(k)
!enddo
!Ensure there are no spikes at the top of the domain
if (abs( xm(gr%nz) - xm_enter_mfl(gr%nz) ) > 10._core_rknd * xm_tol) then
dz = gr%zm(gr%nz) - gr%zm(gr%nz - 1)
xm_density_weighted = rho_ds_zt(gr%nz) &
* (xm(gr%nz) - xm_enter_mfl(gr%nz)) &
* dz
xm_vert_integral &
= vertical_integral &
( ((gr%nz - 1) - 2 + 1), rho_ds_zt(2:gr%nz - 1), &
xm(2:gr%nz - 1), gr%dzt(2:gr%nz - 1) )
!Check to ensure the vertical integral is not zero to avoid a divide
!by zero error
if (xm_vert_integral < eps) then
write(fstderr,*) "Vertical integral of xm is zero;", &
"mfl will remove spike at top of domain,", &
"but it will not conserve xm."
!Remove the spike at the top of the domain
xm(gr%nz) = xm_enter_mfl(gr%nz)
else
xm_adj_coef = xm_density_weighted / xm_vert_integral
!xm_adj_coef can not be smaller than -1
if (xm_adj_coef < -0.99_core_rknd) then
write(fstderr,*) "xm_adj_coef in mfl less than -0.99, " &
// "mx_adj_coef set to -0.99"
xm_adj_coef = -0.99_core_rknd
endif
!Apply the adjustment
xm = xm * (1._core_rknd + xm_adj_coef)
!Remove the spike at the top of the domain
xm(gr%nz) = xm_enter_mfl(gr%nz)
!This code can be uncommented to ensure conservation
!if (abs(sum(rho_ds_zt(2:gr%nz) * xm(2:gr%nz) / gr%invrs_dzt(2:gr%nz)) - &
! sum(rho_ds_zt(2:gr%nz) * xm_enter_mfl(2:gr%nz) / gr%invrs_dzt(2:gr%nz)))&
! > (1000 * xm_tol)) then
! write(fstderr,*) "NON-CONSERVATION in MFL", trim( solve_type ), &
! abs(sum(rho_ds_zt(2:gr%nz) * xm(2:gr%nz) / gr%invrs_dzt(2:gr%nz)) - &
! sum(rho_ds_zt(2:gr%nz) * xm_enter_mfl(2:gr%nz) / &
! gr%invrs_dzt(2:gr%nz)))
!
! write(fstderr,*) "XM_ENTER_MFL=", xm_enter_mfl
! write(fstderr,*) "XM_AFTER_SPIKE_REMOVAL", xm
! write(fstderr,*) "XM_TOL", xm_tol
! write(fstderr,*) "XM_ADJ_COEF", xm_adj_coef
!endif
endif ! xm_vert_integral < eps
endif ! spike at domain top
endif ! any( wpxp_net_adjust(:) /= 0.0_core_rknd )
if ( l_stats_samp ) then
call stat_end_update( iwpxp_mfl, wpxp / dt, stats_zm )
call stat_end_update( ixm_mfl, xm / dt, stats_zt )
if ( solve_type == mono_flux_thlm ) then
call stat_update_var( ithlm_exit_mfl, xm, stats_zt )
call stat_update_var( iwpthlp_exit_mfl, xm, stats_zm )
elseif ( solve_type == mono_flux_rtm ) then
call stat_update_var( irtm_exit_mfl, xm, stats_zt )
call stat_update_var( iwprtp_exit_mfl, xm, stats_zm )
endif
endif
return
end subroutine monotonic_turbulent_flux_limit
!=============================================================================
subroutine mfl_xm_lhs( dt, wm_zt, l_implemented, &
lhs )
! Description:
! This subroutine is part of the process of re-solving for xm at timestep
! index (t+1). This is done because the original solving process produced
! values outside of what is deemed acceptable by the monotonic flux limiter.
! Unlike the original formulation for advancing xm one timestep, which
! combines w'x' and xm in a band-diagonal solver, this formulation uses a
! tridiagonal solver to solve for only the value of xm(t+1), for w'x'(t+1)
! is known.
!
! Subroutine mfl_xm_lhs sets up the left-hand side of the matrix equation.
use grid_class, only: &
gr ! Variable(s)
use mean_adv, only: &
term_ma_zt_lhs ! Procedure(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Constant parameters
integer, parameter :: &
kp1_tdiag = 1, & ! Thermodynamic superdiagonal index.
k_tdiag = 2, & ! Thermodynamic main diagonal index.
km1_tdiag = 3 ! Thermodynamic subdiagonal index.
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep length [s]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
wm_zt ! w wind component on thermodynamic levels [m/s]
logical, intent(in) :: &
l_implemented ! Flag for CLUBB being implemented in a larger model.
! Output Variables
real( kind = core_rknd ), dimension(3,gr%nz), intent(out) :: &
lhs ! Left hand side of tridiagonal matrix
! Local Variables
integer :: k, km1 ! Array index
!-----------------------------------------------------------------------
! Initialize the left-hand side matrix to 0.
lhs = 0.0_core_rknd
! The xm loop runs between k = 2 and k = gr%nz. The value of xm at
! level k = 1, which is below the model surface, is simply set equal to the
! value of xm at level k = 2 after the solve has been completed.
! Setup LHS of the tridiagonal system
do k = 2, gr%nz, 1
km1 = max( k-1,1 )
! LHS xm mean advection (ma) term.
if ( .not. l_implemented ) then
lhs(kp1_tdiag:km1_tdiag,k) &
= lhs(kp1_tdiag:km1_tdiag,k) &
+ term_ma_zt_lhs( wm_zt(k), gr%invrs_dzt(k), k, gr%invrs_dzm(k), gr%invrs_dzm(km1) )
else
lhs(kp1_tdiag:km1_tdiag,k) &
= lhs(kp1_tdiag:km1_tdiag,k) + 0.0_core_rknd
endif
! LHS xm time tendency.
lhs(k_tdiag,k) &
= lhs(k_tdiag,k) + 1.0_core_rknd / dt
enddo ! xm loop: 2..gr%nz
! Boundary conditions.
! Lower boundary
k = 1
lhs(:,k) = 0.0_core_rknd
lhs(k_tdiag,k) = 1.0_core_rknd
return
end subroutine mfl_xm_lhs
!=============================================================================
subroutine mfl_xm_rhs( dt, xm_old, wpxp, xm_forcing, &
rho_ds_zm, invrs_rho_ds_zt, &
rhs )
! Description:
! This subroutine is part of the process of re-solving for xm at timestep
! index (t+1). This is done because the original solving process produced
! values outside of what is deemed acceptable by the monotonic flux limiter.
! Unlike the original formulation for advancing xm one timestep, which
! combines w'x' and xm in a band-diagonal solver, this formulation uses a
! tridiagonal solver to solve for only the value of xm(t+1), for w'x'(t+1)
! is known.
!
! Subroutine mfl_xm_rhs sets up the right-hand side of the matrix equation.
use grid_class, only: &
gr ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep length [s]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
xm_old, & ! xm; timestep (t) (thermodynamic levels) [units vary]
wpxp, & ! w'x'; timestep (t+1); limited (m-levs.) [units vary]
xm_forcing, & ! xm forcings (thermodynamic levels) [units vary]
rho_ds_zm, & ! Dry, static density on momentum levels [kg/m^3]
invrs_rho_ds_zt ! Inv. dry, static density @ thermo. levs. [m^3/kg]
! Output Variable
real( kind = core_rknd ), dimension(gr%nz), intent(out) :: &
rhs ! Right hand side of tridiagonal matrix equation
! Local Variables
integer :: k, km1 ! Array indices
!-----------------------------------------------------------------------
! Initialize the right-hand side vector to 0.
rhs = 0.0_core_rknd
! The xm loop runs between k = 2 and k = gr%nz. The value of xm at
! level k = 1, which is below the model surface, is simply set equal to the
! value of xm at level k = 2 after the solve has been completed.