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 11042)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdydyn2d.F90	(revision 11044)
@@ -256,61 +256,14 @@
       !! ** Purpose : Duplicate sea level across open boundaries
       !!
-      !! ** Method  : - take the average of free ocean neighbours
-      !!
-      !!      ___   !      _|    !   |_____|   !   ___|    !   __|x o   !  ___|  
-      !!   __|x     !   __|x o   !      x      !     x o   !      o     !      x o
-      !!      o     !      o     !      o      !     o     !            !      o 
-      !!                                
-      !!            (special treatments)
-      !!         !   |_   _|     ! |               
-      !!         !     |_|       ! |x o    
-      !!         !    o x o      ! |x_x_ 
-      !!         !      o      
-      !!----------------------------------------------------------------------
-      REAL(wp), DIMENSION(jpi,jpj), INTENT(inout) ::   zssh ! Sea level
-      !!
-      INTEGER  ::   ib_bdy, ib, igrd                        ! local integers
-      INTEGER  ::   ii, ij, zcoef, zcoef1, zcoef2, ip, jp   !   "       "
-      INTEGER  ::   flagu, flagv                            ! short cuts
-      REAL(wp) ::   zr_3
-      !!----------------------------------------------------------------------
-      igrd = 1                       ! Everything is at T-points here
-      zr_3 = 1. / 3.
-      !
+      !!----------------------------------------------------------------------
+      REAL(wp), DIMENSION(jpi,jpj,1), INTENT(inout) ::   zssh ! Sea level, need 3 dimensions to be used by bdy_nmn
+      !!
+      INTEGER  ::   ib_bdy          ! bdy index
+      !!----------------------------------------------------------------------
       DO ib_bdy = 1, nb_bdy
-         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)
-            SELECT CASE( idx_bdy(ib_bdy)%ntreat(ib,igrd) )
-               CASE( 0 )
-                  flagu = NINT( idx_bdy(ib_bdy)%flagu(ib,igrd) )
-                  flagv = NINT( idx_bdy(ib_bdy)%flagv(ib,igrd) )
-                  IF( flagu == 0 .OR. flagv == 0 )   THEN    ! linear bdy   o
-                     zssh(ii,ij) = zssh(ii+flagu,ij+flagv)   !           ___x___ 
-                  ELSE   !   ___  o
-                         !      | x o
-                     zssh(ii,ij) = ( zssh(ii+flagu,ij) + zssh(ii,ij+flagv) ) * 0.5
-                  END IF
-               !               !              !     _____     !     _____     
-               !  1 |   o      !  2  o   |    !  3 | x        !  4     x |    
-               !    |_x_ _     !    _ _x_|    !    |   o      !      o   | 
-               CASE( 1 )   ;   zssh(ii,ij) = zssh(ii+1,ij+1)
-               CASE( 2 )   ;   zssh(ii,ij) = zssh(ii-1,ij+1)
-               CASE( 3 )   ;   zssh(ii,ij) = zssh(ii+1,ij-1)
-               CASE( 4 )   ;   zssh(ii,ij) = zssh(ii-1,ij-1)
-               !    |_  o      !        o  _|  !     ¨¨|_|¨¨   !       o         
-               !  5  _| x o    !  6   o x |_   !  7   o x o    ! 8   o x o       
-               !    |   o      !        o   |  !        o      !    __|¨|__      
-               CASE( 5 )   ;   zssh(ii,ij) = ( zssh(ii  ,ij+1) + zssh(ii+1,ij  ) + zssh(ii  ,ij-1) ) * zr_3
-               CASE( 6 )   ;   zssh(ii,ij) = ( zssh(ii  ,ij+1) + zssh(ii-1,ij  ) + zssh(ii  ,ij-1) ) * zr_3
-               CASE( 7 )   ;   zssh(ii,ij) = ( zssh(ii-1,ij  ) + zssh(ii  ,ij-1) + zssh(ii+1,ij  ) ) * zr_3
-               CASE( 8 )   ;   zssh(ii,ij) = ( zssh(ii-1,ij  ) + zssh(ii  ,ij+1) + zssh(ii+1,ij  ) ) * zr_3
-            END SELECT
-         END DO
-
-         ! Boundary points should be updated
-         CALL lbc_bdy_lnk( 'bdydyn2d', zssh(:,:), 'T', 1., ib_bdy )
-      END DO
-
+         CALL bdy_nmn( idx_bdy(ib_bdy), 1, zssh )   ! zssh is masked
+         CALL lbc_bdy_lnk( 'bdydyn2d', zssh(:,:,1), 'T', 1., ib_bdy )
+      END DO
+      !
    END SUBROUTINE bdy_ssh
 
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 11042)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyini.F90	(revision 11044)
@@ -1246,7 +1246,8 @@
       END DO
       
