@@ -16,24 +16,23 @@ module m_particle_bed
1616
1717contains
1818
19- !> Generate all particle beds and append the resulting particles to patch_ib.
20- !! Called on rank 0 only, before s_mpi_bcast_user_inputs.
21- !! Uses a spatial hash grid (cell size = min_dist) so each candidate requires
22- !! only 3 ^dim distance checks on average instead of O(n).
19+ !> Generate all particle beds and append the resulting particles to patch_ib. Called on rank 0 only, before
20+ !! s_mpi_bcast_user_inputs. Uses a spatial hash grid (cell size = min_dist) so each candidate requires only 3 ^dim distance
21+ !! checks on average instead of O(n).
2322 impure subroutine s_generate_particle_beds ()
2423
25- integer :: b, ib_idx, geom
26- integer :: n_placed, n_attempts, max_attempts
27- real (wp) :: xmin, xmax, ymin, ymax, zmin, zmax, min_dist
28- real (wp) :: rx, ry, rz, dist
29- integer :: seed
30- logical :: overlaps
31- real (wp), allocatable :: placed(:, :)
24+ integer :: b, ib_idx, geom
25+ integer :: n_placed, n_attempts, max_attempts
26+ real (wp) :: xmin, xmax, ymin, ymax, zmin, zmax, min_dist
27+ real (wp) :: rx, ry, rz, dist
28+ integer :: seed
29+ logical :: overlaps
30+ real (wp), allocatable :: placed(:,:)
3231
3332 ! Spatial hash grid
34- integer :: hash_size, slot
35- integer :: bx, by, bz, nbx, nby, nbz
36- integer :: dx_b, dy_b, dz_b, dz_lo, dz_hi, j
33+ integer :: hash_size, slot
34+ integer :: bx, by, bz, nbx, nby, nbz
35+ integer :: dx_b, dy_b, dz_b, dz_lo, dz_hi, j
3736 integer , allocatable :: hash_head(:), chain_next(:)
3837
3938 if (num_particle_beds == 0 ) return
@@ -66,8 +65,8 @@ contains
6665
6766 allocate (placed(3 , particle_bed(b)%num_particles))
6867
69- ! Hash table: 4x overprovisioned for ~25 % load factor, minimum 16 buckets.
70- ! chain_next(i) links placed particle i to the previous occupant of its bucket.
68+ ! Hash table: 4x overprovisioned for ~25 % load factor, minimum 16 buckets. chain_next(i) links placed particle i to the
69+ ! previous occupant of its bucket.
7170 hash_size = max (16 , 4 * particle_bed(b)%num_particles)
7271 allocate (hash_head(hash_size))
7372 allocate (chain_next(particle_bed(b)%num_particles))
@@ -90,7 +89,7 @@ contains
9089 bz = 0
9190 if (p /= 0 ) bz = int (floor(rz/ min_dist))
9291
93- ! Check 3x3 (x3) neighboring bins — O(1 ) average via hash lookup
92+ ! Check 3x3 (x3) neighboring bins - O(1 ) average via hash lookup
9493 overlaps = .false.
9594 outer: do dx_b = - 1 , 1
9695 do dy_b = - 1 , 1
@@ -104,8 +103,7 @@ contains
104103 if (p == 0 ) then
105104 dist = sqrt ((rx - placed(1 , j))** 2 + (ry - placed(2 , j))** 2 )
106105 else
107- dist = sqrt ((rx - placed(1 , j))** 2 + (ry - placed(2 , j))** 2 &
108- + (rz - placed(3 , j))** 2 )
106+ dist = sqrt ((rx - placed(1 , j))** 2 + (ry - placed(2 , j))** 2 + (rz - placed(3 , j))** 2 )
109107 end if
110108 if (dist < min_dist) then
111109 overlaps = .true.
@@ -143,8 +141,8 @@ contains
143141 end do
144142
145143 if (n_placed < particle_bed(b)%num_particles) then
146- print ' ("WARNING: particle_bed(",I0,"): placed ",I0," of ",I0," particles after ",I0," attempts")' , &
147- b, n_placed, particle_bed(b)%num_particles, n_attempts
144+ print ' ("WARNING: particle_bed(",I0,"): placed ",I0," of ",I0," particles after ",I0," attempts")' , b, n_placed, &
145+ & particle_bed(b)%num_particles, n_attempts
148146 end if
149147
150148 deallocate (placed, hash_head, chain_next)
@@ -156,7 +154,7 @@ contains
156154 function f_xorshift (seed ) result(rval)
157155
158156 integer , intent (inout ) :: seed
159- real (wp) :: rval
157+ real (wp) :: rval
160158
161159 seed = ieor (seed, ishft(seed, 13 ))
162160 seed = ieor (seed, ishft(seed, - 17 ))
@@ -166,14 +164,13 @@ contains
166164
167165 end function f_xorshift
168166
169- !> Hash bin coordinates to a 1 - indexed slot in [1 , hash_size].
170- !! Uses large prime multipliers to spread bins across buckets.
171- !! Hash collisions are benign: the distance check catches false neighbours.
167+ !> Hash bin coordinates to a 1 - indexed slot in [1 , hash_size]. Uses large prime multipliers to spread bins across buckets. Hash
168+ !! collisions are benign: the distance check catches false neighbours.
172169 function f_bin_hash (bx , by , bz , hash_size ) result(slot)
173170
174171 integer , intent (in ) :: bx, by, bz, hash_size
175- integer :: slot
176- integer (8 ) :: key
172+ integer :: slot
173+ integer (8 ) :: key
177174
178175 key = ieor (ieor (int (bx, 8 )* 73856093_8 , int (by, 8 )* 19349663_8 ), int (bz, 8 )* 83492791_8 )
179176 slot = int (mod (abs (key), int (hash_size, 8 ))) + 1
0 commit comments