Index: palm/trunk/SOURCE/advec_ws.f90
===================================================================
--- palm/trunk/SOURCE/advec_ws.f90	(revision 4321)
+++ palm/trunk/SOURCE/advec_ws.f90	(revision 4324)
@@ -20,5 +20,6 @@
 ! Current revisions:
 ! ------------------
-! 
+! Vertical fluxes of w are now set to zero at nzt and nzt+1, setting of 
+! advection flags for fluxes in z-direction revised, comments extended
 ! 
 ! Former revisions:
@@ -459,5 +460,6 @@
                 ENDIF                                                          
 !                                                                              
-!--             u component - z-direction. Fluxes are calculated on w-grid level                                      
+!--             u component - z-direction. Fluxes are calculated on w-grid                                      
+!--             level. Boundary u-values at/within walls aren't used. 
 !--             WS1 (6), WS3 (7), WS5 (8)                                      
                 IF ( k == nzb+1 )  THEN                                        
@@ -478,8 +480,10 @@
                                                                                
                 flag_set = .FALSE.                                             
-                IF ( .NOT. BTEST(wall_flags_0(k-1,j,i),1)  .AND.               &
-                           BTEST(wall_flags_0(k,j,i),1)    .OR.                &
-                     .NOT. BTEST(wall_flags_0(k_pp,j,i),1) .AND.               &                              
-                           BTEST(wall_flags_0(k,j,i),1)    .OR.                &
+                IF ( ( .NOT. 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) ) .OR.             &
+                     ( .NOT. BTEST(wall_flags_0(k_pp,j,i),1)  .AND.            &                              
+                             BTEST(wall_flags_0(k+1,j,i),1)   .AND.            &
+                             BTEST(wall_flags_0(k,j,i),1) )   .OR.             &
                      k == nzt )                                                &
                 THEN                                                           
@@ -491,5 +495,6 @@
                                  BTEST(wall_flags_0(k,j,i),1)       .AND.      &
                                  BTEST(wall_flags_0(k+1,j,i),1)     .AND.      &
-                                 BTEST(wall_flags_0(k_pp,j,i),1)    .OR.       &
+                                 BTEST(wall_flags_0(k_pp,j,i),1)    .AND.      &
+                           .NOT. flag_set                           .OR.       & 
                          k == nzt - 1 )                                        &
                 THEN                                                           
@@ -587,6 +592,7 @@
                    advc_flags_m(k,j,i) = IBCLR( advc_flags_m(k,j,i), 12 )
                 ENDIF
-!
-!--             v component - z-direction. Fluxes are calculated on w-grid level
+!                                                              
+!--             v component - z-direction. Fluxes are calculated on w-grid                                      
+!--             level. Boundary v-values at/within walls aren't used. 
 !--             WS1 (15), WS3 (16), WS5 (17)
                 IF ( k == nzb+1 )  THEN
@@ -607,8 +613,10 @@
                 
                 flag_set = .FALSE.
-                IF ( .NOT. BTEST(wall_flags_0(k-1,j,i),2)  .AND.               &
-                           BTEST(wall_flags_0(k,j,i),2)    .OR.                &
-                     .NOT. BTEST(wall_flags_0(k_pp,j,i),2) .AND.               &                              
-                           BTEST(wall_flags_0(k,j,i),2)    .OR.                &
+                IF ( ( .NOT. 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) ) .OR.             &
+                     ( .NOT. BTEST(wall_flags_0(k_pp,j,i),2)  .AND.            &                              
+                             BTEST(wall_flags_0(k+1,j,i),2)   .AND.            &
+                             BTEST(wall_flags_0(k,j,i),2) )   .OR.             &
                      k == nzt )                                                &
                 THEN                                                           
@@ -620,5 +628,6 @@
                                  BTEST(wall_flags_0(k,j,i),2)       .AND.      &
                                  BTEST(wall_flags_0(k+1,j,i),2)     .AND.      &
-                                 BTEST(wall_flags_0(k_pp,j,i),2)    .OR.       &
+                                 BTEST(wall_flags_0(k_pp,j,i),2)    .AND.      &
+                           .NOT. flag_set                           .OR.       & 
                          k == nzt - 1 )                                        &
                 THEN                                                           
@@ -714,7 +723,9 @@
                    advc_flags_m(k,j,i) = IBCLR( advc_flags_m(k,j,i), 21 )
                 ENDIF
-!
+!                                                                              
 !--             w component - z-direction. Fluxes are calculated on scalar grid
-!--             level. WS1 (24), WS3 (25), WS5 (26)
+!--             level. Boundary w-values at walls are used. Flux at k=i is  
+!--             defined at scalar position k=i+1 with i being an integer. 
+!--             WS1 (24), WS3 (25), WS5 (26)
                 IF ( k == nzb+1 )  THEN
                    k_mm = nzb
@@ -755,5 +766,6 @@
                                  BTEST(wall_flags_0(k-1,j,i),3)     .AND.      &
                                  BTEST(wall_flags_0(k,j,i),3)       .AND.      &
-                                 BTEST(wall_flags_0(k+1,j,i),3)     .OR.       &
+                                 BTEST(wall_flags_0(k+1,j,i),3)     .AND.      &
+                           .NOT. flag_set                           .OR.       & 
                          k == nzt - 1 )                                        &
                 THEN                                                           
@@ -1036,5 +1048,6 @@
                 
 !                                                                              
-!--             scalar - z-direction. Fluxes are calculated on w-grid level                                           
+!--             scalar - z-direction. Fluxes are calculated on w-grid                                      
+!--             level. Boundary values at/within walls aren't used.                                            
 !--             WS1 (6), WS3 (7), WS5 (8)                                      
                 IF ( k == nzb+1 )  THEN                                        
@@ -1068,5 +1081,6 @@
                                  BTEST(wall_flags_0(k,j,i),0)       .AND.      &
                                  BTEST(wall_flags_0(k+1,j,i),0)     .AND.      &
-                                 BTEST(wall_flags_0(k_pp,j,i),0)    .OR.       &    
+                                 BTEST(wall_flags_0(k_pp,j,i),0)    .AND.      &
+                           .NOT. flag_set                           .OR.       & 
                          k == nzt - 1 )                                        &
                 THEN                                                           
@@ -3466,4 +3480,16 @@
                                                           )
        ENDDO
+       
+!
+!--    Set resolved/turbulent flux at model top to zero (w-level). Hint: The 
+!--    flux at nzt is defined at the scalar grid point nzt+1. Therefore, the
+!--    flux at nzt+1 is already outside of the model domain
+       flux_t(nzt) = 0.0_wp
+       diss_t(nzt) = 0.0_wp
+       w_comp(nzt) = 0.0_wp
+       
+       flux_t(nzt+1) = 0.0_wp
+       diss_t(nzt+1) = 0.0_wp
+       w_comp(nzt+1) = 0.0_wp
        
        DO  k = nzb+1, nzb_max_l
