Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 2201)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 2232)
@@ -20,5 +20,10 @@
 ! Current revisions:
 ! -----------------
-! 
+! - Adjustments according to new topography representation
+! - Bugfix: Move determination of nzb_max behind topography modification in 
+!   cell-edge case 
+! - Get rid off global arrays required for topography output
+! - Enable topography input via netcdf 
+! - Generic tunnel set-up added
 ! 
 ! Former revisions:
@@ -212,4 +217,7 @@
 ! ------------
 !> Creating grid depending constants
+!> To Do: Setting topo flags only based on topo_3d array - flags for former
+!> nzb_outer arrays are still not set properly. 
+!> To Do: Rearrange topo flag list
 !------------------------------------------------------------------------------!
  SUBROUTINE init_grid
@@ -230,24 +238,22 @@
                dz_stretch_level, dz_stretch_level_index, grid_level, ibc_uv_b, &
                io_blocks, io_group, inflow_l, inflow_n, inflow_r, inflow_s,    &
-               masking_method, maximum_grid_level, message_string,             &
+               lod, masking_method, maximum_grid_level, message_string,        &
                momentum_advec, nest_domain, nest_bound_l, nest_bound_n,        &
                nest_bound_r, nest_bound_s, ocean, outflow_l, outflow_n,        &
                outflow_r, outflow_s, psolver, scalar_advec, topography,        &
-               topography_grid_convention, use_surface_fluxes, use_top_fluxes, &
-               wall_adjustment_factor
+               topography_grid_convention, tunnel_height, tunnel_length,       &
+               tunnel_width_x, tunnel_width_y, tunnel_wall_depth,              &
+               use_surface_fluxes, use_top_fluxes, wall_adjustment_factor
          
     USE grid_variables,                                                        &
-        ONLY:  ddx, ddx2, ddy, ddy2, dx, dx2, dy, dy2, fwxm,                   &
-               fwxp, fwym, fwyp, fxm, fxp, fym, fyp, wall_e_x, wall_e_y,       &
-               wall_u, wall_v, wall_w_x, wall_w_y, zu_s_inner, zw_w_inner
+        ONLY:  ddx, ddx2, ddy, ddy2, dx, dx2, dy, dy2, zu_s_inner, zw_w_inner
         
     USE indices,                                                               &
-        ONLY:  flags, nbgp, nx, nxl, nxlg, nxl_mg, nxr, nxrg, nxr_mg,          &
-               ny, nyn, nyng, nyn_mg, nys, nys_mg, nysg, nz, nzb,              &
-               nzb_diff, nzb_diff_s_inner, nzb_diff_s_outer, nzb_diff_u,       &
-               nzb_diff_v, nzb_max, nzb_s_inner, nzb_s_outer, nzb_u_inner,     &
+        ONLY:  advc_flags_1, advc_flags_2, flags, nbgp, nx, nxl, nxlg, nxl_mg, &
+               nxr, nxrg, nxr_mg, ny, nyn, nyng, nyn_mg, nys, nys_mg, nysg, nz,&
+               nzb, nzb_diff, nzb_diff_s_inner, nzb_diff_s_outer,              &
+               nzb_max, nzb_s_inner, nzb_s_outer, nzb_u_inner,                 &
                nzb_u_outer, nzb_v_inner, nzb_v_outer, nzb_w_inner,             &
-               nzb_w_outer, nzt, nzt_diff, nzt_mg, rflags_invers,              &
-               rflags_s_inner, wall_flags_0, wall_flags_00, wall_flags_1,      &
+               nzb_w_outer, nzt, nzt_mg, wall_flags_0, wall_flags_1,           &
                wall_flags_10, wall_flags_2, wall_flags_3,  wall_flags_4,       &
                wall_flags_5, wall_flags_6, wall_flags_7, wall_flags_8,         &
@@ -255,6 +261,13 @@
     
     USE kinds
-    
+#if defined ( __netcdf )
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_close_file, netcdf_open_read_file, netcdf_get_attribute, &
+               netcdf_get_variable
+#endif
     USE pegrid
+
+    USE surface_mod,                                                           &
+        ONLY:  init_bc
 
     IMPLICIT NONE
@@ -275,8 +288,10 @@
     INTEGER(iwp) ::  cys           !< index for south canyon wall
     INTEGER(iwp) ::  i             !< index variable along x
+    INTEGER(iwp) ::  id_topo       !< NetCDF id of topograhy input file
     INTEGER(iwp) ::  ii            !< loop variable for reading topography file
     INTEGER(iwp) ::  inc           !< incremental parameter for coarsening grid level
     INTEGER(iwp) ::  j             !< index variable along y
     INTEGER(iwp) ::  k             !< index variable along z
+    INTEGER(iwp) ::  k_top         !< topography top index on local PE
     INTEGER(iwp) ::  l             !< loop variable 
     INTEGER(iwp) ::  nxl_l         !< index of left PE boundary for multigrid level
@@ -286,5 +301,4 @@
     INTEGER(iwp) ::  nzb_local_max !< vertical grid index of maximum topography height
     INTEGER(iwp) ::  nzb_local_min !< vertical grid index of minimum topography height
-    INTEGER(iwp) ::  nzb_si        !< dummy index for local nzb_s_inner
     INTEGER(iwp) ::  nzt_l         !< index of top PE boundary for multigrid level
     INTEGER(iwp) ::  num_hole      !< number of holes (in topography) resolved by only one grid point 
@@ -292,28 +306,30 @@
     INTEGER(iwp) ::  num_wall      !< number of surrounding vertical walls for a single grid point
     INTEGER(iwp) ::  skip_n_rows   !< counting variable to skip rows while reading topography file    
-    INTEGER(iwp) ::  vi            !< dummy for vertical influence
-
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE   ::                               &
-                     vertical_influence  !< number of vertical grid points above obstacle where adjustment of near-wall mixing length is required
-                                         
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_nl      !< index of north-left corner location to limit near-wall mixing length
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_nr      !< north-right
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_sl      !< south-left
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  corner_sr      !< south-right
+    INTEGER(iwp) ::  hv_in         !< heavyside function to model inner tunnel surface 
+    INTEGER(iwp) ::  hv_out        !< heavyside function to model outer tunnel surface 
+    INTEGER(iwp) ::  txe_out       !< end position of outer tunnel wall in x
+    INTEGER(iwp) ::  txs_out       !< start position of outer tunnel wall in x
+    INTEGER(iwp) ::  tye_out       !< end position of outer tunnel wall in y
+    INTEGER(iwp) ::  tys_out       !< start position of outer tunnel wall in y
+    INTEGER(iwp) ::  txe_in        !< end position of inner tunnel wall in x
+    INTEGER(iwp) ::  txs_in        !< start position of inner tunnel wall in x
+    INTEGER(iwp) ::  tye_in        !< end position of inner tunnel wall in y
+    INTEGER(iwp) ::  tys_in        !< start position of inner tunnel wall in y
+    INTEGER(iwp) ::  td            !< tunnel wall depth
+    INTEGER(iwp) ::  th            !< height of outer tunnel wall
+                                      
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_local      !< index for topography top at cell-center
     INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  nzb_tmp        !< dummy to calculate topography indices on u- and v-grid
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_l         !< distance to adjacent left-facing wall
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_n         !< north-facing
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_r         !< right-facing
-    INTEGER(iwp), DIMENSION(:,:), ALLOCATABLE ::  wall_s         !< right-facing
+
+    INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  topo_3d !< input array for 3D topography and dummy array for setting "outer"-flags
+
+    LOGICAL  ::  netcdf_extend = .FALSE. !< Flag indicating wether netcdf topography input file or not
 
     REAL(wp) ::  dum           !< dummy variable to skip columns while reading topography file    
     REAL(wp) ::  dz_stretched  !< stretched vertical grid spacing
 
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  oro_height    !< input variable for terrain height
     REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  topo_height   !< input variable for topography height
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  zu_s_inner_l  !< dummy array on global scale to write topography output array
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  zw_w_inner_l  !< dummy array on global scale to write topography output array
-
-    
+
 !
 !-- Calculation of horizontal array bounds including ghost layers
@@ -464,16 +480,6 @@
 !
 !-- Allocate outer and inner index arrays for topography and set
