Solving a Power Flow

To follow along, you can download this tutorial as a Julia script (.jl) or Jupyter notebook (.ipynb).

In this tutorial, you'll solve power flows on a 5-bus test system using three different solvers and compare their results.

Building a System

To get started, load the needed packages.

using PowerSystemCaseBuilder
using PowerFlows
using PowerSystems

Create a PowerSystems.System from PowerSystemCaseBuilder.build_system. We build the test system with runchecks = false to reduce REPL output:

sys = build_system(MatpowerTestSystems, "matpower_case5_sys"; runchecks = false)
System
PropertyValue
Name
Description
System Units BaseSYSTEM_BASE
Base Power100.0
Base Frequency60.0
Num Components28
Static Components
TypeCount
ACBus5
Arc6
Area1
Line5
LoadZone1
PhaseShiftingTransformer2
PowerLoad3
ThermalStandard5

DC Power Flow

DCPowerFlow solves for bus voltage angles using the bus admittance matrix, then computes branch flows from the angle differences. Create a DCPowerFlow solver:

pf_dc = DCPowerFlow()
DCPowerFlow(nothing, nothing, false, false, PowerNetworkMatrices.NetworkReduction[], 1, String[], false, false)

Solve the power flow with solve_power_flow. For DC methods, pass a FlowReporting mode so flows are reported on the same arc basis as elsewhere in the package:

