Index: /palm/trunk/SOURCE/lpm.f90
===================================================================
--- /palm/trunk/SOURCE/lpm.f90	(revision 1935)
+++ /palm/trunk/SOURCE/lpm.f90	(revision 1936)
@@ -19,5 +19,6 @@
 ! Current revisions:
 ! ------------------
-! 
+! Call routine for deallocation of unused memory.
+! Formatting adjustments
 ! 
 ! Former revisions:
@@ -120,5 +121,5 @@
 
     USE lpm_exchange_horiz_mod,                                                &
-        ONLY:  lpm_exchange_horiz, lpm_move_particle
+        ONLY:  dealloc_particles_array, lpm_exchange_horiz, lpm_move_particle
 
     USE lpm_init_mod,                                                          &
@@ -129,10 +130,10 @@
 
     USE particle_attributes,                                                   &
-        ONLY:  collision_kernel, deleted_particles,                            &
+        ONLY:  collision_kernel, deleted_particles, deallocate_memory,         &
                dt_write_particle_data, dt_prel, end_time_prel,                 &
                grid_particles, number_of_particles, number_of_particle_groups, &
-               particles, particle_groups, prt_count, trlp_count_sum,          &
-               time_prel,                                                      &
-               time_write_particle_data, trlp_count_recv_sum, trnp_count_sum,  &
+               particles, particle_groups, prt_count, step_dealloc,            &
+               time_prel, time_write_particle_data, trlp_count_sum,            &
+               trlp_count_recv_sum, trnp_count_sum,                            &
                trnp_count_recv_sum, trrp_count_sum, trrp_count_recv_sum,       &
                trsp_count_sum, trsp_count_recv_sum,                            &
@@ -149,4 +150,5 @@
     INTEGER(iwp)       ::  je                 !<
     INTEGER(iwp)       ::  js                 !<
+    INTEGER(iwp), SAVE ::  lpm_count = 0      !<
     INTEGER(iwp)       ::  k                  !<
     INTEGER(iwp)       ::  ke                 !<
@@ -381,9 +383,13 @@
 !
 !-- Calculate the new liquid water content for each grid box
-    IF ( cloud_droplets )  THEN
-       CALL lpm_calc_liquid_water_content
+    IF ( cloud_droplets )  CALL lpm_calc_liquid_water_content
+!
+!-- Deallocate unused memory
+    IF ( deallocate_memory  .AND.  lpm_count == step_dealloc )  THEN
+       CALL dealloc_particles_array
+       lpm_count = 0
+    ELSEIF ( deallocate_memory )  THEN
+       lpm_count = lpm_count + 1
     ENDIF
-
-
 
 !
Index: /palm/trunk/SOURCE/lpm_advec.f90
===================================================================
--- /palm/trunk/SOURCE/lpm_advec.f90	(revision 1935)
+++ /palm/trunk/SOURCE/lpm_advec.f90	(revision 1936)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-!  
+! Formatting adjustments
 ! 
 ! Former revisions:
@@ -264,5 +264,6 @@
           jlog = ( particles(n)%y + 0.5_wp * dy ) * ddy
 
-          IF ( constant_flux_layer .AND. zv(n) - zw(nzb_s_inner(jlog,ilog)) < z_p )  THEN
+          IF ( constant_flux_layer  .AND.                                      &
+               zv(n) - zw(nzb_s_inner(jlog,ilog)) < z_p )  THEN
 !
 !--          Resolved-scale horizontal particle velocity is zero below z0. 
@@ -272,6 +273,6 @@
 !
 !--             Determine the sublayer. Further used as index.
-                height_p     = ( zv(n) - zw(nzb_s_inner(jlog,ilog)) - z0_av_global )            &
-                                     * REAL( number_of_sublayers, KIND=wp ) &
+                height_p = ( zv(n) - zw(nzb_s_inner(jlog,ilog)) - z0_av_global ) &
+                                     * REAL( number_of_sublayers, KIND=wp )    &
                                      * d_z_p_z0 
 !
@@ -340,5 +341,6 @@
           ilog = ( particles(n)%x + 0.5_wp * dx ) * ddx
           jlog = ( particles(n)%y + 0.5_wp * dy ) * ddy
-          IF ( constant_flux_layer .AND. zv(n) - zw(nzb_s_inner(jlog,ilog)) < z_p )  THEN
+          IF ( constant_flux_layer  .AND.                                      &
+               zv(n) - zw(nzb_s_inner(jlog,ilog)) < z_p )  THEN
 
              IF ( zv(n) - zw(nzb_s_inner(jlog,ilog)) < z0_av_global )  THEN
