Index: palm/trunk/SOURCE/advec_ws.f90
===================================================================
--- palm/trunk/SOURCE/advec_ws.f90	(revision 859)
+++ palm/trunk/SOURCE/advec_ws.f90	(revision 861)
@@ -4,14 +4,25 @@
 ! Current revisions:
 ! ------------------
+! ws-scheme also work with topography in combination with vector version.
+! ws-scheme also work with outflow boundaries in combination with
+! vector version.
+
+! Degradation of the applied order of scheme is now steered by multiplying with
+! Integer wall_flags_0. 2nd order scheme, WS3 and WS5 are calculated on each
+! grid point and mulitplied with the appropriate flag.
+! 2nd order numerical dissipation term changed. Now the appropriate 2nd order
+! term derived according to the 4th and 6th order terms is applied. It turns
+! out that diss_2nd does not provide sufficient dissipation near walls.
+! Therefore, the function diss_2nd is removed.
+
+! Near walls a divergence correction is necessary to overcome numerical
+! instabilities due to too less divergence reduction of the velocity field.
+!
+! boundary_flags and logicals steering the degradation are removed.
+! Empty SUBROUTINE local_diss removed.
 !
 ! Former revisions:
 ! -----------------
 ! $Id$
-!
-! 856 2012-03-20 18:29:17Z suehring
-! Bug concerning numerical dissipation at the first grid level. Function
-! diss_2nd always returns zero.  Because diss_2nd will be replaced by numerical
-! dissipation which is more consistent to the WS-schemes, the return value
-! is set to zero for now.
 !
 ! 801 2012-01-10 17:30:36Z suehring
@@ -61,19 +72,15 @@
 ! routine using for initialisation and steering of the statical evaluation. 
 ! The computation of turbulent fluxes takes place inside the advection
-! routines. 
-! In case of vector architectures Dirichlet and Radiation boundary conditions
-! are outstanding and not available.
-! A further routine local_diss_ij is available (next weeks) and is used if a 
-! control of dissipative fluxes is desired.
-!
-! OUTSTANDING: - Dirichlet and Radiation boundary conditions for 
-!                vector architectures
-!              - dissipation control for cache architectures ( next weeks )
-!              - Topography ( next weeks )
+! routines.
+! Near non-cyclic boundaries the order of the applied advection scheme is
+! degraded.
+! A divergence correction is applied. It is necessary for topography, since
+! the divergence is not sufficiently reduced, resulting in erroneous fluxes and
+! partly numerical instabilities. 
 !-----------------------------------------------------------------------------!
 
     PRIVATE
     PUBLIC   advec_s_ws, advec_u_ws, advec_v_ws, advec_w_ws, &
-             local_diss, ws_init, ws_statistics
+             ws_init, ws_statistics
 
     INTERFACE ws_init
@@ -104,9 +111,4 @@
        MODULE PROCEDURE advec_w_ws_ij
     END INTERFACE advec_w_ws
-
-    INTERFACE local_diss
-       MODULE PROCEDURE local_diss
-       MODULE PROCEDURE local_diss_ij
-    END INTERFACE local_diss
 
  CONTAINS
@@ -126,21 +128,11 @@
 
 !
-!--    Allocate arrays needed for dissipation control.
-       IF ( dissipation_control )  THEN
-!          ALLOCATE(var_x(nzb+1:nzt,nys:nyn,nxl:nxr),        &
-!                   var_y(nzb+1:nzt,nys:nyn,nxl:nxr),        &
-!                   var_z(nzb+1:nzt,nys:nyn,nxl:nxr),        &
-!                   gamma_x(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
-!                   gamma_y(nzb:nzt+1,nysg:nyng,nxlg:nxrg),  &
-!                   gamma_z(nzb:nzt+1,nysg:nyng,nxlg:nxrg))
-       ENDIF
-
-!
 !--    Set the appropriate factors for scalar and momentum advection.
        adv_sca_5 = 1./60.
        adv_sca_3 = 1./12.
+       adv_sca_1 = 1./2.
        adv_mom_5 = 1./120.
        adv_mom_3 = 1./24.
-
+       adv_mom_1 = 1./4.
 !         
 !--    Arrays needed for statical evaluation of fluxes.
@@ -231,29 +223,4 @@
        ENDIF
 
-!
-!--    Determine the flags where the order of the scheme for horizontal 
-!--    advection has to be degraded.
-       ALLOCATE( boundary_flags(nys:nyn,nxl:nxr) )
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-
-             boundary_flags(j,i) = 0
-             IF ( outflow_l )  THEN
-                IF ( i == nxlu  )  boundary_flags(j,i) = 5
-                IF ( i == nxl   )  boundary_flags(j,i) = 6
-             ELSEIF ( outflow_r )  THEN
-                IF ( i == nxr-1 )  boundary_flags(j,i) = 1
-                IF ( i == nxr   )  boundary_flags(j,i) = 2
-             ELSEIF ( outflow_n )  THEN
-                IF ( j == nyn-1 )  boundary_flags(j,i) = 3
-                IF ( j == nyn   )  boundary_flags(j,i) = 4
-             ELSEIF ( outflow_s )  THEN
-                IF ( j == nysv  )  boundary_flags(j,i) = 7
-                IF ( j == nys   )  boundary_flags(j,i) = 8
-             ENDIF
-
-          ENDDO
-       ENDDO
-       
     END SUBROUTINE ws_init
 
@@ -271,5 +238,5 @@
 !       
 !--    The arrays needed for statistical evaluation are set to to 0 at the 
-!--    begin of prognostic_equations.
+!--    beginning of prognostic_equations.
        IF ( ws_scheme_mom )  THEN
           sums_wsus_ws_l = 0.0
@@ -307,7 +274,6 @@
        IMPLICIT NONE
 
-       INTEGER ::  i, i_omp, j, k, tn
-       LOGICAL ::  degraded_l, degraded_s
-       REAL    ::  flux_d, diss_d, u_comp, v_comp
+       INTEGER ::  i, i_omp, j, k, tn, k_ppp, k_pp, k_mm
+       REAL    ::  flux_d, diss_d, u_comp, v_comp, div
        REAL, DIMENSION(:,:,:), POINTER    :: sk
        REAL, DIMENSION(nzb:nzt+1)         :: flux_t, diss_t, flux_r, diss_r,  &
@@ -320,237 +286,245 @@
        CHARACTER (LEN = *), INTENT(IN)    :: sk_char
 
-
-       degraded_l = .FALSE.
-       degraded_s = .FALSE.
-
-       IF ( boundary_flags(j,i) /= 0 )  THEN
-!       
-!--       Degrade the order for Dirichlet bc. at the outflow boundary
-          SELECT CASE ( boundary_flags(j,i) )
-
-             CASE ( 1 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   u_comp    = u(k,j,i+1) - u_gtrans
-                   flux_r(k) = u_comp * (                                      &
-                               7.0 * ( sk(k,j,i+1) + sk(k,j,i)   )             &
-                                   - ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
-                   diss_r(k) = -ABS( u_comp ) * (                              &
-                               3.0 * ( sk(k,j,i+1) - sk(k,j,i)   )             &
-                                   - ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
-                ENDDO
-
-             CASE ( 2 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   u_comp    = u(k,j,i+1) - u_gtrans
-                   flux_r(k) = u_comp * ( sk(k,j,i+1) + sk(k,j,i) ) * 0.5
-                   diss_r(k) = diss_2nd( sk(k,j,i+1), sk(k,j,i+1), sk(k,j,i),  &
-                                         sk(k,j,i-1), sk(k,j,i-2), u_comp,     &
-                                         0.5, ddx )
-                ENDDO
-
-             CASE ( 3 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   v_comp = v(k,j+1,i) - v_gtrans
-                   flux_n(k) = v_comp * (                                      &
-                               7.0 * ( sk(k,j+1,i) + sk(k,j,i)   )             &
-                                   - ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
-                   diss_n(k) = -ABS( v_comp ) * (                              &
-                               3.0 * ( sk(k,j+1,i) - sk(k,j,i)     )           &
-                                   - ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
-               ENDDO
-
-             CASE ( 4 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   v_comp    = v(k,j+1,i) - v_gtrans
-                   flux_n(k) = v_comp* ( sk(k,j+1,i) + sk(k,j,i) ) * 0.5
-                   diss_n(k) = diss_2nd( sk(k,j+1,i), sk(k,j+1,i), sk(k,j,i), &
-                                         sk(k,j-1,i), sk(k,j-2,i), v_comp,    &
-                                         0.5, ddy )     
-                ENDDO
-
-             CASE ( 5 )
-
-                DO  k = nzb_w_inner(j,i)+1, nzt
-                   u_comp    = u(k,j,i+1) - u_gtrans 
-                   flux_r(k) = u_comp  * (                                     &
-                               7.0 * ( sk(k,j,i+1) + sk(k,j,i)   )             &
-                                   - ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
-                   diss_r(k) = -ABS( u_comp ) * (                              &
-                               3.0 * ( sk(k,j,i+1) - sk(k,j,i)   )             &
-                                   - ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
-                ENDDO  
-
-             CASE ( 6 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   u_comp    = u(k,j,i+1) - u_gtrans
-                   flux_r(k) = u_comp * (                                      &
-                               7.0 * ( sk(k,j,i+1) + sk(k,j,i)   )             &
-                                   - ( sk(k,j,i+2) + sk(k,j,i-1) ) ) * adv_sca_3
-                   diss_r(k) = -ABS( u_comp ) * (                              &
-                               3.0 * ( sk(k,j,i+1) - sk(k,j,i)   )             &
-                                   - ( sk(k,j,i+2) - sk(k,j,i-1) ) ) * adv_sca_3
-!                              
-!--                Compute leftside fluxes for the left boundary of PE domain
-                   u_comp                 = u(k,j,i) - u_gtrans
-                   swap_flux_x_local(k,j,tn) = u_comp * (                         &
-                                            sk(k,j,i) + sk(k,j,i-1) ) * 0.5
-                   swap_diss_x_local(k,j,tn) = diss_2nd( sk(k,j,i+2),sk(k,j,i+1), &
-                                                      sk(k,j,i), sk(k,j,i-1),  &
-                                                      sk(k,j,i-1), u_comp,     &
-                                                      0.5, ddx ) 
-                ENDDO
-                degraded_l = .TRUE.
-
-             CASE ( 7 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   v_comp    = v(k,j+1,i)-v_gtrans
-                   flux_n(k) = v_comp * (                                      &
-                               7.0 * ( sk(k,j+1,i) + sk(k,j,i)   )             &
-                                   - ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
-                   diss_n(k) = -ABS( v_comp ) * (                              &
-                               3.0 * ( sk(k,j+1,i) - sk(k,j,i)   )             &
-                                   - ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
-                ENDDO
-
-             CASE ( 8 )
-
-                DO  k = nzb_s_inner(j,i)+1, nzt
-                   v_comp    = v(k,j+1,i) - v_gtrans 
-                   flux_n(k) = v_comp * (                                      &
-                               7.0 * ( sk(k,j+1,i) + sk(k,j,i)   )             &
-                                   - ( sk(k,j+2,i) + sk(k,j-1,i) ) ) * adv_sca_3
-                   diss_n(k) = -ABS( v_comp ) * (                              &
-                               3.0 * ( sk(k,j+1,i) - sk(k,j,i)   )             &
-                                   - ( sk(k,j+2,i) - sk(k,j-1,i) ) ) * adv_sca_3
-!                              
-!--                Compute southside fluxes for the south boundary of PE domain
-                   v_comp               = v(k,j,i) - v_gtrans
-                   swap_flux_y_local(k,tn) = v_comp *                             &
-                                          ( sk(k,j,i) + sk(k,j-1,i) ) * 0.5 
-                   swap_diss_y_local(k,tn) = diss_2nd( sk(k,j+2,i), sk(k,j+1,i),  &
-                                                    sk(k,j,i), sk(k,j-1,i),    &
-                                                    sk(k,j-1,i), v_comp,       &
-                                                    0.5, ddy )
-                ENDDO
-                degraded_s = .TRUE.
-               
-             CASE DEFAULT
-            
-          END SELECT
-
-!         
-!--       Compute the crosswise 5th order fluxes at the outflow
-          IF ( boundary_flags(j,i) == 1  .OR.  boundary_flags(j,i) == 2  .OR. &
-               boundary_flags(j,i) == 5  .OR.  boundary_flags(j,i) == 6 )  THEN
-
-             DO  k = nzb_s_inner(j,i)+1, nzt
-                v_comp    = v(k,j+1,i) - v_gtrans
-                flux_n(k) = v_comp * (                                         &
-                            37.0 * ( sk(k,j+1,i) + sk(k,j,i)   )               &
-                          -  8.0 * ( sk(k,j+2,i) + sk(k,j-1,i) )               &
-                          +        ( sk(k,j+3,i) + sk(k,j-2,i) ) ) * adv_sca_5
-                diss_n(k) = -ABS( v_comp ) * (                                 &
-                            10.0 * ( sk(k,j+1,i) - sk(k,j,i)   )               &
-                          -  5.0 * ( sk(k,j+2,i) - sk(k,j-1,i) )               &
-                          +        ( sk(k,j+3,i) - sk(k,j-2,i) ) ) * adv_sca_5
-             ENDDO
-            
-          ELSE
-         
-             DO  k = nzb_s_inner(j,i)+1, nzt
-                u_comp    = u(k,j,i+1) - u_gtrans  
-                flux_r(k) = u_comp * (                                         &
-                            37.0 * ( sk(k,j,i+1) + sk(k,j,i)   )               &
-                          -  8.0 * ( sk(k,j,i+2) + sk(k,j,i-1) )               &
-                          +        ( sk(k,j,i+3) + sk(k,j,i-2) ) ) * adv_sca_5
-                diss_r(k) = -ABS( u_comp ) * (                                 &
-                            10.0 * ( sk(k,j,i+1) - sk(k,j,i)   )               &
-                          -  5.0 * ( sk(k,j,i+2) - sk(k,j,i-1) )               &
-                          +        ( sk(k,j,i+3) - sk(k,j,i-2) ) ) * adv_sca_5
-             ENDDO
-            
-          ENDIF
-
-       ELSE
-               
-!
-!--       Compute the fifth order fluxes for the interior of PE domain.
-          DO  k = nzb_u_inner(j,i)+1, nzt
-             u_comp    = u(k,j,i+1) - u_gtrans
-             flux_r(k) = u_comp * (                                           &
-                         37.0 * ( sk(k,j,i+1) + sk(k,j,i)   )                 &
-                       -  8.0 * ( sk(k,j,i+2) + sk(k,j,i-1) )                 &
-                       +        ( sk(k,j,i+3) + sk(k,j,i-2) ) ) * adv_sca_5
-             diss_r(k) = -ABS( u_comp ) * (                                   &
-                         10.0 * ( sk(k,j,i+1) - sk(k,j,i)   )                 &
-                       -  5.0 * ( sk(k,j,i+2) - sk(k,j,i-1) )                 &
-                       +        ( sk(k,j,i+3) - sk(k,j,i-2) ) ) * adv_sca_5
-
-             v_comp    = v(k,j+1,i) - v_gtrans
-             flux_n(k) = v_comp * (                                           &
-                         37.0 * ( sk(k,j+1,i) + sk(k,j,i)   )                 &
-                       -  8.0 * ( sk(k,j+2,i) + sk(k,j-1,i) )                 &
-                       +        ( sk(k,j+3,i) + sk(k,j-2,i) ) ) * adv_sca_5
-             diss_n(k) = -ABS( v_comp ) * (                                   &
-                         10.0 * ( sk(k,j+1,i) - sk(k,j,i)   )                 &
-                       -  5.0 * ( sk(k,j+2,i) - sk(k,j-1,i) )                 &
-                       +        ( sk(k,j+3,i) - sk(k,j-2,i) ) ) * adv_sca_5
-          ENDDO
-          
+!
+!--    Compute southside fluxes of the respective PE bounds.
+       IF ( j == nys )  THEN
+!
+!--       Up to the top of the highest topography.
+          DO  k = nzb+1, nzb_max
+
+             v_comp                  = v(k,j,i) - v_gtrans
+             swap_flux_y_local(k,tn) = v_comp * (                             &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)  + sk(k,j-1,i)     )         &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j+1,i) + sk(k,j-2,i)    )         &
+                  +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                         ) *                                                  &
+                                     ( sk(k,j+2,i) + sk(k,j-3,i)    )         &
+                                                )
+
+             swap_diss_y_local(k,tn) = -ABS( v_comp ) * (                     &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)   - sk(k,j-1,i)    )         &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j+1,i) - sk(k,j-2,i)  )           &
+                  +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                         ) *                                                  &
+                                     ( sk(k,j+2,i) - sk(k,j-3,i) )            &
+                                                        )
+
+          ENDDO
+!
+!--       Above to the top of the highest topography. No degradation necessary.
+          DO  k = nzb_max+1, nzt
+
+             v_comp               = v(k,j,i) - v_gtrans
+             swap_flux_y_local(k,tn) = v_comp * (                            &
+                                    37.0 * ( sk(k,j,i)   + sk(k,j-1,i) )     &
+                                  -  8.0 * ( sk(k,j+1,i) + sk(k,j-2,i) )     &
+                                  +        ( sk(k,j+2,i) + sk(k,j-3,i) )     &
+                                             ) * adv_sca_5
+              swap_diss_y_local(k,tn) = -ABS( v_comp ) * (                   &
+                                    10.0 * ( sk(k,j,i)   - sk(k,j-1,i) )     &
+                                  -  5.0 * ( sk(k,j+1,i) - sk(k,j-2,i) )     &
+                                  +          sk(k,j+2,i) - sk(k,j-3,i)       &
+                                                        ) * adv_sca_5
+
+          ENDDO
+
        ENDIF
-!       
-!--    Compute left- and southside fluxes of the respective PE bounds.        
-       IF ( j == nys  .AND.  .NOT. degraded_s )  THEN
-       
-           DO  k = nzb_s_inner(j,i)+1, nzt
-              v_comp               = v(k,j,i) - v_gtrans
-              swap_flux_y_local(k,tn) = v_comp * (                               &
-                                     37.0 * ( sk(k,j,i)   + sk(k,j-1,i) )     &
-                                   -  8.0 * ( sk(k,j+1,i) + sk(k,j-2,i) )     &
-                                   +        ( sk(k,j+2,i) + sk(k,j-3,i) )     &
-                                              ) * adv_sca_5
-              swap_diss_y_local(k,tn) = -ABS( v_comp ) * (                       &
-                                     10.0 * ( sk(k,j,i)   - sk(k,j-1,i) )     &
-                                   -  5.0 * ( sk(k,j+1,i) - sk(k,j-2,i) )     &
-                                   +          sk(k,j+2,i) - sk(k,j-3,i)       &
-                                                      ) * adv_sca_5
-           ENDDO           
-          
-        ENDIF
+!
+!--    Compute leftside fluxes of the respective PE bounds.
+       IF ( i == i_omp )  THEN
         
-        IF ( i == i_omp  .AND.  .NOT. degraded_l )  THEN
-        
-           DO  k = nzb_s_inner(j,i)+1, nzt
-              u_comp                 = u(k,j,i) - u_gtrans
-              swap_flux_x_local(k,j,tn) = u_comp * (                             &
-                                       37.0 * ( sk(k,j,i)   + sk(k,j,i-1) )   &
-                                     -  8.0 * ( sk(k,j,i+1) + sk(k,j,i-2) )   &
-                                     +        ( sk(k,j,i+2) + sk(k,j,i-3) )   &
-                                                ) * adv_sca_5
-              swap_diss_x_local(k,j,tn) = -ABS( u_comp ) * (                     &
-                                       10.0 * ( sk(k,j,i)   - sk(k,j,i-1) )   &
-                                     -  5.0 * ( sk(k,j,i+1) - sk(k,j,i-2) )   &
-                                     +        ( sk(k,j,i+2) - sk(k,j,i-3) )   &
-                                                        ) * adv_sca_5
-           ENDDO
+          DO  k = nzb+1, nzb_max
+
+             u_comp                 = u(k,j,i) - u_gtrans
+             swap_flux_x_local(k,j,tn) = u_comp * (                           &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)   + sk(k,j,i-1)    )         &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j,i+1) + sk(k,j,i-2)    )         &
+                  +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                         ) *         ( sk(k,j,i+2) + sk(k,j,i-3)    )         &
+                                                  )
+
+              swap_diss_x_local(k,j,tn) = -ABS( u_comp ) * (                  &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)   - sk(k,j,i-1)    )         &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j,i+1) - sk(k,j,i-2)  )           &
+                  +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                         ) *                                                  &
+                                     ( sk(k,j,i+2) - sk(k,j,i-3) )            &
+                                                           )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp                 = u(k,j,i) - u_gtrans
+             swap_flux_x_local(k,j,tn) = u_comp * (                          &
+                                      37.0 * ( sk(k,j,i)   + sk(k,j,i-1) )   &
+                                    -  8.0 * ( sk(k,j,i+1) + sk(k,j,i-2) )   &
+                                    +        ( sk(k,j,i+2) + sk(k,j,i-3) )   &
+                                                  ) * adv_sca_5
+
+             swap_diss_x_local(k,j,tn) = -ABS( u_comp ) * (                  &
+                                      10.0 * ( sk(k,j,i)   - sk(k,j,i-1) )   &
+                                    -  5.0 * ( sk(k,j,i+1) - sk(k,j,i-2) )   &
+                                    +        ( sk(k,j,i+2) - sk(k,j,i-3) )   &
+                                                          ) * adv_sca_5
+
+          ENDDO
            
-        ENDIF
-
+       ENDIF
+
+       flux_t(0) = 0.0
+       diss_t(0) = 0.0
+       flux_d    = 0.0
+       diss_d    = 0.0
 !        
-!--    Now compute the tendency terms for the horizontal parts
-       DO  k = nzb_s_inner(j,i)+1, nzt
-          
-          tend(k,j,i) = tend(k,j,i) - (                                      &
-                          ( flux_r(k) + diss_r(k) - swap_flux_x_local(k,j,tn) - &
-                            swap_diss_x_local(k,j,tn) ) * ddx                   &
-                        + ( flux_n(k) + diss_n(k) - swap_flux_y_local(k,tn)   - &
-                            swap_diss_y_local(k,tn)   ) * ddy                   &
-                                      )
+!--    Now compute the fluxes and tendency terms for the horizontal and
+!--    vertical parts up to the top of the highest topography.
+       DO  k = nzb+1, nzb_max
+!
+!--       Note: It is faster to conduct all multiplications explicitly, e.g.
+!--       * adv_sca_5 ... than to determine a factor and multiplicate the
+!--       flux at the end. Indeed, per loop the first requires 6
+!--       multiplications more, but the the second requires 9 more internal
+!--       calls of IBITS(). 
+          u_comp    = u(k,j,i+1) - u_gtrans
+          flux_r(k) = u_comp * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j,i+1) + sk(k,j,i)   )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j,i+2) + sk(k,j,i-1) )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                     ) *     ( sk(k,j,i+3) + sk(k,j,i-2) )                  &
+                               )
+
+          diss_r(k) = -ABS( u_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j,i+1) - sk(k,j,i)  )                   &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j,i+2) - sk(k,j,i-1) )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k,j,i+3) - sk(k,j,i-2) )                  &
+                                       )
+
+          v_comp    = v(k,j+1,i) - v_gtrans
+          flux_n(k) = v_comp * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j+1,i) + sk(k,j,i)   )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j+2,i) + sk(k,j-1,i) )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k,j+3,i) + sk(k,j-2,i) )                  &
+                               )
+
+          diss_n(k) = -ABS( v_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j+1,i) - sk(k,j,i)    )                 &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j+2,i) - sk(k,j-1,i)  )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k,j+3,i) - sk(k,j-2,i) )                  &
+                                       )
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),8,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),6,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),8,1)
+
+
+          flux_t(k) = w(k,j,i) * (                                          &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)  + sk(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i) + sk(k-1,j,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *     ( sk(k_ppp,j,i)+ sk(k_mm,j,i) )                &
+                                 )
+
+          diss_t(k) = -ABS( w(k,j,i) ) * (                                  &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)   - sk(k,j,i)    )               &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i)  - sk(k-1,j,i)  )               &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k_ppp,j,i) - sk(k_mm,j,i) )               &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities caused
+!--       by a not sufficient reduction of divergences near topography. 
+          div         =   ( u(k,j,i+1) - u(k,j,i)   ) * ddx                   &
+                        + ( v(k,j+1,i) - v(k,j,i)   ) * ddy                   &
+                        + ( w(k,j,i)   - w(k-1,j,i) ) * ddzw(k)
+
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                        ( flux_r(k) + diss_r(k) - swap_flux_x_local(k,j,tn) - &
+                          swap_diss_x_local(k,j,tn)            ) * ddx        &
+                      + ( flux_n(k) + diss_n(k) - swap_flux_y_local(k,tn)   - &
+                          swap_diss_y_local(k,tn)              ) * ddy        &
+                      + ( flux_t(k) + diss_t(k) - flux_d - diss_d             &
+                                                               ) * ddzw(k)    &
+                                      ) + sk(k,j,i) * div
 
           swap_flux_y_local(k,tn)   = flux_n(k)
@@ -558,97 +532,95 @@
           swap_flux_x_local(k,j,tn) = flux_r(k)
           swap_diss_x_local(k,j,tn) = diss_r(k)
-          
+          flux_d                    = flux_t(k)
+          diss_d                    = diss_t(k)
+
        ENDDO
-
-!
-!--    Vertical advection, degradation of order near bottom and top. 
-!--    The fluxes flux_d and diss_d at the surface are 0. Due to later
-!--    calculation of statistics the top flux at the surface should be 0.
-       flux_t(nzb_s_inner(j,i)) = 0.0
-       diss_t(nzb_s_inner(j,i)) = 0.0
-
-!       
-!--    2nd-order scheme (bottom)
-       k         = nzb_s_inner(j,i)+1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) * 0.5
-
-!       
-!--    sk(k,j,i) is referenced three times to avoid an access below surface
-       diss_t(k) = diss_2nd( sk(k+2,j,i), sk(k+1,j,i), sk(k,j,i), sk(k,j,i),  &
-                             sk(k,j,i), w(k,j,i), 0.5, ddzw(k) )
-                   
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k) - flux_d - diss_d ) &
-                                   * ddzw(k)
-!
-!--    WS3 as an intermediate step (bottom)
-       k         = nzb_s_inner(j,i) + 2
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       flux_t(k) = w(k,j,i) * (                                               &
-                                7.0 * ( sk(k+1,j,i) + sk(k,j,i)   )           &
-                                    - ( sk(k+2,j,i) + sk(k-1,j,i) )           &
-                              ) * adv_sca_3
-       diss_t(k) = -ABS( w(k,j,i) ) * (                                       &
-                                3.0 * ( sk(k+1,j,i) - sk(k,j,i)   )           & 
-                                    - ( sk(k+2,j,i) - sk(k-1,j,i) )           &
-                                      ) * adv_sca_3
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k) - flux_d - diss_d ) &
-                                   * ddzw(k)
-!
-!--    WS5
-       DO  k = nzb_s_inner(j,i)+3, nzt-2
-       
-          flux_d    = flux_t(k-1)
-          diss_d    = diss_t(k-1)          
-          flux_t(k) = w(k,j,i) * (                                            &
-                                   37.0 * ( sk(k+1,j,i) + sk(k,j,i)   )       &
-                                 -  8.0 * ( sk(k+2,j,i) + sk(k-1,j,i) )       &
-                                 +        ( sk(k+3,j,i) + sk(k-2,j,i) )       &
-                                 ) * adv_sca_5
-          diss_t(k) = -ABS( w(k,j,i) ) * (                                     &
-                                           10.0 * ( sk(k+1,j,i) - sk(k,j,i)   )&
-                                         -  5.0 * ( sk(k+2,j,i) - sk(k-1,j,i) )&
-                                         +        ( sk(k+3,j,i) - sk(k-2,j,i) )&
-                                         ) * adv_sca_5
-
-          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
-                                    - ( flux_d + diss_d ) ) * ddzw(k)
+!
+!--    Now compute the fluxes and tendency terms for the horizontal and
+!--    vertical parts above the top of the highest topography. No degradation
+!--    for the horizontal parts, but for the vertical it is stell needed.
+       DO  k = nzb_max+1, nzt
+
+          u_comp    = u(k,j,i+1) - u_gtrans
+          flux_r(k) = u_comp * (                                            &
+                      37.0 * ( sk(k,j,i+1) + sk(k,j,i)   )                  &
+                    -  8.0 * ( sk(k,j,i+2) + sk(k,j,i-1) )                  &
+                    +        ( sk(k,j,i+3) + sk(k,j,i-2) ) ) * adv_sca_5
+          diss_r(k) = -ABS( u_comp ) * (                                    &
+                      10.0 * ( sk(k,j,i+1) - sk(k,j,i)   )                  &
+                    -  5.0 * ( sk(k,j,i+2) - sk(k,j,i-1) )                  &
+                    +        ( sk(k,j,i+3) - sk(k,j,i-2) ) ) * adv_sca_5
+
+          v_comp    = v(k,j+1,i) - v_gtrans
+          flux_n(k) = v_comp * (                                            &
+                      37.0 * ( sk(k,j+1,i) + sk(k,j,i)   )                  &
+                    -  8.0 * ( sk(k,j+2,i) + sk(k,j-1,i) )                  &
+                    +        ( sk(k,j+3,i) + sk(k,j-2,i) ) ) * adv_sca_5
+          diss_n(k) = -ABS( v_comp ) * (                                    &
+                      10.0 * ( sk(k,j+1,i) - sk(k,j,i)   )                  &
+                    -  5.0 * ( sk(k,j+2,i) - sk(k,j-1,i) )                  &
+                    +        ( sk(k,j+3,i) - sk(k,j-2,i) ) ) * adv_sca_5
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),8,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),6,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),8,1)
+
+          flux_t(k) = w(k,j,i) * (                                          &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)  + sk(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i) + sk(k-1,j,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *     ( sk(k_ppp,j,i)+ sk(k_mm,j,i) )                &
+                                 )
+
+          diss_t(k) = -ABS( w(k,j,i) ) * (                                  &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)   - sk(k,j,i)    )               &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i)  - sk(k-1,j,i)  )               &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k_ppp,j,i) - sk(k_mm,j,i) )               &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography.
+          div         =   ( u(k,j,i+1) - u(k,j,i)   ) * ddx                   &
+                        + ( v(k,j+1,i) - v(k,j,i)   ) * ddy                   &
+                        + ( w(k,j,i)   - w(k-1,j,i) ) * ddzw(k)
+
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                        ( flux_r(k) + diss_r(k) - swap_flux_x_local(k,j,tn) - &
+                          swap_diss_x_local(k,j,tn)            ) * ddx        &
+                      + ( flux_n(k) + diss_n(k) - swap_flux_y_local(k,tn)   - &
+                          swap_diss_y_local(k,tn)              ) * ddy        &
+                      + ( flux_t(k) + diss_t(k) - flux_d - diss_d             &
+                                                               ) * ddzw(k)    &
+                                      ) + sk(k,j,i) * div
+
+          swap_flux_y_local(k,tn)   = flux_n(k)
+          swap_diss_y_local(k,tn)   = diss_n(k)
+          swap_flux_x_local(k,j,tn) = flux_r(k)
+          swap_diss_x_local(k,j,tn) = diss_r(k)
+          flux_d                    = flux_t(k)
+          diss_d                    = diss_t(k)
 
        ENDDO
 
 !
