Skip to content

Commit

Permalink
Merge pull request #1 from GabrielKS/Replace-get_populated_decision_p…
Browse files Browse the repository at this point in the history
…roblem_results

Replace get populated decision problem results
  • Loading branch information
GabrielKS authored Mar 7, 2024
2 parents 87ce18a + cdfbec3 commit 3aa8d80
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 62 deletions.
3 changes: 1 addition & 2 deletions src/PowerAnalytics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export is_col_meta, set_col_meta, set_col_meta!, time_df, time_vec, data_cols, d
with_component_agg_fn, with_time_agg_fn, metric_selector_to_string, get_agg_meta,
set_agg_meta!
export compute, compute_set, compute_all, hcat_timed, aggregate_time, compose_metrics
export read_serialized_system,
get_populated_decision_problem_results, create_problem_results_dict
export create_problem_results_dict
export load_component_selector, storage_component_selector, generator_selectors_by_fuel
export calc_active_power, calc_production_cost, calc_startup_cost, calc_shutdown_cost,
calc_discharge_cycles, calc_system_slack_up, calc_load_forecast, calc_active_power_in,
Expand Down
53 changes: 4 additions & 49 deletions src/input.jl
Original file line number Diff line number Diff line change
@@ -1,49 +1,3 @@
# TODO should this be defined in PSI (or not at all)?
get_system_uuid(results::PSI.SimulationProblemResults) = results.system_uuid

"""
Read from disk the `System` associated with the given `SimulationProblemResults`
# Arguments
- `results::PSI.SimulationProblemResults`: the results whose system to load
"""
function read_serialized_system(results::PSI.SimulationProblemResults)
sys_filename = "system-$(get_system_uuid(results)).json"
sys_path = joinpath(
PSI.get_execution_path(results),
"problems",
PSI.get_model_name(results),
sys_filename,
)
return PowerSystems.System(sys_path)
end

# TODO should this just replace PSI.get_decision_problem_results?
"""
Wrapper around [`PSI.get_decision_problem_results`](@ref) that can also set the system using
[`read_serialized_system`](@ref) and populate the units system.
# 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_populated_decision_problem_results(
sim_results::PSI.SimulationResults,
problem::String;
populate_system::Bool = true,
populate_units::Union{IS.UnitSystem, String, Nothing} = IS.UnitSystem.NATURAL_UNITS,
)
results = PSI.get_decision_problem_results(sim_results, problem)
populate_system && PSI.set_system!(results, read_serialized_system(results))
(populate_units === nothing) ||
PSY.set_units_base_system!(PSI.get_system(results), populate_units)
return results
end

"""
Accept a directory that contains several results subdirectories (that each contain
Expand All @@ -57,20 +11,21 @@ values are loaded results datasets.
- `scenarios::Union{Vector{AbstractString}, Nothing} = nothing`: a list of scenario
subdirectories to load, or `nothing` to load all the subdirectories
- `kwargs...`: keyword arguments to pass through to
[`get_populated_decision_problem_results`](@ref)
[`PSI.get_decision_problem_results`](@ref)
"""
function create_problem_results_dict(
results_dir::AbstractString,
problem::String,
scenarios::Union{Vector{<:AbstractString}, Nothing} = nothing;
populate_system::Bool = false,
kwargs...,
)
if scenarios === nothing
scenarios = filter(x -> isdir(joinpath(results_dir, x)), readdir(results_dir))
end
return SortedDict(
scenario => get_populated_decision_problem_results(
scenario => PSI.get_decision_problem_results(
PSI.SimulationResults(joinpath(results_dir, scenario)),
problem; kwargs...) for scenario in scenarios
problem; populate_system = populate_system, kwargs...) for scenario in scenarios
)
end
16 changes: 5 additions & 11 deletions test/test_input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ stock_results_prob = run_test_prob()
sim_results = SimulationResults(TEST_RESULT_DIR, TEST_SIM_NAME)
decision_problem_names = ("UC", "ED")
my_results_sets = get_decision_problem_results.(Ref(sim_results), decision_problem_names)
populated_results_sets =
get_populated_decision_problem_results.(Ref(sim_results), decision_problem_names)

# TODO is there a better way?
function test_system_equivalence(sys1::System, sys2::System)
Expand All @@ -14,17 +12,13 @@ function test_system_equivalence(sys1::System, sys2::System)
sort(get_name.(get_components(Component, sys2)))
end

@testset "Test per-results functions" begin
for (populated_results, my_results, stock_results) in
zip(populated_results_sets, my_results_sets, stock_decision_results_sets)
@testset "Test results function" begin
for (my_results, stock_results) in
zip(my_results_sets, stock_decision_results_sets)
test_system_equivalence(
read_serialized_system(my_results),
get_system!(my_results),
get_system(stock_results),
)
test_system_equivalence(get_system(populated_results), get_system(stock_results))
# NOTE: why does get_units_base return a string and not an enum value?
@test get_units_base(get_system(populated_results)) ==
string(IS.UnitSystem.NATURAL_UNITS)
end
end

Expand Down Expand Up @@ -64,7 +58,7 @@ end
scenarios = create_problem_results_dict(TEST_RESULT_DIR, problem, scenario_names)
@test Set(keys(scenarios)) == Set(scenario_names)
test_system_equivalence(
get_system(scenarios[TEST_SIM_NAME]),
get_system!(scenarios[TEST_SIM_NAME]),
get_system(stock_results),
)
end
Expand Down

0 comments on commit 3aa8d80

Please sign in to comment.