---
title: "Fault lab: Z-bus and fault currents in code"
heading: Inverting Y-bus into Z-bus with complex arithmetic, fault currents for every fault type from the sequence impedances, phase currents from sequence components, and every bus's voltage during a fault
eyebrow: S10 · Lesson 42 · Fault lab
xp: 60
---

::: hook
Lessons 36 to 40 computed fault currents by hand for one bus at a time. A fault study program does it for every bus and every fault type, to size breakers and set relays across a whole network. Its core is short: build $Z_\text{bus}$, read each bus's Thévenin impedance off the diagonal, connect the sequence networks for each fault type, and turn sequence currents back into phase currents. In this lab you'll write that core and check it against the hand results.
:::

::: play
- The curves are the fault currents at a bus with $Z_1 = Z_2$ = j0.25 pu, against its zero-sequence impedance $Z_0$: three-phase $1/Z_1$, line-to-line $\sqrt3/(Z_1 + Z_2)$ and single line-to-ground $3/(Z_1 + Z_2 + Z_0)$.
- Where $Z_0$ is smaller than $Z_1$, near solidly grounded wye transformers, the ground fault is the largest; with $Z_0$ above $Z_1$ it falls below the three-phase fault.
- The line-to-line fault is always $\sqrt3/2$ = 87% of the three-phase fault when $Z_2 = Z_1$.
:::

```sim plot
view: graph
x: z = 0.02..1
y: I = 0..7
plot: 1/0.25 + 0*z | label: three-phase
plot: sqrt(3)/0.5 + 0*z | label: line to line
plot: 3/(0.5 + z) | label: line to ground
```

::: predict
## A bus has $Z_1 = Z_2$ = j0.25 pu. What zero-sequence impedance makes its single line-to-ground fault current equal to its three-phase fault current?

- [ ] j0.5 pu
- [x] j0.25 pu, the same as Z₁
- [ ] Zero
- [ ] j0.75 pu

::: reveal
Setting $3/(2Z_1 + Z_0) = 1/Z_1$ gives $Z_0 = Z_1$. Below that the ground fault is larger, which is why breakers near grounded-wye transformers are often sized for ground faults rather than three-phase ones.
:::
:::

::: nameit
## What the lab computes

- **Z-bus** by Gauss–Jordan inversion of the complex $Y_\text{bus}$, the same elimination as for real matrices with complex pivots.
- **Fault currents** at a bus with sequence impedances $Z_1, Z_2, Z_0$ and fault impedance $Z_f$, from 1 pu prefault:
  - three-phase: $1/(Z_1 + Z_f)$;
  - line-to-line: $I_{a1} = 1/(Z_1 + Z_2 + Z_f)$, $|I_b| = \sqrt3|I_{a1}|$;
  - single line-to-ground: $I_{a0} = I_{a1} = I_{a2} = 1/(Z_1 + Z_2 + Z_0 + 3Z_f)$, $I_a = 3I_{a0}$;
  - double line-to-ground: $I_{a1} = 1/(Z_1 + Z_2 \parallel (Z_0 + 3Z_f))$, ground current $3I_{a0}$ with $I_{a0} = -I_{a1}Z_2/(Z_2 + Z_0 + 3Z_f)$.
- **Phase currents** from $I_a = I_0 + I_1 + I_2$, $I_b = I_0 + a^2I_1 + aI_2$, $I_c = I_0 + aI_1 + a^2I_2$, $a = e^{j120°}$.
- **Voltages during a three-phase fault** at bus k: $V_i = 1 - Z_{ik}/(Z_{kk} + Z_f)$.
:::

::: check
kind: python

## Write `invert(Y)`: the inverse of a complex square matrix (list of lists) by Gauss–Jordan elimination with partial pivoting.

```python starter
def invert(Y):
    return Y
```

```python solution
def invert(Y):
    n = len(Y)
    M = [[complex(v) for v in row] + [1 + 0j if i == j else 0j for j in range(n)] for i, row in enumerate(Y)]
    for i in range(n):
        p = max(range(i, n), key=lambda r: abs(M[r][i]))
        M[i], M[p] = M[p], M[i]
        piv = M[i][i]
        M[i] = [v / piv for v in M[i]]
        for r in range(n):
            if r != i:
                f = M[r][i]
                M[r] = [a - f * b for a, b in zip(M[r], M[i])]
    return [row[n:] for row in M]
```

```python tests
Z = invert([[-7.5j, 5j], [5j, -7.5j]])
assert abs(Z[0][0] - 0.24j) < 1e-12 and abs(Z[0][1] - 0.16j) < 1e-12, "Lesson 10's two-bus example: Z11 = j0.24, Z12 = j0.16."
Y = [[3 - 9j, -1 + 3j, -2 + 6j], [-1 + 3j, 2 - 7j, -1 + 4j], [-2 + 6j, -1 + 4j, 3.5 - 10.5j]]
Zi = invert(Y)
I = [[sum(Y[i][k] * Zi[k][j] for k in range(3)) for j in range(3)] for i in range(3)]
assert all(abs(I[i][j] - (1 if i == j else 0)) < 1e-12 for i in range(3) for j in range(3)), "Y times Z is the identity."
```

