source: palm/trunk/SOURCE/lpm_init.f90 @ 1725

Last change on this file since 1725 was 1725, checked in by hoffmann, 8 years ago

Bugfix: Processor-dependent seed for random function is generated before it is used

  • Property svn:keywords set to Id
File size: 36.4 KB
Line 
1!> @file lpm_init.f90
2!--------------------------------------------------------------------------------!
3! This file is part of PALM.
4!
5! PALM is free software: you can redistribute it and/or modify it under the terms
6! of the GNU General Public License as published by the Free Software Foundation,
7! either version 3 of the License, or (at your option) any later version.
8!
9! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
10! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12!
13! You should have received a copy of the GNU General Public License along with
14! PALM. If not, see <http://www.gnu.org/licenses/>.
15!
16! Copyright 1997-2015 Leibniz Universitaet Hannover
17!--------------------------------------------------------------------------------!
18!
19! Current revisions:
20! -----------------
21! Bugfix: Processor-dependent seed for random function is generated before it is
22! used.
23!
24! Former revisions:
25! -----------------
26! $Id: lpm_init.f90 1725 2015-11-17 13:01:51Z hoffmann $
27!
28! 1691 2015-10-26 16:17:44Z maronga
29! Renamed prandtl_layer to constant_flux_layer.
30!
31! 1685 2015-10-08 07:32:13Z raasch
32! bugfix concerning vertical index offset in case of ocean
33!
34! 1682 2015-10-07 23:56:08Z knoop
35! Code annotations made doxygen readable
36!
37! 1575 2015-03-27 09:56:27Z raasch
38! initial vertical particle position is allowed to follow the topography
39!
40! 1359 2014-04-11 17:15:14Z hoffmann
41! New particle structure integrated.
42! Kind definition added to all floating point numbers.
43! lpm_init changed form a subroutine to a module.
44!
45! 1327 2014-03-21 11:00:16Z raasch
46! -netcdf_output
47!
48! 1322 2014-03-20 16:38:49Z raasch
49! REAL functions provided with KIND-attribute
50!
51! 1320 2014-03-20 08:40:49Z raasch
52! ONLY-attribute added to USE-statements,
53! kind-parameters added to all INTEGER and REAL declaration statements,
54! kinds are defined in new module kinds,
55! revision history before 2012 removed,
56! comment fields (!:) to be used for variable explanations added to
57! all variable declaration statements
58! bugfix: #if defined( __parallel ) added
59!
60! 1314 2014-03-14 18:25:17Z suehring
61! Vertical logarithmic interpolation of horizontal particle speed for particles
62! between roughness height and first vertical grid level.
63!
64! 1092 2013-02-02 11:24:22Z raasch
65! unused variables removed
66!
67! 1036 2012-10-22 13:43:42Z raasch
68! code put under GPL (PALM 3.9)
69!
70! 849 2012-03-15 10:35:09Z raasch
71! routine renamed: init_particles -> lpm_init
72! de_dx, de_dy, de_dz are allocated here (instead of automatic arrays in
73! advec_particles),
74! sort_particles renamed lpm_sort_arrays, user_init_particles renamed lpm_init
75!
76! 828 2012-02-21 12:00:36Z raasch
77! call of init_kernels, particle feature color renamed class
78!
79! 824 2012-02-17 09:09:57Z raasch
80! particle attributes speed_x|y|z_sgs renamed rvar1|2|3,
81! array particles implemented as pointer
82!
83! 667 2010-12-23 12:06:00Z suehring/gryschka
84! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng for allocation
85! of arrays.
86!
87! Revision 1.1  1999/11/25 16:22:38  raasch
88! Initial revision
89!
90!
91! Description:
92! ------------
93!> This routine initializes a set of particles and their attributes (position,
94!> radius, ..) which are used by the Lagrangian particle model (see lpm).
95!------------------------------------------------------------------------------!
96 MODULE lpm_init_mod
97 
98
99    USE arrays_3d,                                                             &
100        ONLY:  de_dx, de_dy, de_dz, zu, zw, z0
101
102    USE cloud_parameters,                                                      &
103        ONLY:  curvature_solution_effects
104
105    USE control_parameters,                                                    &
106        ONLY:  cloud_droplets, constant_flux_layer, current_timestep_number,   &
107               dz, initializing_actions, message_string, netcdf_data_format,   &
108               ocean, simulated_time
109
110    USE dvrp_variables,                                                        &
111        ONLY:  particle_color, particle_dvrpsize
112
113    USE grid_variables,                                                        &
114        ONLY:  ddx, dx, ddy, dy
115
116    USE indices,                                                               &
117        ONLY:  nx, nxl, nxlg, nxrg, nxr, ny, nyn, nys, nyng, nysg, nz, nzb,    &
118               nzb_w_inner, nzt
119
120    USE kinds
121
122    USE lpm_collision_kernels_mod,                                             &
123        ONLY:  init_kernels
124
125    USE particle_attributes,                                                   &
126        ONLY:   alloc_factor, bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,        &
127                block_offset, block_offset_def, collision_kernel,              &
128                density_ratio, dvrp_psize, grid_particles,                     &
129                initial_weighting_factor, ibc_par_b, ibc_par_lr, ibc_par_ns,   &
130                ibc_par_t, iran_part, log_z_z0,                                &
131                max_number_of_particle_groups, maximum_number_of_particles,    &
132                maximum_number_of_tailpoints, maximum_number_of_tails,         &
133                minimum_tailpoint_distance, min_nr_particle,                   &
134                mpi_particle_type, new_tail_id,                                &
135                number_of_initial_tails, number_of_particles,                  &
136                number_of_particle_groups, number_of_sublayers,                &
137                number_of_tails, offset_ocean_nzt, offset_ocean_nzt_m1,        &
138                particles, particle_advection_start, particle_groups,          &
139                particle_groups_type, particles_per_point,                     &
140                particle_tail_coordinates,  particle_type, pdx, pdy, pdz,      &
141                prt_count, psb, psl, psn, psr, pss, pst,                       &
142                radius, random_start_position, read_particles_from_restartfile,&
143                seed_follows_topography, skip_particles_for_tail, sort_count,  &
144                tail_mask, total_number_of_particles, total_number_of_tails,   &
145                use_particle_tails, use_sgs_for_particles,                     &
146                write_particle_statistics, uniform_particles, zero_particle,   &
147                z0_av_global
148
149    USE pegrid
150
151    USE random_function_mod,                                                   &
152        ONLY:  random_function
153
154    IMPLICIT NONE
155
156    PRIVATE
157
158    INTEGER(iwp), PARAMETER         :: PHASE_INIT    = 1  !<
159    INTEGER(iwp), PARAMETER, PUBLIC :: PHASE_RELEASE = 2  !<
160
161    INTERFACE lpm_init
162       MODULE PROCEDURE lpm_init
163    END INTERFACE lpm_init
164
165    INTERFACE lpm_create_particle
166       MODULE PROCEDURE lpm_create_particle
167    END INTERFACE lpm_create_particle
168
169    PUBLIC lpm_init, lpm_create_particle
170
171CONTAINS
172
173!------------------------------------------------------------------------------!
174! Description:
175! ------------
176!> @todo Missing subroutine description.
177!------------------------------------------------------------------------------!
178 SUBROUTINE lpm_init
179
180    USE lpm_collision_kernels_mod,                                             &
181        ONLY:  init_kernels
182
183    IMPLICIT NONE
184
185    INTEGER(iwp) ::  i                           !<
186    INTEGER(iwp) ::  ip                          !<
187    INTEGER(iwp) ::  j                           !<
188    INTEGER(iwp) ::  jp                          !<
189    INTEGER(iwp) ::  k                           !<
190    INTEGER(iwp) ::  kp                          !<
191    INTEGER(iwp) ::  n                           !<
192    INTEGER(iwp) ::  nn                          !<
193
194#if defined( __parallel )
195    INTEGER(iwp), DIMENSION(3) ::  blocklengths  !<
196    INTEGER(iwp), DIMENSION(3) ::  displacements !<
197    INTEGER(iwp), DIMENSION(3) ::  types         !<
198#endif
199    LOGICAL ::  uniform_particles_l              !<
200
201    REAL(wp) ::  height_int                      !<
202    REAL(wp) ::  height_p                        !<
203    REAL(wp) ::  z_p                             !<
204    REAL(wp) ::  z0_av_local                     !<
205
206#if defined( __parallel )
207!
208!-- Define MPI derived datatype for FORTRAN datatype particle_type (see module
209!-- particle_attributes). Integer length is 4 byte, Real is 8 byte
210#if defined( __twocachelines )
211    blocklengths(1)  =  7;  blocklengths(2)  =  18;  blocklengths(3)  =   1
212    displacements(1) =  0;  displacements(2) =  64;  displacements(3) = 128
213
214    types(1) = MPI_REAL                               ! 64 bit words
215    types(2) = MPI_INTEGER                            ! 32 Bit words
216    types(3) = MPI_UB
217#else
218    blocklengths(1)  = 19;  blocklengths(2)  =   6;  blocklengths(3)  =   1
219    displacements(1) =  0;  displacements(2) = 152;  displacements(3) = 176
220
221    types(1) = MPI_REAL
222    types(2) = MPI_INTEGER
223    types(3) = MPI_UB
224#endif
225    CALL MPI_TYPE_STRUCT( 3, blocklengths, displacements, types, &
226                          mpi_particle_type, ierr )
227    CALL MPI_TYPE_COMMIT( mpi_particle_type, ierr )
228#endif
229
230!
231!-- In case of oceans runs, the vertical index calculations need an offset,
232!-- because otherwise the k indices will become negative
233    IF ( ocean )  THEN
234       offset_ocean_nzt    = nzt
235       offset_ocean_nzt_m1 = nzt - 1
236    ENDIF
237
238!
239!-- Define block offsets for dividing a gridcell in 8 sub cells
240
241    block_offset(0) = block_offset_def (-1,-1,-1)
242    block_offset(1) = block_offset_def (-1,-1, 0)
243    block_offset(2) = block_offset_def (-1, 0,-1)
244    block_offset(3) = block_offset_def (-1, 0, 0)
245    block_offset(4) = block_offset_def ( 0,-1,-1)
246    block_offset(5) = block_offset_def ( 0,-1, 0)
247    block_offset(6) = block_offset_def ( 0, 0,-1)
248    block_offset(7) = block_offset_def ( 0, 0, 0)
249!
250!-- Check the number of particle groups.
251    IF ( number_of_particle_groups > max_number_of_particle_groups )  THEN
252       WRITE( message_string, * ) 'max_number_of_particle_groups =',      &
253                                  max_number_of_particle_groups ,         &
254                                  '&number_of_particle_groups reset to ', &
255                                  max_number_of_particle_groups
256       CALL message( 'lpm_init', 'PA0213', 0, 1, 0, 6, 0 )
257       number_of_particle_groups = max_number_of_particle_groups
258    ENDIF
259
260!
261!-- Set default start positions, if necessary
262    IF ( psl(1) == 9999999.9_wp )  psl(1) = -0.5_wp * dx
263    IF ( psr(1) == 9999999.9_wp )  psr(1) = ( nx + 0.5_wp ) * dx
264    IF ( pss(1) == 9999999.9_wp )  pss(1) = -0.5_wp * dy
265    IF ( psn(1) == 9999999.9_wp )  psn(1) = ( ny + 0.5_wp ) * dy
266    IF ( psb(1) == 9999999.9_wp )  psb(1) = zu(nz/2)
267    IF ( pst(1) == 9999999.9_wp )  pst(1) = psb(1)
268
269    IF ( pdx(1) == 9999999.9_wp  .OR.  pdx(1) == 0.0_wp )  pdx(1) = dx
270    IF ( pdy(1) == 9999999.9_wp  .OR.  pdy(1) == 0.0_wp )  pdy(1) = dy
271    IF ( pdz(1) == 9999999.9_wp  .OR.  pdz(1) == 0.0_wp )  pdz(1) = zu(2) - zu(1)
272
273    DO  j = 2, number_of_particle_groups
274       IF ( psl(j) == 9999999.9_wp )  psl(j) = psl(j-1)
275       IF ( psr(j) == 9999999.9_wp )  psr(j) = psr(j-1)
276       IF ( pss(j) == 9999999.9_wp )  pss(j) = pss(j-1)
277       IF ( psn(j) == 9999999.9_wp )  psn(j) = psn(j-1)
278       IF ( psb(j) == 9999999.9_wp )  psb(j) = psb(j-1)
279       IF ( pst(j) == 9999999.9_wp )  pst(j) = pst(j-1)
280       IF ( pdx(j) == 9999999.9_wp  .OR.  pdx(j) == 0.0_wp )  pdx(j) = pdx(j-1)
281       IF ( pdy(j) == 9999999.9_wp  .OR.  pdy(j) == 0.0_wp )  pdy(j) = pdy(j-1)
282       IF ( pdz(j) == 9999999.9_wp  .OR.  pdz(j) == 0.0_wp )  pdz(j) = pdz(j-1)
283    ENDDO
284
285!
286!-- Allocate arrays required for calculating particle SGS velocities
287    IF ( use_sgs_for_particles )  THEN
288       ALLOCATE( de_dx(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
289                 de_dy(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
290                 de_dz(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
291    ENDIF
292
293!
294!-- Allocate array required for logarithmic vertical interpolation of
295!-- horizontal particle velocities between the surface and the first vertical
296!-- grid level. In order to avoid repeated CPU cost-intensive CALLS of
297!-- intrinsic FORTRAN procedure LOG(z/z0), LOG(z/z0) is precalculated for
298!-- several heights. Splitting into 20 sublayers turned out to be sufficient.
299!-- To obtain exact height levels of particles, linear interpolation is applied
300!-- (see lpm_advec.f90).
301    IF ( constant_flux_layer )  THEN
302       
303       ALLOCATE ( log_z_z0(0:number_of_sublayers) ) 
304       z_p         = zu(nzb+1) - zw(nzb)
305
306!
307!--    Calculate horizontal mean value of z0 used for logartihmic
308!--    interpolation. Note: this is not exact for heterogeneous z0.
309!--    However, sensitivity studies showed that the effect is
310!--    negligible.
311       z0_av_local  = SUM( z0(nys:nyn,nxl:nxr) )
312       z0_av_global = 0.0_wp
313
314#if defined( __parallel )
315       CALL MPI_ALLREDUCE(z0_av_local, z0_av_global, 1, MPI_REAL, MPI_SUM, &
316                          comm2d, ierr )
317#else
318       z0_av_global = z0_av_local
319#endif
320
321       z0_av_global = z0_av_global  / ( ( ny + 1 ) * ( nx + 1 ) )
322!
323!--    Horizontal wind speed is zero below and at z0
324       log_z_z0(0) = 0.0_wp
325!
326!--    Calculate vertical depth of the sublayers
327       height_int  = ( z_p - z0_av_global ) / REAL( number_of_sublayers, KIND=wp )
328!
329!--    Precalculate LOG(z/z0)
330       height_p    = 0.0_wp
331       DO  k = 1, number_of_sublayers
332
333          height_p    = height_p + height_int
334          log_z_z0(k) = LOG( height_p / z0_av_global )
335
336       ENDDO
337
338
339    ENDIF
340
341!
342!-- Check boundary condition and set internal variables
343    SELECT CASE ( bc_par_b )
344   
345       CASE ( 'absorb' )
346          ibc_par_b = 1
347
348       CASE ( 'reflect' )
349          ibc_par_b = 2
350         
351       CASE DEFAULT
352          WRITE( message_string, * )  'unknown boundary condition ',   &
353                                       'bc_par_b = "', TRIM( bc_par_b ), '"'
354          CALL message( 'lpm_init', 'PA0217', 1, 2, 0, 6, 0 )
355         
356    END SELECT
357    SELECT CASE ( bc_par_t )
358   
359       CASE ( 'absorb' )
360          ibc_par_t = 1
361
362       CASE ( 'reflect' )
363          ibc_par_t = 2
364         
365       CASE DEFAULT
366          WRITE( message_string, * ) 'unknown boundary condition ',   &
367                                     'bc_par_t = "', TRIM( bc_par_t ), '"'
368          CALL message( 'lpm_init', 'PA0218', 1, 2, 0, 6, 0 )
369         
370    END SELECT
371    SELECT CASE ( bc_par_lr )
372
373       CASE ( 'cyclic' )
374          ibc_par_lr = 0
375
376       CASE ( 'absorb' )
377          ibc_par_lr = 1
378
379       CASE ( 'reflect' )
380          ibc_par_lr = 2
381         
382       CASE DEFAULT
383          WRITE( message_string, * ) 'unknown boundary condition ',   &
384                                     'bc_par_lr = "', TRIM( bc_par_lr ), '"'
385          CALL message( 'lpm_init', 'PA0219', 1, 2, 0, 6, 0 )
386         
387    END SELECT
388    SELECT CASE ( bc_par_ns )
389
390       CASE ( 'cyclic' )
391          ibc_par_ns = 0
392
393       CASE ( 'absorb' )
394          ibc_par_ns = 1
395
396       CASE ( 'reflect' )
397          ibc_par_ns = 2
398         
399       CASE DEFAULT
400          WRITE( message_string, * ) 'unknown boundary condition ',   &
401                                     'bc_par_ns = "', TRIM( bc_par_ns ), '"'
402          CALL message( 'lpm_init', 'PA0220', 1, 2, 0, 6, 0 )
403         
404    END SELECT
405
406!
407!-- Initialize collision kernels
408    IF ( collision_kernel /= 'none' )  CALL init_kernels
409
410!
411!-- For the first model run of a possible job chain initialize the
412!-- particles, otherwise read the particle data from restart file.
413    IF ( TRIM( initializing_actions ) == 'read_restart_data'  &
414         .AND.  read_particles_from_restartfile )  THEN
415
416       CALL lpm_read_restart_file
417
418    ELSE
419
420!
421!--    Allocate particle arrays and set attributes of the initial set of
422!--    particles, which can be also periodically released at later times.
423!--    Also allocate array for particle tail coordinates, if needed.
424       ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg), &
425                 grid_particles(nzb+1:nzt,nys:nyn,nxl:nxr) )
426
427       maximum_number_of_particles = 0
428       number_of_particles         = 0
429
430       sort_count = 0
431       prt_count  = 0
432
433!
434!--    Initialize all particles with dummy values (otherwise errors may
435!--    occur within restart runs). The reason for this is still not clear
436!--    and may be presumably caused by errors in the respective user-interface.
437#if defined( __twocachelines )
438       zero_particle = particle_type( 0.0_wp, 0.0_sp, 0.0_sp, 0.0_sp, 0.0_sp,  &
439                                      0.0_sp, 0.0_sp, 0.0_wp, 0.0_wp, 0.0_wp,  &
440                                      0, .FALSE., 0.0_wp, 0.0_wp, 0.0_wp,      &
441                                      0.0_sp, 0.0_sp, 0.0_sp, 0.0_sp, 0.0_sp,  &
442                                      0.0_sp, 0, 0, 0, -1)
443#else
444       zero_particle = particle_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
445                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
446                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp,  &
447                                      0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp, 0, 0, 0, &
448                                      0, .FALSE., -1)
449#endif
450       particle_groups = particle_groups_type( 0.0_wp, 0.0_wp, 0.0_wp, 0.0_wp )
451
452!
453!--    Set the default particle size used for dvrp plots
454       IF ( dvrp_psize == 9999999.9_wp )  dvrp_psize = 0.2_wp * dx
455
456!
457!--    Set values for the density ratio and radius for all particle
458!--    groups, if necessary
459       IF ( density_ratio(1) == 9999999.9_wp )  density_ratio(1) = 0.0_wp
460       IF ( radius(1)        == 9999999.9_wp )  radius(1) = 0.0_wp
461       DO  i = 2, number_of_particle_groups
462          IF ( density_ratio(i) == 9999999.9_wp )  THEN
463             density_ratio(i) = density_ratio(i-1)
464          ENDIF
465          IF ( radius(i) == 9999999.9_wp )  radius(i) = radius(i-1)
466       ENDDO
467
468       DO  i = 1, number_of_particle_groups
469          IF ( density_ratio(i) /= 0.0_wp  .AND.  radius(i) == 0 )  THEN
470             WRITE( message_string, * ) 'particle group #', i, 'has a', &
471                                        'density ratio /= 0 but radius = 0'
472             CALL message( 'lpm_init', 'PA0215', 1, 2, 0, 6, 0 )
473          ENDIF
474          particle_groups(i)%density_ratio = density_ratio(i)
475          particle_groups(i)%radius        = radius(i)
476       ENDDO
477
478!
479!--    Set a seed value for the random number generator to be exclusively
480!--    used for the particle code. The generated random numbers should be
481!--    different on the different PEs.
482       iran_part = iran_part + myid
483
484       CALL lpm_create_particle (PHASE_INIT)
485
486!
487!--    User modification of initial particles
488       CALL user_lpm_init
489
490!
491!--    Open file for statistical informations about particle conditions
492       IF ( write_particle_statistics )  THEN
493          CALL check_open( 80 )
494          WRITE ( 80, 8000 )  current_timestep_number, simulated_time,         &
495                              number_of_particles,                             &
496                              maximum_number_of_particles
497          CALL close_file( 80 )
498       ENDIF
499
500!
501!--    Check if particles are really uniform in color and radius (dvrp_size)
502!--    (uniform_particles is preset TRUE)
503       IF ( uniform_particles )  THEN
504          DO  ip = nxl, nxr
505             DO  jp = nys, nyn
506                DO  kp = nzb+1, nzt
507
508                   n = prt_count(kp,jp,ip)
509                   IF ( MINVAL( grid_particles(kp,jp,ip)%particles(1:n)%dvrp_psize  ) ==     &
510                        MAXVAL( grid_particles(kp,jp,ip)%particles(1:n)%dvrp_psize  )  .AND. &
511                        MINVAL( grid_particles(kp,jp,ip)%particles(1:n)%class ) ==           &
512                        MAXVAL( grid_particles(kp,jp,ip)%particles(1:n)%class ) )  THEN
513                      uniform_particles_l = .TRUE.
514                   ELSE
515                      uniform_particles_l = .FALSE.
516                   ENDIF
517
518                ENDDO
519             ENDDO
520          ENDDO
521
522#if defined( __parallel )
523          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
524          CALL MPI_ALLREDUCE( uniform_particles_l, uniform_particles, 1,       &
525                              MPI_LOGICAL, MPI_LAND, comm2d, ierr )
526#else
527          uniform_particles = uniform_particles_l
528#endif
529
530       ENDIF
531
532!
533!--    Particles will probably become none-uniform, if their size and color
534!--    will be determined by flow variables
535       IF ( particle_color /= 'none'  .OR.  particle_dvrpsize /= 'none' )  THEN
536          uniform_particles = .FALSE.
537       ENDIF
538
539! !kk    Not implemented aft individual particle array fort every gridcell
540! !
541! !--    Set the beginning of the particle tails and their age
542!        IF ( use_particle_tails )  THEN
543! !
544! !--       Choose the maximum number of tails with respect to the maximum number
545! !--       of particles and skip_particles_for_tail
546!           maximum_number_of_tails = maximum_number_of_particles / &
547!                                     skip_particles_for_tail
548!
549! !
550! !--       Create a minimum number of tails in case that there is no tail
551! !--       initially (otherwise, index errors will occur when adressing the
552! !--       arrays below)
553!           IF ( maximum_number_of_tails == 0 )  maximum_number_of_tails = 10
554!
555!           ALLOCATE( particle_tail_coordinates(maximum_number_of_tailpoints,5, &
556!                     maximum_number_of_tails),                                 &
557!                     new_tail_id(maximum_number_of_tails),                     &
558!                     tail_mask(maximum_number_of_tails) )
559!
560!           particle_tail_coordinates  = 0.0_wp
561!           minimum_tailpoint_distance = minimum_tailpoint_distance**2
562!           number_of_initial_tails    = number_of_tails
563!
564!           nn = 0
565!           DO  n = 1, number_of_particles
566! !
567! !--          Only for those particles marked above with a provisional tail_id
568! !--          tails will be created. Particles now get their final tail_id.
569!              IF ( particles(n)%tail_id /= 0 )  THEN
570!
571!                 nn = nn + 1
572!                 particles(n)%tail_id = nn
573!
574!                 particle_tail_coordinates(1,1,nn) = particles(n)%x
575!                 particle_tail_coordinates(1,2,nn) = particles(n)%y
576!                 particle_tail_coordinates(1,3,nn) = particles(n)%z
577!                 particle_tail_coordinates(1,4,nn) = particles(n)%class
578!                 particles(n)%tailpoints = 1
579!                 IF ( minimum_tailpoint_distance /= 0.0_wp )  THEN
580!                    particle_tail_coordinates(2,1,nn) = particles(n)%x
581!                    particle_tail_coordinates(2,2,nn) = particles(n)%y
582!                    particle_tail_coordinates(2,3,nn) = particles(n)%z
583!                    particle_tail_coordinates(2,4,nn) = particles(n)%class
584!                    particle_tail_coordinates(1:2,5,nn) = 0.0_wp
585!                    particles(n)%tailpoints = 2
586!                 ENDIF
587!
588!              ENDIF
589!           ENDDO
590!        ENDIF
591!
592! !
593! !--    Plot initial positions of particles (only if particle advection is
594! !--    switched on from the beginning of the simulation (t=0))
595!        IF ( particle_advection_start == 0.0_wp )  CALL data_output_dvrp
596
597    ENDIF
598
599!
600!-- To avoid programm abort, assign particles array to the local version of
601!-- first grid cell
602    number_of_particles = prt_count(nzb+1,nys,nxl)
603    particles => grid_particles(nzb+1,nys,nxl)%particles(1:number_of_particles)
604
605!
606!-- Formats
6078000 FORMAT (I6,1X,F7.2,4X,I10,71X,I10)
608
609 END SUBROUTINE lpm_init
610
611!------------------------------------------------------------------------------!
612! Description:
613! ------------
614!> @todo Missing subroutine description.
615!------------------------------------------------------------------------------!
616 SUBROUTINE lpm_create_particle (phase)
617
618    USE lpm_exchange_horiz_mod,                                                &
619        ONLY: lpm_exchange_horiz, lpm_move_particle, realloc_particles_array
620
621    USE lpm_pack_arrays_mod,                                                   &
622        ONLY: lpm_pack_all_arrays
623
624    IMPLICIT  NONE
625
626    INTEGER(iwp)               ::  alloc_size  !<
627    INTEGER(iwp)               ::  i           !<
628    INTEGER(iwp)               ::  ip          !<
629    INTEGER(iwp)               ::  j           !<
630    INTEGER(iwp)               ::  jp          !<
631    INTEGER(iwp)               ::  kp          !<
632    INTEGER(iwp)               ::  loop_stride !<
633    INTEGER(iwp)               ::  n           !<
634    INTEGER(iwp)               ::  new_size    !<
635    INTEGER(iwp)               ::  nn          !<
636
637    INTEGER(iwp), INTENT(IN)   ::  phase       !<
638
639    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_count !<
640    INTEGER(iwp), DIMENSION(nzb:nzt+1,nysg:nyng,nxlg:nxrg) ::  local_start !<
641
642    LOGICAL                    ::  first_stride !<
643
644    REAL(wp)                   ::  pos_x !<
645    REAL(wp)                   ::  pos_y !<
646    REAL(wp)                   ::  pos_z !<
647
648    TYPE(particle_type),TARGET ::  tmp_particle !<
649
650!
651!-- Calculate particle positions and store particle attributes, if
652!-- particle is situated on this PE
653    DO  loop_stride = 1, 2
654       first_stride = (loop_stride == 1)
655       IF ( first_stride )   THEN
656          local_count = 0           ! count number of particles
657       ELSE
658          local_count = prt_count   ! Start address of new particles
659       ENDIF
660
661       n = 0
662       DO  i = 1, number_of_particle_groups
663
664          pos_z = psb(i)
665
666          DO WHILE ( pos_z <= pst(i) )
667
668             pos_y = pss(i)
669
670             DO WHILE ( pos_y <= psn(i) )
671
672                IF ( pos_y >= ( nys - 0.5_wp ) * dy  .AND.  &
673                     pos_y <  ( nyn + 0.5_wp ) * dy )  THEN
674
675                   pos_x = psl(i)
676
677            xloop: DO WHILE ( pos_x <= psr(i) )
678
679                      IF ( pos_x >= ( nxl - 0.5_wp ) * dx  .AND.  &
680                           pos_x <  ( nxr + 0.5_wp ) * dx )  THEN
681
682                         DO  j = 1, particles_per_point
683
684                            n = n + 1
685#if defined( __twocachelines )
686                            tmp_particle%x             = pos_x
687                            tmp_particle%y             = pos_y
688                            tmp_particle%z             = pos_z
689                            tmp_particle%age           = 0.0_sp
690                            tmp_particle%age_m         = 0.0_sp
691                            tmp_particle%dt_sum        = 0.0_wp
692                            tmp_particle%dvrp_psize    = dvrp_psize
693                            tmp_particle%e_m           = 0.0_sp
694                            IF ( curvature_solution_effects )  THEN
695!
696!--                            Initial values (internal timesteps, derivative)
697!--                            for Rosenbrock method
698                               tmp_particle%rvar1      = 1.0E-12_wp
699                               tmp_particle%rvar2      = 1.0E-3_wp
700                               tmp_particle%rvar3      = -9999999.9_wp
701                            ELSE
702!
703!--                            Initial values for SGS velocities
704                               tmp_particle%rvar1      = 0.0_wp
705                               tmp_particle%rvar2      = 0.0_wp
706                               tmp_particle%rvar3      = 0.0_wp
707                            ENDIF
708                            tmp_particle%speed_x       = 0.0_sp
709                            tmp_particle%speed_y       = 0.0_sp
710                            tmp_particle%speed_z       = 0.0_sp
711                            tmp_particle%origin_x      = pos_x
712                            tmp_particle%origin_y      = pos_y
713                            tmp_particle%origin_z      = pos_z
714                            tmp_particle%radius        = particle_groups(i)%radius
715                            tmp_particle%weight_factor = initial_weighting_factor
716                            tmp_particle%class         = 1
717                            tmp_particle%group         = i
718                            tmp_particle%tailpoints    = 0
719                            tmp_particle%particle_mask = .TRUE.
720#else
721                            tmp_particle%x             = pos_x
722                            tmp_particle%y             = pos_y
723                            tmp_particle%z             = pos_z
724                            tmp_particle%age           = 0.0_wp
725                            tmp_particle%age_m         = 0.0_wp
726                            tmp_particle%dt_sum        = 0.0_wp
727                            tmp_particle%dvrp_psize    = dvrp_psize
728                            tmp_particle%e_m           = 0.0_wp
729                            IF ( curvature_solution_effects )  THEN
730!
731!--                            Initial values (internal timesteps, derivative)
732!--                            for Rosenbrock method
733                               tmp_particle%rvar1      = 1.0E-12_wp
734                               tmp_particle%rvar2      = 1.0E-3_wp
735                               tmp_particle%rvar3      = -9999999.9_wp
736                            ELSE
737!
738!--                            Initial values for SGS velocities
739                               tmp_particle%rvar1      = 0.0_wp
740                               tmp_particle%rvar2      = 0.0_wp
741                               tmp_particle%rvar3      = 0.0_wp
742                            ENDIF
743                            tmp_particle%speed_x       = 0.0_wp
744                            tmp_particle%speed_y       = 0.0_wp
745                            tmp_particle%speed_z       = 0.0_wp
746                            tmp_particle%origin_x      = pos_x
747                            tmp_particle%origin_y      = pos_y
748                            tmp_particle%origin_z      = pos_z
749                            tmp_particle%radius        = particle_groups(i)%radius
750                            tmp_particle%weight_factor = initial_weighting_factor
751                            tmp_particle%class         = 1
752                            tmp_particle%group         = i
753                            tmp_particle%tailpoints    = 0
754                            tmp_particle%particle_mask = .TRUE.
755#endif
756                            IF ( use_particle_tails  .AND. &
757                                 MOD( n, skip_particles_for_tail ) == 0 )  THEN
758                               number_of_tails         = number_of_tails + 1
759!
760!--                            This is a temporary provisional setting (see
761!--                            further below!)
762                               tmp_particle%tail_id    = number_of_tails
763                            ELSE
764                               tmp_particle%tail_id    = 0
765                            ENDIF
766!
767!--                         Determine the grid indices of the particle position
768                            ip = ( tmp_particle%x + 0.5_wp * dx ) * ddx
769                            jp = ( tmp_particle%y + 0.5_wp * dy ) * ddy
770                            kp = tmp_particle%z / dz + 1 + offset_ocean_nzt
771
772                            IF ( seed_follows_topography )  THEN
773!
774!--                            Particle height is given relative to topography
775                               kp = kp + nzb_w_inner(jp,ip)
776                               tmp_particle%z = tmp_particle%z + zw(kp)
777                               IF ( kp > nzt )  THEN
778                                  pos_x = pos_x + pdx(i)
779                                  CYCLE xloop
780                               ENDIF
781                            ENDIF
782
783                            local_count(kp,jp,ip) = local_count(kp,jp,ip) + 1
784                            IF ( .NOT. first_stride )  THEN
785                               IF ( ip < nxl  .OR.  jp < nys  .OR.  kp < nzb+1 )  THEN
786                                  write(6,*) 'xl ',ip,jp,kp,nxl,nys,nzb+1
787                               ENDIF
788                               IF ( ip > nxr  .OR.  jp > nyn  .OR.  kp > nzt )  THEN
789                                  write(6,*) 'xu ',ip,jp,kp,nxr,nyn,nzt
790                               ENDIF
791                               grid_particles(kp,jp,ip)%particles(local_count(kp,jp,ip)) = tmp_particle
792                            ENDIF
793                         ENDDO
794
795                      ENDIF
796
797                      pos_x = pos_x + pdx(i)
798
799                   ENDDO xloop
800
801                ENDIF
802
803                pos_y = pos_y + pdy(i)
804
805             ENDDO
806
807             pos_z = pos_z + pdz(i)
808
809          ENDDO
810
811       ENDDO
812
813       IF ( first_stride )  THEN
814          DO  ip = nxl, nxr
815             DO  jp = nys, nyn
816                DO  kp = nzb+1, nzt
817                   IF ( phase == PHASE_INIT )  THEN
818                      IF ( local_count(kp,jp,ip) > 0 )  THEN
819                         alloc_size = MAX( INT( local_count(kp,jp,ip) *        &
820                            ( 1.0_wp + alloc_factor / 100.0_wp ) ),            &
821                            min_nr_particle )
822                      ELSE
823                         alloc_size = min_nr_particle
824                      ENDIF
825                      ALLOCATE(grid_particles(kp,jp,ip)%particles(1:alloc_size))
826                      DO  n = 1, alloc_size
827                         grid_particles(kp,jp,ip)%particles(n) = zero_particle
828                      ENDDO
829                   ELSEIF ( phase == PHASE_RELEASE )  THEN
830                      IF ( local_count(kp,jp,ip) > 0 )  THEN
831                         new_size   = local_count(kp,jp,ip) + prt_count(kp,jp,ip)
832                         alloc_size = MAX( INT( new_size * ( 1.0_wp +          &
833                            alloc_factor / 100.0_wp ) ), min_nr_particle )
834                         IF( alloc_size > SIZE( grid_particles(kp,jp,ip)%particles) )  THEN
835                           CALL realloc_particles_array(ip,jp,kp,alloc_size)
836                         ENDIF
837                      ENDIF
838                   ENDIF
839                ENDDO
840             ENDDO
841          ENDDO
842       ENDIF
843    ENDDO
844
845    local_start = prt_count+1
846    prt_count   = local_count
847!
848!-- Add random fluctuation to particle positions
849    IF ( random_start_position )  THEN
850       DO  ip = nxl, nxr
851          DO  jp = nys, nyn
852             DO  kp = nzb+1, nzt
853                number_of_particles = prt_count(kp,jp,ip)
854                IF ( number_of_particles <= 0 )  CYCLE
855                particles => grid_particles(kp,jp,ip)%particles(1:number_of_particles)
856
857                DO  n = local_start(kp,jp,ip), number_of_particles              !Move only new particles
858                   IF ( psl(particles(n)%group) /= psr(particles(n)%group) )  THEN
859                      particles(n)%x = particles(n)%x +                        &
860                                   ( random_function( iran_part ) - 0.5_wp ) * &
861                                   pdx(particles(n)%group)
862                   ENDIF
863                   IF ( pss(particles(n)%group) /= psn(particles(n)%group) )  THEN
864                      particles(n)%y = particles(n)%y +                        &
865                                   ( random_function( iran_part ) - 0.5_wp ) * &
866                                   pdy(particles(n)%group)
867                   ENDIF
868                   IF ( psb(particles(n)%group) /= pst(particles(n)%group) )  THEN
869                      particles(n)%z = particles(n)%z +                        &
870                                   ( random_function( iran_part ) - 0.5_wp ) * &
871                                   pdz(particles(n)%group)
872                   ENDIF
873                ENDDO
874!
875!--             Identify particles located outside the model domain
876                CALL lpm_boundary_conds( 'bottom/top' )
877             ENDDO
878          ENDDO
879       ENDDO
880!
881!--    Exchange particles between grid cells and processors
882       CALL lpm_move_particle
883       CALL lpm_exchange_horiz
884
885    ENDIF
886!
887!-- In case of random_start_position, delete particles identified by
888!-- lpm_exchange_horiz and lpm_boundary_conds. Then sort particles into blocks,
889!-- which is needed for a fast interpolation of the LES fields on the particle
890!-- position.
891    CALL lpm_pack_all_arrays
892
893!
894!-- Determine maximum number of particles (i.e., all possible particles that
895!-- have been allocated) and the current number of particles
896    DO  ip = nxl, nxr
897       DO  jp = nys, nyn
898          DO  kp = nzb+1, nzt
899             maximum_number_of_particles = maximum_number_of_particles         &
900                                           + SIZE(grid_particles(kp,jp,ip)%particles)
901             number_of_particles         = number_of_particles                 &
902                                           + prt_count(kp,jp,ip)
903          ENDDO
904       ENDDO
905    ENDDO
906!
907!-- Calculate the number of particles and tails of the total domain
908#if defined( __parallel )
909    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
910    CALL MPI_ALLREDUCE( number_of_particles, total_number_of_particles, 1, &
911    MPI_INTEGER, MPI_SUM, comm2d, ierr )
912    IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
913    CALL MPI_ALLREDUCE( number_of_tails, total_number_of_tails, 1, &
914    MPI_INTEGER, MPI_SUM, comm2d, ierr )
915#else
916    total_number_of_particles = number_of_particles
917    total_number_of_tails     = number_of_tails
918#endif
919
920    RETURN
921
922 END SUBROUTINE lpm_create_particle
923
924END MODULE lpm_init_mod
Note: See TracBrowser for help on using the repository browser.