Skip to content

Commit a2d9f64

Browse files
authored
Replace all references to "particle" by "point" (trixi-framework#31)
* Replace all references to "particle" by "point" * Reformat * Fix typo
1 parent 591827b commit a2d9f64

File tree

9 files changed

+150
-159
lines changed

9 files changed

+150
-159
lines changed

src/PointNeighbors.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include("cell_lists/cell_lists.jl")
1313
include("nhs_grid.jl")
1414
include("nhs_neighbor_lists.jl")
1515

16-
export for_particle_neighbor, foreach_neighbor
16+
export foreach_point_neighbor, foreach_neighbor
1717
export TrivialNeighborhoodSearch, GridNeighborhoodSearch, PrecomputedNeighborhoodSearch
1818
export initialize!, update!, initialize_grid!, update_grid!
1919

src/cell_lists/dictionary.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ function Base.empty!(cell_list::DictionaryCellList)
1616
return cell_list
1717
end
1818

19-
function push_cell!(cell_list::DictionaryCellList, cell, particle)
19+
function push_cell!(cell_list::DictionaryCellList, cell, point)
2020
(; hashtable) = cell_list
2121

2222
if haskey(hashtable, cell)
23-
append!(hashtable[cell], particle)
23+
append!(hashtable[cell], point)
2424
else
25-
hashtable[cell] = [particle]
25+
hashtable[cell] = [point]
2626
end
2727

2828
return cell_list

src/neighborhood_search.jl

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See also [`update!`](@ref).
1515
@inline initialize!(search::AbstractNeighborhoodSearch, x, y) = search
1616

1717
"""
18-
update!(search::AbstractNeighborhoodSearch, x, y; particles_moving = (true, true))
18+
update!(search::AbstractNeighborhoodSearch, x, y; points_moving = (true, true))
1919
2020
Update an already initialized neighborhood search with the two coordinate arrays `x` and `y`.
2121
@@ -29,15 +29,15 @@ If incremental updates are not possible for an implementation, `update!` will fa
2929
to a regular `initialize!`.
3030
3131
Some neighborhood searches might not need to update when only `x` changed since the last
32-
`update!` or `initialize!` and `y` did not change. Pass `particles_moving = (true, false)`
32+
`update!` or `initialize!` and `y` did not change. Pass `points_moving = (true, false)`
3333
in this case to avoid unnecessary updates.
34-
The first flag in `particles_moving` indicates if points in `x` are moving.
34+
The first flag in `points_moving` indicates if points in `x` are moving.
3535
The second flag indicates if points in `y` are moving.
3636
3737
See also [`initialize!`](@ref).
3838
"""
3939
@inline function update!(search::AbstractNeighborhoodSearch, x, y;
40-
particles_moving = (true, true))
40+
points_moving = (true, true))
4141
return search
4242
end
4343

@@ -56,43 +56,42 @@ end
5656
# Otherwise, unspecialized code will cause a lot of allocations
5757
# and heavily impact performance.
5858
# See https://docs.julialang.org/en/v1/manual/performance-tips/#Be-aware-of-when-Julia-avoids-specializing
59-
function for_particle_neighbor(f::T, system_coords, neighbor_coords, neighborhood_search;
60-
particles = axes(system_coords, 2),
61-
parallel = true) where {T}
62-
for_particle_neighbor(f, system_coords, neighbor_coords, neighborhood_search, particles,
63-
Val(parallel))
59+
function foreach_point_neighbor(f::T, system_coords, neighbor_coords, neighborhood_search;
60+
points = axes(system_coords, 2),
61+
parallel = true) where {T}
62+
foreach_point_neighbor(f, system_coords, neighbor_coords, neighborhood_search, points,
63+
Val(parallel))
6464
end
6565

66-
@inline function for_particle_neighbor(f, system_coords, neighbor_coords,
67-
neighborhood_search, particles, parallel::Val{true})
68-
@threaded for particle in particles
69-
foreach_neighbor(f, system_coords, neighbor_coords, neighborhood_search, particle)
66+
@inline function foreach_point_neighbor(f, system_coords, neighbor_coords,
67+
neighborhood_search, points, parallel::Val{true})
68+
@threaded for point in points
69+
foreach_neighbor(f, system_coords, neighbor_coords, neighborhood_search, point)
7070
end
7171

