Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug 74: enforcing reactive power limits for AC power flow #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/nlsolve_ac_powerflow.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _SOLVE_AC_POWERFLOW_KWARGS = Set([:check_reactive_power_limits, :check_connectivity])
"""
Solves a the power flow into the system and writes the solution into the relevant structs.
Updates generators active and reactive power setpoints and branches active and reactive
Expand Down Expand Up @@ -42,9 +43,10 @@ function solve_ac_powerflow!(system::PSY.System; kwargs...)
check_connectivity = get(kwargs, :check_connectivity, true),
)
max_iterations = DEFAULT_MAX_REDISTRIBUTION_ITERATIONS
res = _solve_powerflow!(data, check_reactive_power_limits; kwargs...)
solver_kwargs = filter(p -> !(p.first in _SOLVE_AC_POWERFLOW_KWARGS), kwargs)
res = _solve_powerflow!(data, check_reactive_power_limits; solver_kwargs...)
if res.f_converged
write_powerflow_solution!(system, res.zero, max_iterations)
write_powerflow_solution!(system, res.zero, data, max_iterations)
@info("PowerFlow solve converged, the results have been stored in the system")
#Restore original per unit base
PSY.set_units_base_system!(system, settings_unit_cache)
Expand Down
15 changes: 15 additions & 0 deletions src/post_processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,27 @@ Updates system voltages and powers with power flow results
function write_powerflow_solution!(
sys::PSY.System,
result::Vector{Float64},
data::PowerFlowData,
max_iterations::Int,
)
buses = enumerate(
sort!(collect(PSY.get_components(PSY.Bus, sys)); by = x -> PSY.get_number(x)),
)

# Handle any changes made manually to the PowerFlowData, not necessarily reflected in the solver result
# Right now the only such change we handle is the one in _check_q_limit_bounds!
for (ix, bus) in buses
system_bustype = PSY.get_bustype(bus)
data_bustype = data.bus_type[ix]
(system_bustype == data_bustype) && continue
@assert system_bustype == PSY.ACBusTypes.PV
@assert data_bustype == PSY.ACBusTypes.PQ
@debug "Updating bus $(PSY.get_name(bus)) reactive power and type to PQ due to check_reactive_power_limits"
Q_gen = data.bus_reactivepower_injection[ix]
_reactive_power_redistribution_pv(sys, Q_gen, bus, max_iterations)
PSY.set_bustype!(bus, data_bustype)
end

for (ix, bus) in buses
if bus.bustype == PSY.ACBusTypes.REF
P_gen = result[2 * ix - 1]
Expand Down
24 changes: 22 additions & 2 deletions test/test_nlsolve_powerflow.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@testset "NLsolve Power Flow 14-Bus testing" begin
sys = PSB.build_system(PSB.PSITestSystems, "c_sys14"; add_forecasts = false)
set_units_base_system!(sys, UnitSystem.SYSTEM_BASE)
result_14 = [
2.3255081760423684
-0.15529254415401786
Expand Down Expand Up @@ -34,9 +35,28 @@
#Compare results between finite diff methods and Jacobian method
res1 = PowerFlows._solve_powerflow!(data, false)
@test LinearAlgebra.norm(result_14 - res1.zero) <= 1e-6
@test solve_ac_powerflow!(sys; method = :newton)

# Test enforcing the reactive power Limits
# Test that solve_ac_powerflow! succeeds
solved1 = deepcopy(sys)
@test solve_ac_powerflow!(solved1)

# Test that passing check_reactive_power_limits=false is the default and violates limits
solved2 = deepcopy(sys)
@test solve_ac_powerflow!(solved2; check_reactive_power_limits = false)
@test IS.compare_values(solved1, solved2)
@test get_reactive_power(get_component(ThermalStandard, solved2, "Bus8")) >
get_reactive_power_limits(get_component(ThermalStandard, solved2, "Bus8")).max

# Test that passing check_reactive_power_limits=true fixes that
solved3 = deepcopy(sys)
@test solve_ac_powerflow!(solved3; check_reactive_power_limits = true)
@test get_reactive_power(get_component(ThermalStandard, solved3, "Bus8")) <=
get_reactive_power_limits(get_component(ThermalStandard, solved3, "Bus8")).max

# Test Newton method
@test solve_ac_powerflow!(deepcopy(sys); method = :newton)

# Test enforcing the reactive power limits in closer detail
set_reactive_power!(get_component(PowerLoad, sys, "Bus4"), 0.0)
data = PowerFlows.PowerFlowData(ACPowerFlow(), sys; check_connectivity = true)
res2 = PowerFlows._solve_powerflow!(data, true)
Expand Down
Loading