forked from kshedstrom/roms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathad_nesting.F
7971 lines (7789 loc) · 335 KB
/
ad_nesting.F
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
#include "cppdefs.h"
MODULE ad_nesting_mod
#if defined NESTING && defined ADJOINT
!
!svn $Id$
!================================================== Hernan G. Arango ===
! Copyright (c) 2002-2020 The ROMS/TOMS Group Andrew M. Moore !
! Licensed under a MIT/X style license !
! See License_ROMS.txt !
!=======================================================================
! !
! This module contains several routines to process the connectivity !
! between nested grids. It process the contact region points between !
! data donor and data receiver grids. !
! !
! The locations of the linear interpolation weights in the donor !
! grid with respect the receiver grid contact region at contact !
! point x(Irg,Jrg,Krg) are: !
! !
! 8___________7 (Idg+1,Jdg+1,Kdg) !
! /. /| !
! / . / | !
! (Idg,Jdg+1,Kdg) 5/___________/6 | !
! | . | | !
! | . x | | !
! | 4.........|..|3 (Idg+1,Jdg+1,Kdg-1) !
! | . | / !
! |. | / !
! |___________|/ !
! (Idg,Jdg,Kdg-1) 1 2 !
! !
! Suffix: dg = donor grid !
! rg = receiver grid !
! !
! Routines: !
! ======== !
! !
! ad_nesting Public interface to time-stepping kernel !
! !
! ad_get_composite Composite grid, extracts contact points donor !
! data !
! ad_get_refine Refinement grid, extracts contact points donor !
! data !
! ad_put_composite Composite grid, fills contact points (spatial !
! interpolation) !
! ad_put_refine Refinement grid, fills contact points (spatial !
! and temporal interpolation) !
! !
! ad_bry_fluxes Extracts horizontat advective fluxes the contact !
! boundary of donor and receiver grids !
# ifdef NESTING_DEBUG
! ad_check_massflux If refinement, checks mass fluxes between coarse !
! and fine grids for volume conservation. It is !
! use only for debugging and diagnostics. !
# endif
! ad_correct_tracer Corrects coarse grid tracer at the refinement !
! grid boundary with the refined accumulated !
! fluxes !
! ad_fine2coarse Replace coarse grid state variables with the !
! averaged fine grid values (two-way nesting) !
! !
! ad_get_contact2d Gets 2D field donor grid cell holding contact !
! point !
! ad_get_contact3d Gets 3D field donor grid cell holding contact !
! point !
! ad_get_persisted2d Gets 2D field persisted values on contact points !
! ad_put_contact2d Sets 2D field contact points, spatial !
! interpolation !
! ad_put_contact3d Sets 3D field contact points, spatial !
! interpolation !
! !
! ad_put_refine2d Interpolates (space-time) 2D state variables !
! ad_put_refine3d Interpolates (space-time) 3D state variables !
! !
! ad_z_weights Sets donor grid vertical indices (cell holding !
! contact point) and vertical interpolation !
! weights !
! !
! WARNINGS: !
! ======== !
! !
! All the routines contained in this module are inside of a parallel !
! region, except the main driver routine "nesting", which is called !
! serially several times from main2d or main3d to perform different !
! tasks. Notice that the calls to private "get_***" and "put_***" !
! routines need to be in separated parallel loops because of serial !
! with partitions and shared-memory rules. Furthermore, the donor !
! and receiver grids may have different tile partitions. There is no !
! I/O management inside the nesting routines. !
! !
! The connectivity between donor and receiver grids can be complex. !
! The horizontal mapping between grids is static and done outside of !
! ROMS. Only the time-dependent vertical interpolation weights are !
! computed here. The contact region points I- and J-cell indices !
! between donor and receiver grids, and the horizontal interpolation !
! weights are read from the input nesting connectivity NetCDF file. !
! It makes the nesting efficient and greatly simplifies parallelism. !
! !
!=======================================================================
!
implicit none
!
PUBLIC :: ad_nesting
PUBLIC :: ad_bry_fluxes
# ifdef NESTING_DEBUG
PRIVATE :: ad_check_massflux
# endif
# ifdef SOLVE3D
PRIVATE :: ad_correct_tracer
PRIVATE :: ad_correct_tracer_tile
# endif
PRIVATE :: ad_fine2coarse
PRIVATE :: ad_fine2coarse2d
# ifdef SOLVE3D
PRIVATE :: ad_fine2coarse3d
# endif
PRIVATE :: ad_get_contact2d
# ifdef SOLVE3D
PRIVATE :: ad_get_contact3d
# endif
PRIVATE :: ad_get_composite
PRIVATE :: ad_get_persisted2d
PRIVATE :: ad_get_refine
PRIVATE :: ad_put_composite
PRIVATE :: ad_put_refine
PRIVATE :: ad_put_refine2d
# ifdef SOLVE3D
PRIVATE :: ad_put_refine3d
PRIVATE :: ad_z_weights
# endif
!
CONTAINS
!
SUBROUTINE ad_nesting (ng, model, isection)
!
!=======================================================================
! !
! This routine process the contact region points between composite !
! grids. In composite grids, it is possible to have more than one !
! contact region. !
! !
! On Input: !
! !
! ng Data receiver grid number (integer) !
! model Calling model identifier (integer) !
! isection Governing equations time-stepping section in !
! main2d or main3d indicating which state !
! variables to process (integer) !
! !
!=======================================================================
!
USE mod_param
USE mod_parallel
USE mod_ncparam
USE mod_nesting
USE mod_scalars
!
# ifdef SOLVE3D
USE set_depth_mod, ONLY : set_depth
# endif
USE ad_set_depth_mod, ONLY : ad_set_depth
USE nesting_mod, ONLY : get_metrics
USE nesting_mod, ONLY : mask_hweights
USE nesting_mod, ONLY : z_weights
USE strings_mod, ONLY : FoundError
!
! Imported variable declarations.
!
integer, intent(in) :: ng, model, isection
!
! Local variable declarations.
!
logical :: LputFsur
integer :: subs, tile, thread
integer :: ngc
# ifdef SOLVE3D
!
!-----------------------------------------------------------------------
! Process vertical indices and interpolation weigths associated with
! depth.
!-----------------------------------------------------------------------
!
IF (isection.eq.nzwgt) THEN
DO tile=last_tile(ng),first_tile(ng),-1
CALL z_weights (ng, model, tile)
END DO
!$OMP BARRIER
RETURN
END IF
# endif
# if defined MASKING || defined WET_DRY
!
!-----------------------------------------------------------------------
! If Land/Sea masking, scale horizontal interpolation weights to
! account for land contact points. If wetting and drying, the scaling
! is done at every time-step because masking is time dependent.
!-----------------------------------------------------------------------
!
IF (isection.eq.nmask) THEN
DO tile=last_tile(ng),first_tile(ng),-1
CALL mask_hweights (ng, model, tile)
END DO
!$OMP BARRIER
RETURN
END IF
# endif
!
!-----------------------------------------------------------------------
! If refinement grid, process contact points.
!-----------------------------------------------------------------------
!
IF (RefinedGrid(ng)) THEN
!
! Extract grid spacing metrics (on_u and om_v) and load then to
! REFINE(:) structure. These metrics are needed to impose mass
! flux at the finer grid physical boundaries. It need to be done
! separately because parallelism partions between all nested grid.
!
IF (isection.eq.ndxdy) THEN
DO tile=first_tile(ng),last_tile(ng),+1
CALL get_metrics (ng, model, tile)
END DO
!$OMP BARRIER
!
! Extract and store donor grid data at contact points.
!
ELSE IF (isection.eq.ngetD) THEN
DO tile=first_tile(ng),last_tile(ng),+1
!> CALL tl_get_refine (ng, model, tile)
!>
CALL ad_get_refine (ng, model, tile)
END DO
!$OMP BARRIER
!
! Fill refinement grid contact points variables by interpolating
! (space, time) from extracted donor grid data. The free-surface
! needs to be processed first and in a separate parallel region
! because of shared-memory applications.
!
ELSE IF (isection.eq.nputD) THEN
!$OMP BARRIER
!
LputFsur=.FALSE.
DO tile=first_tile(ng),last_tile(ng),+1
!> CALL tl_put_refine (ng, model, tile, LputFsur)
!>
CALL ad_put_refine (ng, model, tile, LputFsur)
END DO
!$OMP BARRIER
LputFsur=.TRUE.
DO tile=first_tile(ng),last_tile(ng),+1
!> CALL tl_put_refine (ng, model, tile, LputFsur)
!>
CALL ad_put_refine (ng, model, tile, LputFsur)
END DO
# ifdef NESTING_DEBUG
!
! If refinement, check mass flux conservation between coarser and
! finer grids. DIAGNOSTIC ONLY.
!
ELSE IF (isection.eq.nmflx) THEN
DO tile=first_tile(ng),last_tile(ng),+1
!> CALL tl_check_massflux (ng, model, tile)
!>
CALL ad_check_massflux (ng, model, tile)
END DO
# endif
# ifndef ONE_WAY
!
! Fine to coarse coupling: two-way nesting.
!
ELSE IF (isection.eq.n2way) THEN
ngc=CoarserDonor(ng) ! coarse grid number
# ifdef SOLVE3D
!$OMP BARRIER
!
! Replace coarse grid 3D state variables with the averaged fine grid
! values (two-way coupling).
!
DO tile=last_tile(ngc),first_tile(ngc),-1
!> CALL tl_fine2coarse (ng, model, r3dvar, tile)
!>
CALL ad_fine2coarse (ng, model, r3dvar, tile)
END DO
!$OMP BARRIER
IF (FoundError(exit_flag, NoError, __LINE__, &
& __FILE__)) RETURN
!
! Update coarse grid depth variables. We have a new coarse grid
! adjusted free-surface, Zt_avg1.
!
DO tile=first_tile(ngc),last_tile(ngc),+1
!> CALL tl_set_depth (ngc, tile, model)
!>
CALL ad_set_depth (ngc, tile, model)
END DO
# endif
!
! Replace coarse grid 2D state variables with the averaged fine grid
! values (two-way coupling).
!
DO tile=last_tile(ngc),first_tile(ngc),-1
!> CALL fine2coarse (ng, model, r2dvar, tile)
!>
CALL ad_fine2coarse (ng, model, r2dvar, tile)
END DO
!$OMP BARRIER
IF (FoundError(exit_flag, NoError, __LINE__, &
& __FILE__)) RETURN
# if defined SOLVE3D && !defined NO_CORRECT_TRACER
!
! Correct coarse grid tracer values at the refinement grid, ng,
! boundary with the refined accumulated fluxes (Hz*u*T/n, Hz*v*T/m).
!
DO tile=first_tile(ngc),last_tile(ngc),+1
!> CALL tl_correct_tracer (ngc, ng, model, tile)
!>
CALL ad_correct_tracer (ngc, ng, model, tile)
END DO
!$OMP BARRIER
# endif
# else
!
! Fine to coarse coupling (two-way nesting) is not activated!
!
ELSE IF (isection.eq.n2way) THEN
# endif
END IF
ELSE
!
!-----------------------------------------------------------------------
! Otherwise, process contact points in composite grid.
!-----------------------------------------------------------------------
!
!$OMP BARRIER
!
! Fill composite grid contact points variables by interpolating from
! extracted donor grid data.
!
DO tile=last_tile(ng),first_tile(ng),-1
!> CALL tl_put_composite (ng, model, isection, tile)
!>
CALL ad_put_composite (ng, model, isection, tile)
END DO
!$OMP BARRIER
!
! Get composite grid contact points data from donor grid. It extracts
! the donor grid cell data necessary to interpolate state variables
! at each contact point.
!
DO tile=first_tile(ng),last_tile(ng),+1
!> CALL tl_get_composite (ng, model, isection, tile)
!>
CALL ad_get_composite (ng, model, isection, tile)
END DO
END IF
# ifdef SOLVE3D
!
!-----------------------------------------------------------------------
! Process vertical indices and interpolation weigths associated with
! depth.
!-----------------------------------------------------------------------
!
IF (isection.eq.nzwgt) THEN
DO tile=last_tile(ng),first_tile(ng),-1
!> CALL tl_z_weights (ng, model, tile)
!>
CALL ad_z_weights (ng, model, tile)
END DO
!$OMP BARRIER
RETURN
END IF
# endif
RETURN
END SUBROUTINE ad_nesting
!
SUBROUTINE ad_get_composite (ng, model, isection, tile)
!
!=======================================================================
! !
! This routine gets the donor grid data required to process the !
! contact points of the current composite grid. It extracts the !
! donor cell points containing each contact point. In composite !
! grids, it is possible to have more than one contact region. !
! !
! The interpolation of composite grid contact points from donor !
! grid data is carried out in a different parallel region using !
! 'put_composite'. !
! !
! On Input: !
! !
! ng Composite grid number (integer) !
! model Calling model identifier (integer) !
! isection Governing equations time-stepping section in !
! main2d or main3d indicating which state !
! variables to process (integer) !
! tile Domain tile partition (integer) !
! !
! On Output: (mod_nesting) !
! !
! COMPOSITE Updated contact points structure. !
! !
!=======================================================================
!
USE mod_param
USE mod_coupling
USE mod_forces
USE mod_grid
USE mod_ncparam
USE mod_nesting
USE mod_ocean
USE mod_scalars
USE mod_stepping
USE nesting_mod, ONLY : get_contact2d
# ifdef SOLVE3D
USE nesting_mod, ONLY : get_contact3d
# endif
!
! Imported variable declarations.
!
integer, intent(in) :: ng, model, isection, tile
!
! Local variable declarations.
!
integer :: cr, dg, rg, nrec, rec
# ifdef SOLVE3D
integer :: itrc
# endif
integer :: LBi, UBi, LBj, UBj
integer :: Tindex
!
!-----------------------------------------------------------------------
! Get donor grid data needed to process composite grid contact points.
! Only process those variables associated with the governing equation
! time-stepping section.
!-----------------------------------------------------------------------
!
DO cr=1,Ncontact
!
! Get data donor and data receiver grid numbers.
!
dg=Rcontact(cr)%donor_grid
rg=Rcontact(cr)%receiver_grid
!
! Process only contact region data for requested nested grid "ng".
!
IF (rg.eq.ng) THEN
!
! Set donor grid lower and upper array indices.
!
LBi=BOUNDS(dg)%LBi(tile)
UBi=BOUNDS(dg)%UBi(tile)
LBj=BOUNDS(dg)%LBj(tile)
UBj=BOUNDS(dg)%UBj(tile)
!
! Process bottom stress (bustr, bvstr).
!
IF (isection.eq.nbstr) THEN
!> CALL get_contact2d (dg, model, tile, &
!> & u2dvar, Vname(1,idUbms), &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, &
!> & FORCES(dg) % tl_bustr, &
!> & COMPOSITE(cr) % tl_bustr)
!>
CALL ad_get_contact2d (dg, model, tile, &
& u2dvar, Vname(1,idUbms), &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, &
& FORCES(dg) % ad_bustr, &
& COMPOSITE(cr) % ad_bustr)
!> CALL get_contact2d (dg, model, tile, &
!> & v2dvar, Vname(1,idVbms), &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & FORCES(dg) % tl_bvstr, &
!> & COMPOSITE(cr) % tl_bvstr)
!>
CALL ad_get_contact2d (dg, model, tile, &
& v2dvar, Vname(1,idVbms), &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, &
& FORCES(dg) % ad_bvstr, &
& COMPOSITE(cr) % ad_bvstr)
END IF
!
! Process free-surface (zeta) at the appropriate time index.
!
IF ((isection.eq.nFSIC).or. &
& (isection.eq.nzeta).or. &
& (isection.eq.n2dPS).or. &
& (isection.eq.n2dCS)) THEN
IF (isection.eq.nzeta) THEN
nrec=2 ! process time records 1 and 2
ELSE
nrec=1 ! process knew record
END IF
DO rec=1,nrec
IF (isection.eq.nzeta) THEN
Tindex=rec
ELSE
Tindex=knew(dg)
END IF
!> CALL get_contact2d (dg, model, tile, &
!> & r2dvar, Vname(1,idFsur), &
!> & cr, Rcontact(cr)%Npoints, Rcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_zeta(:,:,Tindex), &
!> & COMPOSITE(cr) % tl_zeta(:,:,rec))
!>
CALL ad_get_contact2d (dg, model, tile, &
& r2dvar, Vname(1,idFsur), &
& cr, Rcontact(cr)%Npoints, Rcontact,&
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_zeta(:,:,Tindex), &
& COMPOSITE(cr) % ad_zeta(:,:,rec))
END DO
END IF
!
! Process free-surface equation rigth-hand-side (rzeta) term.
!
IF (isection.eq.n2dPS) THEN
Tindex=1
!> CALL get_contact2d (dg, model, tile, &
!> & r2dvar, Vname(1,idRzet), &
!> & cr, Rcontact(cr)%Npoints, Rcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_rzeta(:,:,Tindex), &
!> & COMPOSITE(cr) % tl_rzeta)
!>
CALL ad_get_contact2d (dg, model, tile, &
& r2dvar, Vname(1,idRzet), &
& cr, Rcontact(cr)%Npoints, Rcontact, &
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_rzeta(:,:,Tindex), &
& COMPOSITE(cr) % ad_rzeta)
END IF
!
! Process 2D momentum components (ubar,vbar) at the appropriate time
! index.
!
IF ((isection.eq.n2dIC).or. &
& (isection.eq.n2dPS).or. &
& (isection.eq.n2dCS).or. &
& (isection.eq.n3duv)) THEN
IF (isection.eq.n3duv) THEN
nrec=2 ! process time records 1 and 2
ELSE
nrec=1 ! process knew record
END IF
DO rec=1,nrec
IF (isection.eq.n3duv) THEN
Tindex=rec
ELSE
Tindex=knew(dg)
END IF
!> CALL get_contact2d (dg, model, tile, &
!> & u2dvar, Vname(1,idUbar), &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_ubar(:,:,Tindex), &
!> & COMPOSITE(cr) % tl_ubar(:,:,rec))
!>
CALL ad_get_contact2d (dg, model, tile, &
& u2dvar, Vname(1,idUbar), &
& cr, Ucontact(cr)%Npoints, Ucontact,&
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_ubar(:,:,Tindex), &
& COMPOSITE(cr) % ad_ubar(:,:,rec))
!> CALL get_contact2d (dg, model, tile, &
!> & v2dvar, Vname(1,idVbar), &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_vbar(:,:,Tindex), &
!> & COMPOSITE(cr) % tl_vbar(:,:,rec))
!>
CALL ad_get_contact2d (dg, model, tile, &
& v2dvar, Vname(1,idVbar), &
& cr, Vcontact(cr)%Npoints, Vcontact,&
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_vbar(:,:,Tindex), &
& COMPOSITE(cr) % ad_vbar(:,:,rec))
END DO
END IF
# ifdef SOLVE3D
!
! Process time averaged free-surface (Zt_avg1) and 2D momentum fluxes
! (DU_avg1, DV_avg1).
!
IF (isection.eq.n2dfx) THEN
!> CALL get_contact2d (dg, model, tile, &
!> & r2dvar, 'Zt_avg1', &
!> & cr, Rcontact(cr)%Npoints, Rcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & COUPLING(dg) % tl_Zt_avg1, &
!> & COMPOSITE(cr) % tl_Zt_avg1)
!>
CALL ad_get_contact2d (dg, model, tile, &
& r2dvar, 'Zt_avg1', &
& cr, Rcontact(cr)%Npoints, Rcontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % ad_Zt_avg1, &
& COMPOSITE(cr) % ad_Zt_avg1)
!
! Do we need to get DU_avg1 and DV_avg1 here? YES.
!
CALL get_contact2d (dg, model, tile, &
& u2dvar, 'DU_avg1', &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % DU_avg1, &
& COMPOSITE(cr) % DU_avg1)
CALL get_contact2d (dg, model, tile, &
& v2dvar, 'DV_avg1', &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % DV_avg1, &
& COMPOSITE(cr) % DV_avg1)
!> CALL get_contact2d (dg, model, tile, &
!> & u2dvar, 'DU_avg1', &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, &
!> & COUPLING(dg) % tl_DU_avg1, &
!> & COMPOSITE(cr) % tl_DU_avg1)
!>
CALL ad_get_contact2d (dg, model, tile, &
& u2dvar, 'DU_avg1', &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % ad_DU_avg1, &
& COMPOSITE(cr) % ad_DU_avg1)
!> CALL get_contact2d (dg, model, tile, &
!> & v2dvar, 'DV_avg1', &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & COUPLING(dg) % tl_DV_avg1, &
!> & COMPOSITE(cr) % tl_DV_avg1)
!>
CALL ad_get_contact2d (dg, model, tile, &
& v2dvar, 'DV_avg1', &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % ad_DV_avg1, &
& COMPOSITE(cr) % ad_DV_avg1)
END IF
# if !defined TS_FIXED
!
! Process tracer variables (t) at the appropriate time index.
!
IF ((isection.eq.nTVIC).or. &
& (isection.eq.nrhst).or. &
& (isection.eq.n3dTV)) THEN
DO itrc=1,NT(ng)
IF (isection.eq.nrhst) THEN
Tindex=3
ELSE
Tindex=nnew(dg)
END IF
!> CALL get_contact3d (dg, model, tile, &
!> & r3dvar, Vname(1,idTvar(itrc)), &
!> & cr, Rcontact(cr)%Npoints, Rcontact, &
!> & LBi, UBi, LBj, UBj, 1, N(dg), &
!> & OCEAN(dg) % tl_t(:,:,:,Tindex,itrc), &
!> & COMPOSITE(cr) % tl_t(:,:,:,itrc))
!>
CALL ad_get_contact3d (dg, model, tile, &
& r3dvar, Vname(1,idTvar(itrc)), &
& cr, Rcontact(cr)%Npoints, Rcontact,&
& LBi, UBi, LBj, UBj, 1, N(dg), &
& OCEAN(dg) % ad_t(:,:,:,Tindex, &
& itrc), &
& COMPOSITE(cr) % ad_t(:,:,:,itrc))
END DO
END IF
# endif
!
! Process 3D momentum (u, v) at the appropriate time-index.
!
IF ((isection.eq.n3dIC).or. &
& (isection.eq.n3duv)) THEN
Tindex=nnew(dg)
!> CALL get_contact3d (dg, model, tile, &
!> & u3dvar, Vname(1,idUvel), &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, 1, N(dg), &
!> & OCEAN(dg) % tl_u(:,:,:,Tindex), &
!> & COMPOSITE(cr) % tl_u)
!>
CALL ad_get_contact3d (dg, model, tile, &
& u3dvar, Vname(1,idUvel), &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, 1, N(dg), &
& OCEAN(dg) % ad_u(:,:,:,Tindex), &
& COMPOSITE(cr) % ad_u)
!> CALL get_contact3d (dg, model, tile, &
!> & v3dvar, Vname(1,idVvel), &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, 1, N(dg), &
!> & OCEAN(dg) % tl_v(:,:,:,Tindex), &
!> & COMPOSITE(cr) % tl_v)
!>
CALL ad_get_contact3d (dg, model, tile, &
& v3dvar, Vname(1,idVvel), &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, 1, N(dg), &
& OCEAN(dg) % ad_v(:,:,:,Tindex), &
& COMPOSITE(cr) % ad_v)
END IF
!
! Process 3D momentum fluxes (Huon, Hvom).
!
IF (isection.eq.n3duv) THEN
!> CALL get_contact3d (dg, model, tile, &
!> & u3dvar, 'Huon', &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, 1, N(dg), &
!> & GRID(dg) % tl_Huon, &
!> & COMPOSITE(cr) % tl_Huon)
!>
CALL ad_get_contact3d (dg, model, tile, &
& u3dvar, 'Huon', &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, 1, N(dg), &
& GRID(dg) % ad_Huon, &
& COMPOSITE(cr) % ad_Huon)
!> CALL get_contact3d (dg, model, tile, &
!> & v3dvar, 'Hvom', &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, 1, N(dg), &
!> & GRID(dg) % tl_Hvom, &
!> & COMPOSITE(cr) % tl_Hvom)
!>
CALL ad_get_contact3d (dg, model, tile, &
& v3dvar, 'Hvom', &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, 1, N(dg), &
& GRID(dg) % ad_Hvom, &
& COMPOSITE(cr) % ad_Hvom)
END IF
# endif
END IF
END DO
RETURN
END SUBROUTINE ad_get_composite
!
SUBROUTINE ad_get_refine (ng, model, tile)
!
!=======================================================================
! !
! This routine gets the donor grid data required to process the !
! contact points of the current refinement grid. It extracts !
! the donor cell points containing each contact point. !
! !
! The extracted data is stored in two-time rolling records which !
! are needed for the space and time interpolation in 'put_refine'. !
! !
! Except for initialization, this routine is called at the bottom !
! of the donor grid time step so all the values are updated for the !
! time(dg)+dt(dg). That is, in 2D applications it is called after !
! "step2d" corrector step and in 3D applications it is called after !
! "step3d_t". This is done to have the coarser grid snapshots at !
! time(dg) and time(dg)+dt(dg) to bound the interpolation of the !
! finer grid contact points. !
! !
! On Input: !
! !
! ng Refinement grid number (integer) !
! model Calling model identifier (integer) !
! tile Domain tile partition (integer) !
! !
! On Output: (mod_nesting) !
! !
! REFINED Updated contact points structure. !
! !
!=======================================================================
!
USE mod_param
USE mod_parallel
USE mod_coupling
USE mod_ncparam
USE mod_nesting
USE mod_ocean
USE mod_scalars
USE mod_stepping
USE nesting_mod, ONLY : get_persisted2d
!
! Imported variable declarations.
!
integer, intent(in) :: ng, model, tile
!
! Local variable declarations.
!
# ifdef NESTING_DEBUG
logical, save :: first = .TRUE.
# endif
integer :: Tindex2d, cr, dg, ir, rg, told, tnew
# ifdef SOLVE3D
integer :: Tindex3d, itrc
# endif
integer :: LBi, UBi, LBj, UBj
!
!-----------------------------------------------------------------------
! Get donor grid data needed to process refinement grid contact points.
! The extracted contact point data is stored in two time records to
! facilitate the space-time interpolation elsewhere.
!-----------------------------------------------------------------------
!
DO cr=1,Ncontact
!
! Get data donor and data receiver grid numbers.
!
dg=Rcontact(cr)%donor_grid
rg=Rcontact(cr)%receiver_grid
!
! Process only contact region data for requested nested grid "ng".
!
IF ((dg.eq.CoarserDonor(rg)).and.(dg.eq.ng)) THEN
!
! Set donor grid lower and upper array indices.
!
LBi=BOUNDS(dg)%LBi(tile)
UBi=BOUNDS(dg)%UBi(tile)
LBj=BOUNDS(dg)%LBj(tile)
UBj=BOUNDS(dg)%UBj(tile)
!
! Update rolling time indices. The contact data is stored in two time
! levels. We need a special case for ROMS initialization in "main2d"
! or "main3d" after the processing "ini_fields". Notice that a dt(dg)
! is added because this routine is called after the end of the time
! step.
!
! tnew=3-RollingIndex(cr)
tnew=RollingIndex(cr)
!
! Set donor grid time index to process. In 3D applications, the 2D
! record index to use can be either 1 or 2 since both ubar(:,:,1:2)
! and vbar(:,:,1:2) are set to its time-averaged values in "step3d_uv".
! That is, we can use Tindex2d=kstp(dg) or Tindex2d=knew(dg). However,
! in 2D applications we need to use Tindex2d=knew(dg).
!
Tindex2d=knew(dg)
# ifdef SOLVE3D
Tindex3d=nnew(dg)
# endif
# ifdef NESTING_DEBUG
!
! If debugging, write information into Fortran unit 102 to check the
! logic of processing donor grid data.
!
IF (DOMAIN(ng)%SouthWest_Test(tile)) THEN
IF (Master) THEN
IF (first) THEN
first=.FALSE.
WRITE (102,10)
END IF
WRITE (102,20) ng, cr, dg, rg, iic(dg), iic(rg), &
& 3-tnew, tnew, Tindex2d, Tindex3d, &
& INT(time(rg)), &
& INT(RollingTime(3-tnew,cr)), &
& INT(time(ng)), &
& INT(RollingTime(tnew,cr))
10 FORMAT (2x,'ng',2x,'cr',2x,'dg',2x,'rg',5x,'iic',5x,'iic',&
& 2x,'told',2x,'tnew',2x,'Tindex',2x,'Tindex', &
& 9x,'time',8x,'time',8x,'time',8x,'time',/, &
& 20x,'(dg)',4x,'(rg)',18x,'2D',6x,'3D',9x,'(rg)', &
& 8x,'told',8x,'(ng)',8x,'tnew',/)
20 FORMAT (4(1x,i3),2(1x,i7),2(2x,i4),2(4x,i4),1x,4(2x,i10))
CALL my_flush (102)
END IF
END IF
# endif
!
! Extract free-surface.
!
# ifdef SOLVE3D
!> CALL get_contact2d (dg, model, tile, &
!> & r2dvar, 'Zt_avg1', &
!> & cr, Rcontact(cr)%Npoints, Rcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & COUPLING(dg) % tl_Zt_avg1, &
!> & REFINED(cr) % tl_zeta(:,:,tnew))
!>
CALL ad_get_contact2d (dg, model, tile, &
& r2dvar, 'Zt_avg1', &
& cr, Rcontact(cr)%Npoints, Rcontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % ad_Zt_avg1, &
& REFINED(cr) % ad_zeta(:,:,tnew))
# else
!> CALL get_contact2d (dg, model, tile, &
!> & r2dvar, 'zeta', &
!> & cr, Rcontact(cr)%Npoints, Rcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_zeta(:,:,Tindex2d), &
!> & REFINED(cr) % tl_zeta(:,:,tnew))
!>
CALL ad_get_contact2d (dg, model, tile, &
& r2dvar, 'zeta', &
& cr, Rcontact(cr)%Npoints, Rcontact, &
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_zeta(:,:,Tindex2d), &
& REFINED(cr) % ad_zeta(:,:,tnew))
# endif
!
! Extract 2D momentum components (ubar, vbar).
!
!> CALL get_contact2d (dg, model, tile, &
!> & u2dvar, Vname(1,idUbar), &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_ubar(:,:,Tindex2d), &
!> & REFINED(cr) % tl_ubar(:,:,tnew))
!>
CALL ad_get_contact2d (dg, model, tile, &
& u2dvar, Vname(1,idUbar), &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_ubar(:,:,Tindex2d), &
& REFINED(cr) % ad_ubar(:,:,tnew))
!> CALL get_contact2d (dg, model, tile, &
!> & v2dvar, Vname(1,idVbar), &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & OCEAN(dg) % tl_vbar(:,:,Tindex2d), &
!> & REFINED(cr) % tl_vbar(:,:,tnew))
!>
CALL ad_get_contact2d (dg, model, tile, &
& v2dvar, Vname(1,idVbar), &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, &
& OCEAN(dg) % ad_vbar(:,:,Tindex2d), &
& REFINED(cr) % ad_vbar(:,:,tnew))
# ifdef SOLVE3D
!
! Extract time-averaged fluxes (DU_avg2, DV_avg2). We will use latter
! only the values at the finer grid physical boundary to impose mass
! flux conservation in routine "put_refine2d".
!
CALL get_persisted2d (dg, rg, model, tile, &
& u2dvar, 'DU_avg2', &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % DU_avg2, &
& REFINED(cr) % DU_avg2(:,:,tnew))
!> CALL get_persisted2d (dg, rg, model, tile, &
!> & u2dvar, 'DU_avg2', &
!> & cr, Ucontact(cr)%Npoints, Ucontact, &
!> & LBi, UBi, LBj, UBj, &
!> & COUPLING(dg) % tl_DU_avg2, &
!> & REFINED(cr) % tl_DU_avg2(:,:,tnew))
!>
CALL ad_get_persisted2d (dg, rg, model, tile, &
& u2dvar, 'DU_avg2', &
& cr, Ucontact(cr)%Npoints, Ucontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % ad_DU_avg2, &
& REFINED(cr) % ad_DU_avg2(:,:,tnew))
CALL get_persisted2d (dg, rg, model, tile, &
& v2dvar, 'DV_avg2', &
& cr, Vcontact(cr)%Npoints, Vcontact, &
& LBi, UBi, LBj, UBj, &
& COUPLING(dg) % DV_avg2, &
& REFINED(cr) % DV_avg2(:,:,tnew))
!> CALL get_persisted2d (dg, rg, model, tile, &
!> & v2dvar, 'DV_avg2', &
!> & cr, Vcontact(cr)%Npoints, Vcontact, &
!> & LBi, UBi, LBj, UBj, &
!> & COUPLING(dg) % tl_DV_avg2, &
!> & REFINED(cr) % tl_DV_avg2(:,:,tnew))
!>
CALL ad_get_persisted2d (dg, rg, model, tile, &