Skip to content

Commit 84943ed

Browse files
authored
Add tests comparing neighbor lists to trivial NHS (#15)
* Add tests comparing neighbor lists to trivial NHS * Extract search radius * Add `Random` as test dependency * Remove `using StaticArrays`
1 parent 9ac6ace commit 84943ed

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
23
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
34

45
[compat]

test/neighborhood_search.jl

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file contains tests for the generic functions in `src/neighborhood_search.jl` and
22
# tests comparing all NHS implementations against the `TrivialNeighborhoodSearch`.
3-
@testset verbose=true "Neighborhood Searches" begin
3+
@testset verbose=true "All Neighborhood Searches" begin
44
@testset verbose=true "Periodicity" begin
55
# These examples are constructed by hand and are therefore a good test for the
66
# trivial neighborhood search as well.
@@ -76,4 +76,70 @@
7676
end
7777
end
7878
end
79+
80+
@testset verbose=true "Compare Against `TrivialNeighborhoodSearch`" begin
81+
cloud_sizes = [
82+
(10, 11),
83+
(100, 90),
84+
(9, 10, 7),
85+
(39, 40, 41),
86+
]
87+
88+
seeds = [1, 2]
89+
@testset verbose=true "$(length(cloud_size))D with $(prod(cloud_size)) Particles ($(seed == 1 ? "`initialize!`" : "`update!`"))" for cloud_size in cloud_sizes,
90+
seed in seeds
91+
92+
coords = point_cloud(cloud_size, seed = seed)
93+
NDIMS = length(cloud_size)
94+
search_radius = 2.5
95+
96+
# Use different coordinates for `initialize!` and then `update!` with the
97+
# correct coordinates to make sure that `update!` is working as well.
98+
coords_initialize = point_cloud(cloud_size, seed = 1)
99+
100+
# Compute expected neighbor lists by brute-force looping over all particles
101+
# as potential neighbors (`TrivialNeighborhoodSearch`).
102+
trivial_nhs = TrivialNeighborhoodSearch{NDIMS}(search_radius, axes(coords, 2))
103+
104+
neighbors_expected = [Int[] for _ in axes(coords, 2)]
105+
106+
for_particle_neighbor(coords, coords, trivial_nhs,
107+
parallel = false) do particle, neighbor,
108+
pos_diff, distance
109+
append!(neighbors_expected[particle], neighbor)
110+
end
111+
112+
neighborhood_searches = [
113+
GridNeighborhoodSearch{NDIMS}(search_radius, size(coords, 2)),
114+
]
115+
116+
neighborhood_searches_names = [
117+
"`GridNeighborhoodSearch`",
118+
]
119+
120+
@testset "$(neighborhood_searches_names[i])" for i in eachindex(neighborhood_searches_names)
121+
nhs = neighborhood_searches[i]
122+
123+
# Initialize with `seed = 1`
124+
initialize!(nhs, coords_initialize, coords_initialize)
125+
126+
# For other seeds, update with the correct coordinates.
127+
# This way, we test only `initialize!` when `seed == 1`,
128+
# and `initialize!` plus `update!` else.
129+
if seed != 1
130+
update!(nhs, coords, coords)
131+
end
132+
133+
neighbors = [Int[] for _ in axes(coords, 2)]
134+
135+
for_particle_neighbor(coords, coords, nhs,
136+
parallel = false) do particle, neighbor,
137+
pos_diff, distance
138+
append!(neighbors[particle], neighbor)
139+
end
140+
141+
@test sort.(neighbors) == neighbors_expected
142+
end
143+
end
144+
end
79145
end;

test/point_cloud.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Random
2+
3+
# Generate a rectangular point cloud, optionally with a perturbation in the point positions
4+
function point_cloud(n_points_per_dimension;
5+
seed = 1, perturbation_factor_position = 1.0)
6+
# Fixed seed to ensure reproducibility
7+
Random.seed!(seed)
8+
9+
n_dims = length(n_points_per_dimension)
10+
coordinates = Array{Float64}(undef, n_dims, prod(n_points_per_dimension))
11+
cartesian_indices = CartesianIndices(n_points_per_dimension)
12+
13+
for i in axes(coordinates, 2)
14+
coordinates[:, i] .= Tuple(cartesian_indices[i])
15+
end
16+
17+
perturb!(coordinates, perturbation_factor_position * 0.5)
18+
19+
return coordinates
20+
end
21+
22+
function perturb!(data, amplitude)
23+
for i in eachindex(data)
24+
# Perturbation in the interval (-amplitude, amplitude)
25+
data[i] += 2 * amplitude * rand() - amplitude
26+
end
27+
28+
return data
29+
end

test/test_util.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,5 @@ macro test_nowarn_mod(expr, additional_ignore_content = String[])
7474
end
7575
end
7676
end
77+
78+
include("point_cloud.jl")

0 commit comments

Comments
 (0)