Index: palm/trunk/SOURCE/diffusion_v.f90
===================================================================
--- palm/trunk/SOURCE/diffusion_v.f90	(revision 2119)
+++ palm/trunk/SOURCE/diffusion_v.f90	(revision 2232)
@@ -20,5 +20,5 @@
 ! Current revisions:
 ! -----------------
-! 
+! Adjustments to new topography and surface concept
 ! 
 ! Former revisions:
@@ -90,6 +90,4 @@
  
 
-    USE wall_fluxes_mod
-
     PRIVATE
     PUBLIC diffusion_v
@@ -111,39 +109,41 @@
 
        USE arrays_3d,                                                          &
-           ONLY:  ddzu, ddzw, km, tend, u, v, vsws, vswst, w,                  &
-                  drho_air, rho_air_zw
+           ONLY:  ddzu, ddzw, km, tend, u, v, w, drho_air, rho_air_zw
        
        USE control_parameters,                                                 &
-           ONLY:  constant_top_momentumflux, topography, use_surface_fluxes,   &
+           ONLY:  constant_top_momentumflux, use_surface_fluxes,               &
                   use_top_fluxes
        
        USE grid_variables,                                                     &
-           ONLY:  ddx, ddy, ddy2, fxm, fxp, wall_v
+           ONLY:  ddx, ddy, ddy2
        
        USE indices,                                                            &
-           ONLY:  nxl, nxr, nyn, nys, nysv, nzb, nzb_diff_v, nzb_v_inner,      &
-                  nzb_v_outer, nzt, nzt_diff
+           ONLY:  nxl, nxr, nyn, nys, nysv, nzb, nzt, wall_flags_0
        
        USE kinds
 
+       USE surface_mod,                                                        &
+           ONLY :  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v, surf_usm_h, &
+                   surf_usm_v
+
        IMPLICIT NONE
 
-       INTEGER(iwp) ::  i     !< 
-       INTEGER(iwp) ::  j     !< 
-       INTEGER(iwp) ::  k     !< 
-       REAL(wp)     ::  kmxm  !< 
-       REAL(wp)     ::  kmxp  !< 
-       REAL(wp)     ::  kmzm  !< 
-       REAL(wp)     ::  kmzp  !< 
-
-       REAL(wp), DIMENSION(nzb:nzt+1,nys:nyn,nxl:nxr) ::  vsus  !< 
-
-!
-!--    First calculate horizontal momentum flux v'u' at vertical walls,
-!--    if neccessary
-       IF ( topography /= 'flat' )  THEN
-          CALL wall_fluxes( vsus, 0.0_wp, 1.0_wp, 0.0_wp, 0.0_wp, nzb_v_inner, &
-                            nzb_v_outer, wall_v )
-       ENDIF
+       INTEGER(iwp) ::  i             !< running index x direction
+       INTEGER(iwp) ::  j             !< running index y direction
+       INTEGER(iwp) ::  k             !< running index z direction
+       INTEGER(iwp) ::  l             !< running index of surface type, south- or north-facing wall
+       INTEGER(iwp) ::  m             !< running index surface elements
+       INTEGER(iwp) ::  surf_e        !< End index of surface elements at (j,i)-gridpoint
+       INTEGER(iwp) ::  surf_s        !< Start index of surface elements at (j,i)-gridpoint
+
+       REAL(wp)     ::  flag          !< flag to mask topography grid points
+       REAL(wp)     ::  kmxm          !< 
+       REAL(wp)     ::  kmxp          !< 
+       REAL(wp)     ::  kmzm          !< 
+       REAL(wp)     ::  kmzp          !< 
+       REAL(wp)     ::  mask_bottom   !< flag to mask vertical upward-facing surface  
+       REAL(wp)     ::  mask_east     !< flag to mask vertical surface south of the grid point 
+       REAL(wp)     ::  mask_west     !< flag to mask vertical surface north of the grid point 
+       REAL(wp)     ::  mask_top      !< flag to mask vertical downward-facing surface      
 
        DO  i = nxl, nxr
@@ -151,56 +151,91 @@
 !
 !--          Compute horizontal diffusion
