From 1b7c1c0c4c65a11808640de2b556157a062bd2ee Mon Sep 17 00:00:00 2001 From: GabrielKS <23368820+GabrielKS@users.noreply.github.com> Date: Tue, 17 Oct 2023 13:30:22 -0600 Subject: [PATCH 1/3] Implement convert_component! for StandardLoad <- PowerLoad - Define convert_component! for conversions from PowerLoad to StandardLoad - Add unit tests for this method along the lines of existing convert_component! unit tests - Refactor convert_component! unit tests to remove repetition --- src/base.jl | 33 ++++++++++++ test/test_powersystemconstructors.jl | 81 +++++++++++++++++++++------- 2 files changed, 94 insertions(+), 20 deletions(-) diff --git a/src/base.jl b/src/base.jl index 16aa0eb0da..193c2e951a 100644 --- a/src/base.jl +++ b/src/base.jl @@ -1991,6 +1991,39 @@ function convert_component!( return end +""" +Converts a PowerLoad component to a StandardLoad component and replaces the original in the +system. Does not set any fields in StandardLoad that lack a PowerLoad equivalent +""" +function convert_component!( + new_type::Type{StandardLoad}, + old_load::PowerLoad, + sys::System; + kwargs..., +) + new_load = new_type(; + name = get_name(old_load), + available = get_available(old_load), + bus = get_bus(old_load), + base_power = get_base_power(old_load), + constant_active_power = get_active_power(old_load), + constant_reactive_power = get_reactive_power(old_load), + max_constant_active_power = get_max_active_power(old_load), + max_constant_reactive_power = get_max_active_power(old_load), + dynamic_injector = get_dynamic_injector(old_load), + internal = deepcopy(get_internal(old_load)), + services = Device[], + time_series_container = InfrastructureSystems.TimeSeriesContainer(), + ) + IS.assign_new_uuid!(new_load) + add_component!(sys, new_load) + copy_time_series!(new_load, old_load) + for service in get_services(old_load) + add_service!(new_load, service, sys) + end + remove_component!(sys, old_load) +end + function _validate_or_skip!(sys, component, skip_validation) if skip_validation && get_runchecks(sys) @warn( diff --git a/test/test_powersystemconstructors.jl b/test/test_powersystemconstructors.jl index efb4b6a1f8..7b6acb40ed 100644 --- a/test/test_powersystemconstructors.jl +++ b/test/test_powersystemconstructors.jl @@ -132,26 +132,51 @@ end end @testset "Test component conversion" begin - test_conversion = + # Reusable resources for this testset + load_test_system = () -> System(joinpath(BAD_DATA, "case5_re.m")) + function setup_time_series!(sys::System, component::Component, ts_name::String) + dates = collect( + Dates.DateTime("2020-01-01T00:00:00"):Dates.Hour(1):Dates.DateTime( + "2020-01-01T23:00:00", + ), + ) + data = collect(1:24) + ta = TimeSeries.TimeArray(dates, data, [get_name(component)]) + time_series = SingleTimeSeries(; name = ts_name, data = ta) + add_time_series!(sys, component, time_series) + @test get_time_series(SingleTimeSeries, component, ts_name) isa SingleTimeSeries + end + + function test_forward_conversion( + new_type::Type{<:Component}, + old_component::Component, + sys, + component_name, + ts_name, + ) + convert_component!(new_type, old_component, sys) + println(typeof(old_component)) + @test isnothing(get_component(typeof(old_component), sys, component_name)) + new_component = get_component(new_type, sys, component_name) + @test !isnothing(new_component) + @test get_name(new_component) == component_name + @test get_time_series(SingleTimeSeries, new_component, ts_name) isa SingleTimeSeries + return new_component + end + + """Tests Line <-> MonitoredLine conversion""" + test_line_conversion = () -> begin - sys = System(joinpath(BAD_DATA, "case5_re.m")) + sys = load_test_system() l = get_component(Line, sys, "bus2-bus3-i_4") - initial_time = Dates.DateTime("2020-01-01T00:00:00") - dates = collect( - initial_time:Dates.Hour(1):Dates.DateTime("2020-01-01T23:00:00"), - ) - data = collect(1:24) - ta = TimeSeries.TimeArray(dates, data, [get_name(l)]) - name = "active_power_flow" - time_series = SingleTimeSeries(; name = name, data = ta) - add_time_series!(sys, l, time_series) - @test get_time_series(SingleTimeSeries, l, name) isa SingleTimeSeries - PSY.convert_component!(MonitoredLine, l, sys) - @test isnothing(get_component(Line, sys, "bus2-bus3-i_4")) - mline = get_component(MonitoredLine, sys, "bus2-bus3-i_4") - @test !isnothing(mline) - @test get_name(mline) == "bus2-bus3-i_4" - @test get_time_series(SingleTimeSeries, mline, name) isa SingleTimeSeries + ts_name = "active_power_flow" + setup_time_series!(sys, l, ts_name) + + # Conversion to MonitoredLine + mline = + test_forward_conversion(MonitoredLine, l, sys, "bus2-bus3-i_4", ts_name) + + # Conversion back (must be forced) @test_throws ErrorException convert_component!( Line, get_component(MonitoredLine, sys, "bus2-bus3-i_4"), @@ -165,7 +190,23 @@ end ) line = get_component(Line, sys, "bus2-bus3-i_4") @test !isnothing(mline) - @test get_time_series(SingleTimeSeries, line, name) isa SingleTimeSeries + @test get_time_series(SingleTimeSeries, line, ts_name) isa SingleTimeSeries end - @test_logs (:error,) min_level = Logging.Error match_mode = :any test_conversion() + + """Tests PowerLoad --> StandardLoad conversion""" + test_load_conversion = + () -> begin + sys = load_test_system() + l = get_component(PowerLoad, sys, "bus2") + ts_name = "max_active_power" + setup_time_series!(sys, l, ts_name) + + # Conversion to StandardLoad + sload = test_forward_conversion(StandardLoad, l, sys, "bus2", ts_name) + + # Conversion back is not implemented + end + + @test_logs (:error,) min_level = Logging.Error match_mode = :any test_line_conversion() + @test_logs (:error,) min_level = Logging.Error match_mode = :any test_load_conversion() end From 9c7ef0fea6f1a7509d541f41c830d0d4ac2cde3c Mon Sep 17 00:00:00 2001 From: GabrielKS <23368820+GabrielKS@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:45:24 -0600 Subject: [PATCH 2/3] Revert unit test refactor, test new method separately --- test/test_powersystemconstructors.jl | 93 ++++++++++++---------------- 1 file changed, 40 insertions(+), 53 deletions(-) diff --git a/test/test_powersystemconstructors.jl b/test/test_powersystemconstructors.jl index 7b6acb40ed..e3057e3795 100644 --- a/test/test_powersystemconstructors.jl +++ b/test/test_powersystemconstructors.jl @@ -132,51 +132,26 @@ end end @testset "Test component conversion" begin - # Reusable resources for this testset - load_test_system = () -> System(joinpath(BAD_DATA, "case5_re.m")) - function setup_time_series!(sys::System, component::Component, ts_name::String) - dates = collect( - Dates.DateTime("2020-01-01T00:00:00"):Dates.Hour(1):Dates.DateTime( - "2020-01-01T23:00:00", - ), - ) - data = collect(1:24) - ta = TimeSeries.TimeArray(dates, data, [get_name(component)]) - time_series = SingleTimeSeries(; name = ts_name, data = ta) - add_time_series!(sys, component, time_series) - @test get_time_series(SingleTimeSeries, component, ts_name) isa SingleTimeSeries - end - - function test_forward_conversion( - new_type::Type{<:Component}, - old_component::Component, - sys, - component_name, - ts_name, - ) - convert_component!(new_type, old_component, sys) - println(typeof(old_component)) - @test isnothing(get_component(typeof(old_component), sys, component_name)) - new_component = get_component(new_type, sys, component_name) - @test !isnothing(new_component) - @test get_name(new_component) == component_name - @test get_time_series(SingleTimeSeries, new_component, ts_name) isa SingleTimeSeries - return new_component - end - - """Tests Line <-> MonitoredLine conversion""" test_line_conversion = () -> begin - sys = load_test_system() + sys = System(joinpath(BAD_DATA, "case5_re.m")) l = get_component(Line, sys, "bus2-bus3-i_4") - ts_name = "active_power_flow" - setup_time_series!(sys, l, ts_name) - - # Conversion to MonitoredLine - mline = - test_forward_conversion(MonitoredLine, l, sys, "bus2-bus3-i_4", ts_name) - - # Conversion back (must be forced) + initial_time = Dates.DateTime("2020-01-01T00:00:00") + dates = collect( + initial_time:Dates.Hour(1):Dates.DateTime("2020-01-01T23:00:00"), + ) + data = collect(1:24) + ta = TimeSeries.TimeArray(dates, data, [get_name(l)]) + name = "active_power_flow" + time_series = SingleTimeSeries(; name = name, data = ta) + add_time_series!(sys, l, time_series) + @test get_time_series(SingleTimeSeries, l, name) isa SingleTimeSeries + PSY.convert_component!(MonitoredLine, l, sys) + @test isnothing(get_component(Line, sys, "bus2-bus3-i_4")) + mline = get_component(MonitoredLine, sys, "bus2-bus3-i_4") + @test !isnothing(mline) + @test get_name(mline) == "bus2-bus3-i_4" + @test get_time_series(SingleTimeSeries, mline, name) isa SingleTimeSeries @test_throws ErrorException convert_component!( Line, get_component(MonitoredLine, sys, "bus2-bus3-i_4"), @@ -190,23 +165,35 @@ end ) line = get_component(Line, sys, "bus2-bus3-i_4") @test !isnothing(mline) - @test get_time_series(SingleTimeSeries, line, ts_name) isa SingleTimeSeries + @test get_time_series(SingleTimeSeries, line, name) isa SingleTimeSeries end - """Tests PowerLoad --> StandardLoad conversion""" test_load_conversion = () -> begin - sys = load_test_system() - l = get_component(PowerLoad, sys, "bus2") + sys = PSB.build_system(PSB.PSITestSystems, "c_sys5") + component_name = "Bus2" ts_name = "max_active_power" - setup_time_series!(sys, l, ts_name) - - # Conversion to StandardLoad - sload = test_forward_conversion(StandardLoad, l, sys, "bus2", ts_name) - + old_component = get_component(PowerLoad, sys, component_name) + dates = collect( + Dates.DateTime("2020-01-01T00:00:00"):Dates.Hour(1):Dates.DateTime( + "2020-01-01T23:00:00", + ), + ) + data = collect(1:24) + ta = TimeSeries.TimeArray(dates, data, [component_name]) + time_series = SingleTimeSeries(; name = ts_name, data = ta) + add_time_series!(sys, old_component, time_series) + @test get_time_series(SingleTimeSeries, old_component, ts_name) isa SingleTimeSeries + + convert_component!(StandardLoad, old_component, sys) + @test isnothing(get_component(typeof(old_component), sys, component_name)) + new_component = get_component(StandardLoad, sys, component_name) + @test !isnothing(new_component) + @test get_name(new_component) == component_name + @test get_time_series(SingleTimeSeries, new_component, ts_name) isa SingleTimeSeries # Conversion back is not implemented end - + @test_logs (:error,) min_level = Logging.Error match_mode = :any test_line_conversion() - @test_logs (:error,) min_level = Logging.Error match_mode = :any test_load_conversion() + test_load_conversion() end From 703762a15651373fff3ddf1905aad34917b30934 Mon Sep 17 00:00:00 2001 From: GabrielKS <23368820+GabrielKS@users.noreply.github.com> Date: Tue, 24 Oct 2023 14:20:02 -0600 Subject: [PATCH 3/3] Change convert_component! argument order - For all non-deprecated convert_component! methods, use the argument order: system, old component, new component type - Deprecate old convert_component! methods for backwards compatibility - Test deprecation - Run formatter --- src/base.jl | 12 ++++----- src/deprecated.jl | 12 +++++++++ test/test_deprecations.jl | 40 +++++++++++++++++++++++++++- test/test_powersystemconstructors.jl | 20 +++++++------- 4 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/base.jl b/src/base.jl index 193c2e951a..67f428295d 100644 --- a/src/base.jl +++ b/src/base.jl @@ -1920,9 +1920,9 @@ Converts a Line component to a MonitoredLine component and replaces the original system """ function convert_component!( - linetype::Type{MonitoredLine}, + sys::System, line::Line, - sys::System; + linetype::Type{MonitoredLine}; kwargs..., ) new_line = linetype( @@ -1954,9 +1954,9 @@ Converts a MonitoredLine component to a Line component and replaces the original system """ function convert_component!( - linetype::Type{Line}, + sys::System, line::MonitoredLine, - sys::System; + linetype::Type{Line}; kwargs..., ) force = get(kwargs, :force, false) @@ -1996,9 +1996,9 @@ Converts a PowerLoad component to a StandardLoad component and replaces the orig system. Does not set any fields in StandardLoad that lack a PowerLoad equivalent """ function convert_component!( - new_type::Type{StandardLoad}, + sys::System, old_load::PowerLoad, - sys::System; + new_type::Type{StandardLoad}; kwargs..., ) new_load = new_type(; diff --git a/src/deprecated.jl b/src/deprecated.jl index a19bb582e7..cdbb63baad 100644 --- a/src/deprecated.jl +++ b/src/deprecated.jl @@ -1 +1,13 @@ # BEGIN 2.0.0 deprecations +Base.@deprecate convert_component!( + linetype::Type{MonitoredLine}, + line::Line, + sys::System; + kwargs..., +) convert_component!(sys, line, linetype; kwargs...) +Base.@deprecate convert_component!( + linetype::Type{Line}, + line::MonitoredLine, + sys::System; + kwargs..., +) convert_component!(sys, line, linetype; kwargs...) diff --git a/test/test_deprecations.jl b/test/test_deprecations.jl index 539687053a..0dd0ef7add 100644 --- a/test/test_deprecations.jl +++ b/test/test_deprecations.jl @@ -1 +1,39 @@ -# Currently no 2.0.0 deprecations +@testset "Test deprecated convert_component!" begin + # Test copied from test_powersystemconstructors.jl + test_line_conversion = + () -> begin + sys = System(joinpath(BAD_DATA, "case5_re.m")) + l = get_component(Line, sys, "bus2-bus3-i_4") + initial_time = Dates.DateTime("2020-01-01T00:00:00") + dates = collect( + initial_time:Dates.Hour(1):Dates.DateTime("2020-01-01T23:00:00"), + ) + data = collect(1:24) + ta = TimeSeries.TimeArray(dates, data, [get_name(l)]) + name = "active_power_flow" + time_series = SingleTimeSeries(; name = name, data = ta) + add_time_series!(sys, l, time_series) + @test get_time_series(SingleTimeSeries, l, name) isa SingleTimeSeries + @test_deprecated PSY.convert_component!(MonitoredLine, l, sys) + @test isnothing(get_component(Line, sys, "bus2-bus3-i_4")) + mline = get_component(MonitoredLine, sys, "bus2-bus3-i_4") + @test !isnothing(mline) + @test get_name(mline) == "bus2-bus3-i_4" + @test get_time_series(SingleTimeSeries, mline, name) isa SingleTimeSeries + @test_deprecated @test_throws ErrorException convert_component!( + Line, + get_component(MonitoredLine, sys, "bus2-bus3-i_4"), + sys, + ) + @test_deprecated convert_component!( + Line, + get_component(MonitoredLine, sys, "bus2-bus3-i_4"), + sys; + force = true, + ) + line = get_component(Line, sys, "bus2-bus3-i_4") + @test !isnothing(mline) + @test get_time_series(SingleTimeSeries, line, name) isa SingleTimeSeries + end + @test_logs (:error,) min_level = Logging.Error match_mode = :any test_line_conversion() +end diff --git a/test/test_powersystemconstructors.jl b/test/test_powersystemconstructors.jl index e3057e3795..d099ce5442 100644 --- a/test/test_powersystemconstructors.jl +++ b/test/test_powersystemconstructors.jl @@ -146,21 +146,21 @@ end time_series = SingleTimeSeries(; name = name, data = ta) add_time_series!(sys, l, time_series) @test get_time_series(SingleTimeSeries, l, name) isa SingleTimeSeries - PSY.convert_component!(MonitoredLine, l, sys) + PSY.convert_component!(sys, l, MonitoredLine) @test isnothing(get_component(Line, sys, "bus2-bus3-i_4")) mline = get_component(MonitoredLine, sys, "bus2-bus3-i_4") @test !isnothing(mline) @test get_name(mline) == "bus2-bus3-i_4" @test get_time_series(SingleTimeSeries, mline, name) isa SingleTimeSeries @test_throws ErrorException convert_component!( - Line, - get_component(MonitoredLine, sys, "bus2-bus3-i_4"), sys, + get_component(MonitoredLine, sys, "bus2-bus3-i_4"), + Line, ) convert_component!( - Line, + sys, get_component(MonitoredLine, sys, "bus2-bus3-i_4"), - sys; + Line; force = true, ) line = get_component(Line, sys, "bus2-bus3-i_4") @@ -183,17 +183,19 @@ end ta = TimeSeries.TimeArray(dates, data, [component_name]) time_series = SingleTimeSeries(; name = ts_name, data = ta) add_time_series!(sys, old_component, time_series) - @test get_time_series(SingleTimeSeries, old_component, ts_name) isa SingleTimeSeries + @test get_time_series(SingleTimeSeries, old_component, ts_name) isa + SingleTimeSeries - convert_component!(StandardLoad, old_component, sys) + convert_component!(sys, old_component, StandardLoad) @test isnothing(get_component(typeof(old_component), sys, component_name)) new_component = get_component(StandardLoad, sys, component_name) @test !isnothing(new_component) @test get_name(new_component) == component_name - @test get_time_series(SingleTimeSeries, new_component, ts_name) isa SingleTimeSeries + @test get_time_series(SingleTimeSeries, new_component, ts_name) isa + SingleTimeSeries # Conversion back is not implemented end - + @test_logs (:error,) min_level = Logging.Error match_mode = :any test_line_conversion() test_load_conversion() end