-!--    WS3 as an intermediate step (top)
-       k         = nzt - 1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       flux_t(k) = w(k,j,i) * (                                                &
-                                7.0 * ( sk(k+1,j,i) + sk(k,j,i)   )            &
-                                    - ( sk(k+2,j,i) + sk(k-1,j,i) )            &
-                              ) * adv_sca_3
-       diss_t(k) = -ABS( w(k,j,i) ) * (                                        &
-                                        3.0 * ( sk(k+1,j,i) - sk(k,j,i)   )    &
-                                            - ( sk(k+2,j,i) - sk(k-1,j,i) )    &
-                                      ) * adv_sca_3
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k) - flux_d - diss_d ) &
-                                   * ddzw(k)
-!                                 
-!--    2nd-order scheme (top)
-       k         = nzt 
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) * 0.5
-
-!       
-!--    sk(k+1) is referenced two times to avoid a segmentation fault at top
-       diss_t(k) = diss_2nd( sk(k+1,j,i), sk(k+1,j,i), sk(k,j,i), sk(k-1,j,i), &
-                             sk(k-2,j,i), w(k,j,i), 0.5, ddzw(k) )
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k) - flux_d - diss_d ) &
-                                   * ddzw(k)
-!       
 !--    Evaluation of statistics
        SELECT CASE ( sk_char )
@@ -656,6 +628,6 @@
           CASE ( 'pt' )
 
-             DO  k = nzb_s_inner(j,i), nzt
-                sums_wspts_ws_l(k,tn) = sums_wspts_ws_l(k,tn) +                      &
+             DO  k = nzb, nzt
+                sums_wspts_ws_l(k,tn) = sums_wspts_ws_l(k,tn) +                &
                                        ( flux_t(k) + diss_t(k) )               &
                                  * weight_substep(intermediate_timestep_count)
@@ -664,6 +636,6 @@
           CASE ( 'sa' )
 
-             DO  k = nzb_s_inner(j,i), nzt
-                sums_wssas_ws_l(k,tn) = sums_wssas_ws_l(k,tn) +                      &
+             DO  k = nzb, nzt
+                sums_wssas_ws_l(k,tn) = sums_wssas_ws_l(k,tn) +                &
                                        ( flux_t(k) + diss_t(k) )               &
                                  * weight_substep(intermediate_timestep_count)
@@ -672,6 +644,6 @@
           CASE ( 'q' )
 
-             DO  k = nzb_s_inner(j,i), nzt
-                sums_wsqs_ws_l(k,tn)  = sums_wsqs_ws_l(k,tn) +                       &
+             DO  k = nzb, nzt
+                sums_wsqs_ws_l(k,tn)  = sums_wsqs_ws_l(k,tn) +                 &
                                       ( flux_t(k) + diss_t(k) )                &
                                  * weight_substep(intermediate_timestep_count)
@@ -699,155 +671,273 @@
        IMPLICIT NONE
 
-       INTEGER ::  i, i_omp, j, k, tn
-       LOGICAL ::  degraded_l, degraded_s
-       REAL    ::  gu, gv, flux_d, diss_d, u_comp_l, v_comp, w_comp
+       INTEGER ::  i, i_omp, j, k, tn, k_ppp, k_pp, k_mm
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp_l, v_comp, w_comp, div
        REAL, DIMENSION(nzb:nzt+1) ::  flux_t, diss_t, flux_r, diss_r,        &
                                       flux_n, diss_n, u_comp
-                                       
-       degraded_l = .FALSE.
-       degraded_s = .FALSE.
 
        gu = 2.0 * u_gtrans
        gv = 2.0 * v_gtrans
-         
-       IF ( boundary_flags(j,i) /= 0 )  THEN
-!       
-!--      Degrade the order for Dirichlet bc. at the outflow boundary
-         SELECT CASE ( boundary_flags(j,i) )
-         
-            CASE ( 1 )
-               DO  k = nzb_u_inner(j,i)+1, nzt
-                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                  flux_r(k) = ( u_comp(k) - gu ) * (                          &
-                              7.0 * ( u(k,j,i+1) + u(k,j,i)   )               &
-                            -       ( u(k,j,i+2) + u(k,j,i-1) ) ) * adv_mom_3
-                  diss_r(k) = - ABS( u_comp(k) - gu ) * (                     &
-                              3.0 * ( u(k,j,i+1) - u(k,j,i)   )               &
-                            -       ( u(k,j,i+2) - u(k,j,i-1) ) ) * adv_mom_3
-              ENDDO
-              
-            CASE ( 2 )
-               DO  k = nzb_u_inner(j,i)+1, nzt
-                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                  flux_r(k) = ( u_comp(k) - gu ) * (                          &
-                                u(k,j,i+1) + u(k,j,i) ) * 0.25 
-                  diss_r(k) = diss_2nd( u(k,j,i+1) ,u(k,j,i+1), u(k,j,i),     &
-                                        u(k,j,i-1), u(k,j,i-2), u_comp(k),    &
-                                        0.25, ddx )
-               ENDDO
-               
-            CASE ( 3 )
-               DO  k = nzb_u_inner(j,i)+1, nzt
-                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                  flux_n(k) = v_comp * (                                      &
-                              7.0 * ( u(k,j+1,i) + u(k,j,i)   )               &
-                            -       ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
-                  diss_n(k) = - ABS( v_comp ) * (                             &
-                              3.0 * ( u(k,j+1,i) - u(k,j,i)     )             &
-                            -       ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
-               ENDDO
-               
-            CASE ( 4 )
-               DO  k = nzb_u_inner(j,i)+1, nzt
-                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                  flux_n(k) = v_comp * ( u(k,j+1,i) + u(k,j,i) ) * 0.25
-                  diss_n(k) = diss_2nd( u(k,j+1,i), u(k,j+1,i), u(k,j,i),     &
-                                        u(k,j-1,i), u(k,j-2,i), v_comp,       &
-                                        0.25, ddy )
-               ENDDO
-               
-            CASE ( 5 )
-               DO  k = nzb_u_inner(j,i)+1, nzt
-!               
-!--               Compute leftside fluxes for the left boundary of PE domain
-                  u_comp(k)     = u(k,j,i) + u(k,j,i-1) - gu
-                  flux_l_u(k,j,tn) = u_comp(k) * ( u(k,j,i) + u(k,j,i-1) ) * 0.25
-                  diss_l_u(k,j,tn) = diss_2nd( u(k,j,i+2), u(k,j,i+1), u(k,j,i), &
-                                            u(k,j,i-1), u(k,j,i-1), u_comp(k),&
-                                            0.25, ddx )
-                 
-                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                  flux_r(k) = ( u_comp(k) - gu ) * (                          &
-                              7.0 * ( u(k,j,i+1) + u(k,j,i)   )               &
-                            -       ( u(k,j,i+2) + u(k,j,i-1) ) ) * adv_mom_3
-                  diss_r(k) = - ABS( u_comp(k) -gu ) * (                      &
-                              3.0 * ( u(k,j,i+1) - u(k,j,i)   )               &
-                            -       ( u(k,j,i+2) - u(k,j,i-1) ) ) * adv_mom_3
-               ENDDO
-               degraded_l = .TRUE.
-               
-            CASE ( 7 )
-               DO  k = nzb_u_inner(j,i)+1, nzt                           
-                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                  flux_n(k) = v_comp * (                                      &
-                              7.0 * ( u(k,j+1,i) + u(k,j,i)   )               &
-                            -       ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
-                  diss_n(k) = - ABS( v_comp ) * (                             &
-                              3.0 * ( u(k,j+1,i) - u(k,j,i)   )               &
-                            -       ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
-               ENDDO 
-               
-            CASE ( 8 )
-               DO  k = nzb_u_inner(j,i)+1, nzt
-!               
-!--              Compute southside fluxes for the south boundary of PE domain
-                  v_comp      = v(k,j,i) + v(k,j,i-1) - gv
-                  flux_s_u(k,tn) = v_comp * ( u(k,j,i) + u(k,j-1,i) ) * 0.25 
-                  diss_s_u(k,tn) = diss_2nd( u(k,j+2,i), u(k,j+1,i), u(k,j,i),   &
-                                          u(k,j-1,i), u(k,j-1,i), v_comp,     &
-                                          0.25, ddy )
-                                  
-                  v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                  flux_n(k) = v_comp * (                                      &
-                              7.0 * ( u(k,j+1,i) + u(k,j,i)   )               &
-                            -       ( u(k,j+2,i) + u(k,j-1,i) ) ) * adv_mom_3
-                  diss_n(k) = - ABS( v_comp ) * (                             &
-                              3.0 * ( u(k,j+1,i) - u(k,j,i)   )               &
-                            -       ( u(k,j+2,i) - u(k,j-1,i) ) ) * adv_mom_3
-               ENDDO  
-               degraded_s = .TRUE.
-               
-            CASE DEFAULT
-            
-         END SELECT
-!         
-!--      Compute the crosswise 5th order fluxes at the outflow
-         IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2  .OR.    &
-              boundary_flags(j,i) == 5 )  THEN
-         
-             DO  k = nzb_u_inner(j,i)+1, nzt
-               v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-               flux_n(k) = v_comp * (                                         &
-                           37.0 * ( u(k,j+1,i) + u(k,j,i)   )                 &
-                         -  8.0 * ( u(k,j+2,i) + u(k,j-1,i) )                 &
-                         +        ( u(k,j+3,i) + u(k,j-2,i) ) ) * adv_mom_5
-               diss_n(k) = - ABS ( v_comp ) * (                               &
-                           10.0 * ( u(k,j+1,i) - u(k,j,i)   )                 &
-                         -  5.0 * ( u(k,j+2,i) - u(k,j-1,i) )                 &
-                         +        ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
-             ENDDO
-             
-         ELSE
-         
-            DO  k = nzb_u_inner(j,i)+1, nzt
-               u_comp(k) = u(k,j,i+1) + u(k,j,i)
-               flux_r(k) = ( u_comp(k) - gu ) * (                             &
-                           37.0 * ( u(k,j,i+1) + u(k,j,i)   )                 &
-                         -  8.0 * ( u(k,j,i+2) + u(k,j,i-1) )                 &
-                         +        ( u(k,j,i+3) + u(k,j,i-2) ) ) * adv_mom_5
-               diss_r(k) = - ABS( u_comp(k) - gu ) * (                        &
-                           10.0 * ( u(k,j,i+1) - u(k,j,i) )                   &
-                         -  5.0 * ( u(k,j,i+2) - u(k,j,i-1) )                 &
-                         +        ( u(k,j,i+3) - u(k,j,i-2) ) ) * adv_mom_5
-            ENDDO
-            
-         ENDIF
-                  
-       ELSE
-!       
-!--       Compute the fifth order fluxes for the interior of PE domain.
-          DO  k = nzb_u_inner(j,i)+1, nzt
-             u_comp(k) = u(k,j,i+1) + u(k,j,i)
-             flux_r(k) = ( u_comp(k) - gu ) * (                               &
+!
+!--    Compute southside fluxes for the respective boundary of PE
+       IF ( j == nys  )  THEN
+       
+          DO  k = nzb+1, nzb_max
+
+             v_comp      = v(k,j,i) + v(k,j,i-1) - gv
+             flux_s_u(k,tn) = v_comp * (                                      &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( u(k,j,i)   + u(k,j-1,i) )              &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( u(k,j+1,i) + u(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( u(k,j+2,i) + u(k,j-3,i) )              &
+                                       )
+
+             diss_s_u(k,tn) = - ABS ( v_comp ) * (                            &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( u(k,j,i)   - u(k,j-1,i) )              &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( u(k,j+1,i) - u(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( u(k,j+2,i) - u(k,j-3,i) )              &
+                                                 )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             v_comp         = v(k,j,i) + v(k,j,i-1) - gv
+             flux_s_u(k,tn) = v_comp * (                                      &
+                           37.0 * ( u(k,j,i) + u(k,j-1,i)   )                 &
+                         -  8.0 * ( u(k,j+1,i) + u(k,j-2,i) )                 &
+                         +        ( u(k,j+2,i) + u(k,j-3,i) ) ) * adv_mom_5
+             diss_s_u(k,tn) = - ABS(v_comp) * (                               &
+                           10.0 * ( u(k,j,i) - u(k,j-1,i)   )                 &
+                         -  5.0 * ( u(k,j+1,i) - u(k,j-2,i) )                 &
+                         +        ( u(k,j+2,i) - u(k,j-3,i) ) ) * adv_mom_5
+
+          ENDDO
+          
+       ENDIF
+!
+!--    Compute leftside fluxes for the respective boundary of PE
+       IF ( i == i_omp )  THEN
+       
+          DO  k = nzb+1, nzb_max
+
+             u_comp_l         = u(k,j,i) + u(k,j,i-1) - gu
+             flux_l_u(k,j,tn) = u_comp_l * (                                   &
+                          ( 37.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +     7.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1 &
+                          ) *                                                  &
+                                     ( u(k,j,i)   + u(k,j,i-1) )               &
+                   -      (  8.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( u(k,j,i+1) + u(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( u(k,j,i+2) + u(k,j,i-3) )               &
+                                           )
+
+              diss_l_u(k,j,tn) = - ABS( u_comp_l ) * (                         &
+                          ( 10.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +     3.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1 &
+                          ) *                                                  &
+                                     ( u(k,j,i)   - u(k,j,i-1) )               &
+                   -      (  5.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( u(k,j,i+1) - u(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( u(k,j,i+2) - u(k,j,i-3) )               &
+                                                     )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp_l         = u(k,j,i) + u(k,j,i-1) - gu
+             flux_l_u(k,j,tn) = u_comp_l * (                                   &
+                             37.0 * ( u(k,j,i) + u(k,j,i-1)   )                &
+                           -  8.0 * ( u(k,j,i+1) + u(k,j,i-2) )                &
+                           +        ( u(k,j,i+2) + u(k,j,i-3) ) ) * adv_mom_5
+             diss_l_u(k,j,tn) = - ABS(u_comp_l) * (                            &
+                             10.0 * ( u(k,j,i) - u(k,j,i-1)   )                &
+                           -  5.0 * ( u(k,j,i+1) - u(k,j,i-2) )                &
+                           +        ( u(k,j,i+2) - u(k,j,i-3) ) ) * adv_mom_5
+
+          ENDDO
+          
+       ENDIF
+
+       flux_t(0) = 0.0
+       diss_t(0) = 0.0
+       flux_d    = 0.0
+       diss_d    = 0.0
+!
+!--    Now compute the fluxes tendency terms for the horizontal and
+!--    vertical parts.
+       DO  k = nzb+1, nzb_max
+
+          u_comp(k) = u(k,j,i+1) + u(k,j,i)
+          flux_r(k) = ( u_comp(k) - gu ) * (                                 &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j,i+1) + u(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j,i+2) + u(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j,i+3) + u(k,j,i-2) )                 &
+                                           )
+
+          diss_r(k) = - ABS( u_comp(k) - gu ) * (                            &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j,i+1) - u(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j,i+2) - u(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j,i+3) - u(k,j,i-2) )                 &
+                                                )
+
+          v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+          flux_n(k) = v_comp * (                                             &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j+1,i) + u(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j+2,i) + u(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j+3,i) + u(k,j-2,i) )                 &
+                                           )
+
+          diss_n(k) = - ABS ( v_comp ) * (                                   &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j+1,i) - u(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j+2,i) - u(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j+3,i) - u(k,j-2,i) )                 &
+                                                )
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),17,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),15,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),17,1)
+
+          w_comp    = w(k,j,i) + w(k,j,i-1)
+          flux_t(k) = w_comp  * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)  + u(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i) + u(k-1,j,i)   )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) + u(k_mm,j,i) )                  &
+                                 )
+
+          diss_t(k) = - ABS( w_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)   - u(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i)  - u(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) - u(k_mm,j,i) )                  &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography. 
+          div = ( ( u_comp(k)   - ( u(k,j,i)   + u(k,j,i-1)   ) ) * ddx      &
+               +  ( v_comp + gv - ( v(k,j,i)   + v(k,j,i-1 )  ) ) * ddy      &
+               +  ( w_comp      - ( w(k-1,j,i) + w(k-1,j,i-1) ) ) * ddzw(k)  &
+                ) * 0.5
+
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                            ( flux_r(k) + diss_r(k)                           &
+                          -   flux_l_u(k,j,tn) - diss_l_u(k,j,tn) ) * ddx     &
+                          + ( flux_n(k) + diss_n(k)                           &
+                          -   flux_s_u(k,tn) - diss_s_u(k,tn)     ) * ddy     &
+                          + ( flux_t(k) + diss_t(k)                           &
+                          -   flux_d    - diss_d                  ) * ddzw(k) &
+                                       ) + div * u(k,j,i)
+
+           flux_l_u(k,j,tn) = flux_r(k)
+           diss_l_u(k,j,tn) = diss_r(k)
+           flux_s_u(k,tn)   = flux_n(k)
+           diss_s_u(k,tn)   = diss_n(k)
+           flux_d           = flux_t(k)
+           diss_d           = diss_t(k)
+!
+!--        Statistical Evaluation of u'u'. The factor has to be applied for
+!--        right evaluation when gallilei_trans = .T. .
+           sums_us2_ws_l(k,tn) = sums_us2_ws_l(k,tn)                          &
+                              + ( flux_r(k) *                                 &
+                                ( u_comp(k) - 2.0 * hom(k,1,1,0) )            &
+                              / ( u_comp(k) - gu + 1.0E-20      )             &
+                              +   diss_r(k) *                                 &
+                                  ABS( u_comp(k) - 2.0 * hom(k,1,1,0) )       &
+                              / ( ABS( u_comp(k) - gu ) + 1.0E-20 ) )         &
+                              *   weight_substep(intermediate_timestep_count)
+!
+!--        Statistical Evaluation of w'u'.
+           sums_wsus_ws_l(k,tn) = sums_wsus_ws_l(k,tn)                        &
+                              + ( flux_t(k) + diss_t(k) )                     &
+                              *   weight_substep(intermediate_timestep_count)
+       ENDDO
+
+       DO  k = nzb_max+1, nzt
+
+          u_comp(k) = u(k,j,i+1) + u(k,j,i)
+          flux_r(k) = ( u_comp(k) - gu ) * (                                  &
                          37.0 * ( u(k,j,i+1) + u(k,j,i)   )                   &
                        -  8.0 * ( u(k,j,i+2) + u(k,j,i-1) )                   &
@@ -867,48 +957,58 @@
                        -  5.0 * ( u(k,j+2,i) - u(k,j-1,i) )                   &
                        +        ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
-          ENDDO 
-         
-       ENDIF
-!       
-!--    Compute left- and southside fluxes for the respective boundary of PE
-       IF ( j == nys .AND. ( .NOT. degraded_s ) )  THEN
-       
-          DO  k = nzb_u_inner(j,i)+1, nzt
-             v_comp      = v(k,j,i) + v(k,j,i-1) - gv
-             flux_s_u(k,tn) = v_comp * (                                         &
-                           37.0 * ( u(k,j,i) + u(k,j-1,i)   )                 &
-                         -  8.0 * ( u(k,j+1,i) + u(k,j-2,i) )                 &
-                         +        ( u(k,j+2,i) + u(k,j-3,i) ) ) * adv_mom_5
-             diss_s_u(k,tn) = - ABS(v_comp) * (                                  &
-                           10.0 * ( u(k,j,i) - u(k,j-1,i)   )                 &
-                         -  5.0 * ( u(k,j+1,i) - u(k,j-2,i) )                 &
-                         +        ( u(k,j+2,i) - u(k,j-3,i) ) ) * adv_mom_5
-          ENDDO
-          
-       ENDIF
-       
-       IF ( i == i_omp .AND. ( .NOT. degraded_l ) )  THEN
-       
-          DO  k = nzb_u_inner(j,i)+1, nzt
-             u_comp_l      = u(k,j,i)+u(k,j,i-1)-gu
-             flux_l_u(k,j,tn) = u_comp_l * (                                     &
-                             37.0 * ( u(k,j,i) + u(k,j,i-1)   )               &
-                           -  8.0 * ( u(k,j,i+1) + u(k,j,i-2) )               &
-                           +        ( u(k,j,i+2) + u(k,j,i-3) ) ) * adv_mom_5
-             diss_l_u(k,j,tn) = - ABS(u_comp_l) * (                              &
-                             10.0 * ( u(k,j,i) - u(k,j,i-1)   )               &
-                           -  5.0 * ( u(k,j,i+1) - u(k,j,i-2) )               &
-                           +        ( u(k,j,i+2) - u(k,j,i-3) ) ) * adv_mom_5
-          ENDDO
-          
-       ENDIF
-!       
-!--    Now compute the tendency terms for the horizontal parts.
-       DO  k = nzb_u_inner(j,i)+1, nzt                   
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),17,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),15,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),17,1)
+
+          w_comp    = w(k,j,i) + w(k,j,i-1)
+          flux_t(k) = w_comp  * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)  + u(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i) + u(k-1,j,i)   )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) + u(k_mm,j,i) )                  &
+                                 )
+
+          diss_t(k) = - ABS( w_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)   - u(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i)  - u(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) - u(k_mm,j,i) )                  &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography.
+          div = ( ( u_comp(k)   - ( u(k,j,i)   + u(k,j,i-1)   ) ) * ddx      &
+               +  ( v_comp + gv - ( v(k,j,i)   + v(k,j,i-1 )  ) ) * ddy      &
+               +  ( w_comp      - ( w(k-1,j,i) + w(k-1,j,i-1) ) ) * ddzw(k)  &
+                ) * 0.5
+
           tend(k,j,i) = tend(k,j,i) - (                                       &
                             ( flux_r(k) + diss_r(k)                           &
-                          -   flux_l_u(k,j,tn) - diss_l_u(k,j,tn) ) * ddx           &
+                          -   flux_l_u(k,j,tn) - diss_l_u(k,j,tn) ) * ddx     &
                           + ( flux_n(k) + diss_n(k)                           &
-                          -   flux_s_u(k,tn) - diss_s_u(k,tn)     ) * ddy  )
+                          -   flux_s_u(k,tn) - diss_s_u(k,tn)     ) * ddy     &
+                          + ( flux_t(k) + diss_t(k)                           &
+                          -   flux_d    - diss_d                  ) * ddzw(k) &
+                                       ) + div * u(k,j,i)
 
            flux_l_u(k,j,tn) = flux_r(k)
@@ -916,110 +1016,29 @@
            flux_s_u(k,tn)   = flux_n(k)
            diss_s_u(k,tn)   = diss_n(k)
-!
-!--        Statistical Evaluation of u'u'. The factor has to be applied for 
+           flux_d           = flux_t(k)
+           diss_d           = diss_t(k)
+!
+!--        Statistical Evaluation of u'u'. The factor has to be applied for
 !--        right evaluation when gallilei_trans = .T. .
-           sums_us2_ws_l(k,tn) = sums_us2_ws_l(k,tn)                                &
-             + ( flux_r(k) *                                                  &
-               ( u_comp(k) - 2.0 * hom(k,1,1,0) )                             &
-             / ( u_comp(k) - gu + 1.0E-20      )                              &
-             +   diss_r(k) *                                                  &
-                 ABS( u_comp(k) - 2.0 * hom(k,1,1,0) )                        &
-             / ( ABS( u_comp(k) - gu ) + 1.0E-20 ) )                          &
-             *   weight_substep(intermediate_timestep_count)
-       ENDDO 
-       sums_us2_ws_l(nzb_u_inner(j,i),tn) = sums_us2_ws_l(nzb_u_inner(j,i)+1,tn)
-                                           
-
-!
-!--    Vertical advection, degradation of order near surface and top. 
-!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--    statistical evaluation the top flux at the surface should be 0
-       flux_t(nzb_u_inner(j,i)) = 0. !statistical reasons
-       diss_t(nzb_u_inner(j,i)) = 0.
-!
-!--    2nd order scheme (bottom)
-       k         = nzb_u_inner(j,i)+1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j,i) + w(k,j,i-1)
-       flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) *0.25
-       diss_t(k) = diss_2nd( u(k+2,j,i), u(k+1,j,i), u(k,j,i), 0., 0.,        &
-                             w_comp, 0.25, ddzw(k) )
-             
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzw(k)
-!
-!--    WS3 as an intermediate step (bottom)
-       k         = nzb_u_inner(j,i)+2
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j,i) + w(k,j,i-1)
-       flux_t(k) = w_comp * (                                                 &
-                   7.0 * ( u(k+1,j,i) + u(k,j,i)   )                          &
-                 -       ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
-       diss_t(k) = -ABS( w_comp) * (                                          &
-                   3.0 * ( u(k+1,j,i) - u(k,j,i)   )                          & 
-                 -       ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzw(k)
-!
-!--    WS5
-       DO  k = nzb_u_inner(j,i)+3, nzt-2
-          flux_d    = flux_t(k-1)
-          diss_d    = diss_t(k-1)
-          w_comp    = w(k,j,i) + w(k,j,i-1)
-          flux_t(k) = w_comp * (                                              &
-                      37.0 * ( u(k+1,j,i) + u(k,j,i)   )                      &
-                    -  8.0 * ( u(k+2,j,i) + u(k-1,j,i) )                      &
-                    +        ( u(k+3,j,i) + u(k-2,j,i) ) ) * adv_mom_5
-          diss_t(k) = - ABS( w_comp ) * (                                     &
-                      10.0 *  ( u(k+1,j,i) - u(k,j,i)   )                     & 
-                    -  5.0 * ( u(k+2,j,i) - u(k-1,j,i) )                      &
-                    +        ( u(k+3,j,i) - u(k-2,j,i) ) ) * adv_mom_5
-
-          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
-                                    -   flux_d    - diss_d      ) * ddzw(k)
+           sums_us2_ws_l(k,tn) = sums_us2_ws_l(k,tn)                          &
+                              + ( flux_r(k) *                                 &
+                                ( u_comp(k) - 2.0 * hom(k,1,1,0) )            &
+                              / ( u_comp(k) - gu + 1.0E-20      )             &
+                              +   diss_r(k) *                                 &
+                                  ABS( u_comp(k) - 2.0 * hom(k,1,1,0) )       &
+                              / ( ABS( u_comp(k) - gu ) + 1.0E-20 ) )         &
+                              *   weight_substep(intermediate_timestep_count)
+!
+!--        Statistical Evaluation of w'u'.
+           sums_wsus_ws_l(k,tn) = sums_wsus_ws_l(k,tn)                        &
+                              + ( flux_t(k) + diss_t(k) )                     &
+                              *   weight_substep(intermediate_timestep_count)
        ENDDO
-!
-!--    WS3 as an intermediate step (top)
-       k         = nzt - 1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j,i) + w(k,j,i-1)
-       flux_t(k) = w_comp * (                                                 &
-                   7.0 * ( u(k+1,j,i) + u(k,j,i)   )                          &
-                 -       ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
-       diss_t(k) = - ABS( w_comp ) * (                                        &
-                   3.0 * ( u(k+1,j,i) - u(k,j,i)   )                          & 
-                 -       ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
-                   
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzw(k)
-       
-!
-!--    2nd order scheme (top)
-       k         = nzt
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j,i)+w(k,j,i-1)
-       flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) * 0.25
-       diss_t(k) = diss_2nd( u(k+1,j,i), u(k+1,j,i), u(k,j,i), u(k-1,j,i),    &
-                             u(k-2,j,i), w_comp, 0.25, ddzw(k) )
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzw(k)
-
-!
-!--    sum up the vertical momentum fluxes
-       DO  k = nzb_u_inner(j,i), nzt
-          sums_wsus_ws_l(k,tn) = sums_wsus_ws_l(k,tn)                               &
-              + ( flux_t(k) + diss_t(k) )                                     &
-              *   weight_substep(intermediate_timestep_count)
-       ENDDO
+
+       sums_us2_ws_l(nzb,tn) = sums_us2_ws_l(nzb+1,tn)
+
 
 
     END SUBROUTINE advec_u_ws_ij
-
 
 
@@ -1039,223 +1058,257 @@
        IMPLICIT NONE
 
-       INTEGER  ::  i, i_omp, j, k, tn
-       LOGICAL  ::  degraded_l, degraded_s
-       REAL     ::  gu, gv, flux_d, diss_d, u_comp, v_comp_l, w_comp
+       INTEGER  ::  i, i_omp, j, k, tn, k_ppp, k_pp, k_mm
+       REAL     ::  gu, gv, flux_d, diss_d, u_comp, v_comp_l, w_comp, div
        REAL, DIMENSION(nzb:nzt+1)  ::  flux_t, diss_t, flux_n,                &
                                        diss_n, flux_r, diss_r, v_comp
-                                       
-       degraded_l = .FALSE.
-       degraded_s = .FALSE.
-      
+
        gu = 2.0 * u_gtrans
        gv = 2.0 * v_gtrans
-       
-       IF ( boundary_flags(j,i) /= 0 )  THEN
+
 !       
