diff --git a/src/simulation/simulation_problem_results.jl b/src/simulation/simulation_problem_results.jl index 5d0cf565e2..a01680ac34 100644 --- a/src/simulation/simulation_problem_results.jl +++ b/src/simulation/simulation_problem_results.jl @@ -65,6 +65,7 @@ get_system(res::SimulationProblemResults) = res.system get_resolution(res::SimulationProblemResults) = res.resolution get_execution_path(res::SimulationProblemResults) = res.execution_path get_model_base_power(res::SimulationProblemResults) = res.base_power +get_system_uuid(results::PSI.SimulationProblemResults) = results.system_uuid IS.get_timestamp(result::SimulationProblemResults) = result.results_timestamps get_interval(res::SimulationProblemResults) = res.timestamps.step IS.get_base_power(result::SimulationProblemResults) = result.base_power diff --git a/src/simulation/simulation_results.jl b/src/simulation/simulation_results.jl index 142f17b5ee..5a05f73b43 100644 --- a/src/simulation/simulation_results.jl +++ b/src/simulation/simulation_results.jl @@ -193,12 +193,54 @@ Base.length(res::SimulationResults) = mapreduce(length, +, values(res.decision_problem_results)) get_exports_folder(x::SimulationResults) = joinpath(x.path, "exports") -function get_decision_problem_results(results::SimulationResults, problem) +""" +Return SimulationProblemResults corresponding to a SimulationResults + +# Arguments + - `sim_results::PSI.SimulationResults`: the simulation results to read from + - `problem::String`: the name of the problem (e.g., "UC", "ED") + - `populate_system::Bool = true`: whether to set the results' system using + [`read_serialized_system`](@ref) + - `populate_units::Union{IS.UnitSystem, String, Nothing} = IS.UnitSystem.NATURAL_UNITS`: + the units system with which to populate the results' system, if any (requires + `populate_system=true`) +""" + +function get_decision_problem_results( + results::SimulationResults, + problem::String; + populate_system::Bool = false, + populate_units::Union{IS.UnitSystem, String, Nothing} = nothing, +) if !haskey(results.decision_problem_results, problem) throw(IS.InvalidValue("$problem is not stored")) end - return results.decision_problem_results[problem] + results = results.decision_problem_results[problem] + + if populate_system + try + get_system!(results) + catch e + error("Can't find the system file or retrieve the system error=$e") + end + + if populate_units !== nothing + PSY.set_units_base_system!(PSI.get_system(results), populate_units) + else + PSY.set_units_base_system!(PSI.get_system(results), IS.UnitSystem.NATURAL_UNITS) + end + + else + (populate_units === nothing) || + throw( + ArgumentError( + "populate_units=$populate_units is unaccepted when populate_system=$populate_system", + ), + ) + end + + return results end function get_emulation_problem_results(results::SimulationResults) diff --git a/test/runtests.jl b/test/runtests.jl index 9f559b3ab9..78298345d2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,6 +26,7 @@ using DataStructures import UUIDs using Random import Serialization +using Base.Filesystem # Code Quality Tests import Aqua diff --git a/test/test_simulation_results.jl b/test/test_simulation_results.jl index cac3e635cb..2351ab5307 100644 --- a/test/test_simulation_results.jl +++ b/test/test_simulation_results.jl @@ -220,7 +220,6 @@ function test_simulation_results(file_path::String, export_path; in_memory = fal @test isempty(results) verify_export_results(results, export_path) - @test length(readdir(export_realized_results(results_ed))) === 17 # Test that you can't read a failed simulation. @@ -690,10 +689,49 @@ function test_simulation_results_from_file(path::AbstractString, c_sys5_hy_ed, c @test get_system(results_uc) === nothing @test length(read_realized_variables(results_uc)) == length(UC_EXPECTED_VARS) + results_ed = get_decision_problem_results(results, "ED") + @test isnothing(get_system(results_ed)) + + results_ed = get_decision_problem_results(results, "ED"; populate_system = true) + @test !isnothing(get_system(results_ed)) + @test PSY.get_units_base(get_system(results_ed)) == "NATURAL_UNITS" + @test_throws IS.InvalidValue set_system!(results_uc, c_sys5_hy_ed) + + current_file = joinpath( + results_uc.execution_path, + "problems", + results_uc.problem, + PSI.make_system_filename(results_uc.system_uuid), + ) + mv(current_file, "system-temporary-file-name.json"; force = true) + + @test_throws ErrorException get_decision_problem_results( + results, + "UC"; + populate_system = true, + ) + mv("system-temporary-file-name.json", current_file) + set_system!(results_ed, c_sys5_hy_ed) set_system!(results_uc, c_sys5_hy_uc) + results_ed = get_decision_problem_results( + results, + "ED"; + populate_system = true, + populate_units = IS.UnitSystem.DEVICE_BASE, + ) + @test !isnothing(PSI.get_system(results_ed)) + @test PSY.get_units_base(get_system(results_ed)) == "DEVICE_BASE" + + @test_throws ArgumentError get_decision_problem_results( + results, + "ED"; + populate_system = false, + populate_units = IS.UnitSystem.DEVICE_BASE, + ) + test_decision_problem_results_values(results_ed, results_uc, c_sys5_hy_ed, c_sys5_hy_uc) end