gridoxide 0.1.0

AC power flow and state estimation solver with a sparse Newton-Raphson core
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Batched DC power flow, and DC contingency screening via LODF.
//!
//! The load-bearing test is `batch_matches_a_sequential_loop_exactly`. The
//! batch never re-solves anything — it exploits DC's exact linearity to express
//! each scenario as the base solve plus a response to that scenario's injection
//! delta, against one factorization built once. That is either exactly right or
//! quietly wrong, so it is checked against a plain loop over `dc_power_flow` at
//! 1e-12 rather than at any tolerance that would hide a modelling slip.

use std::path::PathBuf;

use gridoxide::batch::{BatchError, BusOverride, Scenario};
use gridoxide::linear::{DcBatchSolver, DcOptions, DcSensitivity, dc_branches, dc_power_flow};
use gridoxide::pgm::pgm_to_buses_and_branches;
use gridoxide::types::{Bus, BusType, Line, Transformer};

mod common;

fn load(rel: &str) -> (Vec<Bus>, Vec<Line>, Vec<Transformer>) {
    let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/data/pgm/powerflow")
        .join(rel)
        .join("input.json");
    let input = common::load_pgm_input(&path);
    pgm_to_buses_and_branches(input, 1e6, 50.0)
}

/// A spread of scenarios that move several buses at once, including buses with
/// no load in the template — so the batch cannot pass by only handling the
/// easy "scale existing load" shape.
fn scenarios(buses: &[Bus], count: usize) -> Vec<Scenario> {
    (0..count)
        .map(|k| {
            let factor = 0.5 + 0.1 * k as f64;
            let overrides = buses
                .iter()
                .filter(|b| b.bus_type != BusType::Slack)
                .map(|b| BusOverride::new(b.idx).p(b.p_spec * factor - 0.01 * k as f64))
                .collect();
            Scenario::new(overrides)
        })
        .collect()
}

/// **The oracle.** Batched results must equal a sequential loop over
/// `dc_power_flow`, bus for bus and branch for branch.
#[test]
fn batch_matches_a_sequential_loop_exactly() {
    for name in ["symmetric/transmission-case", "symmetric/distribution-case"] {
        let (buses, lines, transformers) = load(name);
        let opts = DcOptions::default();
        let scenarios = scenarios(&buses, 12);

        let batched = DcBatchSolver::new()
            .solve(&buses, &lines, &transformers, opts, &scenarios)
            .unwrap();
        assert_eq!(batched.len(), scenarios.len());

        for (k, (scenario, got)) in scenarios.iter().zip(&batched).enumerate() {
            let mut expected_buses = buses.clone();
            for ov in &scenario.bus_overrides {
                if let Some(p) = ov.p_spec {
                    expected_buses[ov.bus].p_spec = p;
                }
            }
            let expected = dc_power_flow(&mut expected_buses, &lines, &transformers, opts);

            for (i, bus) in expected_buses.iter().enumerate() {
                assert!(
                    (bus.voltage_ang - got.voltage_ang[i]).abs() < 1e-12,
                    "{name} scenario {k} bus {i}: batch {} vs loop {}",
                    got.voltage_ang[i],
                    bus.voltage_ang
                );
            }
            for (b, (want, have)) in expected
                .branch_p
                .iter()
                .zip(&got.branch_p)
                .enumerate()
                .map(|(b, p)| (b, p))
            {
                assert!(
                    (want - have).abs() < 1e-12,
                    "{name} scenario {k} branch {b}: batch {have} vs loop {want}"
                );
            }
            for (i, island) in expected.islands.iter().enumerate() {
                assert!(
                    (island.slack_pickup - got.slack_pickup[i]).abs() < 1e-12,
                    "{name} scenario {k} island {i}: batch {} vs loop {}",
                    got.slack_pickup[i],
                    island.slack_pickup
                );
            }
        }
    }
}

/// Results come back in scenario order whatever the thread count, and the
/// answer does not depend on how many workers produced it.
#[test]
fn results_are_ordered_and_thread_count_independent() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let scenarios = scenarios(&buses, 40);

    let single = DcBatchSolver::with_threads(1)
        .unwrap()
        .solve(&buses, &lines, &transformers, opts, &scenarios)
        .unwrap();
    let many = DcBatchSolver::with_threads(4)
        .unwrap()
        .solve(&buses, &lines, &transformers, opts, &scenarios)
        .unwrap();

    assert_eq!(single.len(), scenarios.len());
    assert_eq!(
        single, many,
        "thread count changed the batch's answer or its order"
    );

    // And the scenarios really are distinct, so ordering is observable.
    assert!(
        single[0].branch_p != single[1].branch_p,
        "scenarios are identical, so ordering proves nothing"
    );
}

