Skip to content

Commit

Permalink
cleanup file io (#167)
Browse files Browse the repository at this point in the history
* cleanup file io

* remove extra Val
  • Loading branch information
ffreyer authored Oct 18, 2022
1 parent 7525ca6 commit fb06689
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/flavors/DQMC/FileIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ function _load(data, ::Val{:DQMC})
data["CB"] ? CheckerboardTrue : CheckerboardFalse
else CB_type(data["type"]) end
@assert CB <: Checkerboard
parameters = _load(data["Parameters"], Val(:DQMCParameters))
analysis = _load(data["Analysis"], Val(:DQMCAnalysis))
parameters = _load(data["Parameters"])
tag = Val(Symbol(get(data["Analysis"], "tag", :DQMCAnalysis)))
analysis = _load(data["Analysis"], tag)
recorder = _load(data["configs"], to_tag(data["configs"]))
last_sweep = data["last_sweep"]
model = load_model(data["Model"], to_tag(data["Model"]))
if haskey(data, "field")
field = _load(data["field"], Val(:Field), parameters, model)
tag = Val(Symbol(get(data["field"], "tag", :Field)))
field = _load(data["field"], tag, parameters, model)
else
conf = data["conf"]
field = field_hint(model, to_tag(data["Model"]))(parameters, model)
conf!(field, conf)
end
scheduler = if haskey(data, "Scheduler")
_load(data["Scheduler"], to_tag(data["Scheduler"]))
_load(data["Scheduler"])
else
if haskey(data["Parameters"], "global_moves") && Bool(data["Parameters"]["global_moves"])
rate = get(data["Parameters"], "global_rate", 10)
Expand All @@ -61,7 +63,8 @@ function _load(data, ::Val{:DQMC})
end
end

combined_measurements = _load(data["Measurements"], Val(:Measurements))
tag = Val(Symbol(data["Measurements"], "tag", :Measurements))
combined_measurements = _load(data["Measurements"])
thermalization_measurements = combined_measurements[:TH]
measurements = combined_measurements[:ME]

Expand Down Expand Up @@ -123,6 +126,7 @@ end

function _save(file::FileLike, entryname::String, a::DQMCAnalysis)
write(file, entryname * "/VERSION", 1)
write(file, entryname * "/tag", "DQMCAnalysis")
write(file, entryname * "/type", typeof(a))

write(file, entryname * "/th_runtime", a.th_runtime)
Expand Down
1 change: 1 addition & 0 deletions src/flavors/DQMC/fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Base.length(f::AbstractField) = length(conf(f))

function _save(file::FileLike, entryname::String, field::AbstractField)
write(file, entryname * "/VERSION", 1)
write(file, entryname * "/tag", "Field")
write(file, entryname * "/name", nameof(typeof(field)))
write(file, entryname * "/conf", conf(field))
end
Expand Down
14 changes: 7 additions & 7 deletions src/flavors/DQMC/measurements/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ _default_capacity(mc::DQMC) = 2 * ceil(Int, mc.parameters.sweeps / mc.parameters
# though maybe that's a good thing - changing function definitions doesn't
# break stuff this way
function _save(file::FileLike, key::String, m::DQMCMeasurement)
write(file, "$key/VERSION", 2)
write(file, "$key/VERSION", 3)
write(file, "$key/tag", "DQMCMeasurement")
_save(file, "$key/GI", m.greens_iterator)
_save(file, "$key/LI", m.lattice_iterator)
Expand Down Expand Up @@ -172,12 +172,12 @@ function _load(data, ::Val{:DQMCMeasurement})
@warn "Failed to load kernel in module MonteCarlo." exception=e
missing_kernel
end
gi = _load(data["GI"], Val(:GreensIterator))
# This should eventually just work as usual
li = if data["LI"]["tag"] == "Restructure"
_load(data["LI"], Val(:Restructure))
else
_load(data["LI"], Val(:LatticeIterator))
if data["VERSION"] > 2
gi = _load(data["GI"])
li = _load(data["LI"])
else # this makes it harder to adjust things...
gi = _load(data["GI"], Val(:GreensIterator))
li = _load(data["LI"], Val(:LatticeIterator))
end
fi = if haskey(data, "FI")
data["FI"]
Expand Down
5 changes: 3 additions & 2 deletions src/flavors/DQMC/measurements/greens_iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ verify(it::AbstractUnequalTimeGreensIterator, maxerror=1e-6) = maximum(accuracy(

function _save(file::FileLike, key::String, m::T) where {T <: Union{AbstractGreensIterator, Nothing}}
write(file, "$key/VERSION", 1)
write(file, "$key/tag", nameof(T))
write(file, "$key/tag", "GreensIterator")
write(file, "$key/name", nameof(T))
write(file, "$key/fields", getfield.((m,), fieldnames(T)))
return
end

function _load(data, ::Val{:GreensIterator})
# ifelse maybe long but should be better for compile time than adding a
# bunch more _load methods and better for runtime than an eval
tag = data["tag"]
tag = haskey(data, "name") ? data["name"] : data["tag"]
fields = data["fields"]
for T in _all_greens_iterator_types
if tag == nameof(T)
Expand Down
4 changes: 1 addition & 3 deletions src/flavors/DQMC/updates/scheduler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ function _save(file::FileLike, name::String, update::AcceptanceStatistics)
return
end
function _load(f::FileLike, ::Val{:AcceptanceStatistics})
AcceptanceStatistics(
f["accepted"], f["total"], _load(f["update"], Val(f["update/tag"]))
)
AcceptanceStatistics(f["accepted"], f["total"], _load(f["update"]))
end


Expand Down
2 changes: 1 addition & 1 deletion src/models/DummyModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function _load_to_dict(data::FileLike)
output = Dict{String, Any}()
for key in keys(data)
try
push!(output, key => _load(data[key], to_tag(data[key])))
push!(output, key => _load(data[key]))
catch e
push!(output, key => _load_to_dict(data[key]))
end
Expand Down

0 comments on commit fb06689

Please sign in to comment.