@@ -94,14 +94,16 @@ function build_neighbour_cells(ncells::NTuple{d,Int}) where {d}
9494 neighbourcells = Vector {Vector{Int}} (undef, prod (ncells))
9595 ranges = map (Base. OneTo, ncells) # create ranges 1:n1, 1:n2, ...
9696 for mc in Iterators. product (ranges... )
97- c = cell_index (ncells, mc)
97+ c = cell_index (ncells, mc)
9898 neighbours = []
9999 for mc2 in Iterators. product (map (x -> x- 1 : x+ 1 , mc)... )
100100 # Calculate the scalar cell index of the neighbour cell (with PBC)
101101 c2 = cell_index (ncells, mc2)
102102 push! (neighbours, c2)
103103 end
104- neighbourcells[c] = neighbours
104+ # The unique is required if there are fewer than 3 cells in one direction
105+ # Otherwise, the same index can appear multiple times
106+ neighbourcells[c] = unique! (neighbours)
105107 end
106108 return neighbourcells
107109end
@@ -111,16 +113,16 @@ end
111113Cells are chosen so that their dimensions are at least `rcut`, and neighbour cells are precomputed.
112114"""
113115function CellList (box:: SVector{d,T} , rcut:: T , N:: Int ) where {d,T<: AbstractFloat }
114-
116+
115117 # Calculate cell dimensions ensuring they're >= rcut
116118 cell_box = @. box / floor (Int, box / rcut)
117-
119+
118120 # Calculate number of cells in each dimension
119121 ncells = ntuple (i -> max (1 , floor (Int, box[i] / cell_box[i])), d)
120-
122+
121123 # Initialize empty cells
122124 cells = [Int[] for _ in 1 : prod (ncells)]
123-
125+
124126 # Initialize cell indices for particles
125127 cs = zeros (Int, N)
126128 neighbour_cells = build_neighbour_cells (ncells)
@@ -279,4 +281,3 @@ function old_new_cell(system::Particles, i, neighbour_list::LinkedList)
279281 c2 = cell_index (neighbour_list, mc2)
280282 return c, c2
281283end
282-
0 commit comments