Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lattice boundscheck happening before + or - #16

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/lattices/latticepoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ function Base.:-(i::LatticePoint{N,G}, j::LatticePoint{N,G}) where {N,G}
return LatticePoint(i.coordinates .- j.coordinates, i.lattice)
end

Base.:+(i::LatticePoint{N}, j::NTuple{N,Int}) where {N} = i + LatticePoint(j, i.lattice)
Base.:+(i::LatticePoint{1}, j::Int) = i + LatticePoint(j, i.lattice)
Base.:+(i::NTuple{N,Int}, j::LatticePoint{N}) where {N} = LatticePoint(i, j.lattice) + j
Base.:+(i::Int, j::LatticePoint{1}) = LatticePoint(i, j.lattice) + j

Base.:-(i::LatticePoint{N}, j::NTuple{N,Int}) where {N} = i - LatticePoint(j, i.lattice)
Base.:-(i::LatticePoint{1}, j::Int) = i - LatticePoint(j, i.lattice)
Base.:-(i::NTuple{N,Int}, j::LatticePoint{N}) where {N} = LatticePoint(i, j.lattice) - j
Base.:-(i::Int, j::LatticePoint{1}) = LatticePoint(i, j.lattice) - j
function Base.:+(i::LatticePoint{N}, j::NTuple{N,Int}) where {N}
return LatticePoint(i.coordinates .+ j, i.lattice)
end
Base.:+(i::LatticePoint{1}, j::Int) = LatticePoint(i.coordinates .+ j, i.lattice)
Base.:+(i::NTuple{N,Int}, j::LatticePoint{N}) where {N} = j + i
Base.:+(i::Int, j::LatticePoint{1}) = j + i

function Base.:-(i::LatticePoint{N}, j::NTuple{N,Int}) where {N}
return LatticePoint(i.coordinates .+ j, i.lattice)
end
Base.:-(i::LatticePoint{1}, j::Int) = LatticePoint(i.coordinates .+ j, i.lattice)
function Base.:-(i::NTuple{N,Int}, j::LatticePoint{N}) where {N}
return LatticePoint(i .- j.coordinates, j.lattice)
end
Base.:-(i::Int, j::LatticePoint{1}) = LatticePoint(i .- j, j.lattice)

Base.isless(i::L, j::L) where {L<:LatticePoint} = linearize_index(i) < linearize_index(j)

Expand Down
33 changes: 33 additions & 0 deletions test/lattices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ end
end
end

@testset "InfiniteStrip" begin
for L in 2:4
lattice = InfiniteStrip(L)
V = vertices(lattice)
@test length(lattice) == length(V) == L
@test lattice[1, 1] == first(V)

NN = nearest_neighbours(lattice)
@test length(NN) == 2L - 1 # coordination number 4 - 1 boundary
@test allunique(NN)

NNN = next_nearest_neighbours(lattice)
@test length(NNN) == 2L - 2 # coordination number 4 - 2 boundary
@test allunique(NNN)

for N in (2L, 3L)
lattice = InfiniteStrip(L, N)
V = vertices(lattice)
@test length(lattice) == length(V) == N

NN = nearest_neighbours(lattice)
@test length(NN) == 2N - (N ÷ L) # coordination number 4
@test allunique(NN)

NNN = next_nearest_neighbours(lattice)
@test length(NNN) == 2N - 2(N ÷ L) # coordination number 4
@test allunique(NNN)
end

@test_throws ArgumentError InfiniteStrip(L, 5)
end
end

@testset "InfiniteHoneycombYC" begin
for L in 4:4:12, N in 1:3
lattice = HoneycombYC(L, N * L)
Expand Down
Loading