forked from ESCOMP/CLUBB_CESM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
advance_windm_edsclrm_module.F90
1831 lines (1505 loc) · 71.5 KB
/
advance_windm_edsclrm_module.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!------------------------------------------------------------------------
! $Id$
!===============================================================================
module advance_windm_edsclrm_module
implicit none
private ! Set Default Scope
public :: advance_windm_edsclrm, xpwp_fnc
private :: windm_edsclrm_solve, &
compute_uv_tndcy, &
windm_edsclrm_lhs, &
windm_edsclrm_rhs
! Private named constants to avoid string comparisons
integer, parameter, private :: &
windm_edsclrm_um = 1, & ! Named constant to handle um solves
windm_edsclrm_vm = 2, & ! Named constant to handle vm solves
windm_edsclrm_scalar = 3, & ! Named constant to handle scalar solves
clip_upwp = 10, & ! Named constant for upwp clipping
! NOTE: This must be the same as the clip_upwp
! declared in clip_explicit!
clip_vpwp = 11 ! Named constant for vpwp clipping
! NOTE: This must be the same as the clip_vpwp
! declared in clip_explicit!
contains
!=============================================================================
subroutine advance_windm_edsclrm &
( dt, wm_zt, Km_zm, Kmh_zm, ug, vg, um_ref, vm_ref, &
wp2, up2, vp2, um_forcing, vm_forcing, &
edsclrm_forcing, &
rho_ds_zm, invrs_rho_ds_zt, &
fcor, l_implemented, &
um, vm, edsclrm, &
upwp, vpwp, wpedsclrp )
! Description:
! Solves for both mean horizontal wind components, um and vm, and for the
! eddy-scalars (passive scalars that don't use the high-order closure).
! Uses the LAPACK tridiagonal solver subroutine with 2 + # of scalar(s)
! back substitutions (since the left hand side matrix is the same for all
! input variables).
! References:
! Eqn. 8 & 9 on p. 3545 of
! ``A PDF-Based Model for Boundary Layer Clouds. Part I:
! Method and Model Description'' Golaz, et al. (2002)
! JAS, Vol. 59, pp. 3540--3551.
!-----------------------------------------------------------------------
use grid_class, only: &
gr ! Variables(s)
use parameters_model, only: &
ts_nudge, & ! Variable(s)
edsclr_dim
use parameters_tunable, only: &
nu10_vert_res_dep ! Constant
use model_flags, only: &
l_predict_upwp_vpwp, & ! Variable(s)
l_uv_nudge, &
l_tke_aniso
use clubb_precision, only: &
core_rknd ! Variable(s)
use stats_type_utilities, only: &
stat_begin_update, & ! Subroutines
stat_end_update, &
stat_update_var
use stats_variables, only: &
ium_ref, & ! Variables
ivm_ref, &
ium_sdmp, &
ivm_sdmp, &
ium_ndg, &
ivm_ndg, &
iwindm_matrix_condt_num, &
stats_zt, &
l_stats_samp
use clip_explicit, only: &
clip_covar ! Procedure(s)
use error_code, only: &
clubb_at_least_debug_level, & ! Procedure
err_code, & ! Error Indicator
clubb_fatal_error ! Constant
use constants_clubb, only: &
one_half, & ! Constant(s)
zero, &
fstderr, &
eps
use sponge_layer_damping, only: &
uv_sponge_damp_settings, &
uv_sponge_damp_profile, &
sponge_damp_xm ! Procedure(s)
implicit none
! External
intrinsic :: real
! Constant Parameters
real( kind = core_rknd ), dimension(gr%nz) :: &
dummy_nu ! Used to feed zero values into function calls
! Input Variables
real( kind = core_rknd ), intent(in) :: &
dt ! Model timestep [s]
real( kind = core_rknd ), dimension(gr%nz), intent(in) :: &
wm_zt, & ! w wind component on thermodynamic levels [m/s]
Km_zm, & ! Eddy diffusivity of winds on momentum levs. [m^2/s]
Kmh_zm, & ! Eddy diffusivity of themo on momentum levs. [m^s/s]
ug, & ! u (west-to-east) geostrophic wind comp. [m/s]
vg, & ! v (south-to-north) geostrophic wind comp. [m/s]
um_ref, & ! Reference u wind component for nudging [m/s]
vm_ref, & ! Reference v wind component for nudging [m/s]
wp2, & ! w'^2 (momentum levels) [m^2/s^2]
up2, & ! u'^2 (momentum levels) [m^2/s^2]
vp2, & ! v'^2 (momentum levels) [m^2/s^2]
um_forcing, & ! u forcing [m/s/s]
vm_forcing, & ! v forcing [m/s/s]
rho_ds_zm, & ! Dry, static density on momentum levels [kg/m^3]
invrs_rho_ds_zt ! Inv. dry, static density at thermo. levels [m^3/kg]
real( kind = core_rknd ), dimension(gr%nz,edsclr_dim), intent(in) :: &
edsclrm_forcing ! Eddy scalar large-scale forcing [{units vary}/s]
real( kind = core_rknd ), intent(in) :: &
fcor ! Coriolis parameter [s^-1]
logical, intent(in) :: &
l_implemented ! Flag for CLUBB being implemented in a larger model.
! Input/Output Variables
real( kind = core_rknd ), dimension(gr%nz), intent(inout) :: &
um, & ! Mean u (west-to-east) wind component [m/s]
vm, & ! Mean v (south-to-north) wind component [m/s]
upwp, & ! <u'w'> (momentum levels) [m^2/s^2]
vpwp ! <v'w'> (momentum levels) [m^2/s^2]
! Input/Output Variable for eddy-scalars
real( kind = core_rknd ), dimension(gr%nz,edsclr_dim), intent(inout) :: &
edsclrm ! Mean eddy scalar quantity [units vary]
! Output Variable for eddy-scalars
real( kind = core_rknd ), dimension(gr%nz,edsclr_dim), intent(inout) :: &
wpedsclrp ! w'edsclr' (momentum levels) [m/s {units vary}]
! Local Variables
real( kind = core_rknd ), dimension(gr%nz) :: &
um_tndcy, & ! u wind component tendency [m/s^2]
vm_tndcy ! v wind component tendency [m/s^2]
real( kind = core_rknd ), dimension(gr%nz) :: &
upwp_chnge, & ! Net change of u'w' due to clipping [m^2/s^2]
vpwp_chnge ! Net change of v'w' due to clipping [m^2/s^2]
real( kind = core_rknd ), dimension(3,gr%nz) :: &
lhs ! The implicit part of the tridiagonal matrix [units vary]
real( kind = core_rknd ), dimension(gr%nz,max(2,edsclr_dim)) :: &
rhs, &! The explicit part of the tridiagonal matrix [units vary]
solution ! The solution to the tridiagonal matrix [units vary]
real( kind = core_rknd ), dimension(gr%nz) :: &
wind_speed ! wind speed; sqrt(u^2 + v^2) [m/s]
real( kind = core_rknd ) :: &
u_star_sqd ! Surface friction velocity, u_star, squared [m/s]
logical :: &
l_imp_sfc_momentum_flux ! Flag for implicit momentum surface fluxes.
integer :: nrhs ! Number of right hand side terms
integer :: i ! Array index
logical :: l_first_clip_ts, l_last_clip_ts ! flags for clip_covar
!--------------------------- Begin Code ------------------------------------
dummy_nu = 0._core_rknd
if ( .not. l_predict_upwp_vpwp ) then
!----------------------------------------------------------------
! Prepare tridiagonal system for horizontal winds, um and vm
!----------------------------------------------------------------
! Compute Coriolis, geostrophic, and other prescribed wind forcings
! for um.
call compute_uv_tndcy( windm_edsclrm_um, fcor, vm, vg, & ! In
um_forcing, l_implemented, & ! In
um_tndcy ) ! Out
! Compute Coriolis, geostrophic, and other prescribed wind forcings
! for vm.
call compute_uv_tndcy( windm_edsclrm_vm, fcor, um, ug, & ! In
vm_forcing, l_implemented, & ! In
vm_tndcy ) ! Out
! Momentum surface fluxes, u'w'|_sfc and v'w'|_sfc, are applied through
! an implicit method, such that:
! x'w'|_sfc = - ( u_star(t)^2 / wind_speed(t) ) * xm(t+1).
l_imp_sfc_momentum_flux = .true.
! Compute wind speed (use threshold "eps" to prevent divide-by-zero
! error).
wind_speed = max( sqrt( um**2 + vm**2 ), eps )
! Compute u_star_sqd according to the definition of u_star.
u_star_sqd = sqrt( upwp(1)**2 + vpwp(1)**2 )
! Compute the explicit portion of the um equation.
! Build the right-hand side vector.
call windm_edsclrm_rhs( windm_edsclrm_um, dt, nu10_vert_res_dep, & ! In
Km_zm, um, um_tndcy, & ! In
rho_ds_zm, invrs_rho_ds_zt, & ! In
l_imp_sfc_momentum_flux, upwp(1), & ! In
rhs(:,windm_edsclrm_um) ) ! Out
! Compute the explicit portion of the vm equation.
! Build the right-hand side vector.
call windm_edsclrm_rhs( windm_edsclrm_vm, dt, nu10_vert_res_dep, & ! In
Km_zm, vm, vm_tndcy, & ! In
rho_ds_zm, invrs_rho_ds_zt, & ! In
l_imp_sfc_momentum_flux, vpwp(1), & ! In
rhs(:,windm_edsclrm_vm) ) ! Out
! Store momentum flux (explicit component)
! The surface flux, x'w'(1) = x'w'|_sfc, is set elsewhere in the model.
! upwp(1) = upwp_sfc
! vpwp(1) = vpwp_sfc
! Solve for x'w' at all intermediate model levels.
! A Crank-Nicholson timestep is used.
upwp(2:gr%nz-1) &
= -one_half &
* xpwp_fnc( Km_zm(2:gr%nz-1) + nu10_vert_res_dep(2:gr%nz-1), & ! in
um(2:gr%nz-1), um(3:gr%nz), & ! in
gr%invrs_dzm(2:gr%nz-1) )
vpwp(2:gr%nz-1) &
= -one_half &
* xpwp_fnc( Km_zm(2:gr%nz-1) + nu10_vert_res_dep(2:gr%nz-1), & ! in
vm(2:gr%nz-1), vm(3:gr%nz), & ! in
gr%invrs_dzm(2:gr%nz-1) )
! A zero-flux boundary condition at the top of the model, d(xm)/dz = 0,
! means that x'w' at the top model level is 0,
! since x'w' = - K_zm * d(xm)/dz.
upwp(gr%nz) = zero
vpwp(gr%nz) = zero
! Compute the implicit portion of the um and vm equations.
! Build the left-hand side matrix.
call windm_edsclrm_lhs( dt, nu10_vert_res_dep, wm_zt, Km_zm, & ! In
wind_speed, u_star_sqd, & ! In
rho_ds_zm, invrs_rho_ds_zt, & ! In
l_implemented, l_imp_sfc_momentum_flux, & ! In
lhs ) ! Out
! Decompose and back substitute for um and vm
nrhs = 2
call windm_edsclrm_solve( nrhs, iwindm_matrix_condt_num, & ! In
lhs, rhs, & ! In/out
solution ) ! Out
! Check for singular matrices and bad LAPACK arguments
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "Fatal error solving for um/vm"
return
endif
endif
!----------------------------------------------------------------
! Update zonal (west-to-east) component of mean wind, um
!----------------------------------------------------------------
um(1:gr%nz) = solution(1:gr%nz,windm_edsclrm_um)
!----------------------------------------------------------------
! Update meridional (south-to-north) component of mean wind, vm
!----------------------------------------------------------------
vm(1:gr%nz) = solution(1:gr%nz,windm_edsclrm_vm)
if ( l_stats_samp ) then
! Implicit contributions to um and vm
call windm_edsclrm_implicit_stats( windm_edsclrm_um, um ) ! in
call windm_edsclrm_implicit_stats( windm_edsclrm_vm, vm ) ! in
endif ! l_stats_samp
! The values of um(1) and vm(1) are located below the model surface and
! do not affect the rest of the model. The values of um(1) or vm(1) are
! simply set to the values of um(2) and vm(2), respectively, after the
! equation matrices has been solved. Even though um and vm would sharply
! decrease to a value of 0 at the surface, this is done to avoid
! confusion on plots of the vertical profiles of um and vm.
um(1) = um(2)
vm(1) = vm(2)
if ( uv_sponge_damp_settings%l_sponge_damping ) then
if ( l_stats_samp ) then
call stat_begin_update( ium_sdmp, um / dt, stats_zt )
call stat_begin_update( ivm_sdmp, vm / dt, stats_zt )
endif
um(1:gr%nz) = sponge_damp_xm( dt, gr%zt, um_ref(1:gr%nz), &
um(1:gr%nz), uv_sponge_damp_profile )
vm(1:gr%nz) = sponge_damp_xm( dt, gr%zt, vm_ref(1:gr%nz), &
vm(1:gr%nz), uv_sponge_damp_profile )
if ( l_stats_samp ) then
call stat_end_update( ium_sdmp, um / dt, stats_zt )
call stat_end_update( ivm_sdmp, vm / dt, stats_zt )
endif
endif ! uv_sponge_damp_settings%l_sponge_damping
! Second part of momentum (implicit component)
! Solve for x'w' at all intermediate model levels.
! A Crank-Nicholson timestep is used.
upwp(2:gr%nz-1) &
= upwp(2:gr%nz-1) &
- one_half * xpwp_fnc( Km_zm(2:gr%nz-1)+nu10_vert_res_dep(2:gr%nz-1), &
um(2:gr%nz-1), um(3:gr%nz), &
gr%invrs_dzm(2:gr%nz-1) )
vpwp(2:gr%nz-1) &
= vpwp(2:gr%nz-1) &
- one_half * xpwp_fnc( Km_zm(2:gr%nz-1)+nu10_vert_res_dep(2:gr%nz-1), &
vm(2:gr%nz-1), vm(3:gr%nz), &
gr%invrs_dzm(2:gr%nz-1) )
! Adjust um and vm if nudging is turned on.
if ( l_uv_nudge ) then
! Reflect nudging in budget
if ( l_stats_samp ) then
call stat_begin_update( ium_ndg, um / dt, stats_zt )
call stat_begin_update( ivm_ndg, vm / dt, stats_zt )
endif
um(1:gr%nz) &
= um(1:gr%nz) - ( ( um(1:gr%nz) - um_ref(1:gr%nz) ) * (dt/ts_nudge) )
vm(1:gr%nz) &
= vm(1:gr%nz) - ( ( vm(1:gr%nz) - vm_ref(1:gr%nz) ) * (dt/ts_nudge) )
if ( l_stats_samp ) then
call stat_end_update( ium_ndg, um / dt, stats_zt )
call stat_end_update( ivm_ndg, vm / dt, stats_zt )
endif
endif ! l_uv_nudge
if ( l_stats_samp ) then
call stat_update_var( ium_ref, um_ref, stats_zt )
call stat_update_var( ivm_ref, vm_ref, stats_zt )
endif
if ( l_tke_aniso ) then
! 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 u'^2, w'^2, and u'w' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for u'w'
! has to be done three times during each timestep (once after each
! variable has been updated).
! This is the third instance of u'w' clipping.
l_first_clip_ts = .false.
l_last_clip_ts = .true.
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)
! 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 v'^2, w'^2, and v'w' are each advanced in different
! subroutines from each other in advance_clubb_core, clipping for v'w'
! has to be done three times during each timestep (once after each
! variable has been updated).
! This is the third instance of v'w' clipping.
l_first_clip_ts = .false.
l_last_clip_ts = .true.
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, it is assumed that
! u'^2 == v'^2 == w'^2, and the variables `up2' and `vp2' do not
! interact with any other variables.
l_first_clip_ts = .false.
l_last_clip_ts = .true.
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)
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)
endif ! l_tke_aniso
endif ! .not. l_predict_upwp_vpwp
!----------------------------------------------------------------
! Prepare tridiagonal system for eddy-scalars
!----------------------------------------------------------------
if ( edsclr_dim > 0 ) then
! Eddy-scalar surface fluxes, x'w'|_sfc, are applied through an explicit
! method.
l_imp_sfc_momentum_flux = .false.
! Compute the explicit portion of eddy scalar equation.
! Build the right-hand side vector.
! Because of statistics, we have to use a DO rather than a FORALL here
! -dschanen 7 Oct 2008
!HPF$ INDEPENDENT
do i = 1, edsclr_dim
call windm_edsclrm_rhs( windm_edsclrm_scalar, dt, dummy_nu, & ! In
Kmh_zm, edsclrm(:,i), edsclrm_forcing, & ! In
rho_ds_zm, invrs_rho_ds_zt, & ! In
l_imp_sfc_momentum_flux, wpedsclrp(1,i), & ! In
rhs(:,i) ) ! Out
enddo
! Store momentum flux (explicit component)
! The surface flux, x'w'(1) = x'w'|_sfc, is set elsewhere in the model.
! wpedsclrp(1,1:edsclr_dim) = wpedsclrp_sfc(1:edsclr_dim)
! Solve for x'w' at all intermediate model levels.
! A Crank-Nicholson timestep is used.
! Here we use a forall and high performance fortran directive to try to
! parallelize this computation. Note that FORALL is more restrictive than DO.
!HPF$ INDEPENDENT, REDUCTION(wpedsclrp)
forall( i = 1:edsclr_dim )
wpedsclrp(2:gr%nz-1,i) &
= -one_half * xpwp_fnc( Kmh_zm(2:gr%nz-1), edsclrm(2:gr%nz-1,i), &
edsclrm(3:gr%nz,i), gr%invrs_dzm(2:gr%nz-1) )
end forall
! A zero-flux boundary condition at the top of the model, d(xm)/dz = 0,
! means that x'w' at the top model level is 0,
! since x'w' = - K_zm * d(xm)/dz.
wpedsclrp(gr%nz,1:edsclr_dim) = zero
! Compute the implicit portion of the xm (eddy-scalar) equations.
! Build the left-hand side matrix.
call windm_edsclrm_lhs( dt, dummy_nu, wm_zt, Kmh_zm, & ! In
wind_speed, u_star_sqd, & ! In
rho_ds_zm, invrs_rho_ds_zt, & ! In
l_implemented, l_imp_sfc_momentum_flux, & ! In
lhs ) ! Out
! Decompose and back substitute for all eddy-scalar variables
call windm_edsclrm_solve( edsclr_dim, 0, & ! in
lhs, rhs, & ! in/out
solution ) ! out
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "Fatal error solving for eddsclrm"
end if
end if
!----------------------------------------------------------------
! Update Eddy-diff. Passive Scalars
!----------------------------------------------------------------
edsclrm(1:gr%nz,1:edsclr_dim) = solution(1:gr%nz,1:edsclr_dim)
! The value of edsclrm(1) is located below the model surface and does not
! effect the rest of the model. The value of edsclrm(1) is simply set to
! the value of edsclrm(2) after the equation matrix has been solved.
forall( i=1:edsclr_dim )
edsclrm(1,i) = edsclrm(2,i)
end forall
! Second part of momentum (implicit component)
! Solve for x'w' at all intermediate model levels.
! A Crank-Nicholson timestep is used.
!HPF$ INDEPENDENT, REDUCTION(wpedsclrp)
forall( i = 1:edsclr_dim )
wpedsclrp(2:gr%nz-1,i) &
= wpedsclrp(2:gr%nz-1,i) &
- one_half * xpwp_fnc( Kmh_zm(2:gr%nz-1), edsclrm(2:gr%nz-1,i), &
edsclrm(3:gr%nz,i), gr%invrs_dzm(2:gr%nz-1) )
end forall
! Note that the w'edsclr' terms are not clipped, since we don't compute
! the variance of edsclr anywhere. -dschanen 7 Oct 2008
endif
if ( clubb_at_least_debug_level( 0 ) ) then
if ( err_code == clubb_fatal_error ) then
write(fstderr,*) "Error in advance_windm_edsclrm"
write(fstderr,*) "Intent(in)"
write(fstderr,*) "dt = ", dt
write(fstderr,*) "wm_zt = ", wm_zt
write(fstderr,*) "Km_zm = ", Km_zm
write(fstderr,*) "ug = ", ug
write(fstderr,*) "vg = ", vg
write(fstderr,*) "um_ref = ", um_ref
write(fstderr,*) "vm_ref = ", vm_ref
write(fstderr,*) "wp2 = ", wp2
write(fstderr,*) "up2 = ", up2
write(fstderr,*) "vp2 = ", vp2
write(fstderr,*) "um_forcing = ", um_forcing
write(fstderr,*) "vm_forcing = ", vm_forcing
do i = 1, edsclr_dim
write(fstderr,*) "edsclrm_forcing # = ", i, edsclrm_forcing
end do
write(fstderr,*) "fcor = ", fcor
write(fstderr,*) "l_implemented = ", l_implemented
write(fstderr,*) "Intent(inout)"
write(fstderr,*) "um = ", um
write(fstderr,*) "vm = ", vm
do i = 1, edsclr_dim
write(fstderr,*) "edsclrm # ", i, "=", edsclrm(:,i)
end do
write(fstderr,*) "upwp = ", upwp
write(fstderr,*) "vpwp = ", vpwp
write(fstderr,*) "wpedsclrp = ", wpedsclrp
return
end if
end if
return
end subroutine advance_windm_edsclrm
!=============================================================================
subroutine windm_edsclrm_solve( nrhs, ixm_matrix_condt_num, &
lhs, rhs, solution )
! Note: In the "Description" section of this subroutine, the variable
! "invrs_dzm" will be written as simply "dzm", and the variable
! "invrs_dzt" will be written as simply "dzt". This is being done as
! as device to save space and to make some parts of the description
! more readable. This change does not pertain to the actual code.
! Description:
! Solves the horizontal wind or eddy-scalar time-tendency equation, and
! diagnoses the turbulent flux. A Crank-Nicholson time-stepping algorithm
! is used in solving the turbulent advection term and in diagnosing the
! turbulent flux.
!
! The rate of change of an eddy-scalar quantity, xm, is:
!
! d(xm)/dt = - w * d(xm)/dz - (1/rho_ds) * d( rho_ds * x'w' )/dz
! + xm_forcings.
!
!
! The Turbulent Advection Term
! ----------------------------
!
! The above equation contains a turbulent advection term:
!
! - (1/rho_ds) * d( rho_ds * x'w' )/dz;
!
! where the momentum flux, x'w', is closed using a down gradient approach:
!
! x'w' = - K_zm * d(xm)/dz.
!
! The turbulent advection term becomes:
!
! + (1/rho_ds) * d [ rho_ds * K_zm * d(xm)/dz ] / dz;
!
! which is the same as a standard eddy-diffusion term (if "rho_ds * K_zm" in
! the term above is substituted for "K_zm" in a standard eddy-diffusion
! term, and if the standard eddy-diffusion term is multiplied by
! "1/rho_ds"). Thus, the turbulent advection term is treated and solved in
! the same way that a standard eddy-diffusion term would be solved. The
! term is discretized as follows:
!
! The values of xm are found on the thermodynamic levels, while the values
! of K_zm are found on the momentum levels. Additionally, the values of
! rho_ds_zm are found on the momentum levels, and the values of
! invrs_rho_ds_zt are found on the thermodynamic levels. The
! derivatives (d/dz) of xm are taken over the intermediate momentum levels.
! At the intermediate momentum levels, d(xm)/dz is multiplied by K_zm and by
! rho_ds_zm. Then, the derivative of the whole mathematical expression is
! taken over the central thermodynamic level, where it is multiplied by
! invrs_rho_ds_zt, which yields the desired result.
!
! ---xm(kp1)----------------------------------------------------- t(k+1)
!
! ===========d(xm)/dz===K_zm(k)=====rho_ds_zm(k)================= m(k)
!
! ---xm(k)---invrs_rho_ds_zt---d[rho_ds_zm*K_zm*d(xm)/dz]/dz----- t(k)
!
! ===========d(xm)/dz===K_zm(km1)===rho_ds_zm(km1)=============== m(k-1)
!
! ---xm(km1)----------------------------------------------------- t(k-1)
!
! The vertical indices t(k+1), m(k), t(k), m(k-1), and t(k-1) correspond
! with altitudes zt(k+1), zm(k), zt(k), zm(k-1), and zt(k-1), respectively.
! The letter "t" is used for thermodynamic levels and the letter "m" is used
! for momentum levels.
!
! dzt(k) = 1 / ( zm(k) - zm(k-1) )
! dzm(k) = 1 / ( zt(k+1) - zt(k) )
! dzm(k-1) = 1 / ( zt(k) - zt(k-1) )
!
! The vertically discretized form of the turbulent advection term (treated
! as an eddy diffusion term) is written out as:
!
! + invrs_rho_ds_zt(k)
! * dzt(k)
! * [ rho_ds_zm(k) * K_zm(k) * dzm(k) * ( xm(k+1) - xm(k) )
! - rho_ds_zm(k-1) * K_zm(k-1) * dzm(k-1) * ( xm(k) - xm(k-1) ) ].
!
! For this equation, a Crank-Nicholson (semi-implicit) diffusion scheme is
! used to solve the (1/rho_ds) * d [ rho_ds * K_zm * d(xm)/dz ] / dz
! eddy-diffusion term. The discretized implicit portion of the term is
! written out as:
!
! + (1/2) * invrs_rho_ds_zt(k)
! * dzt(k)
! * [ rho_ds_zm(k) * K_zm(k)
! * dzm(k) * ( xm(k+1,<t+1>) - xm(k,<t+1>) )
! - rho_ds_zm(k-1) * K_zm(k-1)
! * dzm(k-1) * ( xm(k,<t+1>) - xm(k-1,<t+1>) ) ].
!
! Note: When the implicit term is brought over to the left-hand side,
! the sign is reversed and the leading "+" in front of the term
! is changed to a "-".
!
! The discretized explicit portion of the term is written out as:
!
! + (1/2) * invrs_rho_ds_zt(k)
! * dzt(k)
! * [ rho_ds_zm(k) * K_zm(k)
! * dzm(k) * ( xm(k+1,<t>) - xm(k,<t>) )
! - rho_ds_zm(k-1) * K_zm(k-1)
! * dzm(k-1) * ( xm(k,<t>) - xm(k-1,<t>) ) ].
!
! Timestep index (t) stands for the index of the current timestep, while
! timestep index (t+1) stands for the index of the next timestep, which is
! being advanced to in solving the d(xm)/dt equation.
!
!
! Boundary Conditions:
!
! An eddy-scalar quantity is not allowed to flux out the upper boundary.
! Thus, a zero-flux boundary condition is used for the upper boundary in the
! eddy-diffusion equation.
!
! The lower boundary condition is much more complicated. It is neither a
! zero-flux nor a fixed-point boundary condition. Rather, it is a
! fixed-flux boundary condition. This term is a turbulent advection term,
! but with the eddy-scalars, the only value of x'w' relevant in solving the
! d(xm)/dt equation is the value of x'w' at the surface (the first momentum
! level), which is written as x'w'|_sfc.
!
! 1) x'w' surface flux; generalized explicit form
!
! The x'w' surface flux is applied to the d(xm)/dt equation through the
! turbulent advection term, which is:
!
! - (1/rho_ds) * d( rho_ds * x'w' )/dz.
!
! At most vertical levels, a substitution can be made for x'w', such
! that:
!
! x'w' = - K_zm * d(xm)/dz.
!
! However, the same substitution cannot be made at the surface (momentum
! level 1), as x'w'|_sfc is a surface flux that is explicitly computed
! elsewhere in the model code.
!
! The lower boundary condition, which in this case needs to be applied to
! the d(xm)/dt equation at level 2, is discretized as follows:
!
! --xm(3)------------------------------------------------------- t(3)
!
! ========[x'w'(2) = -K_zm(2)*d(xm)/dz]===rho_ds_zm(2)========== m(2)
!
! --xm(2)---invrs_rho_ds_zt(2)---d[rho_ds_zm*K_zm*d(xm)/dz]/dz-- t(2)
!
! ========[x'w'|_sfc]=====================rho_ds_zm(1)========== m(1) sfc
!
! --xm(1)-------(below surface; not applicable)----------------- t(1)
!
! where "sfc" is the level of the model surface or lower boundary.
!
! The vertically discretized form of the turbulent advection term
! (treated as an eddy diffusion term), with the explicit surface flux,
! x'w'|_sfc, in place, is written out as:
!
! - invrs_rho_ds_zt(2)
! * dzt(2) * [ rho_ds_zm(2) * x'w'(2) - rho_ds_zm(1) * x'w'|_sfc ];
!
! which can be re-written as:
!
! + invrs_rho_ds_zt(2)
! * dzt(2)
! * [ rho_ds_zm(2) * K_zm(2) * dzm(2) * ( xm(3) - xm(2) )
! + rho_ds_zm(1) * x'w'|_sfc ];
!
! which can be re-written again as:
!
! + invrs_rho_ds_zt(2)
! * dzt(2)
! * rho_ds_zm(2) * K_zm(2) * dzm(2) * ( xm(3) - xm(2) )
! + invrs_rho_ds_zt(2)
! * dzt(2)
! * rho_ds_zm(1) * x'w'|_sfc.
!
! For this equation, a Crank-Nicholson (semi-implicit) diffusion scheme
! is used to solve the (1/rho_ds) * d [ rho_ds * K_zm * d(xm)/dz ] / dz
! eddy-diffusion term. The discretized implicit portion of the term is
! written out as:
!
! + (1/2) * invrs_rho_ds_zt(2)
! * dzt(2)
! * [ rho_ds_zm(2) * K_zm(2)
! * dzm(2) * ( xm(3,<t+1>) - xm(2,<t+1>) ) ].
!
! Note: When the implicit term is brought over to the left-hand side,
! the sign is reversed and the leading "+" in front of the term
! is changed to a "-".
!
! The discretized explicit portion of the term is written out as:
!
! + (1/2) * invrs_rho_ds_zt(2)
! * dzt(2)
! * [ rho_ds_zm(2) * K_zm(2)
! * dzm(2) * ( xm(3,<t>) - xm(2,<t>) ) ]
! + invrs_rho_ds_zt(2)
! * dzt(2)
! * rho_ds_zm(1) * x'w'|_sfc.
!
! Note: The x'w'|_sfc portion of the term written above has been pulled
! away from the rest of the explicit form written above because
! the (1/2) factor due to Crank-Nicholson time_stepping does not
! apply to it, as there isn't an implicit portion for x'w'|_sfc.
!
! Timestep index (t) stands for the index of the current timestep, while
! timestep index (t+1) stands for the index of the next timestep, which
! is being advanced to in solving the d(xm)/dt equation.
!
! 2) x'w' surface flux; implicit form for momentum fluxes u'w' and v'w'
!
! The x'w' surface flux is applied to the d(xm)/dt equation through the
! turbulent advection term, which is:
!
! - (1/rho_ds) * d( rho_ds * x'w' )/dz.
!
! At most vertical levels, a substitution can be made for x'w', such
! that:
!
! x'w' = - K_zm * d(xm)/dz.
!
! However, the same substitution cannot be made at the surface (momentum
! level 1), as x'w'|_sfc is a surface momentum flux that is found by the
! following equation:
!
! x'w'|_sfc = - [ u_star^2 / sqrt( um^2 + vm^2 ) ] * xm;
!
! where x'w'|_sfc and xm are either u'w'|_sfc and um, respectively, or
! v'w'|_sfc and vm, respectively (um and vm are located at the first
! thermodynamic level above the surface, which is thermodynamic level 2),
! sqrt( um^2 + vm^2 ) is the wind speed (also at thermodynamic level 2),
! and u_star is defined as:
!
! u_star = ( u'w'|_sfc^2 + v'w'|_sfc^2 )^(1/4);
!
! and thus u_star^2 is defined as:
!
! u_star^2 = sqrt( u'w'|_sfc^2 + v'w'|_sfc^2 ).
!
! The value of u_star is either set to a constant value or computed
! (through function diag_ustar) based on the surface wind speed, the
! height above surface of the surface wind speed (as compared to the
! roughness height), and the buoyancy flux at the surface. Either way,
! u_star is computed elsewhere in the model, and the values of u'w'|_sfc
! and v'w'|_sfc are based on it and computed along with it. The values
! of u'w'|_sfc and v'w'|_sfc are then passed into advance_clubb_core,
! and are eventually passed into advance_windm_edsclrm. In subroutine
! advance_windm_edsclrm, the value of u_star_sqd is then recomputed
! based on u'w'|_sfc and v'w'|_sfc. The value of sqrt( u_star_sqd ) is
! consistent with the value of the original computation of u_star.
!
! The equation listed above is substituted for x'w'|_sfc. The lower
! boundary condition, which in this case needs to be applied to the
! d(xm)/dt equation at level 2, is discretized as follows:
!
! --xm(3)------------------------------------------------------- t(3)
!
! ===[x'w'(2) = -K_zm(2)*d(xm)/dz]=================rho_ds_zm(2)= m(2)
!
! --xm(2)---invrs_rho_ds_zt(2)---d[rho_ds_zm*K_zm*d(xm)/dz]/dz-- t(2)
!
! ===[x'w'|_sfc = -[u_star^2/sqrt(um^2+vm^2)]*xm]==rho_ds_zm(1)= m(1) sfc
!
! --xm(1)-------(below surface; not applicable)----------------- t(1)
!
! where "sfc" is the level of the model surface or lower boundary.
!
! The vertically discretized form of the turbulent advection term
! (treated as an eddy diffusion term), with the implicit surface momentum
! flux in place, is written out as:
!
! - invrs_rho_ds_zt(2)
! * dzt(2) * [ rho_ds_zm(2) * x'w'(2) - rho_ds_zm(1) * x'w'|_sfc ];
!
! which can be re-written as:
!
! - invrs_rho_ds_zt(2)
! * dzt(2)
! * [ rho_ds_zm(2)
! * { - K_zm(2) * dzm(2) * ( xm(3) - xm(2) ) }
! - rho_ds_zm(1)
! * { - [ u_star^2 / sqrt( um(2)^2 + vm(2)^2 ) ] * xm(2) } ];
!
! which can be re-written as:
!
! + invrs_rho_ds_zt(2)
! * dzt(2)
! * rho_ds_zm(2) * K_zm(2) * dzm(2) * ( xm(3) - xm(2) )
! - invrs_rho_ds_zt(2)
! * dzt(2)
! * rho_ds_zm(1) * [ u_star^2 / sqrt( um(2)^2 + vm(2)^2 ) ] * xm(2).
!
! For this equation, a Crank-Nicholson (semi-implicit) diffusion scheme
! is used to solve the (1/rho_ds) * d [ rho_ds * K_zm * d(xm)/dz ] / dz
! eddy-diffusion term. The discretized implicit portion of the term is
! written out as:
!
! + (1/2) * invrs_rho_ds_zt(2)
! * dzt(2)
! * [ rho_ds_zm(2) * K_zm(2)
! * dzm(2) * ( xm(3,<t+1>) - xm(2,<t+1>) ) ]
! - invrs_rho_ds_zt(2)
! * dzt(2)
! * rho_ds_zm(1)
! * [u_star^2/sqrt( um(2,<t>)^2 + vm(2,<t>)^2 )] * xm(2,<t+1>).
!
! Note: When the implicit term is brought over to the left-hand side,
! the signs are reversed and the leading "+" in front of the first
! part of the term is changed to a "-", while the leading "-" in
! front of the second part of the term is changed to a "+".
!
! Note: The x'w'|_sfc portion of the term written above has been pulled
! away from the rest of the implicit form written above because
! the (1/2) factor due to Crank-Nicholson time_stepping does not
! apply to it. The x'w'|_sfc portion of the term is treated
! completely implicitly in order to enhance numerical stability.
!
! The discretized explicit portion of the term is written out as:
!
! + (1/2) * invrs_rho_ds_zt(2)
! * dzt(2)
! * [ rho_ds_zm(2) * K_zm(2)
! * dzm(2) * ( xm(3,<t>) - xm(2,<t>) ) ].
!
! Timestep index (t) stands for the index of the current timestep, while
! timestep index (t+1) stands for the index of the next timestep, which
! is being advanced to in solving the d(xm)/dt equation.
!
!
! The lower boundary condition for the implicit and explicit portions of the
! turbulent advection term, without the x'w'|_sfc portion of the term, can
! easily be invoked by using the zero-flux boundary conditions found in the
! generalized diffusion function (function diffusion_zt_lhs), which is used
! for many other equations in this model. Either the generalized explicit
! surface flux needs to be added onto the explicit term after the diffusion
! function has been called from subroutine windm_edsclrm_rhs, or the
! implicit momentum surface flux needs to be added onto the implicit term
! after the diffusion function has been called from subroutine
! windm_edsclrm_lhs. However, all other equations in this model that use
! zero-flux diffusion have level 1 as the level to which the lower boundary
! condition needs to be applied. Thus, an adjuster will have to be used at
! level 2 to call diffusion_zt_lhs with level 1 as the input level (the last
! variable being passed in during the function call). However, the other
! variables passed in (rho_ds_zm*K_zm, gr%dzt, and gr%dzm variables) will
! have to be passed in as solving for level 2.
!
! The value of xm(1) is located below the model surface and does not effect
! the rest of the model. Since xm can be either a horizontal wind component
! or a generic eddy scalar quantity, the value of xm(1) is simply set to the
! value of xm(2) after the equation matrix has been solved.
!
!
! Conservation Properties:
!
! When a fixed-flux lower boundary condition is used (combined with a
! zero-flux upper boundary condition), this technique of discretizing the
! turbulent advection term (treated as an eddy-diffusion term) leads to
! conservative differencing. When the implicit momentum surface flux is
! either zero or not used, the column totals for each column in the
! left-hand side matrix (for the turbulent advection term) should be equal
! to 0. Otherwise, the column total for the second column will be equal to
! rho_ds_zm(1) * x'w'|_sfc<t+1>. When the generalized explicit surface
! flux is either zero or not used, the column total for the right-hand side
! vector (for the turbulent advection term) should be equal to 0.
! Otherwise, the column total for the right-hand side vector (for the
! turbulent advection term) will be equal to rho_ds_zm(1) * x'w'|_sfc<t>.
! This ensures that the total amount of quantity xm over the entire vertical
! domain is only changed by the surface flux (neglecting any forcing terms).
! The total amount of change is equal to rho_ds_zm(1) * x'w'|_sfc.
!
! To see that this conservation law is satisfied by the left-hand side
! matrix, compute the turbulent advection (treated as eddy diffusion) of xm,
! neglecting any implicit momentum surface flux, multiply by rho_ds_zt, and
! integrate vertically. In discretized matrix notation (where "i" stands
! for the matrix column and "j" stands for the matrix row):
!
! 0 = Sum_j Sum_i
! (rho_ds_zt)_i ( 1/dzt )_i
! ( 0.5_core_rknd * (1/rho_ds_zt) * dzt * (rho_ds_zm*K_zm*dzm) )_ij (xm<t+1>)_j.
!
! The left-hand side matrix,
! ( 0.5_core_rknd * (1/rho_ds_zt) * dzt * (rho_ds_zm*K_zm*dzm) )_ij, is partially
! written below. The sum over i in the above equation removes (1/rho_ds_zt)
! and dzt everywhere from the matrix below. The sum over j leaves the
! column totals that are desired, which are 0.
!
! Left-hand side matrix contributions from the turbulent advection term
! (treated as an eddy-diffusion term using a Crank-Nicholson timestep);
! first five vertical levels:
!
! ------------------------------------------------------------------------------->
!k=1 | 0 0 0 0
! |
!k=2 | 0 +0.5* -0.5* 0
! | (1/rho_ds_zt(k))* (1/rho_ds_zt(k))*
! | dzt(k)* dzt(k)*
! | rho_ds_zm(k)* rho_ds_zm(k)*
! | K_zm(k)*dzm(k) K_zm(k)*dzm(k)
! |
!k=3 | 0 -0.5* +0.5* -0.5*
! | (1/rho_ds_zt(k))* (1/rho_ds_zt(k))* (1/rho_ds_zt(k))*
! | dzt(k)* dzt(k)* dzt(k)*
! | rho_ds_zm(k-1)* [ rho_ds_zm(k)* rho_ds_zm(k)*
! | K_zm(k-1)*dzm(k-1) K_zm(k)*dzm(k) K_zm(k)*dzm(k)
! | +rho_ds_zm(k-1)*
! | K_zm(k-1)*dzm(k-1) ]
! |
!k=4 | 0 0 -0.5* +0.5*
! | (1/rho_ds_zt(k))* (1/rho_ds_zt(k))*
! | dzt(k)* dzt(k)*
! | rho_ds_zm(k-1)* [ rho_ds_zm(k)*
! | K_zm(k-1)*dzm(k-1) K_zm(k)*dzm(k)
! | +rho_ds_zm(k-1)*
! | K_zm(k-1)*dzm(k-1) ]
! |
!k=5 | 0 0 0 -0.5*
! | (1/rho_ds_zt(k))*