Skip to content
Snippets Groups Projects
advec_weno_k_3_aux.f90 188 KiB
Newer Older
!MNH_LIC Copyright 1994-2019 CNRS, Meteo-France and Universite Paul Sabatier
!MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
!MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
!MNH_LIC for details. version 1.
!-----------------------------------------------------------------
! Modifications:
!  P. Wautelet 10/04/2019: replace ABORT and STOP calls by Print_msg
!-----------------------------------------------------------------
!     ##############################
      MODULE MODI_ADVEC_WENO_K_3_AUX
!     ##############################
!
INTERFACE
!
      SUBROUTINE ADVEC_WENO_K_3_UX(HLBCX,PSRC, PRUCT, PR)
!
USE MODE_ll
USE MODD_LUNIT
USE MODD_CONF
!
CHARACTER (LEN=4), DIMENSION(2), INTENT(IN) :: HLBCX  ! X direction LBC type
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on U grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRUCT ! contrav. comp. on MASS GRID
!
! output source term
REAL, DIMENSION(:,:,:), INTENT(OUT) :: PR
!
END SUBROUTINE ADVEC_WENO_K_3_UX
!
!---------------------------------------------------------------------------------
      SUBROUTINE ADVEC_WENO_K_3_MX(HLBCX,PSRC, PRUCT, PR)
!
USE MODE_ll
USE MODD_LUNIT
USE MODD_CONF
!
CHARACTER (LEN=4), DIMENSION(2), INTENT(IN) :: HLBCX  ! X direction LBC type
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on U grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRUCT ! contrav. comp. on MASS GRID
!
! output source term
REAL, DIMENSION(:,:,:), INTENT(OUT) :: PR
!
END SUBROUTINE ADVEC_WENO_K_3_MX
!
!---------------------------------------------------------------------------------
      SUBROUTINE ADVEC_WENO_K_3_VY(HLBCY,PSRC, PRVCT, PR)
!
USE MODE_ll
USE MODD_LUNIT
USE MODD_CONF
!
CHARACTER (LEN=4), DIMENSION(2), INTENT(IN) :: HLBCY  ! Y direction LBC type
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on U grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRVCT ! contrav. comp. on MASS GRID
!
!
! output source term
REAL, DIMENSION(:,:,:), INTENT(OUT) :: PR
!
END SUBROUTINE ADVEC_WENO_K_3_VY
!
!---------------------------------------------------------------------------------
      SUBROUTINE ADVEC_WENO_K_3_MY(HLBCY,PSRC, PRVCT, PR)
!
USE MODE_ll
USE MODD_LUNIT
USE MODD_CONF
!
CHARACTER (LEN=4), DIMENSION(2), INTENT(IN) :: HLBCY  ! Y direction LBC type
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on U grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRVCT ! contrav. comp. on MASS GRID
!
! output source term
REAL, DIMENSION(:,:,:), INTENT(OUT) :: PR
!
END SUBROUTINE ADVEC_WENO_K_3_MY
!
!---------------------------------------------------------------------------------
!
FUNCTION WENO_K_3_WZ(PSRC, PRWCT) RESULT(PR)
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on W grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRWCT ! contrav. comp. on MASS GRID
!
! output source term
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)) :: PR
!
END FUNCTION WENO_K_3_WZ
!
!---------------------------------------------------------------------------------
!
FUNCTION WENO_K_3_MZ(PSRC, PRWCT) RESULT(PR)
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on MASS grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRWCT ! contrav. comp. on W grid
!
! output source term
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)) :: PR
!
END FUNCTION WENO_K_3_MZ
!
END INTERFACE
!
END MODULE MODI_ADVEC_WENO_K_3_AUX
!
!-----------------------------------------------------------------------------
!
!     ############################################################
      SUBROUTINE ADVEC_WENO_K_3_UX(HLBCX,PSRC, PRUCT, PR)
!     ############################################################
!!
!!**** Computes PRUCT * PUT. Upstream fluxes of U in X direction.  
!!     Input PUT is on U Grid 'ie' (i,j,k) based on UGRID reference
!!     Output PR is on mass Grid 'ie' (i+1/2,j,k) based on UGRID reference
!!              
!!    AUTHOR
!!    ------
!!    F. Visentin   *CNRS/LA*               
!!
!!    MODIFICATIONS
!!    -------------
!!    T. Lunet 02/10/2014:  Correction of periodic boudary conditions
!!       Change of structure in order to adapt WENO to NHALOK
!!       Suppression of second layer HALO pointers
!!       Complete code documentation
!!      J.Escobar : 25/09/2015 : WENO5 & JPHEXT <> 1 
!!      J.Escobar : 02/10/2015 : correction on CYCL/OPEN boundaries
!!
!-------------------------------------------------------------------------------
!
USE MODD_CONF
!
IMPLICIT NONE
!
!*       0.1   Declarations of dummy arguments :
!
CHARACTER (LEN=4), DIMENSION(2), INTENT(IN) :: HLBCX  ! X direction LBC type
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on U grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRUCT ! contrav. comp. on MASS GRID
!
! output source term
REAL, DIMENSION(:,:,:), INTENT(OUT) :: PR
!
!*       0.2   Declarations of local variables :
!
INTEGER :: IIB,IJB    ! Begining useful area in x,y,z directions
INTEGER :: IIE,IJE    ! End useful area in x,y,z directions
INTEGER :: IW,IE      ! Physical boundary index
!
INTEGER:: ILUOUT,IRESP   ! for prints
!
! intermediate reconstruction fluxes for positive wind case
!
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZFPOS1, ZFPOS2, ZFPOS3
!
! intermediate reconstruction fluxes for negative wind case
!
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZFNEG1, ZFNEG2, ZFNEG3
!
! smoothness indicators for positive wind case
!
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZBPOS1, ZBPOS2, ZBPOS3
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZBNEG1, ZBNEG2, ZBNEG3
!
!
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)) :: ZOMP1, ZOMP2, ZOMP3
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)) :: ZOMN1, ZOMN2, ZOMN3
!
! EPSILON for weno weights calculation
! 
REAL, PARAMETER :: ZEPS = 1.0E-15
!
!-----------------------------------------------------------------------------
!*       0.3.     COMPUTES THE DOMAIN DIMENSIONS
!                 ------------------------------
!
CALL GET_INDICE_ll(IIB,IJB,IIE,IJE)
!
!-------------------------------------------------------------------------------
!*       0.4.   INITIALIZE THE FIELD 
!               ---------------------
!
PR(:,:,:) = 0.0
!
ZFPOS1  = 0.0
ZFPOS2  = 0.0
ZFPOS3  = 0.0
ZFNEG1  = 0.0
ZFNEG2  = 0.0
ZFNEG3  = 0.0
ZBPOS1  = 0.0
ZBPOS2  = 0.0
ZBPOS3  = 0.0
ZBNEG1  = 0.0
ZBNEG2  = 0.0
ZBNEG3  = 0.0
ZOMP1   = 0.0
ZOMP2   = 0.0
ZOMP3   = 0.0
ZOMN1   = 0.0
ZOMN2   = 0.0
ZOMN3   = 0.0 
!
!-------------------------------------------------------------------------------
!*       1.1.   Interior Fluxes 
!              ---------------------
IW=IIB
IE=IIE
!-------------------------------------------------------------------------------
! Flux calculation in the physical domain far enough from the boundary 
! WENO scheme order 5, IW+1 -> IE-2
! Computation at the mass point on Ugrid u(i+1/2,j,k)
!-------------------------------------------------------------------------------
! ----- Positive fluxes -----
!
! First positive stencil, needs indices i-2, i-1, i
ZFPOS1(IW+1:IE-2,:,:) = 1./6.   * (2.0*PSRC(IW-1:IE-4,:,:) - 7.0*PSRC(IW:IE-3,:,:) + 11.0*PSRC(IW+1:IE-2,:,:)) ! Flux
ZBPOS1(IW+1:IE-2,:,:) = 13./12. * (    PSRC(IW-1:IE-4,:,:) - 2.0*PSRC(IW:IE-3,:,:) +      PSRC(IW+1:IE-2,:,:))**2 & 
                       + 1./4    * (    PSRC(IW-1:IE-4,:,:) - 4.0*PSRC(IW:IE-3,:,:) + 3.0* PSRC(IW+1:IE-2,:,:))**2 ! Smoothness indicator
ZOMP1(IW+1:IE-2,:,:)  = 1./10. /  (ZEPS + ZBPOS1(IW+1:IE-2,:,:))**2
!
! Second positive stencil, needs indices i-1, i, i+1
ZFPOS2(IW+1:IE-2,:,:) = 1./6.  * (-1.0*PSRC(IW:IE-3,:,:) + 5.0*PSRC(IW+1:IE-2,:,:) + 2.0*PSRC(IW+2:IE-1,:,:))! Flux
ZBPOS2(IW+1:IE-2,:,:) = 13./12 * (     PSRC(IW:IE-3,:,:) - 2.0*PSRC(IW+1:IE-2,:,:) +     PSRC(IW+2:IE-1,:,:))**2 &
                         + 1./4   * (  PSRC(IW:IE-3,:,:) - PSRC(IW+2:IE-1,:,:))**2! Smoothness indicator
ZOMP2(IW+1:IE-2,:,:)  = 3./5. /  (ZEPS + ZBPOS2(IW+1:IE-2,:,:))**2
!
! Third positive stencil, needs indices i, i+1, i+2
ZFPOS3(IW+1:IE-2,:,:) = 1./6   * (2.0*PSRC(IW+1:IE-2,:,:) + 5.0*PSRC(IW+2:IE-1,:,:) - PSRC(IW+3:IE,:,:))! Flux
ZBPOS3(IW+1:IE-2,:,:) = 13./12 * (PSRC(IW+1:IE-2,:,:) - 2.0*PSRC(IW+2:IE-1,:,:) + PSRC(IW+3:IE,:,:))**2 &
+ 1./4   * (3.0*PSRC(IW+1:IE-2,:,:) - 4.0*PSRC(IW+2:IE-1,:,:) + PSRC(IW+3:IE,:,:))**2! Smoothness indicator
ZOMP3(IW+1:IE-2,:,:)  = 3./10. / (ZEPS + ZBPOS3(IW+1:IE-2,:,:))**2
!
! ----- Negative fluxes ----- 
!
! First negative stencil, needs indices i+1, i+2, i+3
ZFNEG1(IW+1:IE-2,:,:) = 1./6.   * (11.0*PSRC(IW+2:IE-1,:,:) - 7.0*PSRC(IW+3:IE,:,:) + 2.0*PSRC(IW+4:IE+1,:,:)) ! Flux
ZBNEG1(IW+1:IE-2,:,:) = 13./12. * (     PSRC(IW+2:IE-1,:,:) - 2.0*PSRC(IW+3:IE,:,:) +     PSRC(IW+4:IE+1,:,:))**2 & 
                        + 1./4   * (3.0* PSRC(IW+2:IE-1,:,:) - 4.0*PSRC(IW+3:IE,:,:) +     PSRC(IW+4:IE+1,:,:))**2 ! Smoothness indicator
