Skip to content

Commit

Permalink
optimize allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mcosovic committed Sep 3, 2024
1 parent f07550c commit b3871b6
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/JuliaGrid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module JuliaGrid

using JuMP

import LinearAlgebra: lu, lu!, ldlt, ldlt!, qr, ldiv, ldiv!, I, Factorization as LagFactorization
import LinearAlgebra: lu, lu!, ldlt, ldlt!, qr, ldiv, ldiv!, I, Factorization
import SparseArrays: SparseMatrixCSC, sparse, spzeros, spdiagm, dropzeros!, nzrange, nnz
import SuiteSparse: UMFPACK, SPQR, CHOLMOD

Expand Down
10 changes: 5 additions & 5 deletions src/definition/analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mutable struct NewtonRaphson
jacobian::SparseMatrixCSC{Float64,Int64}
mismatch::Array{Float64,1}
increment::Array{Float64,1}
factorization::LUQR
factorization::Factorization{Float64}
pq::Array{Int64,1}
pvpq::Array{Int64,1}
pattern::Int64
Expand All @@ -58,7 +58,7 @@ mutable struct FastNewtonRaphsonModel
jacobian::SparseMatrixCSC{Float64,Int64}
mismatch::Array{Float64,1}
increment::Array{Float64,1}
factorization::LUQR
factorization::Factorization{Float64}
end

mutable struct FastNewtonRaphson
Expand Down Expand Up @@ -88,7 +88,7 @@ end

########### DC Power Flow ###########
mutable struct DCPowerFlowMethod
factorization::LULDLtQR
factorization::Factorization{Float64}
dcmodel::Int64
pattern::Int64
end
Expand Down Expand Up @@ -246,7 +246,7 @@ mutable struct LinearWLS{T <: Union{Normal, Orthogonal}}
coefficient::SparseMatrixCSC{Float64,Int64}
precision::SparseMatrixCSC{Float64,Int64}
mean::Array{Float64,1}
factorization::LULDLtQR
factorization::Factorization{Float64}
number::Int64
pattern::Int64
run::Bool
Expand All @@ -258,7 +258,7 @@ mutable struct NonlinearWLS{T <: Union{Normal, Orthogonal}}
mean::Array{Float64,1}
residual::Array{Float64,1}
increment::Array{Float64,1}
factorization::LULDLtQR
factorization::Factorization{Float64}
type::Array{Int8,1}
index::Array{Int64,1}
range::Array{Int64,1}
Expand Down
21 changes: 8 additions & 13 deletions src/definition/internal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,13 @@ end
prefix = PrefixLive()

########### Matrix Factorization Type ###########
export Factorization, LU, QR, LDLt
export LU, QR, LDLt

abstract type Factorization end
abstract type QR <: Factorization end
abstract type LU <: Factorization end
abstract type LDLt <: Factorization end
abstract type QR end
abstract type LU end
abstract type LDLt end

const factorizeMatrix = Dict{DataType, LagFactorization{Float64}}()
factorizeMatrix[LU] = lu(sparse(Matrix(1.0I, 1, 1)))
factorizeMatrix[QR] = qr(sparse(Matrix(1.0I, 1, 1)))
factorizeMatrix[LDLt] = ldlt(sparse(Matrix(1.0I, 1, 1)))

