Index: palm/trunk/SOURCE/init_grid.f90
===================================================================
--- palm/trunk/SOURCE/init_grid.f90	(revision 3067)
+++ palm/trunk/SOURCE/init_grid.f90	(revision 3068)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! New warning message concerning grid stretching has been introduced
+! 
+! 3066 2018-06-12 08:55:55Z Giersch
 ! Bugfix in IF statement before error message
 ! 
@@ -633,8 +636,7 @@
        
 !
-!--    Stretching must not be applied within the prandtl_layer 
-!--    (first two grid points). For the default case dz_stretch_level_start 
-!--    is negative. Therefore the absolut value is checked here.
-       IF ( ANY( dz_stretch_level_start > dz(1) * 1.5_wp ) ) THEN
+!--    Stretching must not be applied close to the surface (last two grid 
+!--    points). For the default case dz_stretch_level_start is negative. 
+       IF ( ANY( dz_stretch_level_start > - dz(1) * 1.5_wp ) ) THEN
           WRITE( message_string, * ) 'Eeach dz_stretch_level_start has to be ',&
                                      'less than ', dz(1) * 1.5
@@ -1148,4 +1150,6 @@
     REAL(wp) ::  stretch_factor_2      !< stretching factor that fulfil Eq. (6) togehter with l_rounded exactly
     
+    REAL(wp) ::  dz_stretch_factor_array_2(9) = 1.08_wp  !< Array that contains all stretch_factor_2 that belongs to stretch_factor_1 
+    
     REAL(wp), PARAMETER ::  stretch_factor_interval = 1.0E-06  !< interval for sampling possible stretching factors
     REAL(wp), PARAMETER ::  stretch_factor_lower_limit = 0.88  !< lowest possible stretching factor
@@ -1153,34 +1157,34 @@
  
  
-       l = 0
-       DO  n = 1, number_end
+    l = 0
+    DO  n = 1, number_end
+    
+       iterations = 1
+       stretch_factor_1 = 1.0 
+       stretch_factor_2 = 1.0
+       delta_total_old = 1.0
        
-          iterations = 1
-          stretch_factor_1 = 1.0 
-          stretch_factor_2 = 1.0
-          delta_total_old = 1.0
-          
-          IF ( dz(n) > dz(n+1) ) THEN
-             DO WHILE ( stretch_factor_1 >= stretch_factor_lower_limit ) 
-                
-                stretch_factor_1 = 1.0 - iterations * stretch_factor_interval
-                distance = ABS( dz_stretch_level_end(n) -                   &
-                           dz_stretch_level_start(n) ) 
-                numerator = distance*stretch_factor_1/dz(n) +               &
-                            stretch_factor_1 - distance/dz(n)
-                
-                IF ( numerator > 0.0 ) THEN
-                   l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
-                   l_rounded = NINT( l )
-                   delta_l = ABS( l_rounded - l ) / l
-                ENDIF
-                
-                stretch_factor_2 = EXP( LOG( dz(n+1)/dz(n) ) / (l_rounded) )
-                
-                delta_stretch_factor = ABS( stretch_factor_1 -              &
-                                            stretch_factor_2 ) /            &
-                                       stretch_factor_2
-                
-                delta_total_new = delta_l + delta_stretch_factor
+       IF ( dz(n) > dz(n+1) ) THEN
+          DO WHILE ( stretch_factor_1 >= stretch_factor_lower_limit ) 
+             
+             stretch_factor_1 = 1.0 - iterations * stretch_factor_interval
+             distance = ABS( dz_stretch_level_end(n) -                   &
+                        dz_stretch_level_start(n) ) 
+             numerator = distance*stretch_factor_1/dz(n) +               &
+                         stretch_factor_1 - distance/dz(n)
+             
+             IF ( numerator > 0.0 ) THEN
+                l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
+                l_rounded = NINT( l )
+                delta_l = ABS( l_rounded - l ) / l
+             ENDIF
+             
+             stretch_factor_2 = EXP( LOG( dz(n+1)/dz(n) ) / (l_rounded) )
+             
+             delta_stretch_factor = ABS( stretch_factor_1 -              &
+                                         stretch_factor_2 ) /            &
+                                    stretch_factor_2
+             
+             delta_total_new = delta_l + delta_stretch_factor
 
 !
@@ -1189,34 +1193,35 @@
 !--                stretch_factor_2 would guarantee that the stretched dz(n) is
 !--                equal to dz(n+1) after l_rounded grid levels.
