source: palm/trunk/SOURCE/advec_particles.f90 @ 646

Last change on this file since 646 was 623, checked in by raasch, 13 years ago

last commit documented

  • Property svn:keywords set to Id
File size: 171.2 KB
Line 
1 SUBROUTINE advec_particles
2
3!------------------------------------------------------------------------------!
4! Current revisions:
5! -----------------
6!
7! TEST: PRINT statements on unit 9 (commented out)
8!
9! Former revisions:
10! -----------------
11! $Id: advec_particles.f90 623 2010-12-10 08:52:17Z raasch $
12!
13! 622 2010-12-10 08:08:13Z raasch
14! optional barriers included in order to speed up collective operations
15!
16! 519 2010-03-19 05:30:02Z raasch
17! NetCDF4 output format allows size of particle array to be extended
18!
19! 420 2010-01-13 15:10:53Z franke
20! Own weighting factor for every cloud droplet is implemented and condensation
21! and collision of cloud droplets are adjusted accordingly. +delta_v, -s_r3,
22! -s_r4
23! Initialization of variables for the (sub-) timestep is moved to the beginning
24! in order to enable deletion of cloud droplets due to collision processes.
25! Collision efficiency for large cloud droplets has changed according to table
26! of Rogers and Yau. (collision_efficiency)
27! Bugfix: calculation of cloud droplet velocity
28! Bugfix: transfer of particles at south/left edge when new position
29!         y>=(ny+0.5)*dy-1.e-12 or x>=(nx+0.5)*dx-1.e-12, very rare
30! Bugfix: calculation of y (collision_efficiency)
31!
32! 336 2009-06-10 11:19:35Z raasch
33! Particle attributes are set with new routine set_particle_attributes.
34! Vertical particle advection depends on the particle group.
35! Output of NetCDF messages with aid of message handling routine.
36! Output of messages replaced by message handling routine
37! Bugfix: error in check, if particles moved further than one subdomain length.
38!         This check must not be applied for newly released particles
39! Bugfix: several tail counters are initialized, particle_tail_coordinates is
40! only written to file if its third index is > 0
41!
42! 212 2008-11-11 09:09:24Z raasch
43! Bugfix in calculating k index in case of oceans runs (sort_particles)
44!
45! 150 2008-02-29 08:19:58Z raasch
46! Bottom boundary condition and vertical index calculations adjusted for
47! ocean runs.
48!
49! 119 2007-10-17 10:27:13Z raasch
50! Sorting of particles is controlled by dt_sort_particles and moved from
51! the SGS timestep loop after the end of this loop.
52! Bugfix: pleft/pright changed to pnorth/psouth in sendrecv of particle tail
53! numbers along y
54! Small bugfixes in the SGS part
55!
56! 106 2007-08-16 14:30:26Z raasch
57! remaining variables iran changed to iran_part
58!
59! 95 2007-06-02 16:48:38Z raasch
60! hydro_press renamed hyp
61!
62! 75 2007-03-22 09:54:05Z raasch
63! Particle reflection at vertical walls implemented in new subroutine
64! particle_boundary_conds,
65! vertical walls are regarded in the SGS model,
66! + user_advec_particles, particles-package is now part of the defaut code,
67! array arguments in sendrecv calls have to refer to first element (1) due to
68! mpich (mpiI) interface requirements,
69! 2nd+3rd argument removed from exchange horiz
70!
71! 16 2007-02-15 13:16:47Z raasch
72! Bugfix: wrong if-clause from revision 1.32
73!
74! r4 | raasch | 2007-02-13 12:33:16 +0100 (Tue, 13 Feb 2007)
75! RCS Log replace by Id keyword, revision history cleaned up
76!
77! Revision 1.32  2007/02/11 12:48:20  raasch
78! Allways the lower level k is used for interpolation
79! Bugfix: new particles are released only if end_time_prel > simulated_time
80! Bugfix: transfer of particles when x < -0.5*dx (0.0 before), etc.,
81!         index i,j used instead of cartesian (x,y) coordinate to check for
82!         transfer because this failed under very rare conditions
83! Bugfix: calculation of number of particles with same radius as the current
84!         particle (cloud droplet code)
85!
86! Revision 1.31  2006/08/17 09:21:01  raasch
87! Two more compilation errors removed from the last revision
88!
89! Revision 1.30  2006/08/17 09:11:17  raasch
90! Two compilation errors removed from the last revision
91!
92! Revision 1.29  2006/08/04 14:05:01  raasch
93! Subgrid scale velocities are (optionally) included for calculating the
94! particle advection, new counters trlp_count_sum, etc. for accumulating
95! the number of particles exchanged between the subdomains during all
96! sub-timesteps (if sgs velocities are included), +3d-arrays de_dx/y/z,
97! izuf renamed iran, output of particle time series
98!
99! Revision 1.1  1999/11/25 16:16:06  raasch
100! Initial revision
101!
102!
103! Description:
104! ------------
105! Particle advection
106!------------------------------------------------------------------------------!
107
108    USE arrays_3d
109    USE cloud_parameters
110    USE constants
111    USE control_parameters
112    USE cpulog
113    USE grid_variables
114    USE indices
115    USE interfaces
116    USE netcdf_control
117    USE particle_attributes
118    USE pegrid
119    USE random_function_mod
120    USE statistics
121
122    IMPLICIT NONE
123
124    INTEGER ::  agp, deleted_particles, deleted_tails, i, ie, ii, inc, is, j,  &
125                jj, js, k, kk, kw, m, n, nc, nn, num_gp, psi, tlength,         &
126                trlp_count, trlp_count_sum, trlp_count_recv,                   &
127                trlp_count_recv_sum, trlpt_count, trlpt_count_recv,            &
128                trnp_count, trnp_count_sum, trnp_count_recv,                   &
129                trnp_count_recv_sum, trnpt_count, trnpt_count_recv,            &
130                trrp_count, trrp_count_sum, trrp_count_recv,                   &
131                trrp_count_recv_sum, trrpt_count, trrpt_count_recv,            &
132                trsp_count, trsp_count_sum, trsp_count_recv,                   &
133                trsp_count_recv_sum, trspt_count, trspt_count_recv, nd
134
135    INTEGER ::  gp_outside_of_building(1:8)
136
137    LOGICAL ::  dt_3d_reached, dt_3d_reached_l, prt_position
138
139    REAL    ::  aa, arg, bb, cc, dd, delta_r, delta_v, dens_ratio, de_dt,      &
140                de_dt_min, de_dx_int, de_dx_int_l, de_dx_int_u, de_dy_int,     &
141                de_dy_int_l, de_dy_int_u, de_dz_int, de_dz_int_l, de_dz_int_u, &
142                diss_int, diss_int_l, diss_int_u, distance, dt_gap,            &
143                dt_particle, dt_particle_m, d_radius, d_sum, e_a, e_int,       &
144                e_int_l, e_int_u, e_mean_int, e_s, exp_arg, exp_term, fs_int,  &
145                gg,lagr_timescale, mean_r, new_r, p_int, pt_int, pt_int_l,     &
146                pt_int_u, q_int, q_int_l, q_int_u, ql_int, ql_int_l, ql_int_u, &
147                random_gauss, sl_r3, sl_r4, t_int, u_int, u_int_l, u_int_u,    &
148                vv_int, v_int, v_int_l, v_int_u, w_int, w_int_l, w_int_u,      &
149                x, y
150
151    REAL, DIMENSION(1:30) ::  de_dxi, de_dyi, de_dzi, dissi, d_gp_pl, ei
152
153    REAL    ::  location(1:30,1:3)
154
155    REAL, DIMENSION(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1) ::  de_dx, de_dy, de_dz
156
157    REAL, DIMENSION(:,:,:), ALLOCATABLE ::  trlpt, trnpt, trrpt, trspt
158
159    TYPE(particle_type) ::  tmp_particle
160
161    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  trlp, trnp, trrp, trsp
162
163
164    CALL cpu_log( log_point(25), 'advec_particles', 'start' )
165
166!    IF ( number_of_particles /= number_of_tails )  THEN
167!       WRITE (9,*) '--- advec_particles: #1'
168!       WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
169!       CALL local_flush( 9 )
170!    ENDIF
171!
172!-- Write particle data on file for later analysis.
173!-- This has to be done here (before particles are advected) in order
174!-- to allow correct output in case of dt_write_particle_data = dt_prel =
175!-- particle_maximum_age. Otherwise (if output is done at the end of this
176!-- subroutine), the relevant particles would have been already deleted.
177!-- The MOD function allows for changes in the output interval with restart
178!-- runs.
179!-- Attention: change version number for unit 85 (in routine check_open)
180!--            whenever the output format for this unit is changed!
181    time_write_particle_data = time_write_particle_data + dt_3d
182    IF ( time_write_particle_data >= dt_write_particle_data )  THEN
183
184       CALL cpu_log( log_point_s(40), 'advec_part_io', 'start' )
185       CALL check_open( 85 )
186       WRITE ( 85 )  simulated_time, maximum_number_of_particles, &
187                     number_of_particles
188       WRITE ( 85 )  particles
189       WRITE ( 85 )  maximum_number_of_tailpoints, maximum_number_of_tails, &
190                     number_of_tails
191       IF ( maximum_number_of_tails > 0 )  THEN
192          WRITE ( 85 )  particle_tail_coordinates
193       ENDIF
194       CALL close_file( 85 )
195
196       IF ( netcdf_output )  CALL output_particles_netcdf
197
198       time_write_particle_data = MOD( time_write_particle_data, &
199                                  MAX( dt_write_particle_data, dt_3d ) )
200       CALL cpu_log( log_point_s(40), 'advec_part_io', 'stop' )
201    ENDIF
202
203!
204!--    Initialize variables for the (sub-) timestep, i.e. for
205!--    marking those particles to be deleted after the timestep
206       particle_mask     = .TRUE.
207       deleted_particles = 0
208       trlp_count_recv   = 0
209       trnp_count_recv   = 0
210       trrp_count_recv   = 0
211       trsp_count_recv   = 0
212       trlpt_count_recv  = 0
213       trnpt_count_recv  = 0
214       trrpt_count_recv  = 0
215       trspt_count_recv  = 0
216       IF ( use_particle_tails )  THEN
217          tail_mask     = .TRUE.
218       ENDIF
219       deleted_tails = 0
220
221!
222!-- Calculate exponential term used in case of particle inertia for each
223!-- of the particle groups
224    CALL cpu_log( log_point_s(41), 'advec_part_exp', 'start' )
225    DO  m = 1, number_of_particle_groups
226       IF ( particle_groups(m)%density_ratio /= 0.0 )  THEN
227          particle_groups(m)%exp_arg  =                                        &
228                    4.5 * particle_groups(m)%density_ratio *                   &
229                    molecular_viscosity / ( particle_groups(m)%radius )**2
230          particle_groups(m)%exp_term = EXP( -particle_groups(m)%exp_arg * &
231                                             dt_3d )
232       ENDIF
233    ENDDO
234    CALL cpu_log( log_point_s(41), 'advec_part_exp', 'stop' )
235
236!    WRITE ( 9, * ) '*** advec_particles: ##0.3'
237!    CALL local_flush( 9 )
238!    nd = 0
239!    DO  n = 1, number_of_particles
240!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
241!    ENDDO
242!    IF ( nd /= deleted_particles ) THEN
243!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
244!       CALL local_flush( 9 )
245!       CALL MPI_ABORT( comm2d, 9999, ierr )
246!    ENDIF
247
248!
249!-- Particle (droplet) growth by condensation/evaporation and collision
250    IF ( cloud_droplets )  THEN
251
252!
253!--    Reset summation arrays
254       ql_c = 0.0;  ql_v = 0.0;  ql_vp = 0.0
255       delta_r=0.0;  delta_v=0.0
256
257!
258!--    Particle growth by condensation/evaporation
259       CALL cpu_log( log_point_s(42), 'advec_part_cond', 'start' )
260       DO  n = 1, number_of_particles
261!
262!--       Interpolate temperature and humidity.
263!--       First determine left, south, and bottom index of the arrays.
264          i = particles(n)%x * ddx
265          j = particles(n)%y * ddy
266          k = ( particles(n)%z + 0.5 * dz * atmos_ocean_sign ) / dz  &
267              + offset_ocean_nzt                     ! only exact if equidistant
268
269          x  = particles(n)%x - i * dx
270          y  = particles(n)%y - j * dy
271          aa = x**2          + y**2
272          bb = ( dx - x )**2 + y**2
273          cc = x**2          + ( dy - y )**2
274          dd = ( dx - x )**2 + ( dy - y )**2
275          gg = aa + bb + cc + dd
276
277          pt_int_l = ( ( gg - aa ) * pt(k,j,i)   + ( gg - bb ) * pt(k,j,i+1)   &
278                     + ( gg - cc ) * pt(k,j+1,i) + ( gg - dd ) * pt(k,j+1,i+1) &
279                     ) / ( 3.0 * gg )
280
281          pt_int_u = ( ( gg-aa ) * pt(k+1,j,i)   + ( gg-bb ) * pt(k+1,j,i+1)   &
282                     + ( gg-cc ) * pt(k+1,j+1,i) + ( gg-dd ) * pt(k+1,j+1,i+1) &
283                     ) / ( 3.0 * gg )
284
285          pt_int = pt_int_l + ( particles(n)%z - zu(k) ) / dz * &
286                              ( pt_int_u - pt_int_l )
287
288          q_int_l = ( ( gg - aa ) * q(k,j,i)   + ( gg - bb ) * q(k,j,i+1)   &
289                    + ( gg - cc ) * q(k,j+1,i) + ( gg - dd ) * q(k,j+1,i+1) &
290                    ) / ( 3.0 * gg )
291
292          q_int_u = ( ( gg-aa ) * q(k+1,j,i)   + ( gg-bb ) * q(k+1,j,i+1)   &
293                    + ( gg-cc ) * q(k+1,j+1,i) + ( gg-dd ) * q(k+1,j+1,i+1) &
294                    ) / ( 3.0 * gg )
295
296          q_int = q_int_l + ( particles(n)%z - zu(k) ) / dz * &
297                              ( q_int_u - q_int_l )
298
299          ql_int_l = ( ( gg - aa ) * ql(k,j,i)   + ( gg - bb ) * ql(k,j,i+1)   &
300                     + ( gg - cc ) * ql(k,j+1,i) + ( gg - dd ) * ql(k,j+1,i+1) &
301                     ) / ( 3.0 * gg )
302
303          ql_int_u = ( ( gg-aa ) * ql(k+1,j,i)   + ( gg-bb ) * ql(k+1,j,i+1)   &
304                     + ( gg-cc ) * ql(k+1,j+1,i) + ( gg-dd ) * ql(k+1,j+1,i+1) &
305                     ) / ( 3.0 * gg )
306
307          ql_int = ql_int_l + ( particles(n)%z - zu(k) ) / dz * &
308                                ( ql_int_u - ql_int_l )
309
310!
311!--       Calculate real temperature and saturation vapor pressure
312          p_int = hyp(k) + ( particles(n)%z - zu(k) ) / dz * ( hyp(k+1)-hyp(k) )
313          t_int = pt_int * ( p_int / 100000.0 )**0.286
314
315          e_s = 611.0 * EXP( l_d_rv * ( 3.6609E-3 - 1.0 / t_int ) )
316
317!
318!--       Current vapor pressure
319          e_a = q_int * p_int / ( 0.378 * q_int + 0.622 )
320
321!
322!--       Change in radius by condensation/evaporation
323!--       ATTENTION: this is only an approximation for large radii
324          arg = particles(n)%radius**2 + 2.0 * dt_3d *                     &
325                        ( e_a / e_s - 1.0 ) /                              &
326                        ( ( l_d_rv / t_int - 1.0 ) * l_v * rho_l / t_int / &
327                          thermal_conductivity_l +                         &
328                          rho_l * r_v * t_int / diff_coeff_l / e_s )
329          IF ( arg < 1.0E-14 )  THEN
330             new_r = 1.0E-7
331          ELSE
332             new_r = SQRT( arg )
333          ENDIF
334
335          delta_r = new_r - particles(n)%radius
336
337! NOTE: this is the correct formula (indipendent of radius).
338!       nevertheless, it give wrong results for large timesteps
339!          d_radius =  1.0 / particles(n)%radius
340!          delta_r = d_radius * ( e_a / e_s - 1.0 - 3.3E-7 / t_int * d_radius + &
341!                                 b_cond * d_radius**3 ) /                      &
342!                    ( ( l_d_rv / t_int - 1.0 ) * l_v * rho_l / t_int /         &
343!                      thermal_conductivity_l +                                 &
344!                      rho_l * r_v * t_int / diff_coeff_l / e_s ) * dt_3d
345
346!          new_r = particles(n)%radius + delta_r
347!          IF ( new_r < 1.0E-7 )  new_r = 1.0E-7
348
349!
350!--       Sum up the change in volume of liquid water for the respective grid
351!--       volume (this is needed later on for calculating the release of
352!--       latent heat)
353          i = ( particles(n)%x + 0.5 * dx ) * ddx
354          j = ( particles(n)%y + 0.5 * dy ) * ddy
355          k = particles(n)%z / dz + 1 + offset_ocean_nzt_m1
356              ! only exact if equidistant
357
358          ql_c(k,j,i) = ql_c(k,j,i) + particles(n)%weight_factor *            &
359                                      rho_l * 1.33333333 * pi *               &
360                                      ( new_r**3 - particles(n)%radius**3 ) / &
361                                      ( rho_surface * dx * dy * dz )
362          IF ( ql_c(k,j,i) > 100.0 )  THEN
363             WRITE( message_string, * ) 'k=',k,' j=',j,' i=',i,      &
364                          ' ql_c=',ql_c(k,j,i), ' &part(',n,')%wf=', &
365                          particles(n)%weight_factor,' delta_r=',delta_r
366             CALL message( 'advec_particles', 'PA0143', 2, 2, -1, 6, 1 )
367          ENDIF
368
369!
370!--       Change the droplet radius
371          IF ( ( new_r - particles(n)%radius ) < 0.0  .AND.  new_r < 0.0 ) &
372          THEN
373             WRITE( message_string, * ) '#1 k=',k,' j=',j,' i=',i,   &
374                          ' e_s=',e_s, ' e_a=',e_a,' t_int=',t_int,  &
375                          ' &d_radius=',d_radius,' delta_r=',delta_r,&
376                          ' particle_radius=',particles(n)%radius
377             CALL message( 'advec_particles', 'PA0144', 2, 2, -1, 6, 1 )
378          ENDIF
379          particles(n)%radius = new_r
380
381!
382!--       Sum up the total volume of liquid water (needed below for
383!--       re-calculating the weighting factors)
384          ql_v(k,j,i) = ql_v(k,j,i) + particles(n)%weight_factor *             &
385                                      particles(n)%radius**3
386       ENDDO
387       CALL cpu_log( log_point_s(42), 'advec_part_cond', 'stop' )
388
389!
390!--    Particle growth by collision
391       CALL cpu_log( log_point_s(43), 'advec_part_coll', 'start' )
392
393       DO  i = nxl, nxr
394          DO  j = nys, nyn
395             DO  k = nzb+1, nzt
396!
397!--             Collision requires at least two particles in the box
398                IF ( prt_count(k,j,i) > 1 )  THEN
399!
400!--                First, sort particles within the gridbox by their size,
401!--                using Shell's method (see Numerical Recipes)
402!--                NOTE: In case of using particle tails, the re-sorting of
403!--                ----  tails would have to be included here!
404                   psi = prt_start_index(k,j,i) - 1
405                   inc = 1
406                   DO WHILE ( inc <= prt_count(k,j,i) )
407                      inc = 3 * inc + 1
408                   ENDDO
409
410                   DO WHILE ( inc > 1 )
411                      inc = inc / 3
412                      DO  is = inc+1, prt_count(k,j,i)
413                         tmp_particle = particles(psi+is)
414                         js = is
415                         DO WHILE ( particles(psi+js-inc)%radius >             &
416                                    tmp_particle%radius )
417                            particles(psi+js) = particles(psi+js-inc)
418                            js = js - inc
419                            IF ( js <= inc )  EXIT
420                         ENDDO
421                         particles(psi+js) = tmp_particle
422                      ENDDO
423                   ENDDO
424
425!
426!--                Calculate the mean radius of all those particles which
427!--                are of smaller size than the current particle
428!--                and use this radius for calculating the collision efficiency
429                   psi = prt_start_index(k,j,i)
430
431                   DO  n = psi+prt_count(k,j,i)-1, psi+1, -1
432                      sl_r3 = 0.0
433                      sl_r4 = 0.0
434
435                      DO is = n-1, psi, -1
436                         IF ( particles(is)%radius < particles(n)%radius ) THEN
437                            sl_r3 = sl_r3 + particles(is)%weight_factor        &
438                                          *( particles(is)%radius**3 )
439                            sl_r4 = sl_r4 + particles(is)%weight_factor        &
440                                          *( particles(is)%radius**4 )
441                         ENDIF
442                      ENDDO
443
444                      IF ( ( sl_r3 ) > 0.0 )  THEN
445                         mean_r = ( sl_r4 ) / ( sl_r3 )
446
447                         CALL collision_efficiency( mean_r,                    &
448                                                    particles(n)%radius,       &
449                                                    effective_coll_efficiency )
450
451                      ELSE
452                         effective_coll_efficiency = 0.0
453                      ENDIF
454
455                      IF ( effective_coll_efficiency > 1.0  .OR.               &
456                           effective_coll_efficiency < 0.0 )                   &
457                      THEN
458                         WRITE( message_string, * )  'collision_efficiency ' , &
459                                   'out of range:' ,effective_coll_efficiency
460                         CALL message( 'advec_particles', 'PA0145', 2, 2, -1,  &
461                                       6, 1 )
462                      ENDIF
463
464!
465!--                   Interpolation of ...
466                      ii = particles(n)%x * ddx
467                      jj = particles(n)%y * ddy
468                      kk = ( particles(n)%z + 0.5 * dz ) / dz
469
470                      x  = particles(n)%x - ii * dx
471                      y  = particles(n)%y - jj * dy
472                      aa = x**2          + y**2
473                      bb = ( dx - x )**2 + y**2
474                      cc = x**2          + ( dy - y )**2
475                      dd = ( dx - x )**2 + ( dy - y )**2
476                      gg = aa + bb + cc + dd
477
478                      ql_int_l = ( ( gg-aa ) * ql(kk,jj,ii)   + ( gg-bb ) *    &
479                                                              ql(kk,jj,ii+1)   &
480                                 + ( gg-cc ) * ql(kk,jj+1,ii) + ( gg-dd ) *    &
481                                                              ql(kk,jj+1,ii+1) &
482                                 ) / ( 3.0 * gg )
483
484                      ql_int_u = ( ( gg-aa ) * ql(kk+1,jj,ii)   + ( gg-bb ) *  &
485                                                            ql(kk+1,jj,ii+1)   &
486                                 + ( gg-cc ) * ql(kk+1,jj+1,ii) + ( gg-dd ) *  &
487                                                            ql(kk+1,jj+1,ii+1) &
488                                 ) / ( 3.0 * gg )
489
490                      ql_int = ql_int_l + ( particles(n)%z - zu(kk) ) / dz *   &
491                                          ( ql_int_u - ql_int_l )
492
493!
494!--                   Interpolate u velocity-component
495                      ii = ( particles(n)%x + 0.5 * dx ) * ddx
496                      jj =   particles(n)%y * ddy
497                      kk = ( particles(n)%z + 0.5 * dz ) / dz  ! only if eq.dist
498
499                      IF ( ( particles(n)%z - zu(kk) ) > ( 0.5*dz ) )  kk = kk+1
500
501                      x  = particles(n)%x + ( 0.5 - ii ) * dx
502                      y  = particles(n)%y - jj * dy
503                      aa = x**2          + y**2
504                      bb = ( dx - x )**2 + y**2
505                      cc = x**2          + ( dy - y )**2
506                      dd = ( dx - x )**2 + ( dy - y )**2
507                      gg = aa + bb + cc + dd
508
509                      u_int_l = ( ( gg-aa ) * u(kk,jj,ii)   + ( gg-bb ) *      &
510                                                              u(kk,jj,ii+1)    &
511                                + ( gg-cc ) * u(kk,jj+1,ii) + ( gg-dd ) *      &
512                                                              u(kk,jj+1,ii+1)  &
513                                ) / ( 3.0 * gg ) - u_gtrans
514                      IF ( kk+1 == nzt+1 )  THEN
515                         u_int = u_int_l
516                      ELSE
517                         u_int_u = ( ( gg-aa ) * u(kk+1,jj,ii)   + ( gg-bb ) * &
518                                                             u(kk+1,jj,ii+1)   &
519                                   + ( gg-cc ) * u(kk+1,jj+1,ii) + ( gg-dd ) * &
520                                                             u(kk+1,jj+1,ii+1) &
521                                   ) / ( 3.0 * gg ) - u_gtrans
522                         u_int = u_int_l + ( particles(n)%z - zu(kk) ) / dz *  &
523                                           ( u_int_u - u_int_l )
524                      ENDIF
525
526!
527!--                   Same procedure for interpolation of the v velocity-compo-
528!--                   nent (adopt index k from u velocity-component)
529                      ii =   particles(n)%x * ddx
530                      jj = ( particles(n)%y + 0.5 * dy ) * ddy
531
532                      x  = particles(n)%x - ii * dx
533                      y  = particles(n)%y + ( 0.5 - jj ) * dy
534                      aa = x**2          + y**2
535                      bb = ( dx - x )**2 + y**2
536                      cc = x**2          + ( dy - y )**2
537                      dd = ( dx - x )**2 + ( dy - y )**2
538                      gg = aa + bb + cc + dd
539
540                      v_int_l = ( ( gg-aa ) * v(kk,jj,ii)   + ( gg-bb ) *      &
541                                                               v(kk,jj,ii+1)   &
542                                + ( gg-cc ) * v(kk,jj+1,ii) + ( gg-dd ) *      &
543                                                               v(kk,jj+1,ii+1) &
544                                ) / ( 3.0 * gg ) - v_gtrans
545                      IF ( kk+1 == nzt+1 )  THEN
546                         v_int = v_int_l
547                      ELSE
548                         v_int_u = ( ( gg-aa ) * v(kk+1,jj,ii)   + ( gg-bb ) * &
549                                                               v(kk+1,jj,ii+1) &
550                                   + ( gg-cc ) * v(kk+1,jj+1,ii) + ( gg-dd ) * &
551                                                             v(kk+1,jj+1,ii+1) &
552                                ) / ( 3.0 * gg ) - v_gtrans
553                         v_int = v_int_l + ( particles(n)%z - zu(kk) ) / dz * &
554                                           ( v_int_u - v_int_l )
555                      ENDIF
556
557!
558!--                   Same procedure for interpolation of the w velocity-compo-
559!--                   nent (adopt index i from v velocity-component)
560                      jj = particles(n)%y * ddy
561                      kk = particles(n)%z / dz
562
563                      x  = particles(n)%x - ii * dx
564                      y  = particles(n)%y - jj * dy
565                      aa = x**2          + y**2
566                      bb = ( dx - x )**2 + y**2
567                      cc = x**2          + ( dy - y )**2
568                      dd = ( dx - x )**2 + ( dy - y )**2
569                      gg = aa + bb + cc + dd
570
571                      w_int_l = ( ( gg-aa ) * w(kk,jj,ii)   + ( gg-bb ) *      &
572                                                                 w(kk,jj,ii+1) &
573                                + ( gg-cc ) * w(kk,jj+1,ii) + ( gg-dd ) *      &
574                                                               w(kk,jj+1,ii+1) &
575                                ) / ( 3.0 * gg )
576                      IF ( kk+1 == nzt+1 )  THEN
577                         w_int = w_int_l
578                      ELSE
579                         w_int_u = ( ( gg-aa ) * w(kk+1,jj,ii)   + ( gg-bb ) * &
580                                                               w(kk+1,jj,ii+1) &
581                                   + ( gg-cc ) * w(kk+1,jj+1,ii) + ( gg-dd ) * &
582                                                             w(kk+1,jj+1,ii+1) &
583                                   ) / ( 3.0 * gg )
584                         w_int = w_int_l + ( particles(n)%z - zw(kk) ) / dz * &
585                                           ( w_int_u - w_int_l )
586                      ENDIF
587
588!
589!--                Change in radius due to collision
590                      delta_r = effective_coll_efficiency / 3.0                &
591                                * pi * sl_r3 * ddx * ddy / dz                  &
592                                * SQRT( ( u_int - particles(n)%speed_x )**2    &
593                                      + ( v_int - particles(n)%speed_y )**2    &
594                                      + ( w_int - particles(n)%speed_z )**2    &
595                                      ) * dt_3d
596!
597!--                Change in volume due to collision
598                      delta_v = particles(n)%weight_factor                     &
599                                * ( ( particles(n)%radius + delta_r )**3       &
600                                    - particles(n)%radius**3 )
601
602!
603!--                Check if collected particles provide enough LWC for volume
604!--                change of collector particle
605                      IF ( delta_v >= sl_r3 .and. sl_r3 > 0.0 ) THEN
606
607                         delta_r = ( ( sl_r3/particles(n)%weight_factor )      &
608                                     + particles(n)%radius**3 )**( 1./3. )     &
609                                     - particles(n)%radius
610
611                         DO is = n-1, psi, -1
612                            IF ( particles(is)%radius < particles(n)%radius )  &
613                            THEN
614                                 particles(is)%weight_factor = 0.0
615                                 particle_mask(is)  = .FALSE.
616                                 deleted_particles = deleted_particles + 1
617                            ENDIF
618                         ENDDO
619
620                      ELSE IF ( delta_v < sl_r3 .and. sl_r3 > 0.0 ) THEN
621
622                         DO is = n-1, psi, -1
623                            IF ( particles(is)%radius < particles(n)%radius    &
624                                 .and. sl_r3 > 0.0 ) THEN
625                                 particles(is)%weight_factor =                 &
626                                               ( ( particles(is)%weight_factor &
627                                               * ( particles(is)%radius**3 ) ) &
628                                               - ( delta_v                     &
629                                               * particles(is)%weight_factor   &
630                                               * ( particles(is)%radius**3 )   &
631                                               / sl_r3 ) )                     &
632                                               / ( particles(is)%radius**3 )
633
634                               IF (particles(is)%weight_factor < 0.0) THEN
635                                  WRITE( message_string, * ) 'negative ',      &
636                                                     'weighting factor: ',     &
637                                                     particles(is)%weight_factor
638                                  CALL message( 'advec_particles', '', 2, 2,   &
639                                                -1, 6, 1 )
640                               ENDIF
641                            ENDIF
642                         ENDDO
643                      ENDIF
644
645                      particles(n)%radius = particles(n)%radius + delta_r
646                      ql_vp(k,j,i) = ql_vp(k,j,i) + particles(n)%weight_factor &
647                                      *( particles(n)%radius**3 )
648                   ENDDO
649
650                   ql_vp(k,j,i) = ql_vp(k,j,i) + particles(psi)%weight_factor  &
651                                  *( particles(psi)%radius**3 )
652
653                ELSE IF ( prt_count(k,j,i) == 1 )  THEN
654                   psi = prt_start_index(k,j,i)
655                   ql_vp(k,j,i) =  particles(psi)%weight_factor                &
656                                  *( particles(psi)%radius**3 )
657                ENDIF
658!
659!--            Check if condensation of LWC was conserved
660!--            during collision process
661                IF (ql_v(k,j,i) /= 0.0 ) THEN
662                   IF (ql_vp(k,j,i)/ql_v(k,j,i) >= 1.00001 .or.                &
663                       ql_vp(k,j,i)/ql_v(k,j,i) <= 0.99999  ) THEN
664                      WRITE( message_string, * ) 'LWC is not conserved during',&
665                                                 ' collision! ',               &
666                                                 'LWC after condensation: ',   &
667                                                 ql_v(k,j,i),                  &
668                                                 ' LWC after collision: ',     &
669                                                 ql_vp(k,j,i)
670                      CALL message( 'advec_particles', '', 2, 2, -1, 6, 1 )
671                   ENDIF
672                ENDIF
673
674             ENDDO
675          ENDDO
676       ENDDO
677
678       CALL cpu_log( log_point_s(43), 'advec_part_coll', 'stop' )
679
680    ENDIF
681
682
683!
684!-- Particle advection.
685!-- In case of including the SGS velocities, the LES timestep has probably
686!-- to be split into several smaller timesteps because of the Lagrangian
687!-- timescale condition. Because the number of timesteps to be carried out is
688!-- not known at the beginning, these steps are carried out in an infinite loop
689!-- with exit condition.
690!
691!-- If SGS velocities are used, gradients of the TKE have to be calculated and
692!-- boundary conditions have to be set first. Also, horizontally averaged
693!-- profiles of the SGS TKE and the resolved-scale velocity variances are
694!-- needed.
695    IF ( use_sgs_for_particles )  THEN
696
697!
698!--    TKE gradient along x and y
699       DO  i = nxl, nxr
700          DO  j = nys, nyn
701             DO  k = nzb, nzt+1
702
703                IF ( k <= nzb_s_inner(j,i-1)  .AND.  &
704                     k  > nzb_s_inner(j,i)    .AND.  &
705                     k  > nzb_s_inner(j,i+1) )  THEN
706                   de_dx(k,j,i) = 2.0 * sgs_wfu_part * &
707                                  ( e(k,j,i+1) - e(k,j,i) ) * ddx
708                ELSEIF ( k  > nzb_s_inner(j,i-1)  .AND.  &
709                         k  > nzb_s_inner(j,i)    .AND.  &
710                         k <= nzb_s_inner(j,i+1) )  THEN
711                   de_dx(k,j,i) = 2.0 * sgs_wfu_part * &
712                                  ( e(k,j,i) - e(k,j,i-1) ) * ddx
713                ELSEIF ( k < nzb_s_inner(j,i)  .AND.  k < nzb_s_inner(j,i+1) ) &
714                THEN
715                   de_dx(k,j,i) = 0.0
716                ELSEIF ( k < nzb_s_inner(j,i-1)  .AND.  k < nzb_s_inner(j,i) ) &
717                THEN
718                   de_dx(k,j,i) = 0.0
719                ELSE
720                   de_dx(k,j,i) = sgs_wfu_part * &
721                                  ( e(k,j,i+1) - e(k,j,i-1) ) * ddx
722                ENDIF
723
724                IF ( k <= nzb_s_inner(j-1,i)  .AND.  &
725                     k  > nzb_s_inner(j,i)    .AND.  &
726                     k  > nzb_s_inner(j+1,i) )  THEN
727                   de_dy(k,j,i) = 2.0 * sgs_wfv_part * &
728                                  ( e(k,j+1,i) - e(k,j,i) ) * ddy
729                ELSEIF ( k  > nzb_s_inner(j-1,i)  .AND.  &
730                         k  > nzb_s_inner(j,i)    .AND.  &
731                         k <= nzb_s_inner(j+1,i) )  THEN
732                   de_dy(k,j,i) = 2.0 * sgs_wfv_part * &
733                                  ( e(k,j,i) - e(k,j-1,i) ) * ddy
734                ELSEIF ( k < nzb_s_inner(j,i)  .AND.  k < nzb_s_inner(j+1,i) ) &
735                THEN
736                   de_dy(k,j,i) = 0.0
737                ELSEIF ( k < nzb_s_inner(j-1,i)  .AND.  k < nzb_s_inner(j,i) ) &
738                THEN
739                   de_dy(k,j,i) = 0.0
740                ELSE
741                   de_dy(k,j,i) = sgs_wfv_part * &
742                                  ( e(k,j+1,i) - e(k,j-1,i) ) * ddy
743                ENDIF
744
745             ENDDO
746          ENDDO
747       ENDDO
748
749!
750!--    TKE gradient along z, including bottom and top boundary conditions
751       DO  i = nxl, nxr
752          DO  j = nys, nyn
753
754             DO  k = nzb_s_inner(j,i)+2, nzt-1
755                de_dz(k,j,i) = 2.0 * sgs_wfw_part * &
756                               ( e(k+1,j,i) - e(k-1,j,i) ) / ( zu(k+1)-zu(k-1) )
757             ENDDO
758
759             k = nzb_s_inner(j,i)
760             de_dz(nzb:k,j,i)   = 0.0
761             de_dz(k+1,j,i) = 2.0 * sgs_wfw_part * ( e(k+2,j,i) - e(k+1,j,i) ) &
762                                                 / ( zu(k+2) - zu(k+1) )
763             de_dz(nzt,j,i)   = 0.0
764             de_dz(nzt+1,j,i) = 0.0
765          ENDDO
766       ENDDO
767
768!
769!--    Lateral boundary conditions
770       CALL exchange_horiz( de_dx )
771       CALL exchange_horiz( de_dy )
772       CALL exchange_horiz( de_dz )
773       CALL exchange_horiz( diss  )
774
775!
776!--    Calculate the horizontally averaged profiles of SGS TKE and resolved
777!--    velocity variances (they may have been already calculated in routine
778!--    flow_statistics).
779       IF ( .NOT. flow_statistics_called )  THEN
780!
781!--       First calculate horizontally averaged profiles of the horizontal
782!--       velocities.
783          sums_l(:,1,0) = 0.0
784          sums_l(:,2,0) = 0.0
785
786          DO  i = nxl, nxr
787             DO  j =  nys, nyn
788                DO  k = nzb_s_outer(j,i), nzt+1
789                   sums_l(k,1,0)  = sums_l(k,1,0)  + u(k,j,i)
790                   sums_l(k,2,0)  = sums_l(k,2,0)  + v(k,j,i)
791                ENDDO
792             ENDDO
793          ENDDO
794
795#if defined( __parallel )
796!
797!--       Compute total sum from local sums
798          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
799          CALL MPI_ALLREDUCE( sums_l(nzb,1,0), sums(nzb,1), nzt+2-nzb, &
800                              MPI_REAL, MPI_SUM, comm2d, ierr )
801          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
802          CALL MPI_ALLREDUCE( sums_l(nzb,2,0), sums(nzb,2), nzt+2-nzb, &
803                              MPI_REAL, MPI_SUM, comm2d, ierr )
804#else
805                   sums(:,1) = sums_l(:,1,0)
806                   sums(:,2) = sums_l(:,2,0)
807#endif
808
809!
810!--       Final values are obtained by division by the total number of grid
811!--       points used for the summation.
812          hom(:,1,1,0) = sums(:,1) / ngp_2dh_outer(:,0)   ! u
813          hom(:,1,2,0) = sums(:,2) / ngp_2dh_outer(:,0)   ! v
814
815!
816!--       Now calculate the profiles of SGS TKE and the resolved-scale
817!--       velocity variances
818          sums_l(:,8,0)  = 0.0
819          sums_l(:,30,0) = 0.0
820          sums_l(:,31,0) = 0.0
821          sums_l(:,32,0) = 0.0
822          DO  i = nxl, nxr
823             DO  j = nys, nyn
824                DO  k = nzb_s_outer(j,i), nzt+1
825                   sums_l(k,8,0)  = sums_l(k,8,0)  + e(k,j,i)
826                   sums_l(k,30,0) = sums_l(k,30,0) + &
827                                    ( u(k,j,i) - hom(k,1,1,0) )**2
828                   sums_l(k,31,0) = sums_l(k,31,0) + &
829                                    ( v(k,j,i) - hom(k,1,2,0) )**2
830                   sums_l(k,32,0) = sums_l(k,32,0) + w(k,j,i)**2
831                ENDDO
832             ENDDO
833          ENDDO
834
835#if defined( __parallel )
836!
837!--       Compute total sum from local sums
838          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
839          CALL MPI_ALLREDUCE( sums_l(nzb,8,0), sums(nzb,8), nzt+2-nzb, &
840                              MPI_REAL, MPI_SUM, comm2d, ierr )
841          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
842          CALL MPI_ALLREDUCE( sums_l(nzb,30,0), sums(nzb,30), nzt+2-nzb, &
843                              MPI_REAL, MPI_SUM, comm2d, ierr )
844          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
845          CALL MPI_ALLREDUCE( sums_l(nzb,31,0), sums(nzb,31), nzt+2-nzb, &
846                              MPI_REAL, MPI_SUM, comm2d, ierr )
847          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
848          CALL MPI_ALLREDUCE( sums_l(nzb,32,0), sums(nzb,32), nzt+2-nzb, &
849                              MPI_REAL, MPI_SUM, comm2d, ierr )
850
851#else
852          sums(:,8)  = sums_l(:,8,0)
853          sums(:,30) = sums_l(:,30,0)
854          sums(:,31) = sums_l(:,31,0)
855          sums(:,32) = sums_l(:,32,0)
856#endif
857
858!
859!--       Final values are obtained by division by the total number of grid
860!--       points used for the summation.
861          hom(:,1,8,0)  = sums(:,8)  / ngp_2dh_outer(:,0)   ! e
862          hom(:,1,30,0) = sums(:,30) / ngp_2dh_outer(:,0)   ! u*2
863          hom(:,1,31,0) = sums(:,31) / ngp_2dh_outer(:,0)   ! v*2
864          hom(:,1,32,0) = sums(:,32) / ngp_2dh_outer(:,0)   ! w*2
865
866       ENDIF
867
868    ENDIF
869
870!
871!-- Initialize variables used for accumulating the number of particles
872!-- exchanged between the subdomains during all sub-timesteps (if sgs
873!-- velocities are included). These data are output further below on the
874!-- particle statistics file.
875    trlp_count_sum      = 0
876    trlp_count_recv_sum = 0
877    trrp_count_sum      = 0
878    trrp_count_recv_sum = 0
879    trsp_count_sum      = 0
880    trsp_count_recv_sum = 0
881    trnp_count_sum      = 0
882    trnp_count_recv_sum = 0
883
884!
885!-- Initialize the variable storing the total time that a particle has advanced
886!-- within the timestep procedure
887    particles(1:number_of_particles)%dt_sum = 0.0
888
889!
890!-- Timestep loop.
891!-- This loop has to be repeated until the advection time of every particle
892!-- (in the total domain!) has reached the LES timestep (dt_3d)
893    DO
894
895       CALL cpu_log( log_point_s(44), 'advec_part_advec', 'start' )
896
897!
898!--    Initialize the switch used for the loop exit condition checked at the
899!--    end of this loop.
900!--    If at least one particle has failed to reach the LES timestep, this
901!--    switch will be set false.
902       dt_3d_reached_l = .TRUE.
903
904       DO  n = 1, number_of_particles
905!
906!--       Move particles only if the LES timestep has not (approximately) been
907!--       reached
908          IF ( ( dt_3d - particles(n)%dt_sum ) < 1E-8 )  CYCLE
909
910!
911!--       Interpolate u velocity-component, determine left, front, bottom
912!--       index of u-array
913          i = ( particles(n)%x + 0.5 * dx ) * ddx
914          j =   particles(n)%y * ddy
915          k = ( particles(n)%z + 0.5 * dz * atmos_ocean_sign ) / dz  &
916              + offset_ocean_nzt                     ! only exact if equidistant
917
918!
919!--       Interpolation of the velocity components in the xy-plane
920          x  = particles(n)%x + ( 0.5 - i ) * dx
921          y  = particles(n)%y - j * dy
922          aa = x**2          + y**2
923          bb = ( dx - x )**2 + y**2
924          cc = x**2          + ( dy - y )**2
925          dd = ( dx - x )**2 + ( dy - y )**2
926          gg = aa + bb + cc + dd
927
928          u_int_l = ( ( gg - aa ) * u(k,j,i)   + ( gg - bb ) * u(k,j,i+1)   &
929                    + ( gg - cc ) * u(k,j+1,i) + ( gg - dd ) * u(k,j+1,i+1) &
930                    ) / ( 3.0 * gg ) - u_gtrans
931          IF ( k+1 == nzt+1 )  THEN
932             u_int = u_int_l
933          ELSE
934             u_int_u = ( ( gg-aa ) * u(k+1,j,i)   + ( gg-bb ) * u(k+1,j,i+1) &
935                    + ( gg-cc ) * u(k+1,j+1,i) + ( gg-dd ) * u(k+1,j+1,i+1)  &
936                    ) / ( 3.0 * gg ) - u_gtrans
937             u_int = u_int_l + ( particles(n)%z - zu(k) ) / dz * &
938                               ( u_int_u - u_int_l )
939          ENDIF
940
941!
942!--       Same procedure for interpolation of the v velocity-component (adopt
943!--       index k from u velocity-component)
944          i =   particles(n)%x * ddx
945          j = ( particles(n)%y + 0.5 * dy ) * ddy
946
947          x  = particles(n)%x - i * dx
948          y  = particles(n)%y + ( 0.5 - j ) * dy
949          aa = x**2          + y**2
950          bb = ( dx - x )**2 + y**2
951          cc = x**2          + ( dy - y )**2
952          dd = ( dx - x )**2 + ( dy - y )**2
953          gg = aa + bb + cc + dd
954
955          v_int_l = ( ( gg - aa ) * v(k,j,i)   + ( gg - bb ) * v(k,j,i+1)   &
956                    + ( gg - cc ) * v(k,j+1,i) + ( gg - dd ) * v(k,j+1,i+1) &
957                    ) / ( 3.0 * gg ) - v_gtrans
958          IF ( k+1 == nzt+1 )  THEN
959             v_int = v_int_l
960          ELSE
961             v_int_u = ( ( gg-aa ) * v(k+1,j,i)   + ( gg-bb ) * v(k+1,j,i+1) &
962                    + ( gg-cc ) * v(k+1,j+1,i) + ( gg-dd ) * v(k+1,j+1,i+1)  &
963                    ) / ( 3.0 * gg ) - v_gtrans
964             v_int = v_int_l + ( particles(n)%z - zu(k) ) / dz * &
965                               ( v_int_u - v_int_l )
966          ENDIF
967
968!
969!--       Same procedure for interpolation of the w velocity-component (adopt
970!--       index i from v velocity-component)
971          IF ( vertical_particle_advection(particles(n)%group) )  THEN
972             j = particles(n)%y * ddy
973             k = particles(n)%z / dz + offset_ocean_nzt_m1
974
975             x  = particles(n)%x - i * dx
976             y  = particles(n)%y - j * dy
977             aa = x**2          + y**2
978             bb = ( dx - x )**2 + y**2
979             cc = x**2          + ( dy - y )**2
980             dd = ( dx - x )**2 + ( dy - y )**2
981             gg = aa + bb + cc + dd
982
983             w_int_l = ( ( gg - aa ) * w(k,j,i)   + ( gg - bb ) * w(k,j,i+1) &
984                    + ( gg - cc ) * w(k,j+1,i) + ( gg - dd ) * w(k,j+1,i+1)  &
985                       ) / ( 3.0 * gg )
986             IF ( k+1 == nzt+1 )  THEN
987                w_int = w_int_l
988             ELSE
989                w_int_u = ( ( gg-aa ) * w(k+1,j,i)   + &
990                            ( gg-bb ) * w(k+1,j,i+1) + &
991                            ( gg-cc ) * w(k+1,j+1,i) + &
992                            ( gg-dd ) * w(k+1,j+1,i+1) &
993                           ) / ( 3.0 * gg )
994                w_int = w_int_l + ( particles(n)%z - zw(k) ) / dz * &
995                                  ( w_int_u - w_int_l )
996             ENDIF
997          ELSE
998             w_int = 0.0
999          ENDIF
1000
1001!
1002!--       Interpolate and calculate quantities needed for calculating the SGS
1003!--       velocities
1004          IF ( use_sgs_for_particles )  THEN
1005!
1006!--          Interpolate TKE
1007             i = particles(n)%x * ddx
1008             j = particles(n)%y * ddy
1009             k = ( particles(n)%z + 0.5 * dz * atmos_ocean_sign ) / dz  &
1010                 + offset_ocean_nzt                      ! only exact if eq.dist
1011
1012             IF ( topography == 'flat' )  THEN
1013
1014                x  = particles(n)%x - i * dx
1015                y  = particles(n)%y - j * dy
1016                aa = x**2          + y**2
1017                bb = ( dx - x )**2 + y**2
1018                cc = x**2          + ( dy - y )**2
1019                dd = ( dx - x )**2 + ( dy - y )**2
1020                gg = aa + bb + cc + dd
1021
1022                e_int_l = ( ( gg-aa ) * e(k,j,i)   + ( gg-bb ) * e(k,j,i+1)   &
1023                          + ( gg-cc ) * e(k,j+1,i) + ( gg-dd ) * e(k,j+1,i+1) &
1024                          ) / ( 3.0 * gg )
1025
1026                IF ( k+1 == nzt+1 )  THEN
1027                   e_int = e_int_l
1028                ELSE
1029                   e_int_u = ( ( gg - aa ) * e(k+1,j,i)   + &
1030                               ( gg - bb ) * e(k+1,j,i+1) + &
1031                               ( gg - cc ) * e(k+1,j+1,i) + &
1032                               ( gg - dd ) * e(k+1,j+1,i+1) &
1033                             ) / ( 3.0 * gg )
1034                   e_int = e_int_l + ( particles(n)%z - zu(k) ) / dz * &
1035                                     ( e_int_u - e_int_l )
1036                ENDIF
1037
1038!
1039!--             Interpolate the TKE gradient along x (adopt incides i,j,k and
1040!--             all position variables from above (TKE))
1041                de_dx_int_l = ( ( gg - aa ) * de_dx(k,j,i)   + &
1042                                ( gg - bb ) * de_dx(k,j,i+1) + &
1043                                ( gg - cc ) * de_dx(k,j+1,i) + &
1044                                ( gg - dd ) * de_dx(k,j+1,i+1) &
1045                              ) / ( 3.0 * gg )
1046
1047                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
1048                   de_dx_int = de_dx_int_l
1049                ELSE
1050                   de_dx_int_u = ( ( gg - aa ) * de_dx(k+1,j,i)   + &
1051                                   ( gg - bb ) * de_dx(k+1,j,i+1) + &
1052                                   ( gg - cc ) * de_dx(k+1,j+1,i) + &
1053                                   ( gg - dd ) * de_dx(k+1,j+1,i+1) &
1054                                 ) / ( 3.0 * gg )
1055                   de_dx_int = de_dx_int_l + ( particles(n)%z - zu(k) ) / dz * &
1056                                             ( de_dx_int_u - de_dx_int_l )
1057                ENDIF
1058
1059!
1060!--             Interpolate the TKE gradient along y
1061                de_dy_int_l = ( ( gg - aa ) * de_dy(k,j,i)   + &
1062                                ( gg - bb ) * de_dy(k,j,i+1) + &
1063                                ( gg - cc ) * de_dy(k,j+1,i) + &
1064                                ( gg - dd ) * de_dy(k,j+1,i+1) &
1065                              ) / ( 3.0 * gg )
1066                IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
1067                   de_dy_int = de_dy_int_l
1068                ELSE
1069                   de_dy_int_u = ( ( gg - aa ) * de_dy(k+1,j,i)   + &
1070                                   ( gg - bb ) * de_dy(k+1,j,i+1) + &
1071                                   ( gg - cc ) * de_dy(k+1,j+1,i) + &
1072                                   ( gg - dd ) * de_dy(k+1,j+1,i+1) &
1073                                 ) / ( 3.0 * gg )
1074                   de_dy_int = de_dy_int_l + ( particles(n)%z - zu(k) ) / dz * &
1075                                             ( de_dy_int_u - de_dy_int_l )
1076                ENDIF
1077
1078!
1079!--             Interpolate the TKE gradient along z
1080                IF ( particles(n)%z < 0.5 * dz ) THEN
1081                   de_dz_int = 0.0
1082                ELSE
1083                   de_dz_int_l = ( ( gg - aa ) * de_dz(k,j,i)   + &
1084                                   ( gg - bb ) * de_dz(k,j,i+1) + &
1085                                   ( gg - cc ) * de_dz(k,j+1,i) + &
1086                                   ( gg - dd ) * de_dz(k,j+1,i+1) &
1087                                 ) / ( 3.0 * gg )
1088
1089                   IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
1090                      de_dz_int = de_dz_int_l
1091                   ELSE
1092                      de_dz_int_u = ( ( gg - aa ) * de_dz(k+1,j,i)   + &
1093                                      ( gg - bb ) * de_dz(k+1,j,i+1) + &
1094                                      ( gg - cc ) * de_dz(k+1,j+1,i) + &
1095                                      ( gg - dd ) * de_dz(k+1,j+1,i+1) &
1096                                    ) / ( 3.0 * gg )
1097                      de_dz_int = de_dz_int_l + ( particles(n)%z - zu(k) ) / dz * &
1098                                                ( de_dz_int_u - de_dz_int_l )
1099                   ENDIF
1100                ENDIF
1101
1102!
1103!--             Interpolate the dissipation of TKE
1104                diss_int_l = ( ( gg - aa ) * diss(k,j,i)   + &
1105                               ( gg - bb ) * diss(k,j,i+1) + &
1106                               ( gg - cc ) * diss(k,j+1,i) + &
1107                               ( gg - dd ) * diss(k,j+1,i+1) &
1108                              ) / ( 3.0 * gg )
1109
1110                IF ( k+1 == nzt+1 )  THEN
1111                   diss_int = diss_int_l
1112                ELSE
1113                   diss_int_u = ( ( gg - aa ) * diss(k+1,j,i)   + &
1114                                  ( gg - bb ) * diss(k+1,j,i+1) + &
1115                                  ( gg - cc ) * diss(k+1,j+1,i) + &
1116                                  ( gg - dd ) * diss(k+1,j+1,i+1) &
1117                                 ) / ( 3.0 * gg )
1118                   diss_int = diss_int_l + ( particles(n)%z - zu(k) ) / dz * &
1119                                           ( diss_int_u - diss_int_l )
1120                ENDIF
1121
1122             ELSE
1123
1124!
1125!--             In case that there are buildings it has to be determined
1126!--             how many of the gridpoints defining the particle box are
1127!--             situated within a building
1128!--             gp_outside_of_building(1): i,j,k
1129!--             gp_outside_of_building(2): i,j+1,k
1130!--             gp_outside_of_building(3): i,j,k+1
1131!--             gp_outside_of_building(4): i,j+1,k+1
1132!--             gp_outside_of_building(5): i+1,j,k
1133!--             gp_outside_of_building(6): i+1,j+1,k
1134!--             gp_outside_of_building(7): i+1,j,k+1
1135!--             gp_outside_of_building(8): i+1,j+1,k+1
1136
1137                gp_outside_of_building = 0
1138                location = 0.0
1139                num_gp = 0
1140
1141                IF ( k > nzb_s_inner(j,i)  .OR.  nzb_s_inner(j,i) == 0 )  THEN
1142                   num_gp = num_gp + 1
1143                   gp_outside_of_building(1) = 1
1144                   location(num_gp,1) = i * dx
1145                   location(num_gp,2) = j * dy
1146                   location(num_gp,3) = k * dz - 0.5 * dz
1147                   ei(num_gp)     = e(k,j,i)
1148                   dissi(num_gp)  = diss(k,j,i)
1149                   de_dxi(num_gp) = de_dx(k,j,i)
1150                   de_dyi(num_gp) = de_dy(k,j,i)
1151                   de_dzi(num_gp) = de_dz(k,j,i)
1152                ENDIF
1153
1154                IF ( k > nzb_s_inner(j+1,i)  .OR.  nzb_s_inner(j+1,i) == 0 ) &
1155                THEN
1156                   num_gp = num_gp + 1
1157                   gp_outside_of_building(2) = 1
1158                   location(num_gp,1) = i * dx
1159                   location(num_gp,2) = (j+1) * dy
1160                   location(num_gp,3) = k * dz - 0.5 * dz
1161                   ei(num_gp)     = e(k,j+1,i)
1162                   dissi(num_gp)  = diss(k,j+1,i)
1163                   de_dxi(num_gp) = de_dx(k,j+1,i)
1164                   de_dyi(num_gp) = de_dy(k,j+1,i)
1165                   de_dzi(num_gp) = de_dz(k,j+1,i)
1166                ENDIF
1167
1168                IF ( k+1 > nzb_s_inner(j,i)  .OR.  nzb_s_inner(j,i) == 0 )  THEN
1169                   num_gp = num_gp + 1
1170                   gp_outside_of_building(3) = 1
1171                   location(num_gp,1) = i * dx
1172                   location(num_gp,2) = j * dy
1173                   location(num_gp,3) = (k+1) * dz - 0.5 * dz
1174                   ei(num_gp)     = e(k+1,j,i)
1175                   dissi(num_gp)  = diss(k+1,j,i)
1176                   de_dxi(num_gp) = de_dx(k+1,j,i)
1177                   de_dyi(num_gp) = de_dy(k+1,j,i)
1178                   de_dzi(num_gp) = de_dz(k+1,j,i)
1179                ENDIF
1180
1181                IF ( k+1 > nzb_s_inner(j+1,i)  .OR.  nzb_s_inner(j+1,i) == 0 ) &
1182                THEN
1183                   num_gp = num_gp + 1
1184                   gp_outside_of_building(4) = 1
1185                   location(num_gp,1) = i * dx
1186                   location(num_gp,2) = (j+1) * dy
1187                   location(num_gp,3) = (k+1) * dz - 0.5 * dz
1188                   ei(num_gp)     = e(k+1,j+1,i)
1189                   dissi(num_gp)  = diss(k+1,j+1,i)
1190                   de_dxi(num_gp) = de_dx(k+1,j+1,i)
1191                   de_dyi(num_gp) = de_dy(k+1,j+1,i)
1192                   de_dzi(num_gp) = de_dz(k+1,j+1,i)
1193                ENDIF
1194 
1195                IF ( k > nzb_s_inner(j,i+1)  .OR.  nzb_s_inner(j,i+1) == 0 ) &
1196                THEN
1197                   num_gp = num_gp + 1
1198                   gp_outside_of_building(5) = 1
1199                   location(num_gp,1) = (i+1) * dx
1200                   location(num_gp,2) = j * dy
1201                   location(num_gp,3) = k * dz - 0.5 * dz
1202                   ei(num_gp)     = e(k,j,i+1)
1203                   dissi(num_gp)  = diss(k,j,i+1)
1204                   de_dxi(num_gp) = de_dx(k,j,i+1)
1205                   de_dyi(num_gp) = de_dy(k,j,i+1)
1206                   de_dzi(num_gp) = de_dz(k,j,i+1)
1207                ENDIF
1208
1209                IF ( k > nzb_s_inner(j+1,i+1) .OR. nzb_s_inner(j+1,i+1) == 0 ) &
1210                THEN
1211                   num_gp = num_gp + 1
1212                   gp_outside_of_building(6) = 1
1213                   location(num_gp,1) = (i+1) * dx
1214                   location(num_gp,2) = (j+1) * dy
1215                   location(num_gp,3) = k * dz - 0.5 * dz
1216                   ei(num_gp)     = e(k,j+1,i+1)
1217                   dissi(num_gp)  = diss(k,j+1,i+1)
1218                   de_dxi(num_gp) = de_dx(k,j+1,i+1)
1219                   de_dyi(num_gp) = de_dy(k,j+1,i+1)
1220                   de_dzi(num_gp) = de_dz(k,j+1,i+1)
1221                ENDIF
1222
1223                IF ( k+1 > nzb_s_inner(j,i+1)  .OR.  nzb_s_inner(j,i+1) == 0 ) &
1224                THEN
1225                   num_gp = num_gp + 1
1226                   gp_outside_of_building(7) = 1
1227                   location(num_gp,1) = (i+1) * dx
1228                   location(num_gp,2) = j * dy
1229                   location(num_gp,3) = (k+1) * dz - 0.5 * dz
1230                   ei(num_gp)     = e(k+1,j,i+1)
1231                   dissi(num_gp)  = diss(k+1,j,i+1)
1232                   de_dxi(num_gp) = de_dx(k+1,j,i+1)
1233                   de_dyi(num_gp) = de_dy(k+1,j,i+1)
1234                   de_dzi(num_gp) = de_dz(k+1,j,i+1)
1235                ENDIF
1236
1237                IF ( k+1 > nzb_s_inner(j+1,i+1) .OR. nzb_s_inner(j+1,i+1) == 0)&
1238                THEN
1239                   num_gp = num_gp + 1
1240                   gp_outside_of_building(8) = 1
1241                   location(num_gp,1) = (i+1) * dx
1242                   location(num_gp,2) = (j+1) * dy
1243                   location(num_gp,3) = (k+1) * dz - 0.5 * dz
1244                   ei(num_gp)     = e(k+1,j+1,i+1)
1245                   dissi(num_gp)  = diss(k+1,j+1,i+1)
1246                   de_dxi(num_gp) = de_dx(k+1,j+1,i+1)
1247                   de_dyi(num_gp) = de_dy(k+1,j+1,i+1)
1248                   de_dzi(num_gp) = de_dz(k+1,j+1,i+1)
1249                ENDIF
1250
1251!
1252!--             If all gridpoints are situated outside of a building, then the
1253!--             ordinary interpolation scheme can be used.
1254                IF ( num_gp == 8 )  THEN
1255
1256                   x  = particles(n)%x - i * dx
1257                   y  = particles(n)%y - j * dy
1258                   aa = x**2          + y**2
1259                   bb = ( dx - x )**2 + y**2
1260                   cc = x**2          + ( dy - y )**2
1261                   dd = ( dx - x )**2 + ( dy - y )**2
1262                   gg = aa + bb + cc + dd
1263
1264                   e_int_l = (( gg-aa ) * e(k,j,i)   + ( gg-bb ) * e(k,j,i+1) &
1265                            + ( gg-cc ) * e(k,j+1,i) + ( gg-dd ) * e(k,j+1,i+1)&
1266                             ) / ( 3.0 * gg )
1267
1268                   IF ( k+1 == nzt+1 )  THEN
1269                      e_int = e_int_l
1270                   ELSE
1271                      e_int_u = ( ( gg - aa ) * e(k+1,j,i)   + &
1272                                  ( gg - bb ) * e(k+1,j,i+1) + &
1273                                  ( gg - cc ) * e(k+1,j+1,i) + &
1274                                  ( gg - dd ) * e(k+1,j+1,i+1) &
1275                                ) / ( 3.0 * gg )
1276                      e_int = e_int_l + ( particles(n)%z - zu(k) ) / dz * &
1277                                        ( e_int_u - e_int_l )
1278                   ENDIF
1279
1280!
1281!--                Interpolate the TKE gradient along x (adopt incides i,j,k
1282!--                and all position variables from above (TKE))
1283                   de_dx_int_l = ( ( gg - aa ) * de_dx(k,j,i)   + &
1284                                   ( gg - bb ) * de_dx(k,j,i+1) + &
1285                                   ( gg - cc ) * de_dx(k,j+1,i) + &
1286                                   ( gg - dd ) * de_dx(k,j+1,i+1) &
1287                                 ) / ( 3.0 * gg )
1288
1289                   IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
1290                      de_dx_int = de_dx_int_l
1291                   ELSE
1292                      de_dx_int_u = ( ( gg - aa ) * de_dx(k+1,j,i)   + &
1293                                      ( gg - bb ) * de_dx(k+1,j,i+1) + &
1294                                      ( gg - cc ) * de_dx(k+1,j+1,i) + &
1295                                      ( gg - dd ) * de_dx(k+1,j+1,i+1) &
1296                                    ) / ( 3.0 * gg )
1297                      de_dx_int = de_dx_int_l + ( particles(n)%z - zu(k) ) / &
1298                                              dz * ( de_dx_int_u - de_dx_int_l )
1299                   ENDIF
1300
1301!
1302!--                Interpolate the TKE gradient along y
1303                   de_dy_int_l = ( ( gg - aa ) * de_dy(k,j,i)   + &
1304                                   ( gg - bb ) * de_dy(k,j,i+1) + &
1305                                   ( gg - cc ) * de_dy(k,j+1,i) + &
1306                                   ( gg - dd ) * de_dy(k,j+1,i+1) &
1307                                 ) / ( 3.0 * gg )
1308                   IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
1309                      de_dy_int = de_dy_int_l
1310                   ELSE
1311                      de_dy_int_u = ( ( gg - aa ) * de_dy(k+1,j,i)   + &
1312                                      ( gg - bb ) * de_dy(k+1,j,i+1) + &
1313                                      ( gg - cc ) * de_dy(k+1,j+1,i) + &
1314                                      ( gg - dd ) * de_dy(k+1,j+1,i+1) &
1315                                    ) / ( 3.0 * gg )
1316                      de_dy_int = de_dy_int_l + ( particles(n)%z - zu(k) ) / &
1317                                              dz * ( de_dy_int_u - de_dy_int_l )
1318                   ENDIF
1319
1320!
1321!--                Interpolate the TKE gradient along z
1322                   IF ( particles(n)%z < 0.5 * dz )  THEN
1323                      de_dz_int = 0.0
1324                   ELSE
1325                      de_dz_int_l = ( ( gg - aa ) * de_dz(k,j,i)   + &
1326                                      ( gg - bb ) * de_dz(k,j,i+1) + &
1327                                      ( gg - cc ) * de_dz(k,j+1,i) + &
1328                                      ( gg - dd ) * de_dz(k,j+1,i+1) &
1329                                    ) / ( 3.0 * gg )
1330
1331                      IF ( ( k+1 == nzt+1 )  .OR.  ( k == nzb ) )  THEN
1332                         de_dz_int = de_dz_int_l
1333                      ELSE
1334                         de_dz_int_u = ( ( gg - aa ) * de_dz(k+1,j,i)   + &
1335                                         ( gg - bb ) * de_dz(k+1,j,i+1) + &
1336                                         ( gg - cc ) * de_dz(k+1,j+1,i) + &
1337                                         ( gg - dd ) * de_dz(k+1,j+1,i+1) &
1338                                       ) / ( 3.0 * gg )
1339                         de_dz_int = de_dz_int_l + ( particles(n)%z - zu(k) ) /&
1340                                              dz * ( de_dz_int_u - de_dz_int_l )
1341                      ENDIF
1342                   ENDIF
1343
1344!
1345!--                Interpolate the dissipation of TKE
1346                   diss_int_l = ( ( gg - aa ) * diss(k,j,i)   + &
1347                                  ( gg - bb ) * diss(k,j,i+1) + &
1348                                  ( gg - cc ) * diss(k,j+1,i) + &
1349                                  ( gg - dd ) * diss(k,j+1,i+1) &
1350                                ) / ( 3.0 * gg )
1351
1352                   IF ( k+1 == nzt+1 )  THEN
1353                      diss_int = diss_int_l
1354                   ELSE
1355                      diss_int_u = ( ( gg - aa ) * diss(k+1,j,i)   + &
1356                                     ( gg - bb ) * diss(k+1,j,i+1) + &
1357                                     ( gg - cc ) * diss(k+1,j+1,i) + &
1358                                     ( gg - dd ) * diss(k+1,j+1,i+1) &
1359                                   ) / ( 3.0 * gg )
1360                      diss_int = diss_int_l + ( particles(n)%z - zu(k) ) / dz *&
1361                                              ( diss_int_u - diss_int_l )
1362                   ENDIF
1363
1364                ELSE
1365
1366!
1367!--                If wall between gridpoint 1 and gridpoint 5, then
1368!--                Neumann boundary condition has to be applied
1369                   IF ( gp_outside_of_building(1) == 1  .AND. &
1370                        gp_outside_of_building(5) == 0 )  THEN
1371                      num_gp = num_gp + 1
1372                      location(num_gp,1) = i * dx + 0.5 * dx
1373                      location(num_gp,2) = j * dy
1374                      location(num_gp,3) = k * dz - 0.5 * dz
1375                      ei(num_gp)     = e(k,j,i)
1376                      dissi(num_gp)  = diss(k,j,i)
1377                      de_dxi(num_gp) = 0.0
1378                      de_dyi(num_gp) = de_dy(k,j,i)
1379                      de_dzi(num_gp) = de_dz(k,j,i)
1380                   ENDIF
1381
1382                   IF ( gp_outside_of_building(5) == 1  .AND. &
1383                      gp_outside_of_building(1) == 0 )  THEN
1384                      num_gp = num_gp + 1
1385                      location(num_gp,1) = i * dx + 0.5 * dx
1386                      location(num_gp,2) = j * dy
1387                      location(num_gp,3) = k * dz - 0.5 * dz
1388                      ei(num_gp)     = e(k,j,i+1)
1389                      dissi(num_gp)  = diss(k,j,i+1)
1390                      de_dxi(num_gp) = 0.0
1391                      de_dyi(num_gp) = de_dy(k,j,i+1)
1392                      de_dzi(num_gp) = de_dz(k,j,i+1)
1393                   ENDIF
1394
1395!
1396!--                If wall between gridpoint 5 and gridpoint 6, then
1397!--                then Neumann boundary condition has to be applied
1398                   IF ( gp_outside_of_building(5) == 1  .AND. &
1399                        gp_outside_of_building(6) == 0 )  THEN
1400                      num_gp = num_gp + 1
1401                      location(num_gp,1) = (i+1) * dx
1402                      location(num_gp,2) = j * dy + 0.5 * dy
1403                      location(num_gp,3) = k * dz - 0.5 * dz
1404                      ei(num_gp)     = e(k,j,i+1)
1405                      dissi(num_gp)  = diss(k,j,i+1)
1406                      de_dxi(num_gp) = de_dx(k,j,i+1)
1407                      de_dyi(num_gp) = 0.0
1408                      de_dzi(num_gp) = de_dz(k,j,i+1)
1409                   ENDIF
1410
1411                   IF ( gp_outside_of_building(6) == 1  .AND. &
1412                        gp_outside_of_building(5) == 0 )  THEN
1413                      num_gp = num_gp + 1
1414                      location(num_gp,1) = (i+1) * dx
1415                      location(num_gp,2) = j * dy + 0.5 * dy
1416                      location(num_gp,3) = k * dz - 0.5 * dz
1417                      ei(num_gp)     = e(k,j+1,i+1)
1418                      dissi(num_gp)  = diss(k,j+1,i+1)
1419                      de_dxi(num_gp) = de_dx(k,j+1,i+1)
1420                      de_dyi(num_gp) = 0.0
1421                      de_dzi(num_gp) = de_dz(k,j+1,i+1)
1422                   ENDIF
1423
1424!
1425!--                If wall between gridpoint 2 and gridpoint 6, then
1426!--                Neumann boundary condition has to be applied
1427                   IF ( gp_outside_of_building(2) == 1  .AND. &
1428                        gp_outside_of_building(6) == 0 )  THEN
1429                      num_gp = num_gp + 1
1430                      location(num_gp,1) = i * dx + 0.5 * dx
1431                      location(num_gp,2) = (j+1) * dy
1432                      location(num_gp,3) = k * dz - 0.5 * dz
1433                      ei(num_gp)     = e(k,j+1,i)
1434                      dissi(num_gp)  = diss(k,j+1,i)
1435                      de_dxi(num_gp) = 0.0
1436                      de_dyi(num_gp) = de_dy(k,j+1,i)
1437                      de_dzi(num_gp) = de_dz(k,j+1,i)
1438                   ENDIF
1439
1440                   IF ( gp_outside_of_building(6) == 1  .AND. &
1441                        gp_outside_of_building(2) == 0 )  THEN
1442                      num_gp = num_gp + 1
1443                      location(num_gp,1) = i * dx + 0.5 * dx
1444                      location(num_gp,2) = (j+1) * dy
1445                      location(num_gp,3) = k * dz - 0.5 * dz
1446                      ei(num_gp)     = e(k,j+1,i+1)
1447                      dissi(num_gp)  = diss(k,j+1,i+1)
1448                      de_dxi(num_gp) = 0.0
1449                      de_dyi(num_gp) = de_dy(k,j+1,i+1)
1450                      de_dzi(num_gp) = de_dz(k,j+1,i+1)
1451                   ENDIF
1452
1453!
1454!--                If wall between gridpoint 1 and gridpoint 2, then
1455!--                Neumann boundary condition has to be applied
1456                   IF ( gp_outside_of_building(1) == 1  .AND. &
1457                        gp_outside_of_building(2) == 0 )  THEN
1458                      num_gp = num_gp + 1
1459                      location(num_gp,1) = i * dx
1460                      location(num_gp,2) = j * dy + 0.5 * dy
1461                      location(num_gp,3) = k * dz - 0.5 * dz
1462                      ei(num_gp)     = e(k,j,i)
1463                      dissi(num_gp)  = diss(k,j,i)
1464                      de_dxi(num_gp) = de_dx(k,j,i)
1465                      de_dyi(num_gp) = 0.0
1466                      de_dzi(num_gp) = de_dz(k,j,i)
1467                   ENDIF
1468
1469                   IF ( gp_outside_of_building(2) == 1  .AND. &
1470                        gp_outside_of_building(1) == 0 )  THEN
1471                      num_gp = num_gp + 1
1472                      location(num_gp,1) = i * dx
1473                      location(num_gp,2) = j * dy + 0.5 * dy
1474                      location(num_gp,3) = k * dz - 0.5 * dz
1475                      ei(num_gp)     = e(k,j+1,i)
1476                      dissi(num_gp)  = diss(k,j+1,i)
1477                      de_dxi(num_gp) = de_dx(k,j+1,i)
1478                      de_dyi(num_gp) = 0.0
1479                      de_dzi(num_gp) = de_dz(k,j+1,i)
1480                   ENDIF
1481
1482!
1483!--                If wall between gridpoint 3 and gridpoint 7, then
1484!--                Neumann boundary condition has to be applied
1485                   IF ( gp_outside_of_building(3) == 1  .AND. &
1486                        gp_outside_of_building(7) == 0 )  THEN
1487                      num_gp = num_gp + 1
1488                      location(num_gp,1) = i * dx + 0.5 * dx
1489                      location(num_gp,2) = j * dy
1490                      location(num_gp,3) = k * dz + 0.5 * dz
1491                      ei(num_gp)     = e(k+1,j,i)
1492                      dissi(num_gp)  = diss(k+1,j,i)
1493                      de_dxi(num_gp) = 0.0
1494                      de_dyi(num_gp) = de_dy(k+1,j,i)
1495                      de_dzi(num_gp) = de_dz(k+1,j,i) 
1496                   ENDIF
1497
1498                   IF ( gp_outside_of_building(7) == 1  .AND. &
1499                        gp_outside_of_building(3) == 0 )  THEN
1500                      num_gp = num_gp + 1
1501                      location(num_gp,1) = i * dx + 0.5 * dx
1502                      location(num_gp,2) = j * dy
1503                      location(num_gp,3) = k * dz + 0.5 * dz
1504                      ei(num_gp)     = e(k+1,j,i+1)
1505                      dissi(num_gp)  = diss(k+1,j,i+1)
1506                      de_dxi(num_gp) = 0.0
1507                      de_dyi(num_gp) = de_dy(k+1,j,i+1)
1508                      de_dzi(num_gp) = de_dz(k+1,j,i+1)
1509                   ENDIF
1510
1511!
1512!--                If wall between gridpoint 7 and gridpoint 8, then
1513!--                Neumann boundary condition has to be applied
1514                   IF ( gp_outside_of_building(7) == 1  .AND. &
1515                        gp_outside_of_building(8) == 0 )  THEN
1516                      num_gp = num_gp + 1
1517                      location(num_gp,1) = (i+1) * dx
1518                      location(num_gp,2) = j * dy + 0.5 * dy
1519                      location(num_gp,3) = k * dz + 0.5 * dz
1520                      ei(num_gp)     = e(k+1,j,i+1)
1521                      dissi(num_gp)  = diss(k+1,j,i+1)
1522                      de_dxi(num_gp) = de_dx(k+1,j,i+1)
1523                      de_dyi(num_gp) = 0.0
1524                      de_dzi(num_gp) = de_dz(k+1,j,i+1)
1525                   ENDIF
1526
1527                   IF ( gp_outside_of_building(8) == 1  .AND. &
1528                        gp_outside_of_building(7) == 0 )  THEN
1529                      num_gp = num_gp + 1
1530                      location(num_gp,1) = (i+1) * dx
1531                      location(num_gp,2) = j * dy + 0.5 * dy
1532                      location(num_gp,3) = k * dz + 0.5 * dz
1533                      ei(num_gp)     = e(k+1,j+1,i+1)
1534                      dissi(num_gp)  = diss(k+1,j+1,i+1)
1535                      de_dxi(num_gp) = de_dx(k+1,j+1,i+1)
1536                      de_dyi(num_gp) = 0.0
1537                      de_dzi(num_gp) = de_dz(k+1,j+1,i+1)
1538                   ENDIF
1539
1540!
1541!--                If wall between gridpoint 4 and gridpoint 8, then
1542!--                Neumann boundary condition has to be applied
1543                   IF ( gp_outside_of_building(4) == 1  .AND. &
1544                        gp_outside_of_building(8) == 0 )  THEN
1545                      num_gp = num_gp + 1
1546                      location(num_gp,1) = i * dx + 0.5 * dx
1547                      location(num_gp,2) = (j+1) * dy
1548                      location(num_gp,3) = k * dz + 0.5 * dz
1549                      ei(num_gp)     = e(k+1,j+1,i)
1550                      dissi(num_gp)  = diss(k+1,j+1,i)
1551                      de_dxi(num_gp) = 0.0
1552                      de_dyi(num_gp) = de_dy(k+1,j+1,i)
1553                      de_dzi(num_gp) = de_dz(k+1,j+1,i)
1554                   ENDIF
1555
1556                   IF ( gp_outside_of_building(8) == 1  .AND. &
1557                        gp_outside_of_building(4) == 0 )  THEN
1558                      num_gp = num_gp + 1
1559                      location(num_gp,1) = i * dx + 0.5 * dx
1560                      location(num_gp,2) = (j+1) * dy
1561                      location(num_gp,3) = k * dz + 0.5 * dz
1562                      ei(num_gp)     = e(k+1,j+1,i+1)
1563                      dissi(num_gp)  = diss(k+1,j+1,i+1)
1564                      de_dxi(num_gp) = 0.0
1565                      de_dyi(num_gp) = de_dy(k+1,j+1,i+1)
1566                      de_dzi(num_gp) = de_dz(k+1,j+1,i+1)
1567                   ENDIF
1568
1569!
1570!--                If wall between gridpoint 3 and gridpoint 4, then
1571!--                Neumann boundary condition has to be applied
1572                   IF ( gp_outside_of_building(3) == 1  .AND. &
1573                        gp_outside_of_building(4) == 0 )  THEN
1574                      num_gp = num_gp + 1
1575                      location(num_gp,1) = i * dx
1576                      location(num_gp,2) = j * dy + 0.5 * dy
1577                      location(num_gp,3) = k * dz + 0.5 * dz
1578                      ei(num_gp)     = e(k+1,j,i)
1579                      dissi(num_gp)  = diss(k+1,j,i)
1580                      de_dxi(num_gp) = de_dx(k+1,j,i)
1581                      de_dyi(num_gp) = 0.0
1582                      de_dzi(num_gp) = de_dz(k+1,j,i)
1583                   ENDIF
1584
1585                   IF ( gp_outside_of_building(4) == 1  .AND. &
1586                        gp_outside_of_building(3) == 0 )  THEN
1587                      num_gp = num_gp + 1
1588                      location(num_gp,1) = i * dx
1589                      location(num_gp,2) = j * dy + 0.5 * dy
1590                      location(num_gp,3) = k * dz + 0.5 * dz
1591                      ei(num_gp)     = e(k+1,j+1,i)
1592                      dissi(num_gp)  = diss(k+1,j+1,i)
1593                      de_dxi(num_gp) = de_dx(k+1,j+1,i)
1594                      de_dyi(num_gp) = 0.0
1595                      de_dzi(num_gp) = de_dz(k+1,j+1,i)
1596                   ENDIF
1597
1598!
1599!--                If wall between gridpoint 1 and gridpoint 3, then
1600!--                Neumann boundary condition has to be applied
1601!--                (only one case as only building beneath is possible)
1602                   IF ( gp_outside_of_building(1) == 0  .AND. &
1603                        gp_outside_of_building(3) == 1 )  THEN
1604                      num_gp = num_gp + 1
1605                      location(num_gp,1) = i * dx
1606                      location(num_gp,2) = j * dy
1607                      location(num_gp,3) = k * dz
1608                      ei(num_gp)     = e(k+1,j,i)
1609                      dissi(num_gp)  = diss(k+1,j,i)
1610                      de_dxi(num_gp) = de_dx(k+1,j,i)
1611                      de_dyi(num_gp) = de_dy(k+1,j,i)
1612                      de_dzi(num_gp) = 0.0
1613                   ENDIF
1614 
1615!
1616!--                If wall between gridpoint 5 and gridpoint 7, then
1617!--                Neumann boundary condition has to be applied
1618!--                (only one case as only building beneath is possible)
1619                   IF ( gp_outside_of_building(5) == 0  .AND. &
1620                        gp_outside_of_building(7) == 1 )  THEN
1621                      num_gp = num_gp + 1
1622                      location(num_gp,1) = (i+1) * dx
1623                      location(num_gp,2) = j * dy
1624                      location(num_gp,3) = k * dz
1625                      ei(num_gp)     = e(k+1,j,i+1)
1626                      dissi(num_gp)  = diss(k+1,j,i+1)
1627                      de_dxi(num_gp) = de_dx(k+1,j,i+1)
1628                      de_dyi(num_gp) = de_dy(k+1,j,i+1)
1629                      de_dzi(num_gp) = 0.0
1630                   ENDIF
1631
1632!
1633!--                If wall between gridpoint 2 and gridpoint 4, then
1634!--                Neumann boundary condition has to be applied
1635!--                (only one case as only building beneath is possible)
1636                   IF ( gp_outside_of_building(2) == 0  .AND. &
1637                        gp_outside_of_building(4) == 1 )  THEN
1638                      num_gp = num_gp + 1
1639                      location(num_gp,1) = i * dx
1640                      location(num_gp,2) = (j+1) * dy
1641                      location(num_gp,3) = k * dz
1642                      ei(num_gp)     = e(k+1,j+1,i)
1643                      dissi(num_gp)  = diss(k+1,j+1,i)
1644                      de_dxi(num_gp) = de_dx(k+1,j+1,i)
1645                      de_dyi(num_gp) = de_dy(k+1,j+1,i)
1646                      de_dzi(num_gp) = 0.0
1647                   ENDIF
1648
1649!
1650!--                If wall between gridpoint 6 and gridpoint 8, then
1651!--                Neumann boundary condition has to be applied
1652!--                (only one case as only building beneath is possible)
1653                   IF ( gp_outside_of_building(6) == 0  .AND. &
1654                        gp_outside_of_building(8) == 1 )  THEN
1655                      num_gp = num_gp + 1
1656                      location(num_gp,1) = (i+1) * dx
1657                      location(num_gp,2) = (j+1) * dy
1658                      location(num_gp,3) = k * dz
1659                      ei(num_gp)     = e(k+1,j+1,i+1)
1660                      dissi(num_gp)  = diss(k+1,j+1,i+1)
1661                      de_dxi(num_gp) = de_dx(k+1,j+1,i+1)
1662                      de_dyi(num_gp) = de_dy(k+1,j+1,i+1)
1663                      de_dzi(num_gp) = 0.0
1664                   ENDIF
1665
1666!
1667!--                Carry out the interpolation
1668                   IF ( num_gp == 1 )  THEN
1669!
1670!--                   If only one of the gridpoints is situated outside of the
1671!--                   building, it follows that the values at the particle
1672!--                   location are the same as the gridpoint values
1673                      e_int     = ei(num_gp)
1674                      diss_int  = dissi(num_gp)
1675                      de_dx_int = de_dxi(num_gp)
1676                      de_dy_int = de_dyi(num_gp)
1677                      de_dz_int = de_dzi(num_gp)
1678                   ELSE IF ( num_gp > 1 )  THEN
1679
1680                      d_sum = 0.0
1681!
1682!--                   Evaluation of the distances between the gridpoints
1683!--                   contributing to the interpolated values, and the particle
1684!--                   location
1685                      DO agp = 1, num_gp
1686                         d_gp_pl(agp) = ( particles(n)%x-location(agp,1) )**2  &
1687                                      + ( particles(n)%y-location(agp,2) )**2  &
1688                                      + ( particles(n)%z-location(agp,3) )**2
1689                         d_sum        = d_sum + d_gp_pl(agp)
1690                      ENDDO
1691
1692!
1693!--                   Finally the interpolation can be carried out
1694                      e_int     = 0.0
1695                      diss_int  = 0.0
1696                      de_dx_int = 0.0
1697                      de_dy_int = 0.0
1698                      de_dz_int = 0.0
1699                      DO agp = 1, num_gp
1700                         e_int     = e_int     + ( d_sum - d_gp_pl(agp) ) * &
1701                                                ei(agp) / ( (num_gp-1) * d_sum )
1702                         diss_int  = diss_int  + ( d_sum - d_gp_pl(agp) ) * &
1703                                             dissi(agp) / ( (num_gp-1) * d_sum )
1704                         de_dx_int = de_dx_int + ( d_sum - d_gp_pl(agp) ) * &
1705                                            de_dxi(agp) / ( (num_gp-1) * d_sum )
1706                         de_dy_int = de_dy_int + ( d_sum - d_gp_pl(agp) ) * &
1707                                            de_dyi(agp) / ( (num_gp-1) * d_sum )
1708                         de_dz_int = de_dz_int + ( d_sum - d_gp_pl(agp) ) * &
1709                                            de_dzi(agp) / ( (num_gp-1) * d_sum )
1710                      ENDDO
1711
1712                   ENDIF
1713
1714                ENDIF
1715
1716             ENDIF 
1717
1718!
1719!--          Vertically interpolate the horizontally averaged SGS TKE and
1720!--          resolved-scale velocity variances and use the interpolated values
1721!--          to calculate the coefficient fs, which is a measure of the ratio
1722!--          of the subgrid-scale turbulent kinetic energy to the total amount
1723!--          of turbulent kinetic energy.
1724             IF ( k == 0 )  THEN
1725                e_mean_int = hom(0,1,8,0)
1726             ELSE
1727                e_mean_int = hom(k,1,8,0) +                                    &
1728                                           ( hom(k+1,1,8,0) - hom(k,1,8,0) ) / &
1729                                           ( zu(k+1) - zu(k) ) *               &
1730                                           ( particles(n)%z - zu(k) )
1731             ENDIF
1732
1733             kw = particles(n)%z / dz
1734
1735             IF ( k == 0 )  THEN
1736                aa  = hom(k+1,1,30,0)  * ( particles(n)%z / &
1737                                         ( 0.5 * ( zu(k+1) - zu(k) ) ) )
1738                bb  = hom(k+1,1,31,0)  * ( particles(n)%z / &
1739                                         ( 0.5 * ( zu(k+1) - zu(k) ) ) )
1740                cc  = hom(kw+1,1,32,0) * ( particles(n)%z / &
1741                                         ( 1.0 * ( zw(kw+1) - zw(kw) ) ) )
1742             ELSE
1743                aa  = hom(k,1,30,0) + ( hom(k+1,1,30,0) - hom(k,1,30,0) ) * &
1744                           ( ( particles(n)%z - zu(k) ) / ( zu(k+1) - zu(k) ) )
1745                bb  = hom(k,1,31,0) + ( hom(k+1,1,31,0) - hom(k,1,31,0) ) * &
1746                           ( ( particles(n)%z - zu(k) ) / ( zu(k+1) - zu(k) ) )
1747                cc  = hom(kw,1,32,0) + ( hom(kw+1,1,32,0)-hom(kw,1,32,0) ) *&
1748                           ( ( particles(n)%z - zw(kw) ) / ( zw(kw+1)-zw(kw) ) )
1749             ENDIF
1750
1751             vv_int = ( 1.0 / 3.0 ) * ( aa + bb + cc )
1752
1753             fs_int = ( 2.0 / 3.0 ) * e_mean_int / &
1754                         ( vv_int + ( 2.0 / 3.0 ) * e_mean_int )
1755
1756!
1757!--          Calculate the Lagrangian timescale according to the suggestion of
1758!--          Weil et al. (2004).
1759             lagr_timescale = ( 4.0 * e_int ) / &
1760                              ( 3.0 * fs_int * c_0 * diss_int )
1761
1762!
1763!--          Calculate the next particle timestep. dt_gap is the time needed to
1764!--          complete the current LES timestep.
1765             dt_gap = dt_3d - particles(n)%dt_sum
1766             dt_particle = MIN( dt_3d, 0.025 * lagr_timescale, dt_gap )
1767
1768!
1769!--          The particle timestep should not be too small in order to prevent
1770!--          the number of particle timesteps of getting too large
1771             IF ( dt_particle < dt_min_part   .AND.  dt_min_part < dt_gap ) &
1772             THEN
1773                dt_particle = dt_min_part 
1774             ENDIF
1775
1776!
1777!--          Calculate the SGS velocity components
1778             IF ( particles(n)%age == 0.0 )  THEN
1779!
1780!--             For new particles the SGS components are derived from the SGS
1781!--             TKE. Limit the Gaussian random number to the interval
1782!--             [-5.0*sigma, 5.0*sigma] in order to prevent the SGS velocities
1783!--             from becoming unrealistically large.
1784                particles(n)%speed_x_sgs = SQRT( 2.0 * sgs_wfu_part * e_int ) *&
1785                                           ( random_gauss( iran_part, 5.0 )    &
1786                                             - 1.0 )
1787                particles(n)%speed_y_sgs = SQRT( 2.0 * sgs_wfv_part * e_int ) *&
1788                                           ( random_gauss( iran_part, 5.0 )    &
1789                                             - 1.0 )
1790                particles(n)%speed_z_sgs = SQRT( 2.0 * sgs_wfw_part * e_int ) *&
1791                                           ( random_gauss( iran_part, 5.0 )    &
1792                                             - 1.0 )
1793
1794             ELSE
1795
1796!
1797!--             Restriction of the size of the new timestep: compared to the
1798!--             previous timestep the increase must not exceed 200%
1799
1800                dt_particle_m = particles(n)%age - particles(n)%age_m
1801                IF ( dt_particle > 2.0 * dt_particle_m )  THEN
1802                   dt_particle = 2.0 * dt_particle_m
1803                ENDIF
1804
1805!
1806!--             For old particles the SGS components are correlated with the
1807!--             values from the previous timestep. Random numbers have also to
1808!--             be limited (see above).
1809!--             As negative values for the subgrid TKE are not allowed, the
1810!--             change of the subgrid TKE with time cannot be smaller than
1811!--             -e_int/dt_particle. This value is used as a lower boundary
1812!--             value for the change of TKE
1813
1814                de_dt_min = - e_int / dt_particle
1815
1816                de_dt = ( e_int - particles(n)%e_m ) / dt_particle_m
1817
1818                IF ( de_dt < de_dt_min )  THEN
1819                   de_dt = de_dt_min
1820                ENDIF
1821
1822                particles(n)%speed_x_sgs = particles(n)%speed_x_sgs -          &
1823                       fs_int * c_0 * diss_int * particles(n)%speed_x_sgs *    &
1824                       dt_particle / ( 4.0 * sgs_wfu_part * e_int ) +          &
1825                       ( 2.0 * sgs_wfu_part * de_dt *                          &
1826                               particles(n)%speed_x_sgs /                      &
1827                         ( 2.0 * sgs_wfu_part * e_int ) + de_dx_int            &
1828                       ) * dt_particle / 2.0                        +          &
1829                       SQRT( fs_int * c_0 * diss_int ) *                       &
1830                       ( random_gauss( iran_part, 5.0 ) - 1.0 ) *              &
1831                       SQRT( dt_particle )
1832
1833                particles(n)%speed_y_sgs = particles(n)%speed_y_sgs -          &
1834                       fs_int * c_0 * diss_int * particles(n)%speed_y_sgs *    &
1835                       dt_particle / ( 4.0 * sgs_wfv_part * e_int ) +          &
1836                       ( 2.0 * sgs_wfv_part * de_dt *                          &
1837                               particles(n)%speed_y_sgs /                      &
1838                         ( 2.0 * sgs_wfv_part * e_int ) + de_dy_int            &
1839                       ) * dt_particle / 2.0                        +          &
1840                       SQRT( fs_int * c_0 * diss_int ) *                       &
1841                       ( random_gauss( iran_part, 5.0 ) - 1.0 ) *              &
1842                       SQRT( dt_particle )
1843
1844                particles(n)%speed_z_sgs = particles(n)%speed_z_sgs -          &
1845                       fs_int * c_0 * diss_int * particles(n)%speed_z_sgs *    &
1846                       dt_particle / ( 4.0 * sgs_wfw_part * e_int ) +          &
1847                       ( 2.0 * sgs_wfw_part * de_dt *                          &
1848                               particles(n)%speed_z_sgs /                      &
1849                         ( 2.0 * sgs_wfw_part * e_int ) + de_dz_int            &
1850                       ) * dt_particle / 2.0                        +          &
1851                       SQRT( fs_int * c_0 * diss_int ) *                       &
1852                       ( random_gauss( iran_part, 5.0 ) - 1.0 ) *              &
1853                       SQRT( dt_particle )
1854
1855             ENDIF
1856
1857             u_int = u_int + particles(n)%speed_x_sgs
1858             v_int = v_int + particles(n)%speed_y_sgs
1859             w_int = w_int + particles(n)%speed_z_sgs
1860
1861!
1862!--          Store the SGS TKE of the current timelevel which is needed for
1863!--          for calculating the SGS particle velocities at the next timestep
1864             particles(n)%e_m = e_int
1865
1866          ELSE
1867!
1868!--          If no SGS velocities are used, only the particle timestep has to
1869!--          be set
1870             dt_particle = dt_3d
1871
1872          ENDIF
1873
1874!
1875!--       Remember the old age of the particle ( needed to prevent that a
1876!--       particle crosses several PEs during one timestep and for the
1877!--       evaluation of the subgrid particle velocity fluctuations )
1878          particles(n)%age_m = particles(n)%age
1879
1880
1881!
1882!--       Particle advection
1883          IF ( particle_groups(particles(n)%group)%density_ratio == 0.0 )  THEN
1884!
1885!--          Pure passive transport (without particle inertia)
1886             particles(n)%x = particles(n)%x + u_int * dt_particle
1887             particles(n)%y = particles(n)%y + v_int * dt_particle
1888             particles(n)%z = particles(n)%z + w_int * dt_particle
1889
1890             particles(n)%speed_x = u_int
1891             particles(n)%speed_y = v_int
1892             particles(n)%speed_z = w_int
1893
1894          ELSE
1895!
1896!--          Transport of particles with inertia
1897             particles(n)%x = particles(n)%x + particles(n)%speed_x * &
1898                                               dt_particle
1899             particles(n)%y = particles(n)%y + particles(n)%speed_y * &
1900                                               dt_particle
1901             particles(n)%z = particles(n)%z + particles(n)%speed_z * &
1902                                               dt_particle
1903
1904!
1905!--          Update of the particle velocity
1906             dens_ratio = particle_groups(particles(n)%group)%density_ratio
1907             IF ( cloud_droplets )  THEN
1908                exp_arg  = 4.5 * dens_ratio * molecular_viscosity /        &
1909                           ( particles(n)%radius )**2 *                    &
1910                           ( 1.0 + 0.15 * ( 2.0 * particles(n)%radius *    &
1911                             SQRT( ( u_int - particles(n)%speed_x )**2 +   &
1912                                   ( v_int - particles(n)%speed_y )**2 +   &
1913                                   ( w_int - particles(n)%speed_z )**2 ) / &
1914                                            molecular_viscosity )**0.687   &
1915                           )
1916                exp_term = EXP( -exp_arg * dt_particle )
1917             ELSEIF ( use_sgs_for_particles )  THEN
1918                exp_arg  = particle_groups(particles(n)%group)%exp_arg
1919                exp_term = EXP( -exp_arg * dt_particle )
1920             ELSE
1921                exp_arg  = particle_groups(particles(n)%group)%exp_arg
1922                exp_term = particle_groups(particles(n)%group)%exp_term
1923             ENDIF
1924             particles(n)%speed_x = particles(n)%speed_x * exp_term + &
1925                                    u_int * ( 1.0 - exp_term )
1926              particles(n)%speed_y = particles(n)%speed_y * exp_term + &
1927                                    v_int * ( 1.0 - exp_term )
1928              particles(n)%speed_z = particles(n)%speed_z * exp_term +       &
1929                              ( w_int - ( 1.0 - dens_ratio ) * g / exp_arg ) &
1930                                    * ( 1.0 - exp_term )
1931          ENDIF
1932
1933!
1934!--       Increment the particle age and the total time that the particle
1935!--       has advanced within the particle timestep procedure
1936          particles(n)%age    = particles(n)%age    + dt_particle
1937          particles(n)%dt_sum = particles(n)%dt_sum + dt_particle
1938
1939!
1940!--       Check whether there is still a particle that has not yet completed
1941!--       the total LES timestep
1942          IF ( ( dt_3d - particles(n)%dt_sum ) > 1E-8 )  THEN
1943             dt_3d_reached_l = .FALSE.
1944          ENDIF
1945
1946       ENDDO   ! advection loop
1947
1948!
1949!--    Particle reflection from walls
1950       CALL particle_boundary_conds
1951
1952!
1953!--    User-defined actions after the calculation of the new particle position
1954       CALL user_advec_particles
1955
1956!
1957!--    Find out, if all particles on every PE have completed the LES timestep
1958!--    and set the switch corespondingly
1959#if defined( __parallel )
1960       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1961       CALL MPI_ALLREDUCE( dt_3d_reached_l, dt_3d_reached, 1, MPI_LOGICAL, &
1962                           MPI_LAND, comm2d, ierr )
1963#else
1964       dt_3d_reached = dt_3d_reached_l
1965#endif     
1966
1967       CALL cpu_log( log_point_s(44), 'advec_part_advec', 'stop' )
1968
1969!
1970!--    Increment time since last release
1971       IF ( dt_3d_reached )  time_prel = time_prel + dt_3d
1972
1973!    WRITE ( 9, * ) '*** advec_particles: ##0.4'
1974!    CALL local_flush( 9 )
1975!    nd = 0
1976!    DO  n = 1, number_of_particles
1977!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
1978!    ENDDO
1979!    IF ( nd /= deleted_particles ) THEN
1980!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
1981!       CALL local_flush( 9 )
1982!       CALL MPI_ABORT( comm2d, 9999, ierr )
1983!    ENDIF
1984
1985!
1986!--    If necessary, release new set of particles
1987       IF ( time_prel >= dt_prel  .AND.  end_time_prel > simulated_time  .AND. &
1988            dt_3d_reached )  THEN
1989
1990!
1991!--       Check, if particle storage must be extended
1992          IF ( number_of_particles + number_of_initial_particles > &
1993               maximum_number_of_particles  )  THEN
1994             IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
1995                message_string = 'maximum_number_of_particles ' //   &
1996                                 'needs to be increased ' //         &
1997                                 '&but this is not allowed with ' // &
1998                                 'netcdf_data_format < 3'
1999                CALL message( 'advec_particles', 'PA0146', 2, 2, -1, 6, 1 )
2000             ELSE
2001!    WRITE ( 9, * ) '*** advec_particles: before allocate_prt_memory dt_prel'
2002!    CALL local_flush( 9 )
2003                CALL allocate_prt_memory( number_of_initial_particles )
2004!    WRITE ( 9, * ) '*** advec_particles: after allocate_prt_memory dt_prel'
2005!    CALL local_flush( 9 )
2006             ENDIF
2007          ENDIF
2008
2009!
2010!--       Check, if tail storage must be extended
2011          IF ( use_particle_tails )  THEN
2012             IF ( number_of_tails + number_of_initial_tails > &
2013                  maximum_number_of_tails  )  THEN
2014                IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2015                   message_string = 'maximum_number_of_tails ' //    &
2016                                    'needs to be increased ' //      &
2017                                    '&but this is not allowed wi' // &
2018                                    'th netcdf_data_format < 3'
2019                   CALL message( 'advec_particles', 'PA0147', 2, 2, -1, 6, 1 )
2020                ELSE
2021!    WRITE ( 9, * ) '*** advec_particles: before allocate_tail_memory dt_prel'
2022!    CALL local_flush( 9 )
2023                   CALL allocate_tail_memory( number_of_initial_tails )
2024!    WRITE ( 9, * ) '*** advec_particles: after allocate_tail_memory dt_prel'
2025!    CALL local_flush( 9 )
2026                ENDIF
2027             ENDIF
2028          ENDIF
2029
2030!
2031!--       The MOD function allows for changes in the output interval with
2032!--       restart runs.
2033          time_prel = MOD( time_prel, MAX( dt_prel, dt_3d ) )
2034          IF ( number_of_initial_particles /= 0 )  THEN
2035             is = number_of_particles+1
2036             ie = number_of_particles+number_of_initial_particles
2037             particles(is:ie) = initial_particles(1:number_of_initial_particles)
2038!
2039!--          Add random fluctuation to particle positions. Particles should
2040!--          remain in the subdomain.
2041             IF ( random_start_position )  THEN
2042                DO  n = is, ie
2043                   IF ( psl(particles(n)%group) /= psr(particles(n)%group) ) &
2044                   THEN
2045                      particles(n)%x = particles(n)%x + &
2046                                       ( random_function( iran_part ) - 0.5 ) *&
2047                                       pdx(particles(n)%group)
2048                      IF ( particles(n)%x  <=  ( nxl - 0.5 ) * dx )  THEN
2049                         particles(n)%x = ( nxl - 0.4999999999 ) * dx
2050                      ELSEIF ( particles(n)%x  >=  ( nxr + 0.5 ) * dx )  THEN
2051                         particles(n)%x = ( nxr + 0.4999999999 ) * dx
2052                      ENDIF
2053                   ENDIF
2054                   IF ( pss(particles(n)%group) /= psn(particles(n)%group) ) &
2055                   THEN
2056                      particles(n)%y = particles(n)%y + &
2057                                       ( random_function( iran_part ) - 0.5 ) *&
2058                                       pdy(particles(n)%group)
2059                      IF ( particles(n)%y  <=  ( nys - 0.5 ) * dy )  THEN
2060                         particles(n)%y = ( nys - 0.4999999999 ) * dy
2061                      ELSEIF ( particles(n)%y  >=  ( nyn + 0.5 ) * dy )  THEN
2062                         particles(n)%y = ( nyn + 0.4999999999 ) * dy
2063                      ENDIF
2064                   ENDIF
2065                   IF ( psb(particles(n)%group) /= pst(particles(n)%group) ) &
2066                   THEN
2067                      particles(n)%z = particles(n)%z + &
2068                                       ( random_function( iran_part ) - 0.5 ) *&
2069                                       pdz(particles(n)%group)
2070                   ENDIF
2071                ENDDO
2072             ENDIF
2073
2074!
2075!--          Set the beginning of the new particle tails and their age
2076             IF ( use_particle_tails )  THEN
2077                DO  n = is, ie
2078!
2079!--                New particles which should have a tail, already have got a
2080!--                provisional tail id unequal zero (see init_particles)
2081                   IF ( particles(n)%tail_id /= 0 )  THEN
2082                      number_of_tails = number_of_tails + 1
2083                      nn = number_of_tails
2084                      particles(n)%tail_id = nn   ! set the final tail id
2085                      particle_tail_coordinates(1,1,nn) = particles(n)%x
2086                      particle_tail_coordinates(1,2,nn) = particles(n)%y
2087                      particle_tail_coordinates(1,3,nn) = particles(n)%z
2088                      particle_tail_coordinates(1,4,nn) = particles(n)%color
2089                      particles(n)%tailpoints = 1
2090                      IF ( minimum_tailpoint_distance /= 0.0 )  THEN
2091                         particle_tail_coordinates(2,1,nn) = particles(n)%x
2092                         particle_tail_coordinates(2,2,nn) = particles(n)%y
2093                         particle_tail_coordinates(2,3,nn) = particles(n)%z
2094                         particle_tail_coordinates(2,4,nn) = particles(n)%color
2095                         particle_tail_coordinates(1:2,5,nn) = 0.0
2096                         particles(n)%tailpoints = 2
2097                      ENDIF
2098                   ENDIF
2099                ENDDO
2100             ENDIF
2101!    WRITE ( 9, * ) '*** advec_particles: after setting the beginning of new tails'
2102!    CALL local_flush( 9 )
2103
2104             number_of_particles = number_of_particles + &
2105                                   number_of_initial_particles
2106          ENDIF
2107
2108       ENDIF
2109
2110!    WRITE ( 9, * ) '*** advec_particles: ##0.5'
2111!    CALL local_flush( 9 )
2112!    nd = 0
2113!    DO  n = 1, number_of_particles
2114!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
2115!    ENDDO
2116!    IF ( nd /= deleted_particles ) THEN
2117!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
2118!       CALL local_flush( 9 )
2119!       CALL MPI_ABORT( comm2d, 9999, ierr )
2120!    ENDIF
2121
2122!    IF ( number_of_particles /= number_of_tails )  THEN
2123!       WRITE (9,*) '--- advec_particles: #2'
2124!       WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
2125!       CALL local_flush( 9 )
2126!    ENDIF
2127!    DO  n = 1, number_of_particles
2128!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
2129!       THEN
2130!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
2131!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
2132!          CALL local_flush( 9 )
2133!          CALL MPI_ABORT( comm2d, 9999, ierr )
2134!       ENDIF
2135!    ENDDO
2136
2137#if defined( __parallel )
2138!
2139!--    As soon as one particle has moved beyond the boundary of the domain, it
2140!--    is included in the relevant transfer arrays and marked for subsequent
2141!--    deletion on this PE.
2142!--    First run for crossings in x direction. Find out first the number of
2143!--    particles to be transferred and allocate temporary arrays needed to store
2144!--    them.
2145!--    For a one-dimensional decomposition along y, no transfer is necessary,
2146!--    because the particle remains on the PE.
2147       trlp_count  = 0
2148       trlpt_count = 0
2149       trrp_count  = 0
2150       trrpt_count = 0
2151       IF ( pdims(1) /= 1 )  THEN
2152!
2153!--       First calculate the storage necessary for sending and receiving the
2154!--       data
2155          DO  n = 1, number_of_particles
2156             i = ( particles(n)%x + 0.5 * dx ) * ddx
2157!
2158!--          Above calculation does not work for indices less than zero
2159             IF ( particles(n)%x < -0.5 * dx )  i = -1
2160
2161             IF ( i < nxl )  THEN
2162                trlp_count = trlp_count + 1
2163                IF ( particles(n)%tail_id /= 0 )  trlpt_count = trlpt_count + 1
2164             ELSEIF ( i > nxr )  THEN
2165                trrp_count = trrp_count + 1
2166                IF ( particles(n)%tail_id /= 0 )  trrpt_count = trrpt_count + 1
2167             ENDIF
2168          ENDDO
2169          IF ( trlp_count  == 0 )  trlp_count  = 1
2170          IF ( trlpt_count == 0 )  trlpt_count = 1
2171          IF ( trrp_count  == 0 )  trrp_count  = 1
2172          IF ( trrpt_count == 0 )  trrpt_count = 1
2173
2174          ALLOCATE( trlp(trlp_count), trrp(trrp_count) )
2175
2176          trlp = particle_type( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2177                                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2178                                0.0, 0, 0, 0, 0 )
2179          trrp = particle_type( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2180                                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2181                                0.0, 0, 0, 0, 0 )
2182
2183          IF ( use_particle_tails )  THEN
2184             ALLOCATE( trlpt(maximum_number_of_tailpoints,5,trlpt_count), &
2185                       trrpt(maximum_number_of_tailpoints,5,trrpt_count) )
2186             tlength = maximum_number_of_tailpoints * 5
2187          ENDIF
2188
2189          trlp_count  = 0
2190          trlpt_count = 0
2191          trrp_count  = 0
2192          trrpt_count = 0
2193
2194       ENDIF
2195
2196!    WRITE ( 9, * ) '*** advec_particles: ##1'
2197!    CALL local_flush( 9 )
2198!    nd = 0
2199!    DO  n = 1, number_of_particles
2200!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
2201!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
2202!       THEN
2203!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
2204!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
2205!          CALL local_flush( 9 )
2206!          CALL MPI_ABORT( comm2d, 9999, ierr )
2207!       ENDIF
2208!    ENDDO
2209!    IF ( nd /= deleted_particles ) THEN
2210!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
2211!       CALL local_flush( 9 )
2212!       CALL MPI_ABORT( comm2d, 9999, ierr )
2213!    ENDIF
2214
2215       DO  n = 1, number_of_particles
2216
2217          nn = particles(n)%tail_id
2218
2219          i = ( particles(n)%x + 0.5 * dx ) * ddx
2220!
2221!--       Above calculation does not work for indices less than zero
2222          IF ( particles(n)%x < - 0.5 * dx )  i = -1
2223
2224          IF ( i <  nxl )  THEN
2225             IF ( i < 0 )  THEN
2226!
2227!--             Apply boundary condition along x
2228                IF ( ibc_par_lr == 0 )  THEN
2229!
2230!--                Cyclic condition
2231                   IF ( pdims(1) == 1 )  THEN
2232                      particles(n)%x        = ( nx + 1 ) * dx + particles(n)%x
2233                      particles(n)%origin_x = ( nx + 1 ) * dx + &
2234                                              particles(n)%origin_x
2235                      IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2236                         i  = particles(n)%tailpoints
2237                         particle_tail_coordinates(1:i,1,nn) = ( nx + 1 ) * dx &
2238                                           + particle_tail_coordinates(1:i,1,nn)
2239                      ENDIF
2240                   ELSE
2241                      trlp_count = trlp_count + 1
2242                      trlp(trlp_count) = particles(n)
2243                      trlp(trlp_count)%x = ( nx + 1 ) * dx + trlp(trlp_count)%x
2244                      trlp(trlp_count)%origin_x = trlp(trlp_count)%origin_x + &
2245                                                  ( nx + 1 ) * dx
2246                      particle_mask(n)  = .FALSE.
2247                      deleted_particles = deleted_particles + 1
2248
2249                      IF ( trlp(trlp_count)%x >= (nx + 0.5)* dx - 1.e-12 ) THEN
2250                         trlp(trlp_count)%x = trlp(trlp_count)%x - 1.e-10
2251                         trlp(trlp_count)%origin_x = trlp(trlp_count)%origin_x &
2252                                                     - 1
2253                      ENDIF
2254
2255                      IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2256                         trlpt_count = trlpt_count + 1
2257                         trlpt(:,:,trlpt_count) = &
2258                                               particle_tail_coordinates(:,:,nn)
2259                         trlpt(:,1,trlpt_count) = ( nx + 1 ) * dx + &
2260                                                  trlpt(:,1,trlpt_count)
2261                         tail_mask(nn) = .FALSE.
2262                         deleted_tails = deleted_tails + 1
2263                      ENDIF
2264                   ENDIF
2265
2266                ELSEIF ( ibc_par_lr == 1 )  THEN
2267!
2268!--                Particle absorption
2269                   particle_mask(n) = .FALSE.
2270                   deleted_particles = deleted_particles + 1
2271                   IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2272                      tail_mask(nn) = .FALSE.
2273                      deleted_tails = deleted_tails + 1
2274                   ENDIF
2275
2276                ELSEIF ( ibc_par_lr == 2 )  THEN
2277!
2278!--                Particle reflection
2279                   particles(n)%x       = -particles(n)%x
2280                   particles(n)%speed_x = -particles(n)%speed_x
2281
2282                ENDIF
2283             ELSE
2284!
2285!--             Store particle data in the transfer array, which will be send
2286!--             to the neighbouring PE
2287                trlp_count = trlp_count + 1
2288                trlp(trlp_count) = particles(n)
2289                particle_mask(n) = .FALSE.
2290                deleted_particles = deleted_particles + 1
2291
2292                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2293                   trlpt_count = trlpt_count + 1
2294                   trlpt(:,:,trlpt_count) = particle_tail_coordinates(:,:,nn)
2295                   tail_mask(nn) = .FALSE.
2296                   deleted_tails = deleted_tails + 1
2297                ENDIF
2298            ENDIF
2299
2300          ELSEIF ( i > nxr )  THEN
2301             IF ( i > nx )  THEN
2302!
2303!--             Apply boundary condition along x
2304                IF ( ibc_par_lr == 0 )  THEN
2305!
2306!--                Cyclic condition
2307                   IF ( pdims(1) == 1 )  THEN
2308                      particles(n)%x = particles(n)%x - ( nx + 1 ) * dx
2309                      particles(n)%origin_x = particles(n)%origin_x - &
2310                                              ( nx + 1 ) * dx
2311                      IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2312                         i = particles(n)%tailpoints
2313                         particle_tail_coordinates(1:i,1,nn) = - ( nx+1 ) * dx &
2314                                           + particle_tail_coordinates(1:i,1,nn)
2315                      ENDIF
2316                   ELSE
2317                      trrp_count = trrp_count + 1
2318                      trrp(trrp_count) = particles(n)
2319                      trrp(trrp_count)%x = trrp(trrp_count)%x - ( nx + 1 ) * dx
2320                      trrp(trrp_count)%origin_x = trrp(trrp_count)%origin_x - &
2321                                                  ( nx + 1 ) * dx
2322                      particle_mask(n) = .FALSE.
2323                      deleted_particles = deleted_particles + 1
2324
2325                      IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2326                         trrpt_count = trrpt_count + 1
2327                         trrpt(:,:,trrpt_count) = &
2328                                               particle_tail_coordinates(:,:,nn)
2329                         trrpt(:,1,trrpt_count) = trrpt(:,1,trrpt_count) - &
2330                                                  ( nx + 1 ) * dx
2331                         tail_mask(nn) = .FALSE.
2332                         deleted_tails = deleted_tails + 1
2333                      ENDIF
2334                   ENDIF
2335
2336                ELSEIF ( ibc_par_lr == 1 )  THEN
2337!
2338!--                Particle absorption
2339                   particle_mask(n) = .FALSE.
2340                   deleted_particles = deleted_particles + 1
2341                   IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2342                      tail_mask(nn) = .FALSE.
2343                      deleted_tails = deleted_tails + 1
2344                   ENDIF
2345
2346                ELSEIF ( ibc_par_lr == 2 )  THEN
2347!
2348!--                Particle reflection
2349                   particles(n)%x       = 2 * ( nx * dx ) - particles(n)%x
2350                   particles(n)%speed_x = -particles(n)%speed_x
2351
2352                ENDIF
2353             ELSE
2354!
2355!--             Store particle data in the transfer array, which will be send
2356!--             to the neighbouring PE
2357                trrp_count = trrp_count + 1
2358                trrp(trrp_count) = particles(n)
2359                particle_mask(n) = .FALSE.
2360                deleted_particles = deleted_particles + 1
2361
2362                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2363                   trrpt_count = trrpt_count + 1
2364                   trrpt(:,:,trrpt_count) = particle_tail_coordinates(:,:,nn)
2365                   tail_mask(nn) = .FALSE.
2366                   deleted_tails = deleted_tails + 1
2367                ENDIF
2368             ENDIF
2369
2370          ENDIF
2371       ENDDO
2372
2373!    WRITE ( 9, * ) '*** advec_particles: ##2'
2374!    CALL local_flush( 9 )
2375!    nd = 0
2376!    DO  n = 1, number_of_particles
2377!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
2378!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
2379!       THEN
2380!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
2381!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
2382!          CALL local_flush( 9 )
2383!          CALL MPI_ABORT( comm2d, 9999, ierr )
2384!       ENDIF
2385!    ENDDO
2386!    IF ( nd /= deleted_particles ) THEN
2387!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
2388!       CALL local_flush( 9 )
2389!       CALL MPI_ABORT( comm2d, 9999, ierr )
2390!    ENDIF
2391
2392!
2393!--    Send left boundary, receive right boundary (but first exchange how many
2394!--    and check, if particle storage must be extended)
2395       IF ( pdims(1) /= 1 )  THEN
2396
2397          CALL cpu_log( log_point_s(23), 'sendrcv_particles', 'start' )
2398          CALL MPI_SENDRECV( trlp_count,      1, MPI_INTEGER, pleft,  0, &
2399                             trrp_count_recv, 1, MPI_INTEGER, pright, 0, &
2400                             comm2d, status, ierr )
2401
2402          IF ( number_of_particles + trrp_count_recv > &
2403               maximum_number_of_particles )           &
2404          THEN
2405             IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2406                 message_string = 'maximum_number_of_particles ' //    &
2407                                  'needs to be increased ' //          &
2408                                  '&but this is not allowed with ' //  &
2409                                  'netcdf-data_format < 3'
2410                CALL message( 'advec_particles', 'PA0146', 2, 2, -1, 6, 1 )
2411             ELSE
2412!    WRITE ( 9, * ) '*** advec_particles: before allocate_prt_memory trrp'
2413!    CALL local_flush( 9 )
2414                CALL allocate_prt_memory( trrp_count_recv )
2415!    WRITE ( 9, * ) '*** advec_particles: after allocate_prt_memory trrp'
2416!    CALL local_flush( 9 )
2417             ENDIF
2418          ENDIF
2419
2420          CALL MPI_SENDRECV( trlp(1)%age, trlp_count, mpi_particle_type,     &
2421                             pleft, 1, particles(number_of_particles+1)%age, &
2422                             trrp_count_recv, mpi_particle_type, pright, 1,  &
2423                             comm2d, status, ierr )
2424
2425          IF ( use_particle_tails )  THEN
2426
2427             CALL MPI_SENDRECV( trlpt_count,      1, MPI_INTEGER, pleft,  0, &
2428                                trrpt_count_recv, 1, MPI_INTEGER, pright, 0, &
2429                                comm2d, status, ierr )
2430
2431             IF ( number_of_tails+trrpt_count_recv > maximum_number_of_tails ) &
2432             THEN
2433                IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2434                   message_string = 'maximum_number_of_tails ' //   &
2435                                    'needs to be increased ' //     &
2436                                    '&but this is not allowed wi'// &
2437                                    'th netcdf_data_format < 3'
2438                   CALL message( 'advec_particles', 'PA0147', 2, 2, -1, 6, 1 )
2439                ELSE
2440!    WRITE ( 9, * ) '*** advec_particles: before allocate_tail_memory trrpt'
2441!    CALL local_flush( 9 )
2442                   CALL allocate_tail_memory( trrpt_count_recv )
2443!    WRITE ( 9, * ) '*** advec_particles: after allocate_tail_memory trrpt'
2444!    CALL local_flush( 9 )
2445                ENDIF
2446             ENDIF
2447
2448             CALL MPI_SENDRECV( trlpt(1,1,1), trlpt_count*tlength, MPI_REAL,   &
2449                                pleft, 1,                                      &
2450                             particle_tail_coordinates(1,1,number_of_tails+1), &
2451                                trrpt_count_recv*tlength, MPI_REAL, pright, 1, &
2452                                comm2d, status, ierr )
2453!
2454!--          Update the tail ids for the transferred particles
2455             nn = number_of_tails
2456             DO  n = number_of_particles+1, number_of_particles+trrp_count_recv
2457                IF ( particles(n)%tail_id /= 0 )  THEN
2458                   nn = nn + 1
2459                   particles(n)%tail_id = nn
2460                ENDIF
2461             ENDDO
2462
2463          ENDIF
2464
2465          number_of_particles = number_of_particles + trrp_count_recv
2466          number_of_tails     = number_of_tails     + trrpt_count_recv
2467!       IF ( number_of_particles /= number_of_tails )  THEN
2468!          WRITE (9,*) '--- advec_particles: #3'
2469!          WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
2470!          CALL local_flush( 9 )
2471!       ENDIF
2472
2473!
2474!--       Send right boundary, receive left boundary
2475          CALL MPI_SENDRECV( trrp_count,      1, MPI_INTEGER, pright, 0, &
2476                             trlp_count_recv, 1, MPI_INTEGER, pleft,  0, &
2477                             comm2d, status, ierr )
2478
2479          IF ( number_of_particles + trlp_count_recv > &
2480               maximum_number_of_particles )           &
2481          THEN
2482             IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2483                message_string = 'maximum_number_of_particles ' //  &
2484                                 'needs to be increased ' //        &
2485                                 '&but this is not allowed with '// &
2486                                 'netcdf_data_format < 3'
2487                CALL message( 'advec_particles', 'PA0146', 2, 2, -1, 6, 1 )
2488             ELSE
2489!    WRITE ( 9, * ) '*** advec_particles: before allocate_prt_memory trlp'
2490!    CALL local_flush( 9 )
2491                CALL allocate_prt_memory( trlp_count_recv )
2492!    WRITE ( 9, * ) '*** advec_particles: after allocate_prt_memory trlp'
2493!    CALL local_flush( 9 )
2494             ENDIF
2495          ENDIF
2496
2497          CALL MPI_SENDRECV( trrp(1)%age, trrp_count, mpi_particle_type,      &
2498                             pright, 1, particles(number_of_particles+1)%age, &
2499                             trlp_count_recv, mpi_particle_type, pleft, 1,    &
2500                             comm2d, status, ierr )
2501
2502          IF ( use_particle_tails )  THEN
2503
2504             CALL MPI_SENDRECV( trrpt_count,      1, MPI_INTEGER, pright, 0, &
2505                                trlpt_count_recv, 1, MPI_INTEGER, pleft,  0, &
2506                                comm2d, status, ierr )
2507
2508             IF ( number_of_tails+trlpt_count_recv > maximum_number_of_tails ) &
2509             THEN
2510                IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2511                   message_string = 'maximum_number_of_tails ' //   &
2512                                    'needs to be increased ' //     &
2513                                    '&but this is not allowed wi'// &
2514                                    'th netcdf_data_format < 3'
2515                   CALL message( 'advec_particles', 'PA0147', 2, 2, -1, 6, 1 ) 
2516                ELSE
2517!    WRITE ( 9, * ) '*** advec_particles: before allocate_tail_memory trlpt'
2518!    CALL local_flush( 9 )
2519                   CALL allocate_tail_memory( trlpt_count_recv )
2520!    WRITE ( 9, * ) '*** advec_particles: after allocate_tail_memory trlpt'
2521!    CALL local_flush( 9 )
2522                ENDIF
2523             ENDIF
2524
2525             CALL MPI_SENDRECV( trrpt(1,1,1), trrpt_count*tlength, MPI_REAL,   &
2526                                pright, 1,                                     &
2527                             particle_tail_coordinates(1,1,number_of_tails+1), &
2528                                trlpt_count_recv*tlength, MPI_REAL, pleft, 1,  &
2529                                comm2d, status, ierr )
2530!
2531!--          Update the tail ids for the transferred particles
2532             nn = number_of_tails
2533             DO  n = number_of_particles+1, number_of_particles+trlp_count_recv
2534                IF ( particles(n)%tail_id /= 0 )  THEN
2535                   nn = nn + 1
2536                   particles(n)%tail_id = nn
2537                ENDIF
2538             ENDDO
2539
2540          ENDIF
2541
2542          number_of_particles = number_of_particles + trlp_count_recv
2543          number_of_tails     = number_of_tails     + trlpt_count_recv
2544!       IF ( number_of_particles /= number_of_tails )  THEN
2545!          WRITE (9,*) '--- advec_particles: #4'
2546!          WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
2547!          CALL local_flush( 9 )
2548!       ENDIF
2549
2550          IF ( use_particle_tails )  THEN
2551             DEALLOCATE( trlpt, trrpt )
2552          ENDIF
2553          DEALLOCATE( trlp, trrp ) 
2554
2555          CALL cpu_log( log_point_s(23), 'sendrcv_particles', 'pause' )
2556
2557       ENDIF
2558
2559!    WRITE ( 9, * ) '*** advec_particles: ##3'
2560!    CALL local_flush( 9 )
2561!    nd = 0
2562!    DO  n = 1, number_of_particles
2563!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
2564!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
2565!       THEN
2566!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
2567!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
2568!          CALL local_flush( 9 )
2569!          CALL MPI_ABORT( comm2d, 9999, ierr )
2570!       ENDIF
2571!    ENDDO
2572!    IF ( nd /= deleted_particles ) THEN
2573!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
2574!       CALL local_flush( 9 )
2575!       CALL MPI_ABORT( comm2d, 9999, ierr )
2576!    ENDIF
2577
2578!
2579!--    Check whether particles have crossed the boundaries in y direction. Note
2580!--    that this case can also apply to particles that have just been received
2581!--    from the adjacent right or left PE.
2582!--    Find out first the number of particles to be transferred and allocate
2583!--    temporary arrays needed to store them.
2584!--    For a one-dimensional decomposition along x, no transfer is necessary,
2585!--    because the particle remains on the PE.
2586       trsp_count  = 0
2587       trspt_count = 0
2588       trnp_count  = 0
2589       trnpt_count = 0
2590       IF ( pdims(2) /= 1 )  THEN
2591!
2592!--       First calculate the storage necessary for sending and receiving the
2593!--       data
2594          DO  n = 1, number_of_particles
2595             IF ( particle_mask(n) )  THEN
2596                j = ( particles(n)%y + 0.5 * dy ) * ddy
2597!
2598!--             Above calculation does not work for indices less than zero
2599                IF ( particles(n)%y < -0.5 * dy )  j = -1
2600
2601                IF ( j < nys )  THEN
2602                   trsp_count = trsp_count + 1
2603                   IF ( particles(n)%tail_id /= 0 )  trspt_count = trspt_count+1
2604                ELSEIF ( j > nyn )  THEN
2605                   trnp_count = trnp_count + 1
2606                   IF ( particles(n)%tail_id /= 0 )  trnpt_count = trnpt_count+1
2607                ENDIF
2608             ENDIF
2609          ENDDO
2610          IF ( trsp_count  == 0 )  trsp_count  = 1
2611          IF ( trspt_count == 0 )  trspt_count = 1
2612          IF ( trnp_count  == 0 )  trnp_count  = 1
2613          IF ( trnpt_count == 0 )  trnpt_count = 1
2614
2615          ALLOCATE( trsp(trsp_count), trnp(trnp_count) )
2616
2617          trsp = particle_type( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2618                                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2619                                0.0, 0, 0, 0, 0 )
2620          trnp = particle_type( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2621                                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
2622                                0.0, 0, 0, 0, 0 )
2623
2624          IF ( use_particle_tails )  THEN
2625             ALLOCATE( trspt(maximum_number_of_tailpoints,5,trspt_count), &
2626                       trnpt(maximum_number_of_tailpoints,5,trnpt_count) )
2627             tlength = maximum_number_of_tailpoints * 5
2628          ENDIF
2629
2630          trsp_count  = 0
2631          trspt_count = 0
2632          trnp_count  = 0
2633          trnpt_count = 0
2634
2635       ENDIF
2636
2637!    WRITE ( 9, * ) '*** advec_particles: ##4'
2638!    CALL local_flush( 9 )
2639!    nd = 0
2640!    DO  n = 1, number_of_particles
2641!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
2642!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
2643!       THEN
2644!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
2645!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
2646!          CALL local_flush( 9 )
2647!          CALL MPI_ABORT( comm2d, 9999, ierr )
2648!       ENDIF
2649!    ENDDO
2650!    IF ( nd /= deleted_particles ) THEN
2651!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
2652!       CALL local_flush( 9 )
2653!       CALL MPI_ABORT( comm2d, 9999, ierr )
2654!    ENDIF
2655
2656       DO  n = 1, number_of_particles
2657
2658          nn = particles(n)%tail_id
2659!
2660!--       Only those particles that have not been marked as 'deleted' may be
2661!--       moved.
2662          IF ( particle_mask(n) )  THEN
2663             j = ( particles(n)%y + 0.5 * dy ) * ddy
2664!
2665!--          Above calculation does not work for indices less than zero
2666             IF ( particles(n)%y < -0.5 * dy )  j = -1
2667
2668             IF ( j < nys )  THEN
2669                IF ( j < 0 )  THEN
2670!
2671!--                Apply boundary condition along y
2672                   IF ( ibc_par_ns == 0 )  THEN
2673!
2674!--                   Cyclic condition
2675                      IF ( pdims(2) == 1 )  THEN
2676                         particles(n)%y = ( ny + 1 ) * dy + particles(n)%y
2677                         particles(n)%origin_y = ( ny + 1 ) * dy + &
2678                                                 particles(n)%origin_y
2679                         IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2680                            i = particles(n)%tailpoints
2681                            particle_tail_coordinates(1:i,2,nn) = ( ny+1 ) * dy&
2682                                           + particle_tail_coordinates(1:i,2,nn)
2683                         ENDIF
2684                      ELSE
2685                         trsp_count = trsp_count + 1
2686                         trsp(trsp_count) = particles(n)
2687                         trsp(trsp_count)%y = ( ny + 1 ) * dy + &
2688                                              trsp(trsp_count)%y
2689                         trsp(trsp_count)%origin_y = trsp(trsp_count)%origin_y &
2690                                              + ( ny + 1 ) * dy
2691                         particle_mask(n) = .FALSE.
2692                         deleted_particles = deleted_particles + 1
2693
2694                         IF ( trsp(trsp_count)%y >= (ny+0.5)* dy - 1.e-12 ) THEN
2695                            trsp(trsp_count)%y = trsp(trsp_count)%y - 1.e-10
2696                            trsp(trsp_count)%origin_y =                        &
2697                                                    trsp(trsp_count)%origin_y - 1
2698                         ENDIF
2699
2700                         IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2701                            trspt_count = trspt_count + 1
2702                            trspt(:,:,trspt_count) = &
2703                                               particle_tail_coordinates(:,:,nn)
2704                            trspt(:,2,trspt_count) = ( ny + 1 ) * dy + &
2705                                                     trspt(:,2,trspt_count)
2706                            tail_mask(nn) = .FALSE.
2707                            deleted_tails = deleted_tails + 1
2708                         ENDIF
2709                      ENDIF
2710
2711                   ELSEIF ( ibc_par_ns == 1 )  THEN
2712!
2713!--                   Particle absorption
2714                      particle_mask(n) = .FALSE.
2715                      deleted_particles = deleted_particles + 1
2716                      IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2717                         tail_mask(nn) = .FALSE.
2718                         deleted_tails = deleted_tails + 1
2719                      ENDIF
2720
2721                   ELSEIF ( ibc_par_ns == 2 )  THEN
2722!
2723!--                   Particle reflection
2724                      particles(n)%y       = -particles(n)%y
2725                      particles(n)%speed_y = -particles(n)%speed_y
2726
2727                   ENDIF
2728                ELSE
2729!
2730!--                Store particle data in the transfer array, which will be send
2731!--                to the neighbouring PE
2732                   trsp_count = trsp_count + 1
2733                   trsp(trsp_count) = particles(n)
2734                   particle_mask(n) = .FALSE.
2735                   deleted_particles = deleted_particles + 1
2736
2737                   IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2738                      trspt_count = trspt_count + 1
2739                      trspt(:,:,trspt_count) = particle_tail_coordinates(:,:,nn)
2740                      tail_mask(nn) = .FALSE.
2741                      deleted_tails = deleted_tails + 1
2742                   ENDIF
2743                ENDIF
2744
2745             ELSEIF ( j > nyn )  THEN
2746                IF ( j > ny )  THEN
2747!
2748!--                Apply boundary condition along x
2749                   IF ( ibc_par_ns == 0 )  THEN
2750!
2751!--                   Cyclic condition
2752                      IF ( pdims(2) == 1 )  THEN
2753                         particles(n)%y = particles(n)%y - ( ny + 1 ) * dy
2754                         particles(n)%origin_y = particles(n)%origin_y - &
2755                                                 ( ny + 1 ) * dy
2756                         IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2757                            i = particles(n)%tailpoints
2758                            particle_tail_coordinates(1:i,2,nn) = - (ny+1) * dy&
2759                                           + particle_tail_coordinates(1:i,2,nn)
2760                         ENDIF
2761                      ELSE
2762                         trnp_count = trnp_count + 1
2763                         trnp(trnp_count) = particles(n)
2764                         trnp(trnp_count)%y = trnp(trnp_count)%y - &
2765                                              ( ny + 1 ) * dy
2766                         trnp(trnp_count)%origin_y = trnp(trnp_count)%origin_y &
2767                                                     - ( ny + 1 ) * dy
2768                         particle_mask(n) = .FALSE.
2769                         deleted_particles = deleted_particles + 1
2770
2771                         IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2772                            trnpt_count = trnpt_count + 1
2773                            trnpt(:,:,trnpt_count) = &
2774                                               particle_tail_coordinates(:,:,nn)
2775                            trnpt(:,2,trnpt_count) = trnpt(:,2,trnpt_count) - &
2776                                                     ( ny + 1 ) * dy
2777                            tail_mask(nn) = .FALSE.
2778                            deleted_tails = deleted_tails + 1
2779                         ENDIF
2780                      ENDIF
2781
2782                   ELSEIF ( ibc_par_ns == 1 )  THEN
2783!
2784!--                   Particle absorption
2785                      particle_mask(n) = .FALSE.
2786                      deleted_particles = deleted_particles + 1
2787                      IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2788                         tail_mask(nn) = .FALSE.
2789                         deleted_tails = deleted_tails + 1
2790                      ENDIF
2791
2792                   ELSEIF ( ibc_par_ns == 2 )  THEN
2793!
2794!--                   Particle reflection
2795                      particles(n)%y       = 2 * ( ny * dy ) - particles(n)%y
2796                      particles(n)%speed_y = -particles(n)%speed_y
2797
2798                   ENDIF
2799                ELSE
2800!
2801!--                Store particle data in the transfer array, which will be send
2802!--                to the neighbouring PE
2803                   trnp_count = trnp_count + 1
2804                   trnp(trnp_count) = particles(n)
2805                   particle_mask(n) = .FALSE.
2806                   deleted_particles = deleted_particles + 1
2807
2808                   IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
2809                      trnpt_count = trnpt_count + 1
2810                      trnpt(:,:,trnpt_count) = particle_tail_coordinates(:,:,nn)
2811                      tail_mask(nn) = .FALSE.
2812                      deleted_tails = deleted_tails + 1
2813                   ENDIF
2814                ENDIF
2815
2816             ENDIF
2817          ENDIF
2818       ENDDO
2819
2820!    WRITE ( 9, * ) '*** advec_particles: ##5'
2821!    CALL local_flush( 9 )
2822!    nd = 0
2823!    DO  n = 1, number_of_particles
2824!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
2825!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
2826!       THEN
2827!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
2828!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
2829!          CALL local_flush( 9 )
2830!          CALL MPI_ABORT( comm2d, 9999, ierr )
2831!       ENDIF
2832!    ENDDO
2833!    IF ( nd /= deleted_particles ) THEN
2834!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
2835!       CALL local_flush( 9 )
2836!       CALL MPI_ABORT( comm2d, 9999, ierr )
2837!    ENDIF
2838
2839!
2840!--    Send front boundary, receive back boundary (but first exchange how many
2841!--    and check, if particle storage must be extended)
2842       IF ( pdims(2) /= 1 )  THEN
2843
2844          CALL cpu_log( log_point_s(23), 'sendrcv_particles', 'continue' )
2845          CALL MPI_SENDRECV( trsp_count,      1, MPI_INTEGER, psouth, 0, &
2846                             trnp_count_recv, 1, MPI_INTEGER, pnorth, 0, &
2847                             comm2d, status, ierr )
2848
2849          IF ( number_of_particles + trnp_count_recv > &
2850               maximum_number_of_particles )           &
2851          THEN
2852             IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2853                message_string = 'maximum_number_of_particles ' //  &
2854                                 'needs to be increased ' //        &
2855                                 '&but this is not allowed with '// &
2856                                 'netcdf_data_format < 3'
2857                CALL message( 'advec_particles', 'PA0146', 2, 2, -1, 6, 1 ) 
2858             ELSE
2859!    WRITE ( 9, * ) '*** advec_particles: before allocate_prt_memory trnp'
2860!    CALL local_flush( 9 )
2861                CALL allocate_prt_memory( trnp_count_recv )
2862!    WRITE ( 9, * ) '*** advec_particles: after allocate_prt_memory trnp'
2863!    CALL local_flush( 9 )
2864             ENDIF
2865          ENDIF
2866
2867          CALL MPI_SENDRECV( trsp(1)%age, trsp_count, mpi_particle_type,      &
2868                             psouth, 1, particles(number_of_particles+1)%age, &
2869                             trnp_count_recv, mpi_particle_type, pnorth, 1,   &
2870                             comm2d, status, ierr )
2871
2872          IF ( use_particle_tails )  THEN
2873
2874             CALL MPI_SENDRECV( trspt_count,      1, MPI_INTEGER, psouth, 0, &
2875                                trnpt_count_recv, 1, MPI_INTEGER, pnorth, 0, &
2876                                comm2d, status, ierr )
2877
2878             IF ( number_of_tails+trnpt_count_recv > maximum_number_of_tails ) &
2879             THEN
2880                IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2881                   message_string = 'maximum_number_of_tails ' //    &
2882                                    'needs to be increased ' //      &
2883                                    '&but this is not allowed wi' // &
2884                                    'th netcdf_data_format < 3'
2885                   CALL message( 'advec_particles', 'PA0147', 2, 2, -1, 6, 1 ) 
2886                ELSE
2887!    WRITE ( 9, * ) '*** advec_particles: before allocate_tail_memory trnpt'
2888!    CALL local_flush( 9 )
2889                   CALL allocate_tail_memory( trnpt_count_recv )
2890!    WRITE ( 9, * ) '*** advec_particles: after allocate_tail_memory trnpt'
2891!    CALL local_flush( 9 )
2892                ENDIF
2893             ENDIF
2894
2895             CALL MPI_SENDRECV( trspt(1,1,1), trspt_count*tlength, MPI_REAL,   &
2896                                psouth, 1,                                     &
2897                             particle_tail_coordinates(1,1,number_of_tails+1), &
2898                                trnpt_count_recv*tlength, MPI_REAL, pnorth, 1, &
2899                                comm2d, status, ierr )
2900
2901!
2902!--          Update the tail ids for the transferred particles
2903             nn = number_of_tails
2904             DO  n = number_of_particles+1, number_of_particles+trnp_count_recv
2905                IF ( particles(n)%tail_id /= 0 )  THEN
2906                   nn = nn + 1
2907                   particles(n)%tail_id = nn
2908                ENDIF
2909             ENDDO
2910
2911          ENDIF
2912
2913          number_of_particles = number_of_particles + trnp_count_recv
2914          number_of_tails     = number_of_tails     + trnpt_count_recv
2915!       IF ( number_of_particles /= number_of_tails )  THEN
2916!          WRITE (9,*) '--- advec_particles: #5'
2917!          WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
2918!          CALL local_flush( 9 )
2919!       ENDIF
2920
2921!
2922!--       Send back boundary, receive front boundary
2923          CALL MPI_SENDRECV( trnp_count,      1, MPI_INTEGER, pnorth, 0, &
2924                             trsp_count_recv, 1, MPI_INTEGER, psouth, 0, &
2925                             comm2d, status, ierr )
2926
2927          IF ( number_of_particles + trsp_count_recv > &
2928               maximum_number_of_particles )           &
2929          THEN
2930             IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2931                message_string = 'maximum_number_of_particles ' //   &
2932                                 'needs to be increased ' //         &
2933                                 '&but this is not allowed with ' // &
2934                                 'netcdf_data_format < 3'
2935               CALL message( 'advec_particles', 'PA0146', 2, 2, -1, 6, 1 ) 
2936             ELSE
2937!    WRITE ( 9, * ) '*** advec_particles: before allocate_prt_memory trsp'
2938!    CALL local_flush( 9 )
2939                CALL allocate_prt_memory( trsp_count_recv )
2940!    WRITE ( 9, * ) '*** advec_particles: after allocate_prt_memory trsp'
2941!    CALL local_flush( 9 )
2942             ENDIF
2943          ENDIF
2944
2945          CALL MPI_SENDRECV( trnp(1)%age, trnp_count, mpi_particle_type,      &
2946                             pnorth, 1, particles(number_of_particles+1)%age, &
2947                             trsp_count_recv, mpi_particle_type, psouth, 1,   &
2948                             comm2d, status, ierr )
2949
2950          IF ( use_particle_tails )  THEN
2951
2952             CALL MPI_SENDRECV( trnpt_count,      1, MPI_INTEGER, pnorth, 0, &
2953                                trspt_count_recv, 1, MPI_INTEGER, psouth, 0, &
2954                                comm2d, status, ierr )
2955
2956             IF ( number_of_tails+trspt_count_recv > maximum_number_of_tails ) &
2957             THEN
2958                IF ( netcdf_output  .AND.  netcdf_data_format < 3 )  THEN
2959                   message_string = 'maximum_number_of_tails ' //   &
2960                                    'needs to be increased ' //     &
2961                                    '&but this is not allowed wi'// &
2962                                    'th NetCDF output switched on'
2963                   CALL message( 'advec_particles', 'PA0147', 2, 2, -1, 6, 1 )
2964                ELSE
2965!    WRITE ( 9, * ) '*** advec_particles: before allocate_tail_memory trspt'
2966!    CALL local_flush( 9 )
2967                   CALL allocate_tail_memory( trspt_count_recv )
2968!    WRITE ( 9, * ) '*** advec_particles: after allocate_tail_memory trspt'
2969!    CALL local_flush( 9 )
2970                ENDIF
2971             ENDIF
2972
2973             CALL MPI_SENDRECV( trnpt(1,1,1), trnpt_count*tlength, MPI_REAL,   &
2974                                pnorth, 1,                                     &
2975                             particle_tail_coordinates(1,1,number_of_tails+1), &
2976                                trspt_count_recv*tlength, MPI_REAL, psouth, 1, &
2977                                comm2d, status, ierr )
2978!
2979!--          Update the tail ids for the transferred particles
2980             nn = number_of_tails
2981             DO  n = number_of_particles+1, number_of_particles+trsp_count_recv
2982                IF ( particles(n)%tail_id /= 0 )  THEN
2983                   nn = nn + 1
2984                   particles(n)%tail_id = nn
2985                ENDIF
2986             ENDDO
2987
2988          ENDIF
2989
2990          number_of_particles = number_of_particles + trsp_count_recv
2991          number_of_tails     = number_of_tails     + trspt_count_recv
2992!       IF ( number_of_particles /= number_of_tails )  THEN
2993!          WRITE (9,*) '--- advec_particles: #6'
2994!          WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
2995!          CALL local_flush( 9 )
2996!       ENDIF
2997
2998          IF ( use_particle_tails )  THEN
2999             DEALLOCATE( trspt, trnpt )
3000          ENDIF
3001          DEALLOCATE( trsp, trnp )
3002
3003          CALL cpu_log( log_point_s(23), 'sendrcv_particles', 'stop' )
3004
3005       ENDIF
3006
3007!    WRITE ( 9, * ) '*** advec_particles: ##6'
3008!    CALL local_flush( 9 )
3009!    nd = 0
3010!    DO  n = 1, number_of_particles
3011!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
3012!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
3013!       THEN
3014!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
3015!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
3016!          CALL local_flush( 9 )
3017!          CALL MPI_ABORT( comm2d, 9999, ierr )
3018!       ENDIF
3019!    ENDDO
3020!    IF ( nd /= deleted_particles ) THEN
3021!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
3022!       CALL local_flush( 9 )
3023!       CALL MPI_ABORT( comm2d, 9999, ierr )
3024!    ENDIF
3025
3026#else
3027
3028!
3029!--    Apply boundary conditions
3030       DO  n = 1, number_of_particles
3031
3032          nn = particles(n)%tail_id
3033
3034          IF ( particles(n)%x < -0.5 * dx )  THEN
3035
3036             IF ( ibc_par_lr == 0 )  THEN
3037!
3038!--             Cyclic boundary. Relevant coordinate has to be changed.
3039                particles(n)%x = ( nx + 1 ) * dx + particles(n)%x
3040                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3041                   i = particles(n)%tailpoints
3042                   particle_tail_coordinates(1:i,1,nn) = ( nx + 1 ) * dx + &
3043                                             particle_tail_coordinates(1:i,1,nn)
3044                ENDIF
3045             ELSEIF ( ibc_par_lr == 1 )  THEN
3046!
3047!--             Particle absorption
3048                particle_mask(n) = .FALSE.
3049                deleted_particles = deleted_particles + 1
3050                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3051                   tail_mask(nn) = .FALSE.
3052                   deleted_tails = deleted_tails + 1
3053                ENDIF
3054             ELSEIF ( ibc_par_lr == 2 )  THEN
3055!
3056!--             Particle reflection
3057                particles(n)%x       = -dx - particles(n)%x
3058                particles(n)%speed_x = -particles(n)%speed_x
3059             ENDIF
3060
3061          ELSEIF ( particles(n)%x >= ( nx + 0.5 ) * dx )  THEN
3062
3063             IF ( ibc_par_lr == 0 )  THEN
3064!
3065!--             Cyclic boundary. Relevant coordinate has to be changed.
3066                particles(n)%x = particles(n)%x - ( nx + 1 ) * dx
3067                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3068                   i = particles(n)%tailpoints
3069                   particle_tail_coordinates(1:i,1,nn) = - ( nx + 1 ) * dx + &
3070                                             particle_tail_coordinates(1:i,1,nn)
3071                ENDIF
3072             ELSEIF ( ibc_par_lr == 1 )  THEN
3073!
3074!--             Particle absorption
3075                particle_mask(n) = .FALSE.
3076                deleted_particles = deleted_particles + 1
3077                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3078                   tail_mask(nn) = .FALSE.
3079                   deleted_tails = deleted_tails + 1
3080                ENDIF
3081             ELSEIF ( ibc_par_lr == 2 )  THEN
3082!
3083!--             Particle reflection
3084                particles(n)%x       = ( nx + 1 ) * dx - particles(n)%x
3085                particles(n)%speed_x = -particles(n)%speed_x
3086             ENDIF
3087
3088          ENDIF
3089
3090          IF ( particles(n)%y < -0.5 * dy )  THEN
3091
3092             IF ( ibc_par_ns == 0 )  THEN
3093!
3094!--             Cyclic boundary. Relevant coordinate has to be changed.
3095                particles(n)%y = ( ny + 1 ) * dy + particles(n)%y
3096                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3097                   i = particles(n)%tailpoints
3098                   particle_tail_coordinates(1:i,2,nn) = ( ny + 1 ) * dy + &
3099                                             particle_tail_coordinates(1:i,2,nn)
3100                ENDIF
3101             ELSEIF ( ibc_par_ns == 1 )  THEN
3102!
3103!--             Particle absorption
3104                particle_mask(n) = .FALSE.
3105                deleted_particles = deleted_particles + 1
3106                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3107                   tail_mask(nn) = .FALSE.
3108                   deleted_tails = deleted_tails + 1
3109                ENDIF
3110             ELSEIF ( ibc_par_ns == 2 )  THEN
3111!
3112!--             Particle reflection
3113                particles(n)%y       = -dy - particles(n)%y
3114                particles(n)%speed_y = -particles(n)%speed_y
3115             ENDIF
3116
3117          ELSEIF ( particles(n)%y >= ( ny + 0.5 ) * dy )  THEN
3118
3119             IF ( ibc_par_ns == 0 )  THEN
3120!
3121!--             Cyclic boundary. Relevant coordinate has to be changed.
3122                particles(n)%y = particles(n)%y - ( ny + 1 ) * dy
3123                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3124                   i = particles(n)%tailpoints
3125                   particle_tail_coordinates(1:i,2,nn) = - ( ny + 1 ) * dy + &
3126                                             particle_tail_coordinates(1:i,2,nn)
3127                ENDIF
3128             ELSEIF ( ibc_par_ns == 1 )  THEN
3129!
3130!--             Particle absorption
3131                particle_mask(n) = .FALSE.
3132                deleted_particles = deleted_particles + 1
3133                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3134                   tail_mask(nn) = .FALSE.
3135                   deleted_tails = deleted_tails + 1
3136                ENDIF
3137             ELSEIF ( ibc_par_ns == 2 )  THEN
3138!
3139!--             Particle reflection
3140                particles(n)%y       = ( ny + 1 ) * dy - particles(n)%y
3141                particles(n)%speed_y = -particles(n)%speed_y
3142             ENDIF
3143
3144          ENDIF
3145       ENDDO
3146
3147#endif
3148
3149!
3150!--    Apply boundary conditions to those particles that have crossed the top or
3151!--    bottom boundary and delete those particles, which are older than allowed
3152       DO  n = 1, number_of_particles
3153
3154          nn = particles(n)%tail_id
3155
3156!
3157!--       Stop if particles have moved further than the length of one
3158!--       PE subdomain (newly released particles have age = age_m!)
3159          IF ( particles(n)%age /= particles(n)%age_m )  THEN
3160             IF ( ABS(particles(n)%speed_x) >                                  &
3161                  ((nxr-nxl+2)*dx)/(particles(n)%age-particles(n)%age_m)  .OR. &
3162                  ABS(particles(n)%speed_y) >                                  &
3163                  ((nyn-nys+2)*dy)/(particles(n)%age-particles(n)%age_m) )  THEN
3164
3165                  WRITE( message_string, * )  'particle too fast.  n = ',  n 
3166                  CALL message( 'advec_particles', 'PA0148', 2, 2, -1, 6, 1 )
3167             ENDIF
3168          ENDIF
3169
3170          IF ( particles(n)%age > particle_maximum_age  .AND.  &
3171               particle_mask(n) )                              &
3172          THEN
3173             particle_mask(n)  = .FALSE.
3174             deleted_particles = deleted_particles + 1
3175             IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3176                tail_mask(nn) = .FALSE.
3177                deleted_tails = deleted_tails + 1
3178             ENDIF
3179          ENDIF
3180
3181          IF ( particles(n)%z >= zu(nz)  .AND.  particle_mask(n) )  THEN
3182             IF ( ibc_par_t == 1 )  THEN
3183!
3184!--             Particle absorption
3185                particle_mask(n)  = .FALSE.
3186                deleted_particles = deleted_particles + 1
3187                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3188                   tail_mask(nn) = .FALSE.
3189                   deleted_tails = deleted_tails + 1
3190                ENDIF
3191             ELSEIF ( ibc_par_t == 2 )  THEN
3192!
3193!--             Particle reflection
3194                particles(n)%z       = 2.0 * zu(nz) - particles(n)%z
3195                particles(n)%speed_z = -particles(n)%speed_z
3196                IF ( use_sgs_for_particles  .AND. &
3197                     particles(n)%speed_z_sgs > 0.0 )  THEN
3198                   particles(n)%speed_z_sgs = -particles(n)%speed_z_sgs
3199                ENDIF
3200                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3201                   particle_tail_coordinates(1,3,nn) = 2.0 * zu(nz) - &
3202                                               particle_tail_coordinates(1,3,nn)
3203                ENDIF
3204             ENDIF
3205          ENDIF
3206          IF ( particles(n)%z < zw(0)  .AND.  particle_mask(n) )  THEN
3207             IF ( ibc_par_b == 1 )  THEN
3208!
3209!--             Particle absorption
3210                particle_mask(n)  = .FALSE.
3211                deleted_particles = deleted_particles + 1
3212                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3213                   tail_mask(nn) = .FALSE.
3214                   deleted_tails = deleted_tails + 1
3215                ENDIF
3216             ELSEIF ( ibc_par_b == 2 )  THEN
3217!
3218!--             Particle reflection
3219                particles(n)%z       = 2.0 * zw(0) - particles(n)%z
3220                particles(n)%speed_z = -particles(n)%speed_z
3221                IF ( use_sgs_for_particles  .AND. &
3222                     particles(n)%speed_z_sgs < 0.0 )  THEN
3223                   particles(n)%speed_z_sgs = -particles(n)%speed_z_sgs
3224                ENDIF
3225                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3226                   particle_tail_coordinates(1,3,nn) = 2.0 * zu(nz) - &
3227                                               particle_tail_coordinates(1,3,nn)
3228                ENDIF
3229                IF ( use_particle_tails  .AND.  nn /= 0 )  THEN
3230                   particle_tail_coordinates(1,3,nn) = 2.0 * zw(0) - &
3231                                               particle_tail_coordinates(1,3,nn)
3232                ENDIF
3233             ENDIF
3234          ENDIF
3235       ENDDO
3236
3237!    WRITE ( 9, * ) '*** advec_particles: ##7'
3238!    CALL local_flush( 9 )
3239!    nd = 0
3240!    DO  n = 1, number_of_particles
3241!       IF ( .NOT. particle_mask(n) )  nd = nd + 1
3242!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
3243!       THEN
3244!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
3245!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
3246!          CALL local_flush( 9 )
3247!          CALL MPI_ABORT( comm2d, 9999, ierr )
3248!       ENDIF
3249!    ENDDO
3250!    IF ( nd /= deleted_particles ) THEN
3251!       WRITE (9,*) '*** nd=',nd,' deleted_particles=',deleted_particles
3252!       CALL local_flush( 9 )
3253!       CALL MPI_ABORT( comm2d, 9999, ierr )
3254!    ENDIF
3255
3256!
3257!--    Pack particles (eliminate those marked for deletion),
3258!--    determine new number of particles
3259       IF ( number_of_particles > 0  .AND.  deleted_particles > 0 )  THEN
3260          nn = 0
3261          nd = 0
3262          DO  n = 1, number_of_particles
3263             IF ( particle_mask(n) )  THEN
3264                nn = nn + 1
3265                particles(nn) = particles(n)
3266             ELSE
3267                nd = nd + 1
3268             ENDIF
3269          ENDDO
3270!       IF ( nd /= deleted_particles ) THEN
3271!          WRITE (9,*) '*** advec_part nd=',nd,' deleted_particles=',deleted_particles
3272!          CALL local_flush( 9 )
3273!          CALL MPI_ABORT( comm2d, 9999, ierr )
3274!       ENDIF
3275
3276          number_of_particles = number_of_particles - deleted_particles
3277!
3278!--       Pack the tails, store the new tail ids and re-assign it to the
3279!--       respective
3280!--       particles
3281          IF ( use_particle_tails )  THEN
3282             nn = 0
3283             nd = 0
3284             DO  n = 1, number_of_tails
3285                IF ( tail_mask(n) )  THEN
3286                   nn = nn + 1
3287                   particle_tail_coordinates(:,:,nn) = &
3288                                                particle_tail_coordinates(:,:,n)
3289                   new_tail_id(n) = nn
3290                ELSE
3291                   nd = nd + 1
3292!                WRITE (9,*) '+++ n=',n,' (of ',number_of_tails,' #oftails)'
3293!                WRITE (9,*) '    id=',new_tail_id(n)
3294!                CALL local_flush( 9 )
3295                ENDIF
3296             ENDDO
3297          ENDIF
3298
3299!       IF ( nd /= deleted_tails  .AND.  use_particle_tails ) THEN
3300!          WRITE (9,*) '*** advec_part nd=',nd,' deleted_tails=',deleted_tails
3301!          CALL local_flush( 9 )
3302!          CALL MPI_ABORT( comm2d, 9999, ierr )
3303!       ENDIF
3304
3305          number_of_tails = number_of_tails - deleted_tails
3306
3307!     nn = 0
3308          DO  n = 1, number_of_particles
3309             IF ( particles(n)%tail_id /= 0 )  THEN
3310!     nn = nn + 1
3311!     IF (  particles(n)%tail_id<0 .OR. particles(n)%tail_id > number_of_tails )  THEN
3312!        WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
3313!        WRITE (9,*) '    tail_id=',particles(n)%tail_id
3314!        WRITE (9,*) '    new_tail_id=', new_tail_id(particles(n)%tail_id), &
3315!                         ' of (',number_of_tails,')'
3316!        CALL local_flush( 9 )
3317!     ENDIF
3318                particles(n)%tail_id = new_tail_id(particles(n)%tail_id)
3319             ENDIF
3320          ENDDO
3321
3322!     IF ( nn /= number_of_tails  .AND.  use_particle_tails ) THEN
3323!        WRITE (9,*) '*** advec_part #of_tails=',number_of_tails,' nn=',nn
3324!        CALL local_flush( 9 )
3325!        DO  n = 1, number_of_particles
3326!           WRITE (9,*) 'prt# ',n,' tail_id=',particles(n)%tail_id, &
3327!                       ' x=',particles(n)%x, ' y=',particles(n)%y, &
3328!                       ' z=',particles(n)%z
3329!        ENDDO
3330!        CALL MPI_ABORT( comm2d, 9999, ierr )
3331!     ENDIF
3332
3333       ENDIF
3334
3335!    IF ( number_of_particles /= number_of_tails )  THEN
3336!       WRITE (9,*) '--- advec_particles: #7'
3337!       WRITE (9,*) '    #of p=',number_of_particles,' #of t=',number_of_tails
3338!       CALL local_flush( 9 )
3339!    ENDIF
3340!    WRITE ( 9, * ) '*** advec_particles: ##8'
3341!    CALL local_flush( 9 )
3342!    DO  n = 1, number_of_particles
3343!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
3344!       THEN
3345!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
3346!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
3347!          CALL local_flush( 9 )
3348!          CALL MPI_ABORT( comm2d, 9999, ierr )
3349!       ENDIF
3350!    ENDDO
3351
3352!    WRITE ( 9, * ) '*** advec_particles: ##9'
3353!    CALL local_flush( 9 )
3354!    DO  n = 1, number_of_particles
3355!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
3356!       THEN
3357!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
3358!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
3359!          CALL local_flush( 9 )
3360!          CALL MPI_ABORT( comm2d, 9999, ierr )
3361!       ENDIF
3362!    ENDDO
3363
3364!
3365!--    Accumulate the number of particles transferred between the subdomains
3366#if defined( __parallel )
3367       trlp_count_sum      = trlp_count_sum      + trlp_count
3368       trlp_count_recv_sum = trlp_count_recv_sum + trlp_count_recv
3369       trrp_count_sum      = trrp_count_sum      + trrp_count
3370       trrp_count_recv_sum = trrp_count_recv_sum + trrp_count_recv
3371       trsp_count_sum      = trsp_count_sum      + trsp_count
3372       trsp_count_recv_sum = trsp_count_recv_sum + trsp_count_recv
3373       trnp_count_sum      = trnp_count_sum      + trnp_count
3374       trnp_count_recv_sum = trnp_count_recv_sum + trnp_count_recv
3375#endif
3376
3377       IF ( dt_3d_reached )  EXIT
3378
3379!
3380!--    Initialize variables for the next (sub-) timestep, i.e. for marking those
3381!--    particles to be deleted after the timestep
3382       particle_mask     = .TRUE.
3383       deleted_particles = 0
3384       trlp_count_recv   = 0
3385       trnp_count_recv   = 0
3386       trrp_count_recv   = 0
3387       trsp_count_recv   = 0
3388       trlpt_count_recv  = 0
3389       trnpt_count_recv  = 0
3390       trrpt_count_recv  = 0
3391       trspt_count_recv  = 0
3392       IF ( use_particle_tails )  THEN
3393          tail_mask     = .TRUE.
3394       ENDIF
3395       deleted_tails = 0
3396
3397    ENDDO   ! timestep loop
3398
3399!
3400!-- Sort particles in the sequence the gridboxes are stored in the memory
3401    time_sort_particles = time_sort_particles + dt_3d
3402    IF ( time_sort_particles >= dt_sort_particles )  THEN
3403       CALL sort_particles
3404       time_sort_particles = MOD( time_sort_particles, &
3405                                  MAX( dt_sort_particles, dt_3d ) )
3406    ENDIF
3407
3408    IF ( cloud_droplets )  THEN
3409
3410       CALL cpu_log( log_point_s(45), 'advec_part_reeval_we', 'start' )
3411
3412       ql = 0.0;  ql_v = 0.0;  ql_vp = 0.0
3413
3414!
3415!--    Calculate the liquid water content
3416       DO  i = nxl, nxr
3417          DO  j = nys, nyn
3418             DO  k = nzb, nzt+1
3419
3420!
3421!--             Calculate the total volume in the boxes (ql_v, weighting factor
3422!--             has to beincluded)
3423                psi = prt_start_index(k,j,i)
3424                DO  n = psi, psi+prt_count(k,j,i)-1
3425                   ql_v(k,j,i)  = ql_v(k,j,i)  + particles(n)%weight_factor *  &
3426                                                 particles(n)%radius**3
3427                ENDDO
3428
3429!
3430!--             Calculate the liquid water content
3431                IF ( ql_v(k,j,i) /= 0.0 )  THEN
3432                   ql(k,j,i) = ql(k,j,i) + rho_l * 1.33333333 * pi *           &
3433                                           ql_v(k,j,i) /                       &
3434                                           ( rho_surface * dx * dy * dz )
3435
3436                   IF ( ql(k,j,i) < 0.0 ) THEN
3437                      WRITE( message_string, * )  'LWC out of range: ' , &
3438                                                  ql(k,j,i)
3439                         CALL message( 'advec_particles', '', 2, 2, -1, 6, 1 )
3440                   ENDIF
3441
3442                ELSE
3443                   ql(k,j,i) = 0.0
3444                ENDIF
3445
3446             ENDDO
3447          ENDDO
3448       ENDDO
3449
3450       CALL cpu_log( log_point_s(45), 'advec_part_reeval_we', 'stop' )
3451
3452    ENDIF
3453
3454!
3455!-- Set particle attributes
3456    CALL set_particle_attributes
3457
3458!
3459!-- Set particle attributes defined by the user
3460    CALL user_particle_attributes
3461!    WRITE ( 9, * ) '*** advec_particles: ##10'
3462!    CALL local_flush( 9 )
3463!    DO  n = 1, number_of_particles
3464!       IF ( particles(n)%tail_id<0 .OR. particles(n)%tail_id>number_of_tails ) &
3465!       THEN
3466!          WRITE (9,*) '+++ n=',n,' (of ',number_of_particles,')'
3467!          WRITE (9,*) '    id=',particles(n)%tail_id,' of (',number_of_tails,')'
3468!          CALL local_flush( 9 )
3469!          CALL MPI_ABORT( comm2d, 9999, ierr )
3470!       ENDIF
3471!    ENDDO
3472
3473!
3474!-- If necessary, add the actual particle positions to the particle tails
3475    IF ( use_particle_tails )  THEN
3476
3477       distance = 0.0
3478       DO  n = 1, number_of_particles
3479
3480          nn = particles(n)%tail_id
3481
3482          IF ( nn /= 0 )  THEN
3483!
3484!--          Calculate the distance between the actual particle position and the
3485!--          next tailpoint
3486!             WRITE ( 9, * ) '*** advec_particles: ##10.1  nn=',nn
3487!             CALL local_flush( 9 )
3488             IF ( minimum_tailpoint_distance /= 0.0 )  THEN
3489                distance = ( particle_tail_coordinates(1,1,nn) -      &
3490                             particle_tail_coordinates(2,1,nn) )**2 + &
3491                           ( particle_tail_coordinates(1,2,nn) -      &
3492                             particle_tail_coordinates(2,2,nn) )**2 + &
3493                           ( particle_tail_coordinates(1,3,nn) -      &
3494                             particle_tail_coordinates(2,3,nn) )**2
3495             ENDIF
3496!             WRITE ( 9, * ) '*** advec_particles: ##10.2'
3497!             CALL local_flush( 9 )
3498!
3499!--          First, increase the index of all existings tailpoints by one
3500             IF ( distance >= minimum_tailpoint_distance )  THEN
3501                DO i = particles(n)%tailpoints, 1, -1
3502                   particle_tail_coordinates(i+1,:,nn) = &
3503                                               particle_tail_coordinates(i,:,nn)
3504                ENDDO
3505!
3506!--             Increase the counter which contains the number of tailpoints.
3507!--             This must always be smaller than the given maximum number of
3508!--             tailpoints because otherwise the index bounds of
3509!--             particle_tail_coordinates would be exceeded
3510                IF ( particles(n)%tailpoints < maximum_number_of_tailpoints-1 )&
3511                THEN
3512                   particles(n)%tailpoints = particles(n)%tailpoints + 1
3513                ENDIF
3514             ENDIF
3515!             WRITE ( 9, * ) '*** advec_particles: ##10.3'
3516!             CALL local_flush( 9 )
3517!
3518!--          In any case, store the new point at the beginning of the tail
3519             particle_tail_coordinates(1,1,nn) = particles(n)%x
3520             particle_tail_coordinates(1,2,nn) = particles(n)%y
3521             particle_tail_coordinates(1,3,nn) = particles(n)%z
3522             particle_tail_coordinates(1,4,nn) = particles(n)%color
3523!             WRITE ( 9, * ) '*** advec_particles: ##10.4'
3524!             CALL local_flush( 9 )
3525!
3526!--          Increase the age of the tailpoints
3527             IF ( minimum_tailpoint_distance /= 0.0 )  THEN
3528                particle_tail_coordinates(2:particles(n)%tailpoints,5,nn) =    &
3529                   particle_tail_coordinates(2:particles(n)%tailpoints,5,nn) + &
3530                   dt_3d
3531!
3532!--             Delete the last tailpoint, if it has exceeded its maximum age
3533                IF ( particle_tail_coordinates(particles(n)%tailpoints,5,nn) > &
3534                     maximum_tailpoint_age )  THEN
3535                   particles(n)%tailpoints = particles(n)%tailpoints - 1
3536                ENDIF
3537             ENDIF
3538!             WRITE ( 9, * ) '*** advec_particles: ##10.5'
3539!             CALL local_flush( 9 )
3540
3541          ENDIF
3542
3543       ENDDO
3544
3545    ENDIF
3546!    WRITE ( 9, * ) '*** advec_particles: ##11'
3547!    CALL local_flush( 9 )
3548
3549!
3550!-- Write particle statistics on file
3551    IF ( write_particle_statistics )  THEN
3552       CALL check_open( 80 )
3553#if defined( __parallel )
3554       WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
3555                           number_of_particles, pleft, trlp_count_sum,      &
3556                           trlp_count_recv_sum, pright, trrp_count_sum,     &
3557                           trrp_count_recv_sum, psouth, trsp_count_sum,     &
3558                           trsp_count_recv_sum, pnorth, trnp_count_sum,     &
3559                           trnp_count_recv_sum, maximum_number_of_particles
3560       CALL close_file( 80 )
3561#else
3562       WRITE ( 80, 8000 )  current_timestep_number+1, simulated_time+dt_3d, &
3563                           number_of_particles, maximum_number_of_particles
3564#endif
3565    ENDIF
3566
3567    CALL cpu_log( log_point(25), 'advec_particles', 'stop' )
3568
3569!
3570!-- Formats
35718000 FORMAT (I6,1X,F7.2,4X,I6,5X,4(I3,1X,I4,'/',I4,2X),6X,I6)
3572
3573 END SUBROUTINE advec_particles
3574
3575
3576 SUBROUTINE allocate_prt_memory( number_of_new_particles )
3577
3578!------------------------------------------------------------------------------!
3579! Description:
3580! ------------
3581! Extend particle memory
3582!------------------------------------------------------------------------------!
3583
3584    USE particle_attributes
3585
3586    IMPLICIT NONE
3587
3588    INTEGER ::  new_maximum_number, number_of_new_particles
3589
3590    LOGICAL, DIMENSION(:), ALLOCATABLE ::  tmp_particle_mask
3591
3592    TYPE(particle_type), DIMENSION(:), ALLOCATABLE ::  tmp_particles
3593
3594
3595    new_maximum_number = maximum_number_of_particles + &
3596                   MAX( 5*number_of_new_particles, number_of_initial_particles )
3597
3598    IF ( write_particle_statistics )  THEN
3599       CALL check_open( 80 )
3600       WRITE ( 80, '(''*** Request: '', I7, '' new_maximum_number(prt)'')' ) &
3601                            new_maximum_number
3602       CALL close_file( 80 )
3603    ENDIF
3604
3605    ALLOCATE( tmp_particles(maximum_number_of_particles), &
3606              tmp_particle_mask(maximum_number_of_particles) )
3607    tmp_particles     = particles
3608    tmp_particle_mask = particle_mask
3609
3610    DEALLOCATE( particles, particle_mask )
3611    ALLOCATE( particles(new_maximum_number), &
3612              particle_mask(new_maximum_number) )
3613    maximum_number_of_particles = new_maximum_number
3614
3615    particles(1:number_of_particles) = tmp_particles(1:number_of_particles)
3616    particle_mask(1:number_of_particles) = &
3617                                  tmp_particle_mask(1:number_of_particles)
3618    particle_mask(number_of_particles+1:maximum_number_of_particles) = .TRUE.
3619    DEALLOCATE( tmp_particles, tmp_particle_mask )
3620
3621 END SUBROUTINE allocate_prt_memory
3622
3623
3624 SUBROUTINE allocate_tail_memory( number_of_new_tails )
3625
3626!------------------------------------------------------------------------------!
3627! Description:
3628! ------------
3629! Extend tail memory
3630!------------------------------------------------------------------------------!
3631
3632    USE particle_attributes
3633
3634    IMPLICIT NONE
3635
3636    INTEGER ::  new_maximum_number, number_of_new_tails
3637
3638    LOGICAL, DIMENSION(maximum_number_of_tails) ::  tmp_tail_mask
3639
3640    REAL, DIMENSION(maximum_number_of_tailpoints,5,maximum_number_of_tails) :: &
3641                                                    tmp_tail
3642
3643
3644    new_maximum_number = maximum_number_of_tails + &
3645                         MAX( 5*number_of_new_tails, number_of_initial_tails )
3646
3647    IF ( write_particle_statistics )  THEN
3648       CALL check_open( 80 )
3649       WRITE ( 80, '(''*** Request: '', I5, '' new_maximum_number(tails)'')' ) &
3650                            new_maximum_number
3651       CALL close_file( 80 )
3652    ENDIF
3653    WRITE (9,*) '*** Request: ',new_maximum_number,' new_maximum_number(tails)'
3654!    CALL local_flush( 9 )
3655
3656    tmp_tail(:,:,1:number_of_tails)  = &
3657                                particle_tail_coordinates(:,:,1:number_of_tails)
3658    tmp_tail_mask(1:number_of_tails) = tail_mask(1:number_of_tails)
3659
3660    DEALLOCATE( new_tail_id, particle_tail_coordinates, tail_mask )
3661    ALLOCATE( new_tail_id(new_maximum_number),                          &
3662              particle_tail_coordinates(maximum_number_of_tailpoints,5, &
3663              new_maximum_number),                                      &
3664              tail_mask(new_maximum_number) )
3665    maximum_number_of_tails = new_maximum_number
3666
3667    particle_tail_coordinates = 0.0
3668    particle_tail_coordinates(:,:,1:number_of_tails) = &
3669                                                 tmp_tail(:,:,1:number_of_tails)
3670    tail_mask(1:number_of_tails) = tmp_tail_mask(1:number_of_tails)
3671    tail_mask(number_of_tails+1:maximum_number_of_tails) = .TRUE.
3672
3673 END SUBROUTINE allocate_tail_memory
3674
3675
3676 SUBROUTINE output_particles_netcdf
3677#if defined( __netcdf )
3678
3679    USE control_parameters
3680    USE netcdf_control
3681    USE particle_attributes
3682
3683    IMPLICIT NONE
3684
3685
3686    CALL check_open( 108 )
3687
3688!
3689!-- Update the NetCDF time axis
3690    prt_time_count = prt_time_count + 1
3691
3692    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_time_prt, (/ simulated_time /), &
3693                            start = (/ prt_time_count /), count = (/ 1 /) )
3694    CALL handle_netcdf_error( 'output_particles_netcdf', 1 )
3695
3696!
3697!-- Output the real number of particles used
3698    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_rnop_prt, &
3699                            (/ number_of_particles /),   &
3700                            start = (/ prt_time_count /), count = (/ 1 /) )
3701    CALL handle_netcdf_error( 'output_particles_netcdf', 2 )
3702
3703!
3704!-- Output all particle attributes
3705    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(1), particles%age,         &
3706                            start = (/ 1, prt_time_count /),                  &
3707                            count = (/ maximum_number_of_particles /) )
3708    CALL handle_netcdf_error( 'output_particles_netcdf', 3 )
3709
3710    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(2), particles%dvrp_psize,  &
3711                            start = (/ 1, prt_time_count /),                  &
3712                            count = (/ maximum_number_of_particles /) )
3713    CALL handle_netcdf_error( 'output_particles_netcdf', 4 )
3714
3715    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(3), particles%origin_x,    &
3716                            start = (/ 1, prt_time_count /),                  &
3717                            count = (/ maximum_number_of_particles /) )
3718    CALL handle_netcdf_error( 'output_particles_netcdf', 5 )
3719
3720    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(4), particles%origin_y,    &
3721                            start = (/ 1, prt_time_count /),                  &
3722                            count = (/ maximum_number_of_particles /) )
3723    CALL handle_netcdf_error( 'output_particles_netcdf', 6 )
3724
3725    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(5), particles%origin_z,    &
3726                            start = (/ 1, prt_time_count /),                  &
3727                            count = (/ maximum_number_of_particles /) )
3728    CALL handle_netcdf_error( 'output_particles_netcdf', 7 )
3729
3730    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(6), particles%radius,      &
3731                            start = (/ 1, prt_time_count /),                  &
3732                            count = (/ maximum_number_of_particles /) )
3733    CALL handle_netcdf_error( 'output_particles_netcdf', 8 )
3734
3735    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(7), particles%speed_x,     &
3736                            start = (/ 1, prt_time_count /),                  &
3737                            count = (/ maximum_number_of_particles /) )
3738    CALL handle_netcdf_error( 'output_particles_netcdf', 9 )
3739
3740    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(8), particles%speed_y,     &
3741                            start = (/ 1, prt_time_count /),                  &
3742                            count = (/ maximum_number_of_particles /) )
3743    CALL handle_netcdf_error( 'output_particles_netcdf', 10 )
3744
3745    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(9), particles%speed_z,     &
3746                            start = (/ 1, prt_time_count /),                  &
3747                            count = (/ maximum_number_of_particles /) )
3748    CALL handle_netcdf_error( 'output_particles_netcdf', 11 )
3749
3750    nc_stat = NF90_PUT_VAR( id_set_prt,id_var_prt(10),particles%weight_factor,&
3751                            start = (/ 1, prt_time_count /),                  &
3752                            count = (/ maximum_number_of_particles /) )
3753    CALL handle_netcdf_error( 'output_particles_netcdf', 12 )
3754
3755    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(11), particles%x,          &
3756                            start = (/ 1, prt_time_count /),                  &
3757                            count = (/ maximum_number_of_particles /) )
3758    CALL handle_netcdf_error( 'output_particles_netcdf', 13 )
3759
3760    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(12), particles%y,          & 
3761                            start = (/ 1, prt_time_count /),                  &
3762                            count = (/ maximum_number_of_particles /) )
3763    CALL handle_netcdf_error( 'output_particles_netcdf', 14 )
3764
3765    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(13), particles%z,          &
3766                            start = (/ 1, prt_time_count /),                  &
3767                            count = (/ maximum_number_of_particles /) )
3768    CALL handle_netcdf_error( 'output_particles_netcdf', 15 )
3769
3770    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(14), particles%color,      &
3771                            start = (/ 1, prt_time_count /),                  &
3772                            count = (/ maximum_number_of_particles /) )
3773    CALL handle_netcdf_error( 'output_particles_netcdf', 16 )
3774
3775    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(15), particles%group,      &
3776                            start = (/ 1, prt_time_count /),                  &
3777                            count = (/ maximum_number_of_particles /) )
3778    CALL handle_netcdf_error( 'output_particles_netcdf', 17 )
3779
3780    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(16), particles%tailpoints, &
3781                            start = (/ 1, prt_time_count /),                  &
3782                            count = (/ maximum_number_of_particles /) )
3783    CALL handle_netcdf_error( 'output_particles_netcdf', 18 )
3784
3785    nc_stat = NF90_PUT_VAR( id_set_prt, id_var_prt(17), particles%tail_id,    &
3786                            start = (/ 1, prt_time_count /),                  &
3787                            count = (/ maximum_number_of_particles /) )
3788    CALL handle_netcdf_error( 'output_particles_netcdf', 19 )
3789
3790#endif
3791 END SUBROUTINE output_particles_netcdf
3792
3793
3794 SUBROUTINE write_particles
3795
3796!------------------------------------------------------------------------------!
3797! Description:
3798! ------------
3799! Write particle data on restart file
3800!------------------------------------------------------------------------------!
3801
3802    USE control_parameters
3803    USE particle_attributes
3804    USE pegrid
3805
3806    IMPLICIT NONE
3807
3808    CHARACTER (LEN=10) ::  particle_binary_version
3809
3810!
3811!-- First open the output unit.
3812    IF ( myid_char == '' )  THEN
3813       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT'//myid_char, &
3814                  FORM='UNFORMATTED')
3815    ELSE
3816       IF ( myid == 0 )  CALL local_system( 'mkdir PARTICLE_RESTART_DATA_OUT' )
3817#if defined( __parallel )
3818!
3819!--    Set a barrier in order to allow that thereafter all other processors
3820!--    in the directory created by PE0 can open their file
3821       CALL MPI_BARRIER( comm2d, ierr )
3822#endif
3823       OPEN ( 90, FILE='PARTICLE_RESTART_DATA_OUT/'//myid_char, &
3824                  FORM='UNFORMATTED' )
3825    ENDIF
3826
3827!
3828!-- Write the version number of the binary format.
3829!-- Attention: After changes to the following output commands the version
3830!-- ---------  number of the variable particle_binary_version must be changed!
3831!--            Also, the version number and the list of arrays to be read in
3832!--            init_particles must be adjusted accordingly.
3833    particle_binary_version = '3.0'
3834    WRITE ( 90 )  particle_binary_version
3835
3836!
3837!-- Write some particle parameters, the size of the particle arrays as well as
3838!-- other dvrp-plot variables.
3839    WRITE ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                    &
3840                  maximum_number_of_particles, maximum_number_of_tailpoints,   &
3841                  maximum_number_of_tails, number_of_initial_particles,        &
3842                  number_of_particles, number_of_particle_groups,              &
3843                  number_of_tails, particle_groups, time_prel,                 &
3844                  time_write_particle_data, uniform_particles
3845
3846    IF ( number_of_initial_particles /= 0 )  WRITE ( 90 )  initial_particles
3847
3848    WRITE ( 90 )  prt_count, prt_start_index
3849    WRITE ( 90 )  particles
3850
3851    IF ( use_particle_tails )  THEN
3852       WRITE ( 90 )  particle_tail_coordinates
3853    ENDIF
3854
3855    CLOSE ( 90 )
3856
3857 END SUBROUTINE write_particles
3858
3859
3860 SUBROUTINE collision_efficiency( mean_r, r, e)
3861!------------------------------------------------------------------------------!
3862! Description:
3863! ------------
3864! Interpolate collision efficiency from table
3865!------------------------------------------------------------------------------!
3866
3867    IMPLICIT NONE
3868
3869    INTEGER       ::  i, j, k
3870
3871    LOGICAL, SAVE ::  first = .TRUE.
3872
3873    REAL          ::  aa, bb, cc, dd, dx, dy, e, gg, mean_r, mean_rm, r, rm, &
3874                      x, y
3875
3876    REAL, DIMENSION(1:9), SAVE      ::  collected_r = 0.0
3877    REAL, DIMENSION(1:19), SAVE     ::  collector_r = 0.0
3878    REAL, DIMENSION(1:9,1:19), SAVE ::  ef = 0.0
3879
3880    mean_rm = mean_r * 1.0E06
3881    rm      = r      * 1.0E06
3882
3883    IF ( first )  THEN
3884       collected_r = (/ 2.0, 3.0, 4.0, 6.0, 8.0, 10.0, 15.0, 20.0, 25.0 /)
3885       collector_r = (/ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 80.0, 100.0, 150.0,&
3886                        200.0, 300.0, 400.0, 500.0, 600.0, 1000.0, 1400.0,     &
3887                        1800.0, 2400.0, 3000.0 /)
3888       ef(:,1) = (/0.017, 0.027, 0.037, 0.052, 0.052, 0.052, 0.052, 0.0,  0.0 /)
3889       ef(:,2) = (/0.001, 0.016, 0.027, 0.060, 0.12,  0.17,  0.17,  0.17, 0.0 /)
3890       ef(:,3) = (/0.001, 0.001, 0.02,  0.13,  0.28,  0.37,  0.54,  0.55, 0.47/)
3891       ef(:,4) = (/0.001, 0.001, 0.02,  0.23,  0.4,   0.55,  0.7,   0.75, 0.75/)
3892       ef(:,5) = (/0.01,  0.01,  0.03,  0.3,   0.4,   0.58,  0.73,  0.75, 0.79/)
3893       ef(:,6) = (/0.01,  0.01,  0.13,  0.38,  0.57,  0.68,  0.80,  0.86, 0.91/)
3894       ef(:,7) = (/0.01,  0.085, 0.23,  0.52,  0.68,  0.76,  0.86,  0.92, 0.95/)
3895       ef(:,8) = (/0.01,  0.14,  0.32,  0.60,  0.73,  0.81,  0.90,  0.94, 0.96/)
3896       ef(:,9) = (/0.025, 0.25,  0.43,  0.66,  0.78,  0.83,  0.92,  0.95, 0.96/)
3897       ef(:,10)= (/0.039, 0.3,   0.46,  0.69,  0.81,  0.87,  0.93,  0.95, 0.96/)
3898       ef(:,11)= (/0.095, 0.33,  0.51,  0.72,  0.82,  0.87,  0.93,  0.96, 0.97/)
3899       ef(:,12)= (/0.098, 0.36,  0.51,  0.73,  0.83,  0.88,  0.93,  0.96, 0.97/)
3900       ef(:,13)= (/0.1,   0.36,  0.52,  0.74,  0.83,  0.88,  0.93,  0.96, 0.97/)
3901       ef(:,14)= (/0.17,  0.4,   0.54,  0.72,  0.83,  0.88,  0.94,  0.98, 1.0 /)
3902       ef(:,15)= (/0.15,  0.37,  0.52,  0.74,  0.82,  0.88,  0.94,  0.98, 1.0 /)
3903       ef(:,16)= (/0.11,  0.34,  0.49,  0.71,  0.83,  0.88,  0.94,  0.95, 1.0 /)
3904       ef(:,17)= (/0.08,  0.29,  0.45,  0.68,  0.8,   0.86,  0.96,  0.94, 1.0 /)
3905       ef(:,18)= (/0.04,  0.22,  0.39,  0.62,  0.75,  0.83,  0.92,  0.96, 1.0 /)
3906       ef(:,19)= (/0.02,  0.16,  0.33,  0.55,  0.71,  0.81,  0.90,  0.94, 1.0 /)
3907    ENDIF
3908
3909    DO  k = 1, 8
3910       IF ( collected_r(k) <= mean_rm )  i = k
3911    ENDDO
3912
3913    DO  k = 1, 18
3914       IF ( collector_r(k) <= rm )  j = k
3915    ENDDO
3916
3917    IF ( rm < 10.0 )  THEN
3918       e = 0.0
3919    ELSEIF ( mean_rm < 2.0 )  THEN
3920       e = 0.001
3921    ELSEIF ( mean_rm >= 25.0 )  THEN
3922       IF( j <= 2 )  e = 0.0
3923       IF( j == 3 )  e = 0.47
3924       IF( j == 4 )  e = 0.8
3925       IF( j == 5 )  e = 0.9
3926       IF( j >=6  )  e = 1.0
3927    ELSEIF ( rm >= 3000.0 )  THEN
3928       IF( i == 1 )  e = 0.02
3929       IF( i == 2 )  e = 0.16
3930       IF( i == 3 )  e = 0.33
3931       IF( i == 4 )  e = 0.55
3932       IF( i == 5 )  e = 0.71
3933       IF( i == 6 )  e = 0.81
3934       IF( i == 7 )  e = 0.90
3935       IF( i >= 8 )  e = 0.94
3936    ELSE
3937       x  = mean_rm - collected_r(i)
3938       y  = rm - collector_r(j)
3939       dx = collected_r(i+1) - collected_r(i)
3940       dy = collector_r(j+1) - collector_r(j)
3941       aa = x**2 + y**2
3942       bb = ( dx - x )**2 + y**2
3943       cc = x**2 + ( dy - y )**2
3944       dd = ( dx - x )**2 + ( dy - y )**2
3945       gg = aa + bb + cc + dd
3946
3947       e = ( (gg-aa)*ef(i,j) + (gg-bb)*ef(i+1,j) + (gg-cc)*ef(i,j+1) + &
3948             (gg-dd)*ef(i+1,j+1) ) / (3.0*gg)
3949    ENDIF
3950
3951 END SUBROUTINE collision_efficiency
3952
3953
3954
3955 SUBROUTINE sort_particles
3956
3957!------------------------------------------------------------------------------!
3958! Description:
3959! ------------
3960! Sort particles in the sequence the grid boxes are stored in memory
3961!------------------------------------------------------------------------------!
3962
3963    USE arrays_3d
3964    USE control_parameters
3965    USE cpulog
3966    USE grid_variables
3967    USE indices
3968    USE interfaces
3969    USE particle_attributes
3970
3971    IMPLICIT NONE
3972
3973    INTEGER ::  i, ilow, j, k, n
3974
3975    TYPE(particle_type), DIMENSION(1:number_of_particles) ::  particles_temp
3976
3977
3978    CALL cpu_log( log_point_s(47), 'sort_particles', 'start' )
3979
3980!
3981!-- Initialize the array used for counting and indexing the particles
3982    prt_count = 0
3983
3984!
3985!-- Count the particles per gridbox
3986    DO  n = 1, number_of_particles
3987
3988       i = ( particles(n)%x + 0.5 * dx ) * ddx
3989       j = ( particles(n)%y + 0.5 * dy ) * ddy
3990       k = particles(n)%z / dz + 1 + offset_ocean_nzt
3991           ! only exact if equidistant
3992
3993       prt_count(k,j,i) = prt_count(k,j,i) + 1
3994
3995       IF ( i < nxl .OR. i > nxr .OR. j < nys .OR. j > nyn .OR. k < nzb+1 .OR. &
3996            k > nzt )  THEN
3997          WRITE( message_string, * ) ' particle out of range: i=', i, ' j=', &
3998                          j, ' k=', k,                                       &
3999                          ' nxl=', nxl, ' nxr=', nxr,                        &
4000                          ' nys=', nys, ' nyn=', nyn,                        &
4001                          ' nzb=', nzb, ' nzt=', nzt
4002         CALL message( 'sort_particles', 'PA0149', 1, 2, 0, 6, 0 ) 
4003       ENDIF
4004
4005    ENDDO
4006
4007!
4008!-- Calculate the lower indices of those ranges of the particles-array
4009!-- containing particles which belong to the same gridpox i,j,k
4010    ilow = 1
4011    DO  i = nxl, nxr
4012       DO  j = nys, nyn
4013          DO  k = nzb+1, nzt
4014             prt_start_index(k,j,i) = ilow
4015             ilow = ilow + prt_count(k,j,i)
4016          ENDDO
4017       ENDDO
4018    ENDDO
4019
4020!
4021!-- Sorting the particles
4022    DO  n = 1, number_of_particles
4023
4024       i = ( particles(n)%x + 0.5 * dx ) * ddx
4025       j = ( particles(n)%y + 0.5 * dy ) * ddy
4026       k = particles(n)%z / dz + 1 + offset_ocean_nzt
4027           ! only exact if equidistant
4028
4029       particles_temp(prt_start_index(k,j,i)) = particles(n)
4030
4031       prt_start_index(k,j,i) = prt_start_index(k,j,i) + 1
4032
4033    ENDDO
4034
4035    particles(1:number_of_particles) = particles_temp
4036
4037!
4038!-- Reset the index array to the actual start position
4039    DO  i = nxl, nxr
4040       DO  j = nys, nyn
4041          DO  k = nzb+1, nzt
4042             prt_start_index(k,j,i) = prt_start_index(k,j,i) - prt_count(k,j,i)
4043          ENDDO
4044       ENDDO
4045    ENDDO
4046
4047    CALL cpu_log( log_point_s(47), 'sort_particles', 'stop' )
4048
4049 END SUBROUTINE sort_particles
Note: See TracBrowser for help on using the repository browser.