Skip to content

Commit

Permalink
Merge pull request #1060 from NREL-Sienna/td/adding-kwargs-to-get_dec…
Browse files Browse the repository at this point in the history
…ision_problem_results

Expand get_decision_problem_results with kwargs
  • Loading branch information
jd-lara authored Mar 6, 2024
2 parents f63119c + 2752563 commit d530d20
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/simulation/simulation_problem_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 44 additions & 2 deletions src/simulation/simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ using DataStructures
import UUIDs
using Random
import Serialization
using Base.Filesystem

# Code Quality Tests
import Aqua
Expand Down
40 changes: 39 additions & 1 deletion test/test_simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ function test_simulation_results(
@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.
Expand Down Expand Up @@ -856,10 +855,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

Expand Down

0 comments on commit d530d20

Please sign in to comment.