Index: palm/trunk/SOURCE/CURRENT_MODIFICATIONS
===================================================================
--- palm/trunk/SOURCE/CURRENT_MODIFICATIONS	(revision 60)
+++ palm/trunk/SOURCE/CURRENT_MODIFICATIONS	(revision 61)
@@ -7,4 +7,6 @@
 
 new d3par-parameter netcdf_64bit_3d to switch on 64bit offset only for 3D files
+
+new d3par-parameter dt_max to define the maximum value for the allowed timestep 
 
 new inipar-parameter pt_refrence. If given, this value is used as the reference that in buoyancy terms (otherwise, the instantaneous horizontally averaged temperature is used).
Index: palm/trunk/SOURCE/parin.f90
===================================================================
--- palm/trunk/SOURCE/parin.f90	(revision 60)
+++ palm/trunk/SOURCE/parin.f90	(revision 61)
@@ -4,5 +4,5 @@
 ! Actual revisions:
 ! -----------------
-! +netcdf_64bit_3d in d3par, +pt_reference in inipar, -data_output_ts
+! +dt_max, netcdf_64bit_3d in d3par, +pt_reference in inipar, -data_output_ts
 !
 ! Former revisions:
@@ -99,5 +99,5 @@
                        dt_data_output_av, dt_disturb, dt_dopr, &
                        dt_dopr_listing, dt_dots, dt_do2d_xy, dt_do2d_xz, &
-                       dt_do2d_yz, dt_do3d, dt_restart, dt_run_control, &
+                       dt_do2d_yz, dt_do3d, dt_max, dt_restart, dt_run_control,&
                        end_time, force_print_header, mg_cycles, &
                        mg_switch_to_pe0_level, netcdf_64bit, netcdf_64bit_3d, &
Index: palm/trunk/SOURCE/particle_boundary_conds.f90
===================================================================
--- palm/trunk/SOURCE/particle_boundary_conds.f90	(revision 60)
+++ palm/trunk/SOURCE/particle_boundary_conds.f90	(revision 61)
@@ -15,4 +15,7 @@
 ! Calculates the reflection of particles from vertical walls.
 ! Routine developed by Jin Zhang (2006-2007)
+! To do: Code structure for finding the t_index values and for checking the
+!        reflection conditions is basically the same for all four cases, so it
+!        should be possible to further simplify/shorten it.
 !------------------------------------------------------------------------------!
 
@@ -30,4 +33,6 @@
                 k3, k5, n, t_index, t_index_number
 
+    LOGICAL ::  reflect_x, reflect_y, reflect_z
+
     REAL ::  dt_particle, pos_x, pos_x_old, pos_y, pos_y_old, pos_z, &
              pos_z_old, prt_x, prt_y, prt_z, tmp_t, xline, yline, zline
@@ -38,4 +43,8 @@
 
 
+
+    reflect_x = .FALSE.
+    reflect_y = .FALSE.
+    reflect_z = .FALSE.
 
     DO  n = 1, number_of_particles
@@ -51,5 +60,6 @@
        prt_z = particles(n)%z
 
-
+!
+!--    If the particle position is below the surface, it has to be reflected
        IF ( k2 <= nzb_s_inner(j2,i2)  .AND.  nzb_s_inner(j2,i2) /=0 )  THEN
 
@@ -114,5 +124,5 @@
              ENDDO
 
-             DO  t_index = 1, t_index_number
+      case1: DO  t_index = 1, t_index_number
 
                 pos_x = pos_x_old + t(t_index) * ( prt_x - pos_x_old )
@@ -128,76 +138,56 @@
                 k5 = pos_z / dz
 
