---
title: "Stability lab: swing curves and critical clearing"
heading: Integrating the swing equation through a fault and its clearing, finding the critical clearing time by bisection, checking it against the equal-area criterion, and computing a two-machine mode
eyebrow: S10 · Lesson 49 · Stability lab
xp: 60
---

::: hook
The equal-area criterion of lesson 44 answers the critical clearing question in closed form, but only for one machine against an infinite bus with simple fault sequences. Real stability programs integrate the swing equations of every machine through the fault, the clearing and the aftermath, and find critical clearing times by repeated simulation. In this lab you'll integrate the swing equation with RK4, bisect for the critical clearing time, confirm it against the equal-area angle, and compute a two-machine oscillation frequency.
:::

::: play
- The curve is the rotor angle of a machine with H = 5 s, $P_m$ = 1 pu and $P_\max$ = 2 pu during a fault that drops its output to zero, up to the moment of clearing: $\delta(t) = \delta_0 + \frac{\omega_sP_m}{4H}t^2$.
- The dashed line is the critical clearing angle from equal areas, 79.6°. The curve crosses it at 0.214 s: the critical clearing time.
- Raise H and the curve flattens: more inertia buys more time to clear.
:::

```sim plot
view: graph
x: t = 0..0.4 s
y: d = 0..140
slider: H = 2..9 = 5
plot: 30 + 377*1/(4*H)*t^2*180/pi | label: rotor angle during the fault, degrees
plot: 79.56 + 0*t | dashed | label: critical clearing angle
```

::: predict
## The machine's fault is on one of two parallel lines: during the fault it can still send $0.5\sin\delta$, and after clearing $1.5\sin\delta$. Compared with the 0.214 s for a terminal fault with no line lost, is its critical clearing time longer or shorter?

- [ ] Much shorter: losing a line always hurts
- [x] A little longer, about 0.23 s: the power still flowing during the fault slows the acceleration more than the weaker post-fault network costs
- [ ] Exactly the same
- [ ] Infinite: it can't lose synchronism

::: reveal
The equal-area angle for this sequence is 70.3°, smaller than 79.6°, because the post-fault network can decelerate the rotor less. But the 0.5 sin δ still transferred during the fault slows the rotor's run toward it, and simulation gives a critical clearing time of 0.230 s. Angles and times can move in opposite directions, which is why stability limits are found by simulation.
:::
:::

::: nameit
## What the lab computes

- **The swing equation** $\frac{2H}{\omega_s}\ddot\delta = P_m - P_{\max}(t)\sin\delta$, with $P_\max$ switching from its fault value to its post-fault value at the clearing time, integrated by RK4 from $\delta_0 = \sin^{-1}(P_m/P_{\max,\text{pre}})$ at rest. A run is unstable if δ passes 180°.
- **Critical clearing time** by bisection on the clearing time between a stable and an unstable run.
- **Equal-area angle**, with fault and post-fault peaks $r_1P_\max$ and $r_2P_\max$ and $\delta_m = \pi - \sin^{-1}(P_m/(r_2P_\max))$:
$$\cos\delta_c = \frac{(P_m/P_\max)(\delta_m - \delta_0) + r_2\cos\delta_m - r_1\cos\delta_0}{r_2 - r_1}$$
- **Two-machine mode**: $P_s = \frac{E_1E_2}{X}\cos\delta_{12}$, $H_{eq} = H_1H_2/(H_1 + H_2)$ and $\omega_n = \sqrt{\omega_sP_s/2H_{eq}}$.
:::

::: check
kind: python

## Write `swing(H, Pm, Mpre, Mf, Mpost, tc, T, dt)`: integrate the swing equation (60 Hz) by RK4 from equilibrium, with the fault's peak transfer Mf until tc and Mpost after; return (stable, largest δ in degrees), calling a run unstable as soon as δ exceeds π.

```python starter
def swing(H, Pm, Mpre, Mf, Mpost, tc, T, dt):
    return True, 30.0
```

```python solution
import math

def swing(H, Pm, Mpre, Mf, Mpost, tc, T, dt):
    ws = 2 * math.pi * 60
    d = math.asin(Pm / Mpre)
    w = 0.0
    t = 0.0
    dmax = d
    acc = lambda d, t: ws / (2 * H) * (Pm - (Mf if t < tc else Mpost) * math.sin(d))
    while t < T:
        k1d, k1w = w, acc(d, t)
        k2d, k2w = w + dt / 2 * k1w, acc(d + dt / 2 * k1d, t + dt / 2)
        k3d, k3w = w + dt / 2 * k2w, acc(d + dt / 2 * k2d, t + dt / 2)
        k4d, k4w = w + dt * k3w, acc(d + dt * k3d, t + dt)
        d += dt / 6 * (k1d + 2 * k2d + 2 * k3d + k4d)
        w += dt / 6 * (k1w + 2 * k2w + 2 * k3w + k4w)
        t += dt
        dmax = max(dmax, d)
        if d > math.pi:
            return False, math.degrees(dmax)
    return True, math.degrees(dmax)
```

```python tests
ok, dm = swing(5, 1, 2, 0, 2, 0.15, 2, 5e-4)
assert ok and 80 < dm < 86, "Cleared at 0.15 s the first swing peaks near 83° and turns back."
ok, _ = swing(5, 1, 2, 0, 2, 0.3, 2, 5e-4)
assert not ok, "Cleared at 0.3 s it loses synchronism."
```