-      ! detect corners and their orientation index 1 to 4 depending on the orientation
-      ! detect geometries with 3 neighbours  index 5 to 8 depending on the orientation
-      ! else                                 index 0
+      ! detect corner interior and its orientation index 1 to 4  depending on the orientation
+      ! detect corner exterior and its orientation index 5 to 8  depending on the orientation
+      ! detect geometries with 3 neighbours        index 9 to 12 depending on the orientation
+      ! else                                       index 0
       DO ib_bdy = 1, nb_bdy
          DO igrd = 1, jpbgrd
@@ -1259,5 +1260,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
                llnobdy = pmask(ii  ,ij+1) == 1.  
                llsobdy = pmask(ii  ,ij-1) == 1. 
@@ -1278,14 +1278,20 @@
                END IF
                IF( inbdy == 2 )   THEN   ! exterior of a corner
-                  idx_bdy(ib_bdy)%ntreat(ib,igrd) = 0   ! regular treatment with flags
+                  !        o      !        o      !    _____|       !       |_____  
+                  !  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
                END IF
                IF( inbdy == 3 )   THEN   ! 3 neighbours __   __
                   !    |_  o      !        o  _|  !       |_|     !       o         
-                  !  5  _| x o    !  6   o x |_   !  7   o x o    ! 8   o x o       
-                  !    |   o      !        o   |  !        o      !    __|¨|__      
-                  IF( llnobdy .AND. lleabdy .AND. llsobdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 5
-                  IF( llnobdy .AND. llwebdy .AND. llsobdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 6
-                  IF( llwebdy .AND. llsobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 7
-                  IF( llwebdy .AND. llnobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 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
                END IF
                IF( inbdy == 4 )   THEN
@@ -1297,5 +1303,4 @@
             END DO
          END DO
-         !CALL lbc_lnk( 'bdyini', ) 
       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 11042)
+++ NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdylib.F90	(revision 11044)
@@ -75,5 +75,4 @@
       REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(inout) ::   pta  ! tracer trend
       !!
-      REAL(wp) ::   zwgt           ! boundary weight
       INTEGER  ::   ib, ik, igrd   ! dummy loop indices
       INTEGER  ::   ii, ij         ! 2D addresses
@@ -434,4 +433,11 @@
       !! ** Purpose : Duplicate the value at open boundaries, zero gradient.
       !! 
+      !!
+      !! ** Method  : - take the average of free ocean neighbours
+      !!
+      !!      ___   !   |_____|   !   ___|    !   __|x o   !   |_   _|     ! |      
+      !!   __|x     !      x      !     x o   !      o     !     |_|       ! |x o   
+      !!      o     !      o      !     o     !            !    o x o      ! |x_x_ 
+      !!                                                   !      o      
       !!----------------------------------------------------------------------
       INTEGER,                    INTENT(in)     ::   igrd     ! grid index
@@ -441,10 +447,12 @@
       REAL(wp) ::   zweight
       REAL(wp), POINTER, DIMENSION(:,:,:)      :: pmask         ! land/sea mask for field
-      REAL(wp), POINTER, DIMENSION(:,:)        :: bdypmask      ! land/sea mask for field
       INTEGER  ::   ib, ik   ! dummy loop indices
       INTEGER  ::   ii, ij   ! 2D addresses
+      INTEGER  ::   ipkm1    ! size of phia third dimension minus 1
       INTEGER  ::   flagu, flagv                        ! short cuts
       INTEGER  ::   ii1, ii2, ii3, ij1, ij2, ij3
       !!----------------------------------------------------------------------
+      !
+      ipkm1 = MAX( SIZE(phia,3) - 1, 1 ) 
       !
       SELECT CASE(igrd)
@@ -458,5 +466,10 @@
          ii = idx%nbi(ib,igrd)
          ij = idx%nbj(ib,igrd)
+         !
          SELECT CASE( idx%ntreat(ib,igrd) )   ! select free ocean neighbours
+            !     o 
+            !  ___x___         ! either flagu or flagv = 0
+            CASE( 0 )   ;   flagu = NINT( idx%flagu(ib,igrd) )   ;   ii1 = ii+flagu
+                            flagv = NINT( idx%flagv(ib,igrd) )   ;   ij1 = ij+flagv
             !               !              !     _____     !     _____     
             !  1 |   o      !  2  o   |    !  3 | x        !  4     x |    
@@ -466,38 +479,37 @@
             CASE( 3 )   ;   ii1 = ii+1   ;   ij1 = ij-1
             CASE( 4 )   ;   ii1 = ii-1   ;   ij1 = ij-1
+            !        o      !        o      !    _____|       !       |_____  
+            !  5 ____x o    !  6   o x___   ! 7      x o      !  8   o x      
+            !         |     !       |       !        o        !        o      
+            CASE( 5 )   ;   ii1 = ii   ;   ij1 = ij+1   ;   ii2 = ii+1   ;   ij2 = ij   
+            CASE( 6 )   ;   ii1 = ii   ;   ij1 = ij+1   ;   ii2 = ii-1   ;   ij2 = ij
+            CASE( 7 )   ;   ii1 = ii   ;   ij1 = ij-1   ;   ii2 = ii+1   ;   ij2 = ij  
+            CASE( 8 )   ;   ii1 = ii   ;   ij1 = ij-1   ;   ii2 = ii-1   ;   ij2 = ij  
             !    |_  o      !        o  _|  !     ¨¨|_|¨¨   !       o         
-            !  5  _| x o    !  6   o x |_   !  7   o x o    ! 8   o x o       
+            !  9  _| x o    !  10  o x |_   !  11  o x o    ! 12  o x o       
             !    |   o      !        o   |  !        o      !    __|¨|__ 
-            CASE( 5 )   ;   ii1 = ii     ;   ij1 = ij+1   ;   ii2 = ii+1   ;   ij2 = ij     ;   ii3 = ii     ;   ij3 = ij-1   
-            CASE( 6 )   ;   ii1 = ii     ;   ij1 = ij+1   ;   ii2 = ii-1   ;   ij2 = ij     ;   ii3 = ii     ;   ij3 = ij-1 
-            CASE( 7 )   ;   ii1 = ii-1   ;   ij1 = ij     ;   ii2 = ii     ;   ij2 = ij-1   ;   ii3 = ii+1   ;   ij3 = ij   
-            CASE( 8 )   ;   ii1 = ii-1   ;   ij1 = ij     ;   ii2 = ii     ;   ij2 = ij+1   ;   ii3 = ii+1   ;   ij3 = ij  
-            CASE DEFAULT
+            CASE( 9  )   ;   ii1 = ii     ;   ij1 = ij+1   ;   ii2 = ii+1   ;   ij2 = ij     ;   ii3 = ii     ;   ij3 = ij-1   
+            CASE( 10 )   ;   ii1 = ii     ;   ij1 = ij+1   ;   ii2 = ii-1   ;   ij2 = ij     ;   ii3 = ii     ;   ij3 = ij-1 
+            CASE( 11 )   ;   ii1 = ii-1   ;   ij1 = ij     ;   ii2 = ii     ;   ij2 = ij-1   ;   ii3 = ii+1   ;   ij3 = ij   
+            CASE( 12 )   ;   ii1 = ii-1   ;   ij1 = ij     ;   ii2 = ii     ;   ij2 = ij+1   ;   ii3 = ii+1   ;   ij3 = ij
          END SELECT
          !
-         SELECT CASE( idx%ntreat(ib,igrd) )         
-            CASE( 0 )
-               flagu = NINT( idx%flagu(ib,igrd) )
-               flagv = NINT( idx%flagv(ib,igrd) )
-               IF( flagu == 0 .OR. flagv == 0 )   THEN     ! linear bdy   o
-                   DO ik = 1, jpkm1                        !           ___x___ 
-                      IF( pmask(ii+flagu,ij+flagv,ik) /= 0. )   phia(ii,ij,ik) = phia(ii+flagu,ij+flagv,ik)  
-                   END DO
-               ELSE                          
-                  DO ik = 1, jpkm1                                              !   ___  o
-                     zweight = pmask(ii+flagu,ij,ik) + pmask(ii,ij+flagv,ik)    !      | x o
-                     IF( zweight /= 0. )   phia(ii,ij,ik) = ( phia(ii+flagu,ij,ik) + phia(ii,ij+flagv,ik) ) / zweight
-                  END DO
-               END IF
-            CASE( 1:4 )
-                   DO ik = 1, jpkm1
-                      IF( pmask(ii1,ij1,ik) /= 0. )   phia(ii,ij,ik) = phia(ii1,ij1,ik)  
-                   END DO    
+         SELECT CASE( idx%ntreat(ib,igrd) )
+            CASE( 0:4 )
+               DO ik = 1, ipkm1
+                  IF( pmask(ii1,ij1,ik) /= 0. )   phia(ii,ij,ik) = phia(ii1,ij1,ik)  
+               END DO
             CASE( 5:8 )
-               DO ik = 1, jpkm1
-                  zweight        =  pmask(ii1,ij1,ik) + pmask(ii2,ij2,ik) + pmask(ii3,ij3,ik)
+               DO ik = 1, ipkm1
+                  zweight = pmask(ii1,ij1,ik) + pmask(ii2,ij2,ik)
+                  IF( zweight /= 0. )   phia(ii,ij,ik) = ( phia(ii1,ij1,ik) + phia (ii2,ij2,ik) ) / zweight
+               END DO
+            CASE( 9:12 )
+               DO ik = 1, ipkm1
+                  zweight = pmask(ii1,ij1,ik) + pmask(ii2,ij2,ik) + pmask(ii3,ij3,ik)
                   IF( zweight /= 0. )   phia(ii,ij,ik) = ( phia(ii1,ij1,ik) + phia (ii2,ij2,ik) + phia (ii3,ij3,ik) ) / zweight
                END DO
          END SELECT
+         !
       END DO
       !
