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

Last change on this file since 1036 was 1036, checked in by raasch, 11 years ago

code has been put under the GNU General Public License (v3)

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