Skip to content

Commit

Permalink
finished restructuring and basic documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Jun 20, 2021
1 parent 4eea6a7 commit 2c006c6
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 193 deletions.
8 changes: 7 additions & 1 deletion docs/src/guide/calculations.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ Remembering all the names of the flags, where they belong, and what types they a
is quite complicated and can lead to easily made mistakes like typos.
DFControl tries to catch these as it knows which flags are allowed for which calculations.
It will report when a flag can not be found for a given [`DFCalculation`](@ref Calculations),
and it will
and it will also try to convert a flag value to the expected type.

```@docs
Base.getindex(::DFCalculation, ::Symbol)
Base.setindex!(::DFCalculation, ::Symbol, ::Any)
set_flags!
rm_flags!
```

## Execs
```@docs
Expand Down
2 changes: 1 addition & 1 deletion src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export save, submit, abort, set_flow!, set_headerword!, isrunning, last_running_
cleanup

#Basic Interaction with DFCalculations inside DFJob
export searchcalculation, searchcalculations, set_cutoffs!
export set_cutoffs!

#Interacting with the Structure inside DFJob
export atom, atoms, set_atoms!, set_pseudo!, set_pseudos!, projections, set_projections!, orbital, cell, a, b, c,
Expand Down
56 changes: 16 additions & 40 deletions src/atom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,39 +142,6 @@ function isequal_species(at1::AbstractAtom, at2::AbstractAtom)
return true
end

#TODO fix documentation
for hub_param in (:U, :J0, , )
f = Symbol("set_Hubbard_$(hub_param)!")
str = "$(hub_param)"
@eval begin
"""
$($(f))(at::AbstractAtom, v::AbstractFloat; print=true)
Set the Hubbard $($(str)) parameter for the specified atom.
Example:
`$($(f))(at, 2.1)'
"""
function $f(at::AbstractAtom{T}, v::AbstractFloat; print=true) where {T}
dftu(at).$(hub_param) = convert(T, v)
print && @info "Hubbard $($(str)) of atom $(at.name) set to $v"
end
end
end

"""
set_Hubbard_J!(at::AbstractAtom, v::Vector{<:AbstractFloat}; print=true)
Set the Hubbard J parameter for the specified atom.
Example:
`set_Hubbard_J(at, [2.1])'
"""
function set_Hubbard_J!(at::AbstractAtom{T}, v::Vector{<:AbstractFloat}; print=true) where {T}
dftu(at).J = convert.(T, v)
print && @info "Hubbard J of atom $(at.name) set to $v"
end

function position_string(::Type{QE}, at::AbstractAtom; relative=true)
pos = relative ? round.(position_cryst(at), digits=5) : round.(ustrip.(uconvert.(Ang, position_cart(at))), digits=5)
return "$(name(at)) $(pos[1]) $(pos[2]) $(pos[3])\n"
Expand Down Expand Up @@ -346,13 +313,20 @@ for hub_param in (:U, :J0, :α, :β)
str = "$hub_param"
@eval begin
"""
$($(f))(at::AbstractAtom, v::AbstractFloat; print=true)
$($(f))(job::DFJob, ats_$($(str))s::Pair{Symbol, <:AbstractFloat}...; print=true)
Set the Hubbard $($(str)) parameter for the specified atoms.
Set the Hubbard $($(str)) parameter for the specified [Atoms](@ref Atom).
The latter function allows for conveniently setting the parameter for all
[Atoms](@ref Atom) with the specified `name`.
Example:
`$($(f))(job, :Ir => 2.1, :Ni => 1.0, :O => 0.0)`
"""
function $f(at::AbstractAtom{T}, v::AbstractFloat; print=true) where {T}
dftu(at).$(hub_param) = convert(T, v)
print && @info "Hubbard $($(str)) of atom $(at.name) set to $v"
end
function $f(job::DFJob, $(hub_param)::Pair{Symbol, <:AbstractFloat}...; print=true)
for (atsym, val) in $(hub_param)
$f.(atoms(job, atsym), val; print=print)
Expand All @@ -363,18 +337,20 @@ for hub_param in (:U, :J0, :α, :β)
end

"""
set_Hubbard_J!(at::AbstractAtom, v::Vector{<:AbstractFloat}; print=true)
set_Hubbard_J!(job::DFJob, ats_Js::Pair{Symbol, Vector{<:AbstractFloat}}...; print=true)
Set the Hubbard J parameter for the specified atom.
The latter function allows for conveniently setting the `Hubbard_J` for all
[Atoms](@ref Atom) with the specified `name`.
Example:
`set_Hubbard_J(at, [2.1])'
`set_Hubbard_J(job, :Ir => [2.1], :Ni => [1.0])'
"""
function set_Hubbard_J!(job::DFJob, ats_Js::Pair{Symbol, <:Vector{<:AbstractFloat}}...; print=true)
for (atsym, val) in ats_Js
set_Hubbard_J!.(atoms(job, atsym), (val,); print=print)
end
function set_Hubbard_J!(at::AbstractAtom{T}, v::Vector{<:AbstractFloat}; print=true) where {T}
dftu(at).J = convert.(T, v)
print && @info "Hubbard J of atom $(at.name) set to $v"
end

export set_Hubbard_J!

81 changes: 75 additions & 6 deletions src/calculationAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,64 @@ function set_name!(calculation::DFCalculation, name::AbstractString; print=true)
name
end

"Returns the flag with given symbol."
"""
getindex(calculation::DFCalculation, n::Symbol)
Returns the flag with given symbol.
getindex(job::DFJob, flag::Symbol)
Searches through the job's calculations for the requested flag.
A `Dict` will be returned with calculation names as the keys and the flags as values.
"""
function Base.getindex(calculation::DFCalculation, n::Symbol)
if haskey(calculation.flags, n)
return calculation.flags[n]
else
error("Flag:$n Not found in flags of Input $(name(calculation))")
end
end
"Sets the flag"

function Base.getindex(job::DFJob, flg::Symbol)
outdict = Dict()
for i in calculations(job)
tfl = flag(i, flg)
if tfl != nothing
outdict[name(i)] = tfl
end
end
return outdict
end

"""
setindex!(calculation::DFCalculation, value, flag::Symbol)
Sets flags.
setindex!(job::DFJob, value, flag::Symbol)
Set `flag` in all the appropriate calculations to the `value`.
"""
Base.setindex!(calculation::DFCalculation, dat, key) = set_flags!(calculation, key => dat)

function Base.setindex!(job::DFJob, value, key::Symbol)
for calculation in calculations(job)
calculation[key] = value
end
end

"""
set_flags!(calculation::DFCalculation, flags...; print=true)
set_flags!(calculation::DFCalculation, flags::Pair{Symbol, Any}...; print=true)
Sets multiple flags in one go. Flag validity and type are verified.
Sets the specified flags in the calculation.
set_flags!(job::DFJob, calculations::Vector{<:DFCalculation}, flags::Pair{Symbol,<:Any}...; print=true)
set_flags!(job::DFJob, flags::Pair{Symbol,<:Any}...; print=true)
Sets the flags in the names to the flags specified.
This only happens if the specified flags are valid for the names.
The values that are supplied will be checked whether they are valid.
"""
function set_flags!(calculation::DFCalculation{T}, flags...; print=true) where T
found_keys = Symbol[]
Expand Down Expand Up @@ -56,12 +99,32 @@ function set_flags!(calculation::DFCalculation{T}, flags...; print=true) where T
return found_keys, calculation
end

function set_flags!(job::DFJob, calculations::Vector{<:DFCalculation}, flags...; print=true)
found_keys = Symbol[]

for calc in calculations
t_, = set_flags!(calc, flags..., print=print)
push!(found_keys, t_...)
end
nfound = setdiff([k for (k, v) in flags], found_keys)
if print && length(nfound) > 0
f = length(nfound) == 1 ? "flag" : "flags"
dfprintln("$f '$(join(":" .* String.(setdiff(flagkeys, found_keys)),", "))' were not found in the allowed calculation variables of the specified calculations!")
end
return job
end
set_flags!(job::DFJob, flags...;kwargs...) =
set_flags!(job, calculations(job), flags...;kwargs...)
set_flags!(job::DFJob, name::String, flags...; fuzzy=true, kwargs...) =
set_flags!(job, calculations(job, name, fuzzy), flags...; kwargs...)

"""
rm_flags!(calculation::DFCalculation, flags...)
rm_flags!(calculation::DFCalculation, flags::Symbol...)
rm_flags!(job::DFJob, flags::Symbol...)
Remove the specified flags.
"""
function rm_flags!(calculation::DFCalculation, flags...; print=true)
function rm_flags!(calculation::DFCalculation, flags::Symbol...; print=true)
for flag in flags
if haskey(calculation.flags, flag)
pop!(calculation.flags, flag, false)
Expand All @@ -71,6 +134,12 @@ function rm_flags!(calculation::DFCalculation, flags...; print=true)
return calculation
end

function rm_flags!(job::DFJob, flags::Symbol...; kwargs...)
for c in calculations(job)
rm_flags!(c, flags...; kwargs...)
end
end

"""
data(calculation::DFCalculation)
data(calculation::DFCalculation, n::Symbol)
Expand Down
2 changes: 1 addition & 1 deletion src/job.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Returns an array of the calculations that match the names.
"""
calculations(job::DFJob, names::Vector, fuzzy=true) = fuzzy ? filter(x -> any(occursin.(names, name(x))), calculations(job)) : calculation.(job, names)
calculations(job::DFJob, n::String, fuzzy=true) = calculations(job, [n], fuzzy)
calculations(job::DFJob, package_::Package) = filter(x->package(x)==package_, calculations(job))
calculations(job::DFJob, ::Type{P}) where {P<:Package} = filter(x->package(x)==P, calculations(job))
inpath(job::DFJob, n) = inpath(calculation(job,n))
outpath(job::DFJob, n) = outpath(calculation(job,n))

Expand Down
114 changes: 1 addition & 113 deletions src/jobAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ end
"""
set_headerword!(job::DFJob, old_new::Pair{String, String})
Replaces the specified word in the header with the new word.
"""
function set_headerword!(job::DFJob, old_new::Pair{String, String}; print=true)
Expand Down Expand Up @@ -213,74 +212,8 @@ function Base.getindex(job::DFJob, id::String)
end
end


"""
getindex(job::DFJob, flag::Symbol)
Searches through the job's calculations for the requested flag.
A `Dict` will be returned with calculation names as the keys and the flags as values.
"""
function Base.getindex(job::DFJob, flg::Symbol)
outdict = Dict()
for i in calculations(job)
tfl = flag(i, flg)
if tfl != nothing
outdict[name(i)] = tfl
end
end
return outdict
end

"""
setindex!(job::DFJob, value, flag::Symbol)
Set `flag` in all the appropriate calculations to the `value`.
"""
function Base.setindex!(job::DFJob, value, key::Symbol)
for calculation in calculations(job)
calculation[key] = value
end
end

Base.getindex(job::DFJob, el::Element) = job.structure[el]

"Fuzzily search calculations in the job whose name contain the fuzzy."
searchcalculations(job::DFJob, fuzzy::AbstractString) = calculations(job, fuzzy, true)

"Fuzzily search the first calculation in the job whose name contains the fuzzy."
searchcalculation(job::DFJob, fuzzy::AbstractString) = calculation(job, fuzzy)

"Returns all calculations from a certain package."
searchcalculations(job::DFJob, ::Type{P}) where {P <: Package} = filter(x->package(x) == P, calculations(job))


"""
set_flags!(job::DFJob, calculations::Vector{<:DFCalculation}, flags...; print=true)
Sets the flags in the names to the flags specified.
This only happens if the specified flags are valid for the names.
If necessary the correct control block will be added to the calculation (e.g. for QEInputs).
The values that are supplied will be checked whether they are valid.
"""
function set_flags!(job::DFJob, calculations::Vector{<:DFCalculation}, flags...; print=true)
found_keys = Symbol[]

for calc in calculations
t_, = set_flags!(calc, flags..., print=print)
push!(found_keys, t_...)
end
nfound = setdiff([k for (k, v) in flags], found_keys)
if print && length(nfound) > 0
f = length(nfound) == 1 ? "flag" : "flags"
dfprintln("$f '$(join(":" .* String.(setdiff(flagkeys, found_keys)),", "))' were not found in the allowed calculation variables of the specified calculations!")
end
return job
end
set_flags!(job::DFJob, flags...;kwargs...) =
set_flags!(job, calculations(job), flags...;kwargs...)
set_flags!(job::DFJob, name::String, flags...; fuzzy=true, kwargs...) =
set_flags!(job, calculations(job, name, fuzzy), flags...; kwargs...)

""" data(job::DFJob, name::String, dataname::Symbol)
Expand Down Expand Up @@ -322,19 +255,6 @@ sets the option of specified data block in all calculations that have the block.
set_data_option!(job::DFJob, n::Symbol, option::Symbol; kw...) =
set_data_option!(job, name.(calculations(job)), n, option; kw...)

"""
rm_flags!(job::DFJob, calculations::Vector{<:DFCalculation}, flags...)
Looks through the calculation names and removes the specified flags.
"""
function rm_flags!(job::DFJob, calculations::Vector{<:DFCalculation}, flags...; kwargs...)
rm_flags!.(calculations, flags...; kwargs...)
return job
end
rm_flags!(job::DFJob, name::String, flags...; fuzzy=true, kwargs...) =
rm_flags!(job, calculations(job, name, fuzzy), flags...; kwargs...)
rm_flags!(job::DFJob, flags...; kwargs...) =
rm_flags!(job, calculations(job), flags...; kwargs...)

"Finds the output files for each of the calculations of a job, and groups all found data into a dictionary."
function outputdata(job::DFJob, calculations::Vector{DFCalculation}; print=true, onlynew=false)
Expand Down Expand Up @@ -377,7 +297,7 @@ DOS, and what the size of the frozen window needs to be to fit enough bands insi
depending on the projections.
"""
function set_wanenergies!(job::DFJob, nscf::DFCalculation, Emin::Real; Epad=5.0)
wancalcs = searchcalculations(job, Wannier90)
wancalcs = calculations(job, Wannier90)
@assert length(wancalcs) != 0 "Job ($(job.name)) has no Wannier90 calculations, nothing to do."
map(x->set_wanenergies!(x, structure(job), nscf, Emin; Epad=Epad), wancalcs)
return job
Expand Down Expand Up @@ -476,38 +396,6 @@ function readbands(job::DFJob)
return readbands(calculation)
end

"""
symmetry_operators(j::DFJob; maxsize=52, tolerance=$DEFAULT_TOLERANCE)
symmetry_operators(s::Structure; maxsize=52, tolerance=$DEFAULT_TOLERANCE)
Finds and returns all the rotations and translations that are symmetry operators of the structure.
"""
symmetry_operators(j::DFJob; kwargs...) = symmetry_operators(j.structure; kwargs...)

"""
international(j::DFJob; tolerance=$DEFAULT_TOLERANCE)
international(s::Structure; tolerance=$DEFAULT_TOLERANCE)
Returns the international symbol of the space group of the structure.
"""
international(j::DFJob; kwargs...) = international(j.structure; kwargs...)

"""
niggli_reduce(j::DFJob; tolerance=$DEFAULT_TOLERANCE)
niggli_reduce(s::Structure; tolerance=$DEFAULT_TOLERANCE)
Returns the niggli reduced lattice cell.
"""
niggli_reduce(j::DFJob; kwargs...) = niggli_reduce(j.structure; kwargs...)

for (calc, tn) in zip((:gencalc_bands, :gencalc_nscf, :gencalc_projwfc), ("scf", "scf", "nscf"))
@eval function $calc(job::DFJob, args...;template_name::String=$tn, kwargs...)
template = calculation(job, template_name)
@assert template !== nothing "No valid calculation with template_name $template_name found in job."
return $calc(template, args...; kwargs...)
end
end

"""
gencalc_wan(job::DFJob, min_window_determinator::Real, extra_wan_flags...; kwargs...)
Expand Down
Loading

0 comments on commit 2c006c6

Please sign in to comment.