/// ZIP terms belong to the template and survive a `p_spec` override, exactly as
/// they do on the AC path. Getting this wrong would double-count or drop them
/// silently, since DC folds them into the same scalar injection.
#[test]
fn zip_terms_survive_a_p_spec_override() {
    use gridoxide::types::{ZipKind, ZipTerm};
    use num_complex::Complex;

    let mut 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: 0.0,
            q_max: 0.0,
            u_rated: 0.0,
            zip_terms: Vec::new(),
        },
        Bus {
            idx: 1,
            bus_type: BusType::PQ,
            voltage_mag: 1.0,
            voltage_ang: 0.0,
            p_spec: -0.2,
            q_spec: 0.0,
            q_min: 0.0,
            q_max: 0.0,
            u_rated: 0.0,
            zip_terms: vec![ZipTerm {
                s_const: Complex::new(-0.3, 0.0),
                kind: ZipKind::ConstImpedance,
            }],
        },
    ];
    let lines = vec![Line {
        from: 0,
        to: 1,
        r: 0.0,
        x: 0.1,
        b_shunt: 0.0,
        g_shunt: 0.0,
    }];
    let opts = DcOptions::default();

    let scenario = Scenario::new(vec![BusOverride::new(1).p(-0.5)]);
    let batched = DcBatchSolver::new()
        .solve(&buses, &lines, &[], opts, std::slice::from_ref(&scenario))
        .unwrap();

    buses[1].p_spec = -0.5;
    let expected = dc_power_flow(&mut buses, &lines, &[], opts);

    // -0.5 override plus the -0.3 constant-impedance term = -0.8 total.
    assert!(
        (batched[0].branch_p[0] - 0.8).abs() < 1e-12,
        "{}",
        batched[0].branch_p[0]
    );
    assert!(
        (batched[0].branch_p[0] - expected.branch_p[0]).abs() < 1e-12,
        "batch dropped or double-counted the ZIP term"
    );
}

/// Input that cannot be interpreted is rejected rather than guessed at.
#[test]
fn malformed_input_is_rejected() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let batch = DcBatchSolver::new();

    let out_of_range = Scenario::new(vec![BusOverride::new(buses.len()).p(0.0)]);
    assert!(matches!(
        batch.solve(&buses, &lines, &transformers, opts, &[out_of_range]),
        Err(BatchError::BusOutOfRange { scenario: 0, .. })
    ));

    let mut outage = Scenario::new(vec![]);
    outage.branch_outages = vec![0];
    assert!(matches!(
        batch.solve(&buses, &lines, &transformers, opts, &[outage]),
        Err(BatchError::OutagesUnsupported { scenario: 0 })
    ));

    assert_eq!(
        batch
            .solve(&buses, &lines, &transformers, opts, &[])
            .unwrap()
            .len(),
        0
    );
}

/// `outage_flows` is the N-1 primitive: it must agree with actually removing
/// the branch and re-solving, which is the only check that matters for it.
#[test]
fn outage_flows_match_an_actual_outage_resolve() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let n_branches = lines.len() + transformers.len();

    let mut base_buses = buses.clone();
    let base = dc_power_flow(&mut base_buses, &lines, &transformers, opts);
    let branches = dc_branches(&lines, &transformers, opts);
    let sensitivity = DcSensitivity::new(&base_buses, &branches, n_branches).unwrap();

    let mut checked = 0;
    for outaged in 0..lines.len() {
        let Some(predicted) = sensitivity.outage_flows(&base.branch_p, outaged) else {
            continue;
        };
        if base.branch_p[outaged].abs() < 1e-9 {
            continue; // Nothing to redistribute; the identity is trivial.
        }

        // Open the line into gridoxide's own half-open representation, the
        // same path a genuinely open branch takes.
        let mut opened = lines.clone();
        opened[outaged].to = opened[outaged].from;
        let mut scratch = buses.clone();
        let actual = dc_power_flow(&mut scratch, &opened, &transformers, opts).branch_p;

        for k in 0..n_branches {
            assert!(
                (predicted[k] - actual[k]).abs() < 1e-9,
                "outage of {outaged}: branch {k} predicted {} but re-solve gives {}",
                predicted[k],
                actual[k]
            );
        }
        assert_eq!(
            predicted[outaged], 0.0,
            "the tripped branch must carry nothing"
        );
        checked += 1;
    }
    assert!(
        checked > 0,
        "no load-carrying, non-radial branch was checked"
    );
}

