Index: NEMO/branches/2019/dev_r11233_AGRIF-05_jchanut_vert_coord_interp/src/NST/agrif_oce_sponge.F90
===================================================================
--- NEMO/branches/2019/dev_r11233_AGRIF-05_jchanut_vert_coord_interp/src/NST/agrif_oce_sponge.F90	(revision 11238)
+++ NEMO/branches/2019/dev_r11233_AGRIF-05_jchanut_vert_coord_interp/src/NST/agrif_oce_sponge.F90	(revision 11574)
@@ -22,4 +22,5 @@
    USE agrif_oce
    USE lbclnk          ! ocean lateral boundary conditions (or mpp link)
+   USE iom
 
    IMPLICIT NONE
@@ -58,4 +59,7 @@
 #endif
       !
+      CALL iom_put("fsaht_spu", fsaht_spu(:,:))
+      CALL iom_put("fsaht_spv", fsaht_spv(:,:))
+      !
    END SUBROUTINE Agrif_Sponge_Tra
 
@@ -85,4 +89,7 @@
 #endif
       !
+      CALL iom_put("fsahm_spt", fsahm_spt(:,:))
+      CALL iom_put("fsahm_spf", fsahm_spf(:,:))
+      !
    END SUBROUTINE Agrif_Sponge_dyn
 
@@ -93,62 +100,146 @@
       !!----------------------------------------------------------------------
       INTEGER  ::   ji, jj, ind1, ind2
-      INTEGER  ::   ispongearea
-      REAL(wp) ::   z1_spongearea
+      INTEGER  ::   ispongearea, jspongearea
+      REAL(wp) ::   z1_ispongearea, z1_jspongearea
       REAL(wp), DIMENSION(jpi,jpj) :: ztabramp
+      REAL(wp), DIMENSION(jpjmax)  :: zmskwest,  zmskeast
+      REAL(wp), DIMENSION(jpimax)  :: zmsknorth, zmsksouth
       !!----------------------------------------------------------------------
       !
 #if defined SPONGE || defined SPONGE_TOP
       IF (( .NOT. spongedoneT ).OR.( .NOT. spongedoneU )) THEN
+         !
+         ! Retrieve masks at open boundaries:
+
+         ! --- West --- !
+         ztabramp(:,:) = 0._wp
+         ind1 = 1+nbghostcells
+         DO ji = mi0(ind1), mi1(ind1)                
+            ztabramp(ji,:) = umask(ji,:,1)
+         END DO
+         !
+         zmskwest(:) = 0._wp
+         zmskwest(1:jpj) = MAXVAL(ztabramp(:,:), dim=1)
+
+         ! --- East --- !
+         ztabramp(:,:) = 0._wp
+         ind1 = jpiglo - nbghostcells - 1
+         DO ji = mi0(ind1), mi1(ind1)                 
+            ztabramp(ji,:) = umask(ji,:,1)
+         END DO
+         !
+         zmskeast(:) = 0._wp
+         zmskeast(1:jpj) = MAXVAL(ztabramp(:,:), dim=1)
+
+         ! --- South --- !
+         ztabramp(:,:) = 0._wp
+         ind1 = 1+nbghostcells
+         DO jj = mj0(ind1), mj1(ind1)                 
+            ztabramp(:,jj) = vmask(:,jj,1)
+         END DO
+         !
+         zmsksouth(:) = 0._wp
+         zmsksouth(1:jpi) = MAXVAL(ztabramp(:,:), dim=2)
+
+         ! --- North --- !
+         ztabramp(:,:) = 0._wp
+         ind1 = jpjglo - nbghostcells - 1
+         DO jj = mj0(ind1), mj1(ind1)                 
+            ztabramp(:,jj) = vmask(:,jj,1)
+         END DO
+         !
+         zmsknorth(:) = 0._wp
+         zmsknorth(1:jpi) = MAXVAL(ztabramp(:,:), dim=2)
+
+#if defined key_mpp_mpi
+         CALL mpp_max( 'AGRIF_sponge', zmskwest(:) , jpjmax )
+         CALL mpp_max( 'AGRIF_Sponge', zmskeast(:) , jpjmax )
+         CALL mpp_max( 'AGRIF_Sponge', zmsksouth(:), jpimax )
+         CALL mpp_max( 'AGRIF_Sponge', zmsknorth(:), jpimax )
+#endif
+
          ! Define ramp from boundaries towards domain interior at T-points
          ! Store it in ztabramp
 
          ispongearea  = 1 + nn_sponge_len * Agrif_irhox()