dc_results = solve_power_flow(pf_dc, sys, FlowReporting.ARC_FLOWS)
Dict{String, Dict{String, DataFrame}} with 1 entry:
  "1" => Dict("flow_results"=>6×10 DataFrame

The result is a Dict{String, Dict{String, DataFrame}}. The outer key is the time step name: "1". The inner dictionary stores the power flow results at that time step: "bus_results" for bus data and "flow_results" for AC line data. (There is also a third key, "lcc_results", for HVDC lines, but this system contains no such components, so the matching dataframe will be empty.) Inspect "bus_results":

dc_results["1"]["bus_results"]
bus_numberVmθP_genP_loadP_netQ_genQ_loadQ_net
Int64Float64Float64Float64Float64Float64Float64Float64Float64
11.00.05722780713861989210.00.0210.00.00.00.0
21.0-0.0131547464565413990.0300.0-300.00.00.00.0
31.0-0.007805692274258128324.498300.024.4979999999999980.00.00.0
41.00.0-5.192000000000085400.0-405.192000000000060.00.00.0
101.00.07186595687488434470.6940.0470.6940.00.00.0

Notice that Vm (voltage magnitude) is 1.0 for all buses, and Q_gen and Q_load are 0. This is expected for DC power flow, which assumes flat voltage magnitudes and ignores reactive power.

dc_results["1"]["flow_results"]
flow_namebus_frombus_toP_from_toQ_from_toP_to_fromQ_to_fromP_lossesQ_lossesangle_difference
StringInt64Int64Float64Float64Float64Float64Float64Float64Float64
1-212250.471721743600680.0-250.471721743600680.01.76288394334902470.00.0703825535951613
1-414188.24936788553130.0-188.24936788553130.01.07730986508278440.00.05722780713861989
1-10110-228.721089629132020.0228.721089629132020.00.334805355783279630.0-0.014638149736264448
2-323-49.5282782563993250.049.5282782563993250.00.026492943748067840.0-0.005349054182283271
3-434-25.030278256399320.025.030278256399320.00.020514758208893270.0-0.007805692274258128
4-10410-241.97291037086810.0241.97291037086810.01.73896141379444070.0-0.07186595687488434

Likewise, Q_from_to and Q_to_from (reactive power flow on the line) are zero, for all lines.

PTDF DC Power Flow

PTDFDCPowerFlow computes branch flows directly from bus power injections using the Power Transfer Distribution Factor matrix, without solving for voltage angles as an intermediate step. (This means we can omit the angle computation in contexts where we only care about line flows, though we don't have that option implemented here.) Create a PTDFDCPowerFlow solver:

pf_ptdf = PTDFDCPowerFlow()
PTDFDCPowerFlow(nothing, false, nothing, false, false, PowerNetworkMatrices.NetworkReduction[], 1, String[], false)

As before, solve the power flow with solve_power_flow:

ptdf_results = solve_power_flow(pf_ptdf, sys, FlowReporting.ARC_FLOWS)
Dict{String, Dict{String, DataFrame}} with 1 entry:
  "1" => Dict("flow_results"=>6×10 DataFrame

Look at the bus results:

ptdf_results["1"]["bus_results"]
bus_numberVmθP_genP_loadP_netQ_genQ_loadQ_net
Int64Float64Float64Float64Float64Float64Float64Float64Float64
11.00.05722780713861989210.00.0210.00.00.00.0
21.0-0.0131547464565413990.0300.0-300.00.00.00.0
31.0-0.007805692274258128324.498300.024.4979999999999980.00.00.0
41.00.0-5.192000000000085400.0-405.192000000000060.00.00.0
101.00.07186595687488434470.6940.0470.6940.00.00.0

The results match DCPowerFlow, as they should: the two are mathematically equivalent. For very large systems where forming the full PTDF matrix would be too expensive, consider vPTDFDCPowerFlow, which computes the same results without storing the dense matrix.

AC Power Flow

Create an ACPowerFlow solver:

pf_ac = ACPowerFlow()
ACPolarPowerFlow{NewtonRaphsonACPowerFlow}(false, nothing, false, false, false, nothing, true, false, false, false, PowerNetworkMatrices.NetworkReduction[], 1, String[], false, false, false, 0.05, :lines_only, Dict{Symbol, Any}())

Solve the power flow:

ac_results = solve_power_flow(pf_ac, sys)
Dict{String, DataFrame} with 7 entries:
  "flow_results"             => 6×10 DataFrame…
  "area_interchange_results" => 0×6 DataFrame…
  "bus_results"              => 5×9 DataFrame…
  "mtdc_line_results"        => 0×5 DataFrame…
  "vsc_results"              => 0×11 DataFrame…
  "mtdc_results"             => 0×8 DataFrame…
  "lcc_results"              => 0×13 DataFrame

AC results are returned as a flat Dict{String, DataFrame}, with the same keys as before: "bus_results", "flow_results" (AC lines), and "lcc_results" (HVDC lines). Look at the bus results:

ac_results["bus_results"]
bus_numberVmθP_genP_loadP_netQ_genQ_loadQ_net
Int64Float64Float64Float64Float64Float64Float64Float64Float64
11.077620.05377973212862824210.00.0210.0155.861991067640730.0155.86199106764073
21.08419462213153488.152349463538256e-50.0300.0-300.00.098.61-98.61
31.10.006160589908746271324.498300.024.497999999999998518.18214946631998.61419.57214946631893
41.064140.02.2015611506636112400.0-397.7984388493364-116.00444489674346131.47-247.47444489674345
101.069070.06665597012564134470.6940.0470.694-164.316989208028990.0-164.31698920802899

Notice that Vm now varies across buses (not all 1.0), and Q_gen has non-zero values.

Look at the line flows:

ac_results["flow_results"]
flow_namebus_frombus_toP_from_toQ_from_toP_to_fromQ_to_fromP_lossesQ_lossesangle_difference
StringInt64Int64Float64Float64Float64Float64Float64Float64Float64
1-212219.04815651765563-41.538343627190756-217.8461727746278352.7265081425219361.201983743027801311.1881645153311770.053698208633992855
1-414206.0317623556807432.25263490048205-204.89263500836265-21.6155781513346241.13912734731811410.6370567491474230.05377973212862824
1-10110-215.0798358132005165.14902875652393215.48841730273134-164.663488024999080.408581489530845940.4855407315248472-0.012876237997013103
2-323-82.153855250951-151.3369565517860782.4232748938078151.821703009275720.26941964285679410.4847464574896376-0.006079066414110889
3-434-57.925274893807945267.7504464570448660.60738862294479-241.670086784350332.68211372913684826.080359672694530.006160589908746271
4-10410-253.5131924639186415.812083986954987255.205691693723280.34649881697113861.692499229804633716.158582803926123-0.06665597012564134

Q_from_to and Q_to_from now show reactive power flows, and P_from_to differs from P_to_from due to losses.

Fast Decoupled AC Power Flow

The solver is an independent type parameter of the AC power flow. ACPowerFlow() above used the default NewtonRaphsonACPowerFlow solver, which refactorizes the Jacobian every iteration. FastDecoupledACPowerFlow instead builds constant approximate Jacobian matrices ($B'$/$B''$) once and reuses them across all iterations and time steps — the fast decoupled power flow of Stott & Alsac, equivalent to PSS/E's FDNS. It converges at a linear rate but at a fraction of the per-iteration cost, which makes it well suited to repeated solves (multi-period dispatch, contingency screening). Select it as a solver type parameter:

pf_fd = ACPowerFlow{FastDecoupledACPowerFlow}()
ACPolarPowerFlow{FastDecoupledACPowerFlow}(false, nothing, false, false, false, nothing, true, false, false, false, PowerNetworkMatrices.NetworkReduction[], 1, String[], false, false, false, 0.05, :lines_only, Dict{Symbol, Any}())

Solve it the same way as any other AC solver:

fd_results = solve_power_flow(pf_fd, sys)
Dict{String, DataFrame} with 7 entries:
  "flow_results"             => 6×10 DataFrame…
  "area_interchange_results" => 0×6 DataFrame…
  "bus_results"              => 5×9 DataFrame…
  "mtdc_line_results"        => 0×5 DataFrame…
  "vsc_results"              => 0×11 DataFrame…
  "mtdc_results"             => 0×8 DataFrame…
  "lcc_results"              => 0×13 DataFrame

The converged solution is identical to the Newton-Raphson result to tolerance: fast decoupled evaluates the exact mismatches every iteration, so only the convergence rate differs, never the solution. Compare the bus results:

fd_results["bus_results"]
bus_numberVmθP_genP_loadP_netQ_genQ_loadQ_net
Int64Float64Float64Float64Float64Float64Float64Float64Float64
11.077620.0537797321284554210.00.0210.0155.861991064216450.0155.86199106421645
21.08419462213145258.152349384123624e-50.0300.0-300.00.098.61-98.61
31.10.0061605899086595835324.498300.024.497999999999998518.182149456615898.61419.57214945661576
41.064140.02.2015611506695176400.0-397.79843884933047-116.00444489698154131.47-247.47444489698154
101.069070.06665597012550893470.6940.0470.694-164.316989208072070.0-164.31698920807207

Handoff to Newton-Raphson

Because the fast decoupled state is a valid iterate of the exact residual, it is also a good warm start for an exact-Newton solver. You can optionally run the cheap fast decoupled stage to a looser tolerance, then hand off to NewtonRaphsonACPowerFlow for final refinement, via solver_settings:

pf_handoff = ACPowerFlow{FastDecoupledACPowerFlow}(;
    solver_settings = Dict(:handoff_solver => NewtonRaphsonACPowerFlow),
)
handoff_results = solve_power_flow(pf_handoff, sys)
Dict{String, DataFrame} with 7 entries:
  "flow_results"             => 6×10 DataFrame…
  "area_interchange_results" => 0×6 DataFrame…
  "bus_results"              => 5×9 DataFrame…
  "mtdc_line_results"        => 0×5 DataFrame…
  "vsc_results"              => 0×11 DataFrame…
  "mtdc_results"             => 0×8 DataFrame…
  "lcc_results"              => 0×13 DataFrame

This staging — fast decoupled as a cheap conditioner, Newton-Raphson as the closer — mirrors commercial practice. For the underlying math ($B'$/$B''$, the XB/BX schemes, the fixed-Jacobian variant, and a solver guidance table), see Fast/Fixed Decoupled Power Flow.

When AC Power Flow Fails

Unlike DC power flow, AC power flow is iterative and not guaranteed to converge. Systems with high impedance lines, poor initial voltage profiles, or insufficient reactive power support can cause the solver to fail. When this happens, solve_power_flow returns missing: you'll also see a logged error. If you encounter convergence failures, consider using a more robust solver such as TrustRegionACPowerFlow or RobustHomotopyPowerFlow.

Next Steps