-!-- defaults.
-
-    ALLOCATE( corner_nl(nys:nyn,nxl:nxr), corner_nr(nys:nyn,nxl:nxr),       &
-              corner_sl(nys:nyn,nxl:nxr), corner_sr(nys:nyn,nxl:nxr),       &
-              wall_l(nys:nyn,nxl:nxr), wall_n(nys:nyn,nxl:nxr),             &
-              wall_r(nys:nyn,nxl:nxr), wall_s(nys:nyn,nxl:nxr) )                     
-     
-    ALLOCATE( fwxm(nysg:nyng,nxlg:nxrg), fwxp(nysg:nyng,nxlg:nxrg),         &
-              fwym(nysg:nyng,nxlg:nxrg), fwyp(nysg:nyng,nxlg:nxrg),         &
-              fxm(nysg:nyng,nxlg:nxrg), fxp(nysg:nyng,nxlg:nxrg),           &
-              fym(nysg:nyng,nxlg:nxrg), fyp(nysg:nyng,nxlg:nxrg),           &
-              nzb_s_inner(nysg:nyng,nxlg:nxrg),                             &
+!-- defaults.                    
+    ALLOCATE( nzb_s_inner(nysg:nyng,nxlg:nxrg),                             &
               nzb_s_outer(nysg:nyng,nxlg:nxrg),                             &
               nzb_u_inner(nysg:nyng,nxlg:nxrg),                             &
@@ -485,21 +491,12 @@
               nzb_diff_s_inner(nysg:nyng,nxlg:nxrg),                        &
               nzb_diff_s_outer(nysg:nyng,nxlg:nxrg),                        &
-              nzb_diff_u(nysg:nyng,nxlg:nxrg),                              &
-              nzb_diff_v(nysg:nyng,nxlg:nxrg),                              &
               nzb_local(nysg:nyng,nxlg:nxrg),                               &
               nzb_tmp(nysg:nyng,nxlg:nxrg),                                 &
-              rflags_s_inner(nzb:nzt+2,nysg:nyng,nxlg:nxrg),                &
-              rflags_invers(nysg:nyng,nxlg:nxrg,nzb:nzt+2),                 &
-              wall_e_x(nysg:nyng,nxlg:nxrg),                                &
-              wall_e_y(nysg:nyng,nxlg:nxrg),                                &
-              wall_u(nysg:nyng,nxlg:nxrg),                                  &
-              wall_v(nysg:nyng,nxlg:nxrg),                                  &
-              wall_w_x(nysg:nyng,nxlg:nxrg),                                &
-              wall_w_y(nysg:nyng,nxlg:nxrg) )
-
-
+              wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+
+    ALLOCATE( topo_3d(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+    topo_3d    = 0
 
     ALLOCATE( l_wall(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-
 
     nzb_s_inner = nzb;  nzb_s_outer = nzb
@@ -507,7 +504,4 @@
     nzb_v_inner = nzb;  nzb_v_outer = nzb
     nzb_w_inner = nzb;  nzb_w_outer = nzb
-
-    rflags_s_inner = 1.0_wp
-    rflags_invers  = 1.0_wp
 
 !
@@ -519,17 +513,6 @@
        nzb_diff = nzb + 1
     ENDIF
-    IF ( use_top_fluxes )  THEN
-       nzt_diff = nzt - 1
-    ELSE
-       nzt_diff = nzt
-    ENDIF
 
     nzb_diff_s_inner = nzb_diff;  nzb_diff_s_outer = nzb_diff
-    nzb_diff_u = nzb_diff;  nzb_diff_v = nzb_diff
-
-    wall_e_x = 0.0_wp;  wall_e_y = 0.0_wp;  wall_u = 0.0_wp;  wall_v = 0.0_wp
-    wall_w_x = 0.0_wp;  wall_w_y = 0.0_wp
-    fwxp = 1.0_wp;  fwxm = 1.0_wp;  fwyp = 1.0_wp;  fwym = 1.0_wp
-    fxp  = 1.0_wp;  fxm  = 1.0_wp;  fyp  = 1.0_wp;  fym  = 1.0_wp
 
 !
@@ -542,10 +525,4 @@
     ENDDO
     l_wall(nzt+1,:,:) = l_grid(nzt)
-
-    ALLOCATE ( vertical_influence(nzb:nzt) )
-    DO  k = 1, nzt
-       vertical_influence(k) = MIN ( INT( l_grid(k) / &
-                     ( wall_adjustment_factor * dzw(k) ) + 0.5_wp ), nzt - k )
-    ENDDO
 
     DO  k = 1, nzt
@@ -561,10 +538,4 @@
        ENDIF
     ENDDO
-    vertical_influence(0) = vertical_influence(1)
-
-    DO  k = nzb + 1, nzb + vertical_influence(nzb)
-       l_wall(k,:,:) = zu(k) - zw(nzb)
-    ENDDO
-
 !
 !-- Set outer and inner index arrays for non-flat topography.
@@ -580,4 +551,10 @@
 !--       nzb_local is required for the multigrid solver
           nzb_local = 0
+!
+!--       Initialilize 3D topography array, used later for initializing flags
+          topo_3d(nzb+1:nzt+1,:,:) = IBSET( topo_3d(nzb+1:nzt+1,:,:), 0 ) 
+!
+!--       level of detail is required for output routines
+          lod       = 1
 
        CASE ( 'single_building' )
@@ -587,11 +564,6 @@
           blx = NINT( building_length_x / dx )
           bly = NINT( building_length_y / dy )
-          IF ( .NOT. ocean )  THEN
-             bh  = MINLOC( ABS( zw - building_height ), 1 ) - 1
-          ELSE
-             bh  = MINLOC( ABS( zw - zw(0) - building_height ), 1 ) - 1
-          ENDIF
-
-          IF ( ABS( zw(bh  ) - building_height ) == &
+          bh  = MINLOC( ABS( zw - building_height ), 1 ) - 1
+          IF ( ABS( zw(bh)   - building_height ) == &
                ABS( zw(bh+1) - building_height )    )  bh = bh + 1
 
@@ -626,4 +598,18 @@
 
           CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
+!
+!--       Set bit array to mask topography
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
+
+                topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
+                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
+             ENDDO
+          ENDDO
+!
+!--       level of detail is required for output routines. Here, 2D topography. 
+          lod = 1
+
+          CALL exchange_horiz_int( topo_3d, nbgp )
 
        CASE ( 'single_street_canyon' )
@@ -658,11 +644,6 @@
           ENDIF
 
-          IF ( .NOT. ocean )  THEN
-             ch  = MINLOC( ABS( zw - canyon_height ), 1 ) - 1
-          ELSE
-             ch  = MINLOC( ABS( zw - zw(0) - canyon_height ), 1 ) - 1
-          ENDIF
-
-          IF ( ABS( zw(ch  ) - canyon_height ) == &
+          ch  = MINLOC( ABS( zw - canyon_height ), 1 ) - 1
+          IF ( ABS( zw(ch)   - canyon_height ) == &
                ABS( zw(ch+1) - canyon_height )    )  ch = ch + 1
 
@@ -707,8 +688,166 @@
 
           CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
+!
+!--       Set bit array to mask topography
+          DO  i = nxlg, nxrg
+             DO  j = nysg, nyng
+                topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
+                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
+             ENDDO
+          ENDDO
+!
+!--       level of detail is required for output routines. Here, 2D topography. 
+          lod = 1
+
+          CALL exchange_horiz_int( topo_3d, nbgp )
+
+       CASE ( 'tunnel' )
+
+!
+!--       Tunnel height
+          IF ( tunnel_height == 9999999.9_wp )  THEN
+             th = zw( INT( 0.2 * nz) )
+          ELSE
+             th = tunnel_height
+          ENDIF
+!
+!--       Tunnel-wall depth
+          IF ( tunnel_wall_depth == 9999999.9_wp )  THEN
+             td = MAX ( dx, dy, dz )
+          ELSE
+             td = tunnel_wall_depth
+          ENDIF
+!
+!--       Check for tunnel width
+          IF ( tunnel_width_x == 9999999.9_wp  .AND.                           &
+               tunnel_width_y == 9999999.9_wp  )  THEN
+             message_string = 'No tunnel width is given. '
+             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
+          ENDIF
+          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &
+               tunnel_width_y /= 9999999.9_wp  )  THEN
+             message_string = 'Inconsistent tunnel parameters:' //             &   
+                              'tunnel can only be oriented' //                 &
+                              'either in x- or in y-direction.'
+             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
+          ENDIF
+!
+!--       Tunnel axis along y
+          IF ( tunnel_width_x /= 9999999.9_wp )  THEN
+             IF ( tunnel_width_x > ( nx + 1 ) * dx )  THEN
+                message_string = 'Tunnel width too large'
+                CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
+             ENDIF
+
+             txs_out = INT( ( nx + 1 ) * 0.5_wp * dx - tunnel_width_x * 0.5_wp )
+             txe_out = INT( ( nx + 1 ) * 0.5_wp * dx + tunnel_width_x * 0.5_wp )
+             txs_in  = INT( ( nx + 1 ) * 0.5_wp * dx -                         &
+                                      ( tunnel_width_x * 0.5_wp - td ) )
+             txe_in  = INT( ( nx + 1 ) * 0.5_wp * dx +                         &
+                                      ( tunnel_width_x * 0.5_wp - td ) )
+
+             tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_length * 0.5_wp )
+             tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_length * 0.5_wp )
+             tys_in  = tys_out
+             tye_in  = tye_out
+          ENDIF
+          IF ( tunnel_width_x /= 9999999.9_wp  .AND.                           &
+               tunnel_width_x - 2.0_wp * tunnel_wall_depth <= 2.0_wp * dx )  THEN
+             message_string = 'Tunnel width too small'
+             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
+          ENDIF
+          IF ( tunnel_width_y /= 9999999.9_wp  .AND.                           &
+               tunnel_width_y - 2.0_wp * tunnel_wall_depth <= 2.0_wp * dy )  THEN
+             message_string = 'Tunnel width too small'
+             CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
+          ENDIF
+!
+!--       Tunnel axis along x
+          IF ( tunnel_width_y /= 9999999.9_wp )  THEN
+             IF ( tunnel_width_y > ( ny + 1 ) * dy )  THEN
+                message_string = 'Tunnel width too large'
+                CALL message( 'init_grid', 'PA0207', 1, 2, 0, 6, 0 )
+             ENDIF
+
+             txs_out = INT( ( nx + 1 ) * 0.5_wp * dx - tunnel_length * 0.5_wp )
+             txe_out = INT( ( nx + 1 ) * 0.5_wp * dx + tunnel_length * 0.5_wp )
+             txs_in  = txs_out
+             txe_in  = txe_out
+
+             tys_out = INT( ( ny + 1 ) * 0.5_wp * dy - tunnel_width_y * 0.5_wp )
+             tye_out = INT( ( ny + 1 ) * 0.5_wp * dy + tunnel_width_y * 0.5_wp )
+             tys_in  = INT( ( ny + 1 ) * 0.5_wp * dy -                         &
+                                     ( tunnel_width_y * 0.5_wp - td ) )
+             tye_in  = INT( ( ny + 1 ) * 0.5_wp * dy +                         &
+                                     ( tunnel_width_y * 0.5_wp - td ) )
+          ENDIF
+
+          topo_3d = 0
+          DO  i = nxl, nxr
+             DO  j = nys, nyn
+!
+!--             Use heaviside function to model outer tunnel surface
+                hv_out = th * 0.5_wp *                                         &
+                              ( ( SIGN( 1.0_wp, i * dx - txs_out ) + 1.0_wp )  &
+                              - ( SIGN( 1.0_wp, i * dx - txe_out ) + 1.0_wp ) )
+
+                hv_out = hv_out * 0.5_wp *                                     &
+                            ( ( SIGN( 1.0_wp, j * dy - tys_out ) + 1.0_wp )    &
+                            - ( SIGN( 1.0_wp, j * dy - tye_out ) + 1.0_wp ) )
+! 
+!--             Use heaviside function to model inner tunnel surface
+                hv_in  = ( th - td ) * 0.5_wp *                                &
+                                ( ( SIGN( 1.0_wp, i * dx - txs_in ) + 1.0_wp ) &
+                                - ( SIGN( 1.0_wp, i * dx - txe_in ) + 1.0_wp ) )
+
+                hv_in = hv_in * 0.5_wp *                                       &
+                                ( ( SIGN( 1.0_wp, j * dy - tys_in ) + 1.0_wp ) &
+                                - ( SIGN( 1.0_wp, j * dy - tye_in ) + 1.0_wp ) )
+!
+!--             Set flags at x-y-positions without any tunnel surface
+                IF ( hv_out - hv_in == 0.0_wp )  THEN
+                   topo_3d(nzb+1:nzt+1,j,i) = IBSET( topo_3d(nzb+1:nzt+1,j,i), 0 )
+!
+!--             Set flags at x-y-positions with tunnel surfaces
+                ELSE
+                   DO  k = nzb + 1, nzt + 1
+!
+!--                   Inner tunnel
+                      IF ( hv_out - hv_in == th )  THEN
+                         IF ( zw(k) <= hv_out )  THEN
+                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                         ELSE
+                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                         ENDIF
+                      ENDIF
+!
+!--                   Lateral tunnel walls
+                      IF ( hv_out - hv_in == td )  THEN
+                         IF ( zw(k) <= hv_in )  THEN 
+                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                         ELSEIF ( zw(k) > hv_in  .AND.  zw(k) <= hv_out )  THEN 
+                            topo_3d(k,j,i) = IBCLR( topo_3d(k,j,i), 0 )
+                         ELSEIF ( zw(k) > hv_out )  THEN 
+                            topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                         ENDIF
+                      ENDIF
+                   ENDDO
+                ENDIF
+             ENDDO
+          ENDDO
+
+          nzb_local = 0
+!
+!--       level of detail is required for output routines. Here, 3D topography. 
+          lod = 2
+
+          CALL exchange_horiz_int( topo_3d, nbgp )
 
        CASE ( 'read_from_file' )
 
+          ALLOCATE ( oro_height(nys:nyn,nxl:nxr)  )
           ALLOCATE ( topo_height(nys:nyn,nxl:nxr) )
+          oro_height  = 0.0_wp
+          topo_height = 0.0_wp
 
           DO  ii = 0, io_blocks-1
@@ -717,35 +856,100 @@
 !
 !--             Arbitrary irregular topography data in PALM format (exactly
-!--             matching the grid size and total domain size)
-                OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),       &
-                          STATUS='OLD', FORM='FORMATTED', ERR=10 )
-!
-!--             Read topography PE-wise. Rows are read from nyn to nys, columns
-!--             are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
-                skip_n_rows = 0
-                DO WHILE ( skip_n_rows < ny - nyn )
-                   READ( 90, * )  
-                   skip_n_rows = skip_n_rows + 1
-                ENDDO
-!
-!--             Read data from nyn to nys and nxl to nxr. Therefore, skip 
-!--             column until nxl-1 is reached
-                DO  j = nyn, nys, -1
-                   READ( 90, *, ERR=11, END=11 )                               &
+!--             matching the grid size and total domain size). 
+!--             First, check if NetCDF file for topography exist or not.
+!--             This case, read topography from NetCDF, else read it from 
+!--             ASCII file.
+#if defined ( __netcdf )
+                INQUIRE( FILE='TOPOGRAPHY_DATA_NC'//TRIM( coupling_char ),     &
+                         EXIST=netcdf_extend )
+!
+!--             NetCDF branch    
+                IF ( netcdf_extend )  THEN
+!
+!--                Open file in read-only mode
+                   CALL netcdf_open_read_file( 'TOPOGRAPHY_DATA_NC',           &
+                                               id_topo, 20 )  !Error number still need to be set properly 
+
+!
+!--                Read terrain height. Reading is done PE-wise, i.e. each
+!--                processor reads its own domain. Reading is realized
+!--                via looping over x-dimension, i.e. calling 
+!--                netcdf_get_variable reads topography along y for given x. 
+!--                Orography is 2D. 
+                   DO  i = nxl, nxr
+                      CALL netcdf_get_variable( id_topo, 'orography_0',        &
+                                                i, oro_height(:,i), 20 )  !Error number still need to be set properly 
+                   ENDDO
+!
+!--                Read attribute lod (level of detail), required for variable 
+!--                buildings_0 
+                   CALL netcdf_get_attribute( id_topo, "lod", lod, .FALSE.,    &
+                                              20, 'buildings_0' ) !Error number still need to be set properly 
+!
+!--                Read building height
+!--                2D for lod = 1, 3D for lod = 2
+                   IF ( lod == 1 )  THEN
+                      DO  i = nxl, nxr
+                         CALL netcdf_get_variable( id_topo, 'buildings_0',     &
+                                                   i, topo_height(:,i), 20 )  !Error number still need to be set properly 
+                      ENDDO
+
+                   ELSEIF ( lod == 2 )  THEN
+!
+!--                   Read data PE-wise. Read yz-slices.
+                      DO  i = nxl, nxr
+                         DO  j = nys, nyn
+                            CALL netcdf_get_variable( id_topo, 'buildings_0',  &
+                                                      i, j, topo_3d(:,j,i), 20 )  !Error number still need to be set properly 
+                         ENDDO
+                      ENDDO
+                   ELSE 
+                      message_string = 'NetCDF attribute lod ' //              &
+                                       '(level of detail) is not set properly.'
+                      CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 ) !Error number still need to be set properly 
+                   ENDIF
+!
+!--                Close topography input file
+                   CALL netcdf_close_file( id_topo, 20 )
+#endif
+
+!
+!--             ASCII branch. Please note, reading of 3D topography is not
+!--             supported in ASCII format. Further, no distinction is made 
+!--             between orography and buildings
+                ELSE
+
+                   OPEN( 90, FILE='TOPOGRAPHY_DATA'//TRIM( coupling_char ),    &
+                         STATUS='OLD', FORM='FORMATTED', ERR=10 )
+!
+!--                Read topography PE-wise. Rows are read from nyn to nys, columns
+!--                are read from nxl to nxr. At first, ny-nyn rows need to be skipped.
+                   skip_n_rows = 0
+                   DO WHILE ( skip_n_rows < ny - nyn )
+                      READ( 90, * )  
+                      skip_n_rows = skip_n_rows + 1
+                   ENDDO
+!
+!--                Read data from nyn to nys and nxl to nxr. Therefore, skip 
+!--                column until nxl-1 is reached
+                   DO  j = nyn, nys, -1
+                      READ( 90, *, ERR=11, END=11 )                            &
                                               ( dum, i = 0, nxl-1 ),           &
                                               ( topo_height(j,i), i = nxl, nxr )
