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

Last change on this file since 4329 was 4329, checked in by motisi, 4 years ago

Renamed wall_flags_0 to wall_flags_static_0

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