ZOMN1(IW+1:IE-2,:,:)  = 1./10. /  (ZEPS + ZBNEG1(IW+1:IE-2,:,:))**2
!
! Second negative stencil, needs indices i, i+1, i+2
ZFNEG2(IW+1:IE-2,:,:) = 1./6.  * (2.0*PSRC(IW+1:IE-2,:,:) + 5.0*PSRC(IW+2:IE-1,:,:) - 1.0*PSRC(IW+3:IE,:,:))! Flux
ZBNEG2(IW+1:IE-2,:,:) = 13./12 * (    PSRC(IW+1:IE-2,:,:) - 2.0*PSRC(IW+2:IE-1,:,:) +     PSRC(IW+3:IE,:,:))**2 &
                         + 1./4   * (    PSRC(IW+1:IE-2,:,:) -  PSRC(IW+3:IE,:,:))**2! Smoothness indicator
ZOMN2(IW+1:IE-2,:,:)  = 3./5. /  (ZEPS + ZBNEG2(IW+1:IE-2,:,:))**2! Non-normalized weight
!
! Third negative stencil, needs indices i-1, i, i+1
ZFNEG3(IW+1:IE-2,:,:) = 1./6   * (-1.0*PSRC(IW:IE-3,:,:) + 5.0*PSRC(IW+1:IE-2,:,:) + 2.0*PSRC(IW+2:IE-1,:,:))! Flux
ZBNEG3(IW+1:IE-2,:,:) = 13./12 * ( PSRC(IW:IE-3,:,:) - 2.0*PSRC(IW+1:IE-2,:,:) +    PSRC(IW+2:IE-1,:,:))**2 &
                          + 1./4   * (     PSRC(IW:IE-3,:,:) - 4.0*PSRC(IW+1:IE-2,:,:) + 3.0*PSRC(IW+2:IE-1,:,:))**2! Smoothness indicator
ZOMN3(IW+1:IE-2,:,:)  = 3./10. / (ZEPS + ZBNEG3(IW+1:IE-2,:,:))**2! Non-normalized weight
!
!
! ----- Total flux -----
!
PR(IW+1:IE-2,:,:) = (ZOMP1(IW+1:IE-2,:,:)/(ZOMP1(IW+1:IE-2,:,:)+ZOMP2(IW+1:IE-2,:,:)+ZOMP3(IW+1:IE-2,:,:)) &
                    * ZFPOS1(IW+1:IE-2,:,:) + &
                      ZOMP2(IW+1:IE-2,:,:)/(ZOMP1(IW+1:IE-2,:,:)+ZOMP2(IW+1:IE-2,:,:)+ZOMP3(IW+1:IE-2,:,:)) &
                    * ZFPOS2(IW+1:IE-2,:,:) + & 
                      ZOMP3(IW+1:IE-2,:,:)/(ZOMP1(IW+1:IE-2,:,:)+ZOMP2(IW+1:IE-2,:,:)+ZOMP3(IW+1:IE-2,:,:)) &
                    * ZFPOS3(IW+1:IE-2,:,:)) &
                    * (0.5+SIGN(0.5,PRUCT(IW+1:IE-2,:,:))) &
                  + (ZOMN1(IW+1:IE-2,:,:)/(ZOMN1(IW+1:IE-2,:,:)+ZOMN2(IW+1:IE-2,:,:)+ZOMN3(IW+1:IE-2,:,:)) &
                   * ZFNEG1(IW+1:IE-2,:,:)  &
                     + ZOMN2(IW+1:IE-2,:,:)/(ZOMN1(IW+1:IE-2,:,:)+ZOMN2(IW+1:IE-2,:,:)+ZOMN3(IW+1:IE-2,:,:)) &
                   * ZFNEG2(IW+1:IE-2,:,:)  &
                     + ZOMN3(IW+1:IE-2,:,:)/(ZOMN1(IW+1:IE-2,:,:)+ZOMN2(IW+1:IE-2,:,:)+ZOMN3(IW+1:IE-2,:,:)) &
                    * ZFNEG3(IW+1:IE-2,:,:))  & 
                    * (0.5-SIGN(0.5,PRUCT(IW+1:IE-2,:,:)))
!-------------------------------------------------------------------------------
!*       1.2.   West border
!               ---------------------
!!IF( LWEST_ll() .AND. .FALSE. ) THEN 
IF( LWEST_ll() ) THEN
!-----------------------------------------------------------------------------
! West border is physical -- IW,IW-1
!-----------------------------------------------------------------------------
SELECT CASE (HLBCX(1)) ! X direction LBC type on left side
CASE ('CYCL')
!---------------------------------------------------------------------------
! Periodic boundary condition
!---------------------------------------------------------------------------
IF( LEAST_ll() .AND. .FALSE. ) THEN! East boundary is physical (monoproc)
!
! First positive stencil, needs indices i-2, i-1, i 
ZFPOS1(IW,:,:)  = 1./6. * (2.0*PSRC(IE,:,:)   - 7.0*PSRC(IW-1,:,:) + 11.0*PSRC(IW,:,:))! Flux IW
ZFPOS1(IW-1,:,:) = 1./6. * (2.0*PSRC(IE-1,:,:) - 7.0*PSRC(IE,:,:)   + 11.0*PSRC(IW-1,:,:))! Flux IW-1
ZBPOS1(IW,:,:)  = 13./12. * (PSRC(IE,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 & 
 + 1./4    * (PSRC(IE,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2 ! Smoothness indicator IW
ZBPOS1(IW-1,:,:) = 13./12. * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) +     PSRC(IW-1,:,:))**2 & 
 + 1./4    * (PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + 3.0*PSRC(IW-1,:,:))**2 ! Smoothness indicator IW-1
ZOMP1(IW-1:IW,:,:)  = 1./10. / (ZEPS + ZBPOS1(IW-1:IW,:,:))**2! Non-normalized weight IW,IW-1
!
! Second positive stencil, needs indices i-1, i, i+1
ZFPOS2(IW,:,:)   = 1./6.* (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:))! Flux IW
ZFPOS2(IW-1,:,:) = 1./6.* (-1.0*PSRC(IE,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW-1
ZBPOS2(IW,:,:)   = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
 + 1./4   * (PSRC(IW-1,:,:) -                    PSRC(IW+1,:,:))**2! Smoothness indicator IW
ZBPOS2(IW-1,:,:) = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IW-1,:,:) + PSRC(IW,:,:))**2 &
 + 1./4   * (PSRC(IE,:,:) -                      PSRC(IW,:,:))**2! Smoothness indicator IW-1
ZOMP2(IW-1:IW,:,:)  = 3./5. / (ZEPS + ZBPOS2(IW-1:IW,:,:))**2! Non-normalized weight IW,IW-1
!
! Third negative stencil, needs indices i-1, i, i+1
ZFNEG3(IW,:,:)   = 1./6 * (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:))! Flux IW
ZFNEG3(IW-1,:,:) = 1./6 * (-1.0*PSRC(IE,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:))! Flux IW-1
ZBNEG3(IW,:,:)   = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) +     PSRC(IW+1,:,:))**2 &
   + 1./4   * (PSRC(IW-1,:,:) - 4.0*PSRC(IW,:,:) + 3.0*PSRC(IW+1,:,:))**2! Smoothness indicator IW
