Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alefcastelli committed Sep 21, 2023
1 parent 5ff0662 commit 2355d9e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 138 deletions.
24 changes: 0 additions & 24 deletions src/lodf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,6 @@ function _calculate_LODF_matrix_KLU(
return lodf_t
end

# ! used only in testing, can be removed in the future
function _calculate_LODF_matrix_KLU2(
a::SparseArrays.SparseMatrixCSC{Int8, Int},
ptdf::Matrix{Float64},
)
ptdf_denominator_t = a * ptdf
linecount = size(ptdf, 2)
lodf_t = zeros(linecount, linecount)
for i in 1:linecount
for j in 1:linecount
if i == j
lodf_t[i, i] = -1.0
else
if (1.0 - ptdf_denominator_t[i, i]) < 1.0E-06
lodf_t[i, j] = ptdf_denominator_t[i, j]
else
lodf_t[i, j] = ptdf_denominator_t[i, j] / (1 - ptdf_denominator_t[i, i])
end
end
end
end
return lodf_t
end

function _calculate_LODF_matrix_DENSE(
a::SparseArrays.SparseMatrixCSC{Int8, Int},
ptdf::Matrix{Float64},
Expand Down
13 changes: 2 additions & 11 deletions test/test_BA_ABA_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,8 @@ end

# test if error is correctly thrown when ref bus is called
rb = collect(ba.ref_bus_positions)[1]
test_val = false
try
aba[rb, :]
catch err
if err isa ErrorException
test_val = true
else
error("Expected an ErrorException but was not thrown.")
end
end
@test test_val
@test_throws ErrorException aba[rb, :]

end

@testset "Test show for A, BA and ABA matrix" begin
Expand Down
35 changes: 3 additions & 32 deletions test/test_lodf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
@test isapprox(maximum(L5.data), maximum(Lodf_5), atol = 1e-3)
@test isapprox(L5[branches_5[1], branches_5[2]], 0.3447946513849093)

# get LODF with second method
a = IncidenceMatrix(sys5)
ptdf = PTDF(sys5)
lodf_t_3 = PNM._calculate_LODF_matrix_KLU2(a.data, ptdf.data)
@test isapprox(lodf_t_3, L5.data, atol = 1e-5)

# get LODF with system
L5NS = LODF(sys5)
@test getindex(L5NS, "5", "6") - -0.3071 <= 1e-4
Expand Down Expand Up @@ -57,37 +51,14 @@
end

# test if error is thrown in case other linear solvers are called
test_value = false
try
L5NS_from_ba_aba = LODF(A, ABA, BA; linear_solver = "Dense")
catch err
if err isa ErrorException
test_value = true
end
end
@test test_value
@test_throws ErrorException LODF(A, ABA, BA; linear_solver = "Dense")

test_value = false
try
L5NS_from_ba_aba = LODF(A, P5; linear_solver = "XXX")
catch err
if err isa ErrorException
test_value = true
end
end
@test test_value
@test_throws ErrorException LODF(A, P5; linear_solver = "XXX")

# test if error is thrown in case `tol` is defined in PTDF
P5 = PTDF(sys5; tol = 1e-3)
test_value = false
try
L5NS_from_ptdf = LODF(A, P5)
catch err
if err isa ErrorException
test_value = true
end
end
@test test_value
@test_throws ErrorException L5NS_from_ptdf = LODF(A, P5)

# get 14 bus system
buses_14 = nodes14()
Expand Down
29 changes: 3 additions & 26 deletions test/test_ptdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,37 +230,14 @@ end
dist_slack = 1 / buscount * ones(buscount)
slack_array = dist_slack / sum(dist_slack)

test_val1 = false
try
ptdf_1 = PTDF(sys; dist_slack = slack_array)
catch err
if err isa ErrorException
test_val1 = true
else
error(
"Error was not thrown for PTDF with distributed slack bus and more than one reference bus.",
)
end
end
@test_throws ErrorException ptdf_1 = PTDF(sys; dist_slack = slack_array)

# incorrect dist_slack arrya length
sys5 = PSB.build_system(PSB.PSITestSystems, "c_sys5")
buscount = length(PNM.get_buses(sys5)) + 1
dist_slack = 1 / buscount * ones(buscount)
slack_array = dist_slack / sum(dist_slack)
test_val2 = false
try
ptdf_2 = PTDF(sys5; dist_slack = slack_array)
catch err
if err isa ErrorException
test_val2 = true
else
error(
"Error was not thrown for PTDF with distributed slack array of incorrect length.",
)
end
end

@test test_val1
@test test_val2
@test_throws ErrorException ptdf_2 = PTDF(sys5; dist_slack = slack_array)

end
10 changes: 1 addition & 9 deletions test/test_virtual_lodf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,7 @@ end
length(axes(vlodf)[1]) * length(axes(vlodf)[2])

# check if error is correctly thrown
test_value = false
try
vlodf[1, 1] = 1
catch err
if err isa ErrorException
test_value = true
end
end
@test test_value
@test_throws ErrorException vlodf[1, 1] = 1

# test show
test_value = false
Expand Down
41 changes: 5 additions & 36 deletions test/test_virtual_ptdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,41 +101,18 @@ end
dist_slack = 1 / buscount * ones(buscount)
slack_array = dist_slack / sum(dist_slack)

test_val1 = false
try
ptdf_1 = VirtualPTDF(sys; dist_slack = slack_array)
ptdf_1[1, 1]
catch err
if err isa ErrorException
test_val1 = true
else
error(
"Error was not thrown for PTDF with distributed slack bus and more than one reference bus.",
)
end
end
ptdf_1 = VirtualPTDF(sys; dist_slack = slack_array)
@test_throws ErrorException ptdf_1[1, 1]

# incorrect dist_slack array length
sys5 = PSB.build_system(PSB.PSITestSystems, "c_sys5")
buscount = length(PNM.get_buses(sys5)) + 1
dist_slack = 1 / buscount * ones(buscount)
slack_array = dist_slack / sum(dist_slack)
test_val2 = false
try
ptdf_2 = VirtualPTDF(sys5; dist_slack = slack_array)
ptdf_2[1, 1]
catch err
if err isa ErrorException
test_val2 = true
else
error(
"Error was not thrown for PTDF with distributed slack array of incorrect length.",
)
end
end
ptdf_2 = VirtualPTDF(sys5; dist_slack = slack_array)
@test_throws ErrorException ptdf_2[1, 1]

@test test_val1
@test test_val2
end

@testset "Test Virtual PTDF auxiliary functions" begin
Expand All @@ -150,15 +127,7 @@ end
@test isempty(vptdf) == false

# check if error is correctly thrown
test_value = false
try
vptdf[1, 1] = 1
catch err
if err isa ErrorException
test_value = true
end
end
@test test_value
@test_throws ErrorException vptdf[1, 1] = 1

# get the rows and full PTDF matrix, test get_ptdf_data
ptdf = PTDF(sys)
Expand Down

0 comments on commit 2355d9e

Please sign in to comment.