Skip to content

Commit

Permalink
Handle Fortran output stars in wout file (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiaojunfeng authored Oct 4, 2023
1 parent 0246a90 commit 5ca2047
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ EzXML = "1.1.0"
FortranFiles = "0.6.0"
StaticArrays = "1"
TOML = "1"
julia = "1"
julia = "1.8"
8 changes: 7 additions & 1 deletion src/util/fortran.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ Parse a string as `Float64`.
The is capable of parsing Fortran outputs, e.g. `1.0D-10`, to the ordinary `1e-10`.
"""
parse_float(s::AbstractString) = parse(Float64, replace(lowercase(strip(s)), "d" => "e"))
function parse_float(s::AbstractString)
if occursin("*", s)
return NaN
else
return parse(Float64, replace(lowercase(strip(s)), "d" => "e"))
end
end

"""
$(SIGNATURES)
Expand Down
54 changes: 23 additions & 31 deletions src/w90/wout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function read_wout(filename::AbstractString)
end_atom = "*----------------------------------------------------------------------------*"
start_finalstate = "Final State"
end_finalstate = "Sum of centres and spreads"
marker_wfc = "WF centre and spread"
marker_ΩI = "Omega I ="
marker_ΩD = "Omega D ="
marker_ΩOD = "Omega OD ="
Expand All @@ -42,27 +43,22 @@ function read_wout(filename::AbstractString)
while !eof(io)
line = strip(readline(io))

if occursin("| Length Unit", line)
@assert occursin("Ang", line)
ang_unit = true
continue
end

if occursin(start_lattice, line)
@assert occursin("Ang", line)
ang_unit = true
lattice = zeros(Float64, 3, 3)

line = split(strip(readline(io)))
@assert line[1] == "a_1"
lattice[:, 1] = parse.(Float64, line[2:end])
lattice[:, 1] = parse_float.(line[2:end])

line = split(strip(readline(io)))
@assert line[1] == "a_2"
lattice[:, 2] = parse.(Float64, line[2:end])
lattice[:, 2] = parse_float.(line[2:end])

line = split(strip(readline(io)))
@assert line[1] == "a_3"
lattice[:, 3] = parse.(Float64, line[2:end])
lattice[:, 3] = parse_float.(line[2:end])

continue
end
Expand All @@ -73,15 +69,15 @@ function read_wout(filename::AbstractString)

line = split(strip(readline(io)))
@assert line[1] == "b_1"
recip_lattice[:, 1] = parse.(Float64, line[2:end])
recip_lattice[:, 1] = parse_float.(line[2:end])

line = split(strip(readline(io)))
@assert line[1] == "b_2"
recip_lattice[:, 2] = parse.(Float64, line[2:end])
recip_lattice[:, 2] = parse_float.(line[2:end])

line = split(strip(readline(io)))
@assert line[1] == "b_3"
recip_lattice[:, 3] = parse.(Float64, line[2:end])
recip_lattice[:, 3] = parse_float.(line[2:end])

continue
end
Expand All @@ -105,9 +101,9 @@ function read_wout(filename::AbstractString)
@assert line[1] == "|" line
push!(atom_labels, line[2])
# cartesian
# atom_positions[:, i] = parse.(Float64, line[8:10])
# atom_positions[i] = Vec3(parse_float.(line[8:10]))
# fractional
atom_positions[i] = Vec3(parse.(Float64, line[4:6])...)
atom_positions[i] = Vec3(parse_float.(line[4:6]))
end

continue
Expand All @@ -125,44 +121,40 @@ function read_wout(filename::AbstractString)
centers = zeros(Vec3{Float64}, n_wann)
spreads = zeros(Float64, n_wann)
for (i, line) in enumerate(lines)
line = split(line)
@assert join(line[1:4], " ") == "WF centre and spread"
@assert i == parse(Int, line[5])
@assert startswith(line, marker_wfc) line
line = split(line, r"[,()]")

x = parse(Float64, replace(line[7], "," => ""))
y = parse(Float64, replace(line[8], "," => ""))
z = parse(Float64, replace(line[9], "," => ""))
s = parse(Float64, line[11])
idx = strip(chopprefix(line[1], marker_wfc))
idx = parse(Int, idx)
@assert i == idx "Wrong WF index at $(line[1])"

centers[i] = Vec3(x, y, z)
spreads[i] = s
vals = map(parse_float, line[2:end])
centers[i] = Vec3(vals[1:3])
spreads[i] = vals[4]
end

continue
end

if occursin("Spreads (Ang^2) Omega I", line)
end

if occursin(marker_ΩI, line)
ΩI = parse(Float64, split(line, marker_ΩI)[2])
ΩI = parse_float(split(line, marker_ΩI)[2])
continue
end
if occursin(marker_ΩD, line)
ΩD = parse(Float64, split(line, marker_ΩD)[2])
ΩD = parse_float(split(line, marker_ΩD)[2])
continue
end
if occursin(marker_ΩOD, line)
ΩOD = parse(Float64, split(line, marker_ΩOD)[2])
ΩOD = parse_float(split(line, marker_ΩOD)[2])
continue
end
if occursin(marker_Ωtotal, line)
Ωtotal = parse(Float64, split(line, marker_Ωtotal)[2])
Ωtotal = parse_float(split(line, marker_Ωtotal)[2])
continue
end
end

@assert ang_unit
@assert ang_unit "wout unit is not Angstrom, not supported yet"
lattice = Mat3(lattice)
recip_lattice = Mat3(recip_lattice)
return (;
Expand Down
15 changes: 15 additions & 0 deletions test/w90/wout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ end
@test wout.ΩOD ref_ΩOD
@test wout.Ωtotal ref_Ωtotal
end

@testitem "read wout fortran stars" begin
woutdir = joinpath(@__DIR__, "wout_testfiles")
wout = read_wout(joinpath(woutdir, "stars.wout"))

ref_centers = [
[-0.866253, 1.973841, 1.973841],
[-0.866253, 0.866253, 0.866253],
[-99.973841, 1.973841, 0.866253],
[NaN, 0.866253, 1.973841],
]
@test wout.centers[1:3] ref_centers[1:3]
@test isnan(wout.centers[end][1])
@test wout.centers[end][2:end] ref_centers[end][2:end]
end
96 changes: 96 additions & 0 deletions test/w90/wout_testfiles/stars.wout
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
Running in serial (with parallel executable)

------
SYSTEM
------

Lattice Vectors (Ang)
a_1 -2.840094 0.000000 2.840094
a_2 0.000000 2.840094 2.840094
a_3 -2.840094 2.840094 0.000000

Unit Cell Volume: 45.81716 (Ang^3)

Reciprocal-Space Vectors (Ang^-1)
b_1 -1.106158 -1.106158 1.106158
b_2 1.106158 1.106158 1.106158
b_3 -1.106158 1.106158 -1.106158

*----------------------------------------------------------------------------*
| Site Fractional Coordinate Cartesian Coordinate (Ang) |
+----------------------------------------------------------------------------+
| Ga 1 0.00000 0.00000 0.00000 | 0.00000 0.00000 0.00000 |
| As 1 0.25000 0.25000 0.25000 | -1.42005 1.42005 1.42005 |
*----------------------------------------------------------------------------*
------------
K-POINT GRID
------------

Grid size = 2 x 2 x 2 Total points = 8

------------------------------------------------------------------------------
Initial State
WF centre and spread 1 ( -0.866632, 1.973462, 1.973462 ) 1.11720303
WF centre and spread 2 ( -0.866632, 0.866632, 0.866632 ) 1.11720303
WF centre and spread 3 ( -1.973462, 1.973462, 0.866632 ) 1.11720303
WF centre and spread 4 ( -1.973462, 0.866632, 1.973462 ) 1.11720303
Sum of centres and spreads ( -5.680188, 5.680188, 5.680188 ) 4.46881212

0 0.447E+01 0.0000000000 4.4688121156 0.00 <-- CONV
O_D= 0.0083198 O_OD= 0.5036294 O_TOT= 4.4688121 <-- SPRD
------------------------------------------------------------------------------
Cycle: 1
WF centre and spread 1 ( -0.866253, 1.973841, 1.973841 ) 1.11672024
WF centre and spread 2 ( -0.866253, 0.866253, 0.866253 ) 1.11672024
WF centre and spread 3 ( -1.973841, 1.973841, 0.866253 ) 1.11672024
WF centre and spread 4 ( -1.973841, 0.866253, 1.973841 ) 1.11672024
Sum of centres and spreads ( -5.680188, 5.680188, 5.680188 ) 4.46688098

1 -0.193E-02 0.0667901301 4.4668809772 0.00 <-- CONV
O_D= 0.0080299 O_OD= 0.5019882 O_TOT= 4.4668810 <-- SPRD
Delta: O_D= -0.2899248E-03 O_OD= -0.1641214E-02 O_TOT= -0.1931138E-02 <-- DLTA
------------------------------------------------------------------------------
Cycle: 2
WF centre and spread 1 ( -0.866253, 1.973841, 1.973841 ) 1.11672024
WF centre and spread 2 ( -0.866253, 0.866253, 0.866253 ) 1.11672024
WF centre and spread 3 ( -1.973841, 1.973841, 0.866253 ) 1.11672024
WF centre and spread 4 ( -1.973841, 0.866253, 1.973841 ) 1.11672024
Sum of centres and spreads ( -5.680188, 5.680188, 5.680188 ) 4.46688098

2 -0.893E-09 0.0000454494 4.4668809763 0.00 <-- CONV
O_D= 0.0080300 O_OD= 0.5019880 O_TOT= 4.4668810 <-- SPRD
Delta: O_D= 0.1844013E-06 O_OD= -0.1852948E-06 O_TOT= -0.8934924E-09 <-- DLTA
------------------------------------------------------------------------------

Cycle: 6
WF centre and spread 1 ( -0.866253, 1.973841, 1.973841 ) 1.11672024
WF centre and spread 2 ( -0.866253, 0.866253, 0.866253 ) 1.11672024
WF centre and spread 3 (**********, 1.973841,***********) 1.11672024
WF centre and spread 4 (9999999999, 0.866253, 999999999) 1.11672024
Sum of centres and spreads (**********,**********,********** ) 5593.44793159

6 0.888E-15 0.0000000001 4.4668809763 0.00 <-- CONV
O_D= 0.0080300 O_OD= 0.5019880 O_TOT= 4.4668810 <-- SPRD
Delta: O_D= -0.8310713E-13 O_OD= 0.8382184E-13 O_TOT= 0.8881784E-15 <-- DLTA
------------------------------------------------------------------------------

Final State
WF centre and spread 1 ( -0.866253, 1.973841, 1.973841 ) 1.11672024
WF centre and spread 2 ( -0.866253, 0.866253, 0.866253 ) 1.11672024
WF centre and spread 3 (-99.973841, 1.973841, 0.866253 ) 1.11672024
WF centre and spread 4 (**********, 0.866253, 1.973841 ) 1.11672024
Sum of centres and spreads (**********,**********,********** ) 2694.74831205

Spreads (Ang^2) Omega I = 3.956862958
================ Omega D = 0.008030049
Omega OD = 0.501987969
Final Spread (Ang^2) Omega Total = 4.466880976
------------------------------------------------------------------------------
Time for wannierise 0.003 (sec)

Writing checkpoint file gaas.chk... done

Time for plotting 0.000 (sec)
Total Execution Time 0.019 (sec)

All done: wannier90 exiting

0 comments on commit 5ca2047

Please sign in to comment.