forked from NOAA-EMC/GFDL_atmos_cubed_sphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfv_tracer2d.F90
848 lines (751 loc) · 29.5 KB
/
fv_tracer2d.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
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the FV3 dynamical core.
!*
!* The FV3 dynamical core is free software: you can redistribute it
!* and/or modify it under the terms of the
!* GNU Lesser General Public License as published by the
!* Free Software Foundation, either version 3 of the License, or
!* (at your option) any later version.
!*
!* The FV3 dynamical core is distributed in the hope that it will be
!* useful, but WITHOUT ANYWARRANTY; without even the implied warranty
!* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!* See the GNU General Public License for more details.
!*
!* You should have received a copy of the GNU Lesser General Public
!* License along with the FV3 dynamical core.
!* If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************
!>@brief The module 'fv_tracer2d.F90' performs sub-cycled tracer advection.
!>@see \cite lin2004vertically
! Modules Included:
! <table>
! <tr>
! <th>Module Name</th>
! <th>Functions Included</th>
! </tr>
! <table>
! <tr>
! <td>boundary_mod</td>
! <td>nested_grid_BC_apply_intT</td>
! </tr>
! <tr>
! <td>fv_arrays_mod</td>
! <td>fv_grid_type, fv_nest_type, fv_atmos_type, fv_grid_bounds_type</td>
! </tr>
! <tr>
! <tr>
! <td>fv_mp_mod</td>
! <td>mp_reduce_max, ng, mp_gather, is_master, group_halo_update_type,
! start_group_halo_update, complete_group_halo_update</td>
! </tr>
! <tr>
! <td>fv_timing_mod</td>
! <td>timing_on, timing_off</td>
! </tr>
! <tr>
! <td>mpp_mod</td>
! <td>mpp_error, FATAL, mpp_broadcast, mpp_send, mpp_recv, mpp_sum, mpp_max</td>
! </tr>
! <tr>
! <td>mpp_domains_mod</td>
! <td>mpp_update_domains, CGRID_NE, domain2d</td>
! </tr>
! <tr>
! <td>tp_core_mod</td>
! <td>fv_tp_2d, copy_corners</td>
! </tr>
! </table>
module fv_tracer2d_mod
use tp_core_mod, only: fv_tp_2d, copy_corners
use fv_mp_mod, only: mp_reduce_max
use fv_mp_mod, only: mp_gather, is_master
use fv_mp_mod, only: group_halo_update_type
use fv_mp_mod, only: start_group_halo_update, complete_group_halo_update
use mpp_domains_mod, only: mpp_update_domains, CGRID_NE, domain2d
use fv_timing_mod, only: timing_on, timing_off
use boundary_mod, only: nested_grid_BC_apply_intT
use fv_regional_mod, only: regional_boundary_update
use fv_regional_mod, only: current_time_in_seconds
use fv_arrays_mod, only: fv_grid_type, fv_nest_type, fv_atmos_type, fv_grid_bounds_type
use mpp_mod, only: mpp_error, FATAL, mpp_broadcast, mpp_send, mpp_recv, mpp_sum, mpp_max
implicit none
private
public :: tracer_2d, tracer_2d_nested, tracer_2d_1L
real, allocatable, dimension(:,:,:) :: nest_fx_west_accum, nest_fx_east_accum, nest_fx_south_accum, nest_fx_north_accum
contains
!>@brief The subroutine 'tracer_2d_1L' performs 2-D horizontal-to-lagrangian transport.
!>@details This subroutine is called if 'z_tracer = .true.'
!! It modifies 'tracer_2d' so that each layer uses a different diagnosed number
!! of split tracer timesteps. This potentially accelerates tracer advection when there
!! is a large difference in layer-maximum wind speeds (cf. polar night jet).
subroutine tracer_2d_1L(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, &
nq, hord, q_split, dt, id_divg, q_pack, dp1_pack, nord_tr, trdm, lim_fac)
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(IN) :: npx
integer, intent(IN) :: npy
integer, intent(IN) :: npz
integer, intent(IN) :: nq !< number of tracers to be advected
integer, intent(IN) :: hord, nord_tr
integer, intent(IN) :: q_split
integer, intent(IN) :: id_divg
real , intent(IN) :: dt, trdm
real , intent(IN) :: lim_fac
type(group_halo_update_type), intent(inout) :: q_pack, dp1_pack
real , intent(INOUT) :: q(bd%isd:bd%ied,bd%jsd:bd%jed,npz,nq) !< Tracers
real , intent(INOUT) :: dp1(bd%isd:bd%ied,bd%jsd:bd%jed,npz) !< DELP before dyn_core
real , intent(INOUT) :: mfx(bd%is:bd%ie+1,bd%js:bd%je, npz) !< Mass Flux X-Dir
real , intent(INOUT) :: mfy(bd%is:bd%ie ,bd%js:bd%je+1,npz) !< Mass Flux Y-Dir
real , intent(INOUT) :: cx(bd%is:bd%ie+1,bd%jsd:bd%jed ,npz) !< Courant Number X-Dir
real , intent(INOUT) :: cy(bd%isd:bd%ied,bd%js :bd%je +1,npz) !< Courant Number Y-Dir
type(fv_grid_type), intent(IN), target :: gridstruct
type(domain2d), intent(INOUT) :: domain
! Local Arrays
real :: qn2(bd%isd:bd%ied,bd%jsd:bd%jed,nq) !< 3D tracers
real :: dp2(bd%is:bd%ie,bd%js:bd%je)
real :: fx(bd%is:bd%ie+1,bd%js:bd%je )
real :: fy(bd%is:bd%ie , bd%js:bd%je+1)
real :: ra_x(bd%is:bd%ie,bd%jsd:bd%jed)
real :: ra_y(bd%isd:bd%ied,bd%js:bd%je)
real :: xfx(bd%is:bd%ie+1,bd%jsd:bd%jed ,npz)
real :: yfx(bd%isd:bd%ied,bd%js: bd%je+1, npz)
real :: cmax(npz)
real :: frac
integer :: nsplt
integer :: i,j,k,it,iq
real, pointer, dimension(:,:) :: area, rarea
real, pointer, dimension(:,:,:) :: sin_sg
real, pointer, dimension(:,:) :: dxa, dya, dx, dy
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
area => gridstruct%area
rarea => gridstruct%rarea
sin_sg => gridstruct%sin_sg
dxa => gridstruct%dxa
dya => gridstruct%dya
dx => gridstruct%dx
dy => gridstruct%dy
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,cx,xfx,dxa,dy, &
!$OMP sin_sg,cy,yfx,dya,dx,cmax)
do k=1,npz
do j=jsd,jed
do i=is,ie+1
if (cx(i,j,k) > 0.) then
xfx(i,j,k) = cx(i,j,k)*dxa(i-1,j)*dy(i,j)*sin_sg(i-1,j,3)
else
xfx(i,j,k) = cx(i,j,k)*dxa(i, j)*dy(i,j)*sin_sg(i, j,1)
endif
enddo
enddo
do j=js,je+1
do i=isd,ied
if (cy(i,j,k) > 0.) then
yfx(i,j,k) = cy(i,j,k)*dya(i,j-1)*dx(i,j)*sin_sg(i,j-1,4)
else
yfx(i,j,k) = cy(i,j,k)*dya(i,j )*dx(i,j)*sin_sg(i,j, 2)
endif
enddo
enddo
cmax(k) = 0.
if ( k < npz/6 ) then
do j=js,je
do i=is,ie
cmax(k) = max( cmax(k), abs(cx(i,j,k)), abs(cy(i,j,k)) )
enddo
enddo
else
do j=js,je
do i=is,ie
cmax(k) = max( cmax(k), max(abs(cx(i,j,k)),abs(cy(i,j,k)))+1.-sin_sg(i,j,5) )
enddo
enddo
endif
enddo ! k-loop
if (trdm>1.e-4) then
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call complete_group_halo_update(dp1_pack, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
endif
call mp_reduce_max(cmax,npz)
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,cx,xfx, &
!$OMP cy,yfx,mfx,mfy,cmax) &
!$OMP private(nsplt, frac)
do k=1,npz
nsplt = int(1. + cmax(k))
if ( nsplt > 1 ) then
frac = 1. / real(nsplt)
do j=jsd,jed
do i=is,ie+1
cx(i,j,k) = cx(i,j,k) * frac
xfx(i,j,k) = xfx(i,j,k) * frac
enddo
enddo
do j=js,je
do i=is,ie+1
mfx(i,j,k) = mfx(i,j,k) * frac
enddo
enddo
do j=js,je+1
do i=isd,ied
cy(i,j,k) = cy(i,j,k) * frac
yfx(i,j,k) = yfx(i,j,k) * frac
enddo
enddo
do j=js,je+1
do i=is,ie
mfy(i,j,k) = mfy(i,j,k) * frac
enddo
enddo
endif
enddo
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call complete_group_halo_update(q_pack, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
! Begin k-independent tracer transport; can not be OpenMPed because the mpp_update call.
do k=1,npz
!$OMP parallel do default(none) shared(k,is,ie,js,je,isd,ied,jsd,jed,xfx,area,yfx,ra_x,ra_y)
do j=jsd,jed
do i=is,ie
ra_x(i,j) = area(i,j) + xfx(i,j,k) - xfx(i+1,j,k)
enddo
if ( j>=js .and. j<=je ) then
do i=isd,ied
ra_y(i,j) = area(i,j) + yfx(i,j,k) - yfx(i,j+1,k)
enddo
endif
enddo
nsplt = int(1. + cmax(k))
do it=1,nsplt
!$OMP parallel do default(none) shared(k,is,ie,js,je,rarea,mfx,mfy,dp1,dp2)
do j=js,je
do i=is,ie
dp2(i,j) = dp1(i,j,k) + (mfx(i,j,k)-mfx(i+1,j,k)+mfy(i,j,k)-mfy(i,j+1,k))*rarea(i,j)
enddo
enddo
!$OMP parallel do default(none) shared(k,nsplt,it,is,ie,js,je,isd,ied,jsd,jed,npx,npy,cx,xfx,hord,trdm, &
!$OMP nord_tr,nq,gridstruct,bd,cy,yfx,mfx,mfy,qn2,q,ra_x,ra_y,dp1,dp2,rarea,lim_fac) &
!$OMP private(fx,fy)
do iq=1,nq
if ( nsplt /= 1 ) then
if ( it==1 ) then
do j=jsd,jed
do i=isd,ied
qn2(i,j,iq) = q(i,j,k,iq)
enddo
enddo
endif
call fv_tp_2d(qn2(isd,jsd,iq), cx(is,jsd,k), cy(isd,js,k), &
npx, npy, hord, fx, fy, xfx(is,jsd,k), yfx(isd,js,k), &
gridstruct, bd, ra_x, ra_y, lim_fac, mfx=mfx(is,js,k), mfy=mfy(is,js,k))
if ( it < nsplt ) then ! not last call
do j=js,je
do i=is,ie
qn2(i,j,iq) = (qn2(i,j,iq)*dp1(i,j,k)+(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1))*rarea(i,j))/dp2(i,j)
enddo
enddo
else
do j=js,je
do i=is,ie
q(i,j,k,iq) = (qn2(i,j,iq)*dp1(i,j,k)+(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1))*rarea(i,j))/dp2(i,j)
enddo
enddo
endif
else
call fv_tp_2d(q(isd,jsd,k,iq), cx(is,jsd,k), cy(isd,js,k), &
npx, npy, hord, fx, fy, xfx(is,jsd,k), yfx(isd,js,k), &
gridstruct, bd, ra_x, ra_y, lim_fac, mfx=mfx(is,js,k), mfy=mfy(is,js,k))
do j=js,je
do i=is,ie
q(i,j,k,iq) = (q(i,j,k,iq)*dp1(i,j,k)+(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1))*rarea(i,j))/dp2(i,j)
enddo
enddo
endif
enddo ! tracer-loop
if ( it < nsplt ) then ! not last call
do j=js,je
do i=is,ie
dp1(i,j,k) = dp2(i,j)
enddo
enddo
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call mpp_update_domains(qn2, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
endif
enddo ! time-split loop
enddo ! k-loop
end subroutine tracer_2d_1L
!>@brief The subroutine 'tracer_2d' is the standard routine for sub-cycled tracer advection.
subroutine tracer_2d(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, &
nq, hord, q_split, dt, id_divg, q_pack, dp1_pack, nord_tr, trdm, lim_fac)
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(IN) :: npx
integer, intent(IN) :: npy
integer, intent(IN) :: npz
integer, intent(IN) :: nq !< number of tracers to be advected
integer, intent(IN) :: hord, nord_tr
integer, intent(IN) :: q_split
integer, intent(IN) :: id_divg
real , intent(IN) :: dt, trdm
real , intent(IN) :: lim_fac
type(group_halo_update_type), intent(inout) :: q_pack, dp1_pack
real , intent(INOUT) :: q(bd%isd:bd%ied,bd%jsd:bd%jed,npz,nq) !< Tracers
real , intent(INOUT) :: dp1(bd%isd:bd%ied,bd%jsd:bd%jed,npz) !< DELP before dyn_core
real , intent(INOUT) :: mfx(bd%is:bd%ie+1,bd%js:bd%je, npz) !< Mass Flux X-Dir
real , intent(INOUT) :: mfy(bd%is:bd%ie ,bd%js:bd%je+1,npz) !< Mass Flux Y-Dir
real , intent(INOUT) :: cx(bd%is:bd%ie+1,bd%jsd:bd%jed ,npz) !< Courant Number X-Dir
real , intent(INOUT) :: cy(bd%isd:bd%ied,bd%js :bd%je +1,npz) !< Courant Number Y-Dir
type(fv_grid_type), intent(IN), target :: gridstruct
type(domain2d), intent(INOUT) :: domain
! Local Arrays
real :: dp2(bd%is:bd%ie,bd%js:bd%je)
real :: fx(bd%is:bd%ie+1,bd%js:bd%je )
real :: fy(bd%is:bd%ie , bd%js:bd%je+1)
real :: ra_x(bd%is:bd%ie,bd%jsd:bd%jed)
real :: ra_y(bd%isd:bd%ied,bd%js:bd%je)
real :: xfx(bd%is:bd%ie+1,bd%jsd:bd%jed ,npz)
real :: yfx(bd%isd:bd%ied,bd%js: bd%je+1, npz)
real :: cmax(npz)
real :: c_global
real :: frac, rdt
integer :: ksplt(npz)
integer :: nsplt
integer :: i,j,k,it,iq
real, pointer, dimension(:,:) :: area, rarea
real, pointer, dimension(:,:,:) :: sin_sg
real, pointer, dimension(:,:) :: dxa, dya, dx, dy
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
area => gridstruct%area
rarea => gridstruct%rarea
sin_sg => gridstruct%sin_sg
dxa => gridstruct%dxa
dya => gridstruct%dya
dx => gridstruct%dx
dy => gridstruct%dy
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,cx,xfx,dxa,dy, &
!$OMP sin_sg,cy,yfx,dya,dx,cmax,q_split,ksplt)
do k=1,npz
do j=jsd,jed
do i=is,ie+1
if (cx(i,j,k) > 0.) then
xfx(i,j,k) = cx(i,j,k)*dxa(i-1,j)*dy(i,j)*sin_sg(i-1,j,3)
else
xfx(i,j,k) = cx(i,j,k)*dxa(i,j)*dy(i,j)*sin_sg(i,j,1)
endif
enddo
enddo
do j=js,je+1
do i=isd,ied
if (cy(i,j,k) > 0.) then
yfx(i,j,k) = cy(i,j,k)*dya(i,j-1)*dx(i,j)*sin_sg(i,j-1,4)
else
yfx(i,j,k) = cy(i,j,k)*dya(i,j)*dx(i,j)*sin_sg(i,j,2)
endif
enddo
enddo
if ( q_split == 0 ) then
cmax(k) = 0.
if ( k < npz/6 ) then
do j=js,je
do i=is,ie
cmax(k) = max( cmax(k), abs(cx(i,j,k)), abs(cy(i,j,k)) )
enddo
enddo
else
do j=js,je
do i=is,ie
cmax(k) = max( cmax(k), max(abs(cx(i,j,k)),abs(cy(i,j,k)))+1.-sin_sg(i,j,5) )
enddo
enddo
endif
endif
ksplt(k) = 1
enddo
!--------------------------------------------------------------------------------
! Determine global nsplt:
if ( q_split == 0 ) then
call mp_reduce_max(cmax,npz)
! find global max courant number and define nsplt to scale cx,cy,mfx,mfy
c_global = cmax(1)
if ( npz /= 1 ) then ! if NOT shallow water test case
do k=2,npz
c_global = max(cmax(k), c_global)
enddo
endif
nsplt = int(1. + c_global)
if ( is_master() .and. nsplt > 4 ) write(*,*) 'Tracer_2d_split=', nsplt, c_global
else
nsplt = q_split
endif
!--------------------------------------------------------------------------------
if( nsplt /= 1 ) then
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,cx,xfx,mfx,cy,yfx,mfy,cmax,nsplt,ksplt) &
!$OMP private( frac )
do k=1,npz
#ifdef GLOBAL_CFL
ksplt(k) = nsplt
#else
ksplt(k) = int(1. + cmax(k))
#endif
frac = 1. / real(ksplt(k))
do j=jsd,jed
do i=is,ie+1
cx(i,j,k) = cx(i,j,k) * frac
xfx(i,j,k) = xfx(i,j,k) * frac
enddo
enddo
do j=js,je
do i=is,ie+1
mfx(i,j,k) = mfx(i,j,k) * frac
enddo
enddo
do j=js,je+1
do i=isd,ied
cy(i,j,k) = cy(i,j,k) * frac
yfx(i,j,k) = yfx(i,j,k) * frac
enddo
enddo
do j=js,je+1
do i=is,ie
mfy(i,j,k) = mfy(i,j,k) * frac
enddo
enddo
enddo
endif
if (trdm>1.e-4) then
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call complete_group_halo_update(dp1_pack, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
endif
do it=1,nsplt
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call complete_group_halo_update(q_pack, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,dp1,mfx,mfy,rarea,nq,ksplt,&
!$OMP area,xfx,yfx,q,cx,cy,npx,npy,hord,gridstruct,bd,it,nsplt,nord_tr,trdm,lim_fac) &
!$OMP private(dp2, ra_x, ra_y, fx, fy)
do k=1,npz
if ( it .le. ksplt(k) ) then
do j=js,je
do i=is,ie
dp2(i,j) = dp1(i,j,k) + (mfx(i,j,k)-mfx(i+1,j,k)+mfy(i,j,k)-mfy(i,j+1,k))*rarea(i,j)
enddo
enddo
do j=jsd,jed
do i=is,ie
ra_x(i,j) = area(i,j) + xfx(i,j,k) - xfx(i+1,j,k)
enddo
enddo
do j=js,je
do i=isd,ied
ra_y(i,j) = area(i,j) + yfx(i,j,k) - yfx(i,j+1,k)
enddo
enddo
do iq=1,nq
if ( it==1 .and. trdm>1.e-4 ) then
call fv_tp_2d(q(isd,jsd,k,iq), cx(is,jsd,k), cy(isd,js,k), &
npx, npy, hord, fx, fy, xfx(is,jsd,k), yfx(isd,js,k), &
gridstruct, bd, ra_x, ra_y, lim_fac, mfx=mfx(is,js,k), mfy=mfy(is,js,k), &
mass=dp1(isd,jsd,k), nord=nord_tr, damp_c=trdm)
else
call fv_tp_2d(q(isd,jsd,k,iq), cx(is,jsd,k), cy(isd,js,k), &
npx, npy, hord, fx, fy, xfx(is,jsd,k), yfx(isd,js,k), &
gridstruct, bd, ra_x, ra_y, lim_fac, mfx=mfx(is,js,k), mfy=mfy(is,js,k))
endif
do j=js,je
do i=is,ie
q(i,j,k,iq) = ( q(i,j,k,iq)*dp1(i,j,k) + &
(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1))*rarea(i,j) )/dp2(i,j)
enddo
enddo
enddo
if ( it /= nsplt ) then
do j=js,je
do i=is,ie
dp1(i,j,k) = dp2(i,j)
enddo
enddo
endif
endif ! ksplt
enddo ! npz
if ( it /= nsplt ) then
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call start_group_halo_update(q_pack, q, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
endif
enddo ! nsplt
end subroutine tracer_2d
subroutine tracer_2d_nested(q, dp1, mfx, mfy, cx, cy, gridstruct, bd, domain, npx, npy, npz, &
nq, hord, q_split, dt, id_divg, q_pack, dp1_pack, nord_tr, trdm, &
k_split, neststruct, parent_grid, n_map, lim_fac)
type(fv_grid_bounds_type), intent(IN) :: bd
integer, intent(IN) :: npx
integer, intent(IN) :: npy
integer, intent(IN) :: npz
integer, intent(IN) :: nq !< number of tracers to be advected
integer, intent(IN) :: hord, nord_tr
integer, intent(IN) :: q_split, k_split, n_map
integer, intent(IN) :: id_divg
real , intent(IN) :: dt, trdm
real , intent(IN) :: lim_fac
type(group_halo_update_type), intent(inout) :: q_pack, dp1_pack
real , intent(INOUT) :: q(bd%isd:bd%ied,bd%jsd:bd%jed,npz,nq) !< Tracers
real , intent(INOUT) :: dp1(bd%isd:bd%ied,bd%jsd:bd%jed,npz) !< DELP before dyn_core
real , intent(INOUT) :: mfx(bd%is:bd%ie+1,bd%js:bd%je, npz) !< Mass Flux X-Dir
real , intent(INOUT) :: mfy(bd%is:bd%ie ,bd%js:bd%je+1,npz) !< Mass Flux Y-Dir
real , intent(INOUT) :: cx(bd%is:bd%ie+1,bd%jsd:bd%jed ,npz) !< Courant Number X-Dir
real , intent(INOUT) :: cy(bd%isd:bd%ied,bd%js :bd%je +1,npz) !< Courant Number Y-Dir
type(fv_grid_type), intent(IN), target :: gridstruct
type(fv_nest_type), intent(INOUT) :: neststruct
type(fv_atmos_type), pointer, intent(IN) :: parent_grid
type(domain2d), intent(INOUT) :: domain
! Local Arrays
real :: dp2(bd%is:bd%ie,bd%js:bd%je)
real :: fx(bd%is:bd%ie+1,bd%js:bd%je )
real :: fy(bd%is:bd%ie , bd%js:bd%je+1)
real :: ra_x(bd%is:bd%ie,bd%jsd:bd%jed)
real :: ra_y(bd%isd:bd%ied,bd%js:bd%je)
real :: xfx(bd%is:bd%ie+1,bd%jsd:bd%jed ,npz)
real :: yfx(bd%isd:bd%ied,bd%js: bd%je+1, npz)
real :: cmax(npz)
real :: cmax_t
real :: c_global
real :: frac, rdt
real :: reg_bc_update_time
integer :: nsplt, nsplt_parent, msg_split_steps = 1
integer :: i,j,k,it,iq
real, pointer, dimension(:,:) :: area, rarea
real, pointer, dimension(:,:,:) :: sin_sg
real, pointer, dimension(:,:) :: dxa, dya, dx, dy
integer :: is, ie, js, je
integer :: isd, ied, jsd, jed
is = bd%is
ie = bd%ie
js = bd%js
je = bd%je
isd = bd%isd
ied = bd%ied
jsd = bd%jsd
jed = bd%jed
area => gridstruct%area
rarea => gridstruct%rarea
sin_sg => gridstruct%sin_sg
dxa => gridstruct%dxa
dya => gridstruct%dya
dx => gridstruct%dx
dy => gridstruct%dy
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,cx,xfx,dxa,dy, &
!$OMP sin_sg,cy,yfx,dya,dx)
do k=1,npz
do j=jsd,jed
do i=is,ie+1
if (cx(i,j,k) > 0.) then
xfx(i,j,k) = cx(i,j,k)*dxa(i-1,j)*dy(i,j)*sin_sg(i-1,j,3)
else
xfx(i,j,k) = cx(i,j,k)*dxa(i,j)*dy(i,j)*sin_sg(i,j,1)
endif
enddo
enddo
do j=js,je+1
do i=isd,ied
if (cy(i,j,k) > 0.) then
yfx(i,j,k) = cy(i,j,k)*dya(i,j-1)*dx(i,j)*sin_sg(i,j-1,4)
else
yfx(i,j,k) = cy(i,j,k)*dya(i,j)*dx(i,j)*sin_sg(i,j,2)
endif
enddo
enddo
enddo
!--------------------------------------------------------------------------------
if ( q_split == 0 ) then
! Determine nsplt
!$OMP parallel do default(none) shared(is,ie,js,je,npz,cmax,cx,cy,sin_sg) &
!$OMP private(cmax_t )
do k=1,npz
cmax(k) = 0.
if ( k < 4 ) then
! Top layers: C < max( abs(c_x), abs(c_y) )
do j=js,je
do i=is,ie
cmax_t = max( abs(cx(i,j,k)), abs(cy(i,j,k)) )
cmax(k) = max( cmax_t, cmax(k) )
enddo
enddo
else
do j=js,je
do i=is,ie
cmax_t = max(abs(cx(i,j,k)), abs(cy(i,j,k))) + 1.-sin_sg(i,j,5)
cmax(k) = max( cmax_t, cmax(k) )
enddo
enddo
endif
enddo
call mp_reduce_max(cmax,npz)
! find global max courant number and define nsplt to scale cx,cy,mfx,mfy
c_global = cmax(1)
if ( npz /= 1 ) then ! if NOT shallow water test case
do k=2,npz
c_global = max(cmax(k), c_global)
enddo
endif
nsplt = int(1. + c_global)
if ( is_master() .and. nsplt > 3 ) write(*,*) 'Tracer_2d_split=', nsplt, c_global
else
nsplt = q_split
if (gridstruct%nested .and. neststruct%nestbctype > 1) msg_split_steps = max(q_split/parent_grid%flagstruct%q_split,1)
endif
!--------------------------------------------------------------------------------
frac = 1. / real(nsplt)
if( nsplt /= 1 ) then
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,cx,frac,xfx,mfx,cy,yfx,mfy)
do k=1,npz
do j=jsd,jed
do i=is,ie+1
cx(i,j,k) = cx(i,j,k) * frac
xfx(i,j,k) = xfx(i,j,k) * frac
enddo
enddo
do j=js,je
do i=is,ie+1
mfx(i,j,k) = mfx(i,j,k) * frac
enddo
enddo
do j=js,je+1
do i=isd,ied
cy(i,j,k) = cy(i,j,k) * frac
yfx(i,j,k) = yfx(i,j,k) * frac
enddo
enddo
do j=js,je+1
do i=is,ie
mfy(i,j,k) = mfy(i,j,k) * frac
enddo
enddo
enddo
endif
do it=1,nsplt
if ( gridstruct%nested ) then
neststruct%tracer_nest_timestep = neststruct%tracer_nest_timestep + 1
end if
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call complete_group_halo_update(q_pack, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
if (gridstruct%nested) then
do iq=1,nq
call nested_grid_BC_apply_intT(q(isd:ied,jsd:jed,:,iq), &
0, 0, npx, npy, npz, bd, &
real(neststruct%tracer_nest_timestep)+real(nsplt*k_split), real(nsplt*k_split), &
neststruct%q_BC(iq), bctype=neststruct%nestbctype )
enddo
endif
if (gridstruct%regional) then
!This is more accurate than the nested BC calculation
! since it takes into account varying nsplit
reg_bc_update_time=current_time_in_seconds+(real(n_map-1) + real(it-1)*frac)*dt
do iq=1,nq
call regional_boundary_update(q(:,:,:,iq), 'q', &
isd, ied, jsd, jed, npz, &
is, ie, js, je, &
isd, ied, jsd, jed, &
reg_bc_update_time, &
it, iq )
enddo
endif
if (trdm>1.e-4) then
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call complete_group_halo_update(dp1_pack, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
endif
!$OMP parallel do default(none) shared(is,ie,js,je,isd,ied,jsd,jed,npz,dp1,mfx,mfy,rarea,nq, &
!$OMP area,xfx,yfx,q,cx,cy,npx,npy,hord,gridstruct,bd,it,nsplt,nord_tr,trdm,lim_fac) &
!$OMP private(dp2, ra_x, ra_y, fx, fy)
do k=1,npz
do j=js,je
do i=is,ie
dp2(i,j) = dp1(i,j,k) + (mfx(i,j,k)-mfx(i+1,j,k)+mfy(i,j,k)-mfy(i,j+1,k))*rarea(i,j)
enddo
enddo
do j=jsd,jed
do i=is,ie
ra_x(i,j) = area(i,j) + xfx(i,j,k) - xfx(i+1,j,k)
enddo
enddo
do j=js,je
do i=isd,ied
ra_y(i,j) = area(i,j) + yfx(i,j,k) - yfx(i,j+1,k)
enddo
enddo
do iq=1,nq
if ( it==1 .and. trdm>1.e-4 ) then
call fv_tp_2d(q(isd,jsd,k,iq), cx(is,jsd,k), cy(isd,js,k), &
npx, npy, hord, fx, fy, xfx(is,jsd,k), yfx(isd,js,k), &
gridstruct, bd, ra_x, ra_y, lim_fac, mfx=mfx(is,js,k), mfy=mfy(is,js,k), &
mass=dp1(isd,jsd,k), nord=nord_tr, damp_c=trdm)
else
call fv_tp_2d(q(isd,jsd,k,iq), cx(is,jsd,k), cy(isd,js,k), &
npx, npy, hord, fx, fy, xfx(is,jsd,k), yfx(isd,js,k), &
gridstruct, bd, ra_x, ra_y, lim_fac, mfx=mfx(is,js,k), mfy=mfy(is,js,k))
endif
do j=js,je
do i=is,ie
q(i,j,k,iq) = ( q(i,j,k,iq)*dp1(i,j,k) + &
(fx(i,j)-fx(i+1,j)+fy(i,j)-fy(i,j+1))*rarea(i,j) )/dp2(i,j)
enddo
enddo
enddo
enddo ! npz
if ( it /= nsplt ) then
call timing_on('COMM_TOTAL')
call timing_on('COMM_TRACER')
call start_group_halo_update(q_pack, q, domain)
call timing_off('COMM_TRACER')
call timing_off('COMM_TOTAL')
endif
enddo ! nsplt
if ( id_divg > 0 ) then
rdt = 1./(frac*dt)
!$OMP parallel do default(none) shared(is,ie,js,je,npz,dp1,xfx,yfx,rarea,rdt)
do k=1,npz
do j=js,je
do i=is,ie
dp1(i,j,k) = (xfx(i+1,j,k)-xfx(i,j,k) + yfx(i,j+1,k)-yfx(i,j,k))*rarea(i,j)*rdt
enddo
enddo
enddo
endif
end subroutine tracer_2d_nested
end module fv_tracer2d_mod