/[MITgcm]/MITgcm/pkg/generic_advdiff/gad_advection.F
ViewVC logotype

Diff of /MITgcm/pkg/generic_advdiff/gad_advection.F

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.7 by adcroft, Fri Sep 28 02:26:57 2001 UTC revision 1.46 by adcroft, Sat Jan 20 21:20:11 2007 UTC
# Line 1  Line 1 
1  C $Header$  C $Header$
2  C $Name$  C $Name$
3    
 CBOI  
 C !TITLE: pkg/generic\_advdiff  
 C !AUTHORS: adcroft@mit.edu  
 C !INTRODUCTION:  
 C \section{Generica Advection Diffusion Package}  
 C  
 C Package "generic\_advdiff" provides a common set of routines for calculating  
 C advective/diffusive fluxes for tracers (cell centered quantities on a C-grid).  
 C  
 C Many different advection schemes are available: the standard centered  
 C second order, centered fourth order and upwind biased third order schemes  
 C are known as linear methods and require some stable time-stepping method  
 C such as Adams-Bashforth. Alternatives such as flux-limited schemes are  
 C stable in the forward sense and are best combined with the multi-dimensional  
 C method provided in gad\_advection.  
 C  
 C There are two high-level routines:  
 C  \begin{itemize}  
 C  \item{GAD\_CALC\_RHS} calculates all fluxes at time level "n" and is used  
 C  for the standard linear schemes. This must be used in conjuction with  
 C  Adams-Bashforth time-stepping. Diffusive and parameterized fluxes are  
 C  always calculated here.  
 C  \item{GAD\_ADVECTION} calculates just the advective fluxes using the  
 C  non-linear schemes and can not be used in conjuction with Adams-Bashforth  
 C  time-stepping.  
 C  \end{itemize}  
 CEOI  
   
4  #include "GAD_OPTIONS.h"  #include "GAD_OPTIONS.h"
5    #undef MULTIDIM_OLD_VERSION
6    
7    C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
8  CBOP  CBOP
9  C !ROUTINE: GAD_ADVECTION  C !ROUTINE: GAD_ADVECTION
10    
11  C !INTERFACE: ==========================================================  C !INTERFACE: ==========================================================
12        SUBROUTINE GAD_ADVECTION(bi,bj,advectionScheme,tracerIdentity,        SUBROUTINE GAD_ADVECTION(
13       U                         Tracer,Gtracer,       I     implicitAdvection, advectionScheme, vertAdvecScheme,
14       I                         myTime,myIter,myThid)       I     tracerIdentity,
15         I     uVel, vVel, wVel, tracer,
16         O     gTracer,
17         I     bi,bj, myTime,myIter,myThid)
18    
19  C !DESCRIPTION:  C !DESCRIPTION:
20  C Calculates the tendancy of a tracer due to advection.  C Calculates the tendency of a tracer due to advection.
21  C It uses the multi-dimensional method given in \ref{sect:multiDimAdvection}  C It uses the multi-dimensional method given in \ref{sect:multiDimAdvection}
22  C and can only be used for the non-linear advection schemes such as the  C and can only be used for the non-linear advection schemes such as the
23  C direct-space-time method and flux-limiters.  C direct-space-time method and flux-limiters.
24  C  C
25  C The algorithm is as follows:  C The algorithm is as follows:
26  C \begin{itemize}  C \begin{itemize}
# Line 56  C      - \Delta t \partial_r (w\theta^{( Line 33  C      - \Delta t \partial_r (w\theta^{(
33  C \item{$G_\theta = ( \theta^{(n+3/3)} - \theta^{(n)} )/\Delta t$}  C \item{$G_\theta = ( \theta^{(n+3/3)} - \theta^{(n)} )/\Delta t$}
34  C \end{itemize}  C \end{itemize}
35  C  C
36  C The tendancy (output) is over-written by this routine.  C The tendency (output) is over-written by this routine.
37    
38  C !USES: ===============================================================  C !USES: ===============================================================
39        IMPLICIT NONE        IMPLICIT NONE
40  #include "SIZE.h"  #include "SIZE.h"
41  #include "EEPARAMS.h"  #include "EEPARAMS.h"
42  #include "PARAMS.h"  #include "PARAMS.h"
 #include "DYNVARS.h"  
