Skip to content

Commit

Permalink
Merge pull request #877 from daniel-thom/fix-sim-dir-increments
Browse files Browse the repository at this point in the history
Fix handling of simulation execution directory
  • Loading branch information
jd-lara authored Aug 29, 2022
2 parents 4c01cb2 + b4ef3ee commit 6320924
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
27 changes: 19 additions & 8 deletions src/simulation/simulation_internal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,31 @@ function make_dirs(internal::SimulationInternal)
end

function _get_output_dir_name(path, sim_name)
# Return the next highest integer.
index = 2
for path_name in readdir(path)
index = _get_most_recent_execution(path, sim_name) + 1
return joinpath(path, "$sim_name-$index")
end

function _get_most_recent_execution(path, sim_name)
sim_dirs = readdir(path)
if isempty(sim_dirs)
fail = true
elseif length(sim_dirs) == 1
fail = sim_dirs[1] != sim_name
else
fail = false
end

fail && error("No simulation directories with name=$sim_name are in $path")
executions = [1]
for path_name in sim_dirs
regex = Regex("\\Q$sim_name\\E-(\\d+)\$")
m = match(regex, path_name)
if !isnothing(m)
num = parse(Int, m.captures[1])
if num >= index
index = num + 1
end
push!(executions, parse(Int, m.captures[1]))
end
end

return joinpath(path, "$sim_name-$index")
return maximum(executions)
end

function configure_logging(internal::SimulationInternal, file_mode)
Expand Down
65 changes: 49 additions & 16 deletions src/simulation/simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,67 @@ struct SimulationResults
store::Union{Nothing, SimulationStore}
end

function SimulationResults(path::AbstractString, execution=nothing; ignore_status=false)
# This method maintains compatibility with the old interface as long as there is only
# one simulation name.
unique_names = Set{String}()
for name in readdir(path)
m = match(r"(.*)-\d+$", name)
if isnothing(m)
push!(unique_names, name)
else
push!(unique_names, m.captures[1])
end
end

if length(unique_names) == 1
name = first(unique_names)
return SimulationResults(path, name, execution; ignore_status=ignore_status)
end

if "data_store" in readdir(path)
return SimulationResults(
dirname(path),
basename(path),
execution;
ignore_status=ignore_status,
)
end

error(
"Found more than one simulation name in $path. Please call the constructor that includes 'name.'",
)
end

"""
Construct SimulationResults from a simulation output directory.
# Arguments
- `path::AbstractString`: Simulation output directory
- `name::AbstractString`: Simulation name
- `execution::AbstractString`: Execution number. Default is the most recent.
- `ignore_status::Bool`: If true, return results even if the simulation failed.
"""
function SimulationResults(path::AbstractString, execution=nothing; ignore_status=false)
# path will be either the execution_path or the directory containing all executions.
contents = readdir(path)
if "data_store" in contents
execution_path = path
function SimulationResults(
path::AbstractString,
name::AbstractString,
execution=nothing;
ignore_status=false,
)
if isnothing(execution)
execution = _get_most_recent_execution(path, name)
end
if execution == 1
execution_path = joinpath(path, name)
else
if execution === nothing
executions = [parse(Int, f) for f in contents if occursin(r"^\d+$", f)]
if isempty(executions)
error("There are no simulation results in the path")
end
execution = maximum(executions)
end
execution_path = joinpath(path, string(execution))
if !isdir(execution_path)
error("Execution $execution not in the simulations results")
end
execution_path = joinpath(path, "$name-$execution")
end
if !isdir(execution_path)
error("No valid simulation in $execution_path: execution = $execution")
end

@info "Loading simulation results from $execution_path"
status = deserialize_status(joinpath(execution_path, RESULTS_DIR))
_check_status(status, ignore_status)

Expand Down
4 changes: 2 additions & 2 deletions test/test_simulation_partitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ end
)
@test execute_simulation(regular_sim) == PSI.RunStatus.SUCCESSFUL

regular_results = SimulationResults(joinpath(sim_dir, regular_name))
partitioned_results = SimulationResults(joinpath(sim_dir, partition_name))
regular_results = SimulationResults(sim_dir, regular_name)
partitioned_results = SimulationResults(sim_dir, partition_name)

functions = (
read_realized_aux_variables,
Expand Down
4 changes: 2 additions & 2 deletions test/test_simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ function test_decision_problem_results(

test_decision_problem_results_values(results_ed, results_uc, c_sys5_hy_ed, c_sys5_hy_uc)
if !in_memory
test_simulation_results_from_file(results.path, c_sys5_hy_ed, c_sys5_hy_uc)
test_simulation_results_from_file(dirname(results.path), c_sys5_hy_ed, c_sys5_hy_uc)
end
end

Expand Down Expand Up @@ -666,7 +666,7 @@ function test_emulation_problem_results(results::SimulationResults, in_memory)
end

function test_simulation_results_from_file(path::AbstractString, c_sys5_hy_ed, c_sys5_hy_uc)
results = SimulationResults(path)
results = SimulationResults(path, "no_cache")
@test list_decision_problems(results) == ["ED", "UC"]
results_uc = get_decision_problem_results(results, "UC")
results_ed = get_decision_problem_results(results, "ED")
Expand Down

0 comments on commit 6320924

Please sign in to comment.