Skip to content

Commit

Permalink
filename refactor done, all tests work now more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Jun 13, 2018
1 parent 3a14e5b commit 6788183
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 227 deletions.
7 changes: 3 additions & 4 deletions src/DFControl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ module DFControl
setflow!,
execs, setexecflags!, setexecdir!, rmexecflags!,
input, inputs,
path, outpath, setfilename!, setkpoints!, setdataoption!, setpseudos!,
inpath, outpath, setname!, setkpoints!, setdataoption!, setpseudos!,
atoms, atom, setatoms!, setprojections!, projections,
addwancalc!, addcalc!,
setwanenergies!,
outputdata,

setheaderword!, setserverdir!, setlocaldir!,
save, submit, abort, isrunning,
undo, undo!,
print_info

undo, undo!

include("constants.jl")
export qe_input_flags

Expand Down
2 changes: 1 addition & 1 deletion src/display/printing_juno.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ getfield_(x, f) = isdefined(x, f) ? getfield(x, f) : UNDEF
end

@render i::Inline x::DFInput begin
runstring = x.run ? span(".syntax--constant.syntax--other.syntax--symbol",x.filename) : span(".syntax--comment", x.filename)
runstring = x.run ? span(".syntax--constant.syntax--other.syntax--symbol", name(x)) : span(".syntax--comment", name(x))
fields = filter(x->x != run, fieldnames(x)[2:end])
Tree(runstring , [SubTree(Text("$f"), getfield(x, f)) for f in fields])
end
Expand Down
35 changes: 14 additions & 21 deletions src/fileio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,41 +126,41 @@ function writeexec(f, exec::Exec)
end

function writetojob(f, job, input::DFInput)
filename = input.filename
filename = infile(input)
should_run = input.run
save(input, job.structure, job.local_dir * filename)
save(input, job.structure)
if !should_run
write(f, "#")
end
writeexec.(f, execs(input))
write(f, "< $filename > $(split(filename,".")[1]).out\n")
write(f, "< $filename > $(outfile(input))\n")
return 1
end

function writetojob(f, job, input::DFInput{Wannier90})
filename = input.filename
filename = infile(input)
should_run = input.run
id = findfirst(job.inputs, input)
seedname = splitext(filename)[1]
seedname = name(input)

pw2wanid = findfirst(x -> contains(x.execs[2].exec, "pw2wannier90.x"), job.inputs[id+1:end])+id
pw2wan = job.inputs[pw2wanid]
setflags!(pw2wan, :seedname => "'$(splitext(input.filename)[1])'",print=false)
setflags!(pw2wan, :seedname => "'$seedname'", print=false)

if !pw2wan.run
write(f, "#")
end
writeexec.(f, execs(input))
write(f, "-pp $(filename[1:end-4]).win > $(filename[1:end-4]).wout\n")
write(f, "-pp $filename > $(outfile(input))\n")

save(input, job.structure, job.local_dir * filename)
save(input, job.structure)
writetojob(f, job, pw2wan)

if !should_run
write(f, "#")
end
writeexec.(f, execs(input))
write(f, "$(filename[1:end-4]).win > $(filename[1:end-4]).wout\n")
write(f, "$filename > $(outfile(input))\n")
return 2
end
"""
Expand All @@ -170,16 +170,15 @@ Writes all the input files and job file that are linked to a DFJob.
"""
function writejobfiles(job::DFJob)
rm.(job.local_dir .* searchdir(job.local_dir, ".in"))
new_filenames = getfield.(job.inputs, :filename)
open(job.local_dir * "job.tt", "w") do f
write(f, "#!/bin/bash\n")
write_job_name(f, job)
write_job_header(f, job)
abiinputs = Vector{DFInput{Abinit}}(filter(x -> package(x) == Abinit, job.inputs))
!isempty(abiinputs) && push!(new_filenames, writetojob(f, job, abiinputs)...)
abiinputs = Vector{DFInput{Abinit}}(filter(x -> package(x) == Abinit, inputs(job)))
!isempty(abiinputs) && writetojob(f, job, abiinputs)
i = length(abiinputs) + 1
while i <= length(job.inputs)
i += writetojob(f, job, job.inputs[i])
while i <= length(inputs(job))
i += writetojob(f, job, inputs(job)[i])
end
end
end
Expand Down Expand Up @@ -253,12 +252,6 @@ function read_job_filenames(job_file::String)
return input_files, output_files
end

"""
read_job_file(job_file::String)
Reads and returns all the relevant information contained in the job input file.
All files that are read contain extension "in".
"""
function read_job_inputs(job_file::String)
dir = splitdir(job_file)[1]
name = ""
Expand All @@ -280,7 +273,7 @@ function read_job_inputs(job_file::String)
elseif only_exec == "wannier90.x"
input, structure = read_wannier_input(joinpath(dir, splitext(inputfile)[1] * ".win"), runcommand=runcommand, run=run, exec=exec)
end
id = find(x-> x == splitext(inputfile)[1], getindex.(splitext.(getfield.(inputs, :filename)),1))
id = find(x-> infile(x) == inputfile, inputs)
if !isempty(id)
inputs[id[1]] = input
structures[id[1]] = structure
Expand Down
58 changes: 41 additions & 17 deletions src/input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@ mutable struct InputData
end

