forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_holes.F90
1461 lines (1114 loc) · 49.6 KB
/
fill_holes.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: fill_holes.F90 8738 2018-07-19 19:58:53Z [email protected] $
!===============================================================================
module fill_holes
implicit none
public :: fill_holes_driver, &
fill_holes_vertical, &
hole_filling_hm_one_lev, &
fill_holes_hydromet, &
fill_holes_wv, &
vertical_avg, &
vertical_integral, &
clip_hydromet_conc_mvr, &
setup_stats_indices
private :: fill_holes_multiplicative
private ! Set Default Scope
contains
!=============================================================================
subroutine fill_holes_vertical( num_draw_pts, threshold, field_grid, &
rho_ds, rho_ds_zm, &
field )
! Description:
! This subroutine clips values of 'field' that are below 'threshold' as much
! as possible (i.e. "fills holes"), but conserves the total integrated mass
! of 'field'. This prevents clipping from acting as a spurious source.
!
! Mass is conserved by reducing the clipped field everywhere by a constant
! multiplicative coefficient.
!
! This subroutine does not guarantee that the clipped field will exceed
! threshold everywhere; blunt clipping is needed for that.
! References:
! ``Numerical Methods for Wave Equations in Geophysical Fluid
! Dynamics'', Durran (1999), p. 292.
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variable
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input variables
integer, intent(in) :: &
num_draw_pts ! The number of points on either side of the hole;
! Mass is drawn from these points to fill the hole. []
real( kind = core_rknd ), intent(in) :: &
threshold ! A threshold (e.g. w_tol*w_tol) below which field must not
! fall [Units vary; same as field]
character(len=2), intent(in) :: &
field_grid ! The grid of the field, either zt or zm
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
rho_ds, & ! Dry, static density on thermodynamic levels [kg/m^3]
rho_ds_zm ! Dry, static density on momentum levels [kg/m^3]
! Input/Output variable
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
field ! The field (e.g. wp2) that contains holes [Units same as threshold]
! Local Variables
integer :: &
k, & ! Loop index for absolute grid level []
begin_idx, & ! Lower grid level of local hole-filling range []
end_idx, & ! Upper grid level of local hole-filling range []
upper_hf_level ! Upper grid level of global hole-filling range []
!-----------------------------------------------------------------------
! Check whether any holes exist in the entire profile.
! The lowest level (k=1) should not be included, as the hole-filling scheme
! should not alter the set value of 'field' at the surface (for momentum
! level variables), or consider the value of 'field' at a level below the
! surface (for thermodynamic level variables). For momentum level variables
! only, the hole-filling scheme should not alter the set value of 'field' at
! the upper boundary level (k=gr%nz).
if ( field_grid == "zt" ) then
! 'field' is on the zt (thermodynamic level) grid
upper_hf_level = gr%nz
elseif ( field_grid == "zm" ) then
! 'field' is on the zm (momentum level) grid
upper_hf_level = gr%nz-1
endif
if ( any( field( 2:upper_hf_level ) < threshold ) ) then
! Make one pass up the profile, filling holes as much as we can using
! nearby mass.
! The lowest level (k=1) should not be included in the loop, as the
! hole-filling scheme should not alter the set value of 'field' at the
! surface (for momentum level variables), or consider the value of
! 'field' at a level below the surface (for thermodynamic level
! variables). For momentum level variables only, the hole-filling scheme
! should not alter the set value of 'field' at the upper boundary
! level (k=gr%nz).
do k = 2+num_draw_pts, upper_hf_level-num_draw_pts, 1
begin_idx = k - num_draw_pts
end_idx = k + num_draw_pts
if ( any( field( begin_idx:end_idx ) < threshold ) ) then
! 'field' is on the zt (thermodynamic level) grid
if ( field_grid == "zt" ) then
call fill_holes_multiplicative &
( begin_idx, end_idx, threshold, &
rho_ds(begin_idx:end_idx), gr%dzt(begin_idx:end_idx), &
field(begin_idx:end_idx) )
! 'field' is on the zm (momentum level) grid
elseif ( field_grid == "zm" ) then
call fill_holes_multiplicative &
( begin_idx, end_idx, threshold, &
rho_ds_zm(begin_idx:end_idx), gr%dzm(begin_idx:end_idx), &
field(begin_idx:end_idx) )
endif
endif
enddo
! Fill holes globally, to maximize the chance that all holes are filled.
! The lowest level (k=1) should not be included, as the hole-filling
! scheme should not alter the set value of 'field' at the surface (for
! momentum level variables), or consider the value of 'field' at a level
! below the surface (for thermodynamic level variables). For momentum
! level variables only, the hole-filling scheme should not alter the set
! value of 'field' at the upper boundary level (k=gr%nz).
if ( any( field( 2:upper_hf_level ) < threshold ) ) then
! 'field' is on the zt (thermodynamic level) grid
if ( field_grid == "zt" ) then
call fill_holes_multiplicative &
( 2, upper_hf_level, threshold, &
rho_ds(2:upper_hf_level), gr%dzt(2:upper_hf_level), &
field(2:upper_hf_level) )
! 'field' is on the zm (momentum level) grid
elseif ( field_grid == "zm" ) then
call fill_holes_multiplicative &
( 2, upper_hf_level, threshold, &
rho_ds_zm(2:upper_hf_level), gr%dzm(2:upper_hf_level), &
field(2:upper_hf_level) )
endif
endif
endif ! End overall check for existence of holes
return
end subroutine fill_holes_vertical
!=============================================================================
subroutine fill_holes_multiplicative &
( begin_idx, end_idx, threshold, &
rho, dz, &
field )
! Description:
! This subroutine clips values of 'field' that are below 'threshold' as much
! as possible (i.e. "fills holes"), but conserves the total integrated mass
! of 'field'. This prevents clipping from acting as a spurious source.
!
! Mass is conserved by reducing the clipped field everywhere by a constant
! multiplicative coefficient.
!
! This subroutine does not guarantee that the clipped field will exceed
! threshold everywhere; blunt clipping is needed for that.
! References:
! ``Numerical Methods for Wave Equations in Geophysical Fluid
! Dynamics", Durran (1999), p. 292.
!-----------------------------------------------------------------------
use constants_clubb, only: &
eps
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input variables
integer, intent(in) :: &
begin_idx, & ! The beginning index (e.g. k=2) of the range of hole-filling
end_idx ! The end index (e.g. k=gr%nz) of the range of hole-filling
real( kind = core_rknd ), intent(in) :: &
threshold ! A threshold (e.g. w_tol*w_tol) below which field must not fall
! [Units vary; same as field]
real( kind = core_rknd ), dimension(end_idx-begin_idx+1), intent(in) :: &
rho, & ! Dry, static density on either thermodynamic or momentum levels [kg/m^3]
dz ! Reciprocal of thermodynamic or momentum level thickness depending on whether
! we're on zt or zm grid.
! Input/Output variable
real( kind = core_rknd ), dimension(end_idx-begin_idx+1), intent(inout) :: &
field ! The field (e.g. wp2) that contains holes
! [Units same as threshold]
! Local Variables
real( kind = core_rknd ), dimension(end_idx-begin_idx+1) :: &
field_clipped ! The raw field (e.g. wp2) that contains no holes
! [Units same as threshold]
real( kind = core_rknd ) :: &
field_avg, & ! Vertical average of field [Units of field]
field_clipped_avg, & ! Vertical average of clipped field [Units of field]
mass_fraction ! Coefficient that multiplies clipped field
! in order to conserve mass. []
!-----------------------------------------------------------------------
! Compute the field's vertical average, which we must conserve.
field_avg = vertical_avg( (end_idx-begin_idx+1), rho, &
field, dz )
! Clip small or negative values from field.
if ( field_avg >= threshold ) then
! We know we can fill in holes completely
field_clipped = max( threshold, field )
else
! We can only fill in holes partly;
! to do so, we remove all mass above threshold.
field_clipped = min( threshold, field )
endif
! Compute the clipped field's vertical integral.
! clipped_total_mass >= original_total_mass
field_clipped_avg = vertical_avg( (end_idx-begin_idx+1), rho, &
field_clipped, dz )
! If the difference between the field_clipped_avg and the threshold is so
! small that it falls within numerical round-off, return to the parent
! subroutine without altering the field in order to avoid divide-by-zero
! error.
if ( abs(field_clipped_avg-threshold) <= abs(field_clipped_avg+threshold)*eps/2) then
return
endif
! Compute coefficient that makes the clipped field have the same mass as the
! original field. We should always have mass_fraction > 0.
mass_fraction = ( field_avg - threshold ) / &
( field_clipped_avg - threshold )
! Output normalized, filled field
field = mass_fraction * ( field_clipped - threshold ) &
+ threshold
return
end subroutine fill_holes_multiplicative
!=============================================================================
function vertical_avg( total_idx, rho_ds, field, dz )
! Description:
! Computes the density-weighted vertical average of a field.
!
! The average value of a function, f, over a set domain, [a,b], is
! calculated by the equation:
!
! f_avg = ( INT(a:b) f*g ) / ( INT(a:b) g );
!
! as long as f is continous and g is nonnegative and integrable. Therefore,
! the density-weighted (by dry, static, base-static density) vertical
! average value of any model field, x, is calculated by the equation:
!
! x_avg|_z = ( INT(z_bot:z_top) x rho_ds dz )
! / ( INT(z_bot:z_top) rho_ds dz );
!
! where z_bot is the bottom of the vertical domain, and z_top is the top of
! the vertical domain.
!
! This calculation is done slightly differently depending on whether x is a
! thermodynamic-level or a momentum-level variable.
!
! Thermodynamic-level computation:
!
! For numerical purposes, INT(z_bot:z_top) x rho_ds dz, which is the
! numerator integral, is calculated as:
!
! SUM(k_bot:k_top) x(k) rho_ds(k) delta_z(k);
!
! where k is the index of the given thermodynamic level, x and rho_ds are
! both thermodynamic-level variables, and delta_z(k) = zm(k) - zm(k-1). The
! indices k_bot and k_top are the indices of the respective lower and upper
! thermodynamic levels involved in the integration.
!
! Likewise, INT(z_bot:z_top) rho_ds dz, which is the denominator integral,
! is calculated as:
!
! SUM(k_bot:k_top) rho_ds(k) delta_z(k).
!
! The first (k=1) thermodynamic level is below ground (or below the
! official lower boundary at the first momentum level), so it should not
! count in a vertical average, whether that vertical average is used for
! the hole-filling scheme or for statistical purposes. Begin no lower
! than level k=2, which is the first thermodynamic level above ground (or
! above the model lower boundary).
!
! For cases where hole-filling over the entire (global) vertical domain
! is desired, or where statistics over the entire (global) vertical
! domain are desired, the lower (thermodynamic-level) index of k = 2 and
! the upper (thermodynamic-level) index of k = gr%nz, means that the
! overall vertical domain will be gr%zm(gr%nz) - gr%zm(1).
!
!
! Momentum-level computation:
!
! For numerical purposes, INT(z_bot:z_top) x rho_ds dz, which is the
! numerator integral, is calculated as:
!
! SUM(k_bot:k_top) x(k) rho_ds(k) delta_z(k);
!
! where k is the index of the given momentum level, x and rho_ds are both
! momentum-level variables, and delta_z(k) = zt(k+1) - zt(k). The indices
! k_bot and k_top are the indices of the respective lower and upper momentum
! levels involved in the integration.
!
! Likewise, INT(z_bot:z_top) rho_ds dz, which is the denominator integral,
! is calculated as:
!
! SUM(k_bot:k_top) rho_ds(k) delta_z(k).
!
! The first (k=1) momentum level is right at ground level (or right at
! the official lower boundary). The momentum level variables that call
! the hole-filling scheme have set values at the surface (or lower
! boundary), and those set values should not be changed. Therefore, the
! vertical average (for purposes of hole-filling) should not include the
! surface level (or lower boundary level). For hole-filling purposes,
! begin no lower than level k=2, which is the second momentum level above
! ground (or above the model lower boundary). Likewise, the value at the
! model upper boundary (k=gr%nz) is also set for momentum level
! variables. That value should also not be changed.
!
! However, this function is also used to keep track (for statistical
! purposes) of the vertical average of certain variables. In that case,
! the vertical average needs to be taken over the entire vertical domain
! (level 1 to level gr%nz).
!
!
! In both the thermodynamic-level computation and the momentum-level
! computation, the numerator integral is divided by the denominator integral
! in order to find the average value (over the vertical domain) of x.
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input variables
integer, intent(in) :: &
total_idx ! The total numer of indices within the range of averaging
real( kind = core_rknd ), dimension(total_idx), intent(in) :: &
rho_ds, & ! Dry, static density on either thermodynamic or momentum levels [kg/m^3]
field, & ! The field (e.g. wp2) to be vertically averaged [Units vary]
dz ! Reciprocal of thermodynamic or momentum level thickness [1/m]
! depending on whether we're on zt or zm grid.
! Note: The rho_ds and field points need to be arranged from
! lowest to highest in altitude, with rho_ds(1) and
! field(1) actually their respective values at level k = 1.
! Output variable
real( kind = core_rknd ) :: &
vertical_avg ! Vertical average of field [Units of field]
! Local variables
real( kind = core_rknd ) :: &
numer_integral, & ! Integral in the numerator (see description)
denom_integral ! Integral in the denominator (see description)
integer :: k
!-----------------------------------------------------------------------
! Initialize variable
numer_integral = 0.0_core_rknd
denom_integral = 0.0_core_rknd
! Compute the numerator and denominator integral.
! Multiply rho_ds at level k by the level thickness
! at level k. Then, sum over all vertical levels.
do k=1, total_idx
numer_integral = numer_integral + rho_ds(k) * dz(k) * field(k)
denom_integral = denom_integral + rho_ds(k) * dz(k)
end do
! Find the vertical average of 'field'.
vertical_avg = numer_integral / denom_integral
return
end function vertical_avg
!=============================================================================
pure function vertical_integral( total_idx, rho_ds, &
field, dz )
! Description:
! Computes the vertical integral. rho_ds, field, and dz must all be
! of size total_idx and should all start at the same index.
!
! References:
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd ! Variable(s)
implicit none
! Input variables
integer, intent(in) :: &
total_idx ! The total numer of indices within the range of averaging
real( kind = core_rknd ), dimension(total_idx), intent(in) :: &
rho_ds, & ! Dry, static density [kg/m^3]
field, & ! The field to be vertically averaged [Units vary]
dz ! Level thickness [1/m]
! Note: The rho_ds and field points need to be arranged from
! lowest to highest in altitude, with rho_ds(1) and
! field(1) actually their respective values at level k = begin_idx.
! Local variables
real( kind = core_rknd ) :: &
vertical_integral ! Integral in the numerator (see description)
!-----------------------------------------------------------------------
! Assertion checks: that begin_idx <= gr%nz - 1
! that end_idx >= 2
! that begin_idx <= end_idx
! Initializing vertical_integral to avoid a compiler warning.
vertical_integral = 0.0_core_rknd
! Compute the integral.
! Multiply the field at level k by rho_ds at level k and by
! the level thickness at level k. Then, sum over all vertical levels.
! Note: The values of the field and rho_ds are passed into this function
! so that field(1) and rho_ds(1) are actually the field and rho_ds
! at level k_start.
vertical_integral = sum( field * rho_ds * dz )
!print *, vertical_integral
return
end function vertical_integral
!===============================================================================
subroutine hole_filling_hm_one_lev( num_hm_fill, hm_one_lev, & ! Intent(in)
hm_one_lev_filled ) ! Intent(out)
! Description:
! Fills holes between same-phase (i.e. either liquid or frozen) hydrometeors for
! one height level.
!
! Warning: Do not input hydrometeors of different phases, e.g. liquid and frozen.
! Otherwise heat will not be conserved.
!
! References:
!
! None
!-----------------------------------------------------------------------
use constants_clubb, only: &
one, & ! Variable(s)
zero, &
eps
use clubb_precision, only: &
core_rknd ! Variable(s)
use error_code, only: &
clubb_at_least_debug_level ! Procedure
implicit none
! Input Variables
integer, intent(in) :: num_hm_fill ! number of hydrometeors involved
real(kind = core_rknd), dimension(num_hm_fill), intent(in) :: hm_one_lev
! Output Variables
real(kind = core_rknd), dimension(num_hm_fill), intent(out) :: hm_one_lev_filled
! Local Variables
integer :: num_neg_hm ! number of holes
real(kind = core_rknd) :: &
total_hole, & ! Size of the hole ( missing mass, less than 0 )
total_mass ! Total mass to fill the hole
! total mass of water substance = total_mass + total_hole
integer :: i ! loop iterator
!-----------------------------------------------------------------------
!----- Begin Code -----
! Initialization
hm_one_lev_filled = 0._core_rknd
total_hole = 0._core_rknd
total_mass = 0._core_rknd
num_neg_hm = 0
! Determine the total size of the hole and the number of neg. hydrometeors
! and the total mass of hole filling material
do i=1, num_hm_fill
! print *, "hm_one_lev(",i,") = ", hm_one_lev(i)
if ( hm_one_lev(i) < zero ) then
total_hole = total_hole + hm_one_lev(i) ! less than zero
num_neg_hm = num_neg_hm + 1
else
total_mass = total_mass + hm_one_lev(i)
endif
enddo
! print *, "total_hole = ", total_hole
! print *, "total_mass = ", total_mass
! print *, "num_neg_hm = ", num_neg_hm
! There is no water substance at all to fill the hole
if ( abs(total_mass) < eps ) then
if ( clubb_at_least_debug_level( 2 ) ) then
print *, "Warning: One-level hole filling was not successful! total_mass ~= 0"
endif
hm_one_lev_filled = hm_one_lev
return
endif
! Fill the holes and adjust the remaining quantities:
! hm_filled(i) = 0, if hm(i) < 0
! or
! hm_filled(i) = (1 + total_hole/total_mass)*hm(i), if hm(i) > 0
do i=1, num_hm_fill
! if there is not enough material, fill the holes partially with all the material available
if ( abs(total_hole) > total_mass ) then
if ( clubb_at_least_debug_level( 2 ) ) then
print *, "Warning: One-level hole filling was not able to fill holes completely!" // &
" The holes were filled partially. |total_hole| > total_mass"
endif
hm_one_lev_filled(i) = min(hm_one_lev(i), zero) * ( one + total_mass / total_hole )
else ! fill holes completely
hm_one_lev_filled(i) = max(hm_one_lev(i), zero) * ( one + total_hole / total_mass )
endif
enddo
! Assertion checks (water substance conservation, non-negativity)
if ( clubb_at_least_debug_level( 2 ) ) then
if ( abs(sum( hm_one_lev ) - sum(hm_one_lev_filled)) > &
abs(sum( hm_one_lev ) + sum(hm_one_lev_filled)) * eps/2 ) then
print *, "Warning: Hole filling was not conservative!"
endif
if ( any( hm_one_lev_filled < zero ) ) then
print *, "Warning: Hole filling failed! A hole could not be filled."
endif
endif
return
end subroutine hole_filling_hm_one_lev
!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
subroutine fill_holes_hydromet( nz, hydromet_dim, hydromet, & ! Intent(in)
hydromet_filled ) ! Intent(out)
! Description:
! Fills holes between same-phase hydrometeors(i.e. for frozen hydrometeors).
! The hole filling conserves water substance between all same-phase (frozen or liquid)
! hydrometeors at each height level.
!
! Attention: The hole filling for the liquid phase hydrometeors is not yet implemented
!
! Attention: l_frozen_hm and l_mix_rat_hm need to be set up before this subroutine is called!
!
! References:
!
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd
use array_index, only: &
l_frozen_hm, & ! Variable(s)
l_mix_rat_hm
use constants_clubb, only: &
zero
implicit none
! Input Variables
integer, intent(in) :: hydromet_dim, nz
real( kind = core_rknd ), dimension(nz,hydromet_dim), intent(in) :: &
hydromet
! Output Variables
real( kind = core_rknd ), dimension(nz,hydromet_dim), intent(out) :: &
hydromet_filled
! Local Variables
integer :: i,j ! Loop iterators
integer :: num_frozen_hm ! Number of frozen hydrometeor mixing ratios
real( kind = core_rknd ), dimension(:,:), allocatable :: &
hydromet_frozen, & ! Frozen hydrometeor mixing ratios
hydromet_frozen_filled ! Frozen hydrometeor mixing ratios after hole filling
!-----------------------------------------------------------------------
!----- Begin Code -----
! Determine the number of frozen hydrometeor mixing ratios
num_frozen_hm = 0
do i=1,hydromet_dim
if ( l_frozen_hm(i) .and. l_mix_rat_hm(i) ) then
num_frozen_hm = num_frozen_hm + 1
endif
enddo
! Allocation
allocate( hydromet_frozen(nz,num_frozen_hm) )
allocate( hydromet_frozen_filled(nz,num_frozen_hm) )
! Determine frozen hydrometeor mixing ratios
j = 1
do i = 1,hydromet_dim
if ( l_frozen_hm(i) .and. l_mix_rat_hm(i) ) then
hydromet_frozen(:,j) = hydromet(:,i)
j = j+1
endif
enddo
! Fill holes for the frozen hydrometeors
do i=1,nz
if ( any( hydromet_frozen(i,:) < zero ) ) then
call hole_filling_hm_one_lev( num_frozen_hm, hydromet_frozen(i,:), & ! Intent(in)
hydromet_frozen_filled(i,:) ) ! Intent(out)
else
hydromet_frozen_filled(i,:) = hydromet_frozen(i,:)
endif
enddo
! Setup the filled hydromet array
j = 1
do i=1, hydromet_dim
if ( l_frozen_hm(i) .and. l_mix_rat_hm(i) ) then
hydromet_filled(:,i) = hydromet_frozen_filled(:,j)
j = j+1
else
hydromet_filled(:,i) = hydromet(:,i)
endif
enddo
!!! Here we could do the same hole filling for all the liquid phase hydrometeors
return
end subroutine fill_holes_hydromet
!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
subroutine fill_holes_wv( nz, dt, exner, hydromet_name, & ! Intent(in)
rvm_mc, thlm_mc, hydromet )! Intent(inout)
! Description:
! Fills holes using the cloud water mixing ratio from the current height level.
!
! References:
!
! None
!-----------------------------------------------------------------------
use clubb_precision, only: &
core_rknd
use constants_clubb, only: &
zero_threshold, &
Lv, &
Ls, &
Cp
implicit none
! Input Variables
integer, intent(in) :: nz
real( kind = core_rknd ), intent(in) :: &
dt ! Timestep [s]
character(len=10), intent(in) :: hydromet_name
real( kind = core_rknd ), dimension(nz), intent(in) :: &
exner ! Exner function [-]
! Input/Output Variables
real( kind = core_rknd ), dimension(nz), intent(inout) :: &
hydromet, & ! Hydrometeor array [units vary]
rvm_mc, &
thlm_mc
! Local Variables
integer :: k ! Loop iterator
real( kind = core_rknd ) :: rvm_clip_tndcy
!-----------------------------------------------------------------------
!----- Begin Code -----
do k = 2, nz, 1
if ( hydromet(k) < zero_threshold ) then
! Set rvm_clip_tndcy to the time tendency applied to vapor and removed
! from the hydrometeor.
rvm_clip_tndcy = hydromet(k) / dt
! Adjust the tendency rvm_mc accordingly
rvm_mc(k) = rvm_mc(k) + rvm_clip_tndcy
! Adjust the tendency of thlm_mc according to whether the
! effect is an evaporation or sublimation tendency.
select case ( trim( hydromet_name ) )
case( "rrm" )
thlm_mc(k) = thlm_mc(k) - rvm_clip_tndcy * ( Lv / ( Cp*exner(k) ) )
case( "rim", "rsm", "rgm" )
thlm_mc(k) = thlm_mc(k) - rvm_clip_tndcy * ( Ls / ( Cp*exner(k) ) )
case default
stop "Fatal error in microphys_driver"
end select
! Set the mixing ratio to 0
hydromet(k) = zero_threshold
endif ! hydromet(k,i) < 0
enddo ! k = 2..gr%nz
return
end subroutine fill_holes_wv
!-----------------------------------------------------------------------
!-----------------------------------------------------------------------
subroutine fill_holes_driver( nz, dt, hydromet_dim, & ! Intent(in)
l_fill_holes_hm, & ! Intent(in)
rho_ds_zm, rho_ds_zt, exner, & ! Intent(in)
thlm_mc, rvm_mc, hydromet ) ! Intent(inout)
! Description:
! Fills holes between same-phase hydrometeors(i.e. for frozen hydrometeors).
! The hole filling conserves water substance between all same-phase (frozen or liquid)
! hydrometeors at each height level.
!
! Attention: The hole filling for the liquid phase hydrometeors is not yet implemented
!
! Attention: l_frozen_hm and l_mix_rat_hm need to be set up before this subroutine is called!
!
! References:
!
! None
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variable(s)
use clubb_precision, only: &
core_rknd ! Variable(s)
use constants_clubb, only: &
zero, &
zero_threshold, &
Lv, &
Ls, &
Cp, &
fstderr
use array_index, only: &
hydromet_list, & ! Names of the hydrometeor species
hydromet_tol
use array_index, only: &
l_mix_rat_hm, & ! Variable(s)
l_frozen_hm
use index_mapping, only: &
Nx2rx_hm_idx, & ! Procedure(s)
mvr_hm_max
use stats_type_utilities, only: &
stat_begin_update, & ! Subroutines
stat_end_update
use stats_variables, only: &
stats_zt, & ! Variables
l_stats_samp
use error_code, only: &
clubb_at_least_debug_level ! Procedure
implicit none
intrinsic :: trim
! Input Variables
integer, intent(in) :: hydromet_dim, nz
logical, intent(in) :: l_fill_holes_hm
real( kind = core_rknd ), intent(in) :: &
dt ! Timestep [s]
real( kind = core_rknd ), dimension(nz), intent(in) :: &
rho_ds_zm, & ! Dry, static density on momentum levels [kg/m^3]
rho_ds_zt ! Dry, static density on thermo. levels [kg/m^3]
real( kind = core_rknd ), dimension(nz), intent(in) :: &
exner ! Exner function [-]
! Input/Output Variables
real( kind = core_rknd ), dimension(nz, hydromet_dim), intent(inout) :: &
hydromet ! Mean of hydrometeor fields [units vary]
real( kind = core_rknd ), dimension(nz), intent(inout) :: &
rvm_mc, & ! Microphysics contributions to vapor water [kg/kg/s]
thlm_mc ! Microphysics contributions to liquid potential temp [K/s]
! Local Variables
integer :: i, k ! Loop iterators
real( kind = core_rknd ), dimension(nz, hydromet_dim) :: &
hydromet_filled, & ! Frozen hydrometeor mixing ratios after hole filling
hydromet_clipped ! Clipped mean of hydrometeor fields [units vary]
character( len = 10 ) :: hydromet_name
real( kind = core_rknd ) :: &
max_velocity ! Maximum sedimentation velocity [m/s]
integer :: ixrm_hf, ixrm_wvhf, ixrm_cl, &
ixrm_bt, ixrm_mc
logical :: l_hole_fill = .true.
!-----------------------------------------------------------------------
!----- Begin Code -----
! Start stats output for the _hf variables (changes in the hydromet array
! due to fill_holes_hydromet and fill_holes_vertical)
if ( l_stats_samp ) then
do i = 1, hydromet_dim
! Set up the stats indices for hydrometeor at index i
call setup_stats_indices( i, & ! Intent(in)
ixrm_bt, ixrm_hf, ixrm_wvhf, & ! Intent(inout)
ixrm_cl, ixrm_mc, & ! Intent(inout)
max_velocity ) ! Intent(inout)
call stat_begin_update( ixrm_hf, hydromet(:,i) &
/ dt, stats_zt )
enddo ! i = 1, hydromet_dim
endif ! l_stats_samp
! If we're dealing with negative hydrometeors, we first try to fill the
! holes proportionally from other same-phase hydrometeors at each height
! level.
if ( any( hydromet < zero_threshold ) .and. l_fill_holes_hm ) then
call fill_holes_hydromet( nz, hydromet_dim, hydromet, & ! Intent(in)
hydromet_filled ) ! Intent(out)
hydromet = hydromet_filled
endif ! any( hydromet < zero ) .and. l_fill_holes_hm
hydromet_filled = zero
do i = 1, hydromet_dim
! Set up the stats indices for hydrometeor at index i
call setup_stats_indices( i, & ! Intent(in)
ixrm_bt, ixrm_hf, ixrm_wvhf, & ! Intent(inout)
ixrm_cl, ixrm_mc, & ! Intent(inout)
max_velocity ) ! Intent(inout)
hydromet_name = hydromet_list(i)
! Print warning message if any hydrometeor species has a value < 0.
if ( clubb_at_least_debug_level( 1 ) ) then
if ( any( hydromet(:,i) < zero_threshold ) ) then
do k = 1, nz
if ( hydromet(k,i) < zero_threshold ) then
write(fstderr,*) trim( hydromet_name ) //" < ", &
zero_threshold, &
" in fill_holes_driver at k= ", k
endif ! hydromet(k,i) < 0
enddo ! k = 1, nz
endif ! hydromet(:,i) < 0
endif ! clubb_at_least_debug_level( 1 )
! Store the previous value of the hydrometeor for the effect of the
! hole-filling scheme.
! if ( l_stats_samp ) then
! call stat_begin_update( ixrm_hf, hydromet(:,i) &
! / dt, stats_zt )
! endif
! If we're dealing with a mixing ratio and hole filling is enabled,
! then we apply the hole filling algorithm
if ( any( hydromet(:,i) < zero_threshold ) ) then
if ( hydromet_name(1:1) == "r" .and. l_hole_fill ) then
! Apply the hole filling algorithm
call fill_holes_vertical( 2, zero_threshold, "zt", &
rho_ds_zt, rho_ds_zm, &
hydromet(:,i) )
endif ! Variable is a mixing ratio and l_hole_fill is true
endif ! hydromet(:,i) < 0
! Enter the new value of the hydrometeor for the effect of the
! hole-filling scheme.
if ( l_stats_samp ) then
call stat_end_update( ixrm_hf, hydromet(:,i) &
/ dt, stats_zt )
endif
! Store the previous value of the hydrometeor for the effect of the water
! vapor hole-filling scheme.
if ( l_stats_samp ) then
call stat_begin_update( ixrm_wvhf, hydromet(:,i) &
/ dt, stats_zt )
endif
if ( any( hydromet(:,i) < zero_threshold ) ) then
if ( hydromet_name(1:1) == "r" .and. l_hole_fill ) then
! If the hole filling algorithm failed, then we attempt to fill
! the missing mass with water vapor mixing ratio.
! We noticed this is needed for ASEX A209, particularly if Latin
! hypercube sampling is enabled. -dschanen 11 Nov 2010
call fill_holes_wv( nz, dt, exner, hydromet_name, & ! Intent(in)
rvm_mc, thlm_mc, hydromet(:,i) ) ! Intent(out)
endif ! Variable is a mixing ratio and l_hole_fill is true
endif ! hydromet(:,i) < 0