Index: /NEMO/trunk/src/OCE/DYN/dynkeg.F90
===================================================================
--- /NEMO/trunk/src/OCE/DYN/dynkeg.F90	(revision 10995)
+++ /NEMO/trunk/src/OCE/DYN/dynkeg.F90	(revision 10996)
@@ -74,10 +74,10 @@
       INTEGER, INTENT( in ) ::   kscheme   ! =0/1   type of KEG scheme 
       !
-      INTEGER  ::   ji, jj, jk, jb    ! dummy loop indices
-      INTEGER  ::   ii, ifu, ib_bdy   ! local integers
-      INTEGER  ::   ij, ifv, igrd     !   -       -
-      REAL(wp) ::   zu, zv            ! local scalars
+      INTEGER  ::   ji, jj, jk, jb           ! dummy loop indices
+      INTEGER  ::   ifu, ifv, igrd, ib_bdy   ! local integers
+      REAL(wp) ::   zu, zv                   ! local scalars
       REAL(wp), DIMENSION(jpi,jpj,jpk)        ::   zhke
       REAL(wp), ALLOCATABLE, DIMENSION(:,:,:) ::   ztrdu, ztrdv 
+      REAL(wp)  :: zweightu, zweightv
       !!----------------------------------------------------------------------
       !
@@ -97,32 +97,4 @@
       
       zhke(:,:,jpk) = 0._wp
-      
-      IF (ln_bdy) THEN
-         ! Maria Luneva & Fred Wobus: July-2016
-         ! compensate for lack of turbulent kinetic energy on liquid bdy points
-         DO ib_bdy = 1, nb_bdy
-            IF( cn_dyn3d(ib_bdy) /= 'none' ) THEN
-               igrd = 2           ! Copying normal velocity into points outside bdy
-               DO jb = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
-                  DO jk = 1, jpkm1
-                     ii   = idx_bdy(ib_bdy)%nbi(jb,igrd)
-                     ij   = idx_bdy(ib_bdy)%nbj(jb,igrd)
-                     ifu   = NINT( idx_bdy(ib_bdy)%flagu(jb,igrd) )
-                     un(ii-ifu,ij,jk) = un(ii,ij,jk) * umask(ii,ij,jk)
-                  END DO
-               END DO
-               !
-               igrd = 3           ! Copying normal velocity into points outside bdy
-               DO jb = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
-                  DO jk = 1, jpkm1
-                     ii   = idx_bdy(ib_bdy)%nbi(jb,igrd)
-                     ij   = idx_bdy(ib_bdy)%nbj(jb,igrd)
-                     ifv   = NINT( idx_bdy(ib_bdy)%flagv(jb,igrd) )
-                     vn(ii,ij-ifv,jk) = vn(ii,ij,jk) * vmask(ii,ij,jk)
-                  END DO
-               END DO
-            ENDIF
-         ENDDO  
-      ENDIF 
 
       SELECT CASE ( kscheme )             !== Horizontal kinetic energy at T-point  ==!
@@ -140,4 +112,28 @@
             END DO
          END DO
+         !
+         IF (ln_bdy) THEN
+            ! Maria Luneva & Fred Wobus: July-2016
+            ! compensate for lack of turbulent kinetic energy on liquid bdy points
+            DO ib_bdy = 1, nb_bdy
+               IF( cn_dyn3d(ib_bdy) /= 'none' ) THEN
+                  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
+                     DO jk = 1, jpkm1
+                        zhke(ji,jj,jk) = 0._wp
+                        zweightu = umask(ji-1,jj  ,jk) + umask(ji,jj,jk)
+                        zweightv = vmask(ji  ,jj-1,jk) + vmask(ji,jj,jk)
+                        zu = un(ji-1,jj  ,jk) * un(ji-1,jj  ,jk)  +  un(ji  ,jj  ,jk) * un(ji  ,jj  ,jk)
+                        zv = vn(ji  ,jj-1,jk) * vn(ji  ,jj-1,jk)  +  vn(ji  ,jj  ,jk) * vn(ji  ,jj  ,jk)
+                        IF( zweightu > 0._wp )   zhke(ji,jj,jk) =  zhke(ji,jj,jk) + zu / (2._wp * zweightu) 
+                        IF( zweightv > 0._wp )   zhke(ji,jj,jk) =  zhke(ji,jj,jk) + zv / (2._wp * zweightv) 
+                     END DO
+                  END DO
+               END IF
+               CALL lbc_bdy_lnk( 'dynkeg', zhke, 'T', 1., ib_bdy )   ! send 2 and recv jpi, jpj used in the computation of the speed tendencies
+            END DO
+         END IF
          !
       CASE ( nkeg_HW )                          !--  Hollingsworth scheme  --!
