Index: NEMO/branches/2019/dev_r11514_HPC-02_single-core-extrahalo/src/OCE/TRA/traadv_mus.F90
===================================================================
--- NEMO/branches/2019/dev_r11514_HPC-02_single-core-extrahalo/src/OCE/TRA/traadv_mus.F90	(revision 11692)
+++ NEMO/branches/2019/dev_r11514_HPC-02_single-core-extrahalo/src/OCE/TRA/traadv_mus.F90	(revision 11719)
@@ -30,5 +30,6 @@
    USE lib_mpp        ! distribued memory computing
    USE lbclnk         ! ocean lateral boundary condition (or mpp link) 
-   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)  
+   USE lib_fortran    ! Fortran utilities (allows no signed zero when 'key_nosignedzero' defined)
+   USE halo_mng
 
    IMPLICIT NONE
@@ -36,4 +37,12 @@
 
    PUBLIC   tra_adv_mus   ! routine called by traadv.F90
+   
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:    ) ::   r1_e1e2t_exh2, r1_e1e2u_exh2, r1_e1e2v_exh2
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:    ) ::   rnfmsk_exh2, upsmsk_exh2, mikt_exh2
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:,:  ) ::   tmask_exh2, wmask_exh2, umask_exh2, vmask_exh2
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:,:  ) ::   e3u_n_exh2, e3v_n_exh2, e3t_n_exh2, e3w_n_exh2
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:,:  ) ::   pun_exh2, pvn_exh2, pwn_exh2   ! 3 ocean velocity components
+   REAL(wp), ALLOCATABLE, DIMENSION(:,:,:,:) ::   ptb_exh2, pta_exh2        ! before and now tracer fields
+
    
    REAL(wp), ALLOCATABLE, SAVE, DIMENSION(:,:)   ::   upsmsk   !: mixed upstream/centered scheme near some straits
@@ -44,4 +53,6 @@
    LOGICAL  ::   l_ptr   ! flag to compute poleward transport
    LOGICAL  ::   l_hst   ! flag to compute heat/salt transport
+   
+   INTEGER :: jphls = 2
 
    !! * Substitutions
@@ -84,11 +95,101 @@
       !
       INTEGER  ::   ji, jj, jk, jn   ! dummy loop indices
-      INTEGER  ::   ierr             ! local integer
+      INTEGER  ::   last_khls, ierr             ! local integer
       REAL(wp) ::   zu, z0u, zzwx, zw , zalpha   ! local scalars
       REAL(wp) ::   zv, z0v, zzwy, z0w           !   -      -
-      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zwx, zslpx   ! 3D workspace
-      REAL(wp), DIMENSION(jpi,jpj,jpk) ::   zwy, zslpy   ! -      - 
+      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::   zwx, zslpx   ! 3D workspace
+      REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::   zwy, zslpy   ! -      - 
       !!----------------------------------------------------------------------
       !