@with_kw mutable struct DFInput{P <: Package}
filename ::String
name ::String
dir ::String
flags ::SymAnyDict
data ::Vector{InputData}
execs ::Vector{Exec}
run ::Bool
outdata ::SymAnyDict=SymAnyDict()
end
DFInput{P}(file, flags, data, execs, run) where P<:Package = DFInput{P}(file, flags, data, execs, run, SymAnyDict())
DFInput{P}(name, dir, flags, data, execs, run) where P<:Package = DFInput{P}(name, dir, flags, data, execs, run, SymAnyDict())

"""
DFInput(template::DFInput, filename, newflags...; runcommand=template.runcommand, run=true)
DFInput(template::DFInput, name, newflags...; runcommand=template.runcommand, run=true)
Creates a new `DFInput` from a template `DFInput`, setting the newflags in the new one.
"""
function DFInput(template::DFInput, filename, newflags...; excs=execs(template), run=true, data=nothing)
function DFInput(template::DFInput, name, newflags...; excs=execs(template), run=true, data=nothing, dir=template.dir)
newflags = Dict(newflags...)

input = deepcopy(template)
input.filename = filename
input.name = name
input.execs = excs
input.run = run
input.dir = dir
setflags!(input, newflags..., print=false)

if data != nothing
Expand All @@ -36,7 +39,16 @@ function DFInput(template::DFInput, filename, newflags...; excs=execs(template),
return input
end

filename(input::DFInput) = input.filename
name(input::DFInput) = input.name
dir(input::DFInput) = input.dir
namewext(input::DFInput, ext) = name(input) * ext
infile(input::DFInput{QE}) = namewext(input, ".in")
infile(input::DFInput{Wannier90}) = namewext(input, ".win")
outfile(input::DFInput{QE}) = namewext(input, ".out")
outfile(input::DFInput{Wannier90}) = namewext(input, ".wout")
inpath(input::DFInput) = joinpath(dir(input), infile(input))
outpath(input::DFInput) = joinpath(dir(input), outfile(input))


flags(input::DFInput) = input.flags
function flag(input::DFInput, flag::Symbol)
Expand All @@ -48,8 +60,6 @@ end
Base.eltype(::DFInput{P}) where P = P
package(::DFInput{P}) where P = P

name(input::DFInput) = splitext(filename(input))[1]

data(input::DFInput) = input.data
data(input::DFInput, name) = getfirst(x-> x.name == name, input.data)
data(input::Vector{InputData}, name) = getfirst(x-> x.name == name, input.data)
Expand All @@ -65,13 +75,8 @@ rmexecflags!(input::DFInput, exec::String, flags...) = rmflags!.(execs(input, ex

runcommand(input::DFInput) = input.execs[1]

outfile(input::DFInput{QE}) = splitext(input.filename)[1]*".out"
outfile(input::DFInput{Wannier90}) = splitext(input.filename)[1]*".wout"

setflow!(input::DFInput, run) = input.run = run

outdata(input::DFInput) = input.outdata
hasoutput(input::DFInput) = !isempty(outdata(input))

"""
setkpoints!(input::DFInput, k_grid)
Expand Down Expand Up @@ -253,11 +258,30 @@ end

isspincalc(input::DFInput) = all(flag(input, :nspin) .!= [nothing, 1])

readoutput(input::DFInput{QE}, filename) = read_qe_output(filename)
readoutput(input::DFInput{Wannier90}, filename) = SymAnyDict()
outdata(input::DFInput) = input.outdata
hasoutput(input::DFInput) = !isempty(outdata(input))

hasoutfile(input::DFInput) = ispath(outpath(input))
hasnewout(input::DFInput, time) = mtime(outpath(input)) > time

"Returns the outputdata for the input."
function outputdata(input::DFInput; print=true, overwrite=true)
if hasoutput(input) && !overwrite
return outdata(input)
end
if hasoutfile(input)
input.outdata = readoutput(input)
return input.outdata
end
print && warn("No output data or output file found for input: $(input.filename).")
return SymAnyDict()
end

readoutput(input::DFInput{QE}) = read_qe_output(outpath(input))
readoutput(input::DFInput{Wannier90}) = SymAnyDict()

function readbands(input::DFInput, filename)
to = readoutput(input, filename)
function readbands(input::DFInput)
to = readoutput(input)
if haskey(to, :bands)
return to[:bands]
else
Expand Down
Loading

0 comments on commit 6788183

Please sign in to comment.