@@ -158,14 +154,37 @@
             END DO
          END DO
+         IF (ln_bdy) THEN
+            ! Maria Luneva & Fred Wobus: July-2016
+            ! compensate for lack of turbulent kinetic energy on liquid bdy points
+            DO ib_bdy = 1, nb_bdy
+               IF( cn_dyn3d(ib_bdy) /= 'none' ) THEN
+                  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
+                     DO jk = 1, jpkm1
+                        zhke(ji,jj,jk) = 0._wp
+                        zweightu = 8._wp * ( umask(ji-1,jj  ,jk) + umask(ji  ,jj  ,jk) ) &
+                             &   + 2._wp * ( umask(ji-1,jj-1,jk) + umask(ji-1,jj+1,jk) + umask(ji  ,jj-1,jk) + umask(ji  ,jj+1,jk) ) 
+                        zweightv = 8._wp * ( vmask(ji  ,jj-1,jk) + vmask(ji  ,jj-1,jk) ) &
+                             &   + 2._wp * ( vmask(ji-1,jj-1,jk) + vmask(ji+1,jj-1,jk) + vmask(ji-1,jj  ,jk) + vmask(ji+1,jj  ,jk) )
+                        zu = 8._wp * ( un(ji-1,jj  ,jk) * un(ji-1,jj  ,jk)    &
+                           &         + un(ji  ,jj  ,jk) * un(ji  ,jj  ,jk) )  &
+                           &   +     ( un(ji-1,jj-1,jk) + un(ji-1,jj+1,jk) ) * ( un(ji-1,jj-1,jk) + un(ji-1,jj+1,jk) )   &
+                           &   +     ( un(ji  ,jj-1,jk) + un(ji  ,jj+1,jk) ) * ( un(ji  ,jj-1,jk) + un(ji  ,jj+1,jk) )
+                        zv = 8._wp * ( vn(ji  ,jj-1,jk) * vn(ji  ,jj-1,jk)    &
+                           &         + vn(ji  ,jj  ,jk) * vn(ji  ,jj  ,jk) )  &
+                           &  +      ( vn(ji-1,jj-1,jk) + vn(ji+1,jj-1,jk) ) * ( vn(ji-1,jj-1,jk) + vn(ji+1,jj-1,jk) )   &
+                           &  +      ( vn(ji-1,jj  ,jk) + vn(ji+1,jj  ,jk) ) * ( vn(ji-1,jj  ,jk) + vn(ji+1,jj  ,jk) )
+                        IF( zweightu > 0._wp )   zhke(ji,jj,jk) =  zhke(ji,jj,jk) +  zu / ( 2._wp * zweightu )
+                        IF( zweightv > 0._wp )   zhke(ji,jj,jk) =  zhke(ji,jj,jk) +  zv / ( 2._wp * zweightv )
+                     END DO
+                  END DO
+               END IF
+            END DO
+         END IF
          CALL lbc_lnk( 'dynkeg', zhke, 'T', 1. )
          !
-      END SELECT
-
-      IF (ln_bdy) THEN
-         ! restore velocity masks at points outside boundary
-         un(:,:,:) = un(:,:,:) * umask(:,:,:)
-         vn(:,:,:) = vn(:,:,:) * vmask(:,:,:)
-      ENDIF      
-
+      END SELECT 
       !
       DO jk = 1, jpkm1                    !==  grad( KE ) added to the general momentum trends  ==!
