source: palm/trunk/SOURCE/init_particles.f90 @ 635

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

last commit documented

  • Property svn:keywords set to Id
File size: 22.1 KB
Line 
1 SUBROUTINE init_particles
2
3!------------------------------------------------------------------------------!
4! Current revisions:
5! -----------------
6!
7!
8! Former revisions:
9! -----------------
10! $Id: init_particles.f90 623 2010-12-10 08:52:17Z raasch $
11!
12! 622 2010-12-10 08:08:13Z raasch
13! optional barriers included in order to speed up collective operations
14!
15! 336 2009-06-10 11:19:35Z raasch
16! Maximum number of tails is calculated from maximum number of particles and
17! skip_particles_for_tail,
18! output of messages replaced by message handling routine
19! Bugfix: arrays for tails are allocated with a minimum size of 10 tails if
20! there is no tail initially
21!
22! 150 2008-02-29 08:19:58Z raasch
23! Setting offset_ocean_* needed for calculating vertical indices within ocean
24! runs
25!
26! 117 2007-10-11 03:27:59Z raasch
27! Sorting of particles only in case of cloud droplets
28!
29! 106 2007-08-16 14:30:26Z raasch
30! variable iran replaced by iran_part
31!
32! 82 2007-04-16 15:40:52Z raasch
33! Preprocessor directives for old systems removed
34!
35! 70 2007-03-18 23:46:30Z raasch
36! displacements for mpi_particle_type changed, age_m initialized,
37! particles-package is now part of the default code
38!
39! 16 2007-02-15 13:16:47Z raasch
40! Bugfix: MPI_REAL in MPI_ALLREDUCE replaced by MPI_INTEGER
41!
42! r4 | raasch | 2007-02-13 12:33:16 +0100 (Tue, 13 Feb 2007)
43! RCS Log replace by Id keyword, revision history cleaned up
44!
45! Revision 1.24  2007/02/11 13:00:17  raasch
46! Bugfix: allocation of tail_mask and new_tail_id in case of restart-runs
47! Bugfix: __ was missing in a cpp-directive
48!
49! Revision 1.1  1999/11/25 16:22:38  raasch
50! Initial revision
51!
52!
53! Description:
54! ------------
55! This routine initializes a set of particles and their attributes (position,
56! radius, ..). Advection of these particles is carried out by advec_particles,
57! plotting is done in data_output_dvrp.
58!------------------------------------------------------------------------------!
59
60    USE arrays_3d
61    USE control_parameters
62    USE dvrp_variables
63    USE grid_variables
64    USE indices
65    USE particle_attributes
66    USE pegrid
67    USE random_function_mod
68
69
70    IMPLICIT NONE
71
72    CHARACTER (LEN=10) ::  particle_binary_version, version_on_file
73
74    INTEGER ::  i, j, n, nn
75#if defined( __parallel )
76    INTEGER, DIMENSION(3) ::  blocklengths, displacements, types
77#endif
78    LOGICAL ::  uniform_particles_l
79    REAL    ::  factor, pos_x, pos_y, pos_z, value
80
81
82#if defined( __parallel )
83!
84!-- Define MPI derived datatype for FORTRAN datatype particle_type (see module
85!-- particle_attributes). Integer length is 4 byte, Real is 8 byte
86    blocklengths(1)  = 19;  blocklengths(2)  =   4;  blocklengths(3)  =   1
87    displacements(1) =  0;  displacements(2) = 152;  displacements(3) = 168
88
89    types(1) = MPI_REAL
90    types(2) = MPI_INTEGER
91    types(3) = MPI_UB
92    CALL MPI_TYPE_STRUCT( 3, blocklengths, displacements, types, &
93                          mpi_particle_type, ierr )
94    CALL MPI_TYPE_COMMIT( mpi_particle_type, ierr )
95#endif
96
97!
98!-- In case of oceans runs, the vertical index calculations need an offset,
99!-- because otherwise the k indices will become negative
100    IF ( ocean )  THEN
101       offset_ocean_nzt    = nzt
102       offset_ocean_nzt_m1 = nzt - 1
103    ENDIF
104
105
106!
107!-- Check the number of particle groups.
108    IF ( number_of_particle_groups > max_number_of_particle_groups )  THEN
109       WRITE( message_string, * ) 'max_number_of_particle_groups =',      &
110                                  max_number_of_particle_groups ,         &
111                                  '&number_of_particle_groups reset to ', &
112                                  max_number_of_particle_groups
113       CALL message( 'init_particles', 'PA0213', 0, 1, 0, 6, 0 )
114       number_of_particle_groups = max_number_of_particle_groups
115    ENDIF
116
117!
118!-- Set default start positions, if necessary
119    IF ( psl(1) == 9999999.9 )  psl(1) = -0.5 * dx
120    IF ( psr(1) == 9999999.9 )  psr(1) = ( nx + 0.5 ) * dx
121    IF ( pss(1) == 9999999.9 )  pss(1) = -0.5 * dy
122    IF ( psn(1) == 9999999.9 )  psn(1) = ( ny + 0.5 ) * dy
123    IF ( psb(1) == 9999999.9 )  psb(1) = zu(nz/2)
124    IF ( pst(1) == 9999999.9 )  pst(1) = psb(1)
125
126    IF ( pdx(1) == 9999999.9  .OR.  pdx(1) == 0.0 )  pdx(1) = dx
127    IF ( pdy(1) == 9999999.9  .OR.  pdy(1) == 0.0 )  pdy(1) = dy
128    IF ( pdz(1) == 9999999.9  .OR.  pdz(1) == 0.0 )  pdz(1) = zu(2) - zu(1)
129
130    DO  j = 2, number_of_particle_groups
131       IF ( psl(j) == 9999999.9 )  psl(j) = psl(j-1)
132       IF ( psr(j) == 9999999.9 )  psr(j) = psr(j-1)
133       IF ( pss(j) == 9999999.9 )  pss(j) = pss(j-1)
134       IF ( psn(j) == 9999999.9 )  psn(j) = psn(j-1)
135       IF ( psb(j) == 9999999.9 )  psb(j) = psb(j-1)
136       IF ( pst(j) == 9999999.9 )  pst(j) = pst(j-1)
137       IF ( pdx(j) == 9999999.9  .OR.  pdx(j) == 0.0 )  pdx(j) = pdx(j-1)
138       IF ( pdy(j) == 9999999.9  .OR.  pdy(j) == 0.0 )  pdy(j) = pdy(j-1)
139       IF ( pdz(j) == 9999999.9  .OR.  pdz(j) == 0.0 )  pdz(j) = pdz(j-1)
140    ENDDO
141
142!
143!-- For the first model run of a possible job chain initialize the
144!-- particles, otherwise read the particle data from file.
145    IF ( TRIM( initializing_actions ) == 'read_restart_data'  &
146         .AND.  read_particles_from_restartfile )  THEN
147
148!
149!--    Read particle data from previous model run.
150!--    First open the input unit.
151       IF ( myid_char == '' )  THEN
152          OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN'//myid_char, &
153                     FORM='UNFORMATTED' )
154       ELSE
155          OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN/'//myid_char, &
156                     FORM='UNFORMATTED' )
157       ENDIF
158
159!
160!--    First compare the version numbers
161       READ ( 90 )  version_on_file
162       particle_binary_version = '3.0'
163       IF ( TRIM( version_on_file ) /= TRIM( particle_binary_version ) )  THEN
164          message_string = 'version mismatch concerning data from prior ' // &
165                           'run &version on file    = "' //                  &
166                                         TRIM( version_on_file ) //          &
167                           '&version in program = "' //                      &
168                                         TRIM( particle_binary_version ) // '"'
169          CALL message( 'init_particles', 'PA0214', 1, 2, 0, 6, 0 )
170       ENDIF
171
172!
173!--    Read some particle parameters and the size of the particle arrays,
174!--    allocate them and read their contents.
175       READ ( 90 )  bc_par_b, bc_par_lr, bc_par_ns, bc_par_t,                  &
176                    maximum_number_of_particles, maximum_number_of_tailpoints, &
177                    maximum_number_of_tails, number_of_initial_particles,      &
178                    number_of_particles, number_of_particle_groups,            &
179                    number_of_tails, particle_groups, time_prel,               &
180                    time_write_particle_data, uniform_particles
181
182       IF ( number_of_initial_particles /= 0 )  THEN
183          ALLOCATE( initial_particles(1:number_of_initial_particles) )
184          READ ( 90 )  initial_particles
185       ENDIF
186
187       ALLOCATE( prt_count(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),       &
188                 prt_start_index(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
189                 particle_mask(maximum_number_of_particles),         &
190                 particles(maximum_number_of_particles) )
191
192       READ ( 90 )  prt_count, prt_start_index
193       READ ( 90 )  particles
194
195       IF ( use_particle_tails )  THEN
196          ALLOCATE( particle_tail_coordinates(maximum_number_of_tailpoints,5, &
197                    maximum_number_of_tails),                                 &
198                    new_tail_id(maximum_number_of_tails),                     &
199                    tail_mask(maximum_number_of_tails) )
200          READ ( 90 )  particle_tail_coordinates
201       ENDIF
202
203       CLOSE ( 90 )
204
205    ELSE
206
207!
208!--    Allocate particle arrays and set attributes of the initial set of
209!--    particles, which can be also periodically released at later times.
210!--    Also allocate array for particle tail coordinates, if needed.
211       ALLOCATE( prt_count(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1),       &
212                 prt_start_index(nzb:nzt+1,nys-1:nyn+1,nxl-1:nxr+1), &
213                 particle_mask(maximum_number_of_particles),         &
214                 particles(maximum_number_of_particles) )
215
216!
217!--    Initialize all particles with dummy values (otherwise errors may
218!--    occur within restart runs). The reason for this is still not clear
219!--    and may be presumably caused by errors in the respective user-interface.
220       particles = particle_type( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
221                                  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, &
222                                  0.0, 0, 0, 0, 0 )
223       particle_groups = particle_groups_type( 0.0, 0.0, 0.0, 0.0 )
224
225!
226!--    Set the default particle size used for dvrp plots
227       IF ( dvrp_psize == 9999999.9 )  dvrp_psize = 0.2 * dx
228
229!
230!--    Set values for the density ratio and radius for all particle
231!--    groups, if necessary
232       IF ( density_ratio(1) == 9999999.9 )  density_ratio(1) = 0.0
233       IF ( radius(1)        == 9999999.9 )  radius(1) = 0.0
234       DO  i = 2, number_of_particle_groups
235          IF ( density_ratio(i) == 9999999.9 )  THEN
236             density_ratio(i) = density_ratio(i-1)
237          ENDIF
238          IF ( radius(i) == 9999999.9 )  radius(i) = radius(i-1)
239       ENDDO
240
241       DO  i = 1, number_of_particle_groups
242          IF ( density_ratio(i) /= 0.0  .AND.  radius(i) == 0 )  THEN
243             WRITE( message_string, * ) 'particle group #', i, 'has a', &
244                                        'density ratio /= 0 but radius = 0'
245             CALL message( 'init_particles', 'PA0215', 1, 2, 0, 6, 0 )
246          ENDIF
247          particle_groups(i)%density_ratio = density_ratio(i)
248          particle_groups(i)%radius        = radius(i)
249       ENDDO
250
251!
252!--    Calculate particle positions and store particle attributes, if
253!--    particle is situated on this PE
254       n = 0
255
256       DO  i = 1, number_of_particle_groups
257
258          pos_z = psb(i)
259
260          DO WHILE ( pos_z <= pst(i) )
261
262             pos_y = pss(i)
263
264             DO WHILE ( pos_y <= psn(i) )
265
266                IF ( pos_y >= ( nys - 0.5 ) * dy  .AND.  &
267                     pos_y <  ( nyn + 0.5 ) * dy )  THEN
268
269                   pos_x = psl(i)
270
271                   DO WHILE ( pos_x <= psr(i) )
272
273                      IF ( pos_x >= ( nxl - 0.5 ) * dx  .AND.  &
274                           pos_x <  ( nxr + 0.5 ) * dx )  THEN
275
276                         DO  j = 1, particles_per_point
277
278                            n = n + 1
279                            IF ( n > maximum_number_of_particles )  THEN
280                               WRITE( message_string, * ) 'number of initial', &
281                                      'particles (', n, ') exceeds',           &
282                                      '&maximum_number_of_particles (',        &
283                                      maximum_number_of_particles, ') on PE ', &
284                                             myid
285                               CALL message( 'init_particles', 'PA0216', &
286                                                                 2, 2, -1, 6, 1 )
287                            ENDIF
288                            particles(n)%x             = pos_x
289                            particles(n)%y             = pos_y
290                            particles(n)%z             = pos_z
291                            particles(n)%age           = 0.0
292                            particles(n)%age_m         = 0.0
293                            particles(n)%dt_sum        = 0.0
294                            particles(n)%dvrp_psize    = dvrp_psize
295                            particles(n)%e_m           = 0.0
296                            particles(n)%speed_x       = 0.0
297                            particles(n)%speed_x_sgs   = 0.0
298                            particles(n)%speed_y       = 0.0
299                            particles(n)%speed_y_sgs   = 0.0
300                            particles(n)%speed_z       = 0.0
301                            particles(n)%speed_z_sgs   = 0.0
302                            particles(n)%origin_x      = pos_x
303                            particles(n)%origin_y      = pos_y
304                            particles(n)%origin_z      = pos_z
305                            particles(n)%radius      = particle_groups(i)%radius
306                            particles(n)%weight_factor =initial_weighting_factor
307                            particles(n)%color         = 1
308                            particles(n)%group         = i
309                            particles(n)%tailpoints    = 0
310                            IF ( use_particle_tails  .AND. &
311                                 MOD( n, skip_particles_for_tail ) == 0 )  THEN
312                               number_of_tails         = number_of_tails + 1
313!
314!--                            This is a temporary provisional setting (see
315!--                            further below!)
316                               particles(n)%tail_id    = number_of_tails
317                            ELSE
318                               particles(n)%tail_id    = 0
319                            ENDIF
320
321                         ENDDO
322
323                      ENDIF
324
325                      pos_x = pos_x + pdx(i)
326
327                   ENDDO
328
329                ENDIF
330
331                pos_y = pos_y + pdy(i)
332
333             ENDDO
334
335             pos_z = pos_z + pdz(i)
336
337          ENDDO
338
339       ENDDO
340
341       number_of_initial_particles = n
342       number_of_particles         = n
343
344!
345!--    Calculate the number of particles and tails of the total domain
346#if defined( __parallel )
347       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
348       CALL MPI_ALLREDUCE( number_of_particles, total_number_of_particles, 1, &
349                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
350       IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
351       CALL MPI_ALLREDUCE( number_of_tails, total_number_of_tails, 1, &
352                           MPI_INTEGER, MPI_SUM, comm2d, ierr )
353#else
354       total_number_of_particles = number_of_particles
355       total_number_of_tails     = number_of_tails
356#endif
357
358!
359!--    Set a seed value for the random number generator to be exclusively
360!--    used for the particle code. The generated random numbers should be
361!--    different on the different PEs.
362       iran_part = iran_part + myid
363
364!
365!--    User modification of initial particles
366       CALL user_init_particles
367
368!
369!--    Store the initial set of particles for release at later times
370       IF ( number_of_initial_particles /= 0 )  THEN
371          ALLOCATE( initial_particles(1:number_of_initial_particles) )
372          initial_particles(1:number_of_initial_particles) = &
373                                        particles(1:number_of_initial_particles)
374       ENDIF
375
376!
377!--    Add random fluctuation to particle positions
378       IF ( random_start_position )  THEN
379
380          DO  n = 1, number_of_initial_particles
381             IF ( psl(particles(n)%group) /= psr(particles(n)%group) )  THEN
382                particles(n)%x = particles(n)%x + &
383                                 ( random_function( iran_part ) - 0.5 ) * &
384                                 pdx(particles(n)%group)
385                IF ( particles(n)%x  <=  ( nxl - 0.5 ) * dx )  THEN
386                   particles(n)%x = ( nxl - 0.4999999999 ) * dx
387                ELSEIF ( particles(n)%x  >=  ( nxr + 0.5 ) * dx )  THEN
388                   particles(n)%x = ( nxr + 0.4999999999 ) * dx
389                ENDIF
390             ENDIF
391             IF ( pss(particles(n)%group) /= psn(particles(n)%group) )  THEN
392                particles(n)%y = particles(n)%y + &
393                                 ( random_function( iran_part ) - 0.5 ) * &
394                                 pdy(particles(n)%group)
395                IF ( particles(n)%y  <=  ( nys - 0.5 ) * dy )  THEN
396                   particles(n)%y = ( nys - 0.4999999999 ) * dy
397                ELSEIF ( particles(n)%y  >=  ( nyn + 0.5 ) * dy )  THEN
398                   particles(n)%y = ( nyn + 0.4999999999 ) * dy
399                ENDIF
400             ENDIF
401             IF ( psb(particles(n)%group) /= pst(particles(n)%group) )  THEN
402                particles(n)%z = particles(n)%z + &
403                                 ( random_function( iran_part ) - 0.5 ) * &
404                                 pdz(particles(n)%group)
405             ENDIF
406          ENDDO
407       ENDIF
408
409!
410!--    Sort particles in the sequence the gridboxes are stored in the memory.
411!--    Only required if cloud droplets are used.
412       IF ( cloud_droplets )  CALL sort_particles
413
414!
415!--    Open file for statistical informations about particle conditions
416       IF ( write_particle_statistics )  THEN
417          CALL check_open( 80 )
418          WRITE ( 80, 8000 )  current_timestep_number, simulated_time, &
419                              number_of_initial_particles,             &
420                              maximum_number_of_particles
421          CALL close_file( 80 )
422       ENDIF
423
424!
425!--    Check if particles are really uniform in color and radius (dvrp_size)
426!--    (uniform_particles is preset TRUE)
427       IF ( uniform_particles )  THEN
428          IF ( number_of_initial_particles == 0 )  THEN
429             uniform_particles_l = .TRUE.
430          ELSE
431             n = number_of_initial_particles
432             IF ( MINVAL( particles(1:n)%dvrp_psize  ) ==     &
433                  MAXVAL( particles(1:n)%dvrp_psize  )  .AND. &
434                  MINVAL( particles(1:n)%color ) ==     &
435                  MAXVAL( particles(1:n)%color ) )  THEN
436                uniform_particles_l = .TRUE.
437             ELSE
438                uniform_particles_l = .FALSE.
439             ENDIF
440          ENDIF
441
442#if defined( __parallel )
443          IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
444          CALL MPI_ALLREDUCE( uniform_particles_l, uniform_particles, 1, &
445                              MPI_LOGICAL, MPI_LAND, comm2d, ierr )
446#else
447          uniform_particles = uniform_particles_l
448#endif
449
450       ENDIF
451
452!
453!--    Particles will probably become none-uniform, if their size and color
454!--    will be determined by flow variables
455       IF ( particle_color /= 'none'  .OR.  particle_dvrpsize /= 'none' )  THEN
456          uniform_particles = .FALSE.
457       ENDIF
458
459!
460!--    Set the beginning of the particle tails and their age
461       IF ( use_particle_tails )  THEN
462!
463!--       Choose the maximum number of tails with respect to the maximum number
464!--       of particles and skip_particles_for_tail
465          maximum_number_of_tails = maximum_number_of_particles / &
466                                    skip_particles_for_tail
467
468!
469!--       Create a minimum number of tails in case that there is no tail
470!--       initially (otherwise, index errors will occur when adressing the
471!--       arrays below)
472          IF ( maximum_number_of_tails == 0 )  maximum_number_of_tails = 10
473
474          ALLOCATE( particle_tail_coordinates(maximum_number_of_tailpoints,5, &
475                    maximum_number_of_tails),                                 &
476                    new_tail_id(maximum_number_of_tails),                     &
477                    tail_mask(maximum_number_of_tails) )
478
479          particle_tail_coordinates  = 0.0
480          minimum_tailpoint_distance = minimum_tailpoint_distance**2
481          number_of_initial_tails    = number_of_tails
482
483          nn = 0
484          DO  n = 1, number_of_particles
485!
486!--          Only for those particles marked above with a provisional tail_id
487!--          tails will be created. Particles now get their final tail_id.
488             IF ( particles(n)%tail_id /= 0 )  THEN
489
490                nn = nn + 1
491                particles(n)%tail_id = nn
492
493                particle_tail_coordinates(1,1,nn) = particles(n)%x
494                particle_tail_coordinates(1,2,nn) = particles(n)%y
495                particle_tail_coordinates(1,3,nn) = particles(n)%z
496                particle_tail_coordinates(1,4,nn) = particles(n)%color
497                particles(n)%tailpoints = 1
498                IF ( minimum_tailpoint_distance /= 0.0 )  THEN
499                   particle_tail_coordinates(2,1,nn) = particles(n)%x
500                   particle_tail_coordinates(2,2,nn) = particles(n)%y
501                   particle_tail_coordinates(2,3,nn) = particles(n)%z
502                   particle_tail_coordinates(2,4,nn) = particles(n)%color
503                   particle_tail_coordinates(1:2,5,nn) = 0.0
504                   particles(n)%tailpoints = 2
505                ENDIF
506
507             ENDIF
508          ENDDO
509       ENDIF
510
511!
512!--    Plot initial positions of particles (only if particle advection is
513!--    switched on from the beginning of the simulation (t=0))
514       IF ( particle_advection_start == 0.0 )  CALL data_output_dvrp
515
516    ENDIF
517
518!
519!-- Check boundary condition and set internal variables
520    SELECT CASE ( bc_par_b )
521   
522       CASE ( 'absorb' )
523          ibc_par_b = 1
524
525       CASE ( 'reflect' )
526          ibc_par_b = 2
527         
528       CASE DEFAULT
529          WRITE( message_string, * )  'unknown boundary condition ',   &
530                                       'bc_par_b = "', TRIM( bc_par_b ), '"'
531          CALL message( 'init_particles', 'PA0217', 1, 2, 0, 6, 0 )
532         
533    END SELECT
534    SELECT CASE ( bc_par_t )
535   
536       CASE ( 'absorb' )
537          ibc_par_t = 1
538
539       CASE ( 'reflect' )
540          ibc_par_t = 2
541         
542       CASE DEFAULT
543          WRITE( message_string, * ) 'unknown boundary condition ',   &
544                                     'bc_par_t = "', TRIM( bc_par_t ), '"'
545          CALL message( 'init_particles', 'PA0218', 1, 2, 0, 6, 0 )
546         
547    END SELECT
548    SELECT CASE ( bc_par_lr )
549
550       CASE ( 'cyclic' )
551          ibc_par_lr = 0
552
553       CASE ( 'absorb' )
554          ibc_par_lr = 1
555
556       CASE ( 'reflect' )
557          ibc_par_lr = 2
558         
559       CASE DEFAULT
560          WRITE( message_string, * ) 'unknown boundary condition ',   &
561                                     'bc_par_lr = "', TRIM( bc_par_lr ), '"'
562          CALL message( 'init_particles', 'PA0219', 1, 2, 0, 6, 0 )
563         
564    END SELECT
565    SELECT CASE ( bc_par_ns )
566
567       CASE ( 'cyclic' )
568          ibc_par_ns = 0
569
570       CASE ( 'absorb' )
571          ibc_par_ns = 1
572
573       CASE ( 'reflect' )
574          ibc_par_ns = 2
575         
576       CASE DEFAULT
577          WRITE( message_string, * ) 'unknown boundary condition ',   &
578                                     'bc_par_ns = "', TRIM( bc_par_ns ), '"'
579          CALL message( 'init_particles', 'PA0220', 1, 2, 0, 6, 0 )
580         
581    END SELECT
582!
583!-- Formats
5848000 FORMAT (I6,1X,F7.2,4X,I6,71X,I6)
585
586 END SUBROUTINE init_particles
Note: See TracBrowser for help on using the repository browser.