Skip to content

Commit

Permalink
Add lattice & bonds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryavorskiy committed Mar 11, 2024
1 parent df9747d commit f47df53
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/core/bonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ end
adjacentsites(bonds::AbstractTranslation, site::AbstractSite) =
(destination(bonds, site), destination(inv(bonds), site))

Base.:(+)(site::AbstractSite, bonds::AbstractTranslation) = destination(bonds, site)
Base.:(+)(::AbstractSite, ::AbstractTranslation{UndefinedLattice}) =
Base.:(+)(site::AbstractSite, bonds::DirectedBonds) = destination(bonds, site)
Base.:(+)(::AbstractSite, ::DirectedBonds{UndefinedLattice}) =
throw(ArgumentError("Using a `AbstractBonds`-type object on undefined lattice is allowed only in `construct_operator`. Please define the lattice."))
Base.:(-)(bonds::AbstractTranslation) = inv(bonds)
Base.:(-)(site::AbstractSite, bonds::AbstractTranslation) = destination(inv(bonds), site)
Base.:(-)(bonds::DirectedBonds) = inv(bonds)
Base.:(-)(site::AbstractSite, bonds::DirectedBonds) = destination(inv(bonds), site)

"""
Translation <: AbstractTranslation
Expand Down Expand Up @@ -365,12 +365,12 @@ function Base.show(io::IO, ::MIME"text/plain", sh::Translation)
end
end

function translate_lattice(l::AbstractLattice, tr::AbstractTranslation)
function translate_lattice(l::AbstractLattice, tr::DirectedBonds)
e = Base.emptymutable(l, eltype(l))
ntr = adapt_bonds(tr, l)
for site in l
push!(e, destination(ntr, site))
end
return e
end
Base.:(+)(l::AbstractLattice, tr::AbstractTranslation) = translate_lattice(l, tr)
Base.:(+)(l::AbstractLattice, tr::DirectedBonds) = translate_lattice(l, tr)
6 changes: 6 additions & 0 deletions src/core/lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ Base.checkbounds(l::AbstractLattice, is) =
!checkbounds(Bool, l, is) && throw(BoundsError(l, is))
Base.push!(l::AbstractLattice{SiteT}, ::SiteT) where SiteT = error("Define `push!` for $l")
Base.push!(l::AbstractLattice{SiteT}, x::Any) where SiteT = push!(l, convert(SiteT, x))
function Base.push!(l::AbstractLattice, xs::Any...)
for x in xs
push!(l, x)
end
return l
end
Base.pop!(l::AbstractLattice) = deleteat!(l, lastindex(l))
Base.popfirst!(l::AbstractLattice) = deleteat!(l, firstindex(l))

Expand Down
5 changes: 4 additions & 1 deletion src/lattices/bravais/bonds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Base.inv(bsm::BravaisSiteMapping) = BravaisSiteMapping(bsm.lat, inv.(bsm.transla
merge_lats(l1::AbstractLattice, l2::AbstractLattice) = throw(IncompatibleLattices(l1, l2))
merge_lats(l::AbstractLattice, ::UndefinedLattice) = l
merge_lats(::UndefinedLattice, l::AbstractLattice) = l
merge_lats(::UndefinedLattice, ::UndefinedLattice) = UndefinedLattice()

function Base.union(tr1::BravaisTranslation, tr2::BravaisTranslation)
u_tr1 = adapt_bonds(tr1, UndefinedLattice())
Expand All @@ -139,6 +140,8 @@ end
Base.union(tr::BravaisTranslation, trs::BravaisSiteMapping) = union(trs, tr)
Base.union(trs::BravaisSiteMapping, trs2::BravaisSiteMapping) =
foldl(union, trs2.translations, init=trs)
Base.union(tr::BravaisTranslation, tr2, args...) = union(union(tr, tr2), args...)
Base.union(trs::BravaisSiteMapping, tr2, args...) = union(union(trs, tr2), args...)

function Base.summary(io::IO, trs::BravaisSiteMapping)
print(io, "BravaisSiteMapping with $(length(trs.translations)) translations")
Expand Down Expand Up @@ -193,4 +196,4 @@ function adapt_bonds(tr::Translation{UndefinedLattice}, l::MaybeWithParams{Brava
end
return BravaisSiteMapping(l, shifts...)
end
Translation(l::MaybeWithParams{BravaisLattice}, R::AbstractVector) = adapt_bonds(Translation(R), l)
Translation(l::MaybeWithParams{BravaisLattice}, R::AbstractVector{<:Number}) = adapt_bonds(Translation(R), l)
1 change: 1 addition & 0 deletions src/lattices/genericlattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ end
Constructs an empty `N`-dimensional `GenericLattice` of `GenericSite`s.
"""
GenericLattice{N}() where N = GenericLattice(GenericSite{N}[])
GenericLattice{SiteT}() where SiteT<:AbstractSite = GenericLattice(SiteT[])

"""
GenericLattice(lat)
Expand Down
42 changes: 41 additions & 1 deletion test/test_lattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,25 @@
LatticeModels.BravaisPointer(SA[3, 2], 1))
xb, yb = coordvalues(SquareLattice(5, 40))
@test_throws LatticeModels.IncompatibleLattices sql[xb.<yb]
s = sql[!, x = 1, y = 2]
sql2 = filter(sql) do site
site (sql[1], sql[end])
site (sql[1], sql[end], s)
end
pop!(sql)
popfirst!(sql)
delete!(sql, s)
@test sql == sql2

sql3 = sql[x = -Inf..5.2]
filter!(site -> site.coords[1] < 5.2, sql)
@test sql == sql3

small_l = SquareLattice(2, 2)
ls1, ls2, ls3, ls4 = small_l
gl1 = GenericLattice(small_l)
gl2 = GenericLattice{typeof(ls1)}()
push!(gl2, ls3, ls4, ls2, ls1)
@test gl1 == gl2
end

l = SquareLattice(10, 10)
Expand Down Expand Up @@ -72,6 +85,12 @@
@test all(mn .≤ xy.values .≤ mx)
imx = findall(==(mx), xy)
@test all((site in imx || xy[site] < mx) for site in l)

gl = GenericLattice{2}()
push!(gl, (1, 2), (2, 1), (3, 4))
gx = coordvalue(gl, :y)
@test gx.values == [2, 1, 4]
@test_throws ArgumentError coordvalue(gl, :j1)
end

@testset "Broadcast" begin
Expand Down Expand Up @@ -143,5 +162,26 @@ end
am2[ls2, ls4] = true
am2[ls3, ls4] = true
@test am.mat == am2.mat

ps = Tuple{Int, Int}[]
for (s1, s2) in am2
push!(ps, (s1.index, s2.index))
end
@test Set(ps) == Set([(1, 2), (1, 3), (2, 4), (3, 4)])
end

@testset "Abstract bonds" begin
l = HoneycombLattice(-2:2, -2:2)
tr = Translation(l, [0, 23/3])
site1 = l[!, j1 = 1, j2 = 1, index = 1]
site2 = site1 + tr
@test site2 == l[!, j1 = 0, j2 = 2, index = 2]
@test only(tr.translations) == BravaisTranslation(1 => 2, [-1, 1])

bsm = LatticeModels.BravaisSiteMapping(l,
BravaisTranslation(axis=1),
BravaisTranslation(axis=2),
BravaisTranslation([1, -1]))
@test bsm.translations == union(Bravais[1], Bravais[0, 1], Bravais[1, -1]).translations
end
end

0 comments on commit f47df53

Please sign in to comment.