Skip to content

Commit

Permalink
Clean up generator_mapping.yaml selectors implementation, test
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielKS committed Nov 1, 2024
1 parent e139fa9 commit 3173796
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/PowerAnalytics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export is_col_meta, set_col_meta, set_col_meta!, time_df, time_vec, data_cols, d
set_agg_meta!, rebuild_metric
export compute, compute_set, compute_all, hcat_timed, aggregate_time, compose_metrics
export create_problem_results_dict
export parse_generator_mapping
export parse_generator_mapping_file, parse_injector_categories, parse_generator_categories
export mean, weighted_mean, unweighted_sum

# IMPORTS
Expand Down
47 changes: 34 additions & 13 deletions src/builtin_component_selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ function make_fuel_component_selector(
end

# Based on old PowerAnalytics' get_generator_mapping
function parse_generator_mapping(
"""
Parse a `generator_mapping.yaml` file into a dictionary of `ComponentSelector`s and a
dictionary of metadata if present
"""
function parse_generator_mapping_file(
filename;
root_type::Type{<:Component} = PSY.StaticInjection,
)
Expand All @@ -73,13 +77,34 @@ function parse_generator_mapping(
return mappings, get(in_data, FUEL_TYPES_META_KEY, nothing)
end

"""
Use [`parse_generator_mapping_file`](@ref) to parse a `generator_mapping.yaml` file into a
dictionary of all `ComponentSelector`s
"""
parse_injector_categories(filename; root_type::Type{<:Component} = PSY.StaticInjection) =
first(parse_generator_mapping_file(filename; root_type = root_type))

"""
Use [`parse_generator_mapping_file`](@ref) to parse a `generator_mapping.yaml` file into a
dictionary of `ComponentSelector`, excluding categories in the 'non_generators' list in
metadata
"""
function parse_generator_categories(filename;
root_type::Type{<:Component} = PSY.StaticInjection)
categories, meta = parse_generator_mapping_file(filename; root_type = root_type)
(isnothing(meta) || !haskey(meta, "non_generators")) && return nothing
return filter(pair -> !(first(pair) in meta["non_generators"]), categories)
end

# SELECTORS MODULE
"`PowerAnalytics` built-in `ComponentSelector`s. Use `names` to list what is available."
module Selectors
import
..make_selector,
..PSY,
..parse_generator_mapping,
..parse_generator_mapping_file,
..parse_injector_categories,
..parse_generator_categories,
..ComponentSelector,
..FUEL_TYPES_DATA_FILE
export
Expand All @@ -101,21 +126,17 @@ A dictionary of `ComponentSelector`s, each of which corresponds to one of the st
injector categories in `generator_mapping.yaml`
"""
injector_categories::AbstractDict{String, ComponentSelector} =
first(parse_generator_mapping(FUEL_TYPES_DATA_FILE))
parse_injector_categories(FUEL_TYPES_DATA_FILE)

"""
A dictionary of `ComponentSelector`s, each of which corresponds to one of the categories in
`generator_mapping.yaml`, only considering the components and categories that represent
`Generator`s (no storage or load)
generators (no storage or load)
"""
generator_categories::Union{AbstractDict{String, ComponentSelector}, Nothing} = let
categories, meta =
parse_generator_mapping(FUEL_TYPES_DATA_FILE)
if isnothing(meta) || !haskey(meta, "non_generators")
nothing
else
filter(pair -> !(first(pair) in meta["non_generators"]), categories)
end
result = parse_generator_categories(FUEL_TYPES_DATA_FILE)
isnothing(result) && @warn "Could not construct generator categories"
result
end

"""
Expand All @@ -126,8 +147,8 @@ categorized_injectors::ComponentSelector =
make_selector(values(injector_categories)...)

"""
A single `ComponentSelector` representing the `Generator`s in a `System` grouped by the
categories in `generator_mapping.yaml`
A single `ComponentSelector` representing the generators in a `System` (no storage or load)
grouped by the categories in `generator_mapping.yaml`
"""
categorized_generators::Union{ComponentSelector, Nothing} =
if isnothing(generator_categories)
Expand Down
46 changes: 25 additions & 21 deletions test/test_builtin_component_selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ name_and_type = component -> (typeof(component), get_name(component))
Set([(EnergyReservoirStorage, "Bat")])
end

# TODO rewrite based on refactored selectors
# @testset "Test `generators_of_category` and `generators_by_category`" begin
# @test isfile(PA.FUEL_TYPES_DATA_FILE)
# @test Set(keys(generators_of_category)) ==
# Set(["Biopower", "CSP", "Coal", "Geothermal", "Hydropower", "NG-CC", "NG-CT",
# "NG-Steam", "Natural gas", "Nuclear", "Other", "PV", "Petroleum", "Storage",
# "Wind"])
# @test Set(
# name_and_type.(get_components(generators_of_category["Wind"], test_sys)),
# ) == Set([(RenewableDispatch, "WindBusB"), (RenewableDispatch, "WindBusC"),
# (RenewableDispatch, "WindBusA")])
# @test Set(
# name_and_type.(get_components(generators_of_category["Coal"], test_sys)),
# ) == Set([(ThermalStandard, "Park City"), (ThermalStandard, "Sundance"),
# (ThermalStandard, "Alta"), (ThermalStandard, "Solitude"),
# (ThermalStandard, "Brighton")])
# @test Set(get_groups(generators_by_category, test_sys)) ==
# Set(values(generators_of_category))
# @test Set(keys(parse_generator_mapping(PA.FUEL_TYPES_DATA_FILE))) ==
# Set(keys(generators_of_category))
# end
@testset "Test `generator_mapping.yaml`-based functionality" begin
@test isfile(PA.FUEL_TYPES_DATA_FILE)
@test Set(keys(injector_categories)) ==
Set(["Biopower", "CSP", "Coal", "Geothermal", "Hydropower", "NG-CC", "NG-CT",
"NG-Steam", "Nuclear", "Other", "PV", "Petroleum", "Wind",
"Storage", "Load"])
@test Set(keys(generator_categories)) ==
Set(["Biopower", "CSP", "Coal", "Geothermal", "Hydropower", "NG-CC", "NG-CT",
"NG-Steam", "Nuclear", "Other", "PV", "Petroleum", "Wind"])
@test Set(
name_and_type.(get_components(injector_categories["Wind"], test_sys)),
) == Set([(RenewableDispatch, "WindBusB"), (RenewableDispatch, "WindBusC"),
(RenewableDispatch, "WindBusA")])
@test Set(
name_and_type.(get_components(generator_categories["Coal"], test_sys)),
) == Set([(ThermalStandard, "Park City"), (ThermalStandard, "Sundance"),
(ThermalStandard, "Alta"), (ThermalStandard, "Solitude"),
(ThermalStandard, "Brighton")])
@test Set(get_groups(categorized_injectors, test_sys)) ==
Set(values(injector_categories))
@test Set(get_groups(categorized_generators, test_sys)) ==
Set(values(generator_categories))
@test Set(keys(first(parse_generator_mapping_file(PA.FUEL_TYPES_DATA_FILE)))) ==
Set(keys(injector_categories))
end

0 comments on commit 3173796

Please sign in to comment.