-             DO  k = nzb_v_outer(j,i)+1, nzt
+             DO  k = nzb+1, nzt
+
+!
+!--             Predetermine flag to mask topography and wall-bounded grid points. 
+!--             It is sufficient to masked only east- and west-facing surfaces, which
+!--             need special treatment for the v-component. 
+                flag      = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i),   2 ) ) 
+                mask_east = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i+1), 2 ) )
+                mask_west = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i-1), 2 ) )
 !
 !--             Interpolate eddy diffusivities on staggered gridpoints
-                kmxp = 0.25_wp * &
-                       ( km(k,j,i)+km(k,j,i+1)+km(k,j-1,i)+km(k,j-1,i+1) )
-                kmxm = 0.25_wp * &
-                       ( km(k,j,i)+km(k,j,i-1)+km(k,j-1,i)+km(k,j-1,i-1) )
-
-                tend(k,j,i) = tend(k,j,i)                                      &
-                      & + ( kmxp * ( v(k,j,i+1) - v(k,j,i)     ) * ddx         &
-                      &   + kmxp * ( u(k,j,i+1) - u(k,j-1,i+1) ) * ddy         &
-                      &   - kmxm * ( v(k,j,i) - v(k,j,i-1) ) * ddx             &
-                      &   - kmxm * ( u(k,j,i) - u(k,j-1,i) ) * ddy             &
-                      &   ) * ddx                                              &
-                      & + 2.0_wp * (                                           &
-                      &           km(k,j,i)   * ( v(k,j+1,i) - v(k,j,i) )      &
-                      &         - km(k,j-1,i) * ( v(k,j,i) - v(k,j-1,i) )      &
-                      &            ) * ddy2
+                kmxp = 0.25_wp * ( km(k,j,i)+km(k,j,i+1)+km(k,j-1,i)+km(k,j-1,i+1) )
+                kmxm = 0.25_wp * ( km(k,j,i)+km(k,j,i-1)+km(k,j-1,i)+km(k,j-1,i-1) )
+
+                tend(k,j,i) = tend(k,j,i) +    (                             &
+                          mask_east * kmxp * (                               &
+                                 ( v(k,j,i+1) - v(k,j,i)     ) * ddx         &
+                               + ( u(k,j,i+1) - u(k,j-1,i+1) ) * ddy         &
+                                             )                               &
+                        - mask_west * kmxm * (                               &
+                                 ( v(k,j,i) - v(k,j,i-1) ) * ddx             &
+                               + ( u(k,j,i) - u(k,j-1,i) ) * ddy             &
+                                             )                               &
+                                               ) * ddx  * flag               &
+                                    + 2.0_wp * (                             &
+                                  km(k,j,i)   * ( v(k,j+1,i) - v(k,j,i)   )  &
+                                - km(k,j-1,i) * ( v(k,j,i)   - v(k,j-1,i) )  &
+                                               ) * ddy2 * flag
+
              ENDDO
 
 !
