Skip to content

Commit

Permalink
reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Bolgaryn committed Dec 6, 2024
1 parent 251f1b3 commit 210937c
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 328 deletions.
58 changes: 29 additions & 29 deletions src/PowerFlowData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ flows and angles, as well as these ones.
- `neighbors::Vector{Set{Int}}`: Vector with the sets of adjacent buses.
"""
struct PowerFlowData{
M <: PNM.PowerNetworkMatrix,
N <: Union{PNM.PowerNetworkMatrix, Nothing},
M<:PNM.PowerNetworkMatrix,
N<:Union{PNM.PowerNetworkMatrix,Nothing},
} <: PowerFlowContainer
bus_lookup::Dict{Int, Int}
branch_lookup::Dict{String, Int}
bus_lookup::Dict{Int,Int}
branch_lookup::Dict{String,Int}
bus_activepower_injection::Matrix{Float64}
bus_reactivepower_injection::Matrix{Float64}
bus_activepower_withdrawals::Matrix{Float64}
Expand All @@ -81,7 +81,7 @@ struct PowerFlowData{
bus_magnitude::Matrix{Float64}
bus_angles::Matrix{Float64}
branch_flow_values::Matrix{Float64}
timestep_map::Dict{Int, String}
timestep_map::Dict{Int,String}
valid_ix::Vector{Int}
power_network_matrix::M
aux_network_matrix::N
Expand Down Expand Up @@ -119,8 +119,8 @@ end
# TODO -> MULTI PERIOD: AC Power Flow Data
function _calculate_neighbors(
Yb::PNM.Ybus{
Tuple{Vector{Int64}, Vector{Int64}},
Tuple{Dict{Int64, Int64}, Dict{Int64, Int64}},
Tuple{Vector{Int64},Vector{Int64}},
Tuple{Dict{Int64,Int64},Dict{Int64,Int64}},
},
)
I, J, V = SparseArrays.findnz(Yb.data)
Expand Down Expand Up @@ -157,11 +157,11 @@ NOTE: use it for AC power flow computations.
WARNING: functions for the evaluation of the multi-period AC PF still to be implemented.
"""
function PowerFlowData(
::Union{NLSolveACPowerFlow, KLUACPowerFlow},
::Union{NLSolveACPowerFlow,KLUACPowerFlow},
sys::PSY.System;
time_steps::Int = 1,
timestep_names::Vector{String} = String[],
check_connectivity::Bool = true)
time_steps::Int=1,
timestep_names::Vector{String}=String[],
check_connectivity::Bool=true)

