|
| 1 | +""" |
| 2 | + SpatialHashingCellList{NDIMS}(; list_size) |
| 3 | +
|
| 4 | +A basic spatial hashing implementation. Similar to [`DictionaryCellList`](@ref), the domain is discretized into cells, |
| 5 | +and the particles in each cell are stored in a hash map. The hash is computed using the spatial location of each cell, |
| 6 | +as described by Ihmsen et al. (2011)(@cite Ihmsen2011). By using a hash map that stores entries only for non-empty cells, |
| 7 | +the domain is effectively infinite. The size of the hash map is recommended to be approximately twice the number of particles |
| 8 | +to balance memory consumption against the likelihood of hash collisions. |
| 9 | +
|
| 10 | +# Arguments |
| 11 | +- `NDIMS::Int`: Number of spatial dimensions (e.g., `2` or `3`). |
| 12 | +- `list_size::Int`: Size of the hash map (e.g., `2 * n_points`) . |
| 13 | +""" |
| 14 | + |
| 15 | +struct SpatialHashingCellList{NDIMS, CL, CI, CF} <: AbstractCellList |
| 16 | + points :: CL |
| 17 | + coords :: CI |
| 18 | + collisions :: CF |
| 19 | + list_size :: Int |
| 20 | +end |
| 21 | + |
| 22 | +@inline index_type(::SpatialHashingCellList) = Int32 |
| 23 | + |
| 24 | +@inline Base.ndims(::SpatialHashingCellList{NDIMS}) where {NDIMS} = NDIMS |
| 25 | + |
| 26 | +function supported_update_strategies(::SpatialHashingCellList) |
| 27 | + return (SerialUpdate,) |
| 28 | +end |
| 29 | + |
| 30 | +function SpatialHashingCellList{NDIMS}(list_size) where {NDIMS} |
| 31 | + points = [Int[] for _ in 1:list_size] |
| 32 | + collisions = [false for _ in 1:list_size] |
| 33 | + coords = [ntuple(_ -> typemin(Int), NDIMS) for _ in 1:list_size] |
| 34 | + return SpatialHashingCellList{NDIMS, typeof(points), typeof(coords), |
| 35 | + typeof(collisions)}(points, coords, collisions, list_size) |
| 36 | +end |
| 37 | + |
| 38 | +function Base.empty!(cell_list::SpatialHashingCellList) |
| 39 | + (; list_size) = cell_list |
| 40 | + NDIMS = ndims(cell_list) |
| 41 | + |
| 42 | + Base.empty!.(cell_list.points) |
| 43 | + cell_list.coords .= [ntuple(_ -> typemin(Int), NDIMS) for _ in 1:list_size] |
| 44 | + cell_list.collisions .= false |
| 45 | + return cell_list |
| 46 | +end |
| 47 | + |
| 48 | +# For each entry in the hash table, store the coordinates of the cell of the first point being inserted at this entry. |
| 49 | +# If a point with a different cell coordinate is being added, we have found a collision. |
| 50 | +function push_cell!(cell_list::SpatialHashingCellList, cell, point) |
| 51 | + (; points, coords, collisions, list_size) = cell_list |
| 52 | + NDIMS = ndims(cell_list) |
| 53 | + hash_key = spatial_hash(cell, list_size) |
| 54 | + push!(points[hash_key], point) |
| 55 | + |
| 56 | + cell_coord = coords[hash_key] |
| 57 | + if cell_coord == ntuple(_ -> typemin(Int), NDIMS) |
| 58 | + # If this cell is not used yet, set cell coordinates |
| 59 | + coords[hash_key] = cell |
| 60 | + elseif cell_coord != cell |
| 61 | + # If it is already used by a different cell, mark as collision |
| 62 | + collisions[hash_key] = true |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +function deleteat_cell!(cell_list::SpatialHashingCellList, cell, i) |
| 67 | + deleteat!(cell_list[cell], i) |
| 68 | +end |
| 69 | + |
| 70 | +@inline each_cell_index(cell_list::SpatialHashingCellList) = eachindex(cell_list.points) |
| 71 | + |
| 72 | +function copy_cell_list(cell_list::SpatialHashingCellList, search_radius, |
| 73 | + periodic_box) |
| 74 | + (; list_size) = cell_list |
| 75 | + NDIMS = ndims(cell_list) |
| 76 | + |
| 77 | + return SpatialHashingCellList{NDIMS}(list_size) |
| 78 | +end |
| 79 | + |
| 80 | +@inline function Base.getindex(cell_list::SpatialHashingCellList, cell::Tuple) |
| 81 | + return cell_list.points[spatial_hash(cell, length(cell_list.points))] |
| 82 | +end |
| 83 | + |
| 84 | +@inline function Base.getindex(cell_list::SpatialHashingCellList, i::Integer) |
| 85 | + return cell_list.points[i] |
| 86 | +end |
| 87 | + |
| 88 | +@inline function is_correct_cell(cell_list::SpatialHashingCellList{<:Any, Nothing}, |
| 89 | + coords, cell_index::Array) |
| 90 | + return coords == cell_index |
| 91 | +end |
| 92 | + |
| 93 | +# Hash functions according to Ihmsen et al. (2001) |
| 94 | +function spatial_hash(cell::NTuple{1, Real}, list_size) |
| 95 | + return mod(cell[1] * 73856093, list_size) + 1 |
| 96 | +end |
| 97 | + |
| 98 | +function spatial_hash(cell::NTuple{2, Real}, list_size) |
| 99 | + i, j = cell |
| 100 | + |
| 101 | + return mod(xor(i * 73856093, j * 19349663), list_size) + 1 |
| 102 | +end |
| 103 | + |
| 104 | +function spatial_hash(cell::NTuple{3, Real}, list_size) |
| 105 | + i, j, k = cell |
| 106 | + |
| 107 | + return mod(xor(i * 73856093, j * 19349663, k * 83492791), list_size) + 1 |
| 108 | +end |
0 commit comments