forked from NOAA-GFDL/SIS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIS_tracer_advect.F90
1835 lines (1644 loc) · 81.7 KB
/
SIS_tracer_advect.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
module SIS_tracer_advect
!***********************************************************************
!* GNU General Public License *
!* This file is a part of SIS2. *
!* *
!* SIS2 is free software; you can redistribute it and/or modify it and *
!* are expected to follow the terms of the GNU General Public License *
!* as published by the Free Software Foundation; either version 2 of *
!* the License, or (at your option) any later version. *
!* *
!* SIS2 is distributed in the hope that it will be useful, but WITHOUT *
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
!* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public *
!* License for more details. *
!* *
!* For the full text of the GNU General Public License, *
!* write to: Free Software Foundation, Inc., *
!* 675 Mass Ave, Cambridge, MA 02139, USA. *
!* or see: http://www.gnu.org/licenses/gpl.html *
!***********************************************************************
!********+*********+*********+*********+*********+*********+*********+**
!* *
!* By Robert Hallberg, 1996 - 2012, adapted for SIS2 in 2014-2016. *
!* *
!* This program contains the subroutines that advect tracers *
!* horizontally (i.e. along layers). This code was modified from the *
!* corresponding MOM6 / GOLD code to work with the snow and ice *
!* tracers of SIS2. *
!* *
!* advect_SIS_tracers advects tracer concentrations using the *
!* modified flux advection scheme from Easter (Mon. Wea. Rev., 1993) *
!* with tracer distributions that are piecewise constant, *
!* piecewise linear (given by the monotonic scheme proposed by *
!* Lin et al. (Mon. Wea. Rev., 1994)), or the montonic piecewise *
!* parabolic method, as described in Carpenter et al. (MWR, 1990). *
!* This detects the mass of ice or snow in a grid cell and thickness *
!* category at the previous instance when the tracer concentration *
!* was changed is consistent with the mass fluxes and the new masses. *
!* *
!* A small fragment of the grid is shown below: *
!* *
!* j+1 x ^ x ^ x At x: q *
!* j+1 > o > o > At ^: v, vh *
!* j x ^ x ^ x At >: u, uh *
!* j > o > o > At o: tr, h *
!* j-1 x ^ x ^ x *
!* i-1 i i+1 At x & ^: *
!* i i+1 At > & o: *
!* *
!* The boundaries always run through q grid points (x). *
!* *
!********+*********+*********+*********+*********+*********+*********+**
use MOM_cpu_clock, only : cpu_clock_id, cpu_clock_begin, cpu_clock_end
use MOM_cpu_clock, only : CLOCK_MODULE, CLOCK_ROUTINE
use SIS_diag_mediator, only : post_SIS_data, query_SIS_averaging_enabled, SIS_diag_ctrl
use SIS_diag_mediator, only : register_SIS_diag_field, safe_alloc_ptr, time_type
use MOM_domains, only : pass_var, pass_vector, sum_across_PEs, max_across_PEs
use MOM_error_handler, only : SIS_error=>MOM_error, FATAL, WARNING, SIS_mesg=>MOM_mesg
use MOM_file_parser, only : get_param, log_version, param_file_type
use SIS_hor_grid, only : SIS_hor_grid_type
use ice_grid, only : ice_grid_type
use SIS_tracer_registry, only : SIS_tracer_registry_type, SIS_tracer_type, SIS_tracer_chksum
implicit none ; private
#include <SIS2_memory.h>
public advect_SIS_tracers, advect_tracers_thicker, advect_scalar
public SIS_tracer_advect_init, SIS_tracer_advect_end
type, public :: SIS_tracer_advect_CS ; private
real :: dt ! The baroclinic dynamics time step, in s.
type(SIS_diag_ctrl), pointer :: diag ! A structure that is used to regulate the
! timing of diagnostic output.
logical :: debug ! If true, write verbose checksums for debugging purposes.
logical :: use_upwind2d ! If true, use the non-split upwind scheme that was
! was used in older versions of SIS.
logical :: usePPM ! If true, use PPM tracer advection instead of PLM.
logical :: usePCM ! If true, use PCM tracer advection instead of PLM.
end type SIS_tracer_advect_CS
logical :: first_call = .true.
integer :: id_clock_advect, id_clock_pass, id_clock_sync
contains
subroutine advect_SIS_tracers(h_prev, h_end, uhtr, vhtr, dt, G, IG, CS, Reg, snow_tr ) ! (, OBC)
type(SIS_hor_grid_type), intent(inout) :: G
type(ice_grid_type), intent(inout) :: IG
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(in) :: h_prev, h_end
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)), intent(in) :: uhtr
real, dimension(SZI_(G),SZJB_(G),SZCAT_(IG)), intent(in) :: vhtr
real, intent(in) :: dt
type(SIS_tracer_advect_CS), pointer :: CS
type(SIS_tracer_registry_type), pointer :: Reg
logical, intent(in) :: snow_tr
! Arguments: h_prev - Category thickness times fractional coverage before advection, in m or kg m-2.
! (in) h_end - Layer thickness times fractional coverage after advection, in m or kg m-2.
! (in) uhtr - Accumulated volume or mass fluxes through zonal faces,
! in m3 s-1 or kg s-1.
! (in) vhtr - Accumulated volume or mass fluxes through meridional faces,
! in m3 s-1 or kg s-1.
!! (in) OBC - This open boundary condition type specifies whether, where,
!! and what open boundary conditions are used.
! (in) dt - Time increment in s.
! (in) G - The ocean's grid structure.
! (in) IG - The sea-ice-specific grid structure.
! (in) CS - The control structure returned by a previous call to
! SIS_tracer_advect_init.
! (in) Reg - A pointer to the tracer registry.
! (in) snow_tr - If true, advect the snow tracers, otherwise advect the
! ice tracers.
integer ntr
if (.not. associated(CS)) call SIS_error(FATAL, "SIS_tracer_advect: "// &
"SIS_tracer_advect_init must be called before advect_tracer.")
if (.not. associated(Reg)) call SIS_error(FATAL, "SIS_tracer_advect: "// &
"register_tracer must be called before advect_tracer.")
ntr = Reg%ntr
if (ntr==0) return
call cpu_clock_begin(id_clock_advect)
if (snow_tr) then
if (CS%use_upwind2d) then
call advect_upwind_2d(Reg%Tr_snow, h_prev, h_end, uhtr, vhtr, ntr, dt, G, IG)
else
call advect_tracer(Reg%Tr_snow, h_prev, h_end, uhtr, vhtr, ntr, dt, G, IG, CS)
endif
else
if (CS%use_upwind2d) then
call advect_upwind_2d(Reg%Tr_ice, h_prev, h_end, uhtr, vhtr, ntr, dt, G, IG)
else
call advect_tracer(Reg%Tr_ice, h_prev, h_end, uhtr, vhtr, ntr, dt, G, IG, CS)
endif
endif
call cpu_clock_end(id_clock_advect)
end subroutine advect_SIS_tracers
subroutine advect_tracer(Tr, h_prev, h_end, uhtr, vhtr, ntr, dt, G, IG, CS) ! (, OBC)
type(SIS_tracer_type), dimension(ntr), intent(inout) :: Tr
type(SIS_hor_grid_type), intent(inout) :: G
type(ice_grid_type), intent(inout) :: IG
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(in) :: h_prev, h_end
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)), intent(in) :: uhtr
real, dimension(SZI_(G),SZJB_(G),SZCAT_(IG)), intent(in) :: vhtr
real, intent(in) :: dt
integer, intent(in) :: ntr
type(SIS_tracer_advect_CS), pointer :: CS
!! type(ocean_OBC_type), pointer :: OBC
! This subroutine time steps the tracer concentration.
! A monotonic, conservative, weakly diffusive scheme is used.
! Arguments: h_prev - Category thickness times fractional coverage before advection, in m or kg m-2.
! (in) h_end - Layer thickness times fractional coverage after advection, in m or kg m-2.
! (in) uhtr - Accumulated volume or mass fluxes through zonal faces,
! in m3 s-1 or kg s-1.
! (in) vhtr - Accumulated volume or mass fluxes through meridional faces,
! in m3 s-1 or kg s-1.
!! (in) OBC - This open boundary condition type specifies whether, where,
!! and what open boundary conditions are used.
! (in) dt - Time increment in s.
! (in) G - The ocean's grid structure.
! (in) IG - The sea-ice-specific grid structure.
! (in) CS - The control structure returned by a previous call to
! SIS_tracer_advect_init.
! (in) Reg - A pointer to the tracer registry.
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)) :: &
hprev ! The cell volume at the end of the previous tracer
! change, in m3.
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)) :: &
uhr ! The remaining zonal thickness flux, in m3.
real, dimension(SZI_(G),SZJB_(G),SZCAT_(IG)) :: &
vhr ! The remaining meridional thickness fluxes, in m3.
real :: uh_neglect(SZIB_(G),SZJ_(G)) ! uh_neglect and vh_neglect are the
real :: vh_neglect(SZI_(G),SZJB_(G)) ! magnitude of remaining transports that
! can be simply discarded, in m3 or kg.
real :: landvolfill ! An arbitrary? nonzero cell volume, m3.
real :: Idt ! 1/dt in s-1.
real :: h_neglect
logical :: domore_u(SZJ_(G),SZCAT_(IG)) ! domore__ indicate whether there is more
logical :: domore_v(SZJB_(G),SZCAT_(IG)) ! advection to be done in the corresponding
! row or column.
logical :: x_first ! If true, advect in the x-direction first.
integer :: max_iter ! The maximum number of iterations in
! each layer.
integer :: domore_k(SZCAT_(IG))
integer :: stensil ! The stensil of the advection scheme.
integer :: nsten_halo ! The number of stensils that fit in the halos.
integer :: i, j, k, l, m, is, ie, js, je, isd, ied, jsd, jed
integer :: ncat, nL_max, itt, do_any
integer :: isv, iev, jsv, jev ! The valid range of the indices.
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; ncat = IG%CatIce
isd = G%isd ; ied = G%ied ; jsd = G%jsd ; jed = G%jed
landvolfill = 1.0e-20 ! This is arbitrary, but must be positive.
stensil = 2 ! The scheme's stensil; 2 for PLM.
if (.not. associated(CS)) call SIS_error(FATAL, "SIS_tracer_advect: "// &
"SIS_tracer_advect_init must be called before advect_tracer.")
if (ntr==0) return
x_first = (MOD(G%first_direction,2) == 0)
Idt = 1.0/dt
nL_max = 0
do m=1,ntr ; nL_max = max(Tr(m)%nL,nL_max) ; enddo
max_iter = 3
if (CS%dt > 0.0) max_iter = 2*INT(CEILING(dt/CS%dt)) + 1
! This initializes the halos of uhr and vhr because pass_vector might do
! calculations on them, even though they are never used.
uhr(:,:,:) = 0.0 ; vhr(:,:,:) = 0.0
hprev(:,:,:) = landvolfill
h_neglect = IG%H_subroundoff
! Initialize domore_u and domore_v to .false.; they will be reevaluated later.
domore_u(:,:) = .false. ; domore_v(:,:) = .false.
!$OMP parallel default(none) shared(ncat,is,ie,js,je,domore_k,uhr,vhr,uhtr,vhtr,dt, &
!$OMP hprev,G,h_prev,h_end,isd,ied,jsd,jed,uh_neglect, &
!$OMP h_neglect,vh_neglect,ntr,Tr,domore_u,domore_v)
!$OMP do
do k=1,ncat
domore_k(k)=1
! Put the remaining (total) thickness fluxes into uhr and vhr.
do j=js,je ; do I=is-1,ie ; uhr(I,j,k) = dt*uhtr(I,j,k) ; enddo ; enddo
do J=js-1,je ; do i=is,ie ; vhr(i,J,k) = dt*vhtr(i,J,k) ; enddo ; enddo
! Find the previous total mass (or volume) of ice, but in the case that this
! category is now dramatically thinner than it was previously, add a tiny
! bit of extra mass to avoid nonsensical tracer concentrations. This will
! lead rarely to a very slight non-conservation of tracers, but not mass.
do j=js,je; do i=is,ie
hprev(i,j,k) = G%areaT(i,j) * (h_prev(i,j,k) + &
max(0.0, 1.0e-13*h_prev(i,j,k) - h_end(i,j,k)))
if (h_end(i,j,k) - h_prev(i,j,k) + ((uhr(I,j,k) - uhr(I-1,j,k)) + &
(vhr(i,J,k) - vhr(i,J-1,k))) * G%IareaT(i,j) > &
1e-10*(h_end(i,j,k) + h_prev(i,j,k))) then
!$OMP critical
call SIS_error(WARNING, "Apparently inconsistent h_prev, h_end, uhr and vhr in advect_tracer.")
!$OMP end critical
endif
enddo ; enddo
enddo
!$OMP end do nowait
!$OMP do
do j=jsd,jed ; do I=isd,ied-1
uh_neglect(I,j) = h_neglect*MIN(G%areaT(i,j),G%areaT(i+1,j))
enddo ; enddo
!$OMP end do nowait
!$OMP do
do J=jsd,jed-1 ; do i=isd,ied
vh_neglect(i,J) = h_neglect*MIN(G%areaT(i,j),G%areaT(i,j+1))
enddo ; enddo
!$OMP end do nowait
!$OMP do
do m=1,ntr
if (associated(Tr(m)%ad2d_x)) then
do j=jsd,jed ; do i=isd,ied ; Tr(m)%ad2d_x(I,j) = 0.0 ; enddo ; enddo
endif
if (associated(Tr(m)%ad2d_y)) then
do J=jsd,jed ; do i=isd,ied ; Tr(m)%ad2d_y(i,J) = 0.0 ; enddo ; enddo
endif
if (associated(Tr(m)%ad3d_x)) then
do k=1,ncat ; do j=jsd,jed ; do i=isd,ied
Tr(m)%ad3d_x(I,j,k) = 0.0
enddo ; enddo ; enddo
endif
if (associated(Tr(m)%ad3d_y)) then
do k=1,ncat ; do J=jsd,jed ; do i=isd,ied
Tr(m)%ad3d_y(i,J,k) = 0.0
enddo ; enddo ; enddo
endif
if (associated(Tr(m)%ad4d_x)) then
do l=1,Tr(m)%nL ; do k=1,ncat ; do j=jsd,jed ; do i=isd,ied
Tr(m)%ad4d_x(I,j,k,l) = 0.0
enddo ; enddo ; enddo ; enddo
endif
if (associated(Tr(m)%ad4d_y)) then
do l=1,Tr(m)%nL ; do k=1,ncat ; do J=jsd,jed ; do i=isd,ied
Tr(m)%ad4d_y(i,J,k,l) = 0.0
enddo ; enddo ; enddo ; enddo
endif
enddo
!$OMP end parallel
isv = is ; iev = ie ; jsv = js ; jev = je
do itt=1,max_iter
if (isv > is-stensil) then
call cpu_clock_begin(id_clock_pass)
call pass_vector(uhr, vhr, G%Domain)
do m=1,ntr ; do l=1,Tr(m)%nL
call pass_var(Tr(m)%t(:,:,:,l), G%Domain, complete=.false.)
enddo ; enddo
call pass_var(hprev, G%Domain)
call cpu_clock_end(id_clock_pass)
nsten_halo = min(is-isd,ied-ie,js-jsd,jed-je)/stensil
isv = is-nsten_halo*stensil ; jsv = js-nsten_halo*stensil
iev = ie+nsten_halo*stensil ; jev = je+nsten_halo*stensil
! Reevaluate domore_u & domore_v unless the valid range is the same size as
! before. Also, do this if there is Strang splitting.
if ((nsten_halo > 1) .or. (itt==1)) then
!$OMP parallel do default(none) shared(ncat,domore_k,isv,iev,jsv,jev,stensil, &
!$OMP domore_u,uhr,vhr,domore_v)
do k=1,ncat ; if (domore_k(k) > 0) then
do j=jsv,jev ; if (.not.domore_u(j,k)) then
do i=isv+stensil-1,iev-stensil; if (uhr(I,j,k) /= 0.0) then
domore_u(j,k) = .true. ; exit
endif ; enddo ! i-loop
endif ; enddo
do J=jsv+stensil-1,jev-stensil ; if (.not.domore_v(J,k)) then
do i=isv+stensil,iev-stensil; if (vhr(i,J,k) /= 0.0) then
domore_v(J,k) = .true. ; exit
endif ; enddo ! i-loop
endif ; enddo
! At this point, domore_k is global. Change it so that it indicates
! whether any work is needed on a layer on this processor.
domore_k(k) = 0
do j=jsv,jev ; if (domore_u(j,k)) domore_k(k) = 1 ; enddo
do J=jsv+stensil-1,jev-stensil ; if (domore_v(J,k)) domore_k(k) = 1 ; enddo
endif ; enddo ! k-loop
endif
endif
! Set the range of valid points after this iteration.
isv = isv + stensil ; iev = iev - stensil
jsv = jsv + stensil ; jev = jev - stensil
!$OMP parallel do default(none) shared(ncat,domore_k,x_first,Tr,hprev,uhr,uh_neglect, &
!$OMP domore_u,ntr,nL_max,Idt,isv,iev,jsv,jev, &
!$OMP stensil,G,CS,vhr,vh_neglect,domore_v,IG)
do k=1,ncat ; if (domore_k(k) > 0) then
! To ensure positive definiteness of the thickness at each iteration, the
! mass fluxes out of each layer are checked each step, and limited to keep
! the thicknesses positive. This means that several iteration may be required
! for all the transport to happen. The sum over domore_k keeps the processors
! synchronized. This may not be very efficient, but it should be reliable.
if (x_first) then
! First, advect zonally.
call advect_x(Tr, hprev, uhr, uh_neglect, domore_u, ntr, nL_max, Idt, &
isv, iev, jsv-stensil, jev+stensil, k, G, IG, &
CS%usePPM, CS%usePCM) !(, OBC)
! Next, advect meridionally.
call advect_y(Tr, hprev, vhr, vh_neglect, domore_v, ntr, nL_max, Idt, &
isv, iev, jsv, jev, k, G, IG, CS%usePPM, CS%usePCM) !(, OBC)
domore_k(k) = 0
do j=jsv-stensil,jev+stensil ; if (domore_u(j,k)) domore_k(k) = 1 ; enddo
do J=jsv-1,jev ; if (domore_v(J,k)) domore_k(k) = 1 ; enddo
else
! First, advect meridionally.
call advect_y(Tr, hprev, vhr, vh_neglect, domore_v, ntr, nL_max, Idt, &
isv-stensil, iev+stensil, jsv, jev, k, G, IG, &
CS%usePPM, CS%usePCM) !(, OBC)
! Next, advect zonally.
call advect_x(Tr, hprev, uhr, uh_neglect, domore_u, ntr, nL_max, Idt, &
isv, iev, jsv, jev, k, G, IG, CS%usePPM, CS%usePCM) !(, OBC)
domore_k(k) = 0
do j=jsv,jev ; if (domore_u(j,k)) domore_k(k) = 1 ; enddo
do J=jsv-1,jev ; if (domore_v(J,k)) domore_k(k) = 1 ; enddo
endif
endif ; enddo ! End of k-loop
! If the advection just isn't finishing after max_iter, move on.
if (itt >= max_iter) exit
! Exit if there are no layers that need more iterations.
if (isv > is-stensil) then
do_any = 0
call cpu_clock_begin(id_clock_sync)
call sum_across_PEs(domore_k(:), ncat)
call cpu_clock_end(id_clock_sync)
do k=1,ncat ; do_any = do_any + domore_k(k) ; enddo
if (do_any == 0) exit
endif
enddo ! Iterations loop
end subroutine advect_tracer
subroutine advect_scalar(scalar, h_prev, h_end, uhtr, vhtr, dt, G, IG, CS) ! (, OBC)
type(SIS_hor_grid_type), intent(inout) :: G
type(ice_grid_type), intent(inout) :: IG
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(inout) :: scalar !< Scalar field to be advected
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(in) :: h_prev, h_end
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)), intent(in) :: uhtr
real, dimension(SZI_(G),SZJB_(G),SZCAT_(IG)), intent(in) :: vhtr
real, intent(in) :: dt
type(SIS_tracer_advect_CS), pointer :: CS
! Arguments: scalar - The scalar tracer field to be advected, in arbitrary units.
! (in) h_prev - Category thickness times fractional coverage before advection, in m or kg m-2.
! (in) h_end - Layer thickness times fractional coverage after advection, in m or kg m-2.
! (in) uhtr - Accumulated volume or mass fluxes through zonal faces,
! in m3 s-1 or kg s-1.
! (in) vhtr - Accumulated volume or mass fluxes through meridional faces,
! in m3 s-1 or kg s-1.
! (in) dt - Time increment in s.
! (in) G - The ocean's grid structure.
! (in) IG - The sea-ice-specific grid structure.
! (in) CS - The control structure returned by a previous call to
! SIS_tracer_advect_init.
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)) :: &
hprev ! The cell volume at the end of the previous tracer
! change, in m3.
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)) :: &
uhr ! The remaining zonal thickness flux, in m3.
real, dimension(SZI_(G),SZJB_(G),SZCAT_(IG)) :: &
vhr ! The remaining meridional thickness fluxes, in m3.
real :: uh_neglect(SZIB_(G),SZJ_(G)) ! uh_neglect and vh_neglect are the
real :: vh_neglect(SZI_(G),SZJB_(G)) ! magnitude of remaining transports that
! can be simply discarded, in m3 or kg.
real :: landvolfill ! An arbitrary? nonzero cell volume, m3.
real :: Idt ! 1/dt in s-1.
real :: h_neglect
logical :: domore_u(SZJ_(G),SZCAT_(IG)) ! domore__ indicate whether there is more
logical :: domore_v(SZJB_(G),SZCAT_(IG)) ! advection to be done in the corresponding
! row or column.
logical :: x_first ! If true, advect in the x-direction first.
integer :: max_iter ! The maximum number of iterations in
! each layer.
real, dimension(SZIB_(G),SZJ_(G)) :: flux_U2d_x ! x-direction tracer fluxes, in conc * kg
real, dimension(SZI_(G),SZJB_(G)) :: flux_U2d_y ! y-direction tracer fluxes, in conc * kg
real :: tr_up ! Upwind tracer concentrations, in conc.
real :: vol_end, Ivol_end ! Cell volume at the end of a step and its inverse.
integer :: domore_k(SZCAT_(IG))
integer :: stensil ! The stensil of the advection scheme.
integer :: nsten_halo ! The number of stensils that fit in the halos.
integer :: i, j, k, l, m, is, ie, js, je, isd, ied, jsd, jed
integer :: ncat, nL_max, itt, do_any
integer :: isv, iev, jsv, jev ! The valid range of the indices.
is = G%isc ; ie = G%iec ; js = G%jsc ; je = G%jec ; ncat = IG%CatIce
isd = G%isd ; ied = G%ied ; jsd = G%jsd ; jed = G%jed
landvolfill = 1.0e-20 ! This is arbitrary, but must be positive.
stensil = 2 ! The scheme's stensil; 2 for PLM.
if (.not. associated(CS)) call SIS_error(FATAL, "advect_scalar: "// &
"SIS_tracer_advect_init must be called before advect_scalar.")
if (CS%use_upwind2d) then
!$OMP parallel do default(none) shared(is,ie,js,je,ncat,uhtr,vhtr,dt,G,h_end, &
!$OMP h_prev,scalar) &
!$OMP private(tr_up,flux_U2d_x,flux_U2d_y,vol_end,Ivol_end)
do k=1,ncat
do j=js,je ; do I=is-1,ie
if (uhtr(I,j,k) >= 0.0) then ; tr_up = scalar(i,j,k)
else ; tr_up = scalar(i+1,j,k) ; endif
flux_U2d_x(I,j) = (dt*uhtr(I,j,k)) * tr_up
enddo ; enddo
do J=js-1,je ; do i=is,ie
if (vhtr(i,J,k) >= 0.0) then ; tr_up = scalar(i,j,k)
else ; tr_up = scalar(i,j+1,k) ; endif
flux_U2d_y(i,J) = (dt*vhtr(i,J,k)) * tr_up
enddo ; enddo
do j=js,je ; do i=is,ie
vol_end = (G%areaT(i,j) * h_end(i,j,k))
Ivol_end = 0.0 ; if (vol_end > 0.0) Ivol_end = 1.0 / vol_end
scalar(i,j,k) = ( (G%areaT(i,j)*h_prev(i,j,k))*scalar(i,j,k) - &
((flux_U2d_x(I,j) - flux_U2d_x(I-1,j)) + &
(flux_U2d_y(i,J) - flux_U2d_y(i,J-1))) ) * Ivol_end
enddo ; enddo
enddo
else
x_first = (MOD(G%first_direction,2) == 0)
Idt = 1.0/dt
max_iter = 3
if (CS%dt > 0.0) max_iter = 2*INT(CEILING(dt/CS%dt)) + 1
! This initializes the halos of uhr and vhr because pass_vector might do
! calculations on them, even though they are never used.
uhr(:,:,:) = 0.0 ; vhr(:,:,:) = 0.0
hprev(:,:,:) = landvolfill
h_neglect = IG%H_subroundoff
! Initialize domore_u and domore_v. Curiously, the value used for
! initialization does not matter to the solutions, because if .false.
! they are reevaluated after the first halo update (and always on the first
! iteration, and if .true. a number of fluxes are exactly 0 anyway. Of the
! two choices, .false. is more efficient in that it avoids extra
! calculations of 0 fluxes.
domore_u(:,:) = .false. ; domore_v(:,:) = .false.
!$OMP parallel default(none) shared(is,ie,js,je,ncat,domore_k,uhr,vhr,uhtr,vhtr,dt,G, &
!$OMP hprev,h_prev,h_end,isd,ied,jsd,jed,uh_neglect, &
!$OMP h_neglect,vh_neglect,domore_u,domore_v)
!$OMP do
do k=1,ncat
domore_k(k)=1
! Put the remaining (total) thickness fluxes into uhr and vhr.
do j=js,je ; do I=is-1,ie ; uhr(I,j,k) = dt*uhtr(I,j,k) ; enddo ; enddo
do J=js-1,je ; do i=is,ie ; vhr(i,J,k) = dt*vhtr(i,J,k) ; enddo ; enddo
! Find the previous total mass (or volume) of ice, but in the case that this
! category is now dramatically thinner than it was previously, add a tiny
! bit of extra mass to avoid nonsensical tracer concentrations. This will
! lead rarely to a very slight non-conservation of tracers, but not mass.
do i=is,ie ; do j=js,je
hprev(i,j,k) = G%areaT(i,j) * (h_prev(i,j,k) + &
max(0.0, 1.0e-13*h_prev(i,j,k) - h_end(i,j,k)))
if (h_end(i,j,k) - h_prev(i,j,k) + ((uhr(I,j,k) - uhr(I-1,j,k)) + &
(vhr(i,J,k) - vhr(i,J-1,k))) * G%IareaT(i,j) > &
1e-10*(h_end(i,j,k) + h_prev(i,j,k))) then
!$OMP critical
call SIS_error(WARNING, "Apparently inconsistent h_prev, h_end, uhr and vhr in advect_tracer.")
!$OMP end critical
endif
enddo ; enddo
enddo
!$OMP end do nowait
!$OMP do
do j=jsd,jed ; do I=isd,ied-1
uh_neglect(I,j) = h_neglect*MIN(G%areaT(i,j),G%areaT(i+1,j))
enddo ; enddo
!$OMP end do nowait
!$OMP do
do J=jsd,jed-1 ; do i=isd,ied
vh_neglect(i,J) = h_neglect*MIN(G%areaT(i,j),G%areaT(i,j+1))
enddo ; enddo
!$OMP end parallel
isv = is ; iev = ie ; jsv = js ; jev = je
do itt=1,max_iter
if (isv > is-stensil) then
call cpu_clock_begin(id_clock_pass)
call pass_vector(uhr, vhr, G%Domain)
call pass_var(scalar, G%Domain, complete=.false.)
call pass_var(hprev, G%Domain)
call cpu_clock_end(id_clock_pass)
nsten_halo = min(is-isd,ied-ie,js-jsd,jed-je)/stensil
isv = is-nsten_halo*stensil ; jsv = js-nsten_halo*stensil
iev = ie+nsten_halo*stensil ; jev = je+nsten_halo*stensil
! Reevaluate domore_u & domore_v unless the valid range is the same size as
! before. Also, do this if there is Strang splitting.
if ((nsten_halo > 1) .or. (itt==1)) then
!$OMP parallel do default(none) shared(isv,iev,jsv,jev,ncat,domore_k,domore_u,domore_v, &
!$OMP stensil,uhr,vhr)
do k=1,ncat ; if (domore_k(k) > 0) then
do j=jsv,jev ; if (.not.domore_u(j,k)) then
do i=isv+stensil-1,iev-stensil; if (uhr(I,j,k) /= 0.0) then
domore_u(j,k) = .true. ; exit
endif ; enddo ! i-loop
endif ; enddo
do J=jsv+stensil-1,jev-stensil ; if (.not.domore_v(J,k)) then
do i=isv+stensil,iev-stensil; if (vhr(i,J,k) /= 0.0) then
domore_v(J,k) = .true. ; exit
endif ; enddo ! i-loop
endif ; enddo
! At this point, domore_k is global. Change it so that it indicates
! whether any work is needed on a layer on this processor.
domore_k(k) = 0
do j=jsv,jev ; if (domore_u(j,k)) domore_k(k) = 1 ; enddo
do J=jsv+stensil-1,jev-stensil ; if (domore_v(J,k)) domore_k(k) = 1 ; enddo
endif ; enddo ! k-loop
endif
endif
! Set the range of valid points after this iteration.
isv = isv + stensil ; iev = iev - stensil
jsv = jsv + stensil ; jev = jev - stensil
!$OMP parallel do default(none) shared(ncat,isv,iev,jsv,jev,x_first,domore_k,scalar, &
!$OMP hprev,uhr,uh_neglect,domore_u,Idt,stensil,G, &
!$OMP CS,vhr,vh_neglect,domore_v,IG)
do k=1,ncat ; if (domore_k(k) > 0) then
! To ensure positive definiteness of the thickness at each iteration, the
! mass fluxes out of each layer are checked each step, and limited to keep
! the thicknesses positive. This means that several iteration may be required
! for all the transport to happen. The sum over domore_k keeps the processors
! synchronized. This may not be very efficient, but it should be reliable.
if (x_first) then
! First, advect zonally.
call advect_scalar_x(scalar, hprev, uhr, uh_neglect, domore_u, Idt, &
isv, iev, jsv-stensil, jev+stensil, k, G, IG, CS%usePPM, CS%usePCM) !(, OBC)
! Next, advect meridionally.
call advect_scalar_y(scalar, hprev, vhr, vh_neglect, domore_v, Idt, &
isv, iev, jsv, jev, k, G, IG, CS%usePPM, CS%usePCM) !(, OBC)
domore_k(k) = 0
do j=jsv-stensil,jev+stensil ; if (domore_u(j,k)) domore_k(k) = 1 ; enddo
do J=jsv-1,jev ; if (domore_v(J,k)) domore_k(k) = 1 ; enddo
else
! First, advect meridionally.
call advect_scalar_y(scalar, hprev, vhr, vh_neglect, domore_v, Idt, &
isv-stensil, iev+stensil, jsv, jev, k, G, IG, CS%usePPM, CS%usePCM) !(, OBC)
! Next, advect zonally.
call advect_scalar_x(scalar, hprev, uhr, uh_neglect, domore_u, Idt, &
isv, iev, jsv, jev, k, G, IG, CS%usePPM, CS%usePCM) !(, OBC)
domore_k(k) = 0
do j=jsv,jev ; if (domore_u(j,k)) domore_k(k) = 1 ; enddo
do J=jsv-1,jev ; if (domore_v(J,k)) domore_k(k) = 1 ; enddo
endif
endif ; enddo ! End of k-loop
! If the advection just isn't finishing after max_iter, move on.
if (itt >= max_iter) exit
! Exit if there are no layers that need more iterations.
if (isv > is-stensil) then
do_any = 0
call cpu_clock_begin(id_clock_sync)
call sum_across_PEs(domore_k(:), ncat)
call cpu_clock_end(id_clock_sync)
do k=1,ncat ; do_any = do_any + domore_k(k) ; enddo
if (do_any == 0) exit
endif
enddo ! Iterations loop
endif
end subroutine advect_scalar
subroutine advect_scalar_x(scalar, hprev, uhr, uh_neglect, domore_u, Idt, &
is, ie, js, je, k, G, IG, usePPM, usePCM) ! (, OBC)
type(SIS_hor_grid_type), intent(inout) :: G
type(ice_grid_type), intent(inout) :: IG
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(inout) :: scalar
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(inout) :: hprev
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)), intent(inout) :: uhr
real, dimension(SZIB_(G),SZJ_(G)), intent(inout) :: uh_neglect
!! type(ocean_OBC_type), pointer :: OBC
logical, dimension(SZJ_(G),SZCAT_(IG)), intent(inout) :: domore_u
real, intent(in) :: Idt
integer, intent(in) :: is, ie, js, je, k
logical, intent(in) :: usePPM, usePCM
! This subroutine does 1-d flux-form advection in the zonal direction using
! a monotonic piecewise linear scheme.
real, dimension(SZI_(G)) :: &
slope_x ! The concentration slope per grid point in units of
! concentration (nondim.).
real, dimension(SZIB_(G)) :: &
Tr_x ! The tracer concentration averaged over the water flux
! across a zonal boundary in conc.
real, dimension(SZIB_(G),SZJ_(G)) :: &
mass_mask ! A multiplicative mask at velocity points that is 1 if
! both neighboring cells have any mass, and 0 otherwise.
real :: maxslope ! The maximum concentration slope per grid point consistent
! with monotonicity, in conc. (nondim.).
real :: hup, hlos ! hup is the upwind volume, hlos is the part of that volume
! that might be lost due to advection out the other side of
! the grid box, both in m3 or kg.
real, dimension(SZIB_(G)) :: &
uhh, & ! The zonal flux that occurs during the current iteration, in m3 or kg.
CFL ! A nondimensional work variable.
real, dimension(SZI_(G)) :: &
hlst, Ihnew, & ! Work variables with units of m3 or kg and m-3 or kg-1.
haddE, haddW ! Tiny amounts of thickness that should be added to the
! tracer update with concentrations that match the average
! over the fluxes through the faces to the nominal east
! and west of the present cell, in m3 or kg.
real :: hnew ! The projected thickness, in m3 or kg.
real :: h_add ! A tiny thickness to add to keep the new tracer calculation
! well defined in the limit of vanishing layers in m3 or kg.
real :: I_htot ! The inverse of the sum of thickness within or passing or
! out of a cell, in m3 or kg.
real :: h_neglect ! A thickness that is so small it is usually lost
! in roundoff and can be neglected, in m.
logical :: do_i(SZI_(G)) ! If true, work on given points.
logical :: do_any_i
integer :: i, j
! real :: aR, aL, dMx, dMn, Tp, Tc, Tm, dA, mA, a6
logical :: usePLMslope
usePLMslope = .not.(usePCM .or. usePPM)
h_neglect = IG%H_subroundoff
do I=is-1,ie ; CFL(I) = 0.0 ; enddo
if (usePCM) then ; do i=is-1,ie+1 ; slope_x(i) = 0.0 ; enddo ; endif
do j=js,je ; if (domore_u(j,k)) then
domore_u(j,k) = .false.
if (usePPM .or. usePLMslope) then ; do I=is-2,ie+1
mass_mask(I,j) = 0.0
if (G%mask2dCu(I,j) * hprev(i,j,k)*hprev(i+1,j,k) > 0.0) mass_mask(I,j) = 1.0
enddo ; endif
! Calculate the i-direction profiles (slopes) of each tracer that is being advected.
if (usePLMslope) then
call kernel_PLM_slope_x(G, is-1, ie+1, j, scalar(:,:,k), mass_mask, slope_x(:))
endif ! usePLMslope
call kernel_uhh_CFL_x(G, is-1, ie, j, hprev(:,:,k), uhr(:,:,k), uhh, CFL, &
domore_u(j,k), h_neglect)
if (usePPM) then
call kernel_PPMH3_Tr_x(G, is-1, ie, j, &
scalar(:,:,k), mass_mask, uhh, CFL, Tr_x(:))
else ! PLM
call kernel_PLM_Tr_x(G, is-1, ie, j, &
scalar(:,:,k), uhh, CFL, slope_x(:), Tr_x(:))
endif ! usePPM
! Calculate new tracer concentration in each cell after accounting for the i-direction fluxes.
! call kernel_uhr_x(G, is, ie, j, uh_neglect, uhh, uhr(:,:,k), hprev(:,:,k), hlst, Ihnew, do_i)
do I=is-1,ie
uhr(I,j,k) = uhr(I,j,k) - uhh(I)
if (abs(uhr(I,j,k)) < uh_neglect(I,j)) uhr(I,j,k) = 0.0
enddo
do i=is,ie
if ((uhh(I) /= 0.0) .or. (uhh(I-1) /= 0.0)) then
do_i(i) = .true.
hlst(i) = max(hprev(i,j,k), 0.0) ! This max is here just for safety.
hnew = hprev(i,j,k) - (uhh(I) - uhh(I-1))
haddW(i) = 0.0 ; haddE(i) = 0.0
if (hnew <= 0.0) then
hnew = 0.0 ; do_i(i) = .false.
elseif (hnew < h_neglect*G%areaT(i,j)) then
! Add a bit of thickness with tracer concentrations that are
! proportional to the mass associated with fluxes and the previous
! mass in the cell.
h_add = h_neglect*G%areaT(i,j) - hnew
I_htot = 1.0 / (hlst(i) + (abs(uhh(I)) + abs(uhh(I-1))))
hlst(i) = hlst(i) + h_add*(hlst(i)*I_htot)
haddW(i) = h_add * (abs(uhh(I-1))*I_htot)
haddE(i) = h_add * (abs(uhh(I))*I_htot)
Ihnew(i) = 1.0 / (h_neglect*G%areaT(i,j))
else
Ihnew(i) = 1.0 / hnew
endif
! Store hnew as hprev for the next iteration.
hprev(i,j,k) = hnew
else ! Nothing changes in this cell, so skip it.
do_i(i) = .false.
endif
enddo
do i=is,ie ; if ((do_i(i)) .and. (Ihnew(i) > 0.0)) then
scalar(i,j,k) = (scalar(i,j,k) * hlst(i) - &
((uhh(I)-haddE(i))*Tr_x(I) - &
(uhh(I-1)+haddW(i))*Tr_x(I-1))) * Ihnew(i)
endif ; enddo
! call kernel_tracer_div_x(G, is, ie, j, do_i, hlst, Ihnew, flux_x(:), scalar(:,:,k))
endif ; enddo ! End of j-loop.
end subroutine advect_scalar_x
subroutine advect_x(Tr, hprev, uhr, uh_neglect, domore_u, ntr, nL_max, Idt, &
is, ie, js, je, k, G, IG, usePPM, usePCM) ! (, OBC)
type(SIS_hor_grid_type), intent(inout) :: G
type(ice_grid_type), intent(inout) :: IG
type(SIS_tracer_type), dimension(ntr), intent(inout) :: Tr
real, dimension(SZI_(G),SZJ_(G),SZCAT_(IG)), intent(inout) :: hprev
real, dimension(SZIB_(G),SZJ_(G),SZCAT_(IG)), intent(inout) :: uhr
real, dimension(SZIB_(G),SZJ_(G)), intent(inout) :: uh_neglect
!! type(ocean_OBC_type), pointer :: OBC
logical, dimension(SZJ_(G),SZCAT_(IG)), intent(inout) :: domore_u
real, intent(in) :: Idt
integer, intent(in) :: ntr, nL_max, is, ie, js, je,k
logical, intent(in) :: usePPM, usePCM
! This subroutine does 1-d flux-form advection in the zonal direction using
! a monotonic piecewise linear scheme.
real, dimension(SZI_(G),nL_max,ntr) :: &
slope_x ! The concentration slope per grid point in units of
! concentration (nondim.).
real, dimension(SZIB_(G),nL_max,ntr) :: &
Tr_x ! The tracer concentration averaged over the water flux
! across a zonal boundary in conc.
real, dimension(SZIB_(G),SZJ_(G)) :: &
mass_mask ! A multiplicative mask at velocity points that is 1 if
! both neighboring cells have any mass, and 0 otherwise.
real :: maxslope ! The maximum concentration slope per grid point consistent
! with monotonicity, in conc. (nondim.).
real :: hup, hlos ! hup is the upwind volume, hlos is the part of that volume
! that might be lost due to advection out the other side of
! the grid box, both in m3 or kg.
real, dimension(SZIB_(G)) :: &
uhh, & ! The zonal flux that occurs during the current iteration, in m3 or kg.
CFL ! A nondimensional work variable.
real, dimension(SZI_(G)) :: &
hlst, Ihnew, & ! Work variables with units of m3 or kg and m-3 or kg-1.
haddE, haddW ! Tiny amounts of thickness that should be added to the
! tracer update with concentrations that match the average
! over the fluxes through the faces to the nominal east
! and west of the present cell, in m3 or kg.
real :: hnew ! The projected thickness, in m3 or kg.
real :: h_add ! A tiny thickness to add to keep the new tracer calculation
! well defined in the limit of vanishing layers in m3 or kg.
real :: I_htot ! The inverse of the sum of thickness within or passing or
! out of a cell, in m3 or kg.
real :: h_neglect ! A thickness that is so small it is usually lost
! in roundoff and can be neglected, in m.
logical :: do_i(SZI_(G)) ! If true, work on given points.
logical :: do_any_i
integer :: i, j, l, m
! real :: aR, aL, dMx, dMn, Tp, Tc, Tm, dA, mA, a6
logical :: usePLMslope
usePLMslope = .not.(usePCM .or. usePPM)
h_neglect = IG%H_subroundoff
do I=is-1,ie ; CFL(I) = 0.0 ; enddo
if (usePCM) then ; do m=1,ntr ; do l=1,Tr(m)%nL ; do i=is-1,ie+1
slope_x(i,l,m) = 0.0
enddo ; enddo ; enddo ; endif
do j=js,je ; if (domore_u(j,k)) then
domore_u(j,k) = .false.
if (usePPM .or. usePLMslope) then ; do I=is-2,ie+1
mass_mask(I,j) = 0.0
if (G%mask2dCu(I,j)*hprev(i,j,k)*hprev(i+1,j,k) > 0.0) mass_mask(I,j) = 1.0
enddo ; endif
! Calculate the i-direction profiles (slopes) of each tracer that is being advected.
if (usePLMslope) then
do m=1,ntr ; do l=1,Tr(m)%nL
call kernel_PLM_slope_x(G, is-1, ie+1, j, Tr(m)%t(:,:,k,l), mass_mask, slope_x(:,l,m))
enddo ; enddo
endif ! usePLMslope
call kernel_uhh_CFL_x(G, is-1, ie, j, hprev(:,:,k), uhr(:,:,k), uhh, CFL, &
domore_u(j,k), h_neglect)
if (usePPM) then
do m=1,ntr ; do l=1,Tr(m)%nL
call kernel_PPMH3_Tr_x(G, is-1, ie, j, &
Tr(m)%t(:,:,k,l), mass_mask, uhh, CFL, Tr_x(:,l,m))
enddo ; enddo
else ! PLM
do m=1,ntr ; do l=1,Tr(m)%nL
call kernel_PLM_Tr_x(G, is-1, ie, j, &
Tr(m)%t(:,:,k,l), uhh, CFL, slope_x(:,l,m), Tr_x(:,l,m))
enddo ; enddo
endif ! usePPM
! Calculate new tracer concentration in each cell after accounting for the i-direction fluxes.
! call kernel_uhr_x(G, is, ie, j, uh_neglect, uhh, uhr(:,:,k), hprev(:,:,k), hlst, Ihnew, do_i)
do I=is-1,ie
uhr(I,j,k) = uhr(I,j,k) - uhh(I)
if (abs(uhr(I,j,k)) < uh_neglect(I,j)) uhr(I,j,k) = 0.0
enddo
do i=is,ie
if ((uhh(I) /= 0.0) .or. (uhh(I-1) /= 0.0)) then
do_i(i) = .true.
hlst(i) = max(hprev(i,j,k), 0.0) ! This max is here just for safety.
hnew = hprev(i,j,k) - (uhh(I) - uhh(I-1))
haddW(i) = 0.0 ; haddE(i) = 0.0
if (hnew <= 0.0) then
hnew = 0.0 ; do_i(i) = .false.
elseif (hnew < h_neglect*G%areaT(i,j)) then
! Add a bit of thickness with tracer concentrations that are
! proportional to the mass associated with fluxes and the previous
! mass in the cell.
h_add = h_neglect*G%areaT(i,j) - hnew
I_htot = 1.0 / (hlst(i) + (abs(uhh(I)) + abs(uhh(I-1))))
hlst(i) = hlst(i) + h_add*(hlst(i)*I_htot)
haddW(i) = h_add * (abs(uhh(I-1))*I_htot)
haddE(i) = h_add * (abs(uhh(I))*I_htot)
Ihnew(i) = 1.0 / (h_neglect*G%areaT(i,j))
else
Ihnew(i) = 1.0 / hnew
endif
! Store hnew as hprev for the next iteration.
hprev(i,j,k) = hnew
else ! Nothing changes in this cell, so skip it.
do_i(i) = .false.
endif
enddo
do m=1,ntr ; do l=1,Tr(m)%nL
! call kernel_tracer_div_x(G, is, ie, j, do_i, hlst, Ihnew, flux_x(:,l,m), Tr(m)%t(:,:,k,l))
do i=is,ie ; if ((do_i(i)) .and. (Ihnew(i) > 0.0)) then
Tr(m)%t(i,j,k,l) = (Tr(m)%t(i,j,k,l) * hlst(i) - &
((uhh(I)-haddE(i))*Tr_x(I,l,m) - &
(uhh(I-1)+haddW(i))*Tr_x(I-1,l,m))) * Ihnew(i)
endif ; enddo
! Diagnostics
if (associated(Tr(m)%ad4d_x)) then ; do i=is,ie ; if (do_i(i)) then
Tr(m)%ad4d_x(I,j,k,l) = Tr(m)%ad4d_x(I,j,k,l) + uhh(I)*Tr_x(I,l,m)*Idt
endif ; enddo ; endif
if (associated(Tr(m)%ad3d_x)) then ; do i=is,ie ; if (do_i(i)) then
Tr(m)%ad3d_x(I,j,k) = Tr(m)%ad3d_x(I,j,k) + uhh(I)*Tr_x(I,l,m)*Idt
endif ; enddo ; endif
if (associated(Tr(m)%ad2d_x)) then ; do i=is,ie ; if (do_i(i)) then
Tr(m)%ad2d_x(I,j) = Tr(m)%ad2d_x(I,j) + uhh(I)*Tr_x(I,l,m)*Idt
endif ; enddo ; endif
enddo ; enddo
endif ; enddo ! End of j-loop.
end subroutine advect_x
!> Calculate the mass flux and CFL such that the flux of tracer uses as much
!! the minimum of the remaining mass flux (uhr) and the half the mass
!! in the cell plus whatever part of its half of the mass flux that
!! the flux through the other side does not require.
subroutine kernel_uhh_CFL_x(G, is, ie, j, hprev, uhr, uhh, CFL, domore_u, h_neglect)
type(SIS_hor_grid_type), intent(in) :: G
integer, intent(in) :: is, ie, j
real, dimension(SZI_(G),SZJ_(G)), intent(in) :: hprev
real, dimension(SZIB_(G),SZJ_(G)), intent(in) :: uhr
real, dimension(SZIB_(G)), intent(inout) :: uhh, CFL
logical, intent(inout) :: domore_u
real, intent(in) :: h_neglect
! Local
integer :: i
real :: hup, hlos
do I=is,ie
if (uhr(I,j) == 0.0) then
uhh(I) = 0.0
CFL(I) = 0.0
elseif (uhr(I,j) < 0.0) then
hup = hprev(i+1,j)
hlos = MAX(0.0,uhr(I+1,j))
if (((hup + uhr(I,j) - hlos) < 0.0) .and. &
((0.5*hup + uhr(I,j)) < 0.0)) then
uhh(I) = MIN(-0.5*hup,-hup+hlos,0.0)
domore_u = .true.
else
uhh(I) = uhr(I,j)
endif
CFL(I) = - uhh(I)/(hprev(i+1,j)+h_neglect) ! CFL is positive
else
hup = hprev(i,j)
hlos = MAX(0.0,-uhr(I-1,j))
if (((hup - uhr(I,j) - hlos) < 0.0) .and. &
((0.5*hup - uhr(I,j)) < 0.0)) then
uhh(I) = MAX(0.5*hup,hup-hlos,0.0)
domore_u = .true.
else
uhh(I) = uhr(I,j)
endif
CFL(I) = uhh(I)/(hprev(i,j)+h_neglect) ! CFL is positive
endif
enddo
end subroutine kernel_uhh_CFL_x
subroutine kernel_PLM_slope_x(G, is, ie, j, scalar, uMask, slope_x)
type(SIS_hor_grid_type), intent(in) :: G
integer, intent(in) :: is, ie, j
real, dimension(SZI_(G),SZJ_(G)), intent(in) :: scalar
real, dimension(SZIB_(G),SZJ_(G)), intent(in) :: uMask
real, dimension(SZI_(G)), intent(inout) :: slope_x
! Local
integer :: i
real :: Tp, Tc, Tm, dMx, dMn
do i = is, ie
Tp = scalar(i+1,j) ; Tc = scalar(i,j) ; Tm = scalar(i-1,j)
dMx = max( Tp, Tc, Tm ) - Tc
dMn= Tc - min( Tp, Tc, Tm )
slope_x(i) = uMask(I,j)*uMask(I-1,j) * &
sign( min(0.5*abs(Tp-Tm), 2.0*dMx, 2.0*dMn), Tp-Tm )
enddo
end subroutine kernel_PLM_slope_x
subroutine kernel_PLM_Tr_x(G, is, ie, j, scalar, uhh, CFL, slope_x, Tr_x)
type(SIS_hor_grid_type), intent(in) :: G
integer, intent(in) :: is, ie, j
real, dimension(SZI_(G),SZJ_(G)), intent(in) :: scalar
real, dimension(SZIB_(G)), intent(in) :: uhh, CFL
real, dimension(SZI_(G)), intent(in) :: slope_x
real, dimension(SZIB_(G)), intent(inout) :: Tr_x
! Local
integer :: i
real :: Tc
do I=is,ie
if (uhh(I) >= 0.0) then
Tc = scalar(i,j)