Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-lara committed Jan 16, 2024
1 parent b628bdd commit a1d636f
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 70 deletions.
14 changes: 7 additions & 7 deletions src/BA_ABA_matrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Structure containing the BA matrix and other relevant data.
with the names of buses and branches
- `ref_bus_positions::Set{Int}`:
Set containing the indexes of the columns of the BA matrix corresponding
to the refence buses
- `radial_branches::RadialNetworkReduction`:
to the reference buses
- `radial_network_reduction::RadialNetworkReduction`:
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::RadialNetworkReduction
radial_network_reduction::RadialNetworkReduction
end

"""
Expand Down Expand Up @@ -72,10 +72,10 @@ Structure containing the ABA matrix and other relevant data.
with the number of the buses.
- `ref_bus_positions::Set{Int}`:
Vector containing the indexes of the columns of the BA matrix corresponding
to the refence buses
to the reference buses
- `K<:Union{Nothing, KLU.KLUFactorization{Float64, Int}}`:
either nothing or a container for KLU factorization matrices (LU factorization)
- `radial_branches::RadialNetworkReduction`:
- `radial_network_reduction::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::RadialNetworkReduction
radial_network_reduction::RadialNetworkReduction
end

"""
Expand Down Expand Up @@ -155,7 +155,7 @@ function factorize(ABA::ABA_Matrix{Ax, L, Nothing}) where {Ax, L <: NTuple{2, Di
deepcopy(ABA.lookup),
deepcopy(ABA.ref_bus_positions),
klu(ABA.data),
deepcopy(ABA.radial_branches),
deepcopy(ABA.radial_network_reduction),
)
return ABA_lu
end
Expand Down
2 changes: 1 addition & 1 deletion src/adjacency_matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The AdjacencyMatrix Struct is indexed using the Bus Numbers, no need for them to
with the names of branches and buses
- `ref_bus_positions::Set{Int}`:
Vector containing the indexes of the columns of the BA matrix corresponding
to the refence buses
to the reference buses
"""
struct AdjacencyMatrix{Ax, L <: NTuple{2, Dict}} <: PowerNetworkMatrix{Int8}
data::SparseArrays.SparseMatrixCSC{Int8, Int}
Expand Down
2 changes: 1 addition & 1 deletion src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Evaluates the transposed BA matrix given the System's banches, reference bus pos
vector containing the branches of the considered system (should be AC branches).
- `ref_bus_positions::Set{Int}`:
Vector containing the indexes of the columns of the BA matrix corresponding
to the refence buses
to the reference buses
- `bus_lookup::Dict{Int, Int}`:
dictionary mapping the bus numbers with their enumerated indexes.
"""
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::RadialNetworkReduction`:
- `radial_network_reduction::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::RadialNetworkReduction`:
- `radial_network_reduction::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand Down
63 changes: 35 additions & 28 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::RadialNetworkReduction`:
- `radial_network_reduction::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::RadialNetworkReduction
radial_network_reduction::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::RadialNetworkReduction`:
- `radial_network_reduction::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::RadialNetworkReduction = RadialNetworkReduction(),
radial_network_reduction::RadialNetworkReduction = RadialNetworkReduction(),
)

