source: palm/trunk/SOURCE/lagrangian_particle_model_mod.f90 @ 4195

Last change on this file since 4195 was 4195, checked in by schwenkel, 5 years ago

Bugfix for simple_corrector interpolation method in case of ocean runs and introduce particle advection interpolation into header

  • Property svn:keywords set to Id
File size: 349.6 KB
Line 
1!> @file lagrangian_particle_model_mod.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: lagrangian_particle_model_mod.f90 4195 2019-08-28 13:44:27Z schwenkel $
27! Bugfix for simple_corrector interpolation method in case of ocean runs and
28! output particle advection interpolation method into header
29!
30! 4182 2019-08-22 15:20:23Z scharf
31! Corrected "Former revisions" section
32!
33! 4168 2019-08-16 13:50:17Z suehring
34! Replace function get_topography_top_index by topo_top_ind
35!
36! 4145 2019-08-06 09:55:22Z schwenkel
37! Some reformatting
38!
39! 4144 2019-08-06 09:11:47Z raasch
40! relational operators .EQ., .NE., etc. replaced by ==, /=, etc.
41!
42! 4143 2019-08-05 15:14:53Z schwenkel
43! Rename variable and change select case to if statement
44!
45! 4122 2019-07-26 13:11:56Z schwenkel
46! Implement reset method as bottom boundary condition
47!
48! 4121 2019-07-26 10:01:22Z schwenkel
49! Implementation of an simple method for interpolating the velocities to
50! particle position
51!
52! 4114 2019-07-23 14:09:27Z schwenkel
53! Bugfix: Added working precision for if statement
54!
55! 4054 2019-06-27 07:42:18Z raasch
56! bugfix for calculating the minimum particle time step
57!
58! 4044 2019-06-19 12:28:27Z schwenkel
59! Bugfix in case of grid strecting: corrected calculation of k-Index
60!
61! 4043 2019-06-18 16:59:00Z schwenkel
62! Remove min_nr_particle, Add lpm_droplet_interactions_ptq into module
63!
64! 4028 2019-06-13 12:21:37Z schwenkel
65! Further modularization of particle code components
66!
67! 4020 2019-06-06 14:57:48Z schwenkel
68! Removing submodules
69!
70! 4018 2019-06-06 13:41:50Z eckhard
71! Bugfix for former revision
72!
73! 4017 2019-06-06 12:16:46Z schwenkel
74! Modularization of all lagrangian particle model code components
75!
76! 3655 2019-01-07 16:51:22Z knoop
77! bugfix to guarantee correct particle releases in case that the release
78! interval is smaller than the model timestep
79!
80! Revision 1.1  1999/11/25 16:16:06  raasch
81! Initial revision
82!
83!
84! Description:
85! ------------
86!> The embedded LPM allows for studying transport and dispersion processes within
87!> turbulent flows. This model including passive particles that do not show any
88!> feedback on the turbulent flow. Further also particles with inertia and
89!> cloud droplets ca be simulated explicitly.
90!>
91!> @todo test lcm
92!>       implement simple interpolation method for subgrid scale velocites
93!> @note <Enter notes on the module>
94!> @bug  <Enter bug on the module>
95!------------------------------------------------------------------------------!
96 MODULE lagrangian_particle_model_mod
97
98    USE, INTRINSIC ::  ISO_C_BINDING
99
100    USE arrays_3d,                                                             &
101        ONLY:  de_dx, de_dy, de_dz, dzw, zu, zw,  ql_c, ql_v, ql_vp, hyp,      &
102               pt, q, exner, ql, diss, e, u, v, w, km, ql_1, ql_2, pt_p, q_p,  &
103               d_exner, u_p, v_p, w_p
104 
105    USE averaging,                                                             &
106        ONLY:  ql_c_av, pr_av, pc_av, ql_vp_av, ql_v_av
107
108    USE basic_constants_and_equations_mod,                                     &
109        ONLY: molecular_weight_of_solute, molecular_weight_of_water, magnus,   &
110              pi, rd_d_rv, rho_l, r_v, rho_s, vanthoff, l_v, kappa, g, lv_d_cp
111
112    USE control_parameters,                                                    &
113        ONLY:  bc_dirichlet_l, bc_dirichlet_n, bc_dirichlet_r, bc_dirichlet_s, &
114               cloud_droplets, constant_flux_layer, current_timestep_number,   &
115               dt_3d, dt_3d_reached, humidity,                                 &
116               dt_3d_reached_l, dt_dopts, dz, initializing_actions,            &
117               message_string, molecular_viscosity, ocean_mode,                &
118               particle_maximum_age, iran,                                     & 
119               simulated_time, topography, dopts_time_count,                   &
120               time_since_reference_point, rho_surface, u_gtrans, v_gtrans,    &
121               dz_stretch_level, dz_stretch_level_start
122
123    USE cpulog,                                                                &
124        ONLY:  cpu_log, log_point, log_point_s
125
126    USE indices,                                                               &
127        ONLY:  nx, nxl, nxlg, nxrg, nxr, ny, nyn, nys, nyng, nysg, nz, nzb,    &
128               nzb_max, nzt,nbgp, ngp_2dh_outer,                               &
129               topo_top_ind,                                                   &
130               wall_flags_0
131
132    USE kinds
133
134    USE pegrid
135
136    USE particle_attributes
137
138    USE pmc_particle_interface,                                                &
139        ONLY: pmcp_c_get_particle_from_parent, pmcp_p_fill_particle_win,       &
140              pmcp_c_send_particle_to_parent, pmcp_p_empty_particle_win,       &
141              pmcp_p_delete_particles_in_fine_grid_area, pmcp_g_init,          &
142              pmcp_g_print_number_of_particles
143
144    USE pmc_interface,                                                         &
145        ONLY: nested_run
146
147    USE grid_variables,                                                        &
148        ONLY:  ddx, dx, ddy, dy
149
150    USE netcdf_interface,                                                      &
151        ONLY:  netcdf_data_format, netcdf_deflate, dopts_num, id_set_pts,      &
152               id_var_dopts, id_var_time_pts, nc_stat,                         &
153               netcdf_handle_error
154
155    USE random_function_mod,                                                   &
156        ONLY:  random_function
157
158    USE statistics,                                                            &
159        ONLY:  hom
160
161    USE surface_mod,                                                           &
162        ONLY:  bc_h,                                                           &
163               surf_def_h,                                                     &
164               surf_lsm_h,                                                     &
165               surf_usm_h
166
167#if defined( __parallel )  &&  !defined( __mpifh )
168    USE MPI
169#endif
170
171#if defined( __parallel )  &&  defined( __mpifh )
172    INCLUDE "mpif.h"
173#endif     
174
175#if defined( __netcdf )
176    USE NETCDF
177#endif
178
179    IMPLICIT NONE
180
181    CHARACTER(LEN=15) ::  aero_species = 'nacl'                   !< aerosol species
182    CHARACTER(LEN=15) ::  aero_type    = 'maritime'               !< aerosol type
183    CHARACTER(LEN=15) ::  bc_par_lr    = 'cyclic'                 !< left/right boundary condition
184    CHARACTER(LEN=15) ::  bc_par_ns    = 'cyclic'                 !< north/south boundary condition
185    CHARACTER(LEN=15) ::  bc_par_b     = 'reflect'                !< bottom boundary condition
186    CHARACTER(LEN=15) ::  bc_par_t     = 'absorb'                 !< top boundary condition
187    CHARACTER(LEN=15) ::  collision_kernel   = 'none'             !< collision kernel
188
189    CHARACTER(LEN=5)  ::  splitting_function = 'gamma'            !< function for calculation critical weighting factor
190    CHARACTER(LEN=5)  ::  splitting_mode     = 'const'            !< splitting mode
191
192    CHARACTER(LEN=25) ::  particle_advection_interpolation = 'trilinear' !< interpolation method for calculatin the particle
193
194    INTEGER(iwp) ::  deleted_particles = 0                        !< number of deleted particles per time step   
195    INTEGER(iwp) ::  i_splitting_mode                             !< dummy for splitting mode
196    INTEGER(iwp) ::  iran_part = -1234567                         !< number for random generator   
197    INTEGER(iwp) ::  max_number_particles_per_gridbox = 100       !< namelist parameter (see documentation)
198    INTEGER(iwp) ::  isf                                          !< dummy for splitting function
199    INTEGER(iwp) ::  number_particles_per_gridbox = -1            !< namelist parameter (see documentation)
200    INTEGER(iwp) ::  number_of_sublayers = 20                     !< number of sublayers for particle velocities betwenn surface and first grid level
201    INTEGER(iwp) ::  offset_ocean_nzt = 0                         !< in case of oceans runs, the vertical index calculations need an offset
202    INTEGER(iwp) ::  offset_ocean_nzt_m1 = 0                      !< in case of oceans runs, the vertical index calculations need an offset
203    INTEGER(iwp) ::  particles_per_point = 1                      !< namelist parameter (see documentation)
204    INTEGER(iwp) ::  radius_classes = 20                          !< namelist parameter (see documentation)
205    INTEGER(iwp) ::  splitting_factor = 2                         !< namelist parameter (see documentation)
206    INTEGER(iwp) ::  splitting_factor_max = 5                     !< namelist parameter (see documentation)
207    INTEGER(iwp) ::  step_dealloc = 100                           !< namelist parameter (see documentation)
208    INTEGER(iwp) ::  total_number_of_particles                    !< total number of particles in the whole model domain
209    INTEGER(iwp) ::  trlp_count_sum                               !< parameter for particle exchange of PEs
210    INTEGER(iwp) ::  trlp_count_recv_sum                          !< parameter for particle exchange of PEs
211    INTEGER(iwp) ::  trrp_count_sum                               !< parameter for particle exchange of PEs
212    INTEGER(iwp) ::  trrp_count_recv_sum                          !< parameter for particle exchange of PEs
213    INTEGER(iwp) ::  trsp_count_sum                               !< parameter for particle exchange of PEs
214    INTEGER(iwp) ::  trsp_count_recv_sum                          !< parameter for particle exchange of PEs
215    INTEGER(iwp) ::  trnp_count_sum                               !< parameter for particle exchange of PEs
216    INTEGER(iwp) ::  trnp_count_recv_sum                          !< parameter for particle exchange of PEs
217
218    LOGICAL ::  lagrangian_particle_model = .FALSE.       !< namelist parameter (see documentation)
219    LOGICAL ::  curvature_solution_effects = .FALSE.      !< namelist parameter (see documentation)
220    LOGICAL ::  deallocate_memory = .TRUE.                !< namelist parameter (see documentation)
221    LOGICAL ::  hall_kernel = .FALSE.                     !< flag for collision kernel
222    LOGICAL ::  merging = .FALSE.                         !< namelist parameter (see documentation)
223    LOGICAL ::  random_start_position = .FALSE.           !< namelist parameter (see documentation)
224    LOGICAL ::  read_particles_from_restartfile = .TRUE.  !< namelist parameter (see documentation)
225    LOGICAL ::  seed_follows_topography = .FALSE.         !< namelist parameter (see documentation)
226    LOGICAL ::  splitting = .FALSE.                       !< namelist parameter (see documentation)
227    LOGICAL ::  use_kernel_tables = .FALSE.               !< parameter, which turns on the use of precalculated collision kernels
228    LOGICAL ::  write_particle_statistics = .FALSE.       !< namelist parameter (see documentation)
229    LOGICAL ::  interpolation_simple_predictor = .FALSE.  !< flag for simple particle advection interpolation with predictor step
230    LOGICAL ::  interpolation_simple_corrector = .FALSE.  !< flag for simple particle advection interpolation with corrector step
231    LOGICAL ::  interpolation_trilinear = .FALSE.         !< flag for trilinear particle advection interpolation
232
233    LOGICAL, DIMENSION(max_number_of_particle_groups) ::   vertical_particle_advection = .TRUE. !< Switch for vertical particle transport
234
235    REAL(wp) ::  aero_weight = 1.0_wp                      !< namelist parameter (see documentation)
236    REAL(wp) ::  dt_min_part = 0.0002_wp                   !< minimum particle time step when SGS velocities are used (s)
237    REAL(wp) ::  dt_prel = 9999999.9_wp                    !< namelist parameter (see documentation)
238    REAL(wp) ::  dt_write_particle_data = 9999999.9_wp     !< namelist parameter (see documentation)
239    REAL(wp) ::  end_time_prel = 9999999.9_wp              !< namelist parameter (see documentation)
240    REAL(wp) ::  initial_weighting_factor = 1.0_wp         !< namelist parameter (see documentation)
241    REAL(wp) ::  last_particle_release_time = 0.0_wp       !< last time of particle release
242    REAL(wp) ::  log_sigma(3) = 1.0_wp                     !< namelist parameter (see documentation)
243    REAL(wp) ::  na(3) = 0.0_wp                            !< namelist parameter (see documentation)
244    REAL(wp) ::  number_concentration = -1.0_wp            !< namelist parameter (see documentation)
245    REAL(wp) ::  radius_merge = 1.0E-7_wp                  !< namelist parameter (see documentation)
246    REAL(wp) ::  radius_split = 40.0E-6_wp                 !< namelist parameter (see documentation)
247    REAL(wp) ::  rm(3) = 1.0E-6_wp                         !< namelist parameter (see documentation)
248    REAL(wp) ::  sgs_wf_part                               !< parameter for sgs
249    REAL(wp) ::  time_write_particle_data = 0.0_wp         !< write particle data at current time on file
250    REAL(wp) ::  weight_factor_merge = -1.0_wp             !< namelist parameter (see documentation)
251    REAL(wp) ::  weight_factor_split = -1.0_wp             !< namelist parameter (see documentation)
252    REAL(wp) ::  z0_av_global                              !< horizontal mean value of z0
253
254    REAL(wp) ::  rclass_lbound !<
255    REAL(wp) ::  rclass_ubound !<
256
257    REAL(wp), PARAMETER ::  c_0 = 3.0_wp         !< parameter for lagrangian timescale
258
259    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  density_ratio = 9999999.9_wp  !< namelist parameter (see documentation)
260    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdx = 9999999.9_wp            !< namelist parameter (see documentation)
261    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdy = 9999999.9_wp            !< namelist parameter (see documentation)
262    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pdz = 9999999.9_wp            !< namelist parameter (see documentation)
263    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psb = 9999999.9_wp            !< namelist parameter (see documentation)
264    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psl = 9999999.9_wp            !< namelist parameter (see documentation)
265    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psn = 9999999.9_wp            !< namelist parameter (see documentation)
266    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  psr = 9999999.9_wp            !< namelist parameter (see documentation)
267    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pss = 9999999.9_wp            !< namelist parameter (see documentation)
268    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  pst = 9999999.9_wp            !< namelist parameter (see documentation).
269    REAL(wp), DIMENSION(max_number_of_particle_groups) ::  radius = 9999999.9_wp         !< namelist parameter (see documentation)
270
271    REAL(wp), DIMENSION(:), ALLOCATABLE     ::  log_z_z0   !< Precalculate LOG(z/z0) 
272
273    INTEGER(iwp), PARAMETER ::  NR_2_direction_move = 10000 !<
274    INTEGER(iwp)            ::  nr_move_north               !<
275    INTEGER(iwp)            ::  nr_move_south               !<
276
277    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  move_also_north
278    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  move_also_south
279
280    REAL(wp) ::  epsilon_collision !<
281    REAL(wp) ::  urms              !<
282
283    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  epsclass  !< dissipation rate class
284    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  radclass  !< radius class
285    REAL(wp), DIMENSION(:),   ALLOCATABLE ::  winf      !<
286
287    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ec        !<
288    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  ecf       !<
289    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  gck       !<
290    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  hkernel   !<
291    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  hwratio   !<
292
293    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  ckernel !<
294
295    INTEGER(iwp), PARAMETER         ::  PHASE_INIT    = 1  !<
296    INTEGER(iwp), PARAMETER, PUBLIC ::  PHASE_RELEASE = 2  !<
297
298    SAVE
299
300    PRIVATE
301
302    PUBLIC lpm_parin,     &
303           lpm_header,    &
304           lpm_init_arrays,&
305           lpm_init,      &
306           lpm_actions,   &
307           lpm_data_output_ptseries, &
308           lpm_interaction_droplets_ptq, &
309           lpm_rrd_local_particles, &
310           lpm_wrd_local, &
311           lpm_rrd_global, &
312           lpm_wrd_global, &
313           lpm_rrd_local, &
314           lpm_check_parameters
315
316    PUBLIC lagrangian_particle_model
317
318    INTERFACE lpm_check_parameters
319       MODULE PROCEDURE lpm_check_parameters
320    END INTERFACE lpm_check_parameters
321
322    INTERFACE lpm_parin
323       MODULE PROCEDURE lpm_parin
324    END INTERFACE lpm_parin
325
326    INTERFACE lpm_header
327       MODULE PROCEDURE lpm_header
328    END INTERFACE lpm_header
329
330    INTERFACE lpm_init_arrays
331       MODULE PROCEDURE lpm_init_arrays
332    END INTERFACE lpm_init_arrays
333 
334    INTERFACE lpm_init
335       MODULE PROCEDURE lpm_init
336    END INTERFACE lpm_init
337
338    INTERFACE lpm_actions
339       MODULE PROCEDURE lpm_actions
340    END INTERFACE lpm_actions
341
342    INTERFACE lpm_data_output_ptseries
343       MODULE PROCEDURE lpm_data_output_ptseries
344    END INTERFACE
345
346    INTERFACE lpm_rrd_local_particles
347       MODULE PROCEDURE lpm_rrd_local_particles
348    END INTERFACE lpm_rrd_local_particles
349
350    INTERFACE lpm_rrd_global
351       MODULE PROCEDURE lpm_rrd_global
352    END INTERFACE lpm_rrd_global
353
354    INTERFACE lpm_rrd_local
355       MODULE PROCEDURE lpm_rrd_local
356    END INTERFACE lpm_rrd_local
357
358    INTERFACE lpm_wrd_local
359       MODULE PROCEDURE lpm_wrd_local
360    END INTERFACE lpm_wrd_local
361
362    INTERFACE lpm_wrd_global
363       MODULE PROCEDURE lpm_wrd_global
364    END INTERFACE lpm_wrd_global
365
366    INTERFACE lpm_advec
367       MODULE PROCEDURE lpm_advec
368    END INTERFACE lpm_advec
369
370    INTERFACE lpm_calc_liquid_water_content
371       MODULE PROCEDURE lpm_calc_liquid_water_content
372    END INTERFACE
373
374    INTERFACE lpm_interaction_droplets_ptq
375       MODULE PROCEDURE lpm_interaction_droplets_ptq
376       MODULE PROCEDURE lpm_interaction_droplets_ptq_ij
377    END INTERFACE lpm_interaction_droplets_ptq
378
379    INTERFACE lpm_boundary_conds
380       MODULE PROCEDURE lpm_boundary_conds
381    END INTERFACE lpm_boundary_conds
382
383    INTERFACE lpm_droplet_condensation
384       MODULE PROCEDURE lpm_droplet_condensation
385    END INTERFACE
386
387    INTERFACE lpm_droplet_collision
388       MODULE PROCEDURE lpm_droplet_collision
389    END INTERFACE lpm_droplet_collision
390
391    INTERFACE lpm_init_kernels
392       MODULE PROCEDURE lpm_init_kernels
393    END INTERFACE lpm_init_kernels
394
395    INTERFACE lpm_splitting
396       MODULE PROCEDURE lpm_splitting
397    END INTERFACE lpm_splitting
398
399    INTERFACE lpm_merging
400       MODULE PROCEDURE lpm_merging
401    END INTERFACE lpm_merging
402
403    INTERFACE lpm_exchange_horiz
404       MODULE PROCEDURE lpm_exchange_horiz
405    END INTERFACE lpm_exchange_horiz
406
407    INTERFACE lpm_move_particle
408       MODULE PROCEDURE lpm_move_particle
409    END INTERFACE lpm_move_particle
410
411    INTERFACE realloc_particles_array
412       MODULE PROCEDURE realloc_particles_array
413    END INTERFACE realloc_particles_array
414
415    INTERFACE dealloc_particles_array
416       MODULE PROCEDURE dealloc_particles_array
417    END INTERFACE dealloc_particles_array
418
419    INTERFACE lpm_sort_and_delete
420       MODULE PROCEDURE lpm_sort_and_delete
421    END INTERFACE lpm_sort_and_delete
422
423    INTERFACE lpm_sort_timeloop_done
424       MODULE PROCEDURE lpm_sort_timeloop_done
425    END INTERFACE lpm_sort_timeloop_done
426
427    INTERFACE lpm_pack
428       MODULE PROCEDURE lpm_pack
429    END INTERFACE lpm_pack
430
431 CONTAINS
432 
433
434!------------------------------------------------------------------------------!
435! Description:
436! ------------
437!> Parin for &particle_parameters for the Lagrangian particle model
438!------------------------------------------------------------------------------!
439 SUBROUTINE lpm_parin
440 
441    CHARACTER (LEN=80) ::  line  !<
442
443    NAMELIST /particles_par/ &
444       aero_species, &
445       aero_type, &
446       aero_weight, &
447       alloc_factor, &
448       bc_par_b, &
449       bc_par_lr, &
450       bc_par_ns, &
451       bc_par_t, &
452       collision_kernel, &
453       curvature_solution_effects, &
454       deallocate_memory, &
455       density_ratio, &
456       dissipation_classes, &
457       dt_dopts, &
458       dt_min_part, &
459       dt_prel, &
460       dt_write_particle_data, &
461       end_time_prel, &
462       initial_weighting_factor, &
463       log_sigma, &
464       max_number_particles_per_gridbox, &
465       merging, &
466       na, &
467       number_concentration, &
468       number_of_particle_groups, &
469       number_particles_per_gridbox, &
470       particles_per_point, &
471       particle_advection_start, &
472       particle_advection_interpolation, &
473       particle_maximum_age, &
474       pdx, &
475       pdy, &
476       pdz, &
477       psb, &
478       psl, &
479       psn, &
480       psr, &
481       pss, &
482       pst, &
483       radius, &
484       radius_classes, &
485       radius_merge, &
486       radius_split, &
487       random_start_position, &
488       read_particles_from_restartfile, &
489       rm, &
490       seed_follows_topography, &
491       splitting, &
492       splitting_factor, &
493       splitting_factor_max, &
494       splitting_function, &
495       splitting_mode, &
496       step_dealloc, &
497       use_sgs_for_particles, &
498       vertical_particle_advection, &
499       weight_factor_merge, &
500       weight_factor_split, &
501       write_particle_statistics
502
503       NAMELIST /particle_parameters/ &
504       aero_species, &
505       aero_type, &
506       aero_weight, &
507       alloc_factor, &
508       bc_par_b, &
509       bc_par_lr, &
510       bc_par_ns, &
511       bc_par_t, &
512       collision_kernel, &
513       curvature_solution_effects, &
514       deallocate_memory, &
515       density_ratio, &
516       dissipation_classes, &
517       dt_dopts, &
518       dt_min_part, &
519       dt_prel, &
520       dt_write_particle_data, &
521       end_time_prel, &
522       initial_weighting_factor, &
523       log_sigma, &
524       max_number_particles_per_gridbox, &
525       merging, &
526       na, &
527       number_concentration, &
528       number_of_particle_groups, &
529       number_particles_per_gridbox, &
530       particles_per_point, &
531       particle_advection_start, &
532       particle_advection_interpolation, &
533       particle_maximum_age, &
534       pdx, &
535       pdy, &
536       pdz, &
537       psb, &
538       psl, &
539       psn, &
540       psr, &
541       pss, &
542       pst, &
543       radius, &
544       radius_classes, &
545       radius_merge, &
546       radius_split, &
547       random_start_position, &
548       read_particles_from_restartfile, &
549       rm, &
550       seed_follows_topography, &
551       splitting, &
552       splitting_factor, &
553       splitting_factor_max, &
554       splitting_function, &
555       splitting_mode, &
556       step_dealloc, &
557       use_sgs_for_particles, &
558       vertical_particle_advection, &
559       weight_factor_merge, &
560       weight_factor_split, &
561       write_particle_statistics
562
563!
564!-- Position the namelist-file at the beginning (it was already opened in
565!-- parin), search for the namelist-group of the package and position the
566!-- file at this line. Do the same for each optionally used package.
567    line = ' '
568   
569!
570!-- Try to find particles package
571    REWIND ( 11 )
572    line = ' '
573    DO   WHILE ( INDEX( line, '&particle_parameters' ) == 0 )
574       READ ( 11, '(A)', END=12 )  line
575    ENDDO
576    BACKSPACE ( 11 )
577!
578!-- Read user-defined namelist
579    READ ( 11, particle_parameters, ERR = 10 )
580!
581!-- Set flag that indicates that particles are switched on
582    particle_advection = .TRUE.
583   
584    GOTO 14
585
58610  BACKSPACE( 11 )
587    READ( 11 , '(A)') line
588    CALL parin_fail_message( 'particle_parameters', line )
589!
590!-- Try to find particles package (old namelist)
59112  REWIND ( 11 )
592    line = ' '
593    DO WHILE ( INDEX( line, '&particles_par' ) == 0 )
594       READ ( 11, '(A)', END=14 )  line
595    ENDDO
596    BACKSPACE ( 11 )
597!
598!-- Read user-defined namelist
599    READ ( 11, particles_par, ERR = 13, END = 14 )
600
601    message_string = 'namelist particles_par is deprecated and will be ' //    &
602                     'removed in near future. Please use namelist ' //         &
603                     'particle_parameters instead'
604    CALL message( 'package_parin', 'PA0487', 0, 1, 0, 6, 0 )
605
606!
607!-- Set flag that indicates that particles are switched on
608    particle_advection = .TRUE.
609
610    GOTO 14
611
61213    BACKSPACE( 11 )
613       READ( 11 , '(A)') line
614       CALL parin_fail_message( 'particles_par', line )
615
61614 CONTINUE
617
618 END SUBROUTINE lpm_parin
619 
620!------------------------------------------------------------------------------!
621! Description:
622! ------------
623!> Writes used particle attributes in header file.
624!------------------------------------------------------------------------------!
625 SUBROUTINE lpm_header ( io )
626
627    CHARACTER (LEN=40) ::  output_format       !< netcdf format
628
629    INTEGER(iwp) ::  i               !<
630    INTEGER(iwp), INTENT(IN) ::  io  !< Unit of the output file
631
632
633     IF ( humidity  .AND.  cloud_droplets )  THEN
634       WRITE ( io, 433 )
635       IF ( curvature_solution_effects )  WRITE ( io, 434 )
636       IF ( collision_kernel /= 'none' )  THEN
637          WRITE ( io, 435 )  TRIM( collision_kernel )
638          IF ( collision_kernel(6:9) == 'fast' )  THEN
639             WRITE ( io, 436 )  radius_classes, dissipation_classes
640          ENDIF
641       ELSE
642          WRITE ( io, 437 )
643       ENDIF
644    ENDIF
645 
646    IF ( particle_advection )  THEN
647!
648!--    Particle attributes
649       WRITE ( io, 480 )  particle_advection_start, TRIM(particle_advection_interpolation), &
650                          dt_prel, bc_par_lr, &
651                          bc_par_ns, bc_par_b, bc_par_t, particle_maximum_age, &
652                          end_time_prel
653       IF ( use_sgs_for_particles )  WRITE ( io, 488 )  dt_min_part
654       IF ( random_start_position )  WRITE ( io, 481 )
655       IF ( seed_follows_topography )  WRITE ( io, 496 )
656       IF ( particles_per_point > 1 )  WRITE ( io, 489 )  particles_per_point
657       WRITE ( io, 495 )  total_number_of_particles
658       IF ( dt_write_particle_data /= 9999999.9_wp )  THEN
659          WRITE ( io, 485 )  dt_write_particle_data
660          IF ( netcdf_data_format > 1 )  THEN
661             output_format = 'netcdf (64 bit offset) and binary'
662          ELSE
663             output_format = 'netcdf and binary'
664          ENDIF
665          IF ( netcdf_deflate == 0 )  THEN
666             WRITE ( io, 344 )  output_format
667          ELSE
668             WRITE ( io, 354 )  TRIM( output_format ), netcdf_deflate
669          ENDIF
670       ENDIF
671       IF ( dt_dopts /= 9999999.9_wp )  WRITE ( io, 494 )  dt_dopts
672       IF ( write_particle_statistics )  WRITE ( io, 486 )
673
674       WRITE ( io, 487 )  number_of_particle_groups
675
676       DO  i = 1, number_of_particle_groups
677          IF ( i == 1  .AND.  density_ratio(i) == 9999999.9_wp )  THEN
678             WRITE ( io, 490 )  i, 0.0_wp
679             WRITE ( io, 492 )
680          ELSE
681             WRITE ( io, 490 )  i, radius(i)
682             IF ( density_ratio(i) /= 0.0_wp )  THEN
683                WRITE ( io, 491 )  density_ratio(i)
684             ELSE
685                WRITE ( io, 492 )
686             ENDIF
687          ENDIF
688          WRITE ( io, 493 )  psl(i), psr(i), pss(i), psn(i), psb(i), pst(i), &
689                             pdx(i), pdy(i), pdz(i)
690          IF ( .NOT. vertical_particle_advection(i) )  WRITE ( io, 482 )
691       ENDDO
692
693    ENDIF
694   
695344 FORMAT ('       Output format: ',A/)
696354 FORMAT ('       Output format: ',A, '   compressed with level: ',I1/)
697
698433 FORMAT ('    Cloud droplets treated explicitly using the Lagrangian part', &
699                 'icle model')
700434 FORMAT ('    Curvature and solution effecs are considered for growth of', &
701                 ' droplets < 1.0E-6 m')
702435 FORMAT ('    Droplet collision is handled by ',A,'-kernel')
703436 FORMAT ('       Fast kernel with fixed radius- and dissipation classes ', &
704                    'are used'/ &
705            '          number of radius classes:       ',I3,'    interval ', &
706                       '[1.0E-6,2.0E-4] m'/ &
707            '          number of dissipation classes:   ',I2,'    interval ', &
708                       '[0,1000] cm**2/s**3')
709437 FORMAT ('    Droplet collision is switched off')
710
711480 FORMAT ('    Particles:'/ &
712            '    ---------'// &
713            '       Particle advection is active (switched on at t = ', F7.1, &
714                    ' s)'/ &
715            '       Interpolation of particle velocities is done by using ', A, &
716                    ' method'/ &
717            '       Start of new particle generations every  ',F6.1,' s'/ &
718            '       Boundary conditions: left/right: ', A, ' north/south: ', A/&
719            '                            bottom:     ', A, ' top:         ', A/&
720            '       Maximum particle age:                 ',F9.1,' s'/ &
721            '       Advection stopped at t = ',F9.1,' s'/)
722481 FORMAT ('       Particles have random start positions'/)
723482 FORMAT ('          Particles are advected only horizontally'/)
724485 FORMAT ('       Particle data are written on file every ', F9.1, ' s')
725486 FORMAT ('       Particle statistics are written on file'/)
726487 FORMAT ('       Number of particle groups: ',I2/)
727488 FORMAT ('       SGS velocity components are used for particle advection'/ &
728            '          minimum timestep for advection:', F8.5/)
729489 FORMAT ('       Number of particles simultaneously released at each ', &
730                    'point: ', I5/)
731490 FORMAT ('       Particle group ',I2,':'/ &
732            '          Particle radius: ',E10.3, 'm')
733491 FORMAT ('          Particle inertia is activated'/ &
734            '             density_ratio (rho_fluid/rho_particle) =',F6.3/)
735492 FORMAT ('          Particles are advected only passively (no inertia)'/)
736493 FORMAT ('          Boundaries of particle source: x:',F8.1,' - ',F8.1,' m'/&
737            '                                         y:',F8.1,' - ',F8.1,' m'/&
738            '                                         z:',F8.1,' - ',F8.1,' m'/&
739            '          Particle distances:  dx = ',F8.1,' m  dy = ',F8.1, &
740                       ' m  dz = ',F8.1,' m'/)
741494 FORMAT ('       Output of particle time series in NetCDF format every ', &
742                    F8.2,' s'/)
743495 FORMAT ('       Number of particles in total domain: ',I10/)
744496 FORMAT ('       Initial vertical particle positions are interpreted ', &
745                    'as relative to the given topography')
746   
747 END SUBROUTINE lpm_header
748 
749!------------------------------------------------------------------------------!
750! Description:
751! ------------
752!> Writes used particle attributes in header file.
753!------------------------------------------------------------------------------! 
754 SUBROUTINE lpm_check_parameters
755 
756!
757!-- Collision kernels:
758    SELECT CASE ( TRIM( collision_kernel ) )
759
760       CASE ( 'hall', 'hall_fast' )
761          hall_kernel = .TRUE.
762
763       CASE ( 'wang', 'wang_fast' )
764          wang_kernel = .TRUE.
765
766       CASE ( 'none' )
767
768
769       CASE DEFAULT
770          message_string = 'unknown collision kernel: collision_kernel = "' // &
771                           TRIM( collision_kernel ) // '"'
772          CALL message( 'check_parameters', 'PA0350', 1, 2, 0, 6, 0 )
773
774    END SELECT
775    IF ( collision_kernel(6:9) == 'fast' )  use_kernel_tables = .TRUE.
776
777!
778!-- Subgrid scale velocites with the simple interpolation method for resolved
779!-- velocites is not implemented for passive particles. However, for cloud
780!-- it can be combined as the sgs-velocites for active particles are
781!-- calculated differently, i.e. no subboxes are needed.
782    IF ( .NOT. TRIM( particle_advection_interpolation ) == 'trilinear'  .AND.  &
783       use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
784          message_string = 'subrgrid scale velocities in combination with ' // &
785                           'simple interpolation method is not '            // &
786                           'implemented'
787          CALL message( 'check_parameters', 'PA0659', 1, 2, 0, 6, 0 )
788    ENDIF
789
790 END SUBROUTINE
791 
792!------------------------------------------------------------------------------!
793! Description:
794! ------------
795!> Initialize arrays for lpm
796!------------------------------------------------------------------------------!   
797 SUBROUTINE lpm_init_arrays
798 
799    IF ( cloud_droplets )  THEN
800!
801!--    Liquid water content, change in liquid water content
802       ALLOCATE ( ql_1(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                      &
803                  ql_2(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
804!
805!--    Real volume of particles (with weighting), volume of particles
806       ALLOCATE ( ql_v(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                      &
807                     ql_vp(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
808    ENDIF
809   
810!
811!--    Initial assignment of the pointers   
812    IF ( cloud_droplets )  THEN
813       ql   => ql_1
814       ql_c => ql_2
815    ENDIF
816   
817 END SUBROUTINE lpm_init_arrays
818 
819!------------------------------------------------------------------------------!
820! Description:
821! ------------
822!> Initialize Lagrangian particle model
823!------------------------------------------------------------------------------!
824 SUBROUTINE lpm_init
825
826    INTEGER(iwp) ::  i                           !<
827    INTEGER(iwp) ::  j                           !<
828    INTEGER(iwp) ::  k                           !<
829
830    REAL(wp) ::  div                             !<
831    REAL(wp) ::  height_int                      !<
832    REAL(wp) ::  height_p                        !<
833    REAL(wp) ::  z_p                             !<
834    REAL(wp) ::  z0_av_local                     !<
835
836!
837!-- In case of oceans runs, the vertical index calculations need an offset,
838!-- because otherwise the k indices will become negative
839    IF ( ocean_mode )  THEN
840       offset_ocean_nzt    = nzt
841       offset_ocean_nzt_m1 = nzt - 1
842    ENDIF
843
844!
845!-- Define block offsets for dividing a gridcell in 8 sub cells
846!-- See documentation for List of subgrid boxes
847!-- See pack_and_sort in lpm_pack_arrays.f90 for assignment of the subgrid boxes
848    block_offset(0) = block_offset_def ( 0, 0, 0)
849    block_offset(1) = block_offset_def ( 0, 0,-1)
850    block_offset(2) = block_offset_def ( 0,-1, 0)
851    block_offset(3) = block_offset_def ( 0,-1,-1)
852    block_offset(4) = block_offset_def (-1, 0, 0)
853    block_offset(5) = block_offset_def (-1, 0,-1)
854    block_offset(6) = block_offset_def (-1,-1, 0)
855    block_offset(7) = block_offset_def (-1,-1,-1)
856!
857!-- Check the number of particle groups.
858    IF ( number_of_particle_groups > max_number_of_particle_groups )  THEN
859       WRITE( message_string, * ) 'max_number_of_particle_groups =',           &
860                                  max_number_of_particle_groups ,              &
861                                  '&number_of_particle_groups reset to ',      &
862                                  max_number_of_particle_groups
863       CALL message( 'lpm_init', 'PA0213', 0, 1, 0, 6, 0 )
864       number_of_particle_groups = max_number_of_particle_groups
865    ENDIF
866!
867!-- Check if downward-facing walls exist. This case, reflection boundary
868!-- conditions (as well as subgrid-scale velocities) may do not work
869!-- propably (not realized so far).
870    IF ( surf_def_h(1)%ns >= 1 )  THEN
871       WRITE( message_string, * ) 'Overhanging topography do not work '//      &
872                                  'with particles'
873       CALL message( 'lpm_init', 'PA0212', 0, 1, 0, 6, 0 )
874
875    ENDIF
876
877!
878!-- Set default start positions, if necessary
879    IF ( psl(1) == 9999999.9_wp )  psl(1) = 0.0_wp
880    IF ( psr(1) == 9999999.9_wp )  psr(1) = ( nx +1 ) * dx
881    IF ( pss(1) == 9999999.9_wp )  pss(1) = 0.0_wp
882    IF ( psn(1) == 9999999.9_wp )  psn(1) = ( ny +1 ) * dy
883    IF ( psb(1) == 9999999.9_wp )  psb(1) = zu(nz/2)
884    IF ( pst(1) == 9999999.9_wp )  pst(1) = psb(1)
885
886    IF ( pdx(1) == 9999999.9_wp  .OR.  pdx(1) == 0.0_wp )  pdx(1) = dx
887    IF ( pdy(1) == 9999999.9_wp  .OR.  pdy(1) == 0.0_wp )  pdy(1) = dy
888    IF ( pdz(1) == 9999999.9_wp  .OR.  pdz(1) == 0.0_wp )  pdz(1) = zu(2) - zu(1)
889
890!
891!-- If number_particles_per_gridbox is set, the parametres pdx, pdy and pdz are
892!-- calculated diagnostically. Therfore an isotropic distribution is prescribed.
893    IF ( number_particles_per_gridbox /= -1 .AND.   &
894         number_particles_per_gridbox >= 1 )    THEN
895       pdx(1) = (( dx * dy * ( zu(2) - zu(1) ) ) /  &
896             REAL(number_particles_per_gridbox))**0.3333333_wp
897!
898!--    Ensure a smooth value (two significant digits) of distance between
899!--    particles (pdx, pdy, pdz).
900       div = 1000.0_wp
901       DO  WHILE ( pdx(1) < div )
902          div = div / 10.0_wp
903       ENDDO
904       pdx(1) = NINT( pdx(1) * 100.0_wp / div ) * div / 100.0_wp
905       pdy(1) = pdx(1)
906       pdz(1) = pdx(1)
907
908    ENDIF
909
910    DO  j = 2, number_of_particle_groups
911       IF ( psl(j) == 9999999.9_wp )  psl(j) = psl(j-1)
912       IF ( psr(j) == 9999999.9_wp )  psr(j) = psr(j-1)
913       IF ( pss(j) == 9999999.9_wp )  pss(j) = pss(j-1)
914       IF ( psn(j) == 9999999.9_wp )  psn(j) = psn(j-1)
915       IF ( psb(j) == 9999999.9_wp )  psb(j) = psb(j-1)
916       IF ( pst(j) == 9999999.9_wp )  pst(j) = pst(j-1)
917       IF ( pdx(j) == 9999999.9_wp  .OR.  pdx(j) == 0.0_wp )  pdx(j) = pdx(j-1)
918       IF ( pdy(j) == 9999999.9_wp  .OR.  pdy(j) == 0.0_wp )  pdy(j) = pdy(j-1)
919       IF ( pdz(j) == 9999999.9_wp  .OR.  pdz(j) == 0.0_wp )  pdz(j) = pdz(j-1)
920    ENDDO
921
922!
923!-- Allocate arrays required for calculating particle SGS velocities.
924!-- Initialize prefactor required for stoachastic Weil equation.
925    IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
926       ALLOCATE( de_dx(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
927                 de_dy(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
928                 de_dz(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
929
930       de_dx = 0.0_wp
931       de_dy = 0.0_wp
932       de_dz = 0.0_wp
933
934       sgs_wf_part = 1.0_wp / 3.0_wp
935    ENDIF
936
937!
938!-- Allocate array required for logarithmic vertical interpolation of
939!-- horizontal particle velocities between the surface and the first vertical
940!-- grid level. In order to avoid repeated CPU cost-intensive CALLS of
941!-- intrinsic FORTRAN procedure LOG(z/z0), LOG(z/z0) is precalculated for
942!-- several heights. Splitting into 20 sublayers turned out to be sufficient.
943!-- To obtain exact height levels of particles, linear interpolation is applied
944!-- (see lpm_advec.f90).
945    IF ( constant_flux_layer )  THEN
946
947       ALLOCATE ( log_z_z0(0:number_of_sublayers) )
948       z_p = zu(nzb+1) - zw(nzb)
949
950!
951!--    Calculate horizontal mean value of z0 used for logartihmic
952!--    interpolation. Note: this is not exact for heterogeneous z0.
953!--    However, sensitivity studies showed that the effect is
954!--    negligible.
955       z0_av_local  = SUM( surf_def_h(0)%z0 ) + SUM( surf_lsm_h%z0 ) +         &
956                      SUM( surf_usm_h%z0 )
957       z0_av_global = 0.0_wp
958
959#if defined( __parallel )
960       CALL MPI_ALLREDUCE(z0_av_local, z0_av_global, 1, MPI_REAL, MPI_SUM, &
961                          comm2d, ierr )
962#else
963       z0_av_global = z0_av_local
964#endif
965
966       z0_av_global = z0_av_global  / ( ( ny + 1 ) * ( nx + 1 ) )
967!
968!--    Horizontal wind speed is zero below and at z0
969       log_z_z0(0) = 0.0_wp
970!
971!--    Calculate vertical depth of the sublayers
972       height_int  = ( z_p - z0_av_global ) / REAL( number_of_sublayers, KIND=wp )
973!
974!--    Precalculate LOG(z/z0)
975       height_p    = z0_av_global
976       DO  k = 1, number_of_sublayers
977
978          height_p    = height_p + height_int
979          log_z_z0(k) = LOG( height_p / z0_av_global )
980
981       ENDDO
982
983    ENDIF
984
985!
986!-- Check which particle interpolation method should be used
987    IF ( TRIM( particle_advection_interpolation )  ==  'trilinear' )  THEN
988       interpolation_simple_corrector = .FALSE.
989       interpolation_simple_predictor = .FALSE.
990       interpolation_trilinear        = .TRUE.
991    ELSEIF ( TRIM( particle_advection_interpolation )  ==  'simple_corrector' )  THEN
992       interpolation_simple_corrector = .TRUE.
993       interpolation_simple_predictor = .FALSE.
994       interpolation_trilinear        = .FALSE.
995    ELSEIF ( TRIM( particle_advection_interpolation )  ==  'simple_predictor' )  THEN
996       interpolation_simple_corrector = .FALSE.
997       interpolation_simple_predictor = .TRUE.
998       interpolation_trilinear        = .FALSE.
999    ENDIF
1000
1001!
1002!-- Check boundary condition and set internal variables
1003    SELECT CASE ( bc_par_b )
1004
1005       CASE ( 'absorb' )
1006          ibc_par_b = 1
1007
1008       CASE ( 'reflect' )
1009          ibc_par_b = 2
1010
1011       CASE ( 'reset' )
1012          ibc_par_b = 3
1013
1014       CASE DEFAULT
1015          WRITE( message_string, * )  'unknown boundary condition ',           &
1016                                       'bc_par_b = "', TRIM( bc_par_b ), '"'
1017          CALL message( 'lpm_init', 'PA0217', 1, 2, 0, 6, 0 )
1018
1019    END SELECT
1020    SELECT CASE ( bc_par_t )
1021
1022       CASE ( 'absorb' )
1023          ibc_par_t = 1
1024
1025       CASE ( 'reflect' )
1026          ibc_par_t = 2
1027
1028       CASE ( 'nested' )
1029          ibc_par_t = 3
1030
1031       CASE DEFAULT
1032          WRITE( message_string, * ) 'unknown boundary condition ',            &
1033                                     'bc_par_t = "', TRIM( bc_par_t ), '"'
1034          CALL message( 'lpm_init', 'PA0218', 1, 2, 0, 6, 0 )
1035
1036    END SELECT
1037    SELECT CASE ( bc_par_lr )
1038
1039       CASE ( 'cyclic' )
1040          ibc_par_lr = 0
1041
1042       CASE ( 'absorb' )
1043          ibc_par_lr = 1
1044
1045       CASE ( 'reflect' )
1046          ibc_par_lr = 2
1047
1048       CASE ( 'nested' )
1049          ibc_par_lr = 3
1050
1051       CASE DEFAULT
1052          WRITE( message_string, * ) 'unknown boundary condition ',   &
1053                                     'bc_par_lr = "', TRIM( bc_par_lr ), '"'
1054          CALL message( 'lpm_init', 'PA0219', 1, 2, 0, 6, 0 )
1055
1056    END SELECT
1057    SELECT CASE ( bc_par_ns )
1058
1059       CASE ( 'cyclic' )
1060          ibc_par_ns = 0
1061
1062       CASE ( 'absorb' )
1063          ibc_par_ns = 1
1064
1065       CASE ( 'reflect' )
1066          ibc_par_ns = 2
1067
1068       CASE ( 'nested' )
1069          ibc_par_ns = 3
1070
1071       CASE DEFAULT
1072          WRITE( message_string, * ) 'unknown boundary condition ',   &
1073                                     'bc_par_ns = "', TRIM( bc_par_ns ), '"'
1074          CALL message( 'lpm_init', 'PA0220', 1, 2, 0, 6, 0 )
1075
1076    END SELECT
1077    SELECT CASE ( splitting_mode )
1078
1079       CASE ( 'const' )
1080          i_splitting_mode = 1
1081
1082       CASE ( 'cl_av' )
1083          i_splitting_mode = 2
1084
1085       CASE ( 'gb_av' )
1086          i_splitting_mode = 3
1087
1088       CASE DEFAULT
1089          WRITE( message_string, * )  'unknown splitting_mode = "',            &
1090                                      TRIM( splitting_mode ), '"'
1091          CALL message( 'lpm_init', 'PA0146', 1, 2, 0, 6, 0 )
1092
1093    END SELECT
1094    SELECT CASE ( splitting_function )
1095
1096       CASE ( 'gamma' )
1097          isf = 1
1098
1099       CASE ( 'log' )
1100          isf = 2
1101
1102       CASE ( 'exp' )
1103          isf = 3
1104
1105       CASE DEFAULT
1106          WRITE( message_string, * )  'unknown splitting function = "',        &
1107                                       TRIM( splitting_function ), '"'
1108          CALL message( 'lpm_init', 'PA0147', 1, 2, 0, 6, 0 )
1109
1110    END SELECT
1111!
1112!-- Initialize collision kernels
1113    IF ( collision_kernel /= 'none' )  CALL lpm_init_kernels
1114!
1115!-- For the first model run of a possible job chain initialize the
1116!-- particles, otherwise read the particle data from restart file.
1117    IF ( TRIM( initializing_actions ) == 'read_restart_data'  &
1118         .AND.  read_particles_from_restartfile )  THEN
1119       CALL lpm_rrd_local_particles
1120    ELSE
1121!
1122!--    Allocate particle arrays and set attributes of the initial set of
1123!--    particles, which can be also periodically released at later times.
1124       ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
1125                 grid_particles(nzb+1:nzt,nys:nyn,nxl:nxr) )
1126
1127       number_of_particles = 0
1128       prt_count           = 0
1129!
1130!--    initialize counter for particle IDs
1131       grid_particles%id_counter = 1
1132!
1133!--    Initialize all particles with dummy values (otherwise errors may
1134!--    occur within restart runs). The reason for this is still not clear
1135!--    and may be presumably caused by errors in the respective user-interface.
1136       zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
1137                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
1138                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
1139                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
1140                                      0, 0, 0_idp, .FALSE., -1 )
1141
1142       particle_groups = particle_groups_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp )
1143!
1144!--    Set values for the density ratio and radius for all particle
1145!--    groups, if necessary
1146       IF ( density_ratio(1) == 9999999.9_wp )  density_ratio(1) = 0.0_wp
1147       IF ( radius(1)        == 9999999.9_wp )  radius(1) = 0.0_wp
1148       DO  i = 2, number_of_particle_groups
1149          IF ( density_ratio(i) == 9999999.9_wp )  THEN
1150             density_ratio(i) = density_ratio(i-1)
1151          ENDIF
1152          IF ( radius(i) == 9999999.9_wp )  radius(i) = radius(i-1)
1153       ENDDO
1154
1155       DO  i = 1, number_of_particle_groups
1156          IF ( density_ratio(i) /= 0.0_wp  .AND.  radius(i) == 0 )  THEN
1157             WRITE( message_string, * ) 'particle group #', i, ' has a',       &
1158                                        'density ratio /= 0 but radius = 0'
1159             CALL message( 'lpm_init', 'PA0215', 1, 2, 0, 6, 0 )
1160          ENDIF
1161          particle_groups(i)%density_ratio = density_ratio(i)
1162          particle_groups(i)%radius        = radius(i)
1163       ENDDO
1164!
1165!--    Set a seed value for the random number generator to be exclusively
1166!--    used for the particle code. The generated random numbers should be
1167!--    different on the different PEs.
1168       iran_part = iran_part + myid
1169!
1170!--    Create the particle set, and set the initial particles
1171       CALL lpm_create_particle( phase_init )
1172       last_particle_release_time = particle_advection_start
1173!
1174!--    User modification of initial particles
1175       CALL user_lpm_init
1176!
1177!--    Open file for statistical informations about particle conditions
1178       IF ( write_particle_statistics )  THEN
1179          CALL check_open( 80 )
1180          WRITE ( 80, 8000 )  current_timestep_number, simulated_time,         &
1181                              number_of_particles
1182          CALL close_file( 80 )
1183       ENDIF
1184
1185    ENDIF
1186
1187    IF ( nested_run )  CALL pmcp_g_init
1188!
1189!-- To avoid programm abort, assign particles array to the local version of
1190!-- first grid cell
1191    number_of_particles = prt_count(nzb+1,nys,nxl)
1192    particles => grid_particles(nzb+1,nys,nxl)%particles(1:number_of_particles)
1193!
1194!-- Formats
11958000 FORMAT (I6,1X,F7.2,4X,I10,71X,I10)
1196
1197 END SUBROUTINE lpm_init
1198 
1199!------------------------------------------------------------------------------!
1200! Description:
1201! ------------
1202!> Create Lagrangian particles
1203!------------------------------------------------------------------------------! 
1204 SUBROUTINE lpm_create_particle (phase)
1205
1206    INTEGER(iwp)               ::  alloc_size  !< relative increase of allocated memory for particles
1207    INTEGER(iwp)               ::  i           !< loop variable ( particle groups )
1208    INTEGER(iwp)               ::  ip          !< index variable along x
1209    INTEGER(iwp)               ::  j           !< loop variable ( particles per point )
1210    INTEGER(iwp)               ::  jp          !< index variable along y
1211    INTEGER(iwp)               ::  k           !< index variable along z
1212    INTEGER(iwp)               ::  k_surf      !< index of surface grid point
1213    INTEGER(iwp)               ::  kp          !< index variable along z
1214    INTEGER(iwp)               ::  loop_stride !< loop variable for initialization
1215    INTEGER(iwp)               ::  n           !< loop variable ( number of particles )
1216    INTEGER(iwp)               ::  new_size    !< new size of allocated memory for particles
1217
1218    INTEGER(iwp), INTENT(IN)   ::  phase       !< mode of inititialization
1219
1220    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_count !< start address of new particle
1221    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_start !< start address of new particle
1222
1223    LOGICAL                    ::  first_stride !< flag for initialization
1224
1225    REAL(wp)                   ::  pos_x      !< increment for particle position in x
1226    REAL(wp)                   ::  pos_y      !< increment for particle position in y
1227    REAL(wp)                   ::  pos_z      !< increment for particle position in z
1228    REAL(wp)                   ::  rand_contr !< dummy argument for random position
1229
1230    TYPE(particle_type),TARGET ::  tmp_particle !< temporary particle used for initialization
1231
1232
1233!
1234!-- Calculate particle positions and store particle attributes, if
1235!-- particle is situated on this PE
1236    DO  loop_stride = 1, 2
1237       first_stride = (loop_stride == 1)
1238       IF ( first_stride )   THEN
1239          local_count = 0           ! count number of particles
1240       ELSE
1241          local_count = prt_count   ! Start address of new particles
1242       ENDIF
1243
1244!
1245!--    Calculate initial_weighting_factor diagnostically
1246       IF ( number_concentration /= -1.0_wp  .AND.  number_concentration > 0.0_wp )  THEN
1247          initial_weighting_factor =  number_concentration  *                           &
1248                                      pdx(1) * pdy(1) * pdz(1)
1249       END IF
1250
1251       n = 0
1252       DO  i = 1, number_of_particle_groups
1253          pos_z = psb(i)
1254          DO WHILE ( pos_z <= pst(i) )
1255             IF ( pos_z >= zw(0) .AND.  pos_z < zw(nzt) )  THEN
1256                pos_y = pss(i)
1257                DO WHILE ( pos_y <= psn(i) )
1258                   IF ( pos_y >= nys * dy  .AND.                  &
1259                        pos_y <  ( nyn + 1 ) * dy  )  THEN
1260                      pos_x = psl(i)
1261               xloop: DO WHILE ( pos_x <= psr(i) )
1262                         IF ( pos_x >= nxl * dx  .AND.            &
1263                              pos_x <  ( nxr + 1) * dx )  THEN
1264                            DO  j = 1, particles_per_point
1265                               n = n + 1
1266                               tmp_particle%x             = pos_x
1267                               tmp_particle%y             = pos_y
1268                               tmp_particle%z             = pos_z
1269                               tmp_particle%age           = 0.0_wp
1270                               tmp_particle%age_m         = 0.0_wp
1271                               tmp_particle%dt_sum        = 0.0_wp
1272                               tmp_particle%e_m           = 0.0_wp
1273                               tmp_particle%rvar1         = 0.0_wp
1274                               tmp_particle%rvar2         = 0.0_wp
1275                               tmp_particle%rvar3         = 0.0_wp
1276                               tmp_particle%speed_x       = 0.0_wp
1277                               tmp_particle%speed_y       = 0.0_wp
1278                               tmp_particle%speed_z       = 0.0_wp
1279                               tmp_particle%origin_x      = pos_x
1280                               tmp_particle%origin_y      = pos_y
1281                               tmp_particle%origin_z      = pos_z
1282                               IF ( curvature_solution_effects )  THEN
1283                                  tmp_particle%aux1      = 0.0_wp    ! dry aerosol radius
1284                                  tmp_particle%aux2      = dt_3d     ! last Rosenbrock timestep
1285                               ELSE
1286                                  tmp_particle%aux1      = 0.0_wp    ! free to use
1287                                  tmp_particle%aux2      = 0.0_wp    ! free to use
1288                               ENDIF
1289                               tmp_particle%radius        = particle_groups(i)%radius
1290                               tmp_particle%weight_factor = initial_weighting_factor
1291                               tmp_particle%class         = 1
1292                               tmp_particle%group         = i
1293                               tmp_particle%id            = 0_idp
1294                               tmp_particle%particle_mask = .TRUE.
1295                               tmp_particle%block_nr      = -1
1296!
1297!--                            Determine the grid indices of the particle position
1298                               ip = INT( tmp_particle%x * ddx )
1299                               jp = INT( tmp_particle%y * ddy )
1300!
1301!--                            In case of stretching the actual k index is found iteratively
1302                               IF ( dz_stretch_level /= -9999999.9_wp  .OR.           &
1303                                    dz_stretch_level_start(1) /= -9999999.9_wp )  THEN
1304                                  kp = MINLOC( ABS( tmp_particle%z - zu ), DIM = 1 ) - 1
1305                               ELSE
1306                                  kp = INT( tmp_particle%z / dz(1) + 1 + offset_ocean_nzt )
1307                               ENDIF
1308!
1309!--                            Determine surface level. Therefore, check for
1310!--                            upward-facing wall on w-grid.
1311                               k_surf = topo_top_ind(jp,ip,3)
1312                               IF ( seed_follows_topography )  THEN
1313!
1314!--                               Particle height is given relative to topography
1315                                  kp = kp + k_surf
1316                                  tmp_particle%z = tmp_particle%z + zw(k_surf)
1317!--                               Skip particle release if particle position is
1318!--                               above model top, or within topography in case
1319!--                               of overhanging structures.
1320                                  IF ( kp > nzt  .OR.                          &
1321                                 .NOT. BTEST( wall_flags_0(kp,jp,ip), 0 ) )  THEN
1322                                     pos_x = pos_x + pdx(i)
1323                                     CYCLE xloop
1324                                  ENDIF
1325!
1326!--                            Skip particle release if particle position is
1327!--                            below surface, or within topography in case
1328!--                            of overhanging structures.
1329                               ELSEIF ( .NOT. seed_follows_topography .AND.    &
1330                                         tmp_particle%z <= zw(k_surf)  .OR.    &
1331                                        .NOT. BTEST( wall_flags_0(kp,jp,ip), 0 ) )&
1332                               THEN
1333                                  pos_x = pos_x + pdx(i)
1334                                  CYCLE xloop
1335                               ENDIF
1336
1337                               local_count(kp,jp,ip) = local_count(kp,jp,ip) + 1
1338
1339                               IF ( .NOT. first_stride )  THEN
1340                                  IF ( ip < nxl  .OR.  jp < nys  .OR.  kp < nzb+1 )  THEN
1341                                     write(6,*) 'xl ',ip,jp,kp,nxl,nys,nzb+1
1342                                  ENDIF
1343                                  IF ( ip > nxr  .OR.  jp > nyn  .OR.  kp > nzt )  THEN
1344                                     write(6,*) 'xu ',ip,jp,kp,nxr,nyn,nzt
1345                                  ENDIF
1346                                  grid_particles(kp,jp,ip)%particles(local_count(kp,jp,ip)) = tmp_particle
1347                               ENDIF
1348                            ENDDO
1349                         ENDIF
1350                         pos_x = pos_x + pdx(i)
1351                      ENDDO xloop
1352                   ENDIF
1353                   pos_y = pos_y + pdy(i)
1354                ENDDO
1355             ENDIF
1356
1357             pos_z = pos_z + pdz(i)
1358          ENDDO
1359       ENDDO
1360
1361       IF ( first_stride )  THEN
1362          DO  ip = nxl, nxr
1363             DO  jp = nys, nyn
1364                DO  kp = nzb+1, nzt
1365                   IF ( phase == PHASE_INIT )  THEN
1366                      IF ( local_count(kp,jp,ip) > 0 )  THEN
1367                         alloc_size = MAX( INT( local_count(kp,jp,ip) *        &
1368                            ( 1.0_wp + alloc_factor / 100.0_wp ) ),            &
1369                            1 )
1370                      ELSE
1371                         alloc_size = 1
1372                      ENDIF
1373                      ALLOCATE(grid_particles(kp,jp,ip)%particles(1:alloc_size))
1374                      DO  n = 1, alloc_size
1375                         grid_particles(kp,jp,ip)%particles(n) = zero_particle
1376                      ENDDO
1377                   ELSEIF ( phase == PHASE_RELEASE )  THEN
1378                      IF ( local_count(kp,jp,ip) > 0 )  THEN
1379                         new_size   = local_count(kp,jp,ip) + prt_count(kp,jp,ip)
1380                         alloc_size = MAX( INT( new_size * ( 1.0_wp +          &
1381                            alloc_factor / 100.0_wp ) ), 1 )
1382                         IF( alloc_size > SIZE( grid_particles(kp,jp,ip)%particles) )  THEN
1383                            CALL realloc_particles_array( ip, jp, kp, alloc_size )
1384                         ENDIF
1385                      ENDIF
1386                   ENDIF
1387                ENDDO
1388             ENDDO
1389          ENDDO
1390       ENDIF
1391
1392    ENDDO
1393
1394    local_start = prt_count+1
1395    prt_count   = local_count
1396!
1397!-- Calculate particle IDs
1398    DO  ip = nxl, nxr
1399       DO  jp = nys, nyn
1400          DO  kp = nzb+1, nzt
1401             number_of_particles = prt_count(kp,jp,ip)
1402             IF ( number_of_particles <= 0 )  CYCLE
1403             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
1404
1405             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
1406
1407                particles(n)%id = 10000_idp**3 * grid_particles(kp,jp,ip)%id_counter + &
1408                                  10000_idp**2 * kp + 10000_idp * jp + ip
1409!
1410!--             Count the number of particles that have been released before
1411                grid_particles(kp,jp,ip)%id_counter =                          &
1412                                         grid_particles(kp,jp,ip)%id_counter + 1
1413
1414             ENDDO
1415
1416          ENDDO
1417       ENDDO
1418    ENDDO
1419!
1420!-- Initialize aerosol background spectrum
1421    IF ( curvature_solution_effects )  THEN
1422       CALL lpm_init_aerosols( local_start )
1423    ENDIF
1424!
1425!-- Add random fluctuation to particle positions.
1426    IF ( random_start_position )  THEN
1427       DO  ip = nxl, nxr
1428          DO  jp = nys, nyn
1429             DO  kp = nzb+1, nzt
1430                number_of_particles = prt_count(kp,jp,ip)
1431                IF ( number_of_particles <= 0 )  CYCLE
1432                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
1433!
1434!--             Move only new particles. Moreover, limit random fluctuation
1435!--             in order to prevent that particles move more than one grid box,
1436!--             which would lead to problems concerning particle exchange
1437!--             between processors in case pdx/pdy are larger than dx/dy,
1438!--             respectively.
1439                DO  n = local_start(kp,jp,ip), number_of_particles
1440                   IF ( psl(particles(n)%group) /= psr(particles(n)%group) )  THEN
1441                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
1442                                     pdx(particles(n)%group)
1443                      particles(n)%x = particles(n)%x +                        &
1444                              MERGE( rand_contr, SIGN( dx, rand_contr ),       &
1445                                     ABS( rand_contr ) < dx                    &
1446                                   )
1447                   ENDIF
1448                   IF ( pss(particles(n)%group) /= psn(particles(n)%group) )  THEN
1449                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
1450                                     pdy(particles(n)%group)
1451                      particles(n)%y = particles(n)%y +                        &
1452                              MERGE( rand_contr, SIGN( dy, rand_contr ),       &
1453                                     ABS( rand_contr ) < dy                    &
1454                                   )
1455                   ENDIF
1456                   IF ( psb(particles(n)%group) /= pst(particles(n)%group) )  THEN
1457                      rand_contr = ( random_function( iran_part ) - 0.5_wp ) * &
1458                                     pdz(particles(n)%group)
1459                      particles(n)%z = particles(n)%z +                        &
1460                              MERGE( rand_contr, SIGN( dzw(kp), rand_contr ),  &
1461                                     ABS( rand_contr ) < dzw(kp)               &
1462                                   )
1463                   ENDIF
1464                ENDDO
1465!
1466!--             Identify particles located outside the model domain and reflect
1467!--             or absorb them if necessary.
1468                CALL lpm_boundary_conds( 'bottom/top', i, j, k )
1469!
1470!--             Furthermore, remove particles located in topography. Note, as
1471!--             the particle speed is still zero at this point, wall
1472!--             reflection boundary conditions will not work in this case.
1473                particles =>                                                   &
1474                       grid_particles(kp,jp,ip)%particles(1:number_of_particles)
1475                DO  n = local_start(kp,jp,ip), number_of_particles
1476                   i = particles(n)%x * ddx
1477                   j = particles(n)%y * ddy
1478                   k = particles(n)%z / dz(1) + 1 + offset_ocean_nzt
1479                   DO WHILE( zw(k) < particles(n)%z )
1480                      k = k + 1
1481                   ENDDO
1482                   DO WHILE( zw(k-1) > particles(n)%z )
1483                      k = k - 1
1484                   ENDDO
1485!
1486!--                Check if particle is within topography
1487                   IF ( .NOT. BTEST( wall_flags_0(k,j,i), 0 ) )  THEN
1488                      particles(n)%particle_mask = .FALSE.
1489                      deleted_particles = deleted_particles + 1
1490                   ENDIF
1491
1492                ENDDO
1493             ENDDO
1494          ENDDO
1495       ENDDO
1496!
1497!--    Exchange particles between grid cells and processors
1498       CALL lpm_move_particle
1499       CALL lpm_exchange_horiz
1500
1501    ENDIF
1502!
1503!-- In case of random_start_position, delete particles identified by
1504!-- lpm_exchange_horiz and lpm_boundary_conds. Then sort particles into blocks,
1505!-- which is needed for a fast interpolation of the LES fields on the particle
1506!-- position.
1507    CALL lpm_sort_and_delete
1508!
1509!-- Determine the current number of particles
1510    DO  ip = nxl, nxr
1511       DO  jp = nys, nyn
1512          DO  kp = nzb+1, nzt
1513             number_of_particles         = number_of_particles                 &
1514                                           + prt_count(kp,jp,ip)
1515          ENDDO
1516       ENDDO
1517    ENDDO
1518!
1519!-- Calculate the number of particles of the total domain
1520#if defined( __parallel )
1521    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1522    CALL MPI_ALLREDUCE( number_of_particles, total_number_of_particles, 1, &
1523    MPI_INTEGER, MPI_SUM, comm2d, ierr )
1524#else
1525    total_number_of_particles = number_of_particles
1526#endif
1527
1528    RETURN
1529
1530 END SUBROUTINE lpm_create_particle
1531 
1532 
1533!------------------------------------------------------------------------------!
1534! Description:
1535! ------------
1536!> This routine initialize the particles as aerosols with physio-chemical
1537!> properties.
1538!------------------------------------------------------------------------------!   
1539 SUBROUTINE lpm_init_aerosols(local_start)
1540
1541    REAL(wp) ::  afactor            !< curvature effects
1542    REAL(wp) ::  bfactor            !< solute effects
1543    REAL(wp) ::  dlogr              !< logarithmic width of radius bin
1544    REAL(wp) ::  e_a                !< vapor pressure
1545    REAL(wp) ::  e_s                !< saturation vapor pressure
1546    REAL(wp) ::  rmin = 0.005e-6_wp !< minimum aerosol radius
1547    REAL(wp) ::  rmax = 10.0e-6_wp  !< maximum aerosol radius
1548    REAL(wp) ::  r_mid              !< mean radius of bin
1549    REAL(wp) ::  r_l                !< left radius of bin
1550    REAL(wp) ::  r_r                !< right radius of bin
1551    REAL(wp) ::  sigma              !< surface tension
1552    REAL(wp) ::  t_int              !< temperature
1553
1554    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg), INTENT(IN) ::  local_start !<
1555
1556    INTEGER(iwp) ::  n              !<
1557    INTEGER(iwp) ::  ip             !<
1558    INTEGER(iwp) ::  jp             !<
1559    INTEGER(iwp) ::  kp             !<
1560
1561!
1562!-- Set constants for different aerosol species
1563    IF ( TRIM( aero_species ) == 'nacl' )  THEN
1564       molecular_weight_of_solute = 0.05844_wp 
1565       rho_s                      = 2165.0_wp
1566       vanthoff                   = 2.0_wp
1567    ELSEIF ( TRIM( aero_species ) == 'c3h4o4' )  THEN
1568       molecular_weight_of_solute = 0.10406_wp 
1569       rho_s                      = 1600.0_wp
1570       vanthoff                   = 1.37_wp
1571    ELSEIF ( TRIM( aero_species ) == 'nh4o3' )  THEN
1572       molecular_weight_of_solute = 0.08004_wp 
1573       rho_s                      = 1720.0_wp
1574       vanthoff                   = 2.31_wp
1575    ELSE
1576       WRITE( message_string, * ) 'unknown aerosol species ',   &
1577                                'aero_species = "', TRIM( aero_species ), '"'
1578       CALL message( 'lpm_init', 'PA0470', 1, 2, 0, 6, 0 )
1579    ENDIF
1580!
1581!-- The following typical aerosol spectra are taken from Jaenicke (1993):
1582!-- Tropospheric aerosols. Published in Aerosol-Cloud-Climate Interactions.
1583    IF ( TRIM( aero_type ) == 'polar' )  THEN
1584       na        = (/ 2.17e1, 1.86e-1, 3.04e-4 /) * 1.0E6_wp
1585       rm        = (/ 0.0689, 0.375, 4.29 /) * 1.0E-6_wp
1586       log_sigma = (/ 0.245, 0.300, 0.291 /)
1587    ELSEIF ( TRIM( aero_type ) == 'background' )  THEN
1588       na        = (/ 1.29e2, 5.97e1, 6.35e1 /) * 1.0E6_wp
1589       rm        = (/ 0.0036, 0.127, 0.259 /) * 1.0E-6_wp
1590       log_sigma = (/ 0.645, 0.253, 0.425 /)
1591    ELSEIF ( TRIM( aero_type ) == 'maritime' )  THEN
1592       na        = (/ 1.33e2, 6.66e1, 3.06e0 /) * 1.0E6_wp
1593       rm        = (/ 0.0039, 0.133, 0.29 /) * 1.0E-6_wp
1594       log_sigma = (/ 0.657, 0.210, 0.396 /)
1595    ELSEIF ( TRIM( aero_type ) == 'continental' )  THEN
1596       na        = (/ 3.20e3, 2.90e3, 3.00e-1 /) * 1.0E6_wp
1597       rm        = (/ 0.01, 0.058, 0.9 /) * 1.0E-6_wp
1598       log_sigma = (/ 0.161, 0.217, 0.380 /)
1599    ELSEIF ( TRIM( aero_type ) == 'desert' )  THEN
1600       na        = (/ 7.26e2, 1.14e3, 1.78e-1 /) * 1.0E6_wp
1601       rm        = (/ 0.001, 0.0188, 10.8 /) * 1.0E-6_wp
1602       log_sigma = (/ 0.247, 0.770, 0.438 /)
1603    ELSEIF ( TRIM( aero_type ) == 'rural' )  THEN
1604       na        = (/ 6.65e3, 1.47e2, 1.99e3 /) * 1.0E6_wp
1605       rm        = (/ 0.00739, 0.0269, 0.0419 /) * 1.0E-6_wp
1606       log_sigma = (/ 0.225, 0.557, 0.266 /)
1607    ELSEIF ( TRIM( aero_type ) == 'urban' )  THEN
1608       na        = (/ 9.93e4, 1.11e3, 3.64e4 /) * 1.0E6_wp
1609       rm        = (/ 0.00651, 0.00714, 0.0248 /) * 1.0E-6_wp
1610       log_sigma = (/ 0.245, 0.666, 0.337 /)
1611    ELSEIF ( TRIM( aero_type ) == 'user' )  THEN
1612       CONTINUE
1613    ELSE
1614       WRITE( message_string, * ) 'unknown aerosol type ',   &
1615                                'aero_type = "', TRIM( aero_type ), '"'
1616       CALL message( 'lpm_init', 'PA0459', 1, 2, 0, 6, 0 )
1617    ENDIF
1618
1619    DO  ip = nxl, nxr
1620       DO  jp = nys, nyn
1621          DO  kp = nzb+1, nzt
1622
1623             number_of_particles = prt_count(kp,jp,ip)
1624             IF ( number_of_particles <= 0 )  CYCLE
1625             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
1626
1627             dlogr   = ( LOG10(rmax) - LOG10(rmin) ) / ( number_of_particles - local_start(kp,jp,ip) + 1 )
1628!
1629!--          Initialize the aerosols with a predefined spectral distribution
1630!--          of the dry radius (logarithmically increasing bins) and a varying
1631!--          weighting factor
1632             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
1633
1634                r_l   = 10.0**( LOG10( rmin ) + (n-1) * dlogr )
1635                r_r   = 10.0**( LOG10( rmin ) + n * dlogr )
1636                r_mid = SQRT( r_l * r_r )
1637
1638                particles(n)%aux1          = r_mid
1639                particles(n)%weight_factor =                                           &
1640                   ( na(1) / ( SQRT( 2.0_wp * pi ) * log_sigma(1) ) *                     &
1641                     EXP( - LOG10( r_mid / rm(1) )**2 / ( 2.0_wp * log_sigma(1)**2 ) ) +  &
1642                     na(2) / ( SQRT( 2.0_wp * pi ) * log_sigma(2) ) *                     &
1643                     EXP( - LOG10( r_mid / rm(2) )**2 / ( 2.0_wp * log_sigma(2)**2 ) ) +  &
1644                     na(3) / ( SQRT( 2.0_wp * pi ) * log_sigma(3) ) *                     &
1645                     EXP( - LOG10( r_mid / rm(3) )**2 / ( 2.0_wp * log_sigma(3)**2 ) )    &
1646                   ) * ( LOG10(r_r) - LOG10(r_l) ) * ( dx * dy * dzw(kp) )
1647
1648!
1649!--             Multiply weight_factor with the namelist parameter aero_weight
1650!--             to increase or decrease the number of simulated aerosols
1651                particles(n)%weight_factor = particles(n)%weight_factor * aero_weight
1652
1653                IF ( particles(n)%weight_factor - FLOOR(particles(n)%weight_factor,KIND=wp) &
1654                     > random_function( iran_part ) )  THEN
1655                   particles(n)%weight_factor = FLOOR(particles(n)%weight_factor,KIND=wp) + 1.0_wp
1656                ELSE
1657                   particles(n)%weight_factor = FLOOR(particles(n)%weight_factor,KIND=wp)
1658                ENDIF
1659!
1660!--             Unnecessary particles will be deleted
1661                IF ( particles(n)%weight_factor <= 0.0_wp )  particles(n)%particle_mask = .FALSE.
1662
1663             ENDDO
1664!
1665!--          Set particle radius to equilibrium radius based on the environmental
1666!--          supersaturation (Khvorostyanov and Curry, 2007, JGR). This avoids
1667!--          the sometimes lengthy growth toward their equilibrium radius within
1668!--          the simulation.
1669             t_int  = pt(kp,jp,ip) * exner(kp)
1670
1671             e_s = magnus( t_int )
1672             e_a = q(kp,jp,ip) * hyp(kp) / ( q(kp,jp,ip) + rd_d_rv )
1673
1674             sigma   = 0.0761_wp - 0.000155_wp * ( t_int - 273.15_wp )
1675             afactor = 2.0_wp * sigma / ( rho_l * r_v * t_int )
1676
1677             bfactor = vanthoff * molecular_weight_of_water *    &
1678                       rho_s / ( molecular_weight_of_solute * rho_l )
1679!
1680!--          The formula is only valid for subsaturated environments. For
1681!--          supersaturations higher than -5 %, the supersaturation is set to -5%.
1682             IF ( e_a / e_s >= 0.95_wp )  e_a = 0.95_wp * e_s
1683
1684             DO  n = local_start(kp,jp,ip), number_of_particles  !only new particles
1685!
1686!--             For details on this equation, see Eq. (14) of Khvorostyanov and
1687!--             Curry (2007, JGR)
1688                particles(n)%radius = bfactor**0.3333333_wp *                  &
1689                   particles(n)%aux1 / ( 1.0_wp - e_a / e_s )**0.3333333_wp / &
1690                   ( 1.0_wp + ( afactor / ( 3.0_wp * bfactor**0.3333333_wp *   &
1691                     particles(n)%aux1 ) ) /                                  &
1692                     ( 1.0_wp - e_a / e_s )**0.6666666_wp                      &
1693                   )
1694
1695             ENDDO
1696
1697          ENDDO
1698       ENDDO
1699    ENDDO
1700
1701 END SUBROUTINE lpm_init_aerosols
1702
1703
1704!------------------------------------------------------------------------------!
1705! Description:
1706! ------------
1707!> Calculates quantities required for considering the SGS velocity fluctuations
1708!> in the particle transport by a stochastic approach. The respective
1709!> quantities are: SGS-TKE gradients and horizontally averaged profiles of the
1710!> SGS TKE and the resolved-scale velocity variances.
1711!------------------------------------------------------------------------------!
1712 SUBROUTINE lpm_init_sgs_tke
1713
1714    USE statistics,                                                            &
1715        ONLY:  flow_statistics_called, hom, sums, sums_l
1716
1717    INTEGER(iwp) ::  i      !< index variable along x
1718    INTEGER(iwp) ::  j      !< index variable along y
1719    INTEGER(iwp) ::  k      !< index variable along z
1720    INTEGER(iwp) ::  m      !< running index for the surface elements
1721
1722    REAL(wp) ::  flag1      !< flag to mask topography
1723
1724!
1725!-- TKE gradient along x and y
1726    DO  i = nxl, nxr
1727       DO  j = nys, nyn
1728          DO  k = nzb, nzt+1
1729
1730             IF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.               &
1731                        BTEST( wall_flags_0(k,j,i), 0   )  .AND.               &
1732                        BTEST( wall_flags_0(k,j,i+1), 0 ) )                    &
1733             THEN
1734                de_dx(k,j,i) = 2.0_wp * sgs_wf_part *                          &
1735                               ( e(k,j,i+1) - e(k,j,i) ) * ddx
1736             ELSEIF ( BTEST( wall_flags_0(k,j,i-1), 0 )  .AND.                 &
1737                      BTEST( wall_flags_0(k,j,i), 0   )  .AND.                 &
1738                .NOT. BTEST( wall_flags_0(k,j,i+1), 0 ) )                      &
1739             THEN
1740                de_dx(k,j,i) = 2.0_wp * sgs_wf_part *                          &
1741                               ( e(k,j,i) - e(k,j,i-1) ) * ddx
1742             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i), 22   )  .AND.          &
1743                      .NOT. BTEST( wall_flags_0(k,j,i+1), 22 ) )               &   
1744             THEN
1745                de_dx(k,j,i) = 0.0_wp
1746             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i-1), 22 )  .AND.          &
1747                      .NOT. BTEST( wall_flags_0(k,j,i), 22   ) )               &
1748             THEN
1749                de_dx(k,j,i) = 0.0_wp
1750             ELSE
1751                de_dx(k,j,i) = sgs_wf_part * ( e(k,j,i+1) - e(k,j,i-1) ) * ddx
1752             ENDIF
1753
1754             IF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.               &
1755                        BTEST( wall_flags_0(k,j,i), 0   )  .AND.               &
1756                        BTEST( wall_flags_0(k,j+1,i), 0 ) )                    &
1757             THEN
1758                de_dy(k,j,i) = 2.0_wp * sgs_wf_part *                          &
1759                               ( e(k,j+1,i) - e(k,j,i) ) * ddy
1760             ELSEIF ( BTEST( wall_flags_0(k,j-1,i), 0 )  .AND.                 &
1761                      BTEST( wall_flags_0(k,j,i), 0   )  .AND.                 &
1762                .NOT. BTEST( wall_flags_0(k,j+1,i), 0 ) )                      &
1763             THEN
1764                de_dy(k,j,i) = 2.0_wp * sgs_wf_part *                          &
1765                               ( e(k,j,i) - e(k,j-1,i) ) * ddy
1766             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j,i), 22   )  .AND.          &
1767                      .NOT. BTEST( wall_flags_0(k,j+1,i), 22 ) )               &   
1768             THEN
1769                de_dy(k,j,i) = 0.0_wp
1770             ELSEIF ( .NOT. BTEST( wall_flags_0(k,j-1,i), 22 )  .AND.          &
1771                      .NOT. BTEST( wall_flags_0(k,j,i), 22   ) )               &
1772             THEN
1773                de_dy(k,j,i) = 0.0_wp
1774             ELSE
1775                de_dy(k,j,i) = sgs_wf_part * ( e(k,j+1,i) - e(k,j-1,i) ) * ddy
1776             ENDIF
1777
1778          ENDDO
1779       ENDDO
1780    ENDDO
1781
1782!
1783!-- TKE gradient along z at topograhy and  including bottom and top boundary conditions
1784    DO  i = nxl, nxr
1785       DO  j = nys, nyn
1786          DO  k = nzb+1, nzt-1
1787!
1788!--          Flag to mask topography
1789             flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 0  ) )
1790
1791             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
1792                           ( e(k+1,j,i) - e(k-1,j,i) ) / ( zu(k+1) - zu(k-1) ) &
1793                                                 * flag1 
1794          ENDDO
1795!
1796!--       upward-facing surfaces
1797          DO  m = bc_h(0)%start_index(j,i), bc_h(0)%end_index(j,i)
1798             k            = bc_h(0)%k(m)
1799             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
1800                           ( e(k+1,j,i) - e(k,j,i)   ) / ( zu(k+1) - zu(k) )
1801          ENDDO
1802!
1803!--       downward-facing surfaces
1804          DO  m = bc_h(1)%start_index(j,i), bc_h(1)%end_index(j,i)
1805             k            = bc_h(1)%k(m)
1806             de_dz(k,j,i) = 2.0_wp * sgs_wf_part *                             &
1807                           ( e(k,j,i) - e(k-1,j,i)   ) / ( zu(k) - zu(k-1) )
1808          ENDDO
1809
1810          de_dz(nzb,j,i)   = 0.0_wp
1811          de_dz(nzt,j,i)   = 0.0_wp
1812          de_dz(nzt+1,j,i) = 0.0_wp
1813       ENDDO
1814    ENDDO
1815!
1816!-- Ghost point exchange
1817    CALL exchange_horiz( de_dx, nbgp )
1818    CALL exchange_horiz( de_dy, nbgp )
1819    CALL exchange_horiz( de_dz, nbgp )
1820    CALL exchange_horiz( diss, nbgp  )
1821!
1822!-- Set boundary conditions at non-periodic boundaries. Note, at non-period
1823!-- boundaries zero-gradient boundary conditions are set for the subgrid TKE.
1824!-- Thus, TKE gradients normal to the respective lateral boundaries are zero,
1825!-- while tangetial TKE gradients then must be the same as within the prognostic
1826!-- domain. 
1827    IF ( bc_dirichlet_l )  THEN
1828       de_dx(:,:,-1) = 0.0_wp
1829       de_dy(:,:,-1) = de_dy(:,:,0) 
1830       de_dz(:,:,-1) = de_dz(:,:,0)
1831    ENDIF
1832    IF ( bc_dirichlet_r )  THEN
1833       de_dx(:,:,nxr+1) = 0.0_wp
1834       de_dy(:,:,nxr+1) = de_dy(:,:,nxr) 
1835       de_dz(:,:,nxr+1) = de_dz(:,:,nxr)
1836    ENDIF
1837    IF ( bc_dirichlet_n )  THEN
1838       de_dx(:,nyn+1,:) = de_dx(:,nyn,:)
1839       de_dy(:,nyn+1,:) = 0.0_wp 
1840       de_dz(:,nyn+1,:) = de_dz(:,nyn,:)
1841    ENDIF
1842    IF ( bc_dirichlet_s )  THEN
1843       de_dx(:,nys-1,:) = de_dx(:,nys,:)
1844       de_dy(:,nys-1,:) = 0.0_wp 
1845       de_dz(:,nys-1,:) = de_dz(:,nys,:)
1846    ENDIF 
1847!
1848!-- Calculate the horizontally averaged profiles of SGS TKE and resolved
1849!-- velocity variances (they may have been already calculated in routine
1850!-- flow_statistics).
1851    IF ( .NOT. flow_statistics_called )  THEN
1852
1853!
1854!--    First calculate horizontally averaged profiles of the horizontal
1855!--    velocities.
1856       sums_l(:,1,0) = 0.0_wp
1857       sums_l(:,2,0) = 0.0_wp
1858
1859       DO  i = nxl, nxr
1860          DO  j =  nys, nyn
1861             DO  k = nzb, nzt+1
1862!
1863!--             Flag indicating vicinity of wall
1864                flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 24 ) )
1865
1866                sums_l(k,1,0)  = sums_l(k,1,0)  + u(k,j,i) * flag1
1867                sums_l(k,2,0)  = sums_l(k,2,0)  + v(k,j,i) * flag1
1868             ENDDO
1869          ENDDO
1870       ENDDO
1871
1872#if defined( __parallel )
1873!
1874!--    Compute total sum from local sums
1875       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1876       CALL MPI_ALLREDUCE( sums_l(nzb,1,0), sums(nzb,1), nzt+2-nzb, &
1877                           MPI_REAL, MPI_SUM, comm2d, ierr )
1878       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1879       CALL MPI_ALLREDUCE( sums_l(nzb,2,0), sums(nzb,2), nzt+2-nzb, &
1880                              MPI_REAL, MPI_SUM, comm2d, ierr )
1881#else
1882       sums(:,1) = sums_l(:,1,0)
1883       sums(:,2) = sums_l(:,2,0)
1884#endif
1885
1886!
1887!--    Final values are obtained by division by the total number of grid
1888!--    points used for the summation.
1889       hom(:,1,1,0) = sums(:,1) / ngp_2dh_outer(:,0)   ! u
1890       hom(:,1,2,0) = sums(:,2) / ngp_2dh_outer(:,0)   ! v
1891
1892!
1893!--    Now calculate the profiles of SGS TKE and the resolved-scale
1894!--    velocity variances
1895       sums_l(:,8,0)  = 0.0_wp
1896       sums_l(:,30,0) = 0.0_wp
1897       sums_l(:,31,0) = 0.0_wp
1898       sums_l(:,32,0) = 0.0_wp
1899       DO  i = nxl, nxr
1900          DO  j = nys, nyn
1901             DO  k = nzb, nzt+1
1902!
1903!--             Flag indicating vicinity of wall
1904                flag1 = MERGE( 1.0_wp, 0.0_wp, BTEST( wall_flags_0(k,j,i), 24 ) )
1905
1906                sums_l(k,8,0)  = sums_l(k,8,0)  + e(k,j,i)                       * flag1
1907                sums_l(k,30,0) = sums_l(k,30,0) + ( u(k,j,i) - hom(k,1,1,0) )**2 * flag1
1908                sums_l(k,31,0) = sums_l(k,31,0) + ( v(k,j,i) - hom(k,1,2,0) )**2 * flag1
1909                sums_l(k,32,0) = sums_l(k,32,0) + w(k,j,i)**2                    * flag1
1910             ENDDO
1911          ENDDO
1912       ENDDO
1913
1914#if defined( __parallel )
1915!
1916!--    Compute total sum from local sums
1917       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1918       CALL MPI_ALLREDUCE( sums_l(nzb,8,0), sums(nzb,8), nzt+2-nzb, &
1919                           MPI_REAL, MPI_SUM, comm2d, ierr )
1920       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1921       CALL MPI_ALLREDUCE( sums_l(nzb,30,0), sums(nzb,30), nzt+2-nzb, &
1922                           MPI_REAL, MPI_SUM, comm2d, ierr )
1923       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1924       CALL MPI_ALLREDUCE( sums_l(nzb,31,0), sums(nzb,31), nzt+2-nzb, &
1925                           MPI_REAL, MPI_SUM, comm2d, ierr )
1926       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1927       CALL MPI_ALLREDUCE( sums_l(nzb,32,0), sums(nzb,32), nzt+2-nzb, &
1928                           MPI_REAL, MPI_SUM, comm2d, ierr )
1929
1930#else
1931       sums(:,8)  = sums_l(:,8,0)
1932       sums(:,30) = sums_l(:,30,0)
1933       sums(:,31) = sums_l(:,31,0)
1934       sums(:,32) = sums_l(:,32,0)
1935#endif
1936
1937!
1938!--    Final values are obtained by division by the total number of grid
1939!--    points used for the summation.
1940       hom(:,1,8,0)  = sums(:,8)  / ngp_2dh_outer(:,0)   ! e
1941       hom(:,1,30,0) = sums(:,30) / ngp_2dh_outer(:,0)   ! u*2
1942       hom(:,1,31,0) = sums(:,31) / ngp_2dh_outer(:,0)   ! v*2
1943       hom(:,1,32,0) = sums(:,32) / ngp_2dh_outer(:,0)   ! w*2
1944
1945    ENDIF
1946
1947 END SUBROUTINE lpm_init_sgs_tke
1948 
1949 
1950!------------------------------------------------------------------------------!
1951! Description:
1952! ------------
1953!> Sobroutine control lpm actions, i.e. all actions during one time step.
1954!------------------------------------------------------------------------------! 
1955 SUBROUTINE lpm_actions( location )
1956
1957    CHARACTER (LEN=*), INTENT(IN) ::  location !< call location string
1958
1959    INTEGER(iwp)       ::  i                  !<
1960    INTEGER(iwp)       ::  ie                 !<
1961    INTEGER(iwp)       ::  is                 !<
1962    INTEGER(iwp)       ::  j                  !<
1963    INTEGER(iwp)       ::  je                 !<
1964    INTEGER(iwp)       ::  js                 !<
1965    INTEGER(iwp), SAVE ::  lpm_count = 0      !<
1966    INTEGER(iwp)       ::  k                  !<
1967    INTEGER(iwp)       ::  ke                 !<
1968    INTEGER(iwp)       ::  ks                 !<
1969    INTEGER(iwp)       ::  m                  !<
1970    INTEGER(iwp), SAVE ::  steps = 0          !<
1971
1972    LOGICAL            ::  first_loop_stride  !<
1973
1974
1975    SELECT CASE ( location )
1976
1977       CASE ( 'after_prognostic_equations' )
1978
1979          CALL cpu_log( log_point(25), 'lpm', 'start' )
1980!
1981!--       Write particle data at current time on file.
1982!--       This has to be done here, before particles are further processed,
1983!--       because they may be deleted within this timestep (in case that
1984!--       dt_write_particle_data = dt_prel = particle_maximum_age).
1985          time_write_particle_data = time_write_particle_data + dt_3d
1986          IF ( time_write_particle_data >= dt_write_particle_data )  THEN
1987
1988             CALL lpm_data_output_particles
1989!
1990!--       The MOD function allows for changes in the output interval with restart
1991!--       runs.
1992             time_write_particle_data = MOD( time_write_particle_data, &
1993                                        MAX( dt_write_particle_data, dt_3d ) )
1994          ENDIF
1995
1996!
1997!--       Initialize arrays for marking those particles to be deleted after the
1998!--       (sub-) timestep
1999          deleted_particles = 0
2000
2001!
2002!--       Initialize variables used for accumulating the number of particles
2003!--       xchanged between the subdomains during all sub-timesteps (if sgs
2004!--       velocities are included). These data are output further below on the
2005!--       particle statistics file.
2006          trlp_count_sum      = 0
2007          trlp_count_recv_sum = 0
2008          trrp_count_sum      = 0
2009          trrp_count_recv_sum = 0
2010          trsp_count_sum      = 0
2011          trsp_count_recv_sum = 0
2012          trnp_count_sum      = 0
2013          trnp_count_recv_sum = 0
2014!
2015!--       Calculate exponential term used in case of particle inertia for each
2016!--       of the particle groups
2017          DO  m = 1, number_of_particle_groups
2018             IF ( particle_groups(m)%density_ratio /= 0.0_wp )  THEN
2019                particle_groups(m)%exp_arg  =                                        &
2020                          4.5_wp * particle_groups(m)%density_ratio *                &
2021                          molecular_viscosity / ( particle_groups(m)%radius )**2
2022
2023                particle_groups(m)%exp_term = EXP( -particle_groups(m)%exp_arg *     &
2024                          dt_3d )
2025             ENDIF
2026          ENDDO
2027!
2028!--       If necessary, release new set of particles
2029          IF ( ( simulated_time - last_particle_release_time ) >= dt_prel  .AND.     &
2030                 end_time_prel > simulated_time )  THEN
2031             DO WHILE ( ( simulated_time - last_particle_release_time ) >= dt_prel )
2032                CALL lpm_create_particle( PHASE_RELEASE )
2033                last_particle_release_time = last_particle_release_time + dt_prel
2034             ENDDO
2035          ENDIF
2036!
2037!--       Reset summation arrays
2038          IF ( cloud_droplets )  THEN
2039             ql_c  = 0.0_wp
2040             ql_v  = 0.0_wp
2041             ql_vp = 0.0_wp
2042          ENDIF
2043
2044          first_loop_stride = .TRUE.
2045          grid_particles(:,:,:)%time_loop_done = .TRUE.
2046!
2047!--       Timestep loop for particle advection.
2048!--       This loop has to be repeated until the advection time of every particle
2049!--       (within the total domain!) has reached the LES timestep (dt_3d).
2050!--       In case of including the SGS velocities, the particle timestep may be
2051!--       smaller than the LES timestep (because of the Lagrangian timescale
2052!--       restriction) and particles may require to undergo several particle
2053!--       timesteps, before the LES timestep is reached. Because the number of these
2054!--       particle timesteps to be carried out is unknown at first, these steps are
2055!--       carried out in the following infinite loop with exit condition.
2056          DO
2057             CALL cpu_log( log_point_s(44), 'lpm_advec', 'start' )
2058             CALL cpu_log( log_point_s(44), 'lpm_advec', 'pause' )
2059
2060!
2061!--          If particle advection includes SGS velocity components, calculate the
2062!--          required SGS quantities (i.e. gradients of the TKE, as well as
2063!--          horizontally averaged profiles of the SGS TKE and the resolved-scale
2064!--          velocity variances)
2065             IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
2066                CALL lpm_init_sgs_tke
2067             ENDIF
2068!
2069!--          In case SGS-particle speed is considered, particles may carry out
2070!--          several particle timesteps. In order to prevent unnecessary
2071!--          treatment of particles that already reached the final time level,
2072!--          particles are sorted into contiguous blocks of finished and
2073!--          not-finished particles, in addition to their already sorting
2074!--          according to their sub-boxes.
2075             IF ( .NOT. first_loop_stride  .AND.  use_sgs_for_particles )            &
2076                CALL lpm_sort_timeloop_done
2077             DO  i = nxl, nxr
2078                DO  j = nys, nyn
2079                   DO  k = nzb+1, nzt
2080
2081                      number_of_particles = prt_count(k,j,i)
2082!
2083!--                   If grid cell gets empty, flag must be true
2084                      IF ( number_of_particles <= 0 )  THEN
2085                         grid_particles(k,j,i)%time_loop_done = .TRUE.
2086                         CYCLE
2087                      ENDIF
2088
2089                      IF ( .NOT. first_loop_stride  .AND.  &
2090                           grid_particles(k,j,i)%time_loop_done )  CYCLE
2091
2092                      particles => grid_particles(k,j,i)%particles(1:number_of_particles)
2093
2094                      particles(1:number_of_particles)%particle_mask = .TRUE.
2095!
2096!--                   Initialize the variable storing the total time that a particle
2097!--                   has advanced within the timestep procedure
2098                      IF ( first_loop_stride )  THEN
2099                         particles(1:number_of_particles)%dt_sum = 0.0_wp
2100                      ENDIF
2101!
2102!--                   Particle (droplet) growth by condensation/evaporation and
2103!--                   collision
2104                      IF ( cloud_droplets  .AND.  first_loop_stride)  THEN
2105!
2106!--                      Droplet growth by condensation / evaporation
2107                         CALL lpm_droplet_condensation(i,j,k)
2108!
2109!--                      Particle growth by collision
2110                         IF ( collision_kernel /= 'none' )  THEN
2111                            CALL lpm_droplet_collision(i,j,k)
2112                         ENDIF
2113
2114                      ENDIF
2115!
2116!--                   Initialize the switch used for the loop exit condition checked
2117!--                   at the end of this loop. If at least one particle has failed to
2118!--                   reach the LES timestep, this switch will be set false in
2119!--                   lpm_advec.
2120                      dt_3d_reached_l = .TRUE.
2121
2122!
2123!--                   Particle advection
2124                      CALL lpm_advec( i, j, k )
2125!
2126!--                   Particle reflection from walls. Only applied if the particles
2127!--                   are in the vertical range of the topography. (Here, some
2128!--                   optimization is still possible.)
2129                      IF ( topography /= 'flat'  .AND.  k < nzb_max + 2 )  THEN
2130                         CALL  lpm_boundary_conds( 'walls', i, j, k )
2131                      ENDIF
2132!
2133!--                   User-defined actions after the calculation of the new particle
2134!--                   position
2135                      CALL user_lpm_advec( i, j, k )
2136!
2137!--                   Apply boundary conditions to those particles that have crossed
2138!--                   the top or bottom boundary and delete those particles, which are
2139!--                   older than allowed
2140                      CALL lpm_boundary_conds( 'bottom/top', i, j, k )
2141!
2142!---                  If not all particles of the actual grid cell have reached the
2143!--                   LES timestep, this cell has to do another loop iteration. Due to
2144!--                   the fact that particles can move into neighboring grid cells,
2145!--                   these neighbor cells also have to perform another loop iteration.
2146!--                   Please note, this realization does not work properly if
2147!--                   particles move into another subdomain.
2148                      IF ( .NOT. dt_3d_reached_l )  THEN
2149                         ks = MAX(nzb+1,k-1)
2150                         ke = MIN(nzt,k+1)
2151                         js = MAX(nys,j-1)
2152                         je = MIN(nyn,j+1)
2153                         is = MAX(nxl,i-1)
2154                         ie = MIN(nxr,i+1)
2155                         grid_particles(ks:ke,js:je,is:ie)%time_loop_done = .FALSE.
2156                      ELSE
2157                         grid_particles(k,j,i)%time_loop_done = .TRUE.
2158                      ENDIF
2159
2160                   ENDDO
2161                ENDDO
2162             ENDDO
2163             steps = steps + 1
2164             dt_3d_reached_l = ALL(grid_particles(:,:,:)%time_loop_done)
2165!
2166!--          Find out, if all particles on every PE have completed the LES timestep
2167!--          and set the switch corespondingly
2168#if defined( __parallel )
2169             IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2170             CALL MPI_ALLREDUCE( dt_3d_reached_l, dt_3d_reached, 1, MPI_LOGICAL, &
2171                                 MPI_LAND, comm2d, ierr )
2172#else
2173             dt_3d_reached = dt_3d_reached_l
2174#endif
2175             CALL cpu_log( log_point_s(44), 'lpm_advec', 'stop' )
2176
2177!
2178!--          Apply splitting and merging algorithm
2179             IF ( cloud_droplets )  THEN
2180                IF ( splitting )  THEN
2181                   CALL lpm_splitting
2182                ENDIF
2183                IF ( merging )  THEN
2184                   CALL lpm_merging
2185                ENDIF
2186             ENDIF
2187!
2188!--          Move Particles local to PE to a different grid cell
2189             CALL lpm_move_particle
2190!
2191!--          Horizontal boundary conditions including exchange between subdmains
2192             CALL lpm_exchange_horiz
2193
2194!
2195!--          IF .FALSE., lpm_sort_and_delete is done inside pcmp
2196             IF ( .NOT. dt_3d_reached  .OR.  .NOT. nested_run )   THEN
2197!
2198!--             Pack particles (eliminate those marked for deletion),
2199!--             determine new number of particles
2200                CALL lpm_sort_and_delete
2201
2202!--             Initialize variables for the next (sub-) timestep, i.e., for marking
2203!--             those particles to be deleted after the timestep
2204                deleted_particles = 0
2205             ENDIF
2206
2207             IF ( dt_3d_reached )  EXIT
2208
2209             first_loop_stride = .FALSE.
2210          ENDDO   ! timestep loop
2211!
2212!--       in case of nested runs do the transfer of particles after every full model time step
2213          IF ( nested_run )   THEN
2214             CALL particles_from_parent_to_child
2215             CALL particles_from_child_to_parent
2216             CALL pmcp_p_delete_particles_in_fine_grid_area
2217
2218             CALL lpm_sort_and_delete
2219
2220             deleted_particles = 0
2221          ENDIF
2222
2223!
2224!--       Calculate the new liquid water content for each grid box
2225          IF ( cloud_droplets )  CALL lpm_calc_liquid_water_content
2226
2227!
2228!--       Deallocate unused memory
2229          IF ( deallocate_memory  .AND.  lpm_count == step_dealloc )  THEN
2230             CALL dealloc_particles_array
2231             lpm_count = 0
2232          ELSEIF ( deallocate_memory )  THEN
2233             lpm_count = lpm_count + 1
2234          ENDIF
2235
2236!
2237!--       Write particle statistics (in particular the number of particles
2238!--       exchanged between the subdomains) on file
2239          IF ( write_particle_statistics )  CALL lpm_write_exchange_statistics
2240
2241          CALL cpu_log( log_point(25), 'lpm', 'stop' )
2242
2243! !
2244! !--       Output of particle time series
2245!           IF ( particle_advection )  THEN
2246!              IF ( time_dopts >= dt_dopts  .OR.                                                        &
2247!                   ( time_since_reference_point >= particle_advection_start  .AND.                     &
2248!                    first_call_lpm ) )  THEN
2249!                 CALL lpm_data_output_ptseries
2250!                 time_dopts = MOD( time_dopts, MAX( dt_dopts, dt_3d ) )
2251!              ENDIF
2252!           ENDIF
2253
2254       CASE DEFAULT
2255          CONTINUE
2256
2257    END SELECT
2258
2259 END SUBROUTINE lpm_actions
2260 
2261 
2262!------------------------------------------------------------------------------!
2263! Description:
2264! ------------
2265!
2266!------------------------------------------------------------------------------!
2267 SUBROUTINE particles_from_parent_to_child
2268    IMPLICIT NONE
2269
2270    CALL pmcp_c_get_particle_from_parent                         ! Child actions
2271    CALL pmcp_p_fill_particle_win                                ! Parent actions
2272
2273    RETURN
2274 END SUBROUTINE particles_from_parent_to_child
2275
2276 
2277!------------------------------------------------------------------------------!
2278! Description:
2279! ------------
2280!
2281!------------------------------------------------------------------------------!
2282 SUBROUTINE particles_from_child_to_parent
2283    IMPLICIT NONE
2284
2285    CALL pmcp_c_send_particle_to_parent                         ! Child actions
2286    CALL pmcp_p_empty_particle_win                              ! Parent actions
2287
2288    RETURN
2289 END SUBROUTINE particles_from_child_to_parent
2290 
2291!------------------------------------------------------------------------------!
2292! Description:
2293! ------------
2294!> This routine write exchange statistics of the lpm in a ascii file.
2295!------------------------------------------------------------------------------!
2296 SUBROUTINE lpm_write_exchange_statistics
2297
2298    INTEGER(iwp) ::  ip         !<
2299    INTEGER(iwp) ::  jp         !<
2300    INTEGER(iwp) ::  kp         !<
2301    INTEGER(iwp) ::  tot_number_of_particles !<
2302
2303!
2304!-- Determine the current number of particles
2305    number_of_particles         = 0
2306    DO  ip = nxl, nxr
2307       DO  jp = nys, nyn
2308          DO  kp = nzb+1, nzt
2309             number_of_particles = number_of_particles                         &
2310                                     + prt_count(kp,jp,ip)
2311          ENDDO
2312       ENDDO
2313    ENDDO
2314
2315    CALL check_open( 80 )
2316#if defined( __parallel )
2317    WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
2318                        number_of_particles, pleft, trlp_count_sum,      &
2319                        trlp_count_recv_sum, pright, trrp_count_sum,     &
2320                        trrp_count_recv_sum, psouth, trsp_count_sum,     &
2321                        trsp_count_recv_sum, pnorth, trnp_count_sum,     &
2322                        trnp_count_recv_sum
2323#else
2324    WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
2325                        number_of_particles
2326#endif
2327    CALL close_file( 80 )
2328
2329    IF ( number_of_particles > 0 )  THEN
2330        WRITE(9,*) 'number_of_particles ', number_of_particles,                &
2331                    current_timestep_number + 1, simulated_time + dt_3d
2332    ENDIF
2333
2334#if defined( __parallel )
2335    CALL MPI_ALLREDUCE( number_of_particles, tot_number_of_particles, 1,       &
2336                        MPI_INTEGER, MPI_SUM, comm2d, ierr )
2337#else
2338    tot_number_of_particles = number_of_particles
2339#endif
2340
2341    IF ( nested_run )  THEN
2342       CALL pmcp_g_print_number_of_particles( simulated_time+dt_3d,            &
2343                                              tot_number_of_particles)
2344    ENDIF
2345
2346!
2347!-- Formats
23488000 FORMAT (I6,1X,F7.2,4X,I10,5X,4(I3,1X,I4,'/',I4,2X),6X,I10)
2349
2350
2351 END SUBROUTINE lpm_write_exchange_statistics
2352 
2353
2354!------------------------------------------------------------------------------!
2355! Description:
2356! ------------
2357!> Write particle data in FORTRAN binary and/or netCDF format
2358!------------------------------------------------------------------------------!
2359 SUBROUTINE lpm_data_output_particles
2360 
2361    INTEGER(iwp) ::  ip !<
2362    INTEGER(iwp) ::  jp !<
2363    INTEGER(iwp) ::  kp !<
2364
2365    CALL cpu_log( log_point_s(40), 'lpm_data_output', 'start' )
2366
2367!
2368!-- Attention: change version number for unit 85 (in routine check_open)
2369!--            whenever the output format for this unit is changed!
2370    CALL check_open( 85 )
2371
2372    WRITE ( 85 )  simulated_time
2373    WRITE ( 85 )  prt_count
2374
2375    DO  ip = nxl, nxr
2376       DO  jp = nys, nyn
2377          DO  kp = nzb+1, nzt
2378             number_of_particles = prt_count(kp,jp,ip)
2379             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
2380             IF ( number_of_particles <= 0 )  CYCLE
2381             WRITE ( 85 )  particles
2382          ENDDO
2383       ENDDO
2384    ENDDO
2385
2386    CALL close_file( 85 )
2387
2388
2389#if defined( __netcdf )
2390! !
2391! !-- Output in netCDF format
2392!     CALL check_open( 108 )
2393!
2394! !
2395! !-- Update the NetCDF time axis
2396!     prt_time_count = prt_time_count + 1
2397!
2398!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_time_prt, &
2399!                             (/ simulated_time /),        &
2400!                             start = (/ prt_time_count /), count = (/ 1 /) )
2401!     CALL netcdf_handle_error( 'lpm_data_output_particles', 1 )
2402!
2403! !
2404! !-- Output the real number of particles used
2405!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_rnop_prt, &
2406!                             (/ number_of_particles /),   &
2407!                             start = (/ prt_time_count /), count = (/ 1 /) )
2408!     CALL netcdf_handle_error( 'lpm_data_output_particles', 2 )
2409!
2410! !
2411! !-- Output all particle attributes
2412!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(1), particles%age,      &
2413!                             start = (/ 1, prt_time_count /),               &
2414!                             count = (/ maximum_number_of_particles /) )
2415!     CALL netcdf_handle_error( 'lpm_data_output_particles', 3 )
2416!
2417!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(2), particles%user,     &
2418!                             start = (/ 1, prt_time_count /),               &
2419!                             count = (/ maximum_number_of_particles /) )
2420!     CALL netcdf_handle_error( 'lpm_data_output_particles', 4 )
2421!
2422!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(3), particles%origin_x, &
2423!                             start = (/ 1, prt_time_count /),               &
2424!                             count = (/ maximum_number_of_particles /) )
2425!     CALL netcdf_handle_error( 'lpm_data_output_particles', 5 )
2426!
2427!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(4), particles%origin_y, &
2428!                             start = (/ 1, prt_time_count /),               &
2429!                             count = (/ maximum_number_of_particles /) )
2430!     CALL netcdf_handle_error( 'lpm_data_output_particles', 6 )
2431!
2432!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(5), particles%origin_z, &
2433!                             start = (/ 1, prt_time_count /),               &
2434!                             count = (/ maximum_number_of_particles /) )
2435!     CALL netcdf_handle_error( 'lpm_data_output_particles', 7 )
2436!
2437!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(6), particles%radius,   &
2438!                             start = (/ 1, prt_time_count /),               &
2439!                             count = (/ maximum_number_of_particles /) )
2440!     CALL netcdf_handle_error( 'lpm_data_output_particles', 8 )
2441!
2442!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(7), particles%speed_x,  &
2443!                             start = (/ 1, prt_time_count /),               &
2444!                             count = (/ maximum_number_of_particles /) )
2445!     CALL netcdf_handle_error( 'lpm_data_output_particles', 9 )
2446!
2447!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(8), particles%speed_y,  &
2448!                             start = (/ 1, prt_time_count /),               &
2449!                             count = (/ maximum_number_of_particles /) )
2450!     CALL netcdf_handle_error( 'lpm_data_output_particles', 10 )
2451!
2452!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(9), particles%speed_z,  &
2453!                             start = (/ 1, prt_time_count /),               &
2454!                             count = (/ maximum_number_of_particles /) )
2455!     CALL netcdf_handle_error( 'lpm_data_output_particles', 11 )
2456!
2457!     nc_stat = NF90_PUT_VAR( id_set_prt,id_var_prt(10),                     &
2458!                             particles%weight_factor,                       &
2459!                             start = (/ 1, prt_time_count /),               &
2460!                             count = (/ maximum_number_of_particles /) )
2461!     CALL netcdf_handle_error( 'lpm_data_output_particles', 12 )
2462!
2463!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(11), particles%x,       &
2464!                             start = (/ 1, prt_time_count /),               &
2465!                             count = (/ maximum_number_of_particles /) )
2466!     CALL netcdf_handle_error( 'lpm_data_output_particles', 13 )
2467!
2468!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(12), particles%y,       &
2469!                             start = (/ 1, prt_time_count /),               &
2470!                             count = (/ maximum_number_of_particles /) )
2471!     CALL netcdf_handle_error( 'lpm_data_output_particles', 14 )
2472!
2473!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(13), particles%z,       &
2474!                             start = (/ 1, prt_time_count /),               &
2475!                             count = (/ maximum_number_of_particles /) )
2476!     CALL netcdf_handle_error( 'lpm_data_output_particles', 15 )
2477!
2478!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(14), particles%class,   &
2479!                             start = (/ 1, prt_time_count /),               &
2480!                             count = (/ maximum_number_of_particles /) )
2481!     CALL netcdf_handle_error( 'lpm_data_output_particles', 16 )
2482!
2483!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(15), particles%group,   &
2484!                             start = (/ 1, prt_time_count /),               &
2485!                             count = (/ maximum_number_of_particles /) )
2486!     CALL netcdf_handle_error( 'lpm_data_output_particles', 17 )
2487!
2488!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(16),                    &
2489!                             particles%id2,                                 &
2490!                             start = (/ 1, prt_time_count /),               &
2491!                             count = (/ maximum_number_of_particles /) )
2492!     CALL netcdf_handle_error( 'lpm_data_output_particles', 18 )
2493!
2494!     nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(17), particles%id1,     &
2495!                             start = (/ 1, prt_time_count /),               &
2496!                             count = (/ maximum_number_of_particles /) )
2497!     CALL netcdf_handle_error( 'lpm_data_output_particles', 19 )
2498!
2499#endif
2500
2501    CALL cpu_log( log_point_s(40), 'lpm_data_output', 'stop' )
2502
2503 END SUBROUTINE lpm_data_output_particles
2504 
2505!------------------------------------------------------------------------------!
2506! Description:
2507! ------------
2508!> This routine calculates and provide particle timeseries output.
2509!------------------------------------------------------------------------------!
2510 SUBROUTINE lpm_data_output_ptseries
2511 
2512    INTEGER(iwp) ::  i    !<
2513    INTEGER(iwp) ::  inum !<
2514    INTEGER(iwp) ::  j    !<
2515    INTEGER(iwp) ::  jg   !<
2516    INTEGER(iwp) ::  k    !<
2517    INTEGER(iwp) ::  n    !<
2518
2519    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pts_value   !<
2520    REAL(wp), DIMENSION(:,:), ALLOCATABLE ::  pts_value_l !<
2521
2522
2523    CALL cpu_log( log_point(36), 'data_output_ptseries', 'start' )
2524
2525    IF ( myid == 0 )  THEN
2526!
2527!--    Open file for time series output in NetCDF format
2528       dopts_time_count = dopts_time_count + 1
2529       CALL check_open( 109 )
2530#if defined( __netcdf )
2531!
2532!--    Update the particle time series time axis
2533       nc_stat = NF90_PUT_VAR( id_set_pts, id_var_time_pts,      &
2534                               (/ time_since_reference_point /), &
2535                               start = (/ dopts_time_count /), count = (/ 1 /) )
2536       CALL netcdf_handle_error( 'data_output_ptseries', 391 )
2537#endif
2538
2539    ENDIF
2540
2541    ALLOCATE( pts_value(0:number_of_particle_groups,dopts_num), &
2542              pts_value_l(0:number_of_particle_groups,dopts_num) )
2543
2544    pts_value_l = 0.0_wp
2545    pts_value_l(:,16) = 9999999.9_wp    ! for calculation of minimum radius
2546
2547!
2548!-- Calculate or collect the particle time series quantities for all particles
2549!-- and seperately for each particle group (if there is more than one group)
2550    DO  i = nxl, nxr
2551       DO  j = nys, nyn
2552          DO  k = nzb, nzt
2553             number_of_particles = prt_count(k,j,i)
2554             IF (number_of_particles <= 0)  CYCLE
2555             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
2556             DO  n = 1, number_of_particles
2557
2558                IF ( particles(n)%particle_mask )  THEN  ! Restrict analysis to active particles
2559
2560                   pts_value_l(0,1)  = pts_value_l(0,1) + 1.0_wp  ! total # of particles
2561                   pts_value_l(0,2)  = pts_value_l(0,2) +                      &
2562                          ( particles(n)%x - particles(n)%origin_x )  ! mean x
2563                   pts_value_l(0,3)  = pts_value_l(0,3) +                      &
2564                          ( particles(n)%y - particles(n)%origin_y )  ! mean y
2565                   pts_value_l(0,4)  = pts_value_l(0,4) +                      &
2566                          ( particles(n)%z - particles(n)%origin_z )  ! mean z
2567                   pts_value_l(0,5)  = pts_value_l(0,5) + particles(n)%z        ! mean z (absolute)
2568                   pts_value_l(0,6)  = pts_value_l(0,6) + particles(n)%speed_x  ! mean u
2569                   pts_value_l(0,7)  = pts_value_l(0,7) + particles(n)%speed_y  ! mean v
2570                   pts_value_l(0,8)  = pts_value_l(0,8) + particles(n)%speed_z  ! mean w
2571                   pts_value_l(0,9)  = pts_value_l(0,9)  + particles(n)%rvar1 ! mean sgsu
2572                   pts_value_l(0,10) = pts_value_l(0,10) + particles(n)%rvar2 ! mean sgsv
2573                   pts_value_l(0,11) = pts_value_l(0,11) + particles(n)%rvar3 ! mean sgsw
2574                   IF ( particles(n)%speed_z > 0.0_wp )  THEN
2575                      pts_value_l(0,12) = pts_value_l(0,12) + 1.0_wp  ! # of upward moving prts
2576                      pts_value_l(0,13) = pts_value_l(0,13) +                  &
2577                                              particles(n)%speed_z ! mean w upw.
2578                   ELSE
2579                      pts_value_l(0,14) = pts_value_l(0,14) +                  &
2580                                              particles(n)%speed_z ! mean w down
2581                   ENDIF
2582                   pts_value_l(0,15) = pts_value_l(0,15) + particles(n)%radius ! mean rad
2583                   pts_value_l(0,16) = MIN( pts_value_l(0,16), particles(n)%radius ) ! minrad
2584                   pts_value_l(0,17) = MAX( pts_value_l(0,17), particles(n)%radius ) ! maxrad
2585                   pts_value_l(0,18) = pts_value_l(0,18) + 1.0_wp
2586                   pts_value_l(0,19) = pts_value_l(0,18) + 1.0_wp
2587!
2588!--                Repeat the same for the respective particle group
2589                   IF ( number_of_particle_groups > 1 )  THEN
2590                      jg = particles(n)%group
2591
2592                      pts_value_l(jg,1)  = pts_value_l(jg,1) + 1.0_wp
2593                      pts_value_l(jg,2)  = pts_value_l(jg,2) +                   &
2594                           ( particles(n)%x - particles(n)%origin_x )
2595                      pts_value_l(jg,3)  = pts_value_l(jg,3) +                   &
2596                           ( particles(n)%y - particles(n)%origin_y )
2597                      pts_value_l(jg,4)  = pts_value_l(jg,4) +                   &
2598                           ( particles(n)%z - particles(n)%origin_z )
2599                      pts_value_l(jg,5)  = pts_value_l(jg,5) + particles(n)%z
2600                      pts_value_l(jg,6)  = pts_value_l(jg,6) + particles(n)%speed_x
2601                      pts_value_l(jg,7)  = pts_value_l(jg,7) + particles(n)%speed_y
2602                      pts_value_l(jg,8)  = pts_value_l(jg,8) + particles(n)%speed_z
2603                      pts_value_l(jg,9)  = pts_value_l(jg,9)  + particles(n)%rvar1
2604                      pts_value_l(jg,10) = pts_value_l(jg,10) + particles(n)%rvar2
2605                      pts_value_l(jg,11) = pts_value_l(jg,11) + particles(n)%rvar3
2606                      IF ( particles(n)%speed_z > 0.0_wp )  THEN
2607                         pts_value_l(jg,12) = pts_value_l(jg,12) + 1.0_wp
2608                         pts_value_l(jg,13) = pts_value_l(jg,13) + particles(n)%speed_z
2609                      ELSE
2610                         pts_value_l(jg,14) = pts_value_l(jg,14) + particles(n)%speed_z
2611                      ENDIF
2612                      pts_value_l(jg,15) = pts_value_l(jg,15) + particles(n)%radius
2613                      pts_value_l(jg,16) = MIN( pts_value(jg,16), particles(n)%radius )
2614                      pts_value_l(jg,17) = MAX( pts_value(jg,17), particles(n)%radius )
2615                      pts_value_l(jg,18) = pts_value_l(jg,18) + 1.0_wp
2616                      pts_value_l(jg,19) = pts_value_l(jg,19) + 1.0_wp
2617                   ENDIF
2618
2619                ENDIF
2620
2621             ENDDO
2622
2623          ENDDO
2624       ENDDO
2625    ENDDO
2626
2627
2628#if defined( __parallel )
2629!
2630!-- Sum values of the subdomains
2631    inum = number_of_particle_groups + 1
2632
2633    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2634    CALL MPI_ALLREDUCE( pts_value_l(0,1), pts_value(0,1), 15*inum, MPI_REAL, &
2635                        MPI_SUM, comm2d, ierr )
2636    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2637    CALL MPI_ALLREDUCE( pts_value_l(0,16), pts_value(0,16), inum, MPI_REAL, &
2638                        MPI_MIN, comm2d, ierr )
2639    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2640    CALL MPI_ALLREDUCE( pts_value_l(0,17), pts_value(0,17), inum, MPI_REAL, &
2641                        MPI_MAX, comm2d, ierr )
2642    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2643    CALL MPI_ALLREDUCE( pts_value_l(0,18), pts_value(0,18), inum, MPI_REAL, &
2644                        MPI_MAX, comm2d, ierr )
2645    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2646    CALL MPI_ALLREDUCE( pts_value_l(0,19), pts_value(0,19), inum, MPI_REAL, &
2647                        MPI_MIN, comm2d, ierr )
2648#else
2649    pts_value(:,1:19) = pts_value_l(:,1:19)
2650#endif
2651
2652!
2653!-- Normalize the above calculated quantities (except min/max values) with the
2654!-- total number of particles
2655    IF ( number_of_particle_groups > 1 )  THEN
2656       inum = number_of_particle_groups
2657    ELSE
2658       inum = 0
2659    ENDIF
2660
2661    DO  j = 0, inum
2662
2663       IF ( pts_value(j,1) > 0.0_wp )  THEN
2664
2665          pts_value(j,2:15) = pts_value(j,2:15) / pts_value(j,1)
2666          IF ( pts_value(j,12) > 0.0_wp  .AND.  pts_value(j,12) < 1.0_wp )  THEN
2667             pts_value(j,13) = pts_value(j,13) / pts_value(j,12)
2668             pts_value(j,14) = pts_value(j,14) / ( 1.0_wp - pts_value(j,12) )
2669          ELSEIF ( pts_value(j,12) == 0.0_wp )  THEN
2670             pts_value(j,13) = -1.0_wp
2671          ELSE
2672             pts_value(j,14) = -1.0_wp
2673          ENDIF
2674
2675       ENDIF
2676
2677    ENDDO
2678
2679!
2680!-- Calculate higher order moments of particle time series quantities,
2681!-- seperately for each particle group (if there is more than one group)
2682    DO  i = nxl, nxr
2683       DO  j = nys, nyn
2684          DO  k = nzb, nzt
2685             number_of_particles = prt_count(k,j,i)
2686             IF (number_of_particles <= 0)  CYCLE
2687             particles => grid_particles(k,j,i)%particles(1:number_of_particles)
2688             DO  n = 1, number_of_particles
2689
2690                pts_value_l(0,20) = pts_value_l(0,20) + ( particles(n)%x - &
2691                                    particles(n)%origin_x - pts_value(0,2) )**2 ! x*2
2692                pts_value_l(0,21) = pts_value_l(0,21) + ( particles(n)%y - &
2693                                    particles(n)%origin_y - pts_value(0,3) )**2 ! y*2
2694                pts_value_l(0,22) = pts_value_l(0,22) + ( particles(n)%z - &
2695                                    particles(n)%origin_z - pts_value(0,4) )**2 ! z*2
2696                pts_value_l(0,23) = pts_value_l(0,23) + ( particles(n)%speed_x - &
2697                                                         pts_value(0,6) )**2   ! u*2
2698                pts_value_l(0,24) = pts_value_l(0,24) + ( particles(n)%speed_y - &
2699                                                          pts_value(0,7) )**2   ! v*2
2700                pts_value_l(0,25) = pts_value_l(0,25) + ( particles(n)%speed_z - &
2701                                                          pts_value(0,8) )**2   ! w*2
2702                pts_value_l(0,26) = pts_value_l(0,26) + ( particles(n)%rvar1 - &
2703                                                          pts_value(0,9) )**2   ! u"2
2704                pts_value_l(0,27) = pts_value_l(0,27) + ( particles(n)%rvar2 - &
2705                                                          pts_value(0,10) )**2  ! v"2
2706                pts_value_l(0,28) = pts_value_l(0,28) + ( particles(n)%rvar3 - &
2707                                                          pts_value(0,11) )**2  ! w"2
2708!
2709!--             Repeat the same for the respective particle group
2710                IF ( number_of_particle_groups > 1 )  THEN
2711                   jg = particles(n)%group
2712
2713                   pts_value_l(jg,20) = pts_value_l(jg,20) + ( particles(n)%x - &
2714                                       particles(n)%origin_x - pts_value(jg,2) )**2
2715                   pts_value_l(jg,21) = pts_value_l(jg,21) + ( particles(n)%y - &
2716                                       particles(n)%origin_y - pts_value(jg,3) )**2
2717                   pts_value_l(jg,22) = pts_value_l(jg,22) + ( particles(n)%z - &
2718                                       particles(n)%origin_z - pts_value(jg,4) )**2
2719                   pts_value_l(jg,23) = pts_value_l(jg,23) + ( particles(n)%speed_x - &
2720                                                             pts_value(jg,6) )**2
2721                   pts_value_l(jg,24) = pts_value_l(jg,24) + ( particles(n)%speed_y - &
2722                                                             pts_value(jg,7) )**2
2723                   pts_value_l(jg,25) = pts_value_l(jg,25) + ( particles(n)%speed_z - &
2724                                                             pts_value(jg,8) )**2
2725                   pts_value_l(jg,26) = pts_value_l(jg,26) + ( particles(n)%rvar1 - &
2726                                                             pts_value(jg,9) )**2
2727                   pts_value_l(jg,27) = pts_value_l(jg,27) + ( particles(n)%rvar2 - &
2728                                                             pts_value(jg,10) )**2
2729                   pts_value_l(jg,28) = pts_value_l(jg,28) + ( particles(n)%rvar3 - &
2730                                                             pts_value(jg,11) )**2
2731                ENDIF
2732
2733             ENDDO
2734          ENDDO
2735       ENDDO
2736    ENDDO
2737
2738    pts_value_l(0,29) = ( number_of_particles - pts_value(0,1) / numprocs )**2
2739                                                 ! variance of particle numbers
2740    IF ( number_of_particle_groups > 1 )  THEN
2741       DO  j = 1, number_of_particle_groups
2742          pts_value_l(j,29) = ( pts_value_l(j,1) - &
2743                                pts_value(j,1) / numprocs )**2
2744       ENDDO
2745    ENDIF
2746
2747#if defined( __parallel )
2748!
2749!-- Sum values of the subdomains
2750    inum = number_of_particle_groups + 1
2751
2752    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
2753    CALL MPI_ALLREDUCE( pts_value_l(0,20), pts_value(0,20), inum*10, MPI_REAL, &
2754                        MPI_SUM, comm2d, ierr )
2755#else
2756    pts_value(:,20:29) = pts_value_l(:,20:29)
2757#endif
2758
2759!
2760!-- Normalize the above calculated quantities with the total number of
2761!-- particles
2762    IF ( number_of_particle_groups > 1 )  THEN
2763       inum = number_of_particle_groups
2764    ELSE
2765       inum = 0
2766    ENDIF
2767
2768    DO  j = 0, inum
2769
2770       IF ( pts_value(j,1) > 0.0_wp )  THEN
2771          pts_value(j,20:28) = pts_value(j,20:28) / pts_value(j,1)
2772       ENDIF
2773       pts_value(j,29) = pts_value(j,29) / numprocs
2774
2775    ENDDO
2776
2777#if defined( __netcdf )
2778!
2779!-- Output particle time series quantities in NetCDF format
2780    IF ( myid == 0 )  THEN
2781       DO  j = 0, inum
2782          DO  i = 1, dopts_num
2783             nc_stat = NF90_PUT_VAR( id_set_pts, id_var_dopts(i,j),  &
2784                                     (/ pts_value(j,i) /),           &
2785                                     start = (/ dopts_time_count /), &
2786                                     count = (/ 1 /) )
2787             CALL netcdf_handle_error( 'data_output_ptseries', 392 )
2788          ENDDO
2789       ENDDO
2790    ENDIF
2791#endif
2792
2793    DEALLOCATE( pts_value, pts_value_l )
2794
2795    CALL cpu_log( log_point(36), 'data_output_ptseries', 'stop' )
2796
2797END SUBROUTINE lpm_data_output_ptseries
2798
2799 
2800!------------------------------------------------------------------------------!
2801! Description:
2802! ------------
2803!> This routine reads the respective restart data for the lpm.
2804!------------------------------------------------------------------------------!
2805 SUBROUTINE lpm_rrd_local_particles
2806
2807    CHARACTER (LEN=10) ::  particle_binary_version    !<
2808    CHARACTER (LEN=10) ::  version_on_file            !<
2809
2810    INTEGER(iwp) ::  alloc_size !<
2811    INTEGER(iwp) ::  ip         !<
2812    INTEGER(iwp) ::  jp         !<
2813    INTEGER(iwp) ::  kp         !<
2814
2815    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  tmp_particles !<
2816
2817!
2818!-- Read particle data from previous model run.
2819!-- First open the input unit.
2820    IF ( myid_char == '' )  THEN
2821       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN'//myid_char,                  &
2822                  FORM='UNFORMATTED' )
2823    ELSE
2824       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN/'//myid_char,                 &
2825                  FORM='UNFORMATTED' )
2826    ENDIF
2827
2828!
2829!-- First compare the version numbers
2830    READ ( 90 )  version_on_file
2831    particle_binary_version = '4.0'
2832    IF ( TRIM( version_on_file ) /= TRIM( particle_binary_version ) )  THEN
2833       message_string = 'version mismatch concerning data from prior ' //      &
2834                        'run &version on file = "' //                          &
2835                                      TRIM( version_on_file ) //               &
2836                        '&version in program = "' //                           &
2837                                      TRIM( particle_binary_version ) // '"'
2838       CALL message( 'lpm_read_restart_file', 'PA0214', 1, 2, 0, 6, 0 )
2839    ENDIF
2840
2841!
2842!-- If less particles are stored on the restart file than prescribed by
2843!-- 1, the remainder is initialized by zero_particle to avoid
2844!-- errors.
2845    zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
2846                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
2847                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
2848                                   0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,     &
2849                                   0, 0, 0_idp, .FALSE., -1 )
2850!
2851!-- Read some particle parameters and the size of the particle arrays,
2852!-- allocate them and read their contents.
2853    READ ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                     &
2854                 last_particle_release_time, number_of_particle_groups,        &
2855                 particle_groups, time_write_particle_data
2856
2857    ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg),                        &
2858              grid_particles(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2859
2860    READ ( 90 )  prt_count
2861
2862    DO  ip = nxl, nxr
2863       DO  jp = nys, nyn
2864          DO  kp = nzb+1, nzt
2865
2866             number_of_particles = prt_count(kp,jp,ip)
2867             IF ( number_of_particles > 0 )  THEN
2868                alloc_size = MAX( INT( number_of_particles *                   &
2869                             ( 1.0_wp + alloc_factor / 100.0_wp ) ),           &
2870                             1 )
2871             ELSE
2872                alloc_size = 1
2873             ENDIF
2874
2875             ALLOCATE( grid_particles(kp,jp,ip)%particles(1:alloc_size) )
2876
2877             IF ( number_of_particles > 0 )  THEN
2878                ALLOCATE( tmp_particles(1:number_of_particles) )
2879                READ ( 90 )  tmp_particles
2880                grid_particles(kp,jp,ip)%particles(1:number_of_particles) = tmp_particles
2881                DEALLOCATE( tmp_particles )
2882                IF ( number_of_particles < alloc_size )  THEN
2883                   grid_particles(kp,jp,ip)%particles(number_of_particles+1:alloc_size) &
2884                      = zero_particle
2885                ENDIF
2886             ELSE
2887                grid_particles(kp,jp,ip)%particles(1:alloc_size) = zero_particle
2888             ENDIF
2889
2890          ENDDO
2891       ENDDO
2892    ENDDO
2893
2894    CLOSE ( 90 )
2895!
2896!-- Must be called to sort particles into blocks, which is needed for a fast
2897!-- interpolation of the LES fields on the particle position.
2898    CALL lpm_sort_and_delete
2899
2900
2901 END SUBROUTINE lpm_rrd_local_particles
2902 
2903 
2904 SUBROUTINE lpm_rrd_local( k, nxlf, nxlc, nxl_on_file, nxrf, nxrc,          &
2905                              nxr_on_file, nynf, nync, nyn_on_file, nysf,  &
2906                              nysc, nys_on_file, tmp_3d, found )
2907
2908
2909   USE control_parameters,                                                 &
2910       ONLY: length, restart_string
2911
2912    INTEGER(iwp) ::  k               !<
2913    INTEGER(iwp) ::  nxlc            !<
2914    INTEGER(iwp) ::  nxlf            !<
2915    INTEGER(iwp) ::  nxl_on_file     !<
2916    INTEGER(iwp) ::  nxrc            !<
2917    INTEGER(iwp) ::  nxrf            !<
2918    INTEGER(iwp) ::  nxr_on_file     !<
2919    INTEGER(iwp) ::  nync            !<
2920    INTEGER(iwp) ::  nynf            !<
2921    INTEGER(iwp) ::  nyn_on_file     !<
2922    INTEGER(iwp) ::  nysc            !<
2923    INTEGER(iwp) ::  nysf            !<
2924    INTEGER(iwp) ::  nys_on_file     !<
2925
2926    LOGICAL, INTENT(OUT)  ::  found
2927
2928    REAL(wp), DIMENSION(nzb:nzt+1,nys_on_file-nbgp:nyn_on_file+nbgp,nxl_on_file-nbgp:nxr_on_file+nbgp) ::  tmp_3d   !<
2929
2930
2931    found = .TRUE.
2932
2933    SELECT CASE ( restart_string(1:length) )
2934
2935       CASE ( 'iran' ) ! matching random numbers is still unresolved issue
2936          IF ( k == 1 )  READ ( 13 )  iran, iran_part
2937
2938        CASE ( 'pc_av' )
2939           IF ( .NOT. ALLOCATED( pc_av ) )  THEN
2940              ALLOCATE( pc_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2941           ENDIF
2942           IF ( k == 1 )  READ ( 13 )  tmp_3d
2943           pc_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =          &
2944              tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
2945
2946        CASE ( 'pr_av' )
2947           IF ( .NOT. ALLOCATED( pr_av ) )  THEN
2948              ALLOCATE( pr_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2949           ENDIF
2950           IF ( k == 1 )  READ ( 13 )  tmp_3d
2951           pr_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =          &
2952              tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
2953 
2954         CASE ( 'ql_c_av' )
2955            IF ( .NOT. ALLOCATED( ql_c_av ) )  THEN
2956               ALLOCATE( ql_c_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2957            ENDIF
2958            IF ( k == 1 )  READ ( 13 )  tmp_3d
2959            ql_c_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =        &
2960               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
2961
2962         CASE ( 'ql_v_av' )
2963            IF ( .NOT. ALLOCATED( ql_v_av ) )  THEN
2964               ALLOCATE( ql_v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2965            ENDIF
2966            IF ( k == 1 )  READ ( 13 )  tmp_3d
2967            ql_v_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =        &
2968               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
2969
2970         CASE ( 'ql_vp_av' )
2971            IF ( .NOT. ALLOCATED( ql_vp_av ) )  THEN
2972               ALLOCATE( ql_vp_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
2973            ENDIF
2974            IF ( k == 1 )  READ ( 13 )  tmp_3d
2975            ql_vp_av(:,nysc-nbgp:nync+nbgp,nxlc-nbgp:nxrc+nbgp) =       &
2976               tmp_3d(:,nysf-nbgp:nynf+nbgp,nxlf-nbgp:nxrf+nbgp)
2977
2978          CASE DEFAULT
2979
2980             found = .FALSE.
2981
2982       END SELECT
2983               
2984
2985 END SUBROUTINE lpm_rrd_local
2986 
2987!------------------------------------------------------------------------------!
2988! Description:
2989! ------------
2990!> This routine writes the respective restart data for the lpm.
2991!------------------------------------------------------------------------------!
2992 SUBROUTINE lpm_wrd_local
2993 
2994    CHARACTER (LEN=10) ::  particle_binary_version   !<
2995
2996    INTEGER(iwp) ::  ip                              !<
2997    INTEGER(iwp) ::  jp                              !<
2998    INTEGER(iwp) ::  kp                              !<
2999!
3000!-- First open the output unit.
3001    IF ( myid_char == '' )  THEN
3002       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT'//myid_char, &
3003                  FORM='UNFORMATTED')
3004    ELSE
3005       IF ( myid == 0 )  CALL local_system( 'mkdir PARTICLE_RESTART_DATA_OUT' )
3006#if defined( __parallel )
3007!
3008!--    Set a barrier in order to allow that thereafter all other processors
3009!--    in the directory created by PE0 can open their file
3010       CALL MPI_BARRIER( comm2d, ierr )
3011#endif
3012       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT/'//myid_char, &
3013                  FORM='UNFORMATTED' )
3014    ENDIF
3015
3016!
3017!-- Write the version number of the binary format.
3018!-- Attention: After changes to the following output commands the version
3019!-- ---------  number of the variable particle_binary_version must be
3020!--            changed! Also, the version number and the list of arrays
3021!--            to be read in lpm_read_restart_file must be adjusted
3022!--            accordingly.
3023    particle_binary_version = '4.0'
3024    WRITE ( 90 )  particle_binary_version
3025
3026!
3027!-- Write some particle parameters, the size of the particle arrays
3028    WRITE ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                    &
3029                  last_particle_release_time, number_of_particle_groups,       &
3030                  particle_groups, time_write_particle_data
3031
3032    WRITE ( 90 )  prt_count
3033         
3034    DO  ip = nxl, nxr
3035       DO  jp = nys, nyn
3036          DO  kp = nzb+1, nzt
3037             number_of_particles = prt_count(kp,jp,ip)
3038             particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
3039             IF ( number_of_particles <= 0 )  CYCLE
3040             WRITE ( 90 )  particles
3041          ENDDO
3042       ENDDO
3043    ENDDO
3044
3045    CLOSE ( 90 )
3046
3047#if defined( __parallel )
3048       CALL MPI_BARRIER( comm2d, ierr )
3049#endif
3050
3051    CALL wrd_write_string( 'iran' ) 
3052    WRITE ( 14 )  iran, iran_part 
3053
3054
3055 END SUBROUTINE lpm_wrd_local
3056
3057
3058!------------------------------------------------------------------------------!
3059! Description:
3060! ------------
3061!> This routine writes the respective restart data for the lpm.
3062!------------------------------------------------------------------------------!
3063 SUBROUTINE lpm_wrd_global
3064 
3065    CALL wrd_write_string( 'curvature_solution_effects' ) 
3066    WRITE ( 14 )  curvature_solution_effects
3067
3068    CALL wrd_write_string( 'interpolation_simple_corrector' )
3069    WRITE ( 14 )  interpolation_simple_corrector
3070
3071    CALL wrd_write_string( 'interpolation_simple_predictor' )
3072    WRITE ( 14 )  interpolation_simple_predictor
3073
3074    CALL wrd_write_string( 'interpolation_trilinear' )
3075    WRITE ( 14 )  interpolation_trilinear
3076
3077 END SUBROUTINE lpm_wrd_global
3078 
3079
3080!------------------------------------------------------------------------------!
3081! Description:
3082! ------------
3083!> This routine writes the respective restart data for the lpm.
3084!------------------------------------------------------------------------------!
3085 SUBROUTINE lpm_rrd_global( found )
3086 
3087    USE control_parameters,                            &
3088        ONLY: length, restart_string
3089
3090    LOGICAL, INTENT(OUT)  ::  found
3091
3092    found = .TRUE.
3093
3094    SELECT CASE ( restart_string(1:length) )
3095
3096       CASE ( 'curvature_solution_effects' )
3097          READ ( 13 )  curvature_solution_effects
3098
3099       CASE ( 'interpolation_simple_corrector' )
3100          READ ( 13 )  interpolation_simple_corrector
3101
3102       CASE ( 'interpolation_simple_predictor' )
3103          READ ( 13 )  interpolation_simple_predictor
3104
3105       CASE ( 'interpolation_trilinear' )
3106          READ ( 13 )  interpolation_trilinear
3107
3108!          CASE ( 'global_paramter' )
3109!             READ ( 13 )  global_parameter
3110!          CASE ( 'global_array' )
3111!             IF ( .NOT. ALLOCATED( global_array ) )  ALLOCATE( global_array(1:10) )
3112!             READ ( 13 )  global_array
3113
3114       CASE DEFAULT
3115
3116          found = .FALSE.
3117
3118    END SELECT
3119   
3120 END SUBROUTINE lpm_rrd_global
3121
3122
3123!------------------------------------------------------------------------------!
3124! Description:
3125! ------------
3126!> This is a submodule of the lagrangian particle model. It contains all
3127!> dynamic processes of the lpm. This includes the advection (resolved and sub-
3128!> grid scale) as well as the boundary conditions of particles. As a next step
3129!> this submodule should be excluded as an own file.
3130!------------------------------------------------------------------------------!
3131 SUBROUTINE lpm_advec (ip,jp,kp)
3132
3133    LOGICAL ::  subbox_at_wall !< flag to see if the current subgridbox is adjacent to a wall
3134
3135    INTEGER(iwp) ::  i                           !< index variable along x
3136    INTEGER(iwp) ::  i_next                      !< index variable along x
3137    INTEGER(iwp) ::  ip                          !< index variable along x
3138    INTEGER(iwp) ::  iteration_steps = 1         !< amount of iterations steps for corrector step
3139    INTEGER(iwp) ::  j                           !< index variable along y
3140    INTEGER(iwp) ::  j_next                      !< index variable along y
3141    INTEGER(iwp) ::  jp                          !< index variable along y
3142    INTEGER(iwp) ::  k                           !< index variable along z
3143    INTEGER(iwp) ::  k_wall                      !< vertical index of topography top
3144    INTEGER(iwp) ::  kp                          !< index variable along z
3145    INTEGER(iwp) ::  k_next                      !< index variable along z
3146    INTEGER(iwp) ::  kw                          !< index variable along z
3147    INTEGER(iwp) ::  kkw                         !< index variable along z
3148    INTEGER(iwp) ::  n                           !< loop variable over all particles in a grid box
3149    INTEGER(iwp) ::  nb                          !< block number particles are sorted in
3150    INTEGER(iwp) ::  particle_end                !< end index for partilce loop
3151    INTEGER(iwp) ::  particle_start              !< start index for particle loop
3152    INTEGER(iwp) ::  surf_start                  !< Index on surface data-type for current grid box
3153    INTEGER(iwp) ::  subbox_end                  !< end index for loop over subboxes in particle advection
3154    INTEGER(iwp) ::  subbox_start                !< start index for loop over subboxes in particle advection
3155    INTEGER(iwp) ::  nn                          !< loop variable over iterations steps
3156
3157    INTEGER(iwp), DIMENSION(0:7) ::  start_index !< start particle index for current block
3158    INTEGER(iwp), DIMENSION(0:7) ::  end_index   !< start particle index for current block
3159
3160    REAL(wp) ::  aa                 !< dummy argument for horizontal particle interpolation
3161    REAL(wp) ::  alpha              !< interpolation facor for x-direction
3162
3163    REAL(wp) ::  bb                 !< dummy argument for horizontal particle interpolation
3164    REAL(wp) ::  beta               !< interpolation facor for y-direction
3165    REAL(wp) ::  cc                 !< dummy argument for horizontal particle interpolation
3166    REAL(wp) ::  d_z_p_z0           !< inverse of interpolation length for logarithmic interpolation
3167    REAL(wp) ::  dd                 !< dummy argument for horizontal particle interpolation
3168    REAL(wp) ::  de_dx_int_l        !< x/y-interpolated TKE gradient (x) at particle position at lower vertical level
3169    REAL(wp) ::  de_dx_int_u        !< x/y-interpolated TKE gradient (x) at particle position at upper vertical level
3170    REAL(wp) ::  de_dy_int_l        !< x/y-interpolated TKE gradient (y) at particle position at lower vertical level
3171    REAL(wp) ::  de_dy_int_u        !< x/y-interpolated TKE gradient (y) at particle position at upper vertical level
3172    REAL(wp) ::  de_dt              !< temporal derivative of TKE experienced by the particle
3173    REAL(wp) ::  de_dt_min          !< lower level for temporal TKE derivative
3174    REAL(wp) ::  de_dz_int_l        !< x/y-interpolated TKE gradient (z) at particle position at lower vertical level
3175    REAL(wp) ::  de_dz_int_u        !< x/y-interpolated TKE gradient (z) at particle position at upper vertical level
3176    REAL(wp) ::  diameter           !< diamter of droplet
3177    REAL(wp) ::  diss_int_l         !< x/y-interpolated dissipation at particle position at lower vertical level
3178    REAL(wp) ::  diss_int_u         !< x/y-interpolated dissipation at particle position at upper vertical level
3179    REAL(wp) ::  dt_particle_m      !< previous particle time step
3180    REAL(wp) ::  dz_temp            !< dummy for the vertical grid spacing
3181    REAL(wp) ::  e_int_l            !< x/y-interpolated TKE at particle position at lower vertical level
3182    REAL(wp) ::  e_int_u            !< x/y-interpolated TKE at particle position at upper vertical level
3183    REAL(wp) ::  e_mean_int         !< horizontal mean TKE at particle height
3184    REAL(wp) ::  exp_arg            !< argument in the exponent - particle radius
3185    REAL(wp) ::  exp_term           !< exponent term
3186    REAL(wp) ::  gamma              !< interpolation facor for z-direction
3187    REAL(wp) ::  gg                 !< dummy argument for horizontal particle interpolation
3188    REAL(wp) ::  height_p           !< dummy argument for logarithmic interpolation
3189    REAL(wp) ::  log_z_z0_int       !< logarithmus used for surface_layer interpolation
3190    REAL(wp) ::  random_gauss       !< Gaussian-distributed random number used for SGS particle advection
3191    REAL(wp) ::  RL                 !< Lagrangian autocorrelation coefficient
3192    REAL(wp) ::  rg1                !< Gaussian distributed random number
3193    REAL(wp) ::  rg2                !< Gaussian distributed random number
3194    REAL(wp) ::  rg3                !< Gaussian distributed random number
3195    REAL(wp) ::  sigma              !< velocity standard deviation
3196    REAL(wp) ::  u_int_l            !< x/y-interpolated u-component at particle position at lower vertical level
3197    REAL(wp) ::  u_int_u            !< x/y-interpolated u-component at particle position at upper vertical level
3198    REAL(wp) ::  unext              !< calculated particle u-velocity of corrector step
3199    REAL(wp) ::  us_int             !< friction velocity at particle grid box
3200    REAL(wp) ::  usws_int           !< surface momentum flux (u component) at particle grid box
3201    REAL(wp) ::  v_int_l            !< x/y-interpolated v-component at particle position at lower vertical level
3202    REAL(wp) ::  v_int_u            !< x/y-interpolated v-component at particle position at upper vertical level
3203    REAL(wp) ::  vsws_int           !< surface momentum flux (u component) at particle grid box
3204    REAL(wp) ::  vnext              !< calculated particle v-velocity of corrector step
3205    REAL(wp) ::  vv_int             !< dummy to compute interpolated mean SGS TKE, used to scale SGS advection
3206    REAL(wp) ::  w_int_l            !< x/y-interpolated w-component at particle position at lower vertical level
3207    REAL(wp) ::  w_int_u            !< x/y-interpolated w-component at particle position at upper vertical level
3208    REAL(wp) ::  wnext              !< calculated particle w-velocity of corrector step
3209    REAL(wp) ::  w_s                !< terminal velocity of droplets
3210    REAL(wp) ::  x                  !< dummy argument for horizontal particle interpolation
3211    REAL(wp) ::  xp                 !< calculated particle position in x of predictor step
3212    REAL(wp) ::  y                  !< dummy argument for horizontal particle interpolation
3213    REAL(wp) ::  yp                 !< calculated particle position in y of predictor step
3214    REAL(wp) ::  z_p                !< surface layer height (0.5 dz)
3215    REAL(wp) ::  zp                 !< calculated particle position in z of predictor step
3216
3217    REAL(wp), PARAMETER ::  a_rog = 9.65_wp      !< parameter for fall velocity
3218    REAL(wp), PARAMETER ::  b_rog = 10.43_wp     !< parameter for fall velocity
3219    REAL(wp), PARAMETER ::  c_rog = 0.6_wp       !< parameter for fall velocity
3220    REAL(wp), PARAMETER ::  k_cap_rog = 4.0_wp   !< parameter for fall velocity
3221    REAL(wp), PARAMETER ::  k_low_rog = 12.0_wp  !< parameter for fall velocity
3222    REAL(wp), PARAMETER ::  d0_rog = 0.745_wp    !< separation diameter
3223
3224    REAL(wp), DIMENSION(number_of_particles) ::  term_1_2       !< flag to communicate whether a particle is near topography or not
3225    REAL(wp), DIMENSION(number_of_particles) ::  dens_ratio     !< ratio between the density of the fluid and the density of the particles
3226    REAL(wp), DIMENSION(number_of_particles) ::  de_dx_int      !< horizontal TKE gradient along x at particle position
3227    REAL(wp), DIMENSION(number_of_particles) ::  de_dy_int      !< horizontal TKE gradient along y at particle position
3228    REAL(wp), DIMENSION(number_of_particles) ::  de_dz_int      !< horizontal TKE gradient along z at particle position
3229    REAL(wp), DIMENSION(number_of_particles) ::  diss_int       !< dissipation at particle position
3230    REAL(wp), DIMENSION(number_of_particles) ::  dt_gap         !< remaining time until particle time integration reaches LES time
3231    REAL(wp), DIMENSION(number_of_particles) ::  dt_particle    !< particle time step
3232    REAL(wp), DIMENSION(number_of_particles) ::  e_int          !< TKE at particle position
3233    REAL(wp), DIMENSION(number_of_particles) ::  fs_int         !< weighting factor for subgrid-scale particle speed
3234    REAL(wp), DIMENSION(number_of_particles) ::  lagr_timescale !< Lagrangian timescale
3235    REAL(wp), DIMENSION(number_of_particles) ::  rvar1_temp     !< SGS particle velocity - u-component
3236    REAL(wp), DIMENSION(number_of_particles) ::  rvar2_temp     !< SGS particle velocity - v-component
3237    REAL(wp), DIMENSION(number_of_particles) ::  rvar3_temp     !< SGS particle velocity - w-component
3238    REAL(wp), DIMENSION(number_of_particles) ::  u_int          !< u-component of particle speed
3239    REAL(wp), DIMENSION(number_of_particles) ::  v_int          !< v-component of particle speed
3240    REAL(wp), DIMENSION(number_of_particles) ::  w_int          !< w-component of particle speed
3241    REAL(wp), DIMENSION(number_of_particles) ::  xv             !< x-position
3242    REAL(wp), DIMENSION(number_of_particles) ::  yv             !< y-position
3243    REAL(wp), DIMENSION(number_of_particles) ::  zv             !< z-position
3244
3245    REAL(wp), DIMENSION(number_of_particles, 3) ::  rg !< vector of Gaussian distributed random numbers
3246
3247    CALL cpu_log( log_point_s(44), 'lpm_advec', 'continue' )
3248!
3249!-- Determine height of Prandtl layer and distance between Prandtl-layer
3250!-- height and horizontal mean roughness height, which are required for
3251!-- vertical logarithmic interpolation of horizontal particle speeds
3252!-- (for particles below first vertical grid level).
3253    z_p      = zu(nzb+1) - zw(nzb)
3254    d_z_p_z0 = 1.0_wp / ( z_p - z0_av_global )
3255
3256    xv = particles(1:number_of_particles)%x
3257    yv = particles(1:number_of_particles)%y
3258    zv = particles(1:number_of_particles)%z
3259    dt_particle = dt_3d
3260
3261!
3262!-- This case uses a simple interpolation method for the particle velocites,
3263!-- and applying a predictor-corrector method. @attention: for the corrector
3264!-- step the velocities of t(n+1) are required. However, at this moment of
3265!-- the time integration they are not free of divergence. This interpolation
3266!-- method is described in more detail in Grabowski et al., 2018 (GMD).
3267    IF ( interpolation_simple_corrector )  THEN
3268!
3269!--    Predictor step
3270       kkw = kp - 1
3271       DO  n = 1, number_of_particles
3272
3273          alpha = MAX( MIN( ( particles(n)%x - ip * dx ) * ddx, 1.0_wp ), 0.0_wp )
3274          u_int(n) = u(kp,jp,ip) * ( 1.0_wp - alpha ) + u(kp,jp,ip+1) * alpha
3275
3276          beta  = MAX( MIN( ( particles(n)%y - jp * dy ) * ddy, 1.0_wp ), 0.0_wp )
3277          v_int(n) = v(kp,jp,ip) * ( 1.0_wp - beta ) + v(kp,jp+1,ip) * beta
3278
3279          gamma = MAX( MIN( ( particles(n)%z - zw(kkw) ) /                   &
3280                            ( zw(kkw+1) - zw(kkw) ), 1.0_wp ), 0.0_wp )
3281          w_int(n) = w(kkw,jp,ip) * ( 1.0_wp - gamma ) + w(kkw+1,jp,ip) * gamma
3282
3283       ENDDO
3284!
3285!--    Corrector step
3286       DO  n = 1, number_of_particles
3287
3288          IF ( .NOT. particles(n)%particle_mask )  CYCLE
3289
3290          DO  nn = 1, iteration_steps
3291
3292!
3293!--          Guess new position
3294             xp = particles(n)%x + u_int(n) * dt_particle(n)
3295             yp = particles(n)%y + v_int(n) * dt_particle(n)
3296             zp = particles(n)%z + w_int(n) * dt_particle(n)
3297!
3298!--          x direction
3299             i_next = FLOOR( xp * ddx , KIND=iwp)
3300             alpha  = MAX( MIN( ( xp - i_next * dx ) * ddx, 1.0_wp ), 0.0_wp )
3301!
3302!--          y direction
3303             j_next = FLOOR( yp * ddy )
3304             beta   = MAX( MIN( ( yp - j_next * dy ) * ddy, 1.0_wp ), 0.0_wp )
3305!
3306!--          z_direction
3307             k_next = MAX( MIN( FLOOR( zp / (zw(kkw+1)-zw(kkw)) + offset_ocean_nzt ), nzt ), 0)
3308             gamma = MAX( MIN( ( zp - zw(k_next) ) /                      &
3309                               ( zw(k_next+1) - zw(k_next) ), 1.0_wp ), 0.0_wp )
3310!
3311!--          Calculate part of the corrector step
3312             unext = u_p(k_next+1, j_next, i_next) * ( 1.0_wp - alpha ) +    &
3313                     u_p(k_next+1, j_next,   i_next+1) * alpha
3314
3315             vnext = v_p(k_next+1, j_next, i_next) * ( 1.0_wp - beta  ) +    &
3316                     v_p(k_next+1, j_next+1, i_next  ) * beta
3317
3318             wnext = w_p(k_next,   j_next, i_next) * ( 1.0_wp - gamma ) +    &
3319                     w_p(k_next+1, j_next, i_next  ) * gamma
3320
3321!
3322!--          Calculate interpolated particle velocity with predictor
3323!--          corrector step. u_int, v_int and w_int describes the part of
3324!--          the predictor step. unext, vnext and wnext is the part of the
3325!--          corrector step. The resulting new position is set below. The
3326!--          implementation is based on Grabowski et al., 2018 (GMD).
3327             u_int(n) = 0.5_wp * ( u_int(n) + unext )
3328             v_int(n) = 0.5_wp * ( v_int(n) + vnext )
3329             w_int(n) = 0.5_wp * ( w_int(n) + wnext )
3330
3331          ENDDO
3332       ENDDO
3333!
3334!-- This case uses a simple interpolation method for the particle velocites,
3335!-- and applying a predictor.
3336    ELSEIF ( interpolation_simple_predictor )  THEN
3337!
3338!--    The particle position for the w velociy is based on the value of kp and kp-1
3339       kkw = kp - 1
3340       DO  n = 1, number_of_particles
3341          IF ( .NOT. particles(n)%particle_mask )  CYCLE
3342
3343          alpha    = MAX( MIN( ( particles(n)%x - ip * dx ) * ddx, 1.0_wp ), 0.0_wp )
3344          u_int(n) = u(kp,jp,ip) * ( 1.0_wp - alpha ) + u(kp,jp,ip+1) * alpha
3345
3346          beta     = MAX( MIN( ( particles(n)%y - jp * dy ) * ddy, 1.0_wp ), 0.0_wp )
3347          v_int(n) = v(kp,jp,ip) * ( 1.0_wp - beta ) + v(kp,jp+1,ip) * beta
3348
3349          gamma    = MAX( MIN( ( particles(n)%z - zw(kkw) ) /                   &
3350                               ( zw(kkw+1) - zw(kkw) ), 1.0_wp ), 0.0_wp )
3351          w_int(n) = w(kkw,jp,ip) * ( 1.0_wp - gamma ) + w(kkw+1,jp,ip) * gamma
3352       ENDDO
3353!
3354!-- The trilinear interpolation.
3355    ELSEIF ( interpolation_trilinear )  THEN
3356
3357       start_index = grid_particles(kp,jp,ip)%start_index
3358       end_index   = grid_particles(kp,jp,ip)%end_index
3359
3360       DO  nb = 0, 7
3361!
3362!--       Interpolate u velocity-component
3363          i = ip
3364          j = jp + block_offset(nb)%j_off
3365          k = kp + block_offset(nb)%k_off
3366
3367          DO  n = start_index(nb), end_index(nb)
3368!
3369!--          Interpolation of the u velocity component onto particle position.
3370!--          Particles are interpolation bi-linearly in the horizontal and a
3371!--          linearly in the vertical. An exception is made for particles below
3372!--          the first vertical grid level in case of a prandtl layer. In this
3373!--          case the horizontal particle velocity components are determined using
3374!--          Monin-Obukhov relations (if branch).
3375!--          First, check if particle is located below first vertical grid level
3376!--          above topography (Prandtl-layer height)
3377!--          Determine vertical index of topography top
3378             k_wall = topo_top_ind(jp,ip,0)
3379
3380             IF ( constant_flux_layer  .AND.  zv(n) - zw(k_wall) < z_p )  THEN
3381!
3382!--             Resolved-scale horizontal particle velocity is zero below z0.
3383                IF ( zv(n) - zw(k_wall) < z0_av_global )  THEN
3384                   u_int(n) = 0.0_wp
3385                ELSE
3386!
3387!--                Determine the sublayer. Further used as index.
3388                   height_p = ( zv(n) - zw(k_wall) - z0_av_global ) &
3389                                        * REAL( number_of_sublayers, KIND=wp )    &
3390                                        * d_z_p_z0
3391!
3392!--                Calculate LOG(z/z0) for exact particle height. Therefore,
3393!--                interpolate linearly between precalculated logarithm.
3394                   log_z_z0_int = log_z_z0(INT(height_p))                         &
3395                                    + ( height_p - INT(height_p) )                &
3396                                    * ( log_z_z0(INT(height_p)+1)                 &
3397                                         - log_z_z0(INT(height_p))                &
3398                                      )
3399!
3400!--                Get friction velocity and momentum flux from new surface data
3401!--                types.
3402                   IF ( surf_def_h(0)%start_index(jp,ip) <=                   &
3403                        surf_def_h(0)%end_index(jp,ip) )  THEN
3404                      surf_start = surf_def_h(0)%start_index(jp,ip)
3405!--                   Limit friction velocity. In narrow canyons or holes the
3406!--                   friction velocity can become very small, resulting in a too
3407!--                   large particle speed.
3408                      us_int    = MAX( surf_def_h(0)%us(surf_start), 0.01_wp )
3409                      usws_int  = surf_def_h(0)%usws(surf_start)
3410                   ELSEIF ( surf_lsm_h%start_index(jp,ip) <=                  &
3411                            surf_lsm_h%end_index(jp,ip) )  THEN
3412                      surf_start = surf_lsm_h%start_index(jp,ip)
3413                      us_int    = MAX( surf_lsm_h%us(surf_start), 0.01_wp )
3414                      usws_int  = surf_lsm_h%usws(surf_start)
3415                   ELSEIF ( surf_usm_h%start_index(jp,ip) <=                  &
3416                            surf_usm_h%end_index(jp,ip) )  THEN
3417                      surf_start = surf_usm_h%start_index(jp,ip)
3418                      us_int    = MAX( surf_usm_h%us(surf_start), 0.01_wp )
3419                      usws_int  = surf_usm_h%usws(surf_start)
3420                   ENDIF
3421!
3422!--                Neutral solution is applied for all situations, e.g. also for
3423!--                unstable and stable situations. Even though this is not exact
3424!--                this saves a lot of CPU time since several calls of intrinsic
3425!--                FORTRAN procedures (LOG, ATAN) are avoided, This is justified
3426!--                as sensitivity studies revealed no significant effect of
3427!--                using the neutral solution also for un/stable situations.
3428                   u_int(n) = -usws_int / ( us_int * kappa + 1E-10_wp )           &
3429                               * log_z_z0_int - u_gtrans
3430                ENDIF
3431!
3432!--          Particle above the first grid level. Bi-linear interpolation in the
3433!--          horizontal and linear interpolation in the vertical direction.
3434             ELSE
3435                x  = xv(n) - i * dx
3436                y  = yv(n) + ( 0.5_wp - j ) * dy
3437                aa = x**2          + y**2
3438                bb = ( dx - x )**2 + y**2
3439                cc = x**2          + ( dy - y )**2
3440                dd = ( dx - x )**2 + ( dy - y )**2
3441                gg = aa + bb + cc + dd
3442
3443                u_int_l = ( ( gg - aa ) * u(k,j,i)   + ( gg - bb ) * u(k,j,i+1)   &
3444                            + ( gg - cc ) * u(k,j+1,i) + ( gg - dd ) *            &
3445                            u(k,j+1,i+1) ) / ( 3.0_wp * gg ) - u_gtrans
3446
3447                IF ( k == nzt )  THEN
3448                   u_int(n) = u_int_l
3449                ELSE
3450                   u_int_u = ( ( gg-aa ) * u(k+1,j,i) + ( gg-bb ) * u(k+1,j,i+1)  &
3451                               + ( gg-cc ) * u(k+1,j+1,i) + ( gg-dd ) *           &
3452                               u(k+1,j+1,i+1) ) / ( 3.0_wp * gg ) - u_gtrans
3453                   u_int(n) = u_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *            &
3454                              ( u_int_u - u_int_l )
3455                ENDIF
3456             ENDIF
3457          ENDDO
3458!
3459!--       Same procedure for interpolation of the v velocity-component
3460          i = ip + block_offset(nb)%i_off
3461          j = jp
3462          k = kp + block_offset(nb)%k_off
3463
3464          DO  n = start_index(nb), end_index(nb)
3465!
3466!--          Determine vertical index of topography top
3467             k_wall = topo_top_ind(jp,ip,0)
3468
3469             IF ( constant_flux_layer  .AND.  zv(n) - zw(k_wall) < z_p )  THEN
3470                IF ( zv(n) - zw(k_wall) < z0_av_global )  THEN
3471!
3472!--                Resolved-scale horizontal particle velocity is zero below z0.
3473                   v_int(n) = 0.0_wp
3474                ELSE
3475!
3476!--                Determine the sublayer. Further used as index. Please note,
3477!--                logarithmus can not be reused from above, as in in case of
3478!--                topography particle on u-grid can be above surface-layer height,
3479!--                whereas it can be below on v-grid.
3480                   height_p = ( zv(n) - zw(k_wall) - z0_av_global ) &
3481                                     * REAL( number_of_sublayers, KIND=wp )       &
3482                                     * d_z_p_z0
3483!
3484!--                Calculate LOG(z/z0) for exact particle height. Therefore,
3485!--                interpolate linearly between precalculated logarithm.
3486                   log_z_z0_int = log_z_z0(INT(height_p))                         &
3487                                    + ( height_p - INT(height_p) )                &
3488                                    * ( log_z_z0(INT(height_p)+1)                 &
3489                                         - log_z_z0(INT(height_p))                &
3490                                      )
3491!
3492!--                Get friction velocity and momentum flux from new surface data
3493!--                types.
3494                   IF ( surf_def_h(0)%start_index(jp,ip) <=                   &
3495                        surf_def_h(0)%end_index(jp,ip) )  THEN
3496                      surf_start = surf_def_h(0)%start_index(jp,ip)
3497!--                   Limit friction velocity. In narrow canyons or holes the
3498!--                   friction velocity can become very small, resulting in a too
3499!--                   large particle speed.
3500                      us_int    = MAX( surf_def_h(0)%us(surf_start), 0.01_wp )
3501                      vsws_int  = surf_def_h(0)%vsws(surf_start)
3502                   ELSEIF ( surf_lsm_h%start_index(jp,ip) <=                  &
3503                            surf_lsm_h%end_index(jp,ip) )  THEN
3504                      surf_start = surf_lsm_h%start_index(jp,ip)
3505                      us_int    = MAX( surf_lsm_h%us(surf_start), 0.01_wp )
3506                      vsws_int  = surf_lsm_h%vsws(surf_start)
3507                   ELSEIF ( surf_usm_h%start_index(jp,ip) <=                  &
3508                            surf_usm_h%end_index(jp,ip) )  THEN
3509                      surf_start = surf_usm_h%start_index(jp,ip)
3510                      us_int    = MAX( surf_usm_h%us(surf_start), 0.01_wp )
3511                      vsws_int  = surf_usm_h%vsws(surf_start)
3512                   ENDIF
3513!
3514!--                Neutral solution is applied for all situations, e.g. also for
3515!--                unstable and stable situations. Even though this is not exact
3516!--                this saves a lot of CPU time since several calls of intrinsic
3517!--                FORTRAN procedures (LOG, ATAN) are avoided, This is justified
3518!--                as sensitivity studies revealed no significant effect of
3519!--                using the neutral solution also for un/stable situations.
3520                   v_int(n) = -vsws_int / ( us_int * kappa + 1E-10_wp )           &
3521                            * log_z_z0_int - v_gtrans
3522
3523                ENDIF
3524             ELSE
3525                x  = xv(n) + ( 0.5_wp - i ) * dx
3526                y  = yv(n) - j * dy
3527                aa = x**2          + y**2
3528                bb = ( dx - x )**2 + y**2
3529                cc = x**2          + ( dy - y )**2
3530                dd = ( dx - x )**2 + ( dy - y )**2
3531                gg = aa + bb + cc + dd
3532
3533                v_int_l = ( ( gg - aa ) * v(k,j,i)   + ( gg - bb ) * v(k,j,i+1)   &
3534                          + ( gg - cc ) * v(k,j+1,i) + ( gg - dd ) * v(k,j+1,i+1) &
3535                          ) / ( 3.0_wp * gg ) - v_gtrans
3536
3537                IF ( k == nzt )  THEN
3538                   v_int(n) = v_int_l
3539                ELSE
3540                   v_int_u = ( ( gg-aa ) * v(k+1,j,i)   + ( gg-bb ) * v(k+1,j,i+1)   &
3541                             + ( gg-cc ) * v(k+1,j+1,i) + ( gg-dd ) * v(k+1,j+1,i+1) &
3542                             ) / ( 3.0_wp * gg ) - v_gtrans
3543                   v_int(n) = v_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *               &
3544                                     ( v_int_u - v_int_l )
3545                ENDIF
3546             ENDIF
3547          ENDDO
3548!
3549!--       Same procedure for interpolation of the w velocity-component
3550          i = ip + block_offset(nb)%i_off
3551          j = jp + block_offset(nb)%j_off
3552          k = kp - 1
3553
3554          DO  n = start_index(nb), end_index(nb)
3555             IF ( vertical_particle_advection(particles(n)%group) )  THEN
3556                x  = xv(n) + ( 0.5_wp - i ) * dx
3557                y  = yv(n) + ( 0.5_wp - j ) * dy
3558                aa = x**2          + y**2
3559                bb = ( dx - x )**2 + y**2
3560                cc = x**2          + ( dy - y )**2
3561                dd = ( dx - x )**2 + ( dy - y )**2
3562                gg = aa + bb + cc + dd
3563
3564                w_int_l = ( ( gg - aa ) * w(k,j,i)   + ( gg - bb ) * w(k,j,i+1)   &
3565                          + ( gg - cc ) * w(k,j+1,i) + ( gg - dd ) * w(k,j+1,i+1) &
3566                          ) / ( 3.0_wp * gg )
3567
3568                IF ( k == nzt )  THEN
3569                   w_int(n) = w_int_l
3570                ELSE
3571                   w_int_u = ( ( gg-aa ) * w(k+1,j,i)   + &
3572                               ( gg-bb ) * w(k+1,j,i+1) + &
3573                               ( gg-cc ) * w(k+1,j+1,i) + &
3574                               ( gg-dd ) * w(k+1,j+1,i+1) &
3575                             ) / ( 3.0_wp * gg )
3576                   w_int(n) = w_int_l + ( zv(n) - zw(k) ) / dzw(k+1) *               &
3577                              ( w_int_u - w_int_l )
3578                ENDIF
3579             ELSE
3580                w_int(n) = 0.0_wp
3581             ENDIF
3582          ENDDO
3583       ENDDO
3584    ENDIF
3585
3586!-- Interpolate and calculate quantities needed for calculating the SGS
3587!-- velocities
3588    IF ( use_sgs_for_particles  .AND.  .NOT. cloud_droplets )  THEN
3589
3590       DO  nb = 0,7
3591
3592          subbox_at_wall = .FALSE.
3593!
3594!--       In case of topography check if subbox is adjacent to a wall
3595          IF ( .NOT. topography == 'flat' )  THEN
3596             i = ip + MERGE( -1_iwp , 1_iwp, BTEST( nb, 2 ) )
3597             j = jp + MERGE( -1_iwp , 1_iwp, BTEST( nb, 1 ) )
3598             k = kp + MERGE( -1_iwp , 1_iwp, BTEST( nb, 0 ) )
3599             IF ( .NOT. BTEST(wall_flags_0(k,  jp, ip), 0) .OR.                &
3600                  .NOT. BTEST(wall_flags_0(kp, j,  ip), 0) .OR.                &
3601                  .NOT. BTEST(wall_flags_0(kp, jp, i ), 0) )                   &
3602             THEN
3603                subbox_at_wall = .TRUE.
3604             ENDIF
3605          ENDIF
3606          IF ( subbox_at_wall )  THEN
3607             e_int(start_index(nb):end_index(nb))     = e(kp,jp,ip) 
3608             diss_int(start_index(nb):end_index(nb))  = diss(kp,jp,ip)
3609             de_dx_int(start_index(nb):end_index(nb)) = de_dx(kp,jp,ip)
3610             de_dy_int(start_index(nb):end_index(nb)) = de_dy(kp,jp,ip)
3611             de_dz_int(start_index(nb):end_index(nb)) = de_dz(kp,jp,ip)
3612!
3613!--          Set flag for stochastic equation.
3614             term_1_2(start_index(nb):end_index(nb)) = 0.0_wp
3615          ELSE
3616             i = ip + block_offset(nb)%i_off
3617             j = jp + block_offset(nb)%j_off
3618             k = kp + block_offset(nb)%k_off
3619
3620             DO  n = start_index(nb), end_index(nb)
3621!
3622!--             Interpolate TKE
3623                x  = xv(n) + ( 0.5_wp - i ) * dx
3624                y  = yv(n) + ( 0.5_wp - j ) * dy
3625                aa = x**2          + y**2
3626                bb = ( dx - x )**2 + y**2
3627                cc = x**2          + ( dy - y )**2
3628                dd = ( dx - x )**2 + ( dy - y )**2
3629                gg = aa + bb + cc + dd
3630
3631                e_int_l = ( ( gg-aa ) * e(k,j,i)   + ( gg-bb ) * e(k,j,i+1)   &
3632                          + ( gg-cc ) * e(k,j+1,i) + ( gg-dd ) * e(k,j+1,i+1) &
3633                          ) / ( 3.0_wp * gg )
3634
3635                IF ( k+1 == nzt+1 )  THEN
3636                   e_int(n) = e_int_l
3637                ELSE
3638                   e_int_u = ( ( gg - aa ) * e(k+1,j,i)   + &
3639                               ( gg - bb ) * e(k+1,j,i+1) + &
3640                               ( gg - cc ) * e(k+1,j+1,i) + &
3641                               ( gg - dd ) * e(k+1,j+1,i+1) &
3642                            ) / ( 3.0_wp * gg )
3643                   e_int(n) = e_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *            &
3644                                     ( e_int_u - e_int_l )
3645                ENDIF
3646!
3647!--             Needed to avoid NaN particle velocities (this might not be
3648!--             required any more)
3649                IF ( e_int(n) <= 0.0_wp )  THEN
3650                   e_int(n) = 1.0E-20_wp
3651                ENDIF
3652!
3653!--             Interpolate the TKE gradient along x (adopt incides i,j,k and
3654!--             all position variables from above (TKE))
3655                de_dx_int_l = ( ( gg - aa ) * de_dx(k,j,i)   + &
3656                                ( gg - bb ) * de_dx(k,j,i+1) + &
3657                                ( gg - cc ) * de_dx(k,j+1,i) + &
3658                                ( gg - dd ) * de_dx(k,j+1,i+1) &
3659                               ) / ( 3.0_wp * gg )
3660
3661                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
3662                   de_dx_int(n) = de_dx_int_l
3663                ELSE
3664                   de_dx_int_u = ( ( gg - aa ) * de_dx(k+1,j,i)   + &
3665                                   ( gg - bb ) * de_dx(k+1,j,i+1) + &
3666                                   ( gg - cc ) * de_dx(k+1,j+1,i) + &
3667                                   ( gg - dd ) * de_dx(k+1,j+1,i+1) &
3668                                  ) / ( 3.0_wp * gg )
3669                   de_dx_int(n) = de_dx_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *    &
3670                                              ( de_dx_int_u - de_dx_int_l )
3671                ENDIF
3672!
3673!--             Interpolate the TKE gradient along y
3674                de_dy_int_l = ( ( gg - aa ) * de_dy(k,j,i)   + &
3675                                ( gg - bb ) * de_dy(k,j,i+1) + &
3676                                ( gg - cc ) * de_dy(k,j+1,i) + &
3677                                ( gg - dd ) * de_dy(k,j+1,i+1) &
3678                               ) / ( 3.0_wp * gg )
3679                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
3680                   de_dy_int(n) = de_dy_int_l
3681                ELSE
3682                   de_dy_int_u = ( ( gg - aa ) * de_dy(k+1,j,i)   + &
3683                                   ( gg - bb ) * de_dy(k+1,j,i+1) + &
3684                                   ( gg - cc ) * de_dy(k+1,j+1,i) + &
3685                                   ( gg - dd ) * de_dy(k+1,j+1,i+1) &
3686                                  ) / ( 3.0_wp * gg )
3687                      de_dy_int(n) = de_dy_int_l + ( zv(n) - zu(k) ) / dzw(k+1) * &
3688                                                 ( de_dy_int_u - de_dy_int_l )
3689                ENDIF
3690
3691!
3692!--             Interpolate the TKE gradient along z
3693                IF ( zv(n) < 0.5_wp * dz(1) )  THEN
3694                   de_dz_int(n) = 0.0_wp
3695                ELSE
3696                   de_dz_int_l = ( ( gg - aa ) * de_dz(k,j,i)   + &
3697                                   ( gg - bb ) * de_dz(k,j,i+1) + &
3698                                   ( gg - cc ) * de_dz(k,j+1,i) + &
3699                                   ( gg - dd ) * de_dz(k,j+1,i+1) &
3700                                  ) / ( 3.0_wp * gg )
3701
3702                   IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
3703                      de_dz_int(n) = de_dz_int_l
3704                   ELSE
3705                      de_dz_int_u = ( ( gg - aa ) * de_dz(k+1,j,i)   + &
3706                                      ( gg - bb ) * de_dz(k+1,j,i+1) + &
3707                                      ( gg - cc ) * de_dz(k+1,j+1,i) + &
3708                                      ( gg - dd ) * de_dz(k+1,j+1,i+1) &
3709                                     ) / ( 3.0_wp * gg )
3710                      de_dz_int(n) = de_dz_int_l + ( zv(n) - zu(k) ) / dzw(k+1) * &
3711                                                 ( de_dz_int_u - de_dz_int_l )
3712                   ENDIF
3713                ENDIF
3714
3715!
3716!--             Interpolate the dissipation of TKE
3717                diss_int_l = ( ( gg - aa ) * diss(k,j,i)   + &
3718                               ( gg - bb ) * diss(k,j,i+1) + &
3719                               ( gg - cc ) * diss(k,j+1,i) + &
3720                               ( gg - dd ) * diss(k,j+1,i+1) &
3721                               ) / ( 3.0_wp * gg )
3722
3723                IF ( k == nzt )  THEN
3724                   diss_int(n) = diss_int_l
3725                ELSE
3726                   diss_int_u = ( ( gg - aa ) * diss(k+1,j,i)   + &
3727                                  ( gg - bb ) * diss(k+1,j,i+1) + &
3728                                  ( gg - cc ) * diss(k+1,j+1,i) + &
3729                                  ( gg - dd ) * diss(k+1,j+1,i+1) &
3730                                 ) / ( 3.0_wp * gg )
3731                   diss_int(n) = diss_int_l + ( zv(n) - zu(k) ) / dzw(k+1) *      &
3732                                            ( diss_int_u - diss_int_l )
3733                ENDIF
3734
3735!
3736!--             Set flag for stochastic equation.
3737                term_1_2(n) = 1.0_wp
3738             ENDDO
3739          ENDIF
3740       ENDDO
3741
3742       DO  nb = 0,7
3743          i = ip + block_offset(nb)%i_off
3744          j = jp + block_offset(nb)%j_off
3745          k = kp + block_offset(nb)%k_off
3746
3747          DO  n = start_index(nb), end_index(nb)
3748!
3749!--          Vertical interpolation of the horizontally averaged SGS TKE and
3750!--          resolved-scale velocity variances and use the interpolated values
3751!--          to calculate the coefficient fs, which is a measure of the ratio
3752!--          of the subgrid-scale turbulent kinetic energy to the total amount
3753!--          of turbulent kinetic energy.
3754             IF ( k == 0 )  THEN
3755                e_mean_int = hom(0,1,8,0)
3756             ELSE
3757                e_mean_int = hom(k,1,8,0) +                                    &
3758                                           ( hom(k+1,1,8,0) - hom(k,1,8,0) ) / &
3759                                           ( zu(k+1) - zu(k) ) *               &
3760                                           ( zv(n) - zu(k) )
3761             ENDIF
3762
3763             kw = kp - 1
3764
3765             IF ( k == 0 )  THEN
3766                aa  = hom(k+1,1,30,0)  * ( zv(n) / &
3767                                         ( 0.5_wp * ( zu(k+1) - zu(k) ) ) )
3768                bb  = hom(k+1,1,31,0)  * ( zv(n) / &
3769                                         ( 0.5_wp * ( zu(k+1) - zu(k) ) ) )
3770                cc  = hom(kw+1,1,32,0) * ( zv(n) / &
3771                                         ( 1.0_wp * ( zw(kw+1) - zw(kw) ) ) )
3772             ELSE
3773                aa  = hom(k,1,30,0) + ( hom(k+1,1,30,0) - hom(k,1,30,0) ) *    &
3774                           ( ( zv(n) - zu(k) ) / ( zu(k+1) - zu(k) ) )
3775                bb  = hom(k,1,31,0) + ( hom(k+1,1,31,0) - hom(k,1,31,0) ) *    &
3776                           ( ( zv(n) - zu(k) ) / ( zu(k+1) - zu(k) ) )
3777                cc  = hom(kw,1,32,0) + ( hom(kw+1,1,32,0)-hom(kw,1,32,0) ) *   &
3778                           ( ( zv(n) - zw(kw) ) / ( zw(kw+1)-zw(kw) ) )
3779             ENDIF
3780
3781             vv_int = ( 1.0_wp / 3.0_wp ) * ( aa + bb + cc )
3782!
3783!--          Needed to avoid NaN particle velocities. The value of 1.0 is just
3784!--          an educated guess for the given case.
3785             IF ( vv_int + ( 2.0_wp / 3.0_wp ) * e_mean_int == 0.0_wp )  THEN
3786                fs_int(n) = 1.0_wp
3787             ELSE
3788                fs_int(n) = ( 2.0_wp / 3.0_wp ) * e_mean_int /                 &
3789                            ( vv_int + ( 2.0_wp / 3.0_wp ) * e_mean_int )
3790             ENDIF
3791
3792          ENDDO
3793       ENDDO
3794
3795       DO  nb = 0, 7
3796          DO  n = start_index(nb), end_index(nb)
3797             rg(n,1) = random_gauss( iran_part, 5.0_wp )
3798             rg(n,2) = random_gauss( iran_part, 5.0_wp )
3799             rg(n,3) = random_gauss( iran_part, 5.0_wp )
3800          ENDDO
3801       ENDDO
3802
3803       DO  nb = 0, 7
3804          DO  n = start_index(nb), end_index(nb)
3805
3806!
3807!--          Calculate the Lagrangian timescale according to Weil et al. (2004).
3808             lagr_timescale(n) = ( 4.0_wp * e_int(n) + 1E-20_wp ) / &
3809                              ( 3.0_wp * fs_int(n) * c_0 * diss_int(n) + 1E-20_wp )
3810
3811!
3812!--          Calculate the next particle timestep. dt_gap is the time needed to
3813!--          complete the current LES timestep.
3814             dt_gap(n) = dt_3d - particles(n)%dt_sum
3815             dt_particle(n) = MIN( dt_3d, 0.025_wp * lagr_timescale(n), dt_gap(n) )
3816             particles(n)%aux1 = lagr_timescale(n)
3817             particles(n)%aux2 = dt_gap(n)
3818!
3819!--          The particle timestep should not be too small in order to prevent
3820!--          the number of particle timesteps of getting too large
3821             IF ( dt_particle(n) < dt_min_part )  THEN
3822                IF ( dt_min_part < dt_gap(n) )  THEN
3823                   dt_particle(n) = dt_min_part
3824                ELSE
3825                   dt_particle(n) = dt_gap(n)
3826                ENDIF
3827             ENDIF
3828             rvar1_temp(n) = particles(n)%rvar1
3829             rvar2_temp(n) = particles(n)%rvar2
3830             rvar3_temp(n) = particles(n)%rvar3
3831!
3832!--          Calculate the SGS velocity components
3833             IF ( particles(n)%age == 0.0_wp )  THEN
3834!
3835!--             For new particles the SGS components are derived from the SGS
3836!--             TKE. Limit the Gaussian random number to the interval
3837!--             [-5.0*sigma, 5.0*sigma] in order to prevent the SGS velocities
3838!--             from becoming unrealistically large.
3839                rvar1_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
3840                                          + 1E-20_wp ) * ( rg(n,1) - 1.0_wp )
3841                rvar2_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
3842                                          + 1E-20_wp ) * ( rg(n,2) - 1.0_wp )
3843                rvar3_temp(n) = SQRT( 2.0_wp * sgs_wf_part * e_int(n)          &
3844                                          + 1E-20_wp ) * ( rg(n,3) - 1.0_wp )
3845
3846             ELSE
3847!
3848!--             Restriction of the size of the new timestep: compared to the
3849!--             previous timestep the increase must not exceed 200%. First,
3850!--             check if age > age_m, in order to prevent that particles get zero
3851!--             timestep.
3852                dt_particle_m = MERGE( dt_particle(n),                         &
3853                                       particles(n)%age - particles(n)%age_m,  &
3854                                       particles(n)%age - particles(n)%age_m < &
3855                                       1E-8_wp )
3856                IF ( dt_particle(n) > 2.0_wp * dt_particle_m )  THEN
3857                   dt_particle(n) = 2.0_wp * dt_particle_m
3858                ENDIF
3859
3860!--             For old particles the SGS components are correlated with the
3861!--             values from the previous timestep. Random numbers have also to
3862!--             be limited (see above).
3863!--             As negative values for the subgrid TKE are not allowed, the
3864!--             change of the subgrid TKE with time cannot be smaller than
3865!--             -e_int(n)/dt_particle. This value is used as a lower boundary
3866!--             value for the change of TKE
3867                de_dt_min = - e_int(n) / dt_particle(n)
3868
3869                de_dt = ( e_int(n) - particles(n)%e_m ) / dt_particle_m
3870
3871                IF ( de_dt < de_dt_min )  THEN
3872                   de_dt = de_dt_min
3873                ENDIF
3874
3875                CALL weil_stochastic_eq( rvar1_temp(n), fs_int(n), e_int(n),    &
3876                                        de_dx_int(n), de_dt, diss_int(n),       &
3877                                        dt_particle(n), rg(n,1), term_1_2(n) )
3878
3879                CALL weil_stochastic_eq( rvar2_temp(n), fs_int(n), e_int(n),    &
3880                                        de_dy_int(n), de_dt, diss_int(n),       &
3881                                        dt_particle(n), rg(n,2), term_1_2(n) )
3882
3883                CALL weil_stochastic_eq( rvar3_temp(n), fs_int(n), e_int(n),    &
3884                                        de_dz_int(n), de_dt, diss_int(n),       &
3885                                        dt_particle(n), rg(n,3), term_1_2(n) )
3886
3887             ENDIF
3888
3889          ENDDO
3890       ENDDO
3891!
3892!--    Check if the added SGS velocities result in a violation of the CFL-
3893!--    criterion. If yes choose a smaller timestep based on the new velocities
3894!--    and calculate SGS velocities again
3895       dz_temp = zw(kp)-zw(kp-1)
3896
3897       DO  nb = 0, 7
3898          DO  n = start_index(nb), end_index(nb)
3899             IF ( .NOT. particles(n)%age == 0.0_wp .AND.                       &
3900                (ABS( u_int(n) + rvar1_temp(n) ) > (dx/dt_particle(n))  .OR.   &
3901                 ABS( v_int(n) + rvar2_temp(n) ) > (dy/dt_particle(n))  .OR.   &
3902                 ABS( w_int(n) + rvar3_temp(n) ) > (dz_temp/dt_particle(n))))  THEN
3903
3904                dt_particle(n) = 0.9_wp * MIN(                                 &
3905                                 ( dx / ABS( u_int(n) + rvar1_temp(n) ) ),     &
3906                                 ( dy / ABS( v_int(n) + rvar2_temp(n) ) ),     &
3907                                 ( dz_temp / ABS( w_int(n) + rvar3_temp(n) ) ) )
3908
3909!
3910!--             Reset temporary SGS velocites to "current" ones
3911                rvar1_temp(n) = particles(n)%rvar1
3912                rvar2_temp(n) = particles(n)%rvar2
3913                rvar3_temp(n) = particles(n)%rvar3
3914
3915                de_dt_min = - e_int(n) / dt_particle(n)
3916
3917                de_dt = ( e_int(n) - particles(n)%e_m ) / dt_particle_m
3918
3919                IF ( de_dt < de_dt_min )  THEN
3920                   de_dt = de_dt_min
3921                ENDIF
3922
3923                CALL weil_stochastic_eq( rvar1_temp(n), fs_int(n), e_int(n),    &
3924                                        de_dx_int(n), de_dt, diss_int(n),       &
3925                                        dt_particle(n), rg(n,1), term_1_2(n) )
3926
3927                CALL weil_stochastic_eq( rvar2_temp(n), fs_int(n), e_int(n),    &
3928                                        de_dy_int(n), de_dt, diss_int(n),       &
3929                                        dt_particle(n), rg(n,2), term_1_2(n) )
3930
3931                CALL weil_stochastic_eq( rvar3_temp(n), fs_int(n), e_int(n),    &
3932                                        de_dz_int(n), de_dt, diss_int(n),       &
3933                                        dt_particle(n), rg(n,3), term_1_2(n) )
3934             ENDIF
3935
3936!
3937!--          Update particle velocites
3938             particles(n)%rvar1 = rvar1_temp(n)
3939             particles(n)%rvar2 = rvar2_temp(n)
3940             particles(n)%rvar3 = rvar3_temp(n)
3941             u_int(n) = u_int(n) + particles(n)%rvar1
3942             v_int(n) = v_int(n) + particles(n)%rvar2
3943             w_int(n) = w_int(n) + particles(n)%rvar3
3944!
3945!--          Store the SGS TKE of the current timelevel which is needed for
3946!--          for calculating the SGS particle velocities at the next timestep
3947             particles(n)%e_m = e_int(n)
3948          ENDDO
3949       ENDDO
3950
3951    ELSE
3952!
3953!--    If no SGS velocities are used, only the particle timestep has to
3954!--    be set
3955       dt_particle = dt_3d
3956
3957    ENDIF
3958
3959    dens_ratio = particle_groups(particles(1:number_of_particles)%group)%density_ratio
3960    IF ( ANY( dens_ratio == 0.0_wp ) )  THEN
3961!
3962!--    Decide whether the particle loop runs over the subboxes or only over 1,
3963!--    number_of_particles. This depends on the selected interpolation method.
3964!--    If particle interpolation method is not trilinear, then the sorting within
3965!--    subboxes is not required. However, therefore the index start_index(nb) and
3966!--    end_index(nb) are not defined and the loops are still over
3967!--    number_of_particles. @todo find a more generic way to write this loop or
3968!--    delete trilinear interpolation
3969       IF ( interpolation_trilinear )  THEN
3970          subbox_start = 0
3971          subbox_end   = 7
3972       ELSE
3973          subbox_start = 1
3974          subbox_end   = 1
3975       ENDIF
3976!
3977!--    loop over subboxes. In case of simple interpolation scheme no subboxes
3978!--    are introduced, as they are not required. Accordingly, this loops goes
3979!--    from 1 to 1.
3980       DO  nb = subbox_start, subbox_end
3981          IF ( interpolation_trilinear )  THEN
3982             particle_start = start_index(nb)
3983             particle_end   = end_index(nb)
3984          ELSE
3985             particle_start = 1
3986             particle_end   = number_of_particles
3987          ENDIF
3988!
3989!--         Loop from particle start to particle end
3990            DO  n = particle_start, particle_end
3991
3992!
3993!--          Particle advection
3994             IF ( dens_ratio(n) == 0.0_wp )  THEN
3995!
3996!--             Pure passive transport (without particle inertia)
3997                particles(n)%x = xv(n) + u_int(n) * dt_particle(n)
3998                particles(n)%y = yv(n) + v_int(n) * dt_particle(n)
3999                particles(n)%z = zv(n) + w_int(n) * dt_particle(n)
4000
4001                particles(n)%speed_x = u_int(n)
4002                particles(n)%speed_y = v_int(n)
4003                particles(n)%speed_z = w_int(n)
4004
4005             ELSE
4006!
4007!--             Transport of particles with inertia
4008                particles(n)%x = particles(n)%x + particles(n)%speed_x * &
4009                                                  dt_particle(n)
4010                particles(n)%y = particles(n)%y + particles(n)%speed_y * &
4011                                                  dt_particle(n)
4012                particles(n)%z = particles(n)%z + particles(n)%speed_z * &
4013                                                  dt_particle(n)
4014
4015!
4016!--             Update of the particle velocity
4017                IF ( cloud_droplets )  THEN
4018!
4019!--                Terminal velocity is computed for vertical direction (Rogers et
4020!--                al., 1993, J. Appl. Meteorol.)
4021                   diameter = particles(n)%radius * 2000.0_wp !diameter in mm
4022                   IF ( diameter <= d0_rog )  THEN
4023                      w_s = k_cap_rog * diameter * ( 1.0_wp - EXP( -k_low_rog * diameter ) )
4024                   ELSE
4025                      w_s = a_rog - b_rog * EXP( -c_rog * diameter )
4026                   ENDIF
4027
4028!
4029!--                If selected, add random velocities following Soelch and Kaercher
4030!--                (2010, Q. J. R. Meteorol. Soc.)
4031                   IF ( use_sgs_for_particles )  THEN
4032                      lagr_timescale(n) = km(kp,jp,ip) / MAX( e(kp,jp,ip), 1.0E-20_wp )
4033                      RL             = EXP( -1.0_wp * dt_3d / MAX( lagr_timescale(n), &
4034                                             1.0E-20_wp ) )
4035                      sigma          = SQRT( e(kp,jp,ip) )
4036
4037                      rg1 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
4038                      rg2 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
4039                      rg3 = random_gauss( iran_part, 5.0_wp ) - 1.0_wp
4040
4041                      particles(n)%rvar1 = RL * particles(n)%rvar1 +              &
4042                                           SQRT( 1.0_wp - RL**2 )