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 all 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
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 @@
- `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 @@
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 @@
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 @@
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 @@

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 @@
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 @@
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 = 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 @@
(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 @@
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 @@
(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
Loading