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

Add ComponentSelector feature: PSI portion #1196

Merged
merged 5 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/PowerSimulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,15 @@ import InfrastructureSystems.Optimization:
import InfrastructureSystems.Optimization: read_results_with_keys, deserialize_key,
encode_key_as_string, encode_keys_as_strings, should_write_resulting_value,
convert_result_to_natural_units, to_matrix, get_store_container_type
import InfrastructureSystems.Optimization: get_source_data

# IS.Optimization imports that stay private, may or may not be additional methods in PowerSimulations

# PowerSystems imports
import PowerSystems:
get_components, get_component, get_available_components, get_available_component,
get_groups, get_available_groups

export get_name
export get_model_base_power
export get_optimizer_stats
Expand Down Expand Up @@ -530,6 +536,7 @@ include("simulation/simulation_store_params.jl")
include("simulation/hdf_simulation_store.jl")
include("simulation/in_memory_simulation_store.jl")
include("simulation/simulation_problem_results.jl")
include("simulation/get_components_interface.jl")
include("simulation/decision_model_simulation_results.jl")
include("simulation/emulation_model_simulation_results.jl")
include("simulation/realized_meta.jl")
Expand Down
74 changes: 74 additions & 0 deletions src/simulation/get_components_interface.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Analogous to `src/get_components_interface.jl` in PowerSystems.jl, see comments there.

# get_components
"""
Calling `get_components` on a `Results` is the same as calling
[`get_available_components`] on the system attached to the results.
"""
PSY.get_components(
::Type{T},
res::IS.Results;
subsystem_name = nothing,
) where {T <: IS.InfrastructureSystemsComponent} =
IS.get_components(T, res; subsystem_name = subsystem_name)

PSY.get_components(res::IS.Results, attribute::IS.SupplementalAttribute) =

Check warning on line 15 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L15

Added line #L15 was not covered by tests
IS.get_components(res, attribute)

PSY.get_components(

Check warning on line 18 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L18

Added line #L18 was not covered by tests
filter_func::Function,
::Type{T},
res::IS.Results;
subsystem_name = nothing,
) where {T <: IS.InfrastructureSystemsComponent} =
IS.get_components(filter_func, T, res; subsystem_name = subsystem_name)

PSY.get_components(

Check warning on line 26 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L26

Added line #L26 was not covered by tests
scope_limiter::Union{Function, Nothing},
selector::IS.ComponentSelector,
res::IS.Results,
) =
IS.get_components(scope_limiter, selector, res)

PSY.get_components(selector::IS.ComponentSelector, res::IS.Results) =

Check warning on line 33 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L33

Added line #L33 was not covered by tests
IS.get_components(selector, res)

# get_component
"""
Calling `get_component` on a `Results` is the same as calling
[`get_available_component`] on the system attached to the results.
"""
PSY.get_component(res::IS.Results, uuid::Base.UUID) = IS.get_component(res, uuid)
PSY.get_component(res::IS.Results, uuid::String) = IS.get_component(res, uuid)

Check warning on line 42 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L41-L42

Added lines #L41 - L42 were not covered by tests

PSY.get_component(

Check warning on line 44 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L44

Added line #L44 was not covered by tests
::Type{T},
res::IS.Results,
name::AbstractString,
) where {T <: IS.InfrastructureSystemsComponent} =
IS.get_component(T, res, name)

PSY.get_component(

Check warning on line 51 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L51

Added line #L51 was not covered by tests
scope_limiter::Union{Function, Nothing},
selector::IS.SingularComponentSelector,
res::IS.Results,
) =
IS.get_component(scope_limiter, selector, res)

PSY.get_component(selector::IS.SingularComponentSelector, res::IS.Results) =

Check warning on line 58 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L58

Added line #L58 was not covered by tests
IS.get_component(selector, res)

# get_groups
"""
Calling `get_groups` on a `Results` is the same as calling [`get_available_groups`] on
the system attached to the results.
"""
PSY.get_groups(

Check warning on line 66 in src/simulation/get_components_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/simulation/get_components_interface.jl#L66

Added line #L66 was not covered by tests
scope_limiter::Union{Function, Nothing},
selector::IS.ComponentSelector,
res::IS.Results,
) =
IS.get_groups(scope_limiter, selector, res)

PSY.get_groups(selector::IS.ComponentSelector, res::IS.Results) =
IS.get_groups(selector, res)
1 change: 1 addition & 0 deletions src/simulation/simulation_problem_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ end

get_model_name(res::SimulationProblemResults) = res.problem
get_system(res::SimulationProblemResults) = res.system
get_source_data(res::SimulationProblemResults) = get_system(res) # Needed for compatibility with the IS.Results interface
get_resolution(res::SimulationProblemResults) = res.resolution
get_execution_path(res::SimulationProblemResults) = res.execution_path
get_model_base_power(res::SimulationProblemResults) = res.base_power
Expand Down
7 changes: 7 additions & 0 deletions test/test_model_decision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ end
@test isa(get_realized_timestamps(res), StepRange{DateTime})
@test isa(IS.Optimization.get_source_data(res), PSY.System)
@test length(get_timestamps(res)) == 24

PSY.set_available!(first(get_components(ThermalStandard, get_system(res))), false)
@test collect(get_components(ThermalStandard, res)) ==
collect(get_available_components(ThermalStandard, get_system(res)))
sel = PSY.make_selector(ThermalStandard; groupby = :each)
@test collect(get_groups(sel, res)) ==
collect(get_available_groups(sel, get_system(res)))
end

@testset "Solve DecisionModelModel with auto-build" begin
Expand Down
17 changes: 17 additions & 0 deletions test/test_simulation_results.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,23 @@ function test_decision_problem_results_values(
@test IS.get_uuid(get_system(results_uc)) === IS.get_uuid(c_sys5_hy_uc)
@test IS.get_uuid(get_system(results_ed)) === IS.get_uuid(c_sys5_hy_ed)

# Temporarily mark some stuff unavailable
unav_uc = first(PSY.get_available_components(ThermalStandard, get_system(results_uc)))
PSY.set_available!(unav_uc, false)
unav_ed = first(PSY.get_available_components(ThermalStandard, get_system(results_ed)))
PSY.set_available!(unav_ed, false)
sel = PSY.make_selector(ThermalStandard; groupby = :each)
@test collect(get_components(ThermalStandard, results_uc)) ==
collect(get_available_components(ThermalStandard, get_system(results_uc)))
@test collect(get_components(ThermalStandard, results_ed)) ==
collect(get_available_components(ThermalStandard, get_system(results_ed)))
@test collect(get_groups(sel, results_uc)) ==
collect(get_available_groups(sel, get_system(results_uc)))
@test collect(get_groups(sel, results_ed)) ==
collect(get_available_groups(sel, get_system(results_ed)))
PSY.set_available!(unav_uc, true)
PSY.set_available!(unav_ed, true)

@test isempty(setdiff(UC_EXPECTED_VARS, list_variable_names(results_uc)))
@test isempty(setdiff(ED_EXPECTED_VARS, list_variable_names(results_ed)))

Expand Down
Loading