Skip to content

Commit 8d1f9d6

Browse files
committed
cleanup and fixes
1 parent a0f8fa1 commit 8d1f9d6

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "DFControl"
22
uuid = "1e31e15d-4957-550d-a244-318eced754ae"
33
authors = ["Louis Ponet <[email protected]>"]
44
repo = "https://github.com/louisponet/DFControl.jl.git"
5-
version = "0.5.29"
5+
version = "0.5.30"
66

77
[deps]
88
ANSIColoredPrinters = "a4c015fc-c6ff-483c-b24f-f7ea428134e9"

src/Client/job.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function RemoteHPC.save(job::Job, workflow = nothing; versioncheck=true, kwargs.
206206
end
207207

208208
linkpath = joinpath(job, "$el.UPF")
209-
if p.path != linkpath || !ispath(server, linkpath)
209+
if !ispath(server, linkpath) || p.path != realpath(server, linkpath)
210210
if ispath(server, linkpath)
211211
rm(server, linkpath)
212212
end
@@ -394,30 +394,30 @@ function outputdata(job::Job; calcs=map(x->x.name, job.calculations), extra_pars
394394
end
395395

396396
"""
397-
bandgap(job::Job, fermi=nothing)
397+
bandgap(job::Job, nelec=nothing)
398398
399399
Calculates the bandgap (possibly indirect) around the fermi level.
400400
Uses the first found bands calculation, if there is none it uses the first found nscf calculation.
401401
"""
402-
function bandgap(job::Job, fermi = nothing, outdat = outputdata(job))
402+
function bandgap(job::Job, nelec = nothing, outdat = outputdata(job))
403403
band_calcs = filter(!isnothing, getfirst.([Calculations.isbands, Calculations.isnscf, Calculations.isscf], (job.calculations,)))
404404
if isempty(band_calcs)
405405
error("No valid calculation found to calculate the bandgap.\nMake sure the job has either a valid bands or nscf calculation.")
406406
end
407407

408-
if fermi === nothing
409-
fermi_calcs = filter(x -> (Calculations.isvcrelax(x) ||
408+
if nelec === nothing
409+
n_calcs = filter(x -> (Calculations.isvcrelax(x) ||
410410
Calculations.isscf(x) ||
411411
Calculations.isnscf(x)), job.calculations)
412412

413-
if isempty(fermi_calcs)
414-
error("No valid calculation found to extract the fermi level.\nPlease supply the fermi level manually.")
413+
if isempty(n_calcs)
414+
error("No valid calculation found to extract the number of electrons.\nPlease supply nelec manually.")
415415
end
416-
fermi = maximum(x->get(get(outdat, x.name, Dict()), :fermi, -Inf), fermi_calcs)
416+
nelec = maximum(x->get(get(outdat, x.name, Dict()), :fermi, -Inf), n_calcs)
417417
end
418418

419419
bands = map(x->outdat[x.name][:bands], filter(x -> haskey(outdat, x.name), band_calcs))
420-
return minimum(bandgap.(bands, fermi))
420+
return minimum(bandgap.(bands, nelec))
421421
end
422422

423423
"""

src/FileIO/qe.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ end
147147
function qe_parse_n_KS(out, line, f)
148148
return out[:n_KS_states] = parse(Int, split(line)[5])
149149
end
150+
function qe_parse_n_electrons(out, line, f)
151+
return out[:n_electrons] = round(Int, parse(Float64, split(line)[5]))
152+
end
150153

151154
function qe_parse_crystal_axes(out, line, f)
152155
m = Mat3(reshape([parse.(Float64, split(readline(f))[4:6]);
@@ -481,6 +484,7 @@ end
481484
const QE_PW_PARSE_FUNCTIONS = ["C/m^2" => qe_parse_polarization,
482485
"lattice parameter" => qe_parse_lattice_parameter,
483486
"number of Kohn-Sham states" => qe_parse_n_KS,
487+
"number of electrons" => qe_parse_n_electrons,
484488
"crystal axes" => qe_parse_crystal_axes,
485489
"EXX-fraction" => (x, y, z) -> x[:hybrid] = true,
486490
"EXX self-consistency reached" => (x,y,z) -> x[:hybrid_converged] = true,

src/Structures/structure.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@ function update_geometry!(str1::Structure, str2::Structure)
8787
str1.cell = copy(str2.cell)
8888
ats2 = str2.atoms
8989
for at1 in str1.atoms
90-
tats2 = filter(y -> y.name == at1.name, ats2)
91-
id = findmin(map(x -> norm(x.position_cart - at1.position_cart), tats2))[2]
90+
id = findmin(map(x -> norm(x.position_cart - at1.position_cart), ats2))[2]
91+
9292
if id === nothing
9393
@error "No atom of the species $(at1.name) found in the second structure"
9494
end
95-
set_position!(at1, tats2[id].position_cryst, str1.cell)
95+
96+
set_position!(at1, ats2[id].position_cryst, str1.cell)
97+
at1.name = ats2[id].name
9698
end
9799
return str1
98100
end

src/types.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,38 @@ end
3030
eigvals(band::Band) = band.eigvals
3131
StructTypes.StructType(::Type{<:AbstractBand}) = StructTypes.Mutable()
3232

33+
"""
34+
bandgap(bands::AbstractVector{Band}, n_electrons::Int)
35+
bandgap(bands::AbstractVector{Band}, fermi::Float64)
36+
37+
Calculates the bandgap (possibly indirect) around the fermi level.
38+
"""
39+
function bandgap(bands::Union{Iterators.Flatten,AbstractVector{<:AbstractBand}},
40+
n_electrons::Int)
41+
bands = sort(bands, by = x->maximum(eigvals(x)))
42+
if length(bands) < n_electrons+1
43+
error("not enough bands for $n_electrons electrons ($(length(bands)) bands)." )
44+
end
45+
gap = minimum(eigvals(bands[n_electrons+1])) - maximum(eigvals(bands[n_electrons]))
46+
return gap < 0 ? 0.0 : gap
47+
end
48+
3349
"""
3450
bandgap(bands::AbstractVector{Band}, fermi=0.0)
3551
3652
Calculates the bandgap (possibly indirect) around the fermi level.
3753
"""
54+
function bandgap(bands::Union{Iterators.Flatten,AbstractVector{<:AbstractBand}},
55+
n_electrons::Int)
56+
maxbands = sort(bands, by = x->maximum(eigvals(x)))
57+
minbands = sort(bands, by = x->minimum(eigvals(x)))
58+
if length(bands) < n_electrons+1
59+
error("not enough bands for $n_electrons electrons ($(length(bands)) bands)." )
60+
end
61+
gap = minimum(eigvals(minbands[n_electrons+1])) - maximum(eigvals(maxbands[n_electrons]))
62+
return gap < 0 ? 0.0 : gap
63+
end
64+
3865
function bandgap(bands::Union{Iterators.Flatten,AbstractVector{<:AbstractBand}},
3966
fermi = 0.0)
4067
max_valence = -Inf
@@ -54,8 +81,9 @@ function bandgap(bands::Union{Iterators.Flatten,AbstractVector{<:AbstractBand}},
5481
end
5582
return min_conduction - max_valence
5683
end
84+
5785
function bandgap(u_d_bands::Union{NamedTuple,Tuple}, args...)
58-
return bandgap(Iterators.flatten(u_d_bands), args...)
86+
return bandgap([u_d_bands.up; u_d_bands.down], args...)
5987
end
6088

6189
mutable struct TimingData

0 commit comments

Comments
 (0)