Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsch committed Jan 21, 2025
1 parent 0e65cd5 commit 20d8034
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 37 deletions.
41 changes: 27 additions & 14 deletions src/Hamiltonians/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ See also [`HubbardRealSpace`](@ref) and [`G2RealSpace`](@ref).
A 3×2 `CubicGrid` is indexed as follows.
```
1─4
2─5
3─6
1 ─ 4
2 ─ 5
3 ─ 6
```
"""
struct CubicGrid{D,Dims,Fold} <: Geometry{D}
Expand Down Expand Up @@ -310,20 +310,26 @@ A 4×4 `HoneycombLattice` is indexed as follows.
```
"""
struct HoneycombLattice{Dims,Fold} <: Geometry{2}
function HoneycombLattice(dims::Tuple{Int,Int}, fold=(true, true))
function HoneycombLattice(dims::Tuple{Int,Int}, fold::Tuple{Bool,Bool}=(true, true))
if fold[1] && isodd(dims[1])
throw(ArgumentError("if `fold[1] == true`, the lattice height must be even"))
end
if fold[2] && isodd(dims[2])
throw(ArgumentError("if `fold[2] == true`, the lattice width must be even"))
end
if any(<(1), dims)
throw(ArgumentError("lattice dimensions must be positive!"))
if any(<(2), dims)
throw(ArgumentError("All dimensions must be at least 2 in size"))
end
return new{dims,fold}()
end
end
HoneycombLattice(h, w, fold=(true, true)) = HoneycombLattice((h, w), fold)
HoneycombLattice(h, w, fold::Tuple{Bool,Bool}=(true, true)) = HoneycombLattice((h, w), fold)

function Base.show(io::IO, geom::HoneycombLattice)
h, w = size(geom)
fold = periodic_dimensions(geom)
print(io, "HoneycombLattice($h, $w, $fold)")
end

Base.size(::HoneycombLattice{Dims}) where {Dims} = Dims
periodic_dimensions(::HoneycombLattice{<:Any,Fold}) where {Fold} = Fold
Expand Down Expand Up @@ -359,13 +365,20 @@ A 3×2 `HexagonalLattice` is indexed as follows.
"""
struct HexagonalLattice{Dims,Fold} <: Geometry{2}
function HexagonalLattice(dims::Tuple{Int,Int}, fold::Tuple{Bool,Bool}=(true,true))
if any(<(1), dims)
throw(ArgumentError("lattice dimensions must be positive!"))
if any(<(2), dims)
throw(ArgumentError("All dimensions must be at least 2 in size"))
end
return new{dims,fold}()
end
end
HexagonalLattice(h, w, fold=(true, true)) = HexagonalLattice((h, w), fold)
HexagonalLattice(h, w, fold::Tuple{Bool,Bool}=(true, true)) = HexagonalLattice((h, w), fold)

function Base.show(io::IO, geom::HexagonalLattice)
h, w = size(geom)
fold = periodic_dimensions(geom)
print(io, "HexagonalLattice($h, $w, $fold)")
end


Base.size(::HexagonalLattice{Dims}) where {Dims} = Dims
periodic_dimensions(::HexagonalLattice{<:Any,Fold}) where {Fold} = Fold
Expand Down
99 changes: 76 additions & 23 deletions test/Hamiltonians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ using DataFrames
using Suppressor
using StaticArrays
using Rimu.Hamiltonians: TransformUndoer, AbstractOffdiagonals
using Rimu.Hamiltonians: num_neighbors, num_dimensions, Geometry

using Rimu.InterfaceTests: test_observable_interface, test_operator_interface,
test_hamiltonian_interface, test_hamiltonian_structure

Expand Down Expand Up @@ -162,35 +164,86 @@ end

using Rimu.Hamiltonians: Directions, Displacements

@testset "CubicGrid" begin
@testset "construtors and basic properties" begin
@test PeriodicBoundaries(3, 3) == CubicGrid(3, 3)
@test HardwallBoundaries(3, 4, 5) == CubicGrid((3, 4, 5), (false, false, false))
@test LadderBoundaries(2, 3, 4) == CubicGrid((2, 3, 4), (false, true, true))
@testset "Geometry" begin
@testset "CubicGrid" begin
@testset "construtors and basic properties" begin
@test_throws ArgumentError CubicGrid(1, 2)
@test_throws ArgumentError CubicGrid(-2, 5)

for (dims, fold) in (
((4,), (false,)), ((2, 5), (true, false)), ((5, 6, 7), (true, true, false))
)
geom = CubicGrid(dims, fold)
@test size(geom) == dims
@test length(geom) == prod(dims)
@test Rimu.Hamiltonians.fold(geom) == fold
@test eval(Meta.parse(repr(geom))) == geom
@test PeriodicBoundaries(3, 3) == CubicGrid(3, 3)
@test HardwallBoundaries(3, 4, 5) == CubicGrid((3, 4, 5), (false, false, false))
@test LadderBoundaries(2, 3, 4) == CubicGrid((2, 3, 4), (false, true, true))

for (dims, fold) in (
((4,), (false,)), ((2, 5), (true, false)), ((5, 6, 7), (true, true, false))
)
geom = CubicGrid(dims, fold)
@test geom isa Geometry{length(dims)}
@test size(geom) == dims
@test size(geom, 1) == dims[1]
@test length(geom) == prod(dims)
@test Rimu.Hamiltonians.periodic_dimensions(geom) == fold
@test eval(Meta.parse(repr(geom))) == geom
@test num_neighbors(geom) == 2 * num_dimensions(geom)
end
end

@testset "getindex" begin
g = CubicGrid((2,3,4), (false,true,false))
for i in 1:length(g)
v = SVector(Tuple(CartesianIndices((2,3,4))[i])...)
@test g[i] == v
@test g[v] == i
end

@test g[SVector(3, 1, 1)] == 0
@test g[SVector(1,4,1)] == 1
@test g[SVector(2,0,4)] == 24
@test g[SVector(2,3,0)] == 0
end
end

@testset "getindex" begin
g = CubicGrid((2,3,4), (false,true,false))
for i in 1:length(g)
v = SVector(Tuple(CartesianIndices((2,3,4))[i])...)
@test g[i] == v
@test g[v] == i
@testset "HoneycombLattice" begin
@testset "constructor errors" begin
@test_throws ArgumentError HoneycombLattice(3, 2)
@test_throws ArgumentError HoneycombLattice(1, 2)
@test_throws ArgumentError HoneycombLattice(2, 0)
@test_throws MethodError HoneycombLattice(2, 2, (false, true, true))
end

@test g[SVector(3, 1, 1)] == 0
@test g[SVector(1,4,1)] == 1
@test g[SVector(2,0,4)] == 24
@test g[SVector(2,3,0)] == 0
@testset "basic properties" begin
for (dims, fold) in (
((4, 4), (true, true)), ((3, 5), (false, false)), ((3, 2), (false, true))
)
geom = HoneycombLattice(dims, fold)
@test geom isa Geometry{2}
@test HoneycombLattice(dims..., fold) == geom
@test size(geom) == dims
@test size(geom, 2) == dims[2]
@test length(geom) == prod(dims)
@test Rimu.Hamiltonians.periodic_dimensions(geom) == fold
@test eval(Meta.parse(repr(geom))) == geom
@test num_neighbors(geom) == 3
end
end
end

@testset "HexagonalLattice" begin
@testset "constructor errors" begin
@test_throws ArgumentError HexagonalLattice(1, 2)
@test_throws ArgumentError HexagonalLattice(2, 0)
@test_throws MethodError HexagonalLattice(2, 2, (false, true, true))
end

@testset "basic properties" begin
geom = HexagonalLattice((5, 4), (true, false))
@test geom isa Hamiltonians.Geometry{2}
@test HexagonalLattice(5, 4, (true, false)) == geom
@test size(geom) == (5, 4)
@test length(geom) == 20
@test Rimu.Hamiltonians.periodic_dimensions(geom) == (true, false)
@test eval(Meta.parse(repr(geom))) == geom
end
end

@testset "Directions" begin
Expand Down

0 comments on commit 20d8034

Please sign in to comment.