-         z1_spongearea = 1._wp / REAL( ispongearea )
+         z1_ispongearea = 1._wp / REAL( ispongearea )
+         jspongearea  = 1 + nn_sponge_len * Agrif_irhoy()
+         z1_jspongearea = 1._wp / REAL( jspongearea )
          
          ztabramp(:,:) = 0._wp
+         IF ( Agrif_irhox()==1 ) ispongearea =-1
+         IF ( Agrif_irhoy()==1 ) jspongearea =-1
 
          ! --- West --- !
-         IF( (nbondi == -1) .OR. (nbondi == 2) ) THEN
-            ind1 = 1+nbghostcells
-            ind2 = 1+nbghostcells + ispongearea 
+         ind1 = 1+nbghostcells
+         ind2 = 1+nbghostcells + ispongearea 
+         DO ji = mi0(ind1), mi1(ind2)   
+            DO jj = 1, jpj               
+               ztabramp(ji,jj) = REAL( ind2 - mig(ji) ) * z1_ispongearea * zmskwest(jj)
+            END DO
+         END DO
+
+         ! ghost cells (cosmetic):
+         ind1 = 1
+         ind2 = nbghostcells
+         DO ji = mi0(ind1), mi1(ind2)   
+            DO jj = 1, jpj               
+               ztabramp(ji,jj) = zmskwest(jj)
+            END DO
+         END DO
+
+         ! --- East --- !
+         ind1 = jpiglo - nbghostcells - ispongearea
+         ind2 = jpiglo - nbghostcells
+         DO ji = mi0(ind1), mi1(ind2)
             DO jj = 1, jpj
-               DO ji = ind1, ind2                
-                  ztabramp(ji,jj) = REAL( ind2 - ji ) * z1_spongearea * umask(ind1,jj,1)
-               END DO
+               ztabramp(ji,jj) = MAX( ztabramp(ji,jj), REAL( mig(ji) - ind1 ) * z1_ispongearea) * zmskeast(jj)
             ENDDO
-         ENDIF
-
-         ! --- East --- !
-         IF( (nbondi == 1) .OR. (nbondi == 2) ) THEN
-            ind1 = nlci - nbghostcells - ispongearea
-            ind2 = nlci - nbghostcells
+         END DO
+
+         ! ghost cells (cosmetic):
+         ind1 = jpiglo - nbghostcells + 1
+         ind2 = jpiglo
+         DO ji = mi0(ind1), mi1(ind2)
             DO jj = 1, jpj
-               DO ji = ind1, ind2
-                  ztabramp(ji,jj) = MAX( ztabramp(ji,jj), REAL( ji - ind1 ) * z1_spongearea * umask(ind2-1,jj,1) )
-               ENDDO
+               ztabramp(ji,jj) = zmskeast(jj)
             ENDDO
-         ENDIF
+         END DO
 
          ! --- South --- !
-         IF( (nbondj == -1) .OR. (nbondj == 2) ) THEN
-            ind1 = 1+nbghostcells
-            ind2 = 1+nbghostcells + ispongearea
-            DO jj = ind1, ind2 
-               DO ji = 1, jpi
-                  ztabramp(ji,jj) = MAX( ztabramp(ji,jj), REAL( ind2 - jj ) * z1_spongearea * vmask(ji,ind1,1) )
-               END DO
-            ENDDO
-         ENDIF
+         ind1 = 1+nbghostcells
+         ind2 = 1+nbghostcells + jspongearea
+         DO jj = mj0(ind1), mj1(ind2) 
+            DO ji = 1, jpi
+               ztabramp(ji,jj) = MAX( ztabramp(ji,jj), REAL( ind2 - mjg(jj) ) * z1_jspongearea) * zmsksouth(ji)
+            END DO
+         END DO
+
+         ! ghost cells (cosmetic):
+         ind1 = 1
+         ind2 = nbghostcells
+         DO jj = mj0(ind1), mj1(ind2) 
+            DO ji = 1, jpi
+               ztabramp(ji,jj) = zmsksouth(ji)
+            END DO
+         END DO
 
          ! --- North --- !