-!--       Degrade the order for Dirichlet bc. at the outflow boundary
-          SELECT CASE ( boundary_flags(j,i) )
-          
-             CASE ( 1 )
-                DO  k = nzb_v_inner(j,i)+1, nzt
-                   u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                   flux_r(k) = u_comp * (                                     &
-                               7.0 * (v(k,j,i+1) + v(k,j,i)    )              &
-                             -       ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
-                   diss_r(k) = - ABS( u_comp ) * (                            &
-                               3.0 * ( v(k,j,i+1) - v(k,j,i)   )              &
-                             -       ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3
-                ENDDO
-                
-             CASE ( 2 )
-                DO  k = nzb_v_inner(j,i)+1, nzt
-                   u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                   flux_r(k) = u_comp * ( v(k,j,i+1) + v(k,j,i) ) * 0.25 
-                   diss_r(k) = diss_2nd( v(k,j,i+1), v(k,j,i+1), v(k,j,i),    &
-                                         v(k,j,i-1), v(k,j,i-2), u_comp,      &
-                                         0.25, ddx )
-                ENDDO
-                
-             CASE ( 3 )
-                DO  k = nzb_v_inner(j,i)+1, nzt
-                   v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                   flux_n(k) = ( v_comp(k)- gv ) * (                          &
-                               7.0 * ( v(k,j+1,i) + v(k,j,i)   )              &
-                             -       ( v(k,j+2,i) + v(k,j-1,i) ) ) * adv_mom_3
-                   diss_n(k) = - ABS( v_comp(k) - gv ) * (                    &
-                               3.0 * ( v(k,j+1,i) - v(k,j,i)   )              &
-                             -       ( v(k,j+2,i) - v(k,j-1,i) ) ) * adv_mom_3
-                ENDDO
-                
-             CASE ( 4 )
-                DO  k = nzb_v_inner(j,i)+1, nzt
-                   v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                   flux_n(k) = ( v_comp(k) - gv ) *                           &
-                               ( v(k,j+1,i) + v(k,j,i) ) * 0.25 
-                   diss_n(k) = diss_2nd( v(k,j+1,i), v(k,j+1,i), v(k,j,i),    &
-                                         v(k,j-1,i), v(k,j-2,i), v_comp(k),   &
-                                         0.25, ddy )
-                ENDDO
-                
-             CASE ( 5 )
-                DO  k = nzb_w_inner(j,i)+1, nzt
-                   u_comp    = u(k,j-1,i) + u(k,j,i) - gu 
-                   flux_r(k) = u_comp * (                                     &
-                               7.0 * ( v(k,j,i+1) + v(k,j,i)   )              &
-                             -       ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
-                   diss_r(k) = - ABS( u_comp ) * (                            &
-                               3.0 * ( w(k,j,i+1) - w(k,j,i)   )              &
-                             -       ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3
-                ENDDO
-                
-             CASE ( 6 )
-                DO  k = nzb_v_inner(j,i)+1, nzt
-                   u_comp        = u(k,j-1,i) + u(k,j,i) - gu
-                   flux_l_v(k,j,tn) = u_comp * ( v(k,j,i) + v(k,j,i-1) ) * 0.25
-                   diss_l_v(k,j,tn) = diss_2nd( v(k,j,i+2), v(k,j,i+1), v(k,j,i),&
-                                             v(k,j,i-1), v(k,j,i-1), u_comp,  &
-                                             0.25, ddx )
-                                  
-                   u_comp = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                   flux_r(k) = u_comp * (                                     &
-                               7.0 * ( v(k,j,i+1) + v(k,j,i)   )              &
-                             -       ( v(k,j,i+2) + v(k,j,i-1) ) ) * adv_mom_3
-                   diss_r(k) = - ABS( u_comp ) * (                            &
-                               3.0 * ( v(k,j,i+1) - v(k,j,i)   )              &
-                             -       ( v(k,j,i+2) - v(k,j,i-1) ) ) * adv_mom_3 
-                ENDDO
-                degraded_l = .TRUE.
-                
-             CASE ( 7 )
-                DO  k = nzb_v_inner(j,i)+1, nzt
-                   v_comp(k)   = v(k,j,i) + v(k,j-1,i) - gv
-                   flux_s_v(k,tn) = v_comp(k) * ( v(k,j,i) + v(k,j-1,i) ) * 0.25
-                   diss_s_v(k,tn) = diss_2nd( v(k,j+2,i), v(k,j+1,i), v(k,j,i),  &
-                                           v(k,j-1,i), v(k,j-1,i), v_comp(k), &
-                                           0.25, ddy )
-                                  
-                   v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                   flux_n(k) = ( v_comp(k) - gv ) * (                         &
-                               7.0 * ( v(k,j+1,i) + v(k,j,i)   )              &
-                             -       ( v(k,j+2,i) + v(k,j-1,i) ) ) * adv_mom_3
-                   diss_n(k) = - ABS( v_comp(k) - gv ) * (                    &
-                               3.0 * ( v(k,j+1,i) - v(k,j,i)   )              &
-                             -       ( v(k,j+2,i) - v(k,j-1,i) ) ) * adv_mom_3
-                ENDDO
-                degraded_s = .TRUE.
-                
-              CASE DEFAULT
-              
-          END SELECT
-!          
-!--       Compute the crosswise 5th order fluxes at the outflow
-          IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2  .OR.   &
-               boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
-          
-             DO  k = nzb_v_inner(j,i)+1, nzt
-                v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                flux_n(k) = ( v_comp(k) - gv ) * (                            &
-                            37.0 * ( v(k,j+1,i) + v(k,j,i)   )                &
-                          -  8.0 * ( v(k,j+2,i) + v(k,j-1,i) )                &
-                          +        ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
-                diss_n(k) = - ABS( v_comp(k) - gv ) * (                       &
-                             10.0 * ( v(k,j+1,i) - v(k,j,i)   )               &
-                           -  5.0 * ( v(k,j+2,i) - v(k,j-1,i) )               &
-                           +        ( v(k,j+3,i) - v(k,j-2,i) ) ) * adv_mom_5
-              ENDDO
-              
-          ELSE
-          
-             DO  k = nzb_v_inner(j,i)+1, nzt
-                u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                flux_r(k) = u_comp * (                                        &
-                            37.0 * ( v(k,j,i+1) + v(k,j,i)   )                &
-                          -  8.0 * ( v(k,j,i+2) + v(k,j,i-1) )                &
-                          +        ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
-                diss_r(k) = - ABS( u_comp ) * (                               &
-                            10.0 * ( v(k,j,i+1) - v(k,j,i)   )                &
-                          -  5.0 * ( v(k,j,i+2) - v(k,j,i-1) )                &
-                          +        ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
-             ENDDO
-             
-          ENDIF
-         
-       ELSE
-!       
-!--       Compute the fifth order fluxes for the interior of PE domain.                 
-          DO  k = nzb_v_inner(j,i)+1, nzt
-             u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-             flux_r(k) = u_comp * (                                           &
-                         37.0 * ( v(k,j,i+1) + v(k,j,i)   )                   &
-                       -  8.0 * ( v(k,j,i+2) + v(k,j,i-1) )                   &
-                       +        ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
-             diss_r(k) = - ABS( u_comp ) * (                                  &
-                         10.0 * ( v(k,j,i+1) - v(k,j,i) )                     &
-                       -  5.0 * ( v(k,j,i+2) - v(k,j,i-1) )                   &
-                       +        ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
-
-             v_comp(k) = v(k,j+1,i) + v(k,j,i)
-             flux_n(k) = ( v_comp(k) - gv ) * (                               &
-                         37.0 * ( v(k,j+1,i) + v(k,j,i)   )                   &
-                       -  8.0 * ( v(k,j+2,i) + v(k,j-1,i) )                   &
-                         +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
-             diss_n(k) = - ABS( v_comp(k) - gv ) * (                          &
-                         10.0 * ( v(k,j+1,i) - v(k,j,i)   )                   &
-                       -  5.0 * ( v(k,j+2,i) - v(k,j-1,i) )                   &
-                       +        ( v(k,j+3,i) - v(k,j-2,i) ) ) * adv_mom_5
-          ENDDO 
-          
-       ENDIF
-!       
-!--    Compute left- and southside fluxes for the respective boundary       
-       IF ( i == i_omp  .AND.  .NOT. degraded_l )  THEN
-
-          DO  k = nzb_v_inner(j,i)+1, nzt
-             u_comp        = u(k,j-1,i) + u(k,j,i) - gu
-             flux_l_v(k,j,tn) = u_comp * (                                       &
+!--    Compute leftside fluxes for the respective boundary.
+       IF ( i == i_omp )  THEN
+
+          DO  k = nzb+1, nzb_max
+
+             u_comp           = u(k,j-1,i) + u(k,j,i) - gu
+             flux_l_v(k,j,tn) = u_comp * (                                     &
+                          ( 37.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +     7.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( v(k,j,i)   + v(k,j,i-1) )               &
+                   -      (  8.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( v(k,j,i+1) + v(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( v(k,j,i+2) + v(k,j,i-3) )               &
+                                         )
+
+              diss_l_v(k,j,tn) = - ABS( u_comp ) * (                           &
+                          ( 10.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +     3.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( v(k,j,i)   - v(k,j,i-1) )               &
+                   -      (  5.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( v(k,j,i+1) - v(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( v(k,j,i+2) - v(k,j,i-3) )               &
+                                                   )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp           = u(k,j-1,i) + u(k,j,i) - gu
+             flux_l_v(k,j,tn) = u_comp * (                                    &
                              37.0 * ( v(k,j,i) + v(k,j,i-1)   )               &
                            -  8.0 * ( v(k,j,i+1) + v(k,j,i-2) )               &
                            +        ( v(k,j,i+2) + v(k,j,i-3) ) ) * adv_mom_5
-             diss_l_v(k,j,tn) = - ABS( u_comp ) * (                              &
+             diss_l_v(k,j,tn) = - ABS( u_comp ) * (                           &
                              10.0 * ( v(k,j,i) - v(k,j,i-1)   )               &
                            -  5.0 * ( v(k,j,i+1) - v(k,j,i-2) )               &
                            +        ( v(k,j,i+2) - v(k,j,i-3) ) ) * adv_mom_5
+
           ENDDO
           
        ENDIF
+!
+!--    Compute southside fluxes for the respective boundary.
+       IF ( j == nysv )  THEN
        
-       IF ( j == nysv  .AND.  .NOT. degraded_s )  THEN
-       
-          DO  k = nzb_v_inner(j,i)+1, nzt
-             v_comp_l    = v(k,j,i) + v(k,j-1,i) - gv
-             flux_s_v(k,tn) = v_comp_l * (                                       &
+          DO  k = nzb+1, nzb_max
+
+             v_comp_l       = v(k,j,i) + v(k,j-1,i) - gv
+             flux_s_v(k,tn) = v_comp_l * (                                    &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( v(k,j,i)   + v(k,j-1,i) )              &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( v(k,j+1,i) + v(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( v(k,j+2,i) + v(k,j-3,i) )              &
+                                         )
+
+             diss_s_v(k,tn) = - ABS( v_comp_l ) * (                           &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( v(k,j,i)   - v(k,j-1,i) )              &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( v(k,j+1,i) - v(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( v(k,j+2,i) - v(k,j-3,i) )              &
+                                                 )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             v_comp_l       = v(k,j,i) + v(k,j-1,i) - gv
+             flux_s_v(k,tn) = v_comp_l * (                                    &
                            37.0 * ( v(k,j,i) + v(k,j-1,i)   )                 &
                          -  8.0 * ( v(k,j+1,i) + v(k,j-2,i) )                 &
                          +        ( v(k,j+2,i) + v(k,j-3,i) ) ) * adv_mom_5
-             diss_s_v(k,tn) = - ABS( v_comp_l ) * (                              &
+             diss_s_v(k,tn) = - ABS( v_comp_l ) * (                           &
                            10.0 * ( v(k,j,i) - v(k,j-1,i)   )                 &
                          -  5.0 * ( v(k,j+1,i) - v(k,j-2,i) )                 &
                          +        ( v(k,j+2,i) - v(k,j-3,i) ) ) * adv_mom_5
+
           ENDDO
           
        ENDIF
-!       
-!--    Now compute the tendency terms for the horizontal parts.         
-       DO  k = nzb_v_inner(j,i)+1, nzt                 
+
+       flux_t(0) = 0.0
+       diss_t(0) = 0.0
+       flux_d    = 0.0
+       diss_d    = 0.0
+!
+!--    Now compute the fluxes and tendency terms for the horizontal and
+!--    verical parts.
+       DO  k = nzb+1, nzb_max
+
+          u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+          flux_r(k) = u_comp * (                                             &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j,i+1) + v(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j,i+2) + v(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j,i+3) + v(k,j,i-2) )                 &
+                               )
+
+          diss_r(k) = - ABS( u_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j,i+1) - v(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j,i+2) - v(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j,i+3) - v(k,j,i-2) )                 &
+                                        )
+
+          v_comp(k) = v(k,j+1,i) + v(k,j,i)
+          flux_n(k) = ( v_comp(k) - gv ) * (                                 &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j+1,i) + v(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j+2,i) + v(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j+3,i) + v(k,j-2,i) )                 &
+                               )
+
+          diss_n(k) = - ABS( v_comp(k) - gv ) * (                            &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j+1,i) - v(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j+2,i) - v(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j+3,i) - v(k,j-2,i) )                 &
+                                        )
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),26,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),24,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),26,1)
+
+          w_comp    = w(k,j-1,i) + w(k,j,i)
+          flux_t(k) = w_comp  * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   + v(k,j,i)    )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  + v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) + v(k_mm,j,i) )                  &
+                                 )
+
+          diss_t(k) = - ABS( w_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   - v(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  - v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) - v(k_mm,j,i) )                  &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography. 
+          div = ( ( u_comp + gu - ( u(k,j-1,i)   + u(k,j,i)   ) ) * ddx      &
+               +  ( v_comp(k)   - ( v(k,j,i)     + v(k,j-1,i) ) ) * ddy      &
+               +  ( w_comp      - ( w(k-1,j-1,i) + w(k-1,j,i) ) ) * ddzw(k)  &
+                ) * 0.5
+
           tend(k,j,i) = tend(k,j,i) - (                                       &
                          ( flux_r(k) + diss_r(k)                              &
-                       -   flux_l_v(k,j,tn) - diss_l_v(k,j,tn)   ) * ddx            &
+                       -   flux_l_v(k,j,tn) - diss_l_v(k,j,tn)   ) * ddx      &
                        + ( flux_n(k) + diss_n(k)                              &
-                       -   flux_s_v(k,tn) - diss_s_v(k,tn)       ) * ddy     )
-        
+                       -   flux_s_v(k,tn) - diss_s_v(k,tn)       ) * ddy      &
+                       + ( flux_t(k) + diss_t(k)                              &
+                       -   flux_d    - diss_d                    ) * ddzw(k)  &
+                                      ) + v(k,j,i) * div
+
            flux_l_v(k,j,tn) = flux_r(k)
            diss_l_v(k,j,tn) = diss_r(k)
            flux_s_v(k,tn)   = flux_n(k)
            diss_s_v(k,tn)   = diss_n(k)
-
-!
-!--        Statistical Evaluation of v'v'. The factor has to be applied for 
+           flux_d           = flux_t(k)
+           diss_d           = diss_t(k)
+
+!
+!--        Statistical Evaluation of v'v'. The factor has to be applied for
 !--        right evaluation when gallilei_trans = .T. .
-
-           sums_vs2_ws_l(k,tn) = sums_vs2_ws_l(k,tn)                                &
+           sums_vs2_ws_l(k,tn) = sums_vs2_ws_l(k,tn)                          &
              + ( flux_n(k)                                                    &
              * ( v_comp(k) - 2.0 * hom(k,1,2,0) )                             &
@@ -1265,92 +1318,118 @@
              / ( ABS( v_comp(k) - gv ) +1.0E-20 ) )                           &
              *   weight_substep(intermediate_timestep_count)
+!
+!--        Statistical Evaluation of w'v'.
+           sums_wsvs_ws_l(k,tn) = sums_wsvs_ws_l(k,tn)                        &
+                              + ( flux_t(k) + diss_t(k) )                     &
+                              *   weight_substep(intermediate_timestep_count)
 
        ENDDO
-       sums_vs2_ws_l(nzb_v_inner(j,i),tn) = sums_vs2_ws_l(nzb_v_inner(j,i)+1,tn)  
-                                           
-!
-!--    Vertical advection, degradation of order near surface and top. 
-!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--    statistical evaluation the top flux at the surface should be 0
-       flux_t(nzb_v_inner(j,i)) = 0.0  !statistical reasons
-       diss_t(nzb_v_inner(j,i)) = 0.0
-!
-!--    2nd order scheme (bottom)      
-       k         = nzb_v_inner(j,i)+1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1) 
-       w_comp    = w(k,j-1,i) + w(k,j,i)
-       flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
-       diss_t(k) = diss_2nd( v(k+2,j,i), v(k+1,j,i), v(k,j,i), 0.0, 0.0,      &
-                             w_comp, 0.25, ddzw(k) ) 
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d       ) * ddzw(k)
-        
-!
-!--    WS3 as an intermediate step (bottom)
-       k         = nzb_v_inner(j,i)+2
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j-1,i) + w(k,j,i)
-       flux_t(k) = w_comp * (                                                 &
-                   7.0 * ( v(k+1,j,i) + v(k,j,i)   )                          &
-                 -       ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
-       diss_t(k) = - ABS( w_comp ) * (                                        &
-                   3.0 * ( v(k+1,j,i) - v(k,j,i)   )                          & 
-                 -       ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d       ) * ddzw(k)
-!                                 
-!--    WS5
-       DO  k = nzb_v_inner(j,i)+3, nzt-2
-          flux_d    = flux_t(k-1)
-          diss_d    = diss_t(k-1)
+
+       DO  k = nzb_max+1, nzt
+
+          u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+          flux_r(k) = u_comp * (                                           &
+                      37.0 * ( v(k,j,i+1) + v(k,j,i)   )                   &
+                    -  8.0 * ( v(k,j,i+2) + v(k,j,i-1) )                   &
+                    +        ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
+
+          diss_r(k) = - ABS( u_comp ) * (                                  &
+                      10.0 * ( v(k,j,i+1) - v(k,j,i) )                     &
+                    -  5.0 * ( v(k,j,i+2) - v(k,j,i-1) )                   &
+                    +        ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
+
+
+          v_comp(k) = v(k,j+1,i) + v(k,j,i)
+          flux_n(k) = ( v_comp(k) - gv ) * (                               &
+                      37.0 * ( v(k,j+1,i) + v(k,j,i)   )                   &
+                    -  8.0 * ( v(k,j+2,i) + v(k,j-1,i) )                   &
+                      +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
+
+          diss_n(k) = - ABS( v_comp(k) - gv ) * (                          &
+                      10.0 * ( v(k,j+1,i) - v(k,j,i)   )                   &
+                    -  5.0 * ( v(k,j+2,i) - v(k,j-1,i) )                   &
+                    +        ( v(k,j+3,i) - v(k,j-2,i) ) ) * adv_mom_5
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),26,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),24,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),26,1)
+
           w_comp    = w(k,j-1,i) + w(k,j,i)
-          flux_t(k) = w_comp * (                                              &
-                      37.0 * ( v(k+1,j,i) + v(k,j,i)   )                      &           
-                    -  8.0 * ( v(k+2,j,i) + v(k-1,j,i) )                      &
-                    +      ( v(k+3,j,i) + v(k-2,j,i) ) ) * adv_mom_5
-          diss_t(k) = - ABS( w_comp ) * (                                     &
-                      10.0 * ( v(k+1,j,i) - v(k,j,i)   )                      & 
-                    -  5.0 * ( v(k+2,j,i) - v(k-1,j,i) )                      &
-                    +        ( v(k+3,j,i) - v(k-2,j,i) ) ) * adv_mom_5
-
-          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
-                                 -   flux_d    - diss_d       ) * ddzw(k)
+          flux_t(k) = w_comp  * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   + v(k,j,i)    )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  + v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) + v(k_mm,j,i) )                  &
+                                 )
+
+          diss_t(k) = - ABS( w_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   - v(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  - v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) - v(k_mm,j,i) )                  &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography.
+          div = ( ( u_comp + gu - ( u(k,j-1,i)   + u(k,j,i)   ) ) * ddx      &
+               +  ( v_comp(k)   - ( v(k,j,i)     + v(k,j-1,i) ) ) * ddy      &
+               +  ( w_comp      - ( w(k-1,j-1,i) + w(k-1,j,i) ) ) * ddzw(k)  &
+                ) * 0.5
+
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                         ( flux_r(k) + diss_r(k)                              &
+                       -   flux_l_v(k,j,tn) - diss_l_v(k,j,tn)   ) * ddx      &
+                       + ( flux_n(k) + diss_n(k)                              &
+                       -   flux_s_v(k,tn) - diss_s_v(k,tn)       ) * ddy      &
+                       + ( flux_t(k) + diss_t(k)                              &
+                       -   flux_d    - diss_d                    ) * ddzw(k)  &
+                                      ) + v(k,j,i) * div
+
+           flux_l_v(k,j,tn) = flux_r(k)
+           diss_l_v(k,j,tn) = diss_r(k)
+           flux_s_v(k,tn)   = flux_n(k)
+           diss_s_v(k,tn)   = diss_n(k)
+           flux_d           = flux_t(k)
+           diss_d           = diss_t(k)
+
+!
+!--        Statistical Evaluation of v'v'. The factor has to be applied for
+!--        right evaluation when gallilei_trans = .T. .
+           sums_vs2_ws_l(k,tn) = sums_vs2_ws_l(k,tn)                          &
+             + ( flux_n(k)                                                    &
+             * ( v_comp(k) - 2.0 * hom(k,1,2,0) )                             &
+             / ( v_comp(k) - gv + 1.0E-20 )                                   &
+             +   diss_n(k)                                                    &
+             *   ABS( v_comp(k) - 2.0 * hom(k,1,2,0) )                        &
+             / ( ABS( v_comp(k) - gv ) +1.0E-20 ) )                           &
+             *   weight_substep(intermediate_timestep_count)
+!
+!--        Statistical Evaluation of w'v'.
+           sums_wsvs_ws_l(k,tn) = sums_wsvs_ws_l(k,tn)                         &
+                              + ( flux_t(k) + diss_t(k) )                      &
+                              *   weight_substep(intermediate_timestep_count)
+
        ENDDO
-!
-!--    WS3 as an intermediate step (top)
-       k         = nzt - 1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j-1,i) + w(k,j,i)
-       flux_t(k) = w_comp * (                                                 &
-                   7.0 * ( v(k+1,j,i) + v(k,j,i)   )                          &    
-                 -       ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
-       diss_t(k) = - ABS( w_comp ) * (                                        &
-                   3.0 * ( v(k+1,j,i) - v(k,j,i) )                            & 
-                 -       ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d       ) * ddzw(k)
-!
-!--    2nd order scheme (top)
-       k         = nzt
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k,j-1,i)+w(k,j,i)
-       flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
-       diss_t(k) = diss_2nd( v(k+1,j,i), v(k+1,j,i), v(k,j,i), v(k-1,j,i),    &
-                             v(k-2,j,i), w_comp, 0.25, ddzw(k) )
- 
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d       ) * ddzw(k)
-              
-       DO  k = nzb_v_inner(j,i), nzt
-          sums_wsvs_ws_l(k,tn) = sums_wsvs_ws_l(k,tn)                               &
-                 + ( flux_t(k) + diss_t(k) )                                  &
-                 *   weight_substep(intermediate_timestep_count) 
-       ENDDO
+       sums_vs2_ws_l(nzb,tn) = sums_vs2_ws_l(nzb+1,tn)
+
 
     END SUBROUTINE advec_v_ws_ij
@@ -1372,317 +1451,352 @@
        IMPLICIT NONE
 
-       INTEGER ::  i, i_omp, j, k, tn
-       LOGICAL ::  degraded_l, degraded_s
-       REAL    ::  gu, gv, flux_d, diss_d, u_comp, v_comp, w_comp
+       INTEGER ::  i, i_omp, j, k, tn, k_ppp, k_pp, k_mm
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp, v_comp, w_comp, div
        REAL, DIMENSION(nzb:nzt+1)  ::  flux_t, diss_t, flux_r, diss_r, flux_n, &
                                        diss_n
-                                       
-       degraded_l = .FALSE.  
-       degraded_s = .FALSE.
 
        gu = 2.0 * u_gtrans
        gv = 2.0 * v_gtrans
 
