From bd47c8dc35fd51b81afc95ef7625aa7f7dddd8a1 Mon Sep 17 00:00:00 2001 From: Jose Daniel Lara Date: Mon, 15 Jan 2024 12:58:27 -0700 Subject: [PATCH 1/4] radial branches rename --- src/BA_ABA_matrices.jl | 16 +++++----- src/PowerNetworkMatrices.jl | 4 +-- src/incedence_matrix.jl | 4 +-- src/lodf_calculations.jl | 20 ++++++------ ...braches.jl => network_radial_reduction.jl} | 32 +++++++++---------- src/ptdf_calculations.jl | 14 ++++---- src/serialization.jl | 2 +- src/virtual_lodf_calculations.jl | 12 +++---- src/virtual_ptdf_calculations.jl | 10 +++--- test/test_A_BA_ABA_with_radial_lines.jl | 4 +-- test/test_lodf_with_radial_branches.jl | 2 +- test/test_ptdf_with_radial_lines.jl | 6 ++-- test/test_radial_branches.jl | 6 ++-- 13 files changed, 66 insertions(+), 66 deletions(-) rename src/{radial_braches.jl => network_radial_reduction.jl} (88%) diff --git a/src/BA_ABA_matrices.jl b/src/BA_ABA_matrices.jl index 315d6666..ddb15a87 100644 --- a/src/BA_ABA_matrices.jl +++ b/src/BA_ABA_matrices.jl @@ -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 -- `radial_branches::RadialBranches`: +- `radial_branches::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -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 end """ @@ -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) @@ -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 """ @@ -89,7 +89,7 @@ struct ABA_Matrix{ lookup::L ref_bus_positions::Set{Int} K::F - radial_branches::RadialBranches + radial_branches::RadialNetworkReduction end """ @@ -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) diff --git a/src/PowerNetworkMatrices.jl b/src/PowerNetworkMatrices.jl index 407e485e..83524523 100644 --- a/src/PowerNetworkMatrices.jl +++ b/src/PowerNetworkMatrices.jl @@ -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 @@ -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") diff --git a/src/incedence_matrix.jl b/src/incedence_matrix.jl index ccd312e6..0b0bbc2d 100644 --- a/src/incedence_matrix.jl +++ b/src/incedence_matrix.jl @@ -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 """ @@ -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 """ diff --git a/src/lodf_calculations.jl b/src/lodf_calculations.jl index 3f5f7c5c..8939a65f 100644 --- a/src/lodf_calculations.jl +++ b/src/lodf_calculations.jl @@ -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 """ @@ -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( @@ -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 """ @@ -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 @@ -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) @@ -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 @@ -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 @@ -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( diff --git a/src/radial_braches.jl b/src/network_radial_reduction.jl similarity index 88% rename from src/radial_braches.jl rename to src/network_radial_reduction.jl index 91ef19f4..40bb6aca 100644 --- a/src/radial_braches.jl +++ b/src/network_radial_reduction.jl @@ -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 +get_radial_branches(rb::RadialNetworkReduction) = rb.radial_branches +get_meshed_branches(rb::RadialNetworkReduction) = rb.meshed_branches -function Base.isempty(rb::RadialBranches) +function Base.isempty(rb::RadialNetworkReduction) if !isempty(rb.bus_reduction_map) return false end @@ -25,12 +25,12 @@ function Base.isempty(rb::RadialBranches) 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, @@ -46,7 +46,7 @@ ignored in the models. - `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 @@ -58,8 +58,8 @@ ignored in the models. - `sys::PSY.System`: System Data """ -function RadialBranches(sys::PSY.System) - return RadialBranches(IncidenceMatrix(sys)) +function RadialNetworkReduction(sys::PSY.System) + return RadialNetworkReduction(IncidenceMatrix(sys)) end function _find_upstream_bus( @@ -205,7 +205,7 @@ function calculate_radial_branches( end push!(meshed_branches, k) end - return RadialBranches( + return RadialNetworkReduction( bus_reduction_map_index, reverse_bus_search_map, radial_branches, @@ -217,10 +217,10 @@ end 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) if isempty(rb) return bus_number end @@ -232,8 +232,8 @@ end ############################################################################## function isequal( - rb1::RadialBranches, - rb2::RadialBranches, + rb1::RadialNetworkReduction, + rb2::RadialNetworkReduction, ) for field in fieldnames(typeof(rb1)) if getfield(rb1, field) != getfield(rb2, field) diff --git a/src/ptdf_calculations.jl b/src/ptdf_calculations.jl index 3c50595a..34444c57 100644 --- a/src/ptdf_calculations.jl +++ b/src/ptdf_calculations.jl @@ -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 """ @@ -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 """ @@ -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 """ @@ -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) @@ -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) @@ -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]) @@ -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) diff --git a/src/serialization.jl b/src/serialization.jl index de0d3684..20a630e6 100644 --- a/src/serialization.jl +++ b/src/serialization.jl @@ -83,7 +83,7 @@ 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 diff --git a/src/virtual_lodf_calculations.jl b/src/virtual_lodf_calculations.jl index be9c1662..189c3488 100644 --- a/src/virtual_lodf_calculations.jl +++ b/src/virtual_lodf_calculations.jl @@ -38,7 +38,7 @@ The VirtualLODF struct is indexed using branch names. Dictionary containing the subsets of buses defining the different subnetwork of the system. - `tol::Base.RefValue{Float64}`: Tolerance related to scarification and values to drop. -- `radial_branches::RadialBranches`: +- `radial_branches::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -55,7 +55,7 @@ struct VirtualLODF{Ax, L <: NTuple{2, Dict}} <: PowerNetworkMatrix{Float64} cache::RowCache subnetworks::Dict{Int, Set{Int}} tol::Base.RefValue{Float64} - radial_branches::RadialBranches + radial_branches::RadialNetworkReduction end function Base.show(io::IO, ::MIME{Symbol("text/plain")}, array::VirtualLODF) @@ -115,9 +115,9 @@ function VirtualLODF( 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) @@ -141,7 +141,7 @@ VirtualLODF struct with an empty cache. max cache size in MiB (inizialized as MAX_CACHE_SIZE_MiB). - `persistent_lines::Vector{String}`: line to be evaluated as soon as the VirtualPTDF is created (initialized as empty vector of strings). -- `radial_branches::RadialBranches`: +- `radial_branches::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -151,7 +151,7 @@ function VirtualLODF( tol::Float64 = eps(), max_cache_size::Int = MAX_CACHE_SIZE_MiB, persistent_lines::Vector{String} = String[], - radial_branches::RadialBranches = RadialBranches(), + radial_branches::RadialNetworkReduction = RadialNetworkReduction(), ) #Get axis names and lookups diff --git a/src/virtual_ptdf_calculations.jl b/src/virtual_ptdf_calculations.jl index b3137070..45223319 100644 --- a/src/virtual_ptdf_calculations.jl +++ b/src/virtual_ptdf_calculations.jl @@ -42,7 +42,7 @@ matrix. Dictionary containing the subsets of buses defining the different subnetwork of the system. - `tol::Base.RefValue{Float64}`: Tolerance related to scarification and values to drop. -- `radial_branches::RadialBranches`: +- `radial_branches::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -58,7 +58,7 @@ struct VirtualPTDF{Ax, L <: NTuple{2, Dict}} <: PowerNetworkMatrix{Float64} cache::RowCache subnetworks::Dict{Int, Set{Int}} tol::Base.RefValue{Float64} - radial_branches::RadialBranches + radial_branches::RadialNetworkReduction end function Base.show(io::IO, ::MIME{Symbol("text/plain")}, array::VirtualPTDF) @@ -89,7 +89,7 @@ VirtualPTDF struct with an empty cache. max cache size in MiB (inizialized as MAX_CACHE_SIZE_MiB). - `persistent_lines::Vector{String}`: line to be evaluated as soon as the VirtualPTDF is created (initialized as empty vector of strings). -- `radial_branches::RadialBranches`: +- `radial_branches::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -100,7 +100,7 @@ function VirtualPTDF( tol::Float64 = eps(), max_cache_size::Int = MAX_CACHE_SIZE_MiB, persistent_lines::Vector{String} = String[], - radial_branches::RadialBranches = RadialBranches(), + radial_branches::RadialNetworkReduction = RadialNetworkReduction(), ) if length(dist_slack) != 0 @info "Distributed bus" @@ -181,7 +181,7 @@ function VirtualPTDF( 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) diff --git a/test/test_A_BA_ABA_with_radial_lines.jl b/test/test_A_BA_ABA_with_radial_lines.jl index c82a6c22..02427d0a 100644 --- a/test/test_A_BA_ABA_with_radial_lines.jl +++ b/test/test_A_BA_ABA_with_radial_lines.jl @@ -27,8 +27,8 @@ end # load the system sys = PSB.build_system(PSB.PSITestSystems, name) - # get the RadialBranches struct - rb = RadialBranches(IncidenceMatrix(sys)) + # get the RadialNetworkReduction struct + rb = RadialNetworkReduction(IncidenceMatrix(sys)) # get the original and reduced IncidenceMatrix, BA and ABA A = IncidenceMatrix(sys) diff --git a/test/test_lodf_with_radial_branches.jl b/test/test_lodf_with_radial_branches.jl index 948d7ecb..0404ec9e 100644 --- a/test/test_lodf_with_radial_branches.jl +++ b/test/test_lodf_with_radial_branches.jl @@ -17,7 +17,7 @@ lodf_rad_A_BA_ABA = LODF(A_rad, ABA_rad, BA_rad; reduce_radial_branches = true) lodf_rad_A_PTDF = LODF(A_rad, ptdf_rad; reduce_radial_branches = true) - rb = RadialBranches(IncidenceMatrix(sys)) + rb = RadialNetworkReduction(IncidenceMatrix(sys)) # at first check if all the matrices are the same @test isapprox(lodf_rad.data, lodf_rad_A_BA_ABA.data, atol = 1e-10) diff --git a/test/test_ptdf_with_radial_lines.jl b/test/test_ptdf_with_radial_lines.jl index ddb154ed..5c062b10 100644 --- a/test/test_ptdf_with_radial_lines.jl +++ b/test/test_ptdf_with_radial_lines.jl @@ -3,8 +3,8 @@ # load the system sys = PSB.build_system(PSB.PSITestSystems, name) - # get the RadialBranches struct - rb = RadialBranches(IncidenceMatrix(sys)) + # get the RadialNetworkReduction struct + rb = RadialNetworkReduction(IncidenceMatrix(sys)) # get the A and BA matrices without radial lines A_rad = IncidenceMatrix(sys) @@ -79,7 +79,7 @@ end sys = PSB.build_system(PSB.PSITestSystems, name) # get the radial branches A = IncidenceMatrix(sys) - rb = RadialBranches(A) + rb = RadialNetworkReduction(A) # get number of buses buscount = length(PNM.get_buses(sys)) # now compute distributed slack vector diff --git a/test/test_radial_branches.jl b/test/test_radial_branches.jl index 76519152..e49125cb 100644 --- a/test/test_radial_branches.jl +++ b/test/test_radial_branches.jl @@ -24,7 +24,7 @@ (0.0, 0.0), ), ) - rb = RadialBranches(IncidenceMatrix(sys)) + rb = RadialNetworkReduction(IncidenceMatrix(sys)) @test get_bus_reduction_map(rb)[7] == Set([61, 8]) @test get_radial_branches(rb) == Set(["tl", "Trans4"]) end @@ -32,7 +32,7 @@ end @testset "Radial Branches Large" begin sys = build_system(MatpowerTestSystems, "matpower_ACTIVSg10k_sys"; add_forecasts = false) - rb = RadialBranches(IncidenceMatrix(sys)) + rb = RadialNetworkReduction(IncidenceMatrix(sys)) for (k, v) in get_bus_reduction_map(rb) @test k ∉ v end @@ -42,7 +42,7 @@ end for name in ["matpower_ACTIVSg2000_sys", "matpower_ACTIVSg10k_sys"] sys = build_system(MatpowerTestSystems, name; add_forecasts = false) a_mat = IncidenceMatrix(sys) - rb = RadialBranches(IncidenceMatrix(sys)) + rb = RadialNetworkReduction(IncidenceMatrix(sys)) leaf_buses = Int64[] for i in keys(rb.bus_reduction_map) append!(leaf_buses, collect(rb.bus_reduction_map[i])) From f7d52fa53dc2cc3882b447d7d5d6faf8ad048626 Mon Sep 17 00:00:00 2001 From: Jose Daniel Lara Date: Mon, 15 Jan 2024 13:01:00 -0700 Subject: [PATCH 2/4] add additional method --- src/network_radial_reduction.jl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/network_radial_reduction.jl b/src/network_radial_reduction.jl index 40bb6aca..f81a1a8d 100644 --- a/src/network_radial_reduction.jl +++ b/src/network_radial_reduction.jl @@ -227,6 +227,17 @@ function get_mapped_bus_number(rb::RadialNetworkReduction, bus_number::Int) 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)) +end + ############################################################################## ########################### Auxiliary functions ############################## ############################################################################## From b628bdd888c5f2bee8fceae4d994349b1456432b Mon Sep 17 00:00:00 2001 From: Jose Daniel Lara Date: Mon, 15 Jan 2024 13:14:33 -0700 Subject: [PATCH 3/4] format --- src/serialization.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/serialization.jl b/src/serialization.jl index 20a630e6..b500645f 100644 --- a/src/serialization.jl +++ b/src/serialization.jl @@ -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, RadialNetworkReduction()) + PTDF( + data, + axes, + lookup, + subnetworks, + ref_bus_positions, + tol, + RadialNetworkReduction(), + ) end end From a1d636f7c93b6f439c57f4836b8b1415fe35946e Mon Sep 17 00:00:00 2001 From: Jose Daniel Lara Date: Tue, 16 Jan 2024 12:59:24 -0700 Subject: [PATCH 4/4] address PR comments --- src/BA_ABA_matrices.jl | 14 +++--- src/adjacency_matrix.jl | 2 +- src/common.jl | 2 +- src/incedence_matrix.jl | 4 +- src/lodf_calculations.jl | 63 ++++++++++++++----------- src/network_radial_reduction.jl | 2 +- src/ptdf_calculations.jl | 44 +++++++++++------ src/virtual_lodf_calculations.jl | 14 +++--- src/virtual_ptdf_calculations.jl | 14 +++--- test/test_A_BA_ABA_with_radial_lines.jl | 2 +- 10 files changed, 91 insertions(+), 70 deletions(-) diff --git a/src/BA_ABA_matrices.jl b/src/BA_ABA_matrices.jl index ddb15a87..c39e2443 100644 --- a/src/BA_ABA_matrices.jl +++ b/src/BA_ABA_matrices.jl @@ -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 """ @@ -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 """ @@ -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 """ @@ -89,7 +89,7 @@ struct ABA_Matrix{ lookup::L ref_bus_positions::Set{Int} K::F - radial_branches::RadialNetworkReduction + radial_network_reduction::RadialNetworkReduction end """ @@ -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 diff --git a/src/adjacency_matrix.jl b/src/adjacency_matrix.jl index 993d75de..49b8538e 100644 --- a/src/adjacency_matrix.jl +++ b/src/adjacency_matrix.jl @@ -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} diff --git a/src/common.jl b/src/common.jl index f3d80ef9..9bd8a019 100644 --- a/src/common.jl +++ b/src/common.jl @@ -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. """ diff --git a/src/incedence_matrix.jl b/src/incedence_matrix.jl index 0b0bbc2d..5c12d82d 100644 --- a/src/incedence_matrix.jl +++ b/src/incedence_matrix.jl @@ -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 """ @@ -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 """ diff --git a/src/lodf_calculations.jl b/src/lodf_calculations.jl index 8939a65f..9f817d9b 100644 --- a/src/lodf_calculations.jl +++ b/src/lodf_calculations.jl @@ -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 """ @@ -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( @@ -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 """ @@ -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 @@ -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 @@ -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 """ @@ -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 = 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 @@ -404,7 +410,7 @@ function LODF( (A.axes[1], A.axes[1]), (ax_ref, ax_ref), Ref(tol), - radial_branches, + radial_network_reduction, ) end return LODF( @@ -412,7 +418,7 @@ function LODF( (A.axes[1], A.axes[1]), (ax_ref, ax_ref), Ref(tol), - radial_branches, + radial_network_reduction, ) end @@ -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( @@ -479,7 +486,7 @@ function LODF( (A.axes[1], A.axes[1]), (ax_ref, ax_ref), Ref(tol), - radial_branches, + radial_network_reduction, ) end return LODF( @@ -487,7 +494,7 @@ function LODF( (A.axes[1], A.axes[1]), (ax_ref, ax_ref), Ref(tol), - radial_branches, + radial_network_reduction, ) end diff --git a/src/network_radial_reduction.jl b/src/network_radial_reduction.jl index f81a1a8d..b927286e 100644 --- a/src/network_radial_reduction.jl +++ b/src/network_radial_reduction.jl @@ -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}, diff --git a/src/ptdf_calculations.jl b/src/ptdf_calculations.jl index 34444c57..7e695ac7 100644 --- a/src/ptdf_calculations.jl +++ b/src/ptdf_calculations.jl @@ -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 """ @@ -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 """ @@ -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 """ @@ -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) @@ -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 """ @@ -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 """ @@ -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]) @@ -529,7 +543,7 @@ function PTDF( Dict{Int, Set{Int}}(), BA.ref_bus_positions, Ref(tol), - radial_branches, + radial_network_reduction, ) else return PTDF( @@ -539,7 +553,7 @@ function PTDF( Dict{Int, Set{Int}}(), BA.ref_bus_positions, Ref(tol), - radial_branches, + radial_network_reduction, ) end end diff --git a/src/virtual_lodf_calculations.jl b/src/virtual_lodf_calculations.jl index 189c3488..a4e9c762 100644 --- a/src/virtual_lodf_calculations.jl +++ b/src/virtual_lodf_calculations.jl @@ -21,7 +21,7 @@ The VirtualLODF struct is indexed using branch names. coming from multuiplying the PTDF matrix with th Incidence matrix - `ref_bus_positions::Set{Int}`: Vector containing the indexes of the rows of the transposed BA matrix - corresponding to the refence buses. + corresponding to the reference buses. - `axes<:NTuple{2, Dict}`: Tuple containing two vectors showing the branch names. - `lookup<:NTuple{2, Dict}`: @@ -38,7 +38,7 @@ The VirtualLODF struct is indexed using branch names. Dictionary containing the subsets of buses defining the different subnetwork of the system. - `tol::Base.RefValue{Float64}`: Tolerance related to scarification and values to drop. -- `radial_branches::RadialNetworkReduction`: +- `radial_network_reduction::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -55,7 +55,7 @@ struct VirtualLODF{Ax, L <: NTuple{2, Dict}} <: PowerNetworkMatrix{Float64} cache::RowCache subnetworks::Dict{Int, Set{Int}} tol::Base.RefValue{Float64} - radial_branches::RadialNetworkReduction + radial_network_reduction::RadialNetworkReduction end function Base.show(io::IO, ::MIME{Symbol("text/plain")}, array::VirtualLODF) @@ -121,7 +121,7 @@ function VirtualLODF( end branches = get_ac_branches(sys, rb.radial_branches) buses = get_buses(sys, rb.bus_reduction_map) - return VirtualLODF(branches, buses; radial_branches = rb, kwargs...) + return VirtualLODF(branches, buses; radial_network_reduction = rb, kwargs...) end """ @@ -141,7 +141,7 @@ VirtualLODF struct with an empty cache. max cache size in MiB (inizialized as MAX_CACHE_SIZE_MiB). - `persistent_lines::Vector{String}`: line to be evaluated as soon as the VirtualPTDF is created (initialized as empty vector of strings). -- `radial_branches::RadialNetworkReduction`: +- `radial_network_reduction::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -151,7 +151,7 @@ function VirtualLODF( tol::Float64 = eps(), max_cache_size::Int = MAX_CACHE_SIZE_MiB, persistent_lines::Vector{String} = String[], - radial_branches::RadialNetworkReduction = RadialNetworkReduction(), + radial_network_reduction::RadialNetworkReduction = RadialNetworkReduction(), ) #Get axis names and lookups @@ -209,7 +209,7 @@ function VirtualLODF( empty_cache, subnetworks, Ref(tol), - radial_branches, + radial_network_reduction, ) end diff --git a/src/virtual_ptdf_calculations.jl b/src/virtual_ptdf_calculations.jl index 45223319..adb8fd0d 100644 --- a/src/virtual_ptdf_calculations.jl +++ b/src/virtual_ptdf_calculations.jl @@ -17,7 +17,7 @@ matrix. BA matric - `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 - `dist_slack::Vector{Float64}`: Vector of weights to be used as distributed slack bus. The distributed slack vector has to be the same length as the number of buses. @@ -42,7 +42,7 @@ matrix. Dictionary containing the subsets of buses defining the different subnetwork of the system. - `tol::Base.RefValue{Float64}`: Tolerance related to scarification and values to drop. -- `radial_branches::RadialNetworkReduction`: +- `radial_network_reduction::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -58,7 +58,7 @@ struct VirtualPTDF{Ax, L <: NTuple{2, Dict}} <: PowerNetworkMatrix{Float64} cache::RowCache subnetworks::Dict{Int, Set{Int}} tol::Base.RefValue{Float64} - radial_branches::RadialNetworkReduction + radial_network_reduction::RadialNetworkReduction end function Base.show(io::IO, ::MIME{Symbol("text/plain")}, array::VirtualPTDF) @@ -89,7 +89,7 @@ VirtualPTDF struct with an empty cache. max cache size in MiB (inizialized as MAX_CACHE_SIZE_MiB). - `persistent_lines::Vector{String}`: line to be evaluated as soon as the VirtualPTDF is created (initialized as empty vector of strings). -- `radial_branches::RadialNetworkReduction`: +- `radial_network_reduction::RadialNetworkReduction`: Structure containing the radial branches and leaf buses that were removed while evaluating the matrix """ @@ -100,7 +100,7 @@ function VirtualPTDF( tol::Float64 = eps(), max_cache_size::Int = MAX_CACHE_SIZE_MiB, persistent_lines::Vector{String} = String[], - radial_branches::RadialNetworkReduction = RadialNetworkReduction(), + radial_network_reduction::RadialNetworkReduction = RadialNetworkReduction(), ) if length(dist_slack) != 0 @info "Distributed bus" @@ -149,7 +149,7 @@ function VirtualPTDF( empty_cache, subnetworks, Ref(tol), - radial_branches, + radial_network_reduction, ) end @@ -189,7 +189,7 @@ function VirtualPTDF( branches, buses; dist_slack = dist_slack, - radial_branches = rb, + radial_network_reduction = rb, kwargs..., ) end diff --git a/test/test_A_BA_ABA_with_radial_lines.jl b/test/test_A_BA_ABA_with_radial_lines.jl index 02427d0a..91a045b7 100644 --- a/test/test_A_BA_ABA_with_radial_lines.jl +++ b/test/test_A_BA_ABA_with_radial_lines.jl @@ -7,7 +7,7 @@ # ... and with radial lines BA_rad = BA_Matrix(sys; reduce_radial_branches = true) # get inidices for the leaf nodes - rb = BA_rad.radial_branches + rb = BA_rad.radial_network_reduction bus_numbers = [] for i in keys(rb.bus_reduction_map) append!(bus_numbers, collect(rb.bus_reduction_map[i]))