Skip to content

Commit

Permalink
Merge 7adabed into 6e87e04
Browse files Browse the repository at this point in the history
  • Loading branch information
navidcy committed Jul 11, 2019
2 parents 6e87e04 + 7adabed commit b0e5fba
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ struct Output
fields::Dict{Symbol,Function}
end

withoutjld2(path) = path[end-4:end] == ".jld2" ? path[1:end-5] : path
withoutjld2(path) = (length(path)>4 && path[end-4:end] == ".jld2") ? path[1:end-5] : path

"""
uniquepath(path)
Returns `path` with a number appended if `isfile(path)`, incremented until `path` does not exist.
"""
function uniquepath(path)
n = 0
n = 1
if isfile(path)
path = withoutjld2(path) * "_$n.jld2"
end
while isfile(path)
n += 1
path = withoutjld2(path) * "_$n.jld2"
path = withoutjld2(path)[1:end-length("_$(n-1)")] * "_$n.jld2"
end
path
end
Expand All @@ -33,6 +36,7 @@ function Output(prob, path, fields::Dict{Symbol,Function})
end

Output(prob, path, fields...) = Output(prob, path, Dict{Symbol,Function}([fields...]))
Output(prob, path, field::Tuple{Symbol,T}) where T = Output(prob, path, Dict{Symbol,Function}([field]))

getindex(out::Output, key) = out.fields[key](out.prob)

Expand All @@ -43,10 +47,10 @@ Save the fields in `out.fields` to `out.path`.
"""
function saveoutput(out)
groupname = "snapshots"
jldopen(out.filename, "a+") do file
file["$groupname/t/$(out.prob.step)"] = out.prob.t
jldopen(out.path, "a+") do path
path["$groupname/t/$(out.prob.clock.step)"] = out.prob.clock.t
for fieldname in keys(out.fields)
file["$groupname/$fieldname/$(out.prob.step)"] = out[fieldname]
path["$groupname/$fieldname/$(out.prob.clock.step)"] = out[fieldname]
end
end
nothing
Expand Down Expand Up @@ -107,7 +111,7 @@ function saveproblem(prob, filename)
nothing
end

saveproblem(out::Output) = saveproblem(out.prob, out.filename)
saveproblem(out::Output) = saveproblem(out.prob, out.path)

"""
savediagnostic(diag, diagname)
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using FourierFlows: parsevalsum2
using LinearAlgebra: mul!, ldiv!, norm

const rtol_fft = 1e-12
const rtol_output = 1e-12
const rtol_timesteppers = 1e-12

const steppers = [
Expand Down Expand Up @@ -196,3 +197,12 @@ end
@test test_scalardiagnostics(freq=1)
@test test_scalardiagnostics(freq=2)
end

@time @testset "Output tests" begin
include("test_output.jl")

@test test_withoutjld2()
@test test_uniquepath()
@test test_outputconstructor()
@test test_getindex()
end
43 changes: 43 additions & 0 deletions test/test_output.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function test_withoutjld2()
namewithjld2 = "blahblah.jld2"
namewithoutjld2 = "blahblah"

return FourierFlows.withoutjld2(namewithjld2) == namewithoutjld2 && FourierFlows.withoutjld2(namewithoutjld2) == namewithoutjld2
end

function test_uniquepath()
path = "foo.jld2"
path1 = "foo_1.jld2"
path2 = "foo_2.jld2"
touch(path)
test1 = FourierFlows.uniquepath(path) == path1
touch(path1)
test2 = FourierFlows.uniquepath(path) == path2
return test1 && test2
end

function test_outputconstructor()
prob = FourierFlows.Diffusion.Problem(nx=32, Lx=2π, kappa=1e-2, dt=1e-7, stepper="ForwardEuler")
filename = joinpath(".", "testoutput.jld2")
get_sol(prob) = prob.sol
get_c(prob) = prob.vars.c

out1 = Output(prob, filename, (:sol, get_sol))
out2 = Output(prob, filename, (:sol, get_sol), (:c, get_c))

return typeof(out1)<:Output && typeof(out2)<:Output
end

function test_getindex()
prob = FourierFlows.Diffusion.Problem(nx=32, Lx=2π, kappa=1e-2, dt=1e-7, stepper="ForwardEuler")
filename = joinpath(".", "testoutput.jld2")

ctest = zeros((prob.grid.nx, ))
ctest[3] = π
prob.vars.c .= ctest

get_c(prob) = prob.vars.c
out = Output(prob, filename, (:c, get_c))

return isapprox(ctest, getindex(out, :c), rtol=rtol_output)
end

0 comments on commit b0e5fba

Please sign in to comment.