+ 		
+	CALL halo_mng_set(jphls)
+	
+	ALLOCATE(zwx(jplbi:jpi,jplbj:jpj,jpk))
+	ALLOCATE(zwy(jplbi:jpi,jplbj:jpj,jpk))
+	ALLOCATE(zslpx(jplbi:jpi,jplbj:jpj,jpk))
+	ALLOCATE(zslpy(jplbi:jpi,jplbj:jpj,jpk))
+
+	IF (kt==kit000) THEN
+		if (.not. allocated(pun_exh2))	ALLOCATE(pun_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(pvn_exh2))	ALLOCATE(pvn_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(pwn_exh2))	ALLOCATE(pwn_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(ptb_exh2))	ALLOCATE(ptb_exh2(jplbi:jpi,jplbj:jpj,jpk,kjpt))
+		if (.not. allocated(pta_exh2))	ALLOCATE(pta_exh2(jplbi:jpi,jplbj:jpj,jpk,kjpt))
+		if (.not. allocated(r1_e1e2t_exh2))	ALLOCATE(r1_e1e2t_exh2(jplbi:jpi,jplbj:jpj))
+		if (.not. allocated(r1_e1e2u_exh2))	ALLOCATE(r1_e1e2u_exh2(jplbi:jpi,jplbj:jpj))
+		if (.not. allocated(r1_e1e2v_exh2))	ALLOCATE(r1_e1e2v_exh2(jplbi:jpi,jplbj:jpj))
+		if (.not. allocated(tmask_exh2))	ALLOCATE(tmask_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(wmask_exh2))	ALLOCATE(wmask_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(umask_exh2))	ALLOCATE(umask_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(vmask_exh2))	ALLOCATE(vmask_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(e3u_n_exh2))	ALLOCATE(e3u_n_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(e3v_n_exh2))	ALLOCATE(e3v_n_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(e3t_n_exh2))	ALLOCATE(e3t_n_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		if (.not. allocated(e3w_n_exh2))	ALLOCATE(e3w_n_exh2(jplbi:jpi,jplbj:jpj,jpk))
+		IF( ln_isfcav.and..not.allocated(mikt_exh2)) ALLOCATE(mikt_exh2(jplbi:jpi,jplbj:jpj))
+		IF( ld_msc_ups.and..not.allocated(rnfmsk_exh2)) ALLOCATE(rnfmsk_exh2(jplbi:jpi,jplbj:jpj))
+		IF( ld_msc_ups.and..not.allocated(upsmsk_exh2)) ALLOCATE(upsmsk_exh2(jplbi:jpi,jplbj:jpj))
+
+		CALL halo_mng_copy(r1_e1e2t, r1_e1e2t_exh2)
+		CALL halo_mng_copy(r1_e1e2u, r1_e1e2u_exh2)
+		CALL halo_mng_copy(r1_e1e2v, r1_e1e2v_exh2)
+		CALL halo_mng_copy(tmask, tmask_exh2)
+		CALL halo_mng_copy(wmask, wmask_exh2)
+		CALL halo_mng_copy(umask, umask_exh2)
+		CALL halo_mng_copy(vmask, vmask_exh2)
+
+		CALL lbc_lnk( 'traadv_mus', r1_e1e2u_exh2, 'U', -1.)
+		CALL lbc_lnk( 'traadv_mus', r1_e1e2v_exh2, 'V', -1.)
+		CALL lbc_lnk( 'traadv_mus', r1_e1e2t_exh2, 'T', 1.)
+		CALL lbc_lnk( 'traadv_mus', tmask_exh2, 'T', 1. )
+		CALL lbc_lnk( 'traadv_mus', wmask_exh2, 'W', 1.)
+		CALL lbc_lnk( 'traadv_mus', umask_exh2, 'U', 1. )
+		CALL lbc_lnk( 'traadv_mus', vmask_exh2, 'V', 1.)
+	ENDIF
+	
+	IF( ln_isfcav ) THEN ; CALL halo_mng_copy(REAL(mikt), mikt_exh2) ; CALL lbc_lnk( 'traadv_mus', mikt_exh2, 'T', 1.) ; ENDIF
+	IF( ld_msc_ups) THEN ; CALL halo_mng_copy(rnfmsk, rnfmsk_exh2) ; CALL lbc_lnk( 'traadv_mus', rnfmsk_exh2, 'T', 1.) ; ENDIF
+	IF( ld_msc_ups) THEN ; CALL halo_mng_copy(upsmsk, upsmsk_exh2) ; CALL lbc_lnk( 'traadv_mus', upsmsk_exh2, 'T', 1.) ; ENDIF
+
+	CALL halo_mng_copy(e3u_n, e3u_n_exh2)
+	CALL halo_mng_copy(e3v_n, e3v_n_exh2)
+	CALL halo_mng_copy(e3t_n, e3t_n_exh2)
+	CALL halo_mng_copy(e3w_n, e3w_n_exh2)
+	CALL halo_mng_copy(pun, pun_exh2)
+	CALL halo_mng_copy(pvn, pvn_exh2)
+	CALL halo_mng_copy(pwn, pwn_exh2)
+	CALL halo_mng_copy(ptb, ptb_exh2)
+	CALL halo_mng_copy(pta, pta_exh2)
+
+	CALL lbc_lnk( 'traadv_mus', e3u_n_exh2, 'U', -1., pfillval = 1.0_wp )
+	CALL lbc_lnk( 'traadv_mus', e3v_n_exh2, 'V', -1., pfillval = 1.0_wp )
+	CALL lbc_lnk( 'traadv_mus', e3t_n_exh2, 'T', 1., pfillval = 1.0_wp )
+	CALL lbc_lnk( 'traadv_mus', e3w_n_exh2, 'W', 1., pfillval = 1.0_wp )
+	CALL lbc_lnk( 'traadv_mus', pun_exh2, 'U', -1.)
+	CALL lbc_lnk( 'traadv_mus', pvn_exh2, 'V', -1.)
+	CALL lbc_lnk( 'traadv_mus', pwn_exh2, 'W', 1.)
+	CALL lbc_lnk( 'traadv_mus', pta_exh2, 'T', 1.)
+	CALL lbc_lnk( 'traadv_mus', ptb_exh2, 'T', 1.)
+
+#     define pun pun_exh2
+#     define pvn pvn_exh2
+#     define pwn pwn_exh2
+#     define ptb ptb_exh2
+#     define pta pta_exh2
+#     define r1_e1e2t r1_e1e2t_exh2
+#     define r1_e1e2u r1_e1e2u_exh2
+#     define r1_e1e2v r1_e1e2v_exh2
+#     define tmask tmask_exh2
+#     define wmask wmask_exh2
+#     define umask umask_exh2
+#     define vmask vmask_exh2
+#     define e3u_n e3u_n_exh2
+#     define e3v_n e3v_n_exh2
+#     define e3t_n e3t_n_exh2
+#     define e3w_n e3w_n_exh2
+#     define mikt mikt_exh2
+#     define rnfmsk rnfmsk_exh2
+#     define upsmsk upsmsk_exh2
+
       IF( kt == kit000 )  THEN
          IF(lwp) WRITE(numout,*)
@@ -100,9 +201,9 @@
          ! Upstream / MUSCL scheme indicator
          !
-         ALLOCATE( xind(jpi,jpj,jpk), STAT=ierr )
+         ALLOCATE( xind(jplbi:jpi,jplbj:jpj,jpk), STAT=ierr )
          xind(:,:,:) = 1._wp              ! set equal to 1 where up-stream is not needed
          !
          IF( ld_msc_ups ) THEN            ! define the upstream indicator (if asked)
-            ALLOCATE( upsmsk(jpi,jpj), STAT=ierr )
+            ALLOCATE( upsmsk(jplbi:jpi,jplbj:jpj), STAT=ierr )
             upsmsk(:,:) = 0._wp                             ! not upstream by default
             !
@@ -132,6 +233,6 @@
          zwy(:,:,jpk) = 0._wp  
          DO jk = 1, jpkm1                       ! interior values
-            DO jj = 1, jpjm1      
-               DO ji = 1, fs_jpim1   ! vector opt.
+            DO jj = jplbj, jpj-1      
+               DO ji = jplbi, jpi-1   ! vector opt.
                   zwx(ji,jj,jk) = umask(ji,jj,jk) * ( ptb(ji+1,jj,jk,jn) - ptb(ji,jj,jk,jn) )
                   zwy(ji,jj,jk) = vmask(ji,jj,jk) * ( ptb(ji,jj+1,jk,jn) - ptb(ji,jj,jk,jn) )
@@ -140,11 +241,11 @@
          END DO
          ! lateral boundary conditions   (changed sign)
-         CALL lbc_lnk_multi( 'traadv_mus', zwx, 'U', -1. , zwy, 'V', -1. )
+         !CALL lbc_lnk_multi( 'traadv_mus', zwx, 'U', -1. , zwy, 'V', -1. )
          !                                !-- Slopes of tracer
          zslpx(:,:,jpk) = 0._wp                 ! bottom values
          zslpy(:,:,jpk) = 0._wp
          DO jk = 1, jpkm1                       ! interior values
-            DO jj = 2, jpj
-               DO ji = fs_2, jpi   ! vector opt.
+            DO jj = jplbj+1, jpj
+               DO ji = jplbi+1, jpi   ! vector opt.
                   zslpx(ji,jj,jk) =                    ( zwx(ji,jj,jk) + zwx(ji-1,jj  ,jk) )   &
                      &            * ( 0.25 + SIGN( 0.25, zwx(ji,jj,jk) * zwx(ji-1,jj  ,jk) ) )
@@ -156,6 +257,6 @@
          !
          DO jk = 1, jpkm1                 !-- Slopes limitation
-            DO jj = 2, jpj
-               DO ji = fs_2, jpi   ! vector opt.
+            DO jj = jplbj+1, jpj
+               DO ji = jplbi+1, jpi   ! vector opt.
                   zslpx(ji,jj,jk) = SIGN( 1., zslpx(ji,jj,jk) ) * MIN(    ABS( zslpx(ji  ,jj,jk) ),   &
                      &                                                 2.*ABS( zwx  (ji-1,jj,jk) ),   &
@@ -169,6 +270,6 @@
          !
          DO jk = 1, jpkm1                 !-- MUSCL horizontal advective fluxes
-            DO jj = 2, jpjm1
-               DO ji = fs_2, fs_jpim1   ! vector opt.
+            DO jj = jplbj+1, jpj-1
+               DO ji = jplbi+1, jpi-1   ! vector opt.
                   ! MUSCL fluxes
                   z0u = SIGN( 0.5, pun(ji,jj,jk) )
@@ -188,9 +289,9 @@
             END DO
          END DO
-         CALL lbc_lnk_multi( 'traadv_mus', zwx, 'U', -1. , zwy, 'V', -1. )   ! lateral boundary conditions   (changed sign)
+         !CALL lbc_lnk_multi( 'traadv_mus', zwx, 'U', -1. , zwy, 'V', -1. )   ! lateral boundary conditions   (changed sign)
          !
          DO jk = 1, jpkm1                 !-- Tracer advective trend
-            DO jj = 2, jpjm1      
-               DO ji = fs_2, fs_jpim1   ! vector opt.
+            DO jj = jplbj+1, jpj-1      
+               DO ji = jplbi+1, jpi-1   ! vector opt.
                   pta(ji,jj,jk,jn) = pta(ji,jj,jk,jn) - ( zwx(ji,jj,jk) - zwx(ji-1,jj  ,jk  )       &
                   &                                     + zwy(ji,jj,jk) - zwy(ji  ,jj-1,jk  ) )     &
@@ -220,6 +321,6 @@
          zslpx(:,:,1) = 0._wp                   ! surface values
          DO jk = 2, jpkm1                       ! interior value
-            DO jj = 1, jpj
-               DO ji = 1, jpi
+            DO jj = jplbj, jpj
+               DO ji = jplbi, jpi
                   zslpx(ji,jj,jk) =                     ( zwx(ji,jj,jk) + zwx(ji,jj,jk+1) )  &
                      &            * (  0.25 + SIGN( 0.25, zwx(ji,jj,jk) * zwx(ji,jj,jk+1) )  )
@@ -228,6 +329,6 @@
          END DO
          DO jk = 2, jpkm1                 !-- Slopes limitation
-            DO jj = 1, jpj                      ! interior values
-               DO ji = 1, jpi
+            DO jj = jplbj, jpj                      ! interior values
+               DO ji = jplbi, jpi
                   zslpx(ji,jj,jk) = SIGN( 1., zslpx(ji,jj,jk) ) * MIN(    ABS( zslpx(ji,jj,jk  ) ),   &
                      &                                                 2.*ABS( zwx  (ji,jj,jk+1) ),   &
@@ -237,6 +338,6 @@
          END DO
          DO jk = 1, jpk-2                 !-- vertical advective flux
-            DO jj = 2, jpjm1      
-               DO ji = fs_2, fs_jpim1   ! vector opt.
+            DO jj = jplbj+1, jpj-1      
+               DO ji = jplbi+1, jpi-1   ! vector opt.
                   z0w = SIGN( 0.5, pwn(ji,jj,jk+1) )
                   zalpha = 0.5 + z0w
@@ -250,6 +351,6 @@
          IF( ln_linssh ) THEN                   ! top values, linear free surface only
             IF( ln_isfcav ) THEN                      ! ice-shelf cavities (top of the ocean)
-               DO jj = 1, jpj
-                  DO ji = 1, jpi
+            DO jj = jplbj, jpj      
+               DO ji = jplbi, jpi
                      zwx(ji,jj, mikt(ji,jj) ) = pwn(ji,jj,mikt(ji,jj)) * ptb(ji,jj,mikt(ji,jj),jn)
                   END DO
@@ -261,6 +362,6 @@
          !
          DO jk = 1, jpkm1                 !-- vertical advective trend
-            DO jj = 2, jpjm1      
-               DO ji = fs_2, fs_jpim1   ! vector opt.
+            DO jj = jplbj+1, jpj-1      
+               DO ji = jplbi+1, jpi-1   ! vector opt.
                   pta(ji,jj,jk,jn) =  pta(ji,jj,jk,jn) - ( zwx(ji,jj,jk) - zwx(ji,jj,jk+1) ) * r1_e1e2t(ji,jj) / e3t_n(ji,jj,jk)
                END DO
@@ -272,4 +373,57 @@
       END DO                     ! end of tracer loop
       !
+#     undef pun
+#     undef pvn
+#     undef pwn
+#     undef ptb
+#     undef pta
+#     undef r1_e1e2t
+#     undef r1_e1e2u
+#     undef r1_e1e2v
+#     undef tmask
+#     undef wmask
+#     undef umask
+#     undef vmask
+#     undef e3u_n
+#     undef e3v_n
+#     undef e3t_n
+#     undef e3w_n
+#     undef mikt
+#     undef rnfmsk
+#     undef upsmsk
+
+	CALL halo_mng_copy(pta_exh2, pta)
+
+    last_khls = jphls - ((SIZE(pta_exh2, 1) - SIZE(pta, 1))/2)
+
+    CALL halo_mng_set(last_khls)
+
+	CALL lbc_lnk( 'traadv_mus', pta, 'T', 1. )
+
+	IF( kt==nitend ) THEN
+		if (allocated(pun_exh2)) DEALLOCATE(pun_exh2)
+    	if (allocated(pvn_exh2)) DEALLOCATE(pvn_exh2)
+		if (allocated(pwn_exh2)) DEALLOCATE(pwn_exh2)
+	   	if (allocated(ptb_exh2)) DEALLOCATE(ptb_exh2)
+		if (allocated(pta_exh2)) DEALLOCATE(pta_exh2)
+		if (allocated(r1_e1e2t_exh2)) DEALLOCATE(r1_e1e2t_exh2)
+		if (allocated(r1_e1e2u_exh2)) DEALLOCATE(r1_e1e2u_exh2)
+		if (allocated(r1_e1e2v_exh2)) DEALLOCATE(r1_e1e2v_exh2)
+		if (allocated(tmask_exh2)) DEALLOCATE(tmask_exh2)
+		if (allocated(wmask_exh2)) DEALLOCATE(wmask_exh2)
+		if (allocated(umask_exh2)) DEALLOCATE(umask_exh2)
+		if (allocated(vmask_exh2)) DEALLOCATE(vmask_exh2)
+		if (allocated(e3u_n_exh2)) DEALLOCATE(e3u_n_exh2)
+		if (allocated(e3v_n_exh2)) DEALLOCATE(e3v_n_exh2)
+		if (allocated(e3t_n_exh2)) DEALLOCATE(e3t_n_exh2)
+		if (allocated(e3w_n_exh2)) DEALLOCATE(e3w_n_exh2)
+       	IF (ln_isfcav.and.allocated(mikt_exh2)) DEALLOCATE(mikt_exh2)
+       	IF( ld_msc_ups.and.allocated(rnfmsk_exh2)) DEALLOCATE(rnfmsk_exh2)
+		IF( ld_msc_ups.and.allocated(upsmsk_exh2)) DEALLOCATE(upsmsk_exh2)
+
+    ENDIF
+
+	DEALLOCATE(zwx,zwy)
+    DEALLOCATE(zslpx,zslpy)
    END SUBROUTINE tra_adv_mus
 