-                IF ( k5 <= nzb_s_inner(j5,i3)  .AND.  nzb_s_inner(j5,i3) /= 0 )&
-                THEN
+                IF ( k5 <= nzb_s_inner(j5,i3)  .AND. &
+                     nzb_s_inner(j5,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j5,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j5,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k5 <= nzb_s_inner(j3,i5)  .AND.  nzb_s_inner(j3,i5) /= 0 )&
-                THEN
+                      reflect_z = .TRUE.
+                      EXIT case1
+                   ENDIF
+
+                ENDIF
+
+                IF ( k5 <= nzb_s_inner(j3,i5)  .AND. &
+                     nzb_s_inner(j3,i5) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i5) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i5) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k3 <= nzb_s_inner(j3,i3)  .AND.  nzb_s_inner(j3,i3) /= 0 )&
-                THEN
+                      reflect_z = .TRUE.
+                      EXIT case1
+                   ENDIF
+
+                ENDIF
+
+                IF ( k3 <= nzb_s_inner(j3,i3)  .AND. &
+                     nzb_s_inner(j3,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case1
                    ENDIF
 
                    IF ( pos_y == ( j3 * dy - 0.5 * dy )  .AND. &
                         pos_z < nzb_s_inner(j3,i3) * dz )  THEN
-                      particles(n)%y       = 2.0 * pos_y - prt_y
-                      particles(n)%speed_y = - particles(n)%speed_y
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_y_sgs = - particles(n)%speed_y_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_y = .TRUE.
+                      EXIT case1
                    ENDIF
 
                    IF ( pos_x == ( i3 * dx - 0.5 * dx )  .AND. &
                         pos_z < nzb_s_inner(j3,i3) * dz )  THEN
-                      particles(n)%x       = 2.0 * pos_x - prt_x
-                      particles(n)%speed_x = - particles(n)%speed_x
-
-                      IF ( use_sgs_for_particles )  THEN 
-                         particles(n)%speed_x_sgs = - particles(n)%speed_x_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-             ENDDO
-
-          ENDIF
+                      reflect_x = .TRUE.
+                      EXIT case1
+                   ENDIF
+
+                ENDIF
+
+             ENDDO case1
 
 !
 !--       Case 2
-          IF ( particles(n)%x > pos_x_old  .AND.  particles(n)%y < pos_y_old ) &
-          THEN
+          ELSEIF ( particles(n)%x > pos_x_old  .AND. &
+                   particles(n)%y < pos_y_old )  THEN
+
              t_index = 1
 
@@ -248,5 +238,5 @@
              ENDDO
 
-             DO  t_index = 1, t_index_number
+      case2: DO  t_index = 1, t_index_number
 
                 pos_x = pos_x_old + t(t_index) * ( prt_x - pos_x_old )
@@ -262,73 +252,56 @@
                 k5 = pos_z / dz
 
-                IF ( k5 <= nzb_s_inner(j3,i5)  .AND.  nzb_s_inner(j3,i5) /= 0 )&
-                THEN
+                IF ( k5 <= nzb_s_inner(j3,i5)  .AND. &
+                     nzb_s_inner(j3,i5) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i5) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i5) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k3 <= nzb_s_inner(j3,i3)  .AND.  nzb_s_inner(j3,i3) /= 0 )&
-                THEN
+                      reflect_z = .TRUE.
+                      EXIT case2
+                   ENDIF
+
+                ENDIF
+
+                IF ( k3 <= nzb_s_inner(j3,i3)  .AND. &
+                     nzb_s_inner(j3,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case2
                    ENDIF
 
                    IF ( pos_x == ( i3 * dx - 0.5 * dx )  .AND. &
                         pos_z < nzb_s_inner(j3,i3) * dz )  THEN
-                      particles(n)%x       = 2.0 * pos_x - prt_x
-                      particles(n)%speed_x = - particles(n)%speed_x
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_x_sgs = - particles(n)%speed_x_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k5 <= nzb_s_inner(j5,i3)  .AND.  nzb_s_inner(j5,i3) /= 0 )&
-                THEN
+                      reflect_x = .TRUE.
+                      EXIT case2
+                   ENDIF
+
+                ENDIF
+
+                IF ( k5 <= nzb_s_inner(j5,i3)  .AND. &
+                     nzb_s_inner(j5,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j5,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j5,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case2
                    ENDIF
 
                    IF ( pos_y == ( j5 * dy + 0.5 * dy )  .AND. &
                         pos_z < nzb_s_inner(j5,i3) * dz )  THEN
-                      particles(n)%y       = 2.0 * pos_y - prt_y
-                      particles(n)%speed_y = - particles(n)%speed_y
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_y_sgs = - particles(n)%speed_y_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-             ENDDO
-          ENDIF
+                      reflect_y = .TRUE.
+                      EXIT case2
+                   ENDIF
+
+                ENDIF
+
+             ENDDO case2
 
 !
 !--       Case 3
-          IF ( particles(n)%x < pos_x_old  .AND. particles(n)%y > pos_y_old ) &
-          THEN
+          ELSEIF ( particles(n)%x < pos_x_old  .AND. &
+                   particles(n)%y > pos_y_old )  THEN
+
              t_index = 1
 
@@ -380,5 +353,5 @@
              ENDDO
 
-             DO  t_index = 1, t_index_number
+      case3: DO  t_index = 1, t_index_number
 
                 pos_x = pos_x_old + t(t_index) * ( prt_x - pos_x_old )
@@ -394,74 +367,56 @@
                 k5 = pos_z / dz
 
-                IF ( k5 <= nzb_s_inner(j5,i3)  .AND.  nzb_s_inner(j5,i3) /= 0 )&
-                THEN
+                IF ( k5 <= nzb_s_inner(j5,i3)  .AND. &
+                     nzb_s_inner(j5,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j5,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j5,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN  
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k3 <= nzb_s_inner(j3,i3)  .AND.  nzb_s_inner(j3,i3) /= 0 )&
-                THEN
+                      reflect_z = .TRUE.
+                      EXIT case3
+                   ENDIF
+
+                ENDIF
+
+                IF ( k3 <= nzb_s_inner(j3,i3)  .AND. &
+                     nzb_s_inner(j3,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case3
                    ENDIF
 
                    IF ( pos_y == ( j3 * dy - 0.5 * dy )  .AND. &
                         pos_z < nzb_s_inner(j3,i3) * dz )  THEN
-                      particles(n)%y       = 2.0 * pos_y - prt_y
-                      particles(n)%speed_y = - particles(n)%speed_y
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_y_sgs = - particles(n)%speed_y_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k5 <= nzb_s_inner(j3,i5)  .AND.  nzb_s_inner(j3,i5) /= 0 )&
-                THEN
+                      reflect_y = .TRUE.
+                      EXIT case3
+                   ENDIF
+
+                ENDIF
+
+                IF ( k5 <= nzb_s_inner(j3,i5)  .AND. &
+                     nzb_s_inner(j3,i5) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i5) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i5) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case3
                    ENDIF
 
                    IF ( pos_x == ( i5 * dx + 0.5 * dx )  .AND. &
                         pos_z < nzb_s_inner(j3,i5) * dz )  THEN
-                      particles(n)%x       = 2.0 * pos_x - prt_x
-                      particles(n)%speed_x = - particles(n)%speed_x
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_x_sgs = - particles(n)%speed_x_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF 
-             ENDDO
-          ENDIF
+                      reflect_x = .TRUE.
+                      EXIT case3
+                   ENDIF
+
+                ENDIF
+
+             ENDDO case3
 
 !
 !--       Case 4
-          IF ( particles(n)%x < pos_x_old  .AND.   particles(n)%y < pos_y_old )&
-          THEN
+          ELSEIF ( particles(n)%x < pos_x_old  .AND. &
+                   particles(n)%y < pos_y_old )  THEN
+
              t_index = 1
 
@@ -513,5 +468,5 @@
              ENDDO
 
-             DO  t_index = 1, t_index_number
+      case4: DO  t_index = 1, t_index_number
 
                 pos_x = pos_x_old + t(t_index) * ( prt_x - pos_x_old )
@@ -527,29 +482,22 @@
                 k5 = pos_z / dz
 
-                IF ( k3 <= nzb_s_inner(j3,i3)  .AND.  nzb_s_inner(j3,i3) /= 0 )&
-                THEN
+                IF ( k3 <= nzb_s_inner(j3,i3)  .AND. &
+                     nzb_s_inner(j3,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i3) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-
-                IF ( k5 <= nzb_s_inner(j3,i5)  .AND.  nzb_s_inner(j3,i5) /= 0 )&
-                THEN
+                      reflect_z = .TRUE.
+                      EXIT case4
+                   ENDIF
+
+                ENDIF
+
+                IF ( k5 <= nzb_s_inner(j3,i5)  .AND. &
+                     nzb_s_inner(j3,i5) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j3,i5) * dz  .AND. &
                         k3 == nzb_s_inner(j3,i5) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN 
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case4
                    ENDIF
 
@@ -557,25 +505,17 @@
                         nzb_s_inner(j3,i5) /=0  .AND.          &
                         pos_z < nzb_s_inner(j3,i5) * dz )  THEN
-                      particles(n)%x       = 2.0 * pos_x - prt_x
-                      particles(n)%speed_x = - particles(n)%speed_x
-
-                      IF ( use_sgs_for_particles )  THEN  
-                         particles(n)%speed_x_sgs = - particles(n)%speed_x_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF
-    
-                IF ( k5 <= nzb_s_inner(j5,i3)  .AND.  nzb_s_inner(j5,i3) /= 0 )&
-                THEN
+                      reflect_x = .TRUE.
+                      EXIT case4
+                   ENDIF
+
+                ENDIF
+
+                IF ( k5 <= nzb_s_inner(j5,i3)  .AND. &
+                     nzb_s_inner(j5,i3) /= 0 )  THEN
+
                    IF ( pos_z == nzb_s_inner(j5,i3) * dz  .AND. &
                         k5 == nzb_s_inner(j5,i3) )  THEN
-                      particles(n)%z       = 2.0 * pos_z - prt_z
-                      particles(n)%speed_z = - particles(n)%speed_z
-
-                      IF ( use_sgs_for_particles )  THEN 
-                         particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
-                      ENDIF
-                      GOTO 999
+                      reflect_z = .TRUE.
+                      EXIT case4
                    ENDIF
 
@@ -583,23 +523,55 @@
                         nzb_s_inner(j5,i3) /= 0  .AND.         &
                         pos_z < nzb_s_inner(j5,i3) * dz )  THEN
-                      particles(n)%y       = 2.0 * pos_y - prt_y
-                      particles(n)%speed_y = - particles(n)%speed_y
-
-                      IF ( use_sgs_for_particles )  THEN
-                         particles(n)%speed_y_sgs = - particles(n)%speed_y_sgs
-                      ENDIF
-                      GOTO 999
-                   ENDIF
-                ENDIF 
-             ENDDO
+                      reflect_y = .TRUE.
+                      EXIT case4
+                   ENDIF
+
+                ENDIF
+ 
+             ENDDO case4
+
           ENDIF
 
-     999  CONTINUE
+       ENDIF      ! Check, if particle position is below the surface
+
+!
+!--    Do the respective reflection, in case that one of the above conditions
+!--    is found to be true
+       IF ( reflect_z )  THEN
+
+          particles(n)%z       = 2.0 * pos_z - prt_z
+          particles(n)%speed_z = - particles(n)%speed_z
+
+          IF ( use_sgs_for_particles )  THEN
+             particles(n)%speed_z_sgs = - particles(n)%speed_z_sgs
+          ENDIF
+          reflect_z = .FALSE.
+
+       ELSEIF ( reflect_y )  THEN
+
+          particles(n)%y       = 2.0 * pos_y - prt_y
+          particles(n)%speed_y = - particles(n)%speed_y
+
+          IF ( use_sgs_for_particles )  THEN
+             particles(n)%speed_y_sgs = - particles(n)%speed_y_sgs
+          ENDIF
+          reflect_y = .FALSE.
+
+       ELSEIF ( reflect_x )  THEN
+
+          particles(n)%x       = 2.0 * pos_x - prt_x
+          particles(n)%speed_x = - particles(n)%speed_x
+
+          IF ( use_sgs_for_particles )  THEN 
+             particles(n)%speed_x_sgs = - particles(n)%speed_x_sgs
+          ENDIF
+          reflect_x = .FALSE.
+
+       ENDIF        
     
-       ENDIF
-
     ENDDO
 
     CALL cpu_log( log_point_s(48), 'advec_part_refle', 'stop' )
 
+
  END SUBROUTINE particle_boundary_conds
