diff --git a/src/cell_lists/dictionary.jl b/src/cell_lists/dictionary.jl index 063887b7..98f048d4 100644 --- a/src/cell_lists/dictionary.jl +++ b/src/cell_lists/dictionary.jl @@ -25,6 +25,8 @@ struct DictionaryCellList{NDIMS} <: AbstractCellList end end +@inline Base.ndims(::DictionaryCellList{NDIMS}) where {NDIMS} = NDIMS + function supported_update_strategies(::DictionaryCellList) return (SemiParallelUpdate, SerialIncrementalUpdate, SerialUpdate) end diff --git a/src/cell_lists/full_grid.jl b/src/cell_lists/full_grid.jl index edf80781..1edd165e 100644 --- a/src/cell_lists/full_grid.jl +++ b/src/cell_lists/full_grid.jl @@ -34,6 +34,8 @@ struct FullGridCellList{C, LI, MINC, MAXC} <: AbstractCellList max_corner :: MAXC end +@inline Base.ndims(cell_list::FullGridCellList) = ndims(cell_list.linear_indices) + function supported_update_strategies(::FullGridCellList{<:DynamicVectorOfVectors}) return (ParallelIncrementalUpdate, ParallelUpdate, SemiParallelUpdate, SerialIncrementalUpdate, SerialUpdate) @@ -47,6 +49,15 @@ function FullGridCellList(; min_corner, max_corner, search_radius = zero(eltype(min_corner)), backend = DynamicVectorOfVectors{Int32}, max_points_per_cell = 100) + if length(min_corner) != length(max_corner) + throw(ArgumentError("min_corner and max_corner must have the same length")) + end + + if length(min_corner) > 100 + throw(ArgumentError("FullGridCellList only supports up to 100 dimensions, " * + "check your `min_corner` and `max_corner`")) + end + # Add one layer in each direction to make sure neighbor cells exist. # Also pad domain a little more to avoid 0 in cell indices due to rounding errors. # We can't just use `eps()`, as one might use lower precision types. diff --git a/src/nhs_grid.jl b/src/nhs_grid.jl index b1ee4faf..7b0d5929 100644 --- a/src/nhs_grid.jl +++ b/src/nhs_grid.jl @@ -75,6 +75,11 @@ function GridNeighborhoodSearch{NDIMS}(; search_radius = 0.0, n_points = 0, periodic_box = nothing, cell_list = DictionaryCellList{NDIMS}(), update_strategy = nothing) where {NDIMS} + if ndims(cell_list) != NDIMS + throw(ArgumentError("a $(NDIMS)D cell list is required for " * + "a GridNeighborhoodSearch{$(NDIMS)}")) + end + if isnothing(update_strategy) # Automatically choose best available update option for this cell list update_strategy = first(supported_update_strategies(cell_list))() diff --git a/test/cell_lists/full_grid.jl b/test/cell_lists/full_grid.jl index 4b25c58a..26ee869a 100644 --- a/test/cell_lists/full_grid.jl +++ b/test/cell_lists/full_grid.jl @@ -1,4 +1,20 @@ @testset "`FullGridCellList`" verbose=true begin + # Test that an error is thrown when too many dimensions are used + @testset "constructor" begin + min_corner = zeros(101) + max_corner = ones(101) + search_radius = 1.0 + + error_string = "FullGridCellList only supports up to 100 dimensions" + @test_throws error_string FullGridCellList(; min_corner, max_corner) + @test_throws error_string FullGridCellList(; min_corner, max_corner, search_radius) + + min_corner = zeros(3) + max_corner = ones(2) + error_string = "min_corner and max_corner must have the same length" + @test_throws error_string FullGridCellList(; min_corner, max_corner) + end + # Test that `update!` throws an error when a particle is outside the bounding box @testset "`update!` bounds check" begin @testset "$(N)D" for N in 1:3 diff --git a/test/nhs_grid.jl b/test/nhs_grid.jl index 59facc83..17ff2fa9 100644 --- a/test/nhs_grid.jl +++ b/test/nhs_grid.jl @@ -1,5 +1,8 @@ @testset verbose=true "GridNeighborhoodSearch" begin @testset "Constructor" begin + error_str = "a 2D cell list is required for a GridNeighborhoodSearch{2}" + @test_throws error_str GridNeighborhoodSearch{2}(cell_list = DictionaryCellList{3}()) + error_str = "is not a valid update strategy" @test_throws "test $error_str" GridNeighborhoodSearch{2}(update_strategy = :test)