11@doc raw """
2- GridNeighborhoodSearch{NDIMS}(search_radius, n_particles ; periodic_box_min_corner=nothing,
2+ GridNeighborhoodSearch{NDIMS}(search_radius, n_points ; periodic_box_min_corner=nothing,
33 periodic_box_max_corner=nothing, threaded_nhs_update=true)
44
55Simple grid-based neighborhood search with uniform search radius.
66The domain is divided into a regular grid.
7- For each (non-empty) grid cell, a list of particles in this cell is stored.
7+ For each (non-empty) grid cell, a list of points in this cell is stored.
88Instead of representing a finite domain by an array of cells, a potentially infinite domain
99is represented by storing cell lists in a hash table (using Julia's `Dict` data structure),
1010indexed by the cell index tuple
@@ -14,18 +14,18 @@ indexed by the cell index tuple
1414```
1515where ``x, y, z`` are the space coordinates and ``d`` is the search radius.
1616
17- To find particles within the search radius around a point , only particles in the neighboring
17+ To find points within the search radius around a position , only points in the neighboring
1818cells are considered.
1919
2020See also (Chalela et al., 2021), (Ihmsen et al. 2011, Section 4.4).
2121
22- As opposed to (Ihmsen et al. 2011), we do not sort the particles in any way,
22+ As opposed to (Ihmsen et al. 2011), we do not sort the points in any way,
2323since not sorting makes our implementation a lot faster (although less parallelizable).
2424
2525# Arguments
2626- `NDIMS`: Number of dimensions.
2727- `search_radius`: The uniform search radius.
28- - `n_particles `: Total number of particles .
28+ - `n_points `: Total number of points .
2929
3030# Keywords
3131- `periodic_box_min_corner`: In order to use a (rectangular) periodic domain, pass the
@@ -36,7 +36,7 @@ since not sorting makes our implementation a lot faster (although less paralleli
3636 directions.
3737- `threaded_nhs_update=true`: Can be used to deactivate thread parallelization in the neighborhood search update.
3838 This can be one of the largest sources of variations between simulations
39- with different thread numbers due to particle ordering changes.
39+ with different thread numbers due to neighbor ordering changes.
4040
4141## References
4242- M. Chalela, E. Sillero, L. Pereyra, M.A. Garcia, J.B. Cabral, M. Lares, M. Merchán.
@@ -58,14 +58,14 @@ struct GridNeighborhoodSearch{NDIMS, ELTYPE, CL, PB} <: AbstractNeighborhoodSear
5858 cell_buffer_indices :: Vector{Int} # Store which entries of `cell_buffer` are initialized
5959 threaded_nhs_update :: Bool
6060
61- function GridNeighborhoodSearch {NDIMS} (search_radius, n_particles ;
61+ function GridNeighborhoodSearch {NDIMS} (search_radius, n_points ;
6262 periodic_box_min_corner = nothing ,
6363 periodic_box_max_corner = nothing ,
6464 threaded_nhs_update = true ) where {NDIMS}
6565 ELTYPE = typeof (search_radius)
6666 cell_list = DictionaryCellList {NDIMS} ()
6767
68- cell_buffer = Array {NTuple{NDIMS, Int}, 2} (undef, n_particles , Threads. nthreads ())
68+ cell_buffer = Array {NTuple{NDIMS, Int}, 2} (undef, n_points , Threads. nthreads ())
6969 cell_buffer_indices = zeros (Int, Threads. nthreads ())
7070
7171 if search_radius < eps () ||
105105
106106@inline Base. ndims (neighborhood_search:: GridNeighborhoodSearch{NDIMS} ) where {NDIMS} = NDIMS
107107
108- @inline function nparticles (neighborhood_search:: GridNeighborhoodSearch )
108+ @inline function npoints (neighborhood_search:: GridNeighborhoodSearch )
109109 return size (neighborhood_search. cell_buffer, 1 )
110110end
111111
@@ -124,23 +124,23 @@ function initialize_grid!(neighborhood_search::GridNeighborhoodSearch, coords_fu
124124
125125 empty! (cell_list)
126126
127- for particle in 1 : nparticles (neighborhood_search)
128- # Get cell index of the particle 's cell
129- cell = cell_coords (coords_fun (particle ), neighborhood_search)
127+ for point in 1 : npoints (neighborhood_search)
128+ # Get cell index of the point 's cell
129+ cell = cell_coords (coords_fun (point ), neighborhood_search)
130130
131- # Add particle to corresponding cell
132- push_cell! (cell_list, cell, particle )
131+ # Add point to corresponding cell
132+ push_cell! (cell_list, cell, point )
133133 end
134134
135135 return neighborhood_search
136136end
137137
138138function update! (neighborhood_search:: GridNeighborhoodSearch ,
139139 x:: AbstractMatrix , y:: AbstractMatrix ;
140- particles_moving = (true , true ))
141- # The coordinates of the first set of particles are irrelevant for this NHS.
140+ points_moving = (true , true ))
141+ # The coordinates of the first set of points are irrelevant for this NHS.
142142 # Only update when the second set is moving.
143- particles_moving [2 ] || return neighborhood_search
143+ points_moving [2 ] || return neighborhood_search
144144
145145 update_grid! (neighborhood_search, y)
146146end
@@ -151,40 +151,40 @@ function update_grid!(neighborhood_search::GridNeighborhoodSearch{NDIMS},
151151 update_grid! (neighborhood_search, i -> extract_svector (y, Val (NDIMS), i))
152152end
153153
154- # Modify the existing hash table by moving particles into their new cells
154+ # Modify the existing hash table by moving points into their new cells
155155function update_grid! (neighborhood_search:: GridNeighborhoodSearch , coords_fun)
156156 (; cell_list, cell_buffer, cell_buffer_indices, threaded_nhs_update) = neighborhood_search
157157
158158 # Reset `cell_buffer` by moving all pointers to the beginning
159159 cell_buffer_indices .= 0
160160
161- # Find all cells containing particles that now belong to another cell
161+ # Find all cells containing points that now belong to another cell
162162 mark_changed_cell! (neighborhood_search, cell_list, coords_fun,
163163 Val (threaded_nhs_update))
164164
165- # Iterate over all marked cells and move the particles into their new cells.
165+ # Iterate over all marked cells and move the points into their new cells.
166166 for thread in 1 : Threads. nthreads ()
167167 # Only the entries `1:cell_buffer_indices[thread]` are initialized for `thread`.
168168 for i in 1 : cell_buffer_indices[thread]
169169 cell = cell_buffer[i, thread]
170- particles = cell_list[cell]
170+ points = cell_list[cell]
171171
172- # Find all particles whose coordinates do not match this cell
173- moved_particle_indices = (i for i in eachindex (particles )
174- if cell_coords (coords_fun (particles [i]),
175- neighborhood_search) != cell)
172+ # Find all points whose coordinates do not match this cell
173+ moved_point_indices = (i for i in eachindex (points )
174+ if cell_coords (coords_fun (points [i]),
175+ neighborhood_search) != cell)
176176
177- # Add moved particles to new cell
178- for i in moved_particle_indices
179- particle = particles [i]
180- new_cell_coords = cell_coords (coords_fun (particle ), neighborhood_search)
177+ # Add moved points to new cell
178+ for i in moved_point_indices
179+ point = points [i]
180+ new_cell_coords = cell_coords (coords_fun (point ), neighborhood_search)
181181
182- # Add particle to corresponding cell or create cell if it does not exist
183- push_cell! (cell_list, new_cell_coords, particle )
182+ # Add point to corresponding cell or create cell if it does not exist
183+ push_cell! (cell_list, new_cell_coords, point )
184184 end
185185
186- # Remove moved particles from this cell
187- deleteat_cell! (cell_list, cell, moved_particle_indices )
186+ # Remove moved points from this cell
187+ deleteat_cell! (cell_list, cell, moved_point_indices )
188188 end
189189 end
190190
213213@inline function mark_changed_cell! (neighborhood_search, cell, coords_fun)
214214 (; cell_list, cell_buffer, cell_buffer_indices) = neighborhood_search
215215
216- for particle in cell_list[cell]
217- if cell_coords (coords_fun (particle ), neighborhood_search) != cell
216+ for point in cell_list[cell]
217+ if cell_coords (coords_fun (point ), neighborhood_search) != cell
218218 # Mark this cell and continue with the next one.
219219 #
220220 # `cell_buffer` is preallocated,
233233 # Generator of all neighboring cells to consider
234234 neighboring_cells = ((x + i) for i in - 1 : 1 )
235235
236- # Merge all lists of particles in the neighboring cells into one iterator
237- Iterators. flatten (particles_in_cell (cell, neighborhood_search)
236+ # Merge all lists of points in the neighboring cells into one iterator
237+ Iterators. flatten (points_in_cell (cell, neighborhood_search)
238238 for cell in neighboring_cells)
239239end
240240
245245 # Generator of all neighboring cells to consider
246246 neighboring_cells = ((x + i, y + j) for i in - 1 : 1 , j in - 1 : 1 )
247247
248- # Merge all lists of particles in the neighboring cells into one iterator
249- Iterators. flatten (particles_in_cell (cell, neighborhood_search)
248+ # Merge all lists of points in the neighboring cells into one iterator
249+ Iterators. flatten (points_in_cell (cell, neighborhood_search)
250250 for cell in neighboring_cells)
251251end
252252
@@ -257,12 +257,12 @@ end
257257 # Generator of all neighboring cells to consider
258258 neighboring_cells = ((x + i, y + j, z + k) for i in - 1 : 1 , j in - 1 : 1 , k in - 1 : 1 )
259259
260- # Merge all lists of particles in the neighboring cells into one iterator
261- Iterators. flatten (particles_in_cell (cell, neighborhood_search)
260+ # Merge all lists of points in the neighboring cells into one iterator
261+ Iterators. flatten (points_in_cell (cell, neighborhood_search)
262262 for cell in neighboring_cells)
263263end
264264
265- @inline function particles_in_cell (cell_index, neighborhood_search)
265+ @inline function points_in_cell (cell_index, neighborhood_search)
266266 (; cell_list) = neighborhood_search
267267
268268 return cell_list[periodic_cell_index (cell_index, neighborhood_search)]
300300 return Tuple (floor_to_int .(offset_coords ./ cell_size))
301301end
302302
303- # When particles end up with coordinates so big that the cell coordinates
303+ # When points end up with coordinates so big that the cell coordinates
304304# exceed the range of Int, then `floor(Int, i)` will fail with an InexactError.
305- # In this case, we can just use typemax(Int), since we can assume that particles
305+ # In this case, we can just use typemax(Int), since we can assume that points
306306# that far away will not interact with anything, anyway.
307307# This usually indicates an instability, but we don't want the simulation to crash,
308308# since adaptive time integration methods may detect the instability and reject the
322322# Create a copy of a neighborhood search but with a different search radius
323323function copy_neighborhood_search (nhs:: GridNeighborhoodSearch , search_radius, x, y)
324324 if nhs. periodic_box === nothing
325- search = GridNeighborhoodSearch {ndims(nhs)} (search_radius, nparticles (nhs))
325+ search = GridNeighborhoodSearch {ndims(nhs)} (search_radius, npoints (nhs))
326326 else
327- search = GridNeighborhoodSearch {ndims(nhs)} (search_radius, nparticles (nhs),
327+ search = GridNeighborhoodSearch {ndims(nhs)} (search_radius, npoints (nhs),
328328 periodic_box_min_corner = nhs. periodic_box. min_corner,
329329 periodic_box_max_corner = nhs. periodic_box. max_corner)
330330 end
0 commit comments