-              
-       IF ( boundary_flags(j,i) /= 0 )  THEN
-!       
-!--      Degrade the order for Dirichlet bc. at the outflow boundary
-         SELECT CASE ( boundary_flags(j,i) )
-         
-            CASE ( 1 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                  flux_r(k) = u_comp * (                                      &
-                              7.0 * ( w(k,j,i+1) + w(k,j,i)   )               &
-                            -       ( w(k,j,i+2) + w(k,j,i-1) ) ) * adv_mom_3
-                  diss_r(k) = -ABS( u_comp ) * (                              &
-                              3.0 * ( w(k,j,i+1) - w(k,j,i)   )               & 
-                            -       ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3  
-               ENDDO
-               
-            CASE ( 2 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                  flux_r(k) = u_comp * ( w(k,j,i+1) + w(k,j,i) ) * 0.25 
-                  diss_r(k) = diss_2nd( w(k,j,i+1), w(k,j,i+1), w(k,j,i),     &
-                                        w(k,j,i-1), w(k,j,i-2), u_comp,       &
-                                        0.25, ddx )
-               ENDDO
-               
-            CASE ( 3 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                  flux_n(k) = v_comp * (                                      &
-                              7.0 * ( w(k,j+1,i) + w(k,j,i)   )               &
-                            -       ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
-                  diss_n(k) = -ABS( v_comp ) * (                              &
-                              3.0 * ( w(k,j+1,i) - w(k,j,i)   )               & 
-                            -       ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
-               ENDDO
-               
-            CASE ( 4 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                  flux_n(k) = v_comp * ( w(k,j+1,i) + w(k,j,i) ) * 0.25 
-                  diss_n(k) = diss_2nd( w(k,j+1,i), w(k,j+1,i), w(k,j,i),     &
-                                        w(k,j-1,i), w(k,j-2,i), v_comp,       &
-                                        0.25, ddy )
-               ENDDO
-               
-            CASE ( 5 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu 
-                  flux_r(k) = u_comp * (                                      &
-                              7.0 * ( w(k,j,i+1) + w(k,j,i)   )               &
-                            -       ( w(k,j,i+2) + w(k,j,i-1) ) ) * adv_mom_3
-                  diss_r(k) = - ABS( u_comp ) * (                             &
-                              3.0 * ( w(k,j,i+1) - w(k,j,i) )                 & 
-                            -       ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3
-               ENDDO
-               
-            CASE ( 6 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-!               
-!--               Compute leftside fluxes for the left boundary of PE domain
-                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                  flux_r(k) = u_comp *(                                       &
-                              7.0 * ( w(k,j,i+1) + w(k,j,i)   )               &
-                            -       ( w(k,j,i+2) + w(k,j,i-1) ) )  * adv_mom_3
-                  diss_r(k) = - ABS( u_comp ) * (                             &
-                              3.0 * ( w(k,j,i+1) - w(k,j,i) )                 & 
-                            -       ( w(k,j,i+2) - w(k,j,i-1) ) ) * adv_mom_3
-                  
-                  u_comp        = u(k+1,j,i) + u(k,j,i) - gu
-                  flux_l_w(k,j,tn) = u_comp * ( w(k,j,i) + w(k,j,i-1) ) * 0.25
-                  diss_l_w(k,j,tn) = diss_2nd( w(k,j,i+2), w(k,j,i+1), w(k,j,i), &
-                                            w(k,j,i-1), w(k,j,i-1), u_comp,   &
-                                            0.25, ddx )
-               ENDDO
-               degraded_l = .TRUE.
-               
-            CASE ( 7 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                  flux_n(k) = v_comp *(                                       &
-                              7.0 * ( w(k,j+1,i) + w(k,j,i)   )               &
-                            -       ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
-                  diss_n(k) = - ABS( v_comp ) * (                             &
-                              3.0 * ( w(k,j+1,i) - w(k,j,i)   )               & 
-                            -       ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
-                ENDDO
-                
-            CASE ( 8 )
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                  flux_n(k) = v_comp * (                                      &
-                              7.0 * ( w(k,j+1,i) + w(k,j,i)   )               &
-                            -       ( w(k,j+2,i) + w(k,j-1,i) ) ) * adv_mom_3
-                  diss_n(k) = - ABS( v_comp ) * (                             &
-                              3.0 * ( w(k,j+1,i) - w(k,j,i) )                 & 
-                            -       ( w(k,j+2,i) - w(k,j-1,i) ) ) * adv_mom_3
-!                    
-!--              Compute southside fluxes for the south boundary of PE domain           
-                  v_comp      = v(k+1,j,i) + v(k,j,i) - gv
-                  flux_s_w(k,tn) = v_comp * ( w(k,j,i) + w(k,j-1,i) ) * 0.25
-                  diss_s_w(k,tn) = diss_2nd( w(k,j+2,i), w(k,j+1,i), w(k,j,i),   &
-                                          w(k,j-1,i), w(k,j-1,i), v_comp,     &
-                                          0.25, ddy )                 
-               ENDDO  
-               degraded_s = .TRUE.
-               
-            CASE DEFAULT
-            
-         END SELECT
-!         
-!--      Compute the crosswise 5th order fluxes at the outflow
-         IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2  .OR.    &
-              boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )  THEN
-         
-            DO  k = nzb_w_inner(j,i)+1, nzt
-               v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-               flux_n(k) = v_comp * (                                         &
-                           37.0 * ( w(k,j+1,i) + w(k,j,i)   )                 &
-                         -  8.0 * ( w(k,j+2,i) + w(k,j-1,i) )                 &
-                         +        ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
-               diss_n(k) = - ABS( v_comp ) * (                                &
-                           10.0 * ( w(k,j+1,i) - w(k,j,i)   )                 &
-                         -  5.0 * ( w(k,j+2,i) - w(k,j-1,i) )                 &
-                         +        ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
-            ENDDO
-            
-         ELSE
-         
-            DO  k = nzb_w_inner(j,i)+1, nzt         
-               u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-               flux_r(k) = u_comp * (                                         &
-                           37.0 * ( w(k,j,i+1) + w(k,j,i) )                   &
-                         -  8.0 * ( w(k,j,i+2) + w(k,j,i-1) )                 &
-                         +        ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
-               diss_r(k) = - ABS( u_comp ) * (                                &
-                           10.0 * ( w(k,j,i+1) - w(k,j,i)   )                 &
-                         -  5.0 * ( w(k,j,i+2) - w(k,j,i-1) )                 &
-                         +        ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
-            ENDDO
-            
-         ENDIF
-         
-       ELSE
-!       
-!--      Compute the fifth order fluxes for the interior of PE domain.               
-         DO  k = nzb_w_inner(j,i)+1, nzt
-            u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-            flux_r(k) = u_comp * (                                            &
-                        37.0 * ( w(k,j,i+1) + w(k,j,i)   )                    &
-                      -  8.0 * ( w(k,j,i+2) + w(k,j,i-1) )                    &
-                      +        ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
-            diss_r(k) = - ABS( u_comp ) * (                                   &
-                        10.0 * ( w(k,j,i+1) - w(k,j,i)   )                    &
-                      -  5.0 * ( w(k,j,i+2) - w(k,j,i-1) )                    &
-                      +        ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
-                 
-            v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-            flux_n(k) = v_comp * (                                            &
-                        37.0 * ( w(k,j+1,i) + w(k,j,i)   )                    &
-                      -  8.0 * ( w(k,j+2,i) + w(k,j-1,i) )                    &
-                      +        ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
-            diss_n(k) = - ABS( v_comp ) * (                                   &
-                        10.0 * ( w(k,j+1,i) - w(k,j,i)   )                    &
-                      -  5.0 * ( w(k,j+2,i) - w(k,j-1,i) )                    &
-                      +        ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
-         ENDDO 
-         
-       ENDIF
-!       
-!--    Compute left- and southside fluxes for the respective boundary      
-       IF ( j == nys  .AND.  .NOT. degraded_s )  THEN
-       
-          DO  k = nzb_w_inner(j,i)+1, nzt
+!
+!--    Compute southside fluxes for the respective boundary.
+       IF ( j == nys )  THEN
+
+          DO  k = nzb+1, nzb_max
+
              v_comp      = v(k+1,j,i) + v(k,j,i) - gv
-             flux_s_w(k,tn) = v_comp * (                                         &
+             flux_s_w(k,tn) = v_comp * (                                      &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( w(k,j,i)   + w(k,j-1,i) )              &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( w(k,j+1,i) + w(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( w(k,j+2,i) + w(k,j-3,i) )              &
+                                         )
+
+             diss_s_w(k,tn) = - ABS( v_comp ) * (                             &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( w(k,j,i)   - w(k,j-1,i) )              &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( w(k,j+1,i) - w(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( w(k,j+2,i) - w(k,j-3,i) )              &
+                                                 )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             v_comp         = v(k+1,j,i) + v(k,j,i) - gv
+             flux_s_w(k,tn) = v_comp * (                                      &
                            37.0 * ( w(k,j,i) + w(k,j-1,i)   )                 &
                          -  8.0 * ( w(k,j+1,i) +w(k,j-2,i)  )                 &
                          +        ( w(k,j+2,i) + w(k,j-3,i) ) ) * adv_mom_5
-             diss_s_w(k,tn) = - ABS( v_comp ) * (                                &
+             diss_s_w(k,tn) = - ABS( v_comp ) * (                             &
                            10.0 * ( w(k,j,i) - w(k,j-1,i)   )                 &
                          -  5.0 * ( w(k,j+1,i) - w(k,j-2,i) )                 &
                          +        ( w(k,j+2,i) - w(k,j-3,i) ) ) * adv_mom_5
-          ENDDO
-          
+
+          ENDDO
+
        ENDIF
-       
-       IF ( i == i_omp  .AND.   .NOT. degraded_l ) THEN
-          
-          DO  k = nzb_w_inner(j,i)+1, nzt
-            u_comp        = u(k+1,j,i) + u(k,j,i) - gu
-            flux_l_w(k,j,tn) = u_comp * (                                        &
+!
+!--    Compute leftside fluxes for the respective boundary.
+       IF ( i == i_omp ) THEN
+
+          DO  k = nzb+1, nzb_max
+
+             u_comp         = u(k+1,j,i) + u(k,j,i) - gu
+             flux_l_w(k,j,tn) = u_comp * (                                     &
+                          ( 37.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +     7.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( w(k,j,i)   + w(k,j,i-1) )               &
+                   -      (  8.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( w(k,j,i+1) + w(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( w(k,j,i+2) + w(k,j,i-3) )               &
+                                         )
+
+               diss_l_w(k,j,tn) = - ABS( u_comp ) * (                          &
+                          ( 10.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +     3.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( w(k,j,i)   - w(k,j,i-1) )               &
+                   -      (  5.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( w(k,j,i+1) - w(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( w(k,j,i+2) - w(k,j,i-3) )               &
+                                                    )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp           = u(k+1,j,i) + u(k,j,i) - gu
+             flux_l_w(k,j,tn) = u_comp * (                                    &
                             37.0 * ( w(k,j,i) + w(k,j,i-1)   )                &
                           -  8.0 * ( w(k,j,i+1) + w(k,j,i-2) )                &
                           +        ( w(k,j,i+2) + w(k,j,i-3) ) ) * adv_mom_5
-            diss_l_w(k,j,tn) = - ABS( u_comp ) * (                               &
+             diss_l_w(k,j,tn) = - ABS( u_comp ) * (                           &
                             10.0 * ( w(k,j,i) - w(k,j,i-1)   )                &
                           -  5.0 * ( w(k,j,i+1) - w(k,j,i-2) )                &
                           +        ( w(k,j,i+2) - w(k,j,i-3) ) ) * adv_mom_5 
-          ENDDO
-          
+
+          ENDDO
+
        ENDIF
 
-!       
-!--    Now compute the tendency terms for the horizontal parts.         
-       DO  k = nzb_w_inner(j,i)+1, nzt
+       flux_t(0) = 0.0
+       diss_t(0) = 0.0
+       flux_d    = 0.0
+       diss_d    = 0.0
+!
+!--    Now compute the fluxes and tendency terms for the horizontal
+!--    and vertical parts.
+       DO  k = nzb+1, nzb_max
+
+          u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+          flux_r(k) = u_comp * (                                             &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j,i+1) + w(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j,i+2) + w(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j,i+3) + w(k,j,i-2) )                 &
+                               )
+
+          diss_r(k) = - ABS( u_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j,i+1) - w(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j,i+2) - w(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j,i+3) - w(k,j,i-2) )                 &
+                                        )
+
+          v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+          flux_n(k) = v_comp * (                                             &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j+1,i) + w(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j+2,i) + w(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j+3,i) + w(k,j-2,i) )                 &
+                               )
+
+          diss_n(k) = - ABS( v_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j+1,i) - w(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j+2,i) - w(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j+3,i) - w(k,j-2,i) )                 &
+                                        )
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),35,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),33,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),35,1)
+
+          w_comp    = w(k+1,j,i) + w(k,j,i)
+          flux_t(k) = w_comp  * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)  + w(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  + w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) + w(k_mm,j,i) )                  &
+                                 )
+
+          diss_t(k) = - ABS( w_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)   - w(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  - w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) - w(k_mm,j,i) )                  &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography. 
+          div = ( ( u_comp + gu - ( u(k+1,j,i) + u(k,j,i)   ) ) * ddx         &
+              +   ( v_comp + gv - ( v(k+1,j,i) + v(k,j,i)   ) ) * ddy         &
+              +   ( w_comp      - ( w(k,j,i)   + w(k-1,j,i) ) ) * ddzu(k+1)   &
+                ) * 0.5
+
           tend(k,j,i) = tend(k,j,i) - (                                       &
                       ( flux_r(k) + diss_r(k)                                 &
-                    -   flux_l_w(k,j,tn) - diss_l_w(k,j,tn)   ) * ddx               &
+                    -   flux_l_w(k,j,tn) - diss_l_w(k,j,tn)   ) * ddx         &
                     + ( flux_n(k) + diss_n(k)                                 &
-                    -   flux_s_w(k,tn) - diss_s_w(k,tn)       ) * ddy  )
+                    -   flux_s_w(k,tn) - diss_s_w(k,tn)       ) * ddy         &
+                    + ( flux_t(k) + diss_t(k)                                 &
+                    -   flux_d    - diss_d                    ) * ddzu(k+1)   &
+                                      ) + div * w(k,j,i)
 
           flux_l_w(k,j,tn) = flux_r(k)
           diss_l_w(k,j,tn) = diss_r(k)
-          flux_s_w(k,tn) = flux_n(k)
-          diss_s_w(k,tn) = diss_n(k)
+          flux_s_w(k,tn)   = flux_n(k)
+          diss_s_w(k,tn)   = diss_n(k)
+          flux_d           = flux_t(k)
+          diss_d           = diss_t(k)
+!
+!--        Statistical Evaluation of w'w'.
+          sums_ws2_ws_l(k,tn)  = sums_ws2_ws_l(k,tn)                         &
+                             + ( flux_t(k) + diss_t(k) )                     &
+                             *   weight_substep(intermediate_timestep_count)
+
        ENDDO
 
-!
-!--    Vertical advection, degradation of order near surface and top. 
-!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--    statistical evaluation the top flux at the surface should be 0
-       flux_t(nzb_w_inner(j,i)) = 0.0  !statistical reasons
-       diss_t(nzb_w_inner(j,i)) = 0.0
-!
-!--    2nd order scheme (bottom)       
-       k         = nzb_w_inner(j,i)+1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k+1,j,i) + w(k,j,i)
-       flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
-       diss_t(k) = diss_2nd( w(k+2,j,i), w(k+1,j,i), w(k,j,i), 0.0, 0.0,        &
-                             w_comp, 0.25, ddzu(k+1) )
-                      
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzu(k+1)               
-!
-!--    WS3 as an intermediate step (bottom)
-       k         = nzb_w_inner(j,i)+2
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k+1,j,i) + w(k,j,i)
-       flux_t(k) = w_comp * (                                                 &
-                   7.0 * ( w(k+1,j,i) + w(k,j,i) )                            &
-                 -       ( w(k+2,j,i) + w(k-1,j,i) ) ) * adv_mom_3
-       diss_t(k) = - ABS( w_comp ) * (                                        &
-                   3.0 * ( w(k+1,j,i) - w(k,j,i) )                            & 
-                 -       ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzu(k+1)
-!                                 
-!--    WS5
-       DO  k = nzb_w_inner(j,i)+3, nzt-2 
-          flux_d    = flux_t(k-1)
-          diss_d    = diss_t(k-1)
+       DO  k = nzb_max+1, nzt
+
+          u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+          flux_r(k) = u_comp * (                                            &
+                      37.0 * ( w(k,j,i+1) + w(k,j,i)   )                    &
+                    -  8.0 * ( w(k,j,i+2) + w(k,j,i-1) )                    &
+                    +        ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
+
+          diss_r(k) = - ABS( u_comp ) * (                                   &
+                      10.0 * ( w(k,j,i+1) - w(k,j,i)   )                    &
+                    -  5.0 * ( w(k,j,i+2) - w(k,j,i-1) )                    &
+                    +        ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
+
+          v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+          flux_n(k) = v_comp * (                                            &
+                      37.0 * ( w(k,j+1,i) + w(k,j,i)   )                    &
+                    -  8.0 * ( w(k,j+2,i) + w(k,j-1,i) )                    &
+                    +        ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
+
+          diss_n(k) = - ABS( v_comp ) * (                                   &
+                      10.0 * ( w(k,j+1,i) - w(k,j,i)   )                    &
+                    -  5.0 * ( w(k,j+2,i) - w(k,j-1,i) )                    &
+                    +        ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
+!
+!--       k index has to be modified near bottom and top, else array
+!--       subscripts will be exceeded.
+          k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),35,1)
+          k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),33,1)  )
+          k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),35,1)
+
           w_comp    = w(k+1,j,i) + w(k,j,i)
-          flux_t(k) = w_comp * (                                              &
-                      37.0 * ( w(k+1,j,i) + w(k,j,i)   )                      &
-                    -  8.0 * ( w(k+2,j,i) + w(k-1,j,i) )                      &
-                    +        ( w(k+3,j,i) + w(k-2,j,i) ) ) * adv_mom_5
-          diss_t(k) = - ABS( w_comp ) * (                                     &
-                      10.0 * ( w(k+1,j,i) - w(k,j,i)   )                      & 
-                    -  5.0 * ( w(k+2,j,i) - w(k-1,j,i) )                      & 
-                    +        ( w(k+3,j,i) - w(k-2,j,i) ) ) * adv_mom_5
-
-          tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                 &
-                                    -   flux_d    - diss_d      ) * ddzu(k+1)
+          flux_t(k) = w_comp  * (                                            &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)  + w(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  + w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) + w(k_mm,j,i) )                  &
+                                 )
+
+          diss_t(k) = - ABS( w_comp ) * (                                    &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)   - w(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  - w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) - w(k_mm,j,i) )                  &
+                                         )
+!
+!--       Calculate the divergence of the velocity field. A respective
+!--       correction is needed to overcome numerical instabilities introduced
+!--       by a not sufficient reduction of divergences near topography.
+          div = ( ( u_comp + gu - ( u(k+1,j,i) + u(k,j,i)   ) ) * ddx         &
+              +   ( v_comp + gv - ( v(k+1,j,i) + v(k,j,i)   ) ) * ddy         &
+              +   ( w_comp      - ( w(k,j,i)   + w(k-1,j,i) ) ) * ddzu(k+1)   &
+                ) * 0.5
+
+          tend(k,j,i) = tend(k,j,i) - (                                       &
+                      ( flux_r(k) + diss_r(k)                                 &
+                    -   flux_l_w(k,j,tn) - diss_l_w(k,j,tn)   ) * ddx         &
+                    + ( flux_n(k) + diss_n(k)                                 &
+                    -   flux_s_w(k,tn) - diss_s_w(k,tn)       ) * ddy         &
+                    + ( flux_t(k) + diss_t(k)                                 &
+                    -   flux_d    - diss_d                    ) * ddzu(k+1)   &
+                                      ) + div * w(k,j,i)
+
+          flux_l_w(k,j,tn) = flux_r(k)
+          diss_l_w(k,j,tn) = diss_r(k)
+          flux_s_w(k,tn)   = flux_n(k)
+          diss_s_w(k,tn)   = diss_n(k)
+          flux_d           = flux_t(k)
+          diss_d           = diss_t(k)
+!
+!--        Statistical Evaluation of w'w'.
+          sums_ws2_ws_l(k,tn)  = sums_ws2_ws_l(k,tn)                         &
+                             + ( flux_t(k) + diss_t(k) )                     &
+                             *   weight_substep(intermediate_timestep_count)
+
        ENDDO
-!--    WS3 as an intermediate step (top)
-       k         = nzt - 1
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k+1,j,i) + w(k,j,i)
-       flux_t(k) = w_comp * (                                                 &
-                   7.0 * ( w(k+1,j,i) + w(k,j,i)   )                          &
-                 -       ( w(k+2,j,i) + w(k-1,j,i) ) ) *adv_mom_3
-       diss_t(k) = - ABS( w_comp ) * (                                        &
-                   3.0 * ( w(k+1,j,i) - w(k,j,i)     )                        & 
-                 -       ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
-                   
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzu(k+1)
-!
-!--    2nd order scheme (top)
-       k         = nzt
-       flux_d    = flux_t(k-1)
-       diss_d    = diss_t(k-1)
-       w_comp    = w(k+1,j,i) + w(k,j,i)
-       flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
-       diss_t(k) = diss_2nd( w(k+1,j,i), w(k+1,j,i), w(k,j,i), w(k-1,j,i),    &
-                             w(k-2,j,i), w_comp, 0.25, ddzu(k+1) )
-
-       tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                    &
-                                 -   flux_d    - diss_d      ) * ddzu(k+1)
-       
-       DO  k = nzb_w_inner(j,i), nzt
-           sums_ws2_ws_l(k,tn)  = sums_ws2_ws_l(k,tn)                               &
-                 + ( flux_t(k) + diss_t(k) )                                  &
-                 *   weight_substep(intermediate_timestep_count)
-       ENDDO
+
 
     END SUBROUTINE advec_w_ws_ij
@@ -1703,10 +1817,10 @@
        IMPLICIT NONE
 
-       INTEGER ::  i, j, k, tn = 0
+       INTEGER ::  i, j, k, tn = 0, k_ppp, k_pp, k_mm
        REAL, DIMENSION(:,:,:), POINTER ::  sk
-       REAL    :: flux_d, diss_d, u_comp, v_comp
-       REAL, DIMENSION(nzb:nzt+1)  ::  flux_r, diss_r, flux_n, diss_n
-       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local, swap_diss_y_local,    &
-                                     flux_t, diss_t
+       REAL    :: flux_d, diss_d, u_comp, v_comp, div
+       REAL, DIMENSION(nzb:nzt)  ::  flux_r, diss_r, flux_n, diss_n, flux_t,  &
+                                     diss_t
+       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local, swap_diss_y_local
        REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local,               &
                                              swap_diss_x_local
@@ -1717,360 +1831,352 @@
        i = nxl
        DO  j = nys, nyn
-          IF ( boundary_flags(j,i) == 6 )  THEN
-          
-             DO  k = nzb_s_inner(j,i)+1, nzt
-                u_comp                 = u(k,j,i) - u_gtrans
-                swap_flux_x_local(k,j) = u_comp * (                           &
-                                         sk(k,j,i) + sk(k,j,i-1)) * 0.5
-                swap_diss_x_local(k,j) = diss_2nd( sk(k,j,i+2), sk(k,j,i+1),  &
-                                                   sk(k,j,i), sk(k,j,i-1),    &
-                                                   sk(k,j,i-1), u_comp,       &
-                                                   0.5, ddx ) 
-             ENDDO
-             
-          ELSE
-          
-             DO  k = nzb_s_inner(j,i)+1, nzt
-                u_comp                 = u(k,j,i) - u_gtrans
-                swap_flux_x_local(k,j) = u_comp*(                              &
-                                         37.0 * ( sk(k,j,i)+sk(k,j,i-1)     )  &
-                                       -  8.0 * ( sk(k,j,i+1) + sk(k,j,i-2) )  &
-                                       +        ( sk(k,j,i+2) + sk(k,j,i-3) ) )&
-                                       * adv_sca_5
-                swap_diss_x_local(k,j) = - ABS( u_comp ) * (                   &
-                                         10.0 * (sk(k,j,i) - sk(k,j,i-1)    )  &
-                                       -  5.0 * ( sk(k,j,i+1) - sk(k,j,i-2) )  &
-                                       +        ( sk(k,j,i+2) - sk(k,j,i-3) ) )&
-                                       * adv_sca_5 
-             ENDDO
-          ENDIF
+
+          DO  k = nzb+1, nzb_max
+
+             u_comp                 = u(k,j,i) - u_gtrans
+             swap_flux_x_local(k,j) = u_comp * (                              &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)   + sk(k,j,i-1)    )         &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j,i+1) + sk(k,j,i-2)    )         &
+                  +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                         ) *         ( sk(k,j,i+2) + sk(k,j,i-3)    )         &
+                                                  )
+
+              swap_diss_x_local(k,j) = -ABS( u_comp ) * (                     &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)   - sk(k,j,i-1)    )         &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j,i+1) - sk(k,j,i-2)  )           &
+                  +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5  &
+                         ) *                                                  &
+                                     ( sk(k,j,i+2) - sk(k,j,i-3) )            &
+                                                           )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp                 = u(k,j,i) - u_gtrans
+             swap_flux_x_local(k,j) = u_comp * (                             &
+                                      37.0 * ( sk(k,j,i)   + sk(k,j,i-1) )   &
+                                    -  8.0 * ( sk(k,j,i+1) + sk(k,j,i-2) )   &
+                                    +        ( sk(k,j,i+2) + sk(k,j,i-3) )   &
+                                                  ) * adv_sca_5
+
+             swap_diss_x_local(k,j) = -ABS( u_comp ) * (                     &
+                                      10.0 * ( sk(k,j,i)   - sk(k,j,i-1) )   &
+                                    -  5.0 * ( sk(k,j,i+1) - sk(k,j,i-2) )   &
+                                    +        ( sk(k,j,i+2) - sk(k,j,i-3) )   &
+                                                          ) * adv_sca_5
+
+          ENDDO
+
        ENDDO
-!
-!--    The following loop computes the horizontal fluxes for the interior of the
-!--    processor domain plus south boundary points. Furthermore tendency terms
-!--    are computed.
+
        DO  i = nxl, nxr
+
           j = nys
-          IF ( boundary_flags(j,i) == 8 )  THEN
-          
-             DO  k = nzb_s_inner(j,i)+1, nzt
-                v_comp               = v(k,j,i) - v_gtrans
-                swap_flux_y_local(k) = v_comp *                                &
-                                       ( sk(k,j,i) + sk(k,j-1,i) ) * 0.5 
-                swap_diss_y_local(k) = diss_2nd( sk(k,j+2,i), sk(k,j+1,i),     &
-                                                 sk(k,j,i), sk(k,j-1,i),       &
-                                                 sk(k,j-1,i), v_comp, 0.5, ddy )
-             ENDDO
-             
-          ELSE
-          
-             DO  k = nzb_s_inner(j,i)+1, nzt
-                v_comp               = v(k,j,i) - v_gtrans
-                swap_flux_y_local(k) = v_comp * (                              &
-                                       37.0 * ( sk(k,j,i) + sk(k,j-1,i)   )    &
-                                     -  8.0 * ( sk(k,j+1,i) + sk(k,j-2,i) )    &
-                                     +        ( sk(k,j+2,i) + sk(k,j-3,i) ) )  &  
-                                     * adv_sca_5
-                swap_diss_y_local(k)= - ABS( v_comp ) * (                      &
-                                      10.0 * ( sk(k,j,i) - sk(k,j-1,i)   )     &
-                                    -  5.0 * ( sk(k,j+1,i) - sk(k,j-2,i) )     &
-                                    +        ( sk(k,j+2,i)-sk(k,j-3,i) ) )     &
-                                    * adv_sca_5
-             ENDDO
-             
-          ENDIF
+          DO  k = nzb+1, nzb_max
+
+             v_comp                  = v(k,j,i) - v_gtrans
+             swap_flux_y_local(k) = v_comp * (                                &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)  + sk(k,j-1,i)     )         &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j+1,i) + sk(k,j-2,i)    )         &
+                  +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                         ) *                                                  &
+                                     ( sk(k,j+2,i) + sk(k,j-3,i)    )         &
+                                             )
+
+             swap_diss_y_local(k) = -ABS( v_comp ) * (                        &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                      +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1  &
+                         ) *                                                  &
+                                     ( sk(k,j,i)   - sk(k,j-1,i)    )         &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                      +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3  &
+                         ) *                                                  &
+                                     ( sk(k,j+1,i) - sk(k,j-2,i)  )           &
+                  +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5  &
+                         ) *                                                  &
+                                     ( sk(k,j+2,i) - sk(k,j-3,i) )            &
+                                                     )
+
+          ENDDO
+!
+!--       Above to the top of the highest topography. No degradation necessary.
+          DO  k = nzb_max+1, nzt
+
+             v_comp               = v(k,j,i) - v_gtrans
+             swap_flux_y_local(k) = v_comp * (                               &
+                                    37.0 * ( sk(k,j,i)   + sk(k,j-1,i) )     &
+                                  -  8.0 * ( sk(k,j+1,i) + sk(k,j-2,i) )     &
+                                  +        ( sk(k,j+2,i) + sk(k,j-3,i) )     &
+                                             ) * adv_sca_5
+              swap_diss_y_local(k) = -ABS( v_comp ) * (                      &
+                                    10.0 * ( sk(k,j,i)   - sk(k,j-1,i) )     &
+                                  -  5.0 * ( sk(k,j+1,i) - sk(k,j-2,i) )     &
+                                  +          sk(k,j+2,i) - sk(k,j-3,i)       &
+                                                      ) * adv_sca_5
+
+          ENDDO
 
           DO  j = nys, nyn
-            IF ( boundary_flags(j,i) /= 0 )  THEN 
-!
-!--            Degrade the order for Dirichlet bc. at the outflow boundary
-               SELECT CASE ( boundary_flags(j,i) )
-               
-                  CASE ( 1 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        u_comp    = u(k,j,i+1) - u_gtrans
-                        flux_r(k) = u_comp * (                                 &
-                                    7.0 * ( sk(k,j,i+1) + sk(k,j,i)   )        &
-                                  -       ( sk(k,j,i+2) + sk(k,j,i-1) ) )      &
-                                  * adv_sca_3
-                        diss_r(k) = - ABS( u_comp ) * (                        &
-                                    3.0 * ( sk(k,j,i+1) - sk(k,j,i)   )        & 
-                                  -       ( sk(k,j,i+2) - sk(k,j,i-1) ) )      &
-                                  * adv_sca_3
-                     ENDDO
-                     
-                  CASE ( 2 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        u_comp    = u(k,j,i+1) - u_gtrans
-                        flux_r(k) = u_comp * ( sk(k,j,i+1) + sk(k,j,i) ) * 0.5
-                        diss_r(k) = diss_2nd( sk(k,j,i+1), sk(k,j,i+1),        &
-                                              sk(k,j,i), sk(k,j,i-1),          &
-                                              sk(k,j,i-2), u_comp, 0.5, ddx )
-                     ENDDO
-                     
-                  CASE ( 3 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        v_comp    = v(k,j+1,i) - v_gtrans
-                        flux_n(k) = v_comp * (                                 &
-                                    7.0 * ( sk(k,j+1,i) + sk(k,j,i)   )        &
-                                  -       ( sk(k,j+2,i) + sk(k,j-1,i) ) )      &
-                                  * adv_sca_3
-                        diss_n(k) = - ABS( v_comp ) * (                        &
-                                    3.0 * ( sk(k,j+1,i) - sk(k,j,i)   )        & 
-                                  -       ( sk(k,j+2,i) - sk(k,j-1,i) ) )      &
-                                  * adv_sca_3
-                     ENDDO
-                     
-                  CASE ( 4 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        v_comp    = v(k,j+1,i) - v_gtrans
-                        flux_n(k) = v_comp * ( sk(k,j+1,i) + sk(k,j,i) ) * 0.5
-                        diss_n(k) = diss_2nd( sk(k,j+1,i), sk(k,j+1,i),        &
-                                              sk(k,j,i), sk(k,j-1,i),          &
-                                              sk(k,j-2,i), v_comp, 0.5, ddy )     
-                     ENDDO
-                     
-                  CASE ( 5 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        u_comp    = u(k,j,i+1) - u_gtrans 
-                        flux_r(k) = u_comp * (                                 &
-                                    7.0 * ( sk(k,j,i+1) + sk(k,j,i)   )        &
-                                  -       ( sk(k,j,i+2) + sk(k,j,i-1) ) )      &
-                                  * adv_sca_3
-                        diss_r(k) = - ABS( u_comp ) * (                        &
-                                    3.0 * ( sk(k,j,i+1) - sk(k,j,i)   )        & 
-                                  -       ( sk(k,j,i+2) - sk(k,j,i-1) ) )      &
-                                  * adv_sca_3
-                     ENDDO  
-                     
-                  CASE ( 6 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        u_comp    = u(k,j,i+1) - u_gtrans
-                        flux_r(k) = u_comp * (                                 &
-                                    7.0 * ( sk(k,j,i+1) + sk(k,j,i)   )        &
-                                  -       ( sk(k,j,i+2) + sk(k,j,i-1) ) )      &
-                                  * adv_sca_3
-                        diss_r(k) = - ABS( u_comp ) * (                        &
-                                    3.0 * ( sk(k,j,i+1) - sk(k,j,i)   )        & 
-                                  -       ( sk(k,j,i+2) - sk(k,j,i-1) ) )      &
-                                  * adv_sca_3
-                     ENDDO
-                     
-                  CASE ( 7 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        v_comp    = v(k,j+1,i) - v_gtrans
-                        flux_n(k) = v_comp * (                                 &
-                                    7.0 * ( sk(k,j+1,i) + sk(k,j,i)   )        &
-                                  -       ( sk(k,j+2,i) + sk(k,j-1,i) ) )      &
-                                  * adv_sca_3
-                        diss_n(k) = - ABS( v_comp ) * (                        &
-                                    3.0 * ( sk(k,j+1,i) - sk(k,j,i)   )        & 
-                                  -       ( sk(k,j+2,i) - sk(k,j-1,i) ) )      &
-                                  * adv_sca_3
-                     ENDDO
-                     
-                  CASE ( 8 )
-                     DO  k = nzb_s_inner(j,i)+1, nzt
-                        v_comp    = v(k,j+1,i) - v_gtrans
-                        flux_n(k) = v_comp * (                                 &
-                                    7.0 * ( sk(k,j+1,i) + sk(k,j,i)   )        &
-                                  -       ( sk(k,j+2,i) + sk(k,j-1,i) ) )      &
-                                  * adv_sca_3
-                        diss_n(k) = -  ABS( v_comp ) * (                       &
-                                    3.0 * ( sk(k,j+1,i) - sk(k,j,i)   )        & 
-                                  -       ( sk(k,j+2,i) - sk(k,j-1,i) ) )      &
-                                  * adv_sca_3
-                     ENDDO  
-                     
-                  CASE DEFAULT
-                  
-               END SELECT
-               
-               IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2 .OR.&
-                   boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )    &
-               THEN
-              
-                  DO  k = nzb_s_inner(j,i)+1, nzt
-                     v_comp    = v(k,j+1,i) - v_gtrans
-                     flux_n(k) = v_comp * (                                    &
-                                 37.0 * ( sk(k,j+1,i) + sk(k,j,i)   )          &
-                               -  8.0 * ( sk(k,j+2,i) + sk(k,j-1,i) )          &
-                               +        ( sk(k,j+3,i) + sk(k,j-2,i) ) )        &
-                               * adv_sca_5
-                     diss_n(k) = - ABS( v_comp ) * (                           &
-                                 10.0 * ( sk(k,j+1,i) - sk(k,j,i)   )          &
-                               -  5.0 * ( sk(k,j+2,i) - sk(k,j-1,i) )          &
-                               +        ( sk(k,j+3,i) - sk(k,j-2,i) ) )        &
-                               * adv_sca_5
-                  ENDDO
-                  
-               ELSE
-               
-                  DO  k = nzb_s_inner(j,i)+1, nzt
-                     u_comp    = u(k,j,i+1) - u_gtrans  
-                     flux_r(k) = u_comp * (                                    & 
-                                 37.0 * ( sk(k,j,i+1) + sk(k,j,i)   )          &
-                               -  8.0 * ( sk(k,j,i+2) + sk(k,j,i-1) )          &
-                               +        ( sk(k,j,i+3) + sk(k,j,i-2) ) )        &
-                               * adv_sca_5
-                     diss_r(k) = - ABS( u_comp ) * (                           &
-                                 10.0 * ( sk(k,j,i+1) - sk(k,j,i)   )          &
-                               -  5.0 * ( sk(k,j,i+2) - sk(k,j,i-1) )          &
-                               +        ( sk(k,j,i+3) - sk(k,j,i-2) ) )        &
-                               * adv_sca_5
-                  ENDDO
-                  
-               ENDIF      
-            
-            ELSE
-            
-               DO  k = nzb_s_inner(j,i)+1, nzt
-                  u_comp    = u(k,j,i+1) - u_gtrans
-                  flux_r(k) = u_comp * (                                       &
-                              37.0 * ( sk(k,j,i+1) + sk(k,j,i)   )             &
-                            -  8.0 * ( sk(k,j,i+2) + sk(k,j,i-1) )             &
-                            +        ( sk(k,j,i+3) + sk(k,j,i-2) ) )           &
-                            * adv_sca_5
-                  diss_r(k) = - ABS( u_comp ) * (                              &
-                              10.0 * ( sk(k,j,i+1) - sk(k,j,i)   )             &
-                            -  5.0 * ( sk(k,j,i+2) - sk(k,j,i-1) )             &
-                            +      ( sk(k,j,i+3) - sk(k,j,i-2) ) )             &
-                            * adv_sca_5
-                  
-                  v_comp    = v(k,j+1,i) - v_gtrans
-                  flux_n(k) = v_comp * (                                       &
-                              37.0 * ( sk(k,j+1,i) + sk(k,j,i)   )             &
-                            -  8.0 * ( sk(k,j+2,i) + sk(k,j-1,i) )             &
-                            +        ( sk(k,j+3,i) + sk(k,j-2,i) ) )           &
-                            * adv_sca_5
-                  diss_n(k) = - ABS( v_comp ) * (                              &
-                              10.0 * ( sk(k,j+1,i) - sk(k,j,i)   )             &
-                            -  5.0 * ( sk(k,j+2,i) - sk(k,j-1,i) )             &
-                            +        ( sk(k,j+3,i) - sk(k,j-2,i) ) )           &
-                            * adv_sca_5                  
-               ENDDO
-               
-            ENDIF
-            
-            DO  k = nzb_s_inner(j,i)+1, nzt
-               tend(k,j,i) = tend(k,j,i) - (                                  &
-                  ( flux_r(k) + diss_r(k)                                     &
-                -   swap_flux_x_local(k,j) - swap_diss_x_local(k,j)   ) * ddx &
-                + ( flux_n(k) + diss_n(k)                                     &
-                -   swap_flux_y_local(k) - swap_diss_y_local(k)       ) * ddy)
-                   
+
+             flux_t(0) = 0.0
+             diss_t(0) = 0.0
+             flux_d    = 0.0
+             diss_d    = 0.0
+
+             DO  k = nzb+1, nzb_max
+
+                u_comp    = u(k,j,i+1) - u_gtrans
+                flux_r(k) = u_comp * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j,i+1) + sk(k,j,i)   )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j,i+2) + sk(k,j,i-1) )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                     ) *     ( sk(k,j,i+3) + sk(k,j,i-2) )                  &
+                                     )
+
+                diss_r(k) = -ABS( u_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),0,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j,i+1) - sk(k,j,i)  )                   &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),1,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j,i+2) - sk(k,j,i-1) )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),2,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k,j,i+3) - sk(k,j,i-2) )                  &
+                                             )
+
+                v_comp    = v(k,j+1,i) - v_gtrans
+                flux_n(k) = v_comp * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j+1,i) + sk(k,j,i)   )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j+2,i) + sk(k,j-1,i) )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k,j+3,i) + sk(k,j-2,i) )                  &
+                                     )
+
+                diss_n(k) = -ABS( v_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),3,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k,j+1,i) - sk(k,j,i)    )                 &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),4,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k,j+2,i) - sk(k,j-1,i)  )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),5,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k,j+3,i) - sk(k,j-2,i) )                  &
+                                             )
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),8,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),6,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),8,1)
+
+
+                flux_t(k) = w(k,j,i) * (                                    &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)  + sk(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i) + sk(k-1,j,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *     ( sk(k_ppp,j,i)+ sk(k_mm,j,i) )                &
+                                       )
+
+                diss_t(k) = -ABS( w(k,j,i) ) * (                            &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)   - sk(k,j,i)    )               &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i)  - sk(k-1,j,i)  )               &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k_ppp,j,i) - sk(k_mm,j,i) )               &
+                                               )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div         =   ( u(k,j,i+1) - u(k,j,i)   ) * ddx             &
+                              + ( v(k,j+1,i) - v(k,j,i)   ) * ddy             &
+                              + ( w(k,j,i)   - w(k-1,j,i) ) * ddzw(k)
+
+                tend(k,j,i) = tend(k,j,i) - (                                 &
+                        ( flux_r(k) + diss_r(k) - swap_flux_x_local(k,j) -    &
+                          swap_diss_x_local(k,j)            ) * ddx           &
+                      + ( flux_n(k) + diss_n(k) - swap_flux_y_local(k)   -    &
+                          swap_diss_y_local(k)              ) * ddy           &
+                      + ( flux_t(k) + diss_t(k) - flux_d - diss_d             &
+                                                               ) * ddzw(k)    &
+                                            ) + sk(k,j,i) * div
+
+                swap_flux_y_local(k)   = flux_n(k)
+                swap_diss_y_local(k)   = diss_n(k)
                 swap_flux_x_local(k,j) = flux_r(k)
                 swap_diss_x_local(k,j) = diss_r(k)
