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

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

Change of arguments in message calls whenever there are errors induced by MPI-ABORT

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