Skip to content

Commit

Permalink
Accommodate ComponentSelector API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Oct 30, 2024
1 parent a512703 commit b37a78d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/builtin_component_selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function make_fuel_component_selector(category_spec::Dict)
selector_name = join(ifelse.(isnothing.(parse_results), "", string.(parse_results)),
COMPONENT_NAME_DELIMITER)

return make_selector(gen_type, filter_closure; name = selector_name)
return make_selector(filter_closure, gen_type; name = selector_name)
end

# Based on old PowerAnalytics' get_generator_mapping
Expand Down Expand Up @@ -69,10 +69,10 @@ export
generators_by_category

"A ComponentSelector representing all the electric load in a System"
all_loads::ComponentSelector = make_selector(PSY.ElectricLoad)
all_loads::ComponentSelector = make_selector(PSY.ElectricLoad; groupby = :all)

"A ComponentSelector representing all the storage in a System"
all_storage::ComponentSelector = make_selector(PSY.Storage)
all_storage::ComponentSelector = make_selector(PSY.Storage; groupby = :all)

"""
A dictionary of `ComponentSelector`s, each of which corresponds to one of the generator
Expand Down
14 changes: 3 additions & 11 deletions src/metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,7 @@ function _compute_one(metric::ComponentTimedMetric, results::IS.Results,
# TODO incorporate allow_missing
agg_fn = get_component_agg_fn(metric)
meta_agg_fn = get_component_meta_agg_fn(metric)
components = get_components(
selector,
PowerSimulations.get_system(results);
scope_limiter = is_available_for_analytics,
)
components = get_components(selector, results)
vals = [
compute(metric, results, com; kwargs...) for
com in components
Expand Down Expand Up @@ -312,18 +308,14 @@ group. Exclude components marked as not available.
"""
function compute(metric::ComponentTimedMetric, results::IS.Results,
selector::ComponentSelector; kwargs...)
subents = PSY.get_groups(
selector,
PowerSimulations.get_system(results);
scope_limiter = is_available_for_analytics,
)
subents = get_groups(selector, results)
subcomputations = [_compute_one(metric, results, sub; kwargs...) for sub in subents]
return hcat_timed(subcomputations...)
end

# COMPUTE_ALL()
_is_single_group(selector::ComponentSelector, results::IS.Results) =
length(get_groups(selector, get_system(results))) == 1
length(get_groups(selector, results)) == 1
_is_single_group(selector, results::IS.Results) = true

# The core of compute_all, shared between the timed and timeless versions
Expand Down
8 changes: 6 additions & 2 deletions test/test_builtin_metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function test_metric(::Val{:calc_capacity_factor})
end

function test_metric(::Val{:calc_curtailment})
@test calc_curtailment(make_selector(RenewableDispatch), results_uc) isa ResultType
# TODO broken for groupby = :each?
@test calc_curtailment(make_selector(RenewableDispatch; groupby = :all), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_curtailment_frac})
Expand All @@ -47,7 +49,9 @@ function test_metric(::Val{:calc_discharge_cycles})
end

function test_metric(::Val{:calc_integration})
@test calc_integration(make_selector(RenewableDispatch), results_uc) isa ResultType
# TODO broken for groupby = :each?
@test calc_integration(make_selector(RenewableDispatch; groupby = :all), results_uc) isa
ResultType
end

function test_metric(::Val{:calc_is_slack_up})
Expand Down
21 changes: 13 additions & 8 deletions test/test_metrics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ end
test_selector_sets = [
make_selector(make_selector(wind_sel, solar_sel)),
make_selector(make_selector(test_selectors...)),
make_selector(ThermalStandard),
make_selector(ThermalStandard; groupby = :all),
]

for (label, res) in pairs(resultses)
Expand Down Expand Up @@ -431,8 +431,10 @@ end
test_calc_production_cost, test_calc_production_cost]
my_component = first(get_components(RenewableDispatch, get_system(results_uc)))
my_selectors =
[make_selector(ThermalStandard), make_selector(RenewableDispatch),
make_selector(ThermalStandard), my_component]
[make_selector(ThermalStandard; groupby = :all),
make_selector(RenewableDispatch; groupby = :all),
make_selector(ThermalStandard; groupby = :all),
my_component]
all_result = compute_all(results_uc, my_metrics, my_selectors)

for (metric, selector) in zip(my_metrics, my_selectors)
Expand Down Expand Up @@ -486,11 +488,12 @@ end
broadcasted_compute_all = compute_all(
results_uc,
[test_calc_active_power, test_calc_active_power],
make_selector(ThermalStandard),
make_selector(ThermalStandard; groupby = :all),
["discard", "ThermalStandard"],
)
@test broadcasted_compute_all[!, [DATETIME_COL, "ThermalStandard"]] ==
compute(test_calc_active_power, results_uc, make_selector(ThermalStandard))
compute(test_calc_active_power, results_uc,
make_selector(ThermalStandard; groupby = :all))

@test compute_all(results_uc, my_metrics, my_selectors, my_names) ==
compute_all(results_uc, collect(zip(my_metrics, my_selectors, my_names))...)
Expand All @@ -513,7 +516,8 @@ end
end

@testset "Test compose_metrics" begin
mysel = make_selector(ThermalStandard)
# TODO broken for groupby = :each?
mysel = make_selector(ThermalStandard; groupby = :all)
"Computes ActivePower*3"
mymet1 = compose_metrics(
"ThriceActivePower",
Expand Down Expand Up @@ -595,7 +599,8 @@ end
end

@testset "Test component_agg_fn and corresponding `compute` aggregation behavior" begin
my_selector = make_selector(ThermalStandard)
# TODO broken for groupby = :each?
my_selector = make_selector(ThermalStandard; groupby = :all)
my_results = results_uc
sum_metric = test_calc_active_power
@test get_component_agg_fn(sum_metric) == sum # Should be the default
Expand Down Expand Up @@ -633,7 +638,7 @@ end
end

@testset "Test time_agg_fn and corresponding `aggregate_time` aggregation behavior" begin
my_selector = make_selector(ThermalStandard)
my_selector = make_selector(ThermalStandard; groupby = :all)
my_results = results_uc
sum_metric = test_calc_active_power
@test get_time_agg_fn(sum_metric) == sum # Should be the default
Expand Down

0 comments on commit b37a78d

Please sign in to comment.