Skip to content

Commit 8e42045

Browse files
committed
Add tests comparing neighbor lists to trivial NHS
1 parent 16abe29 commit 8e42045

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

test/neighborhood_search.jl

Lines changed: 66 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,69 @@
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+
95+
# Use different coordinates for `initialize!` and then `update!` with the
96+
# correct coordinates to make sure that `update!` is working as well.
97+
coords_initialize = point_cloud(cloud_size, seed = 1)
98+
99+
# Compute expected neighbor lists by brute-force looping over all particles
100+
# as potential neighbors (`TrivialNeighborhoodSearch`).
101+
trivial_nhs = TrivialNeighborhoodSearch{NDIMS}(2.5, axes(coords, 2))
102+
103+
neighbors_expected = [Int[] for _ in axes(coords, 2)]
104+
105+
for_particle_neighbor(coords, coords, trivial_nhs,
106+
parallel = false) do particle, neighbor,
107+
pos_diff, distance
108+
append!(neighbors_expected[particle], neighbor)
109+
end
110+
111+
neighborhood_searches = [
112+
GridNeighborhoodSearch{NDIMS}(2.5, size(coords, 2)),
113+
]
114+
115+
neighborhood_searches_names = [
116+
"`GridNeighborhoodSearch`",
117+
]
118+
119+
@testset "$(neighborhood_searches_names[i])" for i in eachindex(neighborhood_searches_names)
120+
nhs = neighborhood_searches[i]
121+
122+
# Initialize with `seed = 1`
123+
initialize!(nhs, coords_initialize, coords_initialize)
124+
125+
# For other seeds, update with the correct coordinates.
126+
# This way, we test only `initialize!` when `seed == 1`,
127+
# and `initialize!` plus `update!` else.
128+
if seed != 1
129+
update!(nhs, coords, coords)
130+
end
131+
132+
neighbors = [Int[] for _ in axes(coords, 2)]
133+
134+
for_particle_neighbor(coords, coords, nhs,
135+
parallel = false) do particle, neighbor,
136+
pos_diff, distance
137+
append!(neighbors[particle], neighbor)
138+
end
139+
140+
@test sort.(neighbors) == neighbors_expected
141+
end
142+
end
143+
end
79144
end;

test/point_cloud.jl

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