Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bug on PTDF calculation with distributed slack #47

Merged
merged 11 commits into from
Aug 2, 2023
14 changes: 10 additions & 4 deletions src/ptdf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ function _calculate_PTDF_matrix_KLU(
@info "Distributed bus"
copyto!(PTDFm_t, BA)
PTDFm_t[valid_ix, :] = KLU.solve!(K, PTDFm_t[valid_ix, :])
PTDFm_t[ref_bus_positions, :] .= 0.0
PTDFm_t[collect(ref_bus_positions), :] .= 0.0
slack_array = dist_slack / sum(dist_slack)
slack_array = reshape(slack_array, 1, buscount)
return PTDFm_t - (PTDFm_t * slack_array) * ones(1, buscount)
return PTDFm_t - ones(buscount, 1) * (slack_array * PTDFm_t)
else
error("Distributed bus specification doesn't match the number of buses.")
end
Expand Down Expand Up @@ -246,10 +246,16 @@ function _calculate_PTDF_matrix_DENSE(
return PTDFm_t
elseif length(dist_slack) == buscount
@info "Distributed bus"
PTDFm_t[valid_ixs, :] = gemm(
'N',
'N',
getri!(ABA, bipiv),
BA[valid_ixs, :],
)
slack_array = dist_slack / sum(dist_slack)
slack_array = reshape(slack_array, buscount, 1)
slack_array = reshape(slack_array, 1, buscount)
return PTDFm_t -
gemm('N', 'N', gemm('N', 'N', PTDFm, slack_array), ones(1, buscount))
gemm('N', 'N', ones(buscount, 1), gemm('N', 'N', slack_array, PTDFm_t))
else
error("Distributed bus specification doesn't match the number of buses.")
end
Expand Down
19 changes: 19 additions & 0 deletions test/test_ptdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,22 @@ end
# Test Throw error when isolated buses are connected to an available branch
@test_throws IS.ConflictingInputsError ptdf_2 = PTDF(sys_2)
end

@test "PTDF matrices with distributed slack" begin
sys5 = PSB.build_system(PSB.PSITestSystems, "c_sys5")

bus_number = length(PNM.get_buses(sys))

dist_slack = 1/bus_number*ones(bus_number)
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
slack_array = dist_slack / sum(dist_slack)
slack_array = reshape(slack_array, 1, buscount)

P5_1 = PTDF(sys5; slack_array=slack_array, linear_solver = "KLU")
P5_2 = PTDF(sys5; slack_array=slack_array, linear_solver = "Dense")
P5_3 = PTDF(sys5; slack_array=slack_array, linear_solver = "MKLPardiso")
jd-lara marked this conversation as resolved.
Show resolved Hide resolved

@assert isapprox(P5_1.data, P5_2.data, atol=1e-5)
@assert isapprox(P5_1.data, P5_3.data, atol=1e-5)
@assert isapprox(P5_2.data, P5_3.data, atol=1e-5)

jd-lara marked this conversation as resolved.
Show resolved Hide resolved
end
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
Loading