Skip to content

Commit

Permalink
Merge pull request #1022 from NREL-Sienna/gks/convert-load
Browse files Browse the repository at this point in the history
Implement convert_component! for StandardLoad <- PowerLoad
  • Loading branch information
jd-lara authored Oct 25, 2023
2 parents 68cceae + 703762a commit 5b08a27
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 12 deletions.
41 changes: 37 additions & 4 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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!(
sys::System,
old_load::PowerLoad,
new_type::Type{StandardLoad};
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(
Expand Down
12 changes: 12 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
@@ -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...)
40 changes: 39 additions & 1 deletion test/test_deprecations.jl
Original file line number Diff line number Diff line change
@@ -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
44 changes: 37 additions & 7 deletions test/test_powersystemconstructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ end
end

@testset "Test component conversion" begin
test_conversion =
test_line_conversion =
() -> begin
sys = System(joinpath(BAD_DATA, "case5_re.m"))
l = get_component(Line, sys, "bus2-bus3-i_4")
Expand All @@ -146,26 +146,56 @@ 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")
@test !isnothing(mline)
@test get_time_series(SingleTimeSeries, line, name) isa SingleTimeSeries
end
@test_logs (:error,) min_level = Logging.Error match_mode = :any test_conversion()

test_load_conversion =
() -> begin
sys = PSB.build_system(PSB.PSITestSystems, "c_sys5")
component_name = "Bus2"
ts_name = "max_active_power"
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!(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
# Conversion back is not implemented
end

@test_logs (:error,) min_level = Logging.Error match_mode = :any test_line_conversion()
test_load_conversion()
end

0 comments on commit 5b08a27

Please sign in to comment.