43  #include "GRID.h"  #include "GRID.h"
44  #include "GAD.h"  #include "GAD.h"
45  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
46  # include "tamc.h"  # include "tamc.h"
47  # include "tamc_keys.h"  # include "tamc_keys.h"
48    # ifdef ALLOW_PTRACERS
49    #  include "PTRACERS_SIZE.h"
50    # endif
51  #endif  #endif
52    #ifdef ALLOW_EXCH2
53    #include "W2_EXCH2_TOPOLOGY.h"
54    #include "W2_EXCH2_PARAMS.h"
55    #endif /* ALLOW_EXCH2 */
56    
57  C !INPUT PARAMETERS: ===================================================  C !INPUT PARAMETERS: ===================================================
58  C  bi,bj                :: tile indices  C  implicitAdvection :: implicit vertical advection (later on)
59  C  advectionScheme      :: advection scheme to use  C  advectionScheme   :: advection scheme to use (Horizontal plane)
60  C  tracerIdentity       :: identifier for the tracer (required only for OBCS)  C  vertAdvecScheme   :: advection scheme to use (vertical direction)
61  C  Tracer               :: tracer field  C  tracerIdentity    :: tracer identifier (required only for OBCS)
62  C  myTime               :: current time  C  uVel              :: velocity, zonal component
63  C  myIter               :: iteration number  C  vVel              :: velocity, meridional component
64  C  myThid               :: thread number  C  wVel              :: velocity, vertical component
65        INTEGER bi,bj  C  tracer            :: tracer field
66        INTEGER advectionScheme  C  bi,bj             :: tile indices
67    C  myTime            :: current time
68    C  myIter            :: iteration number
69    C  myThid            :: thread number
70          LOGICAL implicitAdvection
71          INTEGER advectionScheme, vertAdvecScheme
72        INTEGER tracerIdentity        INTEGER tracerIdentity
73        _RL Gtracer(1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)        _RL uVel  (1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)
74          _RL vVel  (1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)
75          _RL wVel  (1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)
76          _RL tracer(1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)
77          INTEGER bi,bj
78        _RL myTime        _RL myTime
79        INTEGER myIter        INTEGER myIter
80        INTEGER myThid        INTEGER myThid
81    
82  C !OUTPUT PARAMETERS: ==================================================  C !OUTPUT PARAMETERS: ==================================================
83  C  gTracer              :: tendancy array  C  gTracer           :: tendency array
84        _RL Tracer(1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)        _RL gTracer(1-Olx:sNx+Olx,1-Oly:sNy+Oly,Nr,nSx,nSy)
85    
86  C !LOCAL VARIABLES: ====================================================  C !LOCAL VARIABLES: ====================================================
87  C  maskUp               :: 2-D array for mask at W points  C  maskUp        :: 2-D array for mask at W points
88  C  iMin,iMax,jMin,jMax  :: loop range for called routines  C  maskLocW      :: 2-D array for mask at West points
89  C  i,j,k                :: loop indices  C  maskLocS      :: 2-D array for mask at South points
90  C  kup                  :: index into 2 1/2D array, toggles between 1 and 2  C  iMin,iMax,    :: loop range for called routines
91  C  kdown                :: index into 2 1/2D array, toggles between 2 and 1  C  jMin,jMax     :: loop range for called routines
92  C  kp1                  :: =k+1 for k<Nr, =Nr for k=Nr  C [iMin,iMax]Upd :: loop range to update tracer field
93  C  xA,yA                :: areas of X and Y face of tracer cells  C [jMin,jMax]Upd :: loop range to update tracer field
94  C  uTrans,vTrans,rTrans :: 2-D arrays of volume transports at U,V and W points  C  i,j,k         :: loop indices
95  C  af                   :: 2-D array for horizontal advective flux  C  kUp           :: index into 2 1/2D array, toggles between 1 and 2
96  C  fVerT                :: 2 1/2D arrays for vertical advective flux  C  kDown         :: index into 2 1/2D array, toggles between 2 and 1
97  C  localTij             :: 2-D array used as temporary local copy of tracer fld  C  kp1           :: =k+1 for k<Nr, =Nr for k=Nr
98  C  localTijk            :: 3-D array used as temporary local copy of tracer fld  C  xA,yA         :: areas of X and Y face of tracer cells
99  C  kp1Msk               :: flag (0,1) to act as over-riding mask for W levels  C  uFld,vFld     :: 2-D local copy of horizontal velocity, U,V components
100  C  calc_fluxes_X        :: logical to indicate to calculate fluxes in X dir  C  wFld          :: 2-D local copy of vertical velocity
101  C  calc_fluxes_Y        :: logical to indicate to calculate fluxes in Y dir  C  uTrans,vTrans :: 2-D arrays of volume transports at U,V points
102  C  nipass               :: number of passes to make in multi-dimensional method  C  rTrans        :: 2-D arrays of volume transports at W points
103  C  ipass                :: number of the current pass being made  C  rTransKp1     :: vertical volume transport at interface k+1
104        _RS maskUp  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)  C  af            :: 2-D array for horizontal advective flux
105    C  afx           :: 2-D array for horizontal advective flux, x direction
106    C  afy           :: 2-D array for horizontal advective flux, y direction
107    C  fVerT         :: 2 1/2D arrays for vertical advective flux
108    C  localTij      :: 2-D array, temporary local copy of tracer fld
109    C  localTijk     :: 3-D array, temporary local copy of tracer fld
110    C  kp1Msk        :: flag (0,1) for over-riding mask for W levels
111    C  calc_fluxes_X :: logical to indicate to calculate fluxes in X dir
112    C  calc_fluxes_Y :: logical to indicate to calculate fluxes in Y dir
113    C  interiorOnly  :: only update the interior of myTile, but not the edges
114    C  overlapOnly   :: only update the edges of myTile, but not the interior
115    C  nipass        :: number of passes in multi-dimensional method
116    C  ipass         :: number of the current pass being made
117    C  myTile        :: variables used to determine which cube face
118    C  nCFace        :: owns a tile for cube grid runs using
119    C                :: multi-dim advection.
120    C [N,S,E,W]_edge :: true if N,S,E,W edge of myTile is an Edge of the cube
121    c     _RS maskUp  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
122          _RS maskLocW(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
123          _RS maskLocS(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
124        INTEGER iMin,iMax,jMin,jMax        INTEGER iMin,iMax,jMin,jMax
125        INTEGER i,j,k,kup,kDown,kp1        INTEGER iMinUpd,iMaxUpd,jMinUpd,jMaxUpd
126          INTEGER i,j,k,kUp,kDown
127        _RS xA      (1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RS xA      (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
128        _RS yA      (1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RS yA      (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
129          _RL uFld    (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
130          _RL vFld    (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
131          _RL wFld    (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
132        _RL uTrans  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RL uTrans  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
133        _RL vTrans  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RL vTrans  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
134        _RL rTrans  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RL rTrans  (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
135          _RL rTransKp1(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
136        _RL af      (1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RL af      (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
137          _RL afx     (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
138          _RL afy     (1-OLx:sNx+OLx,1-OLy:sNy+OLy)
139        _RL fVerT   (1-OLx:sNx+OLx,1-OLy:sNy+OLy,2)        _RL fVerT   (1-OLx:sNx+OLx,1-OLy:sNy+OLy,2)
140        _RL localTij(1-OLx:sNx+OLx,1-OLy:sNy+OLy)        _RL localTij(1-OLx:sNx+OLx,1-OLy:sNy+OLy)
141        _RL localTijk(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr)        _RL localTijk(1-OLx:sNx+OLx,1-OLy:sNy+OLy,Nr)
142        _RL kp1Msk        _RL kp1Msk
143        LOGICAL calc_fluxes_X,calc_fluxes_Y        LOGICAL calc_fluxes_X, calc_fluxes_Y, withSigns
144          LOGICAL interiorOnly, overlapOnly
145        INTEGER nipass,ipass        INTEGER nipass,ipass
146          INTEGER nCFace
147          LOGICAL N_edge, S_edge, E_edge, W_edge
148    #ifdef ALLOW_EXCH2
149          INTEGER myTile
150    #endif
151    #ifdef ALLOW_DIAGNOSTICS
152          CHARACTER*8 diagName
153          CHARACTER*4 GAD_DIAG_SUFX, diagSufx
154          EXTERNAL    GAD_DIAG_SUFX
155    #endif
156  CEOP  CEOP
157    
158  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
159              act0 = tracerIdentity - 1
160              max0 = maxpass
161            act1 = bi - myBxLo(myThid)            act1 = bi - myBxLo(myThid)
162            max1 = myBxHi(myThid) - myBxLo(myThid) + 1            max1 = myBxHi(myThid) - myBxLo(myThid) + 1
163            act2 = bj - myByLo(myThid)            act2 = bj - myByLo(myThid)
# Line 134  CEOP Line 165  CEOP
165            act3 = myThid - 1            act3 = myThid - 1
166            max3 = nTx*nTy            max3 = nTx*nTy
167            act4 = ikey_dynamics - 1            act4 = ikey_dynamics - 1
168            ikey = (act1 + 1) + act2*max1            igadkey = (act0 + 1)
169       &                      + act3*max1*max2       &                      + act1*max0
170       &                      + act4*max1*max2*max3       &                      + act2*max0*max1
171         &                      + act3*max0*max1*max2
172         &                      + act4*max0*max1*max2*max3
173              if (tracerIdentity.GT.maxpass) then
174                 print *, 'ph-pass gad_advection ', maxpass, tracerIdentity
175                 STOP 'maxpass seems smaller than tracerIdentity'
176              endif
177  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
178    
179    #ifdef ALLOW_DIAGNOSTICS
180    C--   Set diagnostic suffix for the current tracer
181          IF ( useDiagnostics ) THEN
182            diagSufx = GAD_DIAG_SUFX( tracerIdentity, myThid )
183          ENDIF
184    #endif
185    
186  C--   Set up work arrays with valid (i.e. not NaN) values  C--   Set up work arrays with valid (i.e. not NaN) values
187  C     These inital values do not alter the numerical results. They  C     These inital values do not alter the numerical results. They
188  C     just ensure that all memory references are to valid floating  C     just ensure that all memory references are to valid floating
# Line 153  C     uninitialised but inert locations. Line 197  C     uninitialised but inert locations.
197          rTrans(i,j)  = 0. _d 0          rTrans(i,j)  = 0. _d 0
198          fVerT(i,j,1) = 0. _d 0          fVerT(i,j,1) = 0. _d 0
199          fVerT(i,j,2) = 0. _d 0          fVerT(i,j,2) = 0. _d 0
200            rTransKp1(i,j)= 0. _d 0
201    #ifdef ALLOW_AUTODIFF_TAMC
202            localTij(i,j) = 0. _d 0
203            wfld(i,j)    = 0. _d 0
204    #endif
205         ENDDO         ENDDO
206        ENDDO        ENDDO
207    
208    C--   Set tile-specific parameters for horizontal fluxes
209          IF (useCubedSphereExchange) THEN
210           nipass=3
211    #ifdef ALLOW_AUTODIFF_TAMC
212           IF ( nipass.GT.maxcube ) STOP 'maxcube needs to be = 3'
213    #endif
214    #ifdef ALLOW_EXCH2
215           myTile = W2_myTileList(bi)
216           nCFace = exch2_myFace(myTile)
217           N_edge = exch2_isNedge(myTile).EQ.1
218           S_edge = exch2_isSedge(myTile).EQ.1
219           E_edge = exch2_isEedge(myTile).EQ.1
220           W_edge = exch2_isWedge(myTile).EQ.1
221    #else
222           nCFace = bi
223           N_edge = .TRUE.
224           S_edge = .TRUE.
225           E_edge = .TRUE.
226           W_edge = .TRUE.
227    #endif
228          ELSE
229           nipass=2
230           nCFace = bi
231           N_edge = .FALSE.
232           S_edge = .FALSE.
233           E_edge = .FALSE.
234           W_edge = .FALSE.
235          ENDIF
236    
237        iMin = 1-OLx        iMin = 1-OLx
238        iMax = sNx+OLx        iMax = sNx+OLx
239        jMin = 1-OLy        jMin = 1-OLy
# Line 164  C     uninitialised but inert locations. Line 242  C     uninitialised but inert locations.
242  C--   Start of k loop for horizontal fluxes  C--   Start of k loop for horizontal fluxes
243        DO k=1,Nr        DO k=1,Nr
244  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
245           kkey = (ikey-1)*Nr + k           kkey = (igadkey-1)*Nr + k
246  CADJ STORE tracer(:,:,k,bi,bj) = comlev1_bibj_k, key=kkey, byte=isbyte  CADJ STORE tracer(:,:,k,bi,bj) =
247    CADJ &     comlev1_bibj_k_gad, key=kkey, byte=isbyte
248  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
249    
250  C--   Get temporary terms used by tendency routines  C--   Get temporary terms used by tendency routines
251        CALL CALC_COMMON_FACTORS (        CALL CALC_COMMON_FACTORS (
252       I         bi,bj,iMin,iMax,jMin,jMax,k,       I         uVel, vVel,
253       O         xA,yA,uTrans,vTrans,rTrans,maskUp,       O         uFld, vFld, uTrans, vTrans, xA, yA,
254       I         myThid)       I         k,bi,bj, myThid )
255    
256    #ifdef ALLOW_GMREDI
257    C--   Residual transp = Bolus transp + Eulerian transp
258          IF (useGMRedi)
259         &   CALL GMREDI_CALC_UVFLOW(
260         U                  uFld, vFld, uTrans, vTrans,
261         I                  k, bi, bj, myThid )
262    #endif /* ALLOW_GMREDI */
263    
264  C--   Make local copy of tracer array  C--   Make local copy of tracer array and mask West & South
265        DO j=1-OLy,sNy+OLy        DO j=1-OLy,sNy+OLy
266         DO i=1-OLx,sNx+OLx         DO i=1-OLx,sNx+OLx
267          localTij(i,j)=tracer(i,j,k,bi,bj)           localTij(i,j)=tracer(i,j,k,bi,bj)
268             maskLocW(i,j)=maskW(i,j,k,bi,bj)
269             maskLocS(i,j)=maskS(i,j,k,bi,bj)
270         ENDDO         ENDDO
271        ENDDO        ENDDO
272    
273    #ifndef ALLOW_AUTODIFF_TAMC
274        IF (useCubedSphereExchange) THEN        IF (useCubedSphereExchange) THEN
275         nipass=3          withSigns = .FALSE.
276        ELSE          CALL FILL_CS_CORNER_UV_RS(
277         nipass=1       &            withSigns, maskLocW,maskLocS, bi,bj, myThid )
278        ENDIF        ENDIF
279  cph       nipass=1  #endif
280    
281  C--   Multiple passes for different directions on different tiles  C--   Multiple passes for different directions on different tiles
282    C--   For cube need one pass for each of red, green and blue axes.
283        DO ipass=1,nipass        DO ipass=1,nipass
284  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
285           passkey = ipass + (k-1)   *maxpass           passkey = ipass + (k-1)      *maxcube
286       &                   + (ikey-1)*maxpass*Nr       &                   + (igadkey-1)*maxcube*Nr
287           IF (nipass .GT. maxpass) THEN           IF (nipass .GT. maxpass) THEN
288            STOP 'GAD_ADVECTION: nipass > maxpass. check tamc.h'            STOP 'GAD_ADVECTION: nipass > maxcube. check tamc.h'
289           ENDIF           ENDIF
290  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
291    
292        IF (nipass.EQ.3) THEN        interiorOnly = .FALSE.
293         calc_fluxes_X=.FALSE.        overlapOnly  = .FALSE.
294         calc_fluxes_Y=.FALSE.        IF (useCubedSphereExchange) THEN
295         IF (ipass.EQ.1 .AND. (bi.EQ.1 .OR. bi.EQ.2) ) THEN  #ifdef MULTIDIM_OLD_VERSION
296          calc_fluxes_X=.TRUE.  C-    CubedSphere : pass 3 times, with full update of local tracer field
297         ELSEIF (ipass.EQ.1 .AND. (bi.EQ.4 .OR. bi.EQ.5) ) THEN         IF (ipass.EQ.1) THEN
298          calc_fluxes_Y=.TRUE.          calc_fluxes_X = nCFace.EQ.1 .OR. nCFace.EQ.2
299         ELSEIF (ipass.EQ.2 .AND. (bi.EQ.1 .OR. bi.EQ.6) ) THEN          calc_fluxes_Y = nCFace.EQ.4 .OR. nCFace.EQ.5
300          calc_fluxes_Y=.TRUE.         ELSEIF (ipass.EQ.2) THEN
301         ELSEIF (ipass.EQ.2 .AND. (bi.EQ.3 .OR. bi.EQ.4) ) THEN          calc_fluxes_X = nCFace.EQ.3 .OR. nCFace.EQ.4
302          calc_fluxes_X=.TRUE.          calc_fluxes_Y = nCFace.EQ.6 .OR. nCFace.EQ.1
303         ELSEIF (ipass.EQ.3 .AND. (bi.EQ.2 .OR. bi.EQ.3) ) THEN  #else /* MULTIDIM_OLD_VERSION */
304          calc_fluxes_Y=.TRUE.  C-    CubedSphere : pass 3 times, with partial update of local tracer field
305         ELSEIF (ipass.EQ.3 .AND. (bi.EQ.5 .OR. bi.EQ.6) ) THEN         IF (ipass.EQ.1) THEN
306          calc_fluxes_X=.TRUE.          overlapOnly  = MOD(nCFace,3).EQ.0
307            interiorOnly = MOD(nCFace,3).NE.0
308            calc_fluxes_X = nCFace.EQ.6 .OR. nCFace.EQ.1 .OR. nCFace.EQ.2
309            calc_fluxes_Y = nCFace.EQ.3 .OR. nCFace.EQ.4 .OR. nCFace.EQ.5
310           ELSEIF (ipass.EQ.2) THEN
311            overlapOnly  = MOD(nCFace,3).EQ.2
312            calc_fluxes_X = nCFace.EQ.2 .OR. nCFace.EQ.3 .OR. nCFace.EQ.4
313            calc_fluxes_Y = nCFace.EQ.5 .OR. nCFace.EQ.6 .OR. nCFace.EQ.1
314    #endif /* MULTIDIM_OLD_VERSION */
315           ELSE
316            calc_fluxes_X = nCFace.EQ.5 .OR. nCFace.EQ.6
317            calc_fluxes_Y = nCFace.EQ.2 .OR. nCFace.EQ.3
318         ENDIF         ENDIF
319        ELSE        ELSE
320         calc_fluxes_X=.TRUE.  C-    not CubedSphere
321         calc_fluxes_Y=.TRUE.          calc_fluxes_X = MOD(ipass,2).EQ.1
322            calc_fluxes_Y = .NOT.calc_fluxes_X
323        ENDIF        ENDIF
   
 C--   X direction  
       IF (calc_fluxes_X) THEN  
324    
325  C--   Internal exchange for calculations in X  C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
326        IF (useCubedSphereExchange) THEN  C--   X direction
327         DO j=1,Oly  C-     Advective flux in X
328          DO i=1,Olx          DO j=1-Oly,sNy+Oly
329           localTij( 1-i , 1-j )=localTij( 1-j ,    i    )           DO i=1-Olx,sNx+Olx
330           localTij( 1-i ,sNy+j)=localTij( 1-j , sNy+1-i )            af(i,j) = 0.
331           localTij(sNx+i, 1-j )=localTij(sNx+j,    i    )           ENDDO
          localTij(sNx+i,sNy+j)=localTij(sNx+j, sNy+1-i )  
332          ENDDO          ENDDO
333         ENDDO  C
334        ENDIF  #ifdef ALLOW_AUTODIFF_TAMC
335    # ifndef DISABLE_MULTIDIM_ADVECTION
336    CADJ STORE localTij(:,:)  =
337    CADJ &     comlev1_bibj_k_gad_pass, key=passkey, byte=isbyte
338    CADJ STORE af(:,:)  =
339    CADJ &     comlev1_bibj_k_gad_pass, key=passkey, byte=isbyte
340    # endif
341    #endif /* ALLOW_AUTODIFF_TAMC */
342    C
343          IF (calc_fluxes_X) THEN
344    
345  C-    Advective flux in X  C-     Do not compute fluxes if
346        DO j=1-Oly,sNy+Oly  C       a) needed in overlap only
347         DO i=1-Olx,sNx+Olx  C   and b) the overlap of myTile are not cube-face Edges
348          af(i,j) = 0.         IF ( .NOT.overlapOnly .OR. N_edge .OR. S_edge ) THEN
349         ENDDO  
350        ENDDO  #ifndef ALLOW_AUTODIFF_TAMC
351    C-     Internal exchange for calculations in X
352    #ifdef MULTIDIM_OLD_VERSION
353            IF ( useCubedSphereExchange ) THEN
354    #else
355            IF ( useCubedSphereExchange .AND.
356         &       ( overlapOnly .OR. ipass.EQ.1 ) ) THEN
357    #endif
358             CALL FILL_CS_CORNER_TR_RL( .TRUE., localTij, bi,bj, myThid )
359            ENDIF
360    #endif
361    
362  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
363  #ifndef DISABLE_MULTIDIM_ADVECTION  # ifndef DISABLE_MULTIDIM_ADVECTION
364  CADJ STORE localTij(:,:)  = comlev1_bibj_pass, key=passkey, byte=isbyte  CADJ STORE localTij(:,:)  =
365  #endif  CADJ &     comlev1_bibj_k_gad_pass, key=passkey, byte=isbyte
366    # endif
367  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
368    
369        IF (advectionScheme.EQ.ENUM_FLUX_LIMIT) THEN          IF ( advectionScheme.EQ.ENUM_UPWIND_1RST
370         CALL GAD_FLUXLIMIT_ADV_X(       &     .OR. advectionScheme.EQ.ENUM_DST2 ) THEN
371       &      bi,bj,k,deltaTtracer,uTrans,uVel,localTij,af,myThid)            CALL GAD_DST2U1_ADV_X( bi,bj,k, advectionScheme,
372        ELSEIF (advectionScheme.EQ.ENUM_DST3 ) THEN       I                           dTtracerLev(k),uTrans,uFld,localTij,
373         CALL GAD_DST3_ADV_X(       O                           af, myThid )
374       &       bi,bj,k,deltaTtracer,uTrans,uVel,localTij,af,myThid)          ELSEIF (advectionScheme.EQ.ENUM_FLUX_LIMIT) THEN
375        ELSEIF (advectionScheme.EQ.ENUM_DST3_FLUX_LIMIT ) THEN            CALL GAD_FLUXLIMIT_ADV_X( bi,bj,k, dTtracerLev(k),
376         CALL GAD_DST3FL_ADV_X(       I                              uTrans, uFld, maskLocW, localTij,
377       &       bi,bj,k,deltaTtracer,uTrans,uVel,localTij,af,myThid)       O                              af, myThid )
378        ELSE          ELSEIF (advectionScheme.EQ.ENUM_DST3 ) THEN
379         STOP 'GAD_ADVECTION: adv. scheme incompatibale with mutli-dim'            CALL GAD_DST3_ADV_X(      bi,bj,k, dTtracerLev(k),
380        ENDIF       I                              uTrans, uFld, maskLocW, localTij,
381         O                              af, myThid )
382            ELSEIF (advectionScheme.EQ.ENUM_DST3_FLUX_LIMIT ) THEN
383              CALL GAD_DST3FL_ADV_X(    bi,bj,k, dTtracerLev(k),
384         I                              uTrans, uFld, maskLocW, localTij,
385         O                              af, myThid )
386            ELSEIF (advectionScheme.EQ.ENUM_OS7MP ) THEN
387              CALL GAD_OS7MP_ADV_X(     bi,bj,k, dTtracerLev(k),
388         I                              uTrans, uFld, maskLocW, localTij,
389         O                              af, myThid )
390            ELSE
391             STOP 'GAD_ADVECTION: adv. scheme incompatibale with multi-dim'
392            ENDIF
393    
394        DO j=1-Oly,sNy+Oly  C-     Advective flux in X : done
395         DO i=1-Olx,sNx+Olx-1         ENDIF
396          localTij(i,j)=localTij(i,j)-deltaTtracer*  
397       &    _recip_hFacC(i,j,k,bi,bj)*recip_drF(k)  #ifndef ALLOW_AUTODIFF_TAMC
398       &    *recip_rA(i,j,bi,bj)  C-     Internal exchange for next calculations in Y
399       &    *( af(i+1,j)-af(i,j)         IF ( overlapOnly .AND. ipass.EQ.1 ) THEN
400       &      -tracer(i,j,k,bi,bj)*(uTrans(i+1,j)-uTrans(i,j))           CALL FILL_CS_CORNER_TR_RL(.FALSE., localTij, bi,bj, myThid )
401       &     )         ENDIF
402         ENDDO  #endif
403        ENDDO  
404    C-     Update the local tracer field where needed:
405    
406    C      update in overlap-Only
407           IF ( overlapOnly ) THEN
408            iMinUpd = 1-Olx+1
409            iMaxUpd = sNx+Olx-1
410    C- notes: these 2 lines below have no real effect (because recip_hFac=0
411    C         in corner region) but safer to keep them.
412            IF ( W_edge ) iMinUpd = 1
413            IF ( E_edge ) iMaxUpd = sNx
414    
415            IF ( S_edge ) THEN
416             DO j=1-Oly,0
417              DO i=iMinUpd,iMaxUpd
418               localTij(i,j) = localTij(i,j)
419         &      -dTtracerLev(k)*recip_rhoFacC(k)
420         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
421         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
422         &       *( af(i+1,j)-af(i,j)
423         &         -tracer(i,j,k,bi,bj)*(uTrans(i+1,j)-uTrans(i,j))
424         &        )
425              ENDDO
426             ENDDO
427            ENDIF
428            IF ( N_edge ) THEN
429             DO j=sNy+1,sNy+Oly
430              DO i=iMinUpd,iMaxUpd
431               localTij(i,j) = localTij(i,j)
432         &      -dTtracerLev(k)*recip_rhoFacC(k)
433         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
434         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
435         &       *( af(i+1,j)-af(i,j)
436         &         -tracer(i,j,k,bi,bj)*(uTrans(i+1,j)-uTrans(i,j))
437         &        )
438              ENDDO
439             ENDDO
440            ENDIF
441    
442           ELSE
443    C      do not only update the overlap
444            jMinUpd = 1-Oly
445            jMaxUpd = sNy+Oly
446            IF ( interiorOnly .AND. S_edge ) jMinUpd = 1
447            IF ( interiorOnly .AND. N_edge ) jMaxUpd = sNy
448            DO j=jMinUpd,jMaxUpd
449             DO i=1-Olx+1,sNx+Olx-1
450               localTij(i,j) = localTij(i,j)
451         &      -dTtracerLev(k)*recip_rhoFacC(k)
452         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
453         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
454         &       *( af(i+1,j)-af(i,j)
455         &         -tracer(i,j,k,bi,bj)*(uTrans(i+1,j)-uTrans(i,j))
456         &        )
457             ENDDO
458            ENDDO
459    C-      keep advective flux (for diagnostics)
460            DO j=1-Oly,sNy+Oly
461             DO i=1-Olx,sNx+Olx
462              afx(i,j) = af(i,j)
463             ENDDO
464            ENDDO
465    
466  #ifdef ALLOW_OBCS  #ifdef ALLOW_OBCS
467  C--   Apply open boundary conditions  C-     Apply open boundary conditions
468        IF (useOBCS) THEN          IF ( useOBCS ) THEN
469         IF (tracerIdentity.EQ.GAD_TEMPERATURE) THEN           IF (tracerIdentity.EQ.GAD_TEMPERATURE) THEN
470          CALL OBCS_APPLY_TLOC( bi, bj, k, localTij, myThid )            CALL OBCS_APPLY_TLOC( bi, bj, k, localTij, myThid )
471         ELSEIF (tracerIdentity.EQ.GAD_SALINITY) THEN           ELSEIF (tracerIdentity.EQ.GAD_SALINITY) THEN
472          CALL OBCS_APPLY_SLOC( bi, bj, k, localTij, myThid )            CALL OBCS_APPLY_SLOC( bi, bj, k, localTij, myThid )
473         END IF  #ifdef ALLOW_PTRACERS
474        END IF           ELSEIF (tracerIdentity.GE.GAD_TR1) THEN
475              CALL OBCS_APPLY_PTRACER( bi, bj, k,
476         &         tracerIdentity-GAD_TR1+1, localTij, myThid )
477    #endif /* ALLOW_PTRACERS */
478             ENDIF
479            ENDIF
480  #endif /* ALLOW_OBCS */  #endif /* ALLOW_OBCS */
481    
482    C-     end if/else update overlap-Only
483           ENDIF
484    
485  C--   End of X direction  C--   End of X direction
486        ENDIF        ENDIF
487    
488    C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
489  C--   Y direction  C--   Y direction
490    cph-test
491    C-     Advective flux in Y
492            DO j=1-Oly,sNy+Oly
493             DO i=1-Olx,sNx+Olx
494              af(i,j) = 0.
495             ENDDO
496            ENDDO
497    C
498    #ifdef ALLOW_AUTODIFF_TAMC
499    # ifndef DISABLE_MULTIDIM_ADVECTION
500    CADJ STORE localTij(:,:)  =
501    CADJ &     comlev1_bibj_k_gad_pass, key=passkey, byte=isbyte
502    CADJ STORE af(:,:)  =
503    CADJ &     comlev1_bibj_k_gad_pass, key=passkey, byte=isbyte
504    # endif
505    #endif /* ALLOW_AUTODIFF_TAMC */
506    C
507        IF (calc_fluxes_Y) THEN        IF (calc_fluxes_Y) THEN
508    
509  C--   Internal exchange for calculations in Y  C-     Do not compute fluxes if
510        IF (useCubedSphereExchange) THEN  C       a) needed in overlap only
511         DO j=1,Oly  C   and b) the overlap of myTile are not cube-face edges
512          DO i=1,Olx         IF ( .NOT.overlapOnly .OR. E_edge .OR. W_edge ) THEN
513           localTij( 1-i , 1-j )=localTij(   j   , 1-i )  
514           localTij( 1-i ,sNy+j)=localTij(   j   ,sNy+i)  #ifndef ALLOW_AUTODIFF_TAMC
515           localTij(sNx+i, 1-j )=localTij(sNx+1-j, 1-i )  C-     Internal exchange for calculations in Y
516           localTij(sNx+i,sNy+j)=localTij(sNx+1-j,sNy+i)  #ifdef MULTIDIM_OLD_VERSION
517          ENDDO          IF ( useCubedSphereExchange ) THEN
518         ENDDO  #else
519        ENDIF          IF ( useCubedSphereExchange .AND.
520         &       ( overlapOnly .OR. ipass.EQ.1 ) ) THEN
521    #endif
522             CALL FILL_CS_CORNER_TR_RL(.FALSE., localTij, bi,bj, myThid )
523            ENDIF
524    #endif
525    
526  C-    Advective flux in Y  C-     Advective flux in Y
527        DO j=1-Oly,sNy+Oly          DO j=1-Oly,sNy+Oly
528         DO i=1-Olx,sNx+Olx           DO i=1-Olx,sNx+Olx
529          af(i,j) = 0.            af(i,j) = 0.
530         ENDDO           ENDDO
531        ENDDO          ENDDO
532    
533  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
534  #ifndef DISABLE_MULTIDIM_ADVECTION  #ifndef DISABLE_MULTIDIM_ADVECTION
535  CADJ STORE localTij(:,:)  = comlev1_bibj_pass, key=passkey, byte=isbyte  CADJ STORE localTij(:,:)  =
536    CADJ &     comlev1_bibj_k_gad_pass, key=passkey, byte=isbyte
537  #endif  #endif
538  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
539    
540        IF (advectionScheme.EQ.ENUM_FLUX_LIMIT) THEN          IF ( advectionScheme.EQ.ENUM_UPWIND_1RST
541         CALL GAD_FLUXLIMIT_ADV_Y(       &     .OR. advectionScheme.EQ.ENUM_DST2 ) THEN
542       &       bi,bj,k,deltaTtracer,vTrans,vVel,localTij,af,myThid)            CALL GAD_DST2U1_ADV_Y( bi,bj,k, advectionScheme,
543        ELSEIF (advectionScheme.EQ.ENUM_DST3 ) THEN       I                           dTtracerLev(k),vTrans,vFld,localTij,
544         CALL GAD_DST3_ADV_Y(       O                           af, myThid )
545       &       bi,bj,k,deltaTtracer,vTrans,vVel,localTij,af,myThid)          ELSEIF (advectionScheme.EQ.ENUM_FLUX_LIMIT) THEN
546        ELSEIF (advectionScheme.EQ.ENUM_DST3_FLUX_LIMIT ) THEN            CALL GAD_FLUXLIMIT_ADV_Y( bi,bj,k, dTtracerLev(k),
547         CALL GAD_DST3FL_ADV_Y(       I                              vTrans, vFld, maskLocS, localTij,
548       &       bi,bj,k,deltaTtracer,vTrans,vVel,localTij,af,myThid)       O                              af, myThid )
549        ELSE          ELSEIF (advectionScheme.EQ.ENUM_DST3 ) THEN
550         STOP 'GAD_ADVECTION: adv. scheme incompatibale with mutli-dim'            CALL GAD_DST3_ADV_Y(      bi,bj,k, dTtracerLev(k),
551        ENDIF       I                              vTrans, vFld, maskLocS, localTij,
552         O                              af, myThid )
553            ELSEIF (advectionScheme.EQ.ENUM_DST3_FLUX_LIMIT ) THEN
554              CALL GAD_DST3FL_ADV_Y(    bi,bj,k, dTtracerLev(k),
555         I                              vTrans, vFld, maskLocS, localTij,
556         O                              af, myThid )
557            ELSEIF (advectionScheme.EQ.ENUM_OS7MP ) THEN
558              CALL GAD_OS7MP_ADV_Y(     bi,bj,k, dTtracerLev(k),
559         I                              vTrans, vFld, maskLocS, localTij,
560         O                              af, myThid )
561            ELSE
562             STOP 'GAD_ADVECTION: adv. scheme incompatibale with mutli-dim'
563            ENDIF
564    
565        DO j=1-Oly,sNy+Oly-1  C-     Advective flux in Y : done
566         DO i=1-Olx,sNx+Olx         ENDIF
567          localTij(i,j)=localTij(i,j)-deltaTtracer*  
568       &    _recip_hFacC(i,j,k,bi,bj)*recip_drF(k)  #ifndef ALLOW_AUTODIFF_TAMC
569       &    *recip_rA(i,j,bi,bj)  C-     Internal exchange for next calculations in X
570       &    *( af(i,j+1)-af(i,j)         IF ( overlapOnly .AND. ipass.EQ.1 ) THEN
571       &      -tracer(i,j,k,bi,bj)*(vTrans(i,j+1)-vTrans(i,j))           CALL FILL_CS_CORNER_TR_RL( .TRUE., localTij, bi,bj, myThid )
572       &     )         ENDIF
573         ENDDO  #endif
574        ENDDO  
575    C-     Update the local tracer field where needed:
576    
577    C      update in overlap-Only
578           IF ( overlapOnly ) THEN
579            jMinUpd = 1-Oly+1
580            jMaxUpd = sNy+Oly-1
581    C- notes: these 2 lines below have no real effect (because recip_hFac=0
582    C         in corner region) but safer to keep them.
583            IF ( S_edge ) jMinUpd = 1
584            IF ( N_edge ) jMaxUpd = sNy
585    
586            IF ( W_edge ) THEN
587             DO j=jMinUpd,jMaxUpd
588              DO i=1-Olx,0
589               localTij(i,j) = localTij(i,j)
590         &      -dTtracerLev(k)*recip_rhoFacC(k)
591         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
592         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
593         &       *( af(i,j+1)-af(i,j)
594         &         -tracer(i,j,k,bi,bj)*(vTrans(i,j+1)-vTrans(i,j))
595         &        )
596              ENDDO
597             ENDDO
598            ENDIF
599            IF ( E_edge ) THEN
600             DO j=jMinUpd,jMaxUpd
601              DO i=sNx+1,sNx+Olx
602               localTij(i,j) = localTij(i,j)
603         &      -dTtracerLev(k)*recip_rhoFacC(k)
604         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
605         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
606         &       *( af(i,j+1)-af(i,j)
607         &         -tracer(i,j,k,bi,bj)*(vTrans(i,j+1)-vTrans(i,j))
608         &        )
609              ENDDO
610             ENDDO
611            ENDIF
612    
613           ELSE
614    C      do not only update the overlap
615            iMinUpd = 1-Olx
616            iMaxUpd = sNx+Olx
617            IF ( interiorOnly .AND. W_edge ) iMinUpd = 1
618            IF ( interiorOnly .AND. E_edge ) iMaxUpd = sNx
619            DO j=1-Oly+1,sNy+Oly-1
620             DO i=iMinUpd,iMaxUpd
621               localTij(i,j) = localTij(i,j)
622         &      -dTtracerLev(k)*recip_rhoFacC(k)
623         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
624         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
625         &       *( af(i,j+1)-af(i,j)
626         &         -tracer(i,j,k,bi,bj)*(vTrans(i,j+1)-vTrans(i,j))
627         &        )
628             ENDDO
629            ENDDO
630    C-      keep advective flux (for diagnostics)
631            DO j=1-Oly,sNy+Oly
632             DO i=1-Olx,sNx+Olx
633              afy(i,j) = af(i,j)
634             ENDDO
635            ENDDO
636    
637  #ifdef ALLOW_OBCS  #ifdef ALLOW_OBCS
638  C--   Apply open boundary conditions  C-     Apply open boundary conditions
639        IF (useOBCS) THEN          IF (useOBCS) THEN
640         IF (tracerIdentity.EQ.GAD_TEMPERATURE) THEN           IF (tracerIdentity.EQ.GAD_TEMPERATURE) THEN
641          CALL OBCS_APPLY_TLOC( bi, bj, k, localTij, myThid )            CALL OBCS_APPLY_TLOC( bi, bj, k, localTij, myThid )
642         ELSEIF (tracerIdentity.EQ.GAD_SALINITY) THEN           ELSEIF (tracerIdentity.EQ.GAD_SALINITY) THEN
643          CALL OBCS_APPLY_SLOC( bi, bj, k, localTij, myThid )            CALL OBCS_APPLY_SLOC( bi, bj, k, localTij, myThid )
644         END IF  #ifdef ALLOW_PTRACERS
645        END IF           ELSEIF (tracerIdentity.GE.GAD_TR1) THEN
646              CALL OBCS_APPLY_PTRACER( bi, bj, k,
647         &         tracerIdentity-GAD_TR1+1, localTij, myThid )
648    #endif /* ALLOW_PTRACERS */
649             ENDIF
650            ENDIF
651  #endif /* ALLOW_OBCS */  #endif /* ALLOW_OBCS */
652    
653    C      end if/else update overlap-Only
654           ENDIF
655    
656  C--   End of Y direction  C--   End of Y direction
657        ENDIF        ENDIF
658    
       DO j=1-Oly,sNy+Oly  
        DO i=1-Olx,sNx+Olx  
         localTijk(i,j,k)=localTij(i,j)  
        ENDDO  
       ENDDO  
   
659  C--   End of ipass loop  C--   End of ipass loop
660        ENDDO        ENDDO
661    
662          IF ( implicitAdvection ) THEN
663    C-    explicit advection is done ; store tendency in gTracer:
664            DO j=1-Oly,sNy+Oly
665             DO i=1-Olx,sNx+Olx
666              gTracer(i,j,k,bi,bj)=
667         &     (localTij(i,j)-tracer(i,j,k,bi,bj))/dTtracerLev(k)
668             ENDDO
669            ENDDO
670          ELSE
671    C-    horizontal advection done; store intermediate result in 3D array:
672            DO j=1-Oly,sNy+Oly
673             DO i=1-Olx,sNx+Olx
674              localTijk(i,j,k)=localTij(i,j)
675             ENDDO
676            ENDDO
677          ENDIF
678    
679    #ifdef ALLOW_DIAGNOSTICS
680            IF ( useDiagnostics ) THEN
681              diagName = 'ADVx'//diagSufx
682              CALL DIAGNOSTICS_FILL(afx,diagName, k,1, 2,bi,bj, myThid)
683              diagName = 'ADVy'//diagSufx
684              CALL DIAGNOSTICS_FILL(afy,diagName, k,1, 2,bi,bj, myThid)
685            ENDIF
686    #endif
687    
688    #ifdef ALLOW_DEBUG
689          IF ( debugLevel .GE. debLevB
690         &   .AND. tracerIdentity.EQ.GAD_TEMPERATURE
691         &   .AND. k.LE.3 .AND. myIter.EQ.1+nIter0
692         &   .AND. nPx.EQ.1 .AND. nPy.EQ.1
693         &   .AND. useCubedSphereExchange ) THEN
694            CALL DEBUG_CS_CORNER_UV( ' afx,afy from GAD_ADVECTION',
695         &             afx,afy, k, standardMessageUnit,bi,bj,myThid )
696          ENDIF
697    #endif /* ALLOW_DEBUG */
698    
699  C--   End of K loop for horizontal fluxes  C--   End of K loop for horizontal fluxes
700        ENDDO        ENDDO
701    
702    C---+----1----+----2----+----3----+----4----+----5----+----6----+----7-|--+----|
703    
704          IF ( .NOT.implicitAdvection ) THEN
705  C--   Start of k loop for vertical flux  C--   Start of k loop for vertical flux
706        DO k=Nr,1,-1         DO k=Nr,1,-1
707  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
708           kkey = (ikey-1)*Nr + k           kkey = (igadkey-1)*Nr + k
709  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
710    C--   kUp    Cycles through 1,2 to point to w-layer above
 C--   kup    Cycles through 1,2 to point to w-layer above  
711  C--   kDown  Cycles through 2,1 to point to w-layer below  C--   kDown  Cycles through 2,1 to point to w-layer below
712        kup  = 1+MOD(k+1,2)          kUp  = 1+MOD(k+1,2)
713        kDown= 1+MOD(k,2)          kDown= 1+MOD(k,2)
714    c       kp1=min(Nr,k+1)
715            kp1Msk=1.
716            if (k.EQ.Nr) kp1Msk=0.
717    
718    C-- Compute Vertical transport
719    #ifdef ALLOW_AIM
720    C- a hack to prevent Water-Vapor vert.transport into the stratospheric level Nr
721            IF ( k.EQ.1 .OR.
722         &     (useAIM .AND. tracerIdentity.EQ.GAD_SALINITY .AND. k.EQ.Nr)
723         &              ) THEN
724    #else
725            IF ( k.EQ.1 ) THEN
726    #endif
727    
728  C--   Get temporary terms used by tendency routines  #ifdef ALLOW_AUTODIFF_TAMC
729        CALL CALC_COMMON_FACTORS (  CADJ STORE rtrans(:,:)  =
730       I         bi,bj,iMin,iMax,jMin,jMax,k,  CADJ &     comlev1_bibj_k_gad, key=kkey, byte=isbyte
731       O         xA,yA,uTrans,vTrans,rTrans,maskUp,  CADJ STORE wfld(:,:)  =
732       I         myThid)  CADJ &     comlev1_bibj_k_gad, key=kkey, byte=isbyte
733    #endif /* ALLOW_AUTODIFF_TAMC */
734  C-    Advective flux in R  
735        DO j=1-Oly,sNy+Oly  C- Surface interface :
736         DO i=1-Olx,sNx+Olx           DO j=1-Oly,sNy+Oly
737          af(i,j) = 0.            DO i=1-Olx,sNx+Olx
738         ENDDO             rTransKp1(i,j) = kp1Msk*rTrans(i,j)
739        ENDDO             wFld(i,j)   = 0.
740               rTrans(i,j) = 0.
741               fVerT(i,j,kUp) = 0.
742              ENDDO
743             ENDDO
744    
745            ELSE
746    
747    #ifdef ALLOW_AUTODIFF_TAMC
748    CADJ STORE rtrans(:,:)  =
749    CADJ &     comlev1_bibj_k_gad, key=kkey, byte=isbyte
750    CADJ STORE wfld(:,:)  =
751    CADJ &     comlev1_bibj_k_gad, key=kkey, byte=isbyte
752    #endif /* ALLOW_AUTODIFF_TAMC */
753    
754    C- Interior interface :
755             DO j=1-Oly,sNy+Oly
756              DO i=1-Olx,sNx+Olx
757               rTransKp1(i,j) = kp1Msk*rTrans(i,j)
758               wFld(i,j)   = wVel(i,j,k,bi,bj)
759               rTrans(i,j) = wVel(i,j,k,bi,bj)*rA(i,j,bi,bj)
760         &                 *deepFac2F(k)*rhoFacF(k)
761         &                 *maskC(i,j,k-1,bi,bj)
762               fVerT(i,j,kUp) = 0.
763              ENDDO
764             ENDDO
765    
766    #ifdef ALLOW_GMREDI
767    C--   Residual transp = Bolus transp + Eulerian transp
768             IF (useGMRedi)
769         &     CALL GMREDI_CALC_WFLOW(
770         U                 wFld, rTrans,
771         I                 k, bi, bj, myThid )
772    #endif /* ALLOW_GMREDI */
773    
774  #ifdef ALLOW_AUTODIFF_TAMC  #ifdef ALLOW_AUTODIFF_TAMC
775  CADJ STORE localTijk(:,:,k)    CADJ STORE localTijk(:,:,k)  
776  CADJ &     = comlev1_bibj_k, key=kkey, byte=isbyte  CADJ &     = comlev1_bibj_k_gad, key=kkey, byte=isbyte
777    CADJ STORE rTrans(:,:)  
778    CADJ &     = comlev1_bibj_k_gad, key=kkey, byte=isbyte
779  #endif /* ALLOW_AUTODIFF_TAMC */  #endif /* ALLOW_AUTODIFF_TAMC */
780    
 C     Note: wVel needs to be masked  
       IF (K.GE.2) THEN  
781  C-    Compute vertical advective flux in the interior:  C-    Compute vertical advective flux in the interior:
782         IF (advectionScheme.EQ.ENUM_FLUX_LIMIT) THEN           IF ( vertAdvecScheme.EQ.ENUM_UPWIND_1RST
783          CALL GAD_FLUXLIMIT_ADV_R(       &      .OR. vertAdvecScheme.EQ.ENUM_DST2 ) THEN
784       &       bi,bj,k,deltaTtracer,rTrans,wVel,localTijk,af,myThid)             CALL GAD_DST2U1_ADV_R( bi,bj,k, advectionScheme,
785         ELSEIF (advectionScheme.EQ.ENUM_DST3 ) THEN       I                            dTtracerLev(k),rTrans,wFld,localTijk,
786          CALL GAD_DST3_ADV_R(       O                            fVerT(1-Olx,1-Oly,kUp), myThid )
787       &       bi,bj,k,deltaTtracer,rTrans,wVel,localTijk,af,myThid)           ELSEIF( vertAdvecScheme.EQ.ENUM_FLUX_LIMIT) THEN
788         ELSEIF (advectionScheme.EQ.ENUM_DST3_FLUX_LIMIT ) THEN             CALL GAD_FLUXLIMIT_ADV_R( bi,bj,k, dTtracerLev(k),
789          CALL GAD_DST3FL_ADV_R(       I                               rTrans, wFld, localTijk,
790       &       bi,bj,k,deltaTtracer,rTrans,wVel,localTijk,af,myThid)       O                               fVerT(1-Olx,1-Oly,kUp), myThid )
791         ELSE           ELSEIF( vertAdvecScheme.EQ.ENUM_DST3 ) THEN
792          STOP 'GAD_ADVECTION: adv. scheme incompatibale with mutli-dim'             CALL GAD_DST3_ADV_R(      bi,bj,k, dTtracerLev(k),
793         ENDIF       I                               rTrans, wFld, localTijk,
794  C-    Surface "correction" term at k>1 :       O                               fVerT(1-Olx,1-Oly,kUp), myThid )
795         DO j=1-Oly,sNy+Oly           ELSEIF( vertAdvecScheme.EQ.ENUM_DST3_FLUX_LIMIT ) THEN
796          DO i=1-Olx,sNx+Olx             CALL GAD_DST3FL_ADV_R(    bi,bj,k, dTtracerLev(k),
797           af(i,j) = af(i,j)       I                               rTrans, wFld, localTijk,
798       &           + (maskC(i,j,k,bi,bj)-maskC(i,j,k-1,bi,bj))*       O                               fVerT(1-Olx,1-Oly,kUp), myThid )
799       &             rTrans(i,j)*localTijk(i,j,k)           ELSEIF (vertAdvecScheme.EQ.ENUM_OS7MP ) THEN
800          ENDDO             CALL GAD_OS7MP_ADV_R(     bi,bj,k, dTtracerLev(k),
801         ENDDO       I                               rTrans, wFld, localTijk,
802        ELSE       O                               fVerT(1-Olx,1-Oly,kUp), myThid )
803  C-    Surface "correction" term at k=1 :           ELSE
804         DO j=1-Oly,sNy+Oly            STOP 'GAD_ADVECTION: adv. scheme incompatibale with mutli-dim'
805          DO i=1-Olx,sNx+Olx           ENDIF
806           af(i,j) = rTrans(i,j)*localTijk(i,j,k)  
807    C- end Surface/Interior if bloc
808            ENDIF
809    
810    #ifdef ALLOW_AUTODIFF_TAMC
811    CADJ STORE rTrans(:,:)  
812    CADJ &     = comlev1_bibj_k_gad, key=kkey, byte=isbyte
813    CADJ STORE rTranskp1(:,:)  
814    CADJ &     = comlev1_bibj_k_gad, key=kkey, byte=isbyte
815    #endif /* ALLOW_AUTODIFF_TAMC */
816    
817    C--   Divergence of vertical fluxes
818            DO j=1-Oly,sNy+Oly
819             DO i=1-Olx,sNx+Olx
820              localTij(i,j) = localTijk(i,j,k)
821         &      -dTtracerLev(k)*recip_rhoFacC(k)
822         &       *_recip_hFacC(i,j,k,bi,bj)*recip_drF(k)
823         &       *recip_rA(i,j,bi,bj)*recip_deepFac2C(k)
824         &       *( fVerT(i,j,kDown)-fVerT(i,j,kUp)
825         &         -tracer(i,j,k,bi,bj)*(rTransKp1(i,j)-rTrans(i,j))
826         &        )*rkSign
827              gTracer(i,j,k,bi,bj)=
828         &     (localTij(i,j)-tracer(i,j,k,bi,bj))/dTtracerLev(k)
829             ENDDO
830          ENDDO          ENDDO
        ENDDO  
       ENDIF  
 C-    add the advective flux to fVerT  
       DO j=1-Oly,sNy+Oly  
        DO i=1-Olx,sNx+Olx  
         fVerT(i,j,kUp) = af(i,j)  
        ENDDO  
       ENDDO  
831    
832  C--   Divergence of fluxes  #ifdef ALLOW_DIAGNOSTICS
833        kp1=min(Nr,k+1)          IF ( useDiagnostics ) THEN
834        kp1Msk=1.            diagName = 'ADVr'//diagSufx
835        if (k.EQ.Nr) kp1Msk=0.            CALL DIAGNOSTICS_FILL( fVerT(1-Olx,1-Oly,kUp),
836        DO j=1-Oly,sNy+Oly       &                           diagName, k,1, 2,bi,bj, myThid)
837         DO i=1-Olx,sNx+Olx          ENDIF
838          localTij(i,j)=localTijk(i,j,k)-deltaTtracer*  #endif
839       &    _recip_hFacC(i,j,k,bi,bj)*recip_drF(k)  
      &    *recip_rA(i,j,bi,bj)  
      &    *( fVerT(i,j,kUp)-fVerT(i,j,kDown)  
      &      -tracer(i,j,k,bi,bj)*rA(i,j,bi,bj)*  
      &        (wVel(i,j,k,bi,bj)-kp1Msk*wVel(i,j,kp1,bi,bj))  
      &     )*rkFac  
         gTracer(i,j,k,bi,bj)=  
      &   (localTij(i,j)-tracer(i,j,k,bi,bj))/deltaTtracer  
        ENDDO  
       ENDDO  
   
840  C--   End of K loop for vertical flux  C--   End of K loop for vertical flux
841        ENDDO         ENDDO
842    C--   end of if not.implicitAdvection block
843          ENDIF
844    
845        RETURN        RETURN
846        END        END

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.46

  ViewVC Help
Powered by ViewVC 1.1.22