1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Reactive-limit events on the continuation curve.
//!
//! The claim under test is narrow and worth stating precisely: continuation
//! reports the λ at which a generator *actually* saturates, not the λ of
//! whichever step happened to notice it had. The difference is two orders of
//! magnitude or more, and it is the whole reason `events.rs` exists — so the
//! gate here measures it rather than asserting it.
//!
//! The oracle is brute force through code that knows nothing about
//! continuation: bisect λ, solving an ordinary `run_power_flow` with
//! `enforce_q_limits` at each trial, and find where the ordinary
//! `ReactiveLimits` outer loop first clamps that bus. Slow, obviously correct,
//! and completely independent of everything under test.
mod common;
mod pglib;
use gridoxide::PowerFlowOptions;
use gridoxide::continuation::{
ContinuationOptions, CriticalPointKind, LoadingDirection, run_continuation,
};
use gridoxide::network::{build_ybus, power_injections};
use gridoxide::pgm::{PgmInput, pgm_to_buses_and_branches};
use gridoxide::types::{Bus, BusType, Line};
fn fixture(
name: &str,
) -> (
Vec<Bus>,
Vec<gridoxide::types::Line>,
Vec<gridoxide::types::Transformer>,
) {
let raw = pglib::read(name, ".json");
let input: PgmInput = serde_json::from_str(&raw).expect("parse");
pgm_to_buses_and_branches(input, 100e6, 50.0)
}
fn scaled(start: &[Bus], dir: &LoadingDirection, lambda: f64) -> Vec<Bus> {
start
.iter()
.map(|b| Bus {
p_spec: b.p_spec + lambda * dir.d_p[b.idx],
q_spec: b.q_spec + lambda * dir.d_q[b.idx],
..b.clone()
})
.collect()
}
fn q_limits_opts() -> ContinuationOptions {
ContinuationOptions {
power_flow: PowerFlowOptions {
enforce_q_limits: true,
..Default::default()
},
..Default::default()
}
}
/// **The gate.** Every located event must sit where an ordinary
/// `enforce_q_limits` solve first clamps that machine — and turning the locator
/// off must measurably lose that.
///
/// The second half matters as much as the first. Without it the test would pass
/// just as happily if `locate_events` did nothing at all and the steps merely
/// happened to be small.
#[test]
fn located_events_match_a_brute_force_bisection() {
for name in [
"pglib_opf_case5_pjm",
"pglib_opf_case14_ieee",
"pglib_opf_case30_ieee",
] {
let (start, lines, transformers) = fixture(name);
let direction = LoadingDirection::scale_loads(&start);
let clamped_at = |lambda: f64| -> Vec<usize> {
let report = gridoxide::run_power_flow(
scaled(&start, &direction, lambda),
&lines,
&transformers,
&[],
gridoxide::TapData::none(),
PowerFlowOptions {
enforce_q_limits: true,
max_iter: 60,
max_outer_iter: 60,
..Default::default()
},
);
let mut switches = report.outer.map(|o| o.q_limit_switches).unwrap_or_default();
switches.sort_unstable();
switches
};
// Machines already saturated in the base case have no crossing to find.
let at_base = clamped_at(0.0);
let mut worst = [0.0f64; 2];
for (slot, locate) in [(0usize, true), (1usize, false)] {
let curve = run_continuation(
start.clone(),
&lines,
&transformers,
&[],
ContinuationOptions {
direction: direction.clone(),
locate_events: locate,
..q_limits_opts()
},
);
let ceiling = curve.lambda_max().expect("a nose was reported") * 1.05;
for (bus, _, lambda) in curve.q_limit_events() {
if at_base.binary_search(&bus).is_ok()
|| clamped_at(ceiling).binary_search(&bus).is_err()
{
continue;
}
let (mut lo, mut hi) = (0.0f64, ceiling);
for _ in 0..45 {
let mid = 0.5 * (lo + hi);
if clamped_at(mid).binary_search(&bus).is_ok() {
hi = mid;
} else {
lo = mid;
}
}
worst[slot] = worst[slot].max((lambda - 0.5 * (lo + hi)).abs());
}
}
assert!(
worst[0] < 1e-5,
"{name}: located events are off by {:.2e}, which is not 'located'",
worst[0]
);
assert!(
worst[1] > 20.0 * worst[0].max(1e-9),
"{name}: switching at step granularity was off by only {:.2e} against the \
locator's {:.2e} — either the steps happened to be tiny or the locator is \
not doing anything, and this test would not tell the difference",
worst[1],
worst[0]
);
}
}
/// The invariant the whole event mechanism exists to keep: no point on the
/// returned curve may have a machine outside its reactive limits, and every
/// machine that was clamped must be pinned *at* its limit for the rest of the
/// trace.
///
/// The second half is the one that bites. A clamped bus has its `q_spec`
/// rewritten to the limit by `ReactiveLimits`; if the loading direction is not
/// frozen there too, every later step ramps it straight back off the limit. The
/// curve still converges at every step, so nothing looks wrong — the reactive
/// limit is simply not enforced any more.
#[test]
fn no_point_on_the_curve_violates_a_reactive_limit() {
for name in [
"pglib_opf_case5_pjm",
"pglib_opf_case14_ieee",
"pglib_opf_case30_ieee",
] {
let (start, lines, transformers) = fixture(name);
let direction = LoadingDirection::scale_loads(&start);
let ybus = build_ybus(start.len(), &lines, &transformers).finish();
let curve = run_continuation(
start.clone(),
&lines,
&transformers,
&[],
ContinuationOptions {
direction,
..q_limits_opts()
},
);
for (n, point) in curve.points.iter().enumerate() {
let state: Vec<Bus> = (0..start.len())
.map(|i| Bus {
bus_type: point.bus_type[i],
voltage_mag: point.voltage_mag[i],
voltage_ang: point.voltage_ang[i],
..start[i].clone()
})
.collect();
let (_, q_calc) = power_injections(&state, &ybus);
for b in &state {
match b.bus_type {
// Still on voltage control: it must be inside its range.
BusType::PV => assert!(
q_calc[b.idx] <= b.q_max + 1e-5 && q_calc[b.idx] >= b.q_min - 1e-5,
"{name} point {n} (lambda {:.6}): bus {} still holds voltage but \
needs Q = {:.6}, outside [{:.6}, {:.6}]",
point.lambda,
b.idx,
q_calc[b.idx],
b.q_min,
b.q_max
),
// Clamped: it must be sitting *on* a limit, not drifting off
// it. `start`'s type says it was a voltage-controlling
// machine to begin with.
BusType::PQ if start[b.idx].bus_type == BusType::PV => assert!(
(q_calc[b.idx] - b.q_max).abs() < 1e-5
|| (q_calc[b.idx] - b.q_min).abs() < 1e-5,
"{name} point {n} (lambda {:.6}): bus {} was clamped but is now at \
Q = {:.6}, neither limit ({:.6}, {:.6}) — the direction ramped it \
off its own clamp",
point.lambda,
b.idx,
q_calc[b.idx],
b.q_min,
b.q_max
),
_ => {}
}
}
}
}
}
/// Reactive limits can only ever shrink the margin. A machine that stops
/// holding its voltage cannot make the system carry *more* load.
#[test]
fn enforcing_reactive_limits_lowers_the_nose() {
for name in [
"pglib_opf_case5_pjm",
"pglib_opf_case14_ieee",
"pglib_opf_case30_ieee",
] {
let (start, lines, transformers) = fixture(name);
let direction = LoadingDirection::scale_loads(&start);
let nose = |enforce: bool| {
run_continuation(
start.clone(),
&lines,
&transformers,
&[],
ContinuationOptions {
direction: direction.clone(),
power_flow: PowerFlowOptions {
enforce_q_limits: enforce,
..Default::default()
},
..Default::default()
},
)
.lambda_max()
.unwrap_or_else(|| panic!("{name} (enforce={enforce}) found no nose"))
};
let (free, limited) = (nose(false), nose(true));
assert!(
limited < free,
"{name}: reactive limits raised the nose from {free} to {limited}, which is \
not physically possible"
);
}
}
/// A limit-induced bifurcation is a **different answer** from a fold, and it is
/// easy to miss: the Jacobian is not singular there, so a continuation watching
/// only for `dλ/dσ = 0` marches straight past it down the lower branch and
/// reports a λ_max that no operating point can reach.
///
/// `pglib_opf_case118_ieee` has one. What is asserted is the physics rather than
/// the exact λ: the reported maximum must coincide with the last reactive-limit
/// event, and an ordinary solve just past it must fail.
#[test]
fn case118_maximum_is_limit_induced_and_sits_on_its_event() {
let (start, lines, transformers) = fixture("pglib_opf_case118_ieee");
let direction = LoadingDirection::scale_loads(&start);
let curve = run_continuation(
start.clone(),
&lines,
&transformers,
&[],
ContinuationOptions {
direction: direction.clone(),
..q_limits_opts()
},
);
let critical = curve.critical.as_ref().expect("a maximum was reported");
let CriticalPointKind::LimitInduced { bus } = critical.kind else {
panic!("expected a limit-induced maximum, got {:?}", critical.kind);
};
let events = curve.q_limit_events();
let &(event_bus, _, event_lambda) = events
.last()
.expect("a limit-induced maximum implies at least one event");
assert_eq!(
event_bus, bus,
"the maximum must be the machine that just saturated"
);
assert!(
(event_lambda - critical.lambda_max).abs() < 1e-9,
"lambda_max {} should be the event lambda {event_lambda}",
critical.lambda_max
);
let past = gridoxide::run_power_flow(
scaled(&start, &direction, critical.lambda_max * 1.05),
&lines,
&transformers,
&[],
gridoxide::TapData::none(),
PowerFlowOptions {
enforce_q_limits: true,
max_iter: 80,
max_outer_iter: 60,
..Default::default()
},
);
assert_ne!(
past.stats.status,
gridoxide::solver::SolveStatus::Converged,
"past a limit-induced maximum there should be no operating point to find"
);
}
/// A machine with a wide enough range never saturates, so the curve is one
/// segment and the answer is the same as with limits switched off entirely.
#[test]
fn unreachable_limits_leave_the_curve_alone() {
let buses = vec![
Bus {
idx: 0,
bus_type: BusType::Slack,
voltage_mag: 1.0,
voltage_ang: 0.0,
p_spec: 0.0,
q_spec: 0.0,
q_min: -99.0,
q_max: 99.0,
u_rated: 1.0,
zip_terms: vec![],
},
Bus {
idx: 1,
bus_type: BusType::PV,
voltage_mag: 1.0,
voltage_ang: 0.0,
p_spec: 0.3,
q_spec: 0.0,
q_min: -99.0,
q_max: 99.0,
u_rated: 1.0,
zip_terms: vec![],
},
Bus {
idx: 2,
bus_type: BusType::PQ,
voltage_mag: 1.0,
voltage_ang: 0.0,
p_spec: -0.6,
q_spec: -0.2,
q_min: -99.0,
q_max: 99.0,
u_rated: 1.0,
zip_terms: vec![],
},
];
let lines = vec![
Line {
from: 0,
to: 1,
r: 0.02,
x: 0.06,
b_shunt: 0.0,
g_shunt: 0.0,
},
Line {
from: 1,
to: 2,
r: 0.03,
x: 0.09,
b_shunt: 0.0,
g_shunt: 0.0,
},
Line {
from: 0,
to: 2,
r: 0.04,
x: 0.12,
b_shunt: 0.0,
g_shunt: 0.0,
},
];
let direction = LoadingDirection::scale_loads(&buses);
let with = run_continuation(
buses.clone(),
&lines,
&[],
&[],
ContinuationOptions {
direction: direction.clone(),
..q_limits_opts()
},
);
let without = run_continuation(
buses,
&lines,
&[],
&[],
ContinuationOptions {
direction,
..Default::default()
},
);
assert!(
with.q_limit_events().is_empty(),
"nothing should saturate: {:?}",
with.q_limit_events()
);
assert_eq!(
with.segments, 1,
"no bus type changed, so there is one segment"
);
let (a, b) = (with.lambda_max().unwrap(), without.lambda_max().unwrap());
assert!(
(a - b).abs() < 1e-9,
"limits that never bind changed the nose: {a} vs {b}"
);
}