-                ENDDO
-
-                GOTO 12
+                   ENDDO
+
+                   GOTO 12
           
- 10             message_string = 'file TOPOGRAPHY'//TRIM( coupling_char )//    &
-                                 ' does not exist'
-                CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 )
-
- 11             message_string = 'errors in file TOPOGRAPHY_DATA'//            &
-                                 TRIM( coupling_char )
-                CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 )
-
- 12             CLOSE( 90 )
+ 10                message_string = 'file TOPOGRAPHY'//TRIM( coupling_char )// &
+                                    ' does not exist'
+                   CALL message( 'init_grid', 'PA0208', 1, 2, 0, 6, 0 )
+
+ 11                message_string = 'errors in file TOPOGRAPHY_DATA'//         &
+                                    TRIM( coupling_char )
+                   CALL message( 'init_grid', 'PA0209', 1, 2, 0, 6, 0 )
+
+ 12                CLOSE( 90 )
+
+                ENDIF
 
              ENDIF
@@ -761,15 +965,21 @@
              DO  j = nys, nyn
                 IF ( .NOT. ocean )  THEN
-                   nzb_local(j,i) = MINLOC( ABS( zw - topo_height(j,i) ), 1 ) - 1
-                   IF ( ABS( zw(nzb_local(j,i)  ) - topo_height(j,i) ) ==      &
-                        ABS( zw(nzb_local(j,i)+1) - topo_height(j,i) )    )    &
+                   nzb_local(j,i) = MINLOC( ABS( zw - topo_height(j,i)         &
+                                                    - oro_height(j,i) ), 1 ) - 1
+                   IF ( ABS( zw(nzb_local(j,i)  ) - topo_height(j,i)           &
+                                                  - oro_height(j,i)  ) ==      &
+                        ABS( zw(nzb_local(j,i)+1) - topo_height(j,i)           &
+                                                  - oro_height(j,i)  )    )    &
                       nzb_local(j,i) = nzb_local(j,i) + 1
                 ELSE
                    nzb_local(j,i) = MINLOC( ABS( zw - zw(0)                    &
-                                                    - topo_height(j,i) ), 1 ) - 1
+                                                    - topo_height(j,i)         &
+                                                    - oro_height(j,i) ), 1 ) - 1
                    IF ( ABS( zw(nzb_local(j,i)  ) - zw(0)                      &
-                                                  - topo_height(j,i) )  ==     &
+                                                  - topo_height(j,i)           &
+                                                  - oro_height(j,i)  )  ==     &
                         ABS( zw(nzb_local(j,i)+1) - zw(0)                      &
-                                                  - topo_height(j,i) )    )    &
+                                                  - topo_height(j,i)           &
+                                                  - oro_height(j,i)  )    )    &
                       nzb_local(j,i) = nzb_local(j,i) + 1
                 ENDIF
