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

Last change on this file since 263 was 263, checked in by heinze, 15 years ago

Output of NetCDF messages with aid of message handling routine.

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