const LUQR = Union{UMFPACK.UmfpackLU{Float64, Int64}, SPQR.QRSparse{Float64, Int64}}
const LULDLt = Union{CHOLMOD.Factor{Float64}, UMFPACK.UmfpackLU{Float64, Int64}}
const LULDLtQR = Union{CHOLMOD.Factor{Float64}, LUQR}
const factorized = Dict{DataType, Factorization{Float64}}()
factorized[LU] = lu(sparse(Matrix(1.0I, 1, 1)))
factorized[QR] = qr(sparse(Matrix(1.0I, 1, 1)))
factorized[LDLt] = ldlt(sparse(Matrix(1.0I, 1, 1)))
6 changes: 3 additions & 3 deletions src/powerFlow/acPowerFlow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function newtonRaphson(system::PowerSystem, factorization::Type{<:Union{QR, LU}}
jacobian,
mismatch,
increment,
factorizeMatrix[factorization],
factorized[factorization],
pqIndex,
pvpqIndex,
-1
Expand Down Expand Up @@ -333,13 +333,13 @@ end
jacobianActive,
mismatchActive,
incrementActive,
factorizeMatrix[factorization],
factorized[factorization],
),
FastNewtonRaphsonModel(
jacobianReactive,
mismatchReactive,
incrementReactive,
factorizeMatrix[factorization],
factorized[factorization],
),
pqIndex,
pvpqIndex,
Expand Down
2 changes: 1 addition & 1 deletion src/powerFlow/dcPowerFlow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function dcPowerFlow(system::PowerSystem, factorization::Type{<:Union{QR, LDLt,
CartesianReal(Float64[])
),
DCPowerFlowMethod(
factorizeMatrix[factorization],
factorized[factorization],
-1,
-1
)
Expand Down
4 changes: 2 additions & 2 deletions src/stateEstimation/acStateEstimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function gaussNewton(system::PowerSystem, device::Measurement,
mean,
residual,
fill(0.0, 2 * system.bus.number),
factorizeMatrix[factorization],
factorized[factorization],
type,
index,
range,
Expand Down Expand Up @@ -94,7 +94,7 @@ function gaussNewton(system::PowerSystem, device::Measurement, method::Type{<:Or
mean,
residual,
fill(0.0, 2 * system.bus.number),
qr(sparse(Matrix(1.0I, 1, 1))),
factorized[QR],
type,
index,
range,
Expand Down
7 changes: 3 additions & 4 deletions src/stateEstimation/dcStateEstimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ analysis = dcWlsStateEstimation(system, device, Orthogonal)
```
"""
function dcWlsStateEstimation(system::PowerSystem, device::Measurement,
factorization::Type{<:Union{QR, LDLt, LU, Orthogonal}} = LU)
factorization::Type{<:Union{QR, LDLt, LU}} = LU)

coefficient, mean, precision, power = dcStateEstimationWLS(system, device)

Expand All @@ -60,7 +60,7 @@ function dcWlsStateEstimation(system::PowerSystem, device::Measurement,
coefficient,
precision,
mean,
factorizeMatrix[factorization],
factorized[factorization],
device.wattmeter.number + device.pmu.number,
-1,
true,
Expand All @@ -69,7 +69,6 @@ function dcWlsStateEstimation(system::PowerSystem, device::Measurement,
end

function dcWlsStateEstimation(system::PowerSystem, device::Measurement, method::Type{<:Orthogonal})

coefficient, mean, precision, power = dcStateEstimationWLS(system, device)

return DCStateEstimation(
Expand All @@ -81,7 +80,7 @@ function dcWlsStateEstimation(system::PowerSystem, device::Measurement, method::
coefficient,
precision,
mean,
qr(sparse(Matrix(1.0I, 1, 1))),
factorized[QR],
device.wattmeter.number + device.pmu.number,
-1,
true,
Expand Down
4 changes: 2 additions & 2 deletions src/stateEstimation/pmuStateEstimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function pmuWlsStateEstimation(system::PowerSystem, device::Measurement,
coefficient,
precision,
mean,
factorizeMatrix[factorization],
factorized[factorization],
2 * device.pmu.number,
-1,
true,
Expand All @@ -87,7 +87,7 @@ function pmuWlsStateEstimation(system::PowerSystem, device::Measurement, method:
coefficient,
precision,
mean,
qr(sparse(Matrix(1.0I, 1, 1))),
factorized[QR],
2 * device.pmu.number,
-1,
true,
Expand Down
42 changes: 40 additions & 2 deletions src/utility/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,66 @@ PrecompileTools.@setup_workload begin
addPmu!(system, device; bus = 2, magnitude = 1.0, angle = 0.0)

PrecompileTools.@compile_workload begin
########## DC Power Flow ###########
analysis = dcPowerFlow(system)
solve!(system, analysis)

analysis = dcPowerFlow(system, QR)
solve!(system, analysis)

analysis = dcPowerFlow(system, LDLt)
solve!(system, analysis)

########## AC Power Flow ###########
analysis = newtonRaphson(system)
solve!(system, analysis)

analysis = fastNewtonRaphsonBX(system)
analysis = newtonRaphson(system, QR)
solve!(system, analysis)

analysis = fastNewtonRaphsonXB(system)
analysis = fastNewtonRaphsonBX(system)
solve!(system, analysis)

analysis = gaussSeidel(system)
solve!(system, analysis)

########## DC State Estimation ###########
analysis = dcWlsStateEstimation(system, device)
solve!(system, analysis)

analysis = dcWlsStateEstimation(system, device, QR)
solve!(system, analysis)

analysis = dcWlsStateEstimation(system, device, LDLt)
solve!(system, analysis)

analysis = dcWlsStateEstimation(system, device, Orthogonal)
solve!(system, analysis)

########### PMU State Estimation ###########
analysis = pmuWlsStateEstimation(system, device)
solve!(system, analysis)

analysis = pmuWlsStateEstimation(system, device, QR)
solve!(system, analysis)

analysis = pmuWlsStateEstimation(system, device, LDLt)
solve!(system, analysis)

analysis = pmuWlsStateEstimation(system, device, Orthogonal)
solve!(system, analysis)

########### AC State Estimation ###########
analysis = gaussNewton(system, device)
solve!(system, analysis)

analysis = gaussNewton(system, device, QR)
solve!(system, analysis)

analysis = gaussNewton(system, device, LDLt)
solve!(system, analysis)

analysis = gaussNewton(system, device, Orthogonal)
solve!(system, analysis)
end
end

0 comments on commit b3871b6

Please sign in to comment.