-                IF (delta_total_new < delta_total_old) THEN
-                   dz_stretch_factor_array(n) = stretch_factor_1
-                   delta_total_old = delta_total_new
-                ENDIF
-                
-                iterations = iterations + 1
-               
-             ENDDO
-                
-          ELSEIF ( dz(n) < dz(n+1) ) THEN 
-             DO WHILE ( stretch_factor_1 <= stretch_factor_upper_limit )
-                        
-                stretch_factor_1 = 1.0 + iterations * stretch_factor_interval
-                distance = ABS( dz_stretch_level_end(n) -                   &
-                           dz_stretch_level_start(n) ) 
-                numerator = distance*stretch_factor_1/dz(n) +               &
-                            stretch_factor_1 - distance/dz(n)
-                
-                l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
-                l_rounded = NINT( l )
-                delta_l = ABS( l_rounded - l ) / l
-                
-                stretch_factor_2 = EXP( LOG( dz(n+1)/dz(n) ) / (l_rounded) )
-
-                delta_stretch_factor = ABS( stretch_factor_1 -              &
-                                           stretch_factor_2 ) /             &
-                                           stretch_factor_2
-                
-                delta_total_new = delta_l + delta_stretch_factor
-                
+             IF (delta_total_new < delta_total_old) THEN
+                dz_stretch_factor_array(n) = stretch_factor_1
+                dz_stretch_factor_array_2(n) = stretch_factor_2
+                delta_total_old = delta_total_new
+             ENDIF
+             
+             iterations = iterations + 1
+            
+          ENDDO
+             
+       ELSEIF ( dz(n) < dz(n+1) ) THEN 
+          DO WHILE ( stretch_factor_1 <= stretch_factor_upper_limit )
+                     
+             stretch_factor_1 = 1.0 + iterations * stretch_factor_interval
+             distance = ABS( dz_stretch_level_end(n) -                      &
+                        dz_stretch_level_start(n) ) 
+             numerator = distance*stretch_factor_1/dz(n) +                  &
+                         stretch_factor_1 - distance/dz(n)
+             
+             l = LOG( numerator ) / LOG( stretch_factor_1 ) - 1.0
+             l_rounded = NINT( l )
+             delta_l = ABS( l_rounded - l ) / l
+             
+             stretch_factor_2 = EXP( LOG( dz(n+1)/dz(n) ) / (l_rounded) )
+
+             delta_stretch_factor = ABS( stretch_factor_1 -                 &
+                                        stretch_factor_2 ) /                &
+                                        stretch_factor_2
+             
+             delta_total_new = delta_l + delta_stretch_factor
+             
 !
 !--                stretch_factor_1 is taken to guarantee that the stretching
@@ -1224,18 +1229,36 @@
 !--                stretch_factor_2 would guarantee that the stretched dz(n) is
 !--                equal to dz(n+1) after l_rounded grid levels.
-                IF (delta_total_new < delta_total_old) THEN
-                   dz_stretch_factor_array(n) = stretch_factor_1
-                   delta_total_old = delta_total_new
-                ENDIF
-                
-                iterations = iterations + 1
-             ENDDO
+             IF (delta_total_new < delta_total_old) THEN
+                dz_stretch_factor_array(n) = stretch_factor_1
+                dz_stretch_factor_array_2(n) = stretch_factor_2
+                delta_total_old = delta_total_new
+             ENDIF
              
-          ELSE
-             message_string= 'Two adjacent values of dz must be different'
-             CALL message( 'init_grid', 'PA0228', 1, 2, 0, 6, 0 )
-             
-          ENDIF
-       ENDDO
+             iterations = iterations + 1
+          ENDDO
+          
+       ELSE
+          message_string= 'Two adjacent values of dz must be different'
+          CALL message( 'init_grid', 'PA0228', 1, 2, 0, 6, 0 )
+          
+       ENDIF
+
+!
+!--    Check if also the second stretching factor fits into the allowed
+!--    interval. If not, print a warning for the user.
+       IF ( dz_stretch_factor_array_2(n) < stretch_factor_lower_limit .OR.     & 
+            dz_stretch_factor_array_2(n) > stretch_factor_upper_limit ) THEN
+          WRITE( message_string, * ) 'stretch_factor_2 = ',                    &
+                                     dz_stretch_factor_array_2(n), ' which is',&
+                                     ' responsible for exactly reaching& dz =',&
+                                      dz(n+1), 'after a specific amount of',   & 
+                                     ' grid levels& exceeds the upper',        &
+                                     ' limit =', stretch_factor_upper_limit,   &
+                                     ' &or lower limit = ',                    &
+                                     stretch_factor_lower_limit
+          CALL message( 'init_grid', 'PA0499', 0, 1, 0, 6, 0 )
+            
+       ENDIF
+    ENDDO
         
  END SUBROUTINE calculate_stretching_factor
