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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn2d.F90	(revision 11049)
@@ -99,5 +99,4 @@
          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)
@@ -108,5 +107,4 @@
          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)
@@ -165,5 +163,4 @@
          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 
@@ -180,9 +177,9 @@
       DO jb = 1, idx%nblenrim(igrd)
          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
+         ij  = idx%nbj(jb,igrd)
          flagu => idx%flagu(jb,igrd)
          iim1 = ii + MAX( 0, INT( flagu ) )   ! T pts i-indice inside the boundary
          iip1 = ii - MIN( 0, INT( flagu ) )   ! T pts i-indice outside the boundary 
+         IF( iim1 > jpi .OR. iip1 > jpi )   CYCLE
          !
          zcorr = - flagu * SQRT( grav * phur(ii, ij) ) * ( pssh(iim1, ij) - spgu(iip1,ij) )
@@ -199,9 +196,9 @@
       DO jb = 1, idx%nblenrim(igrd)
          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
+         ij  = idx%nbj(jb,igrd)
          flagv => idx%flagv(jb,igrd)
          ijm1 = ij + MAX( 0, INT( flagv ) )   ! T pts j-indice inside the boundary
          ijp1 = ij - MIN( 0, INT( flagv ) )   ! T pts j-indice outside the boundary 
+         IF( ijm1 > jpj .OR. ijp1 > jpj )   CYCLE
          !
          zcorr = - flagv * SQRT( grav * phvr(ii, ij) ) * ( pssh(ii, ijm1) - spgu(ii,ijp1) )
@@ -210,5 +207,5 @@
          ! Use characteristics method instead
          zflag = ABS(flagv)
-         zforc  = dta%v2d(jb) * (1._wp - z1_2*zflag) + z1_2 * zflag * pva2d(ii,ijm1)
+         zforc = dta%v2d(jb) * (1._wp - z1_2*zflag) + z1_2 * zflag * pva2d(ii,ijm1)
          pva2d(ii,ij) = zforc + (1._wp - z1_2*zflag) * zcorr * vmask(ii,ij,1)
       END DO
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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn3d.F90	(revision 11049)
@@ -86,5 +86,4 @@
             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
@@ -96,5 +95,4 @@
             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
@@ -128,10 +126,8 @@
          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      
             !
@@ -147,10 +143,8 @@
          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      
             !
@@ -189,5 +183,4 @@
          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
@@ -199,5 +192,4 @@
          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
@@ -236,5 +228,4 @@
             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)
@@ -247,5 +238,4 @@
             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)
@@ -316,5 +306,4 @@
                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
@@ -328,5 +317,4 @@
                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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyice.F90	(revision 11049)
@@ -116,5 +116,4 @@
             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)
@@ -148,5 +147,4 @@
             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)
@@ -155,8 +153,20 @@
             jpbound = 0   ;   ib = ji   ;   jb = jj
             !
-            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( flagu ==  1. )   THEN
+               IF( ji+1 > jpi  )   CYCLE
+               IF( u_ice(ji  ,jj  ) < 0. )   jpbound = 1 ; ib = ji+1
+            END IF
+            IF( flagu == -1. )   THEN
+               IF( ji-1 < 1    )   CYCLE
+               IF( u_ice(ji-1,jj  ) < 0. )   jpbound = 1 ; ib = ji-1
+            END IF
+            IF( flagv ==  1. )   THEN
+               IF( ji+1 > jpj )   CYCLE
+               IF( v_ice(ji  ,jj  ) < 0. )   jpbound = 1 ; jb = jj+1
+            END IF
+            IF( flagv == -1. )   THEN
+               IF( jj-1 < 1   )   CYCLE
+               IF( v_ice(ji  ,jj-1) < 0. )   jpbound = 1 ; jb = jj-1
+            END IF
             !
             IF( nn_ice_dta(jbdy) == 0 )   jpbound = 0 ; ib = ji ; jb = jj   ! case ice boundaries = initial conditions
@@ -308,5 +318,4 @@
                   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)
                   !     i-1  i   i    |  !        i  i i+1 |  !          i  i i+1 |
