Skip to content

Commit

Permalink
formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Bolgaryn committed Dec 16, 2024
1 parent b0bc565 commit 42f02ab
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/PowerFlowData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ 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(
::ACPowerFlow{<: ACPowerFlowSolverType},
::ACPowerFlow{<:ACPowerFlowSolverType},
sys::PSY.System;
time_steps::Int = 1,
timestep_names::Vector{String} = String[],
Expand Down Expand Up @@ -440,7 +440,11 @@ Create an appropriate `PowerFlowContainer` for the given `PowerFlowEvaluationMod
"""
function make_power_flow_container end

make_power_flow_container(pfem::ACPowerFlow{<: ACPowerFlowSolverType}, sys::PSY.System; kwargs...) =
make_power_flow_container(
pfem::ACPowerFlow{<:ACPowerFlowSolverType},
sys::PSY.System;
kwargs...,
) =
PowerFlowData(pfem, sys; kwargs...)

make_power_flow_container(pfem::DCPowerFlow, sys::PSY.System; kwargs...) =
Expand Down
18 changes: 12 additions & 6 deletions src/newton_ac_powerflow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ solve_ac_powerflow!(sys, method=:newton)
```
"""
function solve_ac_powerflow!(
pf::ACPowerFlow{<: ACPowerFlowSolverType},
pf::ACPowerFlow{<:ACPowerFlowSolverType},
system::PSY.System;
kwargs...,
)
Expand Down Expand Up @@ -72,7 +72,7 @@ res = solve_powerflow(sys, method=:newton)
```
"""
function solve_powerflow(
pf::ACPowerFlow{<: ACPowerFlowSolverType},
pf::ACPowerFlow{<:ACPowerFlowSolverType},
system::PSY.System;
kwargs...,
)
Expand Down Expand Up @@ -128,7 +128,7 @@ function _check_q_limit_bounds!(data::ACPowerFlowData, zero::Vector{Float64})
end

function _solve_powerflow!(
pf::ACPowerFlow{<: ACPowerFlowSolverType},
pf::ACPowerFlow{<:ACPowerFlowSolverType},
data::ACPowerFlowData,
check_reactive_power_limits;
nlsolve_kwargs...,
Expand Down Expand Up @@ -160,12 +160,18 @@ function _newton_powerflow(
df = NLsolve.OnceDifferentiable(pf, J, pf.x0, pf.residual, J.Jv)
res = NLsolve.nlsolve(df, pf.x0; nlsolve_kwargs...)
if !res.f_converged
@error("The powerflow solver NLSolve did not converge (returned convergence = $(res.f_converged))")
@error(
"The powerflow solver NLSolve did not converge (returned convergence = $(res.f_converged))"
)
end
return res.f_converged, res.zero
end

function _newton_powerflow(pf::ACPowerFlow{KLUACPowerFlow}, data::ACPowerFlowData; nlsolve_kwargs...)
function _newton_powerflow(
pf::ACPowerFlow{KLUACPowerFlow},
data::ACPowerFlowData;
nlsolve_kwargs...,
)
# Fetch maxIter and tol from kwargs, or use defaults if not provided
maxIter = get(nlsolve_kwargs, :maxIter, DEFAULT_NR_MAX_ITER)
tol = get(nlsolve_kwargs, :tol, DEFAULT_NR_TOL)
Expand All @@ -180,7 +186,7 @@ function _newton_powerflow(pf::ACPowerFlow{KLUACPowerFlow}, data::ACPowerFlowDat

Vm = data.bus_magnitude[:]
# prevent unfeasible starting values for Vm; for pv and ref buses we cannot do this:
Vm[pq] = clamp.(Vm[pq], 0.9, 1.1)
Vm[pq] = clamp.(Vm[pq], 0.9, 1.1)
Va = data.bus_angles[:]
V = Vm .* exp.(1im * Va)

Expand Down
2 changes: 1 addition & 1 deletion src/post_processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ dictionary will therefore feature just one key linked to one DataFrame.
vector containing the reults for one single time-period.
"""
function write_results(
::ACPowerFlow{<: ACPowerFlowSolverType},
::ACPowerFlow{<:ACPowerFlowSolverType},
sys::PSY.System,
data::ACPowerFlowData,
result::Vector{Float64},
Expand Down
9 changes: 6 additions & 3 deletions src/powerflow_types.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
abstract type PowerFlowEvaluationModel end
abstract type ACPowerFlowSolverType end


struct KLUACPowerFlow <: ACPowerFlowSolverType end
struct NLSolveACPowerFlow <: ACPowerFlowSolverType end

Base.@kwdef struct ACPowerFlow{ACSolver <: ACPowerFlowSolverType} <: PowerFlowEvaluationModel
Base.@kwdef struct ACPowerFlow{ACSolver <: ACPowerFlowSolverType} <:
PowerFlowEvaluationModel
check_reactive_power_limits::Bool = false
end

# Create a constructor that defaults to KLUACPowerFlow
function ACPowerFlow(; check_reactive_power_limits::Bool = false, ACSolver::Type{<:ACPowerFlowSolverType} = KLUACPowerFlow)
function ACPowerFlow(;
check_reactive_power_limits::Bool = false,
ACSolver::Type{<:ACPowerFlowSolverType} = KLUACPowerFlow,
)
return ACPowerFlow{ACSolver}(check_reactive_power_limits)
end

Expand Down
8 changes: 6 additions & 2 deletions test/test_powerflow_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ end
PF.vPTDFPowerFlowData
end

@testset "System <-> PowerFlowData round trip" for ACSolver in (NLSolveACPowerFlow, KLUACPowerFlow)
@testset "System <-> PowerFlowData round trip" for ACSolver in
(NLSolveACPowerFlow, KLUACPowerFlow)
# TODO currently only tested with ACPowerFlow
# TODO test that update_system! errors if the PowerFlowData doesn't correspond to the system

Expand Down Expand Up @@ -57,6 +58,9 @@ end
sys_modify_updated = deepcopy(sys_original)
PF.update_system!(sys_modify_updated, data_modified)
sys_mod_redist = deepcopy(sys_modified)
PF.update_system!(sys_mod_redist, PowerFlowData(ACPowerFlow{ACSolver}(), sys_mod_redist))
PF.update_system!(
sys_mod_redist,
PowerFlowData(ACPowerFlow{ACSolver}(), sys_mod_redist),
)
@test IS.compare_values(powerflow_match_fn, sys_modify_updated, sys_mod_redist)
end
17 changes: 14 additions & 3 deletions test/test_psse_export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ function compare_systems_loosely(sys1::PSY.System, sys2::PSY.System;
return result
end

function test_power_flow(pf::ACPowerFlow{<:ACPowerFlowSolverType}, sys1::System, sys2::System; exclude_reactive_flow = false)
function test_power_flow(
pf::ACPowerFlow{<:ACPowerFlowSolverType},
sys1::System,
sys2::System;
exclude_reactive_flow = false,
)
result1 = solve_powerflow(pf, sys1)
result2 = solve_powerflow(pf, sys2)
reactive_power_tol =
Expand Down Expand Up @@ -319,7 +324,10 @@ end
@test compare_systems_loosely(sys, deepcopy(sys))
end

@testset "PSSE Exporter with system_240[32].json, v33" for (ACSolver, folder_name) in ((NLSolveACPowerFlow, "system_240_NLSolve"), (KLUACPowerFlow, "system_240_KLU"))
@testset "PSSE Exporter with system_240[32].json, v33" for (ACSolver, folder_name) in (
(NLSolveACPowerFlow, "system_240_NLSolve"),
(KLUACPowerFlow, "system_240_KLU"),
)
sys = load_test_system()
pf = ACPowerFlow{ACSolver}()
isnothing(sys) && return
Expand Down Expand Up @@ -365,7 +373,10 @@ end
test_power_flow(pf, sys2, reread_sys2; exclude_reactive_flow = true) # TODO why is reactive flow not matching?
end

@testset "PSSE Exporter with RTS_GMLC_DA_sys, v33" for (ACSolver, folder_name) in ((NLSolveACPowerFlow, "rts_gmlc_NLSolve"), (KLUACPowerFlow, "rts_gmlc_KLU"))
@testset "PSSE Exporter with RTS_GMLC_DA_sys, v33" for (ACSolver, folder_name) in (
(NLSolveACPowerFlow, "rts_gmlc_NLSolve"),
(KLUACPowerFlow, "rts_gmlc_KLU"),
)
sys = create_pf_friendly_rts_gmlc()
pf = ACPowerFlow{ACSolver}()
set_units_base_system!(sys, UnitSystem.SYSTEM_BASE)
Expand Down

0 comments on commit 42f02ab

Please sign in to comment.