::: right
Right. Real programs factor Y once and solve for the few Z columns they need, keeping the sparsity; for small networks the full inverse is fine.
:::
:::

::: check
kind: python

## Write `faults(Z1, Z2, Z0, Zf)`: a dict with the magnitudes of the three-phase current ('3ph'), the line-to-line phase current ('LL'), the single line-to-ground fault current ('SLG') and the double line-to-ground ground current ('DLG'), from 1 pu prefault.

```python starter
def faults(Z1, Z2, Z0, Zf):
    return {'3ph': 0, 'LL': 0, 'SLG': 0, 'DLG': 0}
```

```python solution
import math

def faults(Z1, Z2, Z0, Zf):
    i3 = 1 / (Z1 + Zf)
    ill = math.sqrt(3) / abs(Z1 + Z2 + Zf)
    islg = 3 / (Z1 + Z2 + Z0 + 3 * Zf)
    Zg = Z0 + 3 * Zf
    ia1 = 1 / (Z1 + Z2 * Zg / (Z2 + Zg))
    ia0 = -ia1 * Z2 / (Z2 + Zg)
    return {'3ph': abs(i3), 'LL': ill, 'SLG': abs(islg), 'DLG': abs(3 * ia0)}
```

```python tests
f = faults(0.25j, 0.25j, 0.35j, 0)
assert abs(f['3ph'] - 4) < 1e-12 and abs(f['LL'] - 3.4641) < 1e-4, "Three-phase 4 pu, line-to-line √3/0.5."
assert abs(f['SLG'] - 3 / 0.85) < 1e-12, "Line-to-ground 3/0.85 = 3.53 pu."
ia1 = 1 / (0.25 + 0.25 * 0.35 / 0.6)
assert abs(f['DLG'] - 3 * ia1 * 0.25 / 0.6) < 1e-12, "Double line-to-ground: 3I₀ = 3.16 pu."
assert abs(faults(0.25j, 0.25j, 0.25j, 0)['SLG'] - 4) < 1e-12, "Z₀ = Z₁: ground fault equals the three-phase fault."
```

::: right
Right. Each fault type is just a different connection of the same three sequence networks.
:::
:::

::: check
kind: python

## Write `phase_currents(I0, I1, I2)`: the complex phase currents (Ia, Ib, Ic).

```python starter
def phase_currents(I0, I1, I2):
    return I1, I1, I1
```

```python solution
import cmath, math

def phase_currents(I0, I1, I2):
    a = cmath.exp(2j * math.pi / 3)
    return I0 + I1 + I2, I0 + a * a * I1 + a * I2, I0 + a * I1 + a * a * I2
```

```python tests
I = 1 / (0.85j)
Ia, Ib, Ic = phase_currents(I, I, I)
assert abs(Ia - 3 * I) < 1e-12 and abs(Ib) < 1e-12 and abs(Ic) < 1e-12, "Line-to-ground: all current in phase a."
I1 = 1 / 0.5j
Ia, Ib, Ic = phase_currents(0, I1, -I1)
assert abs(Ia) < 1e-12 and abs(abs(Ib) - 2 * 3 ** 0.5) < 1e-12 and abs(Ib + Ic) < 1e-12, "Line-to-line: b and c opposite, √3 I₁."
```

::: right
Right. The operator a turns sequence quantities into the three phases, and the fault's boundary conditions fix which combination appears.
:::
:::

::: check
kind: python

## Write `fault_voltages(Z, k, Zf)`: the bus voltages during a three-phase fault at bus k (index from 0) through Zf, from 1 pu prefault everywhere, given Z-bus Z.

```python starter
def fault_voltages(Z, k, Zf):
    return [1.0] * len(Z)
```

```python solution
def fault_voltages(Z, k, Zf):
    I = 1 / (Z[k][k] + Zf)
    return [1 - Z[i][k] * I for i in range(len(Z))]
```

```python tests
Z = [[0.24j, 0.16j], [0.16j, 0.24j]]
V = fault_voltages(Z, 0, 0)
assert abs(V[0]) < 1e-12 and abs(V[1] - 1 / 3) < 1e-12, "Bolted fault at bus 1: bus 2 holds a third."
V = fault_voltages(Z, 0, 0.06j)
assert abs(V[0] - 0.2) < 1e-12, "Through j0.06, bus 1 itself holds 0.2 pu."
```

::: right
Right. One column of Z-bus gives the whole network's voltage profile for a fault at that bus.
:::
:::

::: explain
## Why is Z-bus, rather than Y-bus, the natural matrix for fault studies?

::: model
A fault is a current suddenly drawn from one bus, and the question is how every bus's voltage responds. Z-bus maps injected currents directly to voltages, so the column for the faulted bus gives each bus's voltage change per unit of fault current, and its diagonal entry is the Thévenin impedance that sets the fault current itself. Y-bus maps voltages to currents, so using it would require solving the whole network for every fault. Because faults are studied at every bus, programs factor Y-bus once and extract the Z-bus columns they need, combining the sparsity of one with the convenience of the other.
:::
:::
