Skip to content

Commit

Permalink
Added overload for Base.truncate() for moving k-points into [-0.5, …
Browse files Browse the repository at this point in the history
…0.5]
  • Loading branch information
brainandforce committed Jul 13, 2023
1 parent dc55dd9 commit ebbd825
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/data/kpoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
8 changes: 7 additions & 1 deletion test/kpoints.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ebbd825

Please sign in to comment.