ZBNEG3(IW-1,:,:) = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 &
   + 1./4   * (PSRC(IE,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2! Smoothness indicator IW-1
ZOMN3(IW-1:IW,:,:) = 3./10. / (ZEPS + ZBNEG3(IW-1:IW,:,:))**2! Non-normalized weight IW,IW-1
ELSEIF(IW>3) THEN! East boundary is proc border, with minimum 3 HALO points on west side
!
! First positive stencil, needs indices i-2, i-1, i 
ZFPOS1(IW,:,:)   = 1./6. * (2.0*PSRC(IW-2,:,:)   - 7.0*PSRC(IW-1,:,:) + 11.0*PSRC(IW,:,:))! Flux IW
ZFPOS1(IW-1,:,:) = 1./6. * (2.0*PSRC(IW-3,:,:) - 7.0*PSRC(IW-2,:,:)   + 11.0*PSRC(IW-1,:,:))! Flux IW-1
ZBPOS1(IW,:,:)   = 13./12. * (PSRC(IW-1,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 & 
 + 1./4    * (PSRC(IW-1,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2 ! Smoothness indicator IW
ZBPOS1(IW-1,:,:) = 13./12. * (PSRC(IW-3,:,:) - 2.0*PSRC(IW-2,:,:) +     PSRC(IW-1,:,:))**2 & 
 + 1./4    * (PSRC(IW-3,:,:) - 4.0*PSRC(IW-2,:,:) + 3.0*PSRC(IW-1,:,:))**2 ! Smoothness indicator IW-1
ZOMP1(IW-1:IW,:,:)  = 1./10. / (ZEPS + ZBPOS1(IW-1:IW,:,:))**2! Non-normalized weight IW,IW-1
!
! Second positive stencil, needs indices i-1, i, i+1
ZFPOS2(IW,:,:)   = 1./6.* (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:))! Flux IW
ZFPOS2(IW-1,:,:) = 1./6.* (-1.0*PSRC(IW-2,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:))! Flux IW-1
ZBPOS2(IW,:,:)   = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
 + 1./4   * (PSRC(IW-1,:,:) -                    PSRC(IW+1,:,:))**2! Smoothness indicator IW
ZBPOS2(IW-1,:,:) = 13./12 * (PSRC(IW-2,:,:) - 2.0*PSRC(IW-1,:,:) + PSRC(IW,:,:))**2 &
 + 1./4   * (PSRC(IW-2,:,:) -                      PSRC(IW,:,:))**2! Smoothness indicator IW-1
ZOMP2(IW-1:IW,:,:)  = 3./5. / (ZEPS + ZBPOS2(IW-1:IW,:,:))**2! Non-normalized weight IW,IW-1
!
! Third negative stencil, needs indices i-1, i, i+1
ZFNEG3(IW,:,:)   = 1./6 * (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:))! Flux IW
 ZFNEG3(IW-1,:,:) = 1./6 * (-1.0*PSRC(IW-2,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW-1
 ZBNEG3(IW,:,:)   = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) +     PSRC(IW+1,:,:))**2 &
        + 1./4   * (PSRC(IW-1,:,:) - 4.0*PSRC(IW,:,:) + 3.0*PSRC(IW+1,:,:))**2 ! Smoothness indicator IW
 ZBNEG3(IW-1,:,:) = 13./12 * (PSRC(IW-2,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 &
        + 1./4   * (PSRC(IW-2,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2 ! Smoothness indicator IW-1
 ZOMN3(IW-1:IW,:,:) = 3./10. / (ZEPS + ZBNEG3(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ELSE ! East boundary is proc border, with NHALO < 3 on west side
  call Print_msg(NVERB_FATAL,'GEN','ADVEC_WENO_K_3_UX','WENO5/CYCL fluxes calculation needs JPHEXT (&NHALO) >= 3 on west side')
 ENDIF
!
 ! Third positive stencil, needs indices i, i+1, i+2
 ZFPOS3(IW,:,:)   = 1./6 * (2.0*PSRC(IW,:,:) + 5.0*PSRC(IW+1,:,:) - PSRC(IW+2,:,:)) ! Flux IW
 ZFPOS3(IW-1,:,:) = 1./6 * (2.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:) - PSRC(IW+1,:,:)) ! Flux IW-1
 ZBPOS3(IW,:,:)   = 13./12 * (    PSRC(IW,:,:) - 2.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 &
      + 1./4   * (3.0*PSRC(IW,:,:) - 4.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 ! Smoothness indicator IW
 ZBPOS3(IW-1,:,:) = 13./12 * (    PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
      + 1./4   * (3.0*PSRC(IW-1,:,:) - 4.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 ! Smoothness indicator IW-1
 ZOMP3(IW-1:IW,:,:)  = 3./10. / (ZEPS + ZBPOS3(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1 
!
 ! First negative stencil, needs indices i+1, i+2, i+3
 ZFNEG1(IW,:,:)   = 1./6. * (11.0*PSRC(IW+1,:,:) - 7.0*PSRC(IW+2,:,:) + 2.0*PSRC(IW+3,:,:)) ! Flux IW
 ZFNEG1(IW-1,:,:) = 1./6. * (11.0*PSRC(IW,:,:)   - 7.0*PSRC(IW+1,:,:) + 2.0*PSRC(IW+2,:,:)) ! Flux IW-1
 ZBNEG1(IW,:,:)   = 13./12. * (    PSRC(IW+1,:,:) - 2.0*PSRC(IW+2,:,:) + PSRC(IW+3,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IW+1,:,:) - 4.0*PSRC(IW+2,:,:) + PSRC(IW+3,:,:))**2  ! Smoothness indicator IW
 ZBNEG1(IW-1,:,:) = 13./12. * (    PSRC(IW,:,:) - 2.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IW,:,:) - 4.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2  ! Smoothness indicator IW-1
 ZOMN1(IW-1:IW,:,:) = 1./10. / (ZEPS + ZBNEG1(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! Second negative stencil, needs indices i, i+1, i+2
 ZFNEG2(IW,:,:)   = 1./6. * (2.0*PSRC(IW,:,:)   + 5.0*PSRC(IW+1,:,:) - 1.0*PSRC(IW+2,:,:)) ! Flux IW
 ZFNEG2(IW-1,:,:) = 1./6. * (2.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   - 1.0*PSRC(IW+1,:,:)) ! Flux IW-1
 ZBNEG2(IW,:,:)   = 13./12 * (PSRC(IW,:,:) - 2.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 &
      + 1./4   * (PSRC(IW,:,:) -                      PSRC(IW+2,:,:))**2 ! Smoothness indicator IW
 ZBNEG2(IW-1,:,:) = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
      + 1./4   * (PSRC(IW-1,:,:) -                    PSRC(IW+1,:,:))**2 ! Smoothness indicator IW-1
 ZOMN2(IW-1:IW,:,:) = 3./5. / (ZEPS + ZBNEG2(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! ----- Total flux -----
! 
 PR(IW-1:IW,:,:) = ( ZOMP1(IW-1:IW,:,:)/(ZOMP1(IW-1:IW,:,:)+ZOMP2(IW-1:IW,:,:)+ZOMP3(IW-1:IW,:,:)) * ZFPOS1(IW-1:IW,:,:) &
                       + ZOMP2(IW-1:IW,:,:)/(ZOMP1(IW-1:IW,:,:)+ZOMP2(IW-1:IW,:,:)+ZOMP3(IW-1:IW,:,:)) * ZFPOS2(IW-1:IW,:,:) & 
                       + ZOMP3(IW-1:IW,:,:)/(ZOMP1(IW-1:IW,:,:)+ZOMP2(IW-1:IW,:,:)+ZOMP3(IW-1:IW,:,:)) * ZFPOS3(IW-1:IW,:,:)) &
                      * (0.5+SIGN(0.5,PRUCT(IW-1:IW,:,:))) &
                    + ( ZOMN1(IW-1:IW,:,:)/(ZOMN1(IW-1:IW,:,:)+ZOMN2(IW-1:IW,:,:)+ZOMN3(IW-1:IW,:,:)) * ZFNEG1(IW-1:IW,:,:) &
                       + ZOMN2(IW-1:IW,:,:)/(ZOMN1(IW-1:IW,:,:)+ZOMN2(IW-1:IW,:,:)+ZOMN3(IW-1:IW,:,:)) * ZFNEG2(IW-1:IW,:,:) &
                       + ZOMN3(IW-1:IW,:,:)/(ZOMN1(IW-1:IW,:,:)+ZOMN2(IW-1:IW,:,:)+ZOMN3(IW-1:IW,:,:)) * ZFNEG3(IW-1:IW,:,:)) &
                      * (0.5-SIGN(0.5,PRUCT(IW-1:IW,:,:)))
!
!
 CASE ('OPEN','WALL','NEST') 
 !---------------------------------------------------------------------------
 ! Open, or Wall, or Nest boundary condition => WENO order reduction
 !---------------------------------------------------------------------------
!
 ! WENO scheme order 1, IW-1
    PR(IW-1,:,:) = PSRC(IW-1,:,:) * (0.5+SIGN(0.5,PRUCT(IW-1,:,:))) + &
                   PSRC(IW,:,:) * &
                   (0.5-SIGN(0.5,PRUCT(IW-1,:,:)))
!   ! WENO scheme order 3, IW
    ZFPOS1(IW,:,:) = 0.5 * (3.0*PSRC(IW,:,:) - PSRC(IW-1,:,:)) ! First positive flux
    ZFPOS2(IW,:,:) = 0.5 * ( PSRC(IW,:,:) + PSRC(IW+1,:,:)) ! Second positive flux
    ZBPOS1(IW,:,:) = (PSRC(IW,:,:)   - PSRC(IW-1,:,:))**2 ! First positive smoothness indicator
    ZBPOS2(IW,:,:) = (PSRC(IW+1,:,:) - PSRC(IW,:,:))**2  ! Second positive smoothness indicator
!
    ZFNEG1(IW,:,:) = 0.5 * (3.0*PSRC(IW+1,:,:) - PSRC(IW+2,:,:)) ! First negative flux
    ZFNEG2(IW,:,:) = 0.5 * ( PSRC(IW,:,:)   + PSRC(IW+1,:,:)) ! Second negative flux
    ZBNEG1(IW,:,:) = (PSRC(IW+1,:,:) - PSRC(IW+2,:,:))**2 ! First negative smoothness indicator
    ZBNEG2(IW,:,:) = (PSRC(IW,:,:)   - PSRC(IW+1,:,:))**2 ! Second negative smoothness indicator
!
    ZOMP1(IW,:,:) = 1./3. / (ZEPS + ZBPOS1(IW,:,:))**2 ! First positive non-normalized weight
    ZOMP2(IW,:,:) = 2./3. / (ZEPS + ZBPOS2(IW,:,:))**2 ! Second positive non-normalized weight
    ZOMN1(IW,:,:) = 1./3. / (ZEPS + ZBNEG1(IW,:,:))**2 ! First negative non-normalized weight
    ZOMN2(IW,:,:) = 2./3. / (ZEPS + ZBNEG2(IW,:,:))**2 ! Second negative non-normalized weight
! 
    PR(IW,:,:) = (ZOMN2(IW,:,:)/(ZOMN1(IW,:,:)+ZOMN2(IW,:,:)) * ZFNEG2(IW,:,:) + &
             (ZOMN1(IW,:,:)/(ZOMN1(IW,:,:)+ZOMN2(IW,:,:)) * ZFNEG1(IW,:,:))) &
           *(0.5-SIGN(0.5,PRUCT(IW,:,:))) + &
     (ZOMP2(IW,:,:)/(ZOMP1(IW,:,:)+ZOMP2(IW,:,:)) * ZFPOS2(IW,:,:) + &
     (ZOMP1(IW,:,:)/(ZOMP1(IW,:,:)+ZOMP2(IW,:,:)) * ZFPOS1(IW,:,:))) &
     *(0.5+SIGN(0.5,PRUCT(IW,:,:)))  ! Total flux
! 
 END SELECT ! SELECT CASE (HLBCX(1)) ! X direction LBC type on left side
!
ELSE
 !-----------------------------------------------------------------------------
 ! West border is proc border -- IW,IW-1
 !-----------------------------------------------------------------------------
!
 IF (NHALO<3) THEN
  call Print_msg(NVERB_FATAL,'GEN','ADVEC_WENO_K_3_UX','WENO5/west-int not parallelisable with NHALO < 3')
 ELSEIF (NHALO>=3) THEN
 !---------------------------------------------------------------------------
 ! NHALO >3 => WENO5 for all boundary points
 !---------------------------------------------------------------------------
!
 ! ----- Positive fluxes -----
!
 ! First positive stencil, needs indices i-2, i-1, i 
 ZFPOS1(IW,:,:)   = 1./6. * (2.0*PSRC(IW-2,:,:) - 7.0*PSRC(IW-1,:,:) + 11.0*PSRC(IW,:,:)) ! Flux IW
 ZFPOS1(IW-1,:,:) = 1./6. * (2.0*PSRC(IW-3,:,:) - 7.0*PSRC(IW-2,:,:) + 11.0*PSRC(IW-1,:,:)) ! Flux IW-1
 ZBPOS1(IW,:,:)   = 13./12. * (PSRC(IW-2,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 & 
        + 1./4    * (PSRC(IW-2,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2   ! Smoothness indicator IW
 ZBPOS1(IW-1,:,:) = 13./12. * (PSRC(IW-3,:,:) - 2.0*PSRC(IW-2,:,:) +     PSRC(IW-1,:,:))**2 & 
        + 1./4    * (PSRC(IW-3,:,:) - 4.0*PSRC(IW-2,:,:) + 3.0*PSRC(IW-1,:,:))**2  ! Smoothness indicator IW-1
 ZOMP1(IW-1:IW,:,:)  = 1./10. / (ZEPS + ZBPOS1(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! Second positive stencil, needs indices i-1, i, i+1
 ZFPOS2(IW,:,:)   = 1./6.* (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:)) ! Flux IW
 ZFPOS2(IW-1,:,:) = 1./6.* (-1.0*PSRC(IW-2,:,:) + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW-1
 ZBPOS2(IW,:,:)   = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
      + 1./4   * (PSRC(IW-1,:,:) -                    PSRC(IW+1,:,:))**2 ! Smoothness indicator IW
 ZBPOS2(IW-1,:,:) = 13./12 * (PSRC(IW-2,:,:) - 2.0*PSRC(IW-1,:,:) + PSRC(IW,:,:))**2 &
      + 1./4   * (PSRC(IW-2,:,:) -                      PSRC(IW,:,:))**2 ! Smoothness indicator IW-1
 ZOMP2(IW-1:IW,:,:)  = 3./5. / (ZEPS + ZBPOS2(IW-1:IW,:,:))**2  ! Non-normalized weight IW,IW-1
! 
 ! Third positive stencil, needs indices i, i+1, i+2
 ZFPOS3(IW,:,:)   = 1./6 * (2.0*PSRC(IW,:,:) + 5.0*PSRC(IW+1,:,:) - PSRC(IW+2,:,:)) ! Flux IW
 ZFPOS3(IW-1,:,:) = 1./6 * (2.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:) - PSRC(IW+1,:,:)) ! Flux IW-1
 ZBPOS3(IW,:,:)   = 13./12 * (    PSRC(IW,:,:) - 2.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 &
      + 1./4   * (3.0*PSRC(IW,:,:) - 4.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 ! Smoothness indicator IW
 ZBPOS3(IW-1,:,:) = 13./12 * (    PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
      + 1./4   * (3.0*PSRC(IW-1,:,:) - 4.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 ! Smoothness indicator IW-1
 ZOMP3(IW-1:IW,:,:)  = 3./10. / (ZEPS + ZBPOS3(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! ----- Negative fluxes ----- 
!
 ! First negative stencil, needs indices i+1, i+2, i+3
 ZFNEG1(IW,:,:)   = 1./6. * (11.0*PSRC(IW+1,:,:) - 7.0*PSRC(IW+2,:,:) + 2.0*PSRC(IW+3,:,:)) ! Flux IW
 ZFNEG1(IW-1,:,:) = 1./6. * (11.0*PSRC(IW,:,:)   - 7.0*PSRC(IW+1,:,:) + 2.0*PSRC(IW+2,:,:)) ! Flux IW-1
 ZBNEG1(IW,:,:)   = 13./12. * (    PSRC(IW+1,:,:) - 2.0*PSRC(IW+2,:,:) + PSRC(IW+3,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IW+1,:,:) - 4.0*PSRC(IW+2,:,:) + PSRC(IW+3,:,:))**2  ! Smoothness indicator IW
 ZBNEG1(IW-1,:,:) = 13./12. * (    PSRC(IW,:,:) - 2.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IW,:,:) - 4.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2  ! Smoothness indicator IW-1
 ZOMN1(IW-1:IW,:,:) = 1./10. / (ZEPS + ZBNEG1(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! Second negative stencil, needs indices i, i+1, i+2
 ZFNEG2(IW,:,:)   = 1./6. * (2.0*PSRC(IW,:,:)   + 5.0*PSRC(IW+1,:,:) - 1.0*PSRC(IW+2,:,:)) ! Flux IW
 ZFNEG2(IW-1,:,:) = 1./6. * (2.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   - 1.0*PSRC(IW+1,:,:)) ! Flux IW-1
 ZBNEG2(IW,:,:)   = 13./12 * (PSRC(IW,:,:) - 2.0*PSRC(IW+1,:,:) + PSRC(IW+2,:,:))**2 &
      + 1./4   * (PSRC(IW,:,:) -                      PSRC(IW+2,:,:))**2 ! Smoothness indicator IW
 ZBNEG2(IW-1,:,:) = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
      + 1./4   * (PSRC(IW-1,:,:) -                    PSRC(IW+1,:,:))**2 ! Smoothness indicator IW-1
 ZOMN2(IW-1:IW,:,:) = 3./5. / (ZEPS + ZBNEG2(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! Third negative stencil, needs indices i-1, i, i+1
 ZFNEG3(IW,:,:)   = 1./6 * (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:)) ! Flux IW
 ZFNEG3(IW-1,:,:) = 1./6 * (-1.0*PSRC(IW-2,:,:) + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW-1
 ZBNEG3(IW,:,:)   = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) +     PSRC(IW+1,:,:))**2 &
        + 1./4   * (PSRC(IW-1,:,:) - 4.0*PSRC(IW,:,:) + 3.0*PSRC(IW+1,:,:))**2 ! Smoothness indicator IW
 ZBNEG3(IW-1,:,:) = 13./12 * (PSRC(IW-2,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 &
        + 1./4   * (PSRC(IW-2,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2 ! Smoothness indicator IW-1
 ZOMN3(IW-1:IW,:,:) = 3./10. / (ZEPS + ZBNEG3(IW-1:IW,:,:))**2 ! Non-normalized weight IW,IW-1
!
 ! ----- Total flux -----
! 
 PR(IW-1:IW,:,:) = ( ZOMP1(IW-1:IW,:,:)/(ZOMP1(IW-1:IW,:,:)+ZOMP2(IW-1:IW,:,:)+ZOMP3(IW-1:IW,:,:)) * ZFPOS1(IW-1:IW,:,:) &
                       + ZOMP2(IW-1:IW,:,:)/(ZOMP1(IW-1:IW,:,:)+ZOMP2(IW-1:IW,:,:)+ZOMP3(IW-1:IW,:,:)) * ZFPOS2(IW-1:IW,:,:) & 
                       + ZOMP3(IW-1:IW,:,:)/(ZOMP1(IW-1:IW,:,:)+ZOMP2(IW-1:IW,:,:)+ZOMP3(IW-1:IW,:,:)) * ZFPOS3(IW-1:IW,:,:)) &
                      * (0.5+SIGN(0.5,PRUCT(IW-1:IW,:,:))) &
                    + ( ZOMN1(IW-1:IW,:,:)/(ZOMN1(IW-1:IW,:,:)+ZOMN2(IW-1:IW,:,:)+ZOMN3(IW-1:IW,:,:)) * ZFNEG1(IW-1:IW,:,:) &
                       + ZOMN2(IW-1:IW,:,:)/(ZOMN1(IW-1:IW,:,:)+ZOMN2(IW-1:IW,:,:)+ZOMN3(IW-1:IW,:,:)) * ZFNEG2(IW-1:IW,:,:) &
                       + ZOMN3(IW-1:IW,:,:)/(ZOMN1(IW-1:IW,:,:)+ZOMN2(IW-1:IW,:,:)+ZOMN3(IW-1:IW,:,:)) * ZFNEG3(IW-1:IW,:,:)) &
                      * (0.5-SIGN(0.5,PRUCT(IW-1:IW,:,:)))
!-------------------------------------------------------------------------------
!*       1.3.   East border
!               ---------------------
!! IF( LEAST_ll() .AND. .FALSE. ) THEN 
IF( LEAST_ll() ) THEN 

 !-----------------------------------------------------------------------------
 ! East border is physical -- IE-1,IE
 !-----------------------------------------------------------------------------
 SELECT CASE (HLBCX(2)) ! X direction LBC type on right side
! 
 CASE ('CYCL')
 !---------------------------------------------------------------------------
 ! Periodic boundary condition
 !---------------------------------------------------------------------------
! 
 IF (LWEST_ll() .AND. .FALSE. ) THEN  ! West boundary is physical (monoproc)
! 
 ! Third positive stencil, needs indices i, i+1, i+2
 ZFPOS3(IE-1,:,:) = 1./6 * (2.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:) - PSRC(IE+1,:,:)) ! Flux IE-1
 ZFPOS3(IE,:,:)   = 1./6 * (2.0*PSRC(IE,:,:) + 5.0*PSRC(IE+1,:,:) - PSRC(IW,:,:)) ! Flux IE
 ZBPOS3(IE-1,:,:) = 13./12 * (    PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (3.0*PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 ! Smoothness indicator IE-1
 ZBPOS3(IE,:,:)   = 13./12 * (    PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IW,:,:))**2 &
      + 1./4   * (3.0*PSRC(IE,:,:) - 4.0*PSRC(IE+1,:,:) + PSRC(IW,:,:))**2  ! Smoothness indicator IE
 ZOMP3(IE-1:IE,:,:)  = 3./10. / (ZEPS + ZBPOS3(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE 
!
 ! First negative stencil, needs indices i+1, i+2, i+3
 ZFNEG1(IE-1,:,:) = 1./6. * (11.0*PSRC(IE,:,:)   - 7.0*PSRC(IE+1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IE-1
 ZFNEG1(IE,:,:)   = 1./6. * (11.0*PSRC(IE+1,:,:) - 7.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:)) ! Flux IE
 ZBNEG1(IE-1,:,:) = 13./12. * (    PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IW,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IE,:,:) - 4.0*PSRC(IE+1,:,:) + PSRC(IW,:,:))**2  ! Smoothness indicator IE-1
 ZBNEG1(IE,:,:)   = 13./12. * (    PSRC(IE+1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IE+1,:,:) - 4.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2  ! Smoothness indicator IE
 ZOMN1(IE-1:IE,:,:) = 1./10. / (ZEPS + ZBNEG1(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! Second negative stencil, needs indices i, i+1, i+2
 ZFNEG2(IE-1,:,:) = 1./6. * (2.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   - 1.0*PSRC(IE+1,:,:)) ! Flux IE-1
 ZFNEG2(IE,:,:)   = 1./6. * (2.0*PSRC(IE,:,:)   + 5.0*PSRC(IE+1,:,:) - 1.0*PSRC(IW,:,:)) ! Flux IE
 ZBNEG2(IE-1,:,:)  = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (PSRC(IE-1,:,:) -                    PSRC(IE+1,:,:))**2 ! Smoothness indicator IE-1
 ZBNEG2(IE,:,:)   = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IW,:,:))**2 &
      + 1./4   * (PSRC(IE,:,:) -                      PSRC(IW,:,:))**2  ! Smoothness indicator IE
 ZOMN2(IE-1:IE,:,:) = 3./5. / (ZEPS + ZBNEG2(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ELSEIF(IE<=SIZE(PSRC,1)-3) THEN ! West boundary is proc border, with minimum 3 HALO points on east side
!
 ! Third positive stencil, needs indices i, i+1, i+2
 ZFPOS3(IE-1,:,:) = 1./6 * (2.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:) - PSRC(IE+1,:,:)) ! Flux IE-1
 ZFPOS3(IE,:,:)   = 1./6 * (2.0*PSRC(IE,:,:) + 5.0*PSRC(IE+1,:,:) - PSRC(IE+2,:,:)) ! Flux IE
 ZBPOS3(IE-1,:,:) = 13./12 * (    PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (3.0*PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 ! Smoothness indicator IE-1
 ZBPOS3(IE,:,:)   = 13./12 * (    PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2 &
      + 1./4   * (3.0*PSRC(IE,:,:) - 4.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2  ! Smoothness indicator IE
 ZOMP3(IE-1:IE,:,:)  = 3./10. / (ZEPS + ZBPOS3(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! First negative stencil, needs indices i+1, i+2, i+3
 ZFNEG1(IE-1,:,:) = 1./6. * (11.0*PSRC(IE,:,:)   - 7.0*PSRC(IE+1,:,:) + 2.0*PSRC(IE+2,:,:)) ! Flux IE-1
 ZFNEG1(IE,:,:)   = 1./6. * (11.0*PSRC(IE+1,:,:) - 7.0*PSRC(IE+2,:,:)   + 2.0*PSRC(IE+3,:,:)) ! Flux IE
 ZBNEG1(IE-1,:,:) = 13./12. * (    PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IE,:,:) - 4.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2  ! Smoothness indicator IE-1
 ZBNEG1(IE,:,:)   = 13./12. * (    PSRC(IE+1,:,:) - 2.0*PSRC(IE+2,:,:) + PSRC(IE+3,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IE+1,:,:) - 4.0*PSRC(IE+2,:,:) + PSRC(IE+3,:,:))**2  ! Smoothness indicator IE
 ZOMN1(IE-1:IE,:,:) = 1./10. / (ZEPS + ZBNEG1(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! Second negative stencil, needs indices i, i+1, i+2
 ZFNEG2(IE-1,:,:) = 1./6. * (2.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   - 1.0*PSRC(IE+1,:,:)) ! Flux IE-1
 ZFNEG2(IE,:,:)   = 1./6. * (2.0*PSRC(IE,:,:)   + 5.0*PSRC(IE+1,:,:) - 1.0*PSRC(IE+2,:,:)) ! Flux IE
 ZBNEG2(IE-1,:,:)  = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (PSRC(IE-1,:,:) -                    PSRC(IE+1,:,:))**2 ! Smoothness indicator IE-1
 ZBNEG2(IE,:,:)   = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2 &
      + 1./4   * (PSRC(IE,:,:) -                      PSRC(IE+2,:,:))**2  ! Smoothness indicator IE
 ZOMN2(IE-1:IE,:,:) = 3./5. / (ZEPS + ZBNEG2(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ELSE ! West boundary is proc border, with NHALO < 3 on east side
  call Print_msg(NVERB_FATAL,'GEN','ADVEC_WENO_K_3_UX','WENO5/CYCL fluxes calculation needs JPHEXT (&NHALO) >= 3 on east side')
 ENDIF
!
 ! First positive stencil, needs indices i-2, i-1, i 
 ZFPOS1(IE-1,:,:) = 1./6. * (2.0*PSRC(IE-3,:,:) - 7.0*PSRC(IE-2,:,:) + 11.0*PSRC(IE-1,:,:)) ! Flux IE-1
 ZFPOS1(IE,:,:)   = 1./6. * (2.0*PSRC(IE-2,:,:) - 7.0*PSRC(IE-1,:,:) + 11.0*PSRC(IE,:,:)) ! Flux IE
 ZBPOS1(IE-1,:,:) = 13./12. * (PSRC(IE-3,:,:) - 2.0*PSRC(IE-2,:,:) +     PSRC(IE-1,:,:))**2 & 
        + 1./4    * (PSRC(IE-3,:,:) - 4.0*PSRC(IE-2,:,:) + 3.0*PSRC(IE-1,:,:))**2  ! Smoothness indicator IE-1
 ZBPOS1(IE,:,:)   = 13./12. * (PSRC(IE-2,:,:) - 2.0*PSRC(IE-1,:,:) +     PSRC(IE,:,:))**2 & 
        + 1./4    * (PSRC(IE-2,:,:) - 4.0*PSRC(IE-1,:,:) + 3.0*PSRC(IE,:,:))**2  ! Smoothness indicator IE
 ZOMP1(IE-1:IE,:,:)  = 1./10. / (ZEPS + ZBPOS1(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! Second positive stencil, needs indices i-1, i, i+1
 ZFPOS2(IE-1,:,:) = 1./6. * (-1.0*PSRC(IE-2,:,:) + 5.0*PSRC(IE-1,:,:) + 2.0*PSRC(IE,:,:)) ! Flux IE-1
 ZFPOS2(IE,:,:)   = 1./6. * (-1.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   + 2.0*PSRC(IE+1,:,:)) ! Flux IE
 ZBPOS2(IE-1,:,:) = 13./12 * (PSRC(IE-2,:,:) - 2.0*PSRC(IE-1,:,:) + PSRC(IE,:,:))**2 &
      + 1./4   * (PSRC(IE-2,:,:) -                      PSRC(IE,:,:))**2 ! Smoothness indicator IE-1
 ZBPOS2(IE,:,:)   = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (PSRC(IE-1,:,:) -                   PSRC(IE+1,:,:))**2 ! Smoothness indicator IE
 ZOMP2(IE-1:IE,:,:)  = 3./5. / (ZEPS + ZBPOS2(IE-1:IE,:,:))**2  ! Non-normalized weight IE-1,IE
! 
 ! Third negative stencil, needs indices i-1, i, i+1
 ZFNEG3(IE-1,:,:) = 1./6 * (-1.0*PSRC(IE-2,:,:) + 5.0*PSRC(IE-1,:,:) + 2.0*PSRC(IE,:,:)) ! Flux IE-1
 ZFNEG3(IE,:,:)   = 1./6 * (-1.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   + 2.0*PSRC(IE+1,:,:)) ! Flux IE
 ZBNEG3(IE-1,:,:) = 13./12 * (PSRC(IE-2,:,:) - 2.0*PSRC(IE-1,:,:) +     PSRC(IE,:,:))**2 &
        + 1./4   * (PSRC(IE-2,:,:) - 4.0*PSRC(IE-1,:,:) + 3.0*PSRC(IE,:,:))**2 ! Smoothness indicator IE-1
 ZBNEG3(IE,:,:)   = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) +     PSRC(IE+1,:,:))**2 &
        + 1./4   * (PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + 3.0*PSRC(IE+1,:,:))**2 ! Smoothness indicator IE
 ZOMN3(IE-1:IE,:,:) = 3./10. / (ZEPS + ZBNEG3(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! ----- Total flux -----
! 
 PR(IE-1:IE,:,:) = ( ZOMP1(IE-1:IE,:,:)/(ZOMP1(IE-1:IE,:,:)+ZOMP2(IE-1:IE,:,:)+ZOMP3(IE-1:IE,:,:)) * ZFPOS1(IE-1:IE,:,:) &
                       + ZOMP2(IE-1:IE,:,:)/(ZOMP1(IE-1:IE,:,:)+ZOMP2(IE-1:IE,:,:)+ZOMP3(IE-1:IE,:,:)) * ZFPOS2(IE-1:IE,:,:) & 
                       + ZOMP3(IE-1:IE,:,:)/(ZOMP1(IE-1:IE,:,:)+ZOMP2(IE-1:IE,:,:)+ZOMP3(IE-1:IE,:,:)) * ZFPOS3(IE-1:IE,:,:)) &
                      * (0.5+SIGN(0.5,PRUCT(IE-1:IE,:,:))) &
                    + ( ZOMN1(IE-1:IE,:,:)/(ZOMN1(IE-1:IE,:,:)+ZOMN2(IE-1:IE,:,:)+ZOMN3(IE-1:IE,:,:)) * ZFNEG1(IE-1:IE,:,:) &
                       + ZOMN2(IE-1:IE,:,:)/(ZOMN1(IE-1:IE,:,:)+ZOMN2(IE-1:IE,:,:)+ZOMN3(IE-1:IE,:,:)) * ZFNEG2(IE-1:IE,:,:) &
                       + ZOMN3(IE-1:IE,:,:)/(ZOMN1(IE-1:IE,:,:)+ZOMN2(IE-1:IE,:,:)+ZOMN3(IE-1:IE,:,:)) * ZFNEG3(IE-1:IE,:,:)) &
                      * (0.5-SIGN(0.5,PRUCT(IE-1:IE,:,:)))
!
!
 CASE ('OPEN','WALL','NEST') 
 !---------------------------------------------------------------------------
 ! Open, or Wall, or Nest boundary condition => WENO order reduction
 !---------------------------------------------------------------------------
!
 ! WENO scheme order 1, IE
    PR(IE,:,:) = PSRC(IE,:,:) * (0.5+SIGN(0.5,PRUCT(IE,:,:))) + &
                 PSRC(IE+1,:,:) * &
                 (0.5-SIGN(0.5,PRUCT(IE,:,:)))
!
!   ! WENO scheme order 3, IE-1
    ZFPOS1(IE-1,:,:) = 0.5 * (3.0*PSRC(IE-1,:,:) - PSRC(IE-2,:,:)) ! First positive flux
    ZFPOS2(IE-1,:,:) = 0.5 * ( PSRC(IE-1,:,:) + PSRC(IE,:,:)) ! Second positive flux
    ZBPOS1(IE-1,:,:) = (PSRC(IE-1,:,:) - PSRC(IE-2,:,:))**2 ! First positive smoothness indicator
    ZBPOS2(IE-1,:,:) = (PSRC(IE,:,:)   - PSRC(IE-1,:,:))**2 ! Second positive smoothness indicator
!
    ZFNEG1(IE-1,:,:) = 0.5 * (3.0*PSRC(IE,:,:)   - PSRC(IE+1,:,:)) ! First negative flux
    ZFNEG2(IE-1,:,:) = 0.5 * ( PSRC(IE-1,:,:) + PSRC(IE,:,:)) ! Second negative flux
    ZBNEG1(IE-1,:,:) = (PSRC(IE,:,:)   - PSRC(IE+1,:,:))**2 ! First negative smoothness indicator
    ZBNEG2(IE-1,:,:) = (PSRC(IE-1,:,:) - PSRC(IE,:,:))**2  ! Second negative smoothness indicator
!
    ZOMP1(IE-1,:,:) = 1./3. / (ZEPS + ZBPOS1(IE-1,:,:))**2 ! First positive non-normalized weight
    ZOMP2(IE-1,:,:) = 2./3. / (ZEPS + ZBPOS2(IE-1,:,:))**2 ! Second positive non-normalized weight
    ZOMN1(IE-1,:,:) = 1./3. / (ZEPS + ZBNEG1(IE-1,:,:))**2 ! First negative non-normalized weight
    ZOMN2(IE-1,:,:) = 2./3. / (ZEPS + ZBNEG2(IE-1,:,:))**2 ! Second negative non-normalized weight
! 
    PR(IE-1,:,:) = (ZOMN2(IE-1,:,:)/(ZOMN1(IE-1,:,:)+ZOMN2(IE-1,:,:))*ZFNEG2(IE-1,:,:) + &
      (ZOMN1(IE-1,:,:)/(ZOMN1(IE-1,:,:)+ZOMN2(IE-1,:,:))*ZFNEG1(IE-1,:,:))) &
      *(0.5-SIGN(0.5,PRUCT(IE-1,:,:))) + &
                 (ZOMP2(IE-1,:,:)/(ZOMP1(IE-1,:,:)+ZOMP2(IE-1,:,:))*ZFPOS2(IE-1,:,:) + &
                   (ZOMP1(IE-1,:,:)/(ZOMP1(IE-1,:,:)+ZOMP2(IE-1,:,:))*ZFPOS1(IE-1,:,:))) &
      * (0.5+SIGN(0.5,PRUCT(IE-1,:,:)))  ! Total flux
! 
 END SELECT ! SELECT CASE (HLBCX(2)) ! X direction LBC type on right side
!
ELSE
 !-----------------------------------------------------------------------------
 ! East border is proc border -- IE-1,IE
 !-----------------------------------------------------------------------------
!
 IF (NHALO<3) THEN
  call Print_msg(NVERB_FATAL,'GEN','ADVEC_WENO_K_3_UX','WENO5/east-int not parallelisable with NHALO < 3')
 ELSEIF (NHALO>=3) THEN
 !---------------------------------------------------------------------------
 ! NHALO >= 3 => WENO5 for all boundary points
 !---------------------------------------------------------------------------
! 
 ! ----- Positive fluxes -----
!
 ! First positive stencil, needs indices i-2, i-1, i 
 ZFPOS1(IE-1,:,:) = 1./6. * (2.0*PSRC(IE-3,:,:) - 7.0*PSRC(IE-2,:,:) + 11.0*PSRC(IE-1,:,:)) ! Flux IE-1
 ZFPOS1(IE,:,:)   = 1./6. * (2.0*PSRC(IE-2,:,:) - 7.0*PSRC(IE-1,:,:) + 11.0*PSRC(IE,:,:)) ! Flux IE
 ZBPOS1(IE-1,:,:) = 13./12. * (PSRC(IE-3,:,:) - 2.0*PSRC(IE-2,:,:) +     PSRC(IE-1,:,:))**2 & 
        + 1./4    * (PSRC(IE-3,:,:) - 4.0*PSRC(IE-2,:,:) + 3.0*PSRC(IE-1,:,:))**2  ! Smoothness indicator IE-1
 ZBPOS1(IE,:,:)   = 13./12. * (PSRC(IE-2,:,:) - 2.0*PSRC(IE-1,:,:) +     PSRC(IE,:,:))**2 & 
        + 1./4    * (PSRC(IE-2,:,:) - 4.0*PSRC(IE-1,:,:) + 3.0*PSRC(IE,:,:))**2  ! Smoothness indicator IE
 ZOMP1(IE-1:IE,:,:)  = 1./10. / (ZEPS + ZBPOS1(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! Second positive stencil, needs indices i-1, i, i+1
 ZFPOS2(IE-1,:,:) = 1./6. * (-1.0*PSRC(IE-2,:,:) + 5.0*PSRC(IE-1,:,:) + 2.0*PSRC(IE,:,:)) ! Flux IE-1
 ZFPOS2(IE,:,:)   = 1./6. * (-1.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   + 2.0*PSRC(IE+1,:,:)) ! Flux IE
 ZBPOS2(IE-1,:,:) = 13./12 * (PSRC(IE-2,:,:) - 2.0*PSRC(IE-1,:,:) + PSRC(IE,:,:))**2 &
      + 1./4   * (PSRC(IE-2,:,:) -                      PSRC(IE,:,:))**2 ! Smoothness indicator IE-1
 ZBPOS2(IE,:,:)   = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (PSRC(IE-1,:,:) -                   PSRC(IE+1,:,:))**2 ! Smoothness indicator IE
 ZOMP2(IE-1:IE,:,:)  = 3./5. / (ZEPS + ZBPOS2(IE-1:IE,:,:))**2  ! Non-normalized weight IE-1,IE
! 
 ! Third positive stencil, needs indices i, i+1, i+2
 ZFPOS3(IE-1,:,:) = 1./6 * (2.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:) - PSRC(IE+1,:,:)) ! Flux IE-1
 ZFPOS3(IE,:,:)   = 1./6 * (2.0*PSRC(IE,:,:) + 5.0*PSRC(IE+1,:,:) - PSRC(IE+2,:,:)) ! Flux IE
 ZBPOS3(IE-1,:,:) = 13./12 * (    PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (3.0*PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 ! Smoothness indicator IE-1
 ZBPOS3(IE,:,:)   = 13./12 * (    PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2 &
      + 1./4   * (3.0*PSRC(IE,:,:) - 4.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2  ! Smoothness indicator IE
 ZOMP3(IE-1:IE,:,:)  = 3./10. / (ZEPS + ZBPOS3(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! ----- Negative fluxes ----- 
!
 ! First negative stencil, needs indices i+1, i+2, i+3
 ZFNEG1(IE-1,:,:) = 1./6. * (11.0*PSRC(IE,:,:)   - 7.0*PSRC(IE+1,:,:) + 2.0*PSRC(IE+2,:,:)) ! Flux IE-1
 ZFNEG1(IE,:,:)   = 1./6. * (11.0*PSRC(IE+1,:,:) - 7.0*PSRC(IE+2,:,:) + 2.0*PSRC(IE+3,:,:)) ! Flux IE
 ZBNEG1(IE-1,:,:) = 13./12. * (    PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IE,:,:) - 4.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2  ! Smoothness indicator IE-1
 ZBNEG1(IE,:,:)   = 13./12. * (    PSRC(IE+1,:,:) - 2.0*PSRC(IE+2,:,:) + PSRC(IE+3,:,:))**2 & 
      + 1./4    * (3.0*PSRC(IE+1,:,:) - 4.0*PSRC(IE+2,:,:) + PSRC(IE+3,:,:))**2  ! Smoothness indicator IE
 ZOMN1(IE-1:IE,:,:) = 1./10. / (ZEPS + ZBNEG1(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! Second negative stencil, needs indices i, i+1, i+2
 ZFNEG2(IE-1,:,:) = 1./6. * (2.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   - 1.0*PSRC(IE+1,:,:)) ! Flux IE-1
 ZFNEG2(IE,:,:)   = 1./6. * (2.0*PSRC(IE,:,:)   + 5.0*PSRC(IE+1,:,:) - 1.0*PSRC(IE+2,:,:)) ! Flux IE
 ZBNEG2(IE-1,:,:)  = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) + PSRC(IE+1,:,:))**2 &
      + 1./4   * (PSRC(IE-1,:,:) -                    PSRC(IE+1,:,:))**2 ! Smoothness indicator IE-1
 ZBNEG2(IE,:,:)   = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IE+1,:,:) + PSRC(IE+2,:,:))**2 &
      + 1./4   * (PSRC(IE,:,:) -                      PSRC(IE+2,:,:))**2 ! Smoothness indicator IE
 ZOMN2(IE-1:IE,:,:) = 3./5. / (ZEPS + ZBNEG2(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! Third negative stencil, needs indices i-1, i, i+1
 ZFNEG3(IE-1,:,:) = 1./6 * (-1.0*PSRC(IE-2,:,:) + 5.0*PSRC(IE-1,:,:) + 2.0*PSRC(IE,:,:)) ! Flux IE-1
 ZFNEG3(IE,:,:)   = 1./6 * (-1.0*PSRC(IE-1,:,:) + 5.0*PSRC(IE,:,:)   + 2.0*PSRC(IE+1,:,:)) ! Flux IE
 ZBNEG3(IE-1,:,:) = 13./12 * (PSRC(IE-2,:,:) - 2.0*PSRC(IE-1,:,:) +     PSRC(IE,:,:))**2 &
        + 1./4   * (PSRC(IE-2,:,:) - 4.0*PSRC(IE-1,:,:) + 3.0*PSRC(IE,:,:))**2 ! Smoothness indicator IE-1
 ZBNEG3(IE,:,:)   = 13./12 * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) +     PSRC(IE+1,:,:))**2 &
        + 1./4   * (PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + 3.0*PSRC(IE+1,:,:))**2 ! Smoothness indicator IE
 ZOMN3(IE-1:IE,:,:) = 3./10. / (ZEPS + ZBNEG3(IE-1:IE,:,:))**2 ! Non-normalized weight IE-1,IE
!
 ! ----- Total flux -----
! 
 PR(IE-1:IE,:,:) = ( ZOMP1(IE-1:IE,:,:)/(ZOMP1(IE-1:IE,:,:)+ZOMP2(IE-1:IE,:,:)+ZOMP3(IE-1:IE,:,:)) * ZFPOS1(IE-1:IE,:,:) &
                       + ZOMP2(IE-1:IE,:,:)/(ZOMP1(IE-1:IE,:,:)+ZOMP2(IE-1:IE,:,:)+ZOMP3(IE-1:IE,:,:)) * ZFPOS2(IE-1:IE,:,:) & 
                       + ZOMP3(IE-1:IE,:,:)/(ZOMP1(IE-1:IE,:,:)+ZOMP2(IE-1:IE,:,:)+ZOMP3(IE-1:IE,:,:)) * ZFPOS3(IE-1:IE,:,:)) &
                      * (0.5+SIGN(0.5,PRUCT(IE-1:IE,:,:))) &
                    + ( ZOMN1(IE-1:IE,:,:)/(ZOMN1(IE-1:IE,:,:)+ZOMN2(IE-1:IE,:,:)+ZOMN3(IE-1:IE,:,:)) * ZFNEG1(IE-1:IE,:,:) &
                       + ZOMN2(IE-1:IE,:,:)/(ZOMN1(IE-1:IE,:,:)+ZOMN2(IE-1:IE,:,:)+ZOMN3(IE-1:IE,:,:)) * ZFNEG2(IE-1:IE,:,:) &
                       + ZOMN3(IE-1:IE,:,:)/(ZOMN1(IE-1:IE,:,:)+ZOMN2(IE-1:IE,:,:)+ZOMN3(IE-1:IE,:,:)) * ZFNEG3(IE-1:IE,:,:)) &
                      * (0.5-SIGN(0.5,PRUCT(IE-1:IE,:,:)))
! 
 END IF ! NHALO
!
END IF ! IF(LWEST_ll()) 
!-------------------------------------------------------------------------------
!
PR = PR * PRUCT ! Add contravariant flux
!
END SUBROUTINE ADVEC_WENO_K_3_UX
!
!------------------------------------------------------------------------------
!
!     ############################################################
      SUBROUTINE ADVEC_WENO_K_3_MX(HLBCX,PSRC, PRUCT, PR)
!     ############################################################
!!
!!**** Computes PRUCT * PWT (or PRUCT * PVT). Upstream fluxes of W (or V)
!!     variables in X direction.  
!!     Input PWT is on W Grid 'ie' (i,j,k) based on WGRID reference
!!     Output PR is on mass Grid 'ie' (i-1/2,j,k) based on WGRID reference  
!!
!!    AUTHOR
!!    ------
!!    F. Visentin   *CNRS/LA*                
!!
!!    MODIFICATIONS
!!    -------------
!! T. Lunet 02/10/2014:  Correction of periodic boudary conditions
!!       Change of structure in order to adapt WENO to NHALOK
!!       Suppression of second layer HALO pointers
!!       Complete code documentation
!!
!------------------------------------------------------------------------------
!
USE MODD_CONF
!
IMPLICIT NONE
!
!*       0.1   Declarations of dummy arguments :
!
CHARACTER (LEN=4), DIMENSION(2), INTENT(IN) :: HLBCX  ! X direction LBC type
!
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PSRC  ! variable on U grid at t
REAL, DIMENSION(:,:,:), INTENT(IN)  :: PRUCT ! contrav. comp. on MASS GRID
!
! output source term
!
REAL, DIMENSION(:,:,:), INTENT(OUT) :: PR
!
!*       0.2   Declarations of local variables :
!
INTEGER :: IIB,IJB    ! Begining useful area in x,y,z directions
INTEGER :: IIE,IJE    ! End useful area in x,y,z directions
INTEGER::  IW,IE   ! Coordinate of third order diffusion area
!
INTEGER:: ILUOUT,IRESP   ! for prints
!
! intermediate reconstruction fluxes for positive wind case
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZFPOS1, ZFPOS2, ZFPOS3
!
! intermediate reconstruction fluxes for negative wind case
!
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZFNEG1, ZFNEG2, ZFNEG3
!
! smoothness indicators for positive wind case
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZBPOS1, ZBPOS2, ZBPOS3
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)):: ZBNEG1, ZBNEG2, ZBNEG3
!
! WENO weights
!
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)) :: ZOMP1, ZOMP2, ZOMP3
REAL, DIMENSION(SIZE(PSRC,1),SIZE(PSRC,2),SIZE(PSRC,3)) :: ZOMN1, ZOMN2, ZOMN3
!
! EPSILON for weno weights calculation
! 
REAL, PARAMETER :: ZEPS = 1.0E-15
!
!-------------------------------------------------------------------------------
!
!*       0.3.     COMPUTES THE DOMAIN DIMENSIONS
!                 ------------------------------
!
CALL GET_INDICE_ll(IIB,IJB,IIE,IJE)
!
!-----------------------------------------------------------------------------
!
!*       0.4.   INITIALIZE THE FIELD 
!               ---------------------
!
PR(:,:,:) = 0.0
!
ZFPOS1 = 0.0
ZFPOS2 = 0.0
ZFPOS3 = 0.0
ZFNEG1 = 0.0
ZFNEG2 = 0.0
ZFNEG3 = 0.0
ZBPOS1 = 0.0
ZBPOS2 = 0.0
ZBPOS3 = 0.0
ZBNEG1 = 0.0
ZBNEG2 = 0.0
ZBNEG3 = 0.0
ZOMP1  = 0.0
ZOMP2  = 0.0
ZOMP3  = 0.0
ZOMN1  = 0.0
ZOMN2  = 0.0
ZOMN3  = 0.0 
!
!-------------------------------------------------------------------------------
!*       1.1.   Interior Fluxes 
!              ---------------------
IW=IIB
IE=IIE
!-------------------------------------------------------------------------------
! Flux calculation in the physical domain far enough from the boundary 
! WENO scheme order 5, IW+2 -> IE-1
! Computation at the mass point on Ugrid u(i-1/2,j,k)
!-------------------------------------------------------------------------------
! ----- Positive fluxes -----
!
! First positive stencil, needs indices i-3, i-2, i-1
ZFPOS1(IW+2:IE-1,:,:) = 1./6.   * (2.0*PSRC(IW-1:IE-4,:,:) - 7.0*PSRC(IW:IE-3,:,:) + 11.0*PSRC(IW+1:IE-2,:,:))   ! Flux
ZBPOS1(IW+2:IE-1,:,:) = 13./12. * (    PSRC(IW-1:IE-4,:,:) - 2.0*PSRC(IW:IE-3,:,:) +      PSRC(IW+1:IE-2,:,:))**2 & 
      + 1./4    * (    PSRC(IW-1:IE-4,:,:) - 4.0*PSRC(IW:IE-3,:,:) + 3.0* PSRC(IW+1:IE-2,:,:))**2  ! Smoothness indicator
ZOMP1(IW+2:IE-1,:,:)  = 1./10. /  (ZEPS + ZBPOS1(IW+2:IE-1,:,:))**2             ! Non-normalized weight
!
! Second positive stencil, needs indices i-2, i-1, i
ZFPOS2(IW+2:IE-1,:,:) = 1./6.  * (-1.0*PSRC(IW:IE-3,:,:) + 5.0*PSRC(IW+1:IE-2,:,:) + 2.0*PSRC(IW+2:IE-1,:,:))  ! Flux
ZBPOS2(IW+2:IE-1,:,:) = 13./12 * (     PSRC(IW:IE-3,:,:) - 2.0*PSRC(IW+1:IE-2,:,:) +     PSRC(IW+2:IE-1,:,:))**2 &
      + 1./4   * (     PSRC(IW:IE-3,:,:) -                               PSRC(IW+2:IE-1,:,:))**2 ! Smoothness indicator
ZOMP2(IW+2:IE-1,:,:)  = 3./5. /  (ZEPS + ZBPOS2(IW+2:IE-1,:,:))**2             ! Non-normalized weight
!
! Third positive stencil, needs indices i-1, i, i+1
ZFPOS3(IW+2:IE-1,:,:) = 1./6   * (2.0*PSRC(IW+1:IE-2,:,:) + 5.0*PSRC(IW+2:IE-1,:,:) - PSRC(IW+3:IE,:,:))  ! Flux
ZBPOS3(IW+2:IE-1,:,:) = 13./12 * ( PSRC(IW+1:IE-2,:,:) - 2.0*PSRC(IW+2:IE-1,:,:) + PSRC(IW+3:IE,:,:))**2 &
      + 1./4   * (3.0*PSRC(IW+1:IE-2,:,:) - 4.0*PSRC(IW+2:IE-1,:,:) + PSRC(IW+3:IE,:,:))**2 ! Smoothness indicator
ZOMP3(IW+2:IE-1,:,:)  = 3./10. / (ZEPS + ZBPOS3(IW+2:IE-1,:,:))**2           ! Non-normalized weight
!
! ----- Negative fluxes ----- 
!
! First negative stencil, needs indices i, i+1, i+2
ZFNEG1(IW+2:IE-1,:,:) = 1./6.   * (11.0*PSRC(IW+2:IE-1,:,:) - 7.0*PSRC(IW+3:IE,:,:) + 2.0*PSRC(IW+4:IE+1,:,:))   ! Flux
ZBNEG1(IW+2:IE-1,:,:) = 13./12. * (     PSRC(IW+2:IE-1,:,:) - 2.0*PSRC(IW+3:IE,:,:) +     PSRC(IW+4:IE+1,:,:))**2 & 
      + 1./4    * (3.0* PSRC(IW+2:IE-1,:,:) - 4.0*PSRC(IW+3:IE,:,:) +     PSRC(IW+4:IE+1,:,:))**2  ! Smoothness indicator
ZOMN1(IW+2:IE-1,:,:)  = 1./10. /  (ZEPS + ZBNEG1(IW+2:IE-1,:,:))**2             ! Non-normalized weight
!
! Second negative stencil, needs indices i-1, i, i+1
ZFNEG2(IW+2:IE-1,:,:) = 1./6.  * (2.0*PSRC(IW+1:IE-2,:,:) + 5.0*PSRC(IW+2:IE-1,:,:) - 1.0*PSRC(IW+3:IE,:,:))  ! Flux
ZBNEG2(IW+2:IE-1,:,:) = 13./12 * (    PSRC(IW+1:IE-2,:,:) - 2.0*PSRC(IW+2:IE-1,:,:) +     PSRC(IW+3:IE,:,:))**2 &
      + 1./4   * (    PSRC(IW+1:IE-2,:,:) -                               PSRC(IW+3:IE,:,:))**2 ! Smoothness indicator
ZOMN2(IW+2:IE-1,:,:)  = 3./5. /  (ZEPS + ZBNEG2(IW+2:IE-1,:,:))**2            ! Non-normalized weight
!
! Third negative stencil, needs indices i-2, i-1, i
ZFNEG3(IW+2:IE-1,:,:) = 1./6   * (-1.0*PSRC(IW:IE-3,:,:) + 5.0*PSRC(IW+1:IE-2,:,:) + 2.0*PSRC(IW+2:IE-1,:,:))  ! Flux
ZBNEG3(IW+2:IE-1,:,:) = 13./12 * (  PSRC(IW:IE-3,:,:) - 2.0*PSRC(IW+1:IE-2,:,:) +     PSRC(IW+2:IE-1,:,:))**2 &
      + 1./4   * (     PSRC(IW:IE-3,:,:) - 4.0*PSRC(IW+1:IE-2,:,:) + 3.0*PSRC(IW+2:IE-1,:,:))**2 ! Smoothness indicator
ZOMN3(IW+2:IE-1,:,:)  = 3./10. / (ZEPS + ZBNEG3(IW+2:IE-1,:,:))**2             ! Non-normalized weight
!
!
! ----- Total flux -----
!
PR(IW+2:IE-1,:,:) = (ZOMP1(IW+2:IE-1,:,:)/(ZOMP1(IW+2:IE-1,:,:)+ZOMP2(IW+2:IE-1,:,:)+ZOMP3(IW+2:IE-1,:,:)) &
           * ZFPOS1(IW+2:IE-1,:,:) + &
                      ZOMP2(IW+2:IE-1,:,:)/(ZOMP1(IW+2:IE-1,:,:)+ZOMP2(IW+2:IE-1,:,:)+ZOMP3(IW+2:IE-1,:,:)) &
           * ZFPOS2(IW+2:IE-1,:,:) + & 
                      ZOMP3(IW+2:IE-1,:,:)/(ZOMP1(IW+2:IE-1,:,:)+ZOMP2(IW+2:IE-1,:,:)+ZOMP3(IW+2:IE-1,:,:)) &
           * ZFPOS3(IW+2:IE-1,:,:)) &
                    * (0.5+SIGN(0.5,PRUCT(IW+2:IE-1,:,:))) &
                  + (ZOMN1(IW+2:IE-1,:,:)/(ZOMN1(IW+2:IE-1,:,:)+ZOMN2(IW+2:IE-1,:,:)+ZOMN3(IW+2:IE-1,:,:)) &
           * ZFNEG1(IW+2:IE-1,:,:)  &
                     + ZOMN2(IW+2:IE-1,:,:)/(ZOMN1(IW+2:IE-1,:,:)+ZOMN2(IW+2:IE-1,:,:)+ZOMN3(IW+2:IE-1,:,:)) &
           * ZFNEG2(IW+2:IE-1,:,:)  &
                     + ZOMN3(IW+2:IE-1,:,:)/(ZOMN1(IW+2:IE-1,:,:)+ZOMN2(IW+2:IE-1,:,:)+ZOMN3(IW+2:IE-1,:,:)) &
           * ZFNEG3(IW+2:IE-1,:,:))  & 
                    * (0.5-SIGN(0.5,PRUCT(IW+2:IE-1,:,:)))
!-------------------------------------------------------------------------------
!*       1.2.   West border
!               ---------------------
!! IF( LWEST_ll()  .AND. .FALSE. ) THEN 
IF( LWEST_ll() ) THEN 
 !-----------------------------------------------------------------------------
 ! West border is physical -- IW+1,IW
 !-----------------------------------------------------------------------------
 SELECT CASE (HLBCX(1)) ! X direction LBC type on left side
 CASE ('CYCL')
 !---------------------------------------------------------------------------
 ! Periodic boundary condition
 !---------------------------------------------------------------------------
!
 IF(LEAST_ll()  .AND. .FALSE. ) THEN  ! East border is physical
!
 ! First positive stencil, needs indices i-3, i-2, i-1 
 ZFPOS1(IW+1,:,:) = 1./6. * (2.0*PSRC(IE,:,:)   - 7.0*PSRC(IW-1,:,:) + 11.0*PSRC(IW,:,:)) ! Flux IW+1
 ZFPOS1(IW,:,:)   = 1./6. * (2.0*PSRC(IE-1,:,:) - 7.0*PSRC(IE,:,:)   + 11.0*PSRC(IW-1,:,:)) ! Flux IW
 ZBPOS1(IW+1,:,:) = 13./12. * (PSRC(IE,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 & 
        + 1./4    * (PSRC(IE,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2   ! Smoothness indicator IW+1
 ZBPOS1(IW,:,:) = 13./12. * (PSRC(IE-1,:,:) - 2.0*PSRC(IE,:,:) +     PSRC(IW-1,:,:))**2 & 
      + 1./4    * (PSRC(IE-1,:,:) - 4.0*PSRC(IE,:,:) + 3.0*PSRC(IW-1,:,:))**2  ! Smoothness indicator IW
 ZOMP1(IW:IW+1,:,:)  = 1./10. / (ZEPS + ZBPOS1(IW:IW+1,:,:))**2 ! Non-normalized weight IW+1,IW
!
 ! Second positive stencil, needs indices i-2, i-1, i
 ZFPOS2(IW+1,:,:) = 1./6.* (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:)) ! Flux IW+1
 ZFPOS2(IW,:,:)   = 1./6.* (-1.0*PSRC(IE,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW
 ZBPOS2(IW+1,:,:) = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) + PSRC(IW+1,:,:))**2 &
      + 1./4   * (PSRC(IW-1,:,:) -                    PSRC(IW+1,:,:))**2 ! Smoothness indicator IW+1
 ZBPOS2(IW,:,:)   = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IW-1,:,:) + PSRC(IW,:,:))**2 &
      + 1./4   * (PSRC(IE,:,:) -                      PSRC(IW,:,:))**2  ! Smoothness indicator IW
 ZOMP2(IW:IW+1,:,:)  = 3./5. / (ZEPS + ZBPOS2(IW:IW+1,:,:))**2  ! Non-normalized weight IW+1,IW
!
 ! Third negative stencil, needs indices i-2, i-1, i
 ZFNEG3(IW+1,:,:) = 1./6 * (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:)) ! Flux IW+1
 ZFNEG3(IW,:,:)   = 1./6 * (-1.0*PSRC(IE,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW
 ZBNEG3(IW+1,:,:) = 13./12 * (PSRC(IW-1,:,:) - 2.0*PSRC(IW,:,:) +     PSRC(IW+1,:,:))**2 &
        + 1./4   * (PSRC(IW-1,:,:) - 4.0*PSRC(IW,:,:) + 3.0*PSRC(IW+1,:,:))**2 ! Smoothness indicator IW+1
 ZBNEG3(IW,:,:)   = 13./12 * (PSRC(IE,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 &
        + 1./4   * (PSRC(IE,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2 ! Smoothness indicator IW
 ZOMN3(IW:IW+1,:,:) = 3./10. / (ZEPS + ZBNEG3(IW:IW+1,:,:))**2 ! Non-normalized weight IW+1,IW
!
 ELSEIF(IW>3) THEN ! East boundary is proc border, with minimum 3 HALO points on west side
!
 ! First positive stencil, needs indices i-3, i-2, i-1 
 ZFPOS1(IW+1,:,:) = 1./6. * (2.0*PSRC(IW-2,:,:)   - 7.0*PSRC(IW-1,:,:) + 11.0*PSRC(IW,:,:)) ! Flux IW+1
 ZFPOS1(IW,:,:)   = 1./6. * (2.0*PSRC(IW-3,:,:) - 7.0*PSRC(IW-2,:,:)   + 11.0*PSRC(IW-1,:,:)) ! Flux IW
 ZBPOS1(IW+1,:,:) = 13./12. * (PSRC(IW-2,:,:) - 2.0*PSRC(IW-1,:,:) +     PSRC(IW,:,:))**2 & 
        + 1./4    * (PSRC(IW-2,:,:) - 4.0*PSRC(IW-1,:,:) + 3.0*PSRC(IW,:,:))**2   ! Smoothness indicator IW+1
 ZBPOS1(IW,:,:) = 13./12. * (PSRC(IW-3,:,:) - 2.0*PSRC(IW-2,:,:) +     PSRC(IW-1,:,:))**2 & 
      + 1./4    * (PSRC(IW-3,:,:) - 4.0*PSRC(IW-2,:,:) + 3.0*PSRC(IW-1,:,:))**2  ! Smoothness indicator IW
 ZOMP1(IW:IW+1,:,:)  = 1./10. / (ZEPS + ZBPOS1(IW:IW+1,:,:))**2 ! Non-normalized weight IW+1,IW
!
 ! Second positive stencil, needs indices i-2, i-1, i
 ZFPOS2(IW+1,:,:) = 1./6.* (-1.0*PSRC(IW-1,:,:) + 5.0*PSRC(IW,:,:)   + 2.0*PSRC(IW+1,:,:)) ! Flux IW+1
 ZFPOS2(IW,:,:)   = 1./6.* (-1.0*PSRC(IW-2,:,:)   + 5.0*PSRC(IW-1,:,:) + 2.0*PSRC(IW,:,:)) ! Flux IW