+                flux_d                 = flux_t(k)
+                diss_d                 = diss_t(k)
+
+             ENDDO
+
+             DO  k = nzb_max+1, nzt
+
+                u_comp    = u(k,j,i+1) - u_gtrans
+                flux_r(k) = u_comp * (                                      &
+                      37.0 * ( sk(k,j,i+1) + sk(k,j,i)   )                  &
+                    -  8.0 * ( sk(k,j,i+2) + sk(k,j,i-1) )                  &
+                    +        ( sk(k,j,i+3) + sk(k,j,i-2) ) ) * adv_sca_5
+                diss_r(k) = -ABS( u_comp ) * (                              &
+                      10.0 * ( sk(k,j,i+1) - sk(k,j,i)   )                  &
+                    -  5.0 * ( sk(k,j,i+2) - sk(k,j,i-1) )                  &
+                    +        ( sk(k,j,i+3) - sk(k,j,i-2) ) ) * adv_sca_5
+
+                v_comp    = v(k,j+1,i) - v_gtrans
+                flux_n(k) = v_comp * (                                      &
+                      37.0 * ( sk(k,j+1,i) + sk(k,j,i)   )                  &
+                    -  8.0 * ( sk(k,j+2,i) + sk(k,j-1,i) )                  &
+                    +        ( sk(k,j+3,i) + sk(k,j-2,i) ) ) * adv_sca_5
+                diss_n(k) = -ABS( v_comp ) * (                              &
+                      10.0 * ( sk(k,j+1,i) - sk(k,j,i)   )                  &
+                    -  5.0 * ( sk(k,j+2,i) - sk(k,j-1,i) )                  &
+                    +        ( sk(k,j+3,i) - sk(k,j-2,i) ) ) * adv_sca_5
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),8,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),6,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),8,1)
+
+                flux_t(k) = w(k,j,i) * (                                    &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)  + sk(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i) + sk(k-1,j,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *     ( sk(k_ppp,j,i)+ sk(k_mm,j,i) )                &
+                                       )
+
+                diss_t(k) = -ABS( w(k,j,i) ) * (                            &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                  +           IBITS(wall_flags_0(k,j,i),6,1) * adv_sca_1    &
+                     ) *                                                    &
+                             ( sk(k+1,j,i)   - sk(k,j,i)    )               &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                  +           IBITS(wall_flags_0(k,j,i),7,1) * adv_sca_3    &
+                     ) *                                                    &
+                             ( sk(k_pp,j,i)  - sk(k-1,j,i)  )               &
+              +      (        IBITS(wall_flags_0(k,j,i),8,1) * adv_sca_5    &
+                     ) *                                                    &
+                             ( sk(k_ppp,j,i) - sk(k_mm,j,i) )               &
+                                               )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities introduced
+!--             by a not sufficient reduction of divergences near topography.
+                div         =   ( u(k,j,i+1) - u(k,j,i)   ) * ddx             &
+                              + ( v(k,j+1,i) - v(k,j,i)   ) * ddy             &
+                              + ( w(k,j,i)   - w(k-1,j,i) ) * ddzw(k)
+
+                tend(k,j,i) = tend(k,j,i) - (                                 &
+                        ( flux_r(k) + diss_r(k) - swap_flux_x_local(k,j) -    &
+                          swap_diss_x_local(k,j)            ) * ddx           &
+                      + ( flux_n(k) + diss_n(k) - swap_flux_y_local(k)   -    &
+                          swap_diss_y_local(k)              ) * ddy           &
+                      + ( flux_t(k) + diss_t(k) - flux_d - diss_d             &
+                                                               ) * ddzw(k)    &
+                                            ) + sk(k,j,i) * div
+
                 swap_flux_y_local(k)   = flux_n(k)
                 swap_diss_y_local(k)   = diss_n(k)
-            ENDDO
+                swap_flux_x_local(k,j) = flux_r(k)
+                swap_diss_x_local(k,j) = diss_r(k)
+                flux_d                 = flux_t(k)
+                diss_d                 = diss_t(k)
+
+             ENDDO
+!
+!--          evaluation of statistics
+             SELECT CASE ( sk_char )
+
+                 CASE ( 'pt' )
+                    DO  k = nzb, nzt
+                       sums_wspts_ws_l(k,tn) = sums_wspts_ws_l(k,tn)         &
+                        + ( flux_t(k) + diss_t(k) )                          &
+                        *   weight_substep(intermediate_timestep_count)
+                    ENDDO
+                 CASE ( 'sa' )
+                    DO  k = nzb, nzt
+                       sums_wssas_ws_l(k,tn) = sums_wssas_ws_l(k,tn)         &
+                        + ( flux_t(k) + diss_t(k) )                          &
+                        *   weight_substep(intermediate_timestep_count)
+                    ENDDO
+                 CASE ( 'q' )
+                    DO  k = nzb, nzt
+                       sums_wsqs_ws_l(k,tn) = sums_wsqs_ws_l(k,tn)           &
+                        + ( flux_t(k) + diss_t(k) )                          &
+                        *   weight_substep(intermediate_timestep_count)
+                    ENDDO
+
+              END SELECT
+
          ENDDO
       ENDDO
-
-!
-!--   Vertical advection, degradation of order near surface and top. 
-!--   The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--   statistical evaluation the top flux at the surface should be 0
-      DO  i = nxl, nxr
-         DO  j = nys, nyn
-!
-!--         2nd order scheme (bottom)
-            k=nzb_s_inner(j,i)+1
-!            
-!--         The fluxes flux_d and diss_d at the surface are 0. Due to static
-!--         reasons the top flux at the surface should be 0.
-            flux_t(nzb_s_inner(j,i)) = 0.0
-            diss_t(nzb_s_inner(j,i)) = 0.0
-            flux_d = flux_t(k-1)
-            diss_d = diss_t(k-1)
-            flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) * 0.5
-            diss_t(k) = diss_2nd( sk(k+2,j,i), sk(k+1,j,i), sk(k,j,i),        &
-                                  sk(k,j,i), sk(k,j,i), w(k,j,i),             &
-                                  0.5, ddzw(k) )
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d       ) * ddzw(k)    
-!
-!--         WS3 as an intermediate step (bottom)
-            k         = nzb_s_inner(j,i)+2
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            flux_t(k) = w(k,j,i) * (                                          &
-                        7.0 * ( sk(k+1,j,i) + sk(k,j,i) )                     &
-                       - ( sk(k+2,j,i) + sk(k-1,j,i) ) ) * adv_sca_3 
-            diss_t(k) = - ABS( w(k,j,i) ) * (                                 &
-                        3.0 * ( sk(k+1,j,i) - sk(k,j,i)   )                   & 
-                      -       ( sk(k+2,j,i) - sk(k-1,j,i) ) ) * adv_sca_3
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d       ) * ddzw(k)
-!
-!--         WS5
-            DO  k = nzb_s_inner(j,i)+3, nzt-2
-               flux_d    = flux_t(k-1)
-               diss_d    = diss_t(k-1)
-               flux_t(k) = w(k,j,i) * (                                       &
-                           37.0 * ( sk(k+1,j,i) + sk(k,j,i)   )               &
-                         -  8.0 * ( sk(k+2,j,i) + sk(k-1,j,i) )               &
-                         +        ( sk(k+3,j,i) + sk(k-2,j,i) ) ) * adv_sca_5
-               diss_t(k) = - ABS(w(k,j,i)) * (                                &
-                           10.0 * ( sk(k+1,j,i) -sk(k,j,i)    )               &
-                         -  5.0 * ( sk(k+2,j,i) - sk(k-1,j,i) )               &
-                         +        ( sk(k+3,j,i) - sk(k-2,j,i) ) ) * adv_sca_5
-
-               tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)            &
-                                         -   flux_d    - diss_d     ) * ddzw(k)
-            ENDDO
-!
-!--         WS3 as an intermediate step (top)
-            k         = nzt - 1
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            flux_t(k) = w(k,j,i) * (                                          &
-                        7.0 * ( sk(k+1,j,i) + sk(k,j,i)   )                   &
-                      -       ( sk(k+2,j,i) + sk(k-1,j,i) ) ) * adv_sca_3
-            diss_t(k) = - ABS(w(k,j,i)) * (                                   &
-                        3.0 * ( sk(k+1,j,i) - sk(k,j,i)   )                   & 
-                      -       ( sk(k+2,j,i) - sk(k-1,j,i) ) ) * adv_sca_3
-                        
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d       ) * ddzw(k)
-!
-!--         2nd order scheme (top)
-            k         = nzt
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            flux_t(k) = w(k,j,i) * ( sk(k+1,j,i) + sk(k,j,i) ) * 0.5
-            diss_t(k) = diss_2nd( sk(k+1,j,i), sk(k+1,j,i), sk(k,j,i),        &
-                                  sk(k-1,j,i), sk(k-2,j,i), w(k,j,i),         &
-                                  0.5, ddzw(k) )
-
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d       ) * ddzw(k)
-!
-!--         evaluation of statistics
-            SELECT CASE ( sk_char )
-
-               CASE ( 'pt' )
-                 DO  k = nzb_s_inner(j,i), nzt
-                   sums_wspts_ws_l(k,tn) = sums_wspts_ws_l(k,tn)                    &
-                      + ( flux_t(k) + diss_t(k) )                             &
-                      *   weight_substep(intermediate_timestep_count)
-                 ENDDO
-               CASE ( 'sa' )
-                 DO  k = nzb_s_inner(j,i), nzt
-                   sums_wssas_ws_l(k,tn) = sums_wssas_ws_l(k,tn)                    &
-                      + ( flux_t(k) + diss_t(k) )                             &
-                      *   weight_substep(intermediate_timestep_count)
-                 ENDDO
-               CASE ( 'q' )
-                 DO  k = nzb_s_inner(j,i), nzt
-                   sums_wsqs_ws_l(k,tn) = sums_wsqs_ws_l(k,tn)                      &
-                      + ( flux_t(k) + diss_t(k) )                             &
-                      *   weight_substep(intermediate_timestep_count)
-                 ENDDO
-
-            END SELECT
-         ENDDO
-      ENDDO
-
 
     END SUBROUTINE advec_s_ws
@@ -2091,10 +2197,10 @@
        IMPLICIT NONE
 
-       INTEGER ::  i, j, k, tn = 0
-       REAL    ::  gu, gv, flux_d, diss_d, v_comp, w_comp
+       INTEGER ::  i, j, k, tn = 0,  k_ppp, k_pp, k_mm
+       REAL    ::  gu, gv, flux_d, diss_d, v_comp, w_comp, div
        REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local_u, swap_diss_y_local_u
-       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_u,             &
+       REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_u,           &
                                              swap_diss_x_local_u
-       REAL, DIMENSION(nzb:nzt+1) :: flux_t, diss_t, flux_r, diss_r, flux_n,  &
+       REAL, DIMENSION(nzb:nzt) :: flux_t, diss_t, flux_r, diss_r, flux_n,  &
                                      diss_n, u_comp
  
@@ -2106,351 +2212,363 @@
        i = nxlu
        DO  j = nys, nyn
