Skip to content

Commit

Permalink
updated functions and added distributed slakc bus calculation for vPTDF
Browse files Browse the repository at this point in the history
  • Loading branch information
alefcastelli committed Aug 2, 2023
1 parent c4dde3f commit 00c8b20
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function make_entries_zero!(vector::Vector{Float64}, tol::Float64)
vector[i] = 0.0
end
end
return vector
return
end

"""
Expand Down
33 changes: 26 additions & 7 deletions src/virtual_ptdf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ function VirtualPTDF(
tol::Float64 = eps(),
max_cache_size::Int = MAX_CACHE_SIZE_MiB,
persistent_lines::Vector{String} = String[])
if length(dist_slack) != 0
@info "Distributed bus"
end

#Get axis names
line_ax = [PSY.get_name(branch) for branch in branches]
Expand Down Expand Up @@ -171,19 +174,35 @@ function _getindex(
# Needs improvement
valid_ix = vptdf.valid_ix
lin_solve = KLU.solve!(vptdf.K, Vector(vptdf.BA[valid_ix, row]))
buscount = size(vptdf, 1)

# ! missing dist_slack case

for i in eachindex(valid_ix)
vptdf.temp_data[valid_ix[i]] = lin_solve[i]
if !isempty(vptdf.dist_slack) && length(vptdf.ref_bus_positions) != 1
error(
"Distibuted slack is not supported for systems with multiple reference buses.",
)
elseif isempty(vptdf.dist_slack) && length(vptdf.ref_bus_positions) < buscount
for i in eachindex(valid_ix)
vptdf.temp_data[valid_ix[i]] = lin_solve[i]
end
vptdf.cache[row] = deepcopy(vptdf.temp_data)
elseif length(vptdf.dist_slack) == buscount
for i in eachindex(valid_ix)
vptdf.temp_data[valid_ix[i]] = lin_solve[i]
end
slack_array = vptdf.dist_slack / sum(vptdf.dist_slack)
slack_array = reshape(slack_array, buscount)
vptdf.cache[row] =
deepcopy(vptdf.temp_data .- dot(vptdf.temp_data, slack_array))
else
error("Distributed bus specification doesn't match the number of buses.")
end

# add slack bus value (zero) and make copy of temp into the cache
if get_tol(vptdf) > eps()
vptdf.cache[row] = make_entries_zero!(deepcopy(vptdf.temp_data), get_tol(vptdf))
else
vptdf.cache[row] = deepcopy(vptdf.temp_data)
# vptdf.cache[row] = make_entries_zero!(deepcopy(vptdf.temp_data), get_tol(vptdf))
make_entries_zero!(vptdf.cache[row], get_tol(vptdf))
end

return vptdf.cache[row][column]
end
end
Expand Down
14 changes: 14 additions & 0 deletions test/test_virtual_ptdf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ end
@test vptdf.lookup[1][l] keys(vptdf.cache.temp_cache)
end
end

@testset "Test virtual PTDF with distributed slack" begin
# get 5 bus system
sys = PSB.build_system(PSB.PSITestSystems, "c_sys5")
# compute full PTDF
ptdf = PTDF(sys; dist_slack = slack_array)
# compute each row of the virtual PTDF and compare values
vptdf = VirtualPTDF(sys; dist_slack = slack_array)
for row in 1:size(ptdf.data, 2)
# evaluate the column (just needs one element)
vptdf[row, 1]
@assert isapprox(vptdf.cache[row], ptdf[row, :], atol = 1e-5)
end
end

0 comments on commit 00c8b20

Please sign in to comment.