Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 844)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 861)
@@ -4,4 +4,11 @@
 ! Current revisions:
 ! -----------------
+! Set wall_flags_0. The array is needed for degradation in ws-scheme near walls,
+! inflow and outflow boundaries as well as near the bottom and the top of the
+! model domain.
+!
+! Initialization of nzb_s_inner and nzb_w_inner.
+! gls has to be at least nbgp to do not exceed the array bounds of nzb_local
+! while setting wall_flags_0
 !
 ! Former revisions:
@@ -104,4 +111,6 @@
                                              corner_sr, wall_l, wall_n, wall_r,&
                                              wall_s, nzb_local, nzb_tmp
+
+    LOGICAL :: flag_set = .FALSE.
 
     REAL    ::  dx_l, dy_l, dz_stretched
@@ -311,4 +320,5 @@
 !-- the flag arrays needed for the multigrid method
     gls = 2**( maximum_grid_level )
+    IF ( gls < nbgp )  gls = nbgp
 
     ALLOCATE( corner_nl(nys:nyn,nxl:nxr), corner_nr(nys:nyn,nxl:nxr),       &
@@ -589,5 +599,8 @@
 
     END SELECT
-
+!
+!-- Determine the maximum level of topography. Furthermore it is used for
+!-- steering the degradation of order of the applied advection scheme.
+    nzb_max = MAXVAL( nzb_local )
 !
 !-- Consistency checks and index array initialization are only required for
@@ -653,6 +666,6 @@
 !
 !--    Initialize index arrays nzb_s_inner and nzb_w_inner
-       nzb_s_inner = nzb_local(nys-1:nyn+1,nxl-1:nxr+1)
-       nzb_w_inner = nzb_local(nys-1:nyn+1,nxl-1:nxr+1)
+       nzb_s_inner = nzb_local(nysg:nyng,nxlg:nxrg)
+       nzb_w_inner = nzb_local(nysg:nyng,nxlg:nxrg)
 
 !
@@ -1018,5 +1031,5 @@
                 ENDDO
              ENDDO
-          ENDDO 
+          ENDDO
 
 !
@@ -1036,4 +1049,219 @@
 
     ENDIF
+!
+!-- Allocate flags needed for masking walls.
+    ALLOCATE( wall_flags_0(nzb:nzt,nys:nyn,nxl:nxr) )
+    wall_flags_0 = 0
+
+    IF ( scalar_advec == 'ws-scheme' )  THEN
+!
+!--    Set flags to steer the degradation of the advection scheme in advec_ws
+!--    near topography, inflow- and outflow boundaries as well as bottom and
+!--    top of model domain. wall_flags_0 remains zero for all non-prognostic
+!--    grid points.
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_s_inner(j,i)+1, nzt
+!
+!--             scalar - x-direction
+!--             WS1 (0), WS3 (1), WS5 (2)
+                IF ( k <= nzb_s_inner(j,i+1) .OR. ( outflow_l .AND. i == nxl ) &
+                     .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 0 )
+                ELSEIF ( k <= nzb_s_inner(j,i+2) .OR. k <= nzb_s_inner(j,i-1)  &
+                         .OR. ( outflow_r .AND. i == nxr-1 )                   &
+                         .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 1 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 2 )
+                ENDIF
+!
+!--             scalar - y-direction
+!--             WS1 (3), WS3 (4), WS5 (5)
+                IF ( k <= nzb_s_inner(j+1,i) .OR. ( outflow_s .AND. j == nys ) &
+                     .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 3 )
+!--             WS3
+                ELSEIF ( k <= nzb_s_inner(j+2,i) .OR. k <= nzb_s_inner(j-1,i)  &
+                        .OR. ( outflow_s .AND. j == nysv )                     &
+                        .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 4 )
+!--             WS5
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 5 )
+                ENDIF
+!
+!--             scalar - z-direction
+!--             WS1 (6), WS3 (7), WS5 (8)
+                flag_set = .FALSE.
+                IF ( k == nzb_s_inner(j,i) + 1 .OR. k == nzt )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 6 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_s_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 7 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_s_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 8 )
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDIF
+
+    IF ( momentum_advec == 'ws-scheme' )  THEN
+!
+!--    Set wall_flags_0 to steer the degradation of the advection scheme in advec_ws
+!--    near topography, inflow- and outflow boundaries as well as bottom and
+!--    top of model domain. wall_flags_0 remains zero for all non-prognostic
+!--    grid points.
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_u_inner(j,i)+1, nzt
+!
+!--             u component - x-direction
+!--             WS1 (9), WS3 (10), WS5 (11)
+                IF ( k <= nzb_u_inner(j,i+1)                                  &
+                     .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 9 )
+                ELSEIF ( k <= nzb_u_inner(j,i+2) .OR. k <= nzb_u_inner(j,i-1) &
+                         .OR. ( outflow_r .AND. i == nxr-1 )                  &
+                         .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 10 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 11 )
+                ENDIF
+!
+!--             u component - y-direction
+!--             WS1 (12), WS3 (13), WS5 (14)
+                IF ( k <= nzb_u_inner(j+1,i) .OR. ( outflow_s .AND. j == nys ) &
+                    .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 12 )
+                ELSEIF ( k <= nzb_u_inner(j+2,i) .OR. k <= nzb_u_inner(j-1,i)  &
+                    .OR. ( outflow_s .AND. j == nysv )                         &
+                    .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 13 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 14 )
+                ENDIF
+!
+!--             u component - z-direction
+!--             WS1 (15), WS3 (16), WS5 (17)
+                flag_set = .FALSE.
+                IF ( k == nzb_u_inner(j,i) + 1 .OR. k == nzt )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 15 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_u_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 16 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_u_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 17 )
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_v_inner(j,i)+1, nzt
+!
+!--             v component - x-direction
+!--             WS1 (18), WS3 (19), WS5 (20)
+                IF ( k <= nzb_v_inner(j,i+1) .OR. ( outflow_l .AND. i == nxl ) &
+                    .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 18 )
+!--             WS3
+                ELSEIF ( k <= nzb_v_inner(j,i+2) .OR. k <= nzb_v_inner(j,i-1)  &
+                    .OR. ( outflow_r .AND. i == nxr-1 )                        &
+                    .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 19 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 20 )
+                ENDIF
+!
+!--             v component - y-direction
+!--             WS1 (21), WS3 (22), WS5 (23)
+                IF ( k <= nzb_v_inner(j+1,i)                                   &
+                     .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 21 )
+                ELSEIF ( k <= nzb_v_inner(j+2,i) .OR. k <= nzb_v_inner(j-1,i)  &
+                         .OR. ( outflow_s .AND. j == nysv )                    &
+                         .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 22 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 23 )
+                ENDIF
+!
+!--             v component - z-direction
+!--             WS1 (24), WS3 (25), WS5 (26)
+                flag_set = .FALSE.
+                IF ( k == nzb_v_inner(j,i) + 1 .OR. k == nzt )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 24 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_v_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 25 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_v_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 26 )
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb_w_inner(j,i)+1, nzt
+!
+!--             w component - x-direction
+!--             WS1 (27), WS3 (28), WS5 (29)
+                IF ( k <= nzb_w_inner(j,i+1) .OR. ( outflow_l .AND. i == nxl ) &
+                    .OR. ( outflow_r .AND. i == nxr ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 27 )
+                ELSEIF ( k <= nzb_w_inner(j,i+2) .OR. k <= nzb_w_inner(j,i-1)  &
+                         .OR. ( outflow_r .AND. i == nxr-1 )                   &
+                         .OR. ( outflow_l .AND. i == nxlu  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 28 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i),29 )
+                ENDIF
+!
+!--             w component - y-direction
+!--             WS1 (30), WS3 (31), WS5 (32)
+                IF ( k <= nzb_w_inner(j+1,i) .OR. ( outflow_s .AND. j == nys ) &
+                     .OR. ( outflow_n .AND. j == nyn ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 30 )
+                ELSEIF ( k <= nzb_w_inner(j+2,i) .OR. k <= nzb_w_inner(j-1,i)  &
+                         .OR. ( outflow_s .AND. j == nysv )                    &
+                         .OR. ( outflow_n .AND. j == nyn-1  ) )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 31 )
+                ELSE
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 32 )
+                ENDIF
+!
+!--             w component - z-direction
+!--             WS1 (33), WS3 (34), WS5 (35)
+                flag_set = .FALSE.
+                IF ( k == nzb_w_inner(j,i) .OR. k == nzb_w_inner(j,i) + 1      &
+                                           .OR. k == nzt )  THEN
+!
+!--                Please note, at k == nzb_w_inner(j,i) a flag is explictely
+!--                set, although this is not a prognostic level. However, 
+!--                contrary to the advection of u,v and s this is necessary
+!--                because flux_t(nzb_w_inner(j,i)) is used for the tendency
+!--                at k == nzb_w_inner(j,i)+1.
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 33 )
+                   flag_set = .TRUE.
+                ELSEIF ( k == nzb_w_inner(j,i) + 2 .OR. k == nzt - 1 )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 34 )
+                   flag_set = .TRUE.
+                ELSEIF ( k > nzb_w_inner(j,i) .AND. .NOT. flag_set )  THEN
+                   wall_flags_0(k,j,i) = IBSET( wall_flags_0(k,j,i), 35 )
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+
+    ENDIF
 
 !