# get axis names
Expand All @@ -292,14 +292,20 @@ function LODF(

if tol > eps()
lodf_t = _buildlodf(a, ptdf_t, linear_solver)
return LODF(sparsify(lodf_t, tol), axes, look_up, Ref(tol), radial_branches)
return LODF(
sparsify(lodf_t, tol),
axes,
look_up,
Ref(tol),
radial_network_reduction,
)
else
return LODF(
_buildlodf(a, ptdf_t, linear_solver),
axes,
look_up,
Ref(tol),
radial_branches,
radial_network_reduction,
)
end
end
Expand Down Expand Up @@ -329,7 +335,7 @@ function LODF(
end
branches = get_ac_branches(sys, rb.radial_branches)
buses = get_buses(sys, rb.bus_reduction_map)
return LODF(branches, buses; radial_branches = rb, kwargs...)
return LODF(branches, buses; radial_network_reduction = rb, kwargs...)
end

"""
Expand Down Expand Up @@ -373,26 +379,26 @@ function LODF(
end

if reduce_radial_branches
if !isempty(PTDFm.radial_branches)
radial_branches = PTDFm.radial_branches
@info "Non-empty `radial_branches` field found in PTDF matrix. LODF is evaluated considering radial branches and leaf nodes removed."
if !isempty(PTDFm.radial_network_reduction)
radial_network_reduction = PTDFm.radial_network_reduction
@info "Non-empty `radial_network_reduction` field found in PTDF matrix. LODF is evaluated considering radial branches and leaf nodes removed."
else
error("PTDF has empty `radial_branches` field.")
error("PTDF has empty `radial_network_reduction` field.")

Check warning on line 386 in src/lodf_calculations.jl

View check run for this annotation

Codecov / codecov/patch

src/lodf_calculations.jl#L386

Added line #L386 was not covered by tests
end
A_matrix = reduce_A_matrix(
A,
radial_branches.bus_reduction_map,
radial_branches.meshed_branches,
radial_network_reduction.bus_reduction_map,
radial_network_reduction.meshed_branches,
)
ax_ref = make_ax_ref(sort!(collect(radial_branches.meshed_branches)))
ax_ref = make_ax_ref(sort!(collect(radial_network_reduction.meshed_branches)))
else
if isempty(PTDFm.radial_branches)
radial_branches = RadialNetworkReduction()
if isempty(PTDFm.radial_network_reduction)
radial_network_reduction = RadialNetworkReduction()
A_matrix = A.data
ax_ref = make_ax_ref(A.axes[1])
else
error(
"Field `radial_branches` in PTDF must be empty if `reduce_radial_branches` is not true.",
"Field `radial_network_reduction` in PTDF must be empty if `reduce_radial_network_reduction` is not true.",
)
end
end
Expand All @@ -404,15 +410,15 @@ function LODF(
(A.axes[1], A.axes[1]),
(ax_ref, ax_ref),
Ref(tol),
radial_branches,
radial_network_reduction,
)
end
return LODF(
_buildlodf(A_matrix, PTDFm_data, linear_solver),
(A.axes[1], A.axes[1]),
(ax_ref, ax_ref),
Ref(tol),
radial_branches,
radial_network_reduction,
)
end

Expand Down Expand Up @@ -448,22 +454,23 @@ function LODF(
ax_ref = make_ax_ref(A.axes[1])
if reduce_radial_branches
# 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
if !isempty(BA.radial_network_reduction) &&
!isempty(ABA.radial_network_reduction) &&
isequal(BA.radial_network_reduction, ABA.radial_network_reduction)
radial_network_reduction = BA.radial_network_reduction
@info "Non-empty `radial_branches` field found in BA and ABA matrix. LODF is evaluated considering radial branches and leaf nodes removed."
else
error("Mismatch in `radial_branches` field between BA and ABA matrices.")
end
A_matrix = reduce_A_matrix(
A,
radial_branches.bus_reduction_map,
radial_branches.meshed_branches,
radial_network_reduction.bus_reduction_map,
radial_network_reduction.meshed_branches,
)
else
# BA and ABA must contain the same, empty RadialNetworkReduction stucture
if isempty(BA.radial_branches) && isempty(ABA.radial_branches)
radial_branches = RadialNetworkReduction()
if isempty(BA.radial_network_reduction) && isempty(ABA.radial_network_reduction)
radial_network_reduction = RadialNetworkReduction()
A_matrix = A.data
else
error(
Expand All @@ -479,15 +486,15 @@ function LODF(
(A.axes[1], A.axes[1]),
(ax_ref, ax_ref),
Ref(tol),
radial_branches,
radial_network_reduction,
)
end
return LODF(
_buildlodf(A_matrix, ABA.K, BA.data, A.ref_bus_positions, linear_solver),
(A.axes[1], A.axes[1]),
(ax_ref, ax_ref),
Ref(tol),
radial_branches,
radial_network_reduction,
)
end

Expand Down
2 changes: 1 addition & 1 deletion src/network_radial_reduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ ignored in the models by exploring the structure of the incidence matrix
- `bus_map::Dict{Int, Int}`: Map of Bus Name to Matrix Index
- `ref_bus_positions::Set{Int}`:
Set containing the indexes of the columns of the BA matrix corresponding
to the refence buses
to the reference buses
"""
function calculate_radial_branches(
A::SparseArrays.SparseMatrixCSC{Int8, Int64},
Expand Down
44 changes: 29 additions & 15 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::RadialNetworkReduction`:
- `radial_network_reduction::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::RadialNetworkReduction
radial_network_reduction::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::RadialNetworkReduction`:
- `radial_network_reduction::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::RadialNetworkReduction = RadialNetworkReduction(),
radial_network_reduction::RadialNetworkReduction = RadialNetworkReduction(),
)
validate_linear_solver(linear_solver)

Expand Down Expand Up @@ -418,10 +418,18 @@ function PTDF(
subnetworks,
ref_bus_positions,
Ref(tol),
radial_branches,
radial_network_reduction,
)
end
return PTDF(S, axes, look_up, subnetworks, ref_bus_positions, Ref(tol), radial_branches)
return PTDF(
S,
axes,
look_up,
subnetworks,
ref_bus_positions,
Ref(tol),
radial_network_reduction,
)
end

"""
Expand Down Expand Up @@ -454,7 +462,13 @@ function PTDF(
end
branches = get_ac_branches(sys, rb.radial_branches)
buses = get_buses(sys, rb.bus_reduction_map)
return PTDF(branches, buses; dist_slack = dist_slack, radial_branches = rb, kwargs...)
return PTDF(
branches,
buses;
dist_slack = dist_slack,
radial_network_reduction = rb,
kwargs...,
)
end

"""
Expand Down Expand Up @@ -489,22 +503,22 @@ function PTDF(
validate_linear_solver(linear_solver)
@warn "PTDF creates via other matrices doesn't compute the subnetworks"
if reduce_radial_branches
if !isempty(BA.radial_branches)
radial_branches = BA.radial_branches
if !isempty(BA.radial_network_reduction)
radial_network_reduction = BA.radial_network_reduction
@info "Non-empty `radial_branches` field found in BA matrix. PTDF is evaluated considering radial branches and leaf nodes removed."
else
error("BA has empty `radial_branches` field.")
end
A_matrix = reduce_A_matrix(
A,
radial_branches.bus_reduction_map,
radial_branches.meshed_branches,
radial_network_reduction.bus_reduction_map,
radial_network_reduction.meshed_branches,
)
axes = BA.axes
lookup = BA.lookup
else
if isempty(BA.radial_branches)
radial_branches = RadialNetworkReduction()
if isempty(BA.radial_network_reduction)
radial_network_reduction = RadialNetworkReduction()
A_matrix = A.data
axes = (A.axes[2], A.axes[1])
lookup = (A.lookup[2], A.lookup[1])
Expand All @@ -529,7 +543,7 @@ function PTDF(
Dict{Int, Set{Int}}(),
BA.ref_bus_positions,
Ref(tol),
radial_branches,
radial_network_reduction,
)
else
return PTDF(
Expand All @@ -539,7 +553,7 @@ function PTDF(
Dict{Int, Set{Int}}(),
BA.ref_bus_positions,
Ref(tol),
radial_branches,
radial_network_reduction,
)
end
end
Expand Down
Loading

0 comments on commit a1d636f

Please sign in to comment.