Skip to content

Commit

Permalink
Geometry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsch committed May 7, 2024
1 parent 2c12a1d commit a606575
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Hamiltonians/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,21 @@ julia> reshape(Offsets(geometry), (3,4))
"""
struct Offsets{D} <: AbstractVector{SVector{D,Int}}
geometry::Geometry{D}
center::Bool
end
Offsets(geometry; center=false) = Offsets(geometry, center)

Base.size(off::Offsets) = (length(off.geometry),)

@inline function Base.getindex(off::Offsets{D}, i) where {D}
@boundscheck 0 < i length(off) || throw(BoundsError(off, i))
geo = off.geometry
vec = geo[i]
return vec - ones(SVector{D,Int})#+ SVector(ntuple(i -> -cld(size(geo, i), 2), Val(D)))
if !off.center
return vec - ones(SVector{D,Int})
else
return vec - SVector(ntuple(i -> cld(size(geo, i), 2), Val(D)))
end
end

"""
Expand Down
56 changes: 56 additions & 0 deletions test/Hamiltonians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Rimu
using Test
using DataFrames
using Suppressor
using StaticArrays

function exact_energy(ham)
dv = DVec(starting_address(ham) => 1.0)
Expand Down Expand Up @@ -149,6 +150,61 @@ using Rimu.Hamiltonians: momentum_transfer_excitation
end
end

using Rimu.Hamiltonians: UnitVectors, Offsets

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

for (dims, fold) in (
((4,), (false,)), ((2, 5), (true, false)), ((5, 6, 7), (true, true, false))
)
geom = Geometry(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 num_dimensions(geom) == length(dims)
end
end

@testset "getindex" begin
g = Geometry((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

@testset "UnitVectors" begin
@test UnitVectors(1) == [[1], [-1]]
@test UnitVectors(Geometry(2,3)) == [[1,0], [0,1], [-1,0], [0,-1]]
@test UnitVectors(3) == [[1,0,0], [0,1,0], [0,0,1], [-1,0,0], [0,-1,0], [0,0,-1]]
@test_throws BoundsError UnitVectors(3)[0]
@test_throws BoundsError UnitVectors(2)[5]
@test_throws BoundsError UnitVectors(1)[15]
end

@testset "Offsets" begin
@test collect(Offsets(Geometry(3), center=false)) == [[0], [1], [2]]
@test collect(Offsets(Geometry(3), center=true)) == [[-1], [0], [1]]

@test collect(Offsets(Geometry(2,2))) == [[0,0], [1,0], [0,1], [1,1]]
@test collect(Offsets(Geometry(2,3))) == [[0,0], [1,0], [0,1], [1,1], [0,2], [1,2]]
@test collect(Offsets(Geometry(2,3); center=true)) == [
[0,-1], [1,-1], [0,0], [1,0], [0,1], [1,1]
]
end
end

@testset "Interface tests" begin
for H in (
HubbardReal1D(BoseFS((1, 2, 3, 4)); u=1.0, t=2.0),
Expand Down

0 comments on commit a606575

Please sign in to comment.