/// A radial branch has no post-outage flows, because removing it islands the
/// network rather than rerouting anything.
#[test]
fn outage_flows_refuse_a_radial_branch() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let n_branches = lines.len() + transformers.len();

    let mut base_buses = buses.clone();
    let base = dc_power_flow(&mut base_buses, &lines, &transformers, opts);
    let branches = dc_branches(&lines, &transformers, opts);
    let sensitivity = DcSensitivity::new(&base_buses, &branches, n_branches).unwrap();

    let mut radial = 0;
    for branch in 0..n_branches {
        if sensitivity.is_radial(branch) {
            assert!(sensitivity.outage_flows(&base.branch_p, branch).is_none());
            radial += 1;
        }
    }
    assert!(
        radial > 0,
        "fixture has no radial branch, so this proves nothing"
    );

    // A mismatched base-flow vector is rejected rather than read out of bounds.
    assert!(sensitivity.outage_flows(&[0.0], 0).is_none());
}

/// **The N-k oracle.** Predicting a simultaneous multi-branch outage must match
/// actually removing every one of them and re-solving.
///
/// See `chaining_single_outages_is_not_a_substitute` for why this cannot be
/// assembled from single-branch LODF columns.
#[test]
fn multi_outage_flows_match_an_actual_n_minus_2_resolve() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let n_branches = lines.len() + transformers.len();

    let mut base_buses = buses.clone();
    let base = dc_power_flow(&mut base_buses, &lines, &transformers, opts);
    let branches = dc_branches(&lines, &transformers, opts);
    let sensitivity = DcSensitivity::new(&base_buses, &branches, n_branches).unwrap();

    let mut checked = 0;
    for a in 0..lines.len() {
        for b in (a + 1)..lines.len() {
            let Some(predicted) = sensitivity.multi_outage_flows(&base.branch_p, &[a, b]) else {
                continue;
            };

            let mut opened = lines.clone();
            opened[a].to = opened[a].from;
            opened[b].to = opened[b].from;
            let mut scratch = buses.clone();
            let actual = dc_power_flow(&mut scratch, &opened, &transformers, opts).branch_p;

            for k in 0..n_branches {
                assert!(
                    (predicted[k] - actual[k]).abs() < 1e-9,
                    "outage of {a}+{b}: branch {k} predicted {} but re-solve gives {}",
                    predicted[k],
                    actual[k]
                );
            }
            assert_eq!(predicted[a], 0.0);
            assert_eq!(predicted[b], 0.0);
            checked += 1;
        }
    }

    assert!(checked > 0, "no solvable branch pair was checked");
}

/// A one-element set must reproduce the single-branch result exactly — the two
/// entry points share one implementation, and this pins that they stay shared.
#[test]
fn a_single_element_set_matches_the_single_branch_result() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let n_branches = lines.len() + transformers.len();

    let mut base_buses = buses.clone();
    let base = dc_power_flow(&mut base_buses, &lines, &transformers, opts);
    let branches = dc_branches(&lines, &transformers, opts);
    let sensitivity = DcSensitivity::new(&base_buses, &branches, n_branches).unwrap();

    for branch in 0..n_branches {
        assert_eq!(
            sensitivity.outage_flows(&base.branch_p, branch),
            sensitivity.multi_outage_flows(&base.branch_p, &[branch]),
            "branch {branch}"
        );
    }
}

/// A set whose removal disconnects the network has no post-outage flows, and
/// says so rather than returning a large number. Built by hand, because it
/// needs a pair that is individually meshed but jointly breaking — exactly the
/// case single-branch screening misses.
#[test]
fn a_breaking_pair_is_reported_even_when_neither_branch_is_radial() {
    let mk = |idx, bus_type| Bus {
        idx,
        bus_type,
        voltage_mag: 1.0,
        voltage_ang: 0.0,
        p_spec: if bus_type == BusType::Slack {
            0.0
        } else {
            -0.3
        },
        q_spec: 0.0,
        q_min: 0.0,
        q_max: 0.0,
        u_rated: 0.0,
        zip_terms: Vec::new(),
    };
    let line = |from, to, x| Line {
        from,
        to,
        r: 0.0,
        x,
        b_shunt: 0.0,
        g_shunt: 0.0,
    };

    // Buses 0-1 joined by two parallel lines, then a tail to bus 2. Neither
    // parallel line is radial on its own; together they are the only path.
    let buses = vec![
        mk(0, BusType::Slack),
        mk(1, BusType::PQ),
        mk(2, BusType::PQ),
    ];
    let lines = vec![line(0, 1, 0.1), line(0, 1, 0.2), line(1, 2, 0.3)];
    let opts = DcOptions::default();

    let mut scratch = buses.clone();
    let base = dc_power_flow(&mut scratch, &lines, &[], opts);
    let branches = dc_branches(&lines, &[], opts);
    let sensitivity = DcSensitivity::new(&scratch, &branches, lines.len()).unwrap();

    // Fixture assumption: neither of the parallel pair is radial by itself.
    assert!(!sensitivity.is_radial(0) && !sensitivity.is_radial(1));
    assert!(sensitivity.outage_flows(&base.branch_p, 0).is_some());
    assert!(sensitivity.outage_flows(&base.branch_p, 1).is_some());

    // Together they are the only path to buses 1 and 2.
    assert!(sensitivity.is_breaking_set(&[0, 1]));
    assert!(
        sensitivity
            .multi_outage_flows(&base.branch_p, &[0, 1])
            .is_none()
    );

    // Branch 2 is radial on its own, so any set containing it breaks too.
    assert!(sensitivity.is_breaking_set(&[2]));
    assert!(sensitivity.is_breaking_set(&[0, 2]));
}

/// Degenerate and malformed sets are handled explicitly rather than falling
/// through to a singular solve that would look like a breaking set.
#[test]
fn empty_and_malformed_outage_sets() {
    let (buses, lines, transformers) = load("symmetric/transmission-case");
    let opts = DcOptions::default();
    let n_branches = lines.len() + transformers.len();

    let mut base_buses = buses.clone();
    let base = dc_power_flow(&mut base_buses, &lines, &transformers, opts);
    let branches = dc_branches(&lines, &transformers, opts);
    let sensitivity = DcSensitivity::new(&base_buses, &branches, n_branches).unwrap();

    // Outaging nothing changes nothing.
    assert_eq!(
        sensitivity.multi_outage_flows(&base.branch_p, &[]).unwrap(),
        base.branch_p
    );
    // A repeated index is malformed input, not a breaking set.
    assert!(
        sensitivity
            .multi_outage_flows(&base.branch_p, &[0, 0])
            .is_none()
    );
    // Out of range, and a mismatched base-flow vector.
    assert!(
        sensitivity
            .multi_outage_flows(&base.branch_p, &[n_branches])
            .is_none()
    );
    assert!(sensitivity.multi_outage_flows(&[0.0], &[0]).is_none());
}

/// N-3 on a densely meshed hand-built network, against the same re-solve
/// oracle — the formulation is not special-cased for k = 2.
#[test]
fn three_simultaneous_outages_match_a_resolve() {
    let mk = |idx, bus_type, p| Bus {
        idx,
        bus_type,
        voltage_mag: 1.0,
        voltage_ang: 0.0,
        p_spec: p,
        q_spec: 0.0,
        q_min: 0.0,
        q_max: 0.0,
        u_rated: 0.0,
        zip_terms: Vec::new(),
    };
    let line = |from, to, x| Line {
        from,
        to,
        r: 0.0,
        x,
        b_shunt: 0.0,
        g_shunt: 0.0,
    };

    // Five buses, fully meshed enough that removing three still leaves a
    // connected network.
    let buses = vec![
        mk(0, BusType::Slack, 0.0),
        mk(1, BusType::PQ, -0.4),
        mk(2, BusType::PQ, -0.3),
        mk(3, BusType::PQ, -0.2),
        mk(4, BusType::PQ, 0.5),
    ];
    let lines = vec![
        line(0, 1, 0.10),
        line(1, 2, 0.15),
        line(2, 3, 0.20),
        line(3, 4, 0.25),
        line(4, 0, 0.30),
        line(0, 2, 0.35),
        line(1, 3, 0.40),
        line(2, 4, 0.45),
    ];
    let opts = DcOptions::default();

    let mut scratch = buses.clone();
    let base = dc_power_flow(&mut scratch, &lines, &[], opts);
    let branches = dc_branches(&lines, &[], opts);
    let sensitivity = DcSensitivity::new(&scratch, &branches, lines.len()).unwrap();

    let set = [1usize, 5, 7];
    let predicted = sensitivity
        .multi_outage_flows(&base.branch_p, &set)
        .expect("this triple should leave the network connected");

    let mut opened = lines.clone();
    for &l in &set {
        opened[l].to = opened[l].from;
    }
    let mut scratch = buses.clone();
    let actual = dc_power_flow(&mut scratch, &opened, &[], opts).branch_p;

    for k in 0..lines.len() {
        assert!(
            (predicted[k] - actual[k]).abs() < 1e-9,
            "branch {k}: predicted {} but re-solve gives {}",
            predicted[k],
            actual[k]
        );
    }
}

/// Why the simultaneous formulation exists: applying two single-branch LODF
/// columns one after another does **not** give the N-2 answer.
///
/// The second branch's LODF was computed on the *intact* network, so chaining
/// ignores how the first outage changed the way the second one's power
/// redistributes. Algebraically, the sequential route solves
/// `(1 - Ψ_aa)·c_a = f_a` and drops the `-Ψ_ab·c_b` coupling term that the
/// simultaneous system carries — so the two agree only where `Ψ_ab = 0`, which
/// is exactly what a meshed network does not give you.
#[test]
fn chaining_single_outages_is_not_a_substitute() {
    let mk = |idx, bus_type, p| Bus {
        idx,
        bus_type,
        voltage_mag: 1.0,
        voltage_ang: 0.0,
        p_spec: p,
        q_spec: 0.0,
        q_min: 0.0,
        q_max: 0.0,
        u_rated: 0.0,
        zip_terms: Vec::new(),
    };
    let line = |from, to, x| Line {
        from,
        to,
        r: 0.0,
        x,
        b_shunt: 0.0,
        g_shunt: 0.0,
    };

    let buses = vec![
        mk(0, BusType::Slack, 0.0),
        mk(1, BusType::PQ, -0.4),
        mk(2, BusType::PQ, -0.3),
        mk(3, BusType::PQ, -0.2),
        mk(4, BusType::PQ, 0.5),
    ];
    let lines = vec![
        line(0, 1, 0.10),
        line(1, 2, 0.15),
        line(2, 3, 0.20),
        line(3, 4, 0.25),
        line(4, 0, 0.30),
        line(0, 2, 0.35),
        line(1, 3, 0.40),
        line(2, 4, 0.45),
    ];
    let opts = DcOptions::default();

    let mut scratch = buses.clone();
    let base = dc_power_flow(&mut scratch, &lines, &[], opts);
    let branches = dc_branches(&lines, &[], opts);
    let sensitivity = DcSensitivity::new(&scratch, &branches, lines.len()).unwrap();

    let (a, b) = (1usize, 6usize);

    // Ground truth: open both and re-solve.
    let mut opened = lines.clone();
    opened[a].to = opened[a].from;
    opened[b].to = opened[b].from;
    let mut scratch = buses.clone();
    let actual = dc_power_flow(&mut scratch, &opened, &[], opts).branch_p;

    let simultaneous = sensitivity
        .multi_outage_flows(&base.branch_p, &[a, b])
        .unwrap();
    let first = sensitivity.outage_flows(&base.branch_p, a).unwrap();
    let chained = sensitivity.outage_flows(&first, b).unwrap();

    let worst = |v: &[f64]| {
        v.iter()
            .zip(&actual)
            .map(|(x, t)| (x - t).abs())
            .fold(0.0f64, f64::max)
    };
    assert!(
        worst(&simultaneous) < 1e-9,
        "simultaneous is off by {}",
        worst(&simultaneous)
    );
    assert!(
        worst(&chained) > 1e-3,
        "chaining happened to agree here, so this fixture cannot show the \
         difference (gap {})",
        worst(&chained)
    );
}