-          IF( boundary_flags(j,i) == 5 )  THEN
-          
-             DO  k = nzb_u_inner(j,i)+1, nzt
-                u_comp(k)                = u(k,j,i) + u(k,j,i-1) - gu
-                swap_flux_x_local_u(k,j) = u_comp(k) *                         &
-                                           ( u(k,j,i) + u(k,j,i-1) ) * 0.25
-                swap_diss_x_local_u(k,j) = diss_2nd( u(k,j,i+2), u(k,j,i+1),   &
-                                                     u(k,j,i), u(k,j,i-1),     &
-                                                     u(k,j,i-1), u_comp(k),    &
-                                                     0.25, ddx )
-             ENDDO
-             
-          ELSE
-          
-             DO  k = nzb_u_inner(j,i)+1, nzt
-                u_comp(k)                = u(k,j,i) + u(k,j,i-1) - gu
-                swap_flux_x_local_u(k,j) = u_comp(k) * (                       &
-                                           37.0 * ( u(k,j,i) + u(k,j,i-1)   )  &
-                                         -  8.0 * ( u(k,j,i+1) + u(k,j,i-2) )  &
-                                         +        (u(k,j,i+2)+u(k,j,i-3) )  )  &
-                                         * adv_mom_5
-                swap_diss_x_local_u(k,j) = - ABS(u_comp(k)) * (                &
-                                           10.0 * ( u(k,j,i) - u(k,j,i-1)   )  &
-                                         -  5.0 * ( u(k,j,i+1) - u(k,j,i-2) )  &
-                                         +        ( u(k,j,i+2) - u(k,j,i-3) ) )&
-                                         * adv_mom_5
-             ENDDO
-             
-          ENDIF
-          
+          DO  k = nzb+1, nzb_max
+
+             u_comp(k)                = u(k,j,i) + u(k,j,i-1) - gu
+             swap_flux_x_local_u(k,j) = u_comp(k) * (                          &
+                          ( 37.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +     7.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1 &
+                          ) *                                                  &
+                                     ( u(k,j,i)   + u(k,j,i-1) )               &
+                   -      (  8.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( u(k,j,i+1) + u(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( u(k,j,i+2) + u(k,j,i-3) )               &
+                                                   )
+
+              swap_diss_x_local_u(k,j) = - ABS( u_comp(k) ) * (                &
+                          ( 10.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +     3.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1 &
+                          ) *                                                  &
+                                     ( u(k,j,i)   - u(k,j,i-1) )               &
+                   -      (  5.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( u(k,j,i+1) - u(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( u(k,j,i+2) - u(k,j,i-3) )               &
+                                                             )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp(k)         = u(k,j,i) + u(k,j,i-1) - gu
+             swap_flux_x_local_u(k,j) = u_comp(k) * (                          &
+                             37.0 * ( u(k,j,i) + u(k,j,i-1)   )                &
+                           -  8.0 * ( u(k,j,i+1) + u(k,j,i-2) )                &
+                           +        ( u(k,j,i+2) + u(k,j,i-3) ) ) * adv_mom_5
+             swap_diss_x_local_u(k,j) = - ABS(u_comp(k)) * (                   &
+                             10.0 * ( u(k,j,i) - u(k,j,i-1)   )                &
+                           -  5.0 * ( u(k,j,i+1) - u(k,j,i-2) )                &
+                           +        ( u(k,j,i+2) - u(k,j,i-3) ) ) * adv_mom_5
+
+          ENDDO
        ENDDO
 
        DO i = nxlu, nxr
 !       
-!--      The following loop computes the fluxes for the south boundary points
-         j = nys
-         IF ( boundary_flags(j,i) == 8 )  THEN
-!         
-!--         Compute southside fluxes for the south boundary of PE domain 
-            DO  k = nzb_u_inner(j,i)+1, nzt
-               v_comp                 = v(k,j,i) + v(k,j,i-1) - gv
-               swap_flux_y_local_u(k) = v_comp *                              &
-                                        ( u(k,j,i) + u(k,j-1,i) ) * 0.25 
-               swap_diss_y_local_u(k) = diss_2nd( u(k,j+2,i), u(k,j+1,i),     &
-                                                  u(k,j,i), u(k,j-1,i),       &
-                                                  u(k,j-1,i), v_comp,         &
-                                                  0.25, ddy )
-            ENDDO
-            
-         ELSE
-         
-            DO  k = nzb_u_inner(j,i)+1, nzt
-               v_comp                 = v(k,j,i) + v(k,j,i-1) - gv
-               swap_flux_y_local_u(k) = v_comp * (                             &
-                                        37.0 * ( u(k,j,i) + u(k,j-1,i)   )     &
-                                      -  8.0 * ( u(k,j+1,i) + u(k,j-2,i) )     &
-                                      +        ( u(k,j+2,i) + u(k,j-3,i) ) )   &
-                                      * adv_mom_5
-               swap_diss_y_local_u(k) = - ABS( v_comp ) * (                    &
-                                        10.0 * ( u(k,j,i) - u(k,j-1,i)   )     &
-                                      -  5.0 * ( u(k,j+1,i) - u(k,j-2,i) )     &
-                                      +        ( u(k,j+2,i) - u(k,j-3,i) ) )   &
-                                      * adv_mom_5
-            ENDDO
-            
-         ENDIF
-!         
-!--      Computation of interior fluxes and tendency terms
-         DO  j = nys, nyn
-            IF ( boundary_flags(j,i) /= 0 )  THEN
-!            
-!--            Degrade the order for Dirichlet bc. at the outflow boundary
-               SELECT CASE ( boundary_flags(j,i) )
-               
-                  CASE ( 1 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt
-                        u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                        flux_r(k) = ( u_comp(k) - gu ) * (                     &
-                                    7.0 * ( u(k,j,i+1) + u(k,j,i)   )          &
-                                  -       ( u(k,j,i+2) + u(k,j,i-1) ) )        &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS( u_comp(k) - gu ) * (                &
-                                    3.0 * ( u(k,j,i+1) - u(k,j,i) )            & 
-                                  -       ( u(k,j,i+2) - u(k,j,i-1) ) )        &
-                                  * adv_mom_3
-                     ENDDO
-                     
-                  CASE ( 2 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt
-                        u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                        flux_r(k) = ( u_comp(k) - gu ) *                      &
-                                    ( u(k,j,i+1) + u(k,j,i) ) * 0.25 
-                        diss_r(k) = diss_2nd( u(k,j,i+1), u(k,j,i+1),         &
-                                              u(k,j,i), u(k,j,i-1),           &
-                                              u(k,j,i-2), u_comp(k) ,0.25 ,ddx)
-                     ENDDO
-                     
-                  CASE ( 3 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt
-                        v_comp   = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                        flux_n(k) = v_comp * (                                 &
-                                    7.0 * ( u(k,j+1,i) + u(k,j,i)   )          &
-                                  -       ( u(k,j+2,i) + u(k,j-1,i) ) )        &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS( v_comp ) * (                        &
-                                    3.0 * ( u(k,j+1,i) - u(k,j,i)   )          & 
-                                  -       ( u(k,j+2,i) - u(k,j-1,i) ) )        &
-                                  * adv_mom_3
-                     ENDDO
-                     
-                  CASE ( 4 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt
-                        v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                        flux_n(k) = v_comp * ( u(k,j+1,i) + u(k,j,i) ) * 0.25
-                        diss_n(k) = diss_2nd( u(k,j+1,i), u(k,j+1,i),         &
-                                              u(k,j,i), u(k,j-1,i),           &
-                                              u(k,j-2,i), v_comp, 0.25, ddy )
-                     ENDDO
-                     
-                  CASE ( 5 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt        
-                        u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                        flux_r(k) = ( u_comp(k) - gu ) * (                     &
-                                    7.0 * ( u(k,j,i+1) + u(k,j,i)   )          &
-                                  -       ( u(k,j,i+2) + u(k,j,i-1) ) )        &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS( u_comp(k) - gu ) * (                &
-                                    3.0 * ( u(k,j,i+1) - u(k,j,i)   )          & 
-                                  -       ( u(k,j,i+2) - u(k,j,i-1) ) )        &
-                                  * adv_mom_3
-                     ENDDO
-                     
-                  CASE ( 7 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt                           
-                        v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                        flux_n(k) = v_comp * (                                 &
-                                    7.0 * ( u(k,j+1,i) + u(k,j,i)   )          &
-                                  -       ( u(k,j+2,i) + u(k,j-1,i) ) )        &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS( v_comp ) * (                        &
-                                    3.0 * ( u(k,j+1,i) - u(k,j,i)   )          & 
-                                  -       ( u(k,j+2,i) - u(k,j-1,i) ) )        &
-                                  * adv_mom_3
-                     ENDDO 
-                     
-                  CASE ( 8 )
-                     DO  k = nzb_u_inner(j,i)+1, nzt
-                        v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                        flux_n(k) = v_comp * (                                 &
-                                    7.0 * ( u(k,j+1,i) + u(k,j,i)   )          &
-                                  -       ( u(k,j+2,i) + u(k,j-1,i) ) )        &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS( v_comp ) * (                        &
-                                    3.0 * ( u(k,j+1,i) - u(k,j,i)   )          & 
-                                  -       ( u(k,j+2,i) - u(k,j-1,i) ) )        &
-                                  * adv_mom_3
-                     ENDDO  
-                     
-                  CASE DEFAULT
-                  
-               END SELECT
-!               
-!--            Compute the crosswise 5th order fluxes at the outflow
-               IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2 .OR.&
-                    boundary_flags(j,i) == 5 )  THEN
-              
-                  DO  k = nzb_u_inner(j,i)+1, nzt
-                     v_comp = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                     flux_n(k) = v_comp * (                                    &
-                                 37.0 * ( u(k,j+1,i) + u(k,j,i)   )            &
-                               -  8.0 * ( u(k,j+2,i) +u(k,j-1,i)  )            &
-                               +        ( u(k,j+3,i) + u(k,j-2,i) ) )          &
-                               * adv_mom_5
-                     diss_n(k) = - ABS( v_comp ) * (                           &
-                                 10.0 * ( u(k,j+1,i) - u(k,j,i)   )            &
-                               -  5.0 * ( u(k,j+2,i) - u(k,j-1,i) )            &
-                               +        ( u(k,j+3,i) - u(k,j-2,i) ) )          &
-                               * adv_mom_5
-                  ENDDO
-                  
-               ELSE
-               
-                  DO  k = nzb_u_inner(j,i)+1, nzt
-                     u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                     flux_r(k) = ( u_comp(k) - gu ) * (                        &
-                                 37.0 * ( u(k,j,i+1) + u(k,j,i)   )            &
-                               -  8.0 * ( u(k,j,i+2) + u(k,j,i-1) )            &
-                               +        ( u(k,j,i+3) + u(k,j,i-2) ) )          &
-                               * adv_mom_5
-                     diss_r(k) = - ABS( u_comp(k) - gu ) * (                   &
-                                 10.0 * ( u(k,j,i+1) - u(k,j,i)   )            &
-                               -  5.0 * ( u(k,j,i+2) - u(k,j,i-1) )            &
-                               +        ( u(k,j,i+3) - u(k,j,i-2) ) )          &
-                               * adv_mom_5
-                  ENDDO
-                  
-               ENDIF
-               
-            ELSE
-            
-               DO  k = nzb_u_inner(j,i)+1, nzt
-                  u_comp(k) = u(k,j,i+1) + u(k,j,i)
-                  flux_r(k) = ( u_comp(k) - gu ) * (                           &
-                              37.0 * ( u(k,j,i+1) + u(k,j,i)   )               &
-                            -  8.0 * ( u(k,j,i+2) + u(k,j,i-1) )               &
-                            +        ( u(k,j,i+3) + u(k,j,i-2) ) )             &
-                            * adv_mom_5
-                  diss_r(k) = - ABS( u_comp(k) - gu ) * (                      &
-                              10.0 * ( u(k,j,i+1) - u(k,j,i)   )               &
-                            -  5.0 * ( u(k,j,i+2) - u(k,j,i-1) )               &
-                            +        ( u(k,j,i+3) - u(k,j,i-2) ) ) * adv_mom_5
-
-                  v_comp = v(k,j+1,i) + v(k,j+1,i-1) - gv
-                  flux_n(k) = v_comp * (                                       &
-                              37.0 * ( u(k,j+1,i) + u(k,j,i)   )               &
-                            -  8.0 * ( u(k,j+2,i) + u(k,j-1,i) )               &
-                            +        ( u(k,j+3,i) + u(k,j-2,i) ) ) * adv_mom_5
-                  diss_n(k) = - ABS( v_comp ) * (                              &
-                              10.0 * ( u(k,j+1,i) - u(k,j,i)   )               &
-                            -  5.0 * ( u(k,j+2,i) - u(k,j-1,i) )               &
-                            +        ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
-                                
-               ENDDO
-               
-            ENDIF
-            
-            DO  k = nzb_u_inner(j,i)+1, nzt
-            
-               tend(k,j,i) = tend(k,j,i) - (                                   &
-              ( flux_r(k) + diss_r(k)                                          &
-            -   swap_flux_x_local_u(k,j) - swap_diss_x_local_u(k,j)   ) * ddx  &
-            + ( flux_n(k) + diss_n(k)                                          &
-            -   swap_flux_y_local_u(k) - swap_diss_y_local_u(k)       ) * ddy )
-                  
-               swap_flux_x_local_u(k,j) = flux_r(k)   
-               swap_diss_x_local_u(k,j) = diss_r(k)
-               swap_flux_y_local_u(k)   = flux_n(k)     
-               swap_diss_y_local_u(k)   = diss_n(k)
-                      
-               sums_us2_ws_l(k,tn)  = sums_us2_ws_l(k,tn)                            &
-                 + ( flux_r(k)                                                 &
-                 * ( u_comp(k) - 2.0 * hom(k,1,1,0) )                          &
-                 / ( u_comp(k) - gu + 1.0E-20 )                                & 
-                 +   diss_r(k)                                                 &
-                 *   ABS( u_comp(k) - 2.0 * hom(k,1,1,0) )                     &
-                 / ( ABS( u_comp(k) - gu) + 1.0E-20) )                         &
-                 *   weight_substep(intermediate_timestep_count)
-            ENDDO
-            sums_us2_ws_l(nzb_u_inner(j,i),tn) = sums_us2_ws_l(nzb_u_inner(j,i)+1,tn)
-         ENDDO
+!--       The following loop computes the fluxes for the south boundary points
+          j = nys
+          DO  k = nzb+1, nzb_max
+
+             v_comp                 = v(k,j,i) + v(k,j,i-1) - gv
+             swap_flux_y_local_u(k) = v_comp * (                              &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( u(k,j,i)   + u(k,j-1,i) )              &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( u(k,j+1,i) + u(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( u(k,j+2,i) + u(k,j-3,i) )              &
+                                               )
+
+             swap_diss_y_local_u(k) = - ABS ( v_comp ) * (                    &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( u(k,j,i)   - u(k,j-1,i) )              &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( u(k,j+1,i) - u(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( u(k,j+2,i) - u(k,j-3,i) )              &
+                                                         )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             v_comp                 = v(k,j,i) + v(k,j,i-1) - gv
+             swap_flux_y_local_u(k) = v_comp * (                              &
+                           37.0 * ( u(k,j,i) + u(k,j-1,i)   )                 &
+                         -  8.0 * ( u(k,j+1,i) + u(k,j-2,i) )                 &
+                         +        ( u(k,j+2,i) + u(k,j-3,i) ) ) * adv_mom_5
+             swap_diss_y_local_u(k) = - ABS(v_comp) * (                       &
+                           10.0 * ( u(k,j,i) - u(k,j-1,i)   )                 &
+                         -  5.0 * ( u(k,j+1,i) - u(k,j-2,i) )                 &
+                         +        ( u(k,j+2,i) - u(k,j-3,i) ) ) * adv_mom_5
+
+          ENDDO
+!
+!--       Computation of interior fluxes and tendency terms
+          DO  j = nys, nyn
+
+             flux_t(0) = 0.0
+             diss_t(0) = 0.0
+             flux_d    = 0.0
+             diss_d    = 0.0
+
+             DO  k = nzb+1, nzb_max
+
+                u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                flux_r(k) = ( u_comp(k) - gu ) * (                           &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j,i+1) + u(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j,i+2) + u(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j,i+3) + u(k,j,i-2) )                 &
+                                                 )
+
+                diss_r(k) = - ABS( u_comp(k) - gu ) * (                      &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),9,1)  * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j,i+1) - u(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),10,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j,i+2) - u(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),11,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j,i+3) - u(k,j,i-2) )                 &
+                                                     )
+
+                v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                flux_n(k) = v_comp * (                                       &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j+1,i) + u(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j+2,i) + u(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j+3,i) + u(k,j-2,i) )                 &
+                                                 )
+
+                diss_n(k) = - ABS ( v_comp ) * (                             &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),12,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( u(k,j+1,i) - u(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),13,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( u(k,j+2,i) - u(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),14,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( u(k,j+3,i) - u(k,j-2,i) )                 &
+                                                      )
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),17,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),15,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),17,1)
+
+                w_comp    = w(k,j,i) + w(k,j,i-1)
+                flux_t(k) = w_comp  * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)  + u(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i) + u(k-1,j,i)   )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) + u(k_mm,j,i) )                  &
+                                      )
+
+                diss_t(k) = - ABS( w_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)   - u(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i)  - u(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) - u(k_mm,j,i) )                  &
+                                              )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div = ( ( u_comp(k)   - ( u(k,j,i)   + u(k,j,i-1)   ) ) * ddx  &
+                     +  ( v_comp + gv - ( v(k,j,i)   + v(k,j,i-1 )  ) ) * ddy  &
+                     +  ( w_comp      - ( w(k-1,j,i) + w(k-1,j,i-1) ) )        &
+                                                                    * ddzw(k)  &
+                      ) * 0.5
+
+                tend(k,j,i) = tend(k,j,i) - (                                  &
+                 ( flux_r(k) + diss_r(k)                                       &
+               -   swap_flux_x_local_u(k,j) - swap_diss_x_local_u(k,j) ) * ddx &
+               + ( flux_n(k) + diss_n(k)                                       &
+               -   swap_flux_y_local_u(k)   - swap_diss_y_local_u(k)   ) * ddy &
+               + ( flux_t(k) + diss_t(k)                                       &
+               -   flux_d    - diss_d                                          &
+                                                                  ) * ddzw(k)  &
+                                           ) + div * u(k,j,i)
+
+                swap_flux_x_local_u(k,j) = flux_r(k)
+                swap_diss_x_local_u(k,j) = diss_r(k)
+                swap_flux_y_local_u(k)   = flux_n(k)
+                swap_diss_y_local_u(k)   = diss_n(k)
+                flux_d                   = flux_t(k)
+                diss_d                   = diss_t(k)
+!
+!--             Statistical Evaluation of u'u'. The factor has to be applied
+!--             for right evaluation when gallilei_trans = .T. .
+                sums_us2_ws_l(k,tn) = sums_us2_ws_l(k,tn)                     &
+                              + ( flux_r(k) *                                 &
+                                ( u_comp(k) - 2.0 * hom(k,1,1,0) )            &
+                              / ( u_comp(k) - gu + 1.0E-20      )             &
+                              +   diss_r(k) *                                 &
+                                  ABS( u_comp(k) - 2.0 * hom(k,1,1,0) )       &
+                              / ( ABS( u_comp(k) - gu ) + 1.0E-20 ) )         &
+                              *   weight_substep(intermediate_timestep_count)
+!
+!--             Statistical Evaluation of w'u'.
+                sums_wsus_ws_l(k,tn) = sums_wsus_ws_l(k,tn)                   &
+                              + ( flux_t(k) + diss_t(k) )                     &
+                              *   weight_substep(intermediate_timestep_count)
+             ENDDO
+
+             DO  k = nzb_max+1, nzt
+
+                u_comp(k) = u(k,j,i+1) + u(k,j,i)
+                flux_r(k) = ( u_comp(k) - gu ) * (                            &
+                         37.0 * ( u(k,j,i+1) + u(k,j,i)   )                   &
+                       -  8.0 * ( u(k,j,i+2) + u(k,j,i-1) )                   &
+                       +        ( u(k,j,i+3) + u(k,j,i-2) ) ) * adv_mom_5
+                diss_r(k) = - ABS( u_comp(k) - gu ) * (                       &
+                         10.0 * ( u(k,j,i+1) - u(k,j,i)   )                   &
+                       -  5.0 * ( u(k,j,i+2) - u(k,j,i-1) )                   &
+                       +        ( u(k,j,i+3) - u(k,j,i-2) ) ) * adv_mom_5
+
+                v_comp    = v(k,j+1,i) + v(k,j+1,i-1) - gv
+                flux_n(k) = v_comp * (                                        &
+                         37.0 * ( u(k,j+1,i) + u(k,j,i)   )                   &
+                       -  8.0 * ( u(k,j+2,i) + u(k,j-1,i) )                   &
+                       +        ( u(k,j+3,i) + u(k,j-2,i) ) ) * adv_mom_5
+                diss_n(k) = - ABS( v_comp ) * (                               &
+                         10.0 * ( u(k,j+1,i) - u(k,j,i)   )                   &
+                       -  5.0 * ( u(k,j+2,i) - u(k,j-1,i) )                   &
+                       +        ( u(k,j+3,i) - u(k,j-2,i) ) ) * adv_mom_5
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),17,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),15,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),17,1)
+
+                w_comp    = w(k,j,i) + w(k,j,i-1)
+                flux_t(k) = w_comp  * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)  + u(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i) + u(k-1,j,i)   )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) + u(k_mm,j,i) )                  &
+                                      )
+
+                 diss_t(k) = - ABS( w_comp ) * (                             &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),15,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( u(k+1,j,i)   - u(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),16,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( u(k_pp,j,i)  - u(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),17,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( u(k_ppp,j,i) - u(k_mm,j,i) )                  &
+                                               )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div = ( ( u_comp(k)   - ( u(k,j,i)   + u(k,j,i-1)   ) ) * ddx &
+                     +  ( v_comp + gv - ( v(k,j,i)   + v(k,j,i-1 )  ) ) * ddy &
+                     +  ( w_comp      - ( w(k-1,j,i) + w(k-1,j,i-1) ) )       &
+                                                                    * ddzw(k) &
+                      ) * 0.5
+
+                 tend(k,j,i) = tend(k,j,i) - (                                 &
+                 ( flux_r(k) + diss_r(k)                                       &
+               -   swap_flux_x_local_u(k,j) - swap_diss_x_local_u(k,j) ) * ddx &
+               + ( flux_n(k) + diss_n(k)                                       &
+               -   swap_flux_y_local_u(k)   - swap_diss_y_local_u(k)   ) * ddy &
+               + ( flux_t(k) + diss_t(k)                                       &
+               -   flux_d    - diss_d                                          &
+                                                                  ) * ddzw(k)  &
+                                           ) + div * u(k,j,i)
+
+                 swap_flux_x_local_u(k,j) = flux_r(k)
+                 swap_diss_x_local_u(k,j) = diss_r(k)
+                 swap_flux_y_local_u(k)   = flux_n(k)
+                 swap_diss_y_local_u(k)   = diss_n(k)
+                 flux_d                   = flux_t(k)
+                 diss_d                   = diss_t(k)
+!
+!--              Statistical Evaluation of u'u'. The factor has to be applied
+!--              for right evaluation when gallilei_trans = .T. .
+                 sums_us2_ws_l(k,tn) = sums_us2_ws_l(k,tn)                    &
+                              + ( flux_r(k) *                                 &
+                                ( u_comp(k) - 2.0 * hom(k,1,1,0) )            &
+                              / ( u_comp(k) - gu + 1.0E-20      )             &
+                              +   diss_r(k) *                                 &
+                                  ABS( u_comp(k) - 2.0 * hom(k,1,1,0) )       &
+                              / ( ABS( u_comp(k) - gu ) + 1.0E-20 ) )         &
+                              *   weight_substep(intermediate_timestep_count)
+!
+!--              Statistical Evaluation of w'u'.
+                 sums_wsus_ws_l(k,tn) = sums_wsus_ws_l(k,tn)                  &
+                              + ( flux_t(k) + diss_t(k) )                     &
+                              *   weight_substep(intermediate_timestep_count)
        ENDDO
-
-!
-!--   Vertical advection, degradation of order near surface and top. 
-!--   The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--   statistical evaluation the top flux at the surface should be 0
-       DO i = nxlu, nxr
-          DO  j = nys, nyn
-             k = nzb_u_inner(j,i)+1
-!             
-!--         The fluxes flux_d and diss_d at the surface are 0. Due to static
-!--         reasons the top flux at the surface should be 0.
-            flux_t(nzb_u_inner(j,i)) = 0.0
-            diss_t(nzb_u_inner(j,i)) = 0.0
-            flux_d = flux_t(k-1)
-            diss_d = diss_t(k-1)             
-!
-!--         2nd order scheme (bottom)
-             w_comp    = w(k,j,i) + w(k,j,i-1)
-             flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) * 0.25
-             diss_t(k) = diss_2nd( u(k+2,j,i), u(k+1,j,i), u(k,j,i),           &
-                                   0.0, 0.0, w_comp, 0.25, ddzw(k) )
-                                   
-             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                       -   flux_d    -  diss_d       ) * ddzw(k)
-!
-!--         WS3 as an intermediate step (bottom)
-            k         = nzb_u_inner(j,i)+2
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            w_comp    = w(k,j,i) + w(k,j,i-1)
-            flux_t(k) = w_comp * (                                            &
-                        7.0 * ( u(k+1,j,i) + u(k,j,i)   )                     &
-                      -       ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
-            diss_t(k) = - ABS( w_comp ) * (                                   &
-                        3.0 * ( u(k+1,j,i) - u(k,j,i)   )                     & 
-                      -       ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
-
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    -  diss_d      ) * ddzw(k)
-!
-!WS5
-             DO  k = nzb_u_inner(j,i)+3, nzt-2
-
-                flux_d    = flux_t(k-1)
-                diss_d    = diss_t(k-1)
-                w_comp    = w(k,j,i) + w(k,j,i-1)
-                flux_t(k) = w_comp * (                                        &
-                            37.0 * ( u(k+1,j,i) + u(k,j,i)   )                &
-                          -  8.0 * ( u(k+2,j,i) + u(k-1,j,i) )                &
-                          +        ( u(k+3,j,i) + u(k-2,j,i) ) ) * adv_mom_5
-                diss_t(k) = - ABS( w_comp ) * (                               &
-                            10.0 * ( u(k+1,j,i) - u(k,j,i)   )                & 
-                          -  5.0 * ( u(k+2,j,i) - u(k-1,j,i) )                & 
-                          +        ( u(k+3,j,i) - u(k-2,j,i) ) ) * adv_mom_5
-
-                tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)           &
-                                          -   flux_d    -  diss_d   ) * ddzw(k)
-
-             ENDDO
-!
-!--          WS3 as an intermediate step (top)
-             k         = nzt-1
-             flux_d    = flux_t(k-1)
-             diss_d    = diss_t(k-1)
-             w_comp    = w(k,j,i) + w(k,j,i-1)
-             flux_t(k) = w_comp * (                                           &
-                         7.0 * ( u(k+1,j,i) + u(k,j,i) )                      &
-                       -       ( u(k+2,j,i) + u(k-1,j,i) ) ) * adv_mom_3
-             diss_t(k) = - ABS( w_comp ) * (                                  &
-                         3.0 * ( u(k+1,j,i) - u(k,j,i) )                      & 
-                       -       ( u(k+2,j,i) - u(k-1,j,i) ) ) * adv_mom_3
-                            
-             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
-                                       -   flux_d    -  diss_d      ) * ddzw(k)
-!
-!--         2nd order scheme (top)
-             k         = nzt
-             flux_d    = flux_t(k-1)
-             diss_d    = diss_t(k-1)
-             w_comp    = w(k,j,i) + w(k,j,i-1)
-             flux_t(k) = w_comp * ( u(k+1,j,i) + u(k,j,i) ) * 0.25
-             diss_t(k) = diss_2nd( u(nzt+1,j,i), u(nzt+1,j,i), u(k,j,i),      &
-                                   u(k-1,j,i), u(k-2,j,i), w_comp,            &
-                                   0.25, ddzw(k)) 
-
-             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
-                                       -   flux_d    -  diss_d      ) * ddzw(k)
-!
-!-- at last vertical momentum flux is accumulated
-            DO  k = nzb_u_inner(j,i), nzt
-               sums_wsus_ws_l(k,tn) = sums_wsus_ws_l(k,tn)                           &
-                              + ( flux_t(k) + diss_t(k) )                      &
-                              *   weight_substep(intermediate_timestep_count)
-            ENDDO
           ENDDO
        ENDDO
+       sums_us2_ws_l(nzb,tn) = sums_us2_ws_l(nzb+1,tn)
 
 
@@ -2473,359 +2591,388 @@
 
 
-       INTEGER ::  i, j, k, tn = 0
-       REAL    ::  gu, gv, flux_l, flux_s, flux_d, diss_l, diss_s, diss_d,    &
-                   u_comp, w_comp
+       INTEGER ::  i, j, k, tn = 0, k_ppp, k_pp, k_mm
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp, w_comp, div
        REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local_v, swap_diss_y_local_v
        REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_v,             &
                                              swap_diss_x_local_v
-       REAL, DIMENSION(nzb:nzt+1) :: flux_t, diss_t, flux_n, diss_n, flux_r,  &
-                                     diss_r, v_comp
+       REAL, DIMENSION(nzb:nzt) :: flux_t, diss_t, flux_n, diss_n, flux_r,    &
+                                   diss_r, v_comp
 
        gu = 2.0 * u_gtrans
        gv = 2.0 * v_gtrans
 !
-!-- First compute the whole left boundary of the processor domain
+!--    First compute the whole left boundary of the processor domain
        i = nxl
        DO  j = nysv, nyn
-       
-          IF ( boundary_flags(j,i) == 6 )  THEN
-             DO  k = nzb_v_inner(j,i)+1, nzt
-                u_comp                   = u(k,j-1,i) + u(k,j,i) - gu
-                swap_flux_x_local_v(k,j) = u_comp *                           &
-                                           ( v(k,j,i) + v(k,j,i-1)) * 0.25
-                swap_diss_x_local_v(k,j) = diss_2nd( v(k,j,i+2), v(k,j,i+1),  &
-                                                     v(k,j,i), v(k,j,i-1),    &
-                                                     v(k,j,i-1), u_comp,      &
-                                                     0.25, ddx )
+          DO  k = nzb+1, nzb_max
+
+             u_comp                   = u(k,j-1,i) + u(k,j,i) - gu
+             swap_flux_x_local_v(k,j) = u_comp * (                             &
+                          ( 37.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +     7.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( v(k,j,i)   + v(k,j,i-1) )               &
+                   -      (  8.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( v(k,j,i+1) + v(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( v(k,j,i+2) + v(k,j,i-3) )               &
+                                                 )
+
+              swap_diss_x_local_v(k,j) = - ABS( u_comp ) * (                   &
+                          ( 10.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +     3.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( v(k,j,i)   - v(k,j,i-1) )               &
+                   -      (  5.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( v(k,j,i+1) - v(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( v(k,j,i+2) - v(k,j,i-3) )               &
+                                                           )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp                   = u(k,j-1,i) + u(k,j,i) - gu
+             swap_flux_x_local_v(k,j) = u_comp * (                            &
+                             37.0 * ( v(k,j,i) + v(k,j,i-1)   )               &
+                           -  8.0 * ( v(k,j,i+1) + v(k,j,i-2) )               &
+                           +        ( v(k,j,i+2) + v(k,j,i-3) ) ) * adv_mom_5
+             swap_diss_x_local_v(k,j) = - ABS( u_comp ) * (                   &
+                             10.0 * ( v(k,j,i) - v(k,j,i-1)   )               &
+                           -  5.0 * ( v(k,j,i+1) - v(k,j,i-2) )               &
+                           +        ( v(k,j,i+2) - v(k,j,i-3) ) ) * adv_mom_5
+
+          ENDDO
+
+       ENDDO
+
+       DO i = nxl, nxr
+
+          j = nysv
+          DO  k = nzb+1, nzb_max
+
+             v_comp(k)              = v(k,j,i) + v(k,j-1,i) - gv
+             swap_flux_y_local_v(k) = v_comp(k) * (                           &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( v(k,j,i)   + v(k,j-1,i) )              &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( v(k,j+1,i) + v(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( v(k,j+2,i) + v(k,j-3,i) )              &
+                                                 )
+
+             swap_diss_y_local_v(k) = - ABS( v_comp(k) ) * (                  &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( v(k,j,i)   - v(k,j-1,i) )              &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( v(k,j+1,i) - v(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( v(k,j+2,i) - v(k,j-3,i) )              &
+                                                          )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             v_comp(k)              = v(k,j,i) + v(k,j-1,i) - gv
+             swap_flux_y_local_v(k) = v_comp(k) * (                           &
+                           37.0 * ( v(k,j,i) + v(k,j-1,i)   )                 &
+                         -  8.0 * ( v(k,j+1,i) + v(k,j-2,i) )                 &
+                         +        ( v(k,j+2,i) + v(k,j-3,i) ) ) * adv_mom_5
+             swap_diss_y_local_v(k) = - ABS( v_comp(k) ) * (                  &
+                           10.0 * ( v(k,j,i) - v(k,j-1,i)   )                 &
+                         -  5.0 * ( v(k,j+1,i) - v(k,j-2,i) )                 &
+                         +        ( v(k,j+2,i) - v(k,j-3,i) ) ) * adv_mom_5
+
+          ENDDO
+
+          DO  j = nysv, nyn
+
+             flux_t(0) = 0.0
+             diss_t(0) = 0.0
+             flux_d    = 0.0
+             diss_d    = 0.0
+
+             DO  k = nzb+1, nzb_max
+
+                u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                flux_r(k) = u_comp * (                                       &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j,i+1) + v(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j,i+2) + v(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j,i+3) + v(k,j,i-2) )                 &
+                                     )
+
+                diss_r(k) = - ABS( u_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),18,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j,i+1) - v(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),19,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j,i+2) - v(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),20,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j,i+3) - v(k,j,i-2) )                 &
+                                              )
+
+                v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                flux_n(k) = ( v_comp(k) - gv ) * (                           &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j+1,i) + v(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j+2,i) + v(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j+3,i) + v(k,j-2,i) )                 &
+                                     )
+
+                diss_n(k) = - ABS( v_comp(k) - gv ) * (                      &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),21,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( v(k,j+1,i) - v(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),22,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( v(k,j+2,i) - v(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),23,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( v(k,j+3,i) - v(k,j-2,i) )                 &
+                                                      )
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),26,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),24,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),26,1)
+
+                w_comp    = w(k,j-1,i) + w(k,j,i)
+                flux_t(k) = w_comp  * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   + v(k,j,i)    )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  + v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) + v(k_mm,j,i) )                  &
+                                      )
+
+                diss_t(k) = - ABS( w_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   - v(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  - v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) - v(k_mm,j,i) )                  &
+                                               )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div = ( ( u_comp + gu - ( u(k,j-1,i)   + u(k,j,i)   ) ) * ddx &
+                     +  ( v_comp(k)   - ( v(k,j,i)     + v(k,j-1,i) ) ) * ddy &
+                     +  ( w_comp      - ( w(k-1,j-1,i) + w(k-1,j,i) )         &
+                                                                  ) * ddzw(k) &
+                      ) * 0.5
+
+                tend(k,j,i) = tend(k,j,i) - (                                 &
+                       ( flux_r(k) + diss_r(k)                                &
+                     -   swap_flux_x_local_v(k,j) - swap_diss_x_local_v(k,j)  &
+                       ) * ddx                                                &
+                     + ( flux_n(k) + diss_n(k)                                &
+                     -   swap_flux_y_local_v(k) - swap_diss_y_local_v(k)      &
+                       ) * ddy                                                &
+                     + ( flux_t(k) + diss_t(k)                                &
+                     -   flux_d    - diss_d                                   &
+                       ) * ddzw(k)                                            &
+                                            )  + v(k,j,i) * div
+
+                swap_flux_x_local_v(k,j) = flux_r(k)
+                swap_diss_x_local_v(k,j) = diss_r(k)
+                swap_flux_y_local_v(k)   = flux_n(k)
+                swap_diss_y_local_v(k)   = diss_n(k)
+                flux_d                   = flux_t(k)
+                diss_d                   = diss_t(k)
+
+!
+!--             Statistical Evaluation of v'v'. The factor has to be applied
+!--             for right evaluation when gallilei_trans = .T. .
+                sums_vs2_ws_l(k,tn) = sums_vs2_ws_l(k,tn)                     &
+                      + ( flux_n(k)                                           &
+                      * ( v_comp(k) - 2.0 * hom(k,1,2,0) )                    &
+                      / ( v_comp(k) - gv + 1.0E-20 )                          &
+                      +   diss_n(k)                                           &
+                      *   ABS( v_comp(k) - 2.0 * hom(k,1,2,0) )               &
+                      / ( ABS( v_comp(k) - gv ) +1.0E-20 ) )                  &
+                      *   weight_substep(intermediate_timestep_count)
+!
+!--              Statistical Evaluation of w'v'.
+                 sums_wsvs_ws_l(k,tn) = sums_wsvs_ws_l(k,tn)                  &
+                              + ( flux_t(k) + diss_t(k) )                     &
+                              *   weight_substep(intermediate_timestep_count)
+
              ENDDO
-             
-          ELSE
-          
-             DO  k = nzb_v_inner(j,i)+1, nzt
-                u_comp                   = u(k,j-1,i) + u(k,j,i) - gu
-                swap_flux_x_local_v(k,j) = u_comp * (                          &
-                                           37.0 * ( v(k,j,i) + v(k,j,i-1)   )  &
-                                         -  8.0 * ( v(k,j,i+1) + v(k,j,i-2) )  &
-                                         +        ( v(k,j,i+2) + v(k,j,i-3) ) )&
-                                         * adv_mom_5
-                swap_diss_x_local_v(k,j) = - ABS( u_comp ) * (                 &
-                                           10.0 * ( v(k,j,i) - v(k,j,i-1)   )  &
-                                         -  5.0 * ( v(k,j,i+1) - v(k,j,i-2) )  &
-                                         +        ( v(k,j,i+2) - v(k,j,i-3) ) )&
-                                         * adv_mom_5
+
+             DO  k = nzb_max+1, nzt
+
+                u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
+                flux_r(k) = u_comp * (                                        &
+                      37.0 * ( v(k,j,i+1) + v(k,j,i)   )                      &
+                    -  8.0 * ( v(k,j,i+2) + v(k,j,i-1) )                      &
+                    +        ( v(k,j,i+3) + v(k,j,i-2) ) ) * adv_mom_5
+
+                diss_r(k) = - ABS( u_comp ) * (                               &
+                      10.0 * ( v(k,j,i+1) - v(k,j,i) )                        &
+                    -  5.0 * ( v(k,j,i+2) - v(k,j,i-1) )                      &
+                    +        ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
+
+
+                v_comp(k) = v(k,j+1,i) + v(k,j,i)
+                flux_n(k) = ( v_comp(k) - gv ) * (                            &
+                      37.0 * ( v(k,j+1,i) + v(k,j,i)   )                      &
+                    -  8.0 * ( v(k,j+2,i) + v(k,j-1,i) )                      &
+                      +      ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
+
+                diss_n(k) = - ABS( v_comp(k) - gv ) * (                       &
+                      10.0 * ( v(k,j+1,i) - v(k,j,i)   )                      &
+                    -  5.0 * ( v(k,j+2,i) - v(k,j-1,i) )                      &
+                    +        ( v(k,j+3,i) - v(k,j-2,i) ) ) * adv_mom_5
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),26,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),24,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),26,1)
+
+                w_comp    = w(k,j-1,i) + w(k,j,i)
+                flux_t(k) = w_comp  * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   + v(k,j,i)    )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  + v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) + v(k_mm,j,i) )                  &
+                                 )
+
+                diss_t(k) = - ABS( w_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),24,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( v(k+1,j,i)   - v(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),25,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( v(k_pp,j,i)  - v(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),26,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( v(k_ppp,j,i) - v(k_mm,j,i) )                  &
+                                         )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div = ( ( u_comp + gu - ( u(k,j-1,i)   + u(k,j,i)   ) ) * ddx &
+                     +  ( v_comp(k)   - ( v(k,j,i)     + v(k,j-1,i) ) ) * ddy &
+                     +  ( w_comp      - ( w(k-1,j-1,i) + w(k-1,j,i) ) )       &
+                                                                    * ddzw(k) &
+                      ) * 0.5
+ 
+                tend(k,j,i) = tend(k,j,i) - (                                 &
+                       ( flux_r(k) + diss_r(k)                                &
+                     -   swap_flux_x_local_v(k,j) - swap_diss_x_local_v(k,j)  &
+                       ) * ddx                                                &
+                     + ( flux_n(k) + diss_n(k)                                &
+                     -   swap_flux_y_local_v(k) - swap_diss_y_local_v(k)      &
+                       ) * ddy                                                &
+                     + ( flux_t(k) + diss_t(k)                                &
+                     -   flux_d    - diss_d                                   &
+                       ) * ddzw(k)                                            &
+                                            )  + v(k,j,i) * div
+
+                swap_flux_x_local_v(k,j) = flux_r(k)
+                swap_diss_x_local_v(k,j) = diss_r(k)
+                swap_flux_y_local_v(k)   = flux_n(k)
+                swap_diss_y_local_v(k)   = diss_n(k)
+                flux_d                   = flux_t(k)
+                diss_d                   = diss_t(k)
+
+!
+!--             Statistical Evaluation of v'v'. The factor has to be applied
+!--             for right evaluation when gallilei_trans = .T. .
+                sums_vs2_ws_l(k,tn) = sums_vs2_ws_l(k,tn)                     &
+                         + ( flux_n(k)                                        &
+                         * ( v_comp(k) - 2.0 * hom(k,1,2,0) )                 &
+                         / ( v_comp(k) - gv + 1.0E-20 )                       &
+                         +   diss_n(k)                                        &
+                         *   ABS( v_comp(k) - 2.0 * hom(k,1,2,0) )            &
+                         / ( ABS( v_comp(k) - gv ) +1.0E-20 ) )               &
+                         *   weight_substep(intermediate_timestep_count)
+!
+!--             Statistical Evaluation of w'v'.
+                sums_wsvs_ws_l(k,tn) = sums_wsvs_ws_l(k,tn)                    &
+                              + ( flux_t(k) + diss_t(k) )                      &
+                              *   weight_substep(intermediate_timestep_count)
+
              ENDDO
-             
-          ENDIF
-          
+          ENDDO
        ENDDO
-
-       DO i = nxl, nxr
-         j = nysv
-         IF ( boundary_flags(j,i) == 7 )  THEN
-         
-            DO  k = nzb_v_inner(j,i)+1, nzt
-               v_comp(k)              = v(k,j,i) + v(k,j-1,i) - gv
-               swap_flux_y_local_v(k) = v_comp(k) *                           &
-                                        ( v(k,j,i) + v(k,j-1,i) ) * 0.25
-               swap_diss_y_local_v(k) = diss_2nd( v(k,j+2,i), v(k,j+1,i),     &
-                                                  v(k,j,i), v(k,j-1,i),       &
-                                                  v(k,j-1,i), v_comp(k),      &
-                                                  0.25, ddy )                                
-            ENDDO
-            
-         ELSE
-         
-            DO  k = nzb_v_inner(j,i)+1, nzt
-               v_comp(k)              = v(k,j,i) + v(k,j-1,i) - gv
-               swap_flux_y_local_v(k) = v_comp(k) * (                          &
-                                        37.0 * ( v(k,j,i) + v(k,j-1,i)   )     &
-                                      -  8.0 * ( v(k,j+1,i) + v(k,j-2,i) )     &
-                                      +        ( v(k,j+2,i) + v(k,j-3,i) ) )   &
-                                      * adv_mom_5
-               swap_diss_y_local_v(k) = - ABS( v_comp(k) ) * (                 &
-                                        10.0 * ( v(k,j,i) - v(k,j-1,i)   )     &
-                                      -  5.0 * ( v(k,j+1,i) - v(k,j-2,i) )     &
-                                      +        ( v(k,j+2,i) - v(k,j-3,i) ) )   &
-                                      * adv_mom_5
-            ENDDO
-            
-         ENDIF
-         
-         DO  j = nysv, nyn
-            IF ( boundary_flags(j,i) /= 0 )  THEN
-!       
-!--       Degrade the order for Dirichlet bc. at the outflow boundary
-               SELECT CASE ( boundary_flags(j,i) )
-          
-                  CASE ( 1 )
-                     DO  k = nzb_v_inner(j,i)+1, nzt
-                        u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                        flux_r(k) = u_comp * (                                 &
-                                    7.0 * (v(k,j,i+1) + v(k,j,i)    )          &
-                                  -       ( v(k,j,i+2) + v(k,j,i-1) ) )        &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS( u_comp ) * (                        &
-                                    3.0 * ( v(k,j,i+1) - v(k,j,i)   )          & 
-                                  -       ( v(k,j,i+2) - v(k,j,i-1) ) )        &
-                                  * adv_mom_3
-                     ENDDO
-                
-                  CASE ( 2 )
-                     DO  k = nzb_v_inner(j,i)+1, nzt
-                        u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                        flux_r(k) = u_comp * ( v(k,j,i+1) + v(k,j,i) ) * 0.25 
-                        diss_r(k) = diss_2nd( v(k,j,i+1), v(k,j,i+1),         &
-                                              v(k,j,i), v(k,j,i-1),           &
-                                              v(k,j,i-2), u_comp, 0.25, ddx )
-                     ENDDO
-                
-                  CASE ( 3 )
-                     DO  k = nzb_v_inner(j,i)+1, nzt
-                        v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                        flux_n(k) = ( v_comp(k)- gv ) * (                     &
-                                    7.0 * ( v(k,j+1,i) + v(k,j,i)   )         &
-                                  -       ( v(k,j+2,i) + v(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS(v_comp(k) - gv) * (                 &
-                                    3.0 * ( v(k,j+1,i) - v(k,j,i)   )         & 
-                                  -       ( v(k,j+2,i) - v(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                     ENDDO
-                
-                  CASE ( 4 )
-                     DO  k = nzb_v_inner(j,i)+1, nzt
-                        v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                        flux_n(k) = ( v_comp(k) - gv ) *                      &
-                                   ( v(k,j+1,i) + v(k,j,i) ) * 0.25 
-                        diss_n(k) = diss_2nd( v(k,j+1,i), v(k,j+1,i),         &
-                                              v(k,j,i), v(k,j-1,i),           &
-                                              v(k,j-2,i), v_comp(k), 0.25, ddy)
-                     ENDDO
-                
-                  CASE ( 5 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        u_comp    = u(k,j-1,i) + u(k,j,i) - gu 
-                        flux_r(k) = u_comp * (                                &
-                                    7.0 * ( v(k,j,i+1) + v(k,j,i)   )         &
-                                  -       ( v(k,j,i+2) + v(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS (u_comp ) * (                       &
-                                    3.0 * ( w(k,j,i+1) - w(k,j,i)   )         & 
-                                  -       ( v(k,j,i+2) - v(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                     ENDDO
-                 
-                  CASE ( 6 )
-                     DO  k = nzb_v_inner(j,i)+1, nzt
-                                  
-                        u_comp = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                        flux_r(k) = u_comp * (                                &
-                                    7.0 * ( v(k,j,i+1) + v(k,j,i)   )         &
-                                  -       ( v(k,j,i+2) + v(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS( u_comp ) * (                       &
-                                    3.0 * ( v(k,j,i+1) - v(k,j,i)   )         & 
-                                  -       ( v(k,j,i+2) - v(k,j,i-1) ) )       &
-                                  * adv_mom_3 
-                     ENDDO
-                
-                  CASE ( 7 )
-                     DO  k = nzb_v_inner(j,i)+1, nzt                                 
-                        v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                        flux_n(k) = ( v_comp(k) - gv ) * (                    &
-                                    7.0 * ( v(k,j+1,i) + v(k,j,i)   )         &
-                                  -       ( v(k,j+2,i) + v(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS( v_comp(k) - gv ) * (               &
-                                    3.0 * ( v(k,j+1,i) - v(k,j,i)   )         & 
-                                  -       ( v(k,j+2,i) - v(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                     ENDDO
-                
-                  CASE DEFAULT
-              
-               END SELECT
-!          
-!--            Compute the crosswise 5th order fluxes at the outflow
-               IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2 .OR.&
-                    boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )   &
-               THEN
-          
-                  DO  k = nzb_v_inner(j,i)+1, nzt
-                     v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                     flux_n(k) = ( v_comp(k) - gv ) * (                       &
-                                 37.0 * ( v(k,j+1,i) + v(k,j,i)   )           &
-                               -  8.0 * ( v(k,j+2,i) + v(k,j-1,i) )           &
-                               +        ( v(k,j+3,i) + v(k,j-2,i) ) )         &
-                               * adv_mom_5
-                     diss_n(k) = - ABS( v_comp(k) - gv ) * (                  &
-                                 10.0 * ( v(k,j+1,i) - v(k,j,i)   )           &
-                               -  5.0 * ( v(k,j+2,i) - v(k,j-1,i) )           &
-                               +        ( v(k,j+3,i) - v(k,j-2,i) ) )         &
-                               * adv_mom_5
-                   ENDDO
-              
-               ELSE
-           
-                  DO  k = nzb_v_inner(j,i)+1, nzt
-                     u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                     flux_r(k) = u_comp * (                                   &
-                                 37.0 * ( v(k,j,i+1) + v(k,j,i)   )           &
-                              -   8.0 * ( v(k,j,i+2) + v(k,j,i-1) )           &
-                              +         ( v(k,j,i+3) + v(k,j,i-2) ) )         &
-                              * adv_mom_5
-                     diss_r(k) = - ABS( u_comp ) * (                          &
-                                 10.0 * ( v(k,j,i+1) - v(k,j,i)   )           &
-                               -  5.0 * ( v(k,j,i+2) - v(k,j,i-1) )           &
-                               +        ( v(k,j,i+3) - v(k,j,i-2) ) )         &
-                               * adv_mom_5
-                  ENDDO
-               
-               ENDIF
-         
-         
-            ELSE
-         
-               DO  k = nzb_v_inner(j,i)+1, nzt
-                  u_comp    = u(k,j-1,i+1) + u(k,j,i+1) - gu
-                  flux_r(k) = u_comp * (                                      &
-                              37.0 * ( v(k,j,i+1) + v(k,j,i)   )              &
-                            -  8.0 * ( v(k,j,i+2) + v(k,j,i-1) )              &
-                            +        ( v(k,j,i+3) + v(k,j,i-2) ) )            &
-                            * adv_mom_5
-                  diss_r(k) = - ABS ( u_comp ) * (                            &
-                              10.0 * ( v(k,j,i+1) - v(k,j,i)   )              &
-                            -  5.0 * ( v(k,j,i+2) - v(k,j,i-1) )              &
-                            +        ( v(k,j,i+3) - v(k,j,i-2) ) ) * adv_mom_5
-
-                  v_comp(k) = v(k,j+1,i) + v(k,j,i)
-                  flux_n(k) = ( v_comp(k) - gv ) * (                          &
-                              37.0 * ( v(k,j+1,i) + v(k,j,i)   )              &
-                            -  8.0 * ( v(k,j+2,i) + v(k,j-1,i) )              &
-                            +        ( v(k,j+3,i) + v(k,j-2,i) ) ) * adv_mom_5
-                  diss_n(k) = - ABS( v_comp(k) - gv ) * (                     &
-                              10.0 * ( v(k,j+1,i) - v(k,j,i)   )              &
-                            -  5.0 * ( v(k,j+2,i) - v(k,j-1,i) )              &
-                            +        ( v(k,j+3,i) - v(k,j-2,i) ) ) *adv_mom_5
-
-               ENDDO
-            ENDIF
-            
-            DO  k = nzb_v_inner(j,i)+1, nzt
-               tend(k,j,i) = tend(k,j,i) - (                                  &
-              ( flux_r(k) + diss_r(k)                                         &
-            -   swap_flux_x_local_v(k,j) - swap_diss_x_local_v(k,j)   ) * ddx &
-            + ( flux_n(k) + diss_n(k)                                         &
-            -   swap_flux_y_local_v(k) - swap_diss_y_local_v(k)       ) * ddy )
-                  
-               swap_flux_x_local_v(k,j) = flux_r(k)   
-               swap_diss_x_local_v(k,j) = diss_r(k)
-               swap_flux_y_local_v(k)   = flux_n(k)     
-               swap_diss_y_local_v(k)   = diss_n(k)   
-
-               sums_vs2_ws_l(k,tn) = sums_vs2_ws_l(k,tn)                             &
-                  + ( flux_n(k) * ( v_comp(k) - 2.0 * hom(k,1,2,0) )           &
-                  / ( v_comp(k) - gv + 1.0E-20 )                               &
-                  +   diss_n(k) * ABS( v_comp(k) - 2.0 * hom(k,1,2,0) )        &
-                  / ( ABS( v_comp(k) - gv ) + 1.0E-20 ) )                      &
-                  *   weight_substep(intermediate_timestep_count)
-            ENDDO
-            sums_vs2_ws_l(nzb_v_inner(j,i),tn) = sums_vs2_ws_l(nzb_v_inner(j,i)+1,tn)
-         ENDDO
-       ENDDO
-!
-!--   Vertical advection, degradation of order near surface and top. 
-!--   The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--   statistical evaluation the top flux at the surface should be 0
-       DO i = nxl, nxr
-          DO  j = nysv, nyn
-!          
-!--         The fluxes flux_d and diss_d at the surface are 0. Due to static
-!--         reasons the top flux at the surface should be 0.
-             flux_t(nzb_v_inner(j,i)) = 0.0
-             diss_t(nzb_v_inner(j,i)) = 0.0
-             k      = nzb_v_inner(j,i)+1             
-             flux_d = flux_t(k-1)
-             diss_d = diss_t(k-1)
-!
-!--          2nd order scheme (bottom)
-             w_comp    = w(k,j-1,i) + w(k,j,i)
-             flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
-             diss_t(k) = diss_2nd( v(k+2,j,i), v(k+1,j,i), v(k,j,i),          &
-                                   0.0, 0.0, w_comp, 0.25, ddzw(k) )
-
-             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
-                                       -   flux_d    - diss_d       ) * ddzw(k)
-!
-!--          WS3 as an intermediate step (bottom)
-             k          = nzb_v_inner(j,i) + 2
-             flux_d     = flux_t(k-1)
-             diss_d     = diss_t(k-1)
-             w_comp     = w(k,j-1,i) + w(k,j,i)
-             flux_t(k) = w_comp * (                                           &
-                         7.0 * ( v(k+1,j,i) + v(k,j,i) )                      &
-                       -       ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
-             diss_t(k) = - ABS( w_comp ) * (                                  &
-                         3.0 * ( v(k+1,j,i) - v(k,j,i) )                      & 
-                       -       ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
-                         
-             tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)              &
-                                       -   flux_d    - diss_d       ) * ddzw(k)
-
-!
-!-- WS5
-             DO  k = nzb_v_inner(j,i)+3, nzt-2
-                flux_d = flux_t(k-1)
-                diss_d = diss_t(k-1)
-                w_comp = w(k,j-1,i) + w(k,j,i)
-                flux_t(k) = w_comp * (                                        &
-                            37.0 * ( v(k+1,j,i) + v(k,j,i)   )                &
-                          -  8.0 * ( v(k+2,j,i) + v(k-1,j,i) )                &
-                          +        ( v(k+3,j,i) + v(k-2,j,i) ) ) * adv_mom_5
-                diss_t(k) = - ABS( w_comp ) * (                               &
-                            10.0 * ( v(k+1,j,i) - v(k,j,i)   )                & 
-                          -  5.0 * ( v(k+2,j,i) - v(k-1,j,i) )                &
-                          +        ( v(k+3,j,i) - v(k-2,j,i) ) ) * adv_mom_5
-                            
-                tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)           &
-                                          -   flux_d    - diss_d    ) * ddzw(k)
-            ENDDO
-!
-!--         WS3 as an intermediate step (top)
-            k     = nzt-1 
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            w_comp    = w(k,j-1,i) + w(k,j,i)
-            flux_t(k) = w_comp * (                                            &
-                        7.0 * ( v(k+1,j,i) + v(k,j,i)   )                     &
-                      -       ( v(k+2,j,i) + v(k-1,j,i) ) ) * adv_mom_3
-            diss_t(k) = - ABS( w_comp ) * (                                   &
-                        3.0 * ( v(k+1,j,i) - v(k,j,i)   )                     & 
-                      -       ( v(k+2,j,i) - v(k-1,j,i) ) ) * adv_mom_3
-                         
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d     ) * ddzw(k)
-!
-!--         2nd order scheme (top)
-            k         = nzt
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            w_comp    = w(k,j-1,i) + w(k,j,i)
-            flux_t(k) = w_comp * ( v(k+1,j,i) + v(k,j,i) ) * 0.25
-            diss_t(k) = diss_2nd( v(nzt+1,j,i), v(nzt+1,j,i), v(k,j,i),        &
-                                  v(k-1,j,i), v(k-2,j,i), w_comp, 0.25, ddzw(k))
-
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)                &
-                                      -   flux_d    - diss_d       ) * ddzw(k)
-!
-!-          At last vertical momentum flux is accumulated.
-            DO  k = nzb_v_inner(j,i), nzt
-               sums_wsvs_ws_l(k,tn) = sums_wsvs_ws_l(k,tn)                           &
-                               + ( flux_t(k) + diss_t(k) )                     &
-                               *   weight_substep(intermediate_timestep_count)
-            ENDDO
-            sums_vs2_ws_l(nzb_v_inner(j,i),tn) = sums_vs2_ws_l(nzb_v_inner(j,i)+1,tn)
-          ENDDO
-       ENDDO
+       sums_vs2_ws_l(nzb,tn) = sums_vs2_ws_l(nzb+1,tn)
+
 
     END SUBROUTINE advec_v_ws
@@ -2846,9 +2993,10 @@
        IMPLICIT NONE
 
-       INTEGER ::  i, j, k, tn = 0
-       REAL    ::  gu, gv, flux_d, diss_d, u_comp, v_comp, w_comp
-       REAL    ::  flux_t(nzb:nzt+1), diss_t(nzb:nzt+1)
-       REAL, DIMENSION(nzb:nzt+1)  ::  flux_r, diss_r, flux_n, diss_n
-       REAL, DIMENSION(nzb+1:nzt) :: swap_flux_y_local_w, swap_diss_y_local_w
+       INTEGER ::  i, j, k, tn = 0, k_ppp, k_pp, k_mm
+       REAL    ::  gu, gv, flux_d, diss_d, u_comp, v_comp, w_comp, div
+       REAL, DIMENSION(nzb:nzt)    ::  flux_t, diss_t
+       REAL, DIMENSION(nzb+1:nzt)  ::  flux_r, diss_r, flux_n, diss_n,        &
+                                       swap_flux_y_local_w,                   &
+                                       swap_diss_y_local_w
        REAL, DIMENSION(nzb+1:nzt,nys:nyn) :: swap_flux_x_local_w,             &
                                              swap_diss_x_local_w
@@ -2860,378 +3008,347 @@
        i = nxl
        DO  j = nys, nyn
-       
-          IF ( boundary_flags(j,i) == 6 )  THEN
-          
-             DO  k = nzb_v_inner(j,i)+1, nzt
-                u_comp                   = u(k+1,j,i) + u(k,j,i) - gu
-                swap_flux_x_local_w(k,j) = u_comp *                           &
-                                           (w(k,j,i)+w(k,j,i-1)) * 0.25
-                swap_flux_x_local_w(k,j) = diss_2nd( w(k,j,i+2), w(k,j,i+1),  &
-                                                     w(k,j,i), w(k,j,i-1),    &
-                                                     w(k,j,i-1), u_comp,      &
-                                                     0.25, ddx )
+          DO  k = nzb+1, nzb_max
+
+             u_comp                   = u(k+1,j,i) + u(k,j,i) - gu
+             swap_flux_x_local_w(k,j) = u_comp * (                             &
+                          ( 37.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +     7.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( w(k,j,i)   + w(k,j,i-1) )               &
+                   -      (  8.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( w(k,j,i+1) + w(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( w(k,j,i+2) + w(k,j,i-3) )               &
+                                                 )
+
+               swap_diss_x_local_w(k,j) = - ABS( u_comp ) * (                  &
+                          ( 10.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +     3.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                       +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1 &
+                          ) *                                                  &
+                                     ( w(k,j,i)   - w(k,j,i-1) )               &
+                   -      (  5.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                       +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3 &
+                          ) *                                                  &
+                                     ( w(k,j,i+1) - w(k,j,i-2) )               &
+                   +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5 &
+                          ) *                                                  &
+                                     ( w(k,j,i+2) - w(k,j,i-3) )               &
+                                                            )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             u_comp                   = u(k+1,j,i) + u(k,j,i) - gu
+             swap_flux_x_local_w(k,j) = u_comp * (                             &
+                            37.0 * ( w(k,j,i) + w(k,j,i-1)   )                 &
+                          -  8.0 * ( w(k,j,i+1) + w(k,j,i-2) )                 &
+                          +        ( w(k,j,i+2) + w(k,j,i-3) ) ) * adv_mom_5
+             swap_diss_x_local_w(k,j) = - ABS( u_comp ) * (                    &
+                            10.0 * ( w(k,j,i) - w(k,j,i-1)   )                 &
+                          -  5.0 * ( w(k,j,i+1) - w(k,j,i-2) )                 &
+                          +        ( w(k,j,i+2) - w(k,j,i-3) ) ) * adv_mom_5
+
+          ENDDO
+
+       ENDDO
+
+       DO i = nxl, nxr
+
+          j = nys
+          DO  k = nzb+1, nzb_max
+
+             v_comp                 = v(k+1,j,i) + v(k,j,i) - gv
+             swap_flux_y_local_w(k) = v_comp * (                              &
+                         ( 37.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +     7.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( w(k,j,i)   + w(k,j-1,i) )              &
+                  -      (  8.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( w(k,j+1,i) + w(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( w(k,j+2,i) + w(k,j-3,i) )              &
+                                               )
+
+             swap_diss_y_local_w(k) = - ABS( v_comp ) * (                     &
+                         ( 10.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +     3.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                      +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1 &
+                         ) *                                                  &
+                                     ( w(k,j,i)   - w(k,j-1,i) )              &
+                  -      (  5.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                      +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3 &
+                         ) *                                                  &
+                                     ( w(k,j+1,i) - w(k,j-2,i) )              &
+                  +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5 &
+                         ) *                                                  &
+                                     ( w(k,j+2,i) - w(k,j-3,i) )              &
+                                                        )
+
+          ENDDO
+
+          DO  k = nzb_max+1, nzt
+
+             v_comp                 = v(k+1,j,i) + v(k,j,i) - gv
+             swap_flux_y_local_w(k) = v_comp * (                              &
+                           37.0 * ( w(k,j,i) + w(k,j-1,i)   )                 &
+                         -  8.0 * ( w(k,j+1,i) +w(k,j-2,i)  )                 &
+                         +        ( w(k,j+2,i) + w(k,j-3,i) ) ) * adv_mom_5
+             swap_diss_y_local_w(k) = - ABS( v_comp ) * (                     &
+                           10.0 * ( w(k,j,i) - w(k,j-1,i)   )                 &
+                         -  5.0 * ( w(k,j+1,i) - w(k,j-2,i) )                 &
+                         +        ( w(k,j+2,i) - w(k,j-3,i) ) ) * adv_mom_5
+
+          ENDDO
+
+          DO  j = nys, nyn
+
+             flux_t(0) = 0.0
+             diss_t(0) = 0.0
+             flux_d    = 0.0
+             diss_d    = 0.0
+
+             DO  k = nzb+1, nzb_max
+
+                u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                flux_r(k) = u_comp * (                                       &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j,i+1) + w(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j,i+2) + w(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j,i+3) + w(k,j,i-2) )                 &
+                                     )
+
+                diss_r(k) = - ABS( u_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),27,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j,i+1) - w(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),28,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j,i+2) - w(k,j,i-1) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),29,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j,i+3) - w(k,j,i-2) )                 &
+                                              )
+
+                v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                flux_n(k) = v_comp * (                                       &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j+1,i) + w(k,j,i)   )                 &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j+2,i) + w(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j+3,i) + w(k,j-2,i) )                 &
+                                     )
+
+                diss_n(k) = - ABS( v_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),30,1) * adv_mom_1    &
+                     ) *                                                     &
+                                 ( w(k,j+1,i) - w(k,j,i)  )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),31,1) * adv_mom_3    &
+                     ) *                                                     &
+                                 ( w(k,j+2,i) - w(k,j-1,i) )                 &
+              +      (        IBITS(wall_flags_0(k,j,i),32,1) * adv_mom_5    &
+                     ) *                                                     &
+                                 ( w(k,j+3,i) - w(k,j-2,i) )                 &
+                                              )
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),35,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),33,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),35,1)
+
+                w_comp    = w(k+1,j,i) + w(k,j,i)
+                flux_t(k) = w_comp  * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)  + w(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  + w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) + w(k_mm,j,i) )                  &
+                                       )
+
+                diss_t(k) = - ABS( w_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)   - w(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  - w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) - w(k_mm,j,i) )                  &
+                                               )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div = ( ( u_comp + gu - ( u(k+1,j,i) + u(k,j,i)   ) ) * ddx  &
+                    +   ( v_comp + gv - ( v(k+1,j,i) + v(k,j,i)   ) ) * ddy  &
+                    +   ( w_comp      - ( w(k,j,i)   + w(k-1,j,i) ) )        &
+                                                                 * ddzu(k+1) &
+                      ) * 0.5
+
+                tend(k,j,i) = tend(k,j,i) - (                                 &
+                      ( flux_r(k) + diss_r(k)                                 &
+                    -   swap_flux_x_local_w(k,j) - swap_diss_x_local_w(k,j)   &
+                      ) * ddx                                                 &
+                    + ( flux_n(k) + diss_n(k)                                 &
+                    -   swap_flux_y_local_w(k)   - swap_diss_y_local_w(k)     &
+                      ) * ddy                                                 &
+                    + ( flux_t(k) + diss_t(k)                                 &
+                    -   flux_d    - diss_d                                    &
+                      ) * ddzu(k+1)                                           &
+                                            )  + div * w(k,j,i)
+
+                swap_flux_x_local_w(k,j) = flux_r(k)
+                swap_diss_x_local_w(k,j) = diss_r(k)
+                swap_flux_y_local_w(k)   = flux_n(k)
+                swap_diss_y_local_w(k)   = diss_n(k)
+                flux_d                   = flux_t(k)
+                diss_d                   = diss_t(k)
+
+                sums_ws2_ws_l(k,tn)  = sums_ws2_ws_l(k,tn)                    &
+                               + ( flux_t(k) + diss_t(k) )                    &
+                               *   weight_substep(intermediate_timestep_count)
+
              ENDDO
-             
-          ELSE
-          
-             DO  k = nzb_v_inner(j,i)+1, nzt
-                u_comp                   = u(k+1,j,i) + u(k,j,i) - gu
-                swap_flux_x_local_w(k,j) = u_comp * (                          &
-                                           37.0 * ( w(k,j,i) + w(k,j,i-1)   )  &
-                                         -  8.0 * ( w(k,j,i+1) + w(k,j,i-2) )  &
-                                         +        ( w(k,j,i+2) + w(k,j,i-3) ) )&
-                                         * adv_mom_5
-                swap_diss_x_local_w(k,j) = - ABS( u_comp ) * (                 &
-                                           10.0 * ( w(k,j,i) - w(k,j,i-1)   )  &
-                                         -  5.0 * ( w(k,j,i+1) - w(k,j,i-2) )  &
-                                         +        ( w(k,j,i+2) - w(k,j,i-3) ) )&
-                                         * adv_mom_5
+
+             DO  k = nzb_max+1, nzt
+
+                u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
+                flux_r(k) = u_comp * (                                      &
+                      37.0 * ( w(k,j,i+1) + w(k,j,i)   )                    &
+                    -  8.0 * ( w(k,j,i+2) + w(k,j,i-1) )                    &
+                    +        ( w(k,j,i+3) + w(k,j,i-2) ) ) * adv_mom_5
+
+                diss_r(k) = - ABS( u_comp ) * (                             &
+                      10.0 * ( w(k,j,i+1) - w(k,j,i)   )                    &
+                    -  5.0 * ( w(k,j,i+2) - w(k,j,i-1) )                    &
+                    +        ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
+
+                v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
+                flux_n(k) = v_comp * (                                      &
+                      37.0 * ( w(k,j+1,i) + w(k,j,i)   )                    &
+                    -  8.0 * ( w(k,j+2,i) + w(k,j-1,i) )                    &
+                    +        ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
+
+                diss_n(k) = - ABS( v_comp ) * (                             &
+                      10.0 * ( w(k,j+1,i) - w(k,j,i)   )                    &
+                    -  5.0 * ( w(k,j+2,i) - w(k,j-1,i) )                    &
+                    +        ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
+!
+!--             k index has to be modified near bottom and top, else array
+!--             subscripts will be exceeded.
+                k_ppp = k + 3 * IBITS(wall_flags_0(k,j,i),35,1)
+                k_pp  = k + 2 * ( 1 - IBITS(wall_flags_0(k,j,i),33,1)  )
+                k_mm  = k - 2 * IBITS(wall_flags_0(k,j,i),35,1)
+
+                w_comp    = w(k+1,j,i) + w(k,j,i)
+                flux_t(k) = w_comp  * (                                      &
+                     ( 37.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     7.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)  + w(k,j,i)     )                  &
+              -      (  8.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  + w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) + w(k_mm,j,i) )                  &
+                                      )
+
+                diss_t(k) = - ABS( w_comp ) * (                              &
+                     ( 10.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +     3.0 * IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                  +           IBITS(wall_flags_0(k,j,i),33,1) * adv_mom_1    &
+                     ) *                                                     &
+                             ( w(k+1,j,i)   - w(k,j,i)    )                  &
+              -      (  5.0 * IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                  +           IBITS(wall_flags_0(k,j,i),34,1) * adv_mom_3    &
+                     ) *                                                     &
+                             ( w(k_pp,j,i)  - w(k-1,j,i)  )                  &
+              +      (        IBITS(wall_flags_0(k,j,i),35,1) * adv_mom_5    &
+                     ) *                                                     &
+                             ( w(k_ppp,j,i) - w(k_mm,j,i) )                  &
+                                              )
+!
+!--             Calculate the divergence of the velocity field. A respective
+!--             correction is needed to overcome numerical instabilities caused
+!--             by a not sufficient reduction of divergences near topography.
+                div = ( ( u_comp + gu - ( u(k+1,j,i) + u(k,j,i)   ) ) * ddx  &
+                    +   ( v_comp + gv - ( v(k+1,j,i) + v(k,j,i)   ) ) * ddy  &
+                    +   ( w_comp      - ( w(k,j,i)   + w(k-1,j,i) ) )        &
+                                                                 * ddzu(k+1) &
+                      ) * 0.5
+
+                tend(k,j,i) = tend(k,j,i) - (                                 &
+                      ( flux_r(k) + diss_r(k)                                 &
+                    -   swap_flux_x_local_w(k,j) - swap_diss_x_local_w(k,j)   &
+                      ) * ddx                                                 &
+                    + ( flux_n(k) + diss_n(k)                                 &
+                    -   swap_flux_y_local_w(k)   - swap_diss_y_local_w(k)     &
+                      ) * ddy                                                 &
+                    + ( flux_t(k) + diss_t(k)                                 &
+                    -   flux_d    - diss_d                                    &
+                      ) * ddzu(k+1)                                           &
+                                            )  + div * w(k,j,i)
+
+                swap_flux_x_local_w(k,j) = flux_r(k)
+                swap_diss_x_local_w(k,j) = diss_r(k)
+                swap_flux_y_local_w(k)   = flux_n(k)
+                swap_diss_y_local_w(k)   = diss_n(k)
+                flux_d                   = flux_t(k)
+                diss_d                   = diss_t(k)
+
+                sums_ws2_ws_l(k,tn)  = sums_ws2_ws_l(k,tn)                    &
+                               + ( flux_t(k) + diss_t(k) )                    &
+                               *   weight_substep(intermediate_timestep_count)
+
              ENDDO
-             
-          ENDIF
-          
+          ENDDO
        ENDDO
 
-       DO i = nxl, nxr
-         j = nys
-         IF ( boundary_flags(j,i) == 8 )  THEN
-         
-            DO  k = nzb_v_inner(j,i)+1, nzt
-               v_comp                 = v(k+1,j,i) + v(k,j,i) - gv
-               swap_flux_y_local_w(k) = v_comp *                               &
-                                        ( w(k,j,i) + w(k,j-1,i) ) * 0.25
-               swap_diss_y_local_w(k) = diss_2nd( w(k,j+2,i), w(k,j+1,i),      &
-                                                  w(k,j,i), w(k,j-1,i),        &
-                                                  w(k,j-1,i), v_comp, 0.25, ddy)
-            ENDDO
-            
-         ELSE
-         
-            DO  k = nzb_v_inner(j,i)+1, nzt            
-               v_comp                 = v(k+1,j,i) + v(k,j,i) - gv
-               swap_flux_y_local_w(k) = v_comp * (                             &
-                                        37.0 * ( w(k,j,i) + w(k,j-1,i)  )      &
-                                      -  8.0 * ( w(k,j+1,i) +w(k,j-2,i) )      &
-                                      +        ( w(k,j+2,i) +w(k,j-3,i) ) )    &
-                                      * adv_mom_5
-               swap_diss_y_local_w(k) = - ABS( v_comp ) * (                    &
-                                        10.0 * ( w(k,j,i) - w(k,j-1,i)   )     &
-                                      -  5.0 * ( w(k,j+1,i) - w(k,j-2,i) )     &
-                                      +        ( w(k,j+2,i) - w(k,j-3,i) ) )   &
-                                      * adv_mom_5
-            ENDDO
-            
-         ENDIF
-         
-         DO  j = nys, nyn
-
-            IF ( boundary_flags(j,i) /= 0 )  THEN
-!       
-!--            Degrade the order for Dirichlet bc. at the outflow boundary
-               SELECT CASE ( boundary_flags(j,i) )
-         
-                  CASE ( 1 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                        flux_r(k) = u_comp * (                                &
-                                    7.0 * ( w(k,j,i+1) + w(k,j,i)   )         &
-                                  -       ( w(k,j,i+2) + w(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                        diss_r(k) = -ABS( u_comp ) * (                        &
-                                    3.0 * ( w(k,j,i+1) - w(k,j,i)   )         & 
-                                  -       ( w(k,j,i+2) - w(k,j,i-1) ) )       &
-                                  * adv_mom_3  
-                     ENDDO
-               
-                  CASE ( 2 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                        flux_r(k) = u_comp * ( w(k,j,i+1) + w(k,j,i) ) * 0.25 
-                        diss_r(k) = diss_2nd( w(k,j,i+1), w(k,j,i+1),         &
-                                              w(k,j,i), w(k,j,i-1),           &
-                                              w(k,j,i-2), u_comp, 0.25, ddx )
-                     ENDDO
-               
-                  CASE ( 3 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                        flux_n(k) = v_comp * (                                &
-                                     7.0 * ( w(k,j+1,i) + w(k,j,i)   )        &
-                                  -        ( w(k,j+2,i) + w(k,j-1,i) ) )      &
-                                  * adv_mom_3
-                        diss_n(k) = -ABS( v_comp ) * (                        &
-                                    3.0 * ( w(k,j+1,i) - w(k,j,i)   )         & 
-                                  -       ( w(k,j+2,i) - w(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                     ENDDO
-               
-                  CASE ( 4 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                        flux_n(k) = v_comp * ( w(k,j+1,i) + w(k,j,i) ) * 0.25 
-                        diss_n(k) = diss_2nd( w(k,j+1,i), w(k,j+1,i),         &
-                                              w(k,j,i), w(k,j-1,i),           &
-                                              w(k,j-2,i), v_comp, 0.25, ddy )
-                     ENDDO
-               
-                  CASE ( 5 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu 
-                        flux_r(k) = u_comp * (                                &
-                                    7.0 * ( w(k,j,i+1) + w(k,j,i)   )         &
-                                  -       ( w(k,j,i+2) + w(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS( u_comp ) * (                       &
-                                    3.0 * ( w(k,j,i+1) - w(k,j,i) )           & 
-                                  -       ( w(k,j,i+2) - w(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                     ENDDO
-               
-                  CASE ( 6 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                        flux_r(k) = u_comp *(                                 &
-                                    7.0 * ( w(k,j,i+1) + w(k,j,i)   )         &
-                                  -       ( w(k,j,i+2) + w(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                        diss_r(k) = - ABS( u_comp ) * (                       &
-                                    3.0 * ( w(k,j,i+1) - w(k,j,i) )           & 
-                                  -       ( w(k,j,i+2) - w(k,j,i-1) ) )       &
-                                  * adv_mom_3
-                     ENDDO
-               
-                  CASE ( 7 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                        flux_n(k) = v_comp *(                                 &
-                                    7.0 * ( w(k,j+1,i) + w(k,j,i)   )         &
-                                  -       ( w(k,j+2,i) + w(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS( v_comp ) * (                       &
-                                    3.0 * ( w(k,j+1,i) - w(k,j,i)   )         & 
-                                  -       ( w(k,j+2,i) - w(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                      ENDDO
-                
-                  CASE ( 8 )
-                     DO  k = nzb_w_inner(j,i)+1, nzt
-                        v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                        flux_n(k) = v_comp * (                                &
-                                    7.0 * ( w(k,j+1,i) + w(k,j,i)   )         &
-                                  -       ( w(k,j+2,i) + w(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                        diss_n(k) = - ABS( v_comp ) * (                       &
-                                    3.0 * ( w(k,j+1,i) - w(k,j,i) )           & 
-                                  -       ( w(k,j+2,i) - w(k,j-1,i) ) )       &
-                                  * adv_mom_3
-                
-                     ENDDO  
-               
-                  CASE DEFAULT
-            
-               END SELECT
-!         
-!--            Compute the crosswise 5th order fluxes at the outflow
-               IF ( boundary_flags(j,i) == 1 .OR. boundary_flags(j,i) == 2 .OR.&
-                    boundary_flags(j,i) == 5 .OR. boundary_flags(j,i) == 6 )   &
-               THEN
-         
-                  DO  k = nzb_w_inner(j,i)+1, nzt
-                     v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                     flux_n(k) = v_comp * (                                   &
-                                 37.0 * ( w(k,j+1,i) + w(k,j,i)   )           &
-                               -  8.0 * ( w(k,j+2,i) + w(k,j-1,i) )           &
-                               +        ( w(k,j+3,i) + w(k,j-2,i) ) )         &
-                               * adv_mom_5
-                     diss_n(k) = - ABS( v_comp ) * (                          &
-                                 10.0 * ( w(k,j+1,i) - w(k,j,i)   )           &
-                               -  5.0 * ( w(k,j+2,i) - w(k,j-1,i) )           &
-                               +        ( w(k,j+3,i) - w(k,j-2,i) ) )         &
-                               * adv_mom_5
-                  ENDDO
-            
-               ELSE
-         
-                  DO  k = nzb_w_inner(j,i)+1, nzt         
-                     u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                     flux_r(k) = u_comp * (                                   &
-                                 37.0 * ( w(k,j,i+1) + w(k,j,i) )             &
-                               -  8.0 * ( w(k,j,i+2) + w(k,j,i-1) )           &
-                               +        ( w(k,j,i+3) + w(k,j,i-2) ) )         &
-                               * adv_mom_5
-                     diss_r(k) = - ABS( u_comp ) * (                          &
-                                 10.0 * ( w(k,j,i+1) - w(k,j,i)   )           &
-                               -  5.0 * ( w(k,j,i+2) - w(k,j,i-1) )           &
-                               +        ( w(k,j,i+3) - w(k,j,i-2) ) )         &
-                               * adv_mom_5
-                  ENDDO
-            
-               ENDIF
-         
-             ELSE
-!       
-!--            Compute the fifth order fluxes for the interior of PE domain.               
-               DO  k = nzb_w_inner(j,i)+1, nzt
-                  u_comp    = u(k+1,j,i+1) + u(k,j,i+1) - gu
-                  flux_r(k) = u_comp * (                                      &
-                              37.0 * ( w(k,j,i+1) + w(k,j,i)   )              &
-                            -  8.0 * ( w(k,j,i+2) + w(k,j,i-1) )              &
-                            +        ( w(k,j,i+3) + w(k,j,i-2) ) )            &
-                            * adv_mom_5
-                  diss_r(k) = - ABS( u_comp ) * (                             &
-                              10.0 * ( w(k,j,i+1) - w(k,j,i)   )              &
-                            -  5.0 * ( w(k,j,i+2) - w(k,j,i-1) )              &
-                            +        ( w(k,j,i+3) - w(k,j,i-2) ) ) * adv_mom_5
-                 
-                  v_comp    = v(k+1,j+1,i) + v(k,j+1,i) - gv
-                  flux_n(k) = v_comp * (                                      &
-                              37.0 * ( w(k,j+1,i) + w(k,j,i)   )              &
-                            -  8.0 * ( w(k,j+2,i) + w(k,j-1,i) )              &
-                            +        ( w(k,j+3,i) + w(k,j-2,i) ) ) * adv_mom_5
-                  diss_n(k) = - ABS( v_comp ) * (                             &
-                              10.0 * ( w(k,j+1,i) - w(k,j,i)   )              &
-                            -  5.0 * ( w(k,j+2,i) - w(k,j-1,i) )              &
-                            +        ( w(k,j+3,i) - w(k,j-2,i) ) ) * adv_mom_5
-               ENDDO 
-         
-            ENDIF
-            
-            DO  k = nzb_v_inner(j,i)+1, nzt
-               tend(k,j,i) = tend(k,j,i) - (                                  &
-              ( flux_r(k) + diss_r(k)                                         &
-            -   swap_flux_x_local_w(k,j) - swap_diss_x_local_w(k,j)  ) * ddx  &
-            + ( flux_n(k) + diss_n(k)                                         &
-            -   swap_flux_y_local_w(k) - swap_diss_y_local_w(k)      ) * ddy )
-               
-               swap_flux_x_local_w(k,j) = flux_r(k)
-               swap_diss_x_local_w(k,j) = diss_r(k)
-               swap_flux_y_local_w(k)   = flux_n(k)  
-               swap_diss_y_local_w(k)   = diss_n(k)
-            ENDDO
-         ENDDO
-       ENDDO
-
-!
-!--    Vertical advection, degradation of order near surface and top. 
-!--    The fluxes flux_d and diss_d at the surface are 0. Due to reasons of
-!--    statistical evaluation the top flux at the surface should be 0
-       DO i = nxl, nxr
-          DO  j = nys, nyn
-            k      = nzb_w_inner(j,i)+1
-            flux_d = w(k-1,j,i) * ( w(k,j,i) + w(k-1,j,i))
-            flux_t(k-1) = flux_d
-            diss_d = 0.0
-            diss_t(k-1) = diss_d
-!
-!--         2nd order scheme (bottom)           
-            w_comp = w(k+1,j,i) + w(k,j,i)
-            flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
-            diss_t(k) = diss_2nd( w(k+2,j,i), w(k+1,j,i), w(k,j,i), 0.0, 0.0, &
-                                  w_comp, 0.25, ddzu(k+1) )
-
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d      ) * ddzu(k+1)
-!                                 
-!--        WS3 as an intermediate step (bottom)
-            k         = nzb_w_inner(j,i) + 2
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            w_comp    = w(k+1,j,i) + w(k,j,i)
-            flux_t(k) = w_comp * (                                            &
-                        7.0 * ( w(k+1,j,i) + w(k,j,i) )                       &
-                      -       ( w(k+2,j,i) + w(k-1,j,i) ) ) * adv_mom_3
-            diss_t(k) = - ABS( w_comp ) * (                                   &
-                        3.0 * ( w(k+1,j,i) - w(k,j,i) )                       & 
-                      -       ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
-                        
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d      ) * ddzu(k+1)
-!
-!--         WS5
-            DO  k = nzb_v_inner(j,i)+3, nzt-2
-               flux_d = flux_t(k-1)
-               diss_d = diss_t(k-1)
-
-               w_comp = w(k+1,j,i) + w(k,j,i)
-               flux_t(k) = w_comp * (                                          &
-                           37.0 * ( w(k+1,j,i) + w(k,j,i)   )                  &
-                         -  8.0 * ( w(k+2,j,i) + w(k-1,j,i) )                  &
-                         +        ( w(k+3,j,i) + w(k-2,j,i) ) ) * adv_mom_5
-               diss_t(k) = - ABS( w_comp ) * (                                 &
-                           10.0 * ( w(k+1,j,i) - w(k,j,i)   )                  & 
-                         -  5.0 * ( w(k+2,j,i) - w(k-1,j,i) )                  &
-                         +        ( w(k+3,j,i) - w(k-2,j,i) ) ) * adv_mom_5
-
-               tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)             &
-                                         -   flux_d    - diss_d    ) * ddzu(k+1)
-            ENDDO
-!                                 
-!--         WS3 as an intermediate step (top)
-            k         = nzt-1
-            flux_d    = flux_t(k-1)
-            diss_d    = diss_t(k-1)
-            w_comp    = w(k+1,j,i)+w(k,j,i)
-            flux_t(k) = w_comp * (                                            &
-                        7.0 * ( w(k+1,j,i) + w(k,j,i)   )                     &
-                      -       ( w(k+2,j,i) + w(k-1,j,i) ) ) * adv_mom_3
-            diss_t(k) = - ABS( w_comp ) * (                                   &
-                          3.0 * ( w(k+1,j,i) - w(k,j,i)   )                   & 
-                      -       ( w(k+2,j,i) - w(k-1,j,i) ) ) * adv_mom_3
-                        
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d      ) * ddzu(k+1)
-!
-!--         2nd order scheme (top) 
-            k = nzt
-            flux_d = flux_t(k-1)
-            diss_d = diss_t(k-1)
-            w_comp = w(k+1,j,i) + w(k,j,i)
-            flux_t(k) = w_comp * ( w(k+1,j,i) + w(k,j,i) ) * 0.25
-            diss_t(k) = diss_2nd( w(nzt+1,j,i), w(nzt+1,j,i), w(k,j,i),       &
-                                  w(k-1,j,i), w(k-2,j,i), w_comp,             &
-                                  0.25, ddzu(k+1) ) 
-
-            tend(k,j,i) = tend(k,j,i) - ( flux_t(k) + diss_t(k)               &
-                                      -   flux_d    - diss_d      ) * ddzu(k+1)
-!
-!--         at last vertical momentum flux is accumulated
-            DO  k = nzb_w_inner(j,i), nzt
-               sums_ws2_ws_l(k,tn)  = sums_ws2_ws_l(k,tn)                            &
-                               + ( flux_t(k) + diss_t(k) )                     &
-                               *   weight_substep(intermediate_timestep_count)
-            ENDDO
-
-          ENDDO
-       ENDDO
-
     END SUBROUTINE advec_w_ws
-    
- 
- 
-    SUBROUTINE local_diss_ij( i, j, ar, var_char )
-    
-    END SUBROUTINE local_diss_ij
-
-    SUBROUTINE local_diss( ar )
-
-
-    END SUBROUTINE local_diss
-    
-!   
-!-- Computation of 2nd order dissipation. This numerical dissipation is 
-!-- necessary to keep a stable numerical solution in regions where the
-!-- order of the schemes is degraded.
- 
-     REAL FUNCTION diss_2nd( v2, v1, v0, vm1, vm2, vel_comp, factor, grid )   &
-                            RESULT( dissip )
-        
-       IMPLICIT NONE 
-        
-       REAL, INTENT(IN)  :: v2, v1, v0, vm1, vm2, vel_comp, factor,            &
-                            grid
-
-       dissip = 0.0
-              
-    END FUNCTION diss_2nd
 
  END MODULE advec_ws
Index: palm/trunk/SOURCE/check_parameters.f90
===================================================================
--- palm/trunk/SOURCE/check_parameters.f90	(revision 859)
+++ palm/trunk/SOURCE/check_parameters.f90	(revision 861)
@@ -4,5 +4,6 @@
 ! Current revisions:
 ! -----------------
-!
+! Check for topography and ws-scheme removed.
+! Check for loop_optimization = 'vector' and ws-scheme removed.
 !
 ! Former revisions:
@@ -456,8 +457,9 @@
     IF ( topography /= 'flat' )  THEN
        action = ' '
-       IF ( scalar_advec /= 'pw-scheme' )  THEN
+       IF ( scalar_advec /= 'pw-scheme' .AND. scalar_advec /= 'ws-scheme')  THEN
           WRITE( action, '(A,A)' )  'scalar_advec = ', scalar_advec
        ENDIF
-       IF ( momentum_advec /= 'pw-scheme' )  THEN
+       IF ( momentum_advec /= 'pw-scheme' .AND. momentum_advec /= 'ws-scheme' ) &
+       THEN
           WRITE( action, '(A,A)' )  'momentum_advec = ', momentum_advec
        ENDIF
@@ -488,13 +490,4 @@
           CALL message( 'check_parameters', 'PA0014', 1, 2, 0, 6, 0 )
        ENDIF
-       IF ( momentum_advec == 'ws-scheme' .OR. scalar_advec == 'ws-scheme' ) &
-       THEN
-          message_string = 'topography is still not allowed with ' // &
-                           'momentum_advec = "' // TRIM( momentum_advec ) //  &
-                           '"or scalar_advec = "' // TRIM( scalar_advec ) //'"'
-   ! message number still needs modification
-           CALL message( 'check_parameters', 'PA0341', 1, 2, 0, 6, 0 )
-       END IF
-          
 !
 !--    In case of non-flat topography, check whether the convention how to 
@@ -739,5 +732,4 @@
     IF ( collision_kernel(6:9) == 'fast' )  use_kernel_tables = .TRUE.
 
-
     IF ( TRIM( initializing_actions ) /= 'read_restart_data'  .AND.  &
          TRIM( initializing_actions ) /= 'cyclic_fill' )  THEN
@@ -1406,12 +1398,4 @@
           CALL message( 'check_parameters', 'PA0053', 1, 2, 0, 6, 0 )
        ENDIF
-       IF ( (scalar_advec == 'ws-scheme' .OR. momentum_advec == 'ws-scheme' ) &
-          .AND. loop_optimization == 'vector' ) THEN
-          message_string = 'non-cyclic lateral boundaries do not allow ' // &
-                           'loop_optimization = vector and ' //  &
-                           'scalar_advec = "' // TRIM( scalar_advec ) // '"' 
-  ! The error message number still needs modification. 
-          CALL message( 'check_parameters', 'PA0342', 1, 2, 0, 6, 0 )
-       END IF
        IF ( galilei_transformation )  THEN
           message_string = 'non-cyclic lateral boundaries do not allow ' // &
Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 859)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 861)
@@ -4,4 +4,11 @@
 ! Current revisions:
 ! -----------------
+! Set wall_flags_0. The array is needed for degradation in ws-scheme near walls,
+! inflow and outflow boundaries as well as near the bottom and the top of the
+! model domain.
+!
+! Initialization of nzb_s_inner and nzb_w_inner.
+! gls has to be at least nbgp to do not exceed the array bounds of nzb_local
+! while setting wall_flags_0
 !
 ! Former revisions:
@@ -104,4 +111,6 @@
                                              corner_sr, wall_l, wall_n, wall_r,&
                                              wall_s, nzb_local, nzb_tmp
+
+    LOGICAL :: flag_set = .FALSE.
 
     REAL    ::  dx_l, dy_l, dz_stretched
@@ -311,4 +320,5 @@
 !-- the flag arrays needed for the multigrid method
     gls = 2**( maximum_grid_level )
+    IF ( gls < nbgp )  gls = nbgp
 
     ALLOCATE( corner_nl(nys:nyn,nxl:nxr), corner_nr(nys:nyn,nxl:nxr),       &
@@ -589,5 +599,8 @@
 
     END SELECT
-
+!
+!-- Determine the maximum level of topography. Furthermore it is used for
+!-- steering the degradation of order of the applied advection scheme.
+    nzb_max = MAXVAL( nzb_local )
 !
 !-- Consistency checks and index array initialization are only required for
@@ -653,6 +666,6 @@
 !
 !--    Initialize index arrays nzb_s_inner and nzb_w_inner
-       nzb_s_inner = nzb_local(nys-1:nyn+1,nxl-1:nxr+1)
-       nzb_w_inner = nzb_local(nys-1:nyn+1,nxl-1:nxr+1)
+       nzb_s_inner = nzb_local(nysg:nyng,nxlg:nxrg)
+       nzb_w_inner = nzb_local(nysg:nyng,nxlg:nxrg)
 
 !
@@ -1018,5 +1031,5 @@
                 ENDDO
              ENDDO
-          ENDDO 
+          ENDDO
 
 !
@@ -1036,4 +1049,219 @@
 
     ENDIF
+!
+!-- Allocate flags needed for masking walls.
+    ALLOCATE( wall_flags_0(nzb:nzt,nys:nyn,nxl:nxr) )
+    wall_flags_0 = 0
+
+    IF ( scalar_advec == 'ws-scheme' )  THEN
+!
+!--    Set flags to steer the degradation of the advection scheme in advec_ws
+!--    near topography, inflow- and outflow boundaries as well as bottom and
+!--    top of model domain. wall_flags_0 remains zero for all non-prognostic
+!--    grid points.
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_s_inner(j,i)+1, nzt
+!
+!--             scalar - x-direction
+!--             WS1 (0), WS3 (1), WS5 (2)
+                IF ( k <= nzb_s_inner(j,i+1) .OR. ( outflow_l .AND. i == nxl ) &
+                     .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
+                ELSEIF ( k <= nzb_s_inner(j,i+2) .OR. k <= nzb_s_inner(j,i-1)  &
+                         .OR. ( outflow_r .AND. i == nxr-1 )                   &
+                         .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
+                ENDIF
+!
+!--             scalar - y-direction
+!--             WS1 (3), WS3 (4), WS5 (5)
+                IF ( k <= nzb_s_inner(j+1,i) .OR. ( outflow_s .AND. j == nys ) &
+                     .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
+!--             WS3
+                ELSEIF ( k <= nzb_s_inner(j+2,i) .OR. k <= nzb_s_inner(j-1,i)  &
+                        .OR. ( outflow_s .AND. j == nysv )                     &
+                        .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 4 )
+!--             WS5
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 5 )
+                ENDIF
+!
+!--             scalar - z-direction
+!--             WS1 (6), WS3 (7), WS5 (8)
+                flag_set = .FALSE.
+                IF ( k == nzb_s_inner(j,i) + 1 .OR. k == nzt )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 6 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_s_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 7 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_s_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDIF
+
+    IF ( momentum_advec == 'ws-scheme' )  THEN
+!
+!--    Set wall_flags_0 to steer the degradation of the advection scheme in advec_ws
+!--    near topography, inflow- and outflow boundaries as well as bottom and
+!--    top of model domain. wall_flags_0 remains zero for all non-prognostic
+!--    grid points.
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_u_inner(j,i)+1, nzt
+!
+!--             u component - x-direction
+!--             WS1 (9), WS3 (10), WS5 (11)
+                IF ( k <= nzb_u_inner(j,i+1)                                  &
+                     .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 9 )
+                ELSEIF ( k <= nzb_u_inner(j,i+2) .OR. k <= nzb_u_inner(j,i-1) &
+                         .OR. ( outflow_r .AND. i == nxr-1 )                  &
+                         .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 10 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 11 )
+                ENDIF
+!
+!--             u component - y-direction
+!--             WS1 (12), WS3 (13), WS5 (14)
+                IF ( k <= nzb_u_inner(j+1,i) .OR. ( outflow_s .AND. j == nys ) &
+                    .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
+                ELSEIF ( k <= nzb_u_inner(j+2,i) .OR. k <= nzb_u_inner(j-1,i)  &
+                    .OR. ( outflow_s .AND. j == nysv )                         &
+                    .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
+                ENDIF
+!
+!--             u component - z-direction
+!--             WS1 (15), WS3 (16), WS5 (17)
+                flag_set = .FALSE.
+                IF ( k == nzb_u_inner(j,i) + 1 .OR. k == nzt )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 15 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_u_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_u_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 17 )
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_v_inner(j,i)+1, nzt
+!
+!--             v component - x-direction
+!--             WS1 (18), WS3 (19), WS5 (20)
+                IF ( k <= nzb_v_inner(j,i+1) .OR. ( outflow_l .AND. i == nxl ) &
+                    .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
+!--             WS3
+                ELSEIF ( k <= nzb_v_inner(j,i+2) .OR. k <= nzb_v_inner(j,i-1)  &
+                    .OR. ( outflow_r .AND. i == nxr-1 )                        &
+                    .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 19 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 20 )
+                ENDIF
+!
+!--             v component - y-direction
+!--             WS1 (21), WS3 (22), WS5 (23)
+                IF ( k <= nzb_v_inner(j+1,i)                                   &
+                     .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
+                ELSEIF ( k <= nzb_v_inner(j+2,i) .OR. k <= nzb_v_inner(j-1,i)  &
+                         .OR. ( outflow_s .AND. j == nysv )                    &
+                         .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
+                ENDIF
+!
+!--             v component - z-direction
+!--             WS1 (24), WS3 (25), WS5 (26)
+                flag_set = .FALSE.
+                IF ( k == nzb_v_inner(j,i) + 1 .OR. k == nzt )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_v_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 25 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_v_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 26 )
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_w_inner(j,i)+1, nzt
+!
+!--             w component - x-direction
+!--             WS1 (27), WS3 (28), WS5 (29)
+                IF ( k <= nzb_w_inner(j,i+1) .OR. ( outflow_l .AND. i == nxl ) &
+                    .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
+                ELSEIF ( k <= nzb_w_inner(j,i+2) .OR. k <= nzb_w_inner(j,i-1)  &
+                         .OR. ( outflow_r .AND. i == nxr-1 )                   &
+                         .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 28 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i),29 )
+                ENDIF
+!
+!--             w component - y-direction
+!--             WS1 (30), WS3 (31), WS5 (32)
+                IF ( k <= nzb_w_inner(j+1,i) .OR. ( outflow_s .AND. j == nys ) &
+                     .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
+                ELSEIF ( k <= nzb_w_inner(j+2,i) .OR. k <= nzb_w_inner(j-1,i)  &
+                         .OR. ( outflow_s .AND. j == nysv )                    &
+                         .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 31 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 32 )
+                ENDIF
+!
+!--             w component - z-direction
+!--             WS1 (33), WS3 (34), WS5 (35)
+                flag_set = .FALSE.
+                IF ( k == nzb_w_inner(j,i) .OR. k == nzb_w_inner(j,i) + 1      &
+                                           .OR. k == nzt )  THEN
+!
+!--                Please note, at k == nzb_w_inner(j,i) a flag is explictely
+!--                set, although this is not a prognostic level. However, 
+!--                contrary to the advection of u,v and s this is necessary
+!--                because flux_t(nzb_w_inner(j,i)) is used for the tendency
+!--                at k == nzb_w_inner(j,i)+1.
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 33 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_w_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 34 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_w_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 35 )
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+
+    ENDIF
 
 !
Index: palm/trunk/SOURCE/init_pt_anomaly.f90
===================================================================
--- palm/trunk/SOURCE/init_pt_anomaly.f90	(revision 859)
+++ palm/trunk/SOURCE/init_pt_anomaly.f90	(revision 861)
@@ -4,4 +4,6 @@
 ! Current revisions:
 ! -----------------
+!
+! Modification of the amplitude to obtain a visible temperature perturbation. 
 !
 ! Former revisions:
@@ -62,5 +64,5 @@
              radius = SQRT( x**2 + y**2 + z**2 )
              IF ( radius <= rc )  THEN
-                betrag = 5.0 * EXP( -( radius / 2.0 )**2 )
+                betrag = 5.0 * EXP( -( radius * 0.001 / 2.0 )**2 )
              ELSE
                 betrag = 0.0
Index: palm/trunk/SOURCE/modules.f90
===================================================================
--- palm/trunk/SOURCE/modules.f90	(revision 859)
+++ palm/trunk/SOURCE/modules.f90	(revision 861)
@@ -4,5 +4,8 @@
 ! Current revisions:
 ! -----------------
-! 
+! +wall_flags_0
+! -boundary_flags
+! +nzb_max
+! +adv_sca_1, +adv_mom_1
 !
 ! Former revisions:
@@ -422,5 +425,5 @@
 
     REAL    ::  pi = 3.141592654
-    REAL    ::  adv_sca_5, adv_sca_3, adv_mom_5, adv_mom_3
+    REAL    ::  adv_mom_1, adv_mom_3, adv_mom_5, adv_sca_1, adv_sca_3, adv_sca_5
     
 
@@ -913,5 +916,5 @@
                 nxlu, nxr, nxra, nxrg, nx_on_file, nny, ny = 0, ny_a, ny_o,    &
                 nya, nyn, nyna, nyng, nys, nysg, nysv, ny_on_file, nnz, nz = 0,&
-                nza, nzb, nzb_diff, nzt, nzta, nzt_diff               
+                nza, nzb, nzb_diff, nzb_max, nzt, nzta, nzt_diff
 
 
@@ -923,16 +926,20 @@
 
 
-    INTEGER, DIMENSION(:,:), ALLOCATABLE :: boundary_flags,                    &
-                ngp_2dh_outer, ngp_2dh_s_inner, mg_loc_ind, nzb_diff_s_inner,  &
-                nzb_diff_s_outer, nzb_diff_u, nzb_diff_v, nzb_inner, nzb_outer,&
-                nzb_s_inner, nzb_s_outer, nzb_u_inner, nzb_u_outer,            &
-                nzb_v_inner, nzb_v_outer, nzb_w_inner, nzb_w_outer, nzb_2d
+    INTEGER, DIMENSION(:,:), ALLOCATABLE :: ngp_2dh_outer, ngp_2dh_s_inner,    &
+                mg_loc_ind, nzb_diff_s_inner, nzb_diff_s_outer, nzb_diff_u,    &
+                nzb_diff_v, nzb_inner, nzb_outer, nzb_s_inner, nzb_s_outer,    &
+                nzb_u_inner, nzb_u_outer, nzb_v_inner, nzb_v_outer,            &
+                nzb_w_inner, nzb_w_outer, nzb_2d
 
     INTEGER, DIMENSION(:,:,:), POINTER ::  flags
 
-    INTEGER, DIMENSION(:,:,:), ALLOCATABLE, TARGET ::  wall_flags_1,           &
-                wall_flags_2, wall_flags_3, wall_flags_4, wall_flags_5,        &
-                wall_flags_6, wall_flags_7, wall_flags_8, wall_flags_9,        &
-                wall_flags_10
+    INTEGER( KIND = SELECTED_INT_KIND(11) ), DIMENSION(:,:,:), ALLOCATABLE ::  &
+                wall_flags_0  ! need to have 34 Bit
+
+    INTEGER, DIMENSION(:,:,:), ALLOCATABLE,  TARGET ::                         &
+                wall_flags_1, wall_flags_2, wall_flags_3, wall_flags_4,        &
+                wall_flags_5, wall_flags_6, wall_flags_7, wall_flags_8,        &
+                wall_flags_9, wall_flags_10
+
 
     SAVE
Index: palm/trunk/SOURCE/palm.f90
===================================================================
--- palm/trunk/SOURCE/palm.f90	(revision 859)
+++ palm/trunk/SOURCE/palm.f90	(revision 861)
@@ -83,5 +83,5 @@
     INTEGER           ::  i, run_description_header_i(80)
 
-    version = 'PALM 3.8'
+    version = 'PALM 3.8a'
 
 #if defined( __parallel )