-         IF( (nbondj == 1) .OR. (nbondj == 2) ) THEN
-            ind1 = nlcj - nbghostcells - ispongearea
-            ind2 = nlcj - nbghostcells
-            DO jj = ind1, ind2
-               DO ji = 1, jpi
-                  ztabramp(ji,jj) = MAX( ztabramp(ji,jj), REAL( jj - ind1 ) * z1_spongearea * vmask(ji,ind2-1,1) )
-               END DO
-            ENDDO
-         ENDIF
+         ind1 = jpjglo - nbghostcells - jspongearea
+         ind2 = jpjglo - nbghostcells
+         DO jj = mj0(ind1), mj1(ind2)
+            DO ji = 1, jpi
+               ztabramp(ji,jj) = MAX( ztabramp(ji,jj), REAL( mjg(jj) - ind1 ) * z1_jspongearea) * zmsknorth(ji)
+            END DO
+         END DO
+
+         ! ghost cells (cosmetic):
+         ind1 = jpjglo - nbghostcells + 1
+         ind2 = jpjglo
+         DO jj = mj0(ind1), mj1(ind2)
+            DO ji = 1, jpi
+               ztabramp(ji,jj) = zmsknorth(ji)
+            END DO
+         END DO
 
       ENDIF
@@ -160,10 +251,10 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1   ! vector opt.
-               fsaht_spu(ji,jj) = 0.5_wp * visc_tra * ( ztabramp(ji,jj) + ztabramp(ji+1,jj  ) )
-               fsaht_spv(ji,jj) = 0.5_wp * visc_tra * ( ztabramp(ji,jj) + ztabramp(ji  ,jj+1) )
-            END DO
-         END DO
-         CALL lbc_lnk( 'agrif_oce_sponge', fsaht_spu, 'U', 1. )   ! Lateral boundary conditions
-         CALL lbc_lnk( 'agrif_oce_sponge', fsaht_spv, 'V', 1. )
+               fsaht_spu(ji,jj) = 0.5_wp * rn_sponge_tra * ( ztabramp(ji,jj) + ztabramp(ji+1,jj  ) )
+               fsaht_spv(ji,jj) = 0.5_wp * rn_sponge_tra * ( ztabramp(ji,jj) + ztabramp(ji  ,jj+1) )
+            END DO
+         END DO
+         CALL lbc_lnk( 'agrif_Sponge', fsaht_spu, 'U', 1. )   ! Lateral boundary conditions
+         CALL lbc_lnk( 'agrif_Sponge', fsaht_spv, 'V', 1. )
          
          spongedoneT = .TRUE.
@@ -176,11 +267,11 @@
          DO jj = 2, jpjm1
             DO ji = 2, jpim1   ! vector opt.
-               fsahm_spt(ji,jj) = visc_dyn * ztabramp(ji,jj)
-               fsahm_spf(ji,jj) = 0.25_wp * visc_dyn * ( ztabramp(ji  ,jj  ) + ztabramp(ji  ,jj+1) &
-                                                     &  +ztabramp(ji+1,jj+1) + ztabramp(ji+1,jj  ) )
-            END DO
-         END DO
-         CALL lbc_lnk( 'agrif_oce_sponge', fsahm_spt, 'T', 1. )   ! Lateral boundary conditions
-         CALL lbc_lnk( 'agrif_oce_sponge', fsahm_spf, 'F', 1. )
+               fsahm_spt(ji,jj) = rn_sponge_dyn * ztabramp(ji,jj)
+               fsahm_spf(ji,jj) = 0.25_wp * rn_sponge_dyn * ( ztabramp(ji  ,jj  ) + ztabramp(ji  ,jj+1) &
+                                                          &  +ztabramp(ji+1,jj+1) + ztabramp(ji+1,jj  ) )
+            END DO
+         END DO
+         CALL lbc_lnk( 'agrif_Sponge', fsahm_spt, 'T', 1. )   ! Lateral boundary conditions
+         CALL lbc_lnk( 'agrif_Sponge', fsahm_spf, 'F', 1. )
          
          spongedoneU = .TRUE.
