Skip to content

Commit

Permalink
update docstrings, print functions, and polish syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mcosovic committed Jul 10, 2024
1 parent 6560d41 commit efda503
Show file tree
Hide file tree
Showing 23 changed files with 328 additions and 333 deletions.
4 changes: 2 additions & 2 deletions src/definition/analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ mutable struct ACPower
to::Cartesian
series::Cartesian
charging::Cartesian
generator::Union{Cartesian, Nothing}
generator::Cartesian
end

########### Currents in the AC Framework ###########
Expand All @@ -45,7 +45,7 @@ mutable struct DCPower
supply::CartesianReal
from::CartesianReal
to::CartesianReal
generator::Union{CartesianReal, Nothing}
generator::CartesianReal
end

########### Newton-Raphson ###########
Expand Down
4 changes: 3 additions & 1 deletion src/definition/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,6 @@ mutable struct Measurement
wattmeter::Wattmeter
varmeter::Varmeter
pmu::PMU
end
end

const D = Union{Bus, Branch, Generator, Voltmeter, Ammeter, Wattmeter, Varmeter, PMU}
3 changes: 2 additions & 1 deletion src/measurement/ammeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ function addAmmeter!(system::PowerSystem, device::Measurement, analysis::AC;
end

"""
updateAmmeter!(system::PowerSystem, device::Measurement, analysis::Analysis; kwargs...)
updateAmmeter!(system::PowerSystem, device::Measurement, [analysis::Analysis];
kwargs...)
The function allows for the alteration of parameters for an ammeter.
Expand Down
43 changes: 31 additions & 12 deletions src/measurement/load.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,43 @@ device = measurement()
```
"""
function measurement()
label = OrderedDict{String, Int64}()
af = Array{Float64,1}(undef, 0)
ai = Array{Int64,1}(undef, 0)
ai8 = Array{Int8,1}(undef, 0)
ab = Array{Bool,1}(undef, 0)

voltLayout = VoltmeterLayout(copy(ai), 0)
ammLayout = AmmeterLayout(copy(ai), copy(ai), copy(ab), 0)
powerLayout = PowermeterLayout(copy(ai), copy(ab), copy(ab), copy(ab), 0)
pmuLayout = PmuLayout(copy(ai), copy(ab), copy(ab), copy(ab), copy(ab), copy(ab), 0)
gauss = GaussMeter(copy(af), copy(af), copy(ai8))

return Measurement(
Voltmeter(copy(label), deepcopy(gauss), voltLayout, 0),
Ammeter(copy(label), deepcopy(gauss), ammLayout, 0),
Wattmeter(copy(label), deepcopy(gauss), deepcopy(powerLayout), 0),
Varmeter(copy(label), deepcopy(gauss), deepcopy(powerLayout), 0),
PMU(copy(label), deepcopy(gauss), deepcopy(gauss), deepcopy(pmuLayout), 0)
Voltmeter(
OrderedDict{String, Int64}(),
GaussMeter(copy(af), copy(af), copy(ai8)),
VoltmeterLayout(copy(ai), 0),
0
),
Ammeter(
OrderedDict{String, Int64}(),
GaussMeter(copy(af), copy(af), copy(ai8)),
AmmeterLayout(copy(ai), copy(ai), copy(ab), 0),
0
),
Wattmeter(
OrderedDict{String, Int64}(),
GaussMeter(copy(af), copy(af), copy(ai8)),
PowermeterLayout(copy(ai), copy(ab), copy(ab), copy(ab), 0),
0
),
Varmeter(
OrderedDict{String, Int64}(),
GaussMeter(copy(af), copy(af), copy(ai8)),
PowermeterLayout(copy(ai), copy(ab), copy(ab), copy(ab), 0),
0
),
PMU(
OrderedDict{String, Int64}(),
GaussMeter(copy(af), copy(af), copy(ai8)),
GaussMeter(copy(af), copy(af), copy(ai8)),
PmuLayout(copy(ai), copy(ab), copy(ab), copy(ab), copy(ab), copy(ab), 0),
0
)
)
end

Expand Down
5 changes: 3 additions & 2 deletions src/measurement/pmu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function addPmu!(system::PowerSystem, device::Measurement;
pmu = device.pmu
default = template.pmu

location, busFlag, fromFlag, toFlag = checkLocation(pmu, bus, from, to)
location, busFlag, fromFlag, toFlag = checkLocation(bus, from, to)

branchFlag = false
if !busFlag
Expand Down Expand Up @@ -364,7 +364,8 @@ function addPmu!(system::PowerSystem, device::Measurement, analysis::AC;
end

"""
updatePmu!(system::PowerSystem, device::Measurement, analysis::Analysis; kwargs...)
updatePmu!(system::PowerSystem, device::Measurement, [analysis::Analysis];
kwargs...)
The function allows for the alteration of parameters for a PMU.
Expand Down
8 changes: 5 additions & 3 deletions src/measurement/powermeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ end
function addPowerMeter!(system, device, measure, default, prefixPower, label, bus, from, to,
power, variance, status, noise)

location, busFlag, fromFlag, toFlag = checkLocation(device, bus, from, to)
location, busFlag, fromFlag, toFlag = checkLocation(bus, from, to)

branchFlag = false
if !busFlag
Expand Down Expand Up @@ -423,7 +423,8 @@ function addPowermeter!(system, device, measure, powerBus, powerFrom, powerTo, d
end

"""
updateWattmeter!(system::PowerSystem, device::Measurement, analysis::Analysis; kwargs...)
updateWattmeter!(system::PowerSystem, device::Measurement, [analysis::Analysis];
kwargs...)
The function allows for the alteration of parameters for a wattmeter.
Expand Down Expand Up @@ -664,7 +665,8 @@ function updateWattmeter!(system::PowerSystem, device::Measurement, analysis::DC
end

"""
updateVarmeter!(system::PowerSystem, device::Measurement, analysis::Analysis; kwargs...)
updateVarmeter!(system::PowerSystem, device::Measurement, [analysis::Analysis];
kwargs...)
The function allows for the alteration of parameters for a varmeter.
Expand Down
3 changes: 2 additions & 1 deletion src/measurement/voltmeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ function addVoltmeter!(system::PowerSystem, device::Measurement, analysis::AC;
end

"""
updateVoltmeter!(system::PowerSystem, device::Measurement, analysis::Analysis; kwargs...)
updateVoltmeter!(system::PowerSystem, device::Measurement, [analysis::Analysis];
kwargs...)
The function allows for the alteration of parameters for a voltmeter.
Expand Down
8 changes: 2 additions & 6 deletions src/optimalPowerFlow/acOptimalPowerFlow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ function acOptimalPowerFlow(system::PowerSystem, (@nospecialize optimizerFactory
costActive = generator.cost.active
costReactive = generator.cost.reactive

if bus.layout.slack == 0
throw(ErrorException("The slack bus is missing."))
end
if isempty(system.model.ac.nodalMatrix)
acModel!(system)
end
checkSlackBus(system)
model!(system, system.model.ac)

jump = JuMP.Model(optimizerFactory; add_bridges = bridge)
set_string_names_on_creation(jump, name)
Expand Down
12 changes: 5 additions & 7 deletions src/optimalPowerFlow/dcOptimalPowerFlow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ function dcOptimalPowerFlow(system::PowerSystem, (@nospecialize optimizerFactory
generator = system.generator
cost = generator.cost.active

if bus.layout.slack == 0
throw(ErrorException("The slack bus is missing."))
end
if isempty(system.model.dc.nodalMatrix)
dcModel!(system)
end
checkSlackBus(system)
model!(system, system.model.dc)

jump = JuMP.Model(optimizerFactory; add_bridges = bridge)
set_string_names_on_creation(jump, name)
Expand Down Expand Up @@ -115,7 +111,9 @@ function dcOptimalPowerFlow(system::PowerSystem, (@nospecialize optimizerFactory
end

return DCOptimalPowerFlow(
PolarAngle(copy(bus.voltage.angle)),
PolarAngle(
copy(bus.voltage.angle)
),
DCPower(
CartesianReal(Float64[]),
CartesianReal(Float64[]),
Expand Down
65 changes: 32 additions & 33 deletions src/powerFlow/acPowerFlow.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
newtonRaphson(system::PowerSystem, factorization::Factorization)
newtonRaphson(system::PowerSystem, [factorization::Factorization = LU])
The function sets up the Newton-Raphson method to solve the AC power flow.
Expand Down Expand Up @@ -44,13 +44,8 @@ function newtonRaphson(system::PowerSystem, factorization::Type{<:Union{QR, LU}}
ac = system.model.ac
bus = system.bus

if bus.layout.slack == 0
throw(ErrorException("The slack bus is missing."))
end

if isempty(ac.nodalMatrix)
acModel!(system)
end
checkSlackBus(system)
model!(system, ac)
voltageMagnitude, voltageAngle = initializeACPowerFlow(system)

pqIndex = fill(0, bus.number)
Expand Down Expand Up @@ -152,7 +147,7 @@ function newtonRaphson(system::PowerSystem, factorization::Type{<:Union{QR, LU}}
end

"""
fastNewtonRaphsonBX(system::PowerSystem, factorization::Factorization)
fastNewtonRaphsonBX(system::PowerSystem, [factorization::Factorization = LU])
The function sets up the fast Newton-Raphson method of version BX to solve the AC power
flow.
Expand Down Expand Up @@ -201,7 +196,7 @@ function fastNewtonRaphsonBX(system::PowerSystem, factorization::Type{<:Union{QR
end

"""
fastNewtonRaphsonXB(system::PowerSystem, factorization::Factorization)
fastNewtonRaphsonXB(system::PowerSystem, [factorization::Factorization = LU])
The function sets up the fast Newton-Raphson method of version XB to solve the AC power
flow.
Expand Down Expand Up @@ -254,9 +249,8 @@ end
branch = system.branch
ac = system.model.ac

if isempty(ac.nodalMatrix)
acModel!(system)
end
checkSlackBus(system)
model!(system, ac)
voltageMagnitude, voltageAngle = initializeACPowerFlow(system)

pqIndex = fill(0, bus.number)
Expand Down Expand Up @@ -316,7 +310,10 @@ end

method = Dict(LU => lu, QR => qr)
analysis = ACPowerFlow(
Polar(voltageMagnitude,voltageAngle),
Polar(
voltageMagnitude,
voltageAngle
),
ACPower(
Cartesian(Float64[], Float64[]),
Cartesian(Float64[], Float64[]),
Expand Down Expand Up @@ -451,13 +448,12 @@ analysis = gaussSeidel(system)
```
"""
function gaussSeidel(system::PowerSystem)
bus = system.bus

if isempty(system.model.ac.nodalMatrix)
acModel!(system)
end
checkSlackBus(system)
model!(system, system.model.ac)
voltageMagnitude, voltageAngle = initializeACPowerFlow(system)

bus = system.bus

voltage = zeros(ComplexF64, bus.number)
pqIndex = Int64[]
pvIndex = Int64[]
Expand All @@ -473,7 +469,10 @@ function gaussSeidel(system::PowerSystem)
end

return ACPowerFlow(
Polar(voltageMagnitude, voltageAngle),
Polar(
voltageMagnitude,
voltageAngle
),
ACPower(
Cartesian(Float64[], Float64[]),
Cartesian(Float64[], Float64[]),
Expand Down Expand Up @@ -1068,26 +1067,26 @@ function initializeACPowerFlow(system::PowerSystem)
end
end

if isempty(system.bus.supply.generator[system.bus.layout.slack])
changeSlackBus!(system)
end
changeSlackBus!(system)

return magnitude, angle
end

########## Change Slack Bus ##########
function changeSlackBus!(system::PowerSystem)
system.bus.layout.type[system.bus.layout.slack] = 1
@inbounds for i = 1:system.bus.number
if system.bus.layout.type[i] == 2 && !isempty(system.bus.supply.generator[i])
system.bus.layout.type[i] = 3
system.bus.layout.slack = i
@info("The bus with index $i is now the new slack bus since no in-service generator was available at the previous slack bus.")
break
if isempty(system.bus.supply.generator[system.bus.layout.slack])
system.bus.layout.type[system.bus.layout.slack] = 1
@inbounds for i = 1:system.bus.number
if system.bus.layout.type[i] == 2 && !isempty(system.bus.supply.generator[i])
system.bus.layout.type[i] = 3
system.bus.layout.slack = i
@info("The bus with index $i is now the new slack bus since no in-service generator was available at the previous slack bus.")
break
end
end
end

if system.bus.layout.type[system.bus.layout.slack] == 1
throw(ErrorException("No generator buses with an in-service generator found in the power system. Slack bus definition not possible."))
if system.bus.layout.type[system.bus.layout.slack] == 1
throw(ErrorException("No generator buses with an in-service generator found in the power system. Slack bus definition not possible."))
end
end
end
21 changes: 9 additions & 12 deletions src/powerFlow/dcPowerFlow.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
dcPowerFlow(system::PowerSystem, factorization::Factorization)
dcPowerFlow(system::PowerSystem, [factorization::Factorization = LU])
The function sets up the framework to solve the DC power flow.
Expand Down Expand Up @@ -42,19 +42,15 @@ analysis = dcPowerFlow(system, QR)
```
"""
function dcPowerFlow(system::PowerSystem, factorization::Type{<:Union{QR, LDLt, LU}} = LU)
if system.bus.layout.slack == 0
throw(ErrorException("The slack bus is missing."))
end
if isempty(system.model.dc.nodalMatrix)
dcModel!(system)
end
if isempty(system.bus.supply.generator[system.bus.layout.slack])
changeSlackBus!(system)
end
checkSlackBus(system)
model!(system, system.model.dc)
changeSlackBus!(system)

method = Dict(LU => lu, LDLt => ldlt, QR => qr)
return DCPowerFlow(
PolarAngle(Float64[]),
PolarAngle(
Float64[]
),
DCPower(
CartesianReal(Float64[]),
CartesianReal(Float64[]),
Expand All @@ -65,7 +61,8 @@ function dcPowerFlow(system::PowerSystem, factorization::Type{<:Union{QR, LDLt,
DCPowerFlowMethod(
get(method, factorization, lu)(sparse(Matrix(1.0I, 1, 1))),
-1,
-1)
-1
)
)
end

Expand Down
4 changes: 2 additions & 2 deletions src/powerSystem/branch.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
addBranch!(system::PowerSystem, analysis::Analysis; label, from, to, status,
addBranch!(system::PowerSystem, [analysis::Analysis]; label, from, to, status,
resistance, reactance, conductance, susceptance, turnsRatio, shiftAngle,
minDiffAngle, maxDiffAngle, longTerm, shortTerm, emergency, type)
Expand Down Expand Up @@ -248,7 +248,7 @@ function addBranch!(system::PowerSystem, analysis::ACOptimalPowerFlow;
end

"""
updateBranch!(system::PowerSystem, analysis::Analysis; kwargs...)
updateBranch!(system::PowerSystem, [analysis::Analysis]; kwargs...)
The function allows for the alteration of parameters for an existing branch.
Expand Down
2 changes: 1 addition & 1 deletion src/powerSystem/bus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function addBus!(system::PowerSystem, analysis::DCStateEstimation; kwargs...)
end

"""
updateBus!(system::PowerSystem, analysis::Analysis; kwargs...)
updateBus!(system::PowerSystem, [analysis::Analysis]; kwargs...)
The function allows for the alteration of parameters for an existing bus.
Expand Down
Loading

0 comments on commit efda503

Please sign in to comment.