::: right
Right. The first swing reaches its peak and turns back only if the decelerating area after clearing can absorb the energy gained during the fault.
:::
:::

::: check
kind: python

## Write `cct(H, Pm, Mpre, Mf, Mpost)`: the critical clearing time by bisection between 0.01 and 1.5 s, using `swing` with T = 2 s and dt = 5e-4 s and 25 bisection steps.

```python starter
def cct(H, Pm, Mpre, Mf, Mpost):
    return 0.2
```

```python solution
import math

def swing(H, Pm, Mpre, Mf, Mpost, tc, T, dt):
    ws = 2 * math.pi * 60
    d = math.asin(Pm / Mpre)
    w = 0.0
    t = 0.0
    acc = lambda d, t: ws / (2 * H) * (Pm - (Mf if t < tc else Mpost) * math.sin(d))
    while t < T:
        k1d, k1w = w, acc(d, t)
        k2d, k2w = w + dt / 2 * k1w, acc(d + dt / 2 * k1d, t + dt / 2)
        k3d, k3w = w + dt / 2 * k2w, acc(d + dt / 2 * k2d, t + dt / 2)
        k4d, k4w = w + dt * k3w, acc(d + dt * k3d, t + dt)
        d += dt / 6 * (k1d + 2 * k2d + 2 * k3d + k4d)
        w += dt / 6 * (k1w + 2 * k2w + 2 * k3w + k4w)
        t += dt
        if d > math.pi:
            return False
    return True

def cct(H, Pm, Mpre, Mf, Mpost):
    lo, hi = 0.01, 1.5
    for _ in range(25):
        mid = (lo + hi) / 2
        if swing(H, Pm, Mpre, Mf, Mpost, mid, 2.0, 5e-4):
            lo = mid
        else:
            hi = mid
    return lo
```

```python tests
import math
t = cct(5, 1, 2, 0, 2)
d0 = math.asin(0.5)
dc = math.acos(0.5 * (math.pi - 2 * d0) + math.cos(math.pi - d0))
assert abs(t - math.sqrt(4 * 5 * (dc - d0) / (377 * 1))) < 2e-3, "Terminal fault: 0.214 s, as equal areas predict."
assert abs(cct(5, 1, 2, 0.5, 1.5) - 0.230) < 3e-3, "Partial transfer during the fault, one line lost: 0.230 s."
```

::: right
Right. With zero output during the fault the angle is a parabola in time and the closed form exists; with partial transfer only simulation gives the time.
:::
:::

::: check
kind: python

## Write `critical_angle(Pm, Mpre, r1, r2)`: the equal-area critical clearing angle in degrees.

```python starter
def critical_angle(Pm, Mpre, r1, r2):
    return 90.0
```

```python solution
import math

def critical_angle(Pm, Mpre, r1, r2):
    d0 = math.asin(Pm / Mpre)
    dm = math.pi - math.asin(Pm / (r2 * Mpre))
    c = ((Pm / Mpre) * (dm - d0) + r2 * math.cos(dm) - r1 * math.cos(d0)) / (r2 - r1)
    return math.degrees(math.acos(c))
```

```python tests
assert abs(critical_angle(1, 2, 0, 1) - 79.56) < 0.01, "Terminal fault, no line lost: 79.6°."
assert abs(critical_angle(1, 2, 0.25, 0.75) - 70.29) < 0.01, "Lesson 44's second case: 70.3°."
```

::: right
Right. The angle depends only on the curves, not on H; inertia sets how long the rotor takes to get there.
:::
:::

::: check
kind: python

## Write `two_machine_hz(H1, H2, E1, E2, X, d12_deg)`: the frequency in hertz of two machines swinging against each other (60 Hz system).

```python starter
def two_machine_hz(H1, H2, E1, E2, X, d12_deg):
    return 1.0
```

```python solution
import math

def two_machine_hz(H1, H2, E1, E2, X, d12_deg):
    Ps = E1 * E2 / X * math.cos(math.radians(d12_deg))
    Heq = H1 * H2 / (H1 + H2)
    return math.sqrt(2 * math.pi * 60 * Ps / (2 * Heq)) / (2 * math.pi)
```

```python tests
assert abs(two_machine_hz(5, 3, 1.1, 1.1, 0.8, 20) - 1.90) < 0.01, "Lesson 45's example: 1.90 Hz."
assert two_machine_hz(5, 1000, 1.1, 1.1, 0.8, 20) < two_machine_hz(5, 3, 1.1, 1.1, 0.8, 20), "Against a huge machine, the small one swings more slowly."
```

::: right
Right. Two small machines swing against each other faster than one swings against a large system: the equivalent inertia is smaller.
:::
:::

::: explain
## Why do stability studies find critical clearing times by repeated simulation instead of the equal-area criterion?

::: model
The equal-area criterion is exact only for one machine connected to an infinite bus, with the electrical power switching between a few fixed sinusoidal curves, and with the zero-output fault case being the only one where the critical angle converts directly into a time. Real systems have many machines swinging against each other, voltage regulators and governors acting during the swing, loads that depend on voltage, and fault sequences with reclosing. None of these fit the simple area picture, so programs integrate the full set of differential equations through the fault and its clearing, check whether the machines stay together, and bisect on the clearing time, which is exactly what the equal-area result can be used to verify in the simple case.
:::
:::
