Skip to content

Commit

Permalink
Merge pull request #69 from NREL-Sienna/jd/psi_integrationv2
Browse files Browse the repository at this point in the history
PSI integration part 2
  • Loading branch information
jd-lara authored Jan 16, 2024
2 parents 69cd65b + a1d636f commit 2c988a8
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 111 deletions.
22 changes: 11 additions & 11 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::RadialBranches`:
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::RadialBranches
radial_network_reduction::RadialNetworkReduction
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 @@ -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::RadialBranches`:
- `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::RadialBranches
radial_network_reduction::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 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
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
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::RadialBranches`:
- `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::RadialBranches`:
- `radial_network_reduction::RadialNetworkReduction`:
Structure containing the radial branches and leaf buses that were removed
while evaluating the matrix
"""
Expand Down
71 changes: 39 additions & 32 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_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::RadialBranches
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::RadialBranches`:
- `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::RadialBranches = RadialBranches(),
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 All @@ -323,13 +329,13 @@ 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)
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.")
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 = RadialBranches()
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 @@ -447,23 +453,24 @@ 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
if !isempty(BA.radial_branches) && !isempty(ABA.radial_branches) &&
isequal(BA.radial_branches, ABA.radial_branches)
radial_branches = BA.radial_branches
# BA and ABA must contain the same, non-empty RadialNetworkReduction stucture
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 RadialBranches stucture
if isempty(BA.radial_branches) && isempty(ABA.radial_branches)
radial_branches = RadialBranches()
# BA and ABA must contain the same, empty RadialNetworkReduction stucture
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
Loading

0 comments on commit 2c988a8

Please sign in to comment.