diff --git a/src/data/kpoints.jl b/src/data/kpoints.jl index 52039830..44d5fb63 100644 --- a/src/data/kpoints.jl +++ b/src/data/kpoints.jl @@ -39,8 +39,15 @@ Returns the weight associated with a k-point. """ weight(k::KPoint) = k.weight -#---Generated lists of k-points--------------------------------------------------------------------# +# TODO: can we implement a remainder that excludes -0.5? +""" + truncate(k::KPoint) -> KPoint +Moves a k-point so that its values lie within the range [-1/2, 1/2]. The weight is preserved. +""" +Base.truncate(k::KPoint) = KPoint(rem.(k.point, 1, RoundNearest), k.weight) + +#---Generated lists of k-points--------------------------------------------------------------------# """ KPointMesh{D} <: AbstractVector{KPoint{D}} diff --git a/test/kpoints.jl b/test/kpoints.jl index 3ddc853b..ebb23aae 100644 --- a/test/kpoints.jl +++ b/test/kpoints.jl @@ -1,7 +1,13 @@ @testset "k-points" begin @test KPoint(0, 0, 0, weight = 1) != KPoint(0, 0, 0, weight = 2) @test hash(KPoint(0, 0, 0, weight = 1)) != hash(KPoint(0, 0, 0, weight = 2)) - @test KPoint(1, 2, 3) == KPoint(0, 0, 0) + # Truncation + # TODO: see note for trunc() in Electrum.jl/src/kpoints.jl + @test truncate(KPoint(1, 2, 3)) == KPoint(0, 0, 0) + @test truncate(KPoint(0.5, -0.5, 1.5)) == KPoint(0.5, -0.5, -0.5) + @test truncate(KPoint(0.5, -0.5 + eps(Float64), 1.5)) == KPoint(0.5, -0.5 + eps(Float64), -0.5) + @test truncate(KPoint(0.5, -0.5 - eps(Float64), 1.5)) == KPoint(0.5, 0.5 - eps(Float64), -0.5) + # Length measurement @test length(KPoint(1, 2, 3, 4, 5)) == 5 @test length(KPoint{2}) == 2 end