@@ -337,5 +346,4 @@
                   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)
                   !    ¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨     !  ¨¨¨¨ïce¨¨¨(jj+1)¨¨     ! ¨¨¨¨¨¨ö¨¨¨¨(jj+1)       
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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyini.F90	(revision 11049)
@@ -132,5 +132,5 @@
       INTEGER  ::   jpbdtau, jpbdtas                       !   -       -
       INTEGER  ::   ib_bdy1, ib_bdy2, ib1, ib2             !   -       -
-      INTEGER  ::   i_offset, j_offset, inbdy            !   -       -
+      INTEGER  ::   i_offset, j_offset, inbdy, itreat      !   -       -
       INTEGER , POINTER  ::  nbi, nbj, nbr                 ! short cuts
       REAL(wp), POINTER, DIMENSION(:,:)       ::   pmask    ! pointer to 2D mask fields
@@ -146,4 +146,5 @@
       REAL(wp), TARGET, DIMENSION(jpi,jpj) ::   zfmask  ! temporary fmask array excluding coastal boundary condition (shlat)
       REAL(wp)        , DIMENSION(jpi,jpj) ::   ztmp
+      REAL(wp), POINTER                    :: flagu,     flagv     ! short cuts
       LOGICAL  ::   llnobdy, llsobdy, lleabdy, llwebdy     ! local logicals
       !!
@@ -802,5 +803,5 @@
       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
+      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
 
@@ -1274,6 +1275,6 @@
             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.  
@@ -1327,4 +1328,5 @@
          END DO
       END DO
+
       !
       ! Tidy up
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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdylib.F90	(revision 11049)
@@ -56,5 +56,4 @@
             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)
@@ -84,5 +83,4 @@
          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)
@@ -468,5 +466,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 
@@ -499,8 +497,11 @@
          SELECT CASE( idx%ntreat(ib,igrd) )
             CASE( 0:4 )
+               IF( ii1 < 1 .OR. ii1 > jpi .OR. ij1 < 1 .OR. ij1 > jpj )   CYCLE
                DO ik = 1, ipkm1
                   IF( pmask(ii1,ij1,ik) /= 0. )   phia(ii,ij,ik) = phia(ii1,ij1,ik)  
                END DO
             CASE( 5:8 )
+               IF( ii1 < 1 .OR. ii1 > jpi .OR. ij1 < 1 .OR. ij1 > jpj )   CYCLE
+               IF( ii2 < 1 .OR. ii2 > jpi .OR. ij2 < 1 .OR. ij2 > jpj )   CYCLE
                DO ik = 1, ipkm1
                   zweight = pmask(ii1,ij1,ik) + pmask(ii2,ij2,ik)
@@ -508,4 +509,7 @@
                END DO
             CASE( 9:12 )
+               IF( ii1 < 1 .OR. ii1 > jpi .OR. ij1 < 1 .OR. ij1 > jpj )   CYCLE
+               IF( ii2 < 1 .OR. ii2 > jpi .OR. ij2 < 1 .OR. ij2 > jpj )   CYCLE
+               IF( ii3 < 1 .OR. ii3 > jpi .OR. ij3 < 1 .OR. ij3 > jpj )   CYCLE
                DO ik = 1, ipkm1
                   zweight = pmask(ii1,ij1,ik) + pmask(ii2,ij2,ik) + pmask(ii3,ij3,ik)
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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdytra.F90	(revision 11049)
@@ -104,5 +104,4 @@
             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
@@ -136,5 +135,4 @@
                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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyvol.F90	(revision 11049)
@@ -99,5 +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...
+            IF( ii == 1 .OR. ii == jpi .OR. ij == 1 .OR. ij == jpj )  CYCLE   ! to remove
             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
@@ -106,5 +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?
+            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
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 11048)
+++ /NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/DYN/dynkeg.F90	(revision 11049)
@@ -122,5 +122,5 @@
                      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?
+                     IF( ji == 1 .OR. jj == 1 )  CYCLE
                      DO jk = 1, jpkm1
                         zhke(ji,jj,jk) = 0._wp
@@ -164,5 +164,5 @@
                      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
+                     IF( ji == 1 .OR. ji == jpi .OR. jj == 1 .OR. jj == jpj )  CYCLE
                      DO jk = 1, jpkm1
                         zhke(ji,jj,jk) = 0._wp
