Skip to content

Commit

Permalink
Add write_w90_hrdat (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaojunfeng authored Aug 31, 2023
1 parent cfb7359 commit 0b5d055
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/w90/hr.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export read_w90_hrdat
export read_w90_hrdat, write_w90_hrdat

"""
$(SIGNATURES)
Expand Down Expand Up @@ -36,3 +36,59 @@ function read_w90_hrdat(filename::AbstractString)
return (; Rvectors, Rdegens, H, header)
end
end

"""
$(SIGNATURES)
Write `prefix_hr.dat`.
# Keyword arguments
See the return values of [`read_w90_hrdat`](@ref).
"""
function write_w90_hrdat(
filename::AbstractString;
Rvectors::AbstractVector,
Rdegens::AbstractVector,
H::AbstractVector,
header=default_header(),
)
n_Rvecs = length(H)
@assert n_Rvecs > 0 "empty H"
n_wann = size(H[1], 1)
@info "Writing hr.dat file" filename header n_wann n_Rvecs

return open(filename, "w") do io
println(io, strip(header))
@printf(io, "%d\n", n_wann)
@printf(io, "%d\n", n_Rvecs)

n_columns = 15
for (iR, degen) in enumerate(Rdegens)
@printf(io, "%5d", degen)
if mod(iR, n_columns) == 0
println(io)
end
end
if mod(n_Rvecs, n_columns) != 0
println(io)
end

for iR in 1:n_Rvecs
for n in 1:n_wann
for m in 1:n_wann
reH = real(H[iR][m, n])
imH = imag(H[iR][m, n])
@printf(
io,
" %4d %4d %4d %4d %4d %11.6f %11.6f\n",
Rvectors[iR]...,
m,
n,
reH,
imH
)
end
end
end
end
end
15 changes: 15 additions & 0 deletions test/w90/hr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ end
@test hrdat_ws[p] hrdat[p]
end
end

@testitem "write hr" begin
using LazyArtifacts
hrdat = read_w90_hrdat(artifact"Si2_valence/reference/WS/Si2_valence_hr.dat")

tmpfile = tempname(; cleanup=true)
write_w90_hrdat(tmpfile; hrdat...)
hrdat2 = read_w90_hrdat(tmpfile)

@test keys(hrdat) == keys(hrdat2)
for (k, v) in pairs(hrdat)
k == :header && continue
@test hrdat2[k] == v
end
end

0 comments on commit 0b5d055

Please sign in to comment.