-!--          Wall functions at the left and right walls, respectively
-             IF ( wall_v(j,i) /= 0.0_wp )  THEN
-
-                DO  k = nzb_v_inner(j,i)+1, nzb_v_outer(j,i)
-                   kmxp = 0.25_wp *                                            &
-                          ( km(k,j,i)+km(k,j,i+1)+km(k,j-1,i)+km(k,j-1,i+1) )
-                   kmxm = 0.25_wp *                                            &
-                          ( km(k,j,i)+km(k,j,i-1)+km(k,j-1,i)+km(k,j-1,i-1) )
-                   
-                   tend(k,j,i) = tend(k,j,i)                                   &
-                                 + 2.0_wp * (                                  &
-                                       km(k,j,i)   * ( v(k,j+1,i) - v(k,j,i) ) &
-                                     - km(k,j-1,i) * ( v(k,j,i) - v(k,j-1,i) ) &
-                                            ) * ddy2                           &
-                                 + (   fxp(j,i) * (                            &
-                                  kmxp * ( v(k,j,i+1) - v(k,j,i)     ) * ddx   &
-                                + kmxp * ( u(k,j,i+1) - u(k,j-1,i+1) ) * ddy   &
-                                                  )                            &
-                                     - fxm(j,i) * (                            &
-                                  kmxm * ( v(k,j,i) - v(k,j,i-1) ) * ddx       &
-                                + kmxm * ( u(k,j,i) - u(k,j-1,i) ) * ddy       &
-                                                  )                            &
-                                     + wall_v(j,i) * vsus(k,j,i)               &
-                                   ) * ddx
-                ENDDO
-             ENDIF
-
-!
-!--          Compute vertical diffusion. In case of simulating a Prandtl
-!--          layer, index k starts at nzb_v_inner+2.
-             DO  k = nzb_diff_v(j,i), nzt_diff
+!--          Add horizontal momentum flux v'u' at east- (l=2) and west-facing (l=3)
+!--          surfaces. Note, in the the flat case, loops won't be entered as 
+!--          start_index > end_index. Furtermore, note, no vertical natural surfaces
+!--          so far.           
+!--          Default-type surfaces
+             DO  l = 2, 3
+                surf_s = surf_def_v(l)%start_index(j,i)
+                surf_e = surf_def_v(l)%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k           = surf_def_v(l)%k(m)
+                   tend(k,j,i) = tend(k,j,i) +                                 &
+                                    surf_def_v(l)%mom_flux_uv(m) * ddx
+                ENDDO   
+             ENDDO
+!
+!--          Natural-type surfaces
+             DO  l = 2, 3
+                surf_s = surf_lsm_v(l)%start_index(j,i)
+                surf_e = surf_lsm_v(l)%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k           = surf_lsm_v(l)%k(m)
+                   tend(k,j,i) = tend(k,j,i) +                                 &
+                                    surf_lsm_v(l)%mom_flux_uv(m) * ddx
+                ENDDO   
+             ENDDO
+!
+!--          Urban-type surfaces
+             DO  l = 2, 3
+                surf_s = surf_usm_v(l)%start_index(j,i)
+                surf_e = surf_usm_v(l)%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k           = surf_usm_v(l)%k(m)
+                   tend(k,j,i) = tend(k,j,i) +                                 &
+                                    surf_usm_v(l)%mom_flux_uv(m) * ddx
+                ENDDO   
+             ENDDO
+!
+!--          Compute vertical diffusion. In case of simulating a surface layer,
+!--          respective grid diffusive fluxes are masked (flag 10) within this 
+!--          loop, and added further below, else, simple gradient approach is
+!--          applied. Model top is also mask if top-momentum flux is given. 
+             DO  k = nzb+1, nzt
+!
+!--             Determine flags to mask topography below and above. Flag 2 is 
+!--             used to mask topography in general, while flag 8 implies also
+!--             information about use_surface_fluxes. Flag 9 is used to control 
+!--             momentum flux at model top.  
+                mask_bottom = MERGE( 1.0_wp, 0.0_wp,                           &
+                                     BTEST( wall_flags_0(k-1,j,i), 8 ) ) 
+                mask_top    = MERGE( 1.0_wp, 0.0_wp,                           &
+                                     BTEST( wall_flags_0(k+1,j,i), 8 ) ) *     &
+                              MERGE( 1.0_wp, 0.0_wp,                           &
+                                     BTEST( wall_flags_0(k+1,j,i), 9 ) ) 
+                flag        = MERGE( 1.0_wp, 0.0_wp,                           &
+                                     BTEST( wall_flags_0(k,j,i), 2 ) ) 
 !
 !--             Interpolate eddy diffusivities on staggered gridpoints
@@ -213,9 +248,9 @@
                       & + ( kmzp * ( ( v(k+1,j,i) - v(k,j,i) ) * ddzu(k+1)     &
                       &            + ( w(k,j,i) - w(k,j-1,i) ) * ddy           &
-                      &            ) * rho_air_zw(k)                           &
+                      &            ) * rho_air_zw(k)   * mask_top              &
                       &   - kmzm * ( ( v(k,j,i)   - v(k-1,j,i)   ) * ddzu(k)   &
                       &            + ( w(k-1,j,i) - w(k-1,j-1,i) ) * ddy       &
-                      &            ) * rho_air_zw(k-1)                         &
-                      &   ) * ddzw(k) * drho_air(k)
+                      &            ) * rho_air_zw(k-1) * mask_bottom           &
+                      &   ) * ddzw(k) * drho_air(k) * flag
              ENDDO
 
