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

Last change on this file since 82 was 82, checked in by raasch, 17 years ago

vorlaeufige Standalone-Version fuer Linux-Cluster

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