Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand get_decision_problem_results with kwargs #1060

Merged
merged 7 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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

Check warning on line 68 in src/simulation/simulation_problem_results.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/simulation_problem_results.jl#L68

Added line #L68 was not covered by tests
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
35 changes: 33 additions & 2 deletions src/simulation/simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,43 @@ 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)
"""
tengis-nrl marked this conversation as resolved.
Show resolved Hide resolved
# 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} = IS.UnitSystem.NATURAL_UNITS,
)
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
get_system!(results)
(populate_units !== nothing) &&
PSY.set_units_base_system!(PSI.get_system(results), populate_units)
else
(populate_units !== nothing) && (populate_units !== IS.UnitSystem.NATURAL_UNITS) &&
tengis-nrl marked this conversation as resolved.
Show resolved Hide resolved
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
17 changes: 16 additions & 1 deletion test/test_simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -690,10 +689,26 @@ 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)
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"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also test that the errors work as expected when we can't find the system file and when we set populate_units without setting populate_system?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once this test is implemented we can merge

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @GabrielKS, could you confirm or deny the implementation of the test?

test_decision_problem_results_values(results_ed, results_uc, c_sys5_hy_ed, c_sys5_hy_uc)
end

Expand Down
Loading