@@ -228,37 +263,64 @@
 !--          comparison with other (LES) models showed that the values of
 !--          the momentum flux becomes too large in this case.
-!--          The term containing w(k-1,..) (see above equation) is removed here
-!--          because the vertical velocity is assumed to be zero at the surface.
              IF ( use_surface_fluxes )  THEN
-                k = nzb_v_inner(j,i)+1
-!
-!--             Interpolate eddy diffusivities on staggered gridpoints
-                kmzp = 0.25_wp *                                               &
-                       ( km(k,j,i)+km(k+1,j,i)+km(k,j-1,i)+km(k+1,j-1,i) )
-
-                tend(k,j,i) = tend(k,j,i)                                      &
-                      & + ( kmzp * ( ( v(k+1,j,i) - v(k,j,i)   ) * ddzu(k+1)   &
-                      &            + ( w(k,j,i)   - w(k,j-1,i) ) * ddy         &
-                      &            ) * rho_air_zw(k)                           &
-                      &   - ( -vsws(j,i) )                                     &
-                      &   ) * ddzw(k) * drho_air(k)
+!
+!--             Default-type surfaces, upward-facing
+                surf_s = surf_def_h(0)%start_index(j,i)
+                surf_e = surf_def_h(0)%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k   = surf_def_h(0)%k(m)
+
+                   tend(k,j,i) = tend(k,j,i)                                   &
+                        + ( - ( - surf_def_h(0)%vsws(m) )                      &
+                          ) * ddzw(k) * drho_air(k)
+                ENDDO
+!
+!--             Default-type surfaces, dowward-facing
+                surf_s = surf_def_h(1)%start_index(j,i)
+                surf_e = surf_def_h(1)%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k   = surf_def_h(1)%k(m)
+
+                   tend(k,j,i) = tend(k,j,i)                                   &
+                        + ( - surf_def_h(1)%vsws(m)                            &
+                          ) * ddzw(k) * drho_air(k)
+                ENDDO
+!
+!--             Natural-type surfaces, upward-facing
+                surf_s = surf_lsm_h%start_index(j,i)
+                surf_e = surf_lsm_h%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k   = surf_lsm_h%k(m)
+
+                   tend(k,j,i) = tend(k,j,i)                                   &
+                        + ( - ( - surf_lsm_h%vsws(m) )                         &
+                          ) * ddzw(k) * drho_air(k)
+
+                ENDDO
+!
+!--             Urban-type surfaces, upward-facing
+                surf_s = surf_usm_h%start_index(j,i)
+                surf_e = surf_usm_h%end_index(j,i)
+                DO  m = surf_s, surf_e
+                   k   = surf_usm_h%k(m)
+
+                   tend(k,j,i) = tend(k,j,i)                                   &
+                        + ( - ( - surf_usm_h%vsws(m) )                         &
+                          ) * ddzw(k) * drho_air(k)
+
+                ENDDO
              ENDIF
-
-!
-!--          Vertical diffusion at the first gridpoint below the top boundary,
-!--          if the momentum flux at the top is prescribed by the user
-             IF ( use_top_fluxes  .AND.  constant_top_momentumflux )  THEN
-                k = nzt
-!
-!--             Interpolate eddy diffusivities on staggered gridpoints
-                kmzm = 0.25_wp *                                               &
-                       ( km(k,j,i)+km(k-1,j,i)+km(k,j-1,i)+km(k-1,j-1,i) )
-
-                tend(k,j,i) = tend(k,j,i)                                      &
-                      & + ( ( -vswst(j,i) )                                    &
-                      &   - kmzm * ( ( v(k,j,i)   - v(k-1,j,i)   ) * ddzu(k)   &
-                      &            + ( w(k-1,j,i) - w(k-1,j-1,i) ) * ddy       &
-                      &            ) * rho_air_zw(k-1)                         &
-                      &   ) * ddzw(k) * drho_air(k)
+!
+!--          Add momentum flux at model top
+             IF ( use_top_fluxes )  THEN
+                surf_s = surf_def_h(2)%start_index(j,i)
+                surf_e = surf_def_h(2)%end_index(j,i)
+                DO  m = surf_s, surf_e
+
+                   k   = surf_def_h(2)%k(m)
+
+                   tend(k,j,i) = tend(k,j,i)                                   &
+                           + ( - surf_def_h(2)%vsws(m) ) * ddzw(k) * drho_air(k)
+                ENDDO
              ENDIF
 
