Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn2d.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn2d.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn2d.F90	(revision 11048)
@@ -99,4 +99,5 @@
          ii   = idx%nbi(jb,igrd)
          ij   = idx%nbj(jb,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          zwgt = idx%nbw(jb,igrd)
          pua2d(ii,ij) = ( pua2d(ii,ij) + zwgt * ( dta%u2d(jb) - pua2d(ii,ij) ) ) * umask(ii,ij,1)
@@ -107,4 +108,5 @@
          ii   = idx%nbi(jb,igrd)
          ij   = idx%nbj(jb,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          zwgt = idx%nbw(jb,igrd)
          pva2d(ii,ij) = ( pva2d(ii,ij) + zwgt * ( dta%v2d(jb) - pva2d(ii,ij) ) ) * vmask(ii,ij,1)
@@ -163,4 +165,5 @@
          ii = idx%nbi(jb,igrd)
          ij = idx%nbj(jb,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          IF( ll_wd ) THEN
             spgu(ii, ij) = dta%ssh(jb)  - ssh_ref 
@@ -178,4 +181,5 @@
          ii  = idx%nbi(jb,igrd)
          ij  = idx%nbj(jb,igrd) 
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          flagu => idx%flagu(jb,igrd)
          iim1 = ii + MAX( 0, INT( flagu ) )   ! T pts i-indice inside the boundary
@@ -196,4 +200,5 @@
          ii  = idx%nbi(jb,igrd)
          ij  = idx%nbj(jb,igrd) 
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          flagv => idx%flagv(jb,igrd)
          ijm1 = ij + MAX( 0, INT( flagv ) )   ! T pts j-indice inside the boundary
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn3d.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn3d.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn3d.F90	(revision 11048)
@@ -86,4 +86,5 @@
             ii   = idx%nbi(jb,igrd)
             ij   = idx%nbj(jb,igrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
             ua(ii,ij,jk) = dta%u3d(jb,jk) * umask(ii,ij,jk)
          END DO
@@ -95,11 +96,11 @@
             ii   = idx%nbi(jb,igrd)
             ij   = idx%nbj(jb,igrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
             va(ii,ij,jk) = dta%v3d(jb,jk) * vmask(ii,ij,jk)
          END DO
       END DO
+      !
       CALL lbc_bdy_lnk( 'bdydyn3d', ua, 'U', -1., ib_bdy )   ! Boundary points should be updated  
-      CALL lbc_bdy_lnk( 'bdydyn3d', va, 'V', -1., ib_bdy )   
-      !
-      IF( kt == nit000 )   CLOSE( unit = 102 )
+      CALL lbc_bdy_lnk( 'bdydyn3d', va, 'V', -1., ib_bdy )
       !
    END SUBROUTINE bdy_dyn3d_spe
@@ -120,33 +121,47 @@
       INTEGER  ::   jb, jk         ! dummy loop indices
       INTEGER  ::   ii, ij, igrd   ! local integers
-      REAL(wp) ::   zwgt           ! boundary weight
-      INTEGER  ::   fu, fv
+      INTEGER  ::   flagu, flagv           ! short cuts
       !!----------------------------------------------------------------------
       !
       igrd = 2                      ! Copying tangential velocity into bdy points
       DO jb = 1, idx%nblenrim(igrd)
-         DO jk = 1, jpkm1
-            ii   = idx%nbi(jb,igrd)
-            ij   = idx%nbj(jb,igrd)
-            fu   = ABS( ABS (NINT( idx%flagu(jb,igrd) ) ) - 1 )
-            ua(ii,ij,jk) = ua(ii,ij,jk) * REAL( 1 - fu) + ( ua(ii,ij+fu,jk) * umask(ii,ij+fu,jk) &
-                        &+ ua(ii,ij-fu,jk) * umask(ii,ij-fu,jk) ) * umask(ii,ij,jk) * REAL( fu )
-         END DO
+         ii    = idx%nbi(jb,igrd)
+         ij    = idx%nbj(jb,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
+         flagu = NINT(idx%flagu(jb,igrd))
+         flagv = NINT(idx%flagv(jb,igrd))
+         !
+         IF( flagu == 0 )   THEN              ! north/south bdy
+            ! Rare case : rim is parallel to the mpi subdomain border and located next to the halo
+            IF( ij+flagv > jpj .OR. ij+flagv < 1 )   CYCLE      
+            !
+            DO jk = 1, jpkm1
+               ua(ii,ij,jk) = ua(ii,ij+flagv,jk) * umask(ii,ij+flagv,jk)
+            END DO
+            !
+         END IF
       END DO
       !
       igrd = 3                      ! Copying tangential velocity into bdy points
       DO jb = 1, idx%nblenrim(igrd)
-         DO jk = 1, jpkm1
-            ii   = idx%nbi(jb,igrd)
-            ij   = idx%nbj(jb,igrd)
-            fv   = ABS( ABS (NINT( idx%flagv(jb,igrd) ) ) - 1 )
-            va(ii,ij,jk) = va(ii,ij,jk) * REAL( 1 - fv ) + ( va(ii+fv,ij,jk) * vmask(ii+fv,ij,jk) &
-                        &+ va(ii-fv,ij,jk) * vmask(ii-fv,ij,jk) ) * vmask(ii,ij,jk) * REAL( fv )
-         END DO
-      END DO
+         ii    = idx%nbi(jb,igrd)
+         ij    = idx%nbj(jb,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
+         flagu = NINT(idx%flagu(jb,igrd))
+         flagv = NINT(idx%flagv(jb,igrd))
+         !
+         IF( flagv == 0 )   THEN              !  west/east  bdy
+            ! Rare case : rim is parallel to the mpi subdomain border and located next to the halo
+            IF( ii+flagu > jpi .OR. ii+flagu < 1 )   CYCLE      
+            !
+            DO jk = 1, jpkm1
+               va(ii,ij,jk) = va(ii+flagu,ij,jk) * vmask(ii+flagu,ij,jk)
+            END DO
+            !
+         END IF
+      END DO
+      !
       CALL lbc_bdy_lnk( 'bdydyn3d', ua, 'U', -1., ib_bdy )   ! Boundary points should be updated  
       CALL lbc_bdy_lnk( 'bdydyn3d', va, 'V', -1., ib_bdy )   
-      !
-      IF( kt == nit000 )   CLOSE( unit = 102 )
       !
    END SUBROUTINE bdy_dyn3d_zgrad
@@ -174,4 +189,5 @@
          ii = idx%nbi(ib,igrd)
          ij = idx%nbj(ib,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          DO ik = 1, jpkm1
             ua(ii,ij,ik) = 0._wp
@@ -183,4 +199,5 @@
          ii = idx%nbi(ib,igrd)
          ij = idx%nbj(ib,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          DO ik = 1, jpkm1
             va(ii,ij,ik) = 0._wp
@@ -189,6 +206,4 @@
       !
       CALL lbc_bdy_lnk( 'bdydyn3d', ua, 'U', -1., ib_bdy )   ;   CALL lbc_bdy_lnk( 'bdydyn3d', va, 'V', -1.,ib_bdy )   ! Boundary points should be updated
-      !
-      IF( kt == nit000 )   CLOSE( unit = 102 )
       !
    END SUBROUTINE bdy_dyn3d_zro
@@ -221,4 +236,5 @@
             ii   = idx%nbi(jb,igrd)
             ij   = idx%nbj(jb,igrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
             zwgt = idx%nbw(jb,igrd)
             ua(ii,ij,jk) = ( ua(ii,ij,jk) + zwgt * ( dta%u3d(jb,jk) - ua(ii,ij,jk) ) ) * umask(ii,ij,jk)
@@ -231,12 +247,12 @@
             ii   = idx%nbi(jb,igrd)
             ij   = idx%nbj(jb,igrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
             zwgt = idx%nbw(jb,igrd)
             va(ii,ij,jk) = ( va(ii,ij,jk) + zwgt * ( dta%v3d(jb,jk) - va(ii,ij,jk) ) ) * vmask(ii,ij,jk)
          END DO
       END DO 
+      !
       CALL lbc_bdy_lnk( 'bdydyn3d', ua, 'U', -1., ib_bdy )    ! Boundary points should be updated
       CALL lbc_bdy_lnk( 'bdydyn3d', va, 'V', -1., ib_bdy )   
-      !
-      IF( kt == nit000 )   CLOSE( unit = 102 )
       !
    END SUBROUTINE bdy_dyn3d_frs
@@ -300,4 +316,5 @@
                ii   = idx_bdy(ib_bdy)%nbi(jb,igrd)
                ij   = idx_bdy(ib_bdy)%nbj(jb,igrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
                zwgt = idx_bdy(ib_bdy)%nbd(jb,igrd)
                DO jk = 1, jpkm1
@@ -311,4 +328,5 @@
                ii   = idx_bdy(ib_bdy)%nbi(jb,igrd)
                ij   = idx_bdy(ib_bdy)%nbj(jb,igrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
                zwgt = idx_bdy(ib_bdy)%nbd(jb,igrd)
                DO jk = 1, jpkm1
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyice.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyice.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyice.F90	(revision 11048)
@@ -107,4 +107,5 @@
       REAL(wp) ::   zwgt, zwgt1        ! local scalar
       REAL(wp) ::   ztmelts, zdh
+      REAL(wp), POINTER  :: flagu, flagv              ! short cuts
       !!------------------------------------------------------------------------------
       !
@@ -115,4 +116,5 @@
             ji    = idx%nbi(i_bdy,jgrd)
             jj    = idx%nbj(i_bdy,jgrd)
+            IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE   ! to remove
             zwgt  = idx%nbw(i_bdy,jgrd)
             zwgt1 = 1.e0 - idx%nbw(i_bdy,jgrd)
@@ -146,13 +148,15 @@
             ji = idx%nbi(i_bdy,jgrd)
             jj = idx%nbj(i_bdy,jgrd)
-
+            IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE   ! to remove
+            flagu => idx%flagu(i_bdy,jgrd)
+            flagv => idx%flagv(i_bdy,jgrd)
             ! condition on ice thickness depends on the ice velocity
             ! if velocity is outward (strictly), then ice thickness, volume... must be equal to adjacent values
             jpbound = 0   ;   ib = ji   ;   jb = jj
             !
-            IF( u_ice(ji  ,jj  ) < 0. .AND. umask(ji-1,jj  ,1) == 0. )   jpbound = 1 ; ib = ji+1
-            IF( u_ice(ji-1,jj  ) > 0. .AND. umask(ji  ,jj  ,1) == 0. )   jpbound = 1 ; ib = ji-1
-            IF( v_ice(ji  ,jj  ) < 0. .AND. vmask(ji  ,jj-1,1) == 0. )   jpbound = 1 ; jb = jj+1
-            IF( v_ice(ji  ,jj-1) > 0. .AND. vmask(ji  ,jj  ,1) == 0. )   jpbound = 1 ; jb = jj-1
+            IF( u_ice(ji  ,jj  ) < 0. .AND. flagu ==  1. )   jpbound = 1 ; ib = ji+1 
+            IF( u_ice(ji-1,jj  ) > 0. .AND. flagu == -1. )   jpbound = 1 ; ib = ji-1
+            IF( v_ice(ji  ,jj  ) < 0. .AND. flagv ==  1. )   jpbound = 1             ; jb = jj+1
+            IF( v_ice(ji  ,jj-1) > 0. .AND. flagv == -1. )   jpbound = 1             ; jb = jj-1
             !
             IF( nn_ice_dta(jbdy) == 0 )   jpbound = 0 ; ib = ji ; jb = jj   ! case ice boundaries = initial conditions
@@ -304,18 +308,24 @@
                   ji    = idx_bdy(jbdy)%nbi(i_bdy,jgrd)
                   jj    = idx_bdy(jbdy)%nbj(i_bdy,jgrd)
+                  IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE   ! to remove
                   zflag = idx_bdy(jbdy)%flagu(i_bdy,jgrd)
-                  !
-                  IF ( ABS( zflag ) == 1. ) THEN  ! eastern and western boundaries
-                     ! one of the two zmsk is always 0 (because of zflag)
-                     zmsk1 = 1._wp - MAX( 0.0_wp, SIGN ( 1.0_wp , - vt_i(ji+1,jj) ) ) ! 0 if no ice
-                     zmsk2 = 1._wp - MAX( 0.0_wp, SIGN ( 1.0_wp , - vt_i(ji,jj) ) )   ! 0 if no ice
-                     !  
-                     ! u_ice = u_ice of the adjacent grid point except if this grid point is ice-free (then do not change u_ice)
-                     u_ice (ji,jj) = u_ice(ji+1,jj) * 0.5_wp * ABS( zflag + 1._wp ) * zmsk1 + &
-                        &            u_ice(ji-1,jj) * 0.5_wp * ABS( zflag - 1._wp ) * zmsk2 + &
-                        &            u_ice(ji  ,jj) * ( 1._wp - MIN( 1._wp, zmsk1 + zmsk2 ) )
-                  ELSE                             ! everywhere else
-                     u_ice(ji,jj) = 0._wp
-                  ENDIF
+                  !     i-1  i   i    |  !        i  i i+1 |  !          i  i i+1 |
+                  !      >  ice  >    |  !        o  > ice |  !          o  >  o  |      
+                  ! => set at u_ice(i-1) !  => set to O       !  => unchanged
+                  IF( zflag == -1. .AND. ji > 1 .AND. ji < jpi )   THEN  
+                     IF    ( vt_i(ji  ,jj) > 0. )   THEN   ;   u_ice(ji,jj) = u_ice(ji-1,jj) 
+                     ELSEIF( vt_i(ji+1,jj) > 0. )   THEN   ;   u_ice(ji,jj) = 0._wp
+                     END IF
+                  END IF
+                  ! |    i  i+1 i+1        !  |  i   i i+1        !  | i  i i+1
+                  ! |    >  ice  >         !  | ice  >  o         !  | o  >  o   
+                  ! => set at u_ice(i+1)   !     => set to O      !     =>  unchanged
+                  IF( zflag ==  1. .AND. ji+1 < jpi+1 )   THEN
+                     IF    ( vt_i(ji+1,jj) > 0. )   THEN   ;   u_ice(ji,jj) = u_ice(ji+1,jj)
+                     ELSEIF( vt_i(ji  ,jj) > 0. )   THEN   ;   u_ice(ji,jj) = 0._wp
+                     END IF
+                  END IF
+                  !
+                  IF( zflag ==  0. )   u_ice(ji,jj) = 0._wp   ! u_ice = 0  if north/south bdy  
                   !
                END DO
@@ -327,18 +337,28 @@
                   ji    = idx_bdy(jbdy)%nbi(i_bdy,jgrd)
                   jj    = idx_bdy(jbdy)%nbj(i_bdy,jgrd)
+                  IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE   ! to remove
                   zflag = idx_bdy(jbdy)%flagv(i_bdy,jgrd)
-                  !
-                  IF ( ABS( zflag ) == 1. ) THEN  ! northern and southern boundaries
-                     ! one of the two zmsk is always 0 (because of zflag)
-                     zmsk1 = 1._wp - MAX( 0.0_wp, SIGN ( 1.0_wp , - vt_i(ji,jj+1) ) ) ! 0 if no ice
-                     zmsk2 = 1._wp - MAX( 0.0_wp, SIGN ( 1.0_wp , - vt_i(ji,jj) ) )   ! 0 if no ice
-                     !  
-                     ! v_ice = v_ice of the adjacent grid point except if this grid point is ice-free (then do not change v_ice)
-                     v_ice (ji,jj) = v_ice(ji,jj+1) * 0.5_wp * ABS( zflag + 1._wp ) * zmsk1 + &
-                        &            v_ice(ji,jj-1) * 0.5_wp * ABS( zflag - 1._wp ) * zmsk2 + &
-                        &            v_ice(ji,jj  ) * ( 1._wp - MIN( 1._wp, zmsk1 + zmsk2 ) )
-                  ELSE                             ! everywhere else
-                     v_ice(ji,jj) = 0._wp
-                  ENDIF
+                  !    ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨     !  ¨¨¨¨ïce¨¨¨(jj+1)¨¨     ! ¨¨¨¨¨¨ö¨¨¨¨(jj+1)       
+                  !       ^    (jj  )       !       ^    (jj  )       !       ^    (jj  )       
+                  !      ice   (jj  )       !       o    (jj  )       !       o    (jj  )       
+                  !       ^    (jj-1)       !                         !
+                  ! => set to u_ice(jj-1)   !  =>   set to 0          !   => unchanged        
+                  IF( zflag == -1. .AND. jj > 1 .AND. jj < jpj )   THEN                 
+                    IF    ( vt_i(ji,jj  ) > 0. )   THEN   ;   v_ice(ji,jj) = v_ice(ji,jj-1)
+                    ELSEIF( vt_i(ji,jj+1) > 0. )   THEN   ;   v_ice(ji,jj) = 0._wp
+                    END IF
+                  END IF 
+                  !       ^    (jj+1)       !                         !              
+                  !      ice   (jj+1)       !       o    (jj+1)       !       o    (jj+1)       
+                  !       ^    (jj  )       !       ^    (jj  )       !       ^    (jj  )
+                  !   ________________      !  ____ice___(jj  )_      !  _____o____(jj  ) 
+                  ! => set to u_ice(jj+1)   !    => set to 0          !    => unchanged  
+                  IF( zflag ==  1. .AND. jj < jpj )   THEN              
+                     IF    ( vt_i(ji,jj+1) > 0. )   THEN   ;   v_ice(ji,jj) = v_ice(ji,jj+1)
+                     ELSEIF( vt_i(ji,jj  ) > 0. )   THEN   ;   v_ice(ji,jj) = 0._wp
+                     END IF
+                  END IF                                          
+                  !
+                  IF( zflag ==  0. )   v_ice(ji,jj) = 0._wp   ! v_ice = 0  if west/east bdy  
                   !
                END DO
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyini.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyini.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyini.F90	(revision 11048)
@@ -37,4 +37,5 @@
    INTEGER, PARAMETER ::   jp_nseg = 100   ! 
    INTEGER, PARAMETER ::   nrimmax =  20   ! maximum rimwidth in structured
+   INTEGER  :: nde = 1 ! domain extended in the halo to deal with bondaries 
                                                ! open boundary data files
    ! Straight open boundary segment parameters:
@@ -144,4 +145,5 @@
       INTEGER :: iw_b(4), ie_b(4), is_b(4), in_b(4)                ! Arrays for neighbours coordinates
       REAL(wp), TARGET, DIMENSION(jpi,jpj) ::   zfmask  ! temporary fmask array excluding coastal boundary condition (shlat)
+      REAL(wp)        , DIMENSION(jpi,jpj) ::   ztmp
       LOGICAL  ::   llnobdy, llsobdy, lleabdy, llwebdy     ! local logicals
       !!
@@ -798,8 +800,8 @@
 !      is = mjg(1) + 1            ! if monotasking and no zoom, is=2
 !      in = mjg(1) + nlcj-1 - 1   ! if monotasking and no zoom, in=jpjm1      
-      iwe = mig(1) - 1 + 2         ! if monotasking and no zoom, iw=2
-      ies = mig(1) + nlci-1 - 1  ! if monotasking and no zoom, ie=jpim1
-      iso = mjg(1) - 1 + 2         ! if monotasking and no zoom, is=2
-      ino = mjg(1) + nlcj-1 - 1  ! if monotasking and no zoom, in=jpjm1
+      iwe = mig(1) - 1 + 2 - nde       ! if monotasking and no zoom, iw=2
+      ies = mig(1) + nlci-1 - 1 + nde  ! if monotasking and no zoom, ie=jpim1
+      iso = mjg(1) - 1 + 2 -nde        ! if monotasking and no zoom, is=2
+      ino = mjg(1) + nlcj-1 - 1 + nde  ! if monotasking and no zoom, in=jpjm1
 
       ALLOCATE( nbondi_bdy(nb_bdy))
@@ -1173,7 +1175,4 @@
       CALL lbc_lnk_multi( 'bdyini', bdyumask, 'U', 1. , bdyvmask, 'V', 1., bdytmask, 'T', 1. )
       DO ib_bdy = 1, nb_bdy       ! Indices and directions of rim velocity components
-
-         idx_bdy(ib_bdy)%flagu(:,:) = 0._wp
-         idx_bdy(ib_bdy)%flagv(:,:) = 0._wp
          icount = 0 
 
@@ -1190,15 +1189,17 @@
             END SELECT 
             icount = 0
+            ztmp(:,:) = 0._wp
             DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)  
-               nbi => idx_bdy(ib_bdy)%nbi(ib,igrd)
-               nbj => idx_bdy(ib_bdy)%nbj(ib,igrd)
-               zefl = pmask(nbi+i_offset-1,nbj)
-               zwfl = pmask(nbi+i_offset,nbj)
+               ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+               ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE
+               zefl = pmask(ii+i_offset-1,ij)
+               zwfl = pmask(ii+i_offset  ,ij)
                ! This error check only works if you are using the bdyXmask arrays
-               IF( i_offset == 1 .and. zefl + zwfl == 2 ) THEN
+               IF( i_offset == 1 .and. zefl + zwfl == 2. ) THEN
                   icount = icount + 1
-                  IF(lwp) WRITE(numout,*) 'Problem with igrd = ',igrd,' at (global) nbi, nbj : ',mig(nbi),mjg(nbj)
+                  IF(lwp) WRITE(numout,*) 'Problem with igrd = ',igrd,' at (global) nbi, nbj : ',mig(ii),mjg(ij)
                ELSE
-                  idx_bdy(ib_bdy)%flagu(ib,igrd) = -zefl + zwfl
+                  ztmp(ii,ij) = -zefl + zwfl
                ENDIF
             END DO
@@ -1209,4 +1210,10 @@
                CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
             ENDIF 
+            CALL lbc_lnk( 'bdyini', ztmp, 'T', 1. ) 
+            DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)  
+               ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+               ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+               idx_bdy(ib_bdy)%flagu(ib,igrd) = ztmp(ii,ij)
+            END DO
          END DO
 
@@ -1223,15 +1230,17 @@
             END SELECT 
             icount = 0
+            ztmp(:,:) = 0._wp
             DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)  
-               nbi => idx_bdy(ib_bdy)%nbi(ib,igrd)
-               nbj => idx_bdy(ib_bdy)%nbj(ib,igrd)
-               znfl = pmask(nbi,nbj+j_offset-1)
-               zsfl = pmask(nbi,nbj+j_offset  )
+               ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+               ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE
+               znfl = pmask(ii,ij+j_offset-1)
+               zsfl = pmask(ii,ij+j_offset  )
                ! This error check only works if you are using the bdyXmask arrays
-               IF( j_offset == 1 .and. znfl + zsfl == 2 ) THEN
-                  IF(lwp) WRITE(numout,*) 'Problem with igrd = ',igrd,' at (global) nbi, nbj : ',mig(nbi),mjg(nbj)
+               IF( j_offset == 1 .and. znfl + zsfl == 2. ) THEN
+                  IF(lwp) WRITE(numout,*) 'Problem with igrd = ',igrd,' at (global) nbi, nbj : ',mig(ii),mjg(ij)
                   icount = icount + 1
                ELSE
-                  idx_bdy(ib_bdy)%flagv(ib,igrd) = -znfl + zsfl
+                  ztmp(ii,ij) = -znfl + zsfl
                END IF
             END DO
@@ -1241,5 +1250,11 @@
                WRITE(ctmp2,*) ' ========== '
                CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
-            ENDIF 
+            ENDIF
+            CALL lbc_lnk( 'bdyini', ztmp, 'T', 1. ) 
+            DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)  
+               ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+               ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+               idx_bdy(ib_bdy)%flagv(ib,igrd) = ztmp(ii,ij)
+            END DO
          END DO
          !
@@ -1257,7 +1272,9 @@
                CASE( 3 )   ;   pmask => bdyvmask 
             END SELECT
+            ztmp(:,:) = 0._wp
             DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
-               ii        =  idx_bdy(ib_bdy)%nbi(ib,igrd)
-               ij        =  idx_bdy(ib_bdy)%nbj(ib,igrd)
+               ii =  idx_bdy(ib_bdy)%nbi(ib,igrd)
+               ij =  idx_bdy(ib_bdy)%nbj(ib,igrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )   CYCLE
                llnobdy = pmask(ii  ,ij+1) == 1.  
                llsobdy = pmask(ii  ,ij-1) == 1. 
@@ -1268,12 +1285,12 @@
                   !               !              !     _____     !     _____     
                   !  1 |   o      !  2  o   |    !  3 | x        !  4     x |    
-                  !    |_x_ _     !    _ _x_|    !    |   o      !      o   |    
-                  IF( pmask(ii+1,ij+1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 1
-                  IF( pmask(ii-1,ij+1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 2
-                  IF( pmask(ii+1,ij-1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 3
-                  IF( pmask(ii-1,ij-1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 4
+                  !    |_x_ _     !    _ _x_|    !    |   o      !      o   |
+                  IF( pmask(ii+1,ij+1) == 1. )   ztmp(ii,ij) = 1
+                  IF( pmask(ii-1,ij+1) == 1. )   ztmp(ii,ij) = 2
+                  IF( pmask(ii+1,ij-1) == 1. )   ztmp(ii,ij) = 3
+                  IF( pmask(ii-1,ij-1) == 1. )   ztmp(ii,ij) = 4
                END IF
                IF( inbdy == 1 )   THEN   ! middle of linear bdy
-                  idx_bdy(ib_bdy)%ntreat(ib,igrd) = 0   ! regular treatment with flags
+                  ztmp(ii,ij) = 0   ! regular treatment with flags
                END IF
                IF( inbdy == 2 )   THEN   ! exterior of a corner
@@ -1281,8 +1298,8 @@
                   !  5 ____x o    !  6   o x___   ! 7      x o      !  8   o x      
                   !         |     !       |       !        o        !        o 
-                  IF( llnobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 5
-                  IF( llnobdy .AND. llwebdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 6
-                  IF( llsobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 7
-                  IF( llsobdy .AND. llwebdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 8
+                  IF( llnobdy .AND. lleabdy )   ztmp(ii,ij) = 5
+                  IF( llnobdy .AND. llwebdy )   ztmp(ii,ij) = 6
+                  IF( llsobdy .AND. lleabdy )   ztmp(ii,ij) = 7
+                  IF( llsobdy .AND. llwebdy )   ztmp(ii,ij) = 8
                END IF
                IF( inbdy == 3 )   THEN   ! 3 neighbours __   __
@@ -1290,8 +1307,8 @@
                   !  9  _| x o    ! 10   o x |_   ! 11   o x o    ! 12  o x o       
                   !    |   o      !        o   |  !        o      !    __|¨|__    
-                  IF( llnobdy .AND. lleabdy .AND. llsobdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 9
-                  IF( llnobdy .AND. llwebdy .AND. llsobdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 10
-                  IF( llwebdy .AND. llsobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 11
-                  IF( llwebdy .AND. llnobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 12
+                  IF( llnobdy .AND. lleabdy .AND. llsobdy )   ztmp(ii,ij) = 9
+                  IF( llnobdy .AND. llwebdy .AND. llsobdy )   ztmp(ii,ij) = 10
+                  IF( llwebdy .AND. llsobdy .AND. lleabdy )   ztmp(ii,ij) = 11
+                  IF( llwebdy .AND. llnobdy .AND. lleabdy )   ztmp(ii,ij) = 12
                END IF
                IF( inbdy == 4 )   THEN
@@ -1301,4 +1318,10 @@
                   CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
                END IF
+            END DO
+            CALL lbc_lnk( 'bdyini', ztmp, 'T', 1. )
+            DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)  
+               ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
+               ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+               idx_bdy(ib_bdy)%ntreat(ib,igrd) = ztmp(ii,ij)
             END DO
          END DO
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdylib.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdylib.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdylib.F90	(revision 11048)
@@ -56,4 +56,5 @@
             ii = idx%nbi(ib,igrd) 
             ij = idx%nbj(ib,igrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
             zwgt = idx%nbw(ib,igrd)
             pta(ii,ij,ik) = ( pta(ii,ij,ik) + zwgt * (dta(ib,ik) - pta(ii,ij,ik) ) ) * tmask(ii,ij,ik)
@@ -83,4 +84,5 @@
          ii = idx%nbi(ib,igrd)
          ij = idx%nbj(ib,igrd)
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          DO ik = 1, jpkm1
             pta(ii,ij,ik) = dta(ib,ik) * tmask(ii,ij,ik)
@@ -466,5 +468,5 @@
          ii = idx%nbi(ib,igrd)
          ij = idx%nbj(ib,igrd)
-         !
+         IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
          SELECT CASE( idx%ntreat(ib,igrd) )   ! select free ocean neighbours
             !     o 
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytides.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytides.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytides.F90	(revision 11048)
@@ -161,4 +161,5 @@
                      ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
                      ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+                     IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove?
                      td%ssh0(ib,itide,1) = ztr(ii,ij)
                      td%ssh0(ib,itide,2) = zti(ii,ij)
@@ -177,4 +178,5 @@
                      ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
                      ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+                     IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove?
                      td%u0(ib,itide,1) = ztr(ii,ij)
                      td%u0(ib,itide,2) = zti(ii,ij)
@@ -193,4 +195,5 @@
                      ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
                      ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+                     IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove?
                      td%v0(ib,itide,1) = ztr(ii,ij)
                      td%v0(ib,itide,2) = zti(ii,ij)
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytra.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytra.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytra.F90	(revision 11048)
@@ -93,20 +93,19 @@
       INTEGER,                             INTENT(in) ::   jpa  ! TRA index
       !
-      REAL(wp) ::   zwgt           ! boundary weight
-      INTEGER  ::   ib, ik, igrd   ! dummy loop indices
-      INTEGER  ::   ii, ij, ip, jp ! 2D addresses
+      INTEGER  ::   ib, ii, ij, igrd   ! dummy loop indices
+      INTEGER  ::   ik, ip, jp ! 2D addresses
       !!----------------------------------------------------------------------
       !
       igrd = 1                       ! Everything is at T-points here
-      DO ib = 1, idx%nblenrim(igrd)
-         ii = idx%nbi(ib,igrd)
-         ij = idx%nbj(ib,igrd)
-         DO ik = 1, jpkm1
-            ip = bdytmask(ii+1,ij  ) - bdytmask(ii-1,ij  )
-            jp = bdytmask(ii  ,ij+1) - bdytmask(ii  ,ij-1)
-            if (jpa == jp_tem) pta(ii,ij,ik) = pta(ii+ip,ij+jp,ik) * tmask(ii,ij,ik)
-            if (jpa == jp_sal) pta(ii,ij,ik) =                 0.1 * tmask(ii,ij,ik)
+      IF(      jpa == jp_tem )   THEN
+         CALL bdy_nmn( idx, igrd, pta )
+      ELSE IF( jpa == jp_sal )   THEN
+         DO ib = 1, idx%nblenrim(igrd)
+            ii = idx%nbi(ib,igrd)
+            ij = idx%nbj(ib,igrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
+            pta(ii,ij,1:jpkm1) = 0.1 * tmask(ii,ij,1:jpkm1)
          END DO
-      END DO
+      END IF
       !
    END SUBROUTINE bdy_rnf
@@ -137,4 +136,5 @@
                ii = idx_bdy(ib_bdy)%nbi(ib,igrd)
                ij = idx_bdy(ib_bdy)%nbj(ib,igrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
                zwgt = idx_bdy(ib_bdy)%nbd(ib,igrd)
                DO ik = 1, jpkm1
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyvol.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyvol.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyvol.F90	(revision 11048)
@@ -99,4 +99,5 @@
             ii = idx%nbi(jb,jgrd)
             ij = idx%nbj(jb,jgrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove? check tmask_i definition...
             zubtpecor = zubtpecor + idx%flagu(jb,jgrd) * pua2d(ii,ij) * e2u(ii,ij) * phu(ii,ij) * tmask_i(ii,ij) * tmask_i(ii+1,ij)
          END DO
@@ -105,4 +106,5 @@
             ii = idx%nbi(jb,jgrd)
             ij = idx%nbj(jb,jgrd)
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove?
             zubtpecor = zubtpecor + idx%flagv(jb,jgrd) * pva2d(ii,ij) * e1v(ii,ij) * phv(ii,ij) * tmask_i(ii,ij) * tmask_i(ii,ij+1)
          END DO
@@ -126,4 +128,5 @@
                ii = idx%nbi(jb,jgrd)
                ij = idx%nbj(jb,jgrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! sum : else halo couted twice
                pua2d(ii,ij) = pua2d(ii,ij) - idx%flagu(jb,jgrd) * zubtpecor * tmask_i(ii,ij) * tmask_i(ii+1,ij)
          END DO
@@ -132,4 +135,5 @@
                ii = idx%nbi(jb,jgrd)
                ij = idx%nbj(jb,jgrd)
+               IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! sum : else halo couted twice
                pva2d(ii,ij) = pva2d(ii,ij) - idx%flagv(jb,jgrd) * zubtpecor * tmask_i(ii,ij) * tmask_i(ii,ij+1)
          END DO
@@ -150,4 +154,5 @@
                   ii = idx%nbi(jb,jgrd)
                   ij = idx%nbj(jb,jgrd)
+                  IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove?
                   ztranst = ztranst + idx%flagu(jb,jgrd) * pua2d(ii,ij) * e2u(ii,ij) * phu(ii,ij) * tmask_i(ii,ij) * tmask_i(ii+1,ij)
             END DO
@@ -156,4 +161,5 @@
                   ii = idx%nbi(jb,jgrd)
                   ij = idx%nbj(jb,jgrd)
+                  IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove?
                   ztranst = ztranst + idx%flagv(jb,jgrd) * pva2d(ii,ij) * e1v(ii,ij) * phv(ii,ij) * tmask_i(ii,ij) * tmask_i(ii,ij+1)
             END DO
@@ -195,4 +201,5 @@
             nbi => idx_bdy(ib_bdy)%nbi(ib,igrd)
             nbj => idx_bdy(ib_bdy)%nbj(ib,igrd)
+            IF( nbi == 1 .OR. nbi == jpi .OR. nbj == 1 .OR. nbj == jpj )  CYCLE
             zflagu => idx_bdy(ib_bdy)%flagu(ib,igrd)
             bdy_segs_surf = bdy_segs_surf + phu(nbi, nbj)                              &
@@ -207,4 +214,5 @@
             nbi => idx_bdy(ib_bdy)%nbi(ib,igrd)
             nbj => idx_bdy(ib_bdy)%nbj(ib,igrd)
+            IF( nbi == 1 .OR. nbi == jpi .OR. nbj == 1 .OR. nbj == jpj )  CYCLE
             zflagv => idx_bdy(ib_bdy)%flagv(ib,igrd)
             bdy_segs_surf = bdy_segs_surf + phv(nbi, nbj)                              &
Index: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/DYN/dynkeg.F90
===================================================================
--- NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/DYN/dynkeg.F90	(revision 11044)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/DYN/dynkeg.F90	(revision 11048)
@@ -120,6 +120,7 @@
                   igrd = 1           ! compensating null velocity on the bdy
                   DO jb = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
-                     ji   = idx_bdy(ib_bdy)%nbi(jb,igrd)   ! maximum extent : from 2 to jpi-1
-                     jj   = idx_bdy(ib_bdy)%nbj(jb,igrd)   ! maximum extent : from 2 to jpj-1
+                     ji   = idx_bdy(ib_bdy)%nbi(jb,igrd)   ! maximum extent : from 1 to jpi
+                     jj   = idx_bdy(ib_bdy)%nbj(jb,igrd)   ! maximum extent : from 1 to jpj
+                     IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE   ! to remove?
                      DO jk = 1, jpkm1
                         zhke(ji,jj,jk) = 0._wp
@@ -161,6 +162,7 @@
                   igrd = 1           ! compensation null velocity on land at the bdy
                   DO jb = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
-                     ji   = idx_bdy(ib_bdy)%nbi(jb,igrd)   ! maximum extent : from 2 to jpi-1
-                     jj   = idx_bdy(ib_bdy)%nbj(jb,igrd)   ! maximum extent : from 2 to jpj-1
+                     ji   = idx_bdy(ib_bdy)%nbi(jb,igrd)   ! maximum extent : from 1 to jpi
+                     jj   = idx_bdy(ib_bdy)%nbj(jb,igrd)   ! maximum extent : from 1 to jpj
+                     IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE   ! to remove
                      DO jk = 1, jpkm1
                         zhke(ji,jj,jk) = 0._wp