@@ -352,6 +354,6 @@
 !--             topography particle on u-grid can be above surface-layer height,
 !--             whereas it can be below on v-grid. 
-                height_p     = ( zv(n) - zw(nzb_s_inner(jlog,ilog)) - z0_av_global ) &
-                                  * REAL( number_of_sublayers, KIND=wp )    &
+                height_p = ( zv(n) - zw(nzb_s_inner(jlog,ilog)) - z0_av_global ) &
+                                  * REAL( number_of_sublayers, KIND=wp )       &
                                   * d_z_p_z0 
 !
@@ -367,5 +369,5 @@
 !--             friction velocity can become very small, resulting in a too
 !--             large particle speed.
-                us_int   = MAX( 0.5_wp * ( us(jlog,ilog) + us(jlog-1,ilog) ), &
+                us_int   = MAX( 0.5_wp * ( us(jlog,ilog) + us(jlog-1,ilog) ),  &
                                 0.01_wp )    
 !
@@ -376,5 +378,5 @@
 !--             as sensitivity studies revealed no significant effect of 
 !--             using the neutral solution also for un/stable situations.
-                v_int(n) = -vsws(jlog,ilog) / ( us_int * kappa + 1E-10_wp )          &
+                v_int(n) = -vsws(jlog,ilog) / ( us_int * kappa + 1E-10_wp )    &
                          * log_z_z0_int - v_gtrans
 
Index: /palm/trunk/SOURCE/lpm_exchange_horiz.f90
===================================================================
--- /palm/trunk/SOURCE/lpm_exchange_horiz.f90	(revision 1935)
+++ /palm/trunk/SOURCE/lpm_exchange_horiz.f90	(revision 1936)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! Deallocation of unused memory
 ! 
 ! Former revisions:
@@ -138,5 +138,6 @@
 
     PRIVATE
-    PUBLIC lpm_exchange_horiz, lpm_move_particle, realloc_particles_array
+    PUBLIC lpm_exchange_horiz, lpm_move_particle, realloc_particles_array,     &
+           dealloc_particles_array
 
     INTERFACE lpm_exchange_horiz
@@ -152,4 +153,7 @@
     END INTERFACE realloc_particles_array
 
+    INTERFACE dealloc_particles_array
+       MODULE PROCEDURE dealloc_particles_array
+    END INTERFACE dealloc_particles_array
 CONTAINS
 
@@ -1057,5 +1061,4 @@
     TYPE(particle_type), DIMENSION(500)            :: tmp_particles_s !<
 
-
     old_size = SIZE(grid_particles(k,j,i)%particles)
 
@@ -1097,3 +1100,80 @@
  END SUBROUTINE realloc_particles_array
 
+
+
+
+
+ SUBROUTINE dealloc_particles_array
+
+    IMPLICIT NONE
+
+    INTEGER(iwp) ::  i
+    INTEGER(iwp) ::  j
+    INTEGER(iwp) ::  k
+    INTEGER(iwp) :: old_size        !<
+    INTEGER(iwp) :: new_size        !<
+
+    LOGICAL                                        :: dealloc  
+
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE :: tmp_particles_d !<
+    TYPE(particle_type), DIMENSION(500)            :: tmp_particles_s !<
+
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+!
+!--          Determine number of active particles
+             number_of_particles = prt_count(k,j,i)
+!
+!--          Determine allocated memory size
+             old_size = SIZE( grid_particles(k,j,i)%particles )
+!
+!--          Check for large unused memory
+             dealloc = ( ( number_of_particles < min_nr_particle .AND.         &
+                           old_size            > min_nr_particle )  .OR.       &
+                         ( number_of_particles > min_nr_particle .AND.         &
+                           old_size - number_of_particles *                    &
+                              ( 1.0_wp + 0.01_wp * alloc_factor ) > 0.0_wp ) )
+                         
+
+             IF ( dealloc )  THEN
+                IF ( number_of_particles < min_nr_particle )  THEN
+                   new_size = min_nr_particle
+                ELSE 
+                   new_size = INT( number_of_particles * ( 1.0_wp + 0.01_wp * alloc_factor ) )
+                ENDIF
+
+                IF ( number_of_particles <= 500 )  THEN
+
+                   tmp_particles_s(1:number_of_particles) = grid_particles(k,j,i)%particles(1:number_of_particles)
+
+                   DEALLOCATE(grid_particles(k,j,i)%particles)
+                   ALLOCATE(grid_particles(k,j,i)%particles(new_size))
+
+                   grid_particles(k,j,i)%particles(1:number_of_particles)          = tmp_particles_s(1:number_of_particles)
+                   grid_particles(k,j,i)%particles(number_of_particles+1:new_size) = zero_particle
+
+                ELSE
+
+                   ALLOCATE(tmp_particles_d(number_of_particles))
+                   tmp_particles_d(1:number_of_particles) = grid_particles(k,j,i)%particles(1:number_of_particles)
+
+                   DEALLOCATE(grid_particles(k,j,i)%particles)
+                   ALLOCATE(grid_particles(k,j,i)%particles(new_size))
+
+                   grid_particles(k,j,i)%particles(1:number_of_particles)          = tmp_particles_d(1:number_of_particles)
+                   grid_particles(k,j,i)%particles(number_of_particles+1:new_size) = zero_particle
+
+                   DEALLOCATE(tmp_particles_d)
+
+                ENDIF
+
+             ENDIF
+          ENDDO
+       ENDDO
+    ENDDO
+
+ END SUBROUTINE dealloc_particles_array
+
+
 END MODULE lpm_exchange_horiz_mod
Index: /palm/trunk/SOURCE/mod_particle_attributes.f90
===================================================================
--- /palm/trunk/SOURCE/mod_particle_attributes.f90	(revision 1935)
+++ /palm/trunk/SOURCE/mod_particle_attributes.f90	(revision 1936)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! ------------------
-! 
+! +deallocate_memory, step_dealloc
 !
 ! Former revisions:
@@ -83,5 +83,5 @@
                      offset_ocean_nzt_m1 = 0, particles_per_point = 1,         &
                      particle_file_count = 0, radius_classes = 20,             &
-                     sort_count = 0,                                           &
+                     sort_count = 0, step_dealloc = 100,                       &
                      total_number_of_particles,                                &
                      trlp_count_sum, trlp_count_recv_sum, trrp_count_sum,      &
@@ -95,4 +95,5 @@
     LOGICAL ::  all_or_nothing = .FALSE., average_impact = .FALSE.,            &
                 curvature_solution_effects = .FALSE.,                          &
+                deallocate_memory = .TRUE.,                                    &
                 hall_kernel = .FALSE., particle_advection = .FALSE.,           &
                 random_start_position = .FALSE.,                               &
Index: /palm/trunk/SOURCE/package_parin.f90
===================================================================
--- /palm/trunk/SOURCE/package_parin.f90	(revision 1935)
+++ /palm/trunk/SOURCE/package_parin.f90	(revision 1936)
@@ -19,5 +19,5 @@
 ! Current revisions:
 ! -----------------
-!
+! +deallocate_memory, step_dealloc
 ! 
 ! Former revisions:
@@ -154,7 +154,7 @@
         ONLY:  alloc_factor, bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,         &
                collision_algorithm, collision_kernel,                          &
-               curvature_solution_effects, density_ratio, dissipation_classes, &
-               dt_min_part, dt_prel, dt_write_particle_data,                   &
-               end_time_prel, initial_weighting_factor,                        &
+               curvature_solution_effects, deallocate_memory, density_ratio,   &
+               dissipation_classes, dt_min_part, dt_prel,                      &
+               dt_write_particle_data, end_time_prel, initial_weighting_factor,&
                init_aerosol_probabilistic, min_nr_particle,                    &
                monodisperse_aerosols, n1, n2, n3, number_of_particle_groups,   &
@@ -164,6 +164,7 @@
                radius_classes, random_start_position,                          &
                read_particles_from_restartfile, rm1, rm2, rm3,                 &
-               seed_follows_topography, s1, s2, s3, use_sgs_for_particles,     &
-               vertical_particle_advection, write_particle_statistics
+               seed_follows_topography, step_dealloc, s1, s2, s3,              &
+               use_sgs_for_particles, vertical_particle_advection,             &
+               write_particle_statistics
 
     IMPLICIT NONE
@@ -192,5 +193,6 @@
                                   bc_par_ns, bc_par_t, collision_algorithm,    &
                                   collision_kernel, curvature_solution_effects,&
-                                  density_ratio, dissipation_classes, dt_dopts,&
+                                  deallocate_memory, density_ratio,            &
+                                  dissipation_classes, dt_dopts,               &
                                   dt_min_part, dt_prel,                        &
                                   dt_write_particle_data,                      &
@@ -206,6 +208,6 @@
                                   read_particles_from_restartfile,             &
                                   rm1, rm2, rm3,                               &
-                                  seed_follows_topography, s1, s2, s3,         &
-                                  use_sgs_for_particles,                       &
+                                  seed_follows_topography, step_dealloc,       &
+                                  s1, s2, s3, use_sgs_for_particles,           &
                                   vertical_particle_advection,                 &
                                   write_particle_statistics
