Skip to content

Commit

Permalink
use pair for neigh list entry
Browse files Browse the repository at this point in the history
  • Loading branch information
Joroks committed Sep 4, 2024
1 parent 62f8ba1 commit a927265
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/LAMMPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -957,21 +957,19 @@ end

_check_valid_category(category::String) = category in ("compute", "dump", "fix", "group", "molecule", "region", "variable") || error("$category is not a valid category name!")

struct NeighListElement <: AbstractVector{Int32}
iatom::Int32
struct NeighListVec <: AbstractVector{Int32}
numneigh::Int
neighbors::Ptr{Int32}
end

function Base.getindex(nle::NeighListElement, i::Integer)
1 <= i <= nle.numneigh+1 || throw(BoundsError(nle, i))
# add one to account for 1-based indexing in julia.
return i == 1 ? nle.iatom+1 : unsafe_load(nle.neighbors, i-1)+1
function Base.getindex(nle::NeighListVec, i::Integer)
1 <= i <= nle.numneigh || throw(BoundsError(nle, i))
return unsafe_load(nle.neighbors, i)+1
end

Base.size(nle::NeighListElement) = (nle.numneigh+1,)
Base.size(nle::NeighListVec) = (nle.numneigh,)

struct NeighList <: AbstractVector{NeighListElement}
struct NeighList <: AbstractVector{Pair{Int32, NeighListVec}}
lmp::LMP
idx::Int
end
Expand All @@ -982,7 +980,7 @@ function Base.getindex(nl::NeighList, element::Integer)
neighbors = Ref{Ptr{Int32}}()
API.lammps_neighlist_element_neighbors(nl.lmp, nl.idx, element-1 #= 0-based indexing =#, iatom, numneigh, neighbors)
iatom[] == -1 && throw(BoundsError(nl, element))
return NeighListElement(iatom[], numneigh[], neighbors[])
return iatom[]+1 => NeighListVec(numneigh[], neighbors[])
end

Base.size(nl::NeighList) = (API.lammps_neighlist_num_elements(nl.lmp, nl.idx),)
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,9 @@ end

neighlist = find_pair_neighlist(lmp, "zero")
@test length(neighlist) == 27
@test length(neighlist[1]) == 4
iatom, neihgs = neighlist[1]
@test iatom == 1 # account for 1-based indexing
@test length(neihgs) == 3
@test_throws KeyError find_pair_neighlist(lmp, "nonesense")
end
end
Expand Down

0 comments on commit a927265

Please sign in to comment.