@@ -277,33 +339,53 @@
 
        USE arrays_3d,                                                          &
-           ONLY:  ddzu, ddzw, km, tend, u, v, vsws, vswst, w,                  &
-                  drho_air, rho_air_zw
+           ONLY:  ddzu, ddzw, km, tend, u, v, w, drho_air, rho_air_zw
        
        USE control_parameters,                                                 &
-           ONLY:  constant_top_momentumflux, use_surface_fluxes, use_top_fluxes
+           ONLY:  constant_top_momentumflux, use_surface_fluxes,               &
+                  use_top_fluxes
        
        USE grid_variables,                                                     &
-           ONLY:  ddx, ddy, ddy2, fxm, fxp, wall_v
+           ONLY:  ddx, ddy, ddy2
        
        USE indices,                                                            &
-           ONLY:  nzb, nzb_diff_v, nzb_v_inner, nzb_v_outer, nzt, nzt_diff
+           ONLY:  nzb, nzt, wall_flags_0
        
        USE kinds
 
+       USE surface_mod,                                                        &
+           ONLY :  surf_def_h, surf_def_v, surf_lsm_h, surf_lsm_v, surf_usm_h, &
+                   surf_usm_v
+
        IMPLICIT NONE
 
-       INTEGER(iwp) ::  i     !< 
-       INTEGER(iwp) ::  j     !< 
-       INTEGER(iwp) ::  k     !< 
-       REAL(wp)     ::  kmxm  !< 
-       REAL(wp)     ::  kmxp  !< 
-       REAL(wp)     ::  kmzm  !< 
-       REAL(wp)     ::  kmzp  !< 
-
-       REAL(wp), DIMENSION(nzb:nzt+1) ::  vsus  !< 
+
+       INTEGER(iwp) ::  i             !< running index x direction
+       INTEGER(iwp) ::  j             !< running index y direction
+       INTEGER(iwp) ::  k             !< running index z direction
+       INTEGER(iwp) ::  l             !< running index of surface type, south- or north-facing wall
+       INTEGER(iwp) ::  m             !< running index surface elements
+       INTEGER(iwp) ::  surf_e        !< End index of surface elements at (j,i)-gridpoint
+       INTEGER(iwp) ::  surf_s        !< Start index of surface elements at (j,i)-gridpoint
+
+       REAL(wp)     ::  flag          !< flag to mask topography grid points
+       REAL(wp)     ::  kmxm          !< 
+       REAL(wp)     ::  kmxp          !< 
+       REAL(wp)     ::  kmzm          !< 
+       REAL(wp)     ::  kmzp          !< 
+       REAL(wp)     ::  mask_bottom   !< flag to mask vertical upward-facing surface  
+       REAL(wp)     ::  mask_east     !< flag to mask vertical surface south of the grid point 
+       REAL(wp)     ::  mask_west     !< flag to mask vertical surface north of the grid point 
+       REAL(wp)     ::  mask_top      !< flag to mask vertical downward-facing surface
 
 !
 !--    Compute horizontal diffusion
-       DO  k = nzb_v_outer(j,i)+1, nzt
+       DO  k = nzb+1, nzt
+!
+!--       Predetermine flag to mask topography and wall-bounded grid points. 
+!--       It is sufficient to masked only east- and west-facing surfaces, which
+!--       need special treatment for the v-component. 
+          flag      = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i),   2 ) ) 
+          mask_east = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i+1), 2 ) )
+          mask_west = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i-1), 2 ) )
 !
 !--       Interpolate eddy diffusivities on staggered gridpoints
