Skip to content

Commit f6d5dba

Browse files
authored
Merge pull request #105 from luke-kiernan/ABA-slack-bus
Fix issue #102
2 parents 8ce3289 + b30b6d0 commit f6d5dba

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/BA_ABA_matrices.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct ABA_Matrix{
8888
axes::Ax
8989
lookup::L
9090
ref_bus_positions::Set{Int}
91+
ref_bus_numbers::Set{Int}
9192
K::F
9293
radial_network_reduction::RadialNetworkReduction
9394
end
@@ -122,7 +123,11 @@ function ABA_Matrix(
122123
ABA = calculate_ABA_matrix(A, BA, ref_bus_positions)
123124

124125
bus_ax = [PSY.get_number(bus) for bus in buses]
125-
bus_ax_ = setdiff(bus_ax, ref_bus_positions)
126+
ref_bus_numbers = Set([
127+
PSY.get_number(bus) for bus in buses
128+
if PSY.get_bustype(bus) == PSY.ACBusTypes.REF
129+
])
130+
bus_ax_ = setdiff(bus_ax, ref_bus_numbers)
126131
axes = (bus_ax_, bus_ax_)
127132
bus_ax_ref = make_ax_ref(bus_ax_)
128133
lookup = (bus_ax_ref, bus_ax_ref)
@@ -136,6 +141,7 @@ function ABA_Matrix(
136141
axes,
137142
lookup,
138143
ref_bus_positions,
144+
ref_bus_numbers,
139145
K,
140146
rb,
141147
)
@@ -154,6 +160,7 @@ function factorize(ABA::ABA_Matrix{Ax, L, Nothing}) where {Ax, L <: NTuple{2, Di
154160
deepcopy(ABA.axes),
155161
deepcopy(ABA.lookup),
156162
deepcopy(ABA.ref_bus_positions),
163+
deepcopy(ABA.ref_bus_numbers),
157164
klu(ABA.data),
158165
deepcopy(ABA.radial_network_reduction),
159166
)
@@ -181,16 +188,20 @@ function Base.getindex(
181188
return A.data[bus_number, line_number]
182189
end
183190

191+
_is_ref_bus(bus::Int, ref_bus_numbers::Set{Int}) = (bus in ref_bus_numbers)
192+
_is_ref_bus(bus::PSY.ACBus, ref_bus_numbers::Set{Int}) =
193+
(PSY.get_number(bus) in ref_bus_numbers)
194+
_is_ref_bus(::Colon, ::Set{Int}) = false
184195
# get_index functions: ABA_Matrix stores a square matrix whose number of rows
185196
# and column is equal to the number of the system's buses minus the slack ones,
186-
# NOTE: bus_1, bus_2 are bus numbers not row and column indices!
197+
# NOTE: bus_1, bus_2 are bus numbers (or PSY.ACBus objects) not row and column indices!
187198
function Base.getindex(A::ABA_Matrix, bus_1, bus_2)
188-
if bus_1 in A.ref_bus_positions || bus_2 in A.ref_bus_positions
199+
if _is_ref_bus(bus_1, A.ref_bus_numbers) || _is_ref_bus(bus_2, A.ref_bus_numbers)
189200
err_msg = string(
190201
" Rows and columns related to slack buses are not defined for the ABA matrix. \n",
191202
"Indices must be referred to any bus number that is not a slack one. \n",
192203
"For the current Systems the reference slack buses are: ",
193-
collect(A.ref_bus_positions), ". \n")
204+
collect(A.ref_bus_numbers), ". \n")
194205
error(err_msg)
195206
else
196207
i, j = to_index(A, bus_1, bus_2)

test/test_BA_ABA_matrix.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
@testset "Test A, BA, and ABA matrix creation" begin
2-
# test on 5 and 14 bus system
3-
for name in ["c_sys5", "c_sys14"]
4-
sys = PSB.build_system(PSB.PSITestSystems, name)
2+
# test on 5 and 14 bus system, and RTS.
3+
for (category, name) in [
4+
(PSITestSystems, "c_sys5"),
5+
(PSITestSystems, "c_sys14"),
6+
(PSISystems, "RTS_GMLC_DA_sys"),
7+
]
8+
sys = PSB.build_system(category, name)
59
# at first let's see if factorization flag works
610
ABA_no_lu = ABA_Matrix(sys)
711
@test isnothing(ABA_no_lu.K)
@@ -38,7 +42,7 @@
3842
end
3943

4044
@testset "Test BA and ABA matrix indexing" begin
41-
sys = PSB.build_system(PSB.PSITestSystems, "c_sys5")
45+
sys = PSB.build_system(PSB.PSISystems, "RTS_GMLC_DA_sys")
4246

4347
# get the matrices
4448
ba = BA_Matrix(sys)
@@ -66,7 +70,8 @@ end
6670
end
6771

6872
# test if error is correctly thrown when ref bus is called
69-
rb = collect(ba.ref_bus_positions)[1]
73+
buses = get_components(PSY.ACBus, sys)
74+
rb = [bus for bus in buses if get_bustype(bus) == PSY.ACBusTypes.REF][1]
7075
@test_throws ErrorException aba[rb, :]
7176
end
7277

0 commit comments

Comments
 (0)