7272
return nothing
7373
end
7474

75-
@inline function for_particle_neighbor(f, system_coords, neighbor_coords,
76-
neighborhood_search, particles, parallel::Val{false})
77-
for particle in particles
78-
foreach_neighbor(f, system_coords, neighbor_coords, neighborhood_search, particle)
75+
@inline function foreach_point_neighbor(f, system_coords, neighbor_coords,
76+
neighborhood_search, points, parallel::Val{false})
77+
for point in points
78+
foreach_neighbor(f, system_coords, neighbor_coords, neighborhood_search, point)
7979
end
8080

8181
return nothing
8282
end
8383

8484
@inline function foreach_neighbor(f, system_coords, neighbor_system_coords,
85-
neighborhood_search, particle;
85+
neighborhood_search, point;
8686
search_radius = neighborhood_search.search_radius)
8787
(; periodic_box) = neighborhood_search
8888

89-
particle_coords = extract_svector(system_coords, Val(ndims(neighborhood_search)),
90-
particle)
91-
for neighbor in eachneighbor(particle_coords, neighborhood_search)
89+
point_coords = extract_svector(system_coords, Val(ndims(neighborhood_search)), point)
90+
for neighbor in eachneighbor(point_coords, neighborhood_search)
9291
neighbor_coords = extract_svector(neighbor_system_coords,
9392
Val(ndims(neighborhood_search)), neighbor)
9493

95-
pos_diff = particle_coords - neighbor_coords
94+
pos_diff = point_coords - neighbor_coords
9695
distance2 = dot(pos_diff, pos_diff)
9796

9897
pos_diff, distance2 = compute_periodic_distance(pos_diff, distance2, search_radius,
@@ -102,8 +101,8 @@ end
102101
distance = sqrt(distance2)
103102

104103
# Inline to avoid loss of performance
105-
# compared to not using `for_particle_neighbor`.
106-
@inline f(particle, neighbor, pos_diff, distance)
104+
# compared to not using `foreach_point_neighbor`.
105+
@inline f(point, neighbor, pos_diff, distance)
107106
end
108107
end
109108
end
@@ -113,8 +112,7 @@ end
113112
return pos_diff, distance2
114113
end
115114

116-
@inline function compute_periodic_distance(pos_diff, distance2, search_radius,
117-
periodic_box)
115+
@inline function compute_periodic_distance(pos_diff, distance2, search_radius, periodic_box)
118116
if distance2 > search_radius^2
119117
# Use periodic `pos_diff`
120118
pos_diff -= periodic_box.size .* round.(pos_diff ./ periodic_box.size)

src/nhs_grid.jl

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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
55
Simple grid-based neighborhood search with uniform search radius.
66
The 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.
88
Instead of representing a finite domain by an array of cells, a potentially infinite domain
99
is represented by storing cell lists in a hash table (using Julia's `Dict` data structure),
1010
indexed by the cell index tuple
@@ -14,18 +14,18 @@ indexed by the cell index tuple
1414
```
1515
where ``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
1818
cells are considered.
1919
2020
See 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,
2323
since 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() ||
@@ -105,7 +105,7 @@ end
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)
110110
end
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
136136
end
137137

138138
function 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)
146146
end
@@ -151,40 +151,40 @@ function update_grid!(neighborhood_search::GridNeighborhoodSearch{NDIMS},
151151
update_grid!(neighborhood_search, i -> extract_svector(y, Val(NDIMS), i))
152152
end
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
155155
function 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

@@ -213,8 +213,8 @@ end
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,
@@ -233,8 +233,8 @@ end
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)
239239
end
240240

@@ -245,8 +245,8 @@ end
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)
251251
end
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)
263263
end
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)]
@@ -300,9 +300,9 @@ end
300300
return Tuple(floor_to_int.(offset_coords ./ cell_size))
301301
end
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
@@ -322,9 +322,9 @@ end
322322
# Create a copy of a neighborhood search but with a different search radius
323323
function 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

Comments
 (0)