Index: palm/trunk/SOURCE/Makefile
===================================================================
--- palm/trunk/SOURCE/Makefile	(revision 4015)
+++ palm/trunk/SOURCE/Makefile	(revision 4017)
@@ -512,5 +512,5 @@
 # by an include command for preprocessor and compiler options when the simple
 # install process is used instead of mbuild/mrun,
-# +set_particle_attributes, set_slicer_attributes_dvrp
+# +set_particle_attributes
 # +subsidence
 #
@@ -587,9 +587,7 @@
 	data_output_2d.f90 \
 	data_output_3d.f90 \
-	data_output_dvrp.f90 \
 	data_output_flight.f90\
 	data_output_mask.f90 \
 	data_output_profiles.f90 \
-	data_output_ptseries.f90 \
 	data_output_spectra.f90 \
 	data_output_tseries.f90 \
@@ -614,5 +612,4 @@
 	init_advec.f90 \
 	init_coupling.f90 \
-	init_dvrp.f90 \
 	init_grid.f90 \
 	init_masks.f90 \
@@ -623,4 +620,5 @@
 	init_vertical_profiles.f90 \
 	interaction_droplets_ptq.f90 \
+	lagrangian_particle_model_mod.f90 \
 	land_surface_model_mod.f90 \
 	large_scale_forcing_nudging_mod.f90 \
@@ -629,22 +627,4 @@
 	local_tremain.f90 \
 	local_tremain_ini.f90 \
-	lpm_advec.f90 \
-	lpm_boundary_conds.f90 \
-	lpm_calc_liquid_water_content.f90 \
-	lpm_collision_kernels.f90 \
-	lpm_data_output_particles.f90 \
-	lpm_droplet_collision.f90 \
-	lpm_droplet_condensation.f90 \
-	lpm_exchange_horiz.f90 \
-	lpm.f90 \
-	lpm_init.f90 \
-	lpm_init_sgs_tke.f90 \
-	lpm_merging.f90 \
-	lpm_pack_arrays.f90 \
-	lpm_read_restart_file.f90 \
-	lpm_set_attributes.f90 \
-	lpm_splitting.f90 \
-	lpm_write_exchange_statistics.f90 \
-	lpm_write_restart_file.f90 \
 	message.f90 \
 	bulk_cloud_model_mod.f90 \
@@ -660,5 +640,4 @@
 	ocean_mod.f90 \
 	outflow_turbulence.f90 \
-	package_parin.f90 \
 	palm.f90 \
 	parin.f90 \
@@ -686,5 +665,4 @@
 	run_control.f90 \
 	salsa_mod.f90 \
-	set_slicer_attributes_dvrp.f90 \
 	singleton_mod.f90 \
 	sor.f90 \
@@ -708,7 +686,5 @@
 	turbulence_closure_mod.f90 \
 	urban_surface_mod.f90 \
-	user_data_output_dvrp.f90 \
 	user_data_output_mask.f90 \
-	user_dvrp_coltab.f90 \
 	user_flight.f90\
 	user_init_3d_model.f90 \
@@ -721,5 +697,4 @@
 	user_lpm_advec.f90 \
 	user_lpm_init.f90 \
-	user_lpm_set_attributes.f90 \
 	user_module.f90 \
 	user_spectra.f90 \
@@ -756,5 +731,5 @@
 
 clean:
-	rm -f $(PROG) $(OBJS) *.mod *.i *.lst
+	rm -f $(PROG) $(OBJS) *.mod *.smod *.i *.lst 
 
 .f90.o:
@@ -844,5 +819,4 @@
 check_open.o: \
 	mod_kinds.o \
-	mod_particle_attributes.o \
 	modules.o \
 	netcdf_interface_mod.o \
@@ -921,10 +895,4 @@
 	mod_kinds.o \
 	modules.o
-data_output_dvrp.o: \
-	basic_constants_and_equations_mod.o \
-	bulk_cloud_model_mod.o \
-	cpulog_mod.o \
-	mod_kinds.o \
-	modules.o
 data_output_mask.o: \
 	basic_constants_and_equations_mod.o \
@@ -942,10 +910,4 @@
 	cpulog_mod.o \
 	mod_kinds.o \
-	modules.o \
-	netcdf_interface_mod.o
-data_output_ptseries.o: \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
 	modules.o \
 	netcdf_interface_mod.o
@@ -1098,8 +1060,6 @@
 	disturb_heatflux.o \
 	large_scale_forcing_nudging_mod.o \
-	lpm_init.o \
 	model_1d_mod.o \
 	mod_kinds.o \
-	mod_particle_attributes.o \
 	modules.o \
 	multi_agent_system_mod.o \
@@ -1124,7 +1084,4 @@
 	modules.o \
 	vertical_nesting_mod.o
-init_dvrp.o: \
-	mod_kinds.o \
-	modules.o
 init_grid.o: \
 	advec_ws.o \
@@ -1167,4 +1124,16 @@
 	mod_kinds.o \
 	modules.o
+lagrangian_particle_model_mod.o: \
+	basic_constants_and_equations_mod.o \
+	cpulog_mod.o \
+	mod_kinds.o \
+	mod_particle_attributes.o \
+	modules.o \
+	pmc_interface_mod.o \
+	pmc_particle_interface.o \
+	netcdf_interface_mod.o \
+	random_function_mod.o \
+	random_generator_parallel_mod.o \
+	surface_mod.o
 land_surface_model_mod.o: \
 	basic_constants_and_equations_mod.o \
@@ -1194,122 +1163,4 @@
 	cpulog_mod.o \
 	mod_kinds.o \
-	modules.o
-lpm.o: \
-	cpulog_mod.o \
-	lpm_exchange_horiz.o \
-	lpm_init.o \
-	lpm_merging.o \
-	lpm_pack_arrays.o \
-	lpm_splitting.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	pmc_interface_mod.o \
-	pmc_particle_interface.o
-lpm_advec.o: \
-	basic_constants_and_equations_mod.o \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	surface_mod.o
-lpm_boundary_conds.o: \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	surface_mod.o
-lpm_calc_liquid_water_content.o: \
-	basic_constants_and_equations_mod.o \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_collision_kernels.o: \
-	basic_constants_and_equations_mod.o \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_data_output_particles.o: \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	netcdf_interface_mod.o
-lpm_droplet_collision.o: \
-	basic_constants_and_equations_mod.o \
-	cpulog_mod.o \
-	lpm_collision_kernels.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_droplet_condensation.o: \
-	basic_constants_and_equations_mod.o \
-	cpulog_mod.o \
-	lpm_collision_kernels.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_exchange_horiz.o: \
-	cpulog_mod.o \
-	lpm_pack_arrays.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	netcdf_interface_mod.o
-lpm_init.o: \
-	basic_constants_and_equations_mod.o \
-	lpm_collision_kernels.o \
-	lpm_exchange_horiz.o \
-	lpm_pack_arrays.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	netcdf_interface_mod.o \
-	pmc_particle_interface.o \
-	random_function_mod.o \
-	random_generator_parallel_mod.o \
-	surface_mod.o
-lpm_init_sgs_tke.o: \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	surface_mod.o
-lpm_merging.o: \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_pack_arrays.o: \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_read_restart_file.o: \
-	lpm_pack_arrays.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_set_attributes.o: \
-	cpulog_mod.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_splitting.o: \
-	basic_constants_and_equations_mod.o \
-	cpulog_mod.o \
-	lpm_exchange_horiz.o \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
-lpm_write_exchange_statistics.o: \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o \
-	pmc_particle_interface.o
-lpm_write_restart_file.o: \
-	mod_kinds.o \
-	mod_particle_attributes.o \
 	modules.o
 message.o: \
@@ -1330,6 +1181,8 @@
 	gust_mod.o \
 	indoor_model_mod.o \
+	lagrangian_particle_model_mod.o \
 	land_surface_model_mod.o \
 	large_scale_forcing_nudging_mod.o \
+	mod_particle_attributes.o \
 	multi_agent_system_mod.o \
 	nesting_offl_mod.o \
@@ -1406,8 +1259,4 @@
 	mod_kinds.o \
 	modules.o
-package_parin.o: \
-	mod_kinds.o \
-	mod_particle_attributes.o \
-	modules.o
 palm.o: \
 	bulk_cloud_model_mod.o \
@@ -1450,6 +1299,4 @@
 	surface_mod.o
 pmc_particle_interface.o: \
-	lpm_exchange_horiz.o \
-	lpm_pack_arrays.o \
 	mod_particle_attributes.o \
 	modules.o \
@@ -1608,7 +1455,4 @@
 	surface_mod.o \
 	netcdf_data_input_mod.o
-set_slicer_attributes_dvrp.o: \
-	mod_kinds.o \
-	modules.o
 singleton_mod.o: \
 	mod_kinds.o
@@ -1700,5 +1544,4 @@
 	land_surface_model_mod.o \
 	large_scale_forcing_nudging_mod.o \
-	lpm.o \
 	mod_kinds.o \
 	modules.o \
@@ -1783,12 +1626,4 @@
 	surface_mod.o \
 	user_module.o
-user_data_output_dvrp.o: \
-	mod_kinds.o \
-	modules.o \
-	user_module.o
-user_dvrp_coltab.o: \
-	mod_kinds.o \
-	modules.o \
-	user_module.o
 user_flight.o: \
 	mod_kinds.o \
@@ -1835,8 +1670,4 @@
 	user_module.o
 user_lpm_init.o: \
-	mod_kinds.o \
-	modules.o \
-	user_module.o
-user_lpm_set_attributes.o: \
 	mod_kinds.o \
 	modules.o \
@@ -1885,5 +1716,4 @@
 	diagnostic_output_quantities_mod.o \
 	mod_kinds.o \
-	mod_particle_attributes.o \
 	model_1d_mod.o \
 	module_interface.o \
Index: palm/trunk/SOURCE/check_parameters.f90
===================================================================
--- palm/trunk/SOURCE/check_parameters.f90	(revision 4015)
+++ palm/trunk/SOURCE/check_parameters.f90	(revision 4017)
@@ -790,6 +790,4 @@
     USE control_parameters
 
-    USE dvrp_variables
-
     USE grid_variables
 
@@ -817,6 +815,7 @@
                dots_max, dots_num, dots_label
 
-    USE particle_attributes
-
+    USE particle_attributes,                                                   &
+        ONLY:  particle_advection, use_sgs_for_particles
+        
     USE pegrid
 
@@ -1412,25 +1411,4 @@
        CALL message( 'check_parameters', 'PA0442', 1, 2, 0, 6, 0 )
     ENDIF
-
-!
-!-- Collision kernels:
-    SELECT CASE ( TRIM( collision_kernel ) )
-
-       CASE ( 'hall', 'hall_fast' )
-          hall_kernel = .TRUE.
-
-       CASE ( 'wang', 'wang_fast' )
-          wang_kernel = .TRUE.
-
-       CASE ( 'none' )
-
-
-       CASE DEFAULT
-          message_string = 'unknown collision kernel: collision_kernel = "' // &
-                           TRIM( collision_kernel ) // '"'
-          CALL message( 'check_parameters', 'PA0350', 1, 2, 0, 6, 0 )
-
-    END SELECT
-    IF ( collision_kernel(6:9) == 'fast' )  use_kernel_tables = .TRUE.
 
 !
@@ -3787,34 +3765,5 @@
        CALL message( 'check_parameters', 'PA0157', 1, 2, 0, 6, 0 )
     ENDIF
-
-!
-!-- Check particle attributes
-    IF ( particle_color /= 'none' )  THEN
-       IF ( particle_color /= 'absuv'  .AND.  particle_color /= 'pt*'  .AND.   &
-            particle_color /= 'z' )  THEN
-          message_string = 'illegal value for parameter particle_color: ' //   &
-                           TRIM( particle_color)
-          CALL message( 'check_parameters', 'PA0313', 1, 2, 0, 6, 0 )
-       ELSE
-          IF ( color_interval(2) <= color_interval(1) )  THEN
-             message_string = 'color_interval(2) <= color_interval(1)'
-             CALL message( 'check_parameters', 'PA0315', 1, 2, 0, 6, 0 )
-          ENDIF
-       ENDIF
-    ENDIF
-
-    IF ( particle_dvrpsize /= 'none' )  THEN
-       IF ( particle_dvrpsize /= 'absw' )  THEN
-          message_string = 'illegal value for parameter particle_dvrpsize:' // &
-                           ' ' // TRIM( particle_dvrpsize)
-          CALL message( 'check_parameters', 'PA0314', 1, 2, 0, 6, 0 )
-       ELSE
-          IF ( dvrpsize_interval(2) <= dvrpsize_interval(1) )  THEN
-             message_string = 'dvrpsize_interval(2) <= dvrpsize_interval(1)'
-             CALL message( 'check_parameters', 'PA0316', 1, 2, 0, 6, 0 )
-          ENDIF
-       ENDIF
-    ENDIF
-
+    
 !
 !-- Prevent empty time records in volume, cross-section and masked data in case
Index: palm/trunk/SOURCE/data_output_dvrp.f90
===================================================================
--- palm/trunk/SOURCE/data_output_dvrp.f90	(revision 4015)
+++ 	(revision )
@@ -1,591 +1,0 @@
-!> @file data_output_dvrp.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Renamed output variables
-! 
-! 3419 2018-10-24 17:27:31Z gronemeier
-! Modularization of all bulk cloud physics code components
-! 
-! 3045 2018-05-28 07:55:41Z Giersch
-! Code adjusted according to PALM coding standards
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2274 2017-06-09 13:27:48Z Giersch
-! Changed error messages
-! 
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1960 2016-07-12 16:34:24Z suehring
-! Separate humidity and passive scalar
-! 
-! 1873 2016-04-18 14:50:06Z maronga
-! Module renamed (removed _mod)
-! 
-! 
-! 1850 2016-04-08 13:29:27Z maronga
-! Module renamed
-!
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Particles and tails removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1353 2014-04-08 15:21:23Z heinze
-! REAL constants provided with KIND-attribute
-!
-! 1346 2014-03-27 13:18:20Z heinze
-! Bugfix: REAL constants provided with KIND-attribute especially in call of 
-! intrinsic function like MAX, MIN, SIGN
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1318 2014-03-17 13:35:16Z raasch
-! module interfaces removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 828 2012-02-21 12:00:36Z raasch
-! particle feature color renamed class
-!
-! Revision 1.1  2000/04/27 06:27:17  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> Plot of isosurface and slicers with dvrp-software
-!------------------------------------------------------------------------------!
- MODULE dvrp_color
- 
-
-    USE dvrp_variables
-    
-    USE kinds
-
-    IMPLICIT NONE
-
- CONTAINS
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
-    SUBROUTINE color_dvrp( value, color )
-
-       REAL(wp), INTENT(IN)  ::  value    !< 
-       REAL(wp), INTENT(OUT) ::  color(4) !< 
-
-       REAL(wp)              ::  scale    !< 
-
-       scale = ( value - slicer_range_limits_dvrp(1,islice_dvrp) ) /           &
-               ( slicer_range_limits_dvrp(2,islice_dvrp) -                     &
-                 slicer_range_limits_dvrp(1,islice_dvrp) )
-
-       scale = MODULO( 180.0_wp + 180.0_wp * scale, 360.0_wp )
-
-       color = (/ scale, 0.5_wp, 1.0_wp, 0.0_wp /)
-
-    END SUBROUTINE color_dvrp
-
- END MODULE dvrp_color
-
-
- RECURSIVE SUBROUTINE data_output_dvrp
-
-#if defined( __dvrp_graphics )
-
-    USE arrays_3d,                                                             &
-        ONLY:  p, pt, q, ql, s, ts, u, us, v, w, zu, d_exner
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  pi, lv_d_cp
-
-    USE control_parameters,                                                    &
-        ONLY:  cloud_droplets, do2d, do3d, humidity, ibc_uv_b,                 &
-               message_string, nz_do3d, passive_scalar, simulated_time,        &
-               threshold
-        
-    USE cpulog,                                                                &
-        ONLY:  log_point, log_point_s, cpu_log
-        
-    USE DVRP
-    
-    USE dvrp_color
-        
-    USE dvrp_variables
-        
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-        
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nzb
-        
-    USE kinds
-
-    USE bulk_cloud_model_mod,                                                  &
-        ONLY:  bulk_cloud_model
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=2) ::  section_chr      !< 
-    CHARACTER (LEN=6) ::  output_variable  !< 
-    
-    INTEGER(iwp) ::  c_mode           !<  
-    INTEGER(iwp) ::  c_size_x         !< 
-    INTEGER(iwp) ::  c_size_y         !< 
-    INTEGER(iwp) ::  c_size_z         !< 
-    INTEGER(iwp) ::  dvrp_nop         !< 
-    INTEGER(iwp) ::  dvrp_not         !< 
-    INTEGER(iwp) ::  gradient_normals !< 
-    INTEGER(iwp) ::  i                !< 
-    INTEGER(iwp) ::  ip               !< 
-    INTEGER(iwp) ::  j                !< 
-    INTEGER(iwp) ::  jp               !< 
-    INTEGER(iwp) ::  k                !< 
-    INTEGER(iwp) ::  l                !< 
-    INTEGER(iwp) ::  m                !< 
-    INTEGER(iwp) ::  n                !< 
-    INTEGER(iwp) ::  n_isosurface     !< 
-    INTEGER(iwp) ::  n_slicer         !< 
-    INTEGER(iwp) ::  nn               !< 
-    INTEGER(iwp) ::  section_mode     !< 
-    INTEGER(iwp) ::  vn               !< 
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  p_c  !< 
-    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  p_t  !< 
-
-    LOGICAL, DIMENSION(:), ALLOCATABLE ::  dvrp_mask  !< 
-
-    REAL(sp) ::  slicer_position  !< 
-    REAL(sp) ::  tmp_alpha        !< 
-    REAL(sp) ::  tmp_alpha_w      !< 
-    REAL(sp) ::  tmp_b            !< 
-    REAL(sp) ::  tmp_c_alpha      !< 
-    REAL(sp) ::  tmp_g            !< 
-    REAL(sp) ::  tmp_norm         !< 
-    REAL(sp) ::  tmp_pos          !< 
-    REAL(sp) ::  tmp_r            !< 
-    REAL(sp) ::  tmp_t            !< 
-    REAL(sp) ::  tmp_th           !< 
-    REAL(sp), DIMENSION(:),     ALLOCATABLE   ::  psize  !< 
-    REAL(sp), DIMENSION(:),     ALLOCATABLE   ::  p_x    !< 
-    REAL(sp), DIMENSION(:),     ALLOCATABLE   ::  p_y    !< 
-    REAL(sp), DIMENSION(:),     ALLOCATABLE   ::  p_z    !< 
-    REAL(sp), DIMENSION(:,:,:), ALLOCATABLE   ::  local_pf  !< 
-    REAL(sp), DIMENSION(:,:,:,:), ALLOCATABLE ::  local_pfi !< 
-
-
-    CALL cpu_log( log_point(27), 'data_output_dvrp', 'start' )
-
-!
-!-- Loop over all output modes choosed
-    m            = 1
-    n_isosurface = 0  ! isosurface counter (for threshold values and color)
-    n_slicer     = 0  ! slice plane counter (for range of values)
-    DO WHILE ( mode_dvrp(m) /= ' ' )
-!
-!--    Update of the steering variables
-       IF ( .NOT. lock_steering_update )  THEN
-!
-!--       Set lock to avoid recursive calls of DVRP_STEERING_UPDATE
-          lock_steering_update = .TRUE.
-!          CALL DVRP_STEERING_UPDATE( m-1, data_output_dvrp )
-          lock_steering_update = .FALSE.
-       ENDIF
-
-!
-!--    Determine the variable which shall be plotted (in case of slicers or
-!--    isosurfaces)
-       IF ( mode_dvrp(m)(1:10) == 'isosurface' )  THEN
-          READ ( mode_dvrp(m), '(10X,I2)' )  vn
-          output_variable = do3d(0,vn)
-          n_isosurface = n_isosurface + 1
-       ELSEIF ( mode_dvrp(m)(1:6) == 'slicer' )  THEN
-          READ ( mode_dvrp(m), '(6X,I2)' )  vn
-          output_variable = do2d(0,vn)
-          l = MAX( 2, LEN_TRIM( do2d(0,vn) ) )
-          section_chr = do2d(0,vn)(l-1:l)
-          SELECT CASE ( section_chr )
-             CASE ( 'xy' )
-                section_mode = 2
-                slicer_position = zu(MIN( slicer_position_dvrp(m), nz_do3d ))
-             CASE ( 'xz' )
-                section_mode = 1
-                slicer_position = slicer_position_dvrp(m) * dy
-             CASE ( 'yz' )
-                section_mode = 0
-                slicer_position = slicer_position_dvrp(m) * dx
-          END SELECT
-       ENDIF
-
-!
-!--    Select the plot mode (in case of isosurface or slicer only if user has
-!--    defined a variable which shall be plotted; otherwise do nothing)
-       IF ( ( mode_dvrp(m)(1:10) == 'isosurface'  .OR.                         &
-                  mode_dvrp(m)(1:6)  == 'slicer'           )                   &
-                  .AND.  output_variable /= ' ' )  THEN
-
-!
-!--       Create an intermediate array, properly dimensioned for plot-output 
-          ALLOCATE( local_pf(nxl_dvrp:nxr_dvrp+1,nys_dvrp:nyn_dvrp+1,          &
-                             nzb:nz_do3d) )
-
-!
-!--       Move original array to intermediate array
-          IF ( dvrp_overlap )  THEN
-
-             SELECT CASE ( output_variable )
-
-                CASE ( 'u', 'u_xy', 'u_xz', 'u_yz' )
-                   DO  i = nxl_dvrp, nxr_dvrp+1
-                      DO  j = nys_dvrp, nyn_dvrp+1
-                         DO  k = nzb, nz_do3d
-                            local_pf(i,j,k) = u(k,j,i)
-                         ENDDO
-                      ENDDO
-                   ENDDO
-!
-!--                Replace mirrored values at lower surface by real surface
-!--                values
-                   IF ( output_variable == 'u_xz'  .OR.                        &
-                        output_variable == 'u_yz' )  THEN
-                      IF ( ibc_uv_b == 0 )  local_pf(:,:,nzb) = 0.0_wp
-                   ENDIF
-
-
-                CASE ( 'v', 'v_xy', 'v_xz', 'v_yz' )
-                   DO  i = nxl_dvrp, nxr_dvrp+1
-                      DO  j = nys_dvrp, nyn_dvrp+1
-                         DO  k = nzb, nz_do3d
-                            local_pf(i,j,k) = v(k,j,i)
-                         ENDDO
-                      ENDDO
-                   ENDDO
-!
-!--                Replace mirrored values at lower surface by real surface
-!--                values
-                   IF ( output_variable == 'v_xz'  .OR.                        &
-                        output_variable == 'v_yz' )  THEN
-                      IF ( ibc_uv_b == 0 )  local_pf(:,:,nzb) = 0.0_wp
-                   ENDIF
-
-                CASE ( 'w', 'w_xy', 'w_xz', 'w_yz' )
-                   DO  i = nxl_dvrp, nxr_dvrp+1
-                      DO  j = nys_dvrp, nyn_dvrp+1
-                         DO  k = nzb, nz_do3d
-                            local_pf(i,j,k) = w(k,j,i)
-                         ENDDO
-                      ENDDO
-                   ENDDO
-! Averaging for Langmuir circulation
-!                   DO  k = nzb, nz_do3d
-!                      DO  j = nys_dvrp+1, nyn_dvrp
-!                         DO  i = nxl_dvrp, nxr_dvrp+1
-!                            local_pf(i,j,k) = 0.25 * local_pf(i,j-1,k) + &
-!                                              0.50 * local_pf(i,j,k)   + &
-!                                              0.25 * local_pf(i,j+1,k)
-!                         ENDDO
-!                      ENDDO
-!                   ENDDO
-
-                CASE ( 'p', 'p_xy', 'p_xz', 'p_yz' )
-                   DO  i = nxl_dvrp, nxr_dvrp+1
-                      DO  j = nys_dvrp, nyn_dvrp+1
-                         DO  k = nzb, nz_do3d
-                            local_pf(i,j,k) = p(k,j,i)
-                         ENDDO
-                      ENDDO
-                   ENDDO
-
-                CASE ( 'theta', 'theta_xy', 'theta_xz', 'theta_yz' )
-                   IF ( .NOT. bulk_cloud_model ) THEN
-                      DO  i = nxl_dvrp, nxr_dvrp+1
-                         DO  j = nys_dvrp, nyn_dvrp+1
-                            DO  k = nzb, nz_do3d
-                               local_pf(i,j,k) = pt(k,j,i)
-                            ENDDO
-                         ENDDO
-                      ENDDO
-                   ELSE
-                      DO  i = nxl_dvrp, nxr_dvrp+1
-                         DO  j = nys_dvrp, nyn_dvrp+1
-                            DO  k = nzb, nz_do3d
-                               local_pf(i,j,k) = pt(k,j,i) + lv_d_cp *         &
-                                                 d_exner(k) * ql(k,j,i)
-                            ENDDO
-                         ENDDO
-                      ENDDO
-                   ENDIF
-
-                CASE ( 'q', 'q_xy', 'q_xz', 'q_yz' )
-                   IF ( humidity )  THEN
-                      DO  i = nxl_dvrp, nxr_dvrp+1
-                         DO  j = nys_dvrp, nyn_dvrp+1
-                            DO  k = nzb, nz_do3d
-                               local_pf(i,j,k) = q(k,j,i)
-                            ENDDO
-                         ENDDO
-                      ENDDO            
-                   ELSE                    
-                      message_string = 'if humidity = '    //                  & 
-                            '.FALSE. output of ' // TRIM( output_variable ) // &
-                            'is not provided' 
-                      CALL message( 'data_output_dvrp', 'PA0183',&
-                                                                 0, 0, 0, 6, 0 )
-                   ENDIF
-             
-                CASE ( 'ql', 'ql_xy', 'ql_xz', 'ql_yz' )
-                   IF ( bulk_cloud_model  .OR.  cloud_droplets )  THEN
-                      DO  i = nxl_dvrp, nxr_dvrp+1
-                         DO  j = nys_dvrp, nyn_dvrp+1
-                            DO  k = nzb, nz_do3d
-                               local_pf(i,j,k) = ql(k,j,i)
-                            ENDDO
-                         ENDDO
-                      ENDDO
-                   ELSE                     
-                      message_string = 'if bulk_cloud_model = .FALSE. and ' // &
-                                  'cloud_droplets = .FALSE. '
-                                  'output of ' // TRIM( output_variable) //    &
-                                  'is not provided' 
-                      CALL message( 'data_output_dvrp', 'PA0184',&
-                                                                 0, 0, 0, 6, 0 )
-                   ENDIF
-
-                CASE ( 's', 's_xy', 's_xz', 's_yz' )
-                   IF ( passive_scalar )  THEN
-                      DO  i = nxl_dvrp, nxr_dvrp+1
-                         DO  j = nys_dvrp, nyn_dvrp+1
-                            DO  k = nzb, nz_do3d
-                               local_pf(i,j,k) = s(k,j,i)
-                            ENDDO
-                         ENDDO
-                      ENDDO            
-                   ELSE                    
-                      message_string = 'if passive_scalar = '    //            & 
-                            '.FALSE. output of ' // TRIM( output_variable ) //   &
-                            'is not provided' 
-                      CALL message( 'data_output_dvrp', 'PA0183',&
-                                                                 0, 0, 0, 6, 0 )
-                   ENDIF
-
-                CASE ( 'us*_xy' )
-                   DO  i = nxl_dvrp, nxr_dvrp+1
-                      DO  j = nys_dvrp, nyn_dvrp+1
-                         local_pf(i,j,nzb+1) = us(j,i)
-                      ENDDO
-                   ENDDO
-                   slicer_position = zu(nzb+1)
-
-                CASE ( 't*_xy' )
-                   DO  i = nxl_dvrp, nxr_dvrp+1
-                      DO  j = nys_dvrp, nyn_dvrp+1
-                         local_pf(i,j,nzb+1) = ts(j,i)
-                      ENDDO
-                   ENDDO
-                   slicer_position = zu(nzb+1)
-
-
-                CASE DEFAULT
-!
-!--                The DEFAULT case is reached either if output_variable
-!--                contains unsupported variable or if the user has coded a
-!--                special case in the user interface. There, the subroutine
-!--                user_data_output_dvrp checks which of these two conditions
-!--                applies.
-                   CALL user_data_output_dvrp( output_variable, local_pf )
-
-
-             END SELECT
-
-          ELSE
-!
-!--          No overlap of clipping domain with the current subdomain
-             DO  i = nxl_dvrp, nxr_dvrp+1
-                DO  j = nys_dvrp, nyn_dvrp+1
-                   DO  k = nzb, nz_do3d
-                      local_pf(i,j,k) = 0.0_wp
-                   ENDDO
-                ENDDO
-             ENDDO
-
-          ENDIF
-
-          IF ( mode_dvrp(m)(1:10) == 'isosurface' )  THEN
-
-!
-!--          DVRP-Calls for plotting isosurfaces:
-             CALL cpu_log( log_point_s(26), 'dvrp_isosurface', 'start' )
-
-!
-!--          Definition of isosurface color
-             tmp_r = isosurface_color(1,n_isosurface)
-             tmp_g = isosurface_color(2,n_isosurface)
-             tmp_b = isosurface_color(3,n_isosurface)
-             tmp_t = 0.0_wp
-             CALL DVRP_MATERIAL_RGB( m-1, 1, tmp_r, tmp_g, tmp_b, tmp_t )
-
-!
-!--          Compute and plot isosurface in dvr-format
-             CALL DVRP_DATA( m-1, local_pf, 1, nx_dvrp, ny_dvrp, nz_dvrp,      &
-                             cyclic_dvrp, cyclic_dvrp, cyclic_dvrp )
-
-             c_size_x = vc_size_x;  c_size_y = vc_size_y;  c_size_z = vc_size_z
-             CALL DVRP_CLUSTER_SIZE( m-1, c_size_x, c_size_y, c_size_z )
-
-             c_mode   = vc_mode 
-             CALL DVRP_CLUSTERING_MODE( m-1, c_mode )
-
-             gradient_normals = vc_gradient_normals
-             CALL DVRP_GRADIENTNORMALS( m-1, gradient_normals )
-
-!
-!--          A seperate procedure for setting vc_alpha will be in the next
-!--          version of libDVRP
-             tmp_c_alpha = vc_alpha 
-             CALL DVRP_THRESHOLD( -(m-1)-1, tmp_c_alpha )
-
-             IF ( dvrp_overlap )  THEN
-                tmp_th = threshold(n_isosurface)
-             ELSE
-                tmp_th = 1.0_wp  ! nothing is plotted because array values are 0
-             ENDIF
-
-             CALL DVRP_THRESHOLD( m-1, tmp_th )
-
-             CALL DVRP_VISUALIZE( m-1, 21, dvrp_filecount )
-
-             CALL cpu_log( log_point_s(26), 'dvrp_isosurface', 'stop' )
-
-          ELSEIF ( mode_dvrp(m)(1:6) == 'slicer' )  THEN
-
-!
-!--          DVRP-Calls for plotting slicers:
-             CALL cpu_log( log_point_s(27), 'dvrp_slicer', 'start' )
-
-!
-!--          Material and color definitions
-             tmp_r = 0.0_wp;  tmp_g = 0.0_wp;  tmp_b = 0.0_wp;  tmp_t = 0.0_wp
-             CALL DVRP_MATERIAL_RGB( m-1, 1, tmp_r, tmp_g, tmp_b, tmp_t )
-
-             n_slicer = n_slicer + 1
-
-!
-!--           Using dolorfunction has not been properly tested
-!             islice_dvrp = n_slicer
-!             CALL DVRP_COLORFUNCTION( m-1, DVRP_CM_HLS, 25,                 &
-!                                      slicer_range_limits_dvrp(:,n_slicer), &
-!                                      color_dvrp )
-
-!
-!--          Set interval of values defining the colortable
-             CALL set_slicer_attributes_dvrp( n_slicer )
-
-!
-!--          Create user-defined colortable
-             CALL user_dvrp_coltab( 'slicer', output_variable )
-
-             CALL DVRP_COLORTABLE_HLS( m-1, 1, interval_values_dvrp,           &
-                                       interval_h_dvrp, interval_l_dvrp,       &
-                                       interval_s_dvrp, interval_a_dvrp )
-
-!
-!--          Compute and plot slicer in dvr-format
-             CALL DVRP_DATA( m-1, local_pf, 1, nx_dvrp, ny_dvrp, nz_dvrp,      &
-                             cyclic_dvrp, cyclic_dvrp, cyclic_dvrp )
-             tmp_pos = slicer_position
-             CALL DVRP_SLICER( m-1, section_mode, tmp_pos )
-
-             CALL DVRP_VISUALIZE( m-1, 2, dvrp_filecount )
-
-             CALL cpu_log( log_point_s(27), 'dvrp_slicer', 'stop' )
-
-          ENDIF
-
-          DEALLOCATE( local_pf )
-
-       ELSEIF ( mode_dvrp(m)(1:9) == 'pathlines' ) THEN
-
-          ALLOCATE( local_pfi(4,nxl:nxr+1,nys:nyn+1,nzb:nz_do3d) )
-          DO  i = nxl, nxr+1
-             DO  j = nys, nyn+1
-                DO  k = nzb, nz_do3d
-                   local_pfi(1,i,j,k) = u(k,j,i)
-                   local_pfi(2,i,j,k) = v(k,j,i)
-                   local_pfi(3,i,j,k) = w(k,j,i)
-                   tmp_norm           = SQRT( u(k,j,i) * u(k,j,i) +            &
-                                              v(k,j,i) * v(k,j,i) +            &
-                                              w(k,j,i) * w(k,j,i) )
-                   tmp_alpha          = ACOS( 0.0_wp * u(k,j,i) / tmp_norm +   &
-                                              0.0_wp * v(k,j,i) / tmp_norm -   &
-                                              1.0_wp * w(k,j,i) / tmp_norm )
-                   tmp_alpha_w        = tmp_alpha / pi * 180.0_wp
-                   local_pfi(4,i,j,k) = tmp_alpha_w
-                ENDDO
-             ENDDO
-          ENDDO
-
-          CALL cpu_log( log_point_s(31), 'dvrp_pathlines', 'start' )
-
-          CALL DVRP_DATA( m-1, local_pfi, 4, nx_dvrp, ny_dvrp, nz_dvrp,        &
-                          cyclic_dvrp, cyclic_dvrp, cyclic_dvrp )
-          CALL DVRP_VISUALIZE( m-1, 20, dvrp_filecount )
-
-          CALL cpu_log( log_point_s(31), 'dvrp_pathlines', 'stop' )
-
-          DEALLOCATE( local_pfi )
-
-       ENDIF
-
-       m = m + 1
-
-    ENDDO
-
-    dvrp_filecount = dvrp_filecount + 1
-
-    CALL cpu_log( log_point(27), 'data_output_dvrp', 'stop' )
-
-#endif
- END SUBROUTINE data_output_dvrp
Index: palm/trunk/SOURCE/data_output_ptseries.f90
===================================================================
--- palm/trunk/SOURCE/data_output_ptseries.f90	(revision 4015)
+++ 	(revision )
@@ -1,399 +1,0 @@
-!> @file data_output_ptseries.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Corrected "Former revisions" section
-!
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2312 2017-07-14 20:26:51Z hoffmann
-! SGS velocities also possible for curvature_solution_effects = .TRUE. 
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1831 2016-04-07 13:15:51Z hoffmann
-! curvature_solution_effects moved to particle_attributes
-!
-! 1783 2016-03-06 18:36:17Z raasch
-! netcdf module name changed + related changes
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-!
-! 1353 2014-04-08 15:21:23Z heinze
-! REAL constants provided with KIND-attribute
-!
-! 1327 2014-03-21 11:00:16Z raasch
-! -netcdf output queries
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1318 2014-03-17 13:35:16Z raasch
-! barrier argument removed from cpu_log,
-! module interfaces removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 825 2012-02-19 03:03:44Z raasch
-! mean/minimum/maximum particle radius added as output quantity,
-! particle attributes speed_x|y|z_sgs renamed rvar1|2|3
-!
-! Revision 1.1  2006/08/04 14:24:18  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> Output of particle data timeseries in NetCDF format.
-!------------------------------------------------------------------------------!
- SUBROUTINE data_output_ptseries
-
-
-    USE control_parameters,                                                    &
-        ONLY:  dopts_time_count, time_since_reference_point
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point
-
-    USE indices,                                                               &
-        ONLY: nxl, nxr, nys, nyn, nzb, nzt
-
-    USE kinds
-
-#if defined( __netcdf )
-    USE NETCDF
-#endif
-
-    USE netcdf_interface,                                                      &
-        ONLY:  dopts_num, id_set_pts, id_var_dopts, id_var_time_pts, nc_stat,  &
-               netcdf_handle_error
-
-    USE particle_attributes,                                                   &
-        ONLY:  grid_particles, number_of_particles, number_of_particle_groups, &
-               particles, prt_count
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-
-    INTEGER(iwp) ::  i    !<
-    INTEGER(iwp) ::  inum !<
-    INTEGER(iwp) ::  j    !<
-    INTEGER(iwp) ::  jg   !<
-    INTEGER(iwp) ::  k    !<
-    INTEGER(iwp) ::  n    !<
-
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pts_value   !<
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pts_value_l !<
-
-
-    CALL cpu_log( log_point(36), 'data_output_ptseries', 'start' )
-
-    IF ( myid == 0 )  THEN
-!
-!--    Open file for time series output in NetCDF format
-       dopts_time_count = dopts_time_count + 1
-       CALL check_open( 109 )
-#if defined( __netcdf )
-!
-!--    Update the particle time series time axis
-       nc_stat = NF90_PUT_VAR( id_set_pts, id_var_time_pts,      &
-                               (/ time_since_reference_point /), &
-                               start = (/ dopts_time_count /), count = (/ 1 /) )
-       CALL netcdf_handle_error( 'data_output_ptseries', 391 )
-#endif
-
-    ENDIF
-
-    ALLOCATE( pts_value(0:number_of_particle_groups,dopts_num), &
-              pts_value_l(0:number_of_particle_groups,dopts_num) )
-
-    pts_value_l = 0.0_wp
-    pts_value_l(:,16) = 9999999.9_wp    ! for calculation of minimum radius
-
-!
-!-- Calculate or collect the particle time series quantities for all particles
-!-- and seperately for each particle group (if there is more than one group)
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb, nzt
-             number_of_particles = prt_count(k,j,i)
-             IF (number_of_particles <= 0)  CYCLE
-             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-             DO  n = 1, number_of_particles
-
-                IF ( particles(n)%particle_mask )  THEN  ! Restrict analysis to active particles
-
-                   pts_value_l(0,1)  = pts_value_l(0,1) + 1.0_wp  ! total # of particles
-                   pts_value_l(0,2)  = pts_value_l(0,2) +                      &
-                          ( particles(n)%x - particles(n)%origin_x )  ! mean x
-                   pts_value_l(0,3)  = pts_value_l(0,3) +                      &
-                          ( particles(n)%y - particles(n)%origin_y )  ! mean y
-                   pts_value_l(0,4)  = pts_value_l(0,4) +                      &
-                          ( particles(n)%z - particles(n)%origin_z )  ! mean z
-                   pts_value_l(0,5)  = pts_value_l(0,5) + particles(n)%z        ! mean z (absolute)
-                   pts_value_l(0,6)  = pts_value_l(0,6) + particles(n)%speed_x  ! mean u
-                   pts_value_l(0,7)  = pts_value_l(0,7) + particles(n)%speed_y  ! mean v
-                   pts_value_l(0,8)  = pts_value_l(0,8) + particles(n)%speed_z  ! mean w
-                   pts_value_l(0,9)  = pts_value_l(0,9)  + particles(n)%rvar1 ! mean sgsu
-                   pts_value_l(0,10) = pts_value_l(0,10) + particles(n)%rvar2 ! mean sgsv
-                   pts_value_l(0,11) = pts_value_l(0,11) + particles(n)%rvar3 ! mean sgsw
-                   IF ( particles(n)%speed_z > 0.0_wp )  THEN
-                      pts_value_l(0,12) = pts_value_l(0,12) + 1.0_wp  ! # of upward moving prts
-                      pts_value_l(0,13) = pts_value_l(0,13) +                  &
-                                              particles(n)%speed_z ! mean w upw.
-                   ELSE
-                      pts_value_l(0,14) = pts_value_l(0,14) +                  &
-                                              particles(n)%speed_z ! mean w down
-                   ENDIF
-                   pts_value_l(0,15) = pts_value_l(0,15) + particles(n)%radius ! mean rad
-                   pts_value_l(0,16) = MIN( pts_value_l(0,16), particles(n)%radius ) ! minrad
-                   pts_value_l(0,17) = MAX( pts_value_l(0,17), particles(n)%radius ) ! maxrad
-                   pts_value_l(0,18) = pts_value_l(0,18) + 1.0_wp
-                   pts_value_l(0,19) = pts_value_l(0,18) + 1.0_wp
-!
-!--                Repeat the same for the respective particle group
-                   IF ( number_of_particle_groups > 1 )  THEN
-                      jg = particles(n)%group
-
-                      pts_value_l(jg,1)  = pts_value_l(jg,1) + 1.0_wp
-                      pts_value_l(jg,2)  = pts_value_l(jg,2) +                   &
-                           ( particles(n)%x - particles(n)%origin_x )
-                      pts_value_l(jg,3)  = pts_value_l(jg,3) +                   &
-                           ( particles(n)%y - particles(n)%origin_y )
-                      pts_value_l(jg,4)  = pts_value_l(jg,4) +                   &
-                           ( particles(n)%z - particles(n)%origin_z )
-                      pts_value_l(jg,5)  = pts_value_l(jg,5) + particles(n)%z
-                      pts_value_l(jg,6)  = pts_value_l(jg,6) + particles(n)%speed_x
-                      pts_value_l(jg,7)  = pts_value_l(jg,7) + particles(n)%speed_y
-                      pts_value_l(jg,8)  = pts_value_l(jg,8) + particles(n)%speed_z
-                      pts_value_l(jg,9)  = pts_value_l(jg,9)  + particles(n)%rvar1
-                      pts_value_l(jg,10) = pts_value_l(jg,10) + particles(n)%rvar2
-                      pts_value_l(jg,11) = pts_value_l(jg,11) + particles(n)%rvar3
-                      IF ( particles(n)%speed_z > 0.0_wp )  THEN
-                         pts_value_l(jg,12) = pts_value_l(jg,12) + 1.0_wp
-                         pts_value_l(jg,13) = pts_value_l(jg,13) + particles(n)%speed_z
-                      ELSE
-                         pts_value_l(jg,14) = pts_value_l(jg,14) + particles(n)%speed_z
-                      ENDIF
-                      pts_value_l(jg,15) = pts_value_l(jg,15) + particles(n)%radius
-                      pts_value_l(jg,16) = MIN( pts_value(jg,16), particles(n)%radius )
-                      pts_value_l(jg,17) = MAX( pts_value(jg,17), particles(n)%radius )
-                      pts_value_l(jg,18) = pts_value_l(jg,18) + 1.0_wp
-                      pts_value_l(jg,19) = pts_value_l(jg,19) + 1.0_wp
-                   ENDIF
-
-                ENDIF
-
-             ENDDO
-
-          ENDDO
-       ENDDO
-    ENDDO
-
-
-#if defined( __parallel )
-!
-!-- Sum values of the subdomains
-    inum = number_of_particle_groups + 1
-
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( pts_value_l(0,1), pts_value(0,1), 15*inum, MPI_REAL, &
-                        MPI_SUM, comm2d, ierr )
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( pts_value_l(0,16), pts_value(0,16), inum, MPI_REAL, &
-                        MPI_MIN, comm2d, ierr )
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( pts_value_l(0,17), pts_value(0,17), inum, MPI_REAL, &
-                        MPI_MAX, comm2d, ierr )
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( pts_value_l(0,18), pts_value(0,18), inum, MPI_REAL, &
-                        MPI_MAX, comm2d, ierr )
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( pts_value_l(0,19), pts_value(0,19), inum, MPI_REAL, &
-                        MPI_MIN, comm2d, ierr )
-#else
-    pts_value(:,1:19) = pts_value_l(:,1:19)
-#endif
-
-!
-!-- Normalize the above calculated quantities (except min/max values) with the
-!-- total number of particles
-    IF ( number_of_particle_groups > 1 )  THEN
-       inum = number_of_particle_groups
-    ELSE
-       inum = 0
-    ENDIF
-
-    DO  j = 0, inum
-
-       IF ( pts_value(j,1) > 0.0_wp )  THEN
-
-          pts_value(j,2:15) = pts_value(j,2:15) / pts_value(j,1)
-          IF ( pts_value(j,12) > 0.0_wp  .AND.  pts_value(j,12) < 1.0_wp )  THEN
-             pts_value(j,13) = pts_value(j,13) / pts_value(j,12)
-             pts_value(j,14) = pts_value(j,14) / ( 1.0_wp - pts_value(j,12) )
-          ELSEIF ( pts_value(j,12) == 0.0_wp )  THEN
-             pts_value(j,13) = -1.0_wp
-          ELSE
-             pts_value(j,14) = -1.0_wp
-          ENDIF
-
-       ENDIF
-
-    ENDDO
-
-!
-!-- Calculate higher order moments of particle time series quantities,
-!-- seperately for each particle group (if there is more than one group)
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb, nzt
-             number_of_particles = prt_count(k,j,i)
-             IF (number_of_particles <= 0)  CYCLE
-             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-             DO  n = 1, number_of_particles
-
-                pts_value_l(0,20) = pts_value_l(0,20) + ( particles(n)%x - &
-                                    particles(n)%origin_x - pts_value(0,2) )**2 ! x*2
-                pts_value_l(0,21) = pts_value_l(0,21) + ( particles(n)%y - &
-                                    particles(n)%origin_y - pts_value(0,3) )**2 ! y*2
-                pts_value_l(0,22) = pts_value_l(0,22) + ( particles(n)%z - &
-                                    particles(n)%origin_z - pts_value(0,4) )**2 ! z*2
-                pts_value_l(0,23) = pts_value_l(0,23) + ( particles(n)%speed_x - &
-                                                         pts_value(0,6) )**2   ! u*2
-                pts_value_l(0,24) = pts_value_l(0,24) + ( particles(n)%speed_y - &
-                                                          pts_value(0,7) )**2   ! v*2
-                pts_value_l(0,25) = pts_value_l(0,25) + ( particles(n)%speed_z - &
-                                                          pts_value(0,8) )**2   ! w*2
-                pts_value_l(0,26) = pts_value_l(0,26) + ( particles(n)%rvar1 - &
-                                                          pts_value(0,9) )**2   ! u"2
-                pts_value_l(0,27) = pts_value_l(0,27) + ( particles(n)%rvar2 - &
-                                                          pts_value(0,10) )**2  ! v"2
-                pts_value_l(0,28) = pts_value_l(0,28) + ( particles(n)%rvar3 - &
-                                                          pts_value(0,11) )**2  ! w"2
-!
-!--             Repeat the same for the respective particle group
-                IF ( number_of_particle_groups > 1 )  THEN
-                   jg = particles(n)%group
-
-                   pts_value_l(jg,20) = pts_value_l(jg,20) + ( particles(n)%x - &
-                                       particles(n)%origin_x - pts_value(jg,2) )**2
-                   pts_value_l(jg,21) = pts_value_l(jg,21) + ( particles(n)%y - &
-                                       particles(n)%origin_y - pts_value(jg,3) )**2
-                   pts_value_l(jg,22) = pts_value_l(jg,22) + ( particles(n)%z - &
-                                       particles(n)%origin_z - pts_value(jg,4) )**2
-                   pts_value_l(jg,23) = pts_value_l(jg,23) + ( particles(n)%speed_x - &
-                                                             pts_value(jg,6) )**2
-                   pts_value_l(jg,24) = pts_value_l(jg,24) + ( particles(n)%speed_y - &
-                                                             pts_value(jg,7) )**2
-                   pts_value_l(jg,25) = pts_value_l(jg,25) + ( particles(n)%speed_z - &
-                                                             pts_value(jg,8) )**2
-                   pts_value_l(jg,26) = pts_value_l(jg,26) + ( particles(n)%rvar1 - &
-                                                             pts_value(jg,9) )**2
-                   pts_value_l(jg,27) = pts_value_l(jg,27) + ( particles(n)%rvar2 - &
-                                                             pts_value(jg,10) )**2
-                   pts_value_l(jg,28) = pts_value_l(jg,28) + ( particles(n)%rvar3 - &
-                                                             pts_value(jg,11) )**2
-                ENDIF
-
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO
-
-    pts_value_l(0,29) = ( number_of_particles - pts_value(0,1) / numprocs )**2
-                                                 ! variance of particle numbers
-    IF ( number_of_particle_groups > 1 )  THEN
-       DO  j = 1, number_of_particle_groups
-          pts_value_l(j,29) = ( pts_value_l(j,1) - &
-                                pts_value(j,1) / numprocs )**2
-       ENDDO
-    ENDIF
-
-#if defined( __parallel )
-!
-!-- Sum values of the subdomains
-    inum = number_of_particle_groups + 1
-
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( pts_value_l(0,20), pts_value(0,20), inum*10, MPI_REAL, &
-                        MPI_SUM, comm2d, ierr )
-#else
-    pts_value(:,20:29) = pts_value_l(:,20:29)
-#endif
-
-!
-!-- Normalize the above calculated quantities with the total number of
-!-- particles
-    IF ( number_of_particle_groups > 1 )  THEN
-       inum = number_of_particle_groups
-    ELSE
-       inum = 0
-    ENDIF
-
-    DO  j = 0, inum
-
-       IF ( pts_value(j,1) > 0.0_wp )  THEN
-          pts_value(j,20:28) = pts_value(j,20:28) / pts_value(j,1)
-       ENDIF
-       pts_value(j,29) = pts_value(j,29) / numprocs
-
-    ENDDO
-
-#if defined( __netcdf )
-!
-!-- Output particle time series quantities in NetCDF format
-    IF ( myid == 0 )  THEN
-       DO  j = 0, inum
-          DO  i = 1, dopts_num
-             nc_stat = NF90_PUT_VAR( id_set_pts, id_var_dopts(i,j),  &
-                                     (/ pts_value(j,i) /),           &
-                                     start = (/ dopts_time_count /), &
-                                     count = (/ 1 /) )
-             CALL netcdf_handle_error( 'data_output_ptseries', 392 )
-          ENDDO
-       ENDDO
-    ENDIF
-#endif
-
-    DEALLOCATE( pts_value, pts_value_l )
-
-    CALL cpu_log( log_point(36), 'data_output_ptseries', 'stop' )
-
- END SUBROUTINE data_output_ptseries
Index: palm/trunk/SOURCE/header.f90
===================================================================
--- palm/trunk/SOURCE/header.f90	(revision 4015)
+++ palm/trunk/SOURCE/header.f90	(revision 4017)
@@ -1,3 +1,3 @@
-!> @file header.f90
+! !> @file header.f90
 !------------------------------------------------------------------------------!
 ! This file is part of the PALM model system.
@@ -437,7 +437,4 @@
         ONLY:  day_of_year_init, time_utc_init
 
-    USE dvrp_variables,                                                        &
-        ONLY:  use_seperate_pe_for_dvrp_output
-
     USE grid_variables,                                                        &
         ONLY:  dx, dy
@@ -462,17 +459,4 @@
                sa_vertical_gradient, sa_vertical_gradient_level,               &
                sa_vertical_gradient_level_ind
-
-    USE particle_attributes,                                                   &
-        ONLY:  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t, collision_kernel,     &
-               curvature_solution_effects,                                     &
-               density_ratio, dissipation_classes, dt_min_part, dt_prel,       &
-               dt_write_particle_data, end_time_prel,                          &
-               number_of_particle_groups, particle_advection,                  &
-               particle_advection_start,                                       &
-               particles_per_point, pdx, pdy, pdz,  psb, psl, psn, psr, pss,   &
-               pst, radius, radius_classes, random_start_position,             &
-               seed_follows_topography,                                        &
-               total_number_of_particles, use_sgs_for_particles,               &
-               vertical_particle_advection, write_particle_statistics
 
     USE pegrid
@@ -509,5 +493,5 @@
     
     CHARACTER (LEN=40) ::  output_format       !< netcdf format
-    
+        
     CHARACTER (LEN=70) ::  char1               !< dummy varialbe used for various strings
     CHARACTER (LEN=70) ::  char2               !< string containing informating about the advected distance in case of Galilei transformation
@@ -651,5 +635,4 @@
        WRITE ( io, 107 )  'y'
     ENDIF
-    IF ( use_seperate_pe_for_dvrp_output )  WRITE ( io, 105 )
     IF ( numprocs /= maximum_parallel_io_streams )  THEN
        WRITE ( io, 108 )  maximum_parallel_io_streams
@@ -1836,47 +1819,4 @@
     ENDIF
 
-#if defined( __dvrp_graphics )
-!
-!-- Dvrp-output
-    IF ( dt_dvrp /= 9999999.9_wp )  THEN
-       WRITE ( io, 360 )  dt_dvrp, TRIM( dvrp_output ), TRIM( dvrp_host ), &
-                          TRIM( dvrp_username ), TRIM( dvrp_directory )
-       i = 1
-       l = 0
-       m = 0
-       DO WHILE ( mode_dvrp(i) /= ' ' )
-          IF ( mode_dvrp(i)(1:10) == 'isosurface' )  THEN
-             READ ( mode_dvrp(i), '(10X,I2)' )  j
-             l = l + 1
-             IF ( do3d(0,j) /= ' ' )  THEN
-                WRITE ( io, 361 )  TRIM( do3d(0,j) ), threshold(l), &
-                                   isosurface_color(:,l)
-             ENDIF
-          ELSEIF ( mode_dvrp(i)(1:6) == 'slicer' )  THEN
-             READ ( mode_dvrp(i), '(6X,I2)' )  j
-             m = m + 1
-             IF ( do2d(0,j) /= ' ' )  THEN
-                WRITE ( io, 362 )  TRIM( do2d(0,j) ), &
-                                   slicer_range_limits_dvrp(:,m)
-             ENDIF
-          ENDIF
-          i = i + 1
-       ENDDO
-
-       WRITE ( io, 365 )  groundplate_color, superelevation_x, &
-                          superelevation_y, superelevation, clip_dvrp_l, &
-                          clip_dvrp_r, clip_dvrp_s, clip_dvrp_n
-
-       IF ( TRIM( topography ) /= 'flat' )  THEN
-          WRITE ( io, 366 )  topography_color
-          IF ( cluster_size > 1 )  THEN
-             WRITE ( io, 367 )  cluster_size
-          ENDIF
-       ENDIF
-
-    ENDIF
-#endif
-
-
     WRITE ( io, 99 )
 
@@ -1912,17 +1852,4 @@
        WRITE ( io, 431 )
     ENDIF
-    IF ( humidity  .AND.  cloud_droplets )  THEN
-       WRITE ( io, 433 )
-       IF ( curvature_solution_effects )  WRITE ( io, 434 )
-       IF ( collision_kernel /= 'none' )  THEN
-          WRITE ( io, 435 )  TRIM( collision_kernel )
-          IF ( collision_kernel(6:9) == 'fast' )  THEN
-             WRITE ( io, 436 )  radius_classes, dissipation_classes
-          ENDIF
-       ELSE
-          WRITE ( io, 437 )
-       ENDIF
-    ENDIF
-
 !
 !-- LES / turbulence parameters
@@ -1967,53 +1894,4 @@
        WRITE ( io, 477 )  q_surface_initial_change       
     ENDIF
-
-    IF ( particle_advection )  THEN
-!
-!--    Particle attributes
-       WRITE ( io, 480 )  particle_advection_start, dt_prel, bc_par_lr, &
-                          bc_par_ns, bc_par_b, bc_par_t, particle_maximum_age, &
-                          end_time_prel
-       IF ( use_sgs_for_particles )  WRITE ( io, 488 )  dt_min_part
-       IF ( random_start_position )  WRITE ( io, 481 )
-       IF ( seed_follows_topography )  WRITE ( io, 496 )
-       IF ( particles_per_point > 1 )  WRITE ( io, 489 )  particles_per_point
-       WRITE ( io, 495 )  total_number_of_particles
-       IF ( dt_write_particle_data /= 9999999.9_wp )  THEN
-          WRITE ( io, 485 )  dt_write_particle_data
-          IF ( netcdf_data_format > 1 )  THEN
-             output_format = 'netcdf (64 bit offset) and binary'
-          ELSE
-             output_format = 'netcdf and binary'
-          ENDIF
-          IF ( netcdf_deflate == 0 )  THEN
-             WRITE ( io, 344 )  output_format
-          ELSE
-             WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
-          ENDIF
-       ENDIF
-       IF ( dt_dopts /= 9999999.9_wp )  WRITE ( io, 494 )  dt_dopts
-       IF ( write_particle_statistics )  WRITE ( io, 486 )
-
-       WRITE ( io, 487 )  number_of_particle_groups
-
-       DO  i = 1, number_of_particle_groups
-          IF ( i == 1  .AND.  density_ratio(i) == 9999999.9_wp )  THEN
-             WRITE ( io, 490 )  i, 0.0_wp
-             WRITE ( io, 492 )
-          ELSE
-             WRITE ( io, 490 )  i, radius(i)
-             IF ( density_ratio(i) /= 0.0_wp )  THEN
-                WRITE ( io, 491 )  density_ratio(i)
-             ELSE
-                WRITE ( io, 492 )
-             ENDIF
-          ENDIF
-          WRITE ( io, 493 )  psl(i), psr(i), pss(i), psn(i), psb(i), pst(i), &
-                             pdx(i), pdy(i), pdz(i)
-          IF ( .NOT. vertical_particle_advection(i) )  WRITE ( io, 482 )
-       ENDDO
-
-    ENDIF
-
 
 !
@@ -2055,5 +1933,4 @@
 104 FORMAT (' Number of PEs:',10X,I6,4X,'Tasks:',I4,'   threads per task:',I4/ &
               35X,'Processor grid (x,y): (',I4,',',I4,')',1X,A)
-105 FORMAT (35X,'One additional PE is used to handle'/37X,'the dvrp output!')
 107 FORMAT (35X,'A 1d-decomposition along ',A,' is used')
 108 FORMAT (35X,'Max. # of parallel I/O streams is ',I5)
@@ -2278,23 +2155,4 @@
 353 FORMAT  (/'       Number of output time levels allowed: unlimited' /)
 354 FORMAT ('       Output format: ',A, '   compressed with level: ',I1/)
-#if defined( __dvrp_graphics )
-360 FORMAT ('    Plot-Sequence with dvrp-software:'/ &
-            '       Output every      ',F7.1,' s'/ &
-            '       Output mode:      ',A/ &
-            '       Host / User:      ',A,' / ',A/ &
-            '       Directory:        ',A// &
-            '       The sequence contains:')
-361 FORMAT (/'       Isosurface of "',A,'"    Threshold value: ', E12.3/ &
-            '          Isosurface color: (',F4.2,',',F4.2,',',F4.2,') (R,G,B)')
-362 FORMAT (/'       Slicer plane ',A/ &
-            '       Slicer limits: [',F6.2,',',F6.2,']')
-365 FORMAT (/'       Groundplate color: (',F4.2,',',F4.2,',',F4.2,') (R,G,B)'/ &
-            '       Superelevation along (x,y,z): (',F4.1,',',F4.1,',',F4.1, &
-                     ')'/ &
-            '       Clipping limits: from x = ',F9.1,' m to x = ',F9.1,' m'/ &
-            '                        from y = ',F9.1,' m to y = ',F9.1,' m')
-366 FORMAT (/'       Topography color: (',F4.2,',',F4.2,',',F4.2,') (R,G,B)')
-367 FORMAT ('       Polygon reduction for topography: cluster_size = ', I1)
-#endif
 400 FORMAT (//' Physical quantities:'/ &
               ' -------------------'/)
@@ -2350,16 +2208,4 @@
               ' ----------------------------------'/)
 431 FORMAT ('    Humidity is considered, bu no condensation')
-433 FORMAT ('    Cloud droplets treated explicitly using the Lagrangian part', &
-                 'icle model')
-434 FORMAT ('    Curvature and solution effecs are considered for growth of', &
-                 ' droplets < 1.0E-6 m')
-435 FORMAT ('    Droplet collision is handled by ',A,'-kernel')
-436 FORMAT ('       Fast kernel with fixed radius- and dissipation classes ', &
-                    'are used'/ &
-            '          number of radius classes:       ',I3,'    interval ', &
-                       '[1.0E-6,2.0E-4] m'/ &
-            '          number of dissipation classes:   ',I2,'    interval ', &
-                       '[0,1000] cm**2/s**3')
-437 FORMAT ('    Droplet collision is switched off')
 450 FORMAT (//' LES / Turbulence quantities:'/ &
               ' ---------------------------'/)
@@ -2401,37 +2247,4 @@
             '    value is negative) by ',E8.1,' kg/m**3 at the beginning of', &
                  ' the 3D-simulation'/)
-480 FORMAT ('    Particles:'/ &
-            '    ---------'// &
-            '       Particle advection is active (switched on at t = ', F7.1, &
-                    ' s)'/ &
-            '       Start of new particle generations every  ',F6.1,' s'/ &
-            '       Boundary conditions: left/right: ', A, ' north/south: ', A/&
-            '                            bottom:     ', A, ' top:         ', A/&
-            '       Maximum particle age:                 ',F9.1,' s'/ &
-            '       Advection stopped at t = ',F9.1,' s'/)
-481 FORMAT ('       Particles have random start positions'/)
-482 FORMAT ('          Particles are advected only horizontally'/)
-485 FORMAT ('       Particle data are written on file every ', F9.1, ' s')
-486 FORMAT ('       Particle statistics are written on file'/)
-487 FORMAT ('       Number of particle groups: ',I2/)
-488 FORMAT ('       SGS velocity components are used for particle advection'/ &
-            '          minimum timestep for advection:', F8.5/)
-489 FORMAT ('       Number of particles simultaneously released at each ', &
-                    'point: ', I5/)
-490 FORMAT ('       Particle group ',I2,':'/ &
-            '          Particle radius: ',E10.3, 'm')
-491 FORMAT ('          Particle inertia is activated'/ &
-            '             density_ratio (rho_fluid/rho_particle) =',F6.3/)
-492 FORMAT ('          Particles are advected only passively (no inertia)'/)
-493 FORMAT ('          Boundaries of particle source: x:',F8.1,' - ',F8.1,' m'/&
-            '                                         y:',F8.1,' - ',F8.1,' m'/&
-            '                                         z:',F8.1,' - ',F8.1,' m'/&
-            '          Particle distances:  dx = ',F8.1,' m  dy = ',F8.1, &
-                       ' m  dz = ',F8.1,' m'/)
-494 FORMAT ('       Output of particle time series in NetCDF format every ', &
-                    F8.2,' s'/)
-495 FORMAT ('       Number of particles in total domain: ',I10/)
-496 FORMAT ('       Initial vertical particle positions are interpreted ', &
-                    'as relative to the given topography')
 500 FORMAT (//' 1D-Model parameters:'/                           &
               ' -------------------'//                           &
Index: palm/trunk/SOURCE/init_3d_model.f90
===================================================================
--- palm/trunk/SOURCE/init_3d_model.f90	(revision 4015)
+++ palm/trunk/SOURCE/init_3d_model.f90	(revision 4017)
@@ -625,7 +625,4 @@
 
     USE kinds
-
-    USE lpm_init_mod,                                                          &
-        ONLY:  lpm_init
   
     USE lsf_nudging_mod,                                                       &
@@ -652,7 +649,4 @@
     USE nesting_offl_mod,                                                      &
         ONLY:  nesting_offl_init
-
-    USE particle_attributes,                                                   &
-        ONLY:  particle_advection
 
     USE pegrid
@@ -2271,12 +2265,4 @@
     ENDIF
 
-!
-!-- If required, initialize dvrp-software
-    IF ( dt_dvrp /= 9999999.9_wp )  CALL init_dvrp
-
-!
-!-- Initialize quantities for handling cloud physics.
-!-- This routine must be called before lpm_init, becaus otherwise,
-!-- array d_exner, needed in data_output_dvrp (called by lpm_init) is not defined.
     IF ( .NOT. ocean_mode )  THEN
 
@@ -2307,7 +2293,4 @@
     ENDIF
 
-!
-!-- If required, initialize particles
-    IF ( particle_advection )  CALL lpm_init
 !
 !-- If required, initialize particles
Index: palm/trunk/SOURCE/init_dvrp.f90
===================================================================
--- palm/trunk/SOURCE/init_dvrp.f90	(revision 4015)
+++ 	(revision )
@@ -1,870 +1,0 @@
-!> @file init_dvrp.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2516 2017-10-04 11:03:04Z suehring
-! Remove tabs
-! 
-! 2514 2017-10-04 09:52:37Z suehring
-! NEC related cpp directives removed
-! 
-! 2298 2017-06-29 09:28:18Z raasch
-! MPI2 related part removed
-! 
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1808 2016-04-05 19:44:00Z raasch
-! routine local_getenv replaced by standard FORTRAN routine
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1353 2014-04-08 15:21:23Z heinze
-! REAL constants provided with KIND-attribute 
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! REAL constants defined as wp-kind
-! 
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements, 
-! kinds are defined in new module kinds, 
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements 
-! 
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! Revision 1.1  2000/04/27 06:24:39  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> Initializing actions needed when using dvrp-software
-!------------------------------------------------------------------------------!
-  SUBROUTINE init_dvrp
- 
-#if defined( __dvrp_graphics )
-
-    USE arrays_3d,                                                             &
-        ONLY:  zu
-        
-    USE DVRP
-    
-    USE dvrp_variables
-    
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-        
-    USE indices,                                                               &
-        ONLY:  nx, nxl, nxr, ny, nyn, nys, nzb, nzb_s_inner
-        
-    USE kinds
-    
-    USE pegrid
-    
-    USE control_parameters,                                                               &
-        ONLY:  message_string, nz_do3d, run_identifier, topography
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=2)  ::  section_chr      !<
-    CHARACTER (LEN=3)  ::  prefix_chr       !<
-    CHARACTER (LEN=80) ::  dvrp_file_local  !<
-    
-    INTEGER(iwp) ::  cluster_mode      !<
-    INTEGER(iwp) ::  cluster_size_x    !<
-    INTEGER(iwp) ::  cluster_size_y    !<
-    INTEGER(iwp) ::  cluster_size_z    !<
-    INTEGER(iwp) ::  gradient_normals  !<
-    INTEGER(iwp) ::  i                 !<
-    INTEGER(iwp) ::  j                 !<
-    INTEGER(iwp) ::  k                 !<
-    INTEGER(iwp) ::  l                 !<
-    INTEGER(iwp) ::  m                 !<
-    INTEGER(iwp) ::  nx_dvrp_l         !<
-    INTEGER(iwp) ::  nx_dvrp_r         !<
-    INTEGER(iwp) ::  ny_dvrp_n         !<
-    INTEGER(iwp) ::  ny_dvrp_s         !<
-    INTEGER(iwp) ::  pn                !<
-    INTEGER(iwp) ::  tv                !<
-    INTEGER(iwp) ::  vn                !<
-                     
-    LOGICAL  ::  allocated  !<
-    
-    REAL(sp) ::  center(3)      !<
-    REAL(sp) ::  cluster_alpha  !<
-    REAL(sp) ::  distance       !<
-    REAL(sp) ::  tmp_b          !<
-    REAL(sp) ::  tmp_g          !<
-    REAL(sp) ::  tmp_r          !<
-    REAL(sp) ::  tmp_t          !<
-    REAL(sp) ::  tmp_th         !<
-    REAL(sp) ::  tmp_thr        !<
-    REAL(sp) ::  tmp_x1         !<
-    REAL(sp) ::  tmp_x2         !<
-    REAL(sp) ::  tmp_y1         !<
-    REAL(sp) ::  tmp_y2         !<
-    REAL(sp) ::  tmp_z1         !<
-    REAL(sp) ::  tmp_z2         !<
-    REAL(sp) ::  tmp_1          !<
-    REAL(sp) ::  tmp_2          !<
-    REAL(sp) ::  tmp_3          !<
-    REAL(sp) ::  tmp_4          !<
-    REAL(sp) ::  tmp_5          !<
-    REAL(sp) ::  tmp_6          !<
-    REAL(sp) ::  tmp_7          !<
-
-    REAL(sp), DIMENSION(:,:,:), ALLOCATABLE ::  local_pf  !<
-
-    TYPE(CSTRING), SAVE ::  dvrp_directory_c   !<
-    TYPE(CSTRING), SAVE ::  dvrp_file_c        !<
-    TYPE(CSTRING), SAVE ::  dvrp_file_local_c  !<
-    TYPE(CSTRING), SAVE ::  dvrp_host_c        !<
-    TYPE(CSTRING), SAVE ::  dvrp_password_c    !<
-    TYPE(CSTRING), SAVE ::  dvrp_username_c    !<
-    TYPE(CSTRING), SAVE ::  name_c             !<
-
-!
-!-- Set clipping to default (total domain), if not set by user
-    IF ( clip_dvrp_l == 9999999.9_wp )  clip_dvrp_l = 0.0_wp
-    IF ( clip_dvrp_r == 9999999.9_wp )  clip_dvrp_r = ( nx + 1 ) * dx
-    IF ( clip_dvrp_s == 9999999.9_wp )  clip_dvrp_s = 0.0_wp
-    IF ( clip_dvrp_n == 9999999.9_wp )  clip_dvrp_n = ( ny + 1 ) * dy
-
-!
-!-- Calculate the clipping index limits
-    nx_dvrp_l = clip_dvrp_l / dx
-    nx_dvrp_r = clip_dvrp_r / dx
-    ny_dvrp_s = clip_dvrp_s / dy
-    ny_dvrp_n = clip_dvrp_n / dy
-
-    IF ( nx_dvrp_l < nxr  .AND.  nx_dvrp_r > nxl  .AND. &
-         ny_dvrp_s < nyn  .AND.  ny_dvrp_n > nys )  THEN
-
-       dvrp_overlap = .TRUE.
-       nxl_dvrp = MAX( nxl, nx_dvrp_l )
-       nxr_dvrp = MIN( nxr, nx_dvrp_r )
-       nys_dvrp = MAX( nys, ny_dvrp_s )
-       nyn_dvrp = MIN( nyn, ny_dvrp_n )
-
-       IF ( nxl_dvrp == nxl  .AND.  nxr_dvrp == nxr  .AND.  &
-            nys_dvrp == nys  .AND.  nyn_dvrp == nyn )  THEN
-          dvrp_total_overlap = .TRUE.
-       ELSE
-          dvrp_total_overlap = .FALSE.
-       ENDIF
-
-    ELSE
-!
-!--    This subdomain does not overlap with the clipping area. Define an
-!--    arbitrary (small) domain within in the clipping area.
-       dvrp_overlap       = .FALSE.
-       dvrp_total_overlap = .FALSE.
-!       nxl_dvrp = nx_dvrp_l
-!       nxr_dvrp = nxl_dvrp + 4
-!       nys_dvrp = ny_dvrp_s
-!       nyn_dvrp = nys_dvrp + 4
-       nxl_dvrp = nxl
-       nxr_dvrp = MIN( nxl+4, nxr )
-       nys_dvrp = nys
-       nyn_dvrp = MIN( nys+4, nyn )
-
-    ENDIF
-
-!
-!-- Set the maximum time the program can be suspended on user request (by
-!-- dvrp steering). This variable is defined in module DVRP.
-    DVRP_MAX_SUSPEND_TIME = 7200
-
-!
-!-- Allocate array holding the names and limits of the steering variables
-!-- (must have the same number of elements as array mode_dvrp!)
-    ALLOCATE( steering_dvrp(10) )
-
-!
-!-- Check, if output parameters are given and/or allowed
-!-- and set default-values, where necessary
-    IF ( dvrp_username == ' ' )  THEN
-        message_string = 'dvrp_username is undefined'
-        CALL message( 'init_dvrp', 'PA0195', 1, 2, 0, 6, 0 )         
-    ENDIF
-
-    IF ( dvrp_output /= 'ftp'  .AND.  dvrp_output /= 'rtsp'  .AND. &
-         dvrp_output /= 'local' )  THEN
-       message_string = 'dvrp_output="' // TRIM( dvrp_output ) // &
-                        '" not allowed'
-       CALL message( 'init_dvrp', 'PA0196', 1, 2, 0, 6, 0 )
-    ENDIF
-
-    IF ( dvrp_directory == 'default' )  THEN
-       dvrp_directory = TRIM( dvrp_username ) // '/' // TRIM( run_identifier )
-    ENDIF
-
-!
-!-- A local dvrserver running always outputs on temporary directory DATA_DVR
-    IF ( local_dvrserver_running )  THEN
-       dvrp_directory = 'DATA_DVR'
-    ENDIF
-
-    IF ( dvrp_output /= 'local' )  THEN
-       IF ( dvrp_file /= 'default'  .AND.  dvrp_file /= '/dev/null' )  THEN
-          message_string = 'dvrp_file="' // TRIM( dvrp_file ) // '" not allowed'
-          CALL message( 'init_dvrp', 'PA0197', 1, 2, 0, 6, 0 )
-       ENDIF
-    ENDIF
-
-!
-!-- Strings are assigned to strings of special type which have a CHAR( 0 )
-!-- (C end-of-character symbol) at their end. This is needed when strings are
-!-- passed to C routines.
-    dvrp_directory_c = dvrp_directory
-    dvrp_file_c      = dvrp_file
-    dvrp_host_c      = dvrp_host
-    dvrp_password_c  = dvrp_password
-    dvrp_username_c  = dvrp_username
-
-!
-!-- Loop over all output modes choosed
-    m = 1
-    allocated = .FALSE.
-    DO WHILE ( mode_dvrp(m) /= ' ' )
-    
-!
-!--    Check, if mode is allowed
-       IF ( mode_dvrp(m)(1:10) /= 'isosurface'  .AND. &
-            mode_dvrp(m)(1:6)  /= 'slicer'      .AND. &
-            mode_dvrp(m)(1:9)  /= 'particles'   .AND. &
-            mode_dvrp(m)(1:9)  /= 'pathlines' )  THEN
-
-          message_string = 'mode_dvrp="' // TRIM( mode_dvrp(m) ) // &
-                           '" not allowed'
-          CALL message( 'init_dvrp', 'PA0198', 1, 2, 0, 6, 0 )
-          CALL local_stop
-
-       ENDIF
-!
-!--    Determine prefix for dvrp_file
-       WRITE ( prefix_chr, '(I2.2,''_'')' )  m
-!
-!--    Camera position must be computed and written on file when no dvrp-output
-!--    has been generated so far (in former runs)
-!       IF ( dvrp_filecount == 0 )  THEN
-!
-!--       Compute center of domain and distance of camera from center
-          center(1) = ( clip_dvrp_l + clip_dvrp_r ) * 0.5_wp * superelevation_x
-          center(2) = ( clip_dvrp_s + clip_dvrp_n ) * 0.5_wp * superelevation_y
-          center(3) = ( zu(nz_do3d) - zu(nzb) ) * 0.5_wp * superelevation
-          distance  = 1.5_wp * MAX(                                            &
-                           (clip_dvrp_r-clip_dvrp_l) * superelevation_x,       &
-                           (clip_dvrp_n-clip_dvrp_s) * superelevation_y,       &
-                           ( zu(nz_do3d) - zu(nzb) ) * superelevation          &
-                                  )
-
-!
-!--       Write camera position on file
-          CALL DVRP_INIT( m-1, 0 )
-
-!
-!--       Create filename for camera
-          IF ( dvrp_output == 'rtsp' )  THEN
-
-             dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) ) // '/camera.dvr'
-             dvrp_file_c = dvrp_file
-             CALL DVRP_OUTPUT_RTSP( m-1, dvrp_host_c, dvrp_username_c, &
-                                    dvrp_password_c, dvrp_directory_c, &
-                                    dvrp_file_c )
-
-          ELSEIF ( dvrp_output == 'ftp' )  THEN
-
-             dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) ) // '.camera.dvr'
-             dvrp_file_c = dvrp_file
-!             CALL DVRP_OUTPUT_FTP( m-1, 0, dvrp_host_c, dvrp_username_c, &
-!                                   dvrp_password_c, dvrp_directory_c,    &
-!                                   dvrp_file_c )
-
-          ELSE
-
-             IF ( dvrp_file(1:9) /= '/dev/null' )  THEN
-                dvrp_file_local   = prefix_chr // TRIM( mode_dvrp(m) )  &
-                     // '.camera.dvr'
-                dvrp_file_local_c = dvrp_file_local
-             ELSE
-                dvrp_file_local_c = dvrp_file_c
-             ENDIF
-             CALL DVRP_OUTPUT_LOCAL( m-1, 0, dvrp_file_local_c )
-
-          ENDIF
-
-          CALL DVRP_CAMERA( m-1, center, distance )
-
-!
-!--       Define bounding box material and create a bounding box
-          tmp_r = 0.5_wp;  tmp_g = 0.5_wp;  tmp_b = 0.5_wp;  tmp_t = 0.0_wp
-          CALL DVRP_MATERIAL_RGB( m-1, 1, tmp_r, tmp_g, tmp_b, tmp_t )
-
-          tmp_1 = 0.01_wp;
-          tmp_2 = clip_dvrp_l * superelevation_x
-          tmp_3 = clip_dvrp_s * superelevation_y
-          tmp_4 = 0.0_wp
-          tmp_5 = (clip_dvrp_r+dx) * superelevation_x
-          tmp_6 = (clip_dvrp_n+dy) * superelevation_y
-          tmp_7 = zu(nz_do3d) * superelevation
-          CALL DVRP_BOUNDINGBOX( m-1, 1, tmp_1, tmp_2, tmp_3, tmp_4, tmp_5, &
-                                 tmp_6, tmp_7 )
-
-          CALL DVRP_VISUALIZE( m-1, 0, 0 )
-          CALL DVRP_EXIT( m-1 )
-
-!
-!--       Write topography isosurface on file
-          IF ( TRIM( topography ) /= 'flat' )  THEN
-
-             CALL DVRP_INIT( m-1, 0 )
-
-!
-!--          Create filename for topography
-             IF ( dvrp_output == 'rtsp' )  THEN
-
-                dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) )  &
-                              // '/topography.dvr'
-                dvrp_file_c = dvrp_file
-                CALL DVRP_OUTPUT_RTSP( m-1, dvrp_host_c, dvrp_username_c, &
-                                       dvrp_password_c, dvrp_directory_c, &
-                                       dvrp_file_c )
-
-             ELSEIF ( dvrp_output == 'ftp' )  THEN
-
-                dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) )  &
-                              // '.topography.dvr'
-                dvrp_file_c = dvrp_file
-!                CALL DVRP_OUTPUT_FTP( m-1, 0, dvrp_host_c, dvrp_username_c, &
-!                                      dvrp_password_c, dvrp_directory_c,    &
-!                                      dvrp_file_c )
-
-             ELSE
-
-                IF ( dvrp_file(1:9) /= '/dev/null' )  THEN
-                   dvrp_file_local   = prefix_chr // TRIM( mode_dvrp(m) )  &
-                                       // '.topography.dvr'
-                   dvrp_file_local_c = dvrp_file_local
-                ELSE
-                   dvrp_file_local_c = dvrp_file_c
-                ENDIF
-                CALL DVRP_OUTPUT_LOCAL( m-1, 0, dvrp_file_local_c )
-
-             ENDIF
-
-!
-!--          Determine local gridpoint coordinates
-             IF ( .NOT. allocated )  THEN
-                ALLOCATE( xcoor_dvrp(nxl_dvrp:nxr_dvrp+1), &
-                          ycoor_dvrp(nys_dvrp:nyn_dvrp+1), &
-                          zcoor_dvrp(nzb:nz_do3d) )
-                allocated = .TRUE.
-
-                DO  i = nxl_dvrp, nxr_dvrp+1
-                   xcoor_dvrp(i) = i * dx * superelevation_x
-                ENDDO
-                DO  j = nys_dvrp, nyn_dvrp+1
-                   ycoor_dvrp(j) = j * dy * superelevation_y
-                ENDDO
-                zcoor_dvrp = zu(nzb:nz_do3d) * superelevation
-                nx_dvrp    = nxr_dvrp+1 - nxl_dvrp + 1
-                ny_dvrp    = nyn_dvrp+1 - nys_dvrp + 1
-                nz_dvrp    = nz_do3d - nzb + 1
-             ENDIF
-
-!
-!--          Define the grid used by dvrp
-             CALL DVRP_NO_GLOBAL_GRID( m-1, 1 )
-             CALL DVRP_GRID( m-1, nx_dvrp, ny_dvrp, nz_dvrp, xcoor_dvrp, &
-                             ycoor_dvrp, zcoor_dvrp )
-
-             tmp_r = topography_color(1)
-             tmp_g = topography_color(2)
-             tmp_b = topography_color(3)
-             tmp_t = 0.0_wp
-             CALL DVRP_MATERIAL_RGB( m-1, 1, tmp_r, tmp_g, tmp_b, tmp_t )
-
-!
-!--          Compute and plot isosurface in dvr-format
-             ALLOCATE( local_pf(nxl_dvrp:nxr_dvrp+1,nys_dvrp:nyn_dvrp+1, &
-                                nzb:nz_do3d) )
-             local_pf = 0.0_wp
-             IF ( dvrp_overlap )  THEN
-                DO  i = nxl_dvrp, nxr_dvrp+1
-                   DO  j = nys_dvrp, nyn_dvrp+1
-                      IF ( nzb_s_inner(j,i) > 0 )  THEN
-                         local_pf(i,j,nzb:nzb_s_inner(j,i)) = 1.0_wp
-                      ENDIF
-                   ENDDO
-                ENDDO
-             ENDIF
-
-             CALL DVRP_DATA( m-1, local_pf, 1, nx_dvrp, ny_dvrp, nz_dvrp, &
-                             cyclic_dvrp, cyclic_dvrp, cyclic_dvrp )
-
-             tmp_th = 1.0_wp
-             CALL DVRP_THRESHOLD( m-1, tmp_th )
-
-!
-!--          Reduce the number of polygones, if required
-             IF ( cluster_size > 1 )  THEN
-
-                cluster_size_x = cluster_size
-                cluster_size_y = cluster_size
-                cluster_size_z = cluster_size
-                cluster_mode     = 4    ! vertex clustering mode
-                gradient_normals = 0    ! use flat-shading
-
-                CALL DVRP_CLUSTER_SIZE( m-1, cluster_size_x, cluster_size_y, &
-                                        cluster_size_z )
-                CALL DVRP_CLUSTERING_MODE( m-1, cluster_mode )
-                CALL DVRP_GRADIENTNORMALS( m-1, gradient_normals )
-!
-!--             Set parameter for vertex clustering mode 4.
-!--             ATTENTION: A seperate procedure for setting cluster_alpha will
-!--                        be in the next version of libDVRP (Feb 09)
-                cluster_alpha = 38.0_wp
-                CALL DVRP_THRESHOLD( -(m-1)-1, cluster_alpha )
-
-                CALL DVRP_VISUALIZE( m-1, 21, 0 )
-
-             ELSE
-!
-!--             No polygon reduction
-                CALL DVRP_VISUALIZE( m-1, 1, 0 )
-
-             ENDIF
-
-             DEALLOCATE( local_pf )
-
-             CALL DVRP_EXIT( m-1 )
-
-          ENDIF
-
-!
-!--       Write the ground plate (z=0) isosurface on file
-          CALL DVRP_INIT( m-1, 0 )
-
-!
-!--       Create filename for surface
-          IF ( dvrp_output == 'rtsp' )  THEN
-
-             dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) ) // &
-                           '/groundplate.dvr'
-             dvrp_file_c = dvrp_file
-             CALL DVRP_OUTPUT_RTSP( m-1, dvrp_host_c, dvrp_username_c, &
-                                    dvrp_password_c, dvrp_directory_c, &
-                                    dvrp_file_c )
-
-          ELSEIF ( dvrp_output == 'ftp' )  THEN
-
-             dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) ) // &
-                           '.groundplate.dvr'
-             dvrp_file_c = dvrp_file
-!             CALL DVRP_OUTPUT_FTP( m-1, 0, dvrp_host_c, dvrp_username_c, &
-!                                   dvrp_password_c, dvrp_directory_c,    &
-!                                   dvrp_file_c )
-
-          ELSE
-
-             IF ( dvrp_file(1:9) /= '/dev/null' )  THEN
-                dvrp_file_local   = prefix_chr // TRIM( mode_dvrp(m) )  &
-                     // '.groundplate.dvr'
-                dvrp_file_local_c = dvrp_file_local
-             ELSE
-                dvrp_file_local_c = dvrp_file_c
-             ENDIF
-             CALL DVRP_OUTPUT_LOCAL( m-1, 0, dvrp_file_local_c )
-
-          ENDIF
-
-!
-!--       Determine local gridpoint coordinates
-          IF ( .NOT. allocated )  THEN
-             ALLOCATE( xcoor_dvrp(nxl_dvrp:nxr_dvrp+1), &
-                       ycoor_dvrp(nys_dvrp:nyn_dvrp+1), &
-                       zcoor_dvrp(nzb:nz_do3d) )
-             allocated = .TRUE.
-
-             DO  i = nxl_dvrp, nxr_dvrp+1
-                xcoor_dvrp(i) = i * dx * superelevation_x
-             ENDDO
-             DO  j = nys_dvrp, nyn_dvrp+1
-                ycoor_dvrp(j) = j * dy * superelevation_y
-             ENDDO
-             zcoor_dvrp = zu(nzb:nz_do3d) * superelevation
-             nx_dvrp    = nxr_dvrp+1 - nxl_dvrp + 1
-             ny_dvrp    = nyn_dvrp+1 - nys_dvrp + 1
-             nz_dvrp    = nz_do3d - nzb + 1
-          ENDIF
-
-!
-!--       Define the grid used by dvrp
-          CALL DVRP_NO_GLOBAL_GRID( m-1, 1 )
-          CALL DVRP_GRID( m-1, nx_dvrp, ny_dvrp, nz_dvrp, xcoor_dvrp, &
-                          ycoor_dvrp, zcoor_dvrp )
-
-          tmp_r = groundplate_color(1)
-          tmp_g = groundplate_color(2)
-          tmp_b = groundplate_color(3)
-          tmp_t = 0.0_wp
-          CALL DVRP_MATERIAL_RGB( m-1, 1, tmp_r, tmp_g, tmp_b, tmp_t )
-
-!
-!--       Compute and plot isosurface in dvr-format
-          ALLOCATE( local_pf(nxl_dvrp:nxr_dvrp+1,nys_dvrp:nyn_dvrp+1, &
-                             nzb:nz_do3d) )
-          local_pf = 0.0_wp
-          IF (dvrp_overlap )  local_pf(:,:,0) = 1.0_wp
-
-          CALL DVRP_DATA( m-1, local_pf, 1, nx_dvrp, ny_dvrp, nz_dvrp, &
-                          cyclic_dvrp, cyclic_dvrp, cyclic_dvrp )
-          tmp_th = 1.0_wp
-          CALL DVRP_THRESHOLD( m-1, tmp_th )
-
-!
-!--       Always reduce the number of polygones as much as possible
-          cluster_size_x = 5
-          cluster_size_y = 5
-          cluster_size_z = 5
-          cluster_mode     = 4    ! vertex clustering mode
-          gradient_normals = 0    ! use flat-shading
-
-          CALL DVRP_CLUSTER_SIZE( m-1, cluster_size_x, cluster_size_y, &
-                                  cluster_size_z )
-          CALL DVRP_CLUSTERING_MODE( m-1, cluster_mode )
-          CALL DVRP_GRADIENTNORMALS( m-1, gradient_normals )
-!
-!--       Set parameter for vertex clustering mode 4.
-!--       ATTENTION: A seperate procedure for setting cluster_alpha will be in
-!--                  the next version of libDVRP (Feb 09)
-          cluster_alpha = 38.0_wp
-          CALL DVRP_THRESHOLD( -(m-1)-1, cluster_alpha )
-
-          CALL DVRP_VISUALIZE( m-1, 21, 0 )
-
-          DEALLOCATE( local_pf )
-
-          CALL DVRP_EXIT( m-1 )
-    
-!       ENDIF
-
-
-!
-!--    Initialize dvrp for all dvrp-calls during the run
-       CALL DVRP_INIT( m-1, 0 )
-
-!
-!--    Preliminary definition of filename for dvrp-output
-       IF ( dvrp_output == 'rtsp' )  THEN
-
-!
-!--       First initialize parameters for possible interactive steering.
-!--       Every parameter has to be passed to the respective stream.
-          pn = 1
-!
-!--       Initialize threshold counter needed for initialization of the
-!--       isosurface steering variables
-          tv = 0
-
-          DO WHILE ( mode_dvrp(pn) /= ' ' )
-
-             IF ( mode_dvrp(pn)(1:10) == 'isosurface' )  THEN
-
-                READ ( mode_dvrp(pn), '(10X,I2)' )  vn
-                steering_dvrp(pn)%name = do3d(0,vn)
-                tv = tv + 1
-
-                IF ( do3d(0,vn)(1:1) == 'w' )  THEN
-                   steering_dvrp(pn)%min  = -4.0_wp
-                   steering_dvrp(pn)%max  =  5.0_wp
-                ELSE
-                   steering_dvrp(pn)%min  = 288.0_wp
-                   steering_dvrp(pn)%max  = 292.0_wp
-                ENDIF
-
-                name_c  = TRIM( do3d(0,vn) )
-                tmp_thr = threshold(tv)
-                CALL DVRP_STEERING_INIT( m-1, name_c, steering_dvrp(pn)%min, &
-                                         steering_dvrp(pn)%max, tmp_thr )
-
-             ELSEIF ( mode_dvrp(pn)(1:6) == 'slicer' )  THEN
-
-                READ ( mode_dvrp(pn), '(6X,I2)' )  vn
-                steering_dvrp(pn)%name = do2d(0,vn)
-                name_c = TRIM( do2d(0,vn) )
-
-                l = MAX( 2, LEN_TRIM( do2d(0,vn) ) )
-                section_chr = do2d(0,vn)(l-1:l)
-                SELECT CASE ( section_chr )
-                   CASE ( 'xy' )
-                      steering_dvrp(pn)%imin   = 0
-                      steering_dvrp(pn)%imax   = nz_do3d
-                      slicer_position_dvrp(pn) = section(1,1)
-                      CALL DVRP_STEERING_INIT( m-1, name_c,            &
-                                               steering_dvrp(pn)%imin, &
-                                               steering_dvrp(pn)%imax, &
-                                               slicer_position_dvrp(pn) )
-                   CASE ( 'xz' )
-                      steering_dvrp(pn)%imin   = 0
-                      steering_dvrp(pn)%imax   = ny
-                      slicer_position_dvrp(pn) = section(1,2)
-                      CALL DVRP_STEERING_INIT( m-1, name_c,            &
-                                               steering_dvrp(pn)%imin, &
-                                               steering_dvrp(pn)%imax, &
-                                               slicer_position_dvrp(pn) )
-                   CASE ( 'yz' )
-                      steering_dvrp(pn)%imin = 0
-                      steering_dvrp(pn)%imax = nx
-                      slicer_position_dvrp(pn) = section(1,3)
-                      CALL DVRP_STEERING_INIT( m-1, name_c,            &
-                                               steering_dvrp(pn)%imin, &
-                                               steering_dvrp(pn)%imax, &
-                                               slicer_position_dvrp(pn) )
-                END SELECT
-
-             ENDIF
-
-             pn = pn + 1
-
-          ENDDO
-
-          dvrp_file = prefix_chr // TRIM( mode_dvrp(m) ) // '/*****.dvr'
-          dvrp_file_c = dvrp_file
-          CALL DVRP_OUTPUT_RTSP( m-1, dvrp_host_c, dvrp_username_c, &
-                                 dvrp_password_c, dvrp_directory_c, &
-                                 dvrp_file_c )
-
-       ELSEIF ( dvrp_output == 'ftp' )  THEN
-
-          dvrp_file   = prefix_chr // TRIM( mode_dvrp(m) ) // '.%05d.dvr'
-          dvrp_file_c = dvrp_file
-!          CALL DVRP_OUTPUT_FTP( m-1, 0, dvrp_host_c, dvrp_username_c, &
-!                                dvrp_password_c, dvrp_directory_c, dvrp_file_c )
-
-       ELSE
-
-          IF ( dvrp_file(1:9) /= '/dev/null' )  THEN
-             dvrp_file_local   = prefix_chr // TRIM( mode_dvrp(m) )  &
-                  // '_%05d.dvr'
-             dvrp_file_local_c = dvrp_file_local
-          ELSE
-             dvrp_file_local_c = dvrp_file_c
-          ENDIF
-          CALL DVRP_OUTPUT_LOCAL( m-1, 0, dvrp_file_local_c )
-
-       ENDIF
-
-!
-!--    Determine local gridpoint coordinates
-       IF ( .NOT. allocated )  THEN
-          ALLOCATE( xcoor_dvrp(nxl_dvrp:nxr_dvrp+1), &
-                    ycoor_dvrp(nys_dvrp:nyn_dvrp+1), &
-                    zcoor_dvrp(nzb:nz_do3d) )
-          allocated = .TRUE.
-
-          DO  i = nxl_dvrp, nxr_dvrp+1
-             xcoor_dvrp(i) = i * dx * superelevation_x
-          ENDDO
-          DO  j = nys_dvrp, nyn_dvrp+1
-             ycoor_dvrp(j) = j * dy * superelevation_y
-          ENDDO
-          zcoor_dvrp = zu(nzb:nz_do3d) * superelevation
-          nx_dvrp    = nxr_dvrp+1 - nxl_dvrp + 1
-          ny_dvrp    = nyn_dvrp+1 - nys_dvrp + 1
-          nz_dvrp    = nz_do3d - nzb + 1
-       ENDIF
-
-!
-!--    Define the grid used by dvrp
-       IF ( mode_dvrp(m) /= 'pathlines' )  THEN
-          CALL DVRP_NO_GLOBAL_GRID( m-1, 1 )
-       ENDIF
-       CALL DVRP_GRID( m-1, nx_dvrp, ny_dvrp, nz_dvrp, xcoor_dvrp, ycoor_dvrp, &
-                       zcoor_dvrp )
-
-       IF ( mode_dvrp(m) == 'pathlines' )  THEN
-
-          tmp_x1 = 0.0_wp;  tmp_y1 = 0.0_wp;  tmp_z1 = 0.0_wp
-          tmp_x2 = 1.0_wp;  tmp_y2 = 1.0_wp;  tmp_z2 = 0.3_wp
-          CALL DVRP_CUBIC_SEEDING( m-1, tmp_x1, tmp_y1, tmp_z1, tmp_x2, tmp_y2,&
-                                   tmp_z2, pathlines_linecount, 2, 0 )
-!
-!--       Set wavecount and wavetime
-          CALL DVRP_PATHLINES_BEHAVIOUR_WAVE( m-1, pathlines_wavecount, &
-                                              pathlines_wavetime,       &
-                                              pathlines_fadeintime,     &
-                                              pathlines_fadeouttime )
-!
-!--       Set pathline length
-          CALL DVRP_PATHLINES_SETMAXHISTORY( m-1, pathlines_maxhistory )
-          CALL DVRP_PATHLINES_SETFADING( m-1, 1, 0.0_wp )
-
-          CALL DVRP_INIT_PATHLINES( m-1, 0 )
-
-       ENDIF
-
-       IF ( mode_dvrp(m)(1:9) == 'particles' )  THEN
-!
-!--       Define a default colourtable for particles
-          DO  i = 1, 11
-             interval_values_dvrp_prt(1,i) = i - 1.0_wp
-             interval_values_dvrp_prt(2,i) = REAL( i, KIND=wp )
-             interval_h_dvrp_prt(:,i) = 270.0_wp - ( i - 1.0_wp ) * 9.0_wp
-          ENDDO
-
-          DO  i = 12, 22
-             interval_values_dvrp_prt(1,i) = i - 1.0_wp
-             interval_values_dvrp_prt(2,i) = REAL( i, KIND=wp )
-             interval_h_dvrp_prt(:,i) = 70.0_wp - ( i - 12.0_wp ) * 9.5_wp
-          ENDDO
-
-          dvrp_colortable_entries_prt = 22
-
-       ENDIF
-
-       m = m + 1
-
-    ENDDO
-
-#endif
- END SUBROUTINE init_dvrp
-
- 
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Initializes logging events for time measurement with dvrp software
-!> and splits one PE from the global communicator in case that dvrp output
-!> shall be done by one single PE.
-!------------------------------------------------------------------------------!
- 
- SUBROUTINE init_dvrp_logging
-
-#if defined( __dvrp_graphics )
-
-    USE dvrp_variables,                                                        &
-        ONLY:  use_seperate_pe_for_dvrp_output
-    
-    USE kinds
-    
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=4) ::  chr  !<
-    
-    INTEGER(iwp) ::  idummy    !<
-
-!
-!-- Initialize logging of calls by DVRP graphic software
-    CALL DVRP_LOG_INIT( 'DVRP_LOG' // CHAR( 0 ), 0 )
-
-!
-!-- User-defined logging events: #1 (total time needed by PALM)
-    CALL DVRP_LOG_SYMBOL( 1, 'PALM_total' // CHAR( 0 ) )
-    CALL DVRP_LOG_SYMBOL( 2, 'PALM_timestep' // CHAR( 0 ) )
-    CALL DVRP_LOG_EVENT( 1, 1 )
-
-#if defined( __parallel )
-!
-!-- Find out, if dvrp output shall be done by a dedicated PE
-    CALL GET_ENVIRONMENT_VARIABLE( 'use_seperate_pe_for_dvrp_output', chr,     &
-                                   idummy )
-    IF ( chr == 'true' )  THEN
-
-       use_seperate_pe_for_dvrp_output = .TRUE.
-
-!
-!--    Adjustment for new MPI-1 coupling. This might be unnecessary.
-       IF ( coupling_mode /= 'uncoupled' ) THEN
-          message_string = 'split of communicator not realized with' // &
-                          ' MPI1 coupling atmosphere-ocean'
-          CALL message( 'init_dvrp_logging', 'PA0199', 1, 2, 0, 6, 0 )
- 
-          CALL DVRP_SPLIT( comm_inter, comm_palm )
-       ELSE
-          CALL DVRP_SPLIT( MPI_COMM_WORLD, comm_palm )
-       ENDIF
-
-       CALL MPI_COMM_SIZE( comm_palm, numprocs, ierr )
-
-    ENDIF
-#endif
-
-#endif
- END SUBROUTINE init_dvrp_logging
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Exit of dvrp software and finish dvrp logging
-!------------------------------------------------------------------------------!
- 
- SUBROUTINE close_dvrp
-
-#if defined( __dvrp_graphics )
-                                               
-    USE DVRP
-    
-    USE dvrp_variables,                                                        &
-        ONLY: use_seperate_pe_for_dvrp_output
-    
-    USE kinds
-
-    INTEGER(iwp) ::  m  !<
-
-!
-!-- If required, close dvrp-software and logging of dvrp-calls
-    IF ( dt_dvrp /= 9999999.9_wp )  THEN
-       m = 1
-       DO WHILE ( mode_dvrp(m) /= ' ' )
-          CALL DVRP_EXIT( m-1 )
-          m = m + 1
-       ENDDO
-       CALL DVRP_LOG_EVENT( -1, 1 )   ! Logging of total cpu-time used by PALM
-       IF ( use_seperate_pe_for_dvrp_output )  THEN
-          CALL DVRP_SPLIT_EXIT( 1 )      ! Argument 0: reduced output
-       ELSE
-          CALL DVRP_LOG_EXIT( 1 )        ! Argument 0: reduced output
-       ENDIF
-    ENDIF
-
-#endif
- END SUBROUTINE close_dvrp
Index: palm/trunk/SOURCE/lagrangian_particle_model_mod.f90
===================================================================
--- palm/trunk/SOURCE/lagrangian_particle_model_mod.f90	(revision 4017)
+++ palm/trunk/SOURCE/lagrangian_particle_model_mod.f90	(revision 4017)
@@ -0,0 +1,7947 @@
+!> @file lagrangian_particle_model_mod.f90
+!------------------------------------------------------------------------------!
+! This file is part of the PALM model system.
+!
+! PALM is free software: you can redistribute it and/or modify it under the
+! terms of the GNU General Public License as published by the Free Software
+! Foundation, either version 3 of the License, or (at your option) any later
+! version.
+!
+! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
+! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+!
+! You should have received a copy of the GNU General Public License along with
+! PALM. If not, see <http://www.gnu.org/licenses/>.
+!
+! Copyright 1997-2019 Leibniz Universitaet Hannover
+!------------------------------------------------------------------------------!
+!
+! Current revisions:
+! ------------------
+! 
+! 
+! Former revisions:
+! -----------------
+! $Id$
+! Modularization of all lagrangian particle model code components
+! 
+! 3655 2019-01-07 16:51:22Z knoop 
+! bugfix to guarantee correct particle releases in case that the release
+! interval is smaller than the model timestep
+! 
+! 2801 2018-02-14 16:01:55Z thiele
+! Changed lpm from subroutine to module.
+! Introduce particle transfer in nested models.
+! 
+! 2718 2018-01-02 08:49:38Z maronga
+! Corrected "Former revisions" section
+! 
+! 2701 2017-12-15 15:40:50Z suehring
+! Changes from last commit documented
+! 
+! 2698 2017-12-14 18:46:24Z suehring
+! Grid indices passed to lpm_boundary_conds. (responsible Philipp Thiele)
+!
+! 2696 2017-12-14 17:12:51Z kanani
+! Change in file header (GPL part)
+!
+! 2606 2017-11-10 10:36:31Z schwenkel
+! Changed particle box locations: center of particle box now coincides 
+! with scalar grid point of same index.
+! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
+! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
+! lpm_sort -> lpm_sort_timeloop_done
+! 
+! 2418 2017-09-06 15:24:24Z suehring
+! Major bugfixes in modeling SGS particle speeds (since revision 1359).
+! Particle sorting added to distinguish between already completed and 
+! non-completed particles.
+! 
+! 2263 2017-06-08 14:59:01Z schwenkel
+! Implemented splitting and merging algorithm
+! 
+! 2233 2017-05-30 18:08:54Z suehring
+!
+! 2232 2017-05-30 17:47:52Z suehring
+! Adjustments to new topography concept
+! 
+! 2000 2016-08-20 18:09:15Z knoop
+! Forced header and separation lines into 80 columns
+! 
+! 1936 2016-06-13 13:37:44Z suehring
+! Call routine for deallocation of unused memory.
+! Formatting adjustments
+! 
+! 1929 2016-06-09 16:25:25Z suehring
+! Call wall boundary conditions only if particles are in the vertical range of 
+! topography.
+!
+! 1822 2016-04-07 07:49:42Z hoffmann
+! Tails removed.
+!
+! Initialization of sgs model not necessary for the use of cloud_droplets and
+! use_sgs_for_particles.
+!
+! lpm_release_set integrated.
+!
+! Unused variabled removed.
+!
+! 1682 2015-10-07 23:56:08Z knoop
+! Code annotations made doxygen readable 
+! 
+! 1416 2014-06-04 16:04:03Z suehring
+! user_lpm_advec is called for each gridpoint.
+! Bugfix: in order to prevent an infinite loop, time_loop_done is set .TRUE. 
+! at the head of the do-loop.  
+! 
+! 1359 2014-04-11 17:15:14Z hoffmann
+! New particle structure integrated. 
+! Kind definition added to all floating point numbers.
+!
+! 1320 2014-03-20 08:40:49Z raasch
+! ONLY-attribute added to USE-statements,
+! kind-parameters added to all INTEGER and REAL declaration statements,
+! kinds are defined in new module kinds,
+! revision history before 2012 removed,
+! comment fields (!:) to be used for variable explanations added to
+! all variable declaration statements
+!
+! 1318 2014-03-17 13:35:16Z raasch
+! module interfaces removed
+!
+! 1036 2012-10-22 13:43:42Z raasch
+! code put under GPL (PALM 3.9)
+!
+! 851 2012-03-15 14:32:58Z raasch
+! Bugfix: resetting of particle_mask and tail mask moved from routine
+! lpm_exchange_horiz to here (end of sub-timestep loop)
+!
+! 849 2012-03-15 10:35:09Z raasch
+! original routine advec_particles split into several subroutines and renamed
+! lpm
+!
+! 831 2012-02-22 00:29:39Z raasch
+! thermal_conductivity_l and diff_coeff_l now depend on temperature and
+! pressure
+!
+! 828 2012-02-21 12:00:36Z raasch
+! fast hall/wang kernels with fixed radius/dissipation classes added,
+! particle feature color renamed class, routine colker renamed
+! recalculate_kernel,
+! lower limit for droplet radius changed from 1E-7 to 1E-8
+!
+! Bugfix: transformation factor for dissipation changed from 1E5 to 1E4
+!
+! 825 2012-02-19 03:03:44Z raasch
+! droplet growth by condensation may include curvature and solution effects,
+! initialisation of temporary particle array for resorting removed,
+! particle attributes speed_x|y|z_sgs renamed rvar1|2|3,
+! module wang_kernel_mod renamed lpm_collision_kernels_mod,
+! wang_collision_kernel renamed wang_kernel
+!
+!
+! Revision 1.1  1999/11/25 16:16:06  raasch
+! Initial revision
+!
+!
+! Description:
+! ------------
+!> 
+!------------------------------------------------------------------------------!
+ MODULE lagrangian_particle_model_mod
+        
+    USE, INTRINSIC ::  ISO_C_BINDING
+        
+    USE arrays_3d,                                                             &
+        ONLY:  de_dx, de_dy, de_dz, dzw, zu, zw,  ql_c, ql_v, ql_vp, hyp,      &
+               pt, q, exner, ql, diss, e, u, v, w, km     
+ 
+    USE averaging,                                                             &
+        ONLY:  ql_c_av, pr_av, pc_av, ql_vp_av, ql_v_av
+        
+    USE basic_constants_and_equations_mod,                                     &
+        ONLY: molecular_weight_of_solute, molecular_weight_of_water, magnus,   &
+              pi, rd_d_rv, rho_l, r_v, rho_s, vanthoff, l_v, kappa, g                
+        
+    USE control_parameters,                                                    &
+        ONLY:  bc_dirichlet_l, bc_dirichlet_n, bc_dirichlet_r, bc_dirichlet_s, &
+               cloud_droplets, constant_flux_layer, current_timestep_number,   &
+               dt_3d, dt_3d_reached, humidity,                                 &
+               dt_3d_reached_l, dt_dopts, dz, initializing_actions,            &
+               message_string, molecular_viscosity, ocean_mode,                &
+               particle_maximum_age, iran,                                     & 
+               simulated_time, topography, dopts_time_count,                   &
+               time_since_reference_point, rho_surface, u_gtrans, v_gtrans
+
+    USE cpulog,                                                                &
+        ONLY:  cpu_log, log_point, log_point_s
+        
+    USE indices,                                                               &
+        ONLY:  nx, nxl, nxlg, nxrg, nxr, ny, nyn, nys, nyng, nysg, nz, nzb,    &
+               nzb_max, nzt, wall_flags_0,nbgp, ngp_2dh_outer               
+        
+    USE kinds
+           
+    USE pegrid
+    
+    USE particle_attributes
+    
+    USE pmc_particle_interface,                                                &
+        ONLY: pmcp_c_get_particle_from_parent, pmcp_p_fill_particle_win,       &
+              pmcp_c_send_particle_to_parent, pmcp_p_empty_particle_win,       &
+              pmcp_p_delete_particles_in_fine_grid_area, pmcp_g_init,          &
+              pmcp_g_print_number_of_particles
+
+    USE pmc_interface,                                                         &
+        ONLY: nested_run
+
+    USE grid_variables,                                                        &
+        ONLY:  ddx, dx, ddy, dy
+
+    USE netcdf_interface,                                                      &
+        ONLY:  netcdf_data_format, netcdf_deflate, dopts_num, id_set_pts,      &
+               id_var_dopts, id_var_time_pts, nc_stat,                         &
+               netcdf_handle_error
+             
+    USE random_function_mod,                                                   &
+        ONLY:  random_function
+                   
+    USE statistics,                                                            &
+        ONLY:  hom                    
+
+    USE surface_mod,                                                           &
+        ONLY:  get_topography_top_index_ji, surf_def_h, surf_lsm_h, surf_usm_h,&
+               bc_h
+        
+#if defined( __parallel )  &&  !defined( __mpifh )
+    USE MPI
+#endif
+
+#if defined( __parallel )  &&  defined( __mpifh )
+    INCLUDE "mpif.h"
+#endif      
+
+#if defined( __netcdf )
+    USE NETCDF
+#endif
+
+    IMPLICIT NONE
+    
+    CHARACTER(LEN=15) ::  aero_species = 'nacl'                    !< aerosol species
+    CHARACTER(LEN=15) ::  aero_type    = 'maritime'                !< aerosol type
+    CHARACTER(LEN=15) ::  bc_par_lr    = 'cyclic'                  !< left/right boundary condition
+    CHARACTER(LEN=15) ::  bc_par_ns    = 'cyclic'                  !< north/south boundary condition
+    CHARACTER(LEN=15) ::  bc_par_b     = 'reflect'                 !< bottom boundary condition
+    CHARACTER(LEN=15) ::  bc_par_t     = 'absorb'                  !< top boundary condition
+    CHARACTER(LEN=15) ::  collision_kernel   = 'none'              !< collision kernel    
+    
+    CHARACTER(LEN=5)  ::  splitting_function = 'gamma'             !< function for calculation critical weighting factor
+    CHARACTER(LEN=5)  ::  splitting_mode     = 'const'             !< splitting mode
+
+    INTEGER(iwp) ::  deleted_particles = 0                        !< number of deleted particles per time step    
+    INTEGER(iwp) ::  i_splitting_mode                             !< dummy for splitting mode
+    INTEGER(iwp) ::  iran_part = -1234567                         !< number for random generator    
+    INTEGER(iwp) ::  max_number_particles_per_gridbox = 100       !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  isf                                          !< dummy for splitting function
+    INTEGER(iwp) ::  number_particles_per_gridbox = -1            !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  number_of_sublayers = 20                     !< number of sublayers for particle velocities betwenn surface and first grid level
+    INTEGER(iwp) ::  offset_ocean_nzt = 0                         !< in case of oceans runs, the vertical index calculations need an offset
+    INTEGER(iwp) ::  offset_ocean_nzt_m1 = 0                      !< in case of oceans runs, the vertical index calculations need an offset
+    INTEGER(iwp) ::  particles_per_point = 1                      !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  radius_classes = 20                          !< namelist parameter (see documentation)
+    
+    INTEGER(iwp) ::  splitting_factor = 2                         !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  splitting_factor_max = 5                     !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  step_dealloc = 100                           !< namelist parameter (see documentation)
+    INTEGER(iwp) ::  total_number_of_particles                    !< total number of particles in the whole model domain
+    INTEGER(iwp) ::  trlp_count_sum                               !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trlp_count_recv_sum                          !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trrp_count_sum                               !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trrp_count_recv_sum                          !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trsp_count_sum                               !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trsp_count_recv_sum                          !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trnp_count_sum                               !< parameter for particle exchange of PEs
+    INTEGER(iwp) ::  trnp_count_recv_sum                          !< parameter for particle exchange of PEs   
+    
+    LOGICAL ::  lagrangian_particle_model = .FALSE.       !< namelist parameter (see documentation) 
+    LOGICAL ::  curvature_solution_effects = .FALSE.      !< namelist parameter (see documentation)
+    LOGICAL ::  deallocate_memory = .TRUE.                !< namelist parameter (see documentation)
+    LOGICAL ::  hall_kernel = .FALSE.                     !< flag for collision kernel
+    LOGICAL ::  merging = .FALSE.                         !< namelist parameter (see documentation)
+    LOGICAL ::  random_start_position = .FALSE.           !< namelist parameter (see documentation)
+    LOGICAL ::  read_particles_from_restartfile = .TRUE.  !< namelist parameter (see documentation)
+    LOGICAL ::  seed_follows_topography = .FALSE.         !< namelist parameter (see documentation)
+    LOGICAL ::  splitting = .FALSE.                       !< namelist parameter (see documentation)
+    LOGICAL ::  use_kernel_tables = .FALSE.               !< parameter, which turns on the use of precalculated collision kernels
+    LOGICAL ::  write_particle_statistics = .FALSE.       !< namelist parameter (see documentation)
+    
+    LOGICAL, DIMENSION(max_number_of_particle_groups) ::   vertical_particle_advection = .TRUE. !< Switch for vertical particle transport
+    
+    REAL(wp) ::  aero_weight = 1.0_wp                      !< namelist parameter (see documentation)
+    REAL(wp) ::  dt_min_part = 0.0002_wp                   !< minimum particle time step when SGS velocities are used (s)
+    REAL(wp) ::  dt_prel = 9999999.9_wp                    !< namelist parameter (see documentation)
+    REAL(wp) ::  dt_write_particle_data = 9999999.9_wp     !< namelist parameter (see documentation)
+    REAL(wp) ::  end_time_prel = 9999999.9_wp              !< namelist parameter (see documentation)
+    REAL(wp) ::  initial_weighting_factor = 1.0_wp         !< namelist parameter (see documentation)
+    REAL(wp) ::  last_particle_release_time = 0.0_wp       !< last time of particle release
+    REAL(wp) ::  log_sigma(3) = 1.0_wp                     !< namelist parameter (see documentation)
+    REAL(wp) ::  na(3) = 0.0_wp                            !< namelist parameter (see documentation)
+    REAL(wp) ::  number_concentration = -1.0_wp            !< namelist parameter (see documentation)
+    REAL(wp) ::  radius_merge = 1.0E-7_wp                  !< namelist parameter (see documentation)
+    REAL(wp) ::  radius_split = 40.0E-6_wp                 !< namelist parameter (see documentation)
+    REAL(wp) ::  rm(3) = 1.0E-6_wp                         !< namelist parameter (see documentation)
+    REAL(wp) ::  sgs_wf_part                               !< parameter for sgs
+    REAL(wp) ::  time_write_particle_data = 0.0_wp         !< write particle data at current time on file
+    REAL(wp) ::  weight_factor_merge = -1.0_wp             !< namelist parameter (see documentation)
+    REAL(wp) ::  weight_factor_split = -1.0_wp             !< namelist parameter (see documentation)
+    REAL(wp) ::  z0_av_global                              !< horizontal mean value of z0
+    
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  density_ratio = 9999999.9_wp  !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdx = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdy = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdz = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psb = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psl = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psn = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psr = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pss = 9999999.9_wp            !< namelist parameter (see documentation)
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pst = 9999999.9_wp            !< namelist parameter (see documentation).
+    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  radius = 9999999.9_wp         !< namelist parameter (see documentation)
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE     ::  log_z_z0   !< Precalculate LOG(z/z0)  
+    
+    
+    
+    
+    INTEGER(iwp) :: ip
+    INTEGER(iwp) :: jp
+    INTEGER(iwp) :: kp
+    
+    INTEGER(iwp), PARAMETER         :: PHASE_INIT    = 1  !<
+    INTEGER(iwp), PARAMETER, PUBLIC :: PHASE_RELEASE = 2  !<
+
+    SAVE
+    
+    PRIVATE
+    
+    PUBLIC lpm_parin,     &
+           lpm_header,    &
+           lpm_init,      &
+           lpm_actions,   &
+           lpm_data_output_ptseries, &           
+           lpm_rrd_local_particles, &
+           lpm_wrd_local, &
+           lpm_rrd_global, &
+           lpm_wrd_global, &
+           lpm_rrd_local, &
+           lpm_check_parameters
+!            lpm_check_data_output
+
+    PUBLIC lagrangian_particle_model, & 
+           ip, &
+           jp, &                       
+           kp, &
+           max_number_particles_per_gridbox, &
+           radius_merge, &
+           radius_split, &
+           splitting_factor, &
+           splitting_factor_max, &
+           weight_factor_merge, &
+           weight_factor_split
+           
+
+    INTERFACE lpm_check_parameters
+       MODULE PROCEDURE lpm_check_parameters
+    END INTERFACE lpm_check_parameters
+!     
+!     INTERFACE lpm_check_data_output
+!        MODULE PROCEDURE lpm_check_data_output
+!     END INTERFACE lpm_check_data_output    
+
+    INTERFACE lpm_parin
+       MODULE PROCEDURE lpm_parin
+    END INTERFACE lpm_parin    
+    
+    INTERFACE lpm_header
+       MODULE PROCEDURE lpm_header
+    END INTERFACE lpm_header
+    
+    INTERFACE lpm_init
+       MODULE PROCEDURE lpm_init
+    END INTERFACE lpm_init        
+    
+    INTERFACE lpm_actions
+       MODULE PROCEDURE lpm_actions
+    END INTERFACE lpm_actions
+    
+    INTERFACE lpm_data_output_ptseries
+       MODULE PROCEDURE lpm_data_output_ptseries
+    END INTERFACE
+        
+    INTERFACE lpm_rrd_local_particles
+       MODULE PROCEDURE lpm_rrd_local_particles
+    END INTERFACE lpm_rrd_local_particles    
+        
+    INTERFACE lpm_rrd_global
+       MODULE PROCEDURE lpm_rrd_global
+    END INTERFACE lpm_rrd_global
+    
+    INTERFACE lpm_rrd_local
+       MODULE PROCEDURE lpm_rrd_local
+    END INTERFACE lpm_rrd_local        
+
+    INTERFACE lpm_wrd_local
+       MODULE PROCEDURE lpm_wrd_local
+    END INTERFACE lpm_wrd_local
+    
+    INTERFACE lpm_wrd_global
+       MODULE PROCEDURE lpm_wrd_global
+    END INTERFACE lpm_wrd_global    
+    
+    INTERFACE  
+       MODULE SUBROUTINE test_sub
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_advec(ip,jp,kp)
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_calc_liquid_water_content
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_boundary_conds(location_bc, i,j,k)
+           CHARACTER (LEN=*), INTENT(IN) ::  location_bc !< case statement for boundary conditions
+            INTEGER(iwp), INTENT(IN) ::  i !< grid index of particle box along x
+            INTEGER(iwp), INTENT(IN) ::  j !< grid index of particle box along y
+            INTEGER(iwp), INTENT(IN) ::  k !< grid index of particle box along z
+
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_droplet_condensation(i,j,k)
+            INTEGER(iwp), INTENT(IN) ::  i !< grid index of particle box along x
+            INTEGER(iwp), INTENT(IN) ::  j !< grid index of particle box along y
+            INTEGER(iwp), INTENT(IN) ::  k !< grid index of particle box along z
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_droplet_collision(i,j,k)
+            INTEGER(iwp), INTENT(IN) ::  i !< grid index of particle box along x
+            INTEGER(iwp), INTENT(IN) ::  j !< grid index of particle box along y
+            INTEGER(iwp), INTENT(IN) ::  k !< grid index of particle box along z
+       END SUBROUTINE
+    END INTERFACE 
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_init_kernels
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_splitting
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_merging
+       END SUBROUTINE
+    END INTERFACE    
+    
+    INTERFACE lpm_exchange_horiz
+       MODULE SUBROUTINE lpm_exchange_horiz
+       END SUBROUTINE
+    END INTERFACE lpm_exchange_horiz
+
+    INTERFACE lpm_move_particle
+       MODULE SUBROUTINE lpm_move_particle
+       END SUBROUTINE
+    END INTERFACE lpm_move_particle
+
+    INTERFACE realloc_particles_array
+       MODULE SUBROUTINE realloc_particles_array(i,j,k,size_in)
+          INTEGER(iwp), INTENT(IN) ::  i !< grid index of particle box along x
+          INTEGER(iwp), INTENT(IN) ::  j !< grid index of particle box along y
+          INTEGER(iwp), INTENT(IN) ::  k !< grid index of particle box along z
+          INTEGER(iwp), INTENT(IN), OPTIONAL ::  size_in !< grid index of particle box along z
+        END SUBROUTINE  
+    END INTERFACE realloc_particles_array
+
+    INTERFACE dealloc_particles_array
+       MODULE SUBROUTINE dealloc_particles_array
+       END SUBROUTINE
+    END INTERFACE dealloc_particles_array
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_sort_in_subboxes
+       END SUBROUTINE
+    END INTERFACE
+
+    INTERFACE  
+       MODULE SUBROUTINE lpm_sort_timeloop_done
+       END SUBROUTINE
+    END INTERFACE
+    
+    INTERFACE  
+       MODULE SUBROUTINE lpm_pack
+       END SUBROUTINE
+    END INTERFACE
+    
+    
+    
+ CONTAINS
+ 
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Parin for &particle_parameters for the Lagrangian particle model
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_parin
+ 
+    CHARACTER (LEN=80) ::  line  !<
+    
+    NAMELIST /particles_par/ &
+       aero_species, &
+       aero_type, &
+       aero_weight, &       
+       alloc_factor, &
+       bc_par_b, &
+       bc_par_lr, &
+       bc_par_ns, &
+       bc_par_t, &
+       collision_kernel, &
+       curvature_solution_effects, &
+       deallocate_memory, &
+       density_ratio, &
+       dissipation_classes, &
+       dt_dopts, &
+       dt_min_part, &
+       dt_prel, &
+       dt_write_particle_data, &
+       end_time_prel, &
+       initial_weighting_factor, &
+       log_sigma, &
+       max_number_particles_per_gridbox, &
+       merging, &
+       min_nr_particle, &
+       na, &
+       number_concentration, &
+       number_of_particle_groups, &
+       number_particles_per_gridbox, &
+       particles_per_point, &
+       particle_advection_start, &
+       particle_maximum_age, &
+       pdx, &
+       pdy, &
+       pdz, &
+       psb, &
+       psl, &
+       psn, &
+       psr, &
+       pss, &
+       pst, &
+       radius, &
+       radius_classes, &
+       radius_merge, &
+       radius_split, &
+       random_start_position, &
+       read_particles_from_restartfile, &
+       rm, &
+       seed_follows_topography, &
+       splitting, &
+       splitting_factor, &
+       splitting_factor_max, &
+       splitting_function, &
+       splitting_mode, &
+       step_dealloc, &
+       use_sgs_for_particles, &
+       vertical_particle_advection, &
+       weight_factor_merge, &
+       weight_factor_split, &
+       write_particle_statistics
+                                  
+    NAMELIST /particle_parameters/ &
+       aero_species, &
+       aero_type, &
+       aero_weight, &       
+       alloc_factor, &
+       bc_par_b, &
+       bc_par_lr, &
+       bc_par_ns, &
+       bc_par_t, &
+       collision_kernel, &
+       curvature_solution_effects, &
+       deallocate_memory, &
+       density_ratio, &
+       dissipation_classes, &
+       dt_dopts, &
+       dt_min_part, &
+       dt_prel, &
+       dt_write_particle_data, &
+       end_time_prel, &
+       initial_weighting_factor, &
+       log_sigma, &
+       max_number_particles_per_gridbox, &
+       merging, &
+       min_nr_particle, &
+       na, &
+       number_concentration, &
+       number_of_particle_groups, &
+       number_particles_per_gridbox, &
+       particles_per_point, &
+       particle_advection_start, &
+       particle_maximum_age, &
+       pdx, &
+       pdy, &
+       pdz, &
+       psb, &
+       psl, &
+       psn, &
+       psr, &
+       pss, &
+       pst, &
+       radius, &
+       radius_classes, &
+       radius_merge, &
+       radius_split, &
+       random_start_position, &
+       read_particles_from_restartfile, &
+       rm, &
+       seed_follows_topography, &
+       splitting, &
+       splitting_factor, &
+       splitting_factor_max, &
+       splitting_function, &
+       splitting_mode, &
+       step_dealloc, &
+       use_sgs_for_particles, &
+       vertical_particle_advection, &
+       weight_factor_merge, &
+       weight_factor_split, &
+       write_particle_statistics
+       
+!
+!-- Position the namelist-file at the beginning (it was already opened in
+!-- parin), search for the namelist-group of the package and position the
+!-- file at this line. Do the same for each optionally used package.
+    line = ' '
+    
+!
+!-- Try to find particles package
+    REWIND ( 11 )
+    line = ' '
+    DO   WHILE ( INDEX( line, '&particle_parameters' ) == 0 )
+       READ ( 11, '(A)', END=12 )  line
+    ENDDO
+    BACKSPACE ( 11 )
+!
+!-- Read user-defined namelist
+    READ ( 11, particle_parameters, ERR = 10 )
+!
+!-- Set flag that indicates that particles are switched on
+    particle_advection = .TRUE.
+    
+    GOTO 14
+
+10  BACKSPACE( 11 )
+    READ( 11 , '(A)') line
+    CALL parin_fail_message( 'particle_parameters', line )
+!
+!-- Try to find particles package (old namelist)
+12  REWIND ( 11 )
+    line = ' '
+    DO WHILE ( INDEX( line, '&particles_par' ) == 0 )
+       READ ( 11, '(A)', END=14 )  line
+    ENDDO
+    BACKSPACE ( 11 )
+!
+!-- Read user-defined namelist
+    READ ( 11, particles_par, ERR = 13, END = 14 )
+    
+    
+    message_string = 'namelist particles_par is deprecated and will be ' //    &
+                     'removed in near future. Please use namelist ' //         &
+                     'particle_parameters instead'
+    CALL message( 'package_parin', 'PA0487', 0, 1, 0, 6, 0 )
+
+!
+!-- Set flag that indicates that particles are switched on
+    particle_advection = .TRUE.
+
+    GOTO 14
+
+13    BACKSPACE( 11 )
+       READ( 11 , '(A)') line
+       CALL parin_fail_message( 'particles_par', line )
+
+14 CONTINUE
+    
+ END SUBROUTINE lpm_parin
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Writes used particle attributes in header file.
+!------------------------------------------------------------------------------! 
+ SUBROUTINE lpm_header ( io )
+
+    CHARACTER (LEN=40) ::  output_format       !< netcdf format
+ 
+    INTEGER(iwp) ::  i               !<
+    INTEGER(iwp), INTENT(IN) ::  io  !< Unit of the output file
+
+ 
+     IF ( humidity  .AND.  cloud_droplets )  THEN
+       WRITE ( io, 433 )
+       IF ( curvature_solution_effects )  WRITE ( io, 434 )
+       IF ( collision_kernel /= 'none' )  THEN
+          WRITE ( io, 435 )  TRIM( collision_kernel )
+          IF ( collision_kernel(6:9) == 'fast' )  THEN
+             WRITE ( io, 436 )  radius_classes, dissipation_classes
+          ENDIF
+       ELSE
+          WRITE ( io, 437 )
+       ENDIF
+    ENDIF
+ 
+    IF ( particle_advection )  THEN
+!
+!--    Particle attributes
+       WRITE ( io, 480 )  particle_advection_start, dt_prel, bc_par_lr, &
+                          bc_par_ns, bc_par_b, bc_par_t, particle_maximum_age, &
+                          end_time_prel
+       IF ( use_sgs_for_particles )  WRITE ( io, 488 )  dt_min_part
+       IF ( random_start_position )  WRITE ( io, 481 )
+       IF ( seed_follows_topography )  WRITE ( io, 496 )
+       IF ( particles_per_point > 1 )  WRITE ( io, 489 )  particles_per_point
+       WRITE ( io, 495 )  total_number_of_particles
+       IF ( dt_write_particle_data /= 9999999.9_wp )  THEN
+          WRITE ( io, 485 )  dt_write_particle_data
+          IF ( netcdf_data_format > 1 )  THEN
+             output_format = 'netcdf (64 bit offset) and binary'
+          ELSE
+             output_format = 'netcdf and binary'
+          ENDIF
+          IF ( netcdf_deflate == 0 )  THEN
+             WRITE ( io, 344 )  output_format
+          ELSE
+             WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
+          ENDIF
+       ENDIF
+       IF ( dt_dopts /= 9999999.9_wp )  WRITE ( io, 494 )  dt_dopts
+       IF ( write_particle_statistics )  WRITE ( io, 486 )
+
+       WRITE ( io, 487 )  number_of_particle_groups
+
+       DO  i = 1, number_of_particle_groups
+          IF ( i == 1  .AND.  density_ratio(i) == 9999999.9_wp )  THEN
+             WRITE ( io, 490 )  i, 0.0_wp
+             WRITE ( io, 492 )
+          ELSE
+             WRITE ( io, 490 )  i, radius(i)
+             IF ( density_ratio(i) /= 0.0_wp )  THEN
+                WRITE ( io, 491 )  density_ratio(i)
+             ELSE
+                WRITE ( io, 492 )
+             ENDIF
+          ENDIF
+          WRITE ( io, 493 )  psl(i), psr(i), pss(i), psn(i), psb(i), pst(i), &
+                             pdx(i), pdy(i), pdz(i)
+          IF ( .NOT. vertical_particle_advection(i) )  WRITE ( io, 482 )
+       ENDDO
+
+    ENDIF
+    
+344 FORMAT ('       Output format: ',A/)
+354 FORMAT ('       Output format: ',A, '   compressed with level: ',I1/)
+
+433 FORMAT ('    Cloud droplets treated explicitly using the Lagrangian part', &
+                 'icle model')
+434 FORMAT ('    Curvature and solution effecs are considered for growth of', &
+                 ' droplets < 1.0E-6 m')
+435 FORMAT ('    Droplet collision is handled by ',A,'-kernel')
+436 FORMAT ('       Fast kernel with fixed radius- and dissipation classes ', &
+                    'are used'/ &
+            '          number of radius classes:       ',I3,'    interval ', &
+                       '[1.0E-6,2.0E-4] m'/ &
+            '          number of dissipation classes:   ',I2,'    interval ', &
+                       '[0,1000] cm**2/s**3')
+437 FORMAT ('    Droplet collision is switched off')
+
+480 FORMAT ('    Particles:'/ &
+            '    ---------'// &
+            '       Particle advection is active (switched on at t = ', F7.1, &
+                    ' s)'/ &
+            '       Start of new particle generations every  ',F6.1,' s'/ &
+            '       Boundary conditions: left/right: ', A, ' north/south: ', A/&
+            '                            bottom:     ', A, ' top:         ', A/&
+            '       Maximum particle age:                 ',F9.1,' s'/ &
+            '       Advection stopped at t = ',F9.1,' s'/)
+481 FORMAT ('       Particles have random start positions'/)
+482 FORMAT ('          Particles are advected only horizontally'/)
+485 FORMAT ('       Particle data are written on file every ', F9.1, ' s')
+486 FORMAT ('       Particle statistics are written on file'/)
+487 FORMAT ('       Number of particle groups: ',I2/)
+488 FORMAT ('       SGS velocity components are used for particle advection'/ &
+            '          minimum timestep for advection:', F8.5/)
+489 FORMAT ('       Number of particles simultaneously released at each ', &
+                    'point: ', I5/)
+490 FORMAT ('       Particle group ',I2,':'/ &
+            '          Particle radius: ',E10.3, 'm')
+491 FORMAT ('          Particle inertia is activated'/ &
+            '             density_ratio (rho_fluid/rho_particle) =',F6.3/)
+492 FORMAT ('          Particles are advected only passively (no inertia)'/)
+493 FORMAT ('          Boundaries of particle source: x:',F8.1,' - ',F8.1,' m'/&
+            '                                         y:',F8.1,' - ',F8.1,' m'/&
+            '                                         z:',F8.1,' - ',F8.1,' m'/&
+            '          Particle distances:  dx = ',F8.1,' m  dy = ',F8.1, &
+                       ' m  dz = ',F8.1,' m'/)
+494 FORMAT ('       Output of particle time series in NetCDF format every ', &
+                    F8.2,' s'/)
+495 FORMAT ('       Number of particles in total domain: ',I10/)
+496 FORMAT ('       Initial vertical particle positions are interpreted ', &
+                    'as relative to the given topography')
+    
+ END SUBROUTINE lpm_header
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Writes used particle attributes in header file.
+!------------------------------------------------------------------------------!  
+ SUBROUTINE lpm_check_parameters
+ 
+!
+!-- Collision kernels:
+    SELECT CASE ( TRIM( collision_kernel ) )
+
+       CASE ( 'hall', 'hall_fast' )
+          hall_kernel = .TRUE.
+
+       CASE ( 'wang', 'wang_fast' )
+          wang_kernel = .TRUE.
+
+       CASE ( 'none' )
+
+
+       CASE DEFAULT
+          message_string = 'unknown collision kernel: collision_kernel = "' // &
+                           TRIM( collision_kernel ) // '"'
+          CALL message( 'check_parameters', 'PA0350', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    IF ( collision_kernel(6:9) == 'fast' )  use_kernel_tables = .TRUE.
+
+ END SUBROUTINE
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Initialize Lagrangian particle model
+!------------------------------------------------------------------------------! 
+ SUBROUTINE lpm_init
+
+    INTEGER(iwp) ::  i                           !<
+    INTEGER(iwp) ::  j                           !<
+    INTEGER(iwp) ::  k                           !<
+
+    REAL(wp) ::  div                             !<
+    REAL(wp) ::  height_int                      !<
+    REAL(wp) ::  height_p                        !<
+    REAL(wp) ::  z_p                             !<
+    REAL(wp) ::  z0_av_local                     !<
+
+!
+!-- In case of oceans runs, the vertical index calculations need an offset,
+!-- because otherwise the k indices will become negative
+    IF ( ocean_mode )  THEN
+       offset_ocean_nzt    = nzt
+       offset_ocean_nzt_m1 = nzt - 1
+    ENDIF
+
+!
+!-- Define block offsets for dividing a gridcell in 8 sub cells
+!-- See documentation for List of subgrid boxes
+!-- See pack_and_sort in lpm_pack_arrays.f90 for assignment of the subgrid boxes
+    block_offset(0) = block_offset_def ( 0, 0, 0)
+    block_offset(1) = block_offset_def ( 0, 0,-1)
+    block_offset(2) = block_offset_def ( 0,-1, 0)
+    block_offset(3) = block_offset_def ( 0,-1,-1)
+    block_offset(4) = block_offset_def (-1, 0, 0)
+    block_offset(5) = block_offset_def (-1, 0,-1)
+    block_offset(6) = block_offset_def (-1,-1, 0)
+    block_offset(7) = block_offset_def (-1,-1,-1)
+!
+!-- Check the number of particle groups.
+    IF ( number_of_particle_groups > max_number_of_particle_groups )  THEN
+       WRITE( message_string, * ) 'max_number_of_particle_groups =',           &
+                                  max_number_of_particle_groups ,              &
+                                  '&number_of_particle_groups reset to ',      &
+                                  max_number_of_particle_groups
+       CALL message( 'lpm_init', 'PA0213', 0, 1, 0, 6, 0 )
+       number_of_particle_groups = max_number_of_particle_groups
+    ENDIF
+!
+!-- Check if downward-facing walls exist. This case, reflection boundary
+!-- conditions (as well as subgrid-scale velocities) may do not work
+!-- propably (not realized so far).
+    IF ( surf_def_h(1)%ns >= 1 )  THEN
+       WRITE( message_string, * ) 'Overhanging topography do not work '//      &
+                                  'with particles'
+       CALL message( 'lpm_init', 'PA0212', 0, 1, 0, 6, 0 )
+
+    ENDIF
+
+!
+!-- Set default start positions, if necessary
+    IF ( psl(1) == 9999999.9_wp )  psl(1) = 0.0_wp
+    IF ( psr(1) == 9999999.9_wp )  psr(1) = ( nx +1 ) * dx
+    IF ( pss(1) == 9999999.9_wp )  pss(1) = 0.0_wp
+    IF ( psn(1) == 9999999.9_wp )  psn(1) = ( ny +1 ) * dy
+    IF ( psb(1) == 9999999.9_wp )  psb(1) = zu(nz/2)
+    IF ( pst(1) == 9999999.9_wp )  pst(1) = psb(1)
+
+    IF ( pdx(1) == 9999999.9_wp  .OR.  pdx(1) == 0.0_wp )  pdx(1) = dx
+    IF ( pdy(1) == 9999999.9_wp  .OR.  pdy(1) == 0.0_wp )  pdy(1) = dy
+    IF ( pdz(1) == 9999999.9_wp  .OR.  pdz(1) == 0.0_wp )  pdz(1) = zu(2) - zu(1)
+
+!
+!-- If number_particles_per_gridbox is set, the parametres pdx, pdy and pdz are
+!-- calculated diagnostically. Therfore an isotropic distribution is prescribed.
+    IF ( number_particles_per_gridbox /= -1 .AND.   &
+         number_particles_per_gridbox >= 1 )    THEN
+       pdx(1) = (( dx * dy * ( zu(2) - zu(1) ) ) /  &
+             REAL(number_particles_per_gridbox))**0.3333333_wp
+!
+!--    Ensure a smooth value (two significant digits) of distance between
+!--    particles (pdx, pdy, pdz).
+       div = 1000.0_wp
+       DO  WHILE ( pdx(1) < div )
+          div = div / 10.0_wp
+       ENDDO
+       pdx(1) = NINT( pdx(1) * 100.0_wp / div ) * div / 100.0_wp
+       pdy(1) = pdx(1)
+       pdz(1) = pdx(1)
+
+    ENDIF
+
+    DO  j = 2, number_of_particle_groups
+       IF ( psl(j) == 9999999.9_wp )  psl(j) = psl(j-1)
+       IF ( psr(j) == 9999999.9_wp )  psr(j) = psr(j-1)
+       IF ( pss(j) == 9999999.9_wp )  pss(j) = pss(j-1)
+       IF ( psn(j) == 9999999.9_wp )  psn(j) = psn(j-1)
+       IF ( psb(j) == 9999999.9_wp )  psb(j) = psb(j-1)
+       IF ( pst(j) == 9999999.9_wp )  pst(j) = pst(j-1)
+       IF ( pdx(j) == 9999999.9_wp  .OR.  pdx(j) == 0.0_wp )  pdx(j) = pdx(j-1)
+       IF ( pdy(j) == 9999999.9_wp  .OR.  pdy(j) == 0.0_wp )  pdy(j) = pdy(j-1)
+       IF ( pdz(j) == 9999999.9_wp  .OR.  pdz(j) == 0.0_wp )  pdz(j) = pdz(j-1)
+    ENDDO
+
+!
+!-- Allocate arrays required for calculating particle SGS velocities.
+!-- Initialize prefactor required for stoachastic Weil equation.
+    IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
+       ALLOCATE( de_dx(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 de_dy(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 de_dz(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+
+       de_dx = 0.0_wp
+       de_dy = 0.0_wp
+       de_dz = 0.0_wp             
+                 
+       sgs_wf_part = 1.0_wp / 3.0_wp
+    ENDIF
+
+!
+!-- Allocate array required for logarithmic vertical interpolation of
+!-- horizontal particle velocities between the surface and the first vertical
+!-- grid level. In order to avoid repeated CPU cost-intensive CALLS of
+!-- intrinsic FORTRAN procedure LOG(z/z0), LOG(z/z0) is precalculated for
+!-- several heights. Splitting into 20 sublayers turned out to be sufficient.
+!-- To obtain exact height levels of particles, linear interpolation is applied
+!-- (see lpm_advec.f90).
+    IF ( constant_flux_layer )  THEN
+
+       ALLOCATE ( log_z_z0(0:number_of_sublayers) )
+       z_p = zu(nzb+1) - zw(nzb)
+
+!
+!--    Calculate horizontal mean value of z0 used for logartihmic
+!--    interpolation. Note: this is not exact for heterogeneous z0.
+!--    However, sensitivity studies showed that the effect is
+!--    negligible.
+       z0_av_local  = SUM( surf_def_h(0)%z0 ) + SUM( surf_lsm_h%z0 ) +         &
+                      SUM( surf_usm_h%z0 )
+       z0_av_global = 0.0_wp
+
+#if defined( __parallel )
+       CALL MPI_ALLREDUCE(z0_av_local, z0_av_global, 1, MPI_REAL, MPI_SUM, &
+                          comm2d, ierr )
+#else
+       z0_av_global = z0_av_local
+#endif
+
+       z0_av_global = z0_av_global  / ( ( ny + 1 ) * ( nx + 1 ) )
+!
+!--    Horizontal wind speed is zero below and at z0
+       log_z_z0(0) = 0.0_wp
+!
+!--    Calculate vertical depth of the sublayers
+       height_int  = ( z_p - z0_av_global ) / REAL( number_of_sublayers, KIND=wp )
+!
+!--    Precalculate LOG(z/z0)
+       height_p    = z0_av_global
+       DO  k = 1, number_of_sublayers
+
+          height_p    = height_p + height_int
+          log_z_z0(k) = LOG( height_p / z0_av_global )
+
+       ENDDO
+
+    ENDIF
+
+!
+!-- Check boundary condition and set internal variables
+    SELECT CASE ( bc_par_b )
+
+       CASE ( 'absorb' )
+          ibc_par_b = 1
+
+       CASE ( 'reflect' )
+          ibc_par_b = 2
+
+       CASE DEFAULT
+          WRITE( message_string, * )  'unknown boundary condition ',           &
+                                       'bc_par_b = "', TRIM( bc_par_b ), '"'
+          CALL message( 'lpm_init', 'PA0217', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    SELECT CASE ( bc_par_t )
+
+       CASE ( 'absorb' )
+          ibc_par_t = 1
+
+       CASE ( 'reflect' )
+          ibc_par_t = 2
+          
+       CASE ( 'nested' )
+          ibc_par_t = 3
+
+       CASE DEFAULT
+          WRITE( message_string, * ) 'unknown boundary condition ',            &
+                                     'bc_par_t = "', TRIM( bc_par_t ), '"'
+          CALL message( 'lpm_init', 'PA0218', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    SELECT CASE ( bc_par_lr )
+
+       CASE ( 'cyclic' )
+          ibc_par_lr = 0
+
+       CASE ( 'absorb' )
+          ibc_par_lr = 1
+
+       CASE ( 'reflect' )
+          ibc_par_lr = 2
+          
+       CASE ( 'nested' )
+          ibc_par_lr = 3
+
+       CASE DEFAULT
+          WRITE( message_string, * ) 'unknown boundary condition ',   &
+                                     'bc_par_lr = "', TRIM( bc_par_lr ), '"'
+          CALL message( 'lpm_init', 'PA0219', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    SELECT CASE ( bc_par_ns )
+
+       CASE ( 'cyclic' )
+          ibc_par_ns = 0
+
+       CASE ( 'absorb' )
+          ibc_par_ns = 1
+
+       CASE ( 'reflect' )
+          ibc_par_ns = 2
+          
+       CASE ( 'nested' )
+          ibc_par_ns = 3
+
+       CASE DEFAULT
+          WRITE( message_string, * ) 'unknown boundary condition ',   &
+                                     'bc_par_ns = "', TRIM( bc_par_ns ), '"'
+          CALL message( 'lpm_init', 'PA0220', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    SELECT CASE ( splitting_mode )
+
+       CASE ( 'const' )
+          i_splitting_mode = 1
+
+       CASE ( 'cl_av' )
+          i_splitting_mode = 2
+
+       CASE ( 'gb_av' )
+          i_splitting_mode = 3
+
+       CASE DEFAULT
+          WRITE( message_string, * )  'unknown splitting_mode = "',            &
+                                      TRIM( splitting_mode ), '"'
+          CALL message( 'lpm_init', 'PA0146', 1, 2, 0, 6, 0 )
+
+    END SELECT
+    SELECT CASE ( splitting_function )
+
+       CASE ( 'gamma' )
+          isf = 1
+
+       CASE ( 'log' )
+          isf = 2
+
+       CASE ( 'exp' )
+          isf = 3
+
+       CASE DEFAULT
+          WRITE( message_string, * )  'unknown splitting function = "',        &
+                                       TRIM( splitting_function ), '"'
+          CALL message( 'lpm_init', 'PA0147', 1, 2, 0, 6, 0 )
+
+    END SELECT
+!
+!-- Initialize collision kernels
+    IF ( collision_kernel /= 'none' )  CALL lpm_init_kernels
+!
+!-- For the first model run of a possible job chain initialize the
+!-- particles, otherwise read the particle data from restart file.
+    IF ( TRIM( initializing_actions ) == 'read_restart_data'  &
+         .AND.  read_particles_from_restartfile )  THEN
+       CALL lpm_rrd_local_particles
+    ELSE
+!
+!--    Allocate particle arrays and set attributes of the initial set of
+!--    particles, which can be also periodically released at later times.
+       ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
+                 grid_particles(nzb+1:nzt,nys:nyn,nxl:nxr) )
+
+       number_of_particles = 0
+       prt_count           = 0
+!
+!--    initialize counter for particle IDs
+       grid_particles%id_counter = 1
+!
+!--    Initialize all particles with dummy values (otherwise errors may
+!--    occur within restart runs). The reason for this is still not clear
+!--    and may be presumably caused by errors in the respective user-interface.
+       zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
+                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
+                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
+                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
+                                      0, 0, 0_idp, .FALSE., -1 )
+
+       particle_groups = particle_groups_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp )
+!
+!--    Set values for the density ratio and radius for all particle
+!--    groups, if necessary
+       IF ( density_ratio(1) == 9999999.9_wp )  density_ratio(1) = 0.0_wp
+       IF ( radius(1)        == 9999999.9_wp )  radius(1) = 0.0_wp
+       DO  i = 2, number_of_particle_groups
+          IF ( density_ratio(i) == 9999999.9_wp )  THEN
+             density_ratio(i) = density_ratio(i-1)
+          ENDIF
+          IF ( radius(i) == 9999999.9_wp )  radius(i) = radius(i-1)
+       ENDDO
+
+       DO  i = 1, number_of_particle_groups
+          IF ( density_ratio(i) /= 0.0_wp  .AND.  radius(i) == 0 )  THEN
+             WRITE( message_string, * ) 'particle group #', i, ' has a',       &
+                                        'density ratio /= 0 but radius = 0'
+             CALL message( 'lpm_init', 'PA0215', 1, 2, 0, 6, 0 )
+          ENDIF
+          particle_groups(i)%density_ratio = density_ratio(i)
+          particle_groups(i)%radius        = radius(i)
+       ENDDO
+!
+!--    Set a seed value for the random number generator to be exclusively
+!--    used for the particle code. The generated random numbers should be
+!--    different on the different PEs.
+       iran_part = iran_part + myid
+!
+!--    Create the particle set, and set the initial particles
+       CALL lpm_create_particle( phase_init )
+       last_particle_release_time = particle_advection_start
+!
+!--    User modification of initial particles
+       CALL user_lpm_init
+!
+!--    Open file for statistical informations about particle conditions
+       IF ( write_particle_statistics )  THEN
+          CALL check_open( 80 )
+          WRITE ( 80, 8000 )  current_timestep_number, simulated_time,         &
+                              number_of_particles
+          CALL close_file( 80 )
+       ENDIF
+
+    ENDIF
+
+    IF ( nested_run )  CALL pmcp_g_init
+!
+!-- To avoid programm abort, assign particles array to the local version of
+!-- first grid cell
+    number_of_particles = prt_count(nzb+1,nys,nxl)
+    particles => grid_particles(nzb+1,nys,nxl)%particles(1:number_of_particles)
+!
+!-- Formats
+8000 FORMAT (I6,1X,F7.2,4X,I10,71X,I10)
+
+ END SUBROUTINE lpm_init
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Create Lagrangian particles
+!------------------------------------------------------------------------------!  
+ SUBROUTINE lpm_create_particle (phase)
+
+    INTEGER(iwp)               ::  alloc_size  !< relative increase of allocated memory for particles
+    INTEGER(iwp)               ::  i           !< loop variable ( particle groups )
+    INTEGER(iwp)               ::  ip          !< index variable along x
+    INTEGER(iwp)               ::  j           !< loop variable ( particles per point )
+    INTEGER(iwp)               ::  jp          !< index variable along y
+    INTEGER(iwp)               ::  k           !< index variable along z
+    INTEGER(iwp)               ::  k_surf      !< index of surface grid point
+    INTEGER(iwp)               ::  kp          !< index variable along z
+    INTEGER(iwp)               ::  loop_stride !< loop variable for initialization
+    INTEGER(iwp)               ::  n           !< loop variable ( number of particles )
+    INTEGER(iwp)               ::  new_size    !< new size of allocated memory for particles
+
+    INTEGER(iwp), INTENT(IN)   ::  phase       !< mode of inititialization
+
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_count !< start address of new particle
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_start !< start address of new particle
+
+    LOGICAL                    ::  first_stride !< flag for initialization
+
+    REAL(wp)                   ::  pos_x      !< increment for particle position in x
+    REAL(wp)                   ::  pos_y      !< increment for particle position in y
+    REAL(wp)                   ::  pos_z      !< increment for particle position in z
+    REAL(wp)                   ::  rand_contr !< dummy argument for random position
+
+    TYPE(particle_type),TARGET ::  tmp_particle !< temporary particle used for initialization
+
+!
+!-- Calculate particle positions and store particle attributes, if
+!-- particle is situated on this PE
+    DO  loop_stride = 1, 2
+       first_stride = (loop_stride == 1)
+       IF ( first_stride )   THEN
+          local_count = 0           ! count number of particles
+       ELSE
+          local_count = prt_count   ! Start address of new particles
+       ENDIF
+
+!
+!--    Calculate initial_weighting_factor diagnostically
+       IF ( number_concentration /= -1.0_wp .AND. number_concentration > 0.0_wp ) THEN
+          initial_weighting_factor =  number_concentration  *                        &
+                                      pdx(1) * pdy(1) * pdz(1)
+       END IF
+
+       n = 0
+       DO  i = 1, number_of_particle_groups
+          pos_z = psb(i)
+          DO WHILE ( pos_z <= pst(i) )
+             IF ( pos_z >= zw(0) .AND.  pos_z < zw(nzt) )  THEN
+                pos_y = pss(i)
+                DO WHILE ( pos_y <= psn(i) )
+                   IF ( pos_y >= nys * dy  .AND.                  &
+                        pos_y <  ( nyn + 1 ) * dy  ) THEN
+                      pos_x = psl(i)
+               xloop: DO WHILE ( pos_x <= psr(i) )
+                         IF ( pos_x >= nxl * dx  .AND.            &
+                              pos_x <  ( nxr + 1) * dx ) THEN
+                            DO  j = 1, particles_per_point
+                               n = n + 1
+                               tmp_particle%x             = pos_x
+                               tmp_particle%y             = pos_y
+                               tmp_particle%z             = pos_z
+                               tmp_particle%age           = 0.0_wp
+                               tmp_particle%age_m         = 0.0_wp
+                               tmp_particle%dt_sum        = 0.0_wp
+                               tmp_particle%e_m           = 0.0_wp
+                               tmp_particle%rvar1         = 0.0_wp
+                               tmp_particle%rvar2         = 0.0_wp
+                               tmp_particle%rvar3         = 0.0_wp
+                               tmp_particle%speed_x       = 0.0_wp
+                               tmp_particle%speed_y       = 0.0_wp
+                               tmp_particle%speed_z       = 0.0_wp
+                               tmp_particle%origin_x      = pos_x
+                               tmp_particle%origin_y      = pos_y
+                               tmp_particle%origin_z      = pos_z
+                               IF ( curvature_solution_effects )  THEN
+                                  tmp_particle%aux1      = 0.0_wp    ! dry aerosol radius
+                                  tmp_particle%aux2      = dt_3d     ! last Rosenbrock timestep
+                               ELSE
+                                  tmp_particle%aux1      = 0.0_wp    ! free to use
+                                  tmp_particle%aux2      = 0.0_wp    ! free to use
+                               ENDIF
+                               tmp_particle%radius        = particle_groups(i)%radius
+                               tmp_particle%weight_factor = initial_weighting_factor
+                               tmp_particle%class         = 1
+                               tmp_particle%group         = i
+                               tmp_particle%id            = 0_idp
+                               tmp_particle%particle_mask = .TRUE.
+                               tmp_particle%block_nr      = -1
+!
+!--                            Determine the grid indices of the particle position
+                               ip = INT( tmp_particle%x * ddx )
+                               jp = INT( tmp_particle%y * ddy )
+                               kp = INT( tmp_particle%z / dz(1) + 1 + offset_ocean_nzt )
+                               DO WHILE( zw(kp) < tmp_particle%z ) 
+                                  kp = kp + 1
+                               ENDDO
+                               DO WHILE( zw(kp-1) > tmp_particle%z )
+                                  kp = kp - 1
+                               ENDDO 
+!
+!--                            Determine surface level. Therefore, check for
+!--                            upward-facing wall on w-grid. 
+                               k_surf = get_topography_top_index_ji( jp, ip, 'w' )
+                               IF ( seed_follows_topography )  THEN
+!
+!--                               Particle height is given relative to topography
+                                  kp = kp + k_surf
+                                  tmp_particle%z = tmp_particle%z + zw(k_surf)
+!--                               Skip particle release if particle position is
+!--                               above model top, or within topography in case
+!--                               of overhanging structures.
+                                  IF ( kp > nzt  .OR.                          &
+                                 .NOT. BTEST( wall_flags_0(kp,jp,ip), 0 ) )  THEN
+                                     pos_x = pos_x + pdx(i)
+                                     CYCLE xloop
+                                  ENDIF
+!
+!--                            Skip particle release if particle position is
+!--                            below surface, or within topography in case
+!--                            of overhanging structures.
+                               ELSEIF ( .NOT. seed_follows_topography .AND.    &
+                                         tmp_particle%z <= zw(k_surf)  .OR.    &
+                                        .NOT. BTEST( wall_flags_0(kp,jp,ip), 0 ) )&
+                               THEN
+                                  pos_x = pos_x + pdx(i)
+                                  CYCLE xloop
+                               ENDIF
+
+                               local_count(kp,jp,ip) = local_count(kp,jp,ip) + 1
+
+                               IF ( .NOT. first_stride )  THEN
+                                  IF ( ip < nxl  .OR.  jp < nys  .OR.  kp < nzb+1 )  THEN
+                                     write(6,*) 'xl ',ip,jp,kp,nxl,nys,nzb+1
+                                  ENDIF
+                                  IF ( ip > nxr  .OR.  jp > nyn  .OR.  kp > nzt )  THEN
+                                     write(6,*) 'xu ',ip,jp,kp,nxr,nyn,nzt
+                                  ENDIF
+                                  grid_particles(kp,jp,ip)%particles(local_count(kp,jp,ip)) = tmp_particle
+                               ENDIF
+                            ENDDO
+                         ENDIF
+                         pos_x = pos_x + pdx(i)
+                      ENDDO xloop
+                   ENDIF
+                   pos_y = pos_y + pdy(i)
+                ENDDO
+             ENDIF
+
+             pos_z = pos_z + pdz(i)
+          ENDDO
+       ENDDO
+
+       IF ( first_stride )  THEN
+          DO  ip = nxl, nxr
+             DO  jp = nys, nyn
+                DO  kp = nzb+1, nzt
+                   IF ( phase == PHASE_INIT )  THEN
+                      IF ( local_count(kp,jp,ip) > 0 )  THEN
+                         alloc_size = MAX( INT( local_count(kp,jp,ip) *        &
+                            ( 1.0_wp + alloc_factor / 100.0_wp ) ),            &
+                            min_nr_particle )
+                      ELSE
+                         alloc_size = min_nr_particle
+                      ENDIF
+                      ALLOCATE(grid_particles(kp,jp,ip)%particles(1:alloc_size))
+                      DO  n = 1, alloc_size
+                         grid_particles(kp,jp,ip)%particles(n) = zero_particle
+                      ENDDO
+                   ELSEIF ( phase == PHASE_RELEASE )  THEN
+                      IF ( local_count(kp,jp,ip) > 0 )  THEN
+                         new_size   = local_count(kp,jp,ip) + prt_count(kp,jp,ip)
+                         alloc_size = MAX( INT( new_size * ( 1.0_wp +          &
+                            alloc_factor / 100.0_wp ) ), min_nr_particle )
+                         IF( alloc_size > SIZE( grid_particles(kp,jp,ip)%particles) )  THEN
+                            CALL realloc_particles_array(ip,jp,kp,alloc_size)
+                         ENDIF
+                      ENDIF
+                   ENDIF
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDIF
+
+    ENDDO
+
+    local_start = prt_count+1
+    prt_count   = local_count
+!
+!-- Calculate particle IDs
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+
+             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
+
+                particles(n)%id = 10000_idp**3 * grid_particles(kp,jp,ip)%id_counter + &
+                                  10000_idp**2 * kp + 10000_idp * jp + ip
+!
+!--             Count the number of particles that have been released before
+                grid_particles(kp,jp,ip)%id_counter =                          &
+                                         grid_particles(kp,jp,ip)%id_counter + 1
+
+             ENDDO
+
+          ENDDO
+       ENDDO
+    ENDDO
+!
+!-- Initialize aerosol background spectrum
+    IF ( curvature_solution_effects )  THEN
+       CALL lpm_init_aerosols(local_start)
+    ENDIF
+!
+!-- Add random fluctuation to particle positions.
+    IF ( random_start_position )  THEN
+       DO  ip = nxl, nxr
+          DO  jp = nys, nyn
+             DO  kp = nzb+1, nzt
+                number_of_particles = prt_count(kp,jp,ip)
+                IF ( number_of_particles <= 0 )  CYCLE
+                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+!
+!--             Move only new particles. Moreover, limit random fluctuation
+!--             in order to prevent that particles move more than one grid box,
+!--             which would lead to problems concerning particle exchange
+!--             between processors in case pdx/pdy are larger than dx/dy,
+!--             respectively.
+                DO  n = local_start(kp,jp,ip), number_of_particles
+                   IF ( psl(particles(n)%group) /= psr(particles(n)%group) )  THEN
+                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
+                                     pdx(particles(n)%group)
+                      particles(n)%x = particles(n)%x +                        &
+                              MERGE( rand_contr, SIGN( dx, rand_contr ),       &
+                                     ABS( rand_contr ) < dx                    &
+                                   )
+                   ENDIF
+                   IF ( pss(particles(n)%group) /= psn(particles(n)%group) )  THEN
+                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
+                                     pdy(particles(n)%group)
+                      particles(n)%y = particles(n)%y +                        &
+                              MERGE( rand_contr, SIGN( dy, rand_contr ),       &
+                                     ABS( rand_contr ) < dy                    &
+                                   )
+                   ENDIF
+                   IF ( psb(particles(n)%group) /= pst(particles(n)%group) )  THEN
+                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
+                                     pdz(particles(n)%group)
+                      particles(n)%z = particles(n)%z +                        &
+                              MERGE( rand_contr, SIGN( dzw(kp), rand_contr ),  &
+                                     ABS( rand_contr ) < dzw(kp)               &
+                                   )
+                   ENDIF
+                ENDDO
+!
+!--             Identify particles located outside the model domain and reflect
+!--             or absorb them if necessary.
+                CALL lpm_boundary_conds( 'bottom/top', i, j, k )
+!
+!--             Furthermore, remove particles located in topography. Note, as
+!--             the particle speed is still zero at this point, wall
+!--             reflection boundary conditions will not work in this case.
+                particles =>                                                   &
+                       grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+                DO  n = local_start(kp,jp,ip), number_of_particles
+                   i = particles(n)%x * ddx
+                   j = particles(n)%y * ddy
+                   k = particles(n)%z / dz(1) + 1 + offset_ocean_nzt
+                   DO WHILE( zw(k) < particles(n)%z )
+                      k = k + 1
+                   ENDDO
+                   DO WHILE( zw(k-1) > particles(n)%z )
+                      k = k - 1
+                   ENDDO
+!
+!--                Check if particle is within topography
+                   IF ( .NOT. BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
+                      particles(n)%particle_mask = .FALSE.
+                      deleted_particles = deleted_particles + 1
+                   ENDIF
+
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDDO
+!
+!--    Exchange particles between grid cells and processors
+       CALL lpm_move_particle
+       CALL lpm_exchange_horiz
+
+    ENDIF
+!
+!-- In case of random_start_position, delete particles identified by
+!-- lpm_exchange_horiz and lpm_boundary_conds. Then sort particles into blocks,
+!-- which is needed for a fast interpolation of the LES fields on the particle
+!-- position.
+    CALL lpm_sort_in_subboxes
+!
+!-- Determine the current number of particles
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles         = number_of_particles                 &
+                                           + prt_count(kp,jp,ip)
+          ENDDO
+       ENDDO
+    ENDDO
+!
+!-- Calculate the number of particles of the total domain
+#if defined( __parallel )
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( number_of_particles, total_number_of_particles, 1, &
+    MPI_INTEGER, MPI_SUM, comm2d, ierr )
+#else
+    total_number_of_particles = number_of_particles
+#endif
+
+    RETURN
+
+ END SUBROUTINE lpm_create_particle
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine initialize the particles as aerosols with physio-chemical 
+!> properties. 
+!------------------------------------------------------------------------------!   
+ SUBROUTINE lpm_init_aerosols(local_start)
+
+    REAL(wp)  :: afactor            !< curvature effects
+    REAL(wp)  :: bfactor            !< solute effects
+    REAL(wp)  :: dlogr              !< logarithmic width of radius bin
+    REAL(wp)  :: e_a                !< vapor pressure
+    REAL(wp)  :: e_s                !< saturation vapor pressure
+    REAL(wp)  :: rmin = 0.005e-6_wp !< minimum aerosol radius
+    REAL(wp)  :: rmax = 10.0e-6_wp  !< maximum aerosol radius
+    REAL(wp)  :: r_mid              !< mean radius of bin
+    REAL(wp)  :: r_l                !< left radius of bin
+    REAL(wp)  :: r_r                !< right radius of bin
+    REAL(wp)  :: sigma              !< surface tension
+    REAL(wp)  :: t_int              !< temperature
+
+    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(IN) ::  local_start !<
+
+    INTEGER(iwp)  :: n              !<
+    INTEGER(iwp)  :: ip             !<
+    INTEGER(iwp)  :: jp             !<
+    INTEGER(iwp)  :: kp             !<
+
+!
+!-- Set constants for different aerosol species
+    IF ( TRIM(aero_species) .EQ. 'nacl' ) THEN
+       molecular_weight_of_solute = 0.05844_wp 
+       rho_s                      = 2165.0_wp
+       vanthoff                   = 2.0_wp
+    ELSEIF ( TRIM(aero_species) .EQ. 'c3h4o4' ) THEN
+       molecular_weight_of_solute = 0.10406_wp 
+       rho_s                      = 1600.0_wp
+       vanthoff                   = 1.37_wp
+    ELSEIF ( TRIM(aero_species) .EQ. 'nh4o3' ) THEN
+       molecular_weight_of_solute = 0.08004_wp 
+       rho_s                      = 1720.0_wp
+       vanthoff                   = 2.31_wp
+    ELSE
+       WRITE( message_string, * ) 'unknown aerosol species ',   &
+                                'aero_species = "', TRIM( aero_species ), '"'
+       CALL message( 'lpm_init', 'PA0470', 1, 2, 0, 6, 0 )
+    ENDIF
+!
+!-- The following typical aerosol spectra are taken from Jaenicke (1993):
+!-- Tropospheric aerosols. Published in Aerosol-Cloud-Climate Interactions.
+    IF ( TRIM(aero_type) .EQ. 'polar' )  THEN
+       na        = (/ 2.17e1, 1.86e-1, 3.04e-4 /) * 1.0E6
+       rm        = (/ 0.0689, 0.375, 4.29 /) * 1.0E-6
+       log_sigma = (/ 0.245, 0.300, 0.291 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'background' )  THEN
+       na        = (/ 1.29e2, 5.97e1, 6.35e1 /) * 1.0E6
+       rm        = (/ 0.0036, 0.127, 0.259 /) * 1.0E-6
+       log_sigma = (/ 0.645, 0.253, 0.425 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'maritime' )  THEN
+       na        = (/ 1.33e2, 6.66e1, 3.06e0 /) * 1.0E6
+       rm        = (/ 0.0039, 0.133, 0.29 /) * 1.0E-6
+       log_sigma = (/ 0.657, 0.210, 0.396 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'continental' )  THEN
+       na        = (/ 3.20e3, 2.90e3, 3.00e-1 /) * 1.0E6
+       rm        = (/ 0.01, 0.058, 0.9 /) * 1.0E-6
+       log_sigma = (/ 0.161, 0.217, 0.380 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'desert' )  THEN
+       na        = (/ 7.26e2, 1.14e3, 1.78e-1 /) * 1.0E6
+       rm        = (/ 0.001, 0.0188, 10.8 /) * 1.0E-6
+       log_sigma = (/ 0.247, 0.770, 0.438 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'rural' )  THEN
+       na        = (/ 6.65e3, 1.47e2, 1.99e3 /) * 1.0E6
+       rm        = (/ 0.00739, 0.0269, 0.0419 /) * 1.0E-6
+       log_sigma = (/ 0.225, 0.557, 0.266 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'urban' )  THEN
+       na        = (/ 9.93e4, 1.11e3, 3.64e4 /) * 1.0E6
+       rm        = (/ 0.00651, 0.00714, 0.0248 /) * 1.0E-6
+       log_sigma = (/ 0.245, 0.666, 0.337 /)
+    ELSEIF ( TRIM(aero_type) .EQ. 'user' )  THEN
+       CONTINUE
+    ELSE
+       WRITE( message_string, * ) 'unknown aerosol type ',   &
+                                'aero_type = "', TRIM( aero_type ), '"'
+       CALL message( 'lpm_init', 'PA0459', 1, 2, 0, 6, 0 )
+    ENDIF
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+
+             dlogr   = ( LOG10(rmax) - LOG10(rmin) ) / ( number_of_particles - local_start(kp,jp,ip) + 1 )
+!
+!--          Initialize the aerosols with a predefined spectral distribution
+!--          of the dry radius (logarithmically increasing bins) and a varying
+!--          weighting factor
+             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
+
+                r_l   = 10.0**( LOG10( rmin ) + (n-1) * dlogr )
+                r_r   = 10.0**( LOG10( rmin ) + n * dlogr )
+                r_mid = SQRT( r_l * r_r )
+
+                particles(n)%aux1          = r_mid
+                particles(n)%weight_factor =                                           &
+                   ( na(1) / ( SQRT( 2.0 * pi ) * log_sigma(1) ) *                     &
+                     EXP( - LOG10( r_mid / rm(1) )**2 / ( 2.0 * log_sigma(1)**2 ) ) +  &
+                     na(2) / ( SQRT( 2.0 * pi ) * log_sigma(2) ) *                     &
+                     EXP( - LOG10( r_mid / rm(2) )**2 / ( 2.0 * log_sigma(2)**2 ) ) +  &
+                     na(3) / ( SQRT( 2.0 * pi ) * log_sigma(3) ) *                     &
+                     EXP( - LOG10( r_mid / rm(3) )**2 / ( 2.0 * log_sigma(3)**2 ) )    &
+                   ) * ( LOG10(r_r) - LOG10(r_l) ) * ( dx * dy * dzw(kp) )
+
+!
+!--             Multiply weight_factor with the namelist parameter aero_weight
+!--             to increase or decrease the number of simulated aerosols
+                particles(n)%weight_factor = particles(n)%weight_factor * aero_weight
+
+                IF ( particles(n)%weight_factor - FLOOR(particles(n)%weight_factor,KIND=wp) &
+                     .GT. random_function( iran_part ) )  THEN
+                   particles(n)%weight_factor = FLOOR(particles(n)%weight_factor,KIND=wp) + 1.0_wp
+                ELSE
+                   particles(n)%weight_factor = FLOOR(particles(n)%weight_factor,KIND=wp)
+                ENDIF
+!
+!--             Unnecessary particles will be deleted
+                IF ( particles(n)%weight_factor .LE. 0.0 )  particles(n)%particle_mask = .FALSE.
+
+             ENDDO
+!
+!--          Set particle radius to equilibrium radius based on the environmental
+!--          supersaturation (Khvorostyanov and Curry, 2007, JGR). This avoids
+!--          the sometimes lengthy growth toward their equilibrium radius within
+!--          the simulation.
+             t_int  = pt(kp,jp,ip) * exner(kp)
+
+             e_s = magnus( t_int )
+             e_a = q(kp,jp,ip) * hyp(kp) / ( q(kp,jp,ip) + rd_d_rv )
+
+             sigma   = 0.0761_wp - 0.000155_wp * ( t_int - 273.15_wp )
+             afactor = 2.0_wp * sigma / ( rho_l * r_v * t_int )
+
+             bfactor = vanthoff * molecular_weight_of_water *    &
+                       rho_s / ( molecular_weight_of_solute * rho_l )
+!
+!--          The formula is only valid for subsaturated environments. For
+!--          supersaturations higher than -5 %, the supersaturation is set to -5%.
+             IF ( e_a / e_s >= 0.95_wp )  e_a = 0.95_wp * e_s
+
+             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
+!
+!--             For details on this equation, see Eq. (14) of Khvorostyanov and
+!--             Curry (2007, JGR)
+                particles(n)%radius = bfactor**0.3333333_wp *                  &
+                   particles(n)%aux1 / ( 1.0_wp - e_a / e_s )**0.3333333_wp / &
+                   ( 1.0_wp + ( afactor / ( 3.0_wp * bfactor**0.3333333_wp *   &
+                     particles(n)%aux1 ) ) /                                  &
+                     ( 1.0_wp - e_a / e_s )**0.6666666_wp                      &
+                   )
+
+             ENDDO
+
+          ENDDO
+       ENDDO
+    ENDDO
+
+ END SUBROUTINE lpm_init_aerosols
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Calculates quantities required for considering the SGS velocity fluctuations
+!> in the particle transport by a stochastic approach. The respective
+!> quantities are: SGS-TKE gradients and horizontally averaged profiles of the
+!> SGS TKE and the resolved-scale velocity variances. 
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_init_sgs_tke
+    
+    USE statistics,                                                            &
+        ONLY:  flow_statistics_called, hom, sums, sums_l
+
+    INTEGER(iwp) ::  i      !< index variable along x
+    INTEGER(iwp) ::  j      !< index variable along y
+    INTEGER(iwp) ::  k      !< index variable along z
+    INTEGER(iwp) ::  m      !< running index for the surface elements 
+
+    REAL(wp) ::  flag1      !< flag to mask topography
+
+!
+!-- TKE gradient along x and y
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb, nzt+1
+
+             IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.               &
+                        BTEST( wall_flags_0(k,j,i), 0   )  .AND.               &
+                        BTEST( wall_flags_0(k,j,i+1), 0 ) )                    &
+             THEN
+                de_dx(k,j,i) = 2.0_wp * sgs_wf_part *                          &
+                               ( e(k,j,i+1) - e(k,j,i) ) * ddx
+             ELSEIF ( BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.                 &
+                      BTEST( wall_flags_0(k,j,i), 0   )  .AND.                 &
+                .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                      &
+             THEN
+                de_dx(k,j,i) = 2.0_wp * sgs_wf_part *                          &
+                               ( e(k,j,i) - e(k,j,i-1) ) * ddx
+             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i), 22   )  .AND.          &
+                      .NOT. BTEST( wall_flags_0(k,j,i+1), 22 ) )               &   
+             THEN
+                de_dx(k,j,i) = 0.0_wp
+             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 22 )  .AND.          &
+                      .NOT. BTEST( wall_flags_0(k,j,i), 22   ) )               &
+             THEN
+                de_dx(k,j,i) = 0.0_wp
+             ELSE
+                de_dx(k,j,i) = sgs_wf_part * ( e(k,j,i+1) - e(k,j,i-1) ) * ddx
+             ENDIF
+
+             IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.               &
+                        BTEST( wall_flags_0(k,j,i), 0   )  .AND.               &
+                        BTEST( wall_flags_0(k,j+1,i), 0 ) )                    &
+             THEN
+                de_dy(k,j,i) = 2.0_wp * sgs_wf_part *                          &
+                               ( e(k,j+1,i) - e(k,j,i) ) * ddy
+             ELSEIF ( BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.                 &
+                      BTEST( wall_flags_0(k,j,i), 0   )  .AND.                 &
+                .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                      &
+             THEN
+                de_dy(k,j,i) = 2.0_wp * sgs_wf_part *                          &
+                               ( e(k,j,i) - e(k,j-1,i) ) * ddy
+             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i), 22   )  .AND.          &
+                      .NOT. BTEST( wall_flags_0(k,j+1,i), 22 ) )               &   
+             THEN
+                de_dy(k,j,i) = 0.0_wp
+             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 22 )  .AND.          &
+                      .NOT. BTEST( wall_flags_0(k,j,i), 22   ) )               &
+             THEN
+                de_dy(k,j,i) = 0.0_wp
+             ELSE
+                de_dy(k,j,i) = sgs_wf_part * ( e(k,j+1,i) - e(k,j-1,i) ) * ddy
+             ENDIF
+
+          ENDDO
+       ENDDO
+    ENDDO
+
+!
+!-- TKE gradient along z at topograhy and  including bottom and top boundary conditions
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt-1
+!
+!--          Flag to mask topography
+             flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0  ) )
+
+             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
+                           ( e(k+1,j,i) - e(k-1,j,i) ) / ( zu(k+1) - zu(k-1) ) &
+                                                 * flag1 
+          ENDDO
+!
+!--       upward-facing surfaces
+          DO  m = bc_h(0)%start_index(j,i), bc_h(0)%end_index(j,i)
+             k            = bc_h(0)%k(m)
+             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
+                           ( e(k+1,j,i) - e(k,j,i)   ) / ( zu(k+1) - zu(k) )
+          ENDDO
+!
+!--       downward-facing surfaces
+          DO  m = bc_h(1)%start_index(j,i), bc_h(1)%end_index(j,i)
+             k            = bc_h(1)%k(m)
+             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
+                           ( e(k,j,i) - e(k-1,j,i)   ) / ( zu(k) - zu(k-1) )
+          ENDDO
+
+          de_dz(nzb,j,i)   = 0.0_wp
+          de_dz(nzt,j,i)   = 0.0_wp
+          de_dz(nzt+1,j,i) = 0.0_wp
+       ENDDO
+    ENDDO
+!
+!-- Ghost point exchange
+    CALL exchange_horiz( de_dx, nbgp )
+    CALL exchange_horiz( de_dy, nbgp )
+    CALL exchange_horiz( de_dz, nbgp )
+    CALL exchange_horiz( diss, nbgp  )
+!
+!-- Set boundary conditions at non-periodic boundaries. Note, at non-period
+!-- boundaries zero-gradient boundary conditions are set for the subgrid TKE.
+!-- Thus, TKE gradients normal to the respective lateral boundaries are zero, 
+!-- while tangetial TKE gradients then must be the same as within the prognostic
+!-- domain.  
+    IF ( bc_dirichlet_l )  THEN
+       de_dx(:,:,-1) = 0.0_wp
+       de_dy(:,:,-1) = de_dy(:,:,0) 
+       de_dz(:,:,-1) = de_dz(:,:,0)
+    ENDIF
+    IF ( bc_dirichlet_r )  THEN
+       de_dx(:,:,nxr+1) = 0.0_wp
+       de_dy(:,:,nxr+1) = de_dy(:,:,nxr) 
+       de_dz(:,:,nxr+1) = de_dz(:,:,nxr)
+    ENDIF
+    IF ( bc_dirichlet_n )  THEN
+       de_dx(:,nyn+1,:) = de_dx(:,nyn,:)
+       de_dy(:,nyn+1,:) = 0.0_wp 
+       de_dz(:,nyn+1,:) = de_dz(:,nyn,:)
+    ENDIF
+    IF ( bc_dirichlet_s )  THEN
+       de_dx(:,nys-1,:) = de_dx(:,nys,:)
+       de_dy(:,nys-1,:) = 0.0_wp 
+       de_dz(:,nys-1,:) = de_dz(:,nys,:)
+    ENDIF  
+!
+!-- Calculate the horizontally averaged profiles of SGS TKE and resolved
+!-- velocity variances (they may have been already calculated in routine
+!-- flow_statistics).
+    IF ( .NOT. flow_statistics_called )  THEN
+
+!
+!--    First calculate horizontally averaged profiles of the horizontal
+!--    velocities.
+       sums_l(:,1,0) = 0.0_wp
+       sums_l(:,2,0) = 0.0_wp
+
+       DO  i = nxl, nxr
+          DO  j =  nys, nyn
+             DO  k = nzb, nzt+1
+!
+!--             Flag indicating vicinity of wall
+                flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 24 ) )
+
+                sums_l(k,1,0)  = sums_l(k,1,0)  + u(k,j,i) * flag1
+                sums_l(k,2,0)  = sums_l(k,2,0)  + v(k,j,i) * flag1
+             ENDDO
+          ENDDO
+       ENDDO
+
+#if defined( __parallel )
+!
+!--    Compute total sum from local sums
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( sums_l(nzb,1,0), sums(nzb,1), nzt+2-nzb, &
+                           MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( sums_l(nzb,2,0), sums(nzb,2), nzt+2-nzb, &
+                              MPI_REAL, MPI_SUM, comm2d, ierr )
+#else
+       sums(:,1) = sums_l(:,1,0)
+       sums(:,2) = sums_l(:,2,0)
+#endif
+
+!
+!--    Final values are obtained by division by the total number of grid
+!--    points used for the summation.
+       hom(:,1,1,0) = sums(:,1) / ngp_2dh_outer(:,0)   ! u
+       hom(:,1,2,0) = sums(:,2) / ngp_2dh_outer(:,0)   ! v
+
+!
+!--    Now calculate the profiles of SGS TKE and the resolved-scale
+!--    velocity variances
+       sums_l(:,8,0)  = 0.0_wp
+       sums_l(:,30,0) = 0.0_wp
+       sums_l(:,31,0) = 0.0_wp
+       sums_l(:,32,0) = 0.0_wp
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb, nzt+1
+!
+!--             Flag indicating vicinity of wall
+                flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 24 ) )
+
+                sums_l(k,8,0)  = sums_l(k,8,0)  + e(k,j,i)                       * flag1
+                sums_l(k,30,0) = sums_l(k,30,0) + ( u(k,j,i) - hom(k,1,1,0) )**2 * flag1
+                sums_l(k,31,0) = sums_l(k,31,0) + ( v(k,j,i) - hom(k,1,2,0) )**2 * flag1
+                sums_l(k,32,0) = sums_l(k,32,0) + w(k,j,i)**2                    * flag1
+             ENDDO
+          ENDDO
+       ENDDO
+
+#if defined( __parallel )
+!
+!--    Compute total sum from local sums
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( sums_l(nzb,8,0), sums(nzb,8), nzt+2-nzb, &
+                           MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( sums_l(nzb,30,0), sums(nzb,30), nzt+2-nzb, &
+                           MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( sums_l(nzb,31,0), sums(nzb,31), nzt+2-nzb, &
+                           MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( sums_l(nzb,32,0), sums(nzb,32), nzt+2-nzb, &
+                           MPI_REAL, MPI_SUM, comm2d, ierr )
+
+#else
+       sums(:,8)  = sums_l(:,8,0)
+       sums(:,30) = sums_l(:,30,0)
+       sums(:,31) = sums_l(:,31,0)
+       sums(:,32) = sums_l(:,32,0)
+#endif
+
+!
+!--    Final values are obtained by division by the total number of grid
+!--    points used for the summation.
+       hom(:,1,8,0)  = sums(:,8)  / ngp_2dh_outer(:,0)   ! e
+       hom(:,1,30,0) = sums(:,30) / ngp_2dh_outer(:,0)   ! u*2
+       hom(:,1,31,0) = sums(:,31) / ngp_2dh_outer(:,0)   ! v*2 
+       hom(:,1,32,0) = sums(:,32) / ngp_2dh_outer(:,0)   ! w*2
+
+    ENDIF
+
+ END SUBROUTINE lpm_init_sgs_tke
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Sobroutine control lpm actions, i.e. all actions during one time step. 
+!------------------------------------------------------------------------------!  
+ SUBROUTINE lpm_actions( location )
+
+    CHARACTER (LEN=*), INTENT(IN) ::  location !< call location string
+
+    INTEGER(iwp)       ::  i                  !<
+    INTEGER(iwp)       ::  ie                 !<
+    INTEGER(iwp)       ::  is                 !<
+    INTEGER(iwp)       ::  j                  !<
+    INTEGER(iwp)       ::  je                 !<
+    INTEGER(iwp)       ::  js                 !<
+    INTEGER(iwp), SAVE ::  lpm_count = 0      !<
+    INTEGER(iwp)       ::  k                  !<
+    INTEGER(iwp)       ::  ke                 !<
+    INTEGER(iwp)       ::  ks                 !<
+    INTEGER(iwp)       ::  m                  !<
+    INTEGER(iwp), SAVE ::  steps = 0          !<
+
+    LOGICAL            ::  first_loop_stride  !<
+
+    
+    SELECT CASE ( location )
+
+       CASE ( 'after_prognostic_equations' )
+
+          CALL cpu_log( log_point(25), 'lpm', 'start' )
+!
+!--       Write particle data at current time on file.
+!--       This has to be done here, before particles are further processed,
+!--       because they may be deleted within this timestep (in case that
+!--       dt_write_particle_data = dt_prel = particle_maximum_age).
+          time_write_particle_data = time_write_particle_data + dt_3d
+          IF ( time_write_particle_data >= dt_write_particle_data )  THEN
+
+             CALL lpm_data_output_particles
+!
+!--       The MOD function allows for changes in the output interval with restart
+!--       runs.
+             time_write_particle_data = MOD( time_write_particle_data, &
+                                        MAX( dt_write_particle_data, dt_3d ) )
+          ENDIF
+
+!
+!--       Initialize arrays for marking those particles to be deleted after the
+!--       (sub-) timestep
+          deleted_particles = 0
+
+!
+!--       Initialize variables used for accumulating the number of particles
+!--       xchanged between the subdomains during all sub-timesteps (if sgs
+!--       velocities are included). These data are output further below on the
+!--       particle statistics file.
+          trlp_count_sum      = 0
+          trlp_count_recv_sum = 0
+          trrp_count_sum      = 0
+          trrp_count_recv_sum = 0
+          trsp_count_sum      = 0
+          trsp_count_recv_sum = 0
+          trnp_count_sum      = 0
+          trnp_count_recv_sum = 0
+!
+!--       Calculate exponential term used in case of particle inertia for each
+!--       of the particle groups
+          DO  m = 1, number_of_particle_groups
+             IF ( particle_groups(m)%density_ratio /= 0.0_wp )  THEN
+                particle_groups(m)%exp_arg  =                                        &
+                          4.5_wp * particle_groups(m)%density_ratio *                &
+                          molecular_viscosity / ( particle_groups(m)%radius )**2
+
+                particle_groups(m)%exp_term = EXP( -particle_groups(m)%exp_arg *     &
+                          dt_3d )
+             ENDIF
+          ENDDO
+!
+!--       If necessary, release new set of particles
+          IF ( ( simulated_time - last_particle_release_time ) >= dt_prel  .AND. end_time_prel > simulated_time ) &
+          THEN
+             DO WHILE ( ( simulated_time - last_particle_release_time ) >= dt_prel )
+                CALL lpm_create_particle( PHASE_RELEASE )
+                last_particle_release_time = last_particle_release_time + dt_prel
+             ENDDO
+          ENDIF
+!
+!--       Reset summation arrays
+          IF ( cloud_droplets )  THEN
+             ql_c  = 0.0_wp
+             ql_v  = 0.0_wp
+             ql_vp = 0.0_wp
+          ENDIF
+
+          first_loop_stride = .TRUE.
+          grid_particles(:,:,:)%time_loop_done = .TRUE.
+!
+!--       Timestep loop for particle advection.
+!--       This loop has to be repeated until the advection time of every particle
+!--       (within the total domain!) has reached the LES timestep (dt_3d).
+!--       In case of including the SGS velocities, the particle timestep may be
+!--       smaller than the LES timestep (because of the Lagrangian timescale 
+!--       restriction) and particles may require to undergo several particle 
+!--       timesteps, before the LES timestep is reached. Because the number of these 
+!--       particle timesteps to be carried out is unknown at first, these steps are 
+!--       carried out in the following infinite loop with exit condition.
+          DO
+             CALL cpu_log( log_point_s(44), 'lpm_advec', 'start' )
+             CALL cpu_log( log_point_s(44), 'lpm_advec', 'pause' )
+             
+!
+!--          If particle advection includes SGS velocity components, calculate the
+!--          required SGS quantities (i.e. gradients of the TKE, as well as 
+!--          horizontally averaged profiles of the SGS TKE and the resolved-scale 
+!--          velocity variances)
+             IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
+                CALL lpm_init_sgs_tke
+             ENDIF
+!
+!--          In case SGS-particle speed is considered, particles may carry out 
+!--          several particle timesteps. In order to prevent unnecessary 
+!--          treatment of particles that already reached the final time level, 
+!--          particles are sorted into contiguous blocks of finished and 
+!--          not-finished particles, in addition to their already sorting
+!--          according to their sub-boxes. 
+             IF ( .NOT. first_loop_stride  .AND.  use_sgs_for_particles )            &
+                CALL lpm_sort_timeloop_done
+
+             DO  i = nxl, nxr
+                DO  j = nys, nyn
+                   DO  k = nzb+1, nzt
+
+                      number_of_particles = prt_count(k,j,i)
+!
+!--                   If grid cell gets empty, flag must be true
+                      IF ( number_of_particles <= 0 )  THEN
+                         grid_particles(k,j,i)%time_loop_done = .TRUE.
+                         CYCLE
+                      ENDIF
+
+                      IF ( .NOT. first_loop_stride  .AND.  &
+                           grid_particles(k,j,i)%time_loop_done ) CYCLE
+
+                      particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+
+                      particles(1:number_of_particles)%particle_mask = .TRUE.
+!
+!--                   Initialize the variable storing the total time that a particle 
+!--                   has advanced within the timestep procedure
+                      IF ( first_loop_stride )  THEN
+                         particles(1:number_of_particles)%dt_sum = 0.0_wp
+                      ENDIF
+!
+!--                   Particle (droplet) growth by condensation/evaporation and 
+!--                   collision
+                      IF ( cloud_droplets  .AND.  first_loop_stride)  THEN
+!
+!--                      Droplet growth by condensation / evaporation
+                         CALL lpm_droplet_condensation(i,j,k)
+!
+!--                      Particle growth by collision
+                         IF ( collision_kernel /= 'none' )  THEN
+                            CALL lpm_droplet_collision(i,j,k)
+                         ENDIF
+
+                      ENDIF
+!
+!--                   Initialize the switch used for the loop exit condition checked
+!--                   at the end of this loop. If at least one particle has failed to
+!--                   reach the LES timestep, this switch will be set false in 
+!--                   lpm_advec.
+                      dt_3d_reached_l = .TRUE.
+
+!
+!--                   Particle advection
+                      CALL lpm_advec(i,j,k)
+!
+!--                   Particle reflection from walls. Only applied if the particles 
+!--                   are in the vertical range of the topography. (Here, some
+!--                   optimization is still possible.)
+                      IF ( topography /= 'flat' .AND. k < nzb_max + 2 )  THEN
+                         CALL  lpm_boundary_conds( 'walls', i, j, k )
+                      ENDIF
+!
+!--                   User-defined actions after the calculation of the new particle 
+!--                   position
+                      CALL user_lpm_advec(i,j,k)
+!
+!--                   Apply boundary conditions to those particles that have crossed 
+!--                   the top or bottom boundary and delete those particles, which are
+!--                   older than allowed
+                      CALL lpm_boundary_conds( 'bottom/top', i, j, k )
+!
+!---                  If not all particles of the actual grid cell have reached the 
+!--                   LES timestep, this cell has to do another loop iteration. Due to
+!--                   the fact that particles can move into neighboring grid cells, 
+!--                   these neighbor cells also have to perform another loop iteration.
+!--                   Please note, this realization does not work properly if 
+!--                   particles move into another subdomain. 
+                      IF ( .NOT. dt_3d_reached_l )  THEN
+                         ks = MAX(nzb+1,k-1)
+                         ke = MIN(nzt,k+1)
+                         js = MAX(nys,j-1)
+                         je = MIN(nyn,j+1)
+                         is = MAX(nxl,i-1)
+                         ie = MIN(nxr,i+1)
+                         grid_particles(ks:ke,js:je,is:ie)%time_loop_done = .FALSE.
+                      ELSE
+                         grid_particles(k,j,i)%time_loop_done = .TRUE.
+                      ENDIF
+
+                   ENDDO
+                ENDDO
+             ENDDO
+
+             steps = steps + 1
+             dt_3d_reached_l = ALL(grid_particles(:,:,:)%time_loop_done)
+!
+!--          Find out, if all particles on every PE have completed the LES timestep
+!--          and set the switch corespondingly
+#if defined( __parallel )
+             IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+             CALL MPI_ALLREDUCE( dt_3d_reached_l, dt_3d_reached, 1, MPI_LOGICAL, &
+                                 MPI_LAND, comm2d, ierr )
+#else
+             dt_3d_reached = dt_3d_reached_l
+#endif
+
+             CALL cpu_log( log_point_s(44), 'lpm_advec', 'stop' )
+
+!
+!--          Apply splitting and merging algorithm
+             IF ( cloud_droplets )  THEN
+                IF ( splitting ) THEN
+                   CALL lpm_splitting
+                ENDIF
+                IF ( merging ) THEN
+                   CALL lpm_merging
+                ENDIF
+             ENDIF
+!
+!--          Move Particles local to PE to a different grid cell
+             CALL lpm_move_particle
+!
+!--          Horizontal boundary conditions including exchange between subdmains
+             CALL lpm_exchange_horiz
+
+!
+!--          IF .FALSE., lpm_sort_in_subboxes is done inside pcmp
+             IF ( .NOT. dt_3d_reached .OR. .NOT. nested_run )   THEN   
+!
+!--             Pack particles (eliminate those marked for deletion),
+!--             determine new number of particles
+                CALL lpm_sort_in_subboxes
+!
+!--             Initialize variables for the next (sub-) timestep, i.e., for marking
+!--             those particles to be deleted after the timestep
+                deleted_particles = 0
+             ENDIF
+
+             IF ( dt_3d_reached )  EXIT
+
+             first_loop_stride = .FALSE.
+          ENDDO   ! timestep loop
+!    
+!--       in case of nested runs do the transfer of particles after every full model time step
+          IF ( nested_run )   THEN
+             CALL particles_from_parent_to_child
+             CALL particles_from_child_to_parent
+             CALL pmcp_p_delete_particles_in_fine_grid_area
+
+             CALL lpm_sort_in_subboxes
+
+             deleted_particles = 0
+          ENDIF
+
+!
+!--       Calculate the new liquid water content for each grid box
+          IF ( cloud_droplets )  CALL lpm_calc_liquid_water_content
+          IF ( cloud_droplets )  CALL test_sub
+
+!
+!--       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
+
+!
+!--       Write particle statistics (in particular the number of particles
+!--       exchanged between the subdomains) on file
+          IF ( write_particle_statistics )  CALL lpm_write_exchange_statistics
+
+          CALL cpu_log( log_point(25), 'lpm', 'stop' )
+          
+! !
+! !--       Output of particle time series
+!           IF ( particle_advection )  THEN
+!              IF ( time_dopts >= dt_dopts  .OR.                                                        &
+!                   ( time_since_reference_point >= particle_advection_start  .AND.                     &
+!                    first_call_lpm ) )  THEN
+!                 CALL lpm_data_output_ptseries
+!                 time_dopts = MOD( time_dopts, MAX( dt_dopts, dt_3d ) )
+!              ENDIF
+!           ENDIF
+    
+       CASE DEFAULT
+          CONTINUE
+
+    END SELECT
+
+ END SUBROUTINE lpm_actions
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!
+!------------------------------------------------------------------------------!
+ SUBROUTINE particles_from_parent_to_child
+    IMPLICIT NONE
+
+    CALL pmcp_c_get_particle_from_parent                         ! Child actions
+    CALL pmcp_p_fill_particle_win                                ! Parent actions
+
+    RETURN
+ END SUBROUTINE particles_from_parent_to_child
+
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!
+!------------------------------------------------------------------------------!
+ SUBROUTINE particles_from_child_to_parent
+    IMPLICIT NONE
+
+    CALL pmcp_c_send_particle_to_parent                         ! Child actions
+    CALL pmcp_p_empty_particle_win                              ! Parent actions
+
+    RETURN
+ END SUBROUTINE particles_from_child_to_parent
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine write exchange statistics of the lpm in a ascii file. 
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_write_exchange_statistics
+
+    INTEGER(iwp) :: ip         !<
+    INTEGER(iwp) :: jp         !<
+    INTEGER(iwp) :: kp         !<
+    INTEGER(iwp) :: tot_number_of_particles
+
+!
+!-- Determine the current number of particles
+    number_of_particles         = 0
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles = number_of_particles                         &
+                                     + prt_count(kp,jp,ip)
+          ENDDO
+       ENDDO
+    ENDDO
+
+    CALL check_open( 80 )
+#if defined( __parallel )
+    WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
+                        number_of_particles, pleft, trlp_count_sum,      &
+                        trlp_count_recv_sum, pright, trrp_count_sum,     &
+                        trrp_count_recv_sum, psouth, trsp_count_sum,     &
+                        trsp_count_recv_sum, pnorth, trnp_count_sum,     &
+                        trnp_count_recv_sum
+#else
+    WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
+                        number_of_particles
+#endif
+    CALL close_file( 80 )
+
+    IF ( number_of_particles > 0 ) THEN
+        WRITE(9,*) 'number_of_particles ', number_of_particles,                &
+                    current_timestep_number + 1, simulated_time + dt_3d
+    ENDIF
+
+#if defined( __parallel )
+    CALL MPI_ALLREDUCE( number_of_particles, tot_number_of_particles, 1,       &
+                        MPI_INTEGER, MPI_SUM, comm2d, ierr )
+#else
+    tot_number_of_particles = number_of_particles
+#endif
+
+    IF ( nested_run )  THEN
+       CALL pmcp_g_print_number_of_particles( simulated_time+dt_3d,            &
+                                              tot_number_of_particles)
+    ENDIF
+
+!
+!-- Formats
+8000 FORMAT (I6,1X,F7.2,4X,I10,5X,4(I3,1X,I4,'/',I4,2X),6X,I10)
+
+
+ END SUBROUTINE lpm_write_exchange_statistics
+ 
+
+!------------------------------------------------------------------------------! 
+! Description:
+! ------------
+!> Write particle data in FORTRAN binary and/or netCDF format 
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_data_output_particles
+ 
+    INTEGER(iwp) ::  ip !<
+    INTEGER(iwp) ::  jp !<
+    INTEGER(iwp) ::  kp !<
+
+    CALL cpu_log( log_point_s(40), 'lpm_data_output', 'start' )
+
+!
+!-- Attention: change version number for unit 85 (in routine check_open)
+!--            whenever the output format for this unit is changed!
+    CALL check_open( 85 )
+
+    WRITE ( 85 )  simulated_time
+    WRITE ( 85 )  prt_count
+          
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+             IF ( number_of_particles <= 0 )  CYCLE
+             WRITE ( 85 )  particles
+          ENDDO
+       ENDDO
+    ENDDO
+
+    CALL close_file( 85 )
+
+
+#if defined( __netcdf )
+! !
+! !-- Output in netCDF format
+!     CALL check_open( 108 )
+! 
+! !
+! !-- Update the NetCDF time axis
+!     prt_time_count = prt_time_count + 1
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_time_prt, &
+!                             (/ simulated_time /),        &
+!                             start = (/ prt_time_count /), count = (/ 1 /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 1 )
+! 
+! !
+! !-- Output the real number of particles used
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_rnop_prt, &
+!                             (/ number_of_particles /),   &
+!                             start = (/ prt_time_count /), count = (/ 1 /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 2 )
+! 
+! !
+! !-- Output all particle attributes
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(1), particles%age,      &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 3 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(2), particles%user,     &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 4 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(3), particles%origin_x, &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 5 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(4), particles%origin_y, &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 6 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(5), particles%origin_z, &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 7 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(6), particles%radius,   &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 8 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(7), particles%speed_x,  &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 9 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(8), particles%speed_y,  &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 10 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(9), particles%speed_z,  &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 11 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt,id_var_prt(10),                     &
+!                             particles%weight_factor,                       &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 12 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(11), particles%x,       &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 13 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(12), particles%y,       &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 14 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(13), particles%z,       &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 15 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(14), particles%class,   &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 16 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(15), particles%group,   &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 17 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(16),                    &
+!                             particles%id2,                                 &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 18 )
+! 
+!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(17), particles%id1,     &
+!                             start = (/ 1, prt_time_count /),               &
+!                             count = (/ maximum_number_of_particles /) )
+!     CALL netcdf_handle_error( 'lpm_data_output_particles', 19 )
+! 
+#endif
+
+    CALL cpu_log( log_point_s(40), 'lpm_data_output', 'stop' )
+
+ END SUBROUTINE lpm_data_output_particles
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine calculates and provide particle timeseries output.
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_data_output_ptseries
+ 
+    INTEGER(iwp) ::  i    !<
+    INTEGER(iwp) ::  inum !<
+    INTEGER(iwp) ::  j    !<
+    INTEGER(iwp) ::  jg   !<
+    INTEGER(iwp) ::  k    !<
+    INTEGER(iwp) ::  n    !<
+
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pts_value   !<
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pts_value_l !<
+
+
+    CALL cpu_log( log_point(36), 'data_output_ptseries', 'start' )
+
+    IF ( myid == 0 )  THEN
+!
+!--    Open file for time series output in NetCDF format
+       dopts_time_count = dopts_time_count + 1
+       CALL check_open( 109 )
+#if defined( __netcdf )
+!
+!--    Update the particle time series time axis
+       nc_stat = NF90_PUT_VAR( id_set_pts, id_var_time_pts,      &
+                               (/ time_since_reference_point /), &
+                               start = (/ dopts_time_count /), count = (/ 1 /) )
+       CALL netcdf_handle_error( 'data_output_ptseries', 391 )
+#endif
+
+    ENDIF
+
+    ALLOCATE( pts_value(0:number_of_particle_groups,dopts_num), &
+              pts_value_l(0:number_of_particle_groups,dopts_num) )
+
+    pts_value_l = 0.0_wp
+    pts_value_l(:,16) = 9999999.9_wp    ! for calculation of minimum radius
+
+!
+!-- Calculate or collect the particle time series quantities for all particles
+!-- and seperately for each particle group (if there is more than one group)
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb, nzt
+             number_of_particles = prt_count(k,j,i)
+             IF (number_of_particles <= 0)  CYCLE
+             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+             DO  n = 1, number_of_particles
+
+                IF ( particles(n)%particle_mask )  THEN  ! Restrict analysis to active particles
+
+                   pts_value_l(0,1)  = pts_value_l(0,1) + 1.0_wp  ! total # of particles
+                   pts_value_l(0,2)  = pts_value_l(0,2) +                      &
+                          ( particles(n)%x - particles(n)%origin_x )  ! mean x
+                   pts_value_l(0,3)  = pts_value_l(0,3) +                      &
+                          ( particles(n)%y - particles(n)%origin_y )  ! mean y
+                   pts_value_l(0,4)  = pts_value_l(0,4) +                      &
+                          ( particles(n)%z - particles(n)%origin_z )  ! mean z
+                   pts_value_l(0,5)  = pts_value_l(0,5) + particles(n)%z        ! mean z (absolute)
+                   pts_value_l(0,6)  = pts_value_l(0,6) + particles(n)%speed_x  ! mean u
+                   pts_value_l(0,7)  = pts_value_l(0,7) + particles(n)%speed_y  ! mean v
+                   pts_value_l(0,8)  = pts_value_l(0,8) + particles(n)%speed_z  ! mean w
+                   pts_value_l(0,9)  = pts_value_l(0,9)  + particles(n)%rvar1 ! mean sgsu
+                   pts_value_l(0,10) = pts_value_l(0,10) + particles(n)%rvar2 ! mean sgsv
+                   pts_value_l(0,11) = pts_value_l(0,11) + particles(n)%rvar3 ! mean sgsw
+                   IF ( particles(n)%speed_z > 0.0_wp )  THEN
+                      pts_value_l(0,12) = pts_value_l(0,12) + 1.0_wp  ! # of upward moving prts
+                      pts_value_l(0,13) = pts_value_l(0,13) +                  &
+                                              particles(n)%speed_z ! mean w upw.
+                   ELSE
+                      pts_value_l(0,14) = pts_value_l(0,14) +                  &
+                                              particles(n)%speed_z ! mean w down
+                   ENDIF
+                   pts_value_l(0,15) = pts_value_l(0,15) + particles(n)%radius ! mean rad
+                   pts_value_l(0,16) = MIN( pts_value_l(0,16), particles(n)%radius ) ! minrad
+                   pts_value_l(0,17) = MAX( pts_value_l(0,17), particles(n)%radius ) ! maxrad
+                   pts_value_l(0,18) = pts_value_l(0,18) + 1.0_wp
+                   pts_value_l(0,19) = pts_value_l(0,18) + 1.0_wp
+!
+!--                Repeat the same for the respective particle group
+                   IF ( number_of_particle_groups > 1 )  THEN
+                      jg = particles(n)%group
+
+                      pts_value_l(jg,1)  = pts_value_l(jg,1) + 1.0_wp
+                      pts_value_l(jg,2)  = pts_value_l(jg,2) +                   &
+                           ( particles(n)%x - particles(n)%origin_x )
+                      pts_value_l(jg,3)  = pts_value_l(jg,3) +                   &
+                           ( particles(n)%y - particles(n)%origin_y )
+                      pts_value_l(jg,4)  = pts_value_l(jg,4) +                   &
+                           ( particles(n)%z - particles(n)%origin_z )
+                      pts_value_l(jg,5)  = pts_value_l(jg,5) + particles(n)%z
+                      pts_value_l(jg,6)  = pts_value_l(jg,6) + particles(n)%speed_x
+                      pts_value_l(jg,7)  = pts_value_l(jg,7) + particles(n)%speed_y
+                      pts_value_l(jg,8)  = pts_value_l(jg,8) + particles(n)%speed_z
+                      pts_value_l(jg,9)  = pts_value_l(jg,9)  + particles(n)%rvar1
+                      pts_value_l(jg,10) = pts_value_l(jg,10) + particles(n)%rvar2
+                      pts_value_l(jg,11) = pts_value_l(jg,11) + particles(n)%rvar3
+                      IF ( particles(n)%speed_z > 0.0_wp )  THEN
+                         pts_value_l(jg,12) = pts_value_l(jg,12) + 1.0_wp
+                         pts_value_l(jg,13) = pts_value_l(jg,13) + particles(n)%speed_z
+                      ELSE
+                         pts_value_l(jg,14) = pts_value_l(jg,14) + particles(n)%speed_z
+                      ENDIF
+                      pts_value_l(jg,15) = pts_value_l(jg,15) + particles(n)%radius
+                      pts_value_l(jg,16) = MIN( pts_value(jg,16), particles(n)%radius )
+                      pts_value_l(jg,17) = MAX( pts_value(jg,17), particles(n)%radius )
+                      pts_value_l(jg,18) = pts_value_l(jg,18) + 1.0_wp
+                      pts_value_l(jg,19) = pts_value_l(jg,19) + 1.0_wp
+                   ENDIF
+
+                ENDIF
+
+             ENDDO
+
+          ENDDO
+       ENDDO
+    ENDDO
+
+
+#if defined( __parallel )
+!
+!-- Sum values of the subdomains
+    inum = number_of_particle_groups + 1
+
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( pts_value_l(0,1), pts_value(0,1), 15*inum, MPI_REAL, &
+                        MPI_SUM, comm2d, ierr )
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( pts_value_l(0,16), pts_value(0,16), inum, MPI_REAL, &
+                        MPI_MIN, comm2d, ierr )
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( pts_value_l(0,17), pts_value(0,17), inum, MPI_REAL, &
+                        MPI_MAX, comm2d, ierr )
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( pts_value_l(0,18), pts_value(0,18), inum, MPI_REAL, &
+                        MPI_MAX, comm2d, ierr )
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( pts_value_l(0,19), pts_value(0,19), inum, MPI_REAL, &
+                        MPI_MIN, comm2d, ierr )
+#else
+    pts_value(:,1:19) = pts_value_l(:,1:19)
+#endif
+
+!
+!-- Normalize the above calculated quantities (except min/max values) with the
+!-- total number of particles
+    IF ( number_of_particle_groups > 1 )  THEN
+       inum = number_of_particle_groups
+    ELSE
+       inum = 0
+    ENDIF
+
+    DO  j = 0, inum
+
+       IF ( pts_value(j,1) > 0.0_wp )  THEN
+
+          pts_value(j,2:15) = pts_value(j,2:15) / pts_value(j,1)
+          IF ( pts_value(j,12) > 0.0_wp  .AND.  pts_value(j,12) < 1.0_wp )  THEN
+             pts_value(j,13) = pts_value(j,13) / pts_value(j,12)
+             pts_value(j,14) = pts_value(j,14) / ( 1.0_wp - pts_value(j,12) )
+          ELSEIF ( pts_value(j,12) == 0.0_wp )  THEN
+             pts_value(j,13) = -1.0_wp
+          ELSE
+             pts_value(j,14) = -1.0_wp
+          ENDIF
+
+       ENDIF
+
+    ENDDO
+
+!
+!-- Calculate higher order moments of particle time series quantities,
+!-- seperately for each particle group (if there is more than one group)
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb, nzt
+             number_of_particles = prt_count(k,j,i)
+             IF (number_of_particles <= 0)  CYCLE
+             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+             DO  n = 1, number_of_particles
+
+                pts_value_l(0,20) = pts_value_l(0,20) + ( particles(n)%x - &
+                                    particles(n)%origin_x - pts_value(0,2) )**2 ! x*2
+                pts_value_l(0,21) = pts_value_l(0,21) + ( particles(n)%y - &
+                                    particles(n)%origin_y - pts_value(0,3) )**2 ! y*2
+                pts_value_l(0,22) = pts_value_l(0,22) + ( particles(n)%z - &
+                                    particles(n)%origin_z - pts_value(0,4) )**2 ! z*2
+                pts_value_l(0,23) = pts_value_l(0,23) + ( particles(n)%speed_x - &
+                                                         pts_value(0,6) )**2   ! u*2
+                pts_value_l(0,24) = pts_value_l(0,24) + ( particles(n)%speed_y - &
+                                                          pts_value(0,7) )**2   ! v*2
+                pts_value_l(0,25) = pts_value_l(0,25) + ( particles(n)%speed_z - &
+                                                          pts_value(0,8) )**2   ! w*2
+                pts_value_l(0,26) = pts_value_l(0,26) + ( particles(n)%rvar1 - &
+                                                          pts_value(0,9) )**2   ! u"2
+                pts_value_l(0,27) = pts_value_l(0,27) + ( particles(n)%rvar2 - &
+                                                          pts_value(0,10) )**2  ! v"2
+                pts_value_l(0,28) = pts_value_l(0,28) + ( particles(n)%rvar3 - &
+                                                          pts_value(0,11) )**2  ! w"2
+!
+!--             Repeat the same for the respective particle group
+                IF ( number_of_particle_groups > 1 )  THEN
+                   jg = particles(n)%group
+
+                   pts_value_l(jg,20) = pts_value_l(jg,20) + ( particles(n)%x - &
+                                       particles(n)%origin_x - pts_value(jg,2) )**2
+                   pts_value_l(jg,21) = pts_value_l(jg,21) + ( particles(n)%y - &
+                                       particles(n)%origin_y - pts_value(jg,3) )**2
+                   pts_value_l(jg,22) = pts_value_l(jg,22) + ( particles(n)%z - &
+                                       particles(n)%origin_z - pts_value(jg,4) )**2
+                   pts_value_l(jg,23) = pts_value_l(jg,23) + ( particles(n)%speed_x - &
+                                                             pts_value(jg,6) )**2
+                   pts_value_l(jg,24) = pts_value_l(jg,24) + ( particles(n)%speed_y - &
+                                                             pts_value(jg,7) )**2
+                   pts_value_l(jg,25) = pts_value_l(jg,25) + ( particles(n)%speed_z - &
+                                                             pts_value(jg,8) )**2
+                   pts_value_l(jg,26) = pts_value_l(jg,26) + ( particles(n)%rvar1 - &
+                                                             pts_value(jg,9) )**2
+                   pts_value_l(jg,27) = pts_value_l(jg,27) + ( particles(n)%rvar2 - &
+                                                             pts_value(jg,10) )**2
+                   pts_value_l(jg,28) = pts_value_l(jg,28) + ( particles(n)%rvar3 - &
+                                                             pts_value(jg,11) )**2
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO
+
+    pts_value_l(0,29) = ( number_of_particles - pts_value(0,1) / numprocs )**2
+                                                 ! variance of particle numbers
+    IF ( number_of_particle_groups > 1 )  THEN
+       DO  j = 1, number_of_particle_groups
+          pts_value_l(j,29) = ( pts_value_l(j,1) - &
+                                pts_value(j,1) / numprocs )**2
+       ENDDO
+    ENDIF
+
+#if defined( __parallel )
+!
+!-- Sum values of the subdomains
+    inum = number_of_particle_groups + 1
+
+    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+    CALL MPI_ALLREDUCE( pts_value_l(0,20), pts_value(0,20), inum*10, MPI_REAL, &
+                        MPI_SUM, comm2d, ierr )
+#else
+    pts_value(:,20:29) = pts_value_l(:,20:29)
+#endif
+
+!
+!-- Normalize the above calculated quantities with the total number of
+!-- particles
+    IF ( number_of_particle_groups > 1 )  THEN
+       inum = number_of_particle_groups
+    ELSE
+       inum = 0
+    ENDIF
+
+    DO  j = 0, inum
+
+       IF ( pts_value(j,1) > 0.0_wp )  THEN
+          pts_value(j,20:28) = pts_value(j,20:28) / pts_value(j,1)
+       ENDIF
+       pts_value(j,29) = pts_value(j,29) / numprocs
+
+    ENDDO
+
+#if defined( __netcdf )
+!
+!-- Output particle time series quantities in NetCDF format
+    IF ( myid == 0 )  THEN
+       DO  j = 0, inum
+          DO  i = 1, dopts_num
+             nc_stat = NF90_PUT_VAR( id_set_pts, id_var_dopts(i,j),  &
+                                     (/ pts_value(j,i) /),           &
+                                     start = (/ dopts_time_count /), &
+                                     count = (/ 1 /) )
+             CALL netcdf_handle_error( 'data_output_ptseries', 392 )
+          ENDDO
+       ENDDO
+    ENDIF
+#endif
+
+    DEALLOCATE( pts_value, pts_value_l )
+
+    CALL cpu_log( log_point(36), 'data_output_ptseries', 'stop' )
+
+END SUBROUTINE lpm_data_output_ptseries
+
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine reads the respective restart data for the lpm.
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_rrd_local_particles
+
+    CHARACTER (LEN=10) ::  particle_binary_version    !<
+    CHARACTER (LEN=10) ::  version_on_file            !<
+
+    INTEGER(iwp) :: alloc_size !<
+    INTEGER(iwp) :: ip         !<
+    INTEGER(iwp) :: jp         !<
+    INTEGER(iwp) :: kp         !<
+
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE :: tmp_particles !<
+
+!
+!-- Read particle data from previous model run.
+!-- First open the input unit.
+    IF ( myid_char == '' )  THEN
+       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN'//myid_char,                  &
+                  FORM='UNFORMATTED' )
+    ELSE
+       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN/'//myid_char,                 &
+                  FORM='UNFORMATTED' )
+    ENDIF
+
+!
+!-- First compare the version numbers
+    READ ( 90 )  version_on_file
+    particle_binary_version = '4.0'
+    IF ( TRIM( version_on_file ) /= TRIM( particle_binary_version ) )  THEN
+       message_string = 'version mismatch concerning data from prior ' //      &
+                        'run &version on file = "' //                          &
+                                      TRIM( version_on_file ) //               &
+                        '&version in program = "' //                           &
+                                      TRIM( particle_binary_version ) // '"'
+       CALL message( 'lpm_read_restart_file', 'PA0214', 1, 2, 0, 6, 0 )
+    ENDIF
+
+!
+!-- If less particles are stored on the restart file than prescribed by
+!-- min_nr_particle, the remainder is initialized by zero_particle to avoid
+!-- errors.
+    zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
+                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
+                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
+                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
+                                   0, 0, 0_idp, .FALSE., -1 )
+!
+!-- Read some particle parameters and the size of the particle arrays,
+!-- allocate them and read their contents.
+    READ ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                     &
+                 last_particle_release_time, number_of_particle_groups,        &
+                 particle_groups, time_write_particle_data
+
+    ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                        &
+              grid_particles(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+
+    READ ( 90 )  prt_count
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles > 0 )  THEN
+                alloc_size = MAX( INT( number_of_particles *                   &
+                             ( 1.0_wp + alloc_factor / 100.0_wp ) ),           &
+                             min_nr_particle )
+             ELSE
+                alloc_size = min_nr_particle
+             ENDIF
+
+             ALLOCATE( grid_particles(kp,jp,ip)%particles(1:alloc_size) )
+
+             IF ( number_of_particles > 0 )  THEN
+                ALLOCATE( tmp_particles(1:number_of_particles) )
+                READ ( 90 )  tmp_particles
+                grid_particles(kp,jp,ip)%particles(1:number_of_particles) = tmp_particles
+                DEALLOCATE( tmp_particles )
+                IF ( number_of_particles < alloc_size )  THEN
+                   grid_particles(kp,jp,ip)%particles(number_of_particles+1:alloc_size) &
+                      = zero_particle
+                ENDIF
+             ELSE
+                grid_particles(kp,jp,ip)%particles(1:alloc_size) = zero_particle
+             ENDIF
+
+          ENDDO
+       ENDDO
+    ENDDO
+
+    CLOSE ( 90 )
+!
+!-- Must be called to sort particles into blocks, which is needed for a fast
+!-- interpolation of the LES fields on the particle position.
+    CALL lpm_sort_in_subboxes
+       
+
+ END SUBROUTINE lpm_rrd_local_particles
+ 
+ 
+ SUBROUTINE lpm_rrd_local( k, nxlf, nxlc, nxl_on_file, nxrf, nxrc,          &
+                              nxr_on_file, nynf, nync, nyn_on_file, nysf,  &
+                              nysc, nys_on_file, tmp_3d, found )
+                              
+       
+   USE control_parameters,                                                 &
+       ONLY: length, restart_string           
+
+    INTEGER(iwp) ::  k               !<
+    INTEGER(iwp) ::  nxlc            !<
+    INTEGER(iwp) ::  nxlf            !<
+    INTEGER(iwp) ::  nxl_on_file     !<
+    INTEGER(iwp) ::  nxrc            !<
+    INTEGER(iwp) ::  nxrf            !<
+    INTEGER(iwp) ::  nxr_on_file     !<
+    INTEGER(iwp) ::  nync            !<
+    INTEGER(iwp) ::  nynf            !<
+    INTEGER(iwp) ::  nyn_on_file     !<
+    INTEGER(iwp) ::  nysc            !<
+    INTEGER(iwp) ::  nysf            !<
+    INTEGER(iwp) ::  nys_on_file     !<
+
+    LOGICAL, INTENT(OUT)  ::  found
+
+    REAL(wp), DIMENSION(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp) :: tmp_3d   !<
+
+    
+    found = .TRUE.
+
+    SELECT CASE ( restart_string(1:length) )
+    
+       CASE ( 'iran' ) ! matching random numbers is still unresolved issue
+          IF ( k == 1 )  READ ( 13 )  iran, iran_part
+          
+        CASE ( 'pc_av' )
+           IF ( .NOT. ALLOCATED( pc_av ) )  THEN
+              ALLOCATE( pc_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+           ENDIF
+           IF ( k == 1 )  READ ( 13 )  tmp_3d
+           pc_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =          &
+              tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
+
+        CASE ( 'pr_av' )
+           IF ( .NOT. ALLOCATED( pr_av ) )  THEN
+              ALLOCATE( pr_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+           ENDIF
+           IF ( k == 1 )  READ ( 13 )  tmp_3d
+           pr_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =          &
+              tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
+ 
+         CASE ( 'ql_c_av' )
+            IF ( .NOT. ALLOCATED( ql_c_av ) )  THEN
+               ALLOCATE( ql_c_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+            ENDIF
+            IF ( k == 1 )  READ ( 13 )  tmp_3d
+            ql_c_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =        &
+               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
+
+         CASE ( 'ql_v_av' )
+            IF ( .NOT. ALLOCATED( ql_v_av ) )  THEN
+               ALLOCATE( ql_v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+            ENDIF
+            IF ( k == 1 )  READ ( 13 )  tmp_3d
+            ql_v_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =        &
+               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
+
+         CASE ( 'ql_vp_av' )
+            IF ( .NOT. ALLOCATED( ql_vp_av ) )  THEN
+               ALLOCATE( ql_vp_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
+            ENDIF
+            IF ( k == 1 )  READ ( 13 )  tmp_3d
+            ql_vp_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =       &
+               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
+
+          CASE DEFAULT
+
+             found = .FALSE.
+
+       END SELECT
+               
+
+ END SUBROUTINE lpm_rrd_local
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine writes the respective restart data for the lpm.
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_wrd_local
+ 
+    CHARACTER (LEN=10) ::  particle_binary_version   !<
+
+    INTEGER(iwp) ::  ip                              !<
+    INTEGER(iwp) ::  jp                              !<
+    INTEGER(iwp) ::  kp                              !< 
+!
+!-- First open the output unit.
+    IF ( myid_char == '' )  THEN
+       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT'//myid_char, &
+                  FORM='UNFORMATTED')
+    ELSE
+       IF ( myid == 0 )  CALL local_system( 'mkdir PARTICLE_RESTART_DATA_OUT' )
+#if defined( __parallel )
+!
+!--    Set a barrier in order to allow that thereafter all other processors
+!--    in the directory created by PE0 can open their file
+       CALL MPI_BARRIER( comm2d, ierr )
+#endif
+       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT/'//myid_char, &
+                  FORM='UNFORMATTED' )
+    ENDIF
+
+!
+!-- Write the version number of the binary format.
+!-- Attention: After changes to the following output commands the version
+!-- ---------  number of the variable particle_binary_version must be
+!--            changed! Also, the version number and the list of arrays
+!--            to be read in lpm_read_restart_file must be adjusted
+!--            accordingly.
+    particle_binary_version = '4.0'
+    WRITE ( 90 )  particle_binary_version
+
+!
+!-- Write some particle parameters, the size of the particle arrays
+    WRITE ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                    &
+                  last_particle_release_time, number_of_particle_groups,       &
+                  particle_groups, time_write_particle_data
+
+    WRITE ( 90 )  prt_count
+          
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+             IF ( number_of_particles <= 0 )  CYCLE
+             WRITE ( 90 )  particles
+          ENDDO
+       ENDDO
+    ENDDO
+
+    CLOSE ( 90 )
+
+#if defined( __parallel )
+       CALL MPI_BARRIER( comm2d, ierr )
+#endif
+
+    CALL wrd_write_string( 'iran' ) 
+    WRITE ( 14 )  iran, iran_part 
+
+
+ END SUBROUTINE lpm_wrd_local
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine writes the respective restart data for the lpm.
+!------------------------------------------------------------------------------! 
+ SUBROUTINE lpm_wrd_global
+ 
+    CALL wrd_write_string( 'curvature_solution_effects' ) 
+    WRITE ( 14 )  curvature_solution_effects
+
+ END SUBROUTINE lpm_wrd_global
+ 
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This routine writes the respective restart data for the lpm.
+!------------------------------------------------------------------------------! 
+ SUBROUTINE lpm_rrd_global( found )
+ 
+    USE control_parameters,                            &
+        ONLY: length, restart_string
+
+    LOGICAL, INTENT(OUT)  ::  found
+
+    found = .TRUE.
+
+    SELECT CASE ( restart_string(1:length) )
+
+       CASE ( 'curvature_solution_effects' )
+          READ ( 13 )  curvature_solution_effects
+          
+!          CASE ( 'global_paramter' )
+!             READ ( 13 )  global_parameter
+!          CASE ( 'global_array' )
+!             IF ( .NOT. ALLOCATED( global_array ) )  ALLOCATE( global_array(1:10) )
+!             READ ( 13 )  global_array
+
+       CASE DEFAULT
+
+          found = .FALSE.
+
+    END SELECT 
+    
+ END SUBROUTINE lpm_rrd_global
+
+ 
+END MODULE lagrangian_particle_model_mod
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> +++++++TEST SUBMODULE+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+!------------------------------------------------------------------------------!
+SUBMODULE (lagrangian_particle_model_mod) lpm_test_sub
+  
+ CONTAINS
+    
+ MODULE SUBROUTINE test_sub
+     INTEGER ::b
+     b = 5_idp
+ END SUBROUTINE test_sub
+ 
+END SUBMODULE
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This is a submodule of the lagrangian particle model. It contains all 
+!> dynamic processes of the lpm. This includes the advection (resolved and sub-
+!> grid scale) as well as the boundary conditions of particles. As a next step
+!> this submodule should be excluded as an own file. 
+!------------------------------------------------------------------------------!
+SUBMODULE (lagrangian_particle_model_mod) lpm_dynamics
+
+  REAL(wp), PARAMETER ::  c_0 = 3.0_wp         !< parameter for lagrangian timescale
+  
+ CONTAINS
+    
+ MODULE SUBROUTINE lpm_advec (ip,jp,kp)
+
+    LOGICAL ::  subbox_at_wall !< flag to see if the current subgridbox is adjacent to a wall
+
+    INTEGER(iwp) ::  i                           !< index variable along x
+    INTEGER(iwp) ::  ip                          !< index variable along x 
+    INTEGER(iwp) ::  j                           !< index variable along y 
+    INTEGER(iwp) ::  jp                          !< index variable along y 
+    INTEGER(iwp) ::  k                           !< index variable along z 
+    INTEGER(iwp) ::  k_wall                      !< vertical index of topography top 
+    INTEGER(iwp) ::  kp                          !< index variable along z 
+    INTEGER(iwp) ::  kw                          !< index variable along z
+    INTEGER(iwp) ::  n                           !< loop variable over all particles in a grid box
+    INTEGER(iwp) ::  nb                          !< block number particles are sorted in
+    INTEGER(iwp) ::  surf_start                  !< Index on surface data-type for current grid box 
+
+    INTEGER(iwp), DIMENSION(0:7) ::  start_index !< start particle index for current block
+    INTEGER(iwp), DIMENSION(0:7) ::  end_index   !< start particle index for current block
+
+    REAL(wp) ::  aa                 !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  bb                 !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  cc                 !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  d_z_p_z0           !< inverse of interpolation length for logarithmic interpolation 
+    REAL(wp) ::  dd                 !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  de_dx_int_l        !< x/y-interpolated TKE gradient (x) at particle position at lower vertical level
+    REAL(wp) ::  de_dx_int_u        !< x/y-interpolated TKE gradient (x) at particle position at upper vertical level
+    REAL(wp) ::  de_dy_int_l        !< x/y-interpolated TKE gradient (y) at particle position at lower vertical level
+    REAL(wp) ::  de_dy_int_u        !< x/y-interpolated TKE gradient (y) at particle position at upper vertical level
+    REAL(wp) ::  de_dt              !< temporal derivative of TKE experienced by the particle
+    REAL(wp) ::  de_dt_min          !< lower level for temporal TKE derivative
+    REAL(wp) ::  de_dz_int_l        !< x/y-interpolated TKE gradient (z) at particle position at lower vertical level
+    REAL(wp) ::  de_dz_int_u        !< x/y-interpolated TKE gradient (z) at particle position at upper vertical level
+    REAL(wp) ::  diameter           !< diamter of droplet
+    REAL(wp) ::  diss_int_l         !< x/y-interpolated dissipation at particle position at lower vertical level
+    REAL(wp) ::  diss_int_u         !< x/y-interpolated dissipation at particle position at upper vertical level
+    REAL(wp) ::  dt_particle_m      !< previous particle time step
+    REAL(wp) ::  dz_temp            !< dummy for the vertical grid spacing
+    REAL(wp) ::  e_int_l            !< x/y-interpolated TKE at particle position at lower vertical level
+    REAL(wp) ::  e_int_u            !< x/y-interpolated TKE at particle position at upper vertical level
+    REAL(wp) ::  e_mean_int         !< horizontal mean TKE at particle height
+    REAL(wp) ::  exp_arg            !< argument in the exponent - particle radius
+    REAL(wp) ::  exp_term           !< exponent term
+    REAL(wp) ::  gg                 !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  height_p           !< dummy argument for logarithmic interpolation
+    REAL(wp) ::  log_z_z0_int       !< logarithmus used for surface_layer interpolation
+    REAL(wp) ::  random_gauss       !< Gaussian-distributed random number used for SGS particle advection
+    REAL(wp) ::  RL                 !< Lagrangian autocorrelation coefficient
+    REAL(wp) ::  rg1                !< Gaussian distributed random number
+    REAL(wp) ::  rg2                !< Gaussian distributed random number
+    REAL(wp) ::  rg3                !< Gaussian distributed random number
+    REAL(wp) ::  sigma              !< velocity standard deviation
+    REAL(wp) ::  u_int_l            !< x/y-interpolated u-component at particle position at lower vertical level
+    REAL(wp) ::  u_int_u            !< x/y-interpolated u-component at particle position at upper vertical level
+    REAL(wp) ::  us_int             !< friction velocity at particle grid box
+    REAL(wp) ::  usws_int           !< surface momentum flux (u component) at particle grid box
+    REAL(wp) ::  v_int_l            !< x/y-interpolated v-component at particle position at lower vertical level
+    REAL(wp) ::  v_int_u            !< x/y-interpolated v-component at particle position at upper vertical level
+    REAL(wp) ::  vsws_int           !< surface momentum flux (u component) at particle grid box
+    REAL(wp) ::  vv_int             !< dummy to compute interpolated mean SGS TKE, used to scale SGS advection 
+    REAL(wp) ::  w_int_l            !< x/y-interpolated w-component at particle position at lower vertical level
+    REAL(wp) ::  w_int_u            !< x/y-interpolated w-component at particle position at upper vertical level
+    REAL(wp) ::  w_s                !< terminal velocity of droplets
+    REAL(wp) ::  x                  !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  y                  !< dummy argument for horizontal particle interpolation 
+    REAL(wp) ::  z_p                !< surface layer height (0.5 dz)
+
+    REAL(wp), PARAMETER ::  a_rog = 9.65_wp      !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  b_rog = 10.43_wp     !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  c_rog = 0.6_wp       !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp   !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp  !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  d0_rog = 0.745_wp    !< separation diameter
+
+    REAL(wp), DIMENSION(number_of_particles) ::  term_1_2       !< flag to communicate whether a particle is near topography or not
+    REAL(wp), DIMENSION(number_of_particles) ::  dens_ratio     !< ratio between the density of the fluid and the density of the particles 
+    REAL(wp), DIMENSION(number_of_particles) ::  de_dx_int      !< horizontal TKE gradient along x at particle position
+    REAL(wp), DIMENSION(number_of_particles) ::  de_dy_int      !< horizontal TKE gradient along y at particle position
+    REAL(wp), DIMENSION(number_of_particles) ::  de_dz_int      !< horizontal TKE gradient along z at particle position
+    REAL(wp), DIMENSION(number_of_particles) ::  diss_int       !< dissipation at particle position
+    REAL(wp), DIMENSION(number_of_particles) ::  dt_gap         !< remaining time until particle time integration reaches LES time
+    REAL(wp), DIMENSION(number_of_particles) ::  dt_particle    !< particle time step
+    REAL(wp), DIMENSION(number_of_particles) ::  e_int          !< TKE at particle position
+    REAL(wp), DIMENSION(number_of_particles) ::  fs_int         !< weighting factor for subgrid-scale particle speed
+    REAL(wp), DIMENSION(number_of_particles) ::  lagr_timescale !< Lagrangian timescale
+    REAL(wp), DIMENSION(number_of_particles) ::  rvar1_temp     !< SGS particle velocity - u-component
+    REAL(wp), DIMENSION(number_of_particles) ::  rvar2_temp     !< SGS particle velocity - v-component
+    REAL(wp), DIMENSION(number_of_particles) ::  rvar3_temp     !< SGS particle velocity - w-component
+    REAL(wp), DIMENSION(number_of_particles) ::  u_int          !< u-component of particle speed
+    REAL(wp), DIMENSION(number_of_particles) ::  v_int          !< v-component of particle speed 
+    REAL(wp), DIMENSION(number_of_particles) ::  w_int          !< w-component of particle speed
+    REAL(wp), DIMENSION(number_of_particles) ::  xv             !< x-position
+    REAL(wp), DIMENSION(number_of_particles) ::  yv             !< y-position
+    REAL(wp), DIMENSION(number_of_particles) ::  zv             !< z-position
+
+    REAL(wp), DIMENSION(number_of_particles, 3) ::  rg !< vector of Gaussian distributed random numbers 
+
+    CALL cpu_log( log_point_s(44), 'lpm_advec', 'continue' )
+
+!
+!-- Determine height of Prandtl layer and distance between Prandtl-layer
+!-- height and horizontal mean roughness height, which are required for 
+!-- vertical logarithmic interpolation of horizontal particle speeds 
+!-- (for particles below first vertical grid level).
+    z_p      = zu(nzb+1) - zw(nzb)
+    d_z_p_z0 = 1.0_wp / ( z_p - z0_av_global )
+
+    start_index = grid_particles(kp,jp,ip)%start_index
+    end_index   = grid_particles(kp,jp,ip)%end_index
+
+    xv = particles(1:number_of_particles)%x
+    yv = particles(1:number_of_particles)%y
+    zv = particles(1:number_of_particles)%z
+
+    DO  nb = 0, 7
+!
+!--    Interpolate u velocity-component       
+       i = ip
+       j = jp + block_offset(nb)%j_off
+       k = kp + block_offset(nb)%k_off
+
+       DO  n = start_index(nb), end_index(nb)
+!
+!--       Interpolation of the u velocity component onto particle position.  
+!--       Particles are interpolation bi-linearly in the horizontal and a 
+!--       linearly in the vertical. An exception is made for particles below
+!--       the first vertical grid level in case of a prandtl layer. In this 
+!--       case the horizontal particle velocity components are determined using
+!--       Monin-Obukhov relations (if branch). 
+!--       First, check if particle is located below first vertical grid level 
+!--       above topography (Prandtl-layer height)
+!--       Determine vertical index of topography top
+          k_wall = get_topography_top_index_ji( jp, ip, 's' )
+
+          IF ( constant_flux_layer  .AND.  zv(n) - zw(k_wall) < z_p )  THEN
+!
+!--          Resolved-scale horizontal particle velocity is zero below z0. 
+             IF ( zv(n) - zw(k_wall) < z0_av_global )  THEN
+                u_int(n) = 0.0_wp
+             ELSE
+!
+!--             Determine the sublayer. Further used as index.
+                height_p = ( zv(n) - zw(k_wall) - z0_av_global ) &
+                                     * REAL( number_of_sublayers, KIND=wp )    &
+                                     * d_z_p_z0 
+!
+!--             Calculate LOG(z/z0) for exact particle height. Therefore,   
+!--             interpolate linearly between precalculated logarithm. 
+                log_z_z0_int = log_z_z0(INT(height_p))                         &
+                                 + ( height_p - INT(height_p) )                &
+                                 * ( log_z_z0(INT(height_p)+1)                 &
+                                      - log_z_z0(INT(height_p))                &
+                                   ) 
+!
+!--             Get friction velocity and momentum flux from new surface data 
+!--             types.
+                IF ( surf_def_h(0)%start_index(jp,ip) <=                   &
+                     surf_def_h(0)%end_index(jp,ip) )  THEN
+                   surf_start = surf_def_h(0)%start_index(jp,ip)
+!--                Limit friction velocity. In narrow canyons or holes the 
+!--                friction velocity can become very small, resulting in a too
+!--                large particle speed.
+                   us_int    = MAX( surf_def_h(0)%us(surf_start), 0.01_wp )  
+                   usws_int  = surf_def_h(0)%usws(surf_start)
+                ELSEIF ( surf_lsm_h%start_index(jp,ip) <=                  &
+                         surf_lsm_h%end_index(jp,ip) )  THEN
+                   surf_start = surf_lsm_h%start_index(jp,ip)
+                   us_int    = MAX( surf_lsm_h%us(surf_start), 0.01_wp )  
+                   usws_int  = surf_lsm_h%usws(surf_start)
+                ELSEIF ( surf_usm_h%start_index(jp,ip) <=                  &
+                         surf_usm_h%end_index(jp,ip) )  THEN
+                   surf_start = surf_usm_h%start_index(jp,ip)
+                   us_int    = MAX( surf_usm_h%us(surf_start), 0.01_wp )  
+                   usws_int  = surf_usm_h%usws(surf_start)
+                ENDIF
+
+!
+!--             Neutral solution is applied for all situations, e.g. also for 
+!--             unstable and stable situations. Even though this is not exact 
+!--             this saves a lot of CPU time since several calls of intrinsic 
+!--             FORTRAN procedures (LOG, ATAN) are avoided, This is justified 
+!--             as sensitivity studies revealed no significant effect of 
+!--             using the neutral solution also for un/stable situations.
+                u_int(n) = -usws_int / ( us_int * kappa + 1E-10_wp )           & 
+                            * log_z_z0_int - u_gtrans
+                
+             ENDIF
+!
+!--       Particle above the first grid level. Bi-linear interpolation in the 
+!--       horizontal and linear interpolation in the vertical direction.
+          ELSE
+
+             x  = xv(n) - i * dx
+             y  = yv(n) + ( 0.5_wp - j ) * dy
+             aa = x**2          + y**2
+             bb = ( dx - x )**2 + y**2
+             cc = x**2          + ( dy - y )**2
+             dd = ( dx - x )**2 + ( dy - y )**2
+             gg = aa + bb + cc + dd
+
+             u_int_l = ( ( gg - aa ) * u(k,j,i)   + ( gg - bb ) * u(k,j,i+1)   &
+                         + ( gg - cc ) * u(k,j+1,i) + ( gg - dd ) *            &
+                         u(k,j+1,i+1) ) / ( 3.0_wp * gg ) - u_gtrans
+
+             IF ( k == nzt )  THEN
+                u_int(n) = u_int_l
+             ELSE
+                u_int_u = ( ( gg-aa ) * u(k+1,j,i) + ( gg-bb ) * u(k+1,j,i+1)  &
+                            + ( gg-cc ) * u(k+1,j+1,i) + ( gg-dd ) *           &
+                            u(k+1,j+1,i+1) ) / ( 3.0_wp * gg ) - u_gtrans
+                u_int(n) = u_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *               &
+                           ( u_int_u - u_int_l )
+             ENDIF
+
+          ENDIF
+
+       ENDDO
+!
+!--    Same procedure for interpolation of the v velocity-component 
+       i = ip + block_offset(nb)%i_off
+       j = jp
+       k = kp + block_offset(nb)%k_off
+
+       DO  n = start_index(nb), end_index(nb)
+
+!
+!--       Determine vertical index of topography top
+          k_wall = get_topography_top_index_ji( jp,ip, 's' )
+
+          IF ( constant_flux_layer  .AND.  zv(n) - zw(k_wall) < z_p )  THEN
+             IF ( zv(n) - zw(k_wall) < z0_av_global )  THEN
+!
+!--             Resolved-scale horizontal particle velocity is zero below z0. 
+                v_int(n) = 0.0_wp
+             ELSE       
+!
+!--             Determine the sublayer. Further used as index. Please note, 
+!--             logarithmus can not be reused from above, as in in case of
+!--             topography particle on u-grid can be above surface-layer height,
+!--             whereas it can be below on v-grid. 
+                height_p = ( zv(n) - zw(k_wall) - z0_av_global ) &
+                                  * REAL( number_of_sublayers, KIND=wp )       &
+                                  * d_z_p_z0 
+!
+!--             Calculate LOG(z/z0) for exact particle height. Therefore,   
+!--             interpolate linearly between precalculated logarithm. 
+                log_z_z0_int = log_z_z0(INT(height_p))                         &
+                                 + ( height_p - INT(height_p) )                &
+                                 * ( log_z_z0(INT(height_p)+1)                 &
+                                      - log_z_z0(INT(height_p))                &
+                                   ) 
+!
+!--             Get friction velocity and momentum flux from new surface data 
+!--             types.
+                IF ( surf_def_h(0)%start_index(jp,ip) <=                   &
+                     surf_def_h(0)%end_index(jp,ip) )  THEN
+                   surf_start = surf_def_h(0)%start_index(jp,ip)
+!--                Limit friction velocity. In narrow canyons or holes the 
+!--                friction velocity can become very small, resulting in a too
+!--                large particle speed.
+                   us_int    = MAX( surf_def_h(0)%us(surf_start), 0.01_wp )  
+                   vsws_int  = surf_def_h(0)%vsws(surf_start)
+                ELSEIF ( surf_lsm_h%start_index(jp,ip) <=                  &
+                         surf_lsm_h%end_index(jp,ip) )  THEN
+                   surf_start = surf_lsm_h%start_index(jp,ip)
+                   us_int    = MAX( surf_lsm_h%us(surf_start), 0.01_wp )  
+                   vsws_int  = surf_lsm_h%vsws(surf_start)
+                ELSEIF ( surf_usm_h%start_index(jp,ip) <=                  &
+                         surf_usm_h%end_index(jp,ip) )  THEN
+                   surf_start = surf_usm_h%start_index(jp,ip)
+                   us_int    = MAX( surf_usm_h%us(surf_start), 0.01_wp )  
+                   vsws_int  = surf_usm_h%vsws(surf_start)
+                ENDIF 
+!
+!--             Neutral solution is applied for all situations, e.g. also for 
+!--             unstable and stable situations. Even though this is not exact 
+!--             this saves a lot of CPU time since several calls of intrinsic 
+!--             FORTRAN procedures (LOG, ATAN) are avoided, This is justified 
+!--             as sensitivity studies revealed no significant effect of 
+!--             using the neutral solution also for un/stable situations.
+                v_int(n) = -vsws_int / ( us_int * kappa + 1E-10_wp )           &
+                         * log_z_z0_int - v_gtrans
+
+             ENDIF
+
+          ELSE
+             x  = xv(n) + ( 0.5_wp - i ) * dx
+             y  = yv(n) - j * dy
+             aa = x**2          + y**2
+             bb = ( dx - x )**2 + y**2
+             cc = x**2          + ( dy - y )**2
+             dd = ( dx - x )**2 + ( dy - y )**2
+             gg = aa + bb + cc + dd
+
+             v_int_l = ( ( gg - aa ) * v(k,j,i)   + ( gg - bb ) * v(k,j,i+1)   &
+                       + ( gg - cc ) * v(k,j+1,i) + ( gg - dd ) * v(k,j+1,i+1) &
+                       ) / ( 3.0_wp * gg ) - v_gtrans
+
+             IF ( k == nzt )  THEN
+                v_int(n) = v_int_l
+             ELSE
+                v_int_u = ( ( gg-aa ) * v(k+1,j,i)   + ( gg-bb ) * v(k+1,j,i+1)   &
+                          + ( gg-cc ) * v(k+1,j+1,i) + ( gg-dd ) * v(k+1,j+1,i+1) &
+                          ) / ( 3.0_wp * gg ) - v_gtrans
+                v_int(n) = v_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *               &
+                                  ( v_int_u - v_int_l )
+             ENDIF
+
+          ENDIF
+
+       ENDDO
+!
+!--    Same procedure for interpolation of the w velocity-component
+       i = ip + block_offset(nb)%i_off
+       j = jp + block_offset(nb)%j_off
+       k = kp - 1
+
+       DO  n = start_index(nb), end_index(nb)
+
+          IF ( vertical_particle_advection(particles(n)%group) )  THEN
+
+             x  = xv(n) + ( 0.5_wp - i ) * dx
+             y  = yv(n) + ( 0.5_wp - j ) * dy
+             aa = x**2          + y**2
+             bb = ( dx - x )**2 + y**2
+             cc = x**2          + ( dy - y )**2
+             dd = ( dx - x )**2 + ( dy - y )**2
+             gg = aa + bb + cc + dd
+
+             w_int_l = ( ( gg - aa ) * w(k,j,i)   + ( gg - bb ) * w(k,j,i+1)   &
+                       + ( gg - cc ) * w(k,j+1,i) + ( gg - dd ) * w(k,j+1,i+1) &
+                       ) / ( 3.0_wp * gg )
+
+             IF ( k == nzt )  THEN
+                w_int(n) = w_int_l
+             ELSE
+                w_int_u = ( ( gg-aa ) * w(k+1,j,i)   + &
+                            ( gg-bb ) * w(k+1,j,i+1) + &
+                            ( gg-cc ) * w(k+1,j+1,i) + &
+                            ( gg-dd ) * w(k+1,j+1,i+1) &
+                          ) / ( 3.0_wp * gg )
+                w_int(n) = w_int_l + ( zv(n) - zw(k) ) / dzw(k+1) *               &
+                           ( w_int_u - w_int_l )
+             ENDIF
+
+          ELSE
+
+             w_int(n) = 0.0_wp
+
+          ENDIF
+
+       ENDDO
+
+    ENDDO
+
+!-- Interpolate and calculate quantities needed for calculating the SGS
+!-- velocities
+    IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
+    
+       DO  nb = 0,7
+         
+          subbox_at_wall = .FALSE.         
+!
+!--       In case of topography check if subbox is adjacent to a wall
+          IF ( .NOT. topography == 'flat' ) THEN
+             i = ip + MERGE( -1_iwp , 1_iwp, BTEST( nb, 2 ) )
+             j = jp + MERGE( -1_iwp , 1_iwp, BTEST( nb, 1 ) )
+             k = kp + MERGE( -1_iwp , 1_iwp, BTEST( nb, 0 ) )
+             IF ( .NOT. BTEST(wall_flags_0(k,  jp, ip), 0) .OR.                &
+                  .NOT. BTEST(wall_flags_0(kp, j,  ip), 0) .OR.                &
+                  .NOT. BTEST(wall_flags_0(kp, jp, i ), 0) )                   &
+             THEN
+                subbox_at_wall = .TRUE.
+             ENDIF
+          ENDIF
+          IF ( subbox_at_wall ) THEN
+             e_int(start_index(nb):end_index(nb))     = e(kp,jp,ip) 
+             diss_int(start_index(nb):end_index(nb))  = diss(kp,jp,ip)
+             de_dx_int(start_index(nb):end_index(nb)) = de_dx(kp,jp,ip)
+             de_dy_int(start_index(nb):end_index(nb)) = de_dy(kp,jp,ip)
+             de_dz_int(start_index(nb):end_index(nb)) = de_dz(kp,jp,ip)
+!
+!--          Set flag for stochastic equation.
+             term_1_2(start_index(nb):end_index(nb)) = 0.0_wp             
+          ELSE
+             i = ip + block_offset(nb)%i_off
+             j = jp + block_offset(nb)%j_off
+             k = kp + block_offset(nb)%k_off
+
+             DO  n = start_index(nb), end_index(nb)
+!
+!--             Interpolate TKE
+                x  = xv(n) + ( 0.5_wp - i ) * dx
+                y  = yv(n) + ( 0.5_wp - j ) * dy
+                aa = x**2          + y**2
+                bb = ( dx - x )**2 + y**2
+                cc = x**2          + ( dy - y )**2
+                dd = ( dx - x )**2 + ( dy - y )**2
+                gg = aa + bb + cc + dd
+
+                e_int_l = ( ( gg-aa ) * e(k,j,i)   + ( gg-bb ) * e(k,j,i+1)   &
+                          + ( gg-cc ) * e(k,j+1,i) + ( gg-dd ) * e(k,j+1,i+1) &
+                          ) / ( 3.0_wp * gg )
+
+                IF ( k+1 == nzt+1 )  THEN
+                   e_int(n) = e_int_l
+                ELSE
+                   e_int_u = ( ( gg - aa ) * e(k+1,j,i)   + &
+                               ( gg - bb ) * e(k+1,j,i+1) + &
+                               ( gg - cc ) * e(k+1,j+1,i) + &
+                               ( gg - dd ) * e(k+1,j+1,i+1) &
+                            ) / ( 3.0_wp * gg )
+                   e_int(n) = e_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *            &
+                                     ( e_int_u - e_int_l )
+                ENDIF
+!
+!--             Needed to avoid NaN particle velocities (this might not be
+!--             required any more)
+                IF ( e_int(n) <= 0.0_wp )  THEN
+                   e_int(n) = 1.0E-20_wp
+                ENDIF
+!
+!--             Interpolate the TKE gradient along x (adopt incides i,j,k and
+!--             all position variables from above (TKE))
+                de_dx_int_l = ( ( gg - aa ) * de_dx(k,j,i)   + &
+                                ( gg - bb ) * de_dx(k,j,i+1) + &
+                                ( gg - cc ) * de_dx(k,j+1,i) + &
+                                ( gg - dd ) * de_dx(k,j+1,i+1) &
+                               ) / ( 3.0_wp * gg )
+
+                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
+                   de_dx_int(n) = de_dx_int_l
+                ELSE
+                   de_dx_int_u = ( ( gg - aa ) * de_dx(k+1,j,i)   + &
+                                   ( gg - bb ) * de_dx(k+1,j,i+1) + &
+                                   ( gg - cc ) * de_dx(k+1,j+1,i) + &
+                                   ( gg - dd ) * de_dx(k+1,j+1,i+1) &
+                                  ) / ( 3.0_wp * gg )
+                   de_dx_int(n) = de_dx_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *    &
+                                              ( de_dx_int_u - de_dx_int_l )
+                ENDIF
+!
+!--             Interpolate the TKE gradient along y
+                de_dy_int_l = ( ( gg - aa ) * de_dy(k,j,i)   + &
+                                ( gg - bb ) * de_dy(k,j,i+1) + &
+                                ( gg - cc ) * de_dy(k,j+1,i) + &
+                                ( gg - dd ) * de_dy(k,j+1,i+1) &
+                               ) / ( 3.0_wp * gg )
+                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
+                   de_dy_int(n) = de_dy_int_l
+                ELSE
+                   de_dy_int_u = ( ( gg - aa ) * de_dy(k+1,j,i)   + &
+                                   ( gg - bb ) * de_dy(k+1,j,i+1) + &
+                                   ( gg - cc ) * de_dy(k+1,j+1,i) + &
+                                   ( gg - dd ) * de_dy(k+1,j+1,i+1) &
+                                  ) / ( 3.0_wp * gg )
+                      de_dy_int(n) = de_dy_int_l + ( zv(n) - zu(k) ) / dzw(k+1) * &
+                                                 ( de_dy_int_u - de_dy_int_l )
+                ENDIF
+
+!
+!--             Interpolate the TKE gradient along z
+                IF ( zv(n) < 0.5_wp * dz(1) )  THEN
+                   de_dz_int(n) = 0.0_wp
+                ELSE
+                   de_dz_int_l = ( ( gg - aa ) * de_dz(k,j,i)   + &
+                                   ( gg - bb ) * de_dz(k,j,i+1) + &
+                                   ( gg - cc ) * de_dz(k,j+1,i) + &
+                                   ( gg - dd ) * de_dz(k,j+1,i+1) &
+                                  ) / ( 3.0_wp * gg )
+
+                   IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
+                      de_dz_int(n) = de_dz_int_l
+                   ELSE
+                      de_dz_int_u = ( ( gg - aa ) * de_dz(k+1,j,i)   + &
+                                      ( gg - bb ) * de_dz(k+1,j,i+1) + &
+                                      ( gg - cc ) * de_dz(k+1,j+1,i) + &
+                                      ( gg - dd ) * de_dz(k+1,j+1,i+1) &
+                                     ) / ( 3.0_wp * gg )
+                      de_dz_int(n) = de_dz_int_l + ( zv(n) - zu(k) ) / dzw(k+1) * &
+                                                 ( de_dz_int_u - de_dz_int_l )
+                   ENDIF
+                ENDIF
+
+!
+!--             Interpolate the dissipation of TKE
+                diss_int_l = ( ( gg - aa ) * diss(k,j,i)   + &
+                               ( gg - bb ) * diss(k,j,i+1) + &
+                               ( gg - cc ) * diss(k,j+1,i) + &
+                               ( gg - dd ) * diss(k,j+1,i+1) &
+                               ) / ( 3.0_wp * gg )
+
+                IF ( k == nzt )  THEN
+                   diss_int(n) = diss_int_l
+                ELSE
+                   diss_int_u = ( ( gg - aa ) * diss(k+1,j,i)   + &
+                                  ( gg - bb ) * diss(k+1,j,i+1) + &
+                                  ( gg - cc ) * diss(k+1,j+1,i) + &
+                                  ( gg - dd ) * diss(k+1,j+1,i+1) &
+                                 ) / ( 3.0_wp * gg )
+                   diss_int(n) = diss_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *      &
+                                            ( diss_int_u - diss_int_l )
+                ENDIF
+
+!
+!--             Set flag for stochastic equation.
+                term_1_2(n) = 1.0_wp
+             ENDDO
+          ENDIF
+       ENDDO
+
+       DO nb = 0,7
+          i = ip + block_offset(nb)%i_off
+          j = jp + block_offset(nb)%j_off
+          k = kp + block_offset(nb)%k_off
+
+          DO  n = start_index(nb), end_index(nb)
+!
+!--          Vertical interpolation of the horizontally averaged SGS TKE and
+!--          resolved-scale velocity variances and use the interpolated values
+!--          to calculate the coefficient fs, which is a measure of the ratio
+!--          of the subgrid-scale turbulent kinetic energy to the total amount
+!--          of turbulent kinetic energy.
+             IF ( k == 0 )  THEN
+                e_mean_int = hom(0,1,8,0)
+             ELSE
+                e_mean_int = hom(k,1,8,0) +                                    &
+                                           ( hom(k+1,1,8,0) - hom(k,1,8,0) ) / &
+                                           ( zu(k+1) - zu(k) ) *               &
+                                           ( zv(n) - zu(k) )
+             ENDIF
+
+             kw = kp - 1
+
+             IF ( k == 0 )  THEN
+                aa  = hom(k+1,1,30,0)  * ( zv(n) / &
+                                         ( 0.5_wp * ( zu(k+1) - zu(k) ) ) )
+                bb  = hom(k+1,1,31,0)  * ( zv(n) / &
+                                         ( 0.5_wp * ( zu(k+1) - zu(k) ) ) )
+                cc  = hom(kw+1,1,32,0) * ( zv(n) / &
+                                         ( 1.0_wp * ( zw(kw+1) - zw(kw) ) ) )
+             ELSE
+                aa  = hom(k,1,30,0) + ( hom(k+1,1,30,0) - hom(k,1,30,0) ) *    &
+                           ( ( zv(n) - zu(k) ) / ( zu(k+1) - zu(k) ) )
+                bb  = hom(k,1,31,0) + ( hom(k+1,1,31,0) - hom(k,1,31,0) ) *    &
+                           ( ( zv(n) - zu(k) ) / ( zu(k+1) - zu(k) ) )
+                cc  = hom(kw,1,32,0) + ( hom(kw+1,1,32,0)-hom(kw,1,32,0) ) *   &
+                           ( ( zv(n) - zw(kw) ) / ( zw(kw+1)-zw(kw) ) )
+             ENDIF
+
+             vv_int = ( 1.0_wp / 3.0_wp ) * ( aa + bb + cc )
+!
+!--          Needed to avoid NaN particle velocities. The value of 1.0 is just
+!--          an educated guess for the given case.
+             IF ( vv_int + ( 2.0_wp / 3.0_wp ) * e_mean_int == 0.0_wp )  THEN
+                fs_int(n) = 1.0_wp
+             ELSE
+                fs_int(n) = ( 2.0_wp / 3.0_wp ) * e_mean_int /                 &
+                            ( vv_int + ( 2.0_wp / 3.0_wp ) * e_mean_int )
+             ENDIF
+
+          ENDDO
+       ENDDO
+
+       DO  nb = 0, 7
+          DO  n = start_index(nb), end_index(nb)
+             rg(n,1) = random_gauss( iran_part, 5.0_wp )
+             rg(n,2) = random_gauss( iran_part, 5.0_wp )
+             rg(n,3) = random_gauss( iran_part, 5.0_wp )
+          ENDDO
+       ENDDO
+
+       DO  nb = 0, 7
+          DO  n = start_index(nb), end_index(nb)
+
+!
+!--          Calculate the Lagrangian timescale according to Weil et al. (2004).
+             lagr_timescale(n) = ( 4.0_wp * e_int(n) + 1E-20_wp ) / &
+                              ( 3.0_wp * fs_int(n) * c_0 * diss_int(n) + 1E-20_wp )
+
+!
+!--          Calculate the next particle timestep. dt_gap is the time needed to
+!--          complete the current LES timestep.
+             dt_gap(n) = dt_3d - particles(n)%dt_sum
+             dt_particle(n) = MIN( dt_3d, 0.025_wp * lagr_timescale(n), dt_gap(n) )
+             particles(n)%aux1 = lagr_timescale(n)
+             particles(n)%aux2 = dt_gap(n)
+!
+!--          The particle timestep should not be too small in order to prevent
+!--          the number of particle timesteps of getting too large
+             IF ( dt_particle(n) < dt_min_part  .AND.  dt_min_part < dt_gap(n) )  THEN
+                dt_particle(n) = dt_min_part
+             ENDIF
+             rvar1_temp(n) = particles(n)%rvar1
+             rvar2_temp(n) = particles(n)%rvar2
+             rvar3_temp(n) = particles(n)%rvar3
+!
+!--          Calculate the SGS velocity components
+             IF ( particles(n)%age == 0.0_wp )  THEN
+!
+!--             For new particles the SGS components are derived from the SGS
+!--             TKE. Limit the Gaussian random number to the interval
+!--             [-5.0*sigma, 5.0*sigma] in order to prevent the SGS velocities
+!--             from becoming unrealistically large.
+                rvar1_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
+                                          + 1E-20_wp ) * ( rg(n,1) - 1.0_wp )
+                rvar2_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
+                                          + 1E-20_wp ) * ( rg(n,2) - 1.0_wp )
+                rvar3_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
+                                          + 1E-20_wp ) * ( rg(n,3) - 1.0_wp )
+
+             ELSE
+!
+!--             Restriction of the size of the new timestep: compared to the 
+!--             previous timestep the increase must not exceed 200%. First,
+!--             check if age > age_m, in order to prevent that particles get zero
+!--             timestep.
+                dt_particle_m = MERGE( dt_particle(n),                         &
+                                       particles(n)%age - particles(n)%age_m,  &
+                                       particles(n)%age - particles(n)%age_m < &
+                                       1E-8_wp )
+                IF ( dt_particle(n) > 2.0_wp * dt_particle_m )  THEN
+                   dt_particle(n) = 2.0_wp * dt_particle_m
+                ENDIF
+
+!--             For old particles the SGS components are correlated with the
+!--             values from the previous timestep. Random numbers have also to
+!--             be limited (see above).
+!--             As negative values for the subgrid TKE are not allowed, the
+!--             change of the subgrid TKE with time cannot be smaller than
+!--             -e_int(n)/dt_particle. This value is used as a lower boundary
+!--             value for the change of TKE 
+                de_dt_min = - e_int(n) / dt_particle(n)
+
+                de_dt = ( e_int(n) - particles(n)%e_m ) / dt_particle_m
+
+                IF ( de_dt < de_dt_min )  THEN
+                   de_dt = de_dt_min
+                ENDIF
+
+                CALL weil_stochastic_eq(rvar1_temp(n), fs_int(n), e_int(n),& 
+                                        de_dx_int(n), de_dt, diss_int(n),       &
+                                        dt_particle(n), rg(n,1), term_1_2(n) )
+
+                CALL weil_stochastic_eq(rvar2_temp(n), fs_int(n), e_int(n),& 
+                                        de_dy_int(n), de_dt, diss_int(n),       &
+                                        dt_particle(n), rg(n,2), term_1_2(n) )
+
+                CALL weil_stochastic_eq(rvar3_temp(n), fs_int(n), e_int(n),& 
+                                        de_dz_int(n), de_dt, diss_int(n),       &
+                                        dt_particle(n), rg(n,3), term_1_2(n) )
+
+             ENDIF
+
+          ENDDO
+       ENDDO
+!
+!--    Check if the added SGS velocities result in a violation of the CFL-
+!--    criterion. If yes choose a smaller timestep based on the new velocities
+!--    and calculate SGS velocities again
+       dz_temp = zw(kp)-zw(kp-1)
+       
+       DO  nb = 0, 7
+          DO  n = start_index(nb), end_index(nb)
+             IF ( .NOT. particles(n)%age == 0.0_wp .AND.                       &
+                (ABS( u_int(n) + rvar1_temp(n) ) > (dx/dt_particle(n))  .OR.   &
+                 ABS( v_int(n) + rvar2_temp(n) ) > (dy/dt_particle(n))  .OR.   &
+                 ABS( w_int(n) + rvar3_temp(n) ) > (dz_temp/dt_particle(n)))) THEN
+                
+                dt_particle(n) = 0.9_wp * MIN(                                 &
+                                 ( dx / ABS( u_int(n) + rvar1_temp(n) ) ),     &
+                                 ( dy / ABS( v_int(n) + rvar2_temp(n) ) ),     &
+                                 ( dz_temp / ABS( w_int(n) + rvar3_temp(n) ) ) )
+
+!
+!--             Reset temporary SGS velocites to "current" ones
+                rvar1_temp(n) = particles(n)%rvar1
+                rvar2_temp(n) = particles(n)%rvar2
+                rvar3_temp(n) = particles(n)%rvar3
+                
+                de_dt_min = - e_int(n) / dt_particle(n)
+
+                de_dt = ( e_int(n) - particles(n)%e_m ) / dt_particle_m
+
+                IF ( de_dt < de_dt_min )  THEN
+                   de_dt = de_dt_min
+                ENDIF
+
+                CALL weil_stochastic_eq(rvar1_temp(n), fs_int(n), e_int(n),& 
+                                        de_dx_int(n), de_dt, diss_int(n),       &
+                                        dt_particle(n), rg(n,1), term_1_2(n) )
+
+                CALL weil_stochastic_eq(rvar2_temp(n), fs_int(n), e_int(n),& 
+                                        de_dy_int(n), de_dt, diss_int(n),       &
+                                        dt_particle(n), rg(n,2), term_1_2(n) )
+
+                CALL weil_stochastic_eq(rvar3_temp(n), fs_int(n), e_int(n),& 
+                                        de_dz_int(n), de_dt, diss_int(n),       &
+                                        dt_particle(n), rg(n,3), term_1_2(n) )
+             ENDIF                           
+             
+!
+!--          Update particle velocites 
+             particles(n)%rvar1 = rvar1_temp(n)
+             particles(n)%rvar2 = rvar2_temp(n)
+             particles(n)%rvar3 = rvar3_temp(n)
+             u_int(n) = u_int(n) + particles(n)%rvar1
+             v_int(n) = v_int(n) + particles(n)%rvar2
+             w_int(n) = w_int(n) + particles(n)%rvar3
+!
+!--          Store the SGS TKE of the current timelevel which is needed for
+!--          for calculating the SGS particle velocities at the next timestep
+             particles(n)%e_m = e_int(n)
+          ENDDO
+       ENDDO
+       
+    ELSE
+!
+!--    If no SGS velocities are used, only the particle timestep has to
+!--    be set
+       dt_particle = dt_3d
+
+    ENDIF
+
+    dens_ratio = particle_groups(particles(1:number_of_particles)%group)%density_ratio
+
+    IF ( ANY( dens_ratio == 0.0_wp ) )  THEN
+       DO  nb = 0, 7
+          DO  n = start_index(nb), end_index(nb)
+
+!
+!--          Particle advection
+             IF ( dens_ratio(n) == 0.0_wp )  THEN
+!
+!--             Pure passive transport (without particle inertia)
+                particles(n)%x = xv(n) + u_int(n) * dt_particle(n)
+                particles(n)%y = yv(n) + v_int(n) * dt_particle(n)
+                particles(n)%z = zv(n) + w_int(n) * dt_particle(n)
+
+                particles(n)%speed_x = u_int(n)
+                particles(n)%speed_y = v_int(n)
+                particles(n)%speed_z = w_int(n)
+
+             ELSE
+!
+!--             Transport of particles with inertia
+                particles(n)%x = particles(n)%x + particles(n)%speed_x * &
+                                                  dt_particle(n)
+                particles(n)%y = particles(n)%y + particles(n)%speed_y * &
+                                                  dt_particle(n)
+                particles(n)%z = particles(n)%z + particles(n)%speed_z * &
+                                                  dt_particle(n)
+
+!
+!--             Update of the particle velocity
+                IF ( cloud_droplets )  THEN
+!
+!--                Terminal velocity is computed for vertical direction (Rogers et 
+!--                al., 1993, J. Appl. Meteorol.)
+                   diameter = particles(n)%radius * 2000.0_wp !diameter in mm
+                   IF ( diameter <= d0_rog )  THEN
+                      w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
+                   ELSE
+                      w_s = a_rog - b_rog * EXP( -c_rog * diameter )
+                   ENDIF
+
+!
+!--                If selected, add random velocities following Soelch and Kaercher
+!--                (2010, Q. J. R. Meteorol. Soc.)
+                   IF ( use_sgs_for_particles )  THEN
+                      lagr_timescale(n) = km(kp,jp,ip) / MAX( e(kp,jp,ip), 1.0E-20_wp )
+                      RL             = EXP( -1.0_wp * dt_3d / MAX( lagr_timescale(n), &
+                                             1.0E-20_wp ) )
+                      sigma          = SQRT( e(kp,jp,ip) )
+
+                      rg1 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
+                      rg2 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
+                      rg3 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
+
+                      particles(n)%rvar1 = RL * particles(n)%rvar1 +              &
+                                           SQRT( 1.0_wp - RL**2 ) * sigma * rg1
+                      particles(n)%rvar2 = RL * particles(n)%rvar2 +              &
+                                           SQRT( 1.0_wp - RL**2 ) * sigma * rg2
+                      particles(n)%rvar3 = RL * particles(n)%rvar3 +              &
+                                           SQRT( 1.0_wp - RL**2 ) * sigma * rg3
+
+                      particles(n)%speed_x = u_int(n) + particles(n)%rvar1
+                      particles(n)%speed_y = v_int(n) + particles(n)%rvar2
+                      particles(n)%speed_z = w_int(n) + particles(n)%rvar3 - w_s
+                   ELSE
+                      particles(n)%speed_x = u_int(n)
+                      particles(n)%speed_y = v_int(n)
+                      particles(n)%speed_z = w_int(n) - w_s
+                   ENDIF
+
+                ELSE
+
+                   IF ( use_sgs_for_particles )  THEN
+                      exp_arg  = particle_groups(particles(n)%group)%exp_arg
+                      exp_term = EXP( -exp_arg * dt_particle(n) )
+                   ELSE
+                      exp_arg  = particle_groups(particles(n)%group)%exp_arg
+                      exp_term = particle_groups(particles(n)%group)%exp_term
+                   ENDIF
+                   particles(n)%speed_x = particles(n)%speed_x * exp_term +         &
+                                          u_int(n) * ( 1.0_wp - exp_term )
+                   particles(n)%speed_y = particles(n)%speed_y * exp_term +         &
+                                          v_int(n) * ( 1.0_wp - exp_term )
+                   particles(n)%speed_z = particles(n)%speed_z * exp_term +         &
+                                          ( w_int(n) - ( 1.0_wp - dens_ratio(n) ) * &
+                                          g / exp_arg ) * ( 1.0_wp - exp_term )
+                ENDIF
+
+             ENDIF
+          ENDDO
+       ENDDO
+    
+    ELSE
+
+       DO  nb = 0, 7
+          DO  n = start_index(nb), end_index(nb)
+!
+!--          Transport of particles with inertia
+             particles(n)%x = xv(n) + particles(n)%speed_x * dt_particle(n)
+             particles(n)%y = yv(n) + particles(n)%speed_y * dt_particle(n)
+             particles(n)%z = zv(n) + particles(n)%speed_z * dt_particle(n)
+!
+!--          Update of the particle velocity
+             IF ( cloud_droplets )  THEN
+!
+!--             Terminal velocity is computed for vertical direction (Rogers et al., 
+!--             1993, J. Appl. Meteorol.)
+                diameter = particles(n)%radius * 2000.0_wp !diameter in mm
+                IF ( diameter <= d0_rog )  THEN
+                   w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
+                ELSE
+                   w_s = a_rog - b_rog * EXP( -c_rog * diameter )
+                ENDIF
+
+!
+!--             If selected, add random velocities following Soelch and Kaercher
+!--             (2010, Q. J. R. Meteorol. Soc.)
+                IF ( use_sgs_for_particles )  THEN
+                    lagr_timescale(n) = km(kp,jp,ip) / MAX( e(kp,jp,ip), 1.0E-20_wp )
+                     RL             = EXP( -1.0_wp * dt_3d / MAX( lagr_timescale(n), &
+                                             1.0E-20_wp ) )
+                    sigma          = SQRT( e(kp,jp,ip) )
+
+                    rg1 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
+                    rg2 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
+                    rg3 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
+
+                    particles(n)%rvar1 = RL * particles(n)%rvar1 +                &
+                                         SQRT( 1.0_wp - RL**2 ) * sigma * rg1
+                    particles(n)%rvar2 = RL * particles(n)%rvar2 +                &
+                                         SQRT( 1.0_wp - RL**2 ) * sigma * rg2
+                    particles(n)%rvar3 = RL * particles(n)%rvar3 +                &
+                                         SQRT( 1.0_wp - RL**2 ) * sigma * rg3
+
+                    particles(n)%speed_x = u_int(n) + particles(n)%rvar1
+                    particles(n)%speed_y = v_int(n) + particles(n)%rvar2
+                    particles(n)%speed_z = w_int(n) + particles(n)%rvar3 - w_s
+                ELSE
+                    particles(n)%speed_x = u_int(n)
+                    particles(n)%speed_y = v_int(n)
+                    particles(n)%speed_z = w_int(n) - w_s
+                ENDIF
+
+             ELSE
+
+                IF ( use_sgs_for_particles )  THEN
+                   exp_arg  = particle_groups(particles(n)%group)%exp_arg
+                   exp_term = EXP( -exp_arg * dt_particle(n) )
+                ELSE
+                   exp_arg  = particle_groups(particles(n)%group)%exp_arg
+                   exp_term = particle_groups(particles(n)%group)%exp_term
+                ENDIF
+                particles(n)%speed_x = particles(n)%speed_x * exp_term +             &
+                                       u_int(n) * ( 1.0_wp - exp_term )
+                particles(n)%speed_y = particles(n)%speed_y * exp_term +             &
+                                       v_int(n) * ( 1.0_wp - exp_term )
+                particles(n)%speed_z = particles(n)%speed_z * exp_term +             &
+                                       ( w_int(n) - ( 1.0_wp - dens_ratio(n) ) * g / &
+                                       exp_arg ) * ( 1.0_wp - exp_term )
+             ENDIF
+          ENDDO
+       ENDDO
+
+    ENDIF
+
+!
+!-- Store the old age of the particle ( needed to prevent that a
+!-- particle crosses several PEs during one timestep, and for the
+!-- evaluation of the subgrid particle velocity fluctuations )
+    particles(1:number_of_particles)%age_m = particles(1:number_of_particles)%age
+
+    DO  nb = 0, 7
+       DO  n = start_index(nb), end_index(nb)
+!
+!--       Increment the particle age and the total time that the particle
+!--       has advanced within the particle timestep procedure
+          particles(n)%age    = particles(n)%age    + dt_particle(n)
+          particles(n)%dt_sum = particles(n)%dt_sum + dt_particle(n)
+
+!
+!--       Check whether there is still a particle that has not yet completed 
+!--       the total LES timestep
+          IF ( ( dt_3d - particles(n)%dt_sum ) > 1E-8_wp )  THEN
+             dt_3d_reached_l = .FALSE.
+          ENDIF
+
+       ENDDO
+    ENDDO
+
+    CALL cpu_log( log_point_s(44), 'lpm_advec', 'pause' )
+
+
+ END SUBROUTINE lpm_advec
+
+ 
+!------------------------------------------------------------------------------!  
+! Description:
+! ------------
+!> Calculation of subgrid-scale particle speed using the stochastic model 
+!> of Weil et al. (2004, JAS, 61, 2877-2887).
+!------------------------------------------------------------------------------! 
+ SUBROUTINE weil_stochastic_eq( v_sgs, fs_n, e_n, dedxi_n, dedt_n, diss_n,     &
+                                dt_n, rg_n, fac )
+                                
+    REAL(wp) ::  a1      !< dummy argument
+    REAL(wp) ::  dedt_n  !< time derivative of TKE at particle position 
+    REAL(wp) ::  dedxi_n !< horizontal derivative of TKE at particle position
+    REAL(wp) ::  diss_n  !< dissipation at particle position 
+    REAL(wp) ::  dt_n    !< particle timestep
+    REAL(wp) ::  e_n     !< TKE at particle position
+    REAL(wp) ::  fac     !< flag to identify adjacent topography
+    REAL(wp) ::  fs_n    !< weighting factor to prevent that subgrid-scale particle speed becomes too large
+    REAL(wp) ::  rg_n    !< random number
+    REAL(wp) ::  term1   !< memory term
+    REAL(wp) ::  term2   !< drift correction term
+    REAL(wp) ::  term3   !< random term
+    REAL(wp) ::  v_sgs   !< subgrid-scale velocity component 
+
+!-- At first, limit TKE to a small non-zero number, in order to prevent 
+!-- the occurrence of extremely large SGS-velocities in case TKE is zero, 
+!-- (could occur at the simulation begin).
+    e_n = MAX( e_n, 1E-20_wp )
+!
+!-- Please note, terms 1 and 2 (drift and memory term, respectively) are 
+!-- multiplied by a flag to switch of both terms near topography. 
+!-- This is necessary, as both terms may cause a subgrid-scale velocity build up 
+!-- if particles are trapped in regions with very small TKE, e.g. in narrow street 
+!-- canyons resolved by only a few grid points. Hence, term 1 and term 2 are 
+!-- disabled if one of the adjacent grid points belongs to topography. 
+!-- Moreover, in this case, the  previous subgrid-scale component is also set
+!-- to zero.
+
+    a1 = fs_n * c_0 * diss_n
+!
+!-- Memory term
+    term1 = - a1 * v_sgs * dt_n / ( 4.0_wp * sgs_wf_part * e_n + 1E-20_wp )    &
+                 * fac
+!
+!-- Drift correction term
+    term2 = ( ( dedt_n * v_sgs / e_n ) + dedxi_n ) * 0.5_wp * dt_n              &
+                 * fac
+!
+!-- Random term
+    term3 = SQRT( MAX( a1, 1E-20_wp ) ) * ( rg_n - 1.0_wp ) * SQRT( dt_n )
+!
+!-- In cese one of the adjacent grid-boxes belongs to topograhy, the previous
+!-- subgrid-scale velocity component is set to zero, in order to prevent a 
+!-- velocity build-up. 
+!-- This case, set also previous subgrid-scale component to zero. 
+    v_sgs = v_sgs * fac + term1 + term2 + term3
+
+ END SUBROUTINE weil_stochastic_eq 
+ 
+ 
+!------------------------------------------------------------------------------!  
+! Description:
+! ------------
+!> Boundary conditions for the Lagrangian particles.
+!> The routine consists of two different parts. One handles the bottom (flat)
+!> and top boundary. In this part, also particles which exceeded their lifetime
+!> are deleted.
+!> The other part handles the reflection of particles from vertical walls.
+!> This part was developed by Jin Zhang during 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.
+!>
+!> THE WALLS PART OF THIS ROUTINE HAS NOT BEEN TESTED FOR OCEAN RUNS SO FAR!!!!
+!> (see offset_ocean_*)
+!------------------------------------------------------------------------------! 
+ MODULE SUBROUTINE lpm_boundary_conds( location_bc , i, j, k )
+
+    CHARACTER (LEN=*), INTENT(IN) ::  location_bc !< general mode: boundary conditions at bottom/top of the model domain
+                                   !< or at vertical surfaces (buildings, terrain steps)    
+    INTEGER(iwp), INTENT(IN) ::  i !< grid index of particle box along x
+    INTEGER(iwp), INTENT(IN) ::  j !< grid index of particle box along y
+    INTEGER(iwp), INTENT(IN) ::  k !< grid index of particle box along z
+    
+    INTEGER(iwp) ::  inc            !< dummy for sorting algorithmus
+    INTEGER(iwp) ::  ir             !< dummy for sorting algorithmus
+    INTEGER(iwp) ::  i1             !< grid index (x) of old particle position
+    INTEGER(iwp) ::  i2             !< grid index (x) of current particle position
+    INTEGER(iwp) ::  i3             !< grid index (x) of intermediate particle position
+    INTEGER(iwp) ::  jr             !< dummy for sorting algorithmus
+    INTEGER(iwp) ::  j1             !< grid index (y) of old particle position
+    INTEGER(iwp) ::  j2             !< grid index (y) of current particle position
+    INTEGER(iwp) ::  j3             !< grid index (y) of intermediate particle position
+    INTEGER(iwp) ::  k1             !< grid index (z) of old particle position
+    INTEGER(iwp) ::  k2             !< grid index (z) of current particle position
+    INTEGER(iwp) ::  k3             !< grid index (z) of intermediate particle position
+    INTEGER(iwp) ::  n              !< particle number
+    INTEGER(iwp) ::  t_index        !< running index for intermediate particle timesteps in reflection algorithmus
+    INTEGER(iwp) ::  t_index_number !< number of intermediate particle timesteps in reflection algorithmus
+    INTEGER(iwp) ::  tmp_x          !< dummy for sorting algorithm
+    INTEGER(iwp) ::  tmp_y          !< dummy for sorting algorithm
+    INTEGER(iwp) ::  tmp_z          !< dummy for sorting algorithm
+
+    INTEGER(iwp), DIMENSION(0:10) :: x_ind(0:10) = 0 !< index array (x) of intermediate particle positions 
+    INTEGER(iwp), DIMENSION(0:10) :: y_ind(0:10) = 0 !< index array (y) of intermediate particle positions
+    INTEGER(iwp), DIMENSION(0:10) :: z_ind(0:10) = 0 !< index array (z) of intermediate particle positions
+    
+    LOGICAL  ::  cross_wall_x    !< flag to check if particle reflection along x is necessary
+    LOGICAL  ::  cross_wall_y    !< flag to check if particle reflection along y is necessary
+    LOGICAL  ::  cross_wall_z    !< flag to check if particle reflection along z is necessary
+    LOGICAL  ::  reflect_x       !< flag to check if particle is already reflected along x
+    LOGICAL  ::  reflect_y       !< flag to check if particle is already reflected along y
+    LOGICAL  ::  reflect_z       !< flag to check if particle is already reflected along z
+    LOGICAL  ::  tmp_reach_x     !< dummy for sorting algorithmus
+    LOGICAL  ::  tmp_reach_y     !< dummy for sorting algorithmus
+    LOGICAL  ::  tmp_reach_z     !< dummy for sorting algorithmus
+    LOGICAL  ::  x_wall_reached  !< flag to check if particle has already reached wall
+    LOGICAL  ::  y_wall_reached  !< flag to check if particle has already reached wall
+    LOGICAL  ::  z_wall_reached  !< flag to check if particle has already reached wall
+
+    LOGICAL, DIMENSION(0:10) ::  reach_x  !< flag to check if particle is at a yz-wall
+    LOGICAL, DIMENSION(0:10) ::  reach_y  !< flag to check if particle is at a xz-wall
+    LOGICAL, DIMENSION(0:10) ::  reach_z  !< flag to check if particle is at a xy-wall
+
+    REAL(wp) ::  dt_particle    !< particle timestep
+    REAL(wp) ::  eps = 1E-10_wp !< security number to check if particle has reached a wall
+    REAL(wp) ::  pos_x          !< intermediate particle position (x)
+    REAL(wp) ::  pos_x_old      !< particle position (x) at previous particle timestep
+    REAL(wp) ::  pos_y          !< intermediate particle position (y)
+    REAL(wp) ::  pos_y_old      !< particle position (y) at previous particle timestep
+    REAL(wp) ::  pos_z          !< intermediate particle position (z)
+    REAL(wp) ::  pos_z_old      !< particle position (z) at previous particle timestep
+    REAL(wp) ::  prt_x          !< current particle position (x)
+    REAL(wp) ::  prt_y          !< current particle position (y)
+    REAL(wp) ::  prt_z          !< current particle position (z)
+    REAL(wp) ::  t_old          !< previous reflection time
+    REAL(wp) ::  tmp_t          !< dummy for sorting algorithmus
+    REAL(wp) ::  xwall          !< location of wall in x
+    REAL(wp) ::  ywall          !< location of wall in y
+    REAL(wp) ::  zwall          !< location of wall in z
+
+    REAL(wp), DIMENSION(0:10) ::  t  !< reflection time
+
+    SELECT CASE ( location_bc )
+
+       CASE ( 'bottom/top' )
+
+!     IF ( location_bc == 'bottom/top' )  THEN
+
+!
+!--    Apply boundary conditions to those particles that have crossed the top or
+!--    bottom boundary and delete those particles, which are older than allowed
+       DO  n = 1, number_of_particles
+
+!
+!--       Stop if particles have moved further than the length of one 
+!--       PE subdomain (newly released particles have age = age_m!)
+          IF ( particles(n)%age /= particles(n)%age_m )  THEN
+             IF ( ABS(particles(n)%speed_x) >                                  &
+                  ((nxr-nxl+2)*dx)/(particles(n)%age-particles(n)%age_m)  .OR. &
+                  ABS(particles(n)%speed_y) >                                  &
+                  ((nyn-nys+2)*dy)/(particles(n)%age-particles(n)%age_m) )  THEN
+
+                  WRITE( message_string, * )  'particle too fast.  n = ',  n 
+                  CALL message( 'lpm_boundary_conds', 'PA0148', 2, 2, -1, 6, 1 )
+             ENDIF
+          ENDIF
+
+          IF ( particles(n)%age > particle_maximum_age  .AND.  &
+               particles(n)%particle_mask )                              &
+          THEN
+             particles(n)%particle_mask  = .FALSE.
+             deleted_particles = deleted_particles + 1
+          ENDIF
+
+          IF ( particles(n)%z >= zw(nz)  .AND.  particles(n)%particle_mask )  THEN
+             IF ( ibc_par_t == 1 )  THEN
+!
+!--             Particle absorption
+                particles(n)%particle_mask  = .FALSE.
+                deleted_particles = deleted_particles + 1
+             ELSEIF ( ibc_par_t == 2 )  THEN
+!
+!--             Particle reflection
+                particles(n)%z       = 2.0_wp * zw(nz) - particles(n)%z
+                particles(n)%speed_z = -particles(n)%speed_z
+                IF ( use_sgs_for_particles  .AND. &
+                     particles(n)%rvar3 > 0.0_wp )  THEN
+                   particles(n)%rvar3 = -particles(n)%rvar3
+                ENDIF
+             ENDIF
+          ENDIF
+          
+          IF ( particles(n)%z < zw(0)  .AND.  particles(n)%particle_mask )  THEN
+             IF ( ibc_par_b == 1 )  THEN
+!
+!--             Particle absorption
+                particles(n)%particle_mask  = .FALSE.
+                deleted_particles = deleted_particles + 1
+             ELSEIF ( ibc_par_b == 2 )  THEN
+!
+!--             Particle reflection
+                particles(n)%z       = 2.0_wp * zw(0) - particles(n)%z
+                particles(n)%speed_z = -particles(n)%speed_z
+                IF ( use_sgs_for_particles  .AND. &
+                     particles(n)%rvar3 < 0.0_wp )  THEN
+                   particles(n)%rvar3 = -particles(n)%rvar3
+                ENDIF
+             ENDIF
+          ENDIF
+       ENDDO
+
+!     ELSEIF ( location_bc == 'walls' )  THEN
+      CASE ( 'walls' )
+
+       CALL cpu_log( log_point_s(48), 'lpm_wall_reflect', 'start' )
+
+       DO  n = 1, number_of_particles
+!
+!--       Recalculate particle timestep
+          dt_particle = particles(n)%age - particles(n)%age_m
+!
+!--       Obtain x/y indices for current particle position
+          i2 = particles(n)%x * ddx
+          j2 = particles(n)%y * ddy
+          IF (zw(k)   < particles(n)%z ) k2 = k + 1
+          IF (zw(k)   > particles(n)%z .AND. zw(k-1) < particles(n)%z ) k2 = k
+          IF (zw(k-1) > particles(n)%z ) k2 = k - 1 
+!
+!--       Save current particle positions
+          prt_x = particles(n)%x
+          prt_y = particles(n)%y
+          prt_z = particles(n)%z
+!
+!--       Recalculate old particle positions
+          pos_x_old = particles(n)%x - particles(n)%speed_x * dt_particle
+          pos_y_old = particles(n)%y - particles(n)%speed_y * dt_particle
+          pos_z_old = particles(n)%z - particles(n)%speed_z * dt_particle
+!
+!--       Obtain x/y indices for old particle positions
+          i1 = i
+          j1 = j
+          k1 = k
+!
+!--       Determine horizontal as well as vertical walls at which particle can 
+!--       be potentially reflected. 
+!--       Start with walls aligned in yz layer.
+!--       Wall to the right 
+          IF ( prt_x > pos_x_old )  THEN
+             xwall = ( i1 + 1 ) * dx
+!
+!--       Wall to the left
+          ELSE
+             xwall = i1 * dx
+          ENDIF
+!
+!--       Walls aligned in xz layer
+!--       Wall to the north
+          IF ( prt_y > pos_y_old )  THEN
+             ywall = ( j1 +1 ) * dy
+!--       Wall to the south
+          ELSE
+             ywall = j1 * dy
+          ENDIF
+
+          IF ( prt_z > pos_z_old ) THEN
+             zwall = zw(k)
+          ELSE
+             zwall = zw(k-1)
+          ENDIF     
+!
+!--       Initialize flags to check if particle reflection is necessary
+          cross_wall_x = .FALSE.
+          cross_wall_y = .FALSE.
+          cross_wall_z = .FALSE.
+!
+!--       Initialize flags to check if a wall is reached
+          reach_x      = .FALSE.
+          reach_y      = .FALSE.
+          reach_z      = .FALSE.
+!
+!--       Initialize flags to check if a particle was already reflected
+          reflect_x    = .FALSE.
+          reflect_y    = .FALSE.
+          reflect_z    = .FALSE.
+!
+!--       Initialize flags to check if a wall is already crossed.
+!--       ( Required to obtain correct indices. )
+          x_wall_reached = .FALSE.
+          y_wall_reached = .FALSE.
+          z_wall_reached = .FALSE.
+!
+!--       Initialize time array 
+          t     = 0.0_wp
+!
+!--       Check if particle can reach any wall. This case, calculate the 
+!--       fractional time needed to reach this wall. Store this fractional
+!--       timestep in array t. Moreover, store indices for these grid
+!--       boxes where the respective wall belongs to.  
+!--       Start with x-direction.
+          t_index    = 1
+          t(t_index) = ( xwall - pos_x_old )                                   &
+                     / MERGE( MAX( prt_x - pos_x_old,  1E-30_wp ),             &
+                              MIN( prt_x - pos_x_old, -1E-30_wp ),             &
+                              prt_x > pos_x_old )
+          x_ind(t_index)   = i2
+          y_ind(t_index)   = j1
+          z_ind(t_index)   = k1
+          reach_x(t_index) = .TRUE.
+          reach_y(t_index) = .FALSE.
+          reach_z(t_index) = .FALSE.
+!
+!--       Store these values only if particle really reaches any wall. t must 
+!--       be in a interval between [0:1].
+          IF ( t(t_index) <= 1.0_wp .AND. t(t_index) >= 0.0_wp )  THEN
+             t_index      = t_index + 1
+             cross_wall_x = .TRUE.
+          ENDIF
+!
+!--       y-direction
+          t(t_index) = ( ywall - pos_y_old )                                   &
+                     / MERGE( MAX( prt_y - pos_y_old,  1E-30_wp ),             &
+                              MIN( prt_y - pos_y_old, -1E-30_wp ),             &
+                              prt_y > pos_y_old )
+          x_ind(t_index)   = i1
+          y_ind(t_index)   = j2
+          z_ind(t_index)   = k1
+          reach_x(t_index) = .FALSE.
+          reach_y(t_index) = .TRUE.
+          reach_z(t_index) = .FALSE.
+          IF ( t(t_index) <= 1.0_wp .AND. t(t_index) >= 0.0_wp )  THEN
+             t_index      = t_index + 1
+             cross_wall_y = .TRUE.
+          ENDIF
+!
+!--       z-direction
+          t(t_index) = (zwall - pos_z_old )                                    &
+                     / MERGE( MAX( prt_z - pos_z_old,  1E-30_wp ),             &
+                              MIN( prt_z - pos_z_old, -1E-30_wp ),             &
+                              prt_z > pos_z_old )
+                     
+          x_ind(t_index)   = i1
+          y_ind(t_index)   = j1
+          z_ind(t_index)   = k2
+          reach_x(t_index) = .FALSE.
+          reach_y(t_index) = .FALSE.
+          reach_z(t_index) = .TRUE.
+          IF( t(t_index) <= 1.0_wp .AND. t(t_index) >= 0.0_wp) THEN
+             t_index      = t_index + 1
+             cross_wall_z = .TRUE.
+          ENDIF
+          
+          t_index_number = t_index - 1
+!
+!--       Carry out reflection only if particle reaches any wall
+          IF ( cross_wall_x .OR. cross_wall_y .OR. cross_wall_z )  THEN
+!
+!--          Sort fractional timesteps in ascending order. Also sort the 
+!--          corresponding indices and flag according to the time interval a  
+!--          particle reaches the respective wall.
+             inc = 1
+             jr  = 1
+             DO WHILE ( inc <= t_index_number )
+                inc = 3 * inc + 1
+             ENDDO
+
+             DO WHILE ( inc > 1 )
+                inc = inc / 3
+                DO  ir = inc+1, t_index_number
+                   tmp_t       = t(ir)
+                   tmp_x       = x_ind(ir)
+                   tmp_y       = y_ind(ir)
+                   tmp_z       = z_ind(ir)
+                   tmp_reach_x = reach_x(ir)
+                   tmp_reach_y = reach_y(ir)
+                   tmp_reach_z = reach_z(ir)
+                   jr    = ir
+                   DO WHILE ( t(jr-inc) > tmp_t )
+                      t(jr)       = t(jr-inc)
+                      x_ind(jr)   = x_ind(jr-inc)
+                      y_ind(jr)   = y_ind(jr-inc)
+                      z_ind(jr)   = z_ind(jr-inc)
+                      reach_x(jr) = reach_x(jr-inc)
+                      reach_y(jr) = reach_y(jr-inc)
+                      reach_z(jr) = reach_z(jr-inc)
+                      jr    = jr - inc
+                      IF ( jr <= inc )  EXIT
+                   ENDDO
+                   t(jr)       = tmp_t
+                   x_ind(jr)   = tmp_x
+                   y_ind(jr)   = tmp_y
+                   z_ind(jr)   = tmp_z
+                   reach_x(jr) = tmp_reach_x
+                   reach_y(jr) = tmp_reach_y
+                   reach_z(jr) = tmp_reach_z
+                ENDDO
+             ENDDO
+!
+!--          Initialize temporary particle positions
+             pos_x = pos_x_old
+             pos_y = pos_y_old
+             pos_z = pos_z_old
+!
+!--          Loop over all times a particle possibly moves into a new grid box
+             t_old = 0.0_wp
+             DO t_index = 1, t_index_number 
+!           
+!--             Calculate intermediate particle position according to the 
+!--             timesteps a particle reaches any wall.
+                pos_x = pos_x + ( t(t_index) - t_old ) * dt_particle           &
+                                                       * particles(n)%speed_x
+                pos_y = pos_y + ( t(t_index) - t_old ) * dt_particle           &
+                                                       * particles(n)%speed_y
+                pos_z = pos_z + ( t(t_index) - t_old ) * dt_particle           &
+                                                       * particles(n)%speed_z
+!
+!--             Obtain x/y grid indices for intermediate particle position from
+!--             sorted index array
+                i3 = x_ind(t_index)
+                j3 = y_ind(t_index)
+                k3 = z_ind(t_index)
+!
+!--             Check which wall is already reached
+                IF ( .NOT. x_wall_reached )  x_wall_reached = reach_x(t_index) 
+                IF ( .NOT. y_wall_reached )  y_wall_reached = reach_y(t_index)
+                IF ( .NOT. z_wall_reached )  z_wall_reached = reach_z(t_index)
+!
+!--             Check if a particle needs to be reflected at any yz-wall. If 
+!--             necessary, carry out reflection. Please note, a security 
+!--             constant is required, as the particle position does not 
+!--             necessarily exactly match the wall location due to rounding 
+!--             errors.
+                IF ( reach_x(t_index)                      .AND.               & 
+                     ABS( pos_x - xwall ) < eps            .AND.               &
+                     .NOT. BTEST(wall_flags_0(k3,j3,i3),0) .AND.               &
+                     .NOT. reflect_x )  THEN
+! 
+! 
+!--                Reflection in x-direction. 
+!--                Ensure correct reflection by MIN/MAX functions, depending on
+!--                direction of particle transport. 
+!--                Due to rounding errors pos_x does not exactly match the wall
+!--                location, leading to erroneous reflection.             
+                   pos_x = MERGE( MIN( 2.0_wp * xwall - pos_x, xwall ),        &
+                                  MAX( 2.0_wp * xwall - pos_x, xwall ),        &
+                                  particles(n)%x > xwall )
+!
+!--                Change sign of particle speed                     
+                   particles(n)%speed_x = - particles(n)%speed_x
+!
+!--                Also change sign of subgrid-scale particle speed
+                   particles(n)%rvar1 = - particles(n)%rvar1
+!
+!--                Set flag that reflection along x is already done
+                   reflect_x          = .TRUE.
+!
+!--                As the particle does not cross any further yz-wall during 
+!--                this timestep, set further x-indices to the current one.
+                   x_ind(t_index:t_index_number) = i1
+!
+!--             If particle already reached the wall but was not reflected, 
+!--             set further x-indices to the new one.
+                ELSEIF ( x_wall_reached .AND. .NOT. reflect_x )  THEN
+                    x_ind(t_index:t_index_number) = i2
+                ENDIF !particle reflection in x direction done
+
+!
+!--             Check if a particle needs to be reflected at any xz-wall. If 
+!--             necessary, carry out reflection. Please note, a security 
+!--             constant is required, as the particle position does not 
+!--             necessarily exactly match the wall location due to rounding 
+!--             errors. 
+                IF ( reach_y(t_index)                      .AND.               & 
+                     ABS( pos_y - ywall ) < eps            .AND.               &
+                     .NOT. BTEST(wall_flags_0(k3,j3,i3),0) .AND.               &
+                     .NOT. reflect_y )  THEN
+! 
+! 
+!--                Reflection in y-direction. 
+!--                Ensure correct reflection by MIN/MAX functions, depending on
+!--                direction of particle transport. 
+!--                Due to rounding errors pos_y does not exactly match the wall
+!--                location, leading to erroneous reflection.             
+                   pos_y = MERGE( MIN( 2.0_wp * ywall - pos_y, ywall ),        &
+                                  MAX( 2.0_wp * ywall - pos_y, ywall ),        &
+                                  particles(n)%y > ywall )
+!
+!--                Change sign of particle speed                     
+                   particles(n)%speed_y = - particles(n)%speed_y
+!
+!--                Also change sign of subgrid-scale particle speed
+                   particles(n)%rvar2 = - particles(n)%rvar2
+!
+!--                Set flag that reflection along y is already done
+                   reflect_y          = .TRUE.
+!
+!--                As the particle does not cross any further xz-wall during 
+!--                this timestep, set further y-indices to the current one.
+                   y_ind(t_index:t_index_number) = j1
+!
+!--             If particle already reached the wall but was not reflected, 
+!--             set further y-indices to the new one.
+                ELSEIF ( y_wall_reached .AND. .NOT. reflect_y )  THEN
+                    y_ind(t_index:t_index_number) = j2
+                ENDIF !particle reflection in y direction done
+                
+!
+!--             Check if a particle needs to be reflected at any xy-wall. If 
+!--             necessary, carry out reflection. Please note, a security 
+!--             constant is required, as the particle position does not 
+!--             necessarily exactly match the wall location due to rounding 
+!--             errors. 
+                IF ( reach_z(t_index)                      .AND.               & 
+                     ABS( pos_z - zwall ) < eps            .AND.               &
+                     .NOT. BTEST(wall_flags_0(k3,j3,i3),0) .AND.               &
+                     .NOT. reflect_z )  THEN
+! 
+! 
+!--                Reflection in z-direction. 
+!--                Ensure correct reflection by MIN/MAX functions, depending on
+!--                direction of particle transport. 
+!--                Due to rounding errors pos_z does not exactly match the wall
+!--                location, leading to erroneous reflection.             
+                   pos_z = MERGE( MIN( 2.0_wp * zwall - pos_z, zwall ),        &
+                                  MAX( 2.0_wp * zwall - pos_z, zwall ),        &
+                                  particles(n)%z > zwall )
+!
+!--                Change sign of particle speed                     
+                   particles(n)%speed_z = - particles(n)%speed_z
+!
+!--                Also change sign of subgrid-scale particle speed
+                   particles(n)%rvar3 = - particles(n)%rvar3
+!
+!--                Set flag that reflection along z is already done
+                   reflect_z          = .TRUE.
+!
+!--                As the particle does not cross any further xy-wall during 
+!--                this timestep, set further z-indices to the current one.
+                   z_ind(t_index:t_index_number) = k1
+!
+!--             If particle already reached the wall but was not reflected, 
+!--             set further z-indices to the new one.
+                ELSEIF ( z_wall_reached .AND. .NOT. reflect_z )  THEN
+                    z_ind(t_index:t_index_number) = k2
+                ENDIF !particle reflection in z direction done                
+                
+!
+!--             Swap time
+                t_old = t(t_index)
+
+             ENDDO
+!
+!--          If a particle was reflected, calculate final position from last
+!--          intermediate position.
+             IF ( reflect_x .OR. reflect_y .OR. reflect_z )  THEN
+
+                particles(n)%x = pos_x + ( 1.0_wp - t_old ) * dt_particle      &
+                                                         * particles(n)%speed_x
+                particles(n)%y = pos_y + ( 1.0_wp - t_old ) * dt_particle      &
+                                                         * particles(n)%speed_y
+                particles(n)%z = pos_z + ( 1.0_wp - t_old ) * dt_particle      &
+                                                         * particles(n)%speed_z
+
+             ENDIF
+
+          ENDIF
+
+       ENDDO
+
+       CALL cpu_log( log_point_s(48), 'lpm_wall_reflect', 'stop' )
+
+       CASE DEFAULT
+          CONTINUE
+
+    END SELECT
+
+ END SUBROUTINE lpm_boundary_conds 
+ 
+ 
+END SUBMODULE
+ 
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> This is a submodule of the lagrangian particle model. It contains all 
+!> microphysical processes of the lpm. This includes activation, condensation 
+!> and collision of cloud and rain droplets.  
+!> As a next step this submodule should be excluded as an own file. 
+!------------------------------------------------------------------------------!
+SUBMODULE (lagrangian_particle_model_mod) lpm_microphysics
+
+    REAL(wp) ::  epsilon       !<
+    REAL(wp) ::  urms          !<
+
+    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  epsclass  !< dissipation rate class
+    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  radclass  !< radius class
+    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  winf      !<
+    
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ec        !<
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ecf       !<
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  gck       !<
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  hkernel   !<
+    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  hwratio   !<
+    
+    REAL(wp) ::  rclass_lbound !<
+    REAL(wp) ::  rclass_ubound !<    
+    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  ckernel !<    
+ 
+ CONTAINS
+ 
+ MODULE SUBROUTINE lpm_droplet_condensation (i,j,k)
+
+    INTEGER(iwp), INTENT(IN) :: i              !<
+    INTEGER(iwp), INTENT(IN) :: j              !<
+    INTEGER(iwp), INTENT(IN) :: k              !<
+    INTEGER(iwp) :: n                          !<
+
+    REAL(wp) ::  afactor                       !< curvature effects
+    REAL(wp) ::  arg                           !<
+    REAL(wp) ::  bfactor                       !< solute effects
+    REAL(wp) ::  ddenom                        !<
+    REAL(wp) ::  delta_r                       !<
+    REAL(wp) ::  diameter                      !< diameter of cloud droplets
+    REAL(wp) ::  diff_coeff                    !< diffusivity for water vapor
+    REAL(wp) ::  drdt                          !<
+    REAL(wp) ::  dt_ros                        !<
+    REAL(wp) ::  dt_ros_sum                    !<
+    REAL(wp) ::  d2rdtdr                       !<
+    REAL(wp) ::  e_a                           !< current vapor pressure
+    REAL(wp) ::  e_s                           !< current saturation vapor pressure
+    REAL(wp) ::  error                         !< local truncation error in Rosenbrock
+    REAL(wp) ::  k1                            !<
+    REAL(wp) ::  k2                            !<
+    REAL(wp) ::  r_err                         !< First order estimate of Rosenbrock radius
+    REAL(wp) ::  r_ros                         !< Rosenbrock radius
+    REAL(wp) ::  r_ros_ini                     !< initial Rosenbrock radius
+    REAL(wp) ::  r0                            !< gas-kinetic lengthscale
+    REAL(wp) ::  sigma                         !< surface tension of water
+    REAL(wp) ::  thermal_conductivity          !< thermal conductivity for water
+    REAL(wp) ::  t_int                         !< temperature
+    REAL(wp) ::  w_s                           !< terminal velocity of droplets
+    REAL(wp) ::  re_p                          !< particle Reynolds number
+!
+!-- Parameters for Rosenbrock method (see Verwer et al., 1999)
+    REAL(wp), PARAMETER :: prec = 1.0E-3_wp     !< precision of Rosenbrock solution
+    REAL(wp), PARAMETER :: q_increase = 1.5_wp  !< increase factor in timestep
+    REAL(wp), PARAMETER :: q_decrease = 0.9_wp  !< decrease factor in timestep
+    REAL(wp), PARAMETER :: gamma = 0.292893218814_wp !< = 1.0 - 1.0 / SQRT(2.0)
+!
+!-- Parameters for terminal velocity
+    REAL(wp), PARAMETER ::  a_rog = 9.65_wp      !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  b_rog = 10.43_wp     !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  c_rog = 0.6_wp       !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp   !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp  !< parameter for fall velocity
+    REAL(wp), PARAMETER ::  d0_rog = 0.745_wp    !< separation diameter
+
+    REAL(wp), DIMENSION(number_of_particles) ::  ventilation_effect     !<
+    REAL(wp), DIMENSION(number_of_particles) ::  new_r                  !<
+
+    CALL cpu_log( log_point_s(42), 'lpm_droplet_condens', 'start' )
+
+!
+!-- Absolute temperature
+    t_int = pt(k,j,i) * exner(k)
+!
+!-- Saturation vapor pressure (Eq. 10 in Bolton, 1980)
+    e_s = magnus( t_int )
+!
+!-- Current vapor pressure
+    e_a = q(k,j,i) * hyp(k) / ( q(k,j,i) + rd_d_rv )
+!
+!-- Thermal conductivity for water (from Rogers and Yau, Table 7.1)
+    thermal_conductivity = 7.94048E-05_wp * t_int + 0.00227011_wp
+!
+!-- Moldecular diffusivity of water vapor in air (Hall und Pruppacher, 1976)
+    diff_coeff           = 0.211E-4_wp * ( t_int / 273.15_wp )**1.94_wp * &
+                           ( 101325.0_wp / hyp(k) )
+!
+!-- Lengthscale for gas-kinetic effects (from Mordy, 1959, p. 23):
+    r0 = diff_coeff / 0.036_wp * SQRT( 2.0_wp * pi / ( r_v * t_int ) )
+!
+!-- Calculate effects of heat conductivity and diffusion of water vapor on the
+!-- diffusional growth process (usually known as 1.0 / (F_k + F_d) )
+    ddenom  = 1.0_wp / ( rho_l * r_v * t_int / ( e_s * diff_coeff ) +          &
+                         ( l_v / ( r_v * t_int ) - 1.0_wp ) * rho_l *          &
+                         l_v / ( thermal_conductivity * t_int )                &
+                       )
+    new_r = 0.0_wp
+!
+!-- Determine ventilation effect on evaporation of large drops
+    DO  n = 1, number_of_particles
+
+       IF ( particles(n)%radius >= 4.0E-5_wp  .AND.  e_a / e_s < 1.0_wp )  THEN
+!
+!--       Terminal velocity is computed for vertical direction (Rogers et al.,
+!--       1993, J. Appl. Meteorol.)
+          diameter = particles(n)%radius * 2000.0_wp !diameter in mm
+          IF ( diameter <= d0_rog )  THEN
+             w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
+          ELSE
+             w_s = a_rog - b_rog * EXP( -c_rog * diameter )
+          ENDIF
+!
+!--       Calculate droplet's Reynolds number
+          re_p = 2.0_wp * particles(n)%radius * w_s / molecular_viscosity
+!
+!--       Ventilation coefficient (Rogers and Yau, 1989):
+          IF ( re_p > 2.5_wp )  THEN
+             ventilation_effect(n) = 0.78_wp + 0.28_wp * SQRT( re_p )
+          ELSE
+             ventilation_effect(n) = 1.0_wp + 0.09_wp * re_p
+          ENDIF
+       ELSE
+!
+!--       For small droplets or in supersaturated environments, the ventilation
+!--       effect does not play a role
+          ventilation_effect(n) = 1.0_wp
+       ENDIF
+    ENDDO
+
+    IF( .NOT. curvature_solution_effects ) then
+!
+!--    Use analytic model for diffusional growth including gas-kinetic
+!--    effects (Mordy, 1959) but without the impact of aerosols.
+       DO  n = 1, number_of_particles
+          arg      = ( particles(n)%radius + r0 )**2 + 2.0_wp * dt_3d * ddenom * &
+                                                       ventilation_effect(n) *   &
+                                                       ( e_a / e_s - 1.0_wp )
+          arg      = MAX( arg, ( 0.01E-6 + r0 )**2 )
+          new_r(n) = SQRT( arg ) - r0
+       ENDDO
+
+    ELSE
+!
+!--    Integrate the diffusional growth including gas-kinetic (Mordy, 1959),
+!--    as well as curvature and solute effects (e.g., KÃ¶hler, 1936).
+!
+!--    Curvature effect (afactor) with surface tension (sigma) by Straka (2009)
+       sigma = 0.0761_wp - 0.000155_wp * ( t_int - 273.15_wp )
+!
+!--    Solute effect (afactor)
+       afactor = 2.0_wp * sigma / ( rho_l * r_v * t_int )
+
+       DO  n = 1, number_of_particles
+!
+!--       Solute effect (bfactor)
+          bfactor = vanthoff * rho_s * particles(n)%aux1**3 *                    &
+                    molecular_weight_of_water / ( rho_l * molecular_weight_of_solute )
+
+          dt_ros     = particles(n)%aux2  ! use previously stored Rosenbrock timestep
+          dt_ros_sum = 0.0_wp
+
+          r_ros     = particles(n)%radius  ! initialize Rosenbrock particle radius
+          r_ros_ini = r_ros
+!
+!--       Integrate growth equation using a 2nd-order Rosenbrock method
+!--       (see Verwer et al., 1999, Eq. (3.2)). The Rosenbrock method adjusts
+!--       its with internal timestep to minimize the local truncation error.
+          DO WHILE ( dt_ros_sum < dt_3d )
+
+             dt_ros = MIN( dt_ros, dt_3d - dt_ros_sum )
+
+             DO
+
+                drdt = ddenom * ventilation_effect(n) * ( e_a / e_s - 1.0 -    &
+                                                          afactor / r_ros +    &
+                                                          bfactor / r_ros**3   &
+                                                        ) / ( r_ros + r0 )
+
+                d2rdtdr = -ddenom * ventilation_effect(n) * (                  &
+                                                (e_a / e_s - 1.0) * r_ros**4 - &
+                                                afactor * r0 * r_ros**2 -      &
+                                                2.0 * afactor * r_ros**3 +     &
+                                                3.0 * bfactor * r0 +           &
+                                                4.0 * bfactor * r_ros          &
+                                                            )                  &
+                          / ( r_ros**4 * ( r_ros + r0 )**2 )
+
+                k1    = drdt / ( 1.0 - gamma * dt_ros * d2rdtdr )
+
+                r_ros = MAX(r_ros_ini + k1 * dt_ros, particles(n)%aux1)
+                r_err = r_ros
+
+                drdt  = ddenom * ventilation_effect(n) * ( e_a / e_s - 1.0 -   &
+                                                           afactor / r_ros +   &
+                                                           bfactor / r_ros**3  &
+                                                         ) / ( r_ros + r0 )
+
+                k2 = ( drdt - dt_ros * 2.0 * gamma * d2rdtdr * k1 ) / &
+                     ( 1.0 - dt_ros * gamma * d2rdtdr )
+
+                r_ros = MAX(r_ros_ini + dt_ros * ( 1.5 * k1 + 0.5 * k2), particles(n)%aux1)
+   !
+   !--          Check error of the solution, and reduce dt_ros if necessary.
+                error = ABS(r_err - r_ros) / r_ros
+                IF ( error .GT. prec )  THEN
+                   dt_ros = SQRT( q_decrease * prec / error ) * dt_ros
+                   r_ros  = r_ros_ini
+                ELSE
+                   dt_ros_sum = dt_ros_sum + dt_ros
+                   dt_ros     = q_increase * dt_ros
+                   r_ros_ini  = r_ros
+                   EXIT
+                ENDIF
+
+             END DO
+
+          END DO !Rosenbrock loop
+!
+!--       Store new particle radius
+          new_r(n) = r_ros
+!
+!--       Store internal time step value for next PALM step
+          particles(n)%aux2 = dt_ros
+
+       ENDDO !Particle loop
+
+    ENDIF
+
+    DO  n = 1, number_of_particles
+!
+!--    Sum up the change in liquid water for the respective grid
+!--    box for the computation of the release/depletion of water vapor
+!--    and heat.
+       ql_c(k,j,i) = ql_c(k,j,i) + particles(n)%weight_factor *          &
+                                   rho_l * 1.33333333_wp * pi *                &
+                                   ( new_r(n)**3 - particles(n)%radius**3 ) /  &
+                                   ( rho_surface * dx * dy * dzw(k) )
+!
+!--    Check if the increase in liqid water is not too big. If this is the case,
+!--    the model timestep might be too long.
+       IF ( ql_c(k,j,i) > 100.0_wp )  THEN
+          WRITE( message_string, * ) 'k=',k,' j=',j,' i=',i,                &
+                       ' ql_c=',ql_c(k,j,i), '&part(',n,')%wf=',            &
+                       particles(n)%weight_factor,' delta_r=',delta_r
+          CALL message( 'lpm_droplet_condensation', 'PA0143', 2, 2, -1, 6, 1 )
+       ENDIF
+!
+!--    Check if the change in the droplet radius is not too big. If this is the
+!--    case, the model timestep might be too long.
+       delta_r = new_r(n) - particles(n)%radius
+       IF ( delta_r < 0.0_wp  .AND. new_r(n) < 0.0_wp )  THEN
+          WRITE( message_string, * ) '#1 k=',k,' j=',j,' i=',i,             &
+                       ' e_s=',e_s, ' e_a=',e_a,' t_int=',t_int,               &
+                       '&delta_r=',delta_r,                                    &
+                       ' particle_radius=',particles(n)%radius
+          CALL message( 'lpm_droplet_condensation', 'PA0144', 2, 2, -1, 6, 1 )
+       ENDIF
+!
+!--    Sum up the total volume of liquid water (needed below for
+!--    re-calculating the weighting factors)
+       ql_v(k,j,i) = ql_v(k,j,i) + particles(n)%weight_factor * new_r(n)**3
+!
+!--    Determine radius class of the particle needed for collision
+       IF ( use_kernel_tables )  THEN
+          particles(n)%class = ( LOG( new_r(n) ) - rclass_lbound ) /           &
+                               ( rclass_ubound - rclass_lbound ) *             &
+                               radius_classes
+          particles(n)%class = MIN( particles(n)%class, radius_classes )
+          particles(n)%class = MAX( particles(n)%class, 1 )
+       ENDIF
+ !
+ !--   Store new radius to particle features
+       particles(n)%radius = new_r(n)
+
+    ENDDO
+
+    CALL cpu_log( log_point_s(42), 'lpm_droplet_condens', 'stop' )
+
+
+ END SUBROUTINE lpm_droplet_condensation
+ 
+ 
+!------------------------------------------------------------------------------! 
+! Description:
+! ------------
+!> Calculate the liquid water content for each grid box.
+!------------------------------------------------------------------------------!
+ MODULE SUBROUTINE lpm_calc_liquid_water_content
+
+
+    INTEGER(iwp) ::  i   !<
+    INTEGER(iwp) ::  j   !<
+    INTEGER(iwp) ::  k   !<
+    INTEGER(iwp) ::  n   !<
+
+    CALL cpu_log( log_point_s(45), 'lpm_calc_ql', 'start' )
+
+!
+!-- Set water content initially to zero
+    ql = 0.0_wp;  ql_v = 0.0_wp;  ql_vp = 0.0_wp
+
+!
+!-- Calculate for each grid box
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+             number_of_particles = prt_count(k,j,i)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+!
+!--          Calculate the total volume in the boxes (ql_v, weighting factor
+!--          has to beincluded)
+             DO  n = 1, prt_count(k,j,i)
+                ql_v(k,j,i)  = ql_v(k,j,i)  + particles(n)%weight_factor *     &
+                                              particles(n)%radius**3
+             ENDDO
+!
+!--          Calculate the liquid water content
+             IF ( ql_v(k,j,i) /= 0.0_wp )  THEN
+                ql(k,j,i) = ql(k,j,i) + rho_l * 1.33333333_wp * pi *           &
+                                        ql_v(k,j,i) /                          &
+                                        ( rho_surface * dx * dy * dzw(k) )
+                IF ( ql(k,j,i) < 0.0_wp )  THEN
+                   WRITE( message_string, * )  'LWC out of range: ' , &
+                                               ql(k,j,i),i,j,k
+                   CALL message( 'lpm_calc_liquid_water_content', '', 2, 2,    &
+                                 -1, 6, 1 )
+                ENDIF
+             ELSE
+                ql(k,j,i) = 0.0_wp
+             ENDIF
+          ENDDO
+       ENDDO
+    ENDDO
+
+    CALL cpu_log( log_point_s(45), 'lpm_calc_ql', 'stop' )
+
+ END SUBROUTINE lpm_calc_liquid_water_content
+
+ 
+!------------------------------------------------------------------------------! 
+! Description:
+! ------------
+!> Calculates change in droplet radius by collision. Droplet collision is
+!> calculated for each grid box seperately. Collision is parameterized by
+!> using collision kernels. Two different kernels are available:
+!> Hall kernel: Kernel from Hall (1980, J. Atmos. Sci., 2486-2507), which
+!>              considers collision due to pure gravitational effects.
+!> Wang kernel: Beside gravitational effects (treated with the Hall-kernel) also
+!>              the effects of turbulence on the collision are considered using
+!>              parameterizations of Ayala et al. (2008, New J. Phys., 10,
+!>              075015) and Wang and Grabowski (2009, Atmos. Sci. Lett., 10,
+!>              1-8). This kernel includes three possible effects of turbulence:
+!>              the modification of the relative velocity between the droplets,
+!>              the effect of preferential concentration, and the enhancement of
+!>              collision efficiencies.
+!------------------------------------------------------------------------------! 
+ MODULE SUBROUTINE lpm_droplet_collision (i,j,k)
+    
+    INTEGER(iwp), INTENT(IN) ::  i        !<
+    INTEGER(iwp), INTENT(IN) ::  j        !<
+    INTEGER(iwp), INTENT(IN) ::  k        !<
+
+    INTEGER(iwp) ::  eclass   !<
+    INTEGER(iwp) ::  n        !<
+    INTEGER(iwp) ::  m        !<
+    INTEGER(iwp) ::  rclass_l !<
+    INTEGER(iwp) ::  rclass_s !<
+
+    REAL(wp) ::  collection_probability  !< probability for collection
+    REAL(wp) ::  ddV                     !< inverse grid box volume
+    REAL(wp) ::  epsilon                 !< dissipation rate
+    REAL(wp) ::  factor_volume_to_mass   !< 4.0 / 3.0 * pi * rho_l
+    REAL(wp) ::  xm                      !< droplet mass of super-droplet m
+    REAL(wp) ::  xn                      !< droplet mass of super-droplet n
+    REAL(wp) ::  xsm                     !< aerosol mass of super-droplet m
+    REAL(wp) ::  xsn                     !< aerosol mass of super-droplet n
+
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  weight    !< weighting factor
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  mass      !< total mass of super droplet
+    REAL(wp), DIMENSION(:), ALLOCATABLE ::  aero_mass !< total aerosol mass of super droplet
+
+    CALL cpu_log( log_point_s(43), 'lpm_droplet_coll', 'start' )
+
+    number_of_particles   = prt_count(k,j,i)
+    factor_volume_to_mass = 4.0_wp / 3.0_wp * pi * rho_l
+    ddV                   = 1.0_wp / ( dx * dy * dzw(k) )
+!
+!-- Collision requires at least one super droplet inside the box
+    IF ( number_of_particles > 0 )  THEN
+
+       IF ( use_kernel_tables )  THEN
+!
+!--       Fast method with pre-calculated collection kernels for
+!--       discrete radius- and dissipation-classes.
+          IF ( wang_kernel )  THEN
+             eclass = INT( diss(k,j,i) * 1.0E4_wp / 600.0_wp * &
+                           dissipation_classes ) + 1
+             epsilon = diss(k,j,i)
+          ELSE
+             epsilon = 0.0_wp
+          ENDIF
+
+          IF ( hall_kernel  .OR.  epsilon * 1.0E4_wp < 0.001_wp )  THEN
+             eclass = 0   ! Hall kernel is used
+          ELSE
+             eclass = MIN( dissipation_classes, eclass )
+          ENDIF
+
+       ELSE
+!
+!--       Collection kernels are re-calculated for every new
+!--       grid box. First, allocate memory for kernel table.
+!--       Third dimension is 1, because table is re-calculated for
+!--       every new dissipation value.
+          ALLOCATE( ckernel(1:number_of_particles,1:number_of_particles,1:1) )
+!
+!--       Now calculate collection kernel for this box. Note that
+!--       the kernel is based on the previous time step
+          CALL recalculate_kernel( i, j, k )
+
+       ENDIF
+!
+!--    Temporary fields for total mass of super-droplet, aerosol mass, and
+!--    weighting factor are allocated.
+       ALLOCATE(mass(1:number_of_particles), weight(1:number_of_particles))
+       IF ( curvature_solution_effects )  ALLOCATE(aero_mass(1:number_of_particles))
+
+       mass(1:number_of_particles)   = particles(1:number_of_particles)%weight_factor * &
+                                       particles(1:number_of_particles)%radius**3     * &
+                                       factor_volume_to_mass
+
+       weight(1:number_of_particles) = particles(1:number_of_particles)%weight_factor
+
+       IF ( curvature_solution_effects )  THEN
+          aero_mass(1:number_of_particles) = particles(1:number_of_particles)%weight_factor * &
+                                             particles(1:number_of_particles)%aux1**3       * &
+                                             4.0 / 3.0 * pi * rho_s
+       ENDIF
+!
+!--    Calculate collision/coalescence
+       DO  n = 1, number_of_particles
+
+          DO  m = n, number_of_particles
+!
+!--          For collisions, the weighting factor of at least one super-droplet
+!--          needs to be larger or equal to one.
+             IF ( MIN( weight(n), weight(m) ) .LT. 1.0 )  CYCLE
+!
+!--          Get mass of individual droplets (aerosols)
+             xn = mass(n) / weight(n)
+             xm = mass(m) / weight(m)
+             IF ( curvature_solution_effects )  THEN
+                xsn = aero_mass(n) / weight(n)
+                xsm = aero_mass(m) / weight(m)
+             ENDIF
+!
+!--          Probability that the necessary collisions take place
+             IF ( use_kernel_tables )  THEN
+                rclass_l = particles(n)%class
+                rclass_s = particles(m)%class
+
+                collection_probability  = MAX( weight(n), weight(m) ) *     &
+                                          ckernel(rclass_l,rclass_s,eclass) * ddV * dt_3d
+             ELSE
+                collection_probability  = MAX( weight(n), weight(m) ) *     &
+                                          ckernel(n,m,1) * ddV * dt_3d
+             ENDIF
+!
+!--          Calculate the number of collections and consider multiple collections.
+!--          (Accordingly, p_crit will be 0.0, 1.0, 2.0, ...)
+             IF ( collection_probability - FLOOR(collection_probability)    &
+                  .GT. random_function( iran_part ) )  THEN
+                collection_probability = FLOOR(collection_probability) + 1.0_wp
+             ELSE
+                collection_probability = FLOOR(collection_probability)
+             ENDIF
+
+             IF ( collection_probability .GT. 0.0 )  THEN
+!
+!--             Super-droplet n collects droplets of super-droplet m
+                IF ( weight(n) .LT. weight(m) )  THEN
+
+                   mass(n)   = mass(n)   + weight(n) * xm * collection_probability
+                   weight(m) = weight(m) - weight(n)      * collection_probability
+                   mass(m)   = mass(m)   - weight(n) * xm * collection_probability
+                   IF ( curvature_solution_effects )  THEN
+                      aero_mass(n) = aero_mass(n) + weight(n) * xsm * collection_probability
+                      aero_mass(m) = aero_mass(m) - weight(n) * xsm * collection_probability
+                   ENDIF
+
+                ELSEIF ( weight(m) .LT. weight(n) )  THEN
+
+                   mass(m)   = mass(m)   + weight(m) * xn * collection_probability
+                   weight(n) = weight(n) - weight(m)      * collection_probability
+                   mass(n)   = mass(n)   - weight(m) * xn * collection_probability
+                   IF ( curvature_solution_effects )  THEN
+                      aero_mass(m) = aero_mass(m) + weight(m) * xsn * collection_probability
+                      aero_mass(n) = aero_mass(n) - weight(m) * xsn * collection_probability
+                   ENDIF
+
+                ELSE
+!
+!--                Collisions of particles of the same weighting factor.
+!--                Particle n collects 1/2 weight(n) droplets of particle m,
+!--                particle m collects 1/2 weight(m) droplets of particle n.
+!--                The total mass mass changes accordingly.
+!--                If n = m, the first half of the droplets coalesces with the
+!--                second half of the droplets; mass is unchanged because
+!--                xm = xn for n = m.
+!--
+!--                Note: For m = n this equation is an approximation only
+!--                valid for weight >> 1 (which is usually the case). The
+!--                approximation is weight(n)-1 = weight(n).
+                   mass(n)   = mass(n)   + 0.5_wp * weight(n) * ( xm - xn )
+                   mass(m)   = mass(m)   + 0.5_wp * weight(m) * ( xn - xm )
+                   IF ( curvature_solution_effects )  THEN
+                      aero_mass(n) = aero_mass(n) + 0.5_wp * weight(n) * ( xsm - xsn )
+                      aero_mass(m) = aero_mass(m) + 0.5_wp * weight(m) * ( xsn - xsm )
+                   ENDIF
+                   weight(n) = weight(n) - 0.5_wp * weight(m)
+                   weight(m) = weight(n)
+
+                ENDIF
+
+             ENDIF
+
+          ENDDO
+
+          ql_vp(k,j,i) = ql_vp(k,j,i) + mass(n) / factor_volume_to_mass
+
+       ENDDO
+
+       IF ( ANY(weight < 0.0_wp) )  THEN
+             WRITE( message_string, * ) 'negative weighting factor'
+             CALL message( 'lpm_droplet_collision', 'PA0028',      &
+                            2, 2, -1, 6, 1 )
+       ENDIF
+
+       particles(1:number_of_particles)%radius = ( mass(1:number_of_particles) /   &
+                                                   ( weight(1:number_of_particles) &
+                                                     * factor_volume_to_mass       &
+                                                   )                               &
+                                                 )**0.33333333333333_wp
+
+       IF ( curvature_solution_effects )  THEN
+          particles(1:number_of_particles)%aux1 = ( aero_mass(1:number_of_particles) / &
+                                                    ( weight(1:number_of_particles)    &
+                                                      * 4.0_wp / 3.0_wp * pi * rho_s   &
+                                                    )                                  &
+                                                  )**0.33333333333333_wp
+       ENDIF
+
+       particles(1:number_of_particles)%weight_factor = weight(1:number_of_particles)
+
+       DEALLOCATE( weight, mass )
+       IF ( curvature_solution_effects )  DEALLOCATE( aero_mass )
+       IF ( .NOT. use_kernel_tables )  DEALLOCATE( ckernel )
+
+!
+!--    Check if LWC is conserved during collision process
+       IF ( ql_v(k,j,i) /= 0.0_wp )  THEN
+          IF ( ql_vp(k,j,i) / ql_v(k,j,i) >= 1.0001_wp  .OR.                      &
+               ql_vp(k,j,i) / ql_v(k,j,i) <= 0.9999_wp )  THEN
+             WRITE( message_string, * ) ' LWC is not conserved during',           &
+                                        ' collision! ',                           &
+                                        ' LWC after condensation: ', ql_v(k,j,i), &
+                                        ' LWC after collision: ', ql_vp(k,j,i)
+             CALL message( 'lpm_droplet_collision', 'PA0040', 2, 2, -1, 6, 1 )
+          ENDIF
+       ENDIF
+
+    ENDIF
+
+    CALL cpu_log( log_point_s(43), 'lpm_droplet_coll', 'stop' )
+
+ END SUBROUTINE lpm_droplet_collision
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Initialization of the collision efficiency matrix with fixed radius and
+!> dissipation classes, calculated at simulation start only.
+!------------------------------------------------------------------------------! 
+ MODULE SUBROUTINE lpm_init_kernels
+
+    INTEGER(iwp) ::  i !<
+    INTEGER(iwp) ::  j !<
+    INTEGER(iwp) ::  k !<
+    
+!
+!-- Calculate collision efficiencies for fixed radius- and dissipation
+!-- classes
+    IF ( collision_kernel(6:9) == 'fast' )  THEN
+
+       ALLOCATE( ckernel(1:radius_classes,1:radius_classes,                 &
+                 0:dissipation_classes), epsclass(1:dissipation_classes),   &
+                 radclass(1:radius_classes) )
+
+!
+!--    Calculate the radius class bounds with logarithmic distances
+!--    in the interval [1.0E-6, 1000.0E-6] m
+       rclass_lbound = LOG( 1.0E-6_wp )
+       rclass_ubound = LOG( 1000.0E-6_wp )
+       radclass(1)   = EXP( rclass_lbound )
+       DO  i = 2, radius_classes
+          radclass(i) = EXP( rclass_lbound +                                &
+                             ( rclass_ubound - rclass_lbound ) *            &
+                             ( i - 1.0_wp ) / ( radius_classes - 1.0_wp ) )
+       ENDDO
+
+!
+!--    Set the class bounds for dissipation in interval [0.0, 600.0] cm**2/s**3
+       DO  i = 1, dissipation_classes
+          epsclass(i) = 0.06_wp * REAL( i, KIND=wp ) / dissipation_classes
+       ENDDO
+!
+!--    Calculate collision efficiencies of the Wang/ayala kernel
+       ALLOCATE( ec(1:radius_classes,1:radius_classes),  &
+                 ecf(1:radius_classes,1:radius_classes), &
+                 gck(1:radius_classes,1:radius_classes), &
+                 winf(1:radius_classes) )
+
+       DO  k = 1, dissipation_classes
+
+          epsilon = epsclass(k)
+          urms    = 2.02_wp * ( epsilon / 0.04_wp )**( 1.0_wp / 3.0_wp )
+
+          CALL turbsd
+          CALL turb_enhance_eff
+          CALL effic
+
+          DO  j = 1, radius_classes
+             DO  i = 1, radius_classes
+                ckernel(i,j,k) = ec(i,j) * gck(i,j) * ecf(i,j)
+             ENDDO
+          ENDDO
+
+       ENDDO
+
+!
+!--    Calculate collision efficiencies of the Hall kernel
+       ALLOCATE( hkernel(1:radius_classes,1:radius_classes), &
+                 hwratio(1:radius_classes,1:radius_classes) )
+
+       CALL fallg
+       CALL effic
+
+       DO  j = 1, radius_classes
+          DO  i =  1, radius_classes
+             hkernel(i,j) = pi * ( radclass(j) + radclass(i) )**2 &
+                               * ec(i,j) * ABS( winf(j) - winf(i) )
+             ckernel(i,j,0) = hkernel(i,j)  ! hall kernel stored on index 0
+           ENDDO
+       ENDDO
+
+!
+!--    Test output of efficiencies
+       IF ( j == -1 )  THEN
+          PRINT*, '*** Hall kernel'
+          WRITE ( *,'(5X,20(F4.0,1X))' ) ( radclass(i)*1.0E6_wp, &
+                                           i = 1,radius_classes )
+          DO  j = 1, radius_classes
+             WRITE ( *,'(F4.0,1X,20(F8.4,1X))' ) radclass(j),  &
+                                       ( hkernel(i,j), i = 1,radius_classes )
+          ENDDO
+          
+          DO  k = 1, dissipation_classes
+             DO  i = 1, radius_classes
+                DO  j = 1, radius_classes
+                   IF ( hkernel(i,j) == 0.0_wp )  THEN
+                      hwratio(i,j) = 9999999.9_wp
+                   ELSE
+                      hwratio(i,j) = ckernel(i,j,k) / hkernel(i,j)
+                   ENDIF
+                ENDDO
+             ENDDO
+
+             PRINT*, '*** epsilon = ', epsclass(k)
+             WRITE ( *,'(5X,20(F4.0,1X))' ) ( radclass(i) * 1.0E6_wp, &
+                                              i = 1,radius_classes )
+             DO  j = 1, radius_classes
+                WRITE ( *,'(F4.0,1X,20(F8.4,1X))' ) radclass(j) * 1.0E6_wp, &
+                                       ( hwratio(i,j), i = 1,radius_classes )
+             ENDDO
+          ENDDO
+       ENDIF
+
+       DEALLOCATE( ec, ecf, epsclass, gck, hkernel, winf )
+
+    ENDIF
+
+ END SUBROUTINE lpm_init_kernels
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Calculation of collision kernels during each timestep and for each grid box
+!------------------------------------------------------------------------------!
+ SUBROUTINE recalculate_kernel( i1, j1, k1 )
+
+
+    INTEGER(iwp) ::  i      !<
+    INTEGER(iwp) ::  i1     !<
+    INTEGER(iwp) ::  j      !<
+    INTEGER(iwp) ::  j1     !<
+    INTEGER(iwp) ::  k1     !<
+
+
+    number_of_particles = prt_count(k1,j1,i1)
+    radius_classes      = number_of_particles   ! necessary to use the same
+                                                ! subroutines as for 
+                                                ! precalculated kernels
+
+    ALLOCATE( ec(1:number_of_particles,1:number_of_particles), &
+              radclass(1:number_of_particles), winf(1:number_of_particles) )
+
+!
+!-- Store particle radii on the radclass array
+    radclass(1:number_of_particles) = particles(1:number_of_particles)%radius
+
+    IF ( wang_kernel )  THEN
+       epsilon = diss(k1,j1,i1)   ! dissipation rate in m**2/s**3
+    ELSE
+       epsilon = 0.0_wp
+    ENDIF
+    urms    = 2.02_wp * ( epsilon / 0.04_wp )**( 0.33333333333_wp )
+
+    IF ( wang_kernel  .AND.  epsilon > 1.0E-7_wp )  THEN
+!
+!--    Call routines to calculate efficiencies for the Wang kernel
+       ALLOCATE( gck(1:number_of_particles,1:number_of_particles), &
+                 ecf(1:number_of_particles,1:number_of_particles) )
+
+       CALL turbsd
+       CALL turb_enhance_eff
+       CALL effic
+
+       DO  j = 1, number_of_particles
+          DO  i =  1, number_of_particles
+             ckernel(1+i-1,1+j-1,1) = ec(i,j) * gck(i,j) * ecf(i,j)
+          ENDDO
+       ENDDO
+
+       DEALLOCATE( gck, ecf )
+    ELSE
+!
+!--    Call routines to calculate efficiencies for the Hall kernel
+       CALL fallg
+       CALL effic
+
+       DO  j = 1, number_of_particles
+          DO  i =  1, number_of_particles
+             ckernel(i,j,1) = pi * ( radclass(j) + radclass(i) )**2         &
+                                 * ec(i,j) * ABS( winf(j) - winf(i) )
+          ENDDO
+       ENDDO
+    ENDIF
+
+    DEALLOCATE( ec, radclass, winf )
+
+ END SUBROUTINE recalculate_kernel
+
+!------------------------------------------------------------------------------! 
+! Description:
+! ------------
+!> Calculation of effects of turbulence on the geometric collision kernel 
+!> (by including the droplets' average radial relative velocities and their 
+!> radial distribution function) following the analytic model by Aayala et al. 
+!> (2008, New J. Phys.). For details check the second part 2 of the publication,
+!> page 37ff.
+!>
+!> Input parameters, which need to be replaced by PALM parameters:
+!>    water density, air density
+!------------------------------------------------------------------------------!
+ SUBROUTINE turbsd
+
+    INTEGER(iwp) ::  i     !<
+    INTEGER(iwp) ::  j     !<
+
+    REAL(wp) ::  ao        !<
+    REAL(wp) ::  ao_gr     !<
+    REAL(wp) ::  bbb       !<
+    REAL(wp) ::  be        !<
+    REAL(wp) ::  b1        !<
+    REAL(wp) ::  b2        !<
+    REAL(wp) ::  ccc       !<
+    REAL(wp) ::  c1        !<
+    REAL(wp) ::  c1_gr     !<
+    REAL(wp) ::  c2        !<
+    REAL(wp) ::  d1        !<
+    REAL(wp) ::  d2        !<
+    REAL(wp) ::  eta       !<
+    REAL(wp) ::  e1        !<
+    REAL(wp) ::  e2        !<
+    REAL(wp) ::  fao_gr    !<
+    REAL(wp) ::  fr        !<
+    REAL(wp) ::  grfin     !<
+    REAL(wp) ::  lambda    !<
+    REAL(wp) ::  lambda_re !<
+    REAL(wp) ::  lf        !<
+    REAL(wp) ::  rc        !<
+    REAL(wp) ::  rrp       !<
+    REAL(wp) ::  sst       !<
+    REAL(wp) ::  tauk      !<
+    REAL(wp) ::  tl        !<
+    REAL(wp) ::  t2        !<
+    REAL(wp) ::  tt        !<
+    REAL(wp) ::  t1        !<
+    REAL(wp) ::  vk        !<
+    REAL(wp) ::  vrms1xy   !<
+    REAL(wp) ::  vrms2xy   !<
+    REAL(wp) ::  v1        !<
+    REAL(wp) ::  v1v2xy    !<
+    REAL(wp) ::  v1xysq    !<
+    REAL(wp) ::  v2        !<
+    REAL(wp) ::  v2xysq    !<
+    REAL(wp) ::  wrfin     !<
+    REAL(wp) ::  wrgrav2   !<
+    REAL(wp) ::  wrtur2xy  !<
+    REAL(wp) ::  xx        !<
+    REAL(wp) ::  yy        !<
+    REAL(wp) ::  z         !<
+
+    REAL(wp), DIMENSION(1:radius_classes) ::  st  !< Stokes number
+    REAL(wp), DIMENSION(1:radius_classes) ::  tau !< inertial time scale
+
+    lambda    = urms * SQRT( 15.0_wp * molecular_viscosity / epsilon )
+    lambda_re = urms**2 * SQRT( 15.0_wp / epsilon / molecular_viscosity )
+    tl        = urms**2 / epsilon
+    lf        = 0.5_wp * urms**3 / epsilon
+    tauk      = SQRT( molecular_viscosity / epsilon )
+    eta       = ( molecular_viscosity**3 / epsilon )**0.25_wp
+    vk        = eta / tauk
+
+    ao = ( 11.0_wp + 7.0_wp * lambda_re ) / ( 205.0_wp + lambda_re )
+    tt = SQRT( 2.0_wp * lambda_re / ( SQRT( 15.0_wp ) * ao ) ) * tauk
+
+!
+!-- Get terminal velocity of droplets
+    CALL fallg
+
+    DO  i = 1, radius_classes
+       tau(i) = winf(i) / g    ! inertial time scale
+       st(i)  = tau(i) / tauk  ! Stokes number
+    ENDDO
+
+!
+!-- Calculate average radial relative velocity at contact (wrfin)
+    z   = tt / tl
+    be  = SQRT( 2.0_wp ) * lambda / lf
+    bbb = SQRT( 1.0_wp - 2.0_wp * be**2 )
+    d1  = ( 1.0_wp + bbb ) / ( 2.0_wp * bbb )
+    e1  = lf * ( 1.0_wp + bbb ) * 0.5_wp
+    d2  = ( 1.0_wp - bbb ) * 0.5_wp / bbb
+    e2  = lf * ( 1.0_wp - bbb ) * 0.5_wp
+    ccc = SQRT( 1.0_wp - 2.0_wp * z**2 )
+    b1  = ( 1.0_wp + ccc ) * 0.5_wp / ccc
+    c1  = tl * ( 1.0_wp + ccc ) * 0.5_wp
+    b2  = ( 1.0_wp - ccc ) * 0.5_wp / ccc
+    c2  = tl * ( 1.0_wp - ccc ) * 0.5_wp
+
+    DO  i = 1, radius_classes
+
+       v1 = winf(i)
+       t1 = tau(i)
+
+       DO  j = 1, i
+          rrp = radclass(i) + radclass(j)
+          v2  = winf(j)
+          t2  = tau(j)
+
+          v1xysq  = b1 * d1 * phi_w(c1,e1,v1,t1) - b1 * d2 * phi_w(c1,e2,v1,t1) &
+                  - b2 * d1 * phi_w(c2,e1,v1,t1) + b2 * d2 * phi_w(c2,e2,v1,t1)
+          v1xysq  = v1xysq * urms**2 / t1
+          vrms1xy = SQRT( v1xysq )
+
+          v2xysq  = b1 * d1 * phi_w(c1,e1,v2,t2) - b1 * d2 * phi_w(c1,e2,v2,t2) &
+                  - b2 * d1 * phi_w(c2,e1,v2,t2) + b2 * d2 * phi_w(c2,e2,v2,t2)
+          v2xysq  = v2xysq * urms**2 / t2
+          vrms2xy = SQRT( v2xysq )
+
+          IF ( winf(i) >= winf(j) )  THEN
+             v1 = winf(i)
+             t1 = tau(i)
+             v2 = winf(j)
+             t2 = tau(j)
+          ELSE
+             v1 = winf(j)
+             t1 = tau(j)
+             v2 = winf(i)
+             t2 = tau(i)
+          ENDIF
+
+          v1v2xy   =  b1 * d1 * zhi(c1,e1,v1,t1,v2,t2) - &
+                      b1 * d2 * zhi(c1,e2,v1,t1,v2,t2) - &
+                      b2 * d1 * zhi(c2,e1,v1,t1,v2,t2) + &
+                      b2 * d2* zhi(c2,e2,v1,t1,v2,t2)
+          fr       = d1 * EXP( -rrp / e1 ) - d2 * EXP( -rrp / e2 )
+          v1v2xy   = v1v2xy * fr * urms**2 / tau(i) / tau(j)
+          wrtur2xy = vrms1xy**2 + vrms2xy**2 - 2.0_wp * v1v2xy
+          IF ( wrtur2xy < 0.0_wp )  wrtur2xy = 0.0_wp
+          wrgrav2  = pi / 8.0_wp * ( winf(j) - winf(i) )**2
+          wrfin    = SQRT( ( 2.0_wp / pi ) * ( wrtur2xy + wrgrav2) )
+
+!
+!--       Calculate radial distribution function (grfin)
+          IF ( st(j) > st(i) )  THEN
+             sst = st(j)
+          ELSE
+             sst = st(i)
+          ENDIF
+
+          xx = -0.1988_wp * sst**4 + 1.5275_wp * sst**3 - 4.2942_wp *       &
+                sst**2 + 5.3406_wp * sst
+          IF ( xx < 0.0_wp )  xx = 0.0_wp
+          yy = 0.1886_wp * EXP( 20.306_wp / lambda_re )
+
+          c1_gr  =  xx / ( g / vk * tauk )**yy
+
+          ao_gr  = ao + ( pi / 8.0_wp) * ( g / vk * tauk )**2
+          fao_gr = 20.115_wp * SQRT( ao_gr / lambda_re )
+          rc     = SQRT( fao_gr * ABS( st(j) - st(i) ) ) * eta
+
+          grfin  = ( ( eta**2 + rc**2 ) / ( rrp**2 + rc**2) )**( c1_gr*0.5_wp )
+          IF ( grfin < 1.0_wp )  grfin = 1.0_wp
+
+!
+!--       Calculate general collection kernel (without the consideration of
+!--       collection efficiencies)
+          gck(i,j) = 2.0_wp * pi * rrp**2 * wrfin * grfin
+          gck(j,i) = gck(i,j)
+
+       ENDDO
+    ENDDO
+
+ END SUBROUTINE turbsd
+
+ REAL(wp) FUNCTION phi_w( a, b, vsett, tau0 )
+!
+!-- Function used in the Ayala et al. (2008) analytical model for turbulent
+!-- effects on the collision kernel
+    
+
+    REAL(wp) ::  a     !<
+    REAL(wp) ::  aa1   !<
+    REAL(wp) ::  b     !<
+    REAL(wp) ::  tau0  !<
+    REAL(wp) ::  vsett !<
+
+    aa1 = 1.0_wp / tau0 + 1.0_wp / a + vsett / b
+    phi_w = 1.0_wp / aa1  - 0.5_wp * vsett / b / aa1**2
+
+ END FUNCTION phi_w
+
+ REAL(wp) FUNCTION zhi( a, b, vsett1, tau1, vsett2, tau2 )
+!
+!-- Function used in the Ayala et al. (2008) analytical model for turbulent
+!-- effects on the collision kernel
+
+    REAL(wp) ::  a      !<
+    REAL(wp) ::  aa1    !<
+    REAL(wp) ::  aa2    !<
+    REAL(wp) ::  aa3    !<
+    REAL(wp) ::  aa4    !<
+    REAL(wp) ::  aa5    !<
+    REAL(wp) ::  aa6    !<
+    REAL(wp) ::  b      !<
+    REAL(wp) ::  tau1   !<
+    REAL(wp) ::  tau2   !<
+    REAL(wp) ::  vsett1 !<
+    REAL(wp) ::  vsett2 !<
+
+    aa1 = vsett2 / b - 1.0_wp / tau2 - 1.0_wp / a
+    aa2 = vsett1 / b + 1.0_wp / tau1 + 1.0_wp / a
+    aa3 = ( vsett1 - vsett2 ) / b + 1.0_wp / tau1 + 1.0_wp / tau2
+    aa4 = ( vsett2 / b )**2 - ( 1.0_wp / tau2 + 1.0_wp / a )**2
+    aa5 = vsett2 / b + 1.0_wp / tau2 + 1.0_wp / a
+    aa6 = 1.0_wp / tau1 - 1.0_wp / a + ( 1.0_wp / tau2 + 1.0_wp / a) *      &
+          vsett1 / vsett2
+    zhi = (1.0_wp / aa1 - 1.0_wp / aa2 ) * ( vsett1 - vsett2 ) * 0.5_wp /   &
+          b / aa3**2 + ( 4.0_wp / aa4 - 1.0_wp / aa5**2 - 1.0_wp / aa1**2 ) &
+          * vsett2 * 0.5_wp / b /aa6 + ( 2.0_wp * ( b / aa2 - b / aa1 ) -   &
+          vsett1 / aa2**2 + vsett2 / aa1**2 ) * 0.5_wp / b / aa3
+
+ END FUNCTION zhi
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Parameterization of terminal velocity following Rogers et al. (1993, J. Appl.
+!> Meteorol.)
+!------------------------------------------------------------------------------!
+ SUBROUTINE fallg
+
+    INTEGER(iwp) ::  j                            !<
+
+    REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp    !< parameter
+    REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp   !< parameter
+    REAL(wp), PARAMETER ::  a_rog     = 9.65_wp   !< parameter
+    REAL(wp), PARAMETER ::  b_rog     = 10.43_wp  !< parameter
+    REAL(wp), PARAMETER ::  c_rog     = 0.6_wp    !< parameter
+    REAL(wp), PARAMETER ::  d0_rog    = 0.745_wp  !< seperation diameter
+
+    REAL(wp)            ::  diameter              !< droplet diameter in mm
+
+
+    DO  j = 1, radius_classes
+
+       diameter = radclass(j) * 2000.0_wp
+
+       IF ( diameter <= d0_rog )  THEN
+          winf(j) = k_cap_rog * diameter * ( 1.0_wp -                       &
+                                             EXP( -k_low_rog * diameter ) )
+       ELSE
+          winf(j) = a_rog - b_rog * EXP( -c_rog * diameter )
+       ENDIF
+
+    ENDDO
+
+ END SUBROUTINE fallg
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Interpolation of collision efficiencies (Hall, 1980, J. Atmos. Sci.)
+!------------------------------------------------------------------------------!
+ SUBROUTINE effic
+ 
+    INTEGER(iwp) ::  i  !<
+    INTEGER(iwp) ::  iq !<
+    INTEGER(iwp) ::  ir !<
+    INTEGER(iwp) ::  j  !<
+    INTEGER(iwp) ::  k  !<
+
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ira !<
+
+    LOGICAL, SAVE ::  first = .TRUE. !<
+
+    REAL(wp) ::  ek              !<
+    REAL(wp) ::  particle_radius !<
+    REAL(wp) ::  pp              !<
+    REAL(wp) ::  qq              !<
+    REAL(wp) ::  rq              !<
+
+    REAL(wp), DIMENSION(1:21), SAVE ::  rat        !<
+    
+    REAL(wp), DIMENSION(1:15), SAVE ::  r0         !<
+    
+    REAL(wp), DIMENSION(1:15,1:21), SAVE ::  ecoll !<
+
+!
+!-- Initial assignment of constants
+    IF ( first )  THEN
+
+      first = .FALSE.
+      r0  = (/   6.0_wp,   8.0_wp,  10.0_wp, 15.0_wp,  20.0_wp,  25.0_wp,   &
+                30.0_wp,  40.0_wp,  50.0_wp, 60.0_wp,  70.0_wp, 100.0_wp,   &
+               150.0_wp, 200.0_wp, 300.0_wp /)
+
+      rat = (/ 0.00_wp, 0.05_wp, 0.10_wp, 0.15_wp, 0.20_wp, 0.25_wp,        &
+               0.30_wp, 0.35_wp, 0.40_wp, 0.45_wp, 0.50_wp, 0.55_wp,        &
+               0.60_wp, 0.65_wp, 0.70_wp, 0.75_wp, 0.80_wp, 0.85_wp,        &
+               0.90_wp, 0.95_wp, 1.00_wp /)
+
+      ecoll(:,1)  = (/ 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp,    &
+                       0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp,    &
+                       0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp /)
+      ecoll(:,2)  = (/ 0.003_wp, 0.003_wp, 0.003_wp, 0.004_wp, 0.005_wp,    &
+                       0.005_wp, 0.005_wp, 0.010_wp, 0.100_wp, 0.050_wp,    &
+                       0.200_wp, 0.500_wp, 0.770_wp, 0.870_wp, 0.970_wp /)
+      ecoll(:,3)  = (/ 0.007_wp, 0.007_wp, 0.007_wp, 0.008_wp, 0.009_wp,    &
+                       0.010_wp, 0.010_wp, 0.070_wp, 0.400_wp, 0.430_wp,    &
+                       0.580_wp, 0.790_wp, 0.930_wp, 0.960_wp, 1.000_wp /)
+      ecoll(:,4)  = (/ 0.009_wp, 0.009_wp, 0.009_wp, 0.012_wp, 0.015_wp,    &
+                       0.010_wp, 0.020_wp, 0.280_wp, 0.600_wp, 0.640_wp,    &
+                       0.750_wp, 0.910_wp, 0.970_wp, 0.980_wp, 1.000_wp /)
+      ecoll(:,5)  = (/ 0.014_wp, 0.014_wp, 0.014_wp, 0.015_wp, 0.016_wp,    &
+                       0.030_wp, 0.060_wp, 0.500_wp, 0.700_wp, 0.770_wp,    &
+                       0.840_wp, 0.950_wp, 0.970_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,6)  = (/ 0.017_wp, 0.017_wp, 0.017_wp, 0.020_wp, 0.022_wp,    &
+                       0.060_wp, 0.100_wp, 0.620_wp, 0.780_wp, 0.840_wp,    &
+                       0.880_wp, 0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,7)  = (/ 0.030_wp, 0.030_wp, 0.024_wp, 0.022_wp, 0.032_wp,    &
+                       0.062_wp, 0.200_wp, 0.680_wp, 0.830_wp, 0.870_wp,    &
+                       0.900_wp, 0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,8)  = (/ 0.025_wp, 0.025_wp, 0.025_wp, 0.036_wp, 0.043_wp,    &
+                       0.130_wp, 0.270_wp, 0.740_wp, 0.860_wp, 0.890_wp,    &
+                       0.920_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,9)  = (/ 0.027_wp, 0.027_wp, 0.027_wp, 0.040_wp, 0.052_wp,    &
+                       0.200_wp, 0.400_wp, 0.780_wp, 0.880_wp, 0.900_wp,    &
+                       0.940_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,10) = (/ 0.030_wp, 0.030_wp, 0.030_wp, 0.047_wp, 0.064_wp,    &
+                       0.250_wp, 0.500_wp, 0.800_wp, 0.900_wp, 0.910_wp,    &
+                       0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,11) = (/ 0.040_wp, 0.040_wp, 0.033_wp, 0.037_wp, 0.068_wp,    &
+                       0.240_wp, 0.550_wp, 0.800_wp, 0.900_wp, 0.910_wp,    &
+                       0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,12) = (/ 0.035_wp, 0.035_wp, 0.035_wp, 0.055_wp, 0.079_wp,    &
+                       0.290_wp, 0.580_wp, 0.800_wp, 0.900_wp, 0.910_wp,    &
+                       0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,13) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.062_wp, 0.082_wp,    &
+                       0.290_wp, 0.590_wp, 0.780_wp, 0.900_wp, 0.910_wp,    &
+                       0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,14) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.060_wp, 0.080_wp,    &
+                       0.290_wp, 0.580_wp, 0.770_wp, 0.890_wp, 0.910_wp,    &
+                       0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,15) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.041_wp, 0.075_wp,    &
+                       0.250_wp, 0.540_wp, 0.760_wp, 0.880_wp, 0.920_wp,    &
+                       0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,16) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.052_wp, 0.067_wp,    &
+                       0.250_wp, 0.510_wp, 0.770_wp, 0.880_wp, 0.930_wp,    &
+                       0.970_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,17) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.047_wp, 0.057_wp,    &
+                       0.250_wp, 0.490_wp, 0.770_wp, 0.890_wp, 0.950_wp,    &
+                       1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
+      ecoll(:,18) = (/ 0.036_wp, 0.036_wp, 0.036_wp, 0.042_wp, 0.048_wp,    &
+                       0.230_wp, 0.470_wp, 0.780_wp, 0.920_wp, 1.000_wp,    &
+                       1.020_wp, 1.020_wp, 1.020_wp, 1.020_wp, 1.020_wp /)
+      ecoll(:,19) = (/ 0.040_wp, 0.040_wp, 0.035_wp, 0.033_wp, 0.040_wp,    &
+                       0.112_wp, 0.450_wp, 0.790_wp, 1.010_wp, 1.030_wp,    &
+                       1.040_wp, 1.040_wp, 1.040_wp, 1.040_wp, 1.040_wp /)
+      ecoll(:,20) = (/ 0.033_wp, 0.033_wp, 0.033_wp, 0.033_wp, 0.033_wp,    &
+                       0.119_wp, 0.470_wp, 0.950_wp, 1.300_wp, 1.700_wp,    &
+                       2.300_wp, 2.300_wp, 2.300_wp, 2.300_wp, 2.300_wp /)
+      ecoll(:,21) = (/ 0.027_wp, 0.027_wp, 0.027_wp, 0.027_wp, 0.027_wp,    &
+                       0.125_wp, 0.520_wp, 1.400_wp, 2.300_wp, 3.000_wp,    &
+                       4.000_wp, 4.000_wp, 4.000_wp, 4.000_wp, 4.000_wp /)
+    ENDIF
+
+!
+!-- Calculate the radius class index of particles with respect to array r
+!-- Radius has to be in microns
+    ALLOCATE( ira(1:radius_classes) )
+    DO  j = 1, radius_classes
+       particle_radius = radclass(j) * 1.0E6_wp
+       DO  k = 1, 15
+          IF ( particle_radius < r0(k) )  THEN
+             ira(j) = k
+             EXIT
+          ENDIF
+       ENDDO
+       IF ( particle_radius >= r0(15) )  ira(j) = 16
+    ENDDO
+
+!
+!-- Two-dimensional linear interpolation of the collision efficiency.
+!-- Radius has to be in microns
+    DO  j = 1, radius_classes
+       DO  i = 1, j
+
+          ir = MAX( ira(i), ira(j) )
+          rq = MIN( radclass(i) / radclass(j), radclass(j) / radclass(i) )
+          iq = INT( rq * 20 ) + 1
+          iq = MAX( iq , 2)
+
+          IF ( ir < 16 )  THEN
+             IF ( ir >= 2 )  THEN
+                pp = ( ( MAX( radclass(j), radclass(i) ) * 1.0E6_wp ) -     &
+                       r0(ir-1) ) / ( r0(ir) - r0(ir-1) )
+                qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
+                ec(j,i) = ( 1.0_wp - pp ) * ( 1.0_wp - qq )                 &
+                          * ecoll(ir-1,iq-1)                                &
+                          + pp * ( 1.0_wp - qq ) * ecoll(ir,iq-1)           &
+                          + qq * ( 1.0_wp - pp ) * ecoll(ir-1,iq)           &
+                          + pp * qq * ecoll(ir,iq)
+             ELSE
+                qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
+                ec(j,i) = ( 1.0_wp - qq ) * ecoll(1,iq-1) + qq * ecoll(1,iq)
+             ENDIF
+          ELSE
+             qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
+             ek = ( 1.0_wp - qq ) * ecoll(15,iq-1) + qq * ecoll(15,iq)
+             ec(j,i) = MIN( ek, 1.0_wp )
+          ENDIF
+
+          IF ( ec(j,i) < 1.0E-20_wp )  ec(j,i) = 0.0_wp
+
+          ec(i,j) = ec(j,i)
+
+       ENDDO
+    ENDDO
+
+    DEALLOCATE( ira )
+
+ END SUBROUTINE effic
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Interpolation of turbulent enhancement factor for collision efficencies
+!> following Wang and Grabowski (2009, Atmos. Sci. Let.)
+!------------------------------------------------------------------------------!
+ SUBROUTINE turb_enhance_eff
+
+    INTEGER(iwp) :: i  !<
+    INTEGER(iwp) :: iq !<
+    INTEGER(iwp) :: ir !<
+    INTEGER(iwp) :: j  !<
+    INTEGER(iwp) :: k  !<
+    INTEGER(iwp) :: kk !<
+
+    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ira !<
+    
+    LOGICAL, SAVE ::  first = .TRUE. !<
+
+    REAL(wp) ::  particle_radius !<
+    REAL(wp) ::  pp              !<
+    REAL(wp) ::  qq              !<
+    REAL(wp) ::  rq              !<
+    REAL(wp) ::  y1              !<
+    REAL(wp) ::  y2              !<
+    REAL(wp) ::  y3              !<
+
+    REAL(wp), DIMENSION(1:11), SAVE ::  rat           !<
+    REAL(wp), DIMENSION(1:7), SAVE  ::  r0            !<
+    
+    REAL(wp), DIMENSION(1:7,1:11), SAVE ::  ecoll_100 !<
+    REAL(wp), DIMENSION(1:7,1:11), SAVE ::  ecoll_400 !<
+
+!
+!-- Initial assignment of constants
+    IF ( first )  THEN
+
+       first = .FALSE.
+
+       r0  = (/  10.0_wp, 20.0_wp, 30.0_wp, 40.0_wp, 50.0_wp, 60.0_wp,  &
+                100.0_wp /)
+
+       rat = (/ 0.0_wp, 0.1_wp, 0.2_wp, 0.3_wp, 0.4_wp, 0.5_wp, 0.6_wp, &
+                0.7_wp, 0.8_wp, 0.9_wp, 1.0_wp /)
+!
+!--    Tabulated turbulent enhancement factor at 100 cm**2/s**3
+       ecoll_100(:,1)  = (/  1.74_wp,   1.74_wp,   1.773_wp, 1.49_wp,  &
+                             1.207_wp,  1.207_wp,  1.0_wp /)
+       ecoll_100(:,2)  = (/  1.46_wp,   1.46_wp,   1.421_wp, 1.245_wp, &
+                             1.069_wp,  1.069_wp,  1.0_wp /)
+       ecoll_100(:,3)  = (/  1.32_wp,   1.32_wp,   1.245_wp, 1.123_wp, &
+                             1.000_wp,  1.000_wp,  1.0_wp /)
+       ecoll_100(:,4)  = (/  1.250_wp,  1.250_wp,  1.148_wp, 1.087_wp, &
+                             1.025_wp,  1.025_wp,  1.0_wp /)
+       ecoll_100(:,5)  = (/  1.186_wp,  1.186_wp,  1.066_wp, 1.060_wp, &
+                             1.056_wp,  1.056_wp,  1.0_wp /)
+       ecoll_100(:,6)  = (/  1.045_wp,  1.045_wp,  1.000_wp, 1.014_wp, &
+                             1.028_wp,  1.028_wp,  1.0_wp /)
+       ecoll_100(:,7)  = (/  1.070_wp,  1.070_wp,  1.030_wp, 1.038_wp, &
+                             1.046_wp,  1.046_wp,  1.0_wp /)
+       ecoll_100(:,8)  = (/  1.000_wp,  1.000_wp,  1.054_wp, 1.042_wp, &
+                             1.029_wp,  1.029_wp,  1.0_wp /)
+       ecoll_100(:,9)  = (/  1.223_wp,  1.223_wp,  1.117_wp, 1.069_wp, &
+                             1.021_wp,  1.021_wp,  1.0_wp /)
+       ecoll_100(:,10) = (/  1.570_wp,  1.570_wp,  1.244_wp, 1.166_wp, &
+                             1.088_wp,  1.088_wp,  1.0_wp /)
+       ecoll_100(:,11) = (/ 20.3_wp,   20.3_wp,   14.6_wp,   8.61_wp,  &
+                             2.60_wp,   2.60_wp,   1.0_wp /)
+!
+!--    Tabulated turbulent enhancement factor at 400 cm**2/s**3
+       ecoll_400(:,1)  = (/  4.976_wp,  4.976_wp,  3.593_wp,  2.519_wp, &
+                             1.445_wp,  1.445_wp,  1.0_wp /)
+       ecoll_400(:,2)  = (/  2.984_wp,  2.984_wp,  2.181_wp,  1.691_wp, &
+                             1.201_wp,  1.201_wp,  1.0_wp /)
+       ecoll_400(:,3)  = (/  1.988_wp,  1.988_wp,  1.475_wp,  1.313_wp, &
+                             1.150_wp,  1.150_wp,  1.0_wp /)
+       ecoll_400(:,4)  = (/  1.490_wp,  1.490_wp,  1.187_wp,  1.156_wp, &
+                             1.126_wp,  1.126_wp,  1.0_wp /)
+       ecoll_400(:,5)  = (/  1.249_wp,  1.249_wp,  1.088_wp,  1.090_wp, &
+                             1.092_wp,  1.092_wp,  1.0_wp /)
+       ecoll_400(:,6)  = (/  1.139_wp,  1.139_wp,  1.130_wp,  1.091_wp, &
+                             1.051_wp,  1.051_wp,  1.0_wp /)
+       ecoll_400(:,7)  = (/  1.220_wp,  1.220_wp,  1.190_wp,  1.138_wp, &
+                             1.086_wp,  1.086_wp,  1.0_wp /)
+       ecoll_400(:,8)  = (/  1.325_wp,  1.325_wp,  1.267_wp,  1.165_wp, &
+                             1.063_wp,  1.063_wp,  1.0_wp /)
+       ecoll_400(:,9)  = (/  1.716_wp,  1.716_wp,  1.345_wp,  1.223_wp, &
+                             1.100_wp,  1.100_wp,  1.0_wp /)
+       ecoll_400(:,10) = (/  3.788_wp,  3.788_wp,  1.501_wp,  1.311_wp, &
+                             1.120_wp,  1.120_wp,  1.0_wp /)
+       ecoll_400(:,11) = (/ 36.52_wp,  36.52_wp,  19.16_wp,  22.80_wp,  &
+                            26.0_wp,   26.0_wp,    1.0_wp /)
+
+    ENDIF
+
+!
+!-- Calculate the radius class index of particles with respect to array r0
+!-- The droplet radius has to be given in microns.
+    ALLOCATE( ira(1:radius_classes) )
+
+    DO  j = 1, radius_classes
+       particle_radius = radclass(j) * 1.0E6_wp
+       DO  k = 1, 7
+          IF ( particle_radius < r0(k) )  THEN
+             ira(j) = k
+             EXIT
+          ENDIF
+       ENDDO
+       IF ( particle_radius >= r0(7) )  ira(j) = 8
+    ENDDO
+
+!
+!-- Two-dimensional linear interpolation of the turbulent enhancement factor.
+!-- The droplet radius has to be given in microns.
+    DO  j =  1, radius_classes
+       DO  i = 1, j
+
+          ir = MAX( ira(i), ira(j) )
+          rq = MIN( radclass(i) / radclass(j), radclass(j) / radclass(i) )
+
+          DO  kk = 2, 11
+             IF ( rq <= rat(kk) )  THEN
+                iq = kk
+                EXIT
+             ENDIF
+          ENDDO
+
+          y1 = 1.0_wp  ! turbulent enhancement factor at 0 m**2/s**3
+
+          IF ( ir < 8 )  THEN
+             IF ( ir >= 2 )  THEN
+                pp = ( MAX( radclass(j), radclass(i) ) * 1.0E6_wp -  &
+                       r0(ir-1) ) / ( r0(ir) - r0(ir-1) )
+                qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
+                y2 = ( 1.0_wp - pp ) * ( 1.0_wp - qq ) * ecoll_100(ir-1,iq-1) + &
+                             pp * ( 1.0_wp - qq ) * ecoll_100(ir,iq-1)        + &
+                             qq * ( 1.0_wp - pp ) * ecoll_100(ir-1,iq)        + &
+                             pp * qq              * ecoll_100(ir,iq)
+                y3 = ( 1.0-pp ) * ( 1.0_wp - qq ) * ecoll_400(ir-1,iq-1)      + &
+                             pp * ( 1.0_wp - qq ) * ecoll_400(ir,iq-1)        + &
+                             qq * ( 1.0_wp - pp ) * ecoll_400(ir-1,iq)        + &
+                             pp * qq              * ecoll_400(ir,iq)
+             ELSE
+                qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
+                y2 = ( 1.0_wp - qq ) * ecoll_100(1,iq-1) + qq * ecoll_100(1,iq)
+                y3 = ( 1.0_wp - qq ) * ecoll_400(1,iq-1) + qq * ecoll_400(1,iq)
+             ENDIF
+          ELSE
+             qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
+             y2 = ( 1.0_wp - qq ) * ecoll_100(7,iq-1) + qq * ecoll_100(7,iq)
+             y3 = ( 1.0_wp - qq ) * ecoll_400(7,iq-1) + qq * ecoll_400(7,iq)
+          ENDIF
+!
+!--       Linear interpolation of turbulent enhancement factor
+          IF ( epsilon <= 0.01_wp )  THEN
+             ecf(j,i) = ( epsilon - 0.01_wp ) / ( 0.0_wp  - 0.01_wp ) * y1 &
+                      + ( epsilon - 0.0_wp  ) / ( 0.01_wp - 0.0_wp  ) * y2
+          ELSEIF ( epsilon <= 0.06_wp )  THEN
+             ecf(j,i) = ( epsilon - 0.04_wp ) / ( 0.01_wp - 0.04_wp ) * y2 &
+                      + ( epsilon - 0.01_wp ) / ( 0.04_wp - 0.01_wp ) * y3
+          ELSE
+             ecf(j,i) = ( 0.06_wp - 0.04_wp ) / ( 0.01_wp - 0.04_wp ) * y2 &
+                      + ( 0.06_wp - 0.01_wp ) / ( 0.04_wp - 0.01_wp ) * y3
+          ENDIF
+
+          IF ( ecf(j,i) < 1.0_wp )  ecf(j,i) = 1.0_wp
+
+          ecf(i,j) = ecf(j,i)
+
+       ENDDO
+    ENDDO
+
+ END SUBROUTINE turb_enhance_eff
+ 
+ 
+ !------------------------------------------------------------------------------!
+! Description:
+! ------------
+! This routine is a part of the Lagrangian particle model. Super droplets which 
+! fulfill certain criterion's (e.g. a big weighting factor and a large radius) 
+! can be split into several super droplets with a reduced number of 
+! represented particles of every super droplet. This mechanism ensures an
+! improved representation of the right tail of the drop size distribution with 
+! a feasible amount of computational costs. The limits of particle creation 
+! should be chosen carefully! The idea of this algorithm is based on 
+! Unterstrasser and Soelch, 2014. 
+!------------------------------------------------------------------------------!
+ MODULE SUBROUTINE lpm_splitting
+
+    INTEGER(iwp) ::  i                !< 
+    INTEGER(iwp) ::  j                !<
+    INTEGER(iwp) ::  jpp              !<
+    INTEGER(iwp) ::  k                !<
+    INTEGER(iwp) ::  n                !<
+    INTEGER(iwp) ::  new_particles_gb !< counter of created particles within one grid box
+    INTEGER(iwp) ::  new_size         !< new particle array size
+    INTEGER(iwp) ::  np               !< 
+    INTEGER(iwp) ::  old_size         !< old particle array size
+    
+    INTEGER(iwp), PARAMETER ::  n_max = 100 !< number of radii bin for splitting functions    
+    
+    LOGICAL ::  first_loop_stride_sp = .TRUE. !< flag to calculate constants only once
+
+    REAL(wp) ::  diameter                 !< diameter of droplet
+    REAL(wp) ::  dlog                     !< factor for DSD calculation
+    REAL(wp) ::  factor_volume_to_mass    !< pre calculate factor volume to mass
+    REAL(wp) ::  lambda                   !< slope parameter of gamma-distribution
+    REAL(wp) ::  lwc                      !< liquid water content of grid box
+    REAL(wp) ::  lwc_total                !< average liquid water content of cloud
+    REAL(wp) ::  m1                       !< first moment of DSD
+    REAL(wp) ::  m1_total                 !< average over all PEs of first moment of DSD
+    REAL(wp) ::  m2                       !< second moment of DSD
+    REAL(wp) ::  m2_total                 !< average average over all PEs second moment of DSD
+    REAL(wp) ::  m3                       !< third moment of DSD
+    REAL(wp) ::  m3_total                 !< average average over all PEs third moment of DSD
+    REAL(wp) ::  mu                       !< spectral shape parameter of gamma distribution
+    REAL(wp) ::  nrclgb                   !< number of cloudy grid boxes (ql >= 1.0E-5 kg/kg) 
+    REAL(wp) ::  nrclgb_total             !< average over all PEs of number of cloudy grid boxes
+    REAL(wp) ::  nr                       !< number concentration of cloud droplets
+    REAL(wp) ::  nr_total                 !< average over all PEs of number of cloudy grid boxes
+    REAL(wp) ::  nr0                      !< intercept parameter of gamma distribution
+    REAL(wp) ::  pirho_l                  !< pi * rho_l / 6.0
+    REAL(wp) ::  ql_crit = 1.0E-5_wp      !< threshold lwc for cloudy grid cells 
+                                          !< (Siebesma et al 2003, JAS, 60)
+    REAL(wp) ::  rm                       !< volume averaged mean radius
+    REAL(wp) ::  rm_total                 !< average over all PEs of volume averaged mean radius
+    REAL(wp) ::  r_min = 1.0E-6_wp        !< minimum radius of approximated spectra 
+    REAL(wp) ::  r_max = 1.0E-3_wp        !< maximum radius of approximated spectra
+    REAL(wp) ::  sigma_log = 1.5_wp       !< standard deviation of the LOG-distribution
+    REAL(wp) ::  zeta                     !< Parameter for DSD calculation of Seifert
+
+    REAL(wp), DIMENSION(0:n_max-1) ::  an_spl     !< size dependent critical weight factor
+    REAL(wp), DIMENSION(0:n_max-1) ::  r_bin_mid  !< mass weighted mean radius of a bin
+    REAL(wp), DIMENSION(0:n_max)   ::  r_bin      !< boundaries of a radius bin
+    
+    TYPE(particle_type) ::  tmp_particle   !< temporary particle TYPE
+
+    CALL cpu_log( log_point_s(80), 'lpm_splitting', 'start' )
+
+    IF ( first_loop_stride_sp )  THEN
+       IF ( i_splitting_mode == 2  .OR.  i_splitting_mode == 3 )  THEN
+          dlog   = ( LOG10(r_max) - LOG10(r_min) ) / ( n_max - 1 )
+          DO  i = 0, n_max-1
+             r_bin(i) = 10.0_wp**( LOG10(r_min) + i * dlog - 0.5_wp * dlog )
+             r_bin_mid(i) = 10.0_wp**( LOG10(r_min) + i * dlog )
+          ENDDO
+          r_bin(n_max) = 10.0_wp**( LOG10(r_min) + n_max * dlog - 0.5_wp * dlog )
+       ENDIF   
+       factor_volume_to_mass =  4.0_wp / 3.0_wp * pi * rho_l
+       pirho_l  = pi * rho_l / 6.0_wp
+       IF ( weight_factor_split == -1.0_wp )  THEN
+          weight_factor_split = 0.1_wp * initial_weighting_factor 
+       ENDIF
+    ENDIF
+
+
+    IF ( i_splitting_mode == 1 )  THEN
+
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb+1, nzt
+
+                new_particles_gb = 0
+                number_of_particles = prt_count(k,j,i)
+                IF ( number_of_particles <= 0  .OR.                            &  
+                     ql(k,j,i) < ql_crit )  CYCLE
+                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+!                
+!--             Start splitting operations. Each particle is checked if it
+!--             fulfilled the splitting criterion's. In splitting mode 'const'   
+!--             a critical radius  (radius_split) a critical weighting factor
+!--             (weight_factor_split) and a splitting factor (splitting_factor)
+!--             must  be prescribed (see particle_parameters). Super droplets 
+!--             which have a larger radius and larger weighting factor are split 
+!--             into 'splitting_factor' super droplets. Therefore, the weighting 
+!--             factor of  the super droplet and all created clones is reduced 
+!--             by the factor of 'splitting_factor'.
+                DO  n = 1, number_of_particles
+                   IF ( particles(n)%particle_mask  .AND.                      &
+                        particles(n)%radius >= radius_split  .AND.             & 
+                        particles(n)%weight_factor >= weight_factor_split )    &
+                   THEN          
+!
+!--                   Calculate the new number of particles.
+                      new_size = prt_count(k,j,i) + splitting_factor - 1                                    
+!
+!--                   Cycle if maximum number of particles per grid box
+!--                   is greater than the allowed maximum number.
+                      IF ( new_size >= max_number_particles_per_gridbox )  CYCLE                      
+!
+!--                   Reallocate particle array if necessary. 
+                      IF ( new_size > SIZE(particles) )  THEN 
+                         CALL realloc_particles_array(i,j,k,new_size)
+                      ENDIF
+                      old_size = prt_count(k,j,i)
+!
+!--                   Calculate new weighting factor.
+                      particles(n)%weight_factor =  & 
+                         particles(n)%weight_factor / splitting_factor
+                      tmp_particle = particles(n)
+!
+!--                   Create splitting_factor-1 new particles.
+                      DO  jpp = 1, splitting_factor-1
+                         grid_particles(k,j,i)%particles(jpp+old_size) =       & 
+                            tmp_particle         
+                      ENDDO  
+                      new_particles_gb = new_particles_gb + splitting_factor - 1
+!   
+!--                   Save the new number of super droplets for every grid box.
+                      prt_count(k,j,i) = prt_count(k,j,i) +                    &
+                                         splitting_factor - 1         
+                   ENDIF
+                ENDDO
+                
+             ENDDO
+          ENDDO
+       ENDDO
+
+    ELSEIF ( i_splitting_mode == 2 )  THEN 
+!
+!--    Initialize summing variables.
+       lwc          = 0.0_wp
+       lwc_total    = 0.0_wp 
+       m1           = 0.0_wp
+       m1_total     = 0.0_wp
+       m2           = 0.0_wp
+       m2_total     = 0.0_wp
+       m3           = 0.0_wp
+       m3_total     = 0.0_wp
+       nr           = 0.0_wp   
+       nrclgb       = 0.0_wp
+       nrclgb_total = 0.0_wp 
+       nr_total     = 0.0_wp
+       rm           = 0.0_wp
+       rm_total     = 0.0_wp
+       
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb+1, nzt
+                number_of_particles = prt_count(k,j,i)
+                IF ( number_of_particles <= 0  .OR.                            & 
+                     ql(k,j,i) < ql_crit )  CYCLE
+                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+                nrclgb = nrclgb + 1.0_wp
+!                
+!--             Calculate moments of DSD.                
+                DO  n = 1, number_of_particles
+                   IF ( particles(n)%particle_mask  .AND.                      &
+                        particles(n)%radius >= r_min )                         &
+                   THEN
+                      nr  = nr  + particles(n)%weight_factor
+                      rm  = rm  + factor_volume_to_mass  *                     &
+                                 particles(n)%radius**3  *                     &
+                                 particles(n)%weight_factor
+                      IF ( isf == 1 )  THEN           
+                         diameter   = particles(n)%radius * 2.0_wp            
+                         lwc = lwc + factor_volume_to_mass *                   &
+                                     particles(n)%radius**3 *                  & 
+                                     particles(n)%weight_factor 
+                         m1  = m1  + particles(n)%weight_factor * diameter                                                
+                         m2  = m2  + particles(n)%weight_factor * diameter**2           
+                         m3  = m3  + particles(n)%weight_factor * diameter**3
+                      ENDIF   
+                   ENDIF                        
+                ENDDO 
+             ENDDO
+          ENDDO
+       ENDDO
+
+#if defined( __parallel )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( nr, nr_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+       CALL MPI_ALLREDUCE( rm, rm_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( nrclgb, nrclgb_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( lwc, lwc_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( m1, m1_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( m2, m2_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
+       CALL MPI_ALLREDUCE( m3, m3_total, 1 , &
+       MPI_REAL, MPI_SUM, comm2d, ierr )
+#endif 
+
+!
+!--    Calculate number concentration and mean volume averaged radius.
+       nr_total = MERGE( nr_total / nrclgb_total,                              &
+                         0.0_wp, nrclgb_total > 0.0_wp                         &
+                       )
+       rm_total = MERGE( ( rm_total /                                          &
+                            ( nr_total * factor_volume_to_mass )               &
+                          )**0.3333333_wp, 0.0_wp, nrclgb_total > 0.0_wp       &
+                       )                         
+!
+!--    Check which function should be used to approximate the DSD.
+       IF ( isf == 1 )  THEN
+          lwc_total = MERGE( lwc_total / nrclgb_total,                         &
+                             0.0_wp, nrclgb_total > 0.0_wp                     &
+                           )
+          m1_total  = MERGE( m1_total / nrclgb_total,                          &
+                             0.0_wp, nrclgb_total > 0.0_wp                     &
+                           )
+          m2_total  = MERGE( m2_total / nrclgb_total,                          &
+                             0.0_wp, nrclgb_total > 0.0_wp                     &
+                           )
+          m3_total  = MERGE( m3_total / nrclgb_total,                          &
+                             0.0_wp, nrclgb_total > 0.0_wp                     &
+                           )
+          zeta = m1_total * m3_total / m2_total**2                             
+          mu   = MAX( ( ( 1.0_wp - zeta ) * 2.0_wp + 1.0_wp ) /                &
+                        ( zeta - 1.0_wp ), 0.0_wp                              &
+                    )
+
+          lambda = ( pirho_l * nr_total / lwc_total *                          &
+                     ( mu + 3.0_wp ) * ( mu + 2.0_wp ) * ( mu + 1.0_wp )       &                                          
+                   )**0.3333333_wp
+          nr0 = nr_total / gamma( mu + 1.0_wp ) * lambda**( mu + 1.0_wp ) 
+          
+          DO  n = 0, n_max-1
+             diameter  = r_bin_mid(n) * 2.0_wp           
+             an_spl(n) = nr0 * diameter**mu * EXP( -lambda * diameter ) *      & 
+                         ( r_bin(n+1) - r_bin(n) ) * 2.0_wp 
+          ENDDO
+       ELSEIF ( isf == 2 )  THEN
+          DO  n = 0, n_max-1
+             an_spl(n) = nr_total / ( SQRT( 2.0_wp * pi ) *                    &
+                                     LOG(sigma_log) * r_bin_mid(n)             &
+                                     ) *                                       &
+                         EXP( -( LOG( r_bin_mid(n) / rm_total )**2 ) /         &
+                               ( 2.0_wp * LOG(sigma_log)**2 )                  & 
+                             ) *                                               & 
+                         ( r_bin(n+1) - r_bin(n) )
+          ENDDO
+       ELSEIF( isf == 3 )  THEN
+          DO  n = 0, n_max-1 
+             an_spl(n) = 3.0_wp * nr_total * r_bin_mid(n)**2 / rm_total**3  *  &
+                         EXP( - ( r_bin_mid(n)**3 / rm_total**3 ) )         *  &
+                         ( r_bin(n+1) - r_bin(n) )
+          ENDDO
+       ENDIF
+!
+!--    Criterion to avoid super droplets with a weighting factor < 1.0.
+       an_spl = MAX(an_spl, 1.0_wp)
+  
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb+1, nzt
+                number_of_particles = prt_count(k,j,i)
+                IF ( number_of_particles <= 0  .OR.                            &  
+                     ql(k,j,i) < ql_crit )  CYCLE
+                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+                new_particles_gb = 0         
+!                
+!--             Start splitting operations. Each particle is checked if it
+!--             fulfilled the splitting criterion's. In splitting mode 'cl_av'   
+!--             a critical radius (radius_split) and a splitting function must 
+!--             be prescribed (see particles_par). The critical weighting factor
+!--             is calculated while approximating a 'gamma', 'log' or 'exp'-
+!--             drop size distribution. In this mode the DSD is calculated as 
+!--             an average over all cloudy grid boxes. Super droplets which 
+!--             have a larger radius and larger weighting factor are split into
+!--             'splitting_factor' super droplets. In this case the splitting  
+!--             factor is calculated of weighting factor of the super droplet  
+!--             and the approximated number concentration for droplet of such 
+!--             a size. Due to the splitting, the weighting factor of the  
+!--             super droplet and all created clones is reduced by the factor  
+!--             of 'splitting_facor'.
+                DO  n = 1, number_of_particles
+                   DO  np = 0, n_max-1
+                      IF ( r_bin(np) >= radius_split  .AND.                    &
+                           particles(n)%particle_mask  .AND.                   &
+                           particles(n)%radius >= r_bin(np)  .AND.             &
+                           particles(n)%radius < r_bin(np+1)  .AND.            &
+                           particles(n)%weight_factor >= an_spl(np)  )         &
+                      THEN
+!
+!--                      Calculate splitting factor
+                         splitting_factor =                                    & 
+                             MIN( INT( particles(n)%weight_factor /            &
+                                        an_spl(np)                             &
+                                     ), splitting_factor_max                   &
+                                )
+                         IF ( splitting_factor < 2 )  CYCLE
+!
+!--                      Calculate the new number of particles.                                                           
+                         new_size = prt_count(k,j,i) + splitting_factor - 1
+!
+!--                      Cycle if maximum number of particles per grid box
+!--                      is greater than the allowed maximum number.                         
+                         IF ( new_size >= max_number_particles_per_gridbox )   & 
+                         CYCLE
+!
+!--                      Reallocate particle array if necessary. 
+                         IF ( new_size > SIZE(particles) )  THEN 
+                            CALL realloc_particles_array(i,j,k,new_size)
+                         ENDIF
+                         old_size  = prt_count(k,j,i)                             
+                         new_particles_gb = new_particles_gb +                 &
+                                            splitting_factor - 1
+!
+!--                      Calculate new weighting factor.
+                         particles(n)%weight_factor =                          & 
+                            particles(n)%weight_factor / splitting_factor
+                         tmp_particle = particles(n)
+!
+!--                      Create splitting_factor-1 new particles.                                                  
+                         DO  jpp = 1, splitting_factor-1
+                            grid_particles(k,j,i)%particles(jpp+old_size) =    &
+                                                                    tmp_particle         
+                         ENDDO
+!   
+!--                      Save the new number of super droplets. 
+                         prt_count(k,j,i) = prt_count(k,j,i) +                 &
+                                            splitting_factor - 1
+                      ENDIF
+                   ENDDO
+                ENDDO 
+                
+             ENDDO
+          ENDDO
+       ENDDO
+
+    ELSEIF ( i_splitting_mode == 3 )  THEN 
+
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb+1, nzt
+             
+!
+!--             Initialize summing variables.             
+                lwc = 0.0_wp
+                m1  = 0.0_wp
+                m2  = 0.0_wp
+                m3  = 0.0_wp
+                nr  = 0.0_wp
+                rm  = 0.0_wp  
+                
+                new_particles_gb = 0
+                number_of_particles = prt_count(k,j,i)
+                IF ( number_of_particles <= 0  .OR.                            & 
+                     ql(k,j,i) < ql_crit )  CYCLE
+                particles => grid_particles(k,j,i)%particles
+!                
+!--             Calculate moments of DSD.                
+                DO  n = 1, number_of_particles
+                   IF ( particles(n)%particle_mask  .AND.                      &
+                        particles(n)%radius >= r_min )                         &
+                   THEN
+                      nr  = nr + particles(n)%weight_factor
+                      rm  = rm + factor_volume_to_mass  *                      &
+                                 particles(n)%radius**3  *                     &
+                                 particles(n)%weight_factor
+                      IF ( isf == 1 )  THEN            
+                         diameter   = particles(n)%radius * 2.0_wp           
+                         lwc = lwc + factor_volume_to_mass *                   &
+                                     particles(n)%radius**3 *                  &  
+                                     particles(n)%weight_factor 
+                         m1  = m1 + particles(n)%weight_factor * diameter
+                         m2  = m2 + particles(n)%weight_factor * diameter**2
+                         m3  = m3 + particles(n)%weight_factor * diameter**3
+                      ENDIF      
+                   ENDIF                                            
+                ENDDO  
+
+                IF ( nr <= 0.0  .OR.  rm <= 0.0_wp )  CYCLE 
+!
+!--             Calculate mean volume averaged radius.                
+                rm = ( rm / ( nr * factor_volume_to_mass ) )**0.3333333_wp
+!
+!--             Check which function should be used to approximate the DSD.              
+                IF ( isf == 1 )  THEN
+!
+!--                Gamma size distribution to calculate  
+!--                critical weight_factor (e.g. Marshall + Palmer, 1948).
+                   zeta = m1 * m3 / m2**2
+                   mu   = MAX( ( ( 1.0_wp - zeta ) * 2.0_wp + 1.0_wp ) /       &
+                                ( zeta - 1.0_wp ), 0.0_wp                      &
+                             )   
+                   lambda = ( pirho_l * nr / lwc *                             &
+                              ( mu + 3.0_wp ) * ( mu + 2.0_wp ) *              &
+                              ( mu + 1.0_wp )                                  &                                  
+                            )**0.3333333_wp
+                   nr0 =  ( nr / (gamma( mu + 1.0_wp ) ) ) *                   &
+                          lambda**( mu + 1.0_wp ) 
+
+                   DO  n = 0, n_max-1
+                      diameter         = r_bin_mid(n) * 2.0_wp           
+                      an_spl(n) = nr0 * diameter**mu *                         &
+                                  EXP( -lambda * diameter ) *                  & 
+                                  ( r_bin(n+1) - r_bin(n) ) * 2.0_wp 
+                   ENDDO
+                ELSEIF ( isf == 2 )  THEN
+!
+!--                Lognormal size distribution to calculate critical 
+!--                weight_factor (e.g. Levin, 1971, Bradley + Stow, 1974).   
+                   DO  n = 0, n_max-1
+                      an_spl(n) = nr / ( SQRT( 2.0_wp * pi ) *                 &
+                                              LOG(sigma_log) * r_bin_mid(n)    &
+                                        ) *                                    &
+                                  EXP( -( LOG( r_bin_mid(n) / rm )**2 ) /      &
+                                        ( 2.0_wp * LOG(sigma_log)**2 )         & 
+                                      ) *                                      & 
+                                  ( r_bin(n+1) - r_bin(n) )
+                   ENDDO
+                ELSEIF ( isf == 3 )  THEN
+!
+!--                Exponential size distribution to calculate critical 
+!--                weight_factor (e.g. Berry + Reinhardt, 1974).  
+                   DO  n = 0, n_max-1
+                      an_spl(n) = 3.0_wp * nr * r_bin_mid(n)**2 / rm**3 *     &
+                                  EXP( - ( r_bin_mid(n)**3 / rm**3 ) ) *      &
+                                  ( r_bin(n+1) - r_bin(n) )
+                   ENDDO
+                ENDIF
+                
+!
+!--             Criterion to avoid super droplets with a weighting factor < 1.0.                                   
+                an_spl = MAX(an_spl, 1.0_wp)
+!                
+!--             Start splitting operations. Each particle is checked if it
+!--             fulfilled the splitting criterion's. In splitting mode 'gb_av'   
+!--             a critical radius (radius_split) and a splitting function must 
+!--             be prescribed (see particles_par). The critical weighting factor
+!--             is calculated while appoximating a 'gamma', 'log' or 'exp'-
+!--             drop size distribution. In this mode a DSD is calculated for 
+!--             every cloudy grid box. Super droplets which have a larger 
+!--             radius and larger weighting factor are split into
+!--             'splitting_factor' super droplets. In this case the splitting  
+!--             factor is calculated of weighting factor of the super droplet  
+!--             and theapproximated number concentration for droplet of such 
+!--             a size. Due to the splitting, the weighting factor of the  
+!--             super droplet and all created clones is reduced by the factor  
+!--             of 'splitting_facor'.
+                DO  n = 1, number_of_particles
+                   DO  np = 0, n_max-1
+                      IF ( r_bin(np) >= radius_split  .AND.                    &
+                           particles(n)%particle_mask  .AND.                   &
+                           particles(n)%radius >= r_bin(np)    .AND.           &
+                           particles(n)%radius < r_bin(np+1)   .AND.           &
+                           particles(n)%weight_factor >= an_spl(np) )          &
+                      THEN
+!
+!--                      Calculate splitting factor.
+                         splitting_factor =                                    & 
+                             MIN( INT( particles(n)%weight_factor /            &
+                                        an_spl(np)                             &
+                                     ), splitting_factor_max                   &
+                                )
+                         IF ( splitting_factor < 2 )  CYCLE
+
+!
+!--                      Calculate the new number of particles.                                                                                         
+                         new_size = prt_count(k,j,i) + splitting_factor - 1
+!
+!--                      Cycle if maximum number of particles per grid box
+!--                      is greater than the allowed maximum number.                                                  
+                         IF ( new_size >= max_number_particles_per_gridbox )   &
+                         CYCLE
+!
+!--                      Reallocate particle array if necessary.                          
+                         IF ( new_size > SIZE(particles) )  THEN 
+                            CALL realloc_particles_array(i,j,k,new_size)
+                         ENDIF
+!
+!--                      Calculate new weighting factor.
+                         particles(n)%weight_factor = & 
+                            particles(n)%weight_factor / splitting_factor
+                         tmp_particle               = particles(n)
+                         old_size                   = prt_count(k,j,i)
+!
+!--                      Create splitting_factor-1 new particles.
+                         DO jpp = 1, splitting_factor-1
+                            grid_particles(k,j,i)%particles(jpp+old_size) =    &
+                               tmp_particle                  
+                         ENDDO
+!
+!--                      Save the new number of droplets for every grid box.
+                         prt_count(k,j,i)    = prt_count(k,j,i) +              &
+                                               splitting_factor - 1
+                         new_particles_gb    = new_particles_gb +              &
+                                               splitting_factor - 1                                       
+                      ENDIF
+                   ENDDO  
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDIF
+        
+    CALL cpu_log( log_point_s(80), 'lpm_splitting', 'stop' )
+
+ END SUBROUTINE lpm_splitting
+ 
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+! This routine is a part of the Lagrangian particle model. Two Super droplets 
+! which fulfill certain criterion's (e.g. a big weighting factor and a small
+! radius) can be merged into one super droplet with a increased number of 
+! represented particles of the super droplet. This mechanism ensures an
+! improved a feasible amount of computational costs. The limits of particle 
+! creation should be chosen carefully! The idea of this algorithm is based on 
+! Unterstrasser and Soelch, 2014. 
+!------------------------------------------------------------------------------!
+ MODULE SUBROUTINE lpm_merging
+
+    INTEGER(iwp) ::  i         !<      
+    INTEGER(iwp) ::  j         !<       
+    INTEGER(iwp) ::  k         !<       
+    INTEGER(iwp) ::  n         !<
+    INTEGER(iwp) ::  merge_drp = 0     !< number of merged droplets
+    
+    
+    REAL(wp) ::  ql_crit = 1.0E-5_wp  !< threshold lwc for cloudy grid cells 
+                                      !< (e.g. Siebesma et al 2003, JAS, 60)
+    
+    CALL cpu_log( log_point_s(81), 'lpm_merging', 'start' )
+
+    merge_drp  = 0
+    
+    IF ( weight_factor_merge == -1.0_wp )  THEN
+       weight_factor_merge = 0.5_wp * initial_weighting_factor 
+    ENDIF
+
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+   
+             number_of_particles = prt_count(k,j,i)
+             IF ( number_of_particles <= 0  .OR.                               &
+                   ql(k,j,i) >= ql_crit )  CYCLE
+             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+!
+!--          Start merging operations: This routine delete super droplets with
+!--          a small radius (radius <= radius_merge) and a low weighting 
+!--          factor (weight_factor  <= weight_factor_merge). The number of 
+!--          represented particles will be added to the next particle of the 
+!--          particle array. Tests showed that this simplified method can be 
+!--          used because it will only take place outside of cloudy grid 
+!--          boxes where ql <= 1.0E-5 kg/kg. Therefore, especially former cloned   
+!--          and subsequent evaporated super droplets will be merged.
+             DO  n = 1, number_of_particles-1
+                IF ( particles(n)%particle_mask                    .AND.       &
+                     particles(n+1)%particle_mask                  .AND.       &
+                     particles(n)%radius        <= radius_merge    .AND.       &
+                     particles(n)%weight_factor <= weight_factor_merge )       &   
+                THEN
+                   particles(n+1)%weight_factor  =                             &
+                                       particles(n+1)%weight_factor +          &
+                                       ( particles(n)%radius**3     /          &
+                                         particles(n+1)%radius**3   *          &
+                                         particles(n)%weight_factor            &
+                                       )
+                   particles(n)%particle_mask = .FALSE.
+                   deleted_particles          = deleted_particles + 1 
+                   merge_drp                  = merge_drp + 1
+                
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO
+       
+       
+    CALL cpu_log( log_point_s(81), 'lpm_merging', 'stop' )
+
+ END SUBROUTINE lpm_merging
+
+ 
+END SUBMODULE
+
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Exchange of particles between the subdomains.
+!------------------------------------------------------------------------------!
+SUBMODULE(lagrangian_particle_model_mod) lpm_utilities
+
+    INTEGER(iwp), PARAMETER ::  NR_2_direction_move = 10000 !<
+    INTEGER(iwp)            ::  nr_move_north               !<
+    INTEGER(iwp)            ::  nr_move_south               !<
+
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  move_also_north
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  move_also_south
+
+ CONTAINS
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Exchange between subdomains.
+!> As soon as one particle has moved beyond the boundary of the domain, it
+!> is included in the relevant transfer arrays and marked for subsequent
+!> deletion on this PE.
+!> First sweep for crossings in x direction. Find out first the number of
+!> particles to be transferred and allocate temporary arrays needed to store
+!> them.
+!> For a one-dimensional decomposition along y, no transfer is necessary,
+!> because the particle remains on the PE, but the particle coordinate has to
+!> be adjusted.
+!------------------------------------------------------------------------------!
+ MODULE SUBROUTINE lpm_exchange_horiz
+
+    INTEGER(iwp) ::  i                 !< grid index (x) of particle positition
+    INTEGER(iwp) ::  ip                !< index variable along x
+    INTEGER(iwp) ::  j                 !< grid index (y) of particle positition
+    INTEGER(iwp) ::  jp                !< index variable along y
+    INTEGER(iwp) ::  kp                !< index variable along z
+    INTEGER(iwp) ::  n                 !< particle index variable 
+    INTEGER(iwp) ::  par_size          !< Particle size in bytes
+    INTEGER(iwp) ::  trlp_count        !< number of particles send to left PE
+    INTEGER(iwp) ::  trlp_count_recv   !< number of particles receive from right PE
+    INTEGER(iwp) ::  trnp_count        !< number of particles send to north PE
+    INTEGER(iwp) ::  trnp_count_recv   !< number of particles receive from south PE
+    INTEGER(iwp) ::  trrp_count        !< number of particles send to right PE
+    INTEGER(iwp) ::  trrp_count_recv   !< number of particles receive from left PE
+    INTEGER(iwp) ::  trsp_count        !< number of particles send to south PE
+    INTEGER(iwp) ::  trsp_count_recv   !< number of particles receive from north PE
+
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvlp  !< particles received from right PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvnp  !< particles received from south PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvrp  !< particles received from left PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvsp  !< particles received from north PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trlp  !< particles send to left PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trnp  !< particles send to north PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trrp  !< particles send to right PE
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trsp  !< particles send to south PE
+
+    CALL cpu_log( log_point_s(23), 'lpm_exchange_horiz', 'start' )
+
+#if defined( __parallel )
+
+!
+!-- Exchange between subdomains.
+!-- As soon as one particle has moved beyond the boundary of the domain, it
+!-- is included in the relevant transfer arrays and marked for subsequent
+!-- deletion on this PE.
+!-- First sweep for crossings in x direction. Find out first the number of
+!-- particles to be transferred and allocate temporary arrays needed to store
+!-- them.
+!-- For a one-dimensional decomposition along y, no transfer is necessary,
+!-- because the particle remains on the PE, but the particle coordinate has to
+!-- be adjusted.
+    trlp_count  = 0
+    trrp_count  = 0
+
+    trlp_count_recv   = 0
+    trrp_count_recv   = 0
+
+    IF ( pdims(1) /= 1 )  THEN
+!
+!--    First calculate the storage necessary for sending and receiving the data.
+!--    Compute only first (nxl) and last (nxr) loop iterration.
+       DO  ip = nxl, nxr, nxr - nxl
+          DO  jp = nys, nyn
+             DO  kp = nzb+1, nzt
+
+                number_of_particles = prt_count(kp,jp,ip)
+                IF ( number_of_particles <= 0 )  CYCLE
+                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+                DO  n = 1, number_of_particles
+                   IF ( particles(n)%particle_mask )  THEN
+                      i = particles(n)%x * ddx
+!
+!--                   Above calculation does not work for indices less than zero
+                      IF ( particles(n)%x < 0.0_wp)  i = -1
+
+                      IF ( i < nxl )  THEN
+                         trlp_count = trlp_count + 1
+                      ELSEIF ( i > nxr )  THEN
+                         trrp_count = trrp_count + 1
+                      ENDIF
+                   ENDIF
+                ENDDO
+
+             ENDDO
+          ENDDO
+       ENDDO
+
+       IF ( trlp_count  == 0 )  trlp_count  = 1
+       IF ( trrp_count  == 0 )  trrp_count  = 1
+
+       ALLOCATE( trlp(trlp_count), trrp(trrp_count) )
+
+       trlp = zero_particle
+       trrp = zero_particle
+
+       trlp_count  = 0
+       trrp_count  = 0
+
+    ENDIF
+!
+!-- Compute only first (nxl) and last (nxr) loop iterration
+    DO  ip = nxl, nxr, nxr-nxl
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles <= 0 ) CYCLE
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+             DO  n = 1, number_of_particles
+!
+!--             Only those particles that have not been marked as 'deleted' may
+!--             be moved.
+                IF ( particles(n)%particle_mask )  THEN
+
+                   i = particles(n)%x * ddx
+!
+!--                Above calculation does not work for indices less than zero
+                   IF ( particles(n)%x < 0.0_wp )  i = -1
+
+                   IF ( i <  nxl )  THEN
+                      IF ( i < 0 )  THEN
+!
+!--                   Apply boundary condition along x
+                         IF ( ibc_par_lr == 0 )  THEN
+!
+!--                         Cyclic condition
+                            IF ( pdims(1) == 1 )  THEN
+                               particles(n)%x        = ( nx + 1 ) * dx + particles(n)%x
+                               particles(n)%origin_x = ( nx + 1 ) * dx + &
+                               particles(n)%origin_x
+                            ELSE
+                               trlp_count = trlp_count + 1
+                               trlp(trlp_count)   = particles(n)
+                               trlp(trlp_count)%x = ( nx + 1 ) * dx + trlp(trlp_count)%x
+                               trlp(trlp_count)%origin_x = trlp(trlp_count)%origin_x + &
+                               ( nx + 1 ) * dx
+                               particles(n)%particle_mask  = .FALSE.
+                               deleted_particles = deleted_particles + 1
+
+                               IF ( trlp(trlp_count)%x >= (nx + 1)* dx - 1.0E-12_wp )  THEN
+                                  trlp(trlp_count)%x = trlp(trlp_count)%x - 1.0E-10_wp
+                                  !++ why is 1 subtracted in next statement???
+                                  trlp(trlp_count)%origin_x = trlp(trlp_count)%origin_x - 1
+                               ENDIF
+
+                            ENDIF
+
+                         ELSEIF ( ibc_par_lr == 1 )  THEN
+!
+!--                         Particle absorption
+                            particles(n)%particle_mask = .FALSE.
+                            deleted_particles = deleted_particles + 1
+
+                         ELSEIF ( ibc_par_lr == 2 )  THEN
+!
+!--                         Particle reflection
+                            particles(n)%x       = -particles(n)%x
+                            particles(n)%speed_x = -particles(n)%speed_x
+
+                         ENDIF
+                      ELSE
+!
+!--                      Store particle data in the transfer array, which will be 
+!--                      send to the neighbouring PE
+                         trlp_count = trlp_count + 1
+                         trlp(trlp_count) = particles(n)
+                         particles(n)%particle_mask = .FALSE.
+                         deleted_particles = deleted_particles + 1
+
+                      ENDIF
+
+                   ELSEIF ( i > nxr )  THEN
+                      IF ( i > nx )  THEN
+!
+!--                      Apply boundary condition along x
+                         IF ( ibc_par_lr == 0 )  THEN
+!
+!--                         Cyclic condition
+                            IF ( pdims(1) == 1 )  THEN
+                               particles(n)%x = particles(n)%x - ( nx + 1 ) * dx
+                               particles(n)%origin_x = particles(n)%origin_x - &
+                               ( nx + 1 ) * dx
+                            ELSE
+                               trrp_count = trrp_count + 1
+                               trrp(trrp_count) = particles(n)
+                               trrp(trrp_count)%x = trrp(trrp_count)%x - ( nx + 1 ) * dx
+                               trrp(trrp_count)%origin_x = trrp(trrp_count)%origin_x - &
+                               ( nx + 1 ) * dx
+                               particles(n)%particle_mask = .FALSE.
+                               deleted_particles = deleted_particles + 1
+
+                            ENDIF
+
+                         ELSEIF ( ibc_par_lr == 1 )  THEN
+!
+!--                         Particle absorption
+                            particles(n)%particle_mask = .FALSE.
+                            deleted_particles = deleted_particles + 1
+
+                         ELSEIF ( ibc_par_lr == 2 )  THEN
+!
+!--                         Particle reflection
+                            particles(n)%x       = 2 * ( nx * dx ) - particles(n)%x
+                            particles(n)%speed_x = -particles(n)%speed_x
+
+                         ENDIF
+                      ELSE
+!
+!--                      Store particle data in the transfer array, which will be send
+!--                      to the neighbouring PE
+                         trrp_count = trrp_count + 1
+                         trrp(trrp_count) = particles(n)
+                         particles(n)%particle_mask = .FALSE.
+                         deleted_particles = deleted_particles + 1
+
+                      ENDIF
+
+                   ENDIF
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO
+
+!
+!-- STORAGE_SIZE returns the storage size of argument A in bits. However , it 
+!-- is needed in bytes. The function C_SIZEOF which produces this value directly
+!-- causes problems with gfortran. For this reason the use of C_SIZEOF is avoided
+    par_size = STORAGE_SIZE(trlp(1))/8
+
+
+!
+!-- Allocate arrays required for north-south exchange, as these
+!-- are used directly after particles are exchange along x-direction.
+    ALLOCATE( move_also_north(1:NR_2_direction_move) )
+    ALLOCATE( move_also_south(1:NR_2_direction_move) )
+
+    nr_move_north = 0
+    nr_move_south = 0
+!
+!-- Send left boundary, receive right boundary (but first exchange how many
+!-- and check, if particle storage must be extended)
+    IF ( pdims(1) /= 1 )  THEN
+
+       CALL MPI_SENDRECV( trlp_count,      1, MPI_INTEGER, pleft,  0, &
+                          trrp_count_recv, 1, MPI_INTEGER, pright, 0, &
+                          comm2d, status, ierr )
+
+       ALLOCATE(rvrp(MAX(1,trrp_count_recv)))
+
+       CALL MPI_SENDRECV( trlp, max(1,trlp_count)*par_size, MPI_BYTE,&
+                          pleft, 1, rvrp,                            &
+                          max(1,trrp_count_recv)*par_size, MPI_BYTE, pright, 1,&
+                          comm2d, status, ierr )
+
+       IF ( trrp_count_recv > 0 )  CALL lpm_add_particles_to_gridcell(rvrp(1:trrp_count_recv))
+
+       DEALLOCATE(rvrp)
+
+!
+!--    Send right boundary, receive left boundary
+       CALL MPI_SENDRECV( trrp_count,      1, MPI_INTEGER, pright, 0, &
+                          trlp_count_recv, 1, MPI_INTEGER, pleft,  0, &
+                          comm2d, status, ierr )
+
+       ALLOCATE(rvlp(MAX(1,trlp_count_recv)))
+!      
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
+!--    variables in structure particle_type (due to the calculation of par_size)
+       CALL MPI_SENDRECV( trrp, max(1,trrp_count)*par_size, MPI_BYTE,&
+                          pright, 1, rvlp,                           &
+                          max(1,trlp_count_recv)*par_size, MPI_BYTE, pleft, 1, &
+                          comm2d, status, ierr )
+
+       IF ( trlp_count_recv > 0 )  CALL lpm_add_particles_to_gridcell(rvlp(1:trlp_count_recv))
+
+       DEALLOCATE( rvlp )
+       DEALLOCATE( trlp, trrp )
+
+    ENDIF
+
+!
+!-- Check whether particles have crossed the boundaries in y direction. Note 
+!-- that this case can also apply to particles that have just been received
+!-- from the adjacent right or left PE.
+!-- Find out first the number of particles to be transferred and allocate
+!-- temporary arrays needed to store them.
+!-- For a one-dimensional decomposition along y, no transfer is necessary,
+!-- because the particle remains on the PE.
+    trsp_count  = nr_move_south
+    trnp_count  = nr_move_north
+
+    trsp_count_recv   = 0
+    trnp_count_recv   = 0
+
+    IF ( pdims(2) /= 1 )  THEN
+!
+!--    First calculate the storage necessary for sending and receiving the
+!--    data
+       DO  ip = nxl, nxr
+          DO  jp = nys, nyn, nyn-nys    !compute only first (nys) and last (nyn) loop iterration
+             DO  kp = nzb+1, nzt
+                number_of_particles = prt_count(kp,jp,ip)
+                IF ( number_of_particles <= 0 )  CYCLE
+                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+                DO  n = 1, number_of_particles
+                   IF ( particles(n)%particle_mask )  THEN
+                      j = particles(n)%y * ddy
+!
+!--                   Above calculation does not work for indices less than zero
+                      IF ( particles(n)%y < 0.0_wp)  j = -1
+
+                      IF ( j < nys )  THEN
+                         trsp_count = trsp_count + 1
+                      ELSEIF ( j > nyn )  THEN
+                         trnp_count = trnp_count + 1
+                      ENDIF
+                   ENDIF
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDDO
+
+       IF ( trsp_count  == 0 )  trsp_count  = 1
+       IF ( trnp_count  == 0 )  trnp_count  = 1
+
+       ALLOCATE( trsp(trsp_count), trnp(trnp_count) )
+
+       trsp = zero_particle
+       trnp = zero_particle
+
+       trsp_count  = nr_move_south
+       trnp_count  = nr_move_north
+       
+       trsp(1:nr_move_south) = move_also_south(1:nr_move_south)
+       trnp(1:nr_move_north) = move_also_north(1:nr_move_north)
+
+    ENDIF
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn, nyn-nys ! compute only first (nys) and last (nyn) loop iterration
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+             DO  n = 1, number_of_particles
+!
+!--             Only those particles that have not been marked as 'deleted' may
+!--             be moved.
+                IF ( particles(n)%particle_mask )  THEN
+
+                   j = particles(n)%y * ddy
+!
+!--                Above calculation does not work for indices less than zero
+                   IF ( particles(n)%y < 0.0_wp )  j = -1
+
+                   IF ( j < nys )  THEN
+                      IF ( j < 0 )  THEN
+!
+!--                      Apply boundary condition along y
+                         IF ( ibc_par_ns == 0 )  THEN
+!
+!--                         Cyclic condition
+                            IF ( pdims(2) == 1 )  THEN
+                               particles(n)%y = ( ny + 1 ) * dy + particles(n)%y
+                               particles(n)%origin_y = ( ny + 1 ) * dy + &
+                                                     particles(n)%origin_y
+                            ELSE
+                               trsp_count         = trsp_count + 1
+                               trsp(trsp_count)   = particles(n)
+                               trsp(trsp_count)%y = ( ny + 1 ) * dy + &
+                                                 trsp(trsp_count)%y
+                               trsp(trsp_count)%origin_y = trsp(trsp_count)%origin_y &
+                                                + ( ny + 1 ) * dy
+                               particles(n)%particle_mask = .FALSE.
+                               deleted_particles = deleted_particles + 1
+
+                               IF ( trsp(trsp_count)%y >= (ny+1)* dy - 1.0E-12_wp )  THEN
+                                  trsp(trsp_count)%y = trsp(trsp_count)%y - 1.0E-10_wp
+                                  !++ why is 1 subtracted in next statement???
+                                  trsp(trsp_count)%origin_y =                        &
+                                                  trsp(trsp_count)%origin_y - 1
+                               ENDIF
+
+                            ENDIF
+
+                         ELSEIF ( ibc_par_ns == 1 )  THEN
+!
+!--                         Particle absorption
+                            particles(n)%particle_mask = .FALSE.
+                            deleted_particles          = deleted_particles + 1
+
+                         ELSEIF ( ibc_par_ns == 2 )  THEN
+!
+!--                         Particle reflection
+                            particles(n)%y       = -particles(n)%y
+                            particles(n)%speed_y = -particles(n)%speed_y
+
+                         ENDIF
+                      ELSE
+!
+!--                      Store particle data in the transfer array, which will 
+!--                      be send to the neighbouring PE
+                         trsp_count = trsp_count + 1
+                         trsp(trsp_count) = particles(n)
+                         particles(n)%particle_mask = .FALSE.
+                         deleted_particles = deleted_particles + 1
+
+                      ENDIF
+
+                   ELSEIF ( j > nyn )  THEN
+                      IF ( j > ny )  THEN
+!
+!--                       Apply boundary condition along y
+                         IF ( ibc_par_ns == 0 )  THEN
+!
+!--                         Cyclic condition
+                            IF ( pdims(2) == 1 )  THEN
+                               particles(n)%y        = particles(n)%y - ( ny + 1 ) * dy
+                               particles(n)%origin_y =                         &
+                                          particles(n)%origin_y - ( ny + 1 ) * dy
+                            ELSE
+                               trnp_count         = trnp_count + 1
+                               trnp(trnp_count)   = particles(n)
+                               trnp(trnp_count)%y =                            &
+                                          trnp(trnp_count)%y - ( ny + 1 ) * dy
+                               trnp(trnp_count)%origin_y =                     &
+                                         trnp(trnp_count)%origin_y - ( ny + 1 ) * dy
+                               particles(n)%particle_mask = .FALSE.
+                               deleted_particles          = deleted_particles + 1
+                            ENDIF
+
+                         ELSEIF ( ibc_par_ns == 1 )  THEN
+!
+!--                         Particle absorption
+                            particles(n)%particle_mask = .FALSE.
+                            deleted_particles = deleted_particles + 1
+
+                         ELSEIF ( ibc_par_ns == 2 )  THEN
+!
+!--                         Particle reflection
+                            particles(n)%y       = 2 * ( ny * dy ) - particles(n)%y
+                            particles(n)%speed_y = -particles(n)%speed_y
+
+                         ENDIF
+                      ELSE
+!
+!--                      Store particle data in the transfer array, which will 
+!--                      be send to the neighbouring PE
+                         trnp_count = trnp_count + 1
+                         trnp(trnp_count) = particles(n)
+                         particles(n)%particle_mask = .FALSE.
+                         deleted_particles = deleted_particles + 1
+
+                      ENDIF
+
+                   ENDIF
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO
+
+!
+!-- Send front boundary, receive back boundary (but first exchange how many
+!-- and check, if particle storage must be extended)
+    IF ( pdims(2) /= 1 )  THEN
+
+       CALL MPI_SENDRECV( trsp_count,      1, MPI_INTEGER, psouth, 0, &
+                          trnp_count_recv, 1, MPI_INTEGER, pnorth, 0, &
+                          comm2d, status, ierr )
+
+       ALLOCATE(rvnp(MAX(1,trnp_count_recv)))
+!      
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
+!--    variables in structure particle_type (due to the calculation of par_size)
+       CALL MPI_SENDRECV( trsp, trsp_count*par_size, MPI_BYTE,      &
+                          psouth, 1, rvnp,                             &
+                          trnp_count_recv*par_size, MPI_BYTE, pnorth, 1,   &
+                          comm2d, status, ierr )
+
+       IF ( trnp_count_recv  > 0 )  CALL lpm_add_particles_to_gridcell(rvnp(1:trnp_count_recv))
+
+       DEALLOCATE(rvnp)
+
+!
+!--    Send back boundary, receive front boundary
+       CALL MPI_SENDRECV( trnp_count,      1, MPI_INTEGER, pnorth, 0, &
+                          trsp_count_recv, 1, MPI_INTEGER, psouth, 0, &
+                          comm2d, status, ierr )
+
+       ALLOCATE(rvsp(MAX(1,trsp_count_recv)))
+!      
+!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
+!--    variables in structure particle_type (due to the calculation of par_size)
+       CALL MPI_SENDRECV( trnp, trnp_count*par_size, MPI_BYTE,      &
+                          pnorth, 1, rvsp,                          &
+                          trsp_count_recv*par_size, MPI_BYTE, psouth, 1,   &
+                          comm2d, status, ierr )
+
+       IF ( trsp_count_recv > 0 )  CALL lpm_add_particles_to_gridcell(rvsp(1:trsp_count_recv))
+
+       DEALLOCATE(rvsp)
+
+       number_of_particles = number_of_particles + trsp_count_recv
+
+       DEALLOCATE( trsp, trnp )
+
+    ENDIF
+
+    DEALLOCATE( move_also_north )
+    DEALLOCATE( move_also_south )
+
+#else
+
+    DO  ip = nxl, nxr, nxr-nxl
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+             DO  n = 1, number_of_particles
+!
+!--             Apply boundary conditions
+
+                IF ( particles(n)%x < 0.0_wp )  THEN
+
+                   IF ( ibc_par_lr == 0 )  THEN
+!
+!--                   Cyclic boundary. Relevant coordinate has to be changed.
+                      particles(n)%x = ( nx + 1 ) * dx + particles(n)%x
+                      particles(n)%origin_x = ( nx + 1 ) * dx + &
+                               particles(n)%origin_x
+                   ELSEIF ( ibc_par_lr == 1 )  THEN
+!
+!--                   Particle absorption
+                      particles(n)%particle_mask = .FALSE.
+                      deleted_particles = deleted_particles + 1
+
+                   ELSEIF ( ibc_par_lr == 2 )  THEN
+!
+!--                   Particle reflection
+                      particles(n)%x       = -dx - particles(n)%x
+                      particles(n)%speed_x = -particles(n)%speed_x
+                   ENDIF
+
+                ELSEIF ( particles(n)%x >= ( nx + 1) * dx )  THEN
+
+                   IF ( ibc_par_lr == 0 )  THEN
+!
+!--                   Cyclic boundary. Relevant coordinate has to be changed.
+                      particles(n)%x = particles(n)%x - ( nx + 1 ) * dx
+                      particles(n)%origin_x = particles(n)%origin_x - &
+                               ( nx + 1 ) * dx
+
+                   ELSEIF ( ibc_par_lr == 1 )  THEN
+!
+!--                   Particle absorption
+                      particles(n)%particle_mask = .FALSE.
+                      deleted_particles = deleted_particles + 1
+
+                   ELSEIF ( ibc_par_lr == 2 )  THEN
+!
+!--                   Particle reflection
+                      particles(n)%x       = ( nx + 1 ) * dx - particles(n)%x
+                      particles(n)%speed_x = -particles(n)%speed_x
+                   ENDIF
+
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO
+
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn, nyn-nys
+          DO  kp = nzb+1, nzt
+             number_of_particles = prt_count(kp,jp,ip)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+             DO  n = 1, number_of_particles
+
+                IF ( particles(n)%y < 0.0_wp)  THEN
+
+                   IF ( ibc_par_ns == 0 )  THEN
+!
+!--                   Cyclic boundary. Relevant coordinate has to be changed.
+                      particles(n)%y = ( ny + 1 ) * dy + particles(n)%y
+                      particles(n)%origin_y = ( ny + 1 ) * dy + &
+                           particles(n)%origin_y
+
+                   ELSEIF ( ibc_par_ns == 1 )  THEN
+!
+!--                   Particle absorption
+                      particles(n)%particle_mask = .FALSE.
+                      deleted_particles = deleted_particles + 1
+
+                   ELSEIF ( ibc_par_ns == 2 )  THEN
+!
+!--                   Particle reflection
+                      particles(n)%y       = -dy - particles(n)%y
+                      particles(n)%speed_y = -particles(n)%speed_y
+                   ENDIF
+
+                ELSEIF ( particles(n)%y >= ( ny + 1) * dy )  THEN
+
+                   IF ( ibc_par_ns == 0 )  THEN
+!
+!--                   Cyclic boundary. Relevant coordinate has to be changed.
+                      particles(n)%y = particles(n)%y - ( ny + 1 ) * dy
+                      particles(n)%origin_y = particles(n)%origin_y - &
+                                ( ny + 1 ) * dy
+
+                   ELSEIF ( ibc_par_ns == 1 )  THEN
+!
+!--                   Particle absorption
+                      particles(n)%particle_mask = .FALSE.
+                      deleted_particles = deleted_particles + 1
+
+                   ELSEIF ( ibc_par_ns == 2 )  THEN
+!
+!--                   Particle reflection
+                      particles(n)%y       = ( ny + 1 ) * dy - particles(n)%y
+                      particles(n)%speed_y = -particles(n)%speed_y
+                   ENDIF
+
+                ENDIF
+
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO
+#endif
+
+!
+!-- Accumulate the number of particles transferred between the subdomains
+#if defined( __parallel )
+    trlp_count_sum      = trlp_count_sum      + trlp_count
+    trlp_count_recv_sum = trlp_count_recv_sum + trlp_count_recv
+    trrp_count_sum      = trrp_count_sum      + trrp_count
+    trrp_count_recv_sum = trrp_count_recv_sum + trrp_count_recv
+    trsp_count_sum      = trsp_count_sum      + trsp_count
+    trsp_count_recv_sum = trsp_count_recv_sum + trsp_count_recv
+    trnp_count_sum      = trnp_count_sum      + trnp_count
+    trnp_count_recv_sum = trnp_count_recv_sum + trnp_count_recv
+#endif
+
+    CALL cpu_log( log_point_s(23), 'lpm_exchange_horiz', 'stop' )
+
+ END SUBROUTINE lpm_exchange_horiz
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> If a particle moves from one processor to another, this subroutine moves 
+!> the corresponding elements from the particle arrays of the old grid cells 
+!> to the particle arrays of the new grid cells.
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_add_particles_to_gridcell (particle_array)
+
+    IMPLICIT NONE
+
+    INTEGER(iwp)        ::  ip        !< grid index (x) of particle
+    INTEGER(iwp)        ::  jp        !< grid index (x) of particle
+    INTEGER(iwp)        ::  kp        !< grid index (x) of particle
+    INTEGER(iwp)        ::  n         !< index variable of particle
+    INTEGER(iwp)        ::  pindex    !< dummy argument for new number of particles per grid box
+
+    LOGICAL             ::  pack_done !<
+
+    TYPE(particle_type), DIMENSION(:), INTENT(IN)  ::  particle_array !< new particles in a grid box
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  temp_ns        !< temporary particle array for reallocation
+
+    pack_done     = .FALSE.
+
+    DO n = 1, SIZE(particle_array)
+
+       IF ( .NOT. particle_array(n)%particle_mask )  CYCLE
+
+       ip = particle_array(n)%x * ddx
+       jp = particle_array(n)%y * ddy
+       kp = particle_array(n)%z / dz(1) + 1 + offset_ocean_nzt
+!
+!--    In case of grid stretching a particle might be above or below the  
+!--    previously calculated particle grid box (indices).     
+       DO WHILE( zw(kp) < particle_array(n)%z )
+          kp = kp + 1
+       ENDDO
+
+       DO WHILE( zw(kp-1) > particle_array(n)%z )
+          kp = kp - 1
+       ENDDO
+
+       IF ( ip >= nxl  .AND.  ip <= nxr  .AND.  jp >= nys  .AND.  jp <= nyn    &
+            .AND.  kp >= nzb+1  .AND.  kp <= nzt)  THEN ! particle stays on processor
+          number_of_particles = prt_count(kp,jp,ip)
+          particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+
+          pindex = prt_count(kp,jp,ip)+1
+          IF( pindex > SIZE(grid_particles(kp,jp,ip)%particles) )  THEN
+             IF ( pack_done )  THEN
+                CALL realloc_particles_array (ip,jp,kp)
+             ELSE
+                CALL lpm_pack
+                prt_count(kp,jp,ip) = number_of_particles
+                pindex = prt_count(kp,jp,ip)+1
+                IF ( pindex > SIZE(grid_particles(kp,jp,ip)%particles) )  THEN
+                   CALL realloc_particles_array (ip,jp,kp)
+                ENDIF
+                pack_done = .TRUE.
+             ENDIF
+          ENDIF
+          grid_particles(kp,jp,ip)%particles(pindex) = particle_array(n)
+          prt_count(kp,jp,ip) = pindex
+       ELSE
+          IF ( jp <= nys - 1 )  THEN
+             nr_move_south = nr_move_south+1
+!
+!--          Before particle information is swapped to exchange-array, check 
+!--          if enough memory is allocated. If required, reallocate exchange
+!--          array.
+             IF ( nr_move_south > SIZE(move_also_south) )  THEN
+!
+!--             At first, allocate further temporary array to swap particle 
+!--             information.
+                ALLOCATE( temp_ns(SIZE(move_also_south)+NR_2_direction_move) )
+                temp_ns(1:nr_move_south-1) = move_also_south(1:nr_move_south-1)
+                DEALLOCATE( move_also_south )
+                ALLOCATE( move_also_south(SIZE(temp_ns)) )
+                move_also_south(1:nr_move_south-1) = temp_ns(1:nr_move_south-1)
+                DEALLOCATE( temp_ns )
+
+             ENDIF
+
+             move_also_south(nr_move_south) = particle_array(n)
+
+             IF ( jp == -1 )  THEN
+!
+!--             Apply boundary condition along y
+                IF ( ibc_par_ns == 0 )  THEN
+                   move_also_south(nr_move_south)%y =                          &
+                      move_also_south(nr_move_south)%y + ( ny + 1 ) * dy
+                   move_also_south(nr_move_south)%origin_y =                   &
+                      move_also_south(nr_move_south)%origin_y + ( ny + 1 ) * dy
+                ELSEIF ( ibc_par_ns == 1 )  THEN
+!
+!--                Particle absorption
+                   move_also_south(nr_move_south)%particle_mask = .FALSE.
+                   deleted_particles = deleted_particles + 1
+
+                ELSEIF ( ibc_par_ns == 2 )  THEN
+!
+!--                Particle reflection
+                   move_also_south(nr_move_south)%y       =                    &
+                      -move_also_south(nr_move_south)%y
+                   move_also_south(nr_move_south)%speed_y =                    &
+                      -move_also_south(nr_move_south)%speed_y
+
+                ENDIF
+             ENDIF
+          ELSEIF ( jp >= nyn+1 )  THEN
+             nr_move_north = nr_move_north+1
+!
+!--          Before particle information is swapped to exchange-array, check 
+!--          if enough memory is allocated. If required, reallocate exchange
+!--          array.
+             IF ( nr_move_north > SIZE(move_also_north) )  THEN
+!
+!--             At first, allocate further temporary array to swap particle 
+!--             information.
+                ALLOCATE( temp_ns(SIZE(move_also_north)+NR_2_direction_move) )
+                temp_ns(1:nr_move_north-1) = move_also_south(1:nr_move_north-1)
+                DEALLOCATE( move_also_north )
+                ALLOCATE( move_also_north(SIZE(temp_ns)) )
+                move_also_north(1:nr_move_north-1) = temp_ns(1:nr_move_north-1)
+                DEALLOCATE( temp_ns )
+
+             ENDIF
+
+             move_also_north(nr_move_north) = particle_array(n)
+             IF ( jp == ny+1 )  THEN
+!
+!--             Apply boundary condition along y
+                IF ( ibc_par_ns == 0 )  THEN
+
+                   move_also_north(nr_move_north)%y =                          &
+                      move_also_north(nr_move_north)%y - ( ny + 1 ) * dy
+                   move_also_north(nr_move_north)%origin_y =                   &
+                      move_also_north(nr_move_north)%origin_y - ( ny + 1 ) * dy
+                ELSEIF ( ibc_par_ns == 1 )  THEN
+!
+!--                Particle absorption
+                   move_also_north(nr_move_north)%particle_mask = .FALSE.
+                   deleted_particles = deleted_particles + 1
+
+                ELSEIF ( ibc_par_ns == 2 )  THEN
+!
+!--                Particle reflection
+                   move_also_north(nr_move_north)%y       =                    &
+                      -move_also_north(nr_move_north)%y
+                   move_also_north(nr_move_north)%speed_y =                    &
+                      -move_also_north(nr_move_north)%speed_y
+
+                ENDIF
+             ENDIF
+          ELSE
+             WRITE(0,'(a,8i7)') 'particle out of range ',myid,ip,jp,kp,nxl,nxr,nys,nyn
+          ENDIF
+       ENDIF
+    ENDDO
+
+    RETURN
+
+ END SUBROUTINE lpm_add_particles_to_gridcell
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> If a particle moves from one grid cell to another (on the current 
+!> processor!), this subroutine moves the corresponding element from the
+!> particle array of the old grid cell to the particle array of the new grid 
+!> cell.
+!------------------------------------------------------------------------------!
+ MODULE SUBROUTINE lpm_move_particle
+ 
+    INTEGER(iwp)        ::  i           !< grid index (x) of particle position
+    INTEGER(iwp)        ::  ip          !< index variable along x
+    INTEGER(iwp)        ::  j           !< grid index (y) of particle position
+    INTEGER(iwp)        ::  jp          !< index variable along y
+    INTEGER(iwp)        ::  k           !< grid index (z) of particle position
+    INTEGER(iwp)        ::  kp          !< index variable along z
+    INTEGER(iwp)        ::  n           !< index variable for particle array
+    INTEGER(iwp)        ::  np_before_move !< number of particles per grid box before moving
+    INTEGER(iwp)        ::  pindex      !< dummy argument for number of new particle per grid box
+
+    TYPE(particle_type), DIMENSION(:), POINTER  ::  particles_before_move !< particles before moving
+
+    CALL cpu_log( log_point_s(41), 'lpm_move_particle', 'start' )
+    CALL lpm_check_cfl
+    DO  ip = nxl, nxr
+       DO  jp = nys, nyn
+          DO  kp = nzb+1, nzt
+
+             np_before_move = prt_count(kp,jp,ip)
+             IF ( np_before_move <= 0 )  CYCLE
+             particles_before_move => grid_particles(kp,jp,ip)%particles(1:np_before_move)
+             
+             DO  n = 1, np_before_move
+                i = particles_before_move(n)%x * ddx
+                j = particles_before_move(n)%y * ddy
+                k = kp
+!
+!--             Find correct vertical particle grid box (necessary in case of grid stretching)
+!--             Due to the CFL limitations only the neighbouring grid boxes are considered. 
+                IF( zw(k)   < particles_before_move(n)%z ) k = k + 1
+                IF( zw(k-1) > particles_before_move(n)%z ) k = k - 1 
+                
+!--             For lpm_exchange_horiz to work properly particles need to be moved to the outermost gridboxes
+!--             of the respective processor. If the particle index is inside the processor the following lines
+!--             will not change the index
+                i = MIN ( i , nxr )
+                i = MAX ( i , nxl )
+                j = MIN ( j , nyn )
+                j = MAX ( j , nys )
+                
+                k = MIN ( k , nzt )
+                k = MAX ( k , nzb+1 )
+                
+!
+!--             Check, if particle has moved to another grid cell.
+                IF ( i /= ip  .OR.  j /= jp  .OR.  k /= kp )  THEN
+!!
+!--                If the particle stays on the same processor, the particle
+!--                will be added to the particle array of the new processor.
+                   number_of_particles = prt_count(k,j,i)
+                   particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+
+                   pindex = prt_count(k,j,i)+1
+                   IF (  pindex > SIZE(grid_particles(k,j,i)%particles)  )     &
+                   THEN
+                      CALL realloc_particles_array(i,j,k)
+                   ENDIF
+
+                   grid_particles(k,j,i)%particles(pindex) = particles_before_move(n)
+                   prt_count(k,j,i) = pindex
+
+                   particles_before_move(n)%particle_mask = .FALSE.
+                ENDIF
+             ENDDO
+
+          ENDDO
+       ENDDO
+    ENDDO
+
+    CALL cpu_log( log_point_s(41), 'lpm_move_particle', 'stop' )
+
+    RETURN
+
+ END SUBROUTINE lpm_move_particle
+ 
+
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Check CFL-criterion for each particle. If one particle violated the 
+!> criterion the particle will be deleted and a warning message is given.
+!------------------------------------------------------------------------------!
+ SUBROUTINE lpm_check_cfl  
+       
+    IMPLICIT NONE
+    
+    INTEGER(iwp)  ::  i !< running index, x-direction
+    INTEGER(iwp)  ::  j !< running index, y-direction
+    INTEGER(iwp)  ::  k !< running index, z-direction
+    INTEGER(iwp)  ::  n !< running index, number of particles
+
+    DO  i = nxl, nxr
+       DO  j = nys, nyn
+          DO  k = nzb+1, nzt
+             number_of_particles = prt_count(k,j,i)
+             IF ( number_of_particles <= 0 )  CYCLE
+             particles => grid_particles(k,j,i)%particles(1:number_of_particles)         
+             DO n = 1, number_of_particles
+!
+!--             Note, check for CFL does not work at first particle timestep 
+!--             when both, age and age_m are zero. 
+                IF ( particles(n)%age - particles(n)%age_m > 0.0_wp )  THEN  
+                   IF(ABS(particles(n)%speed_x) >                              &
+                      (dx/(particles(n)%age-particles(n)%age_m))  .OR.         &
+                      ABS(particles(n)%speed_y) >                              & 
+                      (dy/(particles(n)%age-particles(n)%age_m))  .OR.         &
+                      ABS(particles(n)%speed_z) >                              &
+                      ((zw(k)-zw(k-1))/(particles(n)%age-particles(n)%age_m))) &
+                   THEN
+                      WRITE( message_string, * )                               &
+                      'Particle violated CFL-criterion: &particle with id ',   &
+                      particles(n)%id, ' will be deleted!'   
+                      CALL message( 'lpm_check_cfl', 'PA0475', 0, 1, -1, 6, 0 )
+                      particles(n)%particle_mask= .FALSE.
+                   ENDIF
+                ENDIF
+             ENDDO
+          ENDDO
+       ENDDO
+    ENDDO   
+
+ END SUBROUTINE lpm_check_cfl
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> If the allocated memory for the particle array do not suffice to add arriving
+!> particles from neighbour grid cells, this subrouting reallocates the 
+!> particle array to assure enough memory is available. 
+!------------------------------------------------------------------------------!
+ MODULE SUBROUTINE realloc_particles_array ( i, j, k, size_in )
+
+    INTEGER(iwp), INTENT(IN)                       ::  i              !<
+    INTEGER(iwp), INTENT(IN)                       ::  j              !<
+    INTEGER(iwp), INTENT(IN)                       ::  k              !<
+    INTEGER(iwp), INTENT(IN), OPTIONAL             ::  size_in        !<
+
+    INTEGER(iwp)                                   ::  old_size        !<
+    INTEGER(iwp)                                   ::  new_size        !<
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  tmp_particles_d !<
+    TYPE(particle_type), DIMENSION(500)            ::  tmp_particles_s !<
+
+    old_size = SIZE(grid_particles(k,j,i)%particles)
+
+    IF ( PRESENT(size_in) )   THEN
+       new_size = size_in
+    ELSE
+       new_size = old_size * ( 1.0_wp + alloc_factor / 100.0_wp )
+    ENDIF
+
+    new_size = MAX( new_size, min_nr_particle, old_size + 1 )
+
+    IF ( old_size <= 500 )  THEN
+
+       tmp_particles_s(1:old_size) = grid_particles(k,j,i)%particles(1:old_size)
+
+       DEALLOCATE(grid_particles(k,j,i)%particles)
+       ALLOCATE(grid_particles(k,j,i)%particles(new_size))
+
+       grid_particles(k,j,i)%particles(1:old_size)          = tmp_particles_s(1:old_size)
+       grid_particles(k,j,i)%particles(old_size+1:new_size) = zero_particle
+
+    ELSE
+
+       ALLOCATE(tmp_particles_d(new_size))
+       tmp_particles_d(1:old_size) = grid_particles(k,j,i)%particles
+
+       DEALLOCATE(grid_particles(k,j,i)%particles)
+       ALLOCATE(grid_particles(k,j,i)%particles(new_size))
+
+       grid_particles(k,j,i)%particles(1:old_size)          = tmp_particles_d(1:old_size)
+       grid_particles(k,j,i)%particles(old_size+1:new_size) = zero_particle
+
+       DEALLOCATE(tmp_particles_d)
+
+    ENDIF
+    particles => grid_particles(k,j,i)%particles(1:new_size)
+
+    RETURN
+    
+ END SUBROUTINE realloc_particles_array
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Not needed but allocated space for particles is dealloced. 
+!------------------------------------------------------------------------------!
+ SUBROUTINE dealloc_particles_array
+
+ 
+    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 
+ 
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! -----------
+!> Routine for the whole processor
+!> Sort all particles into the 8 respective subgrid boxes
+!------------------------------------------------------------------------------!
+   MODULE SUBROUTINE lpm_sort_in_subboxes
+
+       INTEGER(iwp) ::  i  !<
+       INTEGER(iwp) ::  ip !<
+       INTEGER(iwp) ::  is !<
+       INTEGER(iwp) ::  j  !<
+       INTEGER(iwp) ::  jp !<
+       INTEGER(iwp) ::  kp !<
+       INTEGER(iwp) ::  m  !<
+       INTEGER(iwp) ::  n  !<
+       INTEGER(iwp) ::  nn !<
+       INTEGER(iwp) ::  sort_index  !<
+
+       INTEGER(iwp), DIMENSION(0:7) ::  sort_count  !<
+
+       TYPE(particle_type), DIMENSION(:,:), ALLOCATABLE ::  sort_particles  !<
+
+       CALL cpu_log( log_point_s(51), 'lpm_sort_in_subboxes', 'start' )
+       DO  ip = nxl, nxr
+          DO  jp = nys, nyn
+             DO  kp = nzb+1, nzt
+                number_of_particles = prt_count(kp,jp,ip)
+                IF ( number_of_particles <= 0 )  CYCLE
+                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
+                      
+                nn = 0
+                sort_count = 0
+                ALLOCATE( sort_particles(number_of_particles, 0:7) )
+                
+                DO  n = 1, number_of_particles
+                   sort_index = 0
+
+                   IF ( particles(n)%particle_mask )  THEN
+                      nn = nn + 1
+!
+!--                   Sorting particles with a binary scheme
+!--                   sort_index=111_2=7_10 -> particle at the left,south,bottom subgridbox
+!--                   sort_index=000_2=0_10 -> particle at the right,north,top subgridbox
+!--                   For this the center of the gridbox is calculated 
+                      i = (particles(n)%x + 0.5_wp * dx) * ddx
+                      j = (particles(n)%y + 0.5_wp * dy) * ddy
+                      
+                      IF ( i == ip )  sort_index = sort_index + 4 
+                      IF ( j == jp )  sort_index = sort_index + 2                      
+                      IF ( zu(kp) > particles(n)%z ) sort_index = sort_index + 1
+                      
+                      sort_count(sort_index) = sort_count(sort_index) + 1
+                      m = sort_count(sort_index)
+                      sort_particles(m,sort_index) = particles(n)
+                      sort_particles(m,sort_index)%block_nr = sort_index
+                   ENDIF
+                ENDDO
+
+                nn = 0
+                DO is = 0,7
+                   grid_particles(kp,jp,ip)%start_index(is) = nn + 1
+                   DO n = 1,sort_count(is)
+                      nn = nn + 1
+                      particles(nn) = sort_particles(n,is)
+                   ENDDO
+                   grid_particles(kp,jp,ip)%end_index(is) = nn
+                ENDDO
+
+                number_of_particles = nn    
+                prt_count(kp,jp,ip) = number_of_particles
+                DEALLOCATE(sort_particles)
+             ENDDO
+          ENDDO
+       ENDDO
+       CALL cpu_log( log_point_s(51), 'lpm_sort_in_subboxes', 'stop' )
+
+    END SUBROUTINE lpm_sort_in_subboxes
+
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Move all particles not marked for deletion to lowest indices (packing)
+!------------------------------------------------------------------------------!
+    MODULE SUBROUTINE lpm_pack
+    
+       INTEGER(iwp) ::  n       !<
+       INTEGER(iwp) ::  nn      !<
+!
+!--    Find out elements marked for deletion and move data from highest index
+!--    values to these free indices
+       nn = number_of_particles
+
+       DO WHILE ( .NOT. particles(nn)%particle_mask )
+          nn = nn-1
+          IF ( nn == 0 )  EXIT
+       ENDDO
+
+       IF ( nn > 0 )  THEN
+          DO  n = 1, number_of_particles
+             IF ( .NOT. particles(n)%particle_mask )  THEN
+                particles(n) = particles(nn)
+                nn = nn - 1
+                DO WHILE ( .NOT. particles(nn)%particle_mask )
+                   nn = nn-1
+                   IF ( n == nn )  EXIT
+                ENDDO
+             ENDIF
+             IF ( n == nn )  EXIT
+          ENDDO
+       ENDIF
+
+!
+!--    The number of deleted particles has been determined in routines
+!--    lpm_boundary_conds, lpm_droplet_collision, and lpm_exchange_horiz
+       number_of_particles = nn
+
+    END SUBROUTINE lpm_pack 
+                
+                
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> Sort particles in each sub-grid box into two groups: particles that already
+!> completed the LES timestep, and particles that need further timestepping to
+!> complete the LES timestep. 
+!------------------------------------------------------------------------------!
+    MODULE SUBROUTINE lpm_sort_timeloop_done
+
+       INTEGER(iwp) :: end_index     !< particle end index for each sub-box
+       INTEGER(iwp) :: i             !< index of particle grid box in x-direction
+       INTEGER(iwp) :: j             !< index of particle grid box in y-direction
+       INTEGER(iwp) :: k             !< index of particle grid box in z-direction
+       INTEGER(iwp) :: n             !< running index for number of particles
+       INTEGER(iwp) :: nb            !< index of subgrid boux
+       INTEGER(iwp) :: nf            !< indices for particles in each sub-box that already finalized their substeps
+       INTEGER(iwp) :: nnf           !< indices for particles in each sub-box that need further treatment
+       INTEGER(iwp) :: num_finalized !< number of particles in each sub-box that already finalized their substeps
+       INTEGER(iwp) :: start_index   !< particle start index for each sub-box
+
+       TYPE(particle_type), DIMENSION(:), ALLOCATABLE :: sort_particles  !< temporary particle array 
+
+       DO  i = nxl, nxr
+          DO  j = nys, nyn
+             DO  k = nzb+1, nzt
+
+                number_of_particles = prt_count(k,j,i)
+                IF ( number_of_particles <= 0 )  CYCLE
+
+                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
+
+                DO  nb = 0, 7
+!
+!--                Obtain start and end index for each subgrid box
+                   start_index = grid_particles(k,j,i)%start_index(nb)
+                   end_index   = grid_particles(k,j,i)%end_index(nb)
+!
+!--                Allocate temporary array used for sorting. 
+                   ALLOCATE( sort_particles(start_index:end_index) )
+!
+!--                Determine number of particles already completed the LES 
+!--                timestep, and write them into a temporary array. 
+                   nf = start_index
+                   num_finalized = 0
+                   DO  n = start_index, end_index
+                      IF ( dt_3d - particles(n)%dt_sum < 1E-8_wp )  THEN
+                         sort_particles(nf) = particles(n)
+                         nf                 = nf + 1
+                         num_finalized      = num_finalized + 1
+                      ENDIF
+                   ENDDO
+!
+!--                Determine number of particles that not completed the LES 
+!--                timestep, and write them into a temporary array. 
+                   nnf = nf
+                   DO  n = start_index, end_index
+                      IF ( dt_3d - particles(n)%dt_sum > 1E-8_wp )  THEN
+                         sort_particles(nnf) = particles(n)
+                         nnf                 = nnf + 1
+                      ENDIF
+                   ENDDO
+!
+!--                Write back sorted particles
+                   particles(start_index:end_index) =                          &
+                                           sort_particles(start_index:end_index)
+!
+!--                Determine updated start_index, used to masked already 
+!--                completed particles. 
+                   grid_particles(k,j,i)%start_index(nb) =                     &
+                                      grid_particles(k,j,i)%start_index(nb)    &
+                                    + num_finalized
+!
+!--                Deallocate dummy array
+                   DEALLOCATE ( sort_particles )
+!
+!--                Finally, if number of non-completed particles is non zero 
+!--                in any of the sub-boxes, set control flag appropriately. 
+                   IF ( nnf > nf )                                             &
+                      grid_particles(k,j,i)%time_loop_done = .FALSE.
+
+                ENDDO
+             ENDDO
+          ENDDO
+       ENDDO
+
+    END SUBROUTINE lpm_sort_timeloop_done 
+ 
+ 
+END SUBMODULE
Index: palm/trunk/SOURCE/lpm.f90
===================================================================
--- palm/trunk/SOURCE/lpm.f90	(revision 4015)
+++ 	(revision )
@@ -1,532 +1,0 @@
-MODULE lpm_mod
-
-!> @file lpm.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! bugfix to guarantee correct particle releases in case that the release
-! interval is smaller than the model timestep
-! 
-! 2801 2018-02-14 16:01:55Z thiele
-! Changed lpm from subroutine to module.
-! Introduce particle transfer in nested models.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2701 2017-12-15 15:40:50Z suehring
-! Changes from last commit documented
-! 
-! 2698 2017-12-14 18:46:24Z suehring
-! Grid indices passed to lpm_boundary_conds. (responsible Philipp Thiele)
-!
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done
-! 
-! 2418 2017-09-06 15:24:24Z suehring
-! Major bugfixes in modeling SGS particle speeds (since revision 1359).
-! Particle sorting added to distinguish between already completed and 
-! non-completed particles.
-! 
-! 2263 2017-06-08 14:59:01Z schwenkel
-! Implemented splitting and merging algorithm
-! 
-! 2233 2017-05-30 18:08:54Z suehring
-!
-! 2232 2017-05-30 17:47:52Z suehring
-! Adjustments to new topography concept
-! 
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1936 2016-06-13 13:37:44Z suehring
-! Call routine for deallocation of unused memory.
-! Formatting adjustments
-! 
-! 1929 2016-06-09 16:25:25Z suehring
-! Call wall boundary conditions only if particles are in the vertical range of 
-! topography.
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed.
-!
-! Initialization of sgs model not necessary for the use of cloud_droplets and
-! use_sgs_for_particles.
-!
-! lpm_release_set integrated.
-!
-! Unused variabled removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1416 2014-06-04 16:04:03Z suehring
-! user_lpm_advec is called for each gridpoint.
-! Bugfix: in order to prevent an infinite loop, time_loop_done is set .TRUE. 
-! at the head of the do-loop.  
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1318 2014-03-17 13:35:16Z raasch
-! module interfaces removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 851 2012-03-15 14:32:58Z raasch
-! Bugfix: resetting of particle_mask and tail mask moved from routine
-! lpm_exchange_horiz to here (end of sub-timestep loop)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! original routine advec_particles split into several subroutines and renamed
-! lpm
-!
-! 831 2012-02-22 00:29:39Z raasch
-! thermal_conductivity_l and diff_coeff_l now depend on temperature and
-! pressure
-!
-! 828 2012-02-21 12:00:36Z raasch
-! fast hall/wang kernels with fixed radius/dissipation classes added,
-! particle feature color renamed class, routine colker renamed
-! recalculate_kernel,
-! lower limit for droplet radius changed from 1E-7 to 1E-8
-!
-! Bugfix: transformation factor for dissipation changed from 1E5 to 1E4
-!
-! 825 2012-02-19 03:03:44Z raasch
-! droplet growth by condensation may include curvature and solution effects,
-! initialisation of temporary particle array for resorting removed,
-! particle attributes speed_x|y|z_sgs renamed rvar1|2|3,
-! module wang_kernel_mod renamed lpm_collision_kernels_mod,
-! wang_collision_kernel renamed wang_kernel
-!
-!
-! Revision 1.1  1999/11/25 16:16:06  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> Particle advection
-!------------------------------------------------------------------------------!
- 
-
-    USE arrays_3d,                                                             &
-        ONLY:  ql_c, ql_v, ql_vp
-
-    USE control_parameters,                                                    &
-        ONLY:  cloud_droplets, dt_3d, dt_3d_reached, dt_3d_reached_l,          &
-               molecular_viscosity, simulated_time, topography
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point, log_point_s
-
-    USE indices,                                                               &
-        ONLY: nxl, nxr, nys, nyn, nzb, nzb_max, nzt
-
-    USE kinds
-
-    USE lpm_exchange_horiz_mod,                                                &
-        ONLY:  dealloc_particles_array, lpm_exchange_horiz, lpm_move_particle
-
-    USE lpm_init_mod,                                                          &
-        ONLY: lpm_create_particle, PHASE_RELEASE
-
-    USE lpm_pack_and_sort_mod
-
-    USE particle_attributes,                                                   &
-        ONLY:  collision_kernel, deleted_particles, deallocate_memory,         &
-               dt_write_particle_data, dt_prel, end_time_prel, grid_particles, &
-               last_particle_release_time, merging, number_of_particles,       &
-               number_of_particle_groups, particles, particle_groups,          &
-               prt_count, splitting, step_dealloc, 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, use_sgs_for_particles,     &
-               write_particle_statistics
-                              
-    USE pegrid
-
- USE pmc_particle_interface,                                                &
-     ONLY: pmcp_c_get_particle_from_parent, pmcp_p_fill_particle_win,       &
-           pmcp_c_send_particle_to_parent, pmcp_p_empty_particle_win,       &
-           pmcp_p_delete_particles_in_fine_grid_area
-
-    USE pmc_interface,                                                      &
-       ONLY: nested_run
-
- IMPLICIT NONE
- PRIVATE
- SAVE
-
- INTERFACE lpm
-    MODULE PROCEDURE lpm
- END INTERFACE lpm
-
- PUBLIC lpm
-
-CONTAINS
- SUBROUTINE lpm
-    IMPLICIT NONE
-
-    INTEGER(iwp)       ::  i                  !<
-    INTEGER(iwp)       ::  ie                 !<
-    INTEGER(iwp)       ::  is                 !<
-    INTEGER(iwp)       ::  j                  !<
-    INTEGER(iwp)       ::  je                 !<
-    INTEGER(iwp)       ::  js                 !<
-    INTEGER(iwp), SAVE ::  lpm_count = 0      !<
-    INTEGER(iwp)       ::  k                  !<
-    INTEGER(iwp)       ::  ke                 !<
-    INTEGER(iwp)       ::  ks                 !<
-    INTEGER(iwp)       ::  m                  !<
-    INTEGER(iwp), SAVE ::  steps = 0          !<
-
-    LOGICAL            ::  first_loop_stride  !<
-
-    CALL cpu_log( log_point(25), 'lpm', 'start' )
-
-!
-!-- Write particle data at current time on file.
-!-- This has to be done here, before particles are further processed,
-!-- because they may be deleted within this timestep (in case that
-!-- dt_write_particle_data = dt_prel = particle_maximum_age).
-    time_write_particle_data = time_write_particle_data + dt_3d
-    IF ( time_write_particle_data >= dt_write_particle_data )  THEN
-
-       CALL lpm_data_output_particles
-!
-!--    The MOD function allows for changes in the output interval with restart
-!--    runs.
-       time_write_particle_data = MOD( time_write_particle_data, &
-                                  MAX( dt_write_particle_data, dt_3d ) )
-    ENDIF
-
-!
-!-- Initialize arrays for marking those particles to be deleted after the
-!-- (sub-) timestep
-    deleted_particles = 0
-
-!
-!-- Initialize variables used for accumulating the number of particles
-!-- exchanged between the subdomains during all sub-timesteps (if sgs
-!-- velocities are included). These data are output further below on the
-!-- particle statistics file.
-    trlp_count_sum      = 0
-    trlp_count_recv_sum = 0
-    trrp_count_sum      = 0
-    trrp_count_recv_sum = 0
-    trsp_count_sum      = 0
-    trsp_count_recv_sum = 0
-    trnp_count_sum      = 0
-    trnp_count_recv_sum = 0
-
-
-!
-!-- Calculate exponential term used in case of particle inertia for each
-!-- of the particle groups
-    DO  m = 1, number_of_particle_groups
-       IF ( particle_groups(m)%density_ratio /= 0.0_wp )  THEN
-          particle_groups(m)%exp_arg  =                                        &
-                    4.5_wp * particle_groups(m)%density_ratio *                &
-                    molecular_viscosity / ( particle_groups(m)%radius )**2
-
-          particle_groups(m)%exp_term = EXP( -particle_groups(m)%exp_arg *     &
-                    dt_3d )
-       ENDIF
-    ENDDO
-
-!
-!-- If necessary, release new set of particles
-    IF ( ( simulated_time - last_particle_release_time ) >= dt_prel  .AND. end_time_prel > simulated_time ) &
-    THEN
-
-       DO WHILE ( ( simulated_time - last_particle_release_time ) >= dt_prel )
-
-          CALL lpm_create_particle( phase_release )
-          last_particle_release_time = last_particle_release_time + dt_prel
-
-       ENDDO
-
-    ENDIF
-!
-!-- Reset summation arrays
-    IF ( cloud_droplets)  THEN
-       ql_c  = 0.0_wp
-       ql_v  = 0.0_wp
-       ql_vp = 0.0_wp
-    ENDIF
-
-    first_loop_stride = .TRUE.
-    grid_particles(:,:,:)%time_loop_done = .TRUE.
-!
-!-- Timestep loop for particle advection.
-!-- This loop has to be repeated until the advection time of every particle
-!-- (within the total domain!) has reached the LES timestep (dt_3d).
-!-- In case of including the SGS velocities, the particle timestep may be
-!-- smaller than the LES timestep (because of the Lagrangian timescale 
-!-- restriction) and particles may require to undergo several particle 
-!-- timesteps, before the LES timestep is reached. Because the number of these 
-!-- particle timesteps to be carried out is unknown at first, these steps are 
-!-- carried out in the following infinite loop with exit condition.
-    DO
-       CALL cpu_log( log_point_s(44), 'lpm_advec', 'start' )
-       CALL cpu_log( log_point_s(44), 'lpm_advec', 'pause' )
-       
-!
-!--    If particle advection includes SGS velocity components, calculate the
-!--    required SGS quantities (i.e. gradients of the TKE, as well as 
-!--    horizontally averaged profiles of the SGS TKE and the resolved-scale 
-!--    velocity variances)
-       IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
-          CALL lpm_init_sgs_tke
-       ENDIF
-
-!
-!--    In case SGS-particle speed is considered, particles may carry out 
-!--    several particle timesteps. In order to prevent unnecessary 
-!--    treatment of particles that already reached the final time level, 
-!--    particles are sorted into contiguous blocks of finished and 
-!--    not-finished particles, in addition to their already sorting
-!--    according to their sub-boxes. 
-       IF ( .NOT. first_loop_stride  .AND.  use_sgs_for_particles )            &
-          CALL lpm_sort_timeloop_done
-
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-
-                number_of_particles = prt_count(k,j,i)
-!
-!--             If grid cell gets empty, flag must be true
-                IF ( number_of_particles <= 0 )  THEN
-                   grid_particles(k,j,i)%time_loop_done = .TRUE.
-                   CYCLE
-                ENDIF
-
-                IF ( .NOT. first_loop_stride  .AND.  &
-                     grid_particles(k,j,i)%time_loop_done ) CYCLE
-
-                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-
-                particles(1:number_of_particles)%particle_mask = .TRUE.
-!
-!--             Initialize the variable storing the total time that a particle 
-!--             has advanced within the timestep procedure
-                IF ( first_loop_stride )  THEN
-                   particles(1:number_of_particles)%dt_sum = 0.0_wp
-                ENDIF
-!
-!--             Particle (droplet) growth by condensation/evaporation and 
-!--             collision
-                IF ( cloud_droplets  .AND.  first_loop_stride)  THEN
-!
-!--                Droplet growth by condensation / evaporation
-                   CALL lpm_droplet_condensation(i,j,k)
-!
-!--                Particle growth by collision
-                   IF ( collision_kernel /= 'none' )  THEN
-                      CALL lpm_droplet_collision(i,j,k)
-                   ENDIF
-
-                ENDIF
-!
-!--             Initialize the switch used for the loop exit condition checked
-!--             at the end of this loop. If at least one particle has failed to
-!--             reach the LES timestep, this switch will be set false in 
-!--             lpm_advec.
-                dt_3d_reached_l = .TRUE.
-
-!
-!--             Particle advection
-                CALL lpm_advec(i,j,k)
-!
-!--             Particle reflection from walls. Only applied if the particles 
-!--             are in the vertical range of the topography. (Here, some
-!--             optimization is still possible.)
-                IF ( topography /= 'flat' .AND. k < nzb_max + 2 )  THEN
-                   CALL  lpm_boundary_conds( 'walls', i, j, k )
-                ENDIF
-!
-!--             User-defined actions after the calculation of the new particle 
-!--             position
-                CALL user_lpm_advec(i,j,k)
-!
-!--             Apply boundary conditions to those particles that have crossed 
-!--             the top or bottom boundary and delete those particles, which are
-!--             older than allowed
-                CALL lpm_boundary_conds( 'bottom/top', i, j, k )
-!
-!---            If not all particles of the actual grid cell have reached the 
-!--             LES timestep, this cell has to do another loop iteration. Due to
-!--             the fact that particles can move into neighboring grid cells, 
-!--             these neighbor cells also have to perform another loop iteration.
-!--             Please note, this realization does not work properly if 
-!--             particles move into another subdomain. 
-                IF ( .NOT. dt_3d_reached_l )  THEN
-                   ks = MAX(nzb+1,k-1)
-                   ke = MIN(nzt,k+1)
-                   js = MAX(nys,j-1)
-                   je = MIN(nyn,j+1)
-                   is = MAX(nxl,i-1)
-                   ie = MIN(nxr,i+1)
-                   grid_particles(ks:ke,js:je,is:ie)%time_loop_done = .FALSE.
-                ELSE
-                   grid_particles(k,j,i)%time_loop_done = .TRUE.
-                ENDIF
-
-             ENDDO
-          ENDDO
-       ENDDO
-
-       steps = steps + 1
-       dt_3d_reached_l = ALL(grid_particles(:,:,:)%time_loop_done)
-!
-!--    Find out, if all particles on every PE have completed the LES timestep
-!--    and set the switch corespondingly
-#if defined( __parallel )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( dt_3d_reached_l, dt_3d_reached, 1, MPI_LOGICAL, &
-                           MPI_LAND, comm2d, ierr )
-#else
-       dt_3d_reached = dt_3d_reached_l
-#endif
-
-       CALL cpu_log( log_point_s(44), 'lpm_advec', 'stop' )
-
-!
-!--    Apply splitting and merging algorithm
-       IF ( cloud_droplets )  THEN
-          IF ( splitting ) THEN
-             CALL lpm_splitting
-          ENDIF
-          IF ( merging ) THEN
-             CALL lpm_merging
-          ENDIF
-       ENDIF
-!
-!--    Move Particles local to PE to a different grid cell
-       CALL lpm_move_particle
-
-!
-!--    Horizontal boundary conditions including exchange between subdmains
-       CALL lpm_exchange_horiz
-
-       IF ( .NOT. dt_3d_reached .OR. .NOT. nested_run )   THEN    ! IF .FALSE., lpm_sort_in_subboxes is done inside pcmp
-!
-!--       Pack particles (eliminate those marked for deletion),
-!--       determine new number of particles
-          CALL lpm_sort_in_subboxes
-!
-!--       Initialize variables for the next (sub-) timestep, i.e., for marking
-!--       those particles to be deleted after the timestep
-          deleted_particles = 0
-       ENDIF
-
-       IF ( dt_3d_reached )  EXIT
-
-       first_loop_stride = .FALSE.
-    ENDDO   ! timestep loop
-!    
-!-- in case of nested runs do the transfer of particles after every full model time step
-    IF ( nested_run )   THEN
-       CALL particles_from_parent_to_child
-       CALL particles_from_child_to_parent
-       CALL pmcp_p_delete_particles_in_fine_grid_area
-
-       CALL lpm_sort_in_subboxes
-
-       deleted_particles = 0
-    ENDIF
-
-!
-!-- Calculate the new liquid water content for each grid box
-    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
-
-!
-!-- Set particle attributes.
-!-- Feature is not available if collision is activated, because the respective
-!-- particle attribute (class) is then used for storing the particle radius
-!-- class.
-    IF ( collision_kernel == 'none' )  CALL lpm_set_attributes
-
-!
-!-- Set particle attributes defined by the user
-    CALL user_lpm_set_attributes
-
-!
-!-- Write particle statistics (in particular the number of particles
-!-- exchanged between the subdomains) on file
-    IF ( write_particle_statistics )  CALL lpm_write_exchange_statistics
-
-    CALL cpu_log( log_point(25), 'lpm', 'stop' )
-
- END SUBROUTINE lpm
-
- SUBROUTINE particles_from_parent_to_child
-    IMPLICIT NONE
-
-    CALL pmcp_c_get_particle_from_parent                         ! Child actions
-    CALL pmcp_p_fill_particle_win                                ! Parent actions
-
-    RETURN
- END SUBROUTINE particles_from_parent_to_child
-
- SUBROUTINE particles_from_child_to_parent
-    IMPLICIT NONE
-
-    CALL pmcp_c_send_particle_to_parent                         ! Child actions
-    CALL pmcp_p_empty_particle_win                              ! Parent actions
-
-    RETURN
- END SUBROUTINE particles_from_child_to_parent
-
-
-END MODULE lpm_mod
Index: palm/trunk/SOURCE/lpm_advec.f90
===================================================================
--- palm/trunk/SOURCE/lpm_advec.f90	(revision 4015)
+++ 	(revision )
@@ -1,1192 +1,0 @@
-!> @file lpm_advec.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! variables documented
-! 
-! 3274 2018-09-24 15:42:55Z knoop
-! Modularization of all bulk cloud physics code components
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3207 2018-08-27 12:55:33Z schwenkel
-! Minor bugfix for sgs-velocities in case of cloud droplets
-! 
-! 3189 2018-08-06 13:18:55Z raasch
-! Bugfix: Index of the array dzw has to be k+1 during the interpolation. 
-! Otherwise k=0 causes an abortion because dzw is allocated from 1 to nzt+1
-! 
-! 3065 2018-06-12 07:03:02Z Giersch
-! dz values were replaced by dzw or dz(1) to allow for right vertical stretching
-! 
-! 2969 2018-04-13 11:55:09Z thiele
-! Bugfix in Interpolation indices.
-! 
-! 2886 2018-03-14 11:51:53Z thiele
-! Bugfix in passive particle SGS Model:
-! Sometimes the added SGS velocities would lead to a violation of the CFL 
-! criterion for single particles. For this a check was added after the 
-! calculation of SGS velocities.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2701 2017-12-15 15:40:50Z suehring
-! Changes from last commit documented
-! 
-! 2698 2017-12-14 18:46:24Z suehring
-! Particle interpolations at walls in case of SGS velocities revised and not
-! required parts are removed. (responsible Philipp Thiele)
-! Bugfix in get_topography_top_index
-!
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2630 2017-11-20 12:58:20Z schwenkel
-! Removed indices ilog and jlog which are no longer needed since particle box
-! locations are identical to scalar boxes and topography.
-! 
-! 2628 2017-11-20 12:40:38Z raasch
-! bugfix in logarithmic interpolation of v-component (usws was used by mistake)
-! 
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done 
-! 
-! 2417 2017-09-06 15:22:27Z suehring
-! Particle loops adapted for sub-box structure, i.e. for each sub-box the 
-! particle loop runs from start_index up to end_index instead from 1 to 
-! number_of_particles. This way, it is possible to skip unnecessary 
-! computations for particles that already completed the LES timestep. 
-! 
-! 2318 2017-07-20 17:27:44Z suehring
-! Get topography top index via Function call 
-! 
-! 2317 2017-07-20 17:27:19Z suehring
-!
-! 2232 2017-05-30 17:47:52Z suehring
-! Adjustments to new topography and surface concept
-! 
-! 2100 2017-01-05 16:40:16Z suehring
-! Prevent extremely large SGS-velocities in regions where TKE is zero, e.g. 
-! at the begin of simulations and/or in non-turbulent regions. 
-! 
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1936 2016-06-13 13:37:44Z suehring
-! Formatting adjustments
-! 
-! 1929 2016-06-09 16:25:25Z suehring
-! Put stochastic equation in an extra subroutine.
-! Set flag for stochastic equation to communicate whether a particle is near 
-! topography. This case, memory and drift term are disabled in the Weil equation. 
-!
-! Enable vertical logarithmic interpolation also above topography. This case, 
-! set a lower limit for the friction velocity, as it can become very small 
-! in narrow street canyons, leading to too large particle speeds.
-!
-! 1888 2016-04-21 12:20:49Z suehring
-! Bugfix concerning logarithmic interpolation of particle speed
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Random velocity fluctuations for particles added. Terminal fall velocity
-! for droplets is calculated from a parameterization (which is better than
-! the previous, physically correct calculation, which demands a very short
-! time step that is not used in the model).
-!
-! Unused variables deleted.
-!
-! 1691 2015-10-26 16:17:44Z maronga
-! Renamed prandtl_layer to constant_flux_layer.
-!
-! 1685 2015-10-08 07:32:13Z raasch
-! TKE check for negative values (so far, only zero value was checked)
-! offset_ocean_nzt_m1 removed
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1583 2015-04-15 12:16:27Z suehring
-! Bugfix: particle advection within Prandtl-layer in case of Galilei 
-! transformation.
-!
-! 1369 2014-04-24 05:57:38Z raasch
-! usage of module interfaces removed
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! REAL constants defined as wp_kind
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1314 2014-03-14 18:25:17Z suehring
-! Vertical logarithmic interpolation of horizontal particle speed for particles 
-! between roughness height and first vertical grid level.
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Calculation of new particle positions due to advection using a simple Euler
-!> scheme. Particles may feel inertia effects. SGS transport can be included
-!> using the stochastic model of Weil et al. (2004, JAS, 61, 2877-2887).
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_advec (ip,jp,kp)
- 
-
-    USE arrays_3d,                                                             &
-        ONLY:  de_dx, de_dy, de_dz, diss, dzw, e, km, u, v, w, zu, zw
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  g, kappa
-
-    USE cpulog
-
-    USE pegrid
-
-    USE control_parameters,                                                    &
-        ONLY:  cloud_droplets, constant_flux_layer, dt_3d, dt_3d_reached_l,    &
-               dz, topography, u_gtrans, v_gtrans
-
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-        
-    USE indices,                                                               &
-        ONLY:  nzb, nzt, wall_flags_0
-        
-    USE kinds
-    
-    USE particle_attributes,                                                   &
-        ONLY:  block_offset, c_0, dt_min_part, grid_particles, iran_part,      &
-               log_z_z0, number_of_particles, number_of_sublayers,             &
-               particles, particle_groups, sgs_wf_part,                        &
-               use_sgs_for_particles, vertical_particle_advection, z0_av_global
-        
-    USE statistics,                                                            &
-        ONLY:  hom
-
-    USE surface_mod,                                                           &
-        ONLY:  get_topography_top_index_ji, surf_def_h, surf_lsm_h, surf_usm_h
-
-    IMPLICIT NONE
-
-    LOGICAL ::  subbox_at_wall !< flag to see if the current subgridbox is adjacent to a wall
-
-    INTEGER(iwp) ::  i                           !< index variable along x
-    INTEGER(iwp) ::  ip                          !< index variable along x 
-    INTEGER(iwp) ::  j                           !< index variable along y 
-    INTEGER(iwp) ::  jp                          !< index variable along y 
-    INTEGER(iwp) ::  k                           !< index variable along z 
-    INTEGER(iwp) ::  k_wall                      !< vertical index of topography top 
-    INTEGER(iwp) ::  kp                          !< index variable along z 
-    INTEGER(iwp) ::  kw                          !< index variable along z
-    INTEGER(iwp) ::  n                           !< loop variable over all particles in a grid box
-    INTEGER(iwp) ::  nb                          !< block number particles are sorted in
-    INTEGER(iwp) ::  surf_start                  !< Index on surface data-type for current grid box 
-
-    INTEGER(iwp), DIMENSION(0:7) ::  start_index !< start particle index for current block
-    INTEGER(iwp), DIMENSION(0:7) ::  end_index   !< start particle index for current block
-
-    REAL(wp) ::  aa                 !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  bb                 !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  cc                 !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  d_z_p_z0           !< inverse of interpolation length for logarithmic interpolation 
-    REAL(wp) ::  dd                 !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  de_dx_int_l        !< x/y-interpolated TKE gradient (x) at particle position at lower vertical level
-    REAL(wp) ::  de_dx_int_u        !< x/y-interpolated TKE gradient (x) at particle position at upper vertical level
-    REAL(wp) ::  de_dy_int_l        !< x/y-interpolated TKE gradient (y) at particle position at lower vertical level
-    REAL(wp) ::  de_dy_int_u        !< x/y-interpolated TKE gradient (y) at particle position at upper vertical level
-    REAL(wp) ::  de_dt              !< temporal derivative of TKE experienced by the particle
-    REAL(wp) ::  de_dt_min          !< lower level for temporal TKE derivative
-    REAL(wp) ::  de_dz_int_l        !< x/y-interpolated TKE gradient (z) at particle position at lower vertical level
-    REAL(wp) ::  de_dz_int_u        !< x/y-interpolated TKE gradient (z) at particle position at upper vertical level
-    REAL(wp) ::  diameter           !< diamter of droplet
-    REAL(wp) ::  diss_int_l         !< x/y-interpolated dissipation at particle position at lower vertical level
-    REAL(wp) ::  diss_int_u         !< x/y-interpolated dissipation at particle position at upper vertical level
-    REAL(wp) ::  dt_particle_m      !< previous particle time step
-    REAL(wp) ::  dz_temp            !< dummy for the vertical grid spacing
-    REAL(wp) ::  e_int_l            !< x/y-interpolated TKE at particle position at lower vertical level
-    REAL(wp) ::  e_int_u            !< x/y-interpolated TKE at particle position at upper vertical level
-    REAL(wp) ::  e_mean_int         !< horizontal mean TKE at particle height
-    REAL(wp) ::  exp_arg            !< argument in the exponent - particle radius
-    REAL(wp) ::  exp_term           !< exponent term
-    REAL(wp) ::  gg                 !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  height_p           !< dummy argument for logarithmic interpolation
-    REAL(wp) ::  log_z_z0_int       !< logarithmus used for surface_layer interpolation
-    REAL(wp) ::  random_gauss       !< Gaussian-distributed random number used for SGS particle advection
-    REAL(wp) ::  RL                 !< Lagrangian autocorrelation coefficient
-    REAL(wp) ::  rg1                !< Gaussian distributed random number
-    REAL(wp) ::  rg2                !< Gaussian distributed random number
-    REAL(wp) ::  rg3                !< Gaussian distributed random number
-    REAL(wp) ::  sigma              !< velocity standard deviation
-    REAL(wp) ::  u_int_l            !< x/y-interpolated u-component at particle position at lower vertical level
-    REAL(wp) ::  u_int_u            !< x/y-interpolated u-component at particle position at upper vertical level
-    REAL(wp) ::  us_int             !< friction velocity at particle grid box
-    REAL(wp) ::  usws_int           !< surface momentum flux (u component) at particle grid box
-    REAL(wp) ::  v_int_l            !< x/y-interpolated v-component at particle position at lower vertical level
-    REAL(wp) ::  v_int_u            !< x/y-interpolated v-component at particle position at upper vertical level
-    REAL(wp) ::  vsws_int           !< surface momentum flux (u component) at particle grid box
-    REAL(wp) ::  vv_int             !< dummy to compute interpolated mean SGS TKE, used to scale SGS advection 
-    REAL(wp) ::  w_int_l            !< x/y-interpolated w-component at particle position at lower vertical level
-    REAL(wp) ::  w_int_u            !< x/y-interpolated w-component at particle position at upper vertical level
-    REAL(wp) ::  w_s                !< terminal velocity of droplets
-    REAL(wp) ::  x                  !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  y                  !< dummy argument for horizontal particle interpolation 
-    REAL(wp) ::  z_p                !< surface layer height (0.5 dz)
-
-    REAL(wp), PARAMETER ::  a_rog = 9.65_wp      !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  b_rog = 10.43_wp     !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  c_rog = 0.6_wp       !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp   !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp  !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  d0_rog = 0.745_wp    !< separation diameter
-
-    REAL(wp), DIMENSION(number_of_particles) ::  term_1_2       !< flag to communicate whether a particle is near topography or not
-    REAL(wp), DIMENSION(number_of_particles) ::  dens_ratio     !< ratio between the density of the fluid and the density of the particles 
-    REAL(wp), DIMENSION(number_of_particles) ::  de_dx_int      !< horizontal TKE gradient along x at particle position
-    REAL(wp), DIMENSION(number_of_particles) ::  de_dy_int      !< horizontal TKE gradient along y at particle position
-    REAL(wp), DIMENSION(number_of_particles) ::  de_dz_int      !< horizontal TKE gradient along z at particle position
-    REAL(wp), DIMENSION(number_of_particles) ::  diss_int       !< dissipation at particle position
-    REAL(wp), DIMENSION(number_of_particles) ::  dt_gap         !< remaining time until particle time integration reaches LES time
-    REAL(wp), DIMENSION(number_of_particles) ::  dt_particle    !< particle time step
-    REAL(wp), DIMENSION(number_of_particles) ::  e_int          !< TKE at particle position
-    REAL(wp), DIMENSION(number_of_particles) ::  fs_int         !< weighting factor for subgrid-scale particle speed
-    REAL(wp), DIMENSION(number_of_particles) ::  lagr_timescale !< Lagrangian timescale
-    REAL(wp), DIMENSION(number_of_particles) ::  rvar1_temp     !< SGS particle velocity - u-component
-    REAL(wp), DIMENSION(number_of_particles) ::  rvar2_temp     !< SGS particle velocity - v-component
-    REAL(wp), DIMENSION(number_of_particles) ::  rvar3_temp     !< SGS particle velocity - w-component
-    REAL(wp), DIMENSION(number_of_particles) ::  u_int          !< u-component of particle speed
-    REAL(wp), DIMENSION(number_of_particles) ::  v_int          !< v-component of particle speed 
-    REAL(wp), DIMENSION(number_of_particles) ::  w_int          !< w-component of particle speed
-    REAL(wp), DIMENSION(number_of_particles) ::  xv             !< x-position
-    REAL(wp), DIMENSION(number_of_particles) ::  yv             !< y-position
-    REAL(wp), DIMENSION(number_of_particles) ::  zv             !< z-position
-
-    REAL(wp), DIMENSION(number_of_particles, 3) ::  rg !< vector of Gaussian distributed random numbers 
-
-    CALL cpu_log( log_point_s(44), 'lpm_advec', 'continue' )
-
-!
-!-- Determine height of Prandtl layer and distance between Prandtl-layer
-!-- height and horizontal mean roughness height, which are required for 
-!-- vertical logarithmic interpolation of horizontal particle speeds 
-!-- (for particles below first vertical grid level).
-    z_p      = zu(nzb+1) - zw(nzb)
-    d_z_p_z0 = 1.0_wp / ( z_p - z0_av_global )
-
-    start_index = grid_particles(kp,jp,ip)%start_index
-    end_index   = grid_particles(kp,jp,ip)%end_index
-
-    xv = particles(1:number_of_particles)%x
-    yv = particles(1:number_of_particles)%y
-    zv = particles(1:number_of_particles)%z
-
-    DO  nb = 0, 7
-!
-!--    Interpolate u velocity-component       
-       i = ip
-       j = jp + block_offset(nb)%j_off
-       k = kp + block_offset(nb)%k_off
-
-       DO  n = start_index(nb), end_index(nb)
-!
-!--       Interpolation of the u velocity component onto particle position.  
-!--       Particles are interpolation bi-linearly in the horizontal and a 
-!--       linearly in the vertical. An exception is made for particles below
-!--       the first vertical grid level in case of a prandtl layer. In this 
-!--       case the horizontal particle velocity components are determined using
-!--       Monin-Obukhov relations (if branch). 
-!--       First, check if particle is located below first vertical grid level 
-!--       above topography (Prandtl-layer height)
-!--       Determine vertical index of topography top
-          k_wall = get_topography_top_index_ji( jp, ip, 's' )
-
-          IF ( constant_flux_layer  .AND.  zv(n) - zw(k_wall) < z_p )  THEN
-!
-!--          Resolved-scale horizontal particle velocity is zero below z0. 
-             IF ( zv(n) - zw(k_wall) < z0_av_global )  THEN
-                u_int(n) = 0.0_wp
-             ELSE
-!
-!--             Determine the sublayer. Further used as index.
-                height_p = ( zv(n) - zw(k_wall) - z0_av_global ) &
-                                     * REAL( number_of_sublayers, KIND=wp )    &
-                                     * d_z_p_z0 
-!
-!--             Calculate LOG(z/z0) for exact particle height. Therefore,   
-!--             interpolate linearly between precalculated logarithm. 
-                log_z_z0_int = log_z_z0(INT(height_p))                         &
-                                 + ( height_p - INT(height_p) )                &
-                                 * ( log_z_z0(INT(height_p)+1)                 &
-                                      - log_z_z0(INT(height_p))                &
-                                   ) 
-!
-!--             Get friction velocity and momentum flux from new surface data 
-!--             types.
-                IF ( surf_def_h(0)%start_index(jp,ip) <=                   &
-                     surf_def_h(0)%end_index(jp,ip) )  THEN
-                   surf_start = surf_def_h(0)%start_index(jp,ip)
-!--                Limit friction velocity. In narrow canyons or holes the 
-!--                friction velocity can become very small, resulting in a too
-!--                large particle speed.
-                   us_int    = MAX( surf_def_h(0)%us(surf_start), 0.01_wp )  
-                   usws_int  = surf_def_h(0)%usws(surf_start)
-                ELSEIF ( surf_lsm_h%start_index(jp,ip) <=                  &
-                         surf_lsm_h%end_index(jp,ip) )  THEN
-                   surf_start = surf_lsm_h%start_index(jp,ip)
-                   us_int    = MAX( surf_lsm_h%us(surf_start), 0.01_wp )  
-                   usws_int  = surf_lsm_h%usws(surf_start)
-                ELSEIF ( surf_usm_h%start_index(jp,ip) <=                  &
-                         surf_usm_h%end_index(jp,ip) )  THEN
-                   surf_start = surf_usm_h%start_index(jp,ip)
-                   us_int    = MAX( surf_usm_h%us(surf_start), 0.01_wp )  
-                   usws_int  = surf_usm_h%usws(surf_start)
-                ENDIF
-
-!
-!--             Neutral solution is applied for all situations, e.g. also for 
-!--             unstable and stable situations. Even though this is not exact 
-!--             this saves a lot of CPU time since several calls of intrinsic 
-!--             FORTRAN procedures (LOG, ATAN) are avoided, This is justified 
-!--             as sensitivity studies revealed no significant effect of 
-!--             using the neutral solution also for un/stable situations.
-                u_int(n) = -usws_int / ( us_int * kappa + 1E-10_wp )           & 
-                            * log_z_z0_int - u_gtrans
-                
-             ENDIF
-!
-!--       Particle above the first grid level. Bi-linear interpolation in the 
-!--       horizontal and linear interpolation in the vertical direction.
-          ELSE
-
-             x  = xv(n) - i * dx
-             y  = yv(n) + ( 0.5_wp - j ) * dy
-             aa = x**2          + y**2
-             bb = ( dx - x )**2 + y**2
-             cc = x**2          + ( dy - y )**2
-             dd = ( dx - x )**2 + ( dy - y )**2
-             gg = aa + bb + cc + dd
-
-             u_int_l = ( ( gg - aa ) * u(k,j,i)   + ( gg - bb ) * u(k,j,i+1)   &
-                         + ( gg - cc ) * u(k,j+1,i) + ( gg - dd ) *            &
-                         u(k,j+1,i+1) ) / ( 3.0_wp * gg ) - u_gtrans
-
-             IF ( k == nzt )  THEN
-                u_int(n) = u_int_l
-             ELSE
-                u_int_u = ( ( gg-aa ) * u(k+1,j,i) + ( gg-bb ) * u(k+1,j,i+1)  &
-                            + ( gg-cc ) * u(k+1,j+1,i) + ( gg-dd ) *           &
-                            u(k+1,j+1,i+1) ) / ( 3.0_wp * gg ) - u_gtrans
-                u_int(n) = u_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *               &
-                           ( u_int_u - u_int_l )
-             ENDIF
-
-          ENDIF
-
-       ENDDO
-!
-!--    Same procedure for interpolation of the v velocity-component 
-       i = ip + block_offset(nb)%i_off
-       j = jp
-       k = kp + block_offset(nb)%k_off
-
-       DO  n = start_index(nb), end_index(nb)
-
-!
-!--       Determine vertical index of topography top
-          k_wall = get_topography_top_index_ji( jp,ip, 's' )
-
-          IF ( constant_flux_layer  .AND.  zv(n) - zw(k_wall) < z_p )  THEN
-             IF ( zv(n) - zw(k_wall) < z0_av_global )  THEN
-!
-!--             Resolved-scale horizontal particle velocity is zero below z0. 
-                v_int(n) = 0.0_wp
-             ELSE       
-!
-!--             Determine the sublayer. Further used as index. Please note, 
-!--             logarithmus can not be reused from above, as in in case of
-!--             topography particle on u-grid can be above surface-layer height,
-!--             whereas it can be below on v-grid. 
-                height_p = ( zv(n) - zw(k_wall) - z0_av_global ) &
-                                  * REAL( number_of_sublayers, KIND=wp )       &
-                                  * d_z_p_z0 
-!
-!--             Calculate LOG(z/z0) for exact particle height. Therefore,   
-!--             interpolate linearly between precalculated logarithm. 
-                log_z_z0_int = log_z_z0(INT(height_p))                         &
-                                 + ( height_p - INT(height_p) )                &
-                                 * ( log_z_z0(INT(height_p)+1)                 &
-                                      - log_z_z0(INT(height_p))                &
-                                   ) 
-!
-!--             Get friction velocity and momentum flux from new surface data 
-!--             types.
-                IF ( surf_def_h(0)%start_index(jp,ip) <=                   &
-                     surf_def_h(0)%end_index(jp,ip) )  THEN
-                   surf_start = surf_def_h(0)%start_index(jp,ip)
-!--                Limit friction velocity. In narrow canyons or holes the 
-!--                friction velocity can become very small, resulting in a too
-!--                large particle speed.
-                   us_int    = MAX( surf_def_h(0)%us(surf_start), 0.01_wp )  
-                   vsws_int  = surf_def_h(0)%vsws(surf_start)
-                ELSEIF ( surf_lsm_h%start_index(jp,ip) <=                  &
-                         surf_lsm_h%end_index(jp,ip) )  THEN
-                   surf_start = surf_lsm_h%start_index(jp,ip)
-                   us_int    = MAX( surf_lsm_h%us(surf_start), 0.01_wp )  
-                   vsws_int  = surf_lsm_h%vsws(surf_start)
-                ELSEIF ( surf_usm_h%start_index(jp,ip) <=                  &
-                         surf_usm_h%end_index(jp,ip) )  THEN
-                   surf_start = surf_usm_h%start_index(jp,ip)
-                   us_int    = MAX( surf_usm_h%us(surf_start), 0.01_wp )  
-                   vsws_int  = surf_usm_h%vsws(surf_start)
-                ENDIF 
-!
-!--             Neutral solution is applied for all situations, e.g. also for 
-!--             unstable and stable situations. Even though this is not exact 
-!--             this saves a lot of CPU time since several calls of intrinsic 
-!--             FORTRAN procedures (LOG, ATAN) are avoided, This is justified 
-!--             as sensitivity studies revealed no significant effect of 
-!--             using the neutral solution also for un/stable situations.
-                v_int(n) = -vsws_int / ( us_int * kappa + 1E-10_wp )           &
-                         * log_z_z0_int - v_gtrans
-
-             ENDIF
-
-          ELSE
-             x  = xv(n) + ( 0.5_wp - i ) * dx
-             y  = yv(n) - j * dy
-             aa = x**2          + y**2
-             bb = ( dx - x )**2 + y**2
-             cc = x**2          + ( dy - y )**2
-             dd = ( dx - x )**2 + ( dy - y )**2
-             gg = aa + bb + cc + dd
-
-             v_int_l = ( ( gg - aa ) * v(k,j,i)   + ( gg - bb ) * v(k,j,i+1)   &
-                       + ( gg - cc ) * v(k,j+1,i) + ( gg - dd ) * v(k,j+1,i+1) &
-                       ) / ( 3.0_wp * gg ) - v_gtrans
-
-             IF ( k == nzt )  THEN
-                v_int(n) = v_int_l
-             ELSE
-                v_int_u = ( ( gg-aa ) * v(k+1,j,i)   + ( gg-bb ) * v(k+1,j,i+1)   &
-                          + ( gg-cc ) * v(k+1,j+1,i) + ( gg-dd ) * v(k+1,j+1,i+1) &
-                          ) / ( 3.0_wp * gg ) - v_gtrans
-                v_int(n) = v_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *               &
-                                  ( v_int_u - v_int_l )
-             ENDIF
-
-          ENDIF
-
-       ENDDO
-!
-!--    Same procedure for interpolation of the w velocity-component
-       i = ip + block_offset(nb)%i_off
-       j = jp + block_offset(nb)%j_off
-       k = kp - 1
-
-       DO  n = start_index(nb), end_index(nb)
-
-          IF ( vertical_particle_advection(particles(n)%group) )  THEN
-
-             x  = xv(n) + ( 0.5_wp - i ) * dx
-             y  = yv(n) + ( 0.5_wp - j ) * dy
-             aa = x**2          + y**2
-             bb = ( dx - x )**2 + y**2
-             cc = x**2          + ( dy - y )**2
-             dd = ( dx - x )**2 + ( dy - y )**2
-             gg = aa + bb + cc + dd
-
-             w_int_l = ( ( gg - aa ) * w(k,j,i)   + ( gg - bb ) * w(k,j,i+1)   &
-                       + ( gg - cc ) * w(k,j+1,i) + ( gg - dd ) * w(k,j+1,i+1) &
-                       ) / ( 3.0_wp * gg )
-
-             IF ( k == nzt )  THEN
-                w_int(n) = w_int_l
-             ELSE
-                w_int_u = ( ( gg-aa ) * w(k+1,j,i)   + &
-                            ( gg-bb ) * w(k+1,j,i+1) + &
-                            ( gg-cc ) * w(k+1,j+1,i) + &
-                            ( gg-dd ) * w(k+1,j+1,i+1) &
-                          ) / ( 3.0_wp * gg )
-                w_int(n) = w_int_l + ( zv(n) - zw(k) ) / dzw(k+1) *               &
-                           ( w_int_u - w_int_l )
-             ENDIF
-
-          ELSE
-
-             w_int(n) = 0.0_wp
-
-          ENDIF
-
-       ENDDO
-
-    ENDDO
-
-!-- Interpolate and calculate quantities needed for calculating the SGS
-!-- velocities
-    IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
-    
-       DO  nb = 0,7
-         
-          subbox_at_wall = .FALSE.         
-!
-!--       In case of topography check if subbox is adjacent to a wall
-          IF ( .NOT. topography == 'flat' ) THEN
-             i = ip + MERGE( -1_iwp , 1_iwp, BTEST( nb, 2 ) )
-             j = jp + MERGE( -1_iwp , 1_iwp, BTEST( nb, 1 ) )
-             k = kp + MERGE( -1_iwp , 1_iwp, BTEST( nb, 0 ) )
-             IF ( .NOT. BTEST(wall_flags_0(k,  jp, ip), 0) .OR.                &
-                  .NOT. BTEST(wall_flags_0(kp, j,  ip), 0) .OR.                &
-                  .NOT. BTEST(wall_flags_0(kp, jp, i ), 0) )                   &
-             THEN
-                subbox_at_wall = .TRUE.
-             ENDIF
-          ENDIF
-          IF ( subbox_at_wall ) THEN
-             e_int(start_index(nb):end_index(nb))     = e(kp,jp,ip) 
-             diss_int(start_index(nb):end_index(nb))  = diss(kp,jp,ip)
-             de_dx_int(start_index(nb):end_index(nb)) = de_dx(kp,jp,ip)
-             de_dy_int(start_index(nb):end_index(nb)) = de_dy(kp,jp,ip)
-             de_dz_int(start_index(nb):end_index(nb)) = de_dz(kp,jp,ip)
-!
-!--          Set flag for stochastic equation.
-             term_1_2(start_index(nb):end_index(nb)) = 0.0_wp             
-          ELSE
-             i = ip + block_offset(nb)%i_off
-             j = jp + block_offset(nb)%j_off
-             k = kp + block_offset(nb)%k_off
-
-             DO  n = start_index(nb), end_index(nb)
-!
-!--             Interpolate TKE
-                x  = xv(n) + ( 0.5_wp - i ) * dx
-                y  = yv(n) + ( 0.5_wp - j ) * dy
-                aa = x**2          + y**2
-                bb = ( dx - x )**2 + y**2
-                cc = x**2          + ( dy - y )**2
-                dd = ( dx - x )**2 + ( dy - y )**2
-                gg = aa + bb + cc + dd
-
-                e_int_l = ( ( gg-aa ) * e(k,j,i)   + ( gg-bb ) * e(k,j,i+1)   &
-                          + ( gg-cc ) * e(k,j+1,i) + ( gg-dd ) * e(k,j+1,i+1) &
-                          ) / ( 3.0_wp * gg )
-
-                IF ( k+1 == nzt+1 )  THEN
-                   e_int(n) = e_int_l
-                ELSE
-                   e_int_u = ( ( gg - aa ) * e(k+1,j,i)   + &
-                               ( gg - bb ) * e(k+1,j,i+1) + &
-                               ( gg - cc ) * e(k+1,j+1,i) + &
-                               ( gg - dd ) * e(k+1,j+1,i+1) &
-                            ) / ( 3.0_wp * gg )
-                   e_int(n) = e_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *            &
-                                     ( e_int_u - e_int_l )
-                ENDIF
-!
-!--             Needed to avoid NaN particle velocities (this might not be
-!--             required any more)
-                IF ( e_int(n) <= 0.0_wp )  THEN
-                   e_int(n) = 1.0E-20_wp
-                ENDIF
-!
-!--             Interpolate the TKE gradient along x (adopt incides i,j,k and
-!--             all position variables from above (TKE))
-                de_dx_int_l = ( ( gg - aa ) * de_dx(k,j,i)   + &
-                                ( gg - bb ) * de_dx(k,j,i+1) + &
-                                ( gg - cc ) * de_dx(k,j+1,i) + &
-                                ( gg - dd ) * de_dx(k,j+1,i+1) &
-                               ) / ( 3.0_wp * gg )
-
-                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
-                   de_dx_int(n) = de_dx_int_l
-                ELSE
-                   de_dx_int_u = ( ( gg - aa ) * de_dx(k+1,j,i)   + &
-                                   ( gg - bb ) * de_dx(k+1,j,i+1) + &
-                                   ( gg - cc ) * de_dx(k+1,j+1,i) + &
-                                   ( gg - dd ) * de_dx(k+1,j+1,i+1) &
-                                  ) / ( 3.0_wp * gg )
-                   de_dx_int(n) = de_dx_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *    &
-                                              ( de_dx_int_u - de_dx_int_l )
-                ENDIF
-!
-!--             Interpolate the TKE gradient along y
-                de_dy_int_l = ( ( gg - aa ) * de_dy(k,j,i)   + &
-                                ( gg - bb ) * de_dy(k,j,i+1) + &
-                                ( gg - cc ) * de_dy(k,j+1,i) + &
-                                ( gg - dd ) * de_dy(k,j+1,i+1) &
-                               ) / ( 3.0_wp * gg )
-                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
-                   de_dy_int(n) = de_dy_int_l
-                ELSE
-                   de_dy_int_u = ( ( gg - aa ) * de_dy(k+1,j,i)   + &
-                                   ( gg - bb ) * de_dy(k+1,j,i+1) + &
-                                   ( gg - cc ) * de_dy(k+1,j+1,i) + &
-                                   ( gg - dd ) * de_dy(k+1,j+1,i+1) &
-                                  ) / ( 3.0_wp * gg )
-                      de_dy_int(n) = de_dy_int_l + ( zv(n) - zu(k) ) / dzw(k+1) * &
-                                                 ( de_dy_int_u - de_dy_int_l )
-                ENDIF
-
-!
-!--             Interpolate the TKE gradient along z
-                IF ( zv(n) < 0.5_wp * dz(1) )  THEN
-                   de_dz_int(n) = 0.0_wp
-                ELSE
-                   de_dz_int_l = ( ( gg - aa ) * de_dz(k,j,i)   + &
-                                   ( gg - bb ) * de_dz(k,j,i+1) + &
-                                   ( gg - cc ) * de_dz(k,j+1,i) + &
-                                   ( gg - dd ) * de_dz(k,j+1,i+1) &
-                                  ) / ( 3.0_wp * gg )
-
-                   IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
-                      de_dz_int(n) = de_dz_int_l
-                   ELSE
-                      de_dz_int_u = ( ( gg - aa ) * de_dz(k+1,j,i)   + &
-                                      ( gg - bb ) * de_dz(k+1,j,i+1) + &
-                                      ( gg - cc ) * de_dz(k+1,j+1,i) + &
-                                      ( gg - dd ) * de_dz(k+1,j+1,i+1) &
-                                     ) / ( 3.0_wp * gg )
-                      de_dz_int(n) = de_dz_int_l + ( zv(n) - zu(k) ) / dzw(k+1) * &
-                                                 ( de_dz_int_u - de_dz_int_l )
-                   ENDIF
-                ENDIF
-
-!
-!--             Interpolate the dissipation of TKE
-                diss_int_l = ( ( gg - aa ) * diss(k,j,i)   + &
-                               ( gg - bb ) * diss(k,j,i+1) + &
-                               ( gg - cc ) * diss(k,j+1,i) + &
-                               ( gg - dd ) * diss(k,j+1,i+1) &
-                               ) / ( 3.0_wp * gg )
-
-                IF ( k == nzt )  THEN
-                   diss_int(n) = diss_int_l
-                ELSE
-                   diss_int_u = ( ( gg - aa ) * diss(k+1,j,i)   + &
-                                  ( gg - bb ) * diss(k+1,j,i+1) + &
-                                  ( gg - cc ) * diss(k+1,j+1,i) + &
-                                  ( gg - dd ) * diss(k+1,j+1,i+1) &
-                                 ) / ( 3.0_wp * gg )
-                   diss_int(n) = diss_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *      &
-                                            ( diss_int_u - diss_int_l )
-                ENDIF
-
-!
-!--             Set flag for stochastic equation.
-                term_1_2(n) = 1.0_wp
-             ENDDO
-          ENDIF
-       ENDDO
-
-       DO nb = 0,7
-          i = ip + block_offset(nb)%i_off
-          j = jp + block_offset(nb)%j_off
-          k = kp + block_offset(nb)%k_off
-
-          DO  n = start_index(nb), end_index(nb)
-!
-!--          Vertical interpolation of the horizontally averaged SGS TKE and
-!--          resolved-scale velocity variances and use the interpolated values
-!--          to calculate the coefficient fs, which is a measure of the ratio
-!--          of the subgrid-scale turbulent kinetic energy to the total amount
-!--          of turbulent kinetic energy.
-             IF ( k == 0 )  THEN
-                e_mean_int = hom(0,1,8,0)
-             ELSE
-                e_mean_int = hom(k,1,8,0) +                                    &
-                                           ( hom(k+1,1,8,0) - hom(k,1,8,0) ) / &
-                                           ( zu(k+1) - zu(k) ) *               &
-                                           ( zv(n) - zu(k) )
-             ENDIF
-
-             kw = kp - 1
-
-             IF ( k == 0 )  THEN
-                aa  = hom(k+1,1,30,0)  * ( zv(n) / &
-                                         ( 0.5_wp * ( zu(k+1) - zu(k) ) ) )
-                bb  = hom(k+1,1,31,0)  * ( zv(n) / &
-                                         ( 0.5_wp * ( zu(k+1) - zu(k) ) ) )
-                cc  = hom(kw+1,1,32,0) * ( zv(n) / &
-                                         ( 1.0_wp * ( zw(kw+1) - zw(kw) ) ) )
-             ELSE
-                aa  = hom(k,1,30,0) + ( hom(k+1,1,30,0) - hom(k,1,30,0) ) *    &
-                           ( ( zv(n) - zu(k) ) / ( zu(k+1) - zu(k) ) )
-                bb  = hom(k,1,31,0) + ( hom(k+1,1,31,0) - hom(k,1,31,0) ) *    &
-                           ( ( zv(n) - zu(k) ) / ( zu(k+1) - zu(k) ) )
-                cc  = hom(kw,1,32,0) + ( hom(kw+1,1,32,0)-hom(kw,1,32,0) ) *   &
-                           ( ( zv(n) - zw(kw) ) / ( zw(kw+1)-zw(kw) ) )
-             ENDIF
-
-             vv_int = ( 1.0_wp / 3.0_wp ) * ( aa + bb + cc )
-!
-!--          Needed to avoid NaN particle velocities. The value of 1.0 is just
-!--          an educated guess for the given case.
-             IF ( vv_int + ( 2.0_wp / 3.0_wp ) * e_mean_int == 0.0_wp )  THEN
-                fs_int(n) = 1.0_wp
-             ELSE
-                fs_int(n) = ( 2.0_wp / 3.0_wp ) * e_mean_int /                 &
-                            ( vv_int + ( 2.0_wp / 3.0_wp ) * e_mean_int )
-             ENDIF
-
-          ENDDO
-       ENDDO
-
-       DO  nb = 0, 7
-          DO  n = start_index(nb), end_index(nb)
-             rg(n,1) = random_gauss( iran_part, 5.0_wp )
-             rg(n,2) = random_gauss( iran_part, 5.0_wp )
-             rg(n,3) = random_gauss( iran_part, 5.0_wp )
-          ENDDO
-       ENDDO
-
-       DO  nb = 0, 7
-          DO  n = start_index(nb), end_index(nb)
-
-!
-!--          Calculate the Lagrangian timescale according to Weil et al. (2004).
-             lagr_timescale(n) = ( 4.0_wp * e_int(n) + 1E-20_wp ) / &
-                              ( 3.0_wp * fs_int(n) * c_0 * diss_int(n) + 1E-20_wp )
-
-!
-!--          Calculate the next particle timestep. dt_gap is the time needed to
-!--          complete the current LES timestep.
-             dt_gap(n) = dt_3d - particles(n)%dt_sum
-             dt_particle(n) = MIN( dt_3d, 0.025_wp * lagr_timescale(n), dt_gap(n) )
-             particles(n)%aux1 = lagr_timescale(n)
-             particles(n)%aux2 = dt_gap(n)
-!
-!--          The particle timestep should not be too small in order to prevent
-!--          the number of particle timesteps of getting too large
-             IF ( dt_particle(n) < dt_min_part  .AND.  dt_min_part < dt_gap(n) )  THEN
-                dt_particle(n) = dt_min_part
-             ENDIF
-             rvar1_temp(n) = particles(n)%rvar1
-             rvar2_temp(n) = particles(n)%rvar2
-             rvar3_temp(n) = particles(n)%rvar3
-!
-!--          Calculate the SGS velocity components
-             IF ( particles(n)%age == 0.0_wp )  THEN
-!
-!--             For new particles the SGS components are derived from the SGS
-!--             TKE. Limit the Gaussian random number to the interval
-!--             [-5.0*sigma, 5.0*sigma] in order to prevent the SGS velocities
-!--             from becoming unrealistically large.
-                rvar1_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
-                                          + 1E-20_wp ) * ( rg(n,1) - 1.0_wp )
-                rvar2_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
-                                          + 1E-20_wp ) * ( rg(n,2) - 1.0_wp )
-                rvar3_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
-                                          + 1E-20_wp ) * ( rg(n,3) - 1.0_wp )
-
-             ELSE
-!
-!--             Restriction of the size of the new timestep: compared to the 
-!--             previous timestep the increase must not exceed 200%. First,
-!--             check if age > age_m, in order to prevent that particles get zero
-!--             timestep.
-                dt_particle_m = MERGE( dt_particle(n),                         &
-                                       particles(n)%age - particles(n)%age_m,  &
-                                       particles(n)%age - particles(n)%age_m < &
-                                       1E-8_wp )
-                IF ( dt_particle(n) > 2.0_wp * dt_particle_m )  THEN
-                   dt_particle(n) = 2.0_wp * dt_particle_m
-                ENDIF
-
-!--             For old particles the SGS components are correlated with the
-!--             values from the previous timestep. Random numbers have also to
-!--             be limited (see above).
-!--             As negative values for the subgrid TKE are not allowed, the
-!--             change of the subgrid TKE with time cannot be smaller than
-!--             -e_int(n)/dt_particle. This value is used as a lower boundary
-!--             value for the change of TKE 
-                de_dt_min = - e_int(n) / dt_particle(n)
-
-                de_dt = ( e_int(n) - particles(n)%e_m ) / dt_particle_m
-
-                IF ( de_dt < de_dt_min )  THEN
-                   de_dt = de_dt_min
-                ENDIF
-
-                CALL weil_stochastic_eq(rvar1_temp(n), fs_int(n), e_int(n),& 
-                                        de_dx_int(n), de_dt, diss_int(n),       &
-                                        dt_particle(n), rg(n,1), term_1_2(n) )
-
-                CALL weil_stochastic_eq(rvar2_temp(n), fs_int(n), e_int(n),& 
-                                        de_dy_int(n), de_dt, diss_int(n),       &
-                                        dt_particle(n), rg(n,2), term_1_2(n) )
-
-                CALL weil_stochastic_eq(rvar3_temp(n), fs_int(n), e_int(n),& 
-                                        de_dz_int(n), de_dt, diss_int(n),       &
-                                        dt_particle(n), rg(n,3), term_1_2(n) )
-
-             ENDIF
-
-          ENDDO
-       ENDDO
-!
-!--    Check if the added SGS velocities result in a violation of the CFL-
-!--    criterion. If yes choose a smaller timestep based on the new velocities
-!--    and calculate SGS velocities again
-       dz_temp = zw(kp)-zw(kp-1)
-       
-       DO  nb = 0, 7
-          DO  n = start_index(nb), end_index(nb)
-             IF ( .NOT. particles(n)%age == 0.0_wp .AND.                       &
-                (ABS( u_int(n) + rvar1_temp(n) ) > (dx/dt_particle(n))  .OR.   &
-                 ABS( v_int(n) + rvar2_temp(n) ) > (dy/dt_particle(n))  .OR.   &
-                 ABS( w_int(n) + rvar3_temp(n) ) > (dz_temp/dt_particle(n)))) THEN
-                
-                dt_particle(n) = 0.9_wp * MIN(                                 &
-                                 ( dx / ABS( u_int(n) + rvar1_temp(n) ) ),     &
-                                 ( dy / ABS( v_int(n) + rvar2_temp(n) ) ),     &
-                                 ( dz_temp / ABS( w_int(n) + rvar3_temp(n) ) ) )
-
-!
-!--             Reset temporary SGS velocites to "current" ones
-                rvar1_temp(n) = particles(n)%rvar1
-                rvar2_temp(n) = particles(n)%rvar2
-                rvar3_temp(n) = particles(n)%rvar3
-                
-                de_dt_min = - e_int(n) / dt_particle(n)
-
-                de_dt = ( e_int(n) - particles(n)%e_m ) / dt_particle_m
-
-                IF ( de_dt < de_dt_min )  THEN
-                   de_dt = de_dt_min
-                ENDIF
-
-                CALL weil_stochastic_eq(rvar1_temp(n), fs_int(n), e_int(n),& 
-                                        de_dx_int(n), de_dt, diss_int(n),       &
-                                        dt_particle(n), rg(n,1), term_1_2(n) )
-
-                CALL weil_stochastic_eq(rvar2_temp(n), fs_int(n), e_int(n),& 
-                                        de_dy_int(n), de_dt, diss_int(n),       &
-                                        dt_particle(n), rg(n,2), term_1_2(n) )
-
-                CALL weil_stochastic_eq(rvar3_temp(n), fs_int(n), e_int(n),& 
-                                        de_dz_int(n), de_dt, diss_int(n),       &
-                                        dt_particle(n), rg(n,3), term_1_2(n) )
-             ENDIF                           
-             
-!
-!--          Update particle velocites 
-             particles(n)%rvar1 = rvar1_temp(n)
-             particles(n)%rvar2 = rvar2_temp(n)
-             particles(n)%rvar3 = rvar3_temp(n)
-             u_int(n) = u_int(n) + particles(n)%rvar1
-             v_int(n) = v_int(n) + particles(n)%rvar2
-             w_int(n) = w_int(n) + particles(n)%rvar3
-!
-!--          Store the SGS TKE of the current timelevel which is needed for
-!--          for calculating the SGS particle velocities at the next timestep
-             particles(n)%e_m = e_int(n)
-          ENDDO
-       ENDDO
-       
-    ELSE
-!
-!--    If no SGS velocities are used, only the particle timestep has to
-!--    be set
-       dt_particle = dt_3d
-
-    ENDIF
-
-    dens_ratio = particle_groups(particles(1:number_of_particles)%group)%density_ratio
-
-    IF ( ANY( dens_ratio == 0.0_wp ) )  THEN
-       DO  nb = 0, 7
-          DO  n = start_index(nb), end_index(nb)
-
-!
-!--          Particle advection
-             IF ( dens_ratio(n) == 0.0_wp )  THEN
-!
-!--             Pure passive transport (without particle inertia)
-                particles(n)%x = xv(n) + u_int(n) * dt_particle(n)
-                particles(n)%y = yv(n) + v_int(n) * dt_particle(n)
-                particles(n)%z = zv(n) + w_int(n) * dt_particle(n)
-
-                particles(n)%speed_x = u_int(n)
-                particles(n)%speed_y = v_int(n)
-                particles(n)%speed_z = w_int(n)
-
-             ELSE
-!
-!--             Transport of particles with inertia
-                particles(n)%x = particles(n)%x + particles(n)%speed_x * &
-                                                  dt_particle(n)
-                particles(n)%y = particles(n)%y + particles(n)%speed_y * &
-                                                  dt_particle(n)
-                particles(n)%z = particles(n)%z + particles(n)%speed_z * &
-                                                  dt_particle(n)
-
-!
-!--             Update of the particle velocity
-                IF ( cloud_droplets )  THEN
-!
-!--                Terminal velocity is computed for vertical direction (Rogers et 
-!--                al., 1993, J. Appl. Meteorol.)
-                   diameter = particles(n)%radius * 2000.0_wp !diameter in mm
-                   IF ( diameter <= d0_rog )  THEN
-                      w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
-                   ELSE
-                      w_s = a_rog - b_rog * EXP( -c_rog * diameter )
-                   ENDIF
-
-!
-!--                If selected, add random velocities following Soelch and Kaercher
-!--                (2010, Q. J. R. Meteorol. Soc.)
-                   IF ( use_sgs_for_particles )  THEN
-                      lagr_timescale(n) = km(kp,jp,ip) / MAX( e(kp,jp,ip), 1.0E-20_wp )
-                      RL             = EXP( -1.0_wp * dt_3d / MAX( lagr_timescale(n), &
-                                             1.0E-20_wp ) )
-                      sigma          = SQRT( e(kp,jp,ip) )
-
-                      rg1 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
-                      rg2 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
-                      rg3 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
-
-                      particles(n)%rvar1 = RL * particles(n)%rvar1 +              &
-                                           SQRT( 1.0_wp - RL**2 ) * sigma * rg1
-                      particles(n)%rvar2 = RL * particles(n)%rvar2 +              &
-                                           SQRT( 1.0_wp - RL**2 ) * sigma * rg2
-                      particles(n)%rvar3 = RL * particles(n)%rvar3 +              &
-                                           SQRT( 1.0_wp - RL**2 ) * sigma * rg3
-
-                      particles(n)%speed_x = u_int(n) + particles(n)%rvar1
-                      particles(n)%speed_y = v_int(n) + particles(n)%rvar2
-                      particles(n)%speed_z = w_int(n) + particles(n)%rvar3 - w_s
-                   ELSE
-                      particles(n)%speed_x = u_int(n)
-                      particles(n)%speed_y = v_int(n)
-                      particles(n)%speed_z = w_int(n) - w_s
-                   ENDIF
-
-                ELSE
-
-                   IF ( use_sgs_for_particles )  THEN
-                      exp_arg  = particle_groups(particles(n)%group)%exp_arg
-                      exp_term = EXP( -exp_arg * dt_particle(n) )
-                   ELSE
-                      exp_arg  = particle_groups(particles(n)%group)%exp_arg
-                      exp_term = particle_groups(particles(n)%group)%exp_term
-                   ENDIF
-                   particles(n)%speed_x = particles(n)%speed_x * exp_term +         &
-                                          u_int(n) * ( 1.0_wp - exp_term )
-                   particles(n)%speed_y = particles(n)%speed_y * exp_term +         &
-                                          v_int(n) * ( 1.0_wp - exp_term )
-                   particles(n)%speed_z = particles(n)%speed_z * exp_term +         &
-                                          ( w_int(n) - ( 1.0_wp - dens_ratio(n) ) * &
-                                          g / exp_arg ) * ( 1.0_wp - exp_term )
-                ENDIF
-
-             ENDIF
-          ENDDO
-       ENDDO
-    
-    ELSE
-
-       DO  nb = 0, 7
-          DO  n = start_index(nb), end_index(nb)
-!
-!--          Transport of particles with inertia
-             particles(n)%x = xv(n) + particles(n)%speed_x * dt_particle(n)
-             particles(n)%y = yv(n) + particles(n)%speed_y * dt_particle(n)
-             particles(n)%z = zv(n) + particles(n)%speed_z * dt_particle(n)
-!
-!--          Update of the particle velocity
-             IF ( cloud_droplets )  THEN
-!
-!--             Terminal velocity is computed for vertical direction (Rogers et al., 
-!--             1993, J. Appl. Meteorol.)
-                diameter = particles(n)%radius * 2000.0_wp !diameter in mm
-                IF ( diameter <= d0_rog )  THEN
-                   w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
-                ELSE
-                   w_s = a_rog - b_rog * EXP( -c_rog * diameter )
-                ENDIF
-
-!
-!--             If selected, add random velocities following Soelch and Kaercher
-!--             (2010, Q. J. R. Meteorol. Soc.)
-                IF ( use_sgs_for_particles )  THEN
-                    lagr_timescale(n) = km(kp,jp,ip) / MAX( e(kp,jp,ip), 1.0E-20_wp )
-                     RL             = EXP( -1.0_wp * dt_3d / MAX( lagr_timescale(n), &
-                                             1.0E-20_wp ) )
-                    sigma          = SQRT( e(kp,jp,ip) )
-
-                    rg1 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
-                    rg2 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
-                    rg3 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
-
-                    particles(n)%rvar1 = RL * particles(n)%rvar1 +                &
-                                         SQRT( 1.0_wp - RL**2 ) * sigma * rg1
-                    particles(n)%rvar2 = RL * particles(n)%rvar2 +                &
-                                         SQRT( 1.0_wp - RL**2 ) * sigma * rg2
-                    particles(n)%rvar3 = RL * particles(n)%rvar3 +                &
-                                         SQRT( 1.0_wp - RL**2 ) * sigma * rg3
-
-                    particles(n)%speed_x = u_int(n) + particles(n)%rvar1
-                    particles(n)%speed_y = v_int(n) + particles(n)%rvar2
-                    particles(n)%speed_z = w_int(n) + particles(n)%rvar3 - w_s
-                ELSE
-                    particles(n)%speed_x = u_int(n)
-                    particles(n)%speed_y = v_int(n)
-                    particles(n)%speed_z = w_int(n) - w_s
-                ENDIF
-
-             ELSE
-
-                IF ( use_sgs_for_particles )  THEN
-                   exp_arg  = particle_groups(particles(n)%group)%exp_arg
-                   exp_term = EXP( -exp_arg * dt_particle(n) )
-                ELSE
-                   exp_arg  = particle_groups(particles(n)%group)%exp_arg
-                   exp_term = particle_groups(particles(n)%group)%exp_term
-                ENDIF
-                particles(n)%speed_x = particles(n)%speed_x * exp_term +             &
-                                       u_int(n) * ( 1.0_wp - exp_term )
-                particles(n)%speed_y = particles(n)%speed_y * exp_term +             &
-                                       v_int(n) * ( 1.0_wp - exp_term )
-                particles(n)%speed_z = particles(n)%speed_z * exp_term +             &
-                                       ( w_int(n) - ( 1.0_wp - dens_ratio(n) ) * g / &
-                                       exp_arg ) * ( 1.0_wp - exp_term )
-             ENDIF
-          ENDDO
-       ENDDO
-
-    ENDIF
-
-!
-!-- Store the old age of the particle ( needed to prevent that a
-!-- particle crosses several PEs during one timestep, and for the
-!-- evaluation of the subgrid particle velocity fluctuations )
-    particles(1:number_of_particles)%age_m = particles(1:number_of_particles)%age
-
-    DO  nb = 0, 7
-       DO  n = start_index(nb), end_index(nb)
-!
-!--       Increment the particle age and the total time that the particle
-!--       has advanced within the particle timestep procedure
-          particles(n)%age    = particles(n)%age    + dt_particle(n)
-          particles(n)%dt_sum = particles(n)%dt_sum + dt_particle(n)
-
-!
-!--       Check whether there is still a particle that has not yet completed 
-!--       the total LES timestep
-          IF ( ( dt_3d - particles(n)%dt_sum ) > 1E-8_wp )  THEN
-             dt_3d_reached_l = .FALSE.
-          ENDIF
-
-       ENDDO
-    ENDDO
-
-    CALL cpu_log( log_point_s(44), 'lpm_advec', 'pause' )
-
-
- END SUBROUTINE lpm_advec
-
-! Description:
-! ------------
-!> Calculation of subgrid-scale particle speed using the stochastic model 
-!> of Weil et al. (2004, JAS, 61, 2877-2887).
-!------------------------------------------------------------------------------! 
- SUBROUTINE weil_stochastic_eq( v_sgs, fs_n, e_n, dedxi_n, dedt_n, diss_n,     &
-                                dt_n, rg_n, fac )
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  c_0, sgs_wf_part
-
-    IMPLICIT NONE
-
-    REAL(wp) ::  a1      !< dummy argument
-    REAL(wp) ::  dedt_n  !< time derivative of TKE at particle position 
-    REAL(wp) ::  dedxi_n !< horizontal derivative of TKE at particle position
-    REAL(wp) ::  diss_n  !< dissipation at particle position 
-    REAL(wp) ::  dt_n    !< particle timestep
-    REAL(wp) ::  e_n     !< TKE at particle position
-    REAL(wp) ::  fac     !< flag to identify adjacent topography
-    REAL(wp) ::  fs_n    !< weighting factor to prevent that subgrid-scale particle speed becomes too large
-    REAL(wp) ::  rg_n    !< random number
-    REAL(wp) ::  term1   !< memory term
-    REAL(wp) ::  term2   !< drift correction term
-    REAL(wp) ::  term3   !< random term
-    REAL(wp) ::  v_sgs   !< subgrid-scale velocity component 
-
-!-- At first, limit TKE to a small non-zero number, in order to prevent 
-!-- the occurrence of extremely large SGS-velocities in case TKE is zero, 
-!-- (could occur at the simulation begin).
-    e_n = MAX( e_n, 1E-20_wp )
-!
-!-- Please note, terms 1 and 2 (drift and memory term, respectively) are 
-!-- multiplied by a flag to switch of both terms near topography. 
-!-- This is necessary, as both terms may cause a subgrid-scale velocity build up 
-!-- if particles are trapped in regions with very small TKE, e.g. in narrow street 
-!-- canyons resolved by only a few grid points. Hence, term 1 and term 2 are 
-!-- disabled if one of the adjacent grid points belongs to topography. 
-!-- Moreover, in this case, the  previous subgrid-scale component is also set
-!-- to zero.
-
-    a1 = fs_n * c_0 * diss_n
-!
-!-- Memory term
-    term1 = - a1 * v_sgs * dt_n / ( 4.0_wp * sgs_wf_part * e_n + 1E-20_wp )    &
-                 * fac
-!
-!-- Drift correction term
-    term2 = ( ( dedt_n * v_sgs / e_n ) + dedxi_n ) * 0.5_wp * dt_n              &
-                 * fac
-!
-!-- Random term
-    term3 = SQRT( MAX( a1, 1E-20_wp ) ) * ( rg_n - 1.0_wp ) * SQRT( dt_n )
-!
-!-- In cese one of the adjacent grid-boxes belongs to topograhy, the previous
-!-- subgrid-scale velocity component is set to zero, in order to prevent a 
-!-- velocity build-up. 
-!-- This case, set also previous subgrid-scale component to zero. 
-    v_sgs = v_sgs * fac + term1 + term2 + term3
-
- END SUBROUTINE weil_stochastic_eq
Index: palm/trunk/SOURCE/lpm_boundary_conds.f90
===================================================================
--- palm/trunk/SOURCE/lpm_boundary_conds.f90	(revision 4015)
+++ 	(revision )
@@ -1,644 +1,0 @@
-!> @file lpm_boundary_conds.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! variables documented
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3189 2018-08-06 13:18:55Z Giersch
-! Bugfix in calculation of the x/y indices for current particle postion
-! 
-! 3067 2018-06-12 14:04:34Z suehring
-! Remove write statements
-! 
-! 2801 2018-02-14 16:01:55Z thiele
-! Introduce particle transfer in nested models.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2701 2017-12-15 15:40:50Z suehring
-! Changes from last commit documented
-! 
-! 2698 2017-12-14 18:46:24Z suehring
-! Particle reflections at downward-facing walls implemented. Moreover, 
-! reflections are adjusted to revised particle grid box location. 
-! (responsible Philipp Thiele)
-!
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done
-! 
-! 2318 2017-07-20 17:27:44Z suehring
-! Get topography top index via Function call 
-! 
-! 2317 2017-07-20 17:27:19Z suehring
-!
-! 2232 2017-05-30 17:47:52Z suehring
-! Adjustments to new topography and surface concept
-! Rename character range into location, as range is an intrinsic. 
-! 
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1929 2016-06-09 16:25:25Z suehring
-! Rewritten wall reflection 
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed. Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! routine renamed lpm_boundary_conds, bottom and top boundary conditions
-! included (former part of advec_particles)
-!
-! 824 2012-02-17 09:09:57Z raasch
-! particle attributes speed_x|y|z_sgs renamed rvar1|2|3
-!
-! Initial version (2007/03/09)
-!
-! Description:
-! ------------
-!> Boundary conditions for the Lagrangian particles.
-!> The routine consists of two different parts. One handles the bottom (flat)
-!> and top boundary. In this part, also particles which exceeded their lifetime
-!> are deleted.
-!> The other part handles the reflection of particles from vertical walls.
-!> This part was developed by Jin Zhang during 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.
-!>
-!> THE WALLS PART OF THIS ROUTINE HAS NOT BEEN TESTED FOR OCEAN RUNS SO FAR!!!!
-!> (see offset_ocean_*)
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_boundary_conds( location , i, j, k )
- 
-
-    USE arrays_3d,                                                             &
-        ONLY:  zw
-
-    USE control_parameters,                                                    &
-        ONLY:  message_string, particle_maximum_age
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE grid_variables,                                                        &
-        ONLY:  ddx, dx, ddy, dy
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nz, wall_flags_0
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  deleted_particles, ibc_par_b, ibc_par_t, number_of_particles,   &
-               particles, particle_type, use_sgs_for_particles
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=*) ::  location !< general mode: boundary conditions at bottom/top of the model domain
-                                   !< or at vertical surfaces (buildings, terrain steps)
-    
-    INTEGER(iwp), INTENT(IN) ::  i !< grid index of particle box along x
-    INTEGER(iwp), INTENT(IN) ::  j !< grid index of particle box along y
-    INTEGER(iwp), INTENT(IN) ::  k !< grid index of particle box along z
-    
-    INTEGER(iwp) ::  inc            !< dummy for sorting algorithmus
-    INTEGER(iwp) ::  ir             !< dummy for sorting algorithmus
-    INTEGER(iwp) ::  i1             !< grid index (x) of old particle position
-    INTEGER(iwp) ::  i2             !< grid index (x) of current particle position
-    INTEGER(iwp) ::  i3             !< grid index (x) of intermediate particle position
-    INTEGER(iwp) ::  jr             !< dummy for sorting algorithmus
-    INTEGER(iwp) ::  j1             !< grid index (y) of old particle position
-    INTEGER(iwp) ::  j2             !< grid index (y) of current particle position
-    INTEGER(iwp) ::  j3             !< grid index (y) of intermediate particle position
-    INTEGER(iwp) ::  k1             !< grid index (z) of old particle position
-    INTEGER(iwp) ::  k2             !< grid index (z) of current particle position
-    INTEGER(iwp) ::  k3             !< grid index (z) of intermediate particle position
-    INTEGER(iwp) ::  n              !< particle number
-    INTEGER(iwp) ::  t_index        !< running index for intermediate particle timesteps in reflection algorithmus
-    INTEGER(iwp) ::  t_index_number !< number of intermediate particle timesteps in reflection algorithmus
-    INTEGER(iwp) ::  tmp_x          !< dummy for sorting algorithm
-    INTEGER(iwp) ::  tmp_y          !< dummy for sorting algorithm
-    INTEGER(iwp) ::  tmp_z          !< dummy for sorting algorithm
-
-    INTEGER(iwp), DIMENSION(0:10) :: x_ind(0:10) = 0 !< index array (x) of intermediate particle positions 
-    INTEGER(iwp), DIMENSION(0:10) :: y_ind(0:10) = 0 !< index array (y) of intermediate particle positions
-    INTEGER(iwp), DIMENSION(0:10) :: z_ind(0:10) = 0 !< index array (z) of intermediate particle positions
-    
-    LOGICAL  ::  cross_wall_x    !< flag to check if particle reflection along x is necessary
-    LOGICAL  ::  cross_wall_y    !< flag to check if particle reflection along y is necessary
-    LOGICAL  ::  cross_wall_z    !< flag to check if particle reflection along z is necessary
-    LOGICAL  ::  reflect_x       !< flag to check if particle is already reflected along x
-    LOGICAL  ::  reflect_y       !< flag to check if particle is already reflected along y
-    LOGICAL  ::  reflect_z       !< flag to check if particle is already reflected along z
-    LOGICAL  ::  tmp_reach_x     !< dummy for sorting algorithmus
-    LOGICAL  ::  tmp_reach_y     !< dummy for sorting algorithmus
-    LOGICAL  ::  tmp_reach_z     !< dummy for sorting algorithmus
-    LOGICAL  ::  x_wall_reached  !< flag to check if particle has already reached wall
-    LOGICAL  ::  y_wall_reached  !< flag to check if particle has already reached wall
-    LOGICAL  ::  z_wall_reached  !< flag to check if particle has already reached wall
-
-    LOGICAL, DIMENSION(0:10) ::  reach_x  !< flag to check if particle is at a yz-wall
-    LOGICAL, DIMENSION(0:10) ::  reach_y  !< flag to check if particle is at a xz-wall
-    LOGICAL, DIMENSION(0:10) ::  reach_z  !< flag to check if particle is at a xy-wall
-
-    REAL(wp) ::  dt_particle    !< particle timestep
-    REAL(wp) ::  eps = 1E-10_wp !< security number to check if particle has reached a wall
-    REAL(wp) ::  pos_x          !< intermediate particle position (x)
-    REAL(wp) ::  pos_x_old      !< particle position (x) at previous particle timestep
-    REAL(wp) ::  pos_y          !< intermediate particle position (y)
-    REAL(wp) ::  pos_y_old      !< particle position (y) at previous particle timestep
-    REAL(wp) ::  pos_z          !< intermediate particle position (z)
-    REAL(wp) ::  pos_z_old      !< particle position (z) at previous particle timestep
-    REAL(wp) ::  prt_x          !< current particle position (x)
-    REAL(wp) ::  prt_y          !< current particle position (y)
-    REAL(wp) ::  prt_z          !< current particle position (z)
-    REAL(wp) ::  t_old          !< previous reflection time
-    REAL(wp) ::  tmp_t          !< dummy for sorting algorithmus
-    REAL(wp) ::  xwall          !< location of wall in x
-    REAL(wp) ::  ywall          !< location of wall in y
-    REAL(wp) ::  zwall          !< location of wall in z
-
-    REAL(wp), DIMENSION(0:10) ::  t  !< reflection time
-
-
-    IF ( location == 'bottom/top' )  THEN
-
-!
-!--    Apply boundary conditions to those particles that have crossed the top or
-!--    bottom boundary and delete those particles, which are older than allowed
-       DO  n = 1, number_of_particles
-
-!
-!--       Stop if particles have moved further than the length of one 
-!--       PE subdomain (newly released particles have age = age_m!)
-          IF ( particles(n)%age /= particles(n)%age_m )  THEN
-             IF ( ABS(particles(n)%speed_x) >                                  &
-                  ((nxr-nxl+2)*dx)/(particles(n)%age-particles(n)%age_m)  .OR. &
-                  ABS(particles(n)%speed_y) >                                  &
-                  ((nyn-nys+2)*dy)/(particles(n)%age-particles(n)%age_m) )  THEN
-
-                  WRITE( message_string, * )  'particle too fast.  n = ',  n 
-                  CALL message( 'lpm_boundary_conds', 'PA0148', 2, 2, -1, 6, 1 )
-             ENDIF
-          ENDIF
-
-          IF ( particles(n)%age > particle_maximum_age  .AND.  &
-               particles(n)%particle_mask )                              &
-          THEN
-             particles(n)%particle_mask  = .FALSE.
-             deleted_particles = deleted_particles + 1
-          ENDIF
-
-          IF ( particles(n)%z >= zw(nz)  .AND.  particles(n)%particle_mask )  THEN
-             IF ( ibc_par_t == 1 )  THEN
-!
-!--             Particle absorption
-                particles(n)%particle_mask  = .FALSE.
-                deleted_particles = deleted_particles + 1
-             ELSEIF ( ibc_par_t == 2 )  THEN
-!
-!--             Particle reflection
-                particles(n)%z       = 2.0_wp * zw(nz) - particles(n)%z
-                particles(n)%speed_z = -particles(n)%speed_z
-                IF ( use_sgs_for_particles  .AND. &
-                     particles(n)%rvar3 > 0.0_wp )  THEN
-                   particles(n)%rvar3 = -particles(n)%rvar3
-                ENDIF
-             ENDIF
-          ENDIF
-          
-          IF ( particles(n)%z < zw(0)  .AND.  particles(n)%particle_mask )  THEN
-             IF ( ibc_par_b == 1 )  THEN
-!
-!--             Particle absorption
-                particles(n)%particle_mask  = .FALSE.
-                deleted_particles = deleted_particles + 1
-             ELSEIF ( ibc_par_b == 2 )  THEN
-!
-!--             Particle reflection
-                particles(n)%z       = 2.0_wp * zw(0) - particles(n)%z
-                particles(n)%speed_z = -particles(n)%speed_z
-                IF ( use_sgs_for_particles  .AND. &
-                     particles(n)%rvar3 < 0.0_wp )  THEN
-                   particles(n)%rvar3 = -particles(n)%rvar3
-                ENDIF
-             ENDIF
-          ENDIF
-       ENDDO
-
-    ELSEIF ( location == 'walls' )  THEN
-
-
-       CALL cpu_log( log_point_s(48), 'lpm_wall_reflect', 'start' )
-
-       DO  n = 1, number_of_particles
-!
-!--       Recalculate particle timestep
-          dt_particle = particles(n)%age - particles(n)%age_m
-!
-!--       Obtain x/y indices for current particle position
-          i2 = particles(n)%x * ddx
-          j2 = particles(n)%y * ddy
-          IF (zw(k)   < particles(n)%z ) k2 = k + 1
-          IF (zw(k)   > particles(n)%z .AND. zw(k-1) < particles(n)%z ) k2 = k
-          IF (zw(k-1) > particles(n)%z ) k2 = k - 1 
-!
-!--       Save current particle positions
-          prt_x = particles(n)%x
-          prt_y = particles(n)%y
-          prt_z = particles(n)%z
-!
-!--       Recalculate old particle positions
-          pos_x_old = particles(n)%x - particles(n)%speed_x * dt_particle
-          pos_y_old = particles(n)%y - particles(n)%speed_y * dt_particle
-          pos_z_old = particles(n)%z - particles(n)%speed_z * dt_particle
-!
-!--       Obtain x/y indices for old particle positions
-          i1 = i
-          j1 = j
-          k1 = k
-!
-!--       Determine horizontal as well as vertical walls at which particle can 
-!--       be potentially reflected. 
-!--       Start with walls aligned in yz layer.
-!--       Wall to the right 
-          IF ( prt_x > pos_x_old )  THEN
-             xwall = ( i1 + 1 ) * dx
-!
-!--       Wall to the left
-          ELSE
-             xwall = i1 * dx
-          ENDIF
-!
-!--       Walls aligned in xz layer
-!--       Wall to the north
-          IF ( prt_y > pos_y_old )  THEN
-             ywall = ( j1 +1 ) * dy
-!--       Wall to the south
-          ELSE
-             ywall = j1 * dy
-          ENDIF
-
-          IF ( prt_z > pos_z_old ) THEN
-             zwall = zw(k)
-          ELSE
-             zwall = zw(k-1)
-          ENDIF     
-!
-!--       Initialize flags to check if particle reflection is necessary
-          cross_wall_x = .FALSE.
-          cross_wall_y = .FALSE.
-          cross_wall_z = .FALSE.
-!
-!--       Initialize flags to check if a wall is reached
-          reach_x      = .FALSE.
-          reach_y      = .FALSE.
-          reach_z      = .FALSE.
-!
-!--       Initialize flags to check if a particle was already reflected
-          reflect_x    = .FALSE.
-          reflect_y    = .FALSE.
-          reflect_z    = .FALSE.
-!
-!--       Initialize flags to check if a wall is already crossed.
-!--       ( Required to obtain correct indices. )
-          x_wall_reached = .FALSE.
-          y_wall_reached = .FALSE.
-          z_wall_reached = .FALSE.
-!
-!--       Initialize time array 
-          t     = 0.0_wp
-!
-!--       Check if particle can reach any wall. This case, calculate the 
-!--       fractional time needed to reach this wall. Store this fractional
-!--       timestep in array t. Moreover, store indices for these grid
-!--       boxes where the respective wall belongs to.  
-!--       Start with x-direction.
-          t_index    = 1
-          t(t_index) = ( xwall - pos_x_old )                                   &
-                     / MERGE( MAX( prt_x - pos_x_old,  1E-30_wp ),             &
-                              MIN( prt_x - pos_x_old, -1E-30_wp ),             &
-                              prt_x > pos_x_old )
-          x_ind(t_index)   = i2
-          y_ind(t_index)   = j1
-          z_ind(t_index)   = k1
-          reach_x(t_index) = .TRUE.
-          reach_y(t_index) = .FALSE.
-          reach_z(t_index) = .FALSE.
-!
-!--       Store these values only if particle really reaches any wall. t must 
-!--       be in a interval between [0:1].
-          IF ( t(t_index) <= 1.0_wp .AND. t(t_index) >= 0.0_wp )  THEN
-             t_index      = t_index + 1
-             cross_wall_x = .TRUE.
-          ENDIF
-!
-!--       y-direction
-          t(t_index) = ( ywall - pos_y_old )                                   &
-                     / MERGE( MAX( prt_y - pos_y_old,  1E-30_wp ),             &
-                              MIN( prt_y - pos_y_old, -1E-30_wp ),             &
-                              prt_y > pos_y_old )
-          x_ind(t_index)   = i1
-          y_ind(t_index)   = j2
-          z_ind(t_index)   = k1
-          reach_x(t_index) = .FALSE.
-          reach_y(t_index) = .TRUE.
-          reach_z(t_index) = .FALSE.
-          IF ( t(t_index) <= 1.0_wp .AND. t(t_index) >= 0.0_wp )  THEN
-             t_index      = t_index + 1
-             cross_wall_y = .TRUE.
-          ENDIF
-!
-!--       z-direction
-          t(t_index) = (zwall - pos_z_old )                                    &
-                     / MERGE( MAX( prt_z - pos_z_old,  1E-30_wp ),             &
-                              MIN( prt_z - pos_z_old, -1E-30_wp ),             &
-                              prt_z > pos_z_old )
-                     
-          x_ind(t_index)   = i1
-          y_ind(t_index)   = j1
-          z_ind(t_index)   = k2
-          reach_x(t_index) = .FALSE.
-          reach_y(t_index) = .FALSE.
-          reach_z(t_index) = .TRUE.
-          IF( t(t_index) <= 1.0_wp .AND. t(t_index) >= 0.0_wp) THEN
-             t_index      = t_index + 1
-             cross_wall_z = .TRUE.
-          ENDIF
-          
-          t_index_number = t_index - 1
-!
-!--       Carry out reflection only if particle reaches any wall
-          IF ( cross_wall_x .OR. cross_wall_y .OR. cross_wall_z )  THEN
-!
-!--          Sort fractional timesteps in ascending order. Also sort the 
-!--          corresponding indices and flag according to the time interval a  
-!--          particle reaches the respective wall.
-             inc = 1
-             jr  = 1
-             DO WHILE ( inc <= t_index_number )
-                inc = 3 * inc + 1
-             ENDDO
-
-             DO WHILE ( inc > 1 )
-                inc = inc / 3
-                DO  ir = inc+1, t_index_number
-                   tmp_t       = t(ir)
-                   tmp_x       = x_ind(ir)
-                   tmp_y       = y_ind(ir)
-                   tmp_z       = z_ind(ir)
-                   tmp_reach_x = reach_x(ir)
-                   tmp_reach_y = reach_y(ir)
-                   tmp_reach_z = reach_z(ir)
-                   jr    = ir
-                   DO WHILE ( t(jr-inc) > tmp_t )
-                      t(jr)       = t(jr-inc)
-                      x_ind(jr)   = x_ind(jr-inc)
-                      y_ind(jr)   = y_ind(jr-inc)
-                      z_ind(jr)   = z_ind(jr-inc)
-                      reach_x(jr) = reach_x(jr-inc)
-                      reach_y(jr) = reach_y(jr-inc)
-                      reach_z(jr) = reach_z(jr-inc)
-                      jr    = jr - inc
-                      IF ( jr <= inc )  EXIT
-                   ENDDO
-                   t(jr)       = tmp_t
-                   x_ind(jr)   = tmp_x
-                   y_ind(jr)   = tmp_y
-                   z_ind(jr)   = tmp_z
-                   reach_x(jr) = tmp_reach_x
-                   reach_y(jr) = tmp_reach_y
-                   reach_z(jr) = tmp_reach_z
-                ENDDO
-             ENDDO
-!
-!--          Initialize temporary particle positions
-             pos_x = pos_x_old
-             pos_y = pos_y_old
-             pos_z = pos_z_old
-!
-!--          Loop over all times a particle possibly moves into a new grid box
-             t_old = 0.0_wp
-             DO t_index = 1, t_index_number 
-!           
-!--             Calculate intermediate particle position according to the 
-!--             timesteps a particle reaches any wall.
-                pos_x = pos_x + ( t(t_index) - t_old ) * dt_particle           &
-                                                       * particles(n)%speed_x
-                pos_y = pos_y + ( t(t_index) - t_old ) * dt_particle           &
-                                                       * particles(n)%speed_y
-                pos_z = pos_z + ( t(t_index) - t_old ) * dt_particle           &
-                                                       * particles(n)%speed_z
-!
-!--             Obtain x/y grid indices for intermediate particle position from
-!--             sorted index array
-                i3 = x_ind(t_index)
-                j3 = y_ind(t_index)
-                k3 = z_ind(t_index)
-!
-!--             Check which wall is already reached
-                IF ( .NOT. x_wall_reached )  x_wall_reached = reach_x(t_index) 
-                IF ( .NOT. y_wall_reached )  y_wall_reached = reach_y(t_index)
-                IF ( .NOT. z_wall_reached )  z_wall_reached = reach_z(t_index)
-!
-!--             Check if a particle needs to be reflected at any yz-wall. If 
-!--             necessary, carry out reflection. Please note, a security 
-!--             constant is required, as the particle position does not 
-!--             necessarily exactly match the wall location due to rounding 
-!--             errors.
-                IF ( reach_x(t_index)                      .AND.               & 
-                     ABS( pos_x - xwall ) < eps            .AND.               &
-                     .NOT. BTEST(wall_flags_0(k3,j3,i3),0) .AND.               &
-                     .NOT. reflect_x )  THEN
-! 
-! 
-!--                Reflection in x-direction. 
-!--                Ensure correct reflection by MIN/MAX functions, depending on
-!--                direction of particle transport. 
-!--                Due to rounding errors pos_x does not exactly match the wall
-!--                location, leading to erroneous reflection.             
-                   pos_x = MERGE( MIN( 2.0_wp * xwall - pos_x, xwall ),        &
-                                  MAX( 2.0_wp * xwall - pos_x, xwall ),        &
-                                  particles(n)%x > xwall )
-!
-!--                Change sign of particle speed                     
-                   particles(n)%speed_x = - particles(n)%speed_x
-!
-!--                Also change sign of subgrid-scale particle speed
-                   particles(n)%rvar1 = - particles(n)%rvar1
-!
-!--                Set flag that reflection along x is already done
-                   reflect_x          = .TRUE.
-!
-!--                As the particle does not cross any further yz-wall during 
-!--                this timestep, set further x-indices to the current one.
-                   x_ind(t_index:t_index_number) = i1
-!
-!--             If particle already reached the wall but was not reflected, 
-!--             set further x-indices to the new one.
-                ELSEIF ( x_wall_reached .AND. .NOT. reflect_x )  THEN
-                    x_ind(t_index:t_index_number) = i2
-                ENDIF !particle reflection in x direction done
-
-!
-!--             Check if a particle needs to be reflected at any xz-wall. If 
-!--             necessary, carry out reflection. Please note, a security 
-!--             constant is required, as the particle position does not 
-!--             necessarily exactly match the wall location due to rounding 
-!--             errors. 
-                IF ( reach_y(t_index)                      .AND.               & 
-                     ABS( pos_y - ywall ) < eps            .AND.               &
-                     .NOT. BTEST(wall_flags_0(k3,j3,i3),0) .AND.               &
-                     .NOT. reflect_y )  THEN
-! 
-! 
-!--                Reflection in y-direction. 
-!--                Ensure correct reflection by MIN/MAX functions, depending on
-!--                direction of particle transport. 
-!--                Due to rounding errors pos_y does not exactly match the wall
-!--                location, leading to erroneous reflection.             
-                   pos_y = MERGE( MIN( 2.0_wp * ywall - pos_y, ywall ),        &
-                                  MAX( 2.0_wp * ywall - pos_y, ywall ),        &
-                                  particles(n)%y > ywall )
-!
-!--                Change sign of particle speed                     
-                   particles(n)%speed_y = - particles(n)%speed_y
-!
-!--                Also change sign of subgrid-scale particle speed
-                   particles(n)%rvar2 = - particles(n)%rvar2
-!
-!--                Set flag that reflection along y is already done
-                   reflect_y          = .TRUE.
-!
-!--                As the particle does not cross any further xz-wall during 
-!--                this timestep, set further y-indices to the current one.
-                   y_ind(t_index:t_index_number) = j1
-!
-!--             If particle already reached the wall but was not reflected, 
-!--             set further y-indices to the new one.
-                ELSEIF ( y_wall_reached .AND. .NOT. reflect_y )  THEN
-                    y_ind(t_index:t_index_number) = j2
-                ENDIF !particle reflection in y direction done
-                
-!
-!--             Check if a particle needs to be reflected at any xy-wall. If 
-!--             necessary, carry out reflection. Please note, a security 
-!--             constant is required, as the particle position does not 
-!--             necessarily exactly match the wall location due to rounding 
-!--             errors. 
-                IF ( reach_z(t_index)                      .AND.               & 
-                     ABS( pos_z - zwall ) < eps            .AND.               &
-                     .NOT. BTEST(wall_flags_0(k3,j3,i3),0) .AND.               &
-                     .NOT. reflect_z )  THEN
-! 
-! 
-!--                Reflection in z-direction. 
-!--                Ensure correct reflection by MIN/MAX functions, depending on
-!--                direction of particle transport. 
-!--                Due to rounding errors pos_z does not exactly match the wall
-!--                location, leading to erroneous reflection.             
-                   pos_z = MERGE( MIN( 2.0_wp * zwall - pos_z, zwall ),        &
-                                  MAX( 2.0_wp * zwall - pos_z, zwall ),        &
-                                  particles(n)%z > zwall )
-!
-!--                Change sign of particle speed                     
-                   particles(n)%speed_z = - particles(n)%speed_z
-!
-!--                Also change sign of subgrid-scale particle speed
-                   particles(n)%rvar3 = - particles(n)%rvar3
-!
-!--                Set flag that reflection along z is already done
-                   reflect_z          = .TRUE.
-!
-!--                As the particle does not cross any further xy-wall during 
-!--                this timestep, set further z-indices to the current one.
-                   z_ind(t_index:t_index_number) = k1
-!
-!--             If particle already reached the wall but was not reflected, 
-!--             set further z-indices to the new one.
-                ELSEIF ( z_wall_reached .AND. .NOT. reflect_z )  THEN
-                    z_ind(t_index:t_index_number) = k2
-                ENDIF !particle reflection in z direction done                
-                
-!
-!--             Swap time
-                t_old = t(t_index)
-
-             ENDDO
-!
-!--          If a particle was reflected, calculate final position from last
-!--          intermediate position.
-             IF ( reflect_x .OR. reflect_y .OR. reflect_z )  THEN
-
-                particles(n)%x = pos_x + ( 1.0_wp - t_old ) * dt_particle      &
-                                                         * particles(n)%speed_x
-                particles(n)%y = pos_y + ( 1.0_wp - t_old ) * dt_particle      &
-                                                         * particles(n)%speed_y
-                particles(n)%z = pos_z + ( 1.0_wp - t_old ) * dt_particle      &
-                                                         * particles(n)%speed_z
-
-             ENDIF
-
-          ENDIF
-
-       ENDDO
-
-       CALL cpu_log( log_point_s(48), 'lpm_wall_reflect', 'stop' )
-
-    ENDIF
-
- END SUBROUTINE lpm_boundary_conds
Index: palm/trunk/SOURCE/lpm_calc_liquid_water_content.f90
===================================================================
--- palm/trunk/SOURCE/lpm_calc_liquid_water_content.f90	(revision 4015)
+++ 	(revision )
@@ -1,158 +1,0 @@
-!> @file lpm_calc_liquid_water_content.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Modularization of all bulk cloud physics code components
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3039 2018-05-24 13:13:11Z schwenkel
-! bugfix for lcm with grid stretching
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Calculate the liquid water content for each grid box.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_calc_liquid_water_content
- 
-
-    USE arrays_3d,                                                             &
-        ONLY:  dzw, ql, ql_v, ql_vp
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  pi, rho_l
-
-    USE control_parameters,                                                    &
-        ONLY:  message_string, rho_surface
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  grid_particles, number_of_particles, particles, prt_count
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i   !<
-    INTEGER(iwp) ::  j   !<
-    INTEGER(iwp) ::  k   !<
-    INTEGER(iwp) ::  n   !<
-
-    CALL cpu_log( log_point_s(45), 'lpm_calc_ql', 'start' )
-
-!
-!-- Set water content initially to zero
-    ql = 0.0_wp;  ql_v = 0.0_wp;  ql_vp = 0.0_wp
-
-!
-!-- Calculate for each grid box
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb+1, nzt
-
-             number_of_particles = prt_count(k,j,i)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-
-!
-!--          Calculate the total volume in the boxes (ql_v, weighting factor
-!--          has to beincluded)
-             DO  n = 1, prt_count(k,j,i)
-                ql_v(k,j,i)  = ql_v(k,j,i)  + particles(n)%weight_factor *  &
-                                              particles(n)%radius**3
-             ENDDO
-
-!
-!--          Calculate the liquid water content
-             IF ( ql_v(k,j,i) /= 0.0_wp )  THEN
-                ql(k,j,i) = ql(k,j,i) + rho_l * 1.33333333_wp * pi *           &
-                                        ql_v(k,j,i) /                          &
-                                        ( rho_surface * dx * dy * dzw(k) )
-
-                IF ( ql(k,j,i) < 0.0_wp )  THEN
-                   WRITE( message_string, * )  'LWC out of range: ' , &
-                                               ql(k,j,i),i,j,k
-                   CALL message( 'lpm_calc_liquid_water_content', '', 2, 2, &
-                                 -1, 6, 1 )
-                ENDIF
-
-             ELSE
-
-                ql(k,j,i) = 0.0_wp
-
-             ENDIF
-
-          ENDDO
-       ENDDO
-    ENDDO
-
-    CALL cpu_log( log_point_s(45), 'lpm_calc_ql', 'stop' )
-
-
- END SUBROUTINE lpm_calc_liquid_water_content
Index: palm/trunk/SOURCE/lpm_collision_kernels.f90
===================================================================
--- palm/trunk/SOURCE/lpm_collision_kernels.f90	(revision 4015)
+++ 	(revision )
@@ -1,1019 +1,0 @@
-!> @file lpm_collision_kernels.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Modularization of all bulk cloud physics code components
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1880 2016-04-20 09:36:50Z hoffmann
-! Bugfix: The index of the larger particle has to be chosen for interpolation.
-!
-! 1873 2016-04-18 14:50:06Z maronga
-! Module renamed (removed _mod)
-! 
-! 1858 2016-04-13 13:12:11Z hoffmann
-! Interpolation of collision kernels adjusted to more reasonable values.
-! Reformatting of the code. 
-!
-! 1850 2016-04-08 13:29:27Z maronga
-! Module renamed
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! PALM kernel has been deleted.
-! Bugfix in the calculation of the turbulent enhancement factor of the
-! collection efficiency.
-!
-! Unused variables removed.
-!
-! 1776 2016-03-02 17:54:58Z hoffmann
-! Bugfix: Collection efficiencies must be calculated for the larger droplet.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-! 
-! 1519 2015-01-08 10:20:42Z hoffmann
-! Bugfix: Using the new particle structure, particles are not sorted by size.
-! Hence, computation of collision efficiencies must ensure that the ratio of 
-! two colliding droplets is < 1.
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-! Kind definition added to all floating point numbers.
-! 
-! 1346 2014-03-27 13:18:20Z heinze
-! Bugfix: REAL constants provided with KIND-attribute especially in call of 
-! intrinsic function like MAX, MIN, SIGN
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! REAL constants defined as wp_kind
-!
-! 1320 2014-03-20 08:40:49Z 
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1092 2013-02-02 11:24:22Z raasch
-! unused variables removed
-!
-! 1071 2012-11-29 16:54:55Z franke
-! Bugfix: collision efficiencies for Hall kernel should not be < 1.0E-20
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 1007 2012-09-19 14:30:36Z franke
-! converted all units to SI units and replaced some parameters by corresponding
-! PALM parameters
-! Bugfix: factor in calculation of enhancement factor for collision efficencies
-! changed from 10. to 1.0
-!
-! 849 2012-03-15 10:35:09Z raasch
-! routine collision_efficiency_rogers added (moved from former advec_particles
-! to here)
-!
-! 835 2012-02-22 11:21:19Z raasch $
-! Bugfix: array diss can be used only in case of Wang kernel
-!
-! 828 2012-02-21 12:00:36Z raasch
-! code has been completely reformatted, routine colker renamed
-! recalculate_kernel,
-! routine init_kernels added, radius is now communicated to the collision
-! routines by array radclass
-!
-! Bugfix: transformation factor for dissipation changed from 1E5 to 1E4
-!
-! 825 2012-02-19 03:03:44Z raasch
-! routine renamed from wang_kernel to lpm_collision_kernels,
-! turbulence_effects on collision replaced by wang_kernel
-!
-! 790 2011-11-29 03:11:20Z raasch
-! initial revision
-!
-! Description:
-! ------------
-!> This module calculates collision efficiencies either due to pure gravitational
-!> effects (Hall kernel, see Hall, 1980: J. Atmos. Sci., 2486-2507) or
-!> including the effects of turbulence (Wang kernel, see Wang and
-!> Grabowski, 2009: Atmos. Sci. Lett., 10, 1-8, and Ayala et al., 2008: 
-!> New J. Phys., 10, 075016). The original code has been
-!> provided by L.-P. Wang but is substantially reformatted and speed optimized
-!> here.
-!------------------------------------------------------------------------------!
- MODULE lpm_collision_kernels_mod
- 
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  g, pi
-        
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  collision_kernel, dissipation_classes, particles,               &
-               radius_classes
-
-    USE pegrid
-
-
-    IMPLICIT NONE
-
-    PRIVATE
-
-    PUBLIC  ckernel, init_kernels, rclass_lbound, rclass_ubound,               &
-            recalculate_kernel
-
-    REAL(wp) ::  epsilon       !<
-    REAL(wp) ::  rclass_lbound !<
-    REAL(wp) ::  rclass_ubound !<
-    REAL(wp) ::  urms          !<
-
-    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  epsclass  !< dissipation rate class
-    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  radclass  !< radius class
-    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  winf      !<
-    
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ec        !<
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ecf       !<
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  gck       !<
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  hkernel   !<
-    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  hwratio   !<
-    
-    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  ckernel !<
-
-    SAVE
-
-!
-!-- Public interfaces
-    INTERFACE init_kernels
-       MODULE PROCEDURE init_kernels
-    END INTERFACE init_kernels
-
-    INTERFACE recalculate_kernel
-       MODULE PROCEDURE recalculate_kernel
-    END INTERFACE recalculate_kernel
-
-
-    CONTAINS
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Initialization of the collision efficiency matrix with fixed radius and
-!> dissipation classes, calculated at simulation start only.
-!------------------------------------------------------------------------------!
- 
-    SUBROUTINE init_kernels
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i !<
-       INTEGER(iwp) ::  j !<
-       INTEGER(iwp) ::  k !<
-
-
-!
-!--    Calculate collision efficiencies for fixed radius- and dissipation
-!--    classes
-       IF ( collision_kernel(6:9) == 'fast' )  THEN
-
-          ALLOCATE( ckernel(1:radius_classes,1:radius_classes,                 &
-                    0:dissipation_classes), epsclass(1:dissipation_classes),   &
-                    radclass(1:radius_classes) )
-
-!
-!--       Calculate the radius class bounds with logarithmic distances
-!--       in the interval [1.0E-6, 1000.0E-6] m
-          rclass_lbound = LOG( 1.0E-6_wp )
-          rclass_ubound = LOG( 1000.0E-6_wp )
-          radclass(1)   = EXP( rclass_lbound )
-          DO  i = 2, radius_classes
-             radclass(i) = EXP( rclass_lbound +                                &
-                                ( rclass_ubound - rclass_lbound ) *            &
-                                ( i - 1.0_wp ) / ( radius_classes - 1.0_wp ) )
-          ENDDO
-
-!
-!--       Set the class bounds for dissipation in interval [0.0, 600.0] cm**2/s**3
-          DO  i = 1, dissipation_classes
-             epsclass(i) = 0.06_wp * REAL( i, KIND=wp ) / dissipation_classes
-          ENDDO
-!
-!--       Calculate collision efficiencies of the Wang/ayala kernel
-          ALLOCATE( ec(1:radius_classes,1:radius_classes),  &
-                    ecf(1:radius_classes,1:radius_classes), &
-                    gck(1:radius_classes,1:radius_classes), &
-                    winf(1:radius_classes) )
-
-          DO  k = 1, dissipation_classes
-
-             epsilon = epsclass(k)
-             urms    = 2.02_wp * ( epsilon / 0.04_wp )**( 1.0_wp / 3.0_wp )
-
-             CALL turbsd
-             CALL turb_enhance_eff
-             CALL effic
-
-             DO  j = 1, radius_classes
-                DO  i = 1, radius_classes
-                   ckernel(i,j,k) = ec(i,j) * gck(i,j) * ecf(i,j)
-                ENDDO
-             ENDDO
-
-          ENDDO
-
-!
-!--       Calculate collision efficiencies of the Hall kernel
-          ALLOCATE( hkernel(1:radius_classes,1:radius_classes), &
-                    hwratio(1:radius_classes,1:radius_classes) )
-
-          CALL fallg
-          CALL effic
-
-          DO  j = 1, radius_classes
-             DO  i =  1, radius_classes
-                hkernel(i,j) = pi * ( radclass(j) + radclass(i) )**2 &
-                                  * ec(i,j) * ABS( winf(j) - winf(i) )
-                ckernel(i,j,0) = hkernel(i,j)  ! hall kernel stored on index 0
-              ENDDO
-          ENDDO
-
-!
-!--       Test output of efficiencies
-          IF ( j == -1 )  THEN
-
-             PRINT*, '*** Hall kernel'
-             WRITE ( *,'(5X,20(F4.0,1X))' ) ( radclass(i)*1.0E6_wp, &
-                                              i = 1,radius_classes )
-             DO  j = 1, radius_classes
-                WRITE ( *,'(F4.0,1X,20(F8.4,1X))' ) radclass(j),  &
-                                          ( hkernel(i,j), i = 1,radius_classes )
-             ENDDO
-
-             DO  k = 1, dissipation_classes
-                DO  i = 1, radius_classes
-                   DO  j = 1, radius_classes
-                      IF ( hkernel(i,j) == 0.0_wp )  THEN
-                         hwratio(i,j) = 9999999.9_wp
-                      ELSE
-                         hwratio(i,j) = ckernel(i,j,k) / hkernel(i,j)
-                      ENDIF
-                   ENDDO
-                ENDDO
-
-                PRINT*, '*** epsilon = ', epsclass(k)
-                WRITE ( *,'(5X,20(F4.0,1X))' ) ( radclass(i) * 1.0E6_wp, &
-                                                 i = 1,radius_classes )
-                DO  j = 1, radius_classes
-                   WRITE ( *,'(F4.0,1X,20(F8.4,1X))' ) radclass(j) * 1.0E6_wp, &
-                                          ( hwratio(i,j), i = 1,radius_classes )
-                ENDDO
-             ENDDO
-
-          ENDIF
-
-          DEALLOCATE( ec, ecf, epsclass, gck, hkernel, winf )
-
-       ENDIF
-
-    END SUBROUTINE init_kernels
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Calculation of collision kernels during each timestep and for each grid box
-!------------------------------------------------------------------------------!
-    SUBROUTINE recalculate_kernel( i1, j1, k1 )
-
-       USE arrays_3d,                                                          &
-           ONLY:  diss
-
-       USE particle_attributes,                                                &
-           ONLY:  number_of_particles, prt_count, radius_classes, wang_kernel
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i      !<
-       INTEGER(iwp) ::  i1     !<
-       INTEGER(iwp) ::  j      !<
-       INTEGER(iwp) ::  j1     !<
-       INTEGER(iwp) ::  k1     !<
-
-
-       number_of_particles = prt_count(k1,j1,i1)
-       radius_classes      = number_of_particles   ! necessary to use the same
-                                                   ! subroutines as for 
-                                                   ! precalculated kernels
-
-       ALLOCATE( ec(1:number_of_particles,1:number_of_particles), &
-                 radclass(1:number_of_particles), winf(1:number_of_particles) )
-
-!
-!--    Store particle radii on the radclass array
-       radclass(1:number_of_particles) = particles(1:number_of_particles)%radius
-
-       IF ( wang_kernel )  THEN
-          epsilon = diss(k1,j1,i1)   ! dissipation rate in m**2/s**3
-       ELSE
-          epsilon = 0.0_wp
-       ENDIF
-       urms    = 2.02_wp * ( epsilon / 0.04_wp )**( 0.33333333333_wp )
-
-       IF ( wang_kernel  .AND.  epsilon > 1.0E-7_wp )  THEN
-!
-!--       Call routines to calculate efficiencies for the Wang kernel
-          ALLOCATE( gck(1:number_of_particles,1:number_of_particles), &
-                    ecf(1:number_of_particles,1:number_of_particles) )
-
-          CALL turbsd
-          CALL turb_enhance_eff
-          CALL effic
-
-          DO  j = 1, number_of_particles
-             DO  i =  1, number_of_particles
-                ckernel(1+i-1,1+j-1,1) = ec(i,j) * gck(i,j) * ecf(i,j)
-             ENDDO
-          ENDDO
-
-          DEALLOCATE( gck, ecf )
-
-       ELSE
-!
-!--       Call routines to calculate efficiencies for the Hall kernel
-          CALL fallg
-          CALL effic
-
-          DO  j = 1, number_of_particles
-             DO  i =  1, number_of_particles
-                ckernel(i,j,1) = pi * ( radclass(j) + radclass(i) )**2         &
-                                    * ec(i,j) * ABS( winf(j) - winf(i) )
-             ENDDO
-          ENDDO
-
-       ENDIF
-
-       DEALLOCATE( ec, radclass, winf )
-
-    END SUBROUTINE recalculate_kernel
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Calculation of effects of turbulence on the geometric collision kernel 
-!> (by including the droplets' average radial relative velocities and their 
-!> radial distribution function) following the analytic model by Aayala et al. 
-!> (2008, New J. Phys.). For details check the second part 2 of the publication,
-!> page 37ff.
-!>
-!> Input parameters, which need to be replaced by PALM parameters:
-!>    water density, air density
-!------------------------------------------------------------------------------!
-    SUBROUTINE turbsd
-
-       USE control_parameters,                                                 &
-           ONLY:  molecular_viscosity
-    
-       USE particle_attributes,                                                &
-           ONLY:  radius_classes
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i     !<
-       INTEGER(iwp) ::  j     !<
-
-       REAL(wp) ::  ao        !<
-       REAL(wp) ::  ao_gr     !<
-       REAL(wp) ::  bbb       !<
-       REAL(wp) ::  be        !<
-       REAL(wp) ::  b1        !<
-       REAL(wp) ::  b2        !<
-       REAL(wp) ::  ccc       !<
-       REAL(wp) ::  c1        !<
-       REAL(wp) ::  c1_gr     !<
-       REAL(wp) ::  c2        !<
-       REAL(wp) ::  d1        !<
-       REAL(wp) ::  d2        !<
-       REAL(wp) ::  eta       !<
-       REAL(wp) ::  e1        !<
-       REAL(wp) ::  e2        !<
-       REAL(wp) ::  fao_gr    !<
-       REAL(wp) ::  fr        !<
-       REAL(wp) ::  grfin     !<
-       REAL(wp) ::  lambda    !<
-       REAL(wp) ::  lambda_re !<
-       REAL(wp) ::  lf        !<
-       REAL(wp) ::  rc        !<
-       REAL(wp) ::  rrp       !<
-       REAL(wp) ::  sst       !<
-       REAL(wp) ::  tauk      !<
-       REAL(wp) ::  tl        !<
-       REAL(wp) ::  t2        !<
-       REAL(wp) ::  tt        !<
-       REAL(wp) ::  t1        !<
-       REAL(wp) ::  vk        !<
-       REAL(wp) ::  vrms1xy   !<
-       REAL(wp) ::  vrms2xy   !<
-       REAL(wp) ::  v1        !<
-       REAL(wp) ::  v1v2xy    !<
-       REAL(wp) ::  v1xysq    !<
-       REAL(wp) ::  v2        !<
-       REAL(wp) ::  v2xysq    !<
-       REAL(wp) ::  wrfin     !<
-       REAL(wp) ::  wrgrav2   !<
-       REAL(wp) ::  wrtur2xy  !<
-       REAL(wp) ::  xx        !<
-       REAL(wp) ::  yy        !<
-       REAL(wp) ::  z         !<
-
-       REAL(wp), DIMENSION(1:radius_classes) ::  st  !< Stokes number
-       REAL(wp), DIMENSION(1:radius_classes) ::  tau !< inertial time scale
-
-       lambda    = urms * SQRT( 15.0_wp * molecular_viscosity / epsilon )
-       lambda_re = urms**2 * SQRT( 15.0_wp / epsilon / molecular_viscosity )
-       tl        = urms**2 / epsilon
-       lf        = 0.5_wp * urms**3 / epsilon
-       tauk      = SQRT( molecular_viscosity / epsilon )
-       eta       = ( molecular_viscosity**3 / epsilon )**0.25_wp
-       vk        = eta / tauk
-
-       ao = ( 11.0_wp + 7.0_wp * lambda_re ) / ( 205.0_wp + lambda_re )
-       tt = SQRT( 2.0_wp * lambda_re / ( SQRT( 15.0_wp ) * ao ) ) * tauk
-
-!
-!--    Get terminal velocity of droplets
-       CALL fallg
-
-       DO  i = 1, radius_classes
-          tau(i) = winf(i) / g    ! inertial time scale
-          st(i)  = tau(i) / tauk  ! Stokes number
-       ENDDO
-
-!
-!--    Calculate average radial relative velocity at contact (wrfin)
-       z   = tt / tl
-       be  = SQRT( 2.0_wp ) * lambda / lf
-       bbb = SQRT( 1.0_wp - 2.0_wp * be**2 )
-       d1  = ( 1.0_wp + bbb ) / ( 2.0_wp * bbb )
-       e1  = lf * ( 1.0_wp + bbb ) * 0.5_wp
-       d2  = ( 1.0_wp - bbb ) * 0.5_wp / bbb
-       e2  = lf * ( 1.0_wp - bbb ) * 0.5_wp
-       ccc = SQRT( 1.0_wp - 2.0_wp * z**2 )
-       b1  = ( 1.0_wp + ccc ) * 0.5_wp / ccc
-       c1  = tl * ( 1.0_wp + ccc ) * 0.5_wp
-       b2  = ( 1.0_wp - ccc ) * 0.5_wp / ccc
-       c2  = tl * ( 1.0_wp - ccc ) * 0.5_wp
-
-       DO  i = 1, radius_classes
-
-          v1 = winf(i)
-          t1 = tau(i)
-
-          DO  j = 1, i
-             rrp = radclass(i) + radclass(j)
-             v2  = winf(j)
-             t2  = tau(j)
-
-             v1xysq  = b1 * d1 * phi_w(c1,e1,v1,t1) - b1 * d2 * phi_w(c1,e2,v1,t1) &
-                     - b2 * d1 * phi_w(c2,e1,v1,t1) + b2 * d2 * phi_w(c2,e2,v1,t1)
-             v1xysq  = v1xysq * urms**2 / t1
-             vrms1xy = SQRT( v1xysq )
-
-             v2xysq  = b1 * d1 * phi_w(c1,e1,v2,t2) - b1 * d2 * phi_w(c1,e2,v2,t2) &
-                     - b2 * d1 * phi_w(c2,e1,v2,t2) + b2 * d2 * phi_w(c2,e2,v2,t2)
-             v2xysq  = v2xysq * urms**2 / t2
-             vrms2xy = SQRT( v2xysq )
-
-             IF ( winf(i) >= winf(j) )  THEN
-                v1 = winf(i)
-                t1 = tau(i)
-                v2 = winf(j)
-                t2 = tau(j)
-             ELSE
-                v1 = winf(j)
-                t1 = tau(j)
-                v2 = winf(i)
-                t2 = tau(i)
-             ENDIF
-
-             v1v2xy   =  b1 * d1 * zhi(c1,e1,v1,t1,v2,t2) - &
-                         b1 * d2 * zhi(c1,e2,v1,t1,v2,t2) - &
-                         b2 * d1 * zhi(c2,e1,v1,t1,v2,t2) + &
-                         b2 * d2* zhi(c2,e2,v1,t1,v2,t2)
-             fr       = d1 * EXP( -rrp / e1 ) - d2 * EXP( -rrp / e2 )
-             v1v2xy   = v1v2xy * fr * urms**2 / tau(i) / tau(j)
-             wrtur2xy = vrms1xy**2 + vrms2xy**2 - 2.0_wp * v1v2xy
-             IF ( wrtur2xy < 0.0_wp )  wrtur2xy = 0.0_wp
-             wrgrav2  = pi / 8.0_wp * ( winf(j) - winf(i) )**2
-             wrfin    = SQRT( ( 2.0_wp / pi ) * ( wrtur2xy + wrgrav2) )
-
-!
-!--          Calculate radial distribution function (grfin)
-             IF ( st(j) > st(i) )  THEN
-                sst = st(j)
-             ELSE
-                sst = st(i)
-             ENDIF
-
-             xx = -0.1988_wp * sst**4 + 1.5275_wp * sst**3 - 4.2942_wp *       &
-                   sst**2 + 5.3406_wp * sst
-             IF ( xx < 0.0_wp )  xx = 0.0_wp
-             yy = 0.1886_wp * EXP( 20.306_wp / lambda_re )
-
-             c1_gr  =  xx / ( g / vk * tauk )**yy
-
-             ao_gr  = ao + ( pi / 8.0_wp) * ( g / vk * tauk )**2
-             fao_gr = 20.115_wp * SQRT( ao_gr / lambda_re )
-             rc     = SQRT( fao_gr * ABS( st(j) - st(i) ) ) * eta
-
-             grfin  = ( ( eta**2 + rc**2 ) / ( rrp**2 + rc**2) )**( c1_gr*0.5_wp )
-             IF ( grfin < 1.0_wp )  grfin = 1.0_wp
-
-!
-!--          Calculate general collection kernel (without the consideration of
-!--          collection efficiencies)
-             gck(i,j) = 2.0_wp * pi * rrp**2 * wrfin * grfin
-             gck(j,i) = gck(i,j)
-
-          ENDDO
-       ENDDO
-
-    END SUBROUTINE turbsd
-
-    REAL(wp) FUNCTION phi_w( a, b, vsett, tau0 )
-!
-!--    Function used in the Ayala et al. (2008) analytical model for turbulent
-!--    effects on the collision kernel
-       IMPLICIT NONE
-
-       REAL(wp) ::  a     !<
-       REAL(wp) ::  aa1   !<
-       REAL(wp) ::  b     !<
-       REAL(wp) ::  tau0  !<
-       REAL(wp) ::  vsett !<
-
-       aa1 = 1.0_wp / tau0 + 1.0_wp / a + vsett / b
-       phi_w = 1.0_wp / aa1  - 0.5_wp * vsett / b / aa1**2
-
-    END FUNCTION phi_w
-
-    REAL(wp) FUNCTION zhi( a, b, vsett1, tau1, vsett2, tau2 )
-!
-!--    Function used in the Ayala et al. (2008) analytical model for turbulent
-!--    effects on the collision kernel
-       IMPLICIT NONE
-
-       REAL(wp) ::  a      !<
-       REAL(wp) ::  aa1    !<
-       REAL(wp) ::  aa2    !<
-       REAL(wp) ::  aa3    !<
-       REAL(wp) ::  aa4    !<
-       REAL(wp) ::  aa5    !<
-       REAL(wp) ::  aa6    !<
-       REAL(wp) ::  b      !<
-       REAL(wp) ::  tau1   !<
-       REAL(wp) ::  tau2   !<
-       REAL(wp) ::  vsett1 !<
-       REAL(wp) ::  vsett2 !<
-
-       aa1 = vsett2 / b - 1.0_wp / tau2 - 1.0_wp / a
-       aa2 = vsett1 / b + 1.0_wp / tau1 + 1.0_wp / a
-       aa3 = ( vsett1 - vsett2 ) / b + 1.0_wp / tau1 + 1.0_wp / tau2
-       aa4 = ( vsett2 / b )**2 - ( 1.0_wp / tau2 + 1.0_wp / a )**2
-       aa5 = vsett2 / b + 1.0_wp / tau2 + 1.0_wp / a
-       aa6 = 1.0_wp / tau1 - 1.0_wp / a + ( 1.0_wp / tau2 + 1.0_wp / a) *      &
-             vsett1 / vsett2
-       zhi = (1.0_wp / aa1 - 1.0_wp / aa2 ) * ( vsett1 - vsett2 ) * 0.5_wp /   &
-             b / aa3**2 + ( 4.0_wp / aa4 - 1.0_wp / aa5**2 - 1.0_wp / aa1**2 ) &
-             * vsett2 * 0.5_wp / b /aa6 + ( 2.0_wp * ( b / aa2 - b / aa1 ) -   &
-             vsett1 / aa2**2 + vsett2 / aa1**2 ) * 0.5_wp / b / aa3
-
-    END FUNCTION zhi
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Parameterization of terminal velocity following Rogers et al. (1993, J. Appl.
-!> Meteorol.)
-!------------------------------------------------------------------------------!
-    SUBROUTINE fallg
-
-       USE particle_attributes,                                                &
-           ONLY:  radius_classes
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  j                            !<
-
-       REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp    !< parameter
-       REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp   !< parameter
-       REAL(wp), PARAMETER ::  a_rog     = 9.65_wp   !< parameter
-       REAL(wp), PARAMETER ::  b_rog     = 10.43_wp  !< parameter
-       REAL(wp), PARAMETER ::  c_rog     = 0.6_wp    !< parameter
-       REAL(wp), PARAMETER ::  d0_rog    = 0.745_wp  !< seperation diameter
-
-       REAL(wp)            ::  diameter              !< droplet diameter in mm
-
-
-       DO  j = 1, radius_classes
-
-          diameter = radclass(j) * 2000.0_wp
-
-          IF ( diameter <= d0_rog )  THEN
-             winf(j) = k_cap_rog * diameter * ( 1.0_wp -                       &
-                                                EXP( -k_low_rog * diameter ) )
-          ELSE
-             winf(j) = a_rog - b_rog * EXP( -c_rog * diameter )
-          ENDIF
-
-       ENDDO
-
-    END SUBROUTINE fallg
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Interpolation of collision efficiencies (Hall, 1980, J. Atmos. Sci.)
-!------------------------------------------------------------------------------!
-    SUBROUTINE effic
- 
-       USE particle_attributes,                                                &
-           ONLY:  radius_classes
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i  !<
-       INTEGER(iwp) ::  iq !<
-       INTEGER(iwp) ::  ir !<
-       INTEGER(iwp) ::  j  !<
-       INTEGER(iwp) ::  k  !<
-
-       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ira !<
-
-       LOGICAL, SAVE ::  first = .TRUE. !<
-
-       REAL(wp) ::  ek              !<
-       REAL(wp) ::  particle_radius !<
-       REAL(wp) ::  pp              !<
-       REAL(wp) ::  qq              !<
-       REAL(wp) ::  rq              !<
-
-       REAL(wp), DIMENSION(1:21), SAVE ::  rat        !<
-       
-       REAL(wp), DIMENSION(1:15), SAVE ::  r0         !<
-       
-       REAL(wp), DIMENSION(1:15,1:21), SAVE ::  ecoll !<
-
-!
-!--    Initial assignment of constants
-       IF ( first )  THEN
-
-         first = .FALSE.
-         r0  = (/   6.0_wp,   8.0_wp,  10.0_wp, 15.0_wp,  20.0_wp,  25.0_wp,   &
-                   30.0_wp,  40.0_wp,  50.0_wp, 60.0_wp,  70.0_wp, 100.0_wp,   &
-                  150.0_wp, 200.0_wp, 300.0_wp /)
-
-         rat = (/ 0.00_wp, 0.05_wp, 0.10_wp, 0.15_wp, 0.20_wp, 0.25_wp,        &
-                  0.30_wp, 0.35_wp, 0.40_wp, 0.45_wp, 0.50_wp, 0.55_wp,        &
-                  0.60_wp, 0.65_wp, 0.70_wp, 0.75_wp, 0.80_wp, 0.85_wp,        &
-                  0.90_wp, 0.95_wp, 1.00_wp /)
-
-         ecoll(:,1)  = (/ 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp,    &
-                          0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp,    &
-                          0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp, 0.001_wp /)
-         ecoll(:,2)  = (/ 0.003_wp, 0.003_wp, 0.003_wp, 0.004_wp, 0.005_wp,    &
-                          0.005_wp, 0.005_wp, 0.010_wp, 0.100_wp, 0.050_wp,    &
-                          0.200_wp, 0.500_wp, 0.770_wp, 0.870_wp, 0.970_wp /)
-         ecoll(:,3)  = (/ 0.007_wp, 0.007_wp, 0.007_wp, 0.008_wp, 0.009_wp,    &
-                          0.010_wp, 0.010_wp, 0.070_wp, 0.400_wp, 0.430_wp,    &
-                          0.580_wp, 0.790_wp, 0.930_wp, 0.960_wp, 1.000_wp /)
-         ecoll(:,4)  = (/ 0.009_wp, 0.009_wp, 0.009_wp, 0.012_wp, 0.015_wp,    &
-                          0.010_wp, 0.020_wp, 0.280_wp, 0.600_wp, 0.640_wp,    &
-                          0.750_wp, 0.910_wp, 0.970_wp, 0.980_wp, 1.000_wp /)
-         ecoll(:,5)  = (/ 0.014_wp, 0.014_wp, 0.014_wp, 0.015_wp, 0.016_wp,    &
-                          0.030_wp, 0.060_wp, 0.500_wp, 0.700_wp, 0.770_wp,    &
-                          0.840_wp, 0.950_wp, 0.970_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,6)  = (/ 0.017_wp, 0.017_wp, 0.017_wp, 0.020_wp, 0.022_wp,    &
-                          0.060_wp, 0.100_wp, 0.620_wp, 0.780_wp, 0.840_wp,    &
-                          0.880_wp, 0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,7)  = (/ 0.030_wp, 0.030_wp, 0.024_wp, 0.022_wp, 0.032_wp,    &
-                          0.062_wp, 0.200_wp, 0.680_wp, 0.830_wp, 0.870_wp,    &
-                          0.900_wp, 0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,8)  = (/ 0.025_wp, 0.025_wp, 0.025_wp, 0.036_wp, 0.043_wp,    &
-                          0.130_wp, 0.270_wp, 0.740_wp, 0.860_wp, 0.890_wp,    &
-                          0.920_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,9)  = (/ 0.027_wp, 0.027_wp, 0.027_wp, 0.040_wp, 0.052_wp,    &
-                          0.200_wp, 0.400_wp, 0.780_wp, 0.880_wp, 0.900_wp,    &
-                          0.940_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,10) = (/ 0.030_wp, 0.030_wp, 0.030_wp, 0.047_wp, 0.064_wp,    &
-                          0.250_wp, 0.500_wp, 0.800_wp, 0.900_wp, 0.910_wp,    &
-                          0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,11) = (/ 0.040_wp, 0.040_wp, 0.033_wp, 0.037_wp, 0.068_wp,    &
-                          0.240_wp, 0.550_wp, 0.800_wp, 0.900_wp, 0.910_wp,    &
-                          0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,12) = (/ 0.035_wp, 0.035_wp, 0.035_wp, 0.055_wp, 0.079_wp,    &
-                          0.290_wp, 0.580_wp, 0.800_wp, 0.900_wp, 0.910_wp,    &
-                          0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,13) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.062_wp, 0.082_wp,    &
-                          0.290_wp, 0.590_wp, 0.780_wp, 0.900_wp, 0.910_wp,    &
-                          0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,14) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.060_wp, 0.080_wp,    &
-                          0.290_wp, 0.580_wp, 0.770_wp, 0.890_wp, 0.910_wp,    &
-                          0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,15) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.041_wp, 0.075_wp,    &
-                          0.250_wp, 0.540_wp, 0.760_wp, 0.880_wp, 0.920_wp,    &
-                          0.950_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,16) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.052_wp, 0.067_wp,    &
-                          0.250_wp, 0.510_wp, 0.770_wp, 0.880_wp, 0.930_wp,    &
-                          0.970_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,17) = (/ 0.037_wp, 0.037_wp, 0.037_wp, 0.047_wp, 0.057_wp,    &
-                          0.250_wp, 0.490_wp, 0.770_wp, 0.890_wp, 0.950_wp,    &
-                          1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp, 1.000_wp /)
-         ecoll(:,18) = (/ 0.036_wp, 0.036_wp, 0.036_wp, 0.042_wp, 0.048_wp,    &
-                          0.230_wp, 0.470_wp, 0.780_wp, 0.920_wp, 1.000_wp,    &
-                          1.020_wp, 1.020_wp, 1.020_wp, 1.020_wp, 1.020_wp /)
-         ecoll(:,19) = (/ 0.040_wp, 0.040_wp, 0.035_wp, 0.033_wp, 0.040_wp,    &
-                          0.112_wp, 0.450_wp, 0.790_wp, 1.010_wp, 1.030_wp,    &
-                          1.040_wp, 1.040_wp, 1.040_wp, 1.040_wp, 1.040_wp /)
-         ecoll(:,20) = (/ 0.033_wp, 0.033_wp, 0.033_wp, 0.033_wp, 0.033_wp,    &
-                          0.119_wp, 0.470_wp, 0.950_wp, 1.300_wp, 1.700_wp,    &
-                          2.300_wp, 2.300_wp, 2.300_wp, 2.300_wp, 2.300_wp /)
-         ecoll(:,21) = (/ 0.027_wp, 0.027_wp, 0.027_wp, 0.027_wp, 0.027_wp,    &
-                          0.125_wp, 0.520_wp, 1.400_wp, 2.300_wp, 3.000_wp,    &
-                          4.000_wp, 4.000_wp, 4.000_wp, 4.000_wp, 4.000_wp /)
-       ENDIF
-
-!
-!--    Calculate the radius class index of particles with respect to array r
-!--    Radius has to be in microns
-       ALLOCATE( ira(1:radius_classes) )
-       DO  j = 1, radius_classes
-          particle_radius = radclass(j) * 1.0E6_wp
-          DO  k = 1, 15
-             IF ( particle_radius < r0(k) )  THEN
-                ira(j) = k
-                EXIT
-             ENDIF
-          ENDDO
-          IF ( particle_radius >= r0(15) )  ira(j) = 16
-       ENDDO
-
-!
-!--    Two-dimensional linear interpolation of the collision efficiency.
-!--    Radius has to be in microns
-       DO  j = 1, radius_classes
-          DO  i = 1, j
-
-             ir = MAX( ira(i), ira(j) )
-             rq = MIN( radclass(i) / radclass(j), radclass(j) / radclass(i) )
-             iq = INT( rq * 20 ) + 1
-             iq = MAX( iq , 2)
-
-             IF ( ir < 16 )  THEN
-                IF ( ir >= 2 )  THEN
-                   pp = ( ( MAX( radclass(j), radclass(i) ) * 1.0E6_wp ) -     &
-                          r0(ir-1) ) / ( r0(ir) - r0(ir-1) )
-                   qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
-                   ec(j,i) = ( 1.0_wp - pp ) * ( 1.0_wp - qq )                 &
-                             * ecoll(ir-1,iq-1)                                &
-                             + pp * ( 1.0_wp - qq ) * ecoll(ir,iq-1)           &
-                             + qq * ( 1.0_wp - pp ) * ecoll(ir-1,iq)           &
-                             + pp * qq * ecoll(ir,iq)
-                ELSE
-                   qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
-                   ec(j,i) = ( 1.0_wp - qq ) * ecoll(1,iq-1) + qq * ecoll(1,iq)
-                ENDIF
-             ELSE
-                qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
-                ek = ( 1.0_wp - qq ) * ecoll(15,iq-1) + qq * ecoll(15,iq)
-                ec(j,i) = MIN( ek, 1.0_wp )
-             ENDIF
-
-             IF ( ec(j,i) < 1.0E-20_wp )  ec(j,i) = 0.0_wp
-
-             ec(i,j) = ec(j,i)
-
-          ENDDO
-       ENDDO
-
-       DEALLOCATE( ira )
-
-    END SUBROUTINE effic
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Interpolation of turbulent enhancement factor for collision efficencies
-!> following Wang and Grabowski (2009, Atmos. Sci. Let.)
-!------------------------------------------------------------------------------!
-    SUBROUTINE turb_enhance_eff
-
-       USE particle_attributes,                                                &
-           ONLY:  radius_classes
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) :: i  !<
-       INTEGER(iwp) :: iq !<
-       INTEGER(iwp) :: ir !<
-       INTEGER(iwp) :: j  !<
-       INTEGER(iwp) :: k  !<
-       INTEGER(iwp) :: kk !<
-
-       INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  ira !<
-       
-       LOGICAL, SAVE ::  first = .TRUE. !<
-
-       REAL(wp) ::  particle_radius !<
-       REAL(wp) ::  pp              !<
-       REAL(wp) ::  qq              !<
-       REAL(wp) ::  rq              !<
-       REAL(wp) ::  y1              !<
-       REAL(wp) ::  y2              !<
-       REAL(wp) ::  y3              !<
-
-       REAL(wp), DIMENSION(1:11), SAVE ::  rat           !<
-       REAL(wp), DIMENSION(1:7), SAVE  ::  r0            !<
-       
-       REAL(wp), DIMENSION(1:7,1:11), SAVE ::  ecoll_100 !<
-       REAL(wp), DIMENSION(1:7,1:11), SAVE ::  ecoll_400 !<
-
-!
-!--    Initial assignment of constants
-       IF ( first )  THEN
-
-          first = .FALSE.
-
-          r0  = (/  10.0_wp, 20.0_wp, 30.0_wp, 40.0_wp, 50.0_wp, 60.0_wp,  &
-                   100.0_wp /)
-
-          rat = (/ 0.0_wp, 0.1_wp, 0.2_wp, 0.3_wp, 0.4_wp, 0.5_wp, 0.6_wp, &
-                   0.7_wp, 0.8_wp, 0.9_wp, 1.0_wp /)
-!
-!--       Tabulated turbulent enhancement factor at 100 cm**2/s**3
-          ecoll_100(:,1)  = (/  1.74_wp,   1.74_wp,   1.773_wp, 1.49_wp,  &
-                                1.207_wp,  1.207_wp,  1.0_wp /)
-          ecoll_100(:,2)  = (/  1.46_wp,   1.46_wp,   1.421_wp, 1.245_wp, &
-                                1.069_wp,  1.069_wp,  1.0_wp /)
-          ecoll_100(:,3)  = (/  1.32_wp,   1.32_wp,   1.245_wp, 1.123_wp, &
-                                1.000_wp,  1.000_wp,  1.0_wp /)
-          ecoll_100(:,4)  = (/  1.250_wp,  1.250_wp,  1.148_wp, 1.087_wp, &
-                                1.025_wp,  1.025_wp,  1.0_wp /)
-          ecoll_100(:,5)  = (/  1.186_wp,  1.186_wp,  1.066_wp, 1.060_wp, &
-                                1.056_wp,  1.056_wp,  1.0_wp /)
-          ecoll_100(:,6)  = (/  1.045_wp,  1.045_wp,  1.000_wp, 1.014_wp, &
-                                1.028_wp,  1.028_wp,  1.0_wp /)
-          ecoll_100(:,7)  = (/  1.070_wp,  1.070_wp,  1.030_wp, 1.038_wp, &
-                                1.046_wp,  1.046_wp,  1.0_wp /)
-          ecoll_100(:,8)  = (/  1.000_wp,  1.000_wp,  1.054_wp, 1.042_wp, &
-                                1.029_wp,  1.029_wp,  1.0_wp /)
-          ecoll_100(:,9)  = (/  1.223_wp,  1.223_wp,  1.117_wp, 1.069_wp, &
-                                1.021_wp,  1.021_wp,  1.0_wp /)
-          ecoll_100(:,10) = (/  1.570_wp,  1.570_wp,  1.244_wp, 1.166_wp, &
-                                1.088_wp,  1.088_wp,  1.0_wp /)
-          ecoll_100(:,11) = (/ 20.3_wp,   20.3_wp,   14.6_wp,   8.61_wp,  &
-                                2.60_wp,   2.60_wp,   1.0_wp /)
-!
-!--       Tabulated turbulent enhancement factor at 400 cm**2/s**3
-          ecoll_400(:,1)  = (/  4.976_wp,  4.976_wp,  3.593_wp,  2.519_wp, &
-                                1.445_wp,  1.445_wp,  1.0_wp /)
-          ecoll_400(:,2)  = (/  2.984_wp,  2.984_wp,  2.181_wp,  1.691_wp, &
-                                1.201_wp,  1.201_wp,  1.0_wp /)
-          ecoll_400(:,3)  = (/  1.988_wp,  1.988_wp,  1.475_wp,  1.313_wp, &
-                                1.150_wp,  1.150_wp,  1.0_wp /)
-          ecoll_400(:,4)  = (/  1.490_wp,  1.490_wp,  1.187_wp,  1.156_wp, &
-                                1.126_wp,  1.126_wp,  1.0_wp /)
-          ecoll_400(:,5)  = (/  1.249_wp,  1.249_wp,  1.088_wp,  1.090_wp, &
-                                1.092_wp,  1.092_wp,  1.0_wp /)
-          ecoll_400(:,6)  = (/  1.139_wp,  1.139_wp,  1.130_wp,  1.091_wp, &
-                                1.051_wp,  1.051_wp,  1.0_wp /)
-          ecoll_400(:,7)  = (/  1.220_wp,  1.220_wp,  1.190_wp,  1.138_wp, &
-                                1.086_wp,  1.086_wp,  1.0_wp /)
-          ecoll_400(:,8)  = (/  1.325_wp,  1.325_wp,  1.267_wp,  1.165_wp, &
-                                1.063_wp,  1.063_wp,  1.0_wp /)
-          ecoll_400(:,9)  = (/  1.716_wp,  1.716_wp,  1.345_wp,  1.223_wp, &
-                                1.100_wp,  1.100_wp,  1.0_wp /)
-          ecoll_400(:,10) = (/  3.788_wp,  3.788_wp,  1.501_wp,  1.311_wp, &
-                                1.120_wp,  1.120_wp,  1.0_wp /)
-          ecoll_400(:,11) = (/ 36.52_wp,  36.52_wp,  19.16_wp,  22.80_wp,  &
-                               26.0_wp,   26.0_wp,    1.0_wp /)
-
-       ENDIF
-
-!
-!--    Calculate the radius class index of particles with respect to array r0
-!--    The droplet radius has to be given in microns.
-       ALLOCATE( ira(1:radius_classes) )
-
-       DO  j = 1, radius_classes
-          particle_radius = radclass(j) * 1.0E6_wp
-          DO  k = 1, 7
-             IF ( particle_radius < r0(k) )  THEN
-                ira(j) = k
-                EXIT
-             ENDIF
-          ENDDO
-          IF ( particle_radius >= r0(7) )  ira(j) = 8
-       ENDDO
-
-!
-!--    Two-dimensional linear interpolation of the turbulent enhancement factor.
-!--    The droplet radius has to be given in microns.
-       DO  j =  1, radius_classes
-          DO  i = 1, j
-
-             ir = MAX( ira(i), ira(j) )
-             rq = MIN( radclass(i) / radclass(j), radclass(j) / radclass(i) )
-
-             DO  kk = 2, 11
-                IF ( rq <= rat(kk) )  THEN
-                   iq = kk
-                   EXIT
-                ENDIF
-             ENDDO
-
-             y1 = 1.0_wp  ! turbulent enhancement factor at 0 m**2/s**3
-
-             IF ( ir < 8 )  THEN
-                IF ( ir >= 2 )  THEN
-                   pp = ( MAX( radclass(j), radclass(i) ) * 1.0E6_wp -  &
-                          r0(ir-1) ) / ( r0(ir) - r0(ir-1) )
-                   qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
-                   y2 = ( 1.0_wp - pp ) * ( 1.0_wp - qq ) * ecoll_100(ir-1,iq-1) + &
-                                pp * ( 1.0_wp - qq ) * ecoll_100(ir,iq-1)        + &
-                                qq * ( 1.0_wp - pp ) * ecoll_100(ir-1,iq)        + &
-                                pp * qq              * ecoll_100(ir,iq)
-                   y3 = ( 1.0-pp ) * ( 1.0_wp - qq ) * ecoll_400(ir-1,iq-1)      + &
-                                pp * ( 1.0_wp - qq ) * ecoll_400(ir,iq-1)        + &
-                                qq * ( 1.0_wp - pp ) * ecoll_400(ir-1,iq)        + &
-                                pp * qq              * ecoll_400(ir,iq)
-                ELSE
-                   qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
-                   y2 = ( 1.0_wp - qq ) * ecoll_100(1,iq-1) + qq * ecoll_100(1,iq)
-                   y3 = ( 1.0_wp - qq ) * ecoll_400(1,iq-1) + qq * ecoll_400(1,iq)
-                ENDIF
-             ELSE
-                qq = ( rq - rat(iq-1) ) / ( rat(iq) - rat(iq-1) )
-                y2 = ( 1.0_wp - qq ) * ecoll_100(7,iq-1) + qq * ecoll_100(7,iq)
-                y3 = ( 1.0_wp - qq ) * ecoll_400(7,iq-1) + qq * ecoll_400(7,iq)
-             ENDIF
-!
-!--          Linear interpolation of turbulent enhancement factor
-             IF ( epsilon <= 0.01_wp )  THEN
-                ecf(j,i) = ( epsilon - 0.01_wp ) / ( 0.0_wp  - 0.01_wp ) * y1 &
-                         + ( epsilon - 0.0_wp  ) / ( 0.01_wp - 0.0_wp  ) * y2
-             ELSEIF ( epsilon <= 0.06_wp )  THEN
-                ecf(j,i) = ( epsilon - 0.04_wp ) / ( 0.01_wp - 0.04_wp ) * y2 &
-                         + ( epsilon - 0.01_wp ) / ( 0.04_wp - 0.01_wp ) * y3
-             ELSE
-                ecf(j,i) = ( 0.06_wp - 0.04_wp ) / ( 0.01_wp - 0.04_wp ) * y2 &
-                         + ( 0.06_wp - 0.01_wp ) / ( 0.04_wp - 0.01_wp ) * y3
-             ENDIF
-
-             IF ( ecf(j,i) < 1.0_wp )  ecf(j,i) = 1.0_wp
-
-             ecf(i,j) = ecf(j,i)
-
-          ENDDO
-       ENDDO
-
-    END SUBROUTINE turb_enhance_eff
-
- END MODULE lpm_collision_kernels_mod
Index: palm/trunk/SOURCE/lpm_data_output_particles.f90
===================================================================
--- palm/trunk/SOURCE/lpm_data_output_particles.f90	(revision 4015)
+++ 	(revision )
@@ -1,233 +1,0 @@
-!> @file lpm_data_output_particles.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2123 2017-01-18 12:34:59Z hoffmann
-!
-! 2122 2017-01-18 12:22:54Z hoffmann
-! Calculation of particle ID
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed. Unused variables removed.
-!
-! 1783 2016-03-06 18:36:17Z raasch
-! name change of netcdf routines and module + related changes
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! netCDF output currently not available
-! output of particle data in binary format adopted to new particle structure
-! 
-! 1327 2014-03-21 11:00:16Z raasch
-! -netcdf output queries
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! revision history before 2012 removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-! 22/02/12 - Initial version
-!
-! Description:
-! ------------
-!> Write particle data in FORTRAN binary and/or netCDF format 
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_data_output_particles
- 
-
-    USE control_parameters,                                                    &
-        ONLY:  simulated_time
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  grid_particles, number_of_particles,  particles, prt_count
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  ip !<
-    INTEGER(iwp) ::  jp !<
-    INTEGER(iwp) ::  kp !<
-
-    CALL cpu_log( log_point_s(40), 'lpm_data_output', 'start' )
-
-!
-!-- Attention: change version number for unit 85 (in routine check_open)
-!--            whenever the output format for this unit is changed!
-    CALL check_open( 85 )
-
-    WRITE ( 85 )  simulated_time
-    WRITE ( 85 )  prt_count
-          
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-             IF ( number_of_particles <= 0 )  CYCLE
-             WRITE ( 85 )  particles
-          ENDDO
-       ENDDO
-    ENDDO
-
-    CALL close_file( 85 )
-
-
-#if defined( __netcdf )
-! !
-! !-- Output in netCDF format
-!     CALL check_open( 108 )
-! 
-! !
-! !-- Update the NetCDF time axis
-!     prt_time_count = prt_time_count + 1
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_time_prt, &
-!                             (/ simulated_time /),        &
-!                             start = (/ prt_time_count /), count = (/ 1 /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 1 )
-! 
-! !
-! !-- Output the real number of particles used
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_rnop_prt, &
-!                             (/ number_of_particles /),   &
-!                             start = (/ prt_time_count /), count = (/ 1 /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 2 )
-! 
-! !
-! !-- Output all particle attributes
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(1), particles%age,      &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 3 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(2), particles%user,     &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 4 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(3), particles%origin_x, &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 5 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(4), particles%origin_y, &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 6 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(5), particles%origin_z, &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 7 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(6), particles%radius,   &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 8 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(7), particles%speed_x,  &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 9 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(8), particles%speed_y,  &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 10 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(9), particles%speed_z,  &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 11 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt,id_var_prt(10),                     &
-!                             particles%weight_factor,                       &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 12 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(11), particles%x,       &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 13 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(12), particles%y,       &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 14 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(13), particles%z,       &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 15 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(14), particles%class,   &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 16 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(15), particles%group,   &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 17 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(16),                    &
-!                             particles%id2,                                 &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 18 )
-! 
-!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(17), particles%id1,     &
-!                             start = (/ 1, prt_time_count /),               &
-!                             count = (/ maximum_number_of_particles /) )
-!     CALL netcdf_handle_error( 'lpm_data_output_particles', 19 )
-! 
-#endif
-
-    CALL cpu_log( log_point_s(40), 'lpm_data_output', 'stop' )
-
- END SUBROUTINE lpm_data_output_particles
Index: palm/trunk/SOURCE/lpm_droplet_collision.f90
===================================================================
--- palm/trunk/SOURCE/lpm_droplet_collision.f90	(revision 4015)
+++ 	(revision )
@@ -1,372 +1,0 @@
-!> @file lpm_droplet_collision.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Modularization of all bulk cloud physics code components
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3039 2018-05-24 13:13:11Z schwenkel
-! bugfix for lcm with grid stretching
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2375 2017-08-29 14:10:28Z schwenkel
-! Changed ONLY-dependencies
-!
-! 2312 2017-07-14 20:26:51Z hoffmann
-! Consideration of aerosol mass during collision. Average impact algorithm has
-! been removed.
-!
-! 2274 2017-06-09 13:27:48Z Giersch
-! Changed error messages
-!
-! 2123 2017-01-18 12:34:59Z hoffmann
-!
-! 2122 2017-01-18 12:22:54Z hoffmann
-! Some reformatting of the code.
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1884 2016-04-21 11:11:40Z hoffmann
-! Conservation of mass should only be checked if collisions took place.
-!
-! 1860 2016-04-13 13:21:28Z hoffmann
-! Interpolation of dissipation rate adjusted to more reasonable values.
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Integration of a new collision algortithm based on Shima et al. (2009) and
-! Soelch and Kaercher (2010) called all_or_nothing. The previous implemented
-! collision algorithm is called average_impact. Moreover, both algorithms are
-! now positive definit due to their construction, i.e., no negative weighting
-! factors should occur.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-! Kind definition added to all floating point numbers.
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! REAL constants defined as wp_kind
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1092 2013-02-02 11:24:22Z raasch
-! unused variables removed
-!
-! 1071 2012-11-29 16:54:55Z franke
-! Calculation of Hall and Wang kernel now uses collision-coalescence formulation
-! proposed by Wang instead of the continuous collection equation (for more
-! information about new method see PALM documentation)
-! Bugfix: message identifiers added
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Calculates change in droplet radius by collision. Droplet collision is
-!> calculated for each grid box seperately. Collision is parameterized by
-!> using collision kernels. Two different kernels are available:
-!> Hall kernel: Kernel from Hall (1980, J. Atmos. Sci., 2486-2507), which
-!>              considers collision due to pure gravitational effects.
-!> Wang kernel: Beside gravitational effects (treated with the Hall-kernel) also
-!>              the effects of turbulence on the collision are considered using
-!>              parameterizations of Ayala et al. (2008, New J. Phys., 10,
-!>              075015) and Wang and Grabowski (2009, Atmos. Sci. Lett., 10,
-!>              1-8). This kernel includes three possible effects of turbulence:
-!>              the modification of the relative velocity between the droplets,
-!>              the effect of preferential concentration, and the enhancement of
-!>              collision efficiencies.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_droplet_collision (i,j,k)
-
-    USE arrays_3d,                                                             &
-        ONLY:  diss, dzw, ql_v, ql_vp
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  pi, rho_l, rho_s
-
-    USE control_parameters,                                                    &
-        ONLY:  dt_3d, message_string
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-
-    USE kinds
-
-    USE lpm_collision_kernels_mod,                                             &
-        ONLY:  ckernel, recalculate_kernel
-
-    USE particle_attributes,                                                   &
-        ONLY:  curvature_solution_effects, dissipation_classes, hall_kernel,   &
-               iran_part, number_of_particles, particles, particle_type,       &
-               prt_count, use_kernel_tables, wang_kernel
-
-    USE random_function_mod,                                                   &
-        ONLY:  random_function
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  eclass   !<
-    INTEGER(iwp) ::  i        !<
-    INTEGER(iwp) ::  j        !<
-    INTEGER(iwp) ::  k        !<
-    INTEGER(iwp) ::  n        !<
-    INTEGER(iwp) ::  m        !<
-    INTEGER(iwp) ::  rclass_l !<
-    INTEGER(iwp) ::  rclass_s !<
-
-    REAL(wp) ::  collection_probability  !< probability for collection
-    REAL(wp) ::  ddV                     !< inverse grid box volume
-    REAL(wp) ::  epsilon                 !< dissipation rate
-    REAL(wp) ::  factor_volume_to_mass   !< 4.0 / 3.0 * pi * rho_l
-    REAL(wp) ::  xm                      !< droplet mass of super-droplet m
-    REAL(wp) ::  xn                      !< droplet mass of super-droplet n
-    REAL(wp) ::  xsm                     !< aerosol mass of super-droplet m
-    REAL(wp) ::  xsn                     !< aerosol mass of super-droplet n
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  weight    !< weighting factor
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  mass      !< total mass of super droplet
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  aero_mass !< total aerosol mass of super droplet
-
-    CALL cpu_log( log_point_s(43), 'lpm_droplet_coll', 'start' )
-
-    number_of_particles   = prt_count(k,j,i)
-    factor_volume_to_mass = 4.0_wp / 3.0_wp * pi * rho_l
-    ddV                   = 1.0_wp / ( dx * dy * dzw(k) )
-!
-!-- Collision requires at least one super droplet inside the box
-    IF ( number_of_particles > 0 )  THEN
-
-       IF ( use_kernel_tables )  THEN
-!
-!--       Fast method with pre-calculated collection kernels for
-!--       discrete radius- and dissipation-classes.
-          IF ( wang_kernel )  THEN
-             eclass = INT( diss(k,j,i) * 1.0E4_wp / 600.0_wp * &
-                           dissipation_classes ) + 1
-             epsilon = diss(k,j,i)
-          ELSE
-             epsilon = 0.0_wp
-          ENDIF
-
-          IF ( hall_kernel  .OR.  epsilon * 1.0E4_wp < 0.001_wp )  THEN
-             eclass = 0   ! Hall kernel is used
-          ELSE
-             eclass = MIN( dissipation_classes, eclass )
-          ENDIF
-
-       ELSE
-!
-!--       Collection kernels are re-calculated for every new
-!--       grid box. First, allocate memory for kernel table.
-!--       Third dimension is 1, because table is re-calculated for
-!--       every new dissipation value.
-          ALLOCATE( ckernel(1:number_of_particles,1:number_of_particles,1:1) )
-!
-!--       Now calculate collection kernel for this box. Note that
-!--       the kernel is based on the previous time step
-          CALL recalculate_kernel( i, j, k )
-
-       ENDIF
-!
-!--    Temporary fields for total mass of super-droplet, aerosol mass, and
-!--    weighting factor are allocated.
-       ALLOCATE(mass(1:number_of_particles), weight(1:number_of_particles))
-       IF ( curvature_solution_effects )  ALLOCATE(aero_mass(1:number_of_particles))
-
-       mass(1:number_of_particles)   = particles(1:number_of_particles)%weight_factor * &
-                                       particles(1:number_of_particles)%radius**3     * &
-                                       factor_volume_to_mass
-
-       weight(1:number_of_particles) = particles(1:number_of_particles)%weight_factor
-
-       IF ( curvature_solution_effects )  THEN
-          aero_mass(1:number_of_particles) = particles(1:number_of_particles)%weight_factor * &
-                                             particles(1:number_of_particles)%aux1**3       * &
-                                             4.0 / 3.0 * pi * rho_s
-       ENDIF
-!
-!--    Calculate collision/coalescence
-       DO  n = 1, number_of_particles
-
-          DO  m = n, number_of_particles
-!
-!--          For collisions, the weighting factor of at least one super-droplet
-!--          needs to be larger or equal to one.
-             IF ( MIN( weight(n), weight(m) ) .LT. 1.0 )  CYCLE
-!
-!--          Get mass of individual droplets (aerosols)
-             xn = mass(n) / weight(n)
-             xm = mass(m) / weight(m)
-             IF ( curvature_solution_effects )  THEN
-                xsn = aero_mass(n) / weight(n)
-                xsm = aero_mass(m) / weight(m)
-             ENDIF
-!
-!--          Probability that the necessary collisions take place
-             IF ( use_kernel_tables )  THEN
-                rclass_l = particles(n)%class
-                rclass_s = particles(m)%class
-
-                collection_probability  = MAX( weight(n), weight(m) ) *     &
-                                          ckernel(rclass_l,rclass_s,eclass) * ddV * dt_3d
-             ELSE
-                collection_probability  = MAX( weight(n), weight(m) ) *     &
-                                          ckernel(n,m,1) * ddV * dt_3d
-             ENDIF
-!
-!--          Calculate the number of collections and consider multiple collections.
-!--          (Accordingly, p_crit will be 0.0, 1.0, 2.0, ...)
-             IF ( collection_probability - FLOOR(collection_probability)    &
-                  .GT. random_function( iran_part ) )  THEN
-                collection_probability = FLOOR(collection_probability) + 1.0_wp
-             ELSE
-                collection_probability = FLOOR(collection_probability)
-             ENDIF
-
-             IF ( collection_probability .GT. 0.0 )  THEN
-!
-!--             Super-droplet n collects droplets of super-droplet m
-                IF ( weight(n) .LT. weight(m) )  THEN
-
-                   mass(n)   = mass(n)   + weight(n) * xm * collection_probability
-                   weight(m) = weight(m) - weight(n)      * collection_probability
-                   mass(m)   = mass(m)   - weight(n) * xm * collection_probability
-                   IF ( curvature_solution_effects )  THEN
-                      aero_mass(n) = aero_mass(n) + weight(n) * xsm * collection_probability
-                      aero_mass(m) = aero_mass(m) - weight(n) * xsm * collection_probability
-                   ENDIF
-
-                ELSEIF ( weight(m) .LT. weight(n) )  THEN
-
-                   mass(m)   = mass(m)   + weight(m) * xn * collection_probability
-                   weight(n) = weight(n) - weight(m)      * collection_probability
-                   mass(n)   = mass(n)   - weight(m) * xn * collection_probability
-                   IF ( curvature_solution_effects )  THEN
-                      aero_mass(m) = aero_mass(m) + weight(m) * xsn * collection_probability
-                      aero_mass(n) = aero_mass(n) - weight(m) * xsn * collection_probability
-                   ENDIF
-
-                ELSE
-!
-!--                Collisions of particles of the same weighting factor.
-!--                Particle n collects 1/2 weight(n) droplets of particle m,
-!--                particle m collects 1/2 weight(m) droplets of particle n.
-!--                The total mass mass changes accordingly.
-!--                If n = m, the first half of the droplets coalesces with the
-!--                second half of the droplets; mass is unchanged because
-!--                xm = xn for n = m.
-!--
-!--                Note: For m = n this equation is an approximation only
-!--                valid for weight >> 1 (which is usually the case). The
-!--                approximation is weight(n)-1 = weight(n).
-                   mass(n)   = mass(n)   + 0.5_wp * weight(n) * ( xm - xn )
-                   mass(m)   = mass(m)   + 0.5_wp * weight(m) * ( xn - xm )
-                   IF ( curvature_solution_effects )  THEN
-                      aero_mass(n) = aero_mass(n) + 0.5_wp * weight(n) * ( xsm - xsn )
-                      aero_mass(m) = aero_mass(m) + 0.5_wp * weight(m) * ( xsn - xsm )
-                   ENDIF
-                   weight(n) = weight(n) - 0.5_wp * weight(m)
-                   weight(m) = weight(n)
-
-                ENDIF
-
-             ENDIF
-
-          ENDDO
-
-          ql_vp(k,j,i) = ql_vp(k,j,i) + mass(n) / factor_volume_to_mass
-
-       ENDDO
-
-       IF ( ANY(weight < 0.0_wp) )  THEN
-             WRITE( message_string, * ) 'negative weighting factor'
-             CALL message( 'lpm_droplet_collision', 'PA0028',      &
-                            2, 2, -1, 6, 1 )
-       ENDIF
-
-       particles(1:number_of_particles)%radius = ( mass(1:number_of_particles) /   &
-                                                   ( weight(1:number_of_particles) &
-                                                     * factor_volume_to_mass       &
-                                                   )                               &
-                                                 )**0.33333333333333_wp
-
-       IF ( curvature_solution_effects )  THEN
-          particles(1:number_of_particles)%aux1 = ( aero_mass(1:number_of_particles) / &
-                                                    ( weight(1:number_of_particles)    &
-                                                      * 4.0_wp / 3.0_wp * pi * rho_s   &
-                                                    )                                  &
-                                                  )**0.33333333333333_wp
-       ENDIF
-
-       particles(1:number_of_particles)%weight_factor = weight(1:number_of_particles)
-
-       DEALLOCATE( weight, mass )
-       IF ( curvature_solution_effects )  DEALLOCATE( aero_mass )
-       IF ( .NOT. use_kernel_tables )  DEALLOCATE( ckernel )
-
-!
-!--    Check if LWC is conserved during collision process
-       IF ( ql_v(k,j,i) /= 0.0_wp )  THEN
-          IF ( ql_vp(k,j,i) / ql_v(k,j,i) >= 1.0001_wp  .OR.                      &
-               ql_vp(k,j,i) / ql_v(k,j,i) <= 0.9999_wp )  THEN
-             WRITE( message_string, * ) ' LWC is not conserved during',           &
-                                        ' collision! ',                           &
-                                        ' LWC after condensation: ', ql_v(k,j,i), &
-                                        ' LWC after collision: ', ql_vp(k,j,i)
-             CALL message( 'lpm_droplet_collision', 'PA0040', 2, 2, -1, 6, 1 )
-          ENDIF
-       ENDIF
-
-    ENDIF
-
-    CALL cpu_log( log_point_s(43), 'lpm_droplet_coll', 'stop' )
-
- END SUBROUTINE lpm_droplet_collision
Index: palm/trunk/SOURCE/lpm_droplet_condensation.f90
===================================================================
--- palm/trunk/SOURCE/lpm_droplet_condensation.f90	(revision 4015)
+++ 	(revision )
@@ -1,426 +1,0 @@
-!> @file lpm_droplet_condensation.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Modularization of all bulk cloud physics code components
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3049 2018-05-29 13:52:36Z Giersch
-! Error messages revised
-! 
-! 3045 2018-05-28 07:55:41Z Giersch
-! Error messages revised
-! 
-! 3039 2018-05-24 13:13:11Z schwenkel
-! bugfix for lcm with grid stretching
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2608 2017-11-13 14:04:26Z schwenkel
-! Calculation of magnus equation in external module (diagnostic_quantities_mod).
-! 
-! 2375 2017-08-29 14:10:28Z schwenkel
-! Changed ONLY-dependencies
-!
-! 2312 2017-07-14 20:26:51Z hoffmann
-! Rosenbrock scheme improved. Gas-kinetic effect added.
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1890 2016-04-22 08:52:11Z hoffmann
-! Some improvements of the Rosenbrock method. If the Rosenbrock method needs more
-! than 40 iterations to find a sufficient time setp, the model is not aborted.
-! This might lead to small erros. But they can be assumend as negligible, since
-! the maximum timesetp of the Rosenbrock method after 40 iterations will be
-! smaller than 10^-16 s.
-!
-! 1871 2016-04-15 11:46:09Z hoffmann
-! Initialization of aerosols added.
-!
-! 1849 2016-04-08 11:33:18Z hoffmann
-! Interpolation of supersaturation has been removed because it is not in
-! accordance with the release/depletion of latent heat/water vapor in
-! interaction_droplets_ptq.
-! Calculation of particle Reynolds number has been corrected.
-! eps_ros added from modules.
-!
-! 1831 2016-04-07 13:15:51Z hoffmann
-! curvature_solution_effects moved to particle_attributes
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-! Kind definition added to all floating point numbers.
-!
-! 1346 2014-03-27 13:18:20Z heinze
-! Bugfix: REAL constants provided with KIND-attribute especially in call of
-! intrinsic function like MAX, MIN, SIGN
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! REAL constants defined as wp-kind
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1318 2014-03-17 13:35:16Z raasch
-! module interfaces removed
-!
-! 1092 2013-02-02 11:24:22Z raasch
-! unused variables removed
-!
-! 1071 2012-11-29 16:54:55Z franke
-! Ventilation effect for evaporation of large droplets included
-! Check for unreasonable results included in calculation of Rosenbrock method
-! since physically unlikely results were observed and for the same
-! reason the first internal time step in Rosenbrock method should be < 1.0E02 in
-! case of evaporation
-! Unnecessary calculation of ql_int removed
-! Unnecessary calculations in Rosenbrock method (d2rdt2, drdt_m, dt_ros_last)
-! removed
-! Bugfix: factor in calculation of surface tension changed from 0.00155 to
-! 0.000155
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Calculates change in droplet radius by condensation/evaporation, using
-!> either an analytic formula or by numerically integrating the radius growth
-!> equation including curvature and solution effects using Rosenbrocks method
-!> (see Numerical recipes in FORTRAN, 2nd edition, p. 731).
-!> The analytical formula and growth equation follow those given in
-!> Rogers and Yau (A short course in cloud physics, 3rd edition, p. 102/103).
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_droplet_condensation (ip,jp,kp)
-
-
-    USE arrays_3d,                                                             &
-        ONLY:  dzw, hyp, pt, q, ql_c, ql_v, exner
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  l_v, molecular_weight_of_solute, molecular_weight_of_water,     &
-               magnus, pi, rho_l, rho_s, rd_d_rv, r_v, vanthoff
-
-    USE control_parameters,                                                    &
-        ONLY:  dt_3d, message_string, molecular_viscosity, rho_surface
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-
-    USE lpm_collision_kernels_mod,                                             &
-        ONLY:  rclass_lbound, rclass_ubound
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  curvature_solution_effects, number_of_particles,   &
-               particles, radius_classes, use_kernel_tables
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) :: ip                         !<
-    INTEGER(iwp) :: jp                         !<
-    INTEGER(iwp) :: kp                         !<
-    INTEGER(iwp) :: n                          !<
-
-    REAL(wp) ::  afactor                       !< curvature effects
-    REAL(wp) ::  arg                           !<
-    REAL(wp) ::  bfactor                       !< solute effects
-    REAL(wp) ::  ddenom                        !<
-    REAL(wp) ::  delta_r                       !<
-    REAL(wp) ::  diameter                      !< diameter of cloud droplets
-    REAL(wp) ::  diff_coeff                    !< diffusivity for water vapor
-    REAL(wp) ::  drdt                          !<
-    REAL(wp) ::  dt_ros                        !<
-    REAL(wp) ::  dt_ros_sum                    !<
-    REAL(wp) ::  d2rdtdr                       !<
-    REAL(wp) ::  e_a                           !< current vapor pressure
-    REAL(wp) ::  e_s                           !< current saturation vapor pressure
-    REAL(wp) ::  error                         !< local truncation error in Rosenbrock
-    REAL(wp) ::  k1                            !<
-    REAL(wp) ::  k2                            !<
-    REAL(wp) ::  r_err                         !< First order estimate of Rosenbrock radius
-    REAL(wp) ::  r_ros                         !< Rosenbrock radius
-    REAL(wp) ::  r_ros_ini                     !< initial Rosenbrock radius
-    REAL(wp) ::  r0                            !< gas-kinetic lengthscale
-    REAL(wp) ::  sigma                         !< surface tension of water
-    REAL(wp) ::  thermal_conductivity          !< thermal conductivity for water
-    REAL(wp) ::  t_int                         !< temperature
-    REAL(wp) ::  w_s                           !< terminal velocity of droplets
-    REAL(wp) ::  re_p                          !< particle Reynolds number
-!
-!-- Parameters for Rosenbrock method (see Verwer et al., 1999)
-    REAL(wp), PARAMETER :: prec = 1.0E-3_wp     !< precision of Rosenbrock solution
-    REAL(wp), PARAMETER :: q_increase = 1.5_wp  !< increase factor in timestep
-    REAL(wp), PARAMETER :: q_decrease = 0.9_wp  !< decrease factor in timestep
-    REAL(wp), PARAMETER :: gamma = 0.292893218814_wp !< = 1.0 - 1.0 / SQRT(2.0)
-!
-!-- Parameters for terminal velocity
-    REAL(wp), PARAMETER ::  a_rog = 9.65_wp      !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  b_rog = 10.43_wp     !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  c_rog = 0.6_wp       !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp   !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp  !< parameter for fall velocity
-    REAL(wp), PARAMETER ::  d0_rog = 0.745_wp    !< separation diameter
-
-    REAL(wp), DIMENSION(number_of_particles) ::  ventilation_effect     !<
-    REAL(wp), DIMENSION(number_of_particles) ::  new_r                  !<
-
-    CALL cpu_log( log_point_s(42), 'lpm_droplet_condens', 'start' )
-
-!
-!-- Absolute temperature
-    t_int = pt(kp,jp,ip) * exner(kp)
-!
-!-- Saturation vapor pressure (Eq. 10 in Bolton, 1980)
-    e_s = magnus( t_int )
-!
-!-- Current vapor pressure
-    e_a = q(kp,jp,ip) * hyp(kp) / ( q(kp,jp,ip) + rd_d_rv )
-!
-!-- Thermal conductivity for water (from Rogers and Yau, Table 7.1)
-    thermal_conductivity = 7.94048E-05_wp * t_int + 0.00227011_wp
-!
-!-- Moldecular diffusivity of water vapor in air (Hall und Pruppacher, 1976)
-    diff_coeff           = 0.211E-4_wp * ( t_int / 273.15_wp )**1.94_wp * &
-                           ( 101325.0_wp / hyp(kp) )
-!
-!-- Lengthscale for gas-kinetic effects (from Mordy, 1959, p. 23):
-    r0 = diff_coeff / 0.036_wp * SQRT( 2.0_wp * pi / ( r_v * t_int ) )
-!
-!-- Calculate effects of heat conductivity and diffusion of water vapor on the
-!-- diffusional growth process (usually known as 1.0 / (F_k + F_d) )
-    ddenom  = 1.0_wp / ( rho_l * r_v * t_int / ( e_s * diff_coeff ) +          &
-                         ( l_v / ( r_v * t_int ) - 1.0_wp ) * rho_l *          &
-                         l_v / ( thermal_conductivity * t_int )                &
-                       )
-    new_r = 0.0_wp
-!
-!-- Determine ventilation effect on evaporation of large drops
-    DO  n = 1, number_of_particles
-
-       IF ( particles(n)%radius >= 4.0E-5_wp  .AND.  e_a / e_s < 1.0_wp )  THEN
-!
-!--       Terminal velocity is computed for vertical direction (Rogers et al.,
-!--       1993, J. Appl. Meteorol.)
-          diameter = particles(n)%radius * 2000.0_wp !diameter in mm
-          IF ( diameter <= d0_rog )  THEN
-             w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
-          ELSE
-             w_s = a_rog - b_rog * EXP( -c_rog * diameter )
-          ENDIF
-!
-!--       Calculate droplet's Reynolds number
-          re_p = 2.0_wp * particles(n)%radius * w_s / molecular_viscosity
-!
-!--       Ventilation coefficient (Rogers and Yau, 1989):
-          IF ( re_p > 2.5_wp )  THEN
-             ventilation_effect(n) = 0.78_wp + 0.28_wp * SQRT( re_p )
-          ELSE
-             ventilation_effect(n) = 1.0_wp + 0.09_wp * re_p
-          ENDIF
-       ELSE
-!
-!--       For small droplets or in supersaturated environments, the ventilation
-!--       effect does not play a role
-          ventilation_effect(n) = 1.0_wp
-       ENDIF
-    ENDDO
-
-    IF( .NOT. curvature_solution_effects ) then
-!
-!--    Use analytic model for diffusional growth including gas-kinetic
-!--    effects (Mordy, 1959) but without the impact of aerosols.
-       DO  n = 1, number_of_particles
-          arg      = ( particles(n)%radius + r0 )**2 + 2.0_wp * dt_3d * ddenom * &
-                                                       ventilation_effect(n) *   &
-                                                       ( e_a / e_s - 1.0_wp )
-          arg      = MAX( arg, ( 0.01E-6 + r0 )**2 )
-          new_r(n) = SQRT( arg ) - r0
-       ENDDO
-
-    ELSE
-!
-!--    Integrate the diffusional growth including gas-kinetic (Mordy, 1959),
-!--    as well as curvature and solute effects (e.g., KÃ¶hler, 1936).
-!
-!--    Curvature effect (afactor) with surface tension (sigma) by Straka (2009)
-       sigma = 0.0761_wp - 0.000155_wp * ( t_int - 273.15_wp )
-!
-!--    Solute effect (afactor)
-       afactor = 2.0_wp * sigma / ( rho_l * r_v * t_int )
-
-       DO  n = 1, number_of_particles
-!
-!--       Solute effect (bfactor)
-          bfactor = vanthoff * rho_s * particles(n)%aux1**3 *                    &
-                    molecular_weight_of_water / ( rho_l * molecular_weight_of_solute )
-
-          dt_ros     = particles(n)%aux2  ! use previously stored Rosenbrock timestep
-          dt_ros_sum = 0.0_wp
-
-          r_ros     = particles(n)%radius  ! initialize Rosenbrock particle radius
-          r_ros_ini = r_ros
-!
-!--       Integrate growth equation using a 2nd-order Rosenbrock method
-!--       (see Verwer et al., 1999, Eq. (3.2)). The Rosenbrock method adjusts
-!--       its with internal timestep to minimize the local truncation error.
-          DO WHILE ( dt_ros_sum < dt_3d )
-
-             dt_ros = MIN( dt_ros, dt_3d - dt_ros_sum )
-
-             DO
-
-                drdt = ddenom * ventilation_effect(n) * ( e_a / e_s - 1.0 -    &
-                                                          afactor / r_ros +    &
-                                                          bfactor / r_ros**3   &
-                                                        ) / ( r_ros + r0 )
-
-                d2rdtdr = -ddenom * ventilation_effect(n) * (                  &
-                                                (e_a / e_s - 1.0) * r_ros**4 - &
-                                                afactor * r0 * r_ros**2 -      &
-                                                2.0 * afactor * r_ros**3 +     &
-                                                3.0 * bfactor * r0 +           &
-                                                4.0 * bfactor * r_ros          &
-                                                            )                  &
-                          / ( r_ros**4 * ( r_ros + r0 )**2 )
-
-                k1    = drdt / ( 1.0 - gamma * dt_ros * d2rdtdr )
-
-                r_ros = MAX(r_ros_ini + k1 * dt_ros, particles(n)%aux1)
-                r_err = r_ros
-
-                drdt  = ddenom * ventilation_effect(n) * ( e_a / e_s - 1.0 -   &
-                                                           afactor / r_ros +   &
-                                                           bfactor / r_ros**3  &
-                                                         ) / ( r_ros + r0 )
-
-                k2 = ( drdt - dt_ros * 2.0 * gamma * d2rdtdr * k1 ) / &
-                     ( 1.0 - dt_ros * gamma * d2rdtdr )
-
-                r_ros = MAX(r_ros_ini + dt_ros * ( 1.5 * k1 + 0.5 * k2), particles(n)%aux1)
-   !
-   !--          Check error of the solution, and reduce dt_ros if necessary.
-                error = ABS(r_err - r_ros) / r_ros
-                IF ( error .GT. prec )  THEN
-                   dt_ros = SQRT( q_decrease * prec / error ) * dt_ros
-                   r_ros  = r_ros_ini
-                ELSE
-                   dt_ros_sum = dt_ros_sum + dt_ros
-                   dt_ros     = q_increase * dt_ros
-                   r_ros_ini  = r_ros
-                   EXIT
-                ENDIF
-
-             END DO
-
-          END DO !Rosenbrock loop
-!
-!--       Store new particle radius
-          new_r(n) = r_ros
-!
-!--       Store internal time step value for next PALM step
-          particles(n)%aux2 = dt_ros
-
-       ENDDO !Particle loop
-
-    ENDIF
-
-    DO  n = 1, number_of_particles
-!
-!--    Sum up the change in liquid water for the respective grid
-!--    box for the computation of the release/depletion of water vapor
-!--    and heat.
-       ql_c(kp,jp,ip) = ql_c(kp,jp,ip) + particles(n)%weight_factor *          &
-                                   rho_l * 1.33333333_wp * pi *                &
-                                   ( new_r(n)**3 - particles(n)%radius**3 ) /  &
-                                   ( rho_surface * dx * dy * dzw(kp) )
-!
-!--    Check if the increase in liqid water is not too big. If this is the case,
-!--    the model timestep might be too long.
-       IF ( ql_c(kp,jp,ip) > 100.0_wp )  THEN
-          WRITE( message_string, * ) 'k=',kp,' j=',jp,' i=',ip,                &
-                       ' ql_c=',ql_c(kp,jp,ip), '&part(',n,')%wf=',            &
-                       particles(n)%weight_factor,' delta_r=',delta_r
-          CALL message( 'lpm_droplet_condensation', 'PA0143', 2, 2, -1, 6, 1 )
-       ENDIF
-!
-!--    Check if the change in the droplet radius is not too big. If this is the
-!--    case, the model timestep might be too long.
-       delta_r = new_r(n) - particles(n)%radius
-       IF ( delta_r < 0.0_wp  .AND. new_r(n) < 0.0_wp )  THEN
-          WRITE( message_string, * ) '#1 k=',kp,' j=',jp,' i=',ip,             &
-                       ' e_s=',e_s, ' e_a=',e_a,' t_int=',t_int,               &
-                       '&delta_r=',delta_r,                                    &
-                       ' particle_radius=',particles(n)%radius
-          CALL message( 'lpm_droplet_condensation', 'PA0144', 2, 2, -1, 6, 1 )
-       ENDIF
-!
-!--    Sum up the total volume of liquid water (needed below for
-!--    re-calculating the weighting factors)
-       ql_v(kp,jp,ip) = ql_v(kp,jp,ip) + particles(n)%weight_factor * new_r(n)**3
-!
-!--    Determine radius class of the particle needed for collision
-       IF ( use_kernel_tables )  THEN
-          particles(n)%class = ( LOG( new_r(n) ) - rclass_lbound ) /           &
-                               ( rclass_ubound - rclass_lbound ) *             &
-                               radius_classes
-          particles(n)%class = MIN( particles(n)%class, radius_classes )
-          particles(n)%class = MAX( particles(n)%class, 1 )
-       ENDIF
- !
- !--   Store new radius to particle features
-       particles(n)%radius = new_r(n)
-
-    ENDDO
-
-    CALL cpu_log( log_point_s(42), 'lpm_droplet_condens', 'stop' )
-
-
- END SUBROUTINE lpm_droplet_condensation
Index: palm/trunk/SOURCE/lpm_exchange_horiz.f90
===================================================================
--- palm/trunk/SOURCE/lpm_exchange_horiz.f90	(revision 4015)
+++ 	(revision )
@@ -1,1323 +1,0 @@
-!> @file lpm_exchange_horiz.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! index bugfix concerning reallocation of particle array
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! dz was replaced by dz(1) to allow for right vertical stretching
-! 
-! 3049 2018-05-29 13:52:36Z Giersch
-! Error messages revised
-! 
-! 3046 2018-05-29 08:02:15Z Giersch
-! Bugfix for gfortran: Replace the function C_SIZEOF with STORAGE_SIZE.
-! Preprocessor directive(__gfortran) for c_sizeof removed.
-! 
-! 2801 2018-02-14 16:01:55Z thiele
-! Introduce particle transfer in nested models.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2710 2017-12-19 12:50:11Z suehring
-! Changes from last commit documented
-! 
-! 2709 2017-12-19 12:49:40Z suehring
-! Bugfix in CFL check.
-! Further bugfix, non-allocated array used in case of 1D decomposition.
-!
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2632 2017-11-20 15:35:52Z schwenkel
-! Bugfix in write statement
-!
-! 2630 2017-11-20 12:58:20Z schwenkel
-! Enabled particle advection with grid stretching. Furthermore, the CFL-
-! criterion is checked for every particle at every time step.
-! 
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! lpm_move_particle now moves inner particles that would leave the core 
-! to the respective boundary gridbox. Afterwards they get transferred by 
-! lpm_exchange_horiz. This allows particles to move more than one gridbox 
-! independent of domain composition.  
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done
-! 
-! 2330 2017-08-03 14:26:02Z schwenkel
-! Bugfix: Also for gfortran compilable, function c_sizeof is not used. 
-! 
-! 2305 2017-07-06 11:18:47Z hoffmann
-! Improved calculation of particle IDs.
-! 
-! 2245 2017-06-02 14:37:10Z schwenkel
-! Bugfix in Add_particles_to_gridcell:
-! Apply boundary conditions also in y-direction
-! 
-! 2151 2017-02-15 10:42:16Z schwenkel
-! Bugfix in lpm_move_particle that prevents
-! false production of additional particles
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1936 2016-06-13 13:37:44Z suehring
-! Deallocation of unused memory
-! 
-! 1929 2016-06-09 16:25:25Z suehring
-! Bugfixes: 
-! - reallocation of new particles 
-!   ( did not work for small number of min_nr_particle )
-! - dynamical reallocation of north-south exchange arrays ( particles got lost )
-! - north-south exchange ( nr_move_north, nr_move_south were overwritten by zero )
-! - horizontal particle boundary conditions in serial mode
-!
-! Remove unused variables
-! Descriptions in variable declaration blocks added
-!
-! 1873 2016-04-18 14:50:06Z maronga
-! Module renamed (removed _mod)
-! 
-! 
-! 1850 2016-04-08 13:29:27Z maronga
-! Module renamed
-!
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed. Unused variables removed.
-!
-! 1783 2016-03-06 18:36:17Z raasch
-! new netcdf-module included
-!
-! 1691 2015-10-26 16:17:44Z maronga
-! Formatting corrections.
-!
-! 1685 2015-10-08 07:32:13Z raasch
-! bugfix concerning vertical index offset in case of ocean
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-! 
-! 1327 2014-03-21 11:00:16Z raasch
-! -netcdf output queries
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1318 2014-03-17 13:35:16Z raasch
-! module interfaces removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 851 2012-03-15 14:32:58Z raasch
-! Bugfix: resetting of particle_mask and tail mask moved from end of this
-! routine to lpm
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Exchange of particles between the subdomains.
-!------------------------------------------------------------------------------!
- MODULE lpm_exchange_horiz_mod
- 
-    USE, INTRINSIC ::  ISO_C_BINDING
-    
-    USE arrays_3d,                                                             &
-       ONLY:  zw
-    
-    USE control_parameters,                                                    &
-        ONLY:  dz, message_string, simulated_time
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE grid_variables,                                                        &
-        ONLY:  ddx, ddy, dx, dy
-
-    USE indices,                                                               &
-        ONLY:  nx, nxl, nxr, ny, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE lpm_pack_and_sort_mod,                                                   &
-        ONLY:  lpm_pack
-
-    USE netcdf_interface,                                                      &
-        ONLY:  netcdf_data_format
-
-    USE particle_attributes,                                                   &
-        ONLY:  alloc_factor, deleted_particles, grid_particles,                &
-               ibc_par_lr, ibc_par_ns, min_nr_particle,                        &
-               number_of_particles,                                            &
-               offset_ocean_nzt, offset_ocean_nzt_m1, particles,               &
-               particle_type, prt_count, 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, zero_particle
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    INTEGER(iwp), PARAMETER ::  NR_2_direction_move = 10000 !<
-    INTEGER(iwp)            ::  nr_move_north               !<
-    INTEGER(iwp)            ::  nr_move_south               !<
-
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  move_also_north
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  move_also_south
-
-    SAVE
-
-    PRIVATE
-    PUBLIC lpm_exchange_horiz, lpm_move_particle, realloc_particles_array,     &
-           dealloc_particles_array
-
-    INTERFACE lpm_exchange_horiz
-       MODULE PROCEDURE lpm_exchange_horiz
-    END INTERFACE lpm_exchange_horiz
-
-    INTERFACE lpm_move_particle
-       MODULE PROCEDURE lpm_move_particle
-    END INTERFACE lpm_move_particle
-
-    INTERFACE realloc_particles_array
-       MODULE PROCEDURE realloc_particles_array
-    END INTERFACE realloc_particles_array
-
-    INTERFACE dealloc_particles_array
-       MODULE PROCEDURE dealloc_particles_array
-    END INTERFACE dealloc_particles_array
-CONTAINS
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Exchange between subdomains.
-!> As soon as one particle has moved beyond the boundary of the domain, it
-!> is included in the relevant transfer arrays and marked for subsequent
-!> deletion on this PE.
-!> First sweep for crossings in x direction. Find out first the number of
-!> particles to be transferred and allocate temporary arrays needed to store
-!> them.
-!> For a one-dimensional decomposition along y, no transfer is necessary,
-!> because the particle remains on the PE, but the particle coordinate has to
-!> be adjusted.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_exchange_horiz
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i                 !< grid index (x) of particle positition
-    INTEGER(iwp) ::  ip                !< index variable along x
-    INTEGER(iwp) ::  j                 !< grid index (y) of particle positition
-    INTEGER(iwp) ::  jp                !< index variable along y
-    INTEGER(iwp) ::  kp                !< index variable along z
-    INTEGER(iwp) ::  n                 !< particle index variable 
-    INTEGER(iwp) ::  par_size          !< Particle size in bytes
-    INTEGER(iwp) ::  trlp_count        !< number of particles send to left PE
-    INTEGER(iwp) ::  trlp_count_recv   !< number of particles receive from right PE
-    INTEGER(iwp) ::  trnp_count        !< number of particles send to north PE
-    INTEGER(iwp) ::  trnp_count_recv   !< number of particles receive from south PE
-    INTEGER(iwp) ::  trrp_count        !< number of particles send to right PE
-    INTEGER(iwp) ::  trrp_count_recv   !< number of particles receive from left PE
-    INTEGER(iwp) ::  trsp_count        !< number of particles send to south PE
-    INTEGER(iwp) ::  trsp_count_recv   !< number of particles receive from north PE
-
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvlp  !< particles received from right PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvnp  !< particles received from south PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvrp  !< particles received from left PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  rvsp  !< particles received from north PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trlp  !< particles send to left PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trnp  !< particles send to north PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trrp  !< particles send to right PE
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trsp  !< particles send to south PE
-
-    CALL cpu_log( log_point_s(23), 'lpm_exchange_horiz', 'start' )
-
-#if defined( __parallel )
-
-!
-!-- Exchange between subdomains.
-!-- As soon as one particle has moved beyond the boundary of the domain, it
-!-- is included in the relevant transfer arrays and marked for subsequent
-!-- deletion on this PE.
-!-- First sweep for crossings in x direction. Find out first the number of
-!-- particles to be transferred and allocate temporary arrays needed to store
-!-- them.
-!-- For a one-dimensional decomposition along y, no transfer is necessary,
-!-- because the particle remains on the PE, but the particle coordinate has to
-!-- be adjusted.
-    trlp_count  = 0
-    trrp_count  = 0
-
-    trlp_count_recv   = 0
-    trrp_count_recv   = 0
-
-    IF ( pdims(1) /= 1 )  THEN
-!
-!--    First calculate the storage necessary for sending and receiving the data.
-!--    Compute only first (nxl) and last (nxr) loop iterration.
-       DO  ip = nxl, nxr, nxr - nxl
-          DO  jp = nys, nyn
-             DO  kp = nzb+1, nzt
-
-                number_of_particles = prt_count(kp,jp,ip)
-                IF ( number_of_particles <= 0 )  CYCLE
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                DO  n = 1, number_of_particles
-                   IF ( particles(n)%particle_mask )  THEN
-                      i = particles(n)%x * ddx
-!
-!--                   Above calculation does not work for indices less than zero
-                      IF ( particles(n)%x < 0.0_wp)  i = -1
-
-                      IF ( i < nxl )  THEN
-                         trlp_count = trlp_count + 1
-                      ELSEIF ( i > nxr )  THEN
-                         trrp_count = trrp_count + 1
-                      ENDIF
-                   ENDIF
-                ENDDO
-
-             ENDDO
-          ENDDO
-       ENDDO
-
-       IF ( trlp_count  == 0 )  trlp_count  = 1
-       IF ( trrp_count  == 0 )  trrp_count  = 1
-
-       ALLOCATE( trlp(trlp_count), trrp(trrp_count) )
-
-       trlp = zero_particle
-       trrp = zero_particle
-
-       trlp_count  = 0
-       trrp_count  = 0
-
-    ENDIF
-!
-!-- Compute only first (nxl) and last (nxr) loop iterration
-    DO  ip = nxl, nxr, nxr-nxl
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles <= 0 ) CYCLE
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-             DO  n = 1, number_of_particles
-!
-!--             Only those particles that have not been marked as 'deleted' may
-!--             be moved.
-                IF ( particles(n)%particle_mask )  THEN
-
-                   i = particles(n)%x * ddx
-!
-!--                Above calculation does not work for indices less than zero
-                   IF ( particles(n)%x < 0.0_wp )  i = -1
-
-                   IF ( i <  nxl )  THEN
-                      IF ( i < 0 )  THEN
-!
-!--                   Apply boundary condition along x
-                         IF ( ibc_par_lr == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(1) == 1 )  THEN
-                               particles(n)%x        = ( nx + 1 ) * dx + particles(n)%x
-                               particles(n)%origin_x = ( nx + 1 ) * dx + &
-                               particles(n)%origin_x
-                            ELSE
-                               trlp_count = trlp_count + 1
-                               trlp(trlp_count)   = particles(n)
-                               trlp(trlp_count)%x = ( nx + 1 ) * dx + trlp(trlp_count)%x
-                               trlp(trlp_count)%origin_x = trlp(trlp_count)%origin_x + &
-                               ( nx + 1 ) * dx
-                               particles(n)%particle_mask  = .FALSE.
-                               deleted_particles = deleted_particles + 1
-
-                               IF ( trlp(trlp_count)%x >= (nx + 1)* dx - 1.0E-12_wp )  THEN
-                                  trlp(trlp_count)%x = trlp(trlp_count)%x - 1.0E-10_wp
-                                  !++ why is 1 subtracted in next statement???
-                                  trlp(trlp_count)%origin_x = trlp(trlp_count)%origin_x - 1
-                               ENDIF
-
-                            ENDIF
-
-                         ELSEIF ( ibc_par_lr == 1 )  THEN
-!
-!--                         Particle absorption
-                            particles(n)%particle_mask = .FALSE.
-                            deleted_particles = deleted_particles + 1
-
-                         ELSEIF ( ibc_par_lr == 2 )  THEN
-!
-!--                         Particle reflection
-                            particles(n)%x       = -particles(n)%x
-                            particles(n)%speed_x = -particles(n)%speed_x
-
-                         ENDIF
-                      ELSE
-!
-!--                      Store particle data in the transfer array, which will be 
-!--                      send to the neighbouring PE
-                         trlp_count = trlp_count + 1
-                         trlp(trlp_count) = particles(n)
-                         particles(n)%particle_mask = .FALSE.
-                         deleted_particles = deleted_particles + 1
-
-                      ENDIF
-
-                   ELSEIF ( i > nxr )  THEN
-                      IF ( i > nx )  THEN
-!
-!--                      Apply boundary condition along x
-                         IF ( ibc_par_lr == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(1) == 1 )  THEN
-                               particles(n)%x = particles(n)%x - ( nx + 1 ) * dx
-                               particles(n)%origin_x = particles(n)%origin_x - &
-                               ( nx + 1 ) * dx
-                            ELSE
-                               trrp_count = trrp_count + 1
-                               trrp(trrp_count) = particles(n)
-                               trrp(trrp_count)%x = trrp(trrp_count)%x - ( nx + 1 ) * dx
-                               trrp(trrp_count)%origin_x = trrp(trrp_count)%origin_x - &
-                               ( nx + 1 ) * dx
-                               particles(n)%particle_mask = .FALSE.
-                               deleted_particles = deleted_particles + 1
-
-                            ENDIF
-
-                         ELSEIF ( ibc_par_lr == 1 )  THEN
-!
-!--                         Particle absorption
-                            particles(n)%particle_mask = .FALSE.
-                            deleted_particles = deleted_particles + 1
-
-                         ELSEIF ( ibc_par_lr == 2 )  THEN
-!
-!--                         Particle reflection
-                            particles(n)%x       = 2 * ( nx * dx ) - particles(n)%x
-                            particles(n)%speed_x = -particles(n)%speed_x
-
-                         ENDIF
-                      ELSE
-!
-!--                      Store particle data in the transfer array, which will be send
-!--                      to the neighbouring PE
-                         trrp_count = trrp_count + 1
-                         trrp(trrp_count) = particles(n)
-                         particles(n)%particle_mask = .FALSE.
-                         deleted_particles = deleted_particles + 1
-
-                      ENDIF
-
-                   ENDIF
-                ENDIF
-
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO
-
-!
-!-- STORAGE_SIZE returns the storage size of argument A in bits. However , it 
-!-- is needed in bytes. The function C_SIZEOF which produces this value directly
-!-- causes problems with gfortran. For this reason the use of C_SIZEOF is avoided
-    par_size = STORAGE_SIZE(trlp(1))/8
-
-
-!
-!-- Allocate arrays required for north-south exchange, as these
-!-- are used directly after particles are exchange along x-direction.
-    ALLOCATE( move_also_north(1:NR_2_direction_move) )
-    ALLOCATE( move_also_south(1:NR_2_direction_move) )
-
-    nr_move_north = 0
-    nr_move_south = 0
-!
-!-- Send left boundary, receive right boundary (but first exchange how many
-!-- and check, if particle storage must be extended)
-    IF ( pdims(1) /= 1 )  THEN
-
-       CALL MPI_SENDRECV( trlp_count,      1, MPI_INTEGER, pleft,  0, &
-                          trrp_count_recv, 1, MPI_INTEGER, pright, 0, &
-                          comm2d, status, ierr )
-
-       ALLOCATE(rvrp(MAX(1,trrp_count_recv)))
-
-       CALL MPI_SENDRECV( trlp, max(1,trlp_count)*par_size, MPI_BYTE,&
-                          pleft, 1, rvrp,                            &
-                          max(1,trrp_count_recv)*par_size, MPI_BYTE, pright, 1,&
-                          comm2d, status, ierr )
-
-       IF ( trrp_count_recv > 0 )  CALL Add_particles_to_gridcell(rvrp(1:trrp_count_recv))
-
-       DEALLOCATE(rvrp)
-
-!
-!--    Send right boundary, receive left boundary
-       CALL MPI_SENDRECV( trrp_count,      1, MPI_INTEGER, pright, 0, &
-                          trlp_count_recv, 1, MPI_INTEGER, pleft,  0, &
-                          comm2d, status, ierr )
-
-       ALLOCATE(rvlp(MAX(1,trlp_count_recv)))
-!      
-!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--    variables in structure particle_type (due to the calculation of par_size)
-       CALL MPI_SENDRECV( trrp, max(1,trrp_count)*par_size, MPI_BYTE,&
-                          pright, 1, rvlp,                           &
-                          max(1,trlp_count_recv)*par_size, MPI_BYTE, pleft, 1, &
-                          comm2d, status, ierr )
-
-       IF ( trlp_count_recv > 0 )  CALL Add_particles_to_gridcell(rvlp(1:trlp_count_recv))
-
-       DEALLOCATE( rvlp )
-       DEALLOCATE( trlp, trrp )
-
-    ENDIF
-
-!
-!-- Check whether particles have crossed the boundaries in y direction. Note 
-!-- that this case can also apply to particles that have just been received
-!-- from the adjacent right or left PE.
-!-- Find out first the number of particles to be transferred and allocate
-!-- temporary arrays needed to store them.
-!-- For a one-dimensional decomposition along y, no transfer is necessary,
-!-- because the particle remains on the PE.
-    trsp_count  = nr_move_south
-    trnp_count  = nr_move_north
-
-    trsp_count_recv   = 0
-    trnp_count_recv   = 0
-
-    IF ( pdims(2) /= 1 )  THEN
-!
-!--    First calculate the storage necessary for sending and receiving the
-!--    data
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn, nyn-nys    !compute only first (nys) and last (nyn) loop iterration
-             DO  kp = nzb+1, nzt
-                number_of_particles = prt_count(kp,jp,ip)
-                IF ( number_of_particles <= 0 )  CYCLE
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                DO  n = 1, number_of_particles
-                   IF ( particles(n)%particle_mask )  THEN
-                      j = particles(n)%y * ddy
-!
-!--                   Above calculation does not work for indices less than zero
-                      IF ( particles(n)%y < 0.0_wp)  j = -1
-
-                      IF ( j < nys )  THEN
-                         trsp_count = trsp_count + 1
-                      ELSEIF ( j > nyn )  THEN
-                         trnp_count = trnp_count + 1
-                      ENDIF
-                   ENDIF
-                ENDDO
-             ENDDO
-          ENDDO
-       ENDDO
-
-       IF ( trsp_count  == 0 )  trsp_count  = 1
-       IF ( trnp_count  == 0 )  trnp_count  = 1
-
-       ALLOCATE( trsp(trsp_count), trnp(trnp_count) )
-
-       trsp = zero_particle
-       trnp = zero_particle
-
-       trsp_count  = nr_move_south
-       trnp_count  = nr_move_north
-       
-       trsp(1:nr_move_south) = move_also_south(1:nr_move_south)
-       trnp(1:nr_move_north) = move_also_north(1:nr_move_north)
-
-    ENDIF
-
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn, nyn-nys ! compute only first (nys) and last (nyn) loop iterration
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-             DO  n = 1, number_of_particles
-!
-!--             Only those particles that have not been marked as 'deleted' may
-!--             be moved.
-                IF ( particles(n)%particle_mask )  THEN
-
-                   j = particles(n)%y * ddy
-!
-!--                Above calculation does not work for indices less than zero
-                   IF ( particles(n)%y < 0.0_wp )  j = -1
-
-                   IF ( j < nys )  THEN
-                      IF ( j < 0 )  THEN
-!
-!--                      Apply boundary condition along y
-                         IF ( ibc_par_ns == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(2) == 1 )  THEN
-                               particles(n)%y = ( ny + 1 ) * dy + particles(n)%y
-                               particles(n)%origin_y = ( ny + 1 ) * dy + &
-                                                     particles(n)%origin_y
-                            ELSE
-                               trsp_count         = trsp_count + 1
-                               trsp(trsp_count)   = particles(n)
-                               trsp(trsp_count)%y = ( ny + 1 ) * dy + &
-                                                 trsp(trsp_count)%y
-                               trsp(trsp_count)%origin_y = trsp(trsp_count)%origin_y &
-                                                + ( ny + 1 ) * dy
-                               particles(n)%particle_mask = .FALSE.
-                               deleted_particles = deleted_particles + 1
-
-                               IF ( trsp(trsp_count)%y >= (ny+1)* dy - 1.0E-12_wp )  THEN
-                                  trsp(trsp_count)%y = trsp(trsp_count)%y - 1.0E-10_wp
-                                  !++ why is 1 subtracted in next statement???
-                                  trsp(trsp_count)%origin_y =                        &
-                                                  trsp(trsp_count)%origin_y - 1
-                               ENDIF
-
-                            ENDIF
-
-                         ELSEIF ( ibc_par_ns == 1 )  THEN
-!
-!--                         Particle absorption
-                            particles(n)%particle_mask = .FALSE.
-                            deleted_particles          = deleted_particles + 1
-
-                         ELSEIF ( ibc_par_ns == 2 )  THEN
-!
-!--                         Particle reflection
-                            particles(n)%y       = -particles(n)%y
-                            particles(n)%speed_y = -particles(n)%speed_y
-
-                         ENDIF
-                      ELSE
-!
-!--                      Store particle data in the transfer array, which will 
-!--                      be send to the neighbouring PE
-                         trsp_count = trsp_count + 1
-                         trsp(trsp_count) = particles(n)
-                         particles(n)%particle_mask = .FALSE.
-                         deleted_particles = deleted_particles + 1
-
-                      ENDIF
-
-                   ELSEIF ( j > nyn )  THEN
-                      IF ( j > ny )  THEN
-!
-!--                       Apply boundary condition along y
-                         IF ( ibc_par_ns == 0 )  THEN
-!
-!--                         Cyclic condition
-                            IF ( pdims(2) == 1 )  THEN
-                               particles(n)%y        = particles(n)%y - ( ny + 1 ) * dy
-                               particles(n)%origin_y =                         &
-                                          particles(n)%origin_y - ( ny + 1 ) * dy
-                            ELSE
-                               trnp_count         = trnp_count + 1
-                               trnp(trnp_count)   = particles(n)
-                               trnp(trnp_count)%y =                            &
-                                          trnp(trnp_count)%y - ( ny + 1 ) * dy
-                               trnp(trnp_count)%origin_y =                     &
-                                         trnp(trnp_count)%origin_y - ( ny + 1 ) * dy
-                               particles(n)%particle_mask = .FALSE.
-                               deleted_particles          = deleted_particles + 1
-                            ENDIF
-
-                         ELSEIF ( ibc_par_ns == 1 )  THEN
-!
-!--                         Particle absorption
-                            particles(n)%particle_mask = .FALSE.
-                            deleted_particles = deleted_particles + 1
-
-                         ELSEIF ( ibc_par_ns == 2 )  THEN
-!
-!--                         Particle reflection
-                            particles(n)%y       = 2 * ( ny * dy ) - particles(n)%y
-                            particles(n)%speed_y = -particles(n)%speed_y
-
-                         ENDIF
-                      ELSE
-!
-!--                      Store particle data in the transfer array, which will 
-!--                      be send to the neighbouring PE
-                         trnp_count = trnp_count + 1
-                         trnp(trnp_count) = particles(n)
-                         particles(n)%particle_mask = .FALSE.
-                         deleted_particles = deleted_particles + 1
-
-                      ENDIF
-
-                   ENDIF
-                ENDIF
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO
-
-!
-!-- Send front boundary, receive back boundary (but first exchange how many
-!-- and check, if particle storage must be extended)
-    IF ( pdims(2) /= 1 )  THEN
-
-       CALL MPI_SENDRECV( trsp_count,      1, MPI_INTEGER, psouth, 0, &
-                          trnp_count_recv, 1, MPI_INTEGER, pnorth, 0, &
-                          comm2d, status, ierr )
-
-       ALLOCATE(rvnp(MAX(1,trnp_count_recv)))
-!      
-!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--    variables in structure particle_type (due to the calculation of par_size)
-       CALL MPI_SENDRECV( trsp, trsp_count*par_size, MPI_BYTE,      &
-                          psouth, 1, rvnp,                             &
-                          trnp_count_recv*par_size, MPI_BYTE, pnorth, 1,   &
-                          comm2d, status, ierr )
-
-       IF ( trnp_count_recv  > 0 )  CALL Add_particles_to_gridcell(rvnp(1:trnp_count_recv))
-
-       DEALLOCATE(rvnp)
-
-!
-!--    Send back boundary, receive front boundary
-       CALL MPI_SENDRECV( trnp_count,      1, MPI_INTEGER, pnorth, 0, &
-                          trsp_count_recv, 1, MPI_INTEGER, psouth, 0, &
-                          comm2d, status, ierr )
-
-       ALLOCATE(rvsp(MAX(1,trsp_count_recv)))
-!      
-!--    This MPI_SENDRECV should work even with odd mixture on 32 and 64 Bit 
-!--    variables in structure particle_type (due to the calculation of par_size)
-       CALL MPI_SENDRECV( trnp, trnp_count*par_size, MPI_BYTE,      &
-                          pnorth, 1, rvsp,                          &
-                          trsp_count_recv*par_size, MPI_BYTE, psouth, 1,   &
-                          comm2d, status, ierr )
-
-       IF ( trsp_count_recv > 0 )  CALL Add_particles_to_gridcell(rvsp(1:trsp_count_recv))
-
-       DEALLOCATE(rvsp)
-
-       number_of_particles = number_of_particles + trsp_count_recv
-
-       DEALLOCATE( trsp, trnp )
-
-    ENDIF
-
-    DEALLOCATE( move_also_north )
-    DEALLOCATE( move_also_south )
-
-#else
-
-    DO  ip = nxl, nxr, nxr-nxl
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-             DO  n = 1, number_of_particles
-!
-!--             Apply boundary conditions
-
-                IF ( particles(n)%x < 0.0_wp )  THEN
-
-                   IF ( ibc_par_lr == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      particles(n)%x = ( nx + 1 ) * dx + particles(n)%x
-                      particles(n)%origin_x = ( nx + 1 ) * dx + &
-                               particles(n)%origin_x
-                   ELSEIF ( ibc_par_lr == 1 )  THEN
-!
-!--                   Particle absorption
-                      particles(n)%particle_mask = .FALSE.
-                      deleted_particles = deleted_particles + 1
-
-                   ELSEIF ( ibc_par_lr == 2 )  THEN
-!
-!--                   Particle reflection
-                      particles(n)%x       = -dx - particles(n)%x
-                      particles(n)%speed_x = -particles(n)%speed_x
-                   ENDIF
-
-                ELSEIF ( particles(n)%x >= ( nx + 1) * dx )  THEN
-
-                   IF ( ibc_par_lr == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      particles(n)%x = particles(n)%x - ( nx + 1 ) * dx
-                      particles(n)%origin_x = particles(n)%origin_x - &
-                               ( nx + 1 ) * dx
-
-                   ELSEIF ( ibc_par_lr == 1 )  THEN
-!
-!--                   Particle absorption
-                      particles(n)%particle_mask = .FALSE.
-                      deleted_particles = deleted_particles + 1
-
-                   ELSEIF ( ibc_par_lr == 2 )  THEN
-!
-!--                   Particle reflection
-                      particles(n)%x       = ( nx + 1 ) * dx - particles(n)%x
-                      particles(n)%speed_x = -particles(n)%speed_x
-                   ENDIF
-
-                ENDIF
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO
-
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn, nyn-nys
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-             DO  n = 1, number_of_particles
-
-                IF ( particles(n)%y < 0.0_wp)  THEN
-
-                   IF ( ibc_par_ns == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      particles(n)%y = ( ny + 1 ) * dy + particles(n)%y
-                      particles(n)%origin_y = ( ny + 1 ) * dy + &
-                           particles(n)%origin_y
-
-                   ELSEIF ( ibc_par_ns == 1 )  THEN
-!
-!--                   Particle absorption
-                      particles(n)%particle_mask = .FALSE.
-                      deleted_particles = deleted_particles + 1
-
-                   ELSEIF ( ibc_par_ns == 2 )  THEN
-!
-!--                   Particle reflection
-                      particles(n)%y       = -dy - particles(n)%y
-                      particles(n)%speed_y = -particles(n)%speed_y
-                   ENDIF
-
-                ELSEIF ( particles(n)%y >= ( ny + 1) * dy )  THEN
-
-                   IF ( ibc_par_ns == 0 )  THEN
-!
-!--                   Cyclic boundary. Relevant coordinate has to be changed.
-                      particles(n)%y = particles(n)%y - ( ny + 1 ) * dy
-                      particles(n)%origin_y = particles(n)%origin_y - &
-                                ( ny + 1 ) * dy
-
-                   ELSEIF ( ibc_par_ns == 1 )  THEN
-!
-!--                   Particle absorption
-                      particles(n)%particle_mask = .FALSE.
-                      deleted_particles = deleted_particles + 1
-
-                   ELSEIF ( ibc_par_ns == 2 )  THEN
-!
-!--                   Particle reflection
-                      particles(n)%y       = ( ny + 1 ) * dy - particles(n)%y
-                      particles(n)%speed_y = -particles(n)%speed_y
-                   ENDIF
-
-                ENDIF
-
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO
-#endif
-
-!
-!-- Accumulate the number of particles transferred between the subdomains
-#if defined( __parallel )
-    trlp_count_sum      = trlp_count_sum      + trlp_count
-    trlp_count_recv_sum = trlp_count_recv_sum + trlp_count_recv
-    trrp_count_sum      = trrp_count_sum      + trrp_count
-    trrp_count_recv_sum = trrp_count_recv_sum + trrp_count_recv
-    trsp_count_sum      = trsp_count_sum      + trsp_count
-    trsp_count_recv_sum = trsp_count_recv_sum + trsp_count_recv
-    trnp_count_sum      = trnp_count_sum      + trnp_count
-    trnp_count_recv_sum = trnp_count_recv_sum + trnp_count_recv
-#endif
-
-    CALL cpu_log( log_point_s(23), 'lpm_exchange_horiz', 'stop' )
-
- END SUBROUTINE lpm_exchange_horiz
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> If a particle moves from one processor to another, this subroutine moves 
-!> the corresponding elements from the particle arrays of the old grid cells 
-!> to the particle arrays of the new grid cells.
-!------------------------------------------------------------------------------!
- SUBROUTINE Add_particles_to_gridcell (particle_array)
-
-    IMPLICIT NONE
-
-    INTEGER(iwp)        ::  ip        !< grid index (x) of particle
-    INTEGER(iwp)        ::  jp        !< grid index (x) of particle
-    INTEGER(iwp)        ::  kp        !< grid index (x) of particle
-    INTEGER(iwp)        ::  n         !< index variable of particle
-    INTEGER(iwp)        ::  pindex    !< dummy argument for new number of particles per grid box
-
-    LOGICAL             ::  pack_done !<
-
-    TYPE(particle_type), DIMENSION(:), INTENT(IN)  ::  particle_array !< new particles in a grid box
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  temp_ns        !< temporary particle array for reallocation
-
-    pack_done     = .FALSE.
-
-    DO n = 1, SIZE(particle_array)
-
-       IF ( .NOT. particle_array(n)%particle_mask )  CYCLE
-
-       ip = particle_array(n)%x * ddx
-       jp = particle_array(n)%y * ddy
-       kp = particle_array(n)%z / dz(1) + 1 + offset_ocean_nzt
-!
-!--    In case of grid stretching a particle might be above or below the  
-!--    previously calculated particle grid box (indices).     
-       DO WHILE( zw(kp) < particle_array(n)%z )
-          kp = kp + 1
-       ENDDO
-
-       DO WHILE( zw(kp-1) > particle_array(n)%z )
-          kp = kp - 1
-       ENDDO
-
-       IF ( ip >= nxl  .AND.  ip <= nxr  .AND.  jp >= nys  .AND.  jp <= nyn    &
-            .AND.  kp >= nzb+1  .AND.  kp <= nzt)  THEN ! particle stays on processor
-          number_of_particles = prt_count(kp,jp,ip)
-          particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-
-          pindex = prt_count(kp,jp,ip)+1
-          IF( pindex > SIZE(grid_particles(kp,jp,ip)%particles) )  THEN
-             IF ( pack_done )  THEN
-                CALL realloc_particles_array (ip,jp,kp)
-             ELSE
-                CALL lpm_pack
-                prt_count(kp,jp,ip) = number_of_particles
-                pindex = prt_count(kp,jp,ip)+1
-                IF ( pindex > SIZE(grid_particles(kp,jp,ip)%particles) )  THEN
-                   CALL realloc_particles_array (ip,jp,kp)
-                ENDIF
-                pack_done = .TRUE.
-             ENDIF
-          ENDIF
-          grid_particles(kp,jp,ip)%particles(pindex) = particle_array(n)
-          prt_count(kp,jp,ip) = pindex
-       ELSE
-          IF ( jp <= nys - 1 )  THEN
-             nr_move_south = nr_move_south+1
-!
-!--          Before particle information is swapped to exchange-array, check 
-!--          if enough memory is allocated. If required, reallocate exchange
-!--          array.
-             IF ( nr_move_south > SIZE(move_also_south) )  THEN
-!
-!--             At first, allocate further temporary array to swap particle 
-!--             information.
-                ALLOCATE( temp_ns(SIZE(move_also_south)+NR_2_direction_move) )
-                temp_ns(1:nr_move_south-1) = move_also_south(1:nr_move_south-1)
-                DEALLOCATE( move_also_south )
-                ALLOCATE( move_also_south(SIZE(temp_ns)) )
-                move_also_south(1:nr_move_south-1) = temp_ns(1:nr_move_south-1)
-                DEALLOCATE( temp_ns )
-
-             ENDIF
-
-             move_also_south(nr_move_south) = particle_array(n)
-
-             IF ( jp == -1 )  THEN
-!
-!--             Apply boundary condition along y
-                IF ( ibc_par_ns == 0 )  THEN
-                   move_also_south(nr_move_south)%y =                          &
-                      move_also_south(nr_move_south)%y + ( ny + 1 ) * dy
-                   move_also_south(nr_move_south)%origin_y =                   &
-                      move_also_south(nr_move_south)%origin_y + ( ny + 1 ) * dy
-                ELSEIF ( ibc_par_ns == 1 )  THEN
-!
-!--                Particle absorption
-                   move_also_south(nr_move_south)%particle_mask = .FALSE.
-                   deleted_particles = deleted_particles + 1
-
-                ELSEIF ( ibc_par_ns == 2 )  THEN
-!
-!--                Particle reflection
-                   move_also_south(nr_move_south)%y       =                    &
-                      -move_also_south(nr_move_south)%y
-                   move_also_south(nr_move_south)%speed_y =                    &
-                      -move_also_south(nr_move_south)%speed_y
-
-                ENDIF
-             ENDIF
-          ELSEIF ( jp >= nyn+1 )  THEN
-             nr_move_north = nr_move_north+1
-!
-!--          Before particle information is swapped to exchange-array, check 
-!--          if enough memory is allocated. If required, reallocate exchange
-!--          array.
-             IF ( nr_move_north > SIZE(move_also_north) )  THEN
-!
-!--             At first, allocate further temporary array to swap particle 
-!--             information.
-                ALLOCATE( temp_ns(SIZE(move_also_north)+NR_2_direction_move) )
-                temp_ns(1:nr_move_north-1) = move_also_south(1:nr_move_north-1)
-                DEALLOCATE( move_also_north )
-                ALLOCATE( move_also_north(SIZE(temp_ns)) )
-                move_also_north(1:nr_move_north-1) = temp_ns(1:nr_move_north-1)
-                DEALLOCATE( temp_ns )
-
-             ENDIF
-
-             move_also_north(nr_move_north) = particle_array(n)
-             IF ( jp == ny+1 )  THEN
-!
-!--             Apply boundary condition along y
-                IF ( ibc_par_ns == 0 )  THEN
-
-                   move_also_north(nr_move_north)%y =                          &
-                      move_also_north(nr_move_north)%y - ( ny + 1 ) * dy
-                   move_also_north(nr_move_north)%origin_y =                   &
-                      move_also_north(nr_move_north)%origin_y - ( ny + 1 ) * dy
-                ELSEIF ( ibc_par_ns == 1 )  THEN
-!
-!--                Particle absorption
-                   move_also_north(nr_move_north)%particle_mask = .FALSE.
-                   deleted_particles = deleted_particles + 1
-
-                ELSEIF ( ibc_par_ns == 2 )  THEN
-!
-!--                Particle reflection
-                   move_also_north(nr_move_north)%y       =                    &
-                      -move_also_north(nr_move_north)%y
-                   move_also_north(nr_move_north)%speed_y =                    &
-                      -move_also_north(nr_move_north)%speed_y
-
-                ENDIF
-             ENDIF
-          ELSE
-             WRITE(0,'(a,8i7)') 'particle out of range ',myid,ip,jp,kp,nxl,nxr,nys,nyn
-          ENDIF
-       ENDIF
-    ENDDO
-
-    RETURN
-
- END SUBROUTINE Add_particles_to_gridcell
-
-
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> If a particle moves from one grid cell to another (on the current 
-!> processor!), this subroutine moves the corresponding element from the
-!> particle array of the old grid cell to the particle array of the new grid 
-!> cell.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_move_particle
- 
-    IMPLICIT NONE
-
-    INTEGER(iwp)        ::  i           !< grid index (x) of particle position
-    INTEGER(iwp)        ::  ip          !< index variable along x
-    INTEGER(iwp)        ::  j           !< grid index (y) of particle position
-    INTEGER(iwp)        ::  jp          !< index variable along y
-    INTEGER(iwp)        ::  k           !< grid index (z) of particle position
-    INTEGER(iwp)        ::  kp          !< index variable along z
-    INTEGER(iwp)        ::  n           !< index variable for particle array
-    INTEGER(iwp)        ::  np_before_move !< number of particles per grid box before moving
-    INTEGER(iwp)        ::  pindex      !< dummy argument for number of new particle per grid box
-
-    TYPE(particle_type), DIMENSION(:), POINTER  ::  particles_before_move !< particles before moving
-
-    CALL cpu_log( log_point_s(41), 'lpm_move_particle', 'start' )
-    CALL lpm_check_cfl
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-
-             np_before_move = prt_count(kp,jp,ip)
-             IF ( np_before_move <= 0 )  CYCLE
-             particles_before_move => grid_particles(kp,jp,ip)%particles(1:np_before_move)
-             
-             DO  n = 1, np_before_move
-                i = particles_before_move(n)%x * ddx
-                j = particles_before_move(n)%y * ddy
-                k = kp
-!
-!--             Find correct vertical particle grid box (necessary in case of grid stretching)
-!--             Due to the CFL limitations only the neighbouring grid boxes are considered. 
-                IF( zw(k)   < particles_before_move(n)%z ) k = k + 1
-                IF( zw(k-1) > particles_before_move(n)%z ) k = k - 1 
-                
-!--             For lpm_exchange_horiz to work properly particles need to be moved to the outermost gridboxes
-!--             of the respective processor. If the particle index is inside the processor the following lines
-!--             will not change the index
-                i = MIN ( i , nxr )
-                i = MAX ( i , nxl )
-                j = MIN ( j , nyn )
-                j = MAX ( j , nys )
-                
-                k = MIN ( k , nzt )
-                k = MAX ( k , nzb+1 )
-                
-!
-!--             Check, if particle has moved to another grid cell.
-                IF ( i /= ip  .OR.  j /= jp  .OR.  k /= kp )  THEN
-!!
-!--                If the particle stays on the same processor, the particle
-!--                will be added to the particle array of the new processor.
-                   number_of_particles = prt_count(k,j,i)
-                   particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-
-                   pindex = prt_count(k,j,i)+1
-                   IF (  pindex > SIZE(grid_particles(k,j,i)%particles)  )     &
-                   THEN
-                      CALL realloc_particles_array(i,j,k)
-                   ENDIF
-
-                   grid_particles(k,j,i)%particles(pindex) = particles_before_move(n)
-                   prt_count(k,j,i) = pindex
-
-                   particles_before_move(n)%particle_mask = .FALSE.
-                ENDIF
-             ENDDO
-
-          ENDDO
-       ENDDO
-    ENDDO
-
-    CALL cpu_log( log_point_s(41), 'lpm_move_particle', 'stop' )
-
-    RETURN
-
- END SUBROUTINE lpm_move_particle
- 
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Check CFL-criterion for each particle. If one particle violated the 
-!> criterion the particle will be deleted and a warning message is given.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_check_cfl  
-       
-    IMPLICIT NONE
-    
-    INTEGER(iwp)  ::  i !< running index, x-direction
-    INTEGER(iwp)  ::  j !< running index, y-direction
-    INTEGER(iwp)  ::  k !< running index, z-direction
-    INTEGER(iwp)  ::  n !< running index, number of particles
-
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb+1, nzt
-             number_of_particles = prt_count(k,j,i)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(k,j,i)%particles(1:number_of_particles)         
-             DO n = 1, number_of_particles
-!
-!--             Note, check for CFL does not work at first particle timestep 
-!--             when both, age and age_m are zero. 
-                IF ( particles(n)%age - particles(n)%age_m > 0.0_wp )  THEN  
-                   IF(ABS(particles(n)%speed_x) >                              &
-                      (dx/(particles(n)%age-particles(n)%age_m))  .OR.         &
-                      ABS(particles(n)%speed_y) >                              & 
-                      (dy/(particles(n)%age-particles(n)%age_m))  .OR.         &
-                      ABS(particles(n)%speed_z) >                              &
-                      ((zw(k)-zw(k-1))/(particles(n)%age-particles(n)%age_m))) &
-                   THEN
-                      WRITE( message_string, * )                               &
-                      'Particle violated CFL-criterion: &particle with id ',   &
-                      particles(n)%id, ' will be deleted!'   
-                      CALL message( 'lpm_check_cfl', 'PA0475', 0, 1, -1, 6, 0 )
-                      particles(n)%particle_mask= .FALSE.
-                   ENDIF
-                ENDIF
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO   
-
- END SUBROUTINE lpm_check_cfl
- 
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> If the allocated memory for the particle array do not suffice to add arriving
-!> particles from neighbour grid cells, this subrouting reallocates the 
-!> particle array to assure enough memory is available. 
-!------------------------------------------------------------------------------!
- SUBROUTINE realloc_particles_array( i, j, k, size_in )
-
-    IMPLICIT NONE
-
-    INTEGER(iwp), INTENT(in)                       ::  i              !<
-    INTEGER(iwp), INTENT(in)                       ::  j              !<
-    INTEGER(iwp), INTENT(in)                       ::  k              !<
-    INTEGER(iwp), INTENT(in), OPTIONAL             ::  size_in        !<
-
-    INTEGER(iwp)                                   ::  old_size        !<
-    INTEGER(iwp)                                   ::  new_size        !<
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  tmp_particles_d !<
-    TYPE(particle_type), DIMENSION(500)            ::  tmp_particles_s !<
-
-    old_size = SIZE(grid_particles(k,j,i)%particles)
-
-    IF ( PRESENT(size_in) )   THEN
-       new_size = size_in
-    ELSE
-       new_size = old_size * ( 1.0_wp + alloc_factor / 100.0_wp )
-    ENDIF
-
-    new_size = MAX( new_size, min_nr_particle, old_size + 1 )
-
-    IF ( old_size <= 500 )  THEN
-
-       tmp_particles_s(1:old_size) = grid_particles(k,j,i)%particles(1:old_size)
-
-       DEALLOCATE(grid_particles(k,j,i)%particles)
-       ALLOCATE(grid_particles(k,j,i)%particles(new_size))
-
-       grid_particles(k,j,i)%particles(1:old_size)          = tmp_particles_s(1:old_size)
-       grid_particles(k,j,i)%particles(old_size+1:new_size) = zero_particle
-
-    ELSE
-
-       ALLOCATE(tmp_particles_d(new_size))
-       tmp_particles_d(1:old_size) = grid_particles(k,j,i)%particles
-
-       DEALLOCATE(grid_particles(k,j,i)%particles)
-       ALLOCATE(grid_particles(k,j,i)%particles(new_size))
-
-       grid_particles(k,j,i)%particles(1:old_size)          = tmp_particles_d(1:old_size)
-       grid_particles(k,j,i)%particles(old_size+1:new_size) = zero_particle
-
-       DEALLOCATE(tmp_particles_d)
-
-    ENDIF
-    particles => grid_particles(k,j,i)%particles(1:new_size)
-
- 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/lpm_init.f90
===================================================================
--- palm/trunk/SOURCE/lpm_init.f90	(revision 4015)
+++ 	(revision )
@@ -1,1254 +1,0 @@
-!> @file lpm_init.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Bugfix in particle nesting, initialize TKE gradient with zero
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! Changed number concentration unit to SI standard
-! 
-! 3560 2018-11-23 09:20:21Z raasch
-! set the first particle release time
-! 
-! 3524 2018-11-14 13:36:44Z raasch
-! added missing working precision
-! 
-! 3361 2018-10-16 20:39:37Z knoop
-! ocean renamed ocean_mode
-! 
-! 3274 2018-09-24 15:42:55Z knoop
-! Modularization of all bulk cloud physics code components
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3065 2018-06-12 07:03:02Z Giersch
-! dz was replaced by dzw or dz(1) to allow for right vertical stretching
-! 
-! 3049 2018-05-29 13:52:36Z Giersch
-! Error messages revised
-! 
-! 3045 2018-05-28 07:55:41Z Giersch
-! Error message revised
-! 
-! 3039 2018-05-24 13:13:11Z schwenkel
-! bugfix for lcm with grid stretching
-! 
-! 2967 2018-04-13 11:22:08Z raasch
-! nesting routine is only called if nesting is switched on
-! 
-! 2954 2018-04-09 14:35:46Z schwenkel
-! Bugfix for particle initialization in case of ocean
-! 
-! 2801 2018-02-14 16:01:55Z thiele
-! Introduce particle transfer in nested models.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2701 2017-12-15 15:40:50Z suehring
-! Changes from last commit documented
-! 
-! 2698 2017-12-14 18:46:24Z suehring
-! Grid indices passed to lpm_boundary_conds. (responsible Philipp Thiele)
-!
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2628 2017-11-20 12:40:38Z schwenkel
-! Enabled particle advection with grid stretching.
-! 
-! 2608 2017-11-13 14:04:26Z schwenkel
-! Calculation of magnus equation in external module (diagnostic_quantities_mod).
-! 
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done
-! 
-! 2375 2017-08-29 14:10:28Z schwenkel
-! Initialization of chemical aerosol composition
-! 
-! 2346 2017-08-09 16:39:17Z suehring
-! Bugfix, correct determination of topography top index
-! 
-! 2318 2017-07-20 17:27:44Z suehring
-! Get topography top index via Function call 
-!
-! 2317 2017-07-20 17:27:19Z suehring
-! Extended particle data type. Aerosol initialization improved.
-!
-! 2305 2017-07-06 11:18:47Z hoffmann
-! Improved calculation of particle IDs.
-!
-! 2274 2017-06-09 13:27:48Z Giersch
-!  Changed error messages
-!
-! 2265 2017-06-08 16:58:28Z schwenkel
-! Unused variables removed.
-!
-! 2263 2017-06-08 14:59:01Z schwenkel
-! Implemented splitting and merging algorithm
-!
-! 2233 2017-05-30 18:08:54Z suehring
-!
-! 2232 2017-05-30 17:47:52Z suehring
-! Adjustments according to new topography realization
-!
-!
-! 2223 2017-05-15 16:38:09Z suehring
-! Add check for particle release at model top
-!
-! 2182 2017-03-17 14:27:40Z schwenkel
-! Added parameters for simplified particle initialization.
-!
-! 2122 2017-01-18 12:22:54Z hoffmann
-! Improved initialization of equilibrium aerosol radii
-! Calculation of particle ID
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 2016-06-09 16:25:25Z suehring
-! Bugfix in determining initial particle height and grid index in case of
-! seed_follows_topography.
-! Bugfix concerning random positions, ensure that particles do not move more
-! than one grid length.
-! Bugfix logarithmic interpolation.
-! Initial setting of sgs_wf_part.
-!
-! 1890 2016-04-22 08:52:11Z hoffmann
-! Initialization of aerosol equilibrium radius not possible in supersaturated
-! environments. Therefore, a maximum supersaturation of -1 % is assumed during
-! initialization.
-!
-! 1873 2016-04-18 14:50:06Z maronga
-! Module renamed (removed _mod
-!
-! 1871 2016-04-15 11:46:09Z hoffmann
-! Initialization of aerosols added.
-!
-! 1850 2016-04-08 13:29:27Z maronga
-! Module renamed
-!
-! 1831 2016-04-07 13:15:51Z hoffmann
-! curvature_solution_effects moved to particle_attributes
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Unused variables removed.
-!
-! 1783 2016-03-06 18:36:17Z raasch
-! netcdf module added
-!
-! 1725 2015-11-17 13:01:51Z hoffmann
-! Bugfix: Processor-dependent seed for random function is generated before it is
-! used.
-!
-! 1691 2015-10-26 16:17:44Z maronga
-! Renamed prandtl_layer to constant_flux_layer.
-!
-! 1685 2015-10-08 07:32:13Z raasch
-! bugfix concerning vertical index offset in case of ocean
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1575 2015-03-27 09:56:27Z raasch
-! initial vertical particle position is allowed to follow the topography
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-! Kind definition added to all floating point numbers.
-! lpm_init changed form a subroutine to a module.
-!
-! 1327 2014-03-21 11:00:16Z raasch
-! -netcdf_output
-!
-! 1322 2014-03-20 16:38:49Z raasch
-! REAL functions provided with KIND-attribute
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-! bugfix: #if defined( __parallel ) added
-!
-! 1314 2014-03-14 18:25:17Z suehring
-! Vertical logarithmic interpolation of horizontal particle speed for particles
-! between roughness height and first vertical grid level.
-!
-! 1092 2013-02-02 11:24:22Z raasch
-! unused variables removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! routine renamed: init_particles -> lpm_init
-! de_dx, de_dy, de_dz are allocated here (instead of automatic arrays in
-! advec_particles),
-! sort_particles renamed lpm_sort_arrays, user_init_particles renamed lpm_init
-!
-! 828 2012-02-21 12:00:36Z raasch
-! call of init_kernels, particle feature color renamed class
-!
-! 824 2012-02-17 09:09:57Z raasch
-! particle attributes speed_x|y|z_sgs renamed rvar1|2|3,
-! array particles implemented as pointer
-!
-! 667 2010-12-23 12:06:00Z suehring/gryschka
-! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng for allocation
-! of arrays.
-!
-! Revision 1.1  1999/11/25 16:22:38  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> This routine initializes a set of particles and their attributes (position,
-!> radius, ..) which are used by the Lagrangian particle model (see lpm).
-!------------------------------------------------------------------------------!
- MODULE lpm_init_mod
-
-    USE, INTRINSIC ::  ISO_C_BINDING
-
-    USE arrays_3d,                                                             &
-        ONLY:  de_dx, de_dy, de_dz, dzw, zu, zw
-
-    USE control_parameters,                                                    &
-        ONLY:  cloud_droplets, constant_flux_layer, current_timestep_number,   &
-               dt_3d, dz, initializing_actions, message_string, ocean_mode,    &
-               simulated_time
-
-    USE grid_variables,                                                        &
-        ONLY:  ddx, dx, ddy, dy
-
-    USE indices,                                                               &
-        ONLY:  nx, nxl, nxlg, nxrg, nxr, ny, nyn, nys, nyng, nysg, nz, nzb,    &
-               nzt, wall_flags_0
-
-    USE kinds
-
-    USE lpm_collision_kernels_mod,                                             &
-        ONLY:  init_kernels
-
-    USE netcdf_interface,                                                      &
-        ONLY:  netcdf_data_format
-
-    USE particle_attributes,                                                   &
-        ONLY:   alloc_factor, bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,        &
-                block_offset, block_offset_def, collision_kernel,              &
-                curvature_solution_effects, density_ratio, grid_particles,     &
-                isf,i_splitting_mode, initial_weighting_factor, ibc_par_b,     &
-                ibc_par_lr, ibc_par_ns, ibc_par_t, iran_part,                  &
-                last_particle_release_time, log_z_z0,                          &
-                max_number_of_particle_groups, min_nr_particle,                &
-                number_concentration,                                          &
-                number_particles_per_gridbox,  number_of_particles,            &
-                number_of_particle_groups, number_of_sublayers,                &
-                offset_ocean_nzt, offset_ocean_nzt_m1,                         &
-                particles, particle_advection_start, particle_groups,          &
-                particle_groups_type, particles_per_point,                     &
-                particle_type, pdx, pdy, pdz,  prt_count, psb, psl, psn, psr,  &
-                pss, pst, radius, random_start_position,                       &
-                read_particles_from_restartfile, seed_follows_topography,      &
-                sgs_wf_part, sort_count, splitting_function, splitting_mode,   &
-                total_number_of_particles, use_sgs_for_particles,              &
-                write_particle_statistics, zero_particle, z0_av_global
-
-    USE pegrid
-
-    USE random_function_mod,                                                   &
-        ONLY:  random_function
-
-    USE surface_mod,                                                           &
-        ONLY:  get_topography_top_index_ji, surf_def_h, surf_lsm_h, surf_usm_h
-
-    USE pmc_particle_interface,                                                &
-        ONLY:  pmcp_g_init
-
-    IMPLICIT NONE
-
-    PRIVATE
-
-    INTEGER(iwp), PARAMETER         :: PHASE_INIT    = 1  !<
-    INTEGER(iwp), PARAMETER, PUBLIC :: PHASE_RELEASE = 2  !<
-
-    INTERFACE lpm_init
-       MODULE PROCEDURE lpm_init
-    END INTERFACE lpm_init
-
-    INTERFACE lpm_create_particle
-       MODULE PROCEDURE lpm_create_particle
-    END INTERFACE lpm_create_particle
-
-    PUBLIC lpm_init, lpm_create_particle
-
- CONTAINS
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_init
-
-    USE lpm_collision_kernels_mod,                                             &
-        ONLY:  init_kernels
-
-    USE pmc_interface,                                                         &
-        ONLY: nested_run
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i                           !<
-    INTEGER(iwp) ::  j                           !<
-    INTEGER(iwp) ::  k                           !<
-
-    REAL(wp) ::  div                             !<
-    REAL(wp) ::  height_int                      !<
-    REAL(wp) ::  height_p                        !<
-    REAL(wp) ::  z_p                             !<
-    REAL(wp) ::  z0_av_local                     !<
-
-
-!
-!-- In case of oceans runs, the vertical index calculations need an offset,
-!-- because otherwise the k indices will become negative
-    IF ( ocean_mode )  THEN
-       offset_ocean_nzt    = nzt
-       offset_ocean_nzt_m1 = nzt - 1
-    ENDIF
-
-!
-!-- Define block offsets for dividing a gridcell in 8 sub cells
-!-- See documentation for List of subgrid boxes
-!-- See pack_and_sort in lpm_pack_arrays.f90 for assignment of the subgrid boxes
-    block_offset(0) = block_offset_def ( 0, 0, 0)
-    block_offset(1) = block_offset_def ( 0, 0,-1)
-    block_offset(2) = block_offset_def ( 0,-1, 0)
-    block_offset(3) = block_offset_def ( 0,-1,-1)
-    block_offset(4) = block_offset_def (-1, 0, 0)
-    block_offset(5) = block_offset_def (-1, 0,-1)
-    block_offset(6) = block_offset_def (-1,-1, 0)
-    block_offset(7) = block_offset_def (-1,-1,-1)
-!
-!-- Check the number of particle groups.
-    IF ( number_of_particle_groups > max_number_of_particle_groups )  THEN
-       WRITE( message_string, * ) 'max_number_of_particle_groups =',           &
-                                  max_number_of_particle_groups ,              &
-                                  '&number_of_particle_groups reset to ',      &
-                                  max_number_of_particle_groups
-       CALL message( 'lpm_init', 'PA0213', 0, 1, 0, 6, 0 )
-       number_of_particle_groups = max_number_of_particle_groups
-    ENDIF
-!
-!-- Check if downward-facing walls exist. This case, reflection boundary
-!-- conditions (as well as subgrid-scale velocities) may do not work
-!-- propably (not realized so far).
-    IF ( surf_def_h(1)%ns >= 1 )  THEN
-       WRITE( message_string, * ) 'Overhanging topography do not work '//      &
-                                  'with particles'
-       CALL message( 'lpm_init', 'PA0212', 0, 1, 0, 6, 0 )
-
-    ENDIF
-
-!
-!-- Set default start positions, if necessary
-    IF ( psl(1) == 9999999.9_wp )  psl(1) = 0.0_wp
-    IF ( psr(1) == 9999999.9_wp )  psr(1) = ( nx +1 ) * dx
-    IF ( pss(1) == 9999999.9_wp )  pss(1) = 0.0_wp
-    IF ( psn(1) == 9999999.9_wp )  psn(1) = ( ny +1 ) * dy
-    IF ( psb(1) == 9999999.9_wp )  psb(1) = zu(nz/2)
-    IF ( pst(1) == 9999999.9_wp )  pst(1) = psb(1)
-
-    IF ( pdx(1) == 9999999.9_wp  .OR.  pdx(1) == 0.0_wp )  pdx(1) = dx
-    IF ( pdy(1) == 9999999.9_wp  .OR.  pdy(1) == 0.0_wp )  pdy(1) = dy
-    IF ( pdz(1) == 9999999.9_wp  .OR.  pdz(1) == 0.0_wp )  pdz(1) = zu(2) - zu(1)
-
-!
-!-- If number_particles_per_gridbox is set, the parametres pdx, pdy and pdz are
-!-- calculated diagnostically. Therfore an isotropic distribution is prescribed.
-    IF ( number_particles_per_gridbox /= -1 .AND.   &
-         number_particles_per_gridbox >= 1 )    THEN
-       pdx(1) = (( dx * dy * ( zu(2) - zu(1) ) ) /  &
-             REAL(number_particles_per_gridbox))**0.3333333_wp
-!
-!--    Ensure a smooth value (two significant digits) of distance between
-!--    particles (pdx, pdy, pdz).
-       div = 1000.0_wp
-       DO  WHILE ( pdx(1) < div )
-          div = div / 10.0_wp
-       ENDDO
-       pdx(1) = NINT( pdx(1) * 100.0_wp / div ) * div / 100.0_wp
-       pdy(1) = pdx(1)
-       pdz(1) = pdx(1)
-
-    ENDIF
-
-    DO  j = 2, number_of_particle_groups
-       IF ( psl(j) == 9999999.9_wp )  psl(j) = psl(j-1)
-       IF ( psr(j) == 9999999.9_wp )  psr(j) = psr(j-1)
-       IF ( pss(j) == 9999999.9_wp )  pss(j) = pss(j-1)
-       IF ( psn(j) == 9999999.9_wp )  psn(j) = psn(j-1)
-       IF ( psb(j) == 9999999.9_wp )  psb(j) = psb(j-1)
-       IF ( pst(j) == 9999999.9_wp )  pst(j) = pst(j-1)
-       IF ( pdx(j) == 9999999.9_wp  .OR.  pdx(j) == 0.0_wp )  pdx(j) = pdx(j-1)
-       IF ( pdy(j) == 9999999.9_wp  .OR.  pdy(j) == 0.0_wp )  pdy(j) = pdy(j-1)
-       IF ( pdz(j) == 9999999.9_wp  .OR.  pdz(j) == 0.0_wp )  pdz(j) = pdz(j-1)
-    ENDDO
-
-!
-!-- Allocate arrays required for calculating particle SGS velocities.
-!-- Initialize prefactor required for stoachastic Weil equation.
-    IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
-       ALLOCATE( de_dx(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
-                 de_dy(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
-                 de_dz(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-       de_dx = 0.0_wp
-       de_dy = 0.0_wp
-       de_dz = 0.0_wp
-
-       sgs_wf_part = 1.0_wp / 3.0_wp
-    ENDIF
-
-!
-!-- Allocate array required for logarithmic vertical interpolation of
-!-- horizontal particle velocities between the surface and the first vertical
-!-- grid level. In order to avoid repeated CPU cost-intensive CALLS of
-!-- intrinsic FORTRAN procedure LOG(z/z0), LOG(z/z0) is precalculated for
-!-- several heights. Splitting into 20 sublayers turned out to be sufficient.
-!-- To obtain exact height levels of particles, linear interpolation is applied
-!-- (see lpm_advec.f90).
-    IF ( constant_flux_layer )  THEN
-
-       ALLOCATE ( log_z_z0(0:number_of_sublayers) )
-       z_p = zu(nzb+1) - zw(nzb)
-
-!
-!--    Calculate horizontal mean value of z0 used for logartihmic
-!--    interpolation. Note: this is not exact for heterogeneous z0.
-!--    However, sensitivity studies showed that the effect is
-!--    negligible.
-       z0_av_local  = SUM( surf_def_h(0)%z0 ) + SUM( surf_lsm_h%z0 ) +         &
-                      SUM( surf_usm_h%z0 )
-       z0_av_global = 0.0_wp
-
-#if defined( __parallel )
-       CALL MPI_ALLREDUCE(z0_av_local, z0_av_global, 1, MPI_REAL, MPI_SUM, &
-                          comm2d, ierr )
-#else
-       z0_av_global = z0_av_local
-#endif
-
-       z0_av_global = z0_av_global  / ( ( ny + 1 ) * ( nx + 1 ) )
-!
-!--    Horizontal wind speed is zero below and at z0
-       log_z_z0(0) = 0.0_wp
-!
-!--    Calculate vertical depth of the sublayers
-       height_int  = ( z_p - z0_av_global ) / REAL( number_of_sublayers, KIND=wp )
-!
-!--    Precalculate LOG(z/z0)
-       height_p    = z0_av_global
-       DO  k = 1, number_of_sublayers
-
-          height_p    = height_p + height_int
-          log_z_z0(k) = LOG( height_p / z0_av_global )
-
-       ENDDO
-
-    ENDIF
-
-!
-!-- Check boundary condition and set internal variables
-    SELECT CASE ( bc_par_b )
-
-       CASE ( 'absorb' )
-          ibc_par_b = 1
-
-       CASE ( 'reflect' )
-          ibc_par_b = 2
-
-       CASE DEFAULT
-          WRITE( message_string, * )  'unknown boundary condition ',           &
-                                       'bc_par_b = "', TRIM( bc_par_b ), '"'
-          CALL message( 'lpm_init', 'PA0217', 1, 2, 0, 6, 0 )
-
-    END SELECT
-    SELECT CASE ( bc_par_t )
-
-       CASE ( 'absorb' )
-          ibc_par_t = 1
-
-       CASE ( 'reflect' )
-          ibc_par_t = 2
-          
-       CASE ( 'nested' )
-          ibc_par_t = 3
-
-       CASE DEFAULT
-          WRITE( message_string, * ) 'unknown boundary condition ',            &
-                                     'bc_par_t = "', TRIM( bc_par_t ), '"'
-          CALL message( 'lpm_init', 'PA0218', 1, 2, 0, 6, 0 )
-
-    END SELECT
-    SELECT CASE ( bc_par_lr )
-
-       CASE ( 'cyclic' )
-          ibc_par_lr = 0
-
-       CASE ( 'absorb' )
-          ibc_par_lr = 1
-
-       CASE ( 'reflect' )
-          ibc_par_lr = 2
-          
-       CASE ( 'nested' )
-          ibc_par_lr = 3
-
-       CASE DEFAULT
-          WRITE( message_string, * ) 'unknown boundary condition ',   &
-                                     'bc_par_lr = "', TRIM( bc_par_lr ), '"'
-          CALL message( 'lpm_init', 'PA0219', 1, 2, 0, 6, 0 )
-
-    END SELECT
-    SELECT CASE ( bc_par_ns )
-
-       CASE ( 'cyclic' )
-          ibc_par_ns = 0
-
-       CASE ( 'absorb' )
-          ibc_par_ns = 1
-
-       CASE ( 'reflect' )
-          ibc_par_ns = 2
-          
-       CASE ( 'nested' )
-          ibc_par_ns = 3
-
-       CASE DEFAULT
-          WRITE( message_string, * ) 'unknown boundary condition ',   &
-                                     'bc_par_ns = "', TRIM( bc_par_ns ), '"'
-          CALL message( 'lpm_init', 'PA0220', 1, 2, 0, 6, 0 )
-
-    END SELECT
-    SELECT CASE ( splitting_mode )
-
-       CASE ( 'const' )
-          i_splitting_mode = 1
-
-       CASE ( 'cl_av' )
-          i_splitting_mode = 2
-
-       CASE ( 'gb_av' )
-          i_splitting_mode = 3
-
-       CASE DEFAULT
-          WRITE( message_string, * )  'unknown splitting_mode = "',            &
-                                      TRIM( splitting_mode ), '"'
-          CALL message( 'lpm_init', 'PA0146', 1, 2, 0, 6, 0 )
-
-    END SELECT
-    SELECT CASE ( splitting_function )
-
-       CASE ( 'gamma' )
-          isf = 1
-
-       CASE ( 'log' )
-          isf = 2
-
-       CASE ( 'exp' )
-          isf = 3
-
-       CASE DEFAULT
-          WRITE( message_string, * )  'unknown splitting function = "',        &
-                                       TRIM( splitting_function ), '"'
-          CALL message( 'lpm_init', 'PA0147', 1, 2, 0, 6, 0 )
-
-    END SELECT
-
-
-!
-!-- Initialize collision kernels
-    IF ( collision_kernel /= 'none' )  CALL init_kernels
-
-!
-!-- For the first model run of a possible job chain initialize the
-!-- particles, otherwise read the particle data from restart file.
-    IF ( TRIM( initializing_actions ) == 'read_restart_data'  &
-         .AND.  read_particles_from_restartfile )  THEN
-
-       CALL lpm_read_restart_file
-
-    ELSE
-
-!
-!--    Allocate particle arrays and set attributes of the initial set of
-!--    particles, which can be also periodically released at later times.
-       ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
-                 grid_particles(nzb+1:nzt,nys:nyn,nxl:nxr) )
-
-       number_of_particles         = 0
-
-       sort_count = 0
-       prt_count  = 0
-
-!
-!--    initialize counter for particle IDs
-       grid_particles%id_counter = 1
-
-!
-!--    Initialize all particles with dummy values (otherwise errors may
-!--    occur within restart runs). The reason for this is still not clear
-!--    and may be presumably caused by errors in the respective user-interface.
-       zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
-                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
-                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
-                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
-                                      0, 0, 0_idp, .FALSE., -1 )
-
-       particle_groups = particle_groups_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp )
-
-!
-!--    Set values for the density ratio and radius for all particle
-!--    groups, if necessary
-       IF ( density_ratio(1) == 9999999.9_wp )  density_ratio(1) = 0.0_wp
-       IF ( radius(1)        == 9999999.9_wp )  radius(1) = 0.0_wp
-       DO  i = 2, number_of_particle_groups
-          IF ( density_ratio(i) == 9999999.9_wp )  THEN
-             density_ratio(i) = density_ratio(i-1)
-          ENDIF
-          IF ( radius(i) == 9999999.9_wp )  radius(i) = radius(i-1)
-       ENDDO
-
-       DO  i = 1, number_of_particle_groups
-          IF ( density_ratio(i) /= 0.0_wp  .AND.  radius(i) == 0 )  THEN
-             WRITE( message_string, * ) 'particle group #', i, ' has a',       &
-                                        'density ratio /= 0 but radius = 0'
-             CALL message( 'lpm_init', 'PA0215', 1, 2, 0, 6, 0 )
-          ENDIF
-          particle_groups(i)%density_ratio = density_ratio(i)
-          particle_groups(i)%radius        = radius(i)
-       ENDDO
-
-!
-!--    Set a seed value for the random number generator to be exclusively
-!--    used for the particle code. The generated random numbers should be
-!--    different on the different PEs.
-       iran_part = iran_part + myid
-
-!
-!--    Create the particle set, and set the initial particles
-       CALL lpm_create_particle( phase_init )
-       last_particle_release_time = particle_advection_start
-
-!
-!--    User modification of initial particles
-       CALL user_lpm_init
-
-!
-!--    Open file for statistical informations about particle conditions
-       IF ( write_particle_statistics )  THEN
-          CALL check_open( 80 )
-          WRITE ( 80, 8000 )  current_timestep_number, simulated_time,         &
-                              number_of_particles
-          CALL close_file( 80 )
-       ENDIF
-
-    ENDIF
-
-    IF ( nested_run )  CALL pmcp_g_init
-
-!
-!-- To avoid programm abort, assign particles array to the local version of
-!-- first grid cell
-    number_of_particles = prt_count(nzb+1,nys,nxl)
-    particles => grid_particles(nzb+1,nys,nxl)%particles(1:number_of_particles)
-!
-!-- Formats
-8000 FORMAT (I6,1X,F7.2,4X,I10,71X,I10)
-
- END SUBROUTINE lpm_init
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> @todo Missing subroutine description.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_create_particle (phase)
-    
-    USE arrays_3d,                                                             &
-       ONLY:  zw
-    USE lpm_exchange_horiz_mod,                                                &
-        ONLY: lpm_exchange_horiz, lpm_move_particle, realloc_particles_array
-
-    USE lpm_pack_and_sort_mod,                                                 &
-        ONLY: lpm_sort_in_subboxes
-
-    USE particle_attributes,                                                   &
-        ONLY: deleted_particles
-
-    IMPLICIT  NONE
-
-    INTEGER(iwp)               ::  alloc_size  !< relative increase of allocated memory for particles
-    INTEGER(iwp)               ::  i           !< loop variable ( particle groups )
-    INTEGER(iwp)               ::  ip          !< index variable along x
-    INTEGER(iwp)               ::  j           !< loop variable ( particles per point )
-    INTEGER(iwp)               ::  jp          !< index variable along y
-    INTEGER(iwp)               ::  k           !< index variable along z
-    INTEGER(iwp)               ::  k_surf      !< index of surface grid point
-    INTEGER(iwp)               ::  kp          !< index variable along z
-    INTEGER(iwp)               ::  loop_stride !< loop variable for initialization
-    INTEGER(iwp)               ::  n           !< loop variable ( number of particles )
-    INTEGER(iwp)               ::  new_size    !< new size of allocated memory for particles
-
-    INTEGER(iwp), INTENT(IN)   ::  phase       !< mode of inititialization
-
-    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_count !< start address of new particle
-    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_start !< start address of new particle
-
-    LOGICAL                    ::  first_stride !< flag for initialization
-
-    REAL(wp)                   ::  pos_x      !< increment for particle position in x
-    REAL(wp)                   ::  pos_y      !< increment for particle position in y
-    REAL(wp)                   ::  pos_z      !< increment for particle position in z
-    REAL(wp)                   ::  rand_contr !< dummy argument for random position
-
-    TYPE(particle_type),TARGET ::  tmp_particle !< temporary particle used for initialization
-
-!
-!-- Calculate particle positions and store particle attributes, if
-!-- particle is situated on this PE
-    DO  loop_stride = 1, 2
-       first_stride = (loop_stride == 1)
-       IF ( first_stride )   THEN
-          local_count = 0           ! count number of particles
-       ELSE
-          local_count = prt_count   ! Start address of new particles
-       ENDIF
-
-!
-!--    Calculate initial_weighting_factor diagnostically
-       IF ( number_concentration /= -1.0_wp .AND. number_concentration > 0.0_wp ) THEN
-          initial_weighting_factor =  number_concentration  *                        &
-                                      pdx(1) * pdy(1) * pdz(1)
-       END IF
-
-       n = 0
-       DO  i = 1, number_of_particle_groups
-
-          pos_z = psb(i)
-
-          DO WHILE ( pos_z <= pst(i) )
-
-             IF ( pos_z >= zw(0) .AND.  pos_z < zw(nzt) )  THEN
-
-
-                pos_y = pss(i)
-
-                DO WHILE ( pos_y <= psn(i) )
-
-                   IF ( pos_y >= nys * dy  .AND.                  &
-                        pos_y <  ( nyn + 1 ) * dy  ) THEN
-
-                      pos_x = psl(i)
-
-               xloop: DO WHILE ( pos_x <= psr(i) )
-
-                         IF ( pos_x >= nxl * dx  .AND.            &
-                              pos_x <  ( nxr + 1) * dx ) THEN
-
-                            DO  j = 1, particles_per_point
-
-
-                               n = n + 1
-                               tmp_particle%x             = pos_x
-                               tmp_particle%y             = pos_y
-                               tmp_particle%z             = pos_z
-                               tmp_particle%age           = 0.0_wp
-                               tmp_particle%age_m         = 0.0_wp
-                               tmp_particle%dt_sum        = 0.0_wp
-                               tmp_particle%e_m           = 0.0_wp
-                               tmp_particle%rvar1         = 0.0_wp
-                               tmp_particle%rvar2         = 0.0_wp
-                               tmp_particle%rvar3         = 0.0_wp
-                               tmp_particle%speed_x       = 0.0_wp
-                               tmp_particle%speed_y       = 0.0_wp
-                               tmp_particle%speed_z       = 0.0_wp
-                               tmp_particle%origin_x      = pos_x
-                               tmp_particle%origin_y      = pos_y
-                               tmp_particle%origin_z      = pos_z
-                               IF ( curvature_solution_effects )  THEN
-                                  tmp_particle%aux1      = 0.0_wp    ! dry aerosol radius
-                                  tmp_particle%aux2      = dt_3d     ! last Rosenbrock timestep
-                               ELSE
-                                  tmp_particle%aux1      = 0.0_wp    ! free to use
-                                  tmp_particle%aux2      = 0.0_wp    ! free to use
-                               ENDIF
-                               tmp_particle%radius        = particle_groups(i)%radius
-                               tmp_particle%weight_factor = initial_weighting_factor
-                               tmp_particle%class         = 1
-                               tmp_particle%group         = i
-                               tmp_particle%id            = 0_idp
-                               tmp_particle%particle_mask = .TRUE.
-                               tmp_particle%block_nr      = -1
-!
-!--                            Determine the grid indices of the particle position
-                               ip = INT( tmp_particle%x * ddx )
-                               jp = INT( tmp_particle%y * ddy )
-                               kp = INT( tmp_particle%z / dz(1) + 1 + offset_ocean_nzt )
-                               DO WHILE( zw(kp) < tmp_particle%z ) 
-                                  kp = kp + 1
-                               ENDDO
-                               DO WHILE( zw(kp-1) > tmp_particle%z )
-                                  kp = kp - 1
-                               ENDDO 
-!
-!--                            Determine surface level. Therefore, check for
-!--                            upward-facing wall on w-grid. 
-                               k_surf = get_topography_top_index_ji( jp, ip, 'w' )
-
-                               IF ( seed_follows_topography )  THEN
-!
-!--                               Particle height is given relative to topography
-                                  kp = kp + k_surf
-                                  tmp_particle%z = tmp_particle%z + zw(k_surf)
-!--                               Skip particle release if particle position is
-!--                               above model top, or within topography in case
-!--                               of overhanging structures.
-                                  IF ( kp > nzt  .OR.                          &
-                                 .NOT. BTEST( wall_flags_0(kp,jp,ip), 0 ) )  THEN
-                                     pos_x = pos_x + pdx(i)
-                                     CYCLE xloop
-                                  ENDIF
-!
-!--                            Skip particle release if particle position is
-!--                            below surface, or within topography in case
-!--                            of overhanging structures.
-                               ELSEIF ( .NOT. seed_follows_topography .AND.    &
-                                         tmp_particle%z <= zw(k_surf)  .OR.    &
-                                        .NOT. BTEST( wall_flags_0(kp,jp,ip), 0 ) )&
-                               THEN
-                                  pos_x = pos_x + pdx(i)
-                                  CYCLE xloop
-                               ENDIF
-
-                               local_count(kp,jp,ip) = local_count(kp,jp,ip) + 1
-
-                               IF ( .NOT. first_stride )  THEN
-                                  IF ( ip < nxl  .OR.  jp < nys  .OR.  kp < nzb+1 )  THEN
-                                     write(6,*) 'xl ',ip,jp,kp,nxl,nys,nzb+1
-                                  ENDIF
-                                  IF ( ip > nxr  .OR.  jp > nyn  .OR.  kp > nzt )  THEN
-                                     write(6,*) 'xu ',ip,jp,kp,nxr,nyn,nzt
-                                  ENDIF
-                                  grid_particles(kp,jp,ip)%particles(local_count(kp,jp,ip)) = tmp_particle
-
-                               ENDIF
-                            ENDDO
-
-                         ENDIF
-
-                         pos_x = pos_x + pdx(i)
-
-                      ENDDO xloop
-
-                   ENDIF
-
-                   pos_y = pos_y + pdy(i)
-
-                ENDDO
-
-             ENDIF
-
-             pos_z = pos_z + pdz(i)
-
-          ENDDO
-
-       ENDDO
-
-       IF ( first_stride )  THEN
-          DO  ip = nxl, nxr
-             DO  jp = nys, nyn
-                DO  kp = nzb+1, nzt
-                   IF ( phase == PHASE_INIT )  THEN
-                      IF ( local_count(kp,jp,ip) > 0 )  THEN
-                         alloc_size = MAX( INT( local_count(kp,jp,ip) *        &
-                            ( 1.0_wp + alloc_factor / 100.0_wp ) ),            &
-                            min_nr_particle )
-                      ELSE
-                         alloc_size = min_nr_particle
-                      ENDIF
-                      ALLOCATE(grid_particles(kp,jp,ip)%particles(1:alloc_size))
-                      DO  n = 1, alloc_size
-                         grid_particles(kp,jp,ip)%particles(n) = zero_particle
-                      ENDDO
-                   ELSEIF ( phase == PHASE_RELEASE )  THEN
-                      IF ( local_count(kp,jp,ip) > 0 )  THEN
-                         new_size   = local_count(kp,jp,ip) + prt_count(kp,jp,ip)
-                         alloc_size = MAX( INT( new_size * ( 1.0_wp +          &
-                            alloc_factor / 100.0_wp ) ), min_nr_particle )
-                         IF( alloc_size > SIZE( grid_particles(kp,jp,ip)%particles) )  THEN
-                            CALL realloc_particles_array(ip,jp,kp,alloc_size)
-                         ENDIF
-                      ENDIF
-                   ENDIF
-                ENDDO
-             ENDDO
-          ENDDO
-       ENDIF
-
-    ENDDO
-
-
-
-    local_start = prt_count+1
-    prt_count   = local_count
-
-!
-!-- Calculate particle IDs
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-
-             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
-
-                particles(n)%id = 10000_idp**3 * grid_particles(kp,jp,ip)%id_counter + &
-                                  10000_idp**2 * kp + 10000_idp * jp + ip
-!
-!--             Count the number of particles that have been released before
-                grid_particles(kp,jp,ip)%id_counter =                          &
-                                         grid_particles(kp,jp,ip)%id_counter + 1
-
-             ENDDO
-
-          ENDDO
-       ENDDO
-    ENDDO
-
-!
-!-- Initialize aerosol background spectrum
-    IF ( curvature_solution_effects )  THEN
-       CALL lpm_init_aerosols(local_start)
-    ENDIF
-
-!
-!-- Add random fluctuation to particle positions.
-    IF ( random_start_position )  THEN
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             DO  kp = nzb+1, nzt
-                number_of_particles = prt_count(kp,jp,ip)
-                IF ( number_of_particles <= 0 )  CYCLE
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-!
-!--             Move only new particles. Moreover, limit random fluctuation
-!--             in order to prevent that particles move more than one grid box,
-!--             which would lead to problems concerning particle exchange
-!--             between processors in case pdx/pdy are larger than dx/dy,
-!--             respectively.
-                DO  n = local_start(kp,jp,ip), number_of_particles
-                   IF ( psl(particles(n)%group) /= psr(particles(n)%group) )  THEN
-                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
-                                     pdx(particles(n)%group)
-                      particles(n)%x = particles(n)%x +                        &
-                              MERGE( rand_contr, SIGN( dx, rand_contr ),       &
-                                     ABS( rand_contr ) < dx                    &
-                                   )
-                   ENDIF
-                   IF ( pss(particles(n)%group) /= psn(particles(n)%group) )  THEN
-                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
-                                     pdy(particles(n)%group)
-                      particles(n)%y = particles(n)%y +                        &
-                              MERGE( rand_contr, SIGN( dy, rand_contr ),       &
-                                     ABS( rand_contr ) < dy                    &
-                                   )
-                   ENDIF
-                   IF ( psb(particles(n)%group) /= pst(particles(n)%group) )  THEN
-                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
-                                     pdz(particles(n)%group)
-                      particles(n)%z = particles(n)%z +                        &
-                              MERGE( rand_contr, SIGN( dzw(kp), rand_contr ),  &
-                                     ABS( rand_contr ) < dzw(kp)               &
-                                   )
-                   ENDIF
-                ENDDO
-!
-!--             Identify particles located outside the model domain and reflect
-!--             or absorb them if necessary.
-                CALL lpm_boundary_conds( 'bottom/top', i, j, k )
-!
-!--             Furthermore, remove particles located in topography. Note, as
-!--             the particle speed is still zero at this point, wall
-!--             reflection boundary conditions will not work in this case.
-                particles =>                                                   &
-                       grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                DO  n = local_start(kp,jp,ip), number_of_particles
-                   i = particles(n)%x * ddx
-                   j = particles(n)%y * ddy
-                   k = particles(n)%z / dz(1) + 1 + offset_ocean_nzt
-                   DO WHILE( zw(k) < particles(n)%z )
-                      k = k + 1
-                   ENDDO
-                   DO WHILE( zw(k-1) > particles(n)%z )
-                      k = k - 1
-                   ENDDO
-!
-!--                Check if particle is within topography
-                   IF ( .NOT. BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
-                      particles(n)%particle_mask = .FALSE.
-                      deleted_particles = deleted_particles + 1
-                   ENDIF
-
-                ENDDO
-             ENDDO
-          ENDDO
-       ENDDO
-!
-!--    Exchange particles between grid cells and processors
-       CALL lpm_move_particle
-       CALL lpm_exchange_horiz
-
-    ENDIF
-!
-!-- In case of random_start_position, delete particles identified by
-!-- lpm_exchange_horiz and lpm_boundary_conds. Then sort particles into blocks,
-!-- which is needed for a fast interpolation of the LES fields on the particle
-!-- position.
-    CALL lpm_sort_in_subboxes
-
-!
-!-- Determine the current number of particles
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles         = number_of_particles                 &
-                                           + prt_count(kp,jp,ip)
-          ENDDO
-       ENDDO
-    ENDDO
-!
-!-- Calculate the number of particles of the total domain
-#if defined( __parallel )
-    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-    CALL MPI_ALLREDUCE( number_of_particles, total_number_of_particles, 1, &
-    MPI_INTEGER, MPI_SUM, comm2d, ierr )
-#else
-    total_number_of_particles = number_of_particles
-#endif
-
-    RETURN
-
- END SUBROUTINE lpm_create_particle
-
- SUBROUTINE lpm_init_aerosols(local_start)
-
-    USE arrays_3d,                                                             &
-        ONLY: hyp, pt, q, exner
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY: molecular_weight_of_solute, molecular_weight_of_water, magnus,   &
-              pi, rd_d_rv, rho_l, r_v, rho_s, vanthoff
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY: aero_species, aero_type, aero_weight, log_sigma, na, rm
-
-    IMPLICIT NONE
-
-    REAL(wp)  :: afactor            !< curvature effects
-    REAL(wp)  :: bfactor            !< solute effects
-    REAL(wp)  :: dlogr              !< logarithmic width of radius bin
-    REAL(wp)  :: e_a                !< vapor pressure
-    REAL(wp)  :: e_s                !< saturation vapor pressure
-    REAL(wp)  :: rmin = 0.005e-6_wp !< minimum aerosol radius
-    REAL(wp)  :: rmax = 10.0e-6_wp  !< maximum aerosol radius
-    REAL(wp)  :: r_mid              !< mean radius of bin
-    REAL(wp)  :: r_l                !< left radius of bin
-    REAL(wp)  :: r_r                !< right radius of bin
-    REAL(wp)  :: sigma              !< surface tension
-    REAL(wp)  :: t_int              !< temperature
-
-    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(IN) ::  local_start !<
-
-    INTEGER(iwp)  :: n              !<
-    INTEGER(iwp)  :: ip             !<
-    INTEGER(iwp)  :: jp             !<
-    INTEGER(iwp)  :: kp             !<
-
-!
-!-- Set constants for different aerosol species
-    IF ( TRIM(aero_species) .EQ. 'nacl' ) THEN
-       molecular_weight_of_solute = 0.05844_wp 
-       rho_s                      = 2165.0_wp
-       vanthoff                   = 2.0_wp
-    ELSEIF ( TRIM(aero_species) .EQ. 'c3h4o4' ) THEN
-       molecular_weight_of_solute = 0.10406_wp 
-       rho_s                      = 1600.0_wp
-       vanthoff                   = 1.37_wp
-    ELSEIF ( TRIM(aero_species) .EQ. 'nh4o3' ) THEN
-       molecular_weight_of_solute = 0.08004_wp 
-       rho_s                      = 1720.0_wp
-       vanthoff                   = 2.31_wp
-    ELSE
-       WRITE( message_string, * ) 'unknown aerosol species ',   &
-                                'aero_species = "', TRIM( aero_species ), '"'
-       CALL message( 'lpm_init', 'PA0470', 1, 2, 0, 6, 0 )
-    ENDIF
-!
-!-- The following typical aerosol spectra are taken from Jaenicke (1993):
-!-- Tropospheric aerosols. Published in Aerosol-Cloud-Climate Interactions.
-    IF ( TRIM(aero_type) .EQ. 'polar' )  THEN
-       na        = (/ 2.17e1, 1.86e-1, 3.04e-4 /) * 1.0E6
-       rm        = (/ 0.0689, 0.375, 4.29 /) * 1.0E-6
-       log_sigma = (/ 0.245, 0.300, 0.291 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'background' )  THEN
-       na        = (/ 1.29e2, 5.97e1, 6.35e1 /) * 1.0E6
-       rm        = (/ 0.0036, 0.127, 0.259 /) * 1.0E-6
-       log_sigma = (/ 0.645, 0.253, 0.425 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'maritime' )  THEN
-       na        = (/ 1.33e2, 6.66e1, 3.06e0 /) * 1.0E6
-       rm        = (/ 0.0039, 0.133, 0.29 /) * 1.0E-6
-       log_sigma = (/ 0.657, 0.210, 0.396 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'continental' )  THEN
-       na        = (/ 3.20e3, 2.90e3, 3.00e-1 /) * 1.0E6
-       rm        = (/ 0.01, 0.058, 0.9 /) * 1.0E-6
-       log_sigma = (/ 0.161, 0.217, 0.380 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'desert' )  THEN
-       na        = (/ 7.26e2, 1.14e3, 1.78e-1 /) * 1.0E6
-       rm        = (/ 0.001, 0.0188, 10.8 /) * 1.0E-6
-       log_sigma = (/ 0.247, 0.770, 0.438 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'rural' )  THEN
-       na        = (/ 6.65e3, 1.47e2, 1.99e3 /) * 1.0E6
-       rm        = (/ 0.00739, 0.0269, 0.0419 /) * 1.0E-6
-       log_sigma = (/ 0.225, 0.557, 0.266 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'urban' )  THEN
-       na        = (/ 9.93e4, 1.11e3, 3.64e4 /) * 1.0E6
-       rm        = (/ 0.00651, 0.00714, 0.0248 /) * 1.0E-6
-       log_sigma = (/ 0.245, 0.666, 0.337 /)
-    ELSEIF ( TRIM(aero_type) .EQ. 'user' )  THEN
-       CONTINUE
-    ELSE
-       WRITE( message_string, * ) 'unknown aerosol type ',   &
-                                'aero_type = "', TRIM( aero_type ), '"'
-       CALL message( 'lpm_init', 'PA0459', 1, 2, 0, 6, 0 )
-    ENDIF
-
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles <= 0 )  CYCLE
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-
-             dlogr   = ( LOG10(rmax) - LOG10(rmin) ) / ( number_of_particles - local_start(kp,jp,ip) + 1 )
-!
-!--          Initialize the aerosols with a predefined spectral distribution
-!--          of the dry radius (logarithmically increasing bins) and a varying
-!--          weighting factor
-             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
-
-                r_l   = 10.0**( LOG10( rmin ) + (n-1) * dlogr )
-                r_r   = 10.0**( LOG10( rmin ) + n * dlogr )
-                r_mid = SQRT( r_l * r_r )
-
-                particles(n)%aux1          = r_mid
-                particles(n)%weight_factor =                                           &
-                   ( na(1) / ( SQRT( 2.0 * pi ) * log_sigma(1) ) *                     &
-                     EXP( - LOG10( r_mid / rm(1) )**2 / ( 2.0 * log_sigma(1)**2 ) ) +  &
-                     na(2) / ( SQRT( 2.0 * pi ) * log_sigma(2) ) *                     &
-                     EXP( - LOG10( r_mid / rm(2) )**2 / ( 2.0 * log_sigma(2)**2 ) ) +  &
-                     na(3) / ( SQRT( 2.0 * pi ) * log_sigma(3) ) *                     &
-                     EXP( - LOG10( r_mid / rm(3) )**2 / ( 2.0 * log_sigma(3)**2 ) )    &
-                   ) * ( LOG10(r_r) - LOG10(r_l) ) * ( dx * dy * dzw(kp) )
-
-!
-!--             Multiply weight_factor with the namelist parameter aero_weight
-!--             to increase or decrease the number of simulated aerosols
-                particles(n)%weight_factor = particles(n)%weight_factor * aero_weight
-
-                IF ( particles(n)%weight_factor - FLOOR(particles(n)%weight_factor,KIND=wp) &
-                     .GT. random_function( iran_part ) )  THEN
-                   particles(n)%weight_factor = FLOOR(particles(n)%weight_factor,KIND=wp) + 1.0_wp
-                ELSE
-                   particles(n)%weight_factor = FLOOR(particles(n)%weight_factor,KIND=wp)
-                ENDIF
-!
-!--             Unnecessary particles will be deleted
-                IF ( particles(n)%weight_factor .LE. 0.0 )  particles(n)%particle_mask = .FALSE.
-
-             ENDDO
-!
-!--          Set particle radius to equilibrium radius based on the environmental
-!--          supersaturation (Khvorostyanov and Curry, 2007, JGR). This avoids
-!--          the sometimes lengthy growth toward their equilibrium radius within
-!--          the simulation.
-             t_int  = pt(kp,jp,ip) * exner(kp)
-
-             e_s = magnus( t_int )
-             e_a = q(kp,jp,ip) * hyp(kp) / ( q(kp,jp,ip) + rd_d_rv )
-
-             sigma   = 0.0761_wp - 0.000155_wp * ( t_int - 273.15_wp )
-             afactor = 2.0_wp * sigma / ( rho_l * r_v * t_int )
-
-             bfactor = vanthoff * molecular_weight_of_water *    &
-                       rho_s / ( molecular_weight_of_solute * rho_l )
-!
-!--          The formula is only valid for subsaturated environments. For
-!--          supersaturations higher than -5 %, the supersaturation is set to -5%.
-             IF ( e_a / e_s >= 0.95_wp )  e_a = 0.95_wp * e_s
-
-             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
-!
-!--             For details on this equation, see Eq. (14) of Khvorostyanov and
-!--             Curry (2007, JGR)
-                particles(n)%radius = bfactor**0.3333333_wp *                  &
-                   particles(n)%aux1 / ( 1.0_wp - e_a / e_s )**0.3333333_wp / &
-                   ( 1.0_wp + ( afactor / ( 3.0_wp * bfactor**0.3333333_wp *   &
-                     particles(n)%aux1 ) ) /                                  &
-                     ( 1.0_wp - e_a / e_s )**0.6666666_wp                      &
-                   )
-
-             ENDDO
-
-          ENDDO
-       ENDDO
-    ENDDO
-
- END SUBROUTINE lpm_init_aerosols
-
-END MODULE lpm_init_mod
Index: palm/trunk/SOURCE/lpm_init_sgs_tke.f90
===================================================================
--- palm/trunk/SOURCE/lpm_init_sgs_tke.f90	(revision 4015)
+++ 	(revision )
@@ -1,343 +1,0 @@
-!> @file lpm_init_sgs_tke.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Improve comment
-! 
-! 3981 2019-05-15 14:43:01Z suehring
-! Bugfix in particle nesting, set boundary conditions at non-cyclic boundaries
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! unused variables removed
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2233 2017-05-30 18:08:54Z suehring
-!
-! 2232 2017-05-30 17:47:52Z suehring
-! Adjustments according to new topography realization
-! 
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1929 2016-06-09 16:25:25Z suehring
-! sgs_wfu_par, sgs_wfv_par and sgs_wfw_par are replaced by sgs_wf_par
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-! Kind definition added to all floating point numbers.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Calculates quantities required for considering the SGS velocity fluctuations
-!> in the particle transport by a stochastic approach. The respective
-!> quantities are: SGS-TKE gradients and horizontally averaged profiles of the
-!> SGS TKE and the resolved-scale velocity variances. 
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_init_sgs_tke
- 
-
-    USE arrays_3d,                                                             &
-        ONLY:  de_dx, de_dy, de_dz, diss, e, u, v, w, zu
-
-    USE control_parameters,                                                    &
-        ONLY:  bc_dirichlet_l, bc_dirichlet_n, bc_dirichlet_r, bc_dirichlet_s
-        
-    USE grid_variables,                                                        &
-        ONLY:  ddx, ddy
-
-    USE indices,                                                               &
-        ONLY:  nbgp, ngp_2dh_outer, nxl, nxr, nyn, nys, nzb, nzt, wall_flags_0
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  sgs_wf_part
-
-    USE pegrid
-
-    USE statistics,                                                            &
-        ONLY:  flow_statistics_called, hom, sums, sums_l
-
-    USE surface_mod,                                                           &
-        ONLY:  bc_h
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i      !< index variable along x
-    INTEGER(iwp) ::  j      !< index variable along y
-    INTEGER(iwp) ::  k      !< index variable along z
-    INTEGER(iwp) ::  m      !< running index for the surface elements 
-
-    REAL(wp) ::  flag1      !< flag to mask topography
-
-!
-!-- TKE gradient along x and y
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb, nzt+1
-
-             IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.               &
-                        BTEST( wall_flags_0(k,j,i), 0   )  .AND.               &
-                        BTEST( wall_flags_0(k,j,i+1), 0 ) )                    &
-             THEN
-                de_dx(k,j,i) = 2.0_wp * sgs_wf_part *                          &
-                               ( e(k,j,i+1) - e(k,j,i) ) * ddx
-             ELSEIF ( BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.                 &
-                      BTEST( wall_flags_0(k,j,i), 0   )  .AND.                 &
-                .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                      &
-             THEN
-                de_dx(k,j,i) = 2.0_wp * sgs_wf_part *                          &
-                               ( e(k,j,i) - e(k,j,i-1) ) * ddx
-             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i), 22   )  .AND.          &
-                      .NOT. BTEST( wall_flags_0(k,j,i+1), 22 ) )               &   
-             THEN
-                de_dx(k,j,i) = 0.0_wp
-             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 22 )  .AND.          &
-                      .NOT. BTEST( wall_flags_0(k,j,i), 22   ) )               &
-             THEN
-                de_dx(k,j,i) = 0.0_wp
-             ELSE
-                de_dx(k,j,i) = sgs_wf_part * ( e(k,j,i+1) - e(k,j,i-1) ) * ddx
-             ENDIF
-
-             IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.               &
-                        BTEST( wall_flags_0(k,j,i), 0   )  .AND.               &
-                        BTEST( wall_flags_0(k,j+1,i), 0 ) )                    &
-             THEN
-                de_dy(k,j,i) = 2.0_wp * sgs_wf_part *                          &
-                               ( e(k,j+1,i) - e(k,j,i) ) * ddy
-             ELSEIF ( BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.                 &
-                      BTEST( wall_flags_0(k,j,i), 0   )  .AND.                 &
-                .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                      &
-             THEN
-                de_dy(k,j,i) = 2.0_wp * sgs_wf_part *                          &
-                               ( e(k,j,i) - e(k,j-1,i) ) * ddy
-             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i), 22   )  .AND.          &
-                      .NOT. BTEST( wall_flags_0(k,j+1,i), 22 ) )               &   
-             THEN
-                de_dy(k,j,i) = 0.0_wp
-             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 22 )  .AND.          &
-                      .NOT. BTEST( wall_flags_0(k,j,i), 22   ) )               &
-             THEN
-                de_dy(k,j,i) = 0.0_wp
-             ELSE
-                de_dy(k,j,i) = sgs_wf_part * ( e(k,j+1,i) - e(k,j-1,i) ) * ddy
-             ENDIF
-
-          ENDDO
-       ENDDO
-    ENDDO
-
-!
-!-- TKE gradient along z at topograhy and  including bottom and top boundary conditions
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb+1, nzt-1
-!
-!--          Flag to mask topography
-             flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0  ) )
-
-             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
-                           ( e(k+1,j,i) - e(k-1,j,i) ) / ( zu(k+1) - zu(k-1) ) &
-                                                 * flag1 
-          ENDDO
-!
-!--       upward-facing surfaces
-          DO  m = bc_h(0)%start_index(j,i), bc_h(0)%end_index(j,i)
-             k            = bc_h(0)%k(m)
-             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
-                           ( e(k+1,j,i) - e(k,j,i)   ) / ( zu(k+1) - zu(k) )
-          ENDDO
-!
-!--       downward-facing surfaces
-          DO  m = bc_h(1)%start_index(j,i), bc_h(1)%end_index(j,i)
-             k            = bc_h(1)%k(m)
-             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
-                           ( e(k,j,i) - e(k-1,j,i)   ) / ( zu(k) - zu(k-1) )
-          ENDDO
-
-          de_dz(nzb,j,i)   = 0.0_wp
-          de_dz(nzt,j,i)   = 0.0_wp
-          de_dz(nzt+1,j,i) = 0.0_wp
-       ENDDO
-    ENDDO
-!
-!-- Ghost point exchange
-    CALL exchange_horiz( de_dx, nbgp )
-    CALL exchange_horiz( de_dy, nbgp )
-    CALL exchange_horiz( de_dz, nbgp )
-    CALL exchange_horiz( diss, nbgp  )
-!
-!-- Set boundary conditions at non-periodic boundaries. Note, at non-period
-!-- boundaries zero-gradient boundary conditions are set for the subgrid TKE.
-!-- Thus, TKE gradients normal to the respective lateral boundaries are zero, 
-!-- while tangetial TKE gradients then must be the same as within the prognostic
-!-- domain.  
-    IF ( bc_dirichlet_l )  THEN
-       de_dx(:,:,-1) = 0.0_wp
-       de_dy(:,:,-1) = de_dy(:,:,0) 
-       de_dz(:,:,-1) = de_dz(:,:,0)
-    ENDIF
-    IF ( bc_dirichlet_r )  THEN
-       de_dx(:,:,nxr+1) = 0.0_wp
-       de_dy(:,:,nxr+1) = de_dy(:,:,nxr) 
-       de_dz(:,:,nxr+1) = de_dz(:,:,nxr)
-    ENDIF
-    IF ( bc_dirichlet_n )  THEN
-       de_dx(:,nyn+1,:) = de_dx(:,nyn,:)
-       de_dy(:,nyn+1,:) = 0.0_wp 
-       de_dz(:,nyn+1,:) = de_dz(:,nyn,:)
-    ENDIF
-    IF ( bc_dirichlet_s )  THEN
-       de_dx(:,nys-1,:) = de_dx(:,nys,:)
-       de_dy(:,nys-1,:) = 0.0_wp 
-       de_dz(:,nys-1,:) = de_dz(:,nys,:)
-    ENDIF  
-!
-!-- Calculate the horizontally averaged profiles of SGS TKE and resolved
-!-- velocity variances (they may have been already calculated in routine
-!-- flow_statistics).
-    IF ( .NOT. flow_statistics_called )  THEN
-
-!
-!--    First calculate horizontally averaged profiles of the horizontal
-!--    velocities.
-       sums_l(:,1,0) = 0.0_wp
-       sums_l(:,2,0) = 0.0_wp
-
-       DO  i = nxl, nxr
-          DO  j =  nys, nyn
-             DO  k = nzb, nzt+1
-!
-!--             Flag indicating vicinity of wall
-                flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 24 ) )
-
-                sums_l(k,1,0)  = sums_l(k,1,0)  + u(k,j,i) * flag1
-                sums_l(k,2,0)  = sums_l(k,2,0)  + v(k,j,i) * flag1
-             ENDDO
-          ENDDO
-       ENDDO
-
-#if defined( __parallel )
-!
-!--    Compute total sum from local sums
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,1,0), sums(nzb,1), nzt+2-nzb, &
-                           MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,2,0), sums(nzb,2), nzt+2-nzb, &
-                              MPI_REAL, MPI_SUM, comm2d, ierr )
-#else
-       sums(:,1) = sums_l(:,1,0)
-       sums(:,2) = sums_l(:,2,0)
-#endif
-
-!
-!--    Final values are obtained by division by the total number of grid
-!--    points used for the summation.
-       hom(:,1,1,0) = sums(:,1) / ngp_2dh_outer(:,0)   ! u
-       hom(:,1,2,0) = sums(:,2) / ngp_2dh_outer(:,0)   ! v
-
-!
-!--    Now calculate the profiles of SGS TKE and the resolved-scale
-!--    velocity variances
-       sums_l(:,8,0)  = 0.0_wp
-       sums_l(:,30,0) = 0.0_wp
-       sums_l(:,31,0) = 0.0_wp
-       sums_l(:,32,0) = 0.0_wp
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb, nzt+1
-!
-!--             Flag indicating vicinity of wall
-                flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 24 ) )
-
-                sums_l(k,8,0)  = sums_l(k,8,0)  + e(k,j,i)                       * flag1
-                sums_l(k,30,0) = sums_l(k,30,0) + ( u(k,j,i) - hom(k,1,1,0) )**2 * flag1
-                sums_l(k,31,0) = sums_l(k,31,0) + ( v(k,j,i) - hom(k,1,2,0) )**2 * flag1
-                sums_l(k,32,0) = sums_l(k,32,0) + w(k,j,i)**2                    * flag1
-             ENDDO
-          ENDDO
-       ENDDO
-
-#if defined( __parallel )
-!
-!--    Compute total sum from local sums
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,8,0), sums(nzb,8), nzt+2-nzb, &
-                           MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,30,0), sums(nzb,30), nzt+2-nzb, &
-                           MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,31,0), sums(nzb,31), nzt+2-nzb, &
-                           MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,32,0), sums(nzb,32), nzt+2-nzb, &
-                           MPI_REAL, MPI_SUM, comm2d, ierr )
-
-#else
-       sums(:,8)  = sums_l(:,8,0)
-       sums(:,30) = sums_l(:,30,0)
-       sums(:,31) = sums_l(:,31,0)
-       sums(:,32) = sums_l(:,32,0)
-#endif
-
-!
-!--    Final values are obtained by division by the total number of grid
-!--    points used for the summation.
-       hom(:,1,8,0)  = sums(:,8)  / ngp_2dh_outer(:,0)   ! e
-       hom(:,1,30,0) = sums(:,30) / ngp_2dh_outer(:,0)   ! u*2
-       hom(:,1,31,0) = sums(:,31) / ngp_2dh_outer(:,0)   ! v*2 
-       hom(:,1,32,0) = sums(:,32) / ngp_2dh_outer(:,0)   ! w*2
-
-    ENDIF
-
- END SUBROUTINE lpm_init_sgs_tke
Index: palm/trunk/SOURCE/lpm_merging.f90
===================================================================
--- palm/trunk/SOURCE/lpm_merging.f90	(revision 4015)
+++ 	(revision )
@@ -1,130 +1,0 @@
-!> @file lpm_merging.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! unused variables removed
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! Change in file header (GPL part)
-! Added comments
-! 
-! 2263 2017-06-08 14:59:01Z schwenkel
-! Initial revision
-! 
-!
-! Description:
-! ------------
-! This routine is a part of the Lagrangian particle model. Two Super droplets 
-! which fulfill certain criterion's (e.g. a big weighting factor and a small
-! radius) can be merged into one super droplet with a increased number of 
-! represented particles of the super droplet. This mechanism ensures an
-! improved a feasible amount of computational costs. The limits of particle 
-! creation should be chosen carefully! The idea of this algorithm is based on 
-! Unterstrasser and Soelch, 2014. 
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_merging
-
-
-    USE arrays_3d,                                                             &
-        ONLY:  ql
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  deleted_particles, grid_particles, initial_weighting_factor,    &     
-               merge_drp, number_of_particles, particles, prt_count,           &
-               radius_merge, sum_merge_drp, weight_factor_merge
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i         !<      
-    INTEGER(iwp) ::  j         !<       
-    INTEGER(iwp) ::  k         !<       
-    INTEGER(iwp) ::  n         !<   
-    
-    REAL(wp) ::  ql_crit = 1.0E-5_wp  !< threshold lwc for cloudy grid cells 
-                                      !< (e.g. Siebesma et al 2003, JAS, 60)
-    
-    CALL cpu_log( log_point_s(81), 'lpm_merging', 'start' )
-
-    merge_drp  = 0
-    
-    IF ( weight_factor_merge == -1.0_wp )  THEN
-       weight_factor_merge = 0.5_wp * initial_weighting_factor 
-    ENDIF
-
-    DO  i = nxl, nxr
-       DO  j = nys, nyn
-          DO  k = nzb+1, nzt
-   
-             number_of_particles = prt_count(k,j,i)
-             IF ( number_of_particles <= 0  .OR.                               &
-                   ql(k,j,i) >= ql_crit )  CYCLE
-             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-!
-!--          Start merging operations: This routine delete super droplets with
-!--          a small radius (radius <= radius_merge) and a low weighting 
-!--          factor (weight_factor  <= weight_factor_merge). The number of 
-!--          represented particles will be added to the next particle of the 
-!--          particle array. Tests showed that this simplified method can be 
-!--          used because it will only take place outside of cloudy grid 
-!--          boxes where ql <= 1.0E-5 kg/kg. Therefore, especially former cloned   
-!--          and subsequent evaporated super droplets will be merged.
-             DO  n = 1, number_of_particles-1
-                IF ( particles(n)%particle_mask                    .AND.       &
-                     particles(n+1)%particle_mask                  .AND.       &
-                     particles(n)%radius        <= radius_merge    .AND.       &
-                     particles(n)%weight_factor <= weight_factor_merge )       &   
-                THEN
-                   particles(n+1)%weight_factor  =                             &
-                                       particles(n+1)%weight_factor +          &
-                                       ( particles(n)%radius**3     /          &
-                                         particles(n+1)%radius**3   *          &
-                                         particles(n)%weight_factor            &
-                                       )
-                   particles(n)%particle_mask = .FALSE.
-                   deleted_particles          = deleted_particles + 1 
-                   merge_drp                  = merge_drp + 1
-                
-                ENDIF
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDDO
-       
-    sum_merge_drp = sum_merge_drp + merge_drp
-
-    CALL cpu_log( log_point_s(81), 'lpm_merging', 'stop' )
-
- END SUBROUTINE lpm_merging
Index: palm/trunk/SOURCE/lpm_pack_arrays.f90
===================================================================
--- palm/trunk/SOURCE/lpm_pack_arrays.f90	(revision 4015)
+++ 	(revision )
@@ -1,356 +1,0 @@
-!> @file lpm_pack_arrays.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! unused variables removed
-! 
-! 2801 2018-02-14 16:01:55Z thiele
-! Introduce particle transfer in nested models.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2628 2017-11-20 12:40:38Z schwenkel
-! Enabled particle advection with grid stretching.
-! 
-! 2609 2017-11-14 14:14:44Z schwenkel
-! Integrated subroutine pack_and_sort into lpm_sort_in_subboxes
-! 
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done
-! 
-! 2417 2017-09-06 15:22:27Z suehring
-! New routine which sorts particles into particles that completed and not 
-! completed the LES timestep. 
-! 
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed. Unused variables removed.
-!
-! 1685 2015-10-08 07:32:13Z raasch
-! bugfix concerning vertical index calculation in case of ocean
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Pack particle arrays, which means eliminate those elements marked for
-!> deletion and move data with higher index values to these free indices.
-!> Determine the new number of particles.
-!> Moreover, particles are also sorted into groups finished and not finished
-!> its timestep. 
-!------------------------------------------------------------------------------!
- MODULE lpm_pack_and_sort_mod
- 
-
-    USE particle_attributes,                                                   &
-        ONLY:  grid_particles, number_of_particles, offset_ocean_nzt,          &
-               particles, particle_type, prt_count
-
-    PRIVATE
-    PUBLIC lpm_sort_in_subboxes, lpm_pack, lpm_sort_timeloop_done
-
-    INTERFACE lpm_sort_in_subboxes
-       MODULE PROCEDURE lpm_sort_in_subboxes
-    END INTERFACE lpm_sort_in_subboxes
-
-    INTERFACE lpm_pack
-       MODULE PROCEDURE lpm_pack
-    END INTERFACE lpm_pack
-
-    INTERFACE lpm_sort_timeloop_done
-       MODULE PROCEDURE lpm_sort_timeloop_done
-    END INTERFACE lpm_sort_timeloop_done
-
-
- CONTAINS
-
-!------------------------------------------------------------------------------!
-! Description:
-! -----------
-!> Routine for the whole processor
-!> Sort all particles into the 8 respective subgrid boxes
-!------------------------------------------------------------------------------!
-    SUBROUTINE lpm_sort_in_subboxes
-
-       USE cpulog,                                                             &
-          ONLY:  cpu_log, log_point_s
-
-       USE indices,                                                            &
-          ONLY:  nxl, nxr, nys, nyn, nzb, nzt
-
-       USE kinds
-
-       USE grid_variables,                                                     &
-          ONLY: dx,dy,ddx, ddy
-           
-       USE arrays_3d,                                                          &
-          ONLY:  zu
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  i  !<
-       INTEGER(iwp) ::  ip !<
-       INTEGER(iwp) ::  is !<
-       INTEGER(iwp) ::  j  !<
-       INTEGER(iwp) ::  jp !<
-       INTEGER(iwp) ::  kp !<
-       INTEGER(iwp) ::  m  !<
-       INTEGER(iwp) ::  n  !<
-       INTEGER(iwp) ::  nn !<
-       INTEGER(iwp) ::  sort_index  !<
-
-       INTEGER(iwp), DIMENSION(0:7) ::  sort_count  !<
-
-       TYPE(particle_type), DIMENSION(:,:), ALLOCATABLE ::  sort_particles  !<
-
-       CALL cpu_log( log_point_s(51), 'lpm_sort_in_subboxes', 'start' )
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             DO  kp = nzb+1, nzt
-                number_of_particles = prt_count(kp,jp,ip)
-                IF ( number_of_particles <= 0 )  CYCLE
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                      
-                nn = 0
-                sort_count = 0
-                ALLOCATE( sort_particles(number_of_particles, 0:7) )
-                
-                DO  n = 1, number_of_particles
-                   sort_index = 0
-
-                   IF ( particles(n)%particle_mask )  THEN
-                      nn = nn + 1
-!
-!--                   Sorting particles with a binary scheme
-!--                   sort_index=111_2=7_10 -> particle at the left,south,bottom subgridbox
-!--                   sort_index=000_2=0_10 -> particle at the right,north,top subgridbox
-!--                   For this the center of the gridbox is calculated 
-                      i = (particles(n)%x + 0.5_wp * dx) * ddx
-                      j = (particles(n)%y + 0.5_wp * dy) * ddy
-                      
-                      IF ( i == ip )  sort_index = sort_index + 4 
-                      IF ( j == jp )  sort_index = sort_index + 2                      
-                      IF ( zu(kp) > particles(n)%z ) sort_index = sort_index + 1
-                      
-                      sort_count(sort_index) = sort_count(sort_index) + 1
-                      m = sort_count(sort_index)
-                      sort_particles(m,sort_index) = particles(n)
-                      sort_particles(m,sort_index)%block_nr = sort_index
-                   ENDIF
-                ENDDO
-
-                nn = 0
-                DO is = 0,7
-                   grid_particles(kp,jp,ip)%start_index(is) = nn + 1
-                   DO n = 1,sort_count(is)
-                      nn = nn + 1
-                      particles(nn) = sort_particles(n,is)
-                   ENDDO
-                   grid_particles(kp,jp,ip)%end_index(is) = nn
-                ENDDO
-
-                number_of_particles = nn    
-                prt_count(kp,jp,ip) = number_of_particles
-                DEALLOCATE(sort_particles)
-             ENDDO
-          ENDDO
-       ENDDO
-       CALL cpu_log( log_point_s(51), 'lpm_sort_in_subboxes', 'stop' )
-
-    END SUBROUTINE lpm_sort_in_subboxes
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Move all particles not marked for deletion to lowest indices (packing)
-!------------------------------------------------------------------------------!
-    SUBROUTINE lpm_pack
-
-       USE kinds
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) ::  n       !<
-       INTEGER(iwp) ::  nn      !<
-!
-!--    Find out elements marked for deletion and move data from highest index
-!--    values to these free indices
-       nn = number_of_particles
-
-       DO WHILE ( .NOT. particles(nn)%particle_mask )
-          nn = nn-1
-          IF ( nn == 0 )  EXIT
-       ENDDO
-
-       IF ( nn > 0 )  THEN
-          DO  n = 1, number_of_particles
-             IF ( .NOT. particles(n)%particle_mask )  THEN
-                particles(n) = particles(nn)
-                nn = nn - 1
-                DO WHILE ( .NOT. particles(nn)%particle_mask )
-                   nn = nn-1
-                   IF ( n == nn )  EXIT
-                ENDDO
-             ENDIF
-             IF ( n == nn )  EXIT
-          ENDDO
-       ENDIF
-
-!
-!--    The number of deleted particles has been determined in routines
-!--    lpm_boundary_conds, lpm_droplet_collision, and lpm_exchange_horiz
-       number_of_particles = nn
-
-    END SUBROUTINE lpm_pack 
-                
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Sort particles in each sub-grid box into two groups: particles that already
-!> completed the LES timestep, and particles that need further timestepping to
-!> complete the LES timestep. 
-!------------------------------------------------------------------------------!
-    SUBROUTINE lpm_sort_timeloop_done
-
-       USE control_parameters,                                                 &
-           ONLY:  dt_3d
-
-       USE indices,                                                            &
-           ONLY: nxl, nxr, nys, nyn, nzb, nzt
-
-       USE kinds
-
-       IMPLICIT NONE
-
-       INTEGER(iwp) :: end_index     !< particle end index for each sub-box
-       INTEGER(iwp) :: i             !< index of particle grid box in x-direction
-       INTEGER(iwp) :: j             !< index of particle grid box in y-direction
-       INTEGER(iwp) :: k             !< index of particle grid box in z-direction
-       INTEGER(iwp) :: n             !< running index for number of particles
-       INTEGER(iwp) :: nb            !< index of subgrid boux
-       INTEGER(iwp) :: nf            !< indices for particles in each sub-box that already finalized their substeps
-       INTEGER(iwp) :: nnf           !< indices for particles in each sub-box that need further treatment
-       INTEGER(iwp) :: num_finalized !< number of particles in each sub-box that already finalized their substeps
-       INTEGER(iwp) :: start_index   !< particle start index for each sub-box
-
-       TYPE(particle_type), DIMENSION(:), ALLOCATABLE :: sort_particles  !< temporary particle array 
-
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-
-                number_of_particles = prt_count(k,j,i)
-                IF ( number_of_particles <= 0 )  CYCLE
-
-                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-
-                DO  nb = 0, 7
-!
-!--                Obtain start and end index for each subgrid box
-                   start_index = grid_particles(k,j,i)%start_index(nb)
-                   end_index   = grid_particles(k,j,i)%end_index(nb)
-!
-!--                Allocate temporary array used for sorting. 
-                   ALLOCATE( sort_particles(start_index:end_index) )
-!
-!--                Determine number of particles already completed the LES 
-!--                timestep, and write them into a temporary array. 
-                   nf = start_index
-                   num_finalized = 0
-                   DO  n = start_index, end_index
-                      IF ( dt_3d - particles(n)%dt_sum < 1E-8_wp )  THEN
-                         sort_particles(nf) = particles(n)
-                         nf                 = nf + 1
-                         num_finalized      = num_finalized + 1
-                      ENDIF
-                   ENDDO
-!
-!--                Determine number of particles that not completed the LES 
-!--                timestep, and write them into a temporary array. 
-                   nnf = nf
-                   DO  n = start_index, end_index
-                      IF ( dt_3d - particles(n)%dt_sum > 1E-8_wp )  THEN
-                         sort_particles(nnf) = particles(n)
-                         nnf                 = nnf + 1
-                      ENDIF
-                   ENDDO
-!
-!--                Write back sorted particles
-                   particles(start_index:end_index) =                          &
-                                           sort_particles(start_index:end_index)
-!
-!--                Determine updated start_index, used to masked already 
-!--                completed particles. 
-                   grid_particles(k,j,i)%start_index(nb) =                     &
-                                      grid_particles(k,j,i)%start_index(nb)    &
-                                    + num_finalized
-!
-!--                Deallocate dummy array
-                   DEALLOCATE ( sort_particles )
-!
-!--                Finally, if number of non-completed particles is non zero 
-!--                in any of the sub-boxes, set control flag appropriately. 
-                   IF ( nnf > nf )                                             &
-                      grid_particles(k,j,i)%time_loop_done = .FALSE.
-
-                ENDDO
-             ENDDO
-          ENDDO
-       ENDDO
-
-    END SUBROUTINE lpm_sort_timeloop_done
-
- END MODULE lpm_pack_and_sort_mod
Index: palm/trunk/SOURCE/lpm_read_restart_file.f90
===================================================================
--- palm/trunk/SOURCE/lpm_read_restart_file.f90	(revision 4015)
+++ 	(revision )
@@ -1,205 +1,0 @@
-!> @file lpm_read_restart_file.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! time_prel replaced by last_particle_release_time
-! 
-! 3049 2018-05-29 13:52:36Z Giersch
-! Error messages revised
-! 
-! 3045 2018-05-28 07:55:41Z Giersch
-! Error messages revised, code adjusted according to PALMs coding standard
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2606 2017-11-10 10:36:31Z schwenkel
-! Changed particle box locations: center of particle box now coincides 
-! with scalar grid point of same index.
-! Renamed module and subroutines: lpm_pack_arrays_mod -> lpm_pack_and_sort_mod
-! lpm_pack_all_arrays -> lpm_sort_in_subboxes, lpm_pack_arrays -> lpm_pack
-! lpm_sort -> lpm_sort_timeloop_done
-!
-! 2312 2017-07-14 20:26:51Z hoffmann
-! Extended particle data type.
-!
-! 2305 2017-07-06 11:18:47Z hoffmann
-! Improved calculation of particle IDs.
-!
-! 2265 2017-06-08 16:58:28Z schwenkel
-! Unused variables removed.
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed. Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of init_particles)
-!
-!
-! Description:
-! ------------
-!> Read particle data from the restart file.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_read_restart_file
-
-
-    USE control_parameters,                                                    &
-        ONLY:  message_string
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxlg, nxr, nxrg, nyn, nyng, nys, nysg, nzb, nzt
-
-    USE kinds
-
-    USE lpm_pack_and_sort_mod,                                                 &
-        ONLY:  lpm_sort_in_subboxes
-
-    USE particle_attributes,                                                   &
-        ONLY:  alloc_factor, bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,         &
-               grid_particles, last_particle_release_time, min_nr_particle,    &
-               number_of_particles, number_of_particle_groups, particle_groups,&
-               particle_type, prt_count, time_write_particle_data, zero_particle
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=10) ::  particle_binary_version    !<
-    CHARACTER (LEN=10) ::  version_on_file            !<
-
-    INTEGER(iwp) :: alloc_size !<
-    INTEGER(iwp) :: ip         !<
-    INTEGER(iwp) :: jp         !<
-    INTEGER(iwp) :: kp         !<
-
-    TYPE(particle_type), DIMENSION(:), ALLOCATABLE :: tmp_particles !<
-
-!
-!-- Read particle data from previous model run.
-!-- First open the input unit.
-    IF ( myid_char == '' )  THEN
-       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN'//myid_char,                  &
-                  FORM='UNFORMATTED' )
-    ELSE
-       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN/'//myid_char,                 &
-                  FORM='UNFORMATTED' )
-    ENDIF
-
-!
-!-- First compare the version numbers
-    READ ( 90 )  version_on_file
-    particle_binary_version = '4.0'
-    IF ( TRIM( version_on_file ) /= TRIM( particle_binary_version ) )  THEN
-       message_string = 'version mismatch concerning data from prior ' //      &
-                        'run &version on file = "' //                          &
-                                      TRIM( version_on_file ) //               &
-                        '&version in program = "' //                           &
-                                      TRIM( particle_binary_version ) // '"'
-       CALL message( 'lpm_read_restart_file', 'PA0214', 1, 2, 0, 6, 0 )
-    ENDIF
-
-!
-!-- If less particles are stored on the restart file than prescribed by
-!-- min_nr_particle, the remainder is initialized by zero_particle to avoid
-!-- errors.
-    zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
-                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
-                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
-                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
-                                   0, 0, 0_idp, .FALSE., -1 )
-!
-!-- Read some particle parameters and the size of the particle arrays,
-!-- allocate them and read their contents.
-    READ ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                     &
-                 last_particle_release_time, number_of_particle_groups,        &
-                 particle_groups, time_write_particle_data
-
-    ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                        &
-              grid_particles(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-
-    READ ( 90 )  prt_count
-
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-
-             number_of_particles = prt_count(kp,jp,ip)
-             IF ( number_of_particles > 0 )  THEN
-                alloc_size = MAX( INT( number_of_particles *                   &
-                             ( 1.0_wp + alloc_factor / 100.0_wp ) ),           &
-                             min_nr_particle )
-             ELSE
-                alloc_size = min_nr_particle
-             ENDIF
-
-             ALLOCATE( grid_particles(kp,jp,ip)%particles(1:alloc_size) )
-
-             IF ( number_of_particles > 0 )  THEN
-                ALLOCATE( tmp_particles(1:number_of_particles) )
-                READ ( 90 )  tmp_particles
-                grid_particles(kp,jp,ip)%particles(1:number_of_particles) = tmp_particles
-                DEALLOCATE( tmp_particles )
-                IF ( number_of_particles < alloc_size )  THEN
-                   grid_particles(kp,jp,ip)%particles(number_of_particles+1:alloc_size) &
-                      = zero_particle
-                ENDIF
-             ELSE
-                grid_particles(kp,jp,ip)%particles(1:alloc_size) = zero_particle
-             ENDIF
-
-          ENDDO
-       ENDDO
-    ENDDO
-
-    CLOSE ( 90 )
-!
-!-- Must be called to sort particles into blocks, which is needed for a fast
-!-- interpolation of the LES fields on the particle position.
-    CALL lpm_sort_in_subboxes
-
-
- END SUBROUTINE lpm_read_restart_file
Index: palm/trunk/SOURCE/lpm_set_attributes.f90
===================================================================
--- palm/trunk/SOURCE/lpm_set_attributes.f90	(revision 4015)
+++ 	(revision )
@@ -1,413 +1,0 @@
-!> @file lpm_set_attributes.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! unused variables removed
-! 
-! 3065 2018-06-12 07:03:02Z Giersch
-! dz was replaced by dzw to allow for right vertical stretching
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2123 2017-01-18 12:34:59Z hoffmann
-!
-! 2122 2017-01-18 12:22:54Z hoffmann
-! DVRP routine removed
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-! Kind definition added to all floating point numbers.
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1318 2014-03-17 13:35:16Z raasch
-! module interfaces removed
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! routine renamed: set_particle_attributes -> lpm_set_attributes
-!
-! 828 2012-02-21 12:00:36Z raasch
-! particle feature color renamed class
-!
-! 271 2009-03-26 00:47:14Z raasch
-! Initial version
-!
-! Description:
-! ------------
-!> This routine sets certain particle attributes depending on the values that
-!> other PALM variables have at the current particle position.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_set_attributes
- 
-
-    USE arrays_3d,                                                             &
-        ONLY:  dzw, pt, u, v, zu
-
-    USE control_parameters,                                                    &
-        ONLY:  u_gtrans, v_gtrans
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE dvrp_variables,                                                        &
-        ONLY:  color_interval, dvrp_colortable_entries_prt, particle_color
-
-    USE grid_variables,                                                        &
-        ONLY:  dx, dy
-
-    USE indices,                                                               &
-        ONLY:  ngp_2dh, nxl, nxr, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  block_offset, grid_particles, number_of_particles, particles,   &
-               prt_count
-
-    USE pegrid
-
-    USE statistics,                                                            &
-        ONLY:  sums, sums_l
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i        !<
-    INTEGER(iwp) ::  ip       !<
-    INTEGER(iwp) ::  j        !<
-    INTEGER(iwp) ::  jp       !<
-    INTEGER(iwp) ::  k        !<
-    INTEGER(iwp) ::  kp       !<
-    INTEGER(iwp) ::  n        !<
-    INTEGER(iwp) ::  nb       !<
-
-    INTEGER(iwp), DIMENSION(0:7) ::  start_index !<
-    INTEGER(iwp), DIMENSION(0:7) ::  end_index   !<
-
-    REAL(wp)    ::  aa        !<
-    REAL(wp)    ::  absuv     !<
-    REAL(wp)    ::  bb        !<
-    REAL(wp)    ::  cc        !<
-    REAL(wp)    ::  dd        !<
-    REAL(wp)    ::  gg        !<
-    REAL(wp)    ::  height    !<
-    REAL(wp)    ::  pt_int    !<
-    REAL(wp)    ::  pt_int_l  !<
-    REAL(wp)    ::  pt_int_u  !<
-    REAL(wp)    ::  u_int_l   !<
-    REAL(wp)    ::  u_int_u   !<
-    REAL(wp)    ::  v_int_l   !<
-    REAL(wp)    ::  v_int_u   !<
-    REAL(wp)    ::  x         !<
-    REAL(wp)    ::  y         !<
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  u_int                  !<
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  v_int                  !<
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  xv                     !<
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  yv                     !<
-    REAL(wp), DIMENSION(:), ALLOCATABLE ::  zv                     !<
-
-    CALL cpu_log( log_point_s(49), 'lpm_set_attributes', 'start' )
-
-!
-!-- Set particle color
-    IF ( particle_color == 'absuv' )  THEN
-
-!
-!--    Set particle color depending on the absolute value of the horizontal
-!--    velocity
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             DO  kp = nzb+1, nzt
-
-                number_of_particles = prt_count(kp,jp,ip)
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                IF ( number_of_particles <= 0 )  CYCLE
-                start_index = grid_particles(kp,jp,ip)%start_index
-                end_index   = grid_particles(kp,jp,ip)%end_index
-
-                ALLOCATE( u_int(1:number_of_particles), &
-                          v_int(1:number_of_particles), &
-                          xv(1:number_of_particles),    &
-                          yv(1:number_of_particles),    &
-                          zv(1:number_of_particles) )
-
-                xv = particles(1:number_of_particles)%x
-                yv = particles(1:number_of_particles)%y
-                zv = particles(1:number_of_particles)%z
-
-                DO  nb = 0,7
-
-                   i = ip
-                   j = jp + block_offset(nb)%j_off
-                   k = kp + block_offset(nb)%k_off
-
-                   DO  n = start_index(nb), end_index(nb)
-!
-!--                   Interpolation of the velocity components in the xy-plane
-                      x  = xv(n) + ( 0.5_wp - i ) * dx
-                      y  = yv(n) - j * dy
-                      aa = x**2          + y**2
-                      bb = ( dx - x )**2 + y**2
-                      cc = x**2          + ( dy - y )**2
-                      dd = ( dx - x )**2 + ( dy - y )**2
-                      gg = aa + bb + cc + dd
-
-                      u_int_l = ( ( gg - aa ) * u(k,j,i)   + ( gg - bb ) *     &
-                                  u(k,j,i+1) + ( gg - cc ) * u(k,j+1,i) +      &
-                                  ( gg - dd ) * u(k,j+1,i+1)                   &
-                                ) / ( 3.0_wp * gg ) - u_gtrans
-
-                      IF ( k+1 == nzt+1 )  THEN
-                         u_int(n) = u_int_l
-                      ELSE
-                         u_int_u = ( ( gg - aa ) * u(k+1,j,i)   + ( gg - bb ) *  &
-                                     u(k+1,j,i+1) + ( gg - cc ) * u(k+1,j+1,i) + &
-                                     ( gg - dd ) * u(k+1,j+1,i+1)                &
-                                   ) / ( 3.0_wp * gg ) - u_gtrans
-                         u_int(n) = u_int_l + ( zv(n) - zu(k) ) / dzw(k) *      &
-                                           ( u_int_u - u_int_l )
-                      ENDIF
-
-                   ENDDO
-
-                   i = ip + block_offset(nb)%i_off
-                   j = jp
-                   k = kp + block_offset(nb)%k_off
-
-                   DO  n = start_index(nb), end_index(nb)
-!
-!--                   Same procedure for interpolation of the v velocity-component
-                      x  = xv(n) - i * dx
-                      y  = yv(n) + ( 0.5_wp - j ) * dy
-                      aa = x**2          + y**2
-                      bb = ( dx - x )**2 + y**2
-                      cc = x**2          + ( dy - y )**2
-                      dd = ( dx - x )**2 + ( dy - y )**2
-                      gg = aa + bb + cc + dd
-
-                      v_int_l = ( ( gg - aa ) * v(k,j,i)   + ( gg - bb ) *     &
-                                  v(k,j,i+1) + ( gg - cc ) * v(k,j+1,i) +      &
-                                  ( gg - dd ) * v(k,j+1,i+1)                   &
-                                ) / ( 3.0_wp * gg ) - v_gtrans
-
-                      IF ( k+1 == nzt+1 )  THEN
-                         v_int(n) = v_int_l
-                      ELSE
-                         v_int_u  = ( ( gg - aa ) * v(k+1,j,i) + ( gg - bb ) *    &
-                                      v(k+1,j,i+1) + ( gg - cc ) * v(k+1,j+1,i) + &
-                                      ( gg - dd ) * v(k+1,j+1,i+1)                &
-                                    ) / ( 3.0_wp * gg ) - v_gtrans
-                         v_int(n) = v_int_l + ( zv(n) - zu(k) ) / dzw(k) *      &
-                                           ( v_int_u - v_int_l )
-                      ENDIF
-
-                   ENDDO
-
-                ENDDO
-
-                DO  n = 1, number_of_particles
-
-                   absuv = SQRT( u_int(n)**2 + v_int(n)**2 )
-
-!
-!--                Limit values by the given interval and normalize to
-!--                interval [0,1]
-                   absuv = MIN( absuv, color_interval(2) )
-                   absuv = MAX( absuv, color_interval(1) )
-
-                   absuv = ( absuv - color_interval(1) ) / &
-                           ( color_interval(2) - color_interval(1) )
-
-!
-!--                Number of available colors is defined in init_dvrp
-                   particles(n)%class = 1 + absuv *                            &
-                                            ( dvrp_colortable_entries_prt - 1 )
-
-                ENDDO
-
-                DEALLOCATE( u_int, v_int, xv, yv, zv )
-
-             ENDDO
-          ENDDO
-       ENDDO
-
-    ELSEIF ( particle_color == 'pt*' )  THEN
-!
-!--    Set particle color depending on the resolved scale temperature
-!--    fluctuation.
-!--    First, calculate the horizontal average of the potential temperature
-!--    (This is also done in flow_statistics, but flow_statistics is called
-!--    after this routine.)
-       sums_l(:,4,0) = 0.0_wp
-       DO  i = nxl, nxr
-          DO  j =  nys, nyn
-             DO  k = nzb, nzt+1
-                sums_l(k,4,0) = sums_l(k,4,0) + pt(k,j,i)
-             ENDDO
-          ENDDO
-       ENDDO
-
-#if defined( __parallel )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( sums_l(nzb,4,0), sums(nzb,4), nzt+2-nzb, &
-                           MPI_REAL, MPI_SUM, comm2d, ierr )
-#else
-       sums(:,4) = sums_l(:,4,0)
-#endif
-       sums(:,4) = sums(:,4) / ngp_2dh(0)
-
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             DO  kp = nzb+1, nzt
-
-                number_of_particles = prt_count(kp,jp,ip)
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                IF ( number_of_particles <= 0 )  CYCLE
-                start_index = grid_particles(kp,jp,ip)%start_index
-                end_index   = grid_particles(kp,jp,ip)%end_index
-
-                ALLOCATE( xv(1:number_of_particles), &
-                          yv(1:number_of_particles), &
-                          zv(1:number_of_particles) )
-
-                xv = particles(1:number_of_particles)%x
-                yv = particles(1:number_of_particles)%y
-                zv = particles(1:number_of_particles)%z
-
-                DO  nb = 0,7
-
-                   i = ip + block_offset(nb)%i_off
-                   j = jp + block_offset(nb)%j_off
-                   k = kp + block_offset(nb)%k_off
-
-                   DO  n = start_index(nb), end_index(nb)
-!
-!--                   Interpolate temperature to the current particle position
-                      x  = xv(n) - i * dx
-                      y  = yv(n) - j * dy
-                      aa = x**2          + y**2
-                      bb = ( dx - x )**2 + y**2
-                      cc = x**2          + ( dy - y )**2
-                      dd = ( dx - x )**2 + ( dy - y )**2
-                      gg = aa + bb + cc + dd
-
-                      pt_int_l = ( ( gg - aa ) * pt(k,j,i)   + ( gg - bb ) *   &
-                                   pt(k,j,i+1) + ( gg - cc ) * pt(k,j+1,i) +   &
-                                   ( gg - dd ) * pt(k,j+1,i+1)                 &
-                                 ) / ( 3.0_wp * gg ) - sums(k,4)
-
-                      pt_int_u = ( ( gg - aa ) * pt(k+1,j,i) + ( gg - bb ) *     &
-                                   pt(k+1,j,i+1) + ( gg - cc ) * pt(k+1,j+1,i) + &
-                                   ( gg - dd ) * pt(k+1,j+1,i+1)                 &
-                                 ) / ( 3.0_wp * gg ) - sums(k,4)
-
-                      pt_int = pt_int_l + ( zv(n) - zu(k) ) / dzw(k) *          &
-                                          ( pt_int_u - pt_int_l )
-
-!
-!--                   Limit values by the given interval and normalize to
-!--                   interval [0,1]
-                      pt_int = MIN( pt_int, color_interval(2) )
-                      pt_int = MAX( pt_int, color_interval(1) )
-
-                      pt_int = ( pt_int - color_interval(1) ) /                &
-                               ( color_interval(2) - color_interval(1) )
-
-!
-!--                   Number of available colors is defined in init_dvrp
-                      particles(n)%class = 1 + pt_int *                        &
-                                           ( dvrp_colortable_entries_prt - 1 )
-
-                   ENDDO
-                ENDDO
-
-                DEALLOCATE( xv, yv, zv )
-
-             ENDDO
-          ENDDO
-       ENDDO
-
-    ELSEIF ( particle_color == 'z' )  THEN
-!
-!--    Set particle color depending on the height above the bottom
-!--    boundary (z=0)
-       DO  ip = nxl, nxr
-          DO  jp = nys, nyn
-             DO  kp = nzb+1, nzt
-
-                number_of_particles = prt_count(kp,jp,ip)
-                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-                IF ( number_of_particles <= 0 )  CYCLE
-                DO  n = 1, number_of_particles
-
-                   height = particles(n)%z
-!
-!--                Limit values by the given interval and normalize to
-!--                interval [0,1]
-                   height = MIN( height, color_interval(2) )
-                   height = MAX( height, color_interval(1) )
-
-                   height = ( height - color_interval(1) ) / &
-                            ( color_interval(2) - color_interval(1) )
-
-!
-!--                Number of available colors is defined in init_dvrp
-                   particles(n)%class = 1 + height *                           &
-                                            ( dvrp_colortable_entries_prt - 1 )
-
-                ENDDO
-
-             ENDDO
-          ENDDO
-       ENDDO
-
-    ENDIF
-
-    CALL cpu_log( log_point_s(49), 'lpm_set_attributes', 'stop' )
-
-
- END SUBROUTINE lpm_set_attributes
Index: palm/trunk/SOURCE/lpm_splitting.f90
===================================================================
--- palm/trunk/SOURCE/lpm_splitting.f90	(revision 4015)
+++ 	(revision )
@@ -1,608 +1,0 @@
-!> @file lpm_splitting.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Modularization of all bulk cloud physics code components
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 2932 2018-03-26 09:39:22Z maronga
-! renamed particles_par to particle_parameters
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 
-! Change in file header (GPL part)
-!
-! Added comments
-! 
-! 
-! 2263 2017-06-08 14:59:01Z schwenkel
-! Initial revision
-! 
-! 
-!
-! Description:
-! ------------
-! This routine is a part of the Lagrangian particle model. Super droplets which 
-! fulfill certain criterion's (e.g. a big weighting factor and a large radius) 
-! can be split into several super droplets with a reduced number of 
-! represented particles of every super droplet. This mechanism ensures an
-! improved representation of the right tail of the drop size distribution with 
-! a feasible amount of computational costs. The limits of particle creation 
-! should be chosen carefully! The idea of this algorithm is based on 
-! Unterstrasser and Soelch, 2014. 
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_splitting
-
-
-    USE arrays_3d,                                                             &
-        ONLY:  ql
-
-    USE basic_constants_and_equations_mod,                                     &
-        ONLY:  pi, rho_l
-
-    USE cpulog,                                                                &
-        ONLY:  cpu_log, log_point_s
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE lpm_exchange_horiz_mod,                                                &
-        ONLY:  realloc_particles_array
-
-    USE particle_attributes,                                                   &
-        ONLY:  grid_particles, initial_weighting_factor, isf, i_splitting_mode,&
-               max_number_particles_per_gridbox, new_particles, n_max,         &
-               number_of_particles, particles, particle_type, prt_count,       &
-               radius_split, splitting_factor, splitting_factor_max,           &
-               sum_new_particles, weight_factor_split                        
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  i                !< 
-    INTEGER(iwp) ::  j                !<
-    INTEGER(iwp) ::  jpp              !<
-    INTEGER(iwp) ::  k                !<
-    INTEGER(iwp) ::  n                !<
-    INTEGER(iwp) ::  new_particles_gb !< counter of created particles within one grid box
-    INTEGER(iwp) ::  new_size         !< new particle array size
-    INTEGER(iwp) ::  np               !< 
-    INTEGER(iwp) ::  old_size         !< old particle array size
-    
-    LOGICAL ::  first_loop_stride = .TRUE. !< flag to calculate constants only once
-
-    REAL(wp) ::  diameter                 !< diameter of droplet
-    REAL(wp) ::  dlog                     !< factor for DSD calculation
-    REAL(wp) ::  factor_volume_to_mass    !< pre calculate factor volume to mass
-    REAL(wp) ::  lambda                   !< slope parameter of gamma-distribution
-    REAL(wp) ::  lwc                      !< liquid water content of grid box
-    REAL(wp) ::  lwc_total                !< average liquid water content of cloud
-    REAL(wp) ::  m1                       !< first moment of DSD
-    REAL(wp) ::  m1_total                 !< average over all PEs of first moment of DSD
-    REAL(wp) ::  m2                       !< second moment of DSD
-    REAL(wp) ::  m2_total                 !< average average over all PEs second moment of DSD
-    REAL(wp) ::  m3                       !< third moment of DSD
-    REAL(wp) ::  m3_total                 !< average average over all PEs third moment of DSD
-    REAL(wp) ::  mu                       !< spectral shape parameter of gamma distribution
-    REAL(wp) ::  nrclgb                   !< number of cloudy grid boxes (ql >= 1.0E-5 kg/kg) 
-    REAL(wp) ::  nrclgb_total             !< average over all PEs of number of cloudy grid boxes
-    REAL(wp) ::  nr                       !< number concentration of cloud droplets
-    REAL(wp) ::  nr_total                 !< average over all PEs of number of cloudy grid boxes
-    REAL(wp) ::  nr0                      !< intercept parameter of gamma distribution
-    REAL(wp) ::  pirho_l                  !< pi * rho_l / 6.0
-    REAL(wp) ::  ql_crit = 1.0E-5_wp      !< threshold lwc for cloudy grid cells 
-                                          !< (Siebesma et al 2003, JAS, 60)
-    REAL(wp) ::  rm                       !< volume averaged mean radius
-    REAL(wp) ::  rm_total                 !< average over all PEs of volume averaged mean radius
-    REAL(wp) ::  r_min = 1.0E-6_wp        !< minimum radius of approximated spectra 
-    REAL(wp) ::  r_max = 1.0E-3_wp        !< maximum radius of approximated spectra
-    REAL(wp) ::  sigma_log = 1.5_wp       !< standard deviation of the LOG-distribution
-    REAL(wp) ::  zeta                     !< Parameter for DSD calculation of Seifert
-
-    REAL(wp), DIMENSION(0:n_max-1) ::  an_spl     !< size dependent critical weight factor
-    REAL(wp), DIMENSION(0:n_max-1) ::  r_bin_mid  !< mass weighted mean radius of a bin
-    REAL(wp), DIMENSION(0:n_max)   ::  r_bin      !< boundaries of a radius bin
-    
-    TYPE(particle_type) ::  tmp_particle   !< temporary particle TYPE
-
-    CALL cpu_log( log_point_s(80), 'lpm_splitting', 'start' )
-
-    IF ( first_loop_stride )  THEN
-       IF ( i_splitting_mode == 2  .OR.  i_splitting_mode == 3 )  THEN
-          dlog   = ( LOG10(r_max) - LOG10(r_min) ) / ( n_max - 1 )
-          DO  i = 0, n_max-1
-             r_bin(i) = 10.0_wp**( LOG10(r_min) + i * dlog - 0.5_wp * dlog )
-             r_bin_mid(i) = 10.0_wp**( LOG10(r_min) + i * dlog )
-          ENDDO
-          r_bin(n_max) = 10.0_wp**( LOG10(r_min) + n_max * dlog - 0.5_wp * dlog )
-       ENDIF   
-       factor_volume_to_mass =  4.0_wp / 3.0_wp * pi * rho_l
-       pirho_l  = pi * rho_l / 6.0_wp
-       IF ( weight_factor_split == -1.0_wp )  THEN
-          weight_factor_split = 0.1_wp * initial_weighting_factor 
-       ENDIF
-    ENDIF
-
-    new_particles  = 0
-
-    IF ( i_splitting_mode == 1 )  THEN
-
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-
-                new_particles_gb = 0
-                number_of_particles = prt_count(k,j,i)
-                IF ( number_of_particles <= 0  .OR.                            &  
-                     ql(k,j,i) < ql_crit )  CYCLE
-                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-!                
-!--             Start splitting operations. Each particle is checked if it
-!--             fulfilled the splitting criterion's. In splitting mode 'const'   
-!--             a critical radius  (radius_split) a critical weighting factor
-!--             (weight_factor_split) and a splitting factor (splitting_factor)
-!--             must  be prescribed (see particle_parameters). Super droplets 
-!--             which have a larger radius and larger weighting factor are split 
-!--             into 'splitting_factor' super droplets. Therefore, the weighting 
-!--             factor of  the super droplet and all created clones is reduced 
-!--             by the factor of 'splitting_factor'.
-                DO  n = 1, number_of_particles
-                   IF ( particles(n)%particle_mask  .AND.                      &
-                        particles(n)%radius >= radius_split  .AND.             & 
-                        particles(n)%weight_factor >= weight_factor_split )    &
-                   THEN          
-!
-!--                   Calculate the new number of particles.
-                      new_size = prt_count(k,j,i) + splitting_factor - 1                                    
-!
-!--                   Cycle if maximum number of particles per grid box
-!--                   is greater than the allowed maximum number.
-                      IF ( new_size >= max_number_particles_per_gridbox )  CYCLE                      
-!
-!--                   Reallocate particle array if necessary. 
-                      IF ( new_size > SIZE(particles) )  THEN 
-                         CALL realloc_particles_array(i,j,k,new_size)
-                      ENDIF
-                      old_size = prt_count(k,j,i)
-!
-!--                   Calculate new weighting factor.
-                      particles(n)%weight_factor =  & 
-                         particles(n)%weight_factor / splitting_factor
-                      tmp_particle = particles(n)
-!
-!--                   Create splitting_factor-1 new particles.
-                      DO  jpp = 1, splitting_factor-1
-                         grid_particles(k,j,i)%particles(jpp+old_size) =       & 
-                            tmp_particle         
-                      ENDDO  
-                      new_particles_gb = new_particles_gb + splitting_factor - 1
-!   
-!--                   Save the new number of super droplets for every grid box.
-                      prt_count(k,j,i) = prt_count(k,j,i) +                    &
-                                         splitting_factor - 1         
-                   ENDIF
-                ENDDO
-                
-                new_particles       = new_particles     + new_particles_gb
-                sum_new_particles   = sum_new_particles + new_particles_gb 
-             ENDDO
-          ENDDO
-       ENDDO
-
-    ELSEIF ( i_splitting_mode == 2 )  THEN 
-!
-!--    Initialize summing variables.
-       lwc          = 0.0_wp
-       lwc_total    = 0.0_wp 
-       m1           = 0.0_wp
-       m1_total     = 0.0_wp
-       m2           = 0.0_wp
-       m2_total     = 0.0_wp
-       m3           = 0.0_wp
-       m3_total     = 0.0_wp
-       nr           = 0.0_wp   
-       nrclgb       = 0.0_wp
-       nrclgb_total = 0.0_wp 
-       nr_total     = 0.0_wp
-       rm           = 0.0_wp
-       rm_total     = 0.0_wp
-       
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-                number_of_particles = prt_count(k,j,i)
-                IF ( number_of_particles <= 0  .OR.                            & 
-                     ql(k,j,i) < ql_crit )  CYCLE
-                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-                nrclgb = nrclgb + 1.0_wp
-!                
-!--             Calculate moments of DSD.                
-                DO  n = 1, number_of_particles
-                   IF ( particles(n)%particle_mask  .AND.                      &
-                        particles(n)%radius >= r_min )                         &
-                   THEN
-                      nr  = nr  + particles(n)%weight_factor
-                      rm  = rm  + factor_volume_to_mass  *                     &
-                                 particles(n)%radius**3  *                     &
-                                 particles(n)%weight_factor
-                      IF ( isf == 1 )  THEN           
-                         diameter   = particles(n)%radius * 2.0_wp            
-                         lwc = lwc + factor_volume_to_mass *                   &
-                                     particles(n)%radius**3 *                  & 
-                                     particles(n)%weight_factor 
-                         m1  = m1  + particles(n)%weight_factor * diameter                                                
-                         m2  = m2  + particles(n)%weight_factor * diameter**2           
-                         m3  = m3  + particles(n)%weight_factor * diameter**3
-                      ENDIF   
-                   ENDIF                        
-                ENDDO 
-             ENDDO
-          ENDDO
-       ENDDO
-
-#if defined( __parallel )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( nr, nr_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-       CALL MPI_ALLREDUCE( rm, rm_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( nrclgb, nrclgb_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( lwc, lwc_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( m1, m1_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( m2, m2_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
-       CALL MPI_ALLREDUCE( m3, m3_total, 1 , &
-       MPI_REAL, MPI_SUM, comm2d, ierr )
-#endif 
-
-!
-!--    Calculate number concentration and mean volume averaged radius.
-       nr_total = MERGE( nr_total / nrclgb_total,                              &
-                         0.0_wp, nrclgb_total > 0.0_wp                         &
-                       )
-       rm_total = MERGE( ( rm_total /                                          &
-                            ( nr_total * factor_volume_to_mass )               &
-                          )**0.3333333_wp, 0.0_wp, nrclgb_total > 0.0_wp       &
-                       )                         
-!
-!--    Check which function should be used to approximate the DSD.
-       IF ( isf == 1 )  THEN
-          lwc_total = MERGE( lwc_total / nrclgb_total,                         &
-                             0.0_wp, nrclgb_total > 0.0_wp                     &
-                           )
-          m1_total  = MERGE( m1_total / nrclgb_total,                          &
-                             0.0_wp, nrclgb_total > 0.0_wp                     &
-                           )
-          m2_total  = MERGE( m2_total / nrclgb_total,                          &
-                             0.0_wp, nrclgb_total > 0.0_wp                     &
-                           )
-          m3_total  = MERGE( m3_total / nrclgb_total,                          &
-                             0.0_wp, nrclgb_total > 0.0_wp                     &
-                           )
-          zeta = m1_total * m3_total / m2_total**2                             
-          mu   = MAX( ( ( 1.0_wp - zeta ) * 2.0_wp + 1.0_wp ) /                &
-                        ( zeta - 1.0_wp ), 0.0_wp                              &
-                    )
-
-          lambda = ( pirho_l * nr_total / lwc_total *                          &
-                     ( mu + 3.0_wp ) * ( mu + 2.0_wp ) * ( mu + 1.0_wp )       &                                          
-                   )**0.3333333_wp
-          nr0 = nr_total / gamma( mu + 1.0_wp ) * lambda**( mu + 1.0_wp ) 
-          
-          DO  n = 0, n_max-1
-             diameter  = r_bin_mid(n) * 2.0_wp           
-             an_spl(n) = nr0 * diameter**mu * EXP( -lambda * diameter ) *      & 
-                         ( r_bin(n+1) - r_bin(n) ) * 2.0_wp 
-          ENDDO
-       ELSEIF ( isf == 2 )  THEN
-          DO  n = 0, n_max-1
-             an_spl(n) = nr_total / ( SQRT( 2.0_wp * pi ) *                    &
-                                     LOG(sigma_log) * r_bin_mid(n)             &
-                                     ) *                                       &
-                         EXP( -( LOG( r_bin_mid(n) / rm_total )**2 ) /         &
-                               ( 2.0_wp * LOG(sigma_log)**2 )                  & 
-                             ) *                                               & 
-                         ( r_bin(n+1) - r_bin(n) )
-          ENDDO
-       ELSEIF( isf == 3 )  THEN
-          DO  n = 0, n_max-1 
-             an_spl(n) = 3.0_wp * nr_total * r_bin_mid(n)**2 / rm_total**3  *  &
-                         EXP( - ( r_bin_mid(n)**3 / rm_total**3 ) )         *  &
-                         ( r_bin(n+1) - r_bin(n) )
-          ENDDO
-       ENDIF
-!
-!--    Criterion to avoid super droplets with a weighting factor < 1.0.
-       an_spl = MAX(an_spl, 1.0_wp)
-  
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-                number_of_particles = prt_count(k,j,i)
-                IF ( number_of_particles <= 0  .OR.                            &  
-                     ql(k,j,i) < ql_crit )  CYCLE
-                particles => grid_particles(k,j,i)%particles(1:number_of_particles)
-                new_particles_gb = 0         
-!                
-!--             Start splitting operations. Each particle is checked if it
-!--             fulfilled the splitting criterion's. In splitting mode 'cl_av'   
-!--             a critical radius (radius_split) and a splitting function must 
-!--             be prescribed (see particles_par). The critical weighting factor
-!--             is calculated while approximating a 'gamma', 'log' or 'exp'-
-!--             drop size distribution. In this mode the DSD is calculated as 
-!--             an average over all cloudy grid boxes. Super droplets which 
-!--             have a larger radius and larger weighting factor are split into
-!--             'splitting_factor' super droplets. In this case the splitting  
-!--             factor is calculated of weighting factor of the super droplet  
-!--             and the approximated number concentration for droplet of such 
-!--             a size. Due to the splitting, the weighting factor of the  
-!--             super droplet and all created clones is reduced by the factor  
-!--             of 'splitting_facor'.
-                DO  n = 1, number_of_particles
-                   DO  np = 0, n_max-1
-                      IF ( r_bin(np) >= radius_split  .AND.                    &
-                           particles(n)%particle_mask  .AND.                   &
-                           particles(n)%radius >= r_bin(np)  .AND.             &
-                           particles(n)%radius < r_bin(np+1)  .AND.            &
-                           particles(n)%weight_factor >= an_spl(np)  )         &
-                      THEN
-!
-!--                      Calculate splitting factor
-                         splitting_factor =                                    & 
-                             MIN( INT( particles(n)%weight_factor /            &
-                                        an_spl(np)                             &
-                                     ), splitting_factor_max                   &
-                                )
-                         IF ( splitting_factor < 2 )  CYCLE
-!
-!--                      Calculate the new number of particles.                                                           
-                         new_size = prt_count(k,j,i) + splitting_factor - 1
-!
-!--                      Cycle if maximum number of particles per grid box
-!--                      is greater than the allowed maximum number.                         
-                         IF ( new_size >= max_number_particles_per_gridbox )   & 
-                         CYCLE
-!
-!--                      Reallocate particle array if necessary. 
-                         IF ( new_size > SIZE(particles) )  THEN 
-                            CALL realloc_particles_array(i,j,k,new_size)
-                         ENDIF
-                         old_size  = prt_count(k,j,i)                             
-                         new_particles_gb = new_particles_gb +                 &
-                                            splitting_factor - 1
-!
-!--                      Calculate new weighting factor.
-                         particles(n)%weight_factor =                          & 
-                            particles(n)%weight_factor / splitting_factor
-                         tmp_particle = particles(n)
-!
-!--                      Create splitting_factor-1 new particles.                                                  
-                         DO  jpp = 1, splitting_factor-1
-                            grid_particles(k,j,i)%particles(jpp+old_size) =    &
-                                                                    tmp_particle         
-                         ENDDO
-!   
-!--                      Save the new number of super droplets. 
-                         prt_count(k,j,i) = prt_count(k,j,i) +                 &
-                                            splitting_factor - 1
-                      ENDIF
-                   ENDDO
-                ENDDO 
-                
-                new_particles       = new_particles     + new_particles_gb
-                sum_new_particles   = sum_new_particles + new_particles_gb                    
-             ENDDO
-          ENDDO
-       ENDDO
-
-    ELSEIF ( i_splitting_mode == 3 )  THEN 
-
-       DO  i = nxl, nxr
-          DO  j = nys, nyn
-             DO  k = nzb+1, nzt
-             
-!
-!--             Initialize summing variables.             
-                lwc = 0.0_wp
-                m1  = 0.0_wp
-                m2  = 0.0_wp
-                m3  = 0.0_wp
-                nr  = 0.0_wp
-                rm  = 0.0_wp  
-                
-                new_particles_gb = 0
-                number_of_particles = prt_count(k,j,i)
-                IF ( number_of_particles <= 0  .OR.                            & 
-                     ql(k,j,i) < ql_crit )  CYCLE
-                particles => grid_particles(k,j,i)%particles
-!                
-!--             Calculate moments of DSD.                
-                DO  n = 1, number_of_particles
-                   IF ( particles(n)%particle_mask  .AND.                      &
-                        particles(n)%radius >= r_min )                         &
-                   THEN
-                      nr  = nr + particles(n)%weight_factor
-                      rm  = rm + factor_volume_to_mass  *                      &
-                                 particles(n)%radius**3  *                     &
-                                 particles(n)%weight_factor
-                      IF ( isf == 1 )  THEN            
-                         diameter   = particles(n)%radius * 2.0_wp           
-                         lwc = lwc + factor_volume_to_mass *                   &
-                                     particles(n)%radius**3 *                  &  
-                                     particles(n)%weight_factor 
-                         m1  = m1 + particles(n)%weight_factor * diameter
-                         m2  = m2 + particles(n)%weight_factor * diameter**2
-                         m3  = m3 + particles(n)%weight_factor * diameter**3
-                      ENDIF      
-                   ENDIF                                            
-                ENDDO  
-
-                IF ( nr <= 0.0  .OR.  rm <= 0.0_wp )  CYCLE 
-!
-!--             Calculate mean volume averaged radius.                
-                rm = ( rm / ( nr * factor_volume_to_mass ) )**0.3333333_wp
-!
-!--             Check which function should be used to approximate the DSD.              
-                IF ( isf == 1 )  THEN
-!
-!--                Gamma size distribution to calculate  
-!--                critical weight_factor (e.g. Marshall + Palmer, 1948).
-                   zeta = m1 * m3 / m2**2
-                   mu   = MAX( ( ( 1.0_wp - zeta ) * 2.0_wp + 1.0_wp ) /       &
-                                ( zeta - 1.0_wp ), 0.0_wp                      &
-                             )   
-                   lambda = ( pirho_l * nr / lwc *                             &
-                              ( mu + 3.0_wp ) * ( mu + 2.0_wp ) *              &
-                              ( mu + 1.0_wp )                                  &                                  
-                            )**0.3333333_wp
-                   nr0 =  ( nr / (gamma( mu + 1.0_wp ) ) ) *                   &
-                          lambda**( mu + 1.0_wp ) 
-
-                   DO  n = 0, n_max-1
-                      diameter         = r_bin_mid(n) * 2.0_wp           
-                      an_spl(n) = nr0 * diameter**mu *                         &
-                                  EXP( -lambda * diameter ) *                  & 
-                                  ( r_bin(n+1) - r_bin(n) ) * 2.0_wp 
-                   ENDDO
-                ELSEIF ( isf == 2 )  THEN
-!
-!--                Lognormal size distribution to calculate critical 
-!--                weight_factor (e.g. Levin, 1971, Bradley + Stow, 1974).   
-                   DO  n = 0, n_max-1
-                      an_spl(n) = nr / ( SQRT( 2.0_wp * pi ) *                 &
-                                              LOG(sigma_log) * r_bin_mid(n)    &
-                                        ) *                                    &
-                                  EXP( -( LOG( r_bin_mid(n) / rm )**2 ) /      &
-                                        ( 2.0_wp * LOG(sigma_log)**2 )         & 
-                                      ) *                                      & 
-                                  ( r_bin(n+1) - r_bin(n) )
-                   ENDDO
-                ELSEIF ( isf == 3 )  THEN
-!
-!--                Exponential size distribution to calculate critical 
-!--                weight_factor (e.g. Berry + Reinhardt, 1974).  
-                   DO  n = 0, n_max-1
-                      an_spl(n) = 3.0_wp * nr * r_bin_mid(n)**2 / rm**3 *     &
-                                  EXP( - ( r_bin_mid(n)**3 / rm**3 ) ) *      &
-                                  ( r_bin(n+1) - r_bin(n) )
-                   ENDDO
-                ENDIF
-                
-!
-!--             Criterion to avoid super droplets with a weighting factor < 1.0.                                   
-                an_spl = MAX(an_spl, 1.0_wp)
-!                
-!--             Start splitting operations. Each particle is checked if it
-!--             fulfilled the splitting criterion's. In splitting mode 'gb_av'   
-!--             a critical radius (radius_split) and a splitting function must 
-!--             be prescribed (see particles_par). The critical weighting factor
-!--             is calculated while appoximating a 'gamma', 'log' or 'exp'-
-!--             drop size distribution. In this mode a DSD is calculated for 
-!--             every cloudy grid box. Super droplets which have a larger 
-!--             radius and larger weighting factor are split into
-!--             'splitting_factor' super droplets. In this case the splitting  
-!--             factor is calculated of weighting factor of the super droplet  
-!--             and theapproximated number concentration for droplet of such 
-!--             a size. Due to the splitting, the weighting factor of the  
-!--             super droplet and all created clones is reduced by the factor  
-!--             of 'splitting_facor'.
-                DO  n = 1, number_of_particles
-                   DO  np = 0, n_max-1
-                      IF ( r_bin(np) >= radius_split  .AND.                    &
-                           particles(n)%particle_mask  .AND.                   &
-                           particles(n)%radius >= r_bin(np)    .AND.           &
-                           particles(n)%radius < r_bin(np+1)   .AND.           &
-                           particles(n)%weight_factor >= an_spl(np) )          &
-                      THEN
-!
-!--                      Calculate splitting factor.
-                         splitting_factor =                                    & 
-                             MIN( INT( particles(n)%weight_factor /            &
-                                        an_spl(np)                             &
-                                     ), splitting_factor_max                   &
-                                )
-                         IF ( splitting_factor < 2 )  CYCLE
-
-!
-!--                      Calculate the new number of particles.                                                                                         
-                         new_size = prt_count(k,j,i) + splitting_factor - 1
-!
-!--                      Cycle if maximum number of particles per grid box
-!--                      is greater than the allowed maximum number.                                                  
-                         IF ( new_size >= max_number_particles_per_gridbox )   &
-                         CYCLE
-!
-!--                      Reallocate particle array if necessary.                          
-                         IF ( new_size > SIZE(particles) )  THEN 
-                            CALL realloc_particles_array(i,j,k,new_size)
-                         ENDIF
-!
-!--                      Calculate new weighting factor.
-                         particles(n)%weight_factor = & 
-                            particles(n)%weight_factor / splitting_factor
-                         tmp_particle               = particles(n)
-                         old_size                   = prt_count(k,j,i)
-!
-!--                      Create splitting_factor-1 new particles.
-                         DO jpp = 1, splitting_factor-1
-                            grid_particles(k,j,i)%particles(jpp+old_size) =    &
-                               tmp_particle                  
-                         ENDDO
-!
-!--                      Save the new number of droplets for every grid box.
-                         prt_count(k,j,i)    = prt_count(k,j,i) +              &
-                                               splitting_factor - 1
-                         new_particles_gb    = new_particles_gb +              &
-                                               splitting_factor - 1                                       
-                      ENDIF
-                   ENDDO  
-                ENDDO
-
-                new_particles       = new_particles + new_particles_gb
-                sum_new_particles   = sum_new_particles + new_particles_gb                                                 
-             ENDDO
-          ENDDO
-       ENDDO
-    ENDIF
-        
-    CALL cpu_log( log_point_s(80), 'lpm_splitting', 'stop' )
-
- END SUBROUTINE lpm_splitting
- 
Index: palm/trunk/SOURCE/lpm_write_exchange_statistics.f90
===================================================================
--- palm/trunk/SOURCE/lpm_write_exchange_statistics.f90	(revision 4015)
+++ 	(revision )
@@ -1,173 +1,0 @@
-!> @file lpm_write_exchange_statistics.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! unused variables removed
-! 
-! 2967 2018-04-13 11:22:08Z raasch
-! nesting routine is only called if nesting is switched on
-! bugfix: missing parallel cpp-directives added
-! 
-! 2841 2018-02-27 15:02:57Z knoop
-! Bugfix: wrong placement of include 'mpif.h' corrected,
-! kinds module added and pegrid module scope restricted
-! 
-! 2801 2018-02-14 16:01:55Z thiele
-! Introduce particle transfer in nested models.
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2265 2017-06-08 16:58:28Z schwenkel
-! Unused variables removed.
-! 
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Write particle statistics (total particle numbers and number of particles
-!> exchanged between subdomains) on ASCII file.
-!>
-!> @attention Output format of this file could be further improved! At current
-!>            stage it is only a test output.
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_write_exchange_statistics
-
-#if defined( __parallel )  &&  !defined( __mpifh )
-    USE MPI
-#endif
-
-    USE control_parameters,                                                    &
-        ONLY:  current_timestep_number, dt_3d, simulated_time
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nys, nyn, nzb, nzt
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  number_of_particles, prt_count, 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
-
-    USE pmc_particle_interface,                                                &
-        ONLY:  pmcp_g_print_number_of_particles
-
-    USE pegrid,                                                                &
-        ONLY:  comm2d, ierr, pleft, pright, psouth, pnorth
-
-    USE pmc_interface,                                                         &
-        ONLY: nested_run
-
-    IMPLICIT NONE
-
-#if defined( __parallel )  &&  defined( __mpifh )
-    INCLUDE "mpif.h"
-#endif
-
-    INTEGER(iwp) :: ip         !<
-    INTEGER(iwp) :: jp         !<
-    INTEGER(iwp) :: kp         !<
-    INTEGER(iwp) :: tot_number_of_particles
-
-
-
-!
-!-- Determine the current number of particles
-    number_of_particles         = 0
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles = number_of_particles                         &
-                                     + prt_count(kp,jp,ip)
-          ENDDO
-       ENDDO
-    ENDDO
-
-    CALL check_open( 80 )
-#if defined( __parallel )
-    WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
-                        number_of_particles, pleft, trlp_count_sum,      &
-                        trlp_count_recv_sum, pright, trrp_count_sum,     &
-                        trrp_count_recv_sum, psouth, trsp_count_sum,     &
-                        trsp_count_recv_sum, pnorth, trnp_count_sum,     &
-                        trnp_count_recv_sum
-#else
-    WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
-                        number_of_particles
-#endif
-    CALL close_file( 80 )
-
-    IF ( number_of_particles > 0 ) THEN
-        WRITE(9,*) 'number_of_particles ', number_of_particles,                &
-                    current_timestep_number + 1, simulated_time + dt_3d
-    ENDIF
-
-#if defined( __parallel )
-    CALL MPI_ALLREDUCE( number_of_particles, tot_number_of_particles, 1,       &
-                        MPI_INTEGER, MPI_SUM, comm2d, ierr )
-#else
-    tot_number_of_particles = number_of_particles
-#endif
-
-    IF ( nested_run )  THEN
-       CALL pmcp_g_print_number_of_particles( simulated_time+dt_3d,            &
-                                              tot_number_of_particles)
-    ENDIF
-
-!
-!-- Formats
-8000 FORMAT (I6,1X,F7.2,4X,I10,5X,4(I3,1X,I4,'/',I4,2X),6X,I10)
-
-
- END SUBROUTINE lpm_write_exchange_statistics
Index: palm/trunk/SOURCE/lpm_write_restart_file.f90
===================================================================
--- palm/trunk/SOURCE/lpm_write_restart_file.f90	(revision 4015)
+++ 	(revision )
@@ -1,148 +1,0 @@
-!> @file lpm_write_restart_file.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! ------------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! time_prel replaced by last_particle_release_time
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2265 2017-06-08 16:58:28Z schwenkel
-! Unused variables removed.
-! 
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1822 2016-04-07 07:49:42Z hoffmann
-! Tails removed. Unused variables removed.
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! initial revision (former part of advec_particles)
-!
-!
-! Description:
-! ------------
-!> Write particle data in FORTRAN binary format on restart file
-!------------------------------------------------------------------------------!
- SUBROUTINE lpm_write_restart_file
-
-
-    USE indices,                                                               &
-        ONLY:  nxl, nxr, nyn, nys, nzb, nzt
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t, grid_particles,       &
-               last_particle_release_time, number_of_particles,                &
-               number_of_particle_groups, particles, particle_groups,          &
-               prt_count, time_write_particle_data
-
-    USE pegrid
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=10) ::  particle_binary_version   !<
-
-    INTEGER(iwp) ::  ip                              !<
-    INTEGER(iwp) ::  jp                              !<
-    INTEGER(iwp) ::  kp                              !<
-
-!
-!-- First open the output unit.
-    IF ( myid_char == '' )  THEN
-       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT'//myid_char, &
-                  FORM='UNFORMATTED')
-    ELSE
-       IF ( myid == 0 )  CALL local_system( 'mkdir PARTICLE_RESTART_DATA_OUT' )
-#if defined( __parallel )
-!
-!--    Set a barrier in order to allow that thereafter all other processors
-!--    in the directory created by PE0 can open their file
-       CALL MPI_BARRIER( comm2d, ierr )
-#endif
-       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT/'//myid_char, &
-                  FORM='UNFORMATTED' )
-    ENDIF
-
-!
-!-- Write the version number of the binary format.
-!-- Attention: After changes to the following output commands the version
-!-- ---------  number of the variable particle_binary_version must be
-!--            changed! Also, the version number and the list of arrays
-!--            to be read in lpm_read_restart_file must be adjusted
-!--            accordingly.
-    particle_binary_version = '4.0'
-    WRITE ( 90 )  particle_binary_version
-
-!
-!-- Write some particle parameters, the size of the particle arrays as
-!-- well as other dvrp-plot variables.
-    WRITE ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                    &
-                  last_particle_release_time, number_of_particle_groups,       &
-                  particle_groups, time_write_particle_data
-
-    WRITE ( 90 )  prt_count
-          
-    DO  ip = nxl, nxr
-       DO  jp = nys, nyn
-          DO  kp = nzb+1, nzt
-             number_of_particles = prt_count(kp,jp,ip)
-             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-             IF ( number_of_particles <= 0 )  CYCLE
-             WRITE ( 90 )  particles
-          ENDDO
-       ENDDO
-    ENDDO
-
-    CLOSE ( 90 )
-
-#if defined( __parallel )
-       CALL MPI_BARRIER( comm2d, ierr )
-#endif
-
-
- END SUBROUTINE lpm_write_restart_file
Index: palm/trunk/SOURCE/mod_particle_attributes.f90
===================================================================
--- palm/trunk/SOURCE/mod_particle_attributes.f90	(revision 4015)
+++ palm/trunk/SOURCE/mod_particle_attributes.f90	(revision 4017)
@@ -115,15 +115,4 @@
     USE kinds
 
-    CHARACTER(LEN=15) ::  aero_species = 'nacl'                    !< aerosol species
-    CHARACTER(LEN=15) ::  aero_type    = 'maritime'                !< aerosol type
-    CHARACTER(LEN=15) ::  bc_par_lr    = 'cyclic'                  !< left/right boundary condition
-    CHARACTER(LEN=15) ::  bc_par_ns    = 'cyclic'                  !< north/south boundary condition
-    CHARACTER(LEN=15) ::  bc_par_b     = 'reflect'                 !< bottom boundary condition
-    CHARACTER(LEN=15) ::  bc_par_t     = 'absorb'                  !< top boundary condition
-    CHARACTER(LEN=15) ::  collision_kernel   = 'none'              !< collision kernel
-    CHARACTER(LEN=5)  ::  splitting_function = 'gamma'             !< function for calculation critical weighting factor
-    CHARACTER(LEN=5)  ::  splitting_mode     = 'const'             !< splitting mode
-
-    INTEGER(iwp) ::  deleted_particles = 0                        !< number of deleted particles per time step
     INTEGER(iwp) ::  dissipation_classes = 10                     !< namelist parameter (see documentation)
     INTEGER(iwp) ::  ibc_par_b                                    !< particle bottom boundary condition dummy
@@ -131,92 +120,18 @@
     INTEGER(iwp) ::  ibc_par_ns                                   !< particle north/south boundary condition dummy
     INTEGER(iwp) ::  ibc_par_t                                    !< particle top boundary condition dummy
-    INTEGER(iwp) ::  iran_part = -1234567                         !< number for random generator
-    INTEGER(iwp) ::  isf                                          !< dummy for splitting function
-    INTEGER(iwp) ::  i_splitting_mode                             !< dummy for splitting mode
-    INTEGER(iwp) ::  max_number_particles_per_gridbox = 100       !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  merge_drp = 0                                !< number of merged droplets
     INTEGER(iwp) ::  min_nr_particle = 50                         !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  new_particles = 0                            !< number of new particles
-    INTEGER(iwp) ::  n_max = 100                                  !< number of radii bin for splitting functions
     INTEGER(iwp) ::  number_of_particles = 0                      !< number of particles for each grid box (3d array is saved on prt_count)
     INTEGER(iwp) ::  number_of_particle_groups = 1                !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  number_of_sublayers = 20                     !< number of sublayers for particle velocities betwenn surface and first grid level
-    INTEGER(iwp) ::  number_particles_per_gridbox = -1            !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  offset_ocean_nzt = 0                         !< in case of oceans runs, the vertical index calculations need an offset
-    INTEGER(iwp) ::  offset_ocean_nzt_m1 = 0                      !< in case of oceans runs, the vertical index calculations need an offset
-    INTEGER(iwp) ::  particles_per_point = 1                      !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  radius_classes = 20                          !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  sort_count = 0                               !< counter for sorting particles
-    INTEGER(iwp) ::  splitting_factor = 2                         !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  splitting_factor_max = 5                     !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  step_dealloc = 100                           !< namelist parameter (see documentation)
-    INTEGER(iwp) ::  sum_merge_drp = 0                            !< sum of merged super droplets
-    INTEGER(iwp) ::  sum_new_particles = 0                        !< sum of created particles (in splitting algorithm)
-    INTEGER(iwp) ::  total_number_of_particles                    !< total number of particles in the whole model domain
-    INTEGER(iwp) ::  trlp_count_sum                               !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trlp_count_recv_sum                          !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trrp_count_sum                               !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trrp_count_recv_sum                          !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trsp_count_sum                               !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trsp_count_recv_sum                          !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trnp_count_sum                               !< parameter for particle exchange of PEs
-    INTEGER(iwp) ::  trnp_count_recv_sum                          !< parameter for particle exchange of PEs
 
     INTEGER(iwp), PARAMETER ::  max_number_of_particle_groups = 10 !< maximum allowed number of particle groups
 
     INTEGER(iwp), DIMENSION(:,:,:), ALLOCATABLE ::  prt_count  !< 3d array of number of particles of every grid box
-
-    LOGICAL ::  curvature_solution_effects = .FALSE.      !< namelist parameter (see documentation)
-    LOGICAL ::  deallocate_memory = .TRUE.                !< namelist parameter (see documentation)
-    LOGICAL ::  hall_kernel = .FALSE.                     !< flag for collision kernel
-    LOGICAL ::  merging = .FALSE.                         !< namelist parameter (see documentation)
+    
     LOGICAL ::  particle_advection = .FALSE.              !< parameter to steer the advection of particles
-    LOGICAL ::  random_start_position = .FALSE.           !< namelist parameter (see documentation)
-    LOGICAL ::  read_particles_from_restartfile = .TRUE.  !< namelist parameter (see documentation)
-    LOGICAL ::  seed_follows_topography = .FALSE.         !< namelist parameter (see documentation)
-    LOGICAL ::  splitting = .FALSE.                       !< namelist parameter (see documentation)
-    LOGICAL ::  use_kernel_tables = .FALSE.               !< parameter, which turns on the use of precalculated collision kernels
-    LOGICAL ::  use_sgs_for_particles = .FALSE.           !< namelist parameter (see documentation)
+    LOGICAL ::  use_sgs_for_particles = .FALSE.           !< namelist parameter (see documentation)    
     LOGICAL ::  wang_kernel = .FALSE.                     !< flag for collision kernel
-    LOGICAL ::  write_particle_statistics = .FALSE.       !< namelist parameter (see documentation)
-
-    LOGICAL, DIMENSION(max_number_of_particle_groups) ::                       &
-                vertical_particle_advection = .TRUE.              !< Switch on/off vertical particle transport
-
-    REAL(wp) ::  aero_weight = 1.0_wp                      !< namelist parameter (see documentation)
+
     REAL(wp) ::  alloc_factor = 20.0_wp                    !< namelist parameter (see documentation)
-    REAL(wp) ::  c_0 = 3.0_wp                              !< parameter for lagrangian timescale
-    REAL(wp) ::  dt_min_part = 0.0002_wp                   !< minimum particle time step when SGS velocities are used (s)
-    REAL(wp) ::  dt_prel = 9999999.9_wp                    !< namelist parameter (see documentation)
-    REAL(wp) ::  dt_write_particle_data = 9999999.9_wp     !< namelist parameter (see documentation)
-    REAL(wp) ::  end_time_prel = 9999999.9_wp              !< namelist parameter (see documentation)
-    REAL(wp) ::  initial_weighting_factor = 1.0_wp         !< namelist parameter (see documentation)
-    REAL(wp) ::  last_particle_release_time = 0.0_wp       !< last time of particle release
-    REAL(wp) ::  log_sigma(3) = 1.0_wp                     !< namelist parameter (see documentation)
-    REAL(wp) ::  na(3) = 0.0_wp                            !< namelist parameter (see documentation)
-    REAL(wp) ::  number_concentration = -1.0_wp            !< namelist parameter (see documentation)
     REAL(wp) ::  particle_advection_start = 0.0_wp         !< namelist parameter (see documentation)
-    REAL(wp) ::  radius_merge = 1.0E-7_wp                  !< namelist parameter (see documentation)
-    REAL(wp) ::  radius_split = 40.0E-6_wp                 !< namelist parameter (see documentation)
-    REAL(wp) ::  rm(3) = 1.0E-6_wp                         !< namelist parameter (see documentation)
-    REAL(wp) ::  sgs_wf_part                               !< parameter for sgs
-    REAL(wp) ::  time_write_particle_data = 0.0_wp         !< write particle data at current time on file
-    REAL(wp) ::  weight_factor_merge = -1.0_wp             !< namelist parameter (see documentation)
-    REAL(wp) ::  weight_factor_split = -1.0_wp             !< namelist parameter (see documentation)
-    REAL(wp) ::  z0_av_global                              !< horizontal mean value of z0
-
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  density_ratio = 9999999.9_wp  !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdx = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdy = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdz = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psb = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psl = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psn = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psr = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pss = 9999999.9_wp            !< namelist parameter (see documentation)
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pst = 9999999.9_wp            !< namelist parameter (see documentation).
-    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  radius = 9999999.9_wp         !< namelist parameter (see documentation)
-
-    REAL(wp), DIMENSION(:), ALLOCATABLE     ::  log_z_z0   !< Precalculate LOG(z/z0)
 
 !
Index: palm/trunk/SOURCE/module_interface.f90
===================================================================
--- palm/trunk/SOURCE/module_interface.f90	(revision 4015)
+++ palm/trunk/SOURCE/module_interface.f90	(revision 4017)
@@ -242,5 +242,16 @@
                im_data_output_3d,                                              &
                im_init
-
+               
+    USE lagrangian_particle_model_mod,                                         &
+        ONLY:  lpm_parin,                                                      &
+               lpm_header,                                                     &
+               lpm_check_parameters,                                           &
+               lpm_init,                                                       &
+               lpm_actions,                                                    &
+               lpm_rrd_global,                                                 &
+               lpm_rrd_local,                                                  &
+               lpm_wrd_local,                                                  &
+               lpm_wrd_global
+               
     USE land_surface_model_mod,                                                &
         ONLY:  lsm_parin,                                                      &
@@ -290,4 +301,7 @@
                ocean_rrd_local,                                                &
                ocean_wrd_local
+               
+    USE particle_attributes,                                                   &
+        ONLY:  particle_advection               
 
     USE plant_canopy_model_mod,                                                &
@@ -558,4 +572,5 @@
     CALL gust_parin
     CALL im_parin
+    CALL lpm_parin      
     CALL lsm_parin
     ! ToDo: create parin routine for large_scale_forcing and nudging (should be seperate modules or new module switch)
@@ -574,6 +589,4 @@
     CALL wtm_parin
 
-    CALL package_parin ! ToDo: deprecated, needs to be dissolved
-
     IF ( debug_output )  CALL debug_message( 'reading module-specific parameters', 'end' )
 
@@ -596,4 +609,5 @@
     IF ( gust_module_enabled )  CALL gust_check_parameters
     IF ( indoor_model )         CALL im_check_parameters
+    IF ( particle_advection )   CALL lpm_check_parameters        
     IF ( land_surface )         CALL lsm_check_parameters
     IF ( large_scale_forcing  .OR.  nudging )  CALL lsf_nudging_check_parameters ! ToDo: create single module switch
@@ -896,4 +910,5 @@
     IF ( gust_module_enabled )  CALL gust_init
     IF ( indoor_model        )  CALL im_init
+    IF ( particle_advection  )  CALL lpm_init    
     IF ( large_scale_forcing )  CALL lsf_init
     IF ( land_surface        )  CALL lsm_init
@@ -950,4 +965,5 @@
     IF ( virtual_flight      )  CALL flight_header( io )
     IF ( gust_module_enabled )  CALL gust_header( io )
+    IF ( particle_advection  )  CALL lpm_header( io )    
     IF ( land_surface        )  CALL lsm_header( io )
     IF ( large_scale_forcing )  CALL lsf_nudging_header( io )
@@ -981,4 +997,5 @@
     IF ( air_chemistry       )  CALL chem_actions( location )
     IF ( gust_module_enabled )  CALL gust_actions( location )
+    IF ( particle_advection  )  CALL lpm_actions( location )    
     IF ( ocean_mode          )  CALL ocean_actions( location )
     IF ( salsa               )  CALL salsa_actions( location )
@@ -1377,4 +1394,5 @@
     IF ( .NOT. found )  CALL flight_rrd_global( found ) ! ToDo: change interface to pass variable
     IF ( .NOT. found )  CALL gust_rrd_global( found ) ! ToDo: change interface to pass variable
+    IF ( .NOT. found )  CALL lpm_rrd_global( found ) ! ToDo: change interface to pass variable        
     IF ( .NOT. found )  CALL ocean_rrd_global( found ) ! ToDo: change interface to pass variable
     IF ( .NOT. found )  CALL stg_rrd_global ( found ) ! ToDo: change interface to pass variable
@@ -1480,4 +1498,13 @@
                             ) ! ToDo: change interface to pass variable
 
+    IF ( .NOT. found ) CALL lpm_rrd_local(                                     &
+                               map_index,                                      &
+                               nxlf, nxlc, nxl_on_file,                        &
+                               nxrf, nxrc, nxr_on_file,                        &
+                               nynf, nync, nyn_on_file,                        &
+                               nysf, nysc, nys_on_file,                        &
+                               tmp_3d, found                                   &
+                            ) ! ToDo: change interface to pass variable
+
     IF ( .NOT. found ) CALL lsm_rrd_local(                                     &
                                map_index,                                      &
@@ -1557,4 +1584,5 @@
     IF ( air_chemistry )        CALL chem_wrd_local
     IF ( gust_module_enabled )  CALL gust_wrd_local
+    IF ( particle_advection )   CALL lpm_wrd_local    
     IF ( land_surface )         CALL lsm_wrd_local
     IF ( ocean_mode )           CALL ocean_wrd_local
Index: palm/trunk/SOURCE/modules.f90
===================================================================
--- palm/trunk/SOURCE/modules.f90	(revision 4015)
+++ palm/trunk/SOURCE/modules.f90	(revision 4017)
@@ -1195,5 +1195,4 @@
     INTEGER(iwp) ::  dots_time_count = 0               !< number of output intervals for timeseries output
     INTEGER(iwp) ::  dp_level_ind_b = 0                !< lowest grid index for external pressure gradient forcing
-    INTEGER(iwp) ::  dvrp_filecount = 0                !< parameter for dvr visualization software
     INTEGER(iwp) ::  ensemble_member_nr = 0            !< namelist parameter
     INTEGER(iwp) ::  gamma_mg                          !< switch for steering the multigrid cycle: 1: v-cycle, 2: w-cycle
@@ -1460,5 +1459,4 @@
     REAL(wp) ::  dt_do2d_yz = 9999999.9_wp                     !< namelist parameter
     REAL(wp) ::  dt_do3d = 9999999.9_wp                        !< namelist parameter
-    REAL(wp) ::  dt_dvrp = 9999999.9_wp                        !< namelist parameter
     REAL(wp) ::  dt_max = 20.0_wp                              !< namelist parameter
     REAL(wp) ::  dt_restart = 9999999.9_wp                     !< namelist parameter
@@ -1545,5 +1543,4 @@
     REAL(wp) ::  time_do_av = 0.0_wp                           !< time since last averaged-data output
     REAL(wp) ::  time_do_sla = 0.0_wp                          !< time since last 
-    REAL(wp) ::  time_dvrp = 0.0_wp                            !< time since last dvrp output
     REAL(wp) ::  time_restart = 9999999.9_wp                   !< time at which run shall be terminated and restarted
     REAL(wp) ::  time_run_control = 0.0_wp                     !< time since last RUN_CONTROL output
@@ -1627,123 +1624,4 @@
 
  END MODULE control_parameters
-
-
-!------------------------------------------------------------------------------!
-! Description:
-! ------------
-!> Definition of variables used with dvrp-software.
-!------------------------------------------------------------------------------!
- MODULE dvrp_variables
-
-    USE kinds
-
-    CHARACTER (LEN=10) ::  dvrp_output = 'rtsp'        !< dvr namelist parameter
-    CHARACTER (LEN=10) ::  particle_color = 'none'     !< dvr namelist parameter
-    CHARACTER (LEN=10) ::  particle_dvrpsize = 'none'  !< dvr namelist parameter
-
-    CHARACTER (LEN=20), DIMENSION(10) ::  mode_dvrp = &  !< dvr namelist parameter
-                                     (/ ( '                    ', i9 = 1,10 ) /)
-
-    CHARACTER (LEN=80) ::  dvrp_directory = 'default'                     !< dvr namelist parameter
-    CHARACTER (LEN=80) ::  dvrp_file      = 'default'                     !< dvr namelist parameter
-    CHARACTER (LEN=80) ::  dvrp_host      = 'origin.rvs.uni-hannover.de'  !< dvr namelist parameter
-    CHARACTER (LEN=80) ::  dvrp_password  = '********'                    !< dvr namelist parameter
-    CHARACTER (LEN=80) ::  dvrp_username  = ' '                           !< dvr namelist parameter
-
-    INTEGER(iwp) ::  cluster_size = 1                   !< dvr namelist parameter
-    INTEGER(iwp) ::  dvrp_colortable_entries = 4        !< internal dvr software variable
-    INTEGER(iwp) ::  dvrp_colortable_entries_prt = 22   !< internal dvr software variable
-    INTEGER(iwp) ::  islice_dvrp                        !< internal dvr software variable
-    INTEGER(iwp) ::  nx_dvrp                            !< internal dvr software variable
-    INTEGER(iwp) ::  nxl_dvrp                           !< internal dvr software variable
-    INTEGER(iwp) ::  nxr_dvrp                           !< internal dvr software variable
-    INTEGER(iwp) ::  ny_dvrp                            !< internal dvr software variable
-    INTEGER(iwp) ::  nyn_dvrp                           !< internal dvr software variable
-    INTEGER(iwp) ::  nys_dvrp                           !< internal dvr software variable
-    INTEGER(iwp) ::  nz_dvrp                            !< internal dvr software variable
-    INTEGER(iwp) ::  pathlines_fadeintime = 5           !< dvr namelist parameter
-    INTEGER(iwp) ::  pathlines_fadeouttime = 5          !< dvr namelist parameter
-    INTEGER(iwp) ::  pathlines_linecount = 1000         !< dvr namelist parameter
-    INTEGER(iwp) ::  pathlines_maxhistory = 40          !< dvr namelist parameter
-    INTEGER(iwp) ::  pathlines_wavecount = 10           !< dvr namelist parameter
-    INTEGER(iwp) ::  pathlines_wavetime = 50            !< dvr namelist parameter
-    INTEGER(iwp) ::  vc_gradient_normals = 0            !< dvr namelist parameter
-    INTEGER(iwp) ::  vc_mode = 0                        !< dvr namelist parameter
-    INTEGER(iwp) ::  vc_size_x = 2                      !< dvr namelist parameter
-    INTEGER(iwp) ::  vc_size_y = 2                      !< dvr namelist parameter
-    INTEGER(iwp) ::  vc_size_z = 2                      !< dvr namelist parameter
-
-    INTEGER(iwp), DIMENSION(10) ::  slicer_position_dvrp  !< internal dvr software variable
-
-    LOGICAL ::  cyclic_dvrp = .FALSE.                      !< internal dvr software variable
-    LOGICAL ::  dvrp_overlap                               !< internal dvr software variable
-    LOGICAL ::  dvrp_total_overlap                         !< internal dvr software variable
-    LOGICAL ::  local_dvrserver_running                    !< namelist parameter (ENVPAR namelist provided by palmrun)
-    LOGICAL ::  lock_steering_update = .FALSE.             !< internal dvr software variable
-    LOGICAL ::  use_seperate_pe_for_dvrp_output = .FALSE.  !< internal dvr software variable
-
-    REAL(wp) ::  clip_dvrp_l = 9999999.9_wp  !< dvr namelist parameter
-    REAL(wp) ::  clip_dvrp_n = 9999999.9_wp  !< dvr namelist parameter
-    REAL(wp) ::  clip_dvrp_r = 9999999.9_wp  !< dvr namelist parameter
-    REAL(wp) ::  clip_dvrp_s = 9999999.9_wp  !< dvr namelist parameter
-    REAL(wp) ::  superelevation = 1.0_wp     !< dvr namelist parameter
-    REAL(wp) ::  superelevation_x = 1.0_wp   !< dvr namelist parameter
-    REAL(wp) ::  superelevation_y = 1.0_wp   !< dvr namelist parameter
-    REAL(wp) ::  vc_alpha = 38.0_wp          !< dvr namelist parameter
-
-    REAL(wp), DIMENSION(2) ::  color_interval = (/ 0.0_wp, 1.0_wp /)     !< dvr namelist parameter
-    REAL(wp), DIMENSION(2) ::  dvrpsize_interval = (/ 0.0_wp, 1.0_wp /)  !< dvr namelist parameter
-
-    REAL(wp), DIMENSION(3) ::  groundplate_color = (/ 0.0_wp, 0.6_wp, 0.0_wp /)  !< dvr namelist parameter
-    REAL(wp), DIMENSION(3) ::  topography_color = (/ 0.8_wp, 0.7_wp, 0.6_wp /)   !< dvr namelist parameter
-
-    REAL(wp), DIMENSION(2,10) ::  slicer_range_limits_dvrp  !< dvr namelist parameter
-
-    REAL(wp), DIMENSION(3,10) ::  isosurface_color  !< dvr namelist parameter
-
-    REAL(sp), DIMENSION(2,100) ::  interval_values_dvrp          !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_values_dvrp_prt      !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_h_dvrp               !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_h_dvrp_prt           !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_l_dvrp = 0.5_sp      !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_l_dvrp_prt = 0.5_sp  !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_s_dvrp = 1.0_sp      !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_s_dvrp_prt = 1.0_sp  !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_a_dvrp = 0.0_sp      !< internal dvr software variable
-    REAL(sp), DIMENSION(2,100) ::  interval_a_dvrp_prt = 0.0_sp  !< internal dvr software variable
-
-    DATA  slicer_range_limits_dvrp / -1.0_wp, 1.0_wp, -1.0_wp, 1.0_wp, -1.0_wp, 1.0_wp, &  !< internal dvr software variable
-                                     -1.0_wp, 1.0_wp, -1.0_wp, 1.0_wp, -1.0_wp, 1.0_wp, &
-                                     -1.0_wp, 1.0_wp, -1.0_wp, 1.0_wp, -1.0_wp, 1.0_wp, &
-                                     -1.0_wp, 1.0_wp /
-
-    DATA  isosurface_color / 0.9_wp, 0.9_wp, 0.9_wp,  0.8_wp, 0.1_wp, 0.1_wp,  0.1_wp, 0.1_wp, 0.8_wp, &  !< internal dvr software variable
-                             0.1_wp, 0.8_wp, 0.1_wp,  0.6_wp, 0.1_wp, 0.1_wp,  0.1_wp, 0.1_wp, 0.6_wp, &
-                             0.1_wp, 0.6_wp, 0.1_wp,  0.4_wp, 0.1_wp, 0.1_wp,  0.1_wp, 0.1_wp, 0.4_wp, &
-                             0.1_wp, 0.4_wp, 0.1_wp /
-
-    DATA  interval_h_dvrp / 270.0_wp, 225.0_wp, 225.0_wp, 180.0_wp, 70.0_wp, 25.0_wp, &  !< internal dvr software variable
-                            25.0_wp, -25.0_wp, 192 * 0.0_wp /
-
-    DATA  interval_h_dvrp_prt / 270.0_wp, 225.0_wp, 225.0_wp, 180.0_wp, 70.0_wp, 25.0_wp, &  !< internal dvr software variable
-                                25.0_wp, -25.0_wp, 192 * 0.0_wp /
-
-    REAL(sp), DIMENSION(:), ALLOCATABLE ::  xcoor_dvrp  !< internal dvr software variable
-    REAL(sp), DIMENSION(:), ALLOCATABLE ::  ycoor_dvrp  !< internal dvr software variable
-    REAL(sp), DIMENSION(:), ALLOCATABLE ::  zcoor_dvrp  !< internal dvr software variable
-
-    TYPE steering
-       CHARACTER (LEN=24) ::  name  !< internal dvr software variable
-       REAL(sp)           ::  min   !< internal dvr software variable
-       REAL(sp)           ::  max   !< internal dvr software variable
-       INTEGER(iwp)       ::  imin  !< internal dvr software variable
-       INTEGER(iwp)       ::  imax  !< internal dvr software variable
-    END TYPE steering
-
-    TYPE(steering), DIMENSION(:), ALLOCATABLE ::  steering_dvrp  !< internal dvr software variable
-
-    SAVE
-
- END MODULE dvrp_variables
 
 
Index: palm/trunk/SOURCE/package_parin.f90
===================================================================
--- palm/trunk/SOURCE/package_parin.f90	(revision 4015)
+++ 	(revision )
@@ -1,379 +1,0 @@
-!> @file package_parin.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! Minor formating changes
-! 
-! 3246 2018-09-13 15:14:50Z sward
-! Added error handling for input namelist via parin_fail_message
-! 
-! 3241 2018-09-12 15:02:00Z raasch
-! unused variables removed
-! 
-! 3049 2018-05-29 13:52:36Z Giersch
-! Error messages revised
-! 
-! 2932 2018-03-26 09:39:22Z Giersch
-! renamed particles_par to particle_parameters
-! 
-! 2718 2018-01-02 08:49:38Z maronga
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2375 2017-08-29 14:10:28Z schwenkel
-! Added aerosol_species
-!
-! 2312 2017-07-14 20:26:51Z hoffmann
-! Aerosol initialization improved.
-!
-! 2263 2017-06-08 14:59:01Z schwenkel
-! Implemented splitting and merging algorithm
-!
-! 2183 2017-03-17 14:29:15Z schwenkel
-!
-! 2182 2017-03-17 14:27:40Z schwenkel
-! Added parameters for simplified particle initialization.
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-!
-! 1936 2016-06-13 13:37:44Z suehring
-! +deallocate_memory, step_dealloc
-!
-! 1871 2016-04-15 11:46:09Z hoffmann
-! Initialization of aerosols added.
-!
-! 1833 2016-04-07 14:23:03Z raasch
-! reading of spectra_par moved to spectra_mod
-!
-! 1831 2016-04-07 13:15:51Z hoffmann
-! curvature_solution_effects added
-!
-! 1826 2016-04-07 12:01:39Z maronga
-! Reading of &radiation_par moved to radiation_model_mod.
-! Reading of &canopy_par moved to plant_canopy_model_mod.
-!
-! 822 2016-04-07 07:49:42Z hoffmann
-! +collision_algorithm
-! Tails removed.
-!
-! 1817 2016-04-06 15:44:20Z maronga
-! Reading of &lsm_par moved to land_surface_model_mod.
-!
-! 1788 2016-03-10 11:01:04Z maronga
-! Parameter dewfall removed.
-!
-! 1786 2016-03-08 05:49:27Z raasch
-! cpp-direktives for spectra removed
-!
-! 1757 2016-02-22 15:49:32Z maronga
-! Added parameter unscheduled_radiation_calls
-!
-! 1691 2015-10-26 16:17:44Z maronga
-! Added skip_time_do_lsm, skip_time_do_radiation, and emissivity
-!
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable
-!
-! 1585 2015-04-30 07:05:52Z maronga
-! Added several radiation_par parameters
-!
-! 1575 2015-03-27 09:56:27Z raasch
-! +seed_follows_topography in particles_par
-!
-! 1553 2015-03-03 17:33:54Z maronga
-! Resorting of lsm_par
-!
-! 1551 2015-03-03 14:18:16Z maronga
-! Several changes in the liste for land surface model and radiation model
-!
-! 1496 2014-12-02 17:25:50Z maronga
-! Added support for the land surface model and radiation scheme
-!
-! 1484 2014-10-21 10:53:05Z kanani
-! Changes due to new module structure of the plant canopy model:
-!   module plant_canopy_model_mod added,
-!   new package/namelist canopy_par added, i.e. the canopy model is no longer
-!   steered over the inipar-namelist,
-!   drag_coefficient, leaf_surface_concentration and scalar_exchange_coefficient
-!   renamed to canopy_drag_coeff, leaf_surface_conc and leaf_scalar_exch_coeff.
-! Changed statement tags in CONTINUE-statement
-!
-! 1367 2014-04-23 15:18:30Z witha
-! Bugfix: module kinds must be used
-!
-! 1359 2014-04-11 17:15:14Z hoffmann
-! +alloc_factor, + min_nr_particle
-! -dt_sort_particles, -maximum_number_of_particles
-!
-! 1340 2014-03-25 19:45:13Z kanani
-! REAL constants defined as wp-kinds
-!
-! 1324 2014-03-21 09:13:16Z suehring
-! Bugfix: Missing variable dt_data_output output added to ONLY statement
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! old module precision_kind is removed,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 828 2012-02-21 12:00:36Z raasch
-! +dissipation_classes, radius_classes in parpar
-!
-! 825 2012-02-19 03:03:44Z raasch
-! wang_collision_kernel and turbulence_effects_on_collision in particles_par
-! replaced by collision_kernel
-!
-! Revision 1.1  2000/12/28 13:21:57  raasch
-! Initial revision
-!
-!
-! Description:
-! ------------
-!> This subroutine reads from the NAMELIST file variables controling model
-!> software packages which are used optionally in the run.
-!>
-!> @todo Perform all actions in the respective submodules and remove
-!>       package_parin
-!------------------------------------------------------------------------------!
- SUBROUTINE package_parin
-
-
-    USE control_parameters,                                                    &
-        ONLY:  dt_dopts, dt_dvrp, message_string, particle_maximum_age,        &
-               threshold
-
-    USE dvrp_variables,                                                        &
-        ONLY:  clip_dvrp_l, clip_dvrp_n, clip_dvrp_r, clip_dvrp_s,             &
-               cluster_size, color_interval, dvrpsize_interval,                &
-               dvrp_directory, dvrp_file, dvrp_host, dvrp_output,              &
-               dvrp_password, dvrp_username, groundplate_color,                &
-               isosurface_color, mode_dvrp, particle_color,                    &
-               particle_dvrpsize, pathlines_fadeintime,                        &
-               pathlines_fadeouttime, pathlines_linecount,                     &
-               pathlines_maxhistory, pathlines_wavecount,                      &
-               pathlines_wavetime, slicer_range_limits_dvrp, superelevation,   &
-               superelevation_x, superelevation_y, topography_color,           &
-               vc_alpha, vc_gradient_normals, vc_mode, vc_size_x, vc_size_y,   &
-               vc_size_z
-
-    USE kinds
-
-    USE particle_attributes,                                                   &
-        ONLY:  aero_species, aero_type, aero_weight, alloc_factor, bc_par_b,   &
-               bc_par_lr, bc_par_ns, bc_par_t, collision_kernel,               &
-               curvature_solution_effects, deallocate_memory, density_ratio,   &
-               dissipation_classes, dt_min_part, dt_prel,                      &
-               dt_write_particle_data, end_time_prel, initial_weighting_factor,&
-               log_sigma, max_number_particles_per_gridbox,                    &
-               merging, min_nr_particle, na,                                   &
-               number_concentration, number_of_particle_groups,                &
-               number_particles_per_gridbox, particles_per_point,              &
-               particle_advection, particle_advection_start, pdx, pdy, pdz,    &
-               psb, psl, psn, psr, pss, pst, radius, radius_classes,           &
-               radius_merge, radius_split, random_start_position,              &
-               read_particles_from_restartfile, rm,                            &
-               seed_follows_topography, splitting, splitting_factor,           &
-               splitting_factor_max, splitting_function, splitting_mode,       &
-               step_dealloc, use_sgs_for_particles,                            &
-               vertical_particle_advection, weight_factor_merge,               &
-               weight_factor_split, write_particle_statistics
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=80) ::  line  !<
-
-
-
-    NAMELIST /dvrp_graphics_par/  clip_dvrp_l, clip_dvrp_n, clip_dvrp_r,       &
-                                  clip_dvrp_s, cluster_size, color_interval,   &
-                                  dt_dvrp, dvrpsize_interval, dvrp_directory,  &
-                                  dvrp_file, dvrp_host, dvrp_output,           &
-                                  dvrp_password, dvrp_username,                &
-                                  groundplate_color, isosurface_color,         &
-                                  mode_dvrp, particle_color, particle_dvrpsize,&
-                                  pathlines_fadeintime, pathlines_fadeouttime, &
-                                  pathlines_linecount, pathlines_maxhistory,   &
-                                  pathlines_wavecount, pathlines_wavetime,     &
-                                  slicer_range_limits_dvrp, superelevation,    &
-                                  superelevation_x, superelevation_y,          &
-                                  threshold, topography_color, vc_alpha,       &
-                                  vc_gradient_normals, vc_mode, vc_size_x,     &
-                                  vc_size_y, vc_size_z
-
-    NAMELIST /particles_par/      aero_species, aero_type, aero_weight,        &
-                                  alloc_factor, bc_par_b, bc_par_lr,           &
-                                  bc_par_ns, bc_par_t,                         &
-                                  collision_kernel, curvature_solution_effects,&
-                                  deallocate_memory, density_ratio,            &
-                                  dissipation_classes, dt_dopts,               &
-                                  dt_min_part, dt_prel,                        &
-                                  dt_write_particle_data,                      &
-                                  end_time_prel, initial_weighting_factor,     &
-                                  log_sigma,                                   &
-                                  max_number_particles_per_gridbox, merging,   &
-                                  min_nr_particle,                             &
-                                  na, number_concentration,                    &
-                                  number_of_particle_groups,                   &
-                                  number_particles_per_gridbox,                &
-                                  particles_per_point,                         &
-                                  particle_advection_start,                    &
-                                  particle_maximum_age, pdx, pdy, pdz, psb,    &
-                                  psl, psn, psr, pss, pst, radius,             &
-                                  radius_classes, radius_merge, radius_split,  &
-                                  random_start_position,                       &
-                                  read_particles_from_restartfile, rm,         &
-                                  seed_follows_topography,                     &
-                                  splitting, splitting_factor,                 &
-                                  splitting_factor_max, splitting_function,    &
-                                  splitting_mode, step_dealloc,                &
-                                  use_sgs_for_particles,                       &
-                                  vertical_particle_advection,                 &
-                                  weight_factor_merge, weight_factor_split,    &
-                                  write_particle_statistics
-
-                                  
-    NAMELIST /particle_parameters/                                             &
-                                  aero_species, aero_type, aero_weight,        &
-                                  alloc_factor, bc_par_b, bc_par_lr,           &
-                                  bc_par_ns, bc_par_t,                         &
-                                  collision_kernel, curvature_solution_effects,&
-                                  deallocate_memory, density_ratio,            &
-                                  dissipation_classes, dt_dopts,               &
-                                  dt_min_part, dt_prel,                        &
-                                  dt_write_particle_data,                      &
-                                  end_time_prel, initial_weighting_factor,     &
-                                  log_sigma,                                   &
-                                  max_number_particles_per_gridbox, merging,   &
-                                  min_nr_particle,                             &
-                                  na, number_concentration,                    &
-                                  number_of_particle_groups,                   &
-                                  number_particles_per_gridbox,                &
-                                  particles_per_point,                         &
-                                  particle_advection_start,                    &
-                                  particle_maximum_age, pdx, pdy, pdz, psb,    &
-                                  psl, psn, psr, pss, pst, radius,             &
-                                  radius_classes, radius_merge, radius_split,  &
-                                  random_start_position,                       &
-                                  read_particles_from_restartfile, rm,         &
-                                  seed_follows_topography,                     &
-                                  splitting, splitting_factor,                 &
-                                  splitting_factor_max, splitting_function,    &
-                                  splitting_mode, step_dealloc,                &
-                                  use_sgs_for_particles,                       &
-                                  vertical_particle_advection,                 &
-                                  weight_factor_merge, weight_factor_split,    &
-                                  write_particle_statistics
-!
-!-- Position the namelist-file at the beginning (it was already opened in
-!-- parin), search for the namelist-group of the package and position the
-!-- file at this line. Do the same for each optionally used package.
-    line = ' '
-
-
-#if defined( __dvrp_graphics )
-    REWIND ( 11 )
-    line = ' '
-    DO   WHILE ( INDEX( line, '&dvrp_graphics_par' ) == 0 )
-       READ ( 11, '(A)', END=21 )  line
-    ENDDO
-    BACKSPACE ( 11 )
-
-!
-!-- Read user-defined namelist
-    READ ( 11, dvrp_graphics_par, ERR = 20 )
-
-    GOTO 21
- 20 BACKSPACE( 11 )
-    READ( 11 ,fmt='(A)') line
-    CALL parin_fail_message( 'dvrp_graphics_par', line )
-
- 21 CONTINUE
-#endif
-
-!
-!-- Try to find particles package
-    REWIND ( 11 )
-    line = ' '
-    DO   WHILE ( INDEX( line, '&particle_parameters' ) == 0 )
-       READ ( 11, '(A)', END=12 )  line
-    ENDDO
-    BACKSPACE ( 11 )
-
-!
-!-- Read user-defined namelist
-    READ ( 11, particle_parameters, ERR = 10 )
-
-!
-!-- Set flag that indicates that particles are switched on
-    particle_advection = .TRUE.
-    
-    GOTO 14
-
-10  BACKSPACE( 11 )
-    READ( 11 , '(A)') line
-    CALL parin_fail_message( 'particle_parameters', line )
-
-!
-!-- Try to find particles package (old namelist)
-12  REWIND ( 11 )
-    line = ' '
-    DO WHILE ( INDEX( line, '&particles_par' ) == 0 )
-       READ ( 11, '(A)', END=14 )  line
-    ENDDO
-    BACKSPACE ( 11 )
-
-!
-!-- Read user-defined namelist
-    READ ( 11, particles_par, ERR = 13, END = 14 )
-    
-    
-    message_string = 'namelist particles_par is deprecated and will be ' //    &
-                     'removed in near future. Please use namelist ' //         &
-                     'particle_parameters instead'
-    CALL message( 'package_parin', 'PA0487', 0, 1, 0, 6, 0 )
-
-!
-!-- Set flag that indicates that particles are switched on
-    particle_advection = .TRUE.
-
-       GOTO 14
-
- 13    BACKSPACE( 11 )
-       READ( 11 , '(A)') line
-       CALL parin_fail_message( 'particles_par', line )
-
- 14 CONTINUE
-
- END SUBROUTINE package_parin
Index: palm/trunk/SOURCE/palm.f90
===================================================================
--- palm/trunk/SOURCE/palm.f90	(revision 4015)
+++ palm/trunk/SOURCE/palm.f90	(revision 4017)
@@ -1,3 +1,3 @@
-!> @file palm.f90
+! !> @file palm.f90
 !------------------------------------------------------------------------------!
 ! This file is part of the PALM model system.
@@ -321,7 +321,4 @@
         ONLY:  netcdf_data_input_inquire_file, netcdf_data_input_init,         &
                netcdf_data_input_surface_data, netcdf_data_input_topo
-
-    USE particle_attributes,                                                   &
-        ONLY:  particle_advection
 
     USE pegrid
@@ -452,6 +449,4 @@
     CALL MPI_COMM_RANK( comm_palm, myid, ierr )
 #endif
-
-    CALL init_dvrp_logging
 
 !
@@ -614,8 +609,4 @@
 
        CALL cpu_log( log_point(22), 'wrd_local', 'stop' )
-
-!
-!--    If required, write particle data in own restart files
-       IF ( particle_advection )  CALL lpm_write_restart_file
        
     ENDIF
@@ -641,5 +632,4 @@
 !-- Close files
     CALL close_file( 0 )
-    CALL close_dvrp
 
 !
Index: palm/trunk/SOURCE/parin.f90
===================================================================
--- palm/trunk/SOURCE/parin.f90	(revision 4015)
+++ palm/trunk/SOURCE/parin.f90	(revision 4017)
@@ -503,7 +503,4 @@
     USE date_and_time_mod,                                                     &
         ONLY:  date_init, day_of_year_init, time_utc_init
-
-    USE dvrp_variables,                                                        &
-        ONLY:  local_dvrserver_running
 
     USE grid_variables,                                                        &
@@ -744,5 +741,5 @@
              termination_time_needed, vnest_start_time
 
-    NAMELIST /envpar/  progress_bar_disabled, host, local_dvrserver_running,   &
+    NAMELIST /envpar/  progress_bar_disabled, host,                            &
                        maximum_cpu_time_allowed, maximum_parallel_io_streams,  &
                        read_svf, revision, run_identifier, tasks_per_node,     &
Index: palm/trunk/SOURCE/pmc_particle_interface.f90
===================================================================
--- palm/trunk/SOURCE/pmc_particle_interface.f90	(revision 4015)
+++ palm/trunk/SOURCE/pmc_particle_interface.f90	(revision 4017)
@@ -90,10 +90,7 @@
        ONLY:  prt_count, particles, grid_particles,                            &
               particle_type, number_of_particles, zero_particle,               &
-              ibc_par_t, ibc_par_lr, ibc_par_ns, alloc_factor
-
-   USE lpm_pack_and_sort_mod
-
-   USE lpm_exchange_horiz_mod,                                                 &
-       ONLY: realloc_particles_array
+              ibc_par_t, ibc_par_lr, ibc_par_ns, alloc_factor, min_nr_particle
+
+!    USE lpm_pack_and_sort_mod
 
 #if defined( __parallel )
@@ -956,5 +953,5 @@
                    prt_count(kp,jp,ip) = prt_count(kp,jp,ip) + 1
                    IF ( prt_count(kp,jp,ip) > SIZE( grid_particles(kp,jp,ip)%particles ) ) THEN
-                      CALL realloc_particles_array( ip, jp, kp )
+                      CALL pmc_realloc_particles_array( ip, jp, kp )
                    ENDIF
                    coarse_particles(jc,ic)%parent_particles(n)%x = xc                   ! Adjust coordinates to child grid
@@ -1082,5 +1079,5 @@
 !- Pack particles (eliminate those marked for deletion),
 !- determine new number of particles
-   CALL lpm_sort_in_subboxes
+!    CALL lpm_sort_in_subboxes
 
 #endif
@@ -1137,5 +1134,5 @@
                 prt_count(k,j,i) = prt_count(k,j,i) + 1
                 IF ( prt_count(k,j,i) > SIZE( grid_particles(k,j,i)%particles ) ) THEN
-                   CALL realloc_particles_array( i, j, k )
+                   CALL pmc_realloc_particles_array( i, j, k )
                 ENDIF
                 grid_particles(k,j,i)%particles(prt_count(k,j,i)) = particle_in_win(pindex)
@@ -1155,5 +1152,63 @@
 #endif
  END SUBROUTINE p_copy_particle_to_org_grid
-
+ 
+!------------------------------------------------------------------------------!
+! Description:
+! ------------
+!> If the allocated memory for the particle array do not suffice to add arriving
+!> particles from neighbour grid cells, this subrouting reallocates the 
+!> particle array to assure enough memory is available. 
+!------------------------------------------------------------------------------!
+ SUBROUTINE pmc_realloc_particles_array ( i, j, k, size_in )
+
+    INTEGER(iwp), INTENT(IN)                       ::  i              !<
+    INTEGER(iwp), INTENT(IN)                       ::  j              !<
+    INTEGER(iwp), INTENT(IN)                       ::  k              !<
+    INTEGER(iwp), INTENT(IN), OPTIONAL             ::  size_in        !<
+
+    INTEGER(iwp)                                   ::  old_size        !<
+    INTEGER(iwp)                                   ::  new_size        !<
+    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  tmp_particles_d !<
+    TYPE(particle_type), DIMENSION(500)            ::  tmp_particles_s !<
+
+    old_size = SIZE(grid_particles(k,j,i)%particles)
+
+    IF ( PRESENT(size_in) )   THEN
+       new_size = size_in
+    ELSE
+       new_size = old_size * ( 1.0_wp + alloc_factor / 100.0_wp )
+    ENDIF
+
+    new_size = MAX( new_size, min_nr_particle, old_size + 1 )
+
+    IF ( old_size <= 500 )  THEN
+
+       tmp_particles_s(1:old_size) = grid_particles(k,j,i)%particles(1:old_size)
+
+       DEALLOCATE(grid_particles(k,j,i)%particles)
+       ALLOCATE(grid_particles(k,j,i)%particles(new_size))
+
+       grid_particles(k,j,i)%particles(1:old_size)          = tmp_particles_s(1:old_size)
+       grid_particles(k,j,i)%particles(old_size+1:new_size) = zero_particle
+
+    ELSE
+
+       ALLOCATE(tmp_particles_d(new_size))
+       tmp_particles_d(1:old_size) = grid_particles(k,j,i)%particles
+
+       DEALLOCATE(grid_particles(k,j,i)%particles)
+       ALLOCATE(grid_particles(k,j,i)%particles(new_size))
+
+       grid_particles(k,j,i)%particles(1:old_size)          = tmp_particles_d(1:old_size)
+       grid_particles(k,j,i)%particles(old_size+1:new_size) = zero_particle
+
+       DEALLOCATE(tmp_particles_d)
+
+    ENDIF
+    particles => grid_particles(k,j,i)%particles(1:new_size)
+
+    RETURN
+    
+ END SUBROUTINE pmc_realloc_particles_array
 
 END MODULE pmc_particle_interface
Index: palm/trunk/SOURCE/read_restart_data_mod.f90
===================================================================
--- palm/trunk/SOURCE/read_restart_data_mod.f90	(revision 4015)
+++ palm/trunk/SOURCE/read_restart_data_mod.f90	(revision 4017)
@@ -155,7 +155,4 @@
     USE netcdf_interface,                                                      &
         ONLY:  netcdf_precision, output_for_t0
-
-    USE particle_attributes,                                                   &
-        ONLY:  curvature_solution_effects, iran_part
 
     USE pegrid
@@ -410,6 +407,4 @@
              CASE ( 'current_timestep_number' )
                 READ ( 13 )  current_timestep_number
-             CASE ( 'curvature_solution_effects' )
-                READ ( 13 )  curvature_solution_effects
              CASE ( 'cycle_mg' )
                 READ ( 13 )  cycle_mg
@@ -442,6 +437,4 @@
              CASE ( 'dt_run_control_1d' )
                 READ ( 13 )  dt_run_control_1d
-             CASE ( 'dvrp_filecount' )
-                READ ( 13 )  dvrp_filecount
              CASE ( 'dx' )
                 READ ( 13 )  dx
@@ -676,6 +669,4 @@
              CASE ( 'time_dots' )
                 READ ( 13 )  time_dots
-             CASE ( 'time_dvrp' )
-                READ ( 13 )  time_dvrp
              CASE ( 'time_radiation' )
                 READ ( 13 )  time_radiation
@@ -1349,7 +1340,4 @@
                       tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
-                CASE ( 'iran' ) ! matching random numbers is still unresolved issue
-                   IF ( k == 1 )  READ ( 13 )  iran, iran_part
-
                 CASE ( 'kh' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
@@ -1407,20 +1395,4 @@
                       tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
-                CASE ( 'pc_av' )
-                   IF ( .NOT. ALLOCATED( pc_av ) )  THEN
-                      ALLOCATE( pc_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pc_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =                              &
-                      tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
-
-                CASE ( 'pr_av' )
-                   IF ( .NOT. ALLOCATED( pr_av ) )  THEN
-                      ALLOCATE( pr_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   pr_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =                              &
-                      tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
-
                 CASE ( 'pt' )
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
@@ -1468,28 +1440,4 @@
                    IF ( k == 1 )  READ ( 13 )  tmp_3d
                    ql_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =                              &
-                      tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
-
-                CASE ( 'ql_c_av' )
-                   IF ( .NOT. ALLOCATED( ql_c_av ) )  THEN
-                      ALLOCATE( ql_c_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_c_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =                            &
-                      tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
-
-                CASE ( 'ql_v_av' )
-                   IF ( .NOT. ALLOCATED( ql_v_av ) )  THEN
-                      ALLOCATE( ql_v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_v_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =                            &
-                      tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
-
-                CASE ( 'ql_vp_av' )
-                   IF ( .NOT. ALLOCATED( ql_vp_av ) )  THEN
-                      ALLOCATE( ql_vp_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
-                   ENDIF
-                   IF ( k == 1 )  READ ( 13 )  tmp_3d
-                   ql_vp_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =                           &
                       tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
 
Index: palm/trunk/SOURCE/set_slicer_attributes_dvrp.f90
===================================================================
--- palm/trunk/SOURCE/set_slicer_attributes_dvrp.f90	(revision 4015)
+++ 	(revision )
@@ -1,119 +1,0 @@
-!> @file set_slicer_attributes_dvrp.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! statements added to avoid compiler warnings about unused variables
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1353 2014-04-08 15:21:23Z heinze
-! REAL constants provided with KIND-attribute
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! ONLY-attribute added to USE-statements,
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! old module precision_kind is removed,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 305 2009-04-27 11:58:42Z raasch
-! Initial version
-!
-! Description:
-! ------------
-!> This routine sets the dvrp-slicer attributes
-!------------------------------------------------------------------------------!
- SUBROUTINE set_slicer_attributes_dvrp( n_slicer )
- 
-
-#if defined( __dvrp_graphics )
-
-    USE dvrp_variables,                                                        &
-        ONLY:  dvrp_colortable_entries, interval_h_dvrp, interval_values_dvrp, &
-               slicer_range_limits_dvrp
-
-    USE kinds
-
-    IMPLICIT NONE
-
-    INTEGER(iwp) ::  j         !<
-    INTEGER(iwp) ::  n_slicer  !<
-
-    REAL(wp)     ::  maxv      !<
-    REAL(wp)     ::  meav      !<
-    REAL(wp)     ::  minv      !<
-
-
-!
-!-- Set interval values to user settings.
-!-- The middle of this interval defines the change from blue to yellow
-    minv = slicer_range_limits_dvrp(1,n_slicer)
-    maxv = slicer_range_limits_dvrp(2,n_slicer)
-    meav = ( minv + maxv ) * 0.5_wp
-
-!
-!-- Create appropriate colortable with 100 entries.
-!-- This table ranges from deep blue (min) to deep red (max)
-    DO  j = 1, 50
-       interval_values_dvrp(1,j) = minv + (meav-minv) * ( j - 1.0_wp ) / 50.0_wp
-       interval_values_dvrp(2,j) = minv + (meav-minv) * ( j )          / 50.0_wp
-       interval_h_dvrp(:,j) = 270.0_wp - ( j - 1.0_wp ) * 90.0_wp / 49.0_wp
-    ENDDO
-
-    DO  j = 51, 100
-       interval_values_dvrp(1,j) = meav + (maxv-meav) * ( j - 51.0_wp ) / 50.0_wp
-       interval_values_dvrp(2,j) = meav + (maxv-meav) * ( j - 50.0_wp ) / 50.0_wp
-       interval_h_dvrp(:,j) = 70.0_wp - ( j - 51.0_wp ) * 90.0_wp / 49.0_wp
-    ENDDO
-
-    dvrp_colortable_entries = 100
-
-#else
-!
-!-- These statements are just to avoid compiler warnings about unused variables
-    INTEGER ::  n_slicer  !<
-
-    IF ( n_slicer == 0 )  RETURN
-
-#endif
-
- END SUBROUTINE set_slicer_attributes_dvrp
Index: palm/trunk/SOURCE/time_integration.f90
===================================================================
--- palm/trunk/SOURCE/time_integration.f90	(revision 4015)
+++ palm/trunk/SOURCE/time_integration.f90	(revision 4017)
@@ -544,5 +544,5 @@
                do_sum, dt_3d, dt_averaging_input, dt_averaging_input_pr, dt_coupling,              &
                dt_data_output_av, dt_disturb, dt_do2d_xy, dt_do2d_xz, dt_do2d_yz, dt_do3d,         &
-               dt_domask,dt_dopts, dt_dopr, dt_dopr_listing, dt_dots, dt_dvrp, dt_run_control,     &
+               dt_domask,dt_dopts, dt_dopr, dt_dopr_listing, dt_dots, dt_run_control,              &
                end_time, first_call_lpm, first_call_mas, galilei_transformation, humidity,         &
                indoor_model, intermediate_timestep_count, intermediate_timestep_count_max,         &
@@ -557,5 +557,5 @@
                time_do2d_xz, time_do2d_yz, time_do3d, time_domask, time_dopr, time_dopr_av,        &
                time_dopr_listing, time_dopts, time_dosp, time_dosp_av, time_dots, time_do_av,      &
-               time_do_sla, time_disturb, time_dvrp, time_run_control, time_since_reference_point, &
+               time_do_sla, time_disturb, time_run_control, time_since_reference_point,            &
                turbulent_inflow, turbulent_outflow, urban_surface,                                 &
                use_initial_profile_as_reference, use_single_reference_value, u_gtrans, v_gtrans,   &
@@ -591,6 +591,6 @@
         ONLY:  lsm_boundary_condition, lsm_energy_balance, lsm_soil_model, skip_time_do_lsm
 
-    USE lpm_mod,                                                                                   &
-        ONLY:  lpm
+    USE lagrangian_particle_model_mod,                                                             &
+        ONLY:  lpm_data_output_ptseries
 
     USE lsf_nudging_mod,                                                                           &
@@ -846,10 +846,4 @@
        ENDIF
     ENDIF
-
-#if defined( __dvrp_graphics )
-!
-!-- Time measurement with dvrp software  
-    CALL DVRP_LOG_EVENT( 2, current_timestep_number )
-#endif
 
     CALL location_message( 'atmosphere (and/or ocean) time-stepping', 'start' )
@@ -1003,5 +997,5 @@
                .AND.  intermediate_timestep_count == 1 )                                           &
           THEN
-             CALL lpm
+             CALL module_interface_actions( 'after_prognostic_equations' )
              first_call_lpm = .FALSE.
           ENDIF
@@ -1541,5 +1535,4 @@
           ENDIF
        ENDDO
-       time_dvrp          = time_dvrp        + dt_3d
        IF ( time_since_reference_point >= skip_time_dosp )  THEN
           time_dosp       = time_dosp        + dt_3d
@@ -1838,21 +1831,8 @@
                ( time_since_reference_point >= particle_advection_start  .AND.                     &
                  first_call_lpm ) )  THEN
-             CALL data_output_ptseries
+             CALL lpm_data_output_ptseries
              time_dopts = MOD( time_dopts, MAX( dt_dopts, dt_3d ) )
           ENDIF
        ENDIF
-
-!
-!--    Output of dvrp-graphics (isosurface, particles, slicer)
-#if defined( __dvrp_graphics )
-       CALL DVRP_LOG_EVENT( -2, current_timestep_number-1 )
-#endif
-       IF ( time_dvrp >= dt_dvrp )  THEN
-          CALL data_output_dvrp
-          time_dvrp = MOD( time_dvrp, MAX( dt_dvrp, dt_3d ) )
-       ENDIF
-#if defined( __dvrp_graphics )
-       CALL DVRP_LOG_EVENT( 2, current_timestep_number )
-#endif
 
 !
@@ -1934,8 +1914,4 @@
     IF ( myid == 0 )  CALL finish_progress_bar
 
-#if defined( __dvrp_graphics )
-    CALL DVRP_LOG_EVENT( -2, current_timestep_number )
-#endif
-
     CALL location_message( 'atmosphere (and/or ocean) time-stepping', 'finished' )
 
Index: palm/trunk/SOURCE/user_data_output_dvrp.f90
===================================================================
--- palm/trunk/SOURCE/user_data_output_dvrp.f90	(revision 4015)
+++ 	(revision )
@@ -1,127 +1,0 @@
-!> @file user_data_output_dvrp.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! variables commented + statement added to avoid compiler warnings about unused variables
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! Error messages revised
-! 
-! 2718 2018-01-02 08:49:38Z Giersch
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1320 2014-03-20 08:40:49Z raasch
-! kind-parameters added to all INTEGER and REAL declaration statements,
-! kinds are defined in new module kinds,
-! old module precision_kind is removed,
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements 
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 211 2008-11-11 04:46:24Z raasch
-! Former file user_interface.f90 split into one file per subroutine
-!
-! Description:
-! ------------
-!> Execution of user-defined dvrp output
-!------------------------------------------------------------------------------!
- SUBROUTINE user_data_output_dvrp( output_variable, local_pf )
- 
-
-    USE control_parameters
-
-    USE dvrp_variables
-
-    USE indices
-
-    USE kinds
-
-    USE pegrid
-
-    USE user
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=*) ::  output_variable !< 
-
-!    INTEGER(iwp) ::  i !<
-!    INTEGER(iwp) ::  j !<
-!    INTEGER(iwp) ::  k !<
-
-    REAL(wp), DIMENSION(nxl_dvrp:nxr_dvrp+1,nys_dvrp:nyn_dvrp+1,nzb:nz_do3d) :: &
-              local_pf !< 
-
-!
-!-- Next line is to avoid compiler warning about unused variables. Please remove.
-    IF ( local_pf(nxl_dvrp,nys_dvrp,nzb) == 0.0_wp )  CONTINUE
-
-!
-!-- Here the user-defined DVRP output follows:
-
-!
-!-- Move original array to intermediate array
-    SELECT CASE ( output_variable )
-
-!       CASE ( 'u2', 'u2_xy', 'u2_xz', 'u2_yz'  )
-!!
-!!--       Here the user can add user_defined output quantities. 
-!!--       Uncomment and extend the following lines, if necessary.
-!          DO  i = nxl_dvrp, nxr_dvrp+1
-!             DO  j = nys_dvrp, nyn_dvrp+1
-!                DO  k = nzb, nz_do3d
-!                   local_pf(i,j,k) = u2(k,j,i)
-!                ENDDO
-!             ENDDO
-!          ENDDO
-
-
-       CASE DEFAULT
-!
-!--       The DEFAULT case is reached if output_variable contains a
-!--       wrong character string that is neither recognized in data_output_dvrp
-!--       nor here in user_data_output_dvrp.
-          WRITE( message_string, * ) 'no output possible for: ',               &
-                                     output_variable
-          CALL message( 'user_data_output_dvrp', 'UI0003', 0, 1, 0, 6, 0 )
-          
-
-    END SELECT
-
-
- END SUBROUTINE user_data_output_dvrp
-
Index: palm/trunk/SOURCE/user_dvrp_coltab.f90
===================================================================
--- palm/trunk/SOURCE/user_dvrp_coltab.f90	(revision 4015)
+++ 	(revision )
@@ -1,97 +1,0 @@
-!> @file user_dvrp_coltab.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! statement added to avoid compiler warning about unused variable
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! revision history before 2012 removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements 
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 211 2008-11-11 04:46:24Z raasch
-! Former file user_interface.f90 split into one file per subroutine
-!
-! Description:
-! ------------
-!> Definition of the colour table to be used by the dvrp software.
-!------------------------------------------------------------------------------!
- SUBROUTINE user_dvrp_coltab( mode, variable )
- 
-    
-    USE control_parameters
-        
-    USE dvrp_variables
-    
-    USE kinds
-    
-    USE pegrid
-    
-    USE user
-
-    IMPLICIT NONE
-
-    CHARACTER (LEN=*) ::  mode       !< 
-    CHARACTER (LEN=*) ::  variable   !< 
-
-!
-!-- Next line is to avoid compiler warning about unused variables. Please remove.
-    IF ( variable(1:1) == ' ' )  CONTINUE
-
-
-!
-!-- Here the user-defined actions follow
-    SELECT CASE ( mode )
-
-       CASE ( 'particles' )
-
-       CASE ( 'slicer' )
-
-       CASE DEFAULT
-          message_string = 'unknown mode "' // mode // '"'
-          CALL message( 'user_dvrp_coltab', 'UI0004', 1, 2, 0, 6, 0 )
-
-
-    END SELECT
-
- END SUBROUTINE user_dvrp_coltab
-
Index: palm/trunk/SOURCE/user_lpm_set_attributes.f90
===================================================================
--- palm/trunk/SOURCE/user_lpm_set_attributes.f90	(revision 4015)
+++ 	(revision )
@@ -1,102 +1,0 @@
-!> @file user_lpm_set_attributes.f90
-!------------------------------------------------------------------------------!
-! This file is part of the PALM model system.
-!
-! PALM is free software: you can redistribute it and/or modify it under the
-! terms of the GNU General Public License as published by the Free Software
-! Foundation, either version 3 of the License, or (at your option) any later
-! version.
-!
-! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
-! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
-!
-! You should have received a copy of the GNU General Public License along with
-! PALM. If not, see <http://www.gnu.org/licenses/>.
-!
-! Copyright 1997-2019 Leibniz Universitaet Hannover
-!------------------------------------------------------------------------------!
-!
-! Current revisions:
-! -----------------
-! 
-! 
-! Former revisions:
-! -----------------
-! $Id$
-! unused variables commented out to avoid compiler warnings
-! 
-! 3655 2019-01-07 16:51:22Z knoop
-! Corrected "Former revisions" section
-! 
-! 2696 2017-12-14 17:12:51Z kanani
-! Change in file header (GPL part)
-!
-! 2101 2017-01-05 16:42:31Z suehring
-!
-! 2000 2016-08-20 18:09:15Z knoop
-! Forced header and separation lines into 80 columns
-! 
-! 1682 2015-10-07 23:56:08Z knoop
-! Code annotations made doxygen readable 
-! 
-! 1359 2014-04-11 17:15:14Z hoffmann
-! New particle structure integrated. 
-!
-! 1320 2014-03-20 08:40:49Z raasch
-! kind-parameters added to all INTEGER and REAL declaration statements, 
-! kinds are defined in new module kinds, 
-! old module precision_kind is removed,
-! comment fields (!:) to be used for variable explanations added to
-! all variable declaration statements 
-!
-! 1036 2012-10-22 13:43:42Z raasch
-! code put under GPL (PALM 3.9)
-!
-! 849 2012-03-15 10:35:09Z raasch
-! routine renamed: user_particle_attributes -> user_lpm_set_attributes
-!
-! 211 2008-11-11 04:46:24Z raasch
-! Former file user_interface.f90 split into one file per subroutine
-!
-! Description:
-! ------------
-!> Define the actual particle attributes (size, colour) by the user.
-!------------------------------------------------------------------------------!
- SUBROUTINE user_lpm_set_attributes
- 
-
-!    USE indices,                                                               &
-!        ONLY:  nxl, nxr, nys, nyn, nzb, nzt
-
-    USE kinds
-    
-    USE particle_attributes
-    
-    USE user
-
-    IMPLICIT NONE
-
-!    INTEGER(iwp) ::  ip   !<
-!    INTEGER(iwp) ::  jp   !<
-!    INTEGER(iwp) ::  kp   !<
-!    INTEGER(iwp) ::  n    !<
-
-!
-!-- Here the user-defined actions follow
-!     DO  ip = nxl, nxr
-!        DO  jp = nys, nyn
-!           DO  kp = nzb+1, nzt
-!              number_of_particles = prt_count(kp,jp,ip)
-!              particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
-!              IF ( number_of_particles <= 0 )  CYCLE
-!              DO  n = 1, number_of_particles
-!
-!              ENDDO
-!           ENDDO
-!        ENDDO
-!     ENDDO
-    
-
- END SUBROUTINE user_lpm_set_attributes
-
Index: palm/trunk/SOURCE/write_restart_data_mod.f90
===================================================================
--- palm/trunk/SOURCE/write_restart_data_mod.f90	(revision 4015)
+++ palm/trunk/SOURCE/write_restart_data_mod.f90	(revision 4017)
@@ -116,7 +116,4 @@
         ONLY:  netcdf_precision, output_for_t0
 
-    USE particle_attributes,                                                   &
-        ONLY:  curvature_solution_effects, iran_part
-
     USE pegrid,                                                                &
         ONLY:  collective_wait, hor_index_bounds, myid, numprocs
@@ -324,7 +321,4 @@
        WRITE ( 14 )  current_timestep_number
 
-       CALL wrd_write_string( 'curvature_solution_effects' ) 
-       WRITE ( 14 )  curvature_solution_effects
-
        CALL wrd_write_string( 'cycle_mg' ) 
        WRITE ( 14 )  cycle_mg
@@ -371,7 +365,4 @@
        CALL wrd_write_string( 'dt_run_control_1d' ) 
        WRITE ( 14 )  dt_run_control_1d
-
-       CALL wrd_write_string( 'dvrp_filecount' ) 
-       WRITE ( 14 )  dvrp_filecount
 
        CALL wrd_write_string( 'dx' ) 
@@ -707,7 +698,4 @@
        WRITE ( 14 )  time_dots
 
-       CALL wrd_write_string( 'time_dvrp' ) 
-       WRITE ( 14 )  time_dvrp
-
        CALL wrd_write_string( 'time_radiation' ) 
        WRITE ( 14 )  time_radiation
@@ -935,7 +923,4 @@
        ENDIF
 
-       CALL wrd_write_string( 'iran' ) 
-       WRITE ( 14 )  iran, iran_part 
-
        CALL wrd_write_string( 'kh' ) 
        WRITE ( 14 )  kh