# assign timestep_names
# timestep names are then allocated in a dictionary to map matrix columns
Expand All @@ -174,7 +174,7 @@ function PowerFlowData(
end

# get data for calculations
power_network_matrix = PNM.Ybus(sys; check_connectivity = check_connectivity)
power_network_matrix = PNM.Ybus(sys; check_connectivity=check_connectivity)

# get number of buses and branches
n_buses = length(axes(power_network_matrix, 1))
Expand All @@ -185,8 +185,8 @@ function PowerFlowData(
n_branches = length(branches)

bus_lookup = power_network_matrix.lookup[2]
branch_lookup = Dict{String, Int}()
temp_bus_map = Dict{Int, String}(
branch_lookup = Dict{String,Int}()
temp_bus_map = Dict{Int,String}(
PSY.get_number(b) => PSY.get_name(b) for b in PSY.get_components(PSY.Bus, sys)
)
branch_types = Vector{DataType}(undef, n_branches)
Expand Down Expand Up @@ -250,9 +250,9 @@ NOTE: use it for DC power flow computations.
function PowerFlowData(
::DCPowerFlow,
sys::PSY.System;
time_steps::Int = 1,
timestep_names::Vector{String} = String[],
check_connectivity::Bool = true)
time_steps::Int=1,
timestep_names::Vector{String}=String[],
check_connectivity::Bool=true)

# assign timestep_names
# timestep names are then allocated in a dictionary to map matrix columns
Expand All @@ -263,7 +263,7 @@ function PowerFlowData(
end

# get the network matrices
power_network_matrix = PNM.ABA_Matrix(sys; factorize = true)
power_network_matrix = PNM.ABA_Matrix(sys; factorize=true)
aux_network_matrix = PNM.BA_Matrix(sys)

# get number of buses and branches
Expand All @@ -272,7 +272,7 @@ function PowerFlowData(

bus_lookup = aux_network_matrix.lookup[1]
branch_lookup = aux_network_matrix.lookup[2]
temp_bus_map = Dict{Int, String}(
temp_bus_map = Dict{Int,String}(
PSY.get_number(b) => PSY.get_name(b) for b in PSY.get_components(PSY.ACBus, sys)
)
valid_ix = setdiff(1:n_buses, aux_network_matrix.ref_bus_positions)
Expand Down Expand Up @@ -317,9 +317,9 @@ NOTE: use it for DC power flow computations.
function PowerFlowData(
::PTDFDCPowerFlow,
sys::PSY.System;
time_steps::Int = 1,
timestep_names::Vector{String} = String[],
check_connectivity::Bool = true)
time_steps::Int=1,
timestep_names::Vector{String}=String[],
check_connectivity::Bool=true)

# assign timestep_names
# timestep names are then allocated in a dictionary to map matrix columns
Expand All @@ -333,15 +333,15 @@ function PowerFlowData(

# get the network matrices
power_network_matrix = PNM.PTDF(sys)
aux_network_matrix = PNM.ABA_Matrix(sys; factorize = true)
aux_network_matrix = PNM.ABA_Matrix(sys; factorize=true)

# get number of buses and branches
n_buses = length(axes(power_network_matrix, 1))
n_branches = length(axes(power_network_matrix, 2))

bus_lookup = power_network_matrix.lookup[1]
branch_lookup = power_network_matrix.lookup[2]
temp_bus_map = Dict{Int, String}(
temp_bus_map = Dict{Int,String}(
PSY.get_number(b) => PSY.get_name(b) for b in PSY.get_components(PSY.Bus, sys)
)
valid_ix = setdiff(1:n_buses, aux_network_matrix.ref_bus_positions)
Expand Down Expand Up @@ -385,9 +385,9 @@ NOTE: use it for DC power flow computations.
function PowerFlowData(
::vPTDFDCPowerFlow,
sys::PSY.System;
time_steps::Int = 1,
timestep_names::Vector{String} = String[],
check_connectivity::Bool = true)
time_steps::Int=1,
timestep_names::Vector{String}=String[],
check_connectivity::Bool=true)

# assign timestep_names
# timestep names are then allocated in a dictionary to map matrix columns
Expand All @@ -401,15 +401,15 @@ function PowerFlowData(

# get the network matrices
power_network_matrix = PNM.VirtualPTDF(sys) # evaluates an empty virtual PTDF
aux_network_matrix = PNM.ABA_Matrix(sys; factorize = true)
aux_network_matrix = PNM.ABA_Matrix(sys; factorize=true)

# get number of buses and branches
n_buses = length(axes(power_network_matrix, 2))
n_branches = length(axes(power_network_matrix, 1))

bus_lookup = power_network_matrix.lookup[2]
branch_lookup = power_network_matrix.lookup[1]
temp_bus_map = Dict{Int, String}(
temp_bus_map = Dict{Int,String}(
PSY.get_number(b) => PSY.get_name(b) for b in PSY.get_components(PSY.Bus, sys)
)
valid_ix = setdiff(1:n_buses, aux_network_matrix.ref_bus_positions)
Expand Down
26 changes: 13 additions & 13 deletions src/nlsolve_ac_powerflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ solve_ac_powerflow!(sys)
solve_ac_powerflow!(sys, method=:newton)
```
"""
function solve_ac_powerflow!(pf::Union{NLSolveACPowerFlow, KLUACPowerFlow}, system::PSY.System; kwargs...)
function solve_ac_powerflow!(pf::Union{NLSolveACPowerFlow,KLUACPowerFlow}, system::PSY.System; kwargs...)
#Save per-unit flag
settings_unit_cache = deepcopy(system.units_settings.unit_system)
#Work in System per unit
PSY.set_units_base_system!(system, "SYSTEM_BASE")
check_reactive_power_limits = get(kwargs, :check_reactive_power_limits, false)
data = PowerFlowData(
NLSolveACPowerFlow(; check_reactive_power_limits = check_reactive_power_limits),
NLSolveACPowerFlow(; check_reactive_power_limits=check_reactive_power_limits),
system;
check_connectivity = get(kwargs, :check_connectivity, true),
check_connectivity=get(kwargs, :check_connectivity, true),
)
max_iterations = DEFAULT_MAX_REDISTRIBUTION_ITERATIONS
converged, x = _solve_powerflow!(pf, data, check_reactive_power_limits; kwargs...)
Expand Down Expand Up @@ -68,7 +68,7 @@ res = solve_powerflow(sys, method=:newton)
```
"""
function solve_powerflow(
pf::Union{NLSolveACPowerFlow, KLUACPowerFlow},
pf::Union{NLSolveACPowerFlow,KLUACPowerFlow},
system::PSY.System;
kwargs...,
)
Expand All @@ -79,7 +79,7 @@ function solve_powerflow(
data = PowerFlowData(
pf,
system;
check_connectivity = get(kwargs, :check_connectivity, true),
check_connectivity=get(kwargs, :check_connectivity, true),
)

converged, x = _solve_powerflow!(pf, data, pf.check_reactive_power_limits; kwargs...)
Expand All @@ -101,7 +101,7 @@ function _check_q_limit_bounds!(data::ACPowerFlowData, zero::Vector{Float64})
within_limits = true
for (ix, b) in enumerate(data.bus_type)
if b == PSY.ACBusTypes.PV
Q_gen = zero[2 * ix - 1]
Q_gen = zero[2*ix-1]
else
continue
end
Expand All @@ -124,7 +124,7 @@ function _check_q_limit_bounds!(data::ACPowerFlowData, zero::Vector{Float64})
end

function _solve_powerflow!(
pf::Union{NLSolveACPowerFlow, KLUACPowerFlow},
pf::Union{NLSolveACPowerFlow,KLUACPowerFlow},
data::ACPowerFlowData,
check_reactive_power_limits;
nlsolve_kwargs...,
Expand Down Expand Up @@ -235,16 +235,16 @@ function _nlsolve_powerflow(pf::KLUACPowerFlow, data::ACPowerFlowData; nlsolve_k
for (ix, b) in enumerate(data.bus_type)
if b == PSY.ACBusTypes.REF
# When bustype == REFERENCE PSY.Bus, state variables are Active and Reactive Power Generated
x[2 * ix - 1] = real(Sbus_result[ix]) + data.bus_activepower_withdrawals[ix]
x[2 * ix] = imag(Sbus_result[ix]) + data.bus_reactivepower_withdrawals[ix]
x[2*ix-1] = real(Sbus_result[ix]) + data.bus_activepower_withdrawals[ix]
x[2*ix] = imag(Sbus_result[ix]) + data.bus_reactivepower_withdrawals[ix]
elseif b == PSY.ACBusTypes.PV
# When bustype == PV PSY.Bus, state variables are Reactive Power Generated and Voltage Angle
x[2 * ix - 1] = imag(Sbus_result[ix]) + data.bus_reactivepower_withdrawals[ix]
x[2 * ix] = Va[ix]
x[2*ix-1] = imag(Sbus_result[ix]) + data.bus_reactivepower_withdrawals[ix]
x[2*ix] = Va[ix]
elseif b == PSY.ACBusTypes.PQ
# When bustype == PQ PSY.Bus, state variables are Voltage Magnitude and Voltage Angle
x[2 * ix - 1] = Vm[ix]
x[2 * ix] = Va[ix]
x[2*ix-1] = Vm[ix]
x[2*ix] = Va[ix]
end
end

Expand Down
Loading

0 comments on commit 210937c

Please sign in to comment.