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

PSI integration part 2 #69

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/BA_ABA_matrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Structure containing the BA matrix and other relevant data.
- `ref_bus_positions::Set{Int}`:
Set containing the indexes of the columns of the BA matrix corresponding
to the refence buses
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand All @@ -25,7 +25,7 @@ struct BA_Matrix{Ax, L <: NTuple{2, Dict}} <: PowerNetworkMatrix{Float64}
axes::Ax
lookup::L
ref_bus_positions::Set{Int}
radial_branches::RadialBranches
radial_branches::RadialNetworkReduction
jd-lara marked this conversation as resolved.
Show resolved Hide resolved
end

"""
Expand All @@ -40,9 +40,9 @@ Build the BA matrix from a given System
"""
function BA_Matrix(sys::PSY.System; reduce_radial_branches::Bool = false)
if reduce_radial_branches
rb = RadialBranches(IncidenceMatrix(sys))
rb = RadialNetworkReduction(IncidenceMatrix(sys))
else
rb = RadialBranches()
rb = RadialNetworkReduction()
end
branches = get_ac_branches(sys, rb.radial_branches)
buses = get_buses(sys, rb.bus_reduction_map)
Expand Down Expand Up @@ -75,7 +75,7 @@ Structure containing the ABA matrix and other relevant data.
to the refence buses
- `K<:Union{Nothing, KLU.KLUFactorization{Float64, Int}}`:
either nothing or a container for KLU factorization matrices (LU factorization)
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand All @@ -89,7 +89,7 @@ struct ABA_Matrix{
lookup::L
ref_bus_positions::Set{Int}
K::F
radial_branches::RadialBranches
radial_branches::RadialNetworkReduction
end

"""
Expand All @@ -108,9 +108,9 @@ function ABA_Matrix(
reduce_radial_branches::Bool = false,
)
if reduce_radial_branches
rb = RadialBranches(IncidenceMatrix(sys))
rb = RadialNetworkReduction(IncidenceMatrix(sys))
else
rb = RadialBranches()
rb = RadialNetworkReduction()
end

branches = get_ac_branches(sys, rb.radial_branches)
Expand Down
4 changes: 2 additions & 2 deletions src/PowerNetworkMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export IncidenceMatrix
export is_factorized
export LODF
export PTDF
export RadialBranches
export RadialNetworkReduction
export to_hdf5
export validate_connectivity
export VirtualLODF
Expand Down Expand Up @@ -50,7 +50,7 @@ import Pardiso
include("PowerNetworkMatrix.jl")
include("incedence_matrix.jl")
include("adjacency_matrix.jl")
include("radial_braches.jl")
include("network_radial_reduction.jl")
include("common.jl")
include("BA_ABA_matrices.jl")
include("definitions.jl")
Expand Down
4 changes: 2 additions & 2 deletions src/incedence_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Incidence matrix: shows connection between buses, defining lines
and buses with their enumerated indexes.
- `ref_bus_positions::Set{Int}`:
Vector containing the indices of the reference slack buses.
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand Down Expand Up @@ -52,7 +52,7 @@ values.

# Arguments
- `sys::PSY.System`: the PowerSystem system to consider
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand Down
20 changes: 10 additions & 10 deletions src/lodf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ of how a change in a line's flow affects the flows on other lines in the system.
- `tol::Base.RefValue{Float64}`:
tolerance used for sparsifying the matrix (dropping element whose
absolute value is below this threshold).
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand All @@ -24,7 +24,7 @@ struct LODF{Ax, L <: NTuple{2, Dict}, M <: AbstractArray{Float64, 2}} <:
axes::Ax
lookup::L
tol::Base.RefValue{Float64}
radial_branches::RadialBranches
radial_branches::RadialNetworkReduction
end

function _buildlodf(
Expand Down Expand Up @@ -268,7 +268,7 @@ Builds the LODF matrix given the data of branches and buses of the system.
Default solver: "KLU".
- `tol::Float64`:
Tolerance to eliminate entries in the LODF matrix (default eps())
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the ma
"""
Expand All @@ -277,7 +277,7 @@ function LODF(
buses::Vector{PSY.ACBus};
linear_solver::String = "KLU",
tol::Float64 = eps(),
radial_branches::RadialBranches = RadialBranches(),
radial_branches::RadialNetworkReduction = RadialNetworkReduction(),
)

# get axis names
Expand Down Expand Up @@ -323,9 +323,9 @@ function LODF(
kwargs...,
)
if reduce_radial_branches
rb = RadialBranches(IncidenceMatrix(sys))
rb = RadialNetworkReduction(IncidenceMatrix(sys))
else
rb = RadialBranches()
rb = RadialNetworkReduction()
end
branches = get_ac_branches(sys, rb.radial_branches)
buses = get_buses(sys, rb.bus_reduction_map)
Expand Down Expand Up @@ -387,7 +387,7 @@ function LODF(
ax_ref = make_ax_ref(sort!(collect(radial_branches.meshed_branches)))
else
if isempty(PTDFm.radial_branches)
radial_branches = RadialBranches()
radial_branches = RadialNetworkReduction()
A_matrix = A.data
ax_ref = make_ax_ref(A.axes[1])
else
Expand Down Expand Up @@ -447,7 +447,7 @@ function LODF(
validate_linear_solver(linear_solver)
ax_ref = make_ax_ref(A.axes[1])
if reduce_radial_branches
# BA and ABA must contain the same, non-empty RadialBranches stucture
# BA and ABA must contain the same, non-empty RadialNetworkReduction stucture
if !isempty(BA.radial_branches) && !isempty(ABA.radial_branches) &&
isequal(BA.radial_branches, ABA.radial_branches)
radial_branches = BA.radial_branches
Expand All @@ -461,9 +461,9 @@ function LODF(
radial_branches.meshed_branches,
)
else
# BA and ABA must contain the same, empty RadialBranches stucture
# BA and ABA must contain the same, empty RadialNetworkReduction stucture
if isempty(BA.radial_branches) && isempty(ABA.radial_branches)
radial_branches = RadialBranches()
radial_branches = RadialNetworkReduction()
A_matrix = A.data
else
error(
Expand Down
43 changes: 27 additions & 16 deletions src/radial_braches.jl → src/network_radial_reduction.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
struct RadialBranches
struct RadialNetworkReduction
bus_reduction_map::Dict{Int, Set{Int}}
reverse_bus_search_map::Dict{Int, Int}
radial_branches::Set{String}
meshed_branches::Set{String}
end

get_bus_reduction_map(rb::RadialBranches) = rb.bus_reduction_map
get_reverse_bus_search_map(rb::RadialBranches) = rb.reverse_bus_search_map
get_radial_branches(rb::RadialBranches) = rb.radial_branches
get_meshed_branches(rb::RadialBranches) = rb.meshed_branches
get_bus_reduction_map(rb::RadialNetworkReduction) = rb.bus_reduction_map
get_reverse_bus_search_map(rb::RadialNetworkReduction) = rb.reverse_bus_search_map

Check warning on line 9 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L9

Added line #L9 was not covered by tests
get_radial_branches(rb::RadialNetworkReduction) = rb.radial_branches
get_meshed_branches(rb::RadialNetworkReduction) = rb.meshed_branches

Check warning on line 11 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L11

Added line #L11 was not covered by tests

function Base.isempty(rb::RadialBranches)
function Base.isempty(rb::RadialNetworkReduction)
if !isempty(rb.bus_reduction_map)
return false
end
Expand All @@ -25,12 +25,12 @@
return true
end

function RadialBranches(;
function RadialNetworkReduction(;
bus_reduction_map::Dict{Int, Set{Int}} = Dict{Int, Set{Int}}(),
reverse_bus_search_map::Dict{Int, Int} = Dict{Int, Int}(),
radial_branches::Set{String} = Set{String}(),
meshed_branches::Set{String} = Set{String}())
return RadialBranches(
return RadialNetworkReduction(
bus_reduction_map,
reverse_bus_search_map,
radial_branches,
Expand All @@ -46,7 +46,7 @@
- `A::IncidenceMatrix`: IncidenceMatrix

"""
function RadialBranches(A::IncidenceMatrix)
function RadialNetworkReduction(A::IncidenceMatrix)
return calculate_radial_branches(A.data, A.lookup[1], A.lookup[2], A.ref_bus_positions)
end

Expand All @@ -58,8 +58,8 @@
- `sys::PSY.System`: System Data
"""

function RadialBranches(sys::PSY.System)
return RadialBranches(IncidenceMatrix(sys))
function RadialNetworkReduction(sys::PSY.System)
return RadialNetworkReduction(IncidenceMatrix(sys))

Check warning on line 62 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L61-L62

Added lines #L61 - L62 were not covered by tests
end

function _find_upstream_bus(
Expand Down Expand Up @@ -205,7 +205,7 @@
end
push!(meshed_branches, k)
end
return RadialBranches(
return RadialNetworkReduction(
bus_reduction_map_index,
reverse_bus_search_map,
radial_branches,
Expand All @@ -217,23 +217,34 @@
Interface to obtain the parent bus number of a reduced bus when radial branches are eliminated

# Arguments
- `rb::RadialBranches`: RadialBranches object
- `rb::RadialNetworkReduction`: RadialNetworkReduction object
- `bus_number::Int`: Bus number of the reduced bus
"""
function get_mapped_bus_number(rb::RadialBranches, bus_number::Int)
function get_mapped_bus_number(rb::RadialNetworkReduction, bus_number::Int)

Check warning on line 223 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L223

Added line #L223 was not covered by tests
if isempty(rb)
return bus_number
end
return get(rb.reverse_bus_search_map, bus_number, bus_number)
end

"""
Interface to obtain the parent bus number of a reduced bus when radial branches are eliminated

# Arguments
- `rb::RadialNetworkReduction`: RadialNetworkReduction object
- `bus::ACBus`: Reduced bus
"""
function get_mapped_bus_number(rb::RadialNetworkReduction, bus::PSY.ACBus)
return get_mapped_bus_number(rb, PSY.get_number(bus))

Check warning on line 238 in src/network_radial_reduction.jl

View check run for this annotation

Codecov / codecov/patch

src/network_radial_reduction.jl#L237-L238

Added lines #L237 - L238 were not covered by tests
end

##############################################################################
########################### Auxiliary functions ##############################
##############################################################################

function isequal(
rb1::RadialBranches,
rb2::RadialBranches,
rb1::RadialNetworkReduction,
rb2::RadialNetworkReduction,
)
for field in fieldnames(typeof(rb1))
if getfield(rb1, field) != getfield(rb2, field)
Expand Down
14 changes: 7 additions & 7 deletions src/ptdf_calculations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ The PTDF struct is indexed using the Bus numbers and Branch names.
- `tol::Base.RefValue{Float64}`:
tolerance used for sparsifying the matrix (dropping element whose
absolute value is below this threshold).
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand All @@ -33,7 +33,7 @@ struct PTDF{Ax, L <: NTuple{2, Dict}, M <: AbstractArray{Float64, 2}} <:
subnetworks::Dict{Int, Set{Int}}
ref_bus_positions::Set{Int}
tol::Base.RefValue{Float64}
radial_branches::RadialBranches
radial_branches::RadialNetworkReduction
end

"""
Expand Down Expand Up @@ -377,7 +377,7 @@ Builds the PTDF matrix from a group of branches and buses. The return is a PTDF
Linear solver to be used. Options are "Dense", "KLU" and "MKLPardiso
- `tol::Float64`:
Tolerance to eliminate entries in the PTDF matrix (default eps())
- `radial_branches::RadialBranches`:
- `radial_branches::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the ma
"""
Expand All @@ -387,7 +387,7 @@ function PTDF(
dist_slack::Vector{Float64} = Float64[],
linear_solver::String = "KLU",
tol::Float64 = eps(),
radial_branches::RadialBranches = RadialBranches(),
radial_branches::RadialNetworkReduction = RadialNetworkReduction(),
)
validate_linear_solver(linear_solver)

Expand Down Expand Up @@ -450,7 +450,7 @@ function PTDF(
A = IncidenceMatrix(sys)
dist_slack, rb = redistribute_dist_slack(dist_slack, A)
else
rb = RadialBranches()
rb = RadialNetworkReduction()
end
branches = get_ac_branches(sys, rb.radial_branches)
buses = get_buses(sys, rb.bus_reduction_map)
Expand Down Expand Up @@ -504,7 +504,7 @@ function PTDF(
lookup = BA.lookup
else
if isempty(BA.radial_branches)
radial_branches = RadialBranches()
radial_branches = RadialNetworkReduction()
A_matrix = A.data
axes = (A.axes[2], A.axes[1])
lookup = (A.lookup[2], A.lookup[1])
Expand Down Expand Up @@ -583,7 +583,7 @@ function redistribute_dist_slack(
A::IncidenceMatrix,
)
dist_slack1 = deepcopy(dist_slack)
rb = RadialBranches(A)
rb = RadialNetworkReduction(A)
# if original length of dist_slack is correct
if length(dist_slack) == size(A.data, 2)
for i in keys(rb.bus_reduction_map)
Expand Down
10 changes: 9 additions & 1 deletion src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ function from_hdf5(::Type{PTDF}, filename::AbstractString)
subnetworks[key] = Set(subnetworks_matrix[:, col])
end

PTDF(data, axes, lookup, subnetworks, ref_bus_positions, tol, RadialBranches())
PTDF(
data,
axes,
lookup,
subnetworks,
ref_bus_positions,
tol,
RadialNetworkReduction(),
)
end
end

Expand Down
Loading
Loading