@@ -311,53 +393,73 @@
           kmxm = 0.25_wp * ( km(k,j,i)+km(k,j,i-1)+km(k,j-1,i)+km(k,j-1,i-1) )
 
-          tend(k,j,i) = tend(k,j,i)                                            &
-                      & + ( kmxp * ( v(k,j,i+1) - v(k,j,i)     ) * ddx         &
-                      &   + kmxp * ( u(k,j,i+1) - u(k,j-1,i+1) ) * ddy         &
-                      &   - kmxm * ( v(k,j,i) - v(k,j,i-1) ) * ddx             &
-                      &   - kmxm * ( u(k,j,i) - u(k,j-1,i) ) * ddy             &
-                      &   ) * ddx                                              &
-                      & + 2.0_wp * (                                           &
-                      &           km(k,j,i)   * ( v(k,j+1,i) - v(k,j,i) )      &
-                      &         - km(k,j-1,i) * ( v(k,j,i) - v(k,j-1,i) )      &
-                      &            ) * ddy2
+          tend(k,j,i) = tend(k,j,i) +          (                             &
+                          mask_east * kmxp * (                               &
+                                 ( v(k,j,i+1) - v(k,j,i)     ) * ddx         &
+                               + ( u(k,j,i+1) - u(k,j-1,i+1) ) * ddy         &
+                                             )                               &
+                        - mask_west * kmxm * (                               &
+                                 ( v(k,j,i) - v(k,j,i-1) ) * ddx             &
+                               + ( u(k,j,i) - u(k,j-1,i) ) * ddy             &
+                                             )                               &
+                                               ) * ddx  * flag               &
+                                    + 2.0_wp * (                             &
+                                  km(k,j,i)   * ( v(k,j+1,i) - v(k,j,i)   )  &
+                                - km(k,j-1,i) * ( v(k,j,i)   - v(k,j-1,i) )  &
+                                               ) * ddy2 * flag
        ENDDO
 
 !
