SUBROUTINE init_particles !------------------------------------------------------------------------------! ! Current revisions: ! ----------------- ! ! Former revisions: ! ----------------- ! $Id: init_particles.f90 668 2010-12-23 13:22:58Z witha $ ! ! 667 2010-12-23 12:06:00Z suehring/gryschka ! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng for allocation ! of arrays. ! ! 622 2010-12-10 08:08:13Z raasch ! optional barriers included in order to speed up collective operations ! ! 336 2009-06-10 11:19:35Z raasch ! Maximum number of tails is calculated from maximum number of particles and ! skip_particles_for_tail, ! output of messages replaced by message handling routine ! Bugfix: arrays for tails are allocated with a minimum size of 10 tails if ! there is no tail initially ! ! 150 2008-02-29 08:19:58Z raasch ! Setting offset_ocean_* needed for calculating vertical indices within ocean ! runs ! ! 117 2007-10-11 03:27:59Z raasch ! Sorting of particles only in case of cloud droplets ! ! 106 2007-08-16 14:30:26Z raasch ! variable iran replaced by iran_part ! ! 82 2007-04-16 15:40:52Z raasch ! Preprocessor directives for old systems removed ! ! 70 2007-03-18 23:46:30Z raasch ! displacements for mpi_particle_type changed, age_m initialized, ! particles-package is now part of the default code ! ! 16 2007-02-15 13:16:47Z raasch ! Bugfix: MPI_REAL in MPI_ALLREDUCE replaced by MPI_INTEGER ! ! r4 | raasch | 2007-02-13 12:33:16 +0100 (Tue, 13 Feb 2007) ! RCS Log replace by Id keyword, revision history cleaned up ! ! Revision 1.24 2007/02/11 13:00:17 raasch ! Bugfix: allocation of tail_mask and new_tail_id in case of restart-runs ! Bugfix: __ was missing in a cpp-directive ! ! Revision 1.1 1999/11/25 16:22:38 raasch ! Initial revision ! ! ! Description: ! ------------ ! This routine initializes a set of particles and their attributes (position, ! radius, ..). Advection of these particles is carried out by advec_particles, ! plotting is done in data_output_dvrp. !------------------------------------------------------------------------------! USE arrays_3d USE control_parameters USE dvrp_variables USE grid_variables USE indices USE particle_attributes USE pegrid USE random_function_mod IMPLICIT NONE CHARACTER (LEN=10) :: particle_binary_version, version_on_file INTEGER :: i, j, n, nn #if defined( __parallel ) INTEGER, DIMENSION(3) :: blocklengths, displacements, types #endif LOGICAL :: uniform_particles_l REAL :: factor, pos_x, pos_y, pos_z, value #if defined( __parallel ) ! !-- Define MPI derived datatype for FORTRAN datatype particle_type (see module !-- particle_attributes). Integer length is 4 byte, Real is 8 byte blocklengths(1) = 19; blocklengths(2) = 4; blocklengths(3) = 1 displacements(1) = 0; displacements(2) = 152; displacements(3) = 168 types(1) = MPI_REAL types(2) = MPI_INTEGER types(3) = MPI_UB CALL MPI_TYPE_STRUCT( 3, blocklengths, displacements, types, & mpi_particle_type, ierr ) CALL MPI_TYPE_COMMIT( mpi_particle_type, ierr ) #endif ! !-- In case of oceans runs, the vertical index calculations need an offset, !-- because otherwise the k indices will become negative IF ( ocean ) THEN offset_ocean_nzt = nzt offset_ocean_nzt_m1 = nzt - 1 ENDIF ! !-- Check the number of particle groups. IF ( number_of_particle_groups > max_number_of_particle_groups ) THEN WRITE( message_string, * ) 'max_number_of_particle_groups =', & max_number_of_particle_groups , & '&number_of_particle_groups reset to ', & max_number_of_particle_groups CALL message( 'init_particles', 'PA0213', 0, 1, 0, 6, 0 ) number_of_particle_groups = max_number_of_particle_groups ENDIF ! !-- Set default start positions, if necessary IF ( psl(1) == 9999999.9 ) psl(1) = -0.5 * dx IF ( psr(1) == 9999999.9 ) psr(1) = ( nx + 0.5 ) * dx IF ( pss(1) == 9999999.9 ) pss(1) = -0.5 * dy IF ( psn(1) == 9999999.9 ) psn(1) = ( ny + 0.5 ) * dy IF ( psb(1) == 9999999.9 ) psb(1) = zu(nz/2) IF ( pst(1) == 9999999.9 ) pst(1) = psb(1) IF ( pdx(1) == 9999999.9 .OR. pdx(1) == 0.0 ) pdx(1) = dx IF ( pdy(1) == 9999999.9 .OR. pdy(1) == 0.0 ) pdy(1) = dy IF ( pdz(1) == 9999999.9 .OR. pdz(1) == 0.0 ) pdz(1) = zu(2) - zu(1) DO j = 2, number_of_particle_groups IF ( psl(j) == 9999999.9 ) psl(j) = psl(j-1) IF ( psr(j) == 9999999.9 ) psr(j) = psr(j-1) IF ( pss(j) == 9999999.9 ) pss(j) = pss(j-1) IF ( psn(j) == 9999999.9 ) psn(j) = psn(j-1) IF ( psb(j) == 9999999.9 ) psb(j) = psb(j-1) IF ( pst(j) == 9999999.9 ) pst(j) = pst(j-1) IF ( pdx(j) == 9999999.9 .OR. pdx(j) == 0.0 ) pdx(j) = pdx(j-1) IF ( pdy(j) == 9999999.9 .OR. pdy(j) == 0.0 ) pdy(j) = pdy(j-1) IF ( pdz(j) == 9999999.9 .OR. pdz(j) == 0.0 ) pdz(j) = pdz(j-1) ENDDO ! !-- For the first model run of a possible job chain initialize the !-- particles, otherwise read the particle data from file. IF ( TRIM( initializing_actions ) == 'read_restart_data' & .AND. read_particles_from_restartfile ) THEN ! !-- Read particle data from previous model run. !-- First open the input unit. IF ( myid_char == '' ) THEN OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN'//myid_char, & FORM='UNFORMATTED' ) ELSE OPEN ( 90, FILE='PARTICLE_RESTART_DATA_IN/'//myid_char, & FORM='UNFORMATTED' ) ENDIF ! !-- First compare the version numbers READ ( 90 ) version_on_file particle_binary_version = '3.0' IF ( TRIM( version_on_file ) /= TRIM( particle_binary_version ) ) THEN message_string = 'version mismatch concerning data from prior ' // & 'run &version on file = "' // & TRIM( version_on_file ) // & '&version in program = "' // & TRIM( particle_binary_version ) // '"' CALL message( 'init_particles', 'PA0214', 1, 2, 0, 6, 0 ) ENDIF ! !-- Read some particle parameters and the size of the particle arrays, !-- allocate them and read their contents. READ ( 90 ) bc_par_b, bc_par_lr, bc_par_ns, bc_par_t, & maximum_number_of_particles, maximum_number_of_tailpoints, & maximum_number_of_tails, number_of_initial_particles, & number_of_particles, number_of_particle_groups, & number_of_tails, particle_groups, time_prel, & time_write_particle_data, uniform_particles IF ( number_of_initial_particles /= 0 ) THEN ALLOCATE( initial_particles(1:number_of_initial_particles) ) READ ( 90 ) initial_particles ENDIF ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg), & prt_start_index(nzb:nzt+1,nysg:nyng,nxlg:nxrg), & particle_mask(maximum_number_of_particles), & particles(maximum_number_of_particles) ) READ ( 90 ) prt_count, prt_start_index READ ( 90 ) particles IF ( use_particle_tails ) THEN ALLOCATE( particle_tail_coordinates(maximum_number_of_tailpoints,5, & maximum_number_of_tails), & new_tail_id(maximum_number_of_tails), & tail_mask(maximum_number_of_tails) ) READ ( 90 ) particle_tail_coordinates ENDIF CLOSE ( 90 ) ELSE ! !-- Allocate particle arrays and set attributes of the initial set of !-- particles, which can be also periodically released at later times. !-- Also allocate array for particle tail coordinates, if needed. ALLOCATE( prt_count(nzb:nzt+1,nysg:nyng,nxlg:nxrg), & prt_start_index(nzb:nzt+1,nysg:nyng,nxlg:nxrg), & particle_mask(maximum_number_of_particles), & particles(maximum_number_of_particles) ) ! !-- Initialize all particles with dummy values (otherwise errors may !-- occur within restart runs). The reason for this is still not clear !-- and may be presumably caused by errors in the respective user-interface. particles = particle_type( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, & 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, & 0.0, 0, 0, 0, 0 ) particle_groups = particle_groups_type( 0.0, 0.0, 0.0, 0.0 ) ! !-- Set the default particle size used for dvrp plots IF ( dvrp_psize == 9999999.9 ) dvrp_psize = 0.2 * dx ! !-- Set values for the density ratio and radius for all particle !-- groups, if necessary IF ( density_ratio(1) == 9999999.9 ) density_ratio(1) = 0.0 IF ( radius(1) == 9999999.9 ) radius(1) = 0.0 DO i = 2, number_of_particle_groups IF ( density_ratio(i) == 9999999.9 ) THEN density_ratio(i) = density_ratio(i-1) ENDIF IF ( radius(i) == 9999999.9 ) radius(i) = radius(i-1) ENDDO DO i = 1, number_of_particle_groups IF ( density_ratio(i) /= 0.0 .AND. radius(i) == 0 ) THEN WRITE( message_string, * ) 'particle group #', i, 'has a', & 'density ratio /= 0 but radius = 0' CALL message( 'init_particles', 'PA0215', 1, 2, 0, 6, 0 ) ENDIF particle_groups(i)%density_ratio = density_ratio(i) particle_groups(i)%radius = radius(i) ENDDO ! !-- Calculate particle positions and store particle attributes, if !-- particle is situated on this PE n = 0 DO i = 1, number_of_particle_groups pos_z = psb(i) DO WHILE ( pos_z <= pst(i) ) pos_y = pss(i) DO WHILE ( pos_y <= psn(i) ) IF ( pos_y >= ( nys - 0.5 ) * dy .AND. & pos_y < ( nyn + 0.5 ) * dy ) THEN pos_x = psl(i) DO WHILE ( pos_x <= psr(i) ) IF ( pos_x >= ( nxl - 0.5 ) * dx .AND. & pos_x < ( nxr + 0.5 ) * dx ) THEN DO j = 1, particles_per_point n = n + 1 IF ( n > maximum_number_of_particles ) THEN WRITE( message_string, * ) 'number of initial', & 'particles (', n, ') exceeds', & '&maximum_number_of_particles (', & maximum_number_of_particles, ') on PE ', & myid CALL message( 'init_particles', 'PA0216', & 2, 2, -1, 6, 1 ) ENDIF particles(n)%x = pos_x particles(n)%y = pos_y particles(n)%z = pos_z particles(n)%age = 0.0 particles(n)%age_m = 0.0 particles(n)%dt_sum = 0.0 particles(n)%dvrp_psize = dvrp_psize particles(n)%e_m = 0.0 particles(n)%speed_x = 0.0 particles(n)%speed_x_sgs = 0.0 particles(n)%speed_y = 0.0 particles(n)%speed_y_sgs = 0.0 particles(n)%speed_z = 0.0 particles(n)%speed_z_sgs = 0.0 particles(n)%origin_x = pos_x particles(n)%origin_y = pos_y particles(n)%origin_z = pos_z particles(n)%radius = particle_groups(i)%radius particles(n)%weight_factor =initial_weighting_factor particles(n)%color = 1 particles(n)%group = i particles(n)%tailpoints = 0 IF ( use_particle_tails .AND. & MOD( n, skip_particles_for_tail ) == 0 ) THEN number_of_tails = number_of_tails + 1 ! !-- This is a temporary provisional setting (see !-- further below!) particles(n)%tail_id = number_of_tails ELSE particles(n)%tail_id = 0 ENDIF ENDDO ENDIF pos_x = pos_x + pdx(i) ENDDO ENDIF pos_y = pos_y + pdy(i) ENDDO pos_z = pos_z + pdz(i) ENDDO ENDDO number_of_initial_particles = n number_of_particles = n ! !-- Calculate the number of particles and tails of the total domain #if defined( __parallel ) IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr ) CALL MPI_ALLREDUCE( number_of_particles, total_number_of_particles, 1, & MPI_INTEGER, MPI_SUM, comm2d, ierr ) IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr ) CALL MPI_ALLREDUCE( number_of_tails, total_number_of_tails, 1, & MPI_INTEGER, MPI_SUM, comm2d, ierr ) #else total_number_of_particles = number_of_particles total_number_of_tails = number_of_tails #endif ! !-- Set a seed value for the random number generator to be exclusively !-- used for the particle code. The generated random numbers should be !-- different on the different PEs. iran_part = iran_part + myid ! !-- User modification of initial particles CALL user_init_particles ! !-- Store the initial set of particles for release at later times IF ( number_of_initial_particles /= 0 ) THEN ALLOCATE( initial_particles(1:number_of_initial_particles) ) initial_particles(1:number_of_initial_particles) = & particles(1:number_of_initial_particles) ENDIF ! !-- Add random fluctuation to particle positions IF ( random_start_position ) THEN DO n = 1, number_of_initial_particles IF ( psl(particles(n)%group) /= psr(particles(n)%group) ) THEN particles(n)%x = particles(n)%x + & ( random_function( iran_part ) - 0.5 ) * & pdx(particles(n)%group) IF ( particles(n)%x <= ( nxl - 0.5 ) * dx ) THEN particles(n)%x = ( nxl - 0.4999999999 ) * dx ELSEIF ( particles(n)%x >= ( nxr + 0.5 ) * dx ) THEN particles(n)%x = ( nxr + 0.4999999999 ) * dx ENDIF ENDIF IF ( pss(particles(n)%group) /= psn(particles(n)%group) ) THEN particles(n)%y = particles(n)%y + & ( random_function( iran_part ) - 0.5 ) * & pdy(particles(n)%group) IF ( particles(n)%y <= ( nys - 0.5 ) * dy ) THEN particles(n)%y = ( nys - 0.4999999999 ) * dy ELSEIF ( particles(n)%y >= ( nyn + 0.5 ) * dy ) THEN particles(n)%y = ( nyn + 0.4999999999 ) * dy ENDIF ENDIF IF ( psb(particles(n)%group) /= pst(particles(n)%group) ) THEN particles(n)%z = particles(n)%z + & ( random_function( iran_part ) - 0.5 ) * & pdz(particles(n)%group) ENDIF ENDDO ENDIF ! !-- Sort particles in the sequence the gridboxes are stored in the memory. !-- Only required if cloud droplets are used. IF ( cloud_droplets ) CALL sort_particles ! !-- Open file for statistical informations about particle conditions IF ( write_particle_statistics ) THEN CALL check_open( 80 ) WRITE ( 80, 8000 ) current_timestep_number, simulated_time, & number_of_initial_particles, & maximum_number_of_particles CALL close_file( 80 ) ENDIF ! !-- Check if particles are really uniform in color and radius (dvrp_size) !-- (uniform_particles is preset TRUE) IF ( uniform_particles ) THEN IF ( number_of_initial_particles == 0 ) THEN uniform_particles_l = .TRUE. ELSE n = number_of_initial_particles IF ( MINVAL( particles(1:n)%dvrp_psize ) == & MAXVAL( particles(1:n)%dvrp_psize ) .AND. & MINVAL( particles(1:n)%color ) == & MAXVAL( particles(1:n)%color ) ) THEN uniform_particles_l = .TRUE. ELSE uniform_particles_l = .FALSE. ENDIF ENDIF #if defined( __parallel ) IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr ) CALL MPI_ALLREDUCE( uniform_particles_l, uniform_particles, 1, & MPI_LOGICAL, MPI_LAND, comm2d, ierr ) #else uniform_particles = uniform_particles_l #endif ENDIF ! !-- Particles will probably become none-uniform, if their size and color !-- will be determined by flow variables IF ( particle_color /= 'none' .OR. particle_dvrpsize /= 'none' ) THEN uniform_particles = .FALSE. ENDIF ! !-- Set the beginning of the particle tails and their age IF ( use_particle_tails ) THEN ! !-- Choose the maximum number of tails with respect to the maximum number !-- of particles and skip_particles_for_tail maximum_number_of_tails = maximum_number_of_particles / & skip_particles_for_tail ! !-- Create a minimum number of tails in case that there is no tail !-- initially (otherwise, index errors will occur when adressing the !-- arrays below) IF ( maximum_number_of_tails == 0 ) maximum_number_of_tails = 10 ALLOCATE( particle_tail_coordinates(maximum_number_of_tailpoints,5, & maximum_number_of_tails), & new_tail_id(maximum_number_of_tails), & tail_mask(maximum_number_of_tails) ) particle_tail_coordinates = 0.0 minimum_tailpoint_distance = minimum_tailpoint_distance**2 number_of_initial_tails = number_of_tails nn = 0 DO n = 1, number_of_particles ! !-- Only for those particles marked above with a provisional tail_id !-- tails will be created. Particles now get their final tail_id. IF ( particles(n)%tail_id /= 0 ) THEN nn = nn + 1 particles(n)%tail_id = nn particle_tail_coordinates(1,1,nn) = particles(n)%x particle_tail_coordinates(1,2,nn) = particles(n)%y particle_tail_coordinates(1,3,nn) = particles(n)%z particle_tail_coordinates(1,4,nn) = particles(n)%color particles(n)%tailpoints = 1 IF ( minimum_tailpoint_distance /= 0.0 ) THEN particle_tail_coordinates(2,1,nn) = particles(n)%x particle_tail_coordinates(2,2,nn) = particles(n)%y particle_tail_coordinates(2,3,nn) = particles(n)%z particle_tail_coordinates(2,4,nn) = particles(n)%color particle_tail_coordinates(1:2,5,nn) = 0.0 particles(n)%tailpoints = 2 ENDIF ENDIF ENDDO ENDIF ! !-- Plot initial positions of particles (only if particle advection is !-- switched on from the beginning of the simulation (t=0)) IF ( particle_advection_start == 0.0 ) CALL data_output_dvrp ENDIF ! !-- Check boundary condition and set internal variables SELECT CASE ( bc_par_b ) CASE ( 'absorb' ) ibc_par_b = 1 CASE ( 'reflect' ) ibc_par_b = 2 CASE DEFAULT WRITE( message_string, * ) 'unknown boundary condition ', & 'bc_par_b = "', TRIM( bc_par_b ), '"' CALL message( 'init_particles', 'PA0217', 1, 2, 0, 6, 0 ) END SELECT SELECT CASE ( bc_par_t ) CASE ( 'absorb' ) ibc_par_t = 1 CASE ( 'reflect' ) ibc_par_t = 2 CASE DEFAULT WRITE( message_string, * ) 'unknown boundary condition ', & 'bc_par_t = "', TRIM( bc_par_t ), '"' CALL message( 'init_particles', 'PA0218', 1, 2, 0, 6, 0 ) END SELECT SELECT CASE ( bc_par_lr ) CASE ( 'cyclic' ) ibc_par_lr = 0 CASE ( 'absorb' ) ibc_par_lr = 1 CASE ( 'reflect' ) ibc_par_lr = 2 CASE DEFAULT WRITE( message_string, * ) 'unknown boundary condition ', & 'bc_par_lr = "', TRIM( bc_par_lr ), '"' CALL message( 'init_particles', 'PA0219', 1, 2, 0, 6, 0 ) END SELECT SELECT CASE ( bc_par_ns ) CASE ( 'cyclic' ) ibc_par_ns = 0 CASE ( 'absorb' ) ibc_par_ns = 1 CASE ( 'reflect' ) ibc_par_ns = 2 CASE DEFAULT WRITE( message_string, * ) 'unknown boundary condition ', & 'bc_par_ns = "', TRIM( bc_par_ns ), '"' CALL message( 'init_particles', 'PA0220', 1, 2, 0, 6, 0 ) END SELECT ! !-- Formats 8000 FORMAT (I6,1X,F7.2,4X,I6,71X,I6) END SUBROUTINE init_particles