Skip to content

Commit 97ec147

Browse files
authored
Fix error in calc_connected_components if there are no active buses (#933)
* return empty set if there are no active buses * fix typo
1 parent 7da140d commit 97ec147

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/core/data.jl

+5
Original file line numberDiff line numberDiff line change
@@ -2527,7 +2527,12 @@ function calc_connected_components(data::Dict{String,<:Any}; edges=["branch", "d
25272527
end
25282528

25292529
sorted_bus_ids = sort(collect(active_bus_ids))
2530+
if isempty(sorted_bus_ids)
2531+
# This is to avoid an error trying to assign i0 below.
2532+
return Set{Set{Int}}()
2533+
end
25302534
i0 = sorted_bus_ids[1] - 1 # this is to track un-visited buses
2535+
25312536
# The two dictionaries below are used to track the connected components
25322537
# `component_lookup` maps each bus to the ID of the connected component it belongs to, which is the smallest bus ID in said components
25332538
# `components` maps the connected component ID to a `Set{Int}` of all bus IDs in that component

test/data.jl

+14-4
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ end
345345
end
346346

347347

348-
@testset "connecected components" begin
348+
@testset "connected components" begin
349349
data = PowerModels.parse_file("../test/data/matpower/case6.m")
350350
cc = PowerModels.calc_connected_components(data)
351351

@@ -364,7 +364,17 @@ end
364364
@test cc2 == cc
365365
end
366366

367-
@testset "connecected components with switches" begin
367+
@testset "connected components with no active buses" begin
368+
data = PowerModels.parse_file("../test/data/matpower/case6.m")
369+
for (i, bus) in data["bus"]
370+
bus["bus_type"] = 4
371+
end
372+
cc = PowerModels.calc_connected_components(data)
373+
cc_ordered = sort(collect(cc); by=length)
374+
@test length(cc_ordered) == 0
375+
end
376+
377+
@testset "connected components with switches" begin
368378
data = PowerModels.parse_file("../test/data/matpower/case5_sw_nb.m")
369379
cc = PowerModels.calc_connected_components(data)
370380

@@ -374,7 +384,7 @@ end
374384
@test length(cc_ordered[1]) == 19
375385
end
376386

377-
@testset "connecected components with simplify network" begin
387+
@testset "connected components with simplify network" begin
378388
data = PowerModels.parse_file("../test/data/matpower/case7_tplgy.m")
379389
PowerModels.simplify_network!(data)
380390
cc = PowerModels.calc_connected_components(data)
@@ -467,7 +477,7 @@ end
467477

468478

469479
@testset "bus type corrections" begin
470-
@testset "no generator in connecected comp." begin
480+
@testset "no generator in connected comp." begin
471481
data = parse_file("../test/data/matpower/case7_tplgy.m")
472482

473483
data["gen"]["2"]["gen_status"] = 0

0 commit comments

Comments
 (0)