@@ -778,4 +988,5 @@
           ENDDO
 
+          DEALLOCATE ( oro_height  )
           DEALLOCATE ( topo_height )
 !
@@ -836,10 +1047,25 @@
              CALL message( 'init_grid', 'PA0430', 0, 0, 0, 6, 0 )
           ENDIF
+
+!
+!--       Set bit array to mask topography. Only required for lod = 1
+          IF ( lod == 1 )  THEN
+             DO  i = nxlg, nxrg
+                DO  j = nysg, nyng
+                   topo_3d(nzb_local(j,i)+1:nzt+1,j,i) =                          &
+                                 IBSET( topo_3d(nzb_local(j,i)+1:nzt+1,j,i), 0 ) 
+                ENDDO
+             ENDDO
+          ENDIF
 !
 !--       Exchange ghost-points, as well as add cyclic or Neumann boundary 
 !--       conditions.
+          CALL exchange_horiz_int( topo_3d, nbgp )
           CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
           
           IF ( .NOT. bc_ns_cyc )  THEN
+             IF ( nys == 0  )  topo_3d(:,-1,:)   = topo_3d(:,0,:)
+             IF ( nyn == ny )  topo_3d(:,ny+1,:) = topo_3d(:,ny,:)
+
              IF ( nys == 0  )  nzb_local(-1,:)   = nzb_local(0,:)
              IF ( nyn == ny )  nzb_local(ny+1,:) = nzb_local(ny,:)
@@ -847,7 +1073,11 @@
 
           IF ( .NOT. bc_lr_cyc )  THEN
+             IF ( nxl == 0  )  topo_3d(:,:,-1)   = topo_3d(:,:,0)
+             IF ( nxr == nx )  topo_3d(:,:,nx+1) = topo_3d(:,:,nx)     
+
              IF ( nxl == 0  )  nzb_local(:,-1)   = nzb_local(:,0)
              IF ( nxr == nx )  nzb_local(:,nx+1) = nzb_local(:,nx)          
           ENDIF
+
 
        CASE DEFAULT
@@ -857,25 +1087,7 @@
 !--       case in the user interface. There, the subroutine user_init_grid 
 !--       checks which of these two conditions applies.
-          CALL user_init_grid( nzb_local )
+          CALL user_init_grid( topo_3d )
 
     END SELECT
-!
-!-- Determine the maximum level of topography. Furthermore it is used for
-!-- steering the degradation of order of the applied advection scheme.
-!-- In case of non-cyclic lateral boundaries, the order of the advection
-!-- scheme has to be reduced up to nzt (required at the lateral boundaries).
-#if defined( __parallel )
-    CALL MPI_ALLREDUCE( MAXVAL( nzb_local ) + 1, nzb_max, 1, MPI_INTEGER,      &
-                        MPI_MAX, comm2d, ierr )
-#else
-    nzb_max = MAXVAL( nzb_local ) + 1
-#endif
-    IF ( inflow_l .OR. outflow_l .OR. inflow_r .OR. outflow_r .OR.             &
-         inflow_n .OR. outflow_n .OR. inflow_s .OR. outflow_s .OR.             &
-         nest_domain )                                                         &
-    THEN
-       nzb_max = nzt
-    ENDIF
-
 !
 !-- Consistency checks and index array initialization are only required for
@@ -901,4 +1113,41 @@
                                 '&MAXVAL( nzb_local ) = ', nzb_local_max
           CALL message( 'init_grid', 'PA0210', 1, 2, 0, 6, 0 )
