S10 · Lesson 51 · Power-flow lab
Writing the first half of a power-flow program in Python, a bus admittance matrix builder with line charging and off-nominal taps, a vectorized power-injection calculation, and a Gauss–Seidel solver checked against the two-bus case and a lossy three-bus network
Every power-flow program, from a 1960s FORTRAN deck to the tools that run today's grids, is built from the same three pieces: assemble the bus admittance matrix from the branch list, compute the power each bus injects for a given set of voltages, and iterate the voltages until the injections match the specifications. In this lab you'll write all three, and the project that follows adds Newton–Raphson with generator buses, branch flows and fault calculations.
Predict
Your Gauss–Seidel solver updates bus i with . Where does that come from?
Conjugating gives . Pull the j = i term out and solve for it. The on the left uses the old value, which is why the method needs iterating.
Name it
The branch model
Each branch (i, j, r, x, b, a) is a series admittance with total charging b split between its ends, and an ideal transformer of ratio a:1 at bus i's end (a = 1 for lines). Its contribution to :
Injections
With all bus voltages in a complex vector V, the injections are , one line of NumPy.
Gauss–Seidel
Bus 0 is the slack. Sweep buses 1 to n − 1 with the update above, using each new value at once, and stop when the largest change in a sweep is below the tolerance. Lesson 18 showed the two-bus case takes 12 sweeps to reach 10⁻⁶.
Worked example
What should build_ybus produce for the network of lesson 10 (branches j0.1, j0.2 and j0.25 between buses 0–1, 0–2 and 1–2)?
Show the solution
- The matrix.
- Diagonal entries. Each diagonal sums the admittances of the branches at that bus.
- Off-diagonal entries. Each off-diagonal is minus the admittance between the pair.
- The generator. Lesson 10 also had the generator's −j5 at bus 0, a shunt added separately.
Answer diagonal entries of −j15, −j14 and −j9, and off-diagonal entries of j10 (0–1), j5 (0–2) and j4 (1–2).
Worked example
Check injections on the two-bus solution of lesson 17.
Show the solution
- The inputs. and V = (1, 0.8554∠−13.52°).
- The injections. The load bus injects −1 − j0.5 (the load) and the slack 1 + j0.842 (the load plus the line's 0.342 pu of vars).
- A common bug. If your conjugate is in the wrong place, the second number comes out with the wrong sign.
Answer −1 − j0.5 at the load bus and 1 + j0.842 at the slack.
Check
Step 1. Write build_ybus(n, branches): series admittances, line charging and off-nominal taps.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write injections(Y, V): the complex power injected at every bus.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write gauss_seidel(Y, S, V0): the slack held fixed, every other bus PQ.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does the slack bus never get updated in the Gauss–Seidel sweep?
The slack bus's voltage magnitude and angle are specified: it's the reference for every angle in the system and the bus that balances generation, load and losses, which aren't known until the solution is found. So its voltage is an input, not an unknown, and the equations being solved are those of the other buses. Its injection is whatever comes out at the end, computed from the converged voltages with the same injection formula. Updating it would remove the angle reference and leave the problem with one more unknown than equations.
Practice
Keep practicing
3 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.