Skip to content

Commit

Permalink
Parse unknown blocks in win file (#15)
Browse files Browse the repository at this point in the history
Return Vector of String.
  • Loading branch information
qiaojunfeng authored Sep 7, 2023
1 parent 0b5d055 commit 445d6a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/w90/win.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function read_win(filename::AbstractString, ::Wannier90Text; fix_inputs::Bool=tr
line === nothing && break

# first handle special cases, e.g., blocks
if occursin(r"begin\s+unit_cell_cart", line)
if occursin(r"^begin\s+unit_cell_cart", line)
block_name = "unit_cell_cart"
unit_cell = zeros(Float64, 3, 3)
unit = read_line_until_nonempty(; block_name)
Expand All @@ -106,15 +106,15 @@ function read_win(filename::AbstractString, ::Wannier90Text; fix_inputs::Bool=tr
unit_cell[:, i] = parse_array(line)
line = read_line_until_nonempty(; block_name)
end
@assert occursin(r"end\s+unit_cell_cart", line) "error parsing $block_name: `end $block_name` not found"
@assert occursin(r"^end\s+unit_cell_cart", line) "error parsing $block_name: `end $block_name` not found"
if startswith(unit, "b")
# convert to angstrom
unit_cell .*= Bohr
end
unit_cell = Mat3{Float64}(unit_cell)
push!(params, :unit_cell_cart => unit_cell)
elseif occursin(r"begin\s+atoms_(frac|cart)", line)
iscart = occursin(r"cart", line)
elseif occursin(r"^begin\s+atoms_(frac|cart)", line)
iscart = occursin("cart", line)
block_name = "atoms_$(iscart ? "cart" : "frac")"
# do not lowercase due to atomic label
line = read_line_until_nonempty(; lower=false, block_name)
Expand All @@ -131,7 +131,7 @@ function read_win(filename::AbstractString, ::Wannier90Text; fix_inputs::Bool=tr

# I need to read all lines and get n_atoms
lines = Vector{String}()
while !occursin(Regex("end\\s+" * block_name), lowercase(line))
while !occursin(Regex("^end\\s+" * block_name), lowercase(line))
push!(lines, line)
line = read_line_until_nonempty(; lower=false, block_name)
end
Expand All @@ -153,21 +153,21 @@ function read_win(filename::AbstractString, ::Wannier90Text; fix_inputs::Bool=tr
else
push!(params, :atoms_frac => atoms_frac)
end
elseif occursin(r"begin\s+projections", line)
elseif occursin(r"^begin\s+projections", line)
block_name = "projections"
projections = Vector{String}()
line = read_line_until_nonempty(; lower=false, block_name)
while !occursin(r"end\s+projections", lowercase(line))
while !occursin(r"^end\s+projections", lowercase(line))
push!(projections, line)
line = read_line_until_nonempty(; lower=false, block_name)
end
push!(params, :projections => projections)
elseif occursin(r"begin\s+kpoints", line)
elseif occursin(r"^begin\s+kpoints", line)
block_name = "kpoints"
line = read_line_until_nonempty(; block_name)
# I need to read all lines and get n_kpts
lines = Vector{String}()
while !occursin(r"end\s+kpoints", line)
while !occursin(r"^end\s+kpoints", line)
push!(lines, line)
line = read_line_until_nonempty(; block_name)
end
Expand All @@ -179,13 +179,13 @@ function read_win(filename::AbstractString, ::Wannier90Text; fix_inputs::Bool=tr
kpoints[i] = Vec3(parse_array(lines[i])[1:3])
end
push!(params, :kpoints => kpoints)
elseif occursin(r"begin\skpoint_path", line)
elseif occursin(r"^begin\s+kpoint_path", line)
block_name = "kpoint_path"
kpoint_path = Vector{Vector{SymbolVec3{Float64}}}()

# allow uppercase
line = read_line_until_nonempty(; lower=false, block_name)
while !occursin(r"end\s+kpoint_path", lowercase(line))
while !occursin(r"^end\s+kpoint_path", lowercase(line))
l = split(line)
length(l) == 8 || error("Invalid kpoint_path line: $line")
# start kpoint
Expand All @@ -200,6 +200,17 @@ function read_win(filename::AbstractString, ::Wannier90Text; fix_inputs::Bool=tr
line = read_line_until_nonempty(; lower=false, block_name)
end
push!(params, :kpoint_path => kpoint_path)
elseif occursin(r"^begin\s+(.+)", line)
# treat all remaining unknown blocks as Vector of String
block_name = match(r"^begin\s+(.+)", line).captures[1]
block_content = Vector{String}()
# allow uppercase
line = read_line_until_nonempty(; lower=false, block_name)
while !occursin(Regex("^end\\s+" * block_name), lowercase(line))
push!(block_content, line)
line = read_line_until_nonempty(; lower=false, block_name)
end
push!(params, Symbol(block_name) => block_content)
else
# now treat remaining lines as key-value pairs
line = strip(replace(line, "=" => " ", ":" => " ", "," => " "))
Expand Down
8 changes: 8 additions & 0 deletions test/w90/win.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ end
end
end
end

@testitem "read win: unknown blocks" begin
windir = joinpath(@__DIR__, "win_testfiles")
win = read_win(joinpath(windir, "unknown_blocks.win"))

@test win.unknown_a == ["A1", "A2"]
@test win.unknown_b == ["B1 B2"]
end
29 changes: 29 additions & 0 deletions test/w90/win_testfiles/unknown_blocks.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mp_grid = 1 1 2
num_wann = 8

begin atoms_cart
ang
Ga 0.0000000000 0.0000000000 0.0000000000
As 1.4200470448 1.4200470448 4.2601411346
end atoms_cart

begin unknown_A
A1
A2
end unknown_A

begin unknown_b
B1 B2
end unknown_b

begin kpoints
0.0000000000 0.0000000000 0.0000000000
0.0000000000 0.0000000000 0.1000000000
end kpoints

begin unit_cell_cart
ang
0.0000000000 2.8400940897 2.8400940897
2.8400940897 0.0000000000 2.8400940897
2.8400940897 2.8400940897 0.0000000000
end unit_cell_cart

0 comments on commit 445d6a0

Please sign in to comment.