diff --git a/src/builtin_metrics.jl b/src/builtin_metrics.jl index a9a240e..e711906 100644 --- a/src/builtin_metrics.jl +++ b/src/builtin_metrics.jl @@ -64,7 +64,9 @@ import ..weighted_mean, ..unweighted_sum, ..mean, - ..read_component_result + ..read_component_result, + ...Selectors.all_loads, # number of dots obtained by trial and error + ...Selectors.all_storage export calc_active_power, calc_production_cost, calc_active_power_in, @@ -160,7 +162,7 @@ calc_system_load_from_storage = let eval_fn = ( res::IS.Results; kwargs... ) -> - compute(calc_load_from_storage, res, storage_component_selector; kwargs...), + compute(calc_load_from_storage, res, all_storage; kwargs...), ) end diff --git a/src/metrics.jl b/src/metrics.jl index 86392cf..558666d 100644 --- a/src/metrics.jl +++ b/src/metrics.jl @@ -113,7 +113,7 @@ get_time_agg_fn(m::TimedMetric) = m.time_agg_fn get_component_agg_fn(m::ComponentTimedMetric) = m.component_agg_fn get_time_meta_agg_fn(m::TimedMetric) = m.time_meta_agg_fn get_component_meta_agg_fn(m::ComponentTimedMetric) = m.component_meta_agg_fn -get_eval_zero(m::ComponentTimedMetric) = m.component_meta_agg_fn +get_eval_zero(m::ComponentTimedMetric) = m.eval_zero """ Returns a `Metric` identical to the input `metric` except with the changes to its diff --git a/test/setuptests.jl b/test/setuptests.jl index 1313c46..438289d 100644 --- a/test/setuptests.jl +++ b/test/setuptests.jl @@ -9,6 +9,7 @@ import InfrastructureSystems: Deterministic, Probabilistic, Scenarios, Forecast using PowerSystems using PowerAnalytics using PowerAnalytics.Selectors +using PowerAnalytics.Metrics using PowerSimulations using GLPK using TimeSeries @@ -35,6 +36,7 @@ const PSB = PowerSystemCaseBuilder const DISABLED_TEST_FILES = [ # Can generate with ls -1 test | grep "test_.*.jl" # "test_builtin_component_selectors.jl", +# "test_builtin_metrics.jl", # "test_input.jl", # "test_metrics.jl", # "test_result_sorting.jl", diff --git a/test/test_builtin_metrics.jl b/test/test_builtin_metrics.jl new file mode 100644 index 0000000..59db099 --- /dev/null +++ b/test/test_builtin_metrics.jl @@ -0,0 +1,121 @@ +# For now we are mostly just testing that all the metrics can be called without error, +# though I've built out the structure to do much more than that. TODO it would be great if +# we did. + +(results_uc, results_ed) = run_test_sim(TEST_RESULT_DIR, TEST_SIM_NAME) +const ResultType = AbstractDataFrame + +# Future implementers of built-in metrics must add tests as shown below or this will trigger +function test_metric(::Val{metric_name}) where {metric_name} + throw("Could not find test for $metric_name") +end + +function test_metric(::Val{:calc_active_power}) + @test calc_active_power(make_selector(ThermalStandard), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_active_power_forecast}) + @test calc_active_power_forecast(make_selector(RenewableDispatch), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_active_power_in}) + @test calc_active_power_in(make_selector(EnergyReservoirStorage), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_active_power_out}) + @test calc_active_power_out(make_selector(EnergyReservoirStorage), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_capacity_factor}) + @test calc_capacity_factor(make_selector(RenewableDispatch), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_curtailment}) + @test calc_curtailment(make_selector(RenewableDispatch), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_curtailment_frac}) + @test calc_curtailment_frac(make_selector(RenewableDispatch), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_discharge_cycles}) + @test calc_discharge_cycles(make_selector(EnergyReservoirStorage), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_integration}) + @test calc_integration(make_selector(RenewableDispatch), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_is_slack_up}) + @test calc_is_slack_up(results_ed) isa ResultType +end + +function test_metric(::Val{:calc_load_forecast}) + @test calc_load_forecast(make_selector(ElectricLoad), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_load_from_storage}) + @test calc_load_from_storage(make_selector(EnergyReservoirStorage), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_net_load_forecast}) + @test calc_load_from_storage(make_selector(EnergyReservoirStorage), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_production_cost}) + @test calc_production_cost(make_selector(ThermalStandard), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_shutdown_cost}) + @test calc_shutdown_cost(make_selector(ThermalStandard), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_startup_cost}) + @test calc_startup_cost(make_selector(ThermalStandard), results_uc) isa ResultType +end + +function test_metric(::Val{:calc_stored_energy}) + @test calc_stored_energy(make_selector(EnergyReservoirStorage), results_uc) isa + ResultType +end + +function test_metric(::Val{:calc_sum_bytes_alloc}) + @test calc_sum_bytes_alloc(results_uc) isa ResultType +end + +function test_metric(::Val{:calc_sum_objective_value}) + @test calc_sum_objective_value(results_uc) isa ResultType +end + +function test_metric(::Val{:calc_sum_solve_time}) + @test calc_sum_solve_time(results_uc) isa ResultType +end + +function test_metric(::Val{:calc_system_load_forecast}) + @test calc_system_load_forecast(results_uc) isa ResultType +end + +function test_metric(::Val{:calc_system_load_from_storage}) + @test calc_system_load_from_storage(results_uc) isa ResultType +end + +function test_metric(::Val{:calc_system_slack_up}) + @test calc_system_slack_up(results_ed) isa ResultType +end + +function test_metric(::Val{:calc_total_cost}) + @test calc_total_cost(make_selector(ThermalStandard), results_uc) isa ResultType +end + +all_metric_names = filter(x -> getproperty(PowerAnalytics.Metrics, x) isa Metric, + names(PowerAnalytics.Metrics)) + +@testset for metric_name in all_metric_names + test_metric(Val(metric_name)) +end