Skip to content

Commit a44f7b3

Browse files
efaulhaberLasNikas
andauthored
Make max_points_per_cell work with templates (#76)
* Make `max_points_per_cell` work with templates * Reformat code --------- Co-authored-by: Niklas Neher <[email protected]>
1 parent 26c98ba commit a44f7b3

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/cell_lists/full_grid.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ function FullGridCellList(; min_corner, max_corner, search_radius = 0.0,
4848
# Pad domain to avoid 0 in cell indices due to rounding errors.
4949
# We can't just use `eps()`, as one might use lower precision types.
5050
# This padding is safe, and will give us one more layer of cells in the worst case.
51-
min_corner = SVector(Tuple(min_corner .- 1e-3 * search_radius))
52-
max_corner = SVector(Tuple(max_corner .+ 1e-3 * search_radius))
51+
min_corner = SVector(Tuple(min_corner .- 1.0f-3 * search_radius))
52+
max_corner = SVector(Tuple(max_corner .+ 1.0f-3 * search_radius))
5353

5454
if search_radius < eps()
5555
# Create an empty "template" cell list to be used with `copy_cell_list`
56-
cells = construct_backend(backend, 0, 0)
56+
cells = construct_backend(backend, 0, max_points_per_cell)
5757
linear_indices = nothing
5858
else
5959
# Note that we don't shift everything so that the first cell starts at `min_corner`.
@@ -180,5 +180,15 @@ function copy_cell_list(cell_list::FullGridCellList, search_radius, periodic_box
180180
(; min_corner, max_corner) = cell_list
181181

182182
return FullGridCellList(; min_corner, max_corner, search_radius,
183-
backend = typeof(cell_list.cells))
183+
backend = typeof(cell_list.cells),
184+
max_points_per_cell = max_points_per_cell(cell_list.cells))
185+
end
186+
187+
function max_points_per_cell(cells::DynamicVectorOfVectors)
188+
return size(cells.backend, 1)
189+
end
190+
191+
# Fallback when backend is a `Vector{Vector{T}}`
192+
function max_points_per_cell(cells)
193+
return 100
184194
end

src/nhs_grid.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ function initialize_grid!(neighborhood_search::GridNeighborhoodSearch, y::Abstra
167167

168168
empty!(cell_list)
169169

170+
if neighborhood_search.search_radius < eps()
171+
# Cannot initialize with zero search radius.
172+
# This is used in TrixiParticles when a neighborhood search is not used.
173+
return neighborhood_search
174+
end
175+
170176
for point in axes(y, 2)
171177
# Get cell index of the point's cell
172178
point_coords = extract_svector(y, Val(ndims(neighborhood_search)), point)

test/nhs_grid.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444

4545
# Check that the update strategy is preserved
4646
nhs = GridNeighborhoodSearch{2}(cell_list = FullGridCellList(; min_corner,
47-
max_corner),
47+
max_corner,
48+
max_points_per_cell = 101),
4849
update_strategy = SerialUpdate())
4950
copy = copy_neighborhood_search(nhs, 1.0, 10)
5051

5152
@test copy.update_strategy == SerialUpdate()
53+
@test size(copy.cell_list.cells.backend, 1) == 101
5254
end
5355

5456
@testset "Cells at Coordinate Limits" begin

0 commit comments

Comments
 (0)