S1051 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

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 Vi←1Yii[(SiVi)*−∑j≠iYijVj]. Where does that come from?

Name it

The branch model

Each branch (i, j, r, x, b, a) is a series admittance y=1/(r+jx) 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 Ybus:

Yii+=ya2+jb2,Yjj+=y+jb2,Yij=Yji−=ya

Injections

With all bus voltages in a complex vector V, the injections are S=V⊙(YV)*, 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
  1. The matrix. Y=−j(15−10−5−1014−4−5−49)
  2. Diagonal entries. Each diagonal sums the admittances of the branches at that bus.
  3. Off-diagonal entries. Each off-diagonal is minus the admittance between the pair.
  4. 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
  1. The inputs. Y=(−j5j5j5−j5) and V = (1, 0.8554∠−13.52°).
  2. 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).
  3. 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?

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.