-!--    Wall functions at the left and right walls, respectively
-       IF ( wall_v(j,i) /= 0.0_wp )  THEN
-
-!
-!--       Calculate the horizontal momentum flux v'u'
-          CALL wall_fluxes( i, j, nzb_v_inner(j,i)+1, nzb_v_outer(j,i),        &
-                            vsus, 0.0_wp, 1.0_wp, 0.0_wp, 0.0_wp )
-
-          DO  k = nzb_v_inner(j,i)+1, nzb_v_outer(j,i)
-             kmxp = 0.25_wp *                                                  &
-                    ( km(k,j,i)+km(k,j,i+1)+km(k,j-1,i)+km(k,j-1,i+1) )
-             kmxm = 0.25_wp *                                                  &
-                    ( km(k,j,i)+km(k,j,i-1)+km(k,j-1,i)+km(k,j-1,i-1) )
-
-             tend(k,j,i) = tend(k,j,i)                                         &
-                                 + 2.0_wp * (                                  &
-                                       km(k,j,i)   * ( v(k,j+1,i) - v(k,j,i) ) &
-                                     - km(k,j-1,i) * ( v(k,j,i) - v(k,j-1,i) ) &
-                                            ) * ddy2                           &
-                                 + (   fxp(j,i) * (                            &
-                                  kmxp * ( v(k,j,i+1) - v(k,j,i)     ) * ddx   &
-                                + kmxp * ( u(k,j,i+1) - u(k,j-1,i+1) ) * ddy   &
-                                                  )                            &
-                                     - fxm(j,i) * (                            &
-                                  kmxm * ( v(k,j,i) - v(k,j,i-1) ) * ddx       &
-                                + kmxm * ( u(k,j,i) - u(k,j-1,i) ) * ddy       &
-                                                  )                            &
-                                     + wall_v(j,i) * vsus(k)                   &
-                                   ) * ddx
-          ENDDO
-       ENDIF
-
-!
-!--    Compute vertical diffusion. In case of simulating a Prandtl layer,
-!--    index k starts at nzb_v_inner+2.
-       DO  k = nzb_diff_v(j,i), nzt_diff
+!--    Add horizontal momentum flux v'u' at east- (l=2) and west-facing (l=3)
+!--    surfaces. Note, in the the flat case, loops won't be entered as 
+!--    start_index > end_index. Furtermore, note, no vertical natural surfaces
+!--    so far.           
+!--    Default-type surfaces
+       DO  l = 2, 3
+          surf_s = surf_def_v(l)%start_index(j,i)
+          surf_e = surf_def_v(l)%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k           = surf_def_v(l)%k(m)
+             tend(k,j,i) = tend(k,j,i) + surf_def_v(l)%mom_flux_uv(m) * ddx
+          ENDDO   
+       ENDDO
+!
+!--    Natural-type surfaces
+       DO  l = 2, 3
+          surf_s = surf_lsm_v(l)%start_index(j,i)
+          surf_e = surf_lsm_v(l)%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k           = surf_lsm_v(l)%k(m)
+             tend(k,j,i) = tend(k,j,i) + surf_lsm_v(l)%mom_flux_uv(m) * ddx
+          ENDDO   
+       ENDDO
+!
+!--    Urban-type surfaces
+       DO  l = 2, 3
+          surf_s = surf_usm_v(l)%start_index(j,i)
+          surf_e = surf_usm_v(l)%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k           = surf_usm_v(l)%k(m)
+             tend(k,j,i) = tend(k,j,i) + surf_usm_v(l)%mom_flux_uv(m) * ddx
+          ENDDO   
+       ENDDO
+!
+!--    Compute vertical diffusion. In case of simulating a surface layer,
+!--    respective grid diffusive fluxes are masked (flag 8) within this 
+!--    loop, and added further below, else, simple gradient approach is
+!--    applied. Model top is also mask if top-momentum flux is given.
+       DO  k = nzb+1, nzt
+!
+!--       Determine flags to mask topography below and above. Flag 2 is 
+!--       used to mask topography in general, while flag 10 implies also
+!--       information about use_surface_fluxes. Flag 9 is used to control 
+!--       momentum flux at model top.  
+          mask_bottom = MERGE( 1.0_wp, 0.0_wp,                                 &
+                               BTEST( wall_flags_0(k-1,j,i), 8 ) ) 
+          mask_top    = MERGE( 1.0_wp, 0.0_wp,                                 &
+                               BTEST( wall_flags_0(k+1,j,i), 8 ) ) *           &
+                        MERGE( 1.0_wp, 0.0_wp,                                 &
+                               BTEST( wall_flags_0(k+1,j,i), 9 ) ) 
+          flag        = MERGE( 1.0_wp, 0.0_wp,                                 &
+                               BTEST( wall_flags_0(k,j,i), 2 ) )
 !
 !--       Interpolate eddy diffusivities on staggered gridpoints
@@ -368,9 +470,9 @@
                       & + ( kmzp * ( ( v(k+1,j,i) - v(k,j,i) ) * ddzu(k+1)     &
                       &            + ( w(k,j,i) - w(k,j-1,i) ) * ddy           &
-                      &            ) * rho_air_zw(k)                           &
+                      &            ) * rho_air_zw(k)   * mask_top              &
                       &   - kmzm * ( ( v(k,j,i)   - v(k-1,j,i)   ) * ddzu(k)   &
                       &            + ( w(k-1,j,i) - w(k-1,j-1,i) ) * ddy       &
-                      &            ) * rho_air_zw(k-1)                         &
-                      &   ) * ddzw(k) * drho_air(k)
+                      &            ) * rho_air_zw(k-1) * mask_bottom           &
+                      &   ) * ddzw(k) * drho_air(k) * flag
        ENDDO
 
@@ -383,35 +485,64 @@
 !--    other (LES) models showed that the values of the momentum flux becomes
 !--    too large in this case.
-!--    The term containing w(k-1,..) (see above equation) is removed here
-!--    because the vertical velocity is assumed to be zero at the surface.
        IF ( use_surface_fluxes )  THEN
-          k = nzb_v_inner(j,i)+1
-!
-!--       Interpolate eddy diffusivities on staggered gridpoints
-          kmzp = 0.25_wp * ( km(k,j,i)+km(k+1,j,i)+km(k,j-1,i)+km(k+1,j-1,i) )
-
-          tend(k,j,i) = tend(k,j,i)                                            &
-                      & + ( kmzp * ( ( v(k+1,j,i) - v(k,j,i)   ) * ddzu(k+1)   &
-                      &            + ( w(k,j,i)   - w(k,j-1,i) ) * ddy         &
-                      &            ) * rho_air_zw(k)                           &
-                      &   - ( -vsws(j,i) )                                     &
-                      &   ) * ddzw(k) * drho_air(k)
+!
+!--       Default-type surfaces, upward-facing
+          surf_s = surf_def_h(0)%start_index(j,i)
+          surf_e = surf_def_h(0)%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k   = surf_def_h(0)%k(m)
+
+             tend(k,j,i) = tend(k,j,i)                                         &
+                        + ( - ( - surf_def_h(0)%vsws(m) )                      &
+                          ) * ddzw(k) * drho_air(k)
+          ENDDO
+!
+!--       Default-type surfaces, dowward-facing
+          surf_s = surf_def_h(1)%start_index(j,i)
+          surf_e = surf_def_h(1)%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k   = surf_def_h(1)%k(m)
+
+             tend(k,j,i) = tend(k,j,i)                                         &
+                        + ( - surf_def_h(1)%vsws(m)                            &
+                          ) * ddzw(k) * drho_air(k)
+          ENDDO
+!
+!--       Natural-type surfaces, upward-facing
+          surf_s = surf_lsm_h%start_index(j,i)
+          surf_e = surf_lsm_h%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k   = surf_lsm_h%k(m)
+
+             tend(k,j,i) = tend(k,j,i)                                         &
+                        + ( - ( - surf_lsm_h%vsws(m) )                         &
+                          ) * ddzw(k) * drho_air(k)
+
+          ENDDO
+!
+!--       Urban-type surfaces, upward-facing
+          surf_s = surf_usm_h%start_index(j,i)
+          surf_e = surf_usm_h%end_index(j,i)
+          DO  m = surf_s, surf_e
+             k   = surf_usm_h%k(m)
+
+             tend(k,j,i) = tend(k,j,i)                                         &
+                        + ( - ( - surf_usm_h%vsws(m) )                         &
+                          ) * ddzw(k) * drho_air(k)
+
+          ENDDO
        ENDIF
-
-!
-!--    Vertical diffusion at the first gridpoint below the top boundary,
-!--    if the momentum flux at the top is prescribed by the user
-       IF ( use_top_fluxes  .AND.  constant_top_momentumflux )  THEN
-          k = nzt
-!
-!--       Interpolate eddy diffusivities on staggered gridpoints
-          kmzm = 0.25_wp * ( km(k,j,i)+km(k-1,j,i)+km(k,j-1,i)+km(k-1,j-1,i) )
-
-          tend(k,j,i) = tend(k,j,i)                                            &
-                      & + ( ( -vswst(j,i) )                                    &
-                      &   - kmzm * ( ( v(k,j,i)   - v(k-1,j,i)   ) * ddzu(k)   &
-                      &            + ( w(k-1,j,i) - w(k-1,j-1,i) ) * ddy       &
-                      &            ) * rho_air_zw(k-1)                         &
-                      &   ) * ddzw(k) * drho_air(k)
+!
+!--    Add momentum flux at model top
+       IF ( use_top_fluxes )  THEN
+          surf_s = surf_def_h(2)%start_index(j,i)
+          surf_e = surf_def_h(2)%end_index(j,i)
+          DO  m = surf_s, surf_e
+
+             k   = surf_def_h(2)%k(m)
+
+             tend(k,j,i) = tend(k,j,i)                                         &
+                           + ( - surf_def_h(2)%vsws(m) ) * ddzw(k) * drho_air(k)
+          ENDDO
        ENDIF
 