+       ENDIF
+!
+!--    In case of non-flat topography, check whether the convention how to 
+!--    define the topography grid has been set correctly, or whether the default
+!--    is applicable. If this is not possible, abort.
+       IF ( TRIM( topography_grid_convention ) == ' ' )  THEN
+          IF ( TRIM( topography ) /= 'single_building' .AND.                   &
+               TRIM( topography ) /= 'single_street_canyon' .AND.              &
+               TRIM( topography ) /= 'tunnel'  .AND.                           &
+               TRIM( topography ) /= 'read_from_file')  THEN
+!--          The default value is not applicable here, because it is only valid
+!--          for the two standard cases 'single_building' and 'read_from_file'
+!--          defined in init_grid.
+             WRITE( message_string, * )                                        &
+                  'The value for "topography_grid_convention" ',               &
+                  'is not set. Its default value is & only valid for ',        &
+                  '"topography" = ''single_building'', ',                      &
+                  '''single_street_canyon'' & or ''read_from_file''.',         &
+                  ' & Choose ''cell_edge'' or ''cell_center''.'
+             CALL message( 'init_grid', 'PA0239', 1, 2, 0, 6, 0 )
+          ELSE
+!--          The default value is applicable here.
+!--          Set convention according to topography.
+             IF ( TRIM( topography ) == 'single_building' .OR.                 &
+                  TRIM( topography ) == 'single_street_canyon' )  THEN
+                topography_grid_convention = 'cell_edge'
+             ELSEIF ( TRIM( topography ) == 'read_from_file'  .OR.             &
+                      TRIM( topography ) == 'tunnel')  THEN
+                topography_grid_convention = 'cell_center'
+             ENDIF
+          ENDIF
+       ELSEIF ( TRIM( topography_grid_convention ) /= 'cell_edge' .AND.        &
+                TRIM( topography_grid_convention ) /= 'cell_center' )  THEN
+          WRITE( message_string, * )                                           &
+               'The value for "topography_grid_convention" is ',               &
+               'not recognized. & Choose ''cell_edge'' or ''cell_center''.'
+          CALL message( 'init_grid', 'PA0240', 1, 2, 0, 6, 0 )
        ENDIF
 
@@ -964,11 +1213,38 @@
                 nzb_local(j,i) = MIN( nzb_local(j,i), nzb_local(j+1,i) )
              ENDDO
-          ENDDO
+          ENDDO  
 !
 !--       Exchange ghost points          
           CALL exchange_horiz_2d_int( nzb_local, nys, nyn, nxl, nxr, nbgp )
+!
+!--       Apply cell-edge convention also for 3D topo array. The former setting
+!--       of nzb_local will be removed later. 
+          DO  j = nys+1, nyn+1
+             DO  i = nxl-1, nxr
+                DO  k = nzb, nzt+1
+                   IF ( BTEST( topo_3d(k,j,i), 0 )  .OR.                       &
+                        BTEST( topo_3d(k,j,i+1), 0 ) )                         &
+                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                ENDDO
+             ENDDO
+          ENDDO      
+          CALL exchange_horiz_int( topo_3d, nbgp )   
+
+          DO  i = nxl, nxr+1
+             DO  j = nys-1, nyn
+                DO  k = nzb, nzt+1
+                   IF ( BTEST( topo_3d(k,j,i), 0 )  .OR.                       &
+                        BTEST( topo_3d(k,j+1,i), 0 ) )                         &
+                      topo_3d(k,j,i) = IBSET( topo_3d(k,j,i), 0 )
+                ENDDO
+             ENDDO
+          ENDDO  
+          CALL exchange_horiz_int( topo_3d, nbgp )
+   
        ENDIF
+      
 !
 !--    Initialize index arrays nzb_s_inner and nzb_w_inner
+
        nzb_s_inner = nzb_local
        nzb_w_inner = nzb_local
@@ -1103,62 +1379,4 @@
        CALL exchange_horiz_2d_int( nzb_s_outer, nys, nyn, nxl, nxr, nbgp )
 
-!
-!--    Allocate and set the arrays containing the topography height
-       ALLOCATE( zu_s_inner(0:nx+1,0:ny+1), zw_w_inner(0:nx+1,0:ny+1),         &
-                 zu_s_inner_l(0:nx+1,0:ny+1), zw_w_inner_l(0:nx+1,0:ny+1) )
-                 
-       zu_s_inner   = 0.0_wp
-       zw_w_inner   = 0.0_wp
-       zu_s_inner_l = 0.0_wp
-       zw_w_inner_l = 0.0_wp
-       
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             zu_s_inner_l(i,j) = zu(nzb_local(j,i))
-             zw_w_inner_l(i,j) = zw(nzb_local(j,i))
-          ENDDO
-       ENDDO
-       
-#if defined( __parallel )
-       CALL MPI_REDUCE( zu_s_inner_l, zu_s_inner, (nx+2)*(ny+2),         &
-                           MPI_REAL, MPI_SUM, 0, comm2d, ierr )       
-       CALL MPI_REDUCE( zw_w_inner_l, zw_w_inner, (nx+2)*(ny+2),         &
-                           MPI_REAL, MPI_SUM, 0, comm2d, ierr )  
-#else
-       zu_s_inner = zu_s_inner_l
-       zw_w_inner = zw_w_inner_l
-#endif
-
-      DEALLOCATE( zu_s_inner_l, zw_w_inner_l )
-      IF ( myid /= 0 )  DEALLOCATE( zu_s_inner, zw_w_inner )
-!
-!--   Set south and left ghost points, required for netcdf output
-      IF ( myid == 0 )  THEN
-         IF( bc_lr_cyc )  THEN
-            zu_s_inner(nx+1,:) = zu_s_inner(0,:)
-            zw_w_inner(nx+1,:) = zw_w_inner(0,:)
-         ELSE
-            zu_s_inner(nx+1,:) = zu_s_inner(nx,:)
-            zw_w_inner(nx+1,:) = zw_w_inner(nx,:)
-         ENDIF
-         IF( bc_ns_cyc )  THEN
-            zu_s_inner(:,ny+1) = zu_s_inner(:,0)
-            zw_w_inner(:,ny+1) = zw_w_inner(:,0)
-         ELSE
-            zu_s_inner(:,ny+1) = zu_s_inner(:,ny)
-            zw_w_inner(:,ny+1) = zw_w_inner(:,ny)
-         ENDIF
-      ENDIF
-!
-!--    Set flag arrays to be used for masking of grid points
-       DO  i = nxlg, nxrg
-          DO  j = nysg, nyng
-             DO  k = nzb, nzt+1
-                IF ( k <= nzb_s_inner(j,i) )  rflags_s_inner(k,j,i) = 0.0_wp
-                IF ( k <= nzb_s_inner(j,i) )  rflags_invers(j,i,k)  = 0.0_wp
-             ENDDO
-          ENDDO
-       ENDDO
-
     ENDIF
 !
@@ -1166,4 +1384,34 @@
 !-- grid-levels further below.
     DEALLOCATE( nzb_tmp )
+
+!
+!-- Determine the maximum level of topography. It is used for
+!-- steering the degradation of order of the applied advection scheme.
+!-- In case of non-cyclic lateral boundaries, the order of the advection
+!-- scheme has to be reduced up to nzt (required at the lateral boundaries).
+    k_top = 0
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb, nzt + 1
+             k_top = MAX( k_top, MERGE( k, 0,                                  &
+                                        .NOT. BTEST( topo_3d(k,j,i), 0 ) ) )
+          ENDDO
+       ENDDO
+    ENDDO
+#if defined( __parallel )
+    CALL MPI_ALLREDUCE( k_top + 1, nzb_max, 1, MPI_INTEGER,                    & !is +1 really necessary here?
+                        MPI_MAX, comm2d, ierr )
+#else
+    nzb_max = k_top + 1
+#endif
+    IF ( inflow_l .OR. outflow_l .OR. inflow_r .OR. outflow_r .OR.             &
+         inflow_n .OR. outflow_n .OR. inflow_s .OR. outflow_s .OR.             &
+         nest_domain )                                                         &
+    THEN
+       nzb_max = nzt
+    ENDIF
+!
+!-- Finally, if topography extents up to the model top, limit nzb_max to nzt.
+    nzb_max = MIN( nzb_max, nzt ) 
 
 !
@@ -1172,11 +1420,7 @@
 !-- applied 
     IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
-       nzb_diff_u         = nzb_u_inner + 2
-       nzb_diff_v         = nzb_v_inner + 2
        nzb_diff_s_inner   = nzb_s_inner + 2
        nzb_diff_s_outer   = nzb_s_outer + 2
     ELSE
-       nzb_diff_u         = nzb_u_inner + 1
-       nzb_diff_v         = nzb_v_inner + 1
        nzb_diff_s_inner   = nzb_s_inner + 1
        nzb_diff_s_outer   = nzb_s_outer + 1
@@ -1184,108 +1428,289 @@
 
 !
-!-- Calculation of wall switches and factors required by diffusion_u/v.f90 and 
-!-- for limitation of near-wall mixing length l_wall further below
-    corner_nl = 0
-    corner_nr = 0
-    corner_sl = 0
-    corner_sr = 0
-    wall_l    = 0
-    wall_n    = 0
-    wall_r    = 0
-    wall_s    = 0
-
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-!
-!--       u-component
-          IF ( nzb_u_outer(j,i) > nzb_u_outer(j+1,i) )  THEN
-             wall_u(j,i) = 1.0_wp   ! north wall (location of adjacent fluid)
-             fym(j,i)    = 0.0_wp
-             fyp(j,i)    = 1.0_wp
-          ELSEIF ( nzb_u_outer(j,i) > nzb_u_outer(j-1,i) )  THEN
-             wall_u(j,i) = 1.0_wp   ! south wall (location of adjacent fluid)
-             fym(j,i)    = 1.0_wp
-             fyp(j,i)    = 0.0_wp
-          ENDIF
-!
-!--       v-component
-          IF ( nzb_v_outer(j,i) > nzb_v_outer(j,i+1) )  THEN
-             wall_v(j,i) = 1.0_wp   ! rigth wall (location of adjacent fluid)
-             fxm(j,i)    = 0.0_wp
-             fxp(j,i)    = 1.0_wp
-          ELSEIF ( nzb_v_outer(j,i) > nzb_v_outer(j,i-1) )  THEN
-             wall_v(j,i) = 1.0_wp   ! left wall (location of adjacent fluid)
-             fxm(j,i)    = 1.0_wp
-             fxp(j,i)    = 0.0_wp
-          ENDIF
-!
-!--       w-component, also used for scalars, separate arrays for shear 
-!--       production of tke
-          IF ( nzb_w_outer(j,i) > nzb_w_outer(j+1,i) )  THEN
-             wall_e_y(j,i) =  1.0_wp   ! north wall (location of adjacent fluid)
-             wall_w_y(j,i) =  1.0_wp
-             fwym(j,i)     =  0.0_wp
-             fwyp(j,i)     =  1.0_wp
-          ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j-1,i) )  THEN
-             wall_e_y(j,i) = -1.0_wp   ! south wall (location of adjacent fluid)
-             wall_w_y(j,i) =  1.0_wp
-             fwym(j,i)     =  1.0_wp
-             fwyp(j,i)     =  0.0_wp
-          ENDIF
-          IF ( nzb_w_outer(j,i) > nzb_w_outer(j,i+1) )  THEN
-             wall_e_x(j,i) =  1.0_wp   ! right wall (location of adjacent fluid)
-             wall_w_x(j,i) =  1.0_wp
-             fwxm(j,i)     =  0.0_wp
-             fwxp(j,i)     =  1.0_wp
-          ELSEIF ( nzb_w_outer(j,i) > nzb_w_outer(j,i-1) )  THEN
-             wall_e_x(j,i) = -1.0_wp   ! left wall (location of adjacent fluid)
-             wall_w_x(j,i) =  1.0_wp
-             fwxm(j,i)     =  1.0_wp
-             fwxp(j,i)     =  0.0_wp
-          ENDIF
-!
-!--       Wall and corner locations inside buildings for limitation of
-!--       near-wall mixing length l_wall
-          IF ( nzb_s_inner(j,i) > nzb_s_inner(j+1,i) )  THEN
-
-             wall_n(j,i) = nzb_s_inner(j+1,i) + 1            ! North wall
-
-             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) )  THEN
-                corner_nl(j,i) = MAX( nzb_s_inner(j+1,i),  & ! Northleft corner
-                                      nzb_s_inner(j,i-1) ) + 1
-             ENDIF
-
-             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) )  THEN
-                corner_nr(j,i) = MAX( nzb_s_inner(j+1,i),  & ! Northright corner
-                                      nzb_s_inner(j,i+1) ) + 1
-             ENDIF
-
-          ENDIF
-
-          IF ( nzb_s_inner(j,i) > nzb_s_inner(j-1,i) )  THEN
-
-             wall_s(j,i) = nzb_s_inner(j-1,i) + 1            ! South wall
-             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) )  THEN
-                corner_sl(j,i) = MAX( nzb_s_inner(j-1,i),  & ! Southleft corner
-                                      nzb_s_inner(j,i-1) ) + 1
-             ENDIF
-
-             IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) )  THEN
-                corner_sr(j,i) = MAX( nzb_s_inner(j-1,i),  & ! Southright corner
-                                      nzb_s_inner(j,i+1) ) + 1
-             ENDIF
-
-          ENDIF
-
-          IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i-1) )  THEN
-             wall_l(j,i) = nzb_s_inner(j,i-1) + 1            ! Left wall
-          ENDIF
-
-          IF ( nzb_s_inner(j,i) > nzb_s_inner(j,i+1) )  THEN
-             wall_r(j,i) = nzb_s_inner(j,i+1) + 1            ! Right wall
-          ENDIF
+!-- Set-up topography flags. First, set flags only for s, u, v and w-grid.
+!-- Further special flags will be set in following loops. 
+    wall_flags_0 = 0
+    DO  j = nys, nyn
+       DO  i = nxl, nxr
+          DO  k = nzb, nzt+1
+!
+!--          scalar grid
+             IF ( BTEST( topo_3d(k,j,i), 0 ) )                                 &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
+!
+!--          v grid
+             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
+                  BTEST( topo_3d(k,j-1,i), 0 ) )                               &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
+!
+!--     To do: set outer arrays on basis of topo_3d array, adjust for downward-facing walls
+!--          s grid outer array
+             IF ( k >= nzb_s_outer(j,i) )                                      &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
+!
+!--          s grid outer array
+             IF ( k >= nzb_u_outer(j,i) )                                      &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 26 )
+!
+!--          s grid outer array
+             IF ( k >= nzb_v_outer(j,i) )                                      &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
+!
+!--          w grid outer array
+             IF ( k >= nzb_w_outer(j,i) )                                      &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 28 )
+          ENDDO
+
+          DO k = nzb, nzt
+!
+!--          w grid
+             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
+                  BTEST( topo_3d(k+1,j,i), 0 ) )                               &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
+          ENDDO
+          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 3 )
 
        ENDDO
     ENDDO
+!
+!-- u grid. Note, reverse 
+!-- memory access is required for setting flag on u-grid
+    DO  j = nys, nyn
+       DO  i = nxl, nxr
+          DO k = nzb, nzt+1
+             IF ( BTEST( topo_3d(k,j,i),   0 )  .AND.                          &
+                  BTEST( topo_3d(k,j,i-1), 0 ) )                               &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
+          ENDDO
+       ENDDO
+    ENDDO
+!
+!-- Set further special flags
+    DO i = nxl, nxr
+       DO j = nys, nyn
+          DO k = nzb, nzt+1
+!
+!--          scalar grid, former nzb_diff_s_inner.
+!--          Note, use this flag also to mask topography in diffusion_u and 
+!--          diffusion_v along the vertical direction. In case of 
+!--          use_surface_fluxes, fluxes are calculated via MOST, else, simple
+!--          gradient approach is applied. Please note, in case of u- and v-
+!--          diffuison, a small error is made at edges (on the east side for u,
+!--          at the north side for v), since topography on scalar grid point
+!--          is used instead of topography on u/v-grid. As number of topography grid
+!--          points on uv-grid is different than s-grid, different number of 
+!--          surface elements would be required. In order to avoid this, 
+!--          treat edges (u(k,j,i+1)) simply by a gradient approach, i.e. these
+!--          points are not masked within diffusion_u. Tests had shown that the
+!--          effect on the flow is negligible. 
+             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
+                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
+             ELSE
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
+             ENDIF
+
+          ENDDO
+!
+!--       Special flag to control vertical diffusion at model top - former 
+!--       nzt_diff
+          wall_flags_0(:,j,i) = IBSET( wall_flags_0(:,j,i), 9 )
+          IF ( use_top_fluxes )                                                &
+             wall_flags_0(nzt:nzt+1,j,i) = IBCLR( wall_flags_0(nzt:nzt+1,j,i), 9 )
+
+          DO k = nzb+1, nzt
+!
+!--          Special flag on u grid, former nzb_u_inner + 1, required   
+!--          for disturb_field and initialization. Do not disturb directly at
+!--          topography, as well as initialize u with zero one grid point outside
+!--          of topography.
+             IF ( BTEST( wall_flags_0(k-1,j,i), 1 )  .AND.                     &
+                  BTEST( wall_flags_0(k,j,i),   1 )  .AND.                     &
+                  BTEST( wall_flags_0(k+1,j,i), 1 ) )                          &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 20 )
+!
+!--          Special flag on v grid, former nzb_v_inner + 1, required   
+!--          for disturb_field and initialization. Do not disturb directly at
+!--          topography, as well as initialize v with zero one grid point outside
+!--          of topography.
+             IF ( BTEST( wall_flags_0(k-1,j,i), 2 )  .AND.                     &
+                  BTEST( wall_flags_0(k,j,i),   2 )  .AND.                     &
+                  BTEST( wall_flags_0(k+1,j,i), 2 ) )                          &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
+!
+!--          Special flag on scalar grid, former nzb_s_inner+1. Used for 
+!--          lpm_sgs_tke
+             IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                     &
+                  BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
+                  BTEST( wall_flags_0(k+1,j,i), 0 ) )                          &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 25 )
+!
+!--          Special flag on scalar grid, nzb_diff_s_outer - 1, required in 
+!--          in production_e
+             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
+                IF ( BTEST( wall_flags_0(k,j,i),   24 )  .AND.                 &
+                     BTEST( wall_flags_0(k-1,j,i), 24 )  .AND.                 &
+                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 29 )
+             ELSE
+                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 29 )
+             ENDIF
+!
+!--          Special flag on scalar grid, nzb_diff_s_outer - 1, required in 
+!--          in production_e
+             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
+                IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
+                     BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                  &
+                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
+             ELSE
+                IF ( BTEST( wall_flags_0(k,j,i), 0 ) )                         &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
+             ENDIF
+          ENDDO
+!
+!--       Flags indicating downward facing walls
+          DO k = nzb+1, nzt
+!
+!--          Scalar grid
+             IF ( BTEST( wall_flags_0(k-1,j,i), 0 )  .AND.                     &
+            .NOT. BTEST( wall_flags_0(k,j,i), 0   ) )                          & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 ) 
+!
+!--          Downward facing wall on u grid
+             IF ( BTEST( wall_flags_0(k-1,j,i), 1 )  .AND.                     &
+            .NOT. BTEST( wall_flags_0(k,j,i), 1   ) )                          & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 15 )
+!
+!--          Downward facing wall on v grid
+             IF ( BTEST( wall_flags_0(k-1,j,i), 2 )  .AND.                     &
+            .NOT. BTEST( wall_flags_0(k,j,i), 2   ) )                          & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 17 )
+!
+!--          Downward facing wall on w grid
+             IF ( BTEST( wall_flags_0(k-1,j,i), 3 )  .AND.                     &
+            .NOT. BTEST( wall_flags_0(k,j,i), 3 ) )                            & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 19 )
+          ENDDO
+!
+!--       Flags indicating upward facing walls
+          DO k = nzb, nzt
+!
+!--          Upward facing wall on scalar grid
+             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   0 )  .AND.               &
+                        BTEST( wall_flags_0(k+1,j,i), 0 ) )                    & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
+!
+!--          Upward facing wall on u grid
+             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   1 )  .AND.               &
+                        BTEST( wall_flags_0(k+1,j,i), 1 ) )                    & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
+
+!
+!--          Upward facing wall on v grid
+             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   2 )  .AND.               &
+                        BTEST( wall_flags_0(k+1,j,i), 2 ) )                    & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
+
+!
+!--          Upward facing wall on w grid
+             IF ( .NOT. BTEST( wall_flags_0(k,j,i),   3 )  .AND.               &
+                        BTEST( wall_flags_0(k+1,j,i), 3 ) )                    & 
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
+!
+!--          Special flag on scalar grid, former nzb_s_inner
+             IF ( BTEST( wall_flags_0(k,j,i), 0 )  .OR.                        &
+                  BTEST( wall_flags_0(k,j,i), 12 ) .OR.                        &
+                  BTEST( wall_flags_0(k,j,i), 13 ) )                           &
+                wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
+!
+!--          Special flag on scalar grid, nzb_diff_s_inner - 1, required for 
+!--          flow_statistics
+             IF ( constant_flux_layer  .OR.  use_surface_fluxes )  THEN
+                IF ( BTEST( wall_flags_0(k,j,i),   0 )  .AND.                  &
+                     BTEST( wall_flags_0(k+1,j,i), 0 ) )                       &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
+             ELSE
+                IF ( BTEST( wall_flags_0(k,j,i), 22 ) )                        &
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
+             ENDIF
+
+
+          ENDDO
+          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 22 )
+          wall_flags_0(nzt+1,j,i) = IBSET( wall_flags_0(nzt+1,j,i), 23 )
+       ENDDO
+    ENDDO
+!
+!-- Exchange ghost points for wall flags
+    CALL exchange_horiz_int( wall_flags_0, nbgp )
+!
+!-- Set boundary conditions also for flags. Can be interpreted as Neumann
+!-- boundary conditions for topography. 
+    IF ( .NOT. bc_ns_cyc )  THEN
+       IF ( nys == 0  )  wall_flags_0(:,-1,:)   = wall_flags_0(:,0,:)
+       IF ( nyn == ny )  wall_flags_0(:,ny+1,:) = wall_flags_0(:,ny,:)
+    ENDIF
+    IF ( .NOT. bc_lr_cyc )  THEN
+       IF ( nxl == 0  )  wall_flags_0(:,:,-1)   = wall_flags_0(:,:,0)
+       IF ( nxr == nx )  wall_flags_0(:,:,nx+1) = wall_flags_0(:,:,nx)           
+    ENDIF
+
+!
+!-- Initialize boundary conditions via surface type
+    CALL init_bc
+!
+!-- Allocate and set topography height arrays required for data output
+    IF ( TRIM( topography ) /= 'flat' )  THEN
+!
+!--    Allocate and set the arrays containing the topography height
+
+       IF ( nxr == nx  .AND.  nyn /= ny )  THEN
+          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn),                             &
+                    zw_w_inner(nxl:nxr+1,nys:nyn) )
+       ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
+          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn+1),                             &
+                    zw_w_inner(nxl:nxr,nys:nyn+1) )
+       ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
+          ALLOCATE( zu_s_inner(nxl:nxr+1,nys:nyn+1),                           &
+                    zw_w_inner(nxl:nxr+1,nys:nyn+1) )
+       ELSE
+          ALLOCATE( zu_s_inner(nxl:nxr,nys:nyn),                               &
+                    zw_w_inner(nxl:nxr,nys:nyn) )
+       ENDIF
+
+       zu_s_inner   = 0.0_wp
+       zw_w_inner   = 0.0_wp
+!
+!--    Determine local topography height on scalar and w-grid. Note, setting
+!--    lateral boundary values is not necessary, realized via wall_flags_0 
+!--    array. Further, please note that loop bounds are different from 
+!--    nxl to nxr and nys to nyn on south and right model boundary, hence, 
+!--    use intrinsic lbound and ubound functions to infer array bounds.
+       DO  i = lbound(zu_s_inner, 1), ubound(zu_s_inner, 1)
+          DO  j = lbound(zu_s_inner, 2), ubound(zu_s_inner, 2)
+!
+!--          Topography height on scalar grid. Therefore, determine index of 
+!--          upward-facing surface element on scalar grid (bit 12). 
+             zu_s_inner(i,j) = zu( MAXLOC( MERGE(                              &
+                                         1, 0, BTEST( wall_flags_0(:,j,i), 12 )&
+                                                ), DIM = 1                     &
+                                         ) - 1                                 &
+                                 )
+!
+!--          Topography height on w grid. Therefore, determine index of 
+!--          upward-facing surface element on w grid (bit 18).
+             zw_w_inner(i,j) = zw( MAXLOC( MERGE(                              &
+                                         1, 0, BTEST( wall_flags_0(:,j,i), 18 )&
+                                                ), DIM = 1                     &
+                                         ) - 1                                 &
+                                 )
+          ENDDO
+       ENDDO
+
+
+    ENDIF
+
 !
 !-- Calculate wall flag arrays for the multigrid method.
@@ -1450,8 +1875,8 @@
 !-- required in the ws-scheme, the arrays need to be allocated here as they are 
 !-- used in OpenACC directives. 
-    ALLOCATE( wall_flags_0(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
-              wall_flags_00(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-    wall_flags_0  = 0
-    wall_flags_00 = 0
+    ALLOCATE( advc_flags_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                     &
+              advc_flags_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+    advc_flags_1 = 0
+    advc_flags_2 = 0
 !
 !-- Init flags for ws-scheme to degrade order of the numerics near walls, i.e. 
@@ -1466,148 +1891,91 @@
 !-- Go through all points of the subdomain one by one and look for the closest
 !-- surface
-    IF ( TRIM(topography) /= 'flat' )  THEN
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-
-             nzb_si = nzb_s_inner(j,i)
-             vi     = vertical_influence(nzb_si)
-
-             IF ( wall_n(j,i) > 0 )  THEN
-!
-!--             North wall (y distance)
-                DO  k = wall_n(j,i), nzb_si
-                   l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i), 0.5_wp * dy )
-                ENDDO
-!
-!--             Above North wall (yz distance)
-                DO  k = nzb_si + 1, nzb_si + vi
-                   l_wall(k,j+1,i) = MIN( l_wall(k,j+1,i),                     &
-                                          SQRT( 0.25_wp * dy**2 +              &
-                                          ( zu(k) - zw(nzb_si) )**2 ) )
-                ENDDO
-!
-!--             Northleft corner (xy distance)
-                IF ( corner_nl(j,i) > 0 )  THEN
-                   DO  k = corner_nl(j,i), nzb_si
-                      l_wall(k,j+1,i-1) = MIN( l_wall(k,j+1,i-1), &
-                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
-                   ENDDO
-!
-!--                Above Northleft corner (xyz distance)
-                   DO  k = nzb_si + 1, nzb_si + vi
-                      l_wall(k,j+1,i-1) = MIN( l_wall(k,j+1,i-1),              &
-                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
-                                            ( zu(k) - zw(nzb_si) )**2 ) )
-                   ENDDO
-                ENDIF
-!
-!--             Northright corner (xy distance)
-                IF ( corner_nr(j,i) > 0 )  THEN
-                   DO  k = corner_nr(j,i), nzb_si
-                       l_wall(k,j+1,i+1) = MIN( l_wall(k,j+1,i+1),             &
-                                                0.5_wp * SQRT( dx**2 + dy**2 ) )
-                   ENDDO
-!
-!--                Above northright corner (xyz distance)
-                   DO  k = nzb_si + 1, nzb_si + vi
-                      l_wall(k,j+1,i+1) = MIN( l_wall(k,j+1,i+1),              &
-                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
-                                            ( zu(k) - zw(nzb_si) )**2 ) )
-                   ENDDO
-                ENDIF
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+!
+!--          Check if current gridpoint belongs to the atmosphere
+             IF ( BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
+!
+!--             Check for neighbouring grid-points.
+!--             Vertical distance, down
+                IF ( .NOT. BTEST( wall_flags_0(k-1,j,i), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_grid(k), zu(k) - zw(k-1) )
+!
+!--             Vertical distance, up
+                IF ( .NOT. BTEST( wall_flags_0(k+1,j,i), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_grid(k), zw(k) - zu(k) )
+!
+!--             y-distance
+                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .OR.             &
+                     .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dy )
+!
+!--             x-distance
+                IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .OR.             &
+                     .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                 &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k), 0.5_wp * dx )
+!
+!--              yz-distance (vertical edges, down)
+                 IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k-1,j+1,i), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dy**2 +                &
+                                       ( zu(k) - zw(k-1) )**2 ) )
+!
+!--              yz-distance (vertical edges, up)
+                 IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k+1,j+1,i), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dy**2 +                &
+                                       ( zw(k) - zu(k) )**2 ) )
+!
+!--              xz-distance (vertical edges, down)
+                 IF ( .NOT. BTEST( wall_flags_0(k-1,j,i-1), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k-1,j,i+1), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dx**2 +                &
+                                       ( zu(k) - zw(k-1) )**2 ) )
+!
+!--              xz-distance (vertical edges, up)
+                 IF ( .NOT. BTEST( wall_flags_0(k+1,j,i-1), 0 )  .OR.          &
+                      .NOT. BTEST( wall_flags_0(k+1,j,i+1), 0 )  )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * dx**2 +                &
+                                       ( zw(k) - zu(k) )**2 ) )
+!
+!--             xy-distance (horizontal edges)
+                IF ( .NOT. BTEST( wall_flags_0(k,j-1,i-1), 0 )  .OR.           &
+                     .NOT. BTEST( wall_flags_0(k,j+1,i-1), 0 )  .OR.           &
+                     .NOT. BTEST( wall_flags_0(k,j-1,i+1), 0 )  .OR.           &
+                     .NOT. BTEST( wall_flags_0(k,j+1,i+1), 0 ) )               &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * ( dx**2 + dy**2 ) ) )
+!
+!--             xyz distance (vertical and horizontal edges, down)
+                IF ( .NOT. BTEST( wall_flags_0(k-1,j-1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k-1,j+1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k-1,j-1,i+1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k-1,j+1,i+1), 0 ) )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
+                                              +  ( zu(k) - zw(k-1) )**2  ) )
+!
+!--             xyz distance (vertical and horizontal edges, up)
+                IF ( .NOT. BTEST( wall_flags_0(k+1,j-1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k+1,j+1,i-1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k+1,j-1,i+1), 0 )  .OR.         &
+                     .NOT. BTEST( wall_flags_0(k+1,j+1,i+1), 0 ) )             &
+                   l_wall(k,j,i) = MIN( l_wall(k,j,i), l_grid(k),              &
+                                        SQRT( 0.25_wp * ( dx**2 + dy**2 )      &
+                                              +  ( zw(k) - zu(k) )**2  ) )
+                  
              ENDIF
-
-             IF ( wall_s(j,i) > 0 )  THEN
-!
-!--             South wall (y distance)
-                DO  k = wall_s(j,i), nzb_si
-                   l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i), 0.5_wp * dy )
-                ENDDO
-!
-!--             Above south wall (yz distance)
-                DO  k = nzb_si + 1, nzb_si + vi
-                   l_wall(k,j-1,i) = MIN( l_wall(k,j-1,i),                     &
-                                          SQRT( 0.25_wp * dy**2 +              &
-                                          ( zu(k) - zw(nzb_si) )**2 ) )
-                ENDDO
-!
-!--             Southleft corner (xy distance)
-                IF ( corner_sl(j,i) > 0 )  THEN
-                   DO  k = corner_sl(j,i), nzb_si
-                      l_wall(k,j-1,i-1) = MIN( l_wall(k,j-1,i-1),              &
-                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
-                   ENDDO
-!
-!--                Above southleft corner (xyz distance)
-                   DO  k = nzb_si + 1, nzb_si + vi
-                      l_wall(k,j-1,i-1) = MIN( l_wall(k,j-1,i-1),              &
-                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
-                                            ( zu(k) - zw(nzb_si) )**2 ) )
-                   ENDDO
-                ENDIF
-!
-!--             Southright corner (xy distance)
-                IF ( corner_sr(j,i) > 0 )  THEN
-                   DO  k = corner_sr(j,i), nzb_si
-                      l_wall(k,j-1,i+1) = MIN( l_wall(k,j-1,i+1),              &
-                                               0.5_wp * SQRT( dx**2 + dy**2 ) )
-                   ENDDO
-!
-!--                Above southright corner (xyz distance)
-                   DO  k = nzb_si + 1, nzb_si + vi
-                      l_wall(k,j-1,i+1) = MIN( l_wall(k,j-1,i+1),              &
-                                            SQRT( 0.25_wp * (dx**2 + dy**2) +  &
-                                            ( zu(k) - zw(nzb_si) )**2 ) )
-                   ENDDO
-                ENDIF
-
-             ENDIF
-
-             IF ( wall_l(j,i) > 0 )  THEN
-!
-!--             Left wall (x distance)
-                DO  k = wall_l(j,i), nzb_si
-                   l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1), 0.5_wp * dx )
-                ENDDO
-!
-!--             Above left wall (xz distance)
-                DO  k = nzb_si + 1, nzb_si + vi
-                   l_wall(k,j,i-1) = MIN( l_wall(k,j,i-1),                     &
-                                       SQRT( 0.25_wp * dx**2 +                 &
-                                       ( zu(k) - zw(nzb_si) )**2 ) )
-                ENDDO
-             ENDIF
-
-             IF ( wall_r(j,i) > 0 )  THEN
-!
-!--             Right wall (x distance)
-                DO  k = wall_r(j,i), nzb_si
-                   l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1), 0.5_wp * dx )
-                ENDDO
-!
-!--             Above right wall (xz distance)
-                DO  k = nzb_si + 1, nzb_si + vi
-                   l_wall(k,j,i+1) = MIN( l_wall(k,j,i+1),                     &
-                                          SQRT( 0.25_wp * dx**2 +              &
-                                          ( zu(k) - zw(nzb_si) )**2 ) )
-                ENDDO
-
-             ENDIF
-
           ENDDO
        ENDDO
-
-    ENDIF
-
-!
-!-- Multiplication with wall_adjustment_factor
-    l_wall = wall_adjustment_factor * l_wall
-
+    ENDDO
 !
 !-- Set lateral boundary conditions for l_wall
-    CALL exchange_horiz( l_wall, nbgp )
-
-    DEALLOCATE( corner_nl, corner_nr, corner_sl, corner_sr, nzb_local, &
-                vertical_influence, wall_l, wall_n, wall_r, wall_s )
+    CALL exchange_horiz( l_wall, nbgp )     
 
 
