|
1 | 1 | # This file contains tests for the generic functions in `src/neighborhood_search.jl` and |
2 | 2 | # tests comparing all NHS implementations against the `TrivialNeighborhoodSearch`. |
3 | | -@testset verbose=true "Neighborhood Searches" begin |
| 3 | +@testset verbose=true "All Neighborhood Searches" begin |
4 | 4 | @testset verbose=true "Periodicity" begin |
5 | 5 | # These examples are constructed by hand and are therefore a good test for the |
6 | 6 | # trivial neighborhood search as well. |
|
76 | 76 | end |
77 | 77 | end |
78 | 78 | 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 |
79 | 145 | end; |
0 commit comments