powerio-tx 0.11.7

Compiler infrastructure for power systems: parse, convert, validate, and lower grid models
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
fn parse_mpc(content: &str) -> crate::Result<crate::network::BalancedNetwork> {
    let mut warnings = crate::collect::Diagnostics::new();
    super::parse_matpower_source(content, None, &mut warnings)
}

/// Parse a case with the collector located in a source named `/input`, and
/// return the failure with the byte range left on the collector.
fn parse_mpc_located(content: &str) -> (crate::Error, Option<powerio_core::SourceSpan>) {
    let mut warnings = crate::collect::Diagnostics::new();
    warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
    let error = super::parse_matpower_source(content, None, &mut warnings)
        .expect_err("the case is malformed");
    (error, warnings.record_span())
}

fn byte_range(source: &str, text: &str) -> (u64, u64) {
    let start = source.find(text).expect("the text is in the source");
    (start as u64, (start + text.len()) as u64)
}

#[test]
fn a_short_row_leaves_the_row_byte_range_on_the_collector() {
    let src = "mpc.baseMVA = 100;\nmpc.bus = [\n\t1  3  0 0 0 0 1 1.0 0 345 1 1.1 0.9;\n\t2  1  0 0;\n];\n";
    let (error, span) = parse_mpc_located(src);
    assert!(matches!(
        error,
        crate::Error::ShortRow {
            field: "bus",
            row: 1,
            ..
        }
    ));
    let span = span.unwrap();
    assert_eq!(span.source().as_str(), "/input");
    assert_eq!(
        (span.byte_start(), span.byte_end()),
        byte_range(src, "2  1  0 0")
    );
}

#[test]
fn a_malformed_token_marks_its_row_up_to_the_end_of_the_line() {
    let src = "mpc.baseMVA = 100;\nmpc.bus = [\n\t1  3  0 0 0 0 1 1.0 0 345 1 1.1 0.9;\n\t2  1  0 abc 0 0 1 1.0 0 345 1 1.1 0.9;  % comment\n];\n";
    let (error, span) = parse_mpc_located(src);
    assert!(matches!(
        error,
        crate::Error::BadFloat {
            field: "bus",
            row: 1,
            ..
        }
    ));
    let span = span.unwrap();
    assert_eq!(
        (span.byte_start(), span.byte_end()),
        byte_range(src, "2  1  0 abc 0 0 1 1.0 0 345 1 1.1 0.9;")
    );
}

#[test]
fn a_truncated_matrix_marks_the_whole_assignment() {
    let src = "mpc.baseMVA = 100;\nmpc.bus = [\n\t1  3  0 0 0 0 1 1.0 0 345 1 1.1 0.9;\n";
    let (error, span) = parse_mpc_located(src);
    assert!(matches!(error, crate::Error::UnbalancedBrackets("bus")));
    let span = span.unwrap();
    assert_eq!(
        (span.byte_start(), span.byte_end()),
        byte_range(src, "mpc.bus = [\n\t1  3  0 0 0 0 1 1.0 0 345 1 1.1 0.9;")
    );
}

#[test]
fn a_cost_count_mismatch_marks_the_cost_table() {
    let src = "mpc.baseMVA = 100;\nmpc.bus = [\n\t1  3  0 0 0 0 1 1.0 0 345 1 1.1 0.9;\n];\nmpc.branch = [];\nmpc.gen = [\n\t1 0 0 10 -10 1 100 1 20 0;\n];\nmpc.gencost = [\n\t2 0 0 3 0.1 1 0;\n\t2 0 0 3 0.1 1 0;\n\t2 0 0 3 0.1 1 0;\n];\n";
    let (error, span) = parse_mpc_located(src);
    assert!(matches!(
        error,
        crate::Error::GenCostCountMismatch {
            gens: 1,
            gencost: 3
        }
    ));
    let span = span.unwrap();
    let (start, end) = byte_range(src, "mpc.gencost = [");
    assert_eq!(span.byte_start(), start);
    assert!(span.byte_end() > end);
    assert_eq!(
        &src[span.byte_start() as usize..span.byte_end() as usize].trim_end(),
        &src[start as usize..].trim_end()
    );
}

#[test]
fn a_well_formed_case_leaves_no_record_on_the_collector() {
    let mut warnings = crate::collect::Diagnostics::new();
    warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
    super::parse_matpower_source(CASE_TINY, None, &mut warnings).unwrap();
    assert_eq!(warnings.record_span(), None);
}
use super::write_matpower;
use crate::indexed::IndexedNetwork;
use crate::network::{
    BalancedNetwork, Branch, Bus, BusId, BusType, GenCost, Generator, SourceFormat,
};

const CASE_TINY: &str = r"
function mpc = tiny
%TINY  3-bus test
mpc.version = '2';
mpc.baseMVA = 100;

% Bus matrix: standard 13-column layout
mpc.bus = [
    1  3  0     0     0   0   1  1.0  0   345  1  1.1  0.9;
    2  2  10    5     0   0   1  1.0  0   345  1  1.1  0.9;
    3  1  20    8     0   0   1  1.0  0   345  1  1.1  0.9;
];

% Branch matrix: standard 13-column layout
mpc.branch = [
    1  2  0.02  0.06  0.01  0  0  0  0  0  1  -360  360;
    2  3  0.01  0.04  0.015 0  0  0  1  0  1  -360  360;
];
";

#[test]
// base_mva is `100` verbatim in the source, so the exact compare is intended.
#[allow(clippy::float_cmp)]
fn parses_tiny_case() {
    let net = parse_mpc(CASE_TINY).expect("parse tiny");
    assert_eq!(net.name(), "tiny");
    assert_eq!(net.base_mva(), 100.0);
    assert_eq!(net.buses().len(), 3);
    assert_eq!(net.branches().len(), 2);
    // First bus has zero demand, so it produces no load; buses 2 and 3 do.
    assert_eq!(net.buses()[0].id, BusId(1));
    assert_eq!(net.loads().len(), 2);
    assert!(net.loads().iter().all(|l| l.bus != BusId(1)));
    // Branch 0: 1->2, r=0.02, x=0.06, in service.
    assert_eq!(net.branches()[0].from, BusId(1));
    assert_eq!(net.branches()[0].to, BusId(2));
    assert!((net.branches()[0].r - 0.02).abs() < 1e-12);
    assert!((net.branches()[0].x - 0.06).abs() < 1e-12);
    assert!(net.branches()[0].in_service);
}

#[test]
fn maps_non_contiguous_bus_ids() {
    let src = r"
function mpc = sparse_ids
mpc.baseMVA = 100;
mpc.bus = [
    7   3  0  0  0  0  1  1  0  345  1  1.1  0.9;
    42  1  10 5  0  0  1  1  0  345  1  1.1  0.9;
];
mpc.branch = [
    7  42  0.01  0.05  0.02  0  0  0  0  0  1  -360  360;
];
";
    let net = parse_mpc(src).expect("parse sparse-ids");
    assert_eq!(net.buses().len(), 2);
    let g = IndexedNetwork::new(&net);
    assert_eq!(g.bus_index(BusId(7)), Some(0));
    assert_eq!(g.bus_index(BusId(42)), Some(1));
    assert_eq!(g.bus_index(BusId(99)), None);
}

#[test]
fn rejects_short_bus_row() {
    let src = r"
mpc.baseMVA = 100;
mpc.bus = [
    1 3 0;
];
mpc.branch = [
    1 1 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;
];
";
    let err = parse_mpc(src).expect_err("should fail on short bus row");
    let msg = err.to_string();
    assert!(msg.contains("bus"), "expected bus error, got: {msg}");
}

#[test]
fn handles_inline_percent_in_string() {
    let src = r"
mpc.baseMVA = 100;
mpc.version = '2 (50% capacity)';
mpc.bus = [
    1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
];
mpc.branch = [
    1 1 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;
];
";
    parse_mpc(src).expect("string with embedded % shouldn't break parse");
}

#[test]
fn parses_storage_block() {
    // Storage row values taken from pglib_opf_case5_pjm_storage (the
    // PowerModels / pglib 17-column layout), plus a second out-of-service row.
    let src = r"
mpc.baseMVA = 100;
mpc.bus = [
    1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
    4 1 0 0 0 0 1 1 0 345 1 1.1 0.9;
];
mpc.branch = [
    1 4 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;
];
mpc.storage = [
    4  0.0  0.0  1.00  600.0  300.0  216.0  0.9  0.85  1000  -1000  1000  0.1  0.01  0  0  1;
    1  0.0  0.0  0.50  200.0  100.0  100.0  0.95 0.9   500   -500   500   0.2  0.02  0  0  0;
];
";
    let net = parse_mpc(src).expect("parse storage");
    assert_eq!(net.storage().len(), 2);
    let s = &net.storage()[0];
    assert_eq!(s.bus, BusId(4));
    assert!((s.energy - 1.0).abs() < 1e-12);
    assert!((s.energy_rating - 600.0).abs() < 1e-12);
    assert!((s.charge_efficiency - 0.9).abs() < 1e-12);
    assert!((s.discharge_efficiency - 0.85).abs() < 1e-12);
    assert!((s.qmin - (-1000.0)).abs() < 1e-12);
    assert!((s.x - 0.01).abs() < 1e-12);
    assert!(s.in_service);
    assert!(!net.storage()[1].in_service);
}

#[test]
fn absent_storage_is_empty() {
    let net = parse_mpc(CASE_TINY).expect("parse tiny");
    assert_eq!(net.storage().as_slice(), []);
}

#[test]
fn rejects_short_storage_row() {
    let src = r"
mpc.baseMVA = 100;
mpc.bus = [
    1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
];
mpc.branch = [
    1 1 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;
];
mpc.storage = [
    1 0.0 0.0 1.0;
];
";
    let err = parse_mpc(src).expect_err("short storage row should fail");
    assert!(err.to_string().contains("storage"), "got: {err}");
}

#[test]
fn rejects_oversized_gencost_ncost_without_panicking() {
    // NCOST is read from the file as an f64 truncated to usize, so a huge value
    // saturates near usize::MAX. The row-width requirement (`start + 2*ncost` for
    // model 1, `start + ncost` for model 2) must be computed without overflowing:
    // a malformed NCOST has to surface as a loud `ShortRow`, not an add-overflow
    // panic (debug) or a reversed-slice panic (release). Both cost models exercise
    // a distinct saturating op (`saturating_mul` vs `saturating_add`).
    let case = |gencost: &str| {
        format!(
            "mpc.baseMVA = 100;\n\
             mpc.bus = [\n1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;\n];\n\
             mpc.gen = [\n1 0 0 100 -100 1 100 1 100 0 0 0 0 0 0 0 0 0 0 0 0;\n];\n\
             mpc.branch = [\n1 1 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;\n];\n\
             mpc.gencost = [\n{gencost}\n];\n"
        )
    };
    // model 2 (polynomial): want = ncost
    let err = parse_mpc(&case("2 0 0 1e20 500 300 200;")).expect_err("huge model-2 ncost");
    assert!(err.to_string().contains("gencost"), "got: {err}");
    // model 1 (piecewise): want = 2 * ncost, the saturating_mul path
    let err = parse_mpc(&case("1 0 0 1e19 0 0 1 1;")).expect_err("huge model-1 ncost");
    assert!(err.to_string().contains("gencost"), "got: {err}");
}

#[test]
fn rejects_unterminated_matrix() {
    // A `mpc.bus = [ … ` truncated at EOF with no closing `];`. The streaming
    // row parser must reject this (old `find_matrix` returned UnbalancedBrackets)
    // rather than silently accept the partial matrix.
    let src = "mpc.baseMVA = 100;\n\
               mpc.bus = [\n\
               \t1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;\n\
               \t2 1 0 0 0 0 1 1 0 345 1 1.1 0.9;\n";
    let err = parse_mpc(src).expect_err("unterminated bus matrix should fail");
    let msg = err.to_string();
    assert!(
        msg.contains("unbalanced"),
        "expected unbalanced error, got: {msg}"
    );
    assert!(msg.contains("bus"), "expected bus field named, got: {msg}");
}

#[test]
fn accepts_last_row_without_trailing_semicolon() {
    // A properly closed matrix whose final row has no trailing `;` before `];`
    // must still parse — the unbalanced check keys on the missing `]`, not `;`.
    let src = "mpc.baseMVA = 100;\n\
               mpc.bus = [\n\
               \t1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;\n\
               \t2 1 0 0 0 0 1 1 0 345 1 1.1 0.9\n\
               ];\n\
               mpc.branch = [\n\
               \t1 2 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;\n\
               ];\n";
    let net = parse_mpc(src).expect("closed matrix with unterminated last row");
    assert_eq!(net.buses().len(), 2);
}

#[test]
// base_mva is `100` verbatim in the source, so the exact compare is intended.
#[allow(clippy::float_cmp)]
fn parsed_case_keeps_source_in_memory_case_does_not() {
    // The byte exact echo belongs to the module write path; the typed writer
    // always emits canonical text, whatever the network's origin.
    let parsed = parse_mpc(CASE_TINY).expect("parse tiny");
    let mut built = parsed.clone();
    *built.source_format_mut() = SourceFormat::InMemory; // Canonical output is parseable and keeps the headline values.
    let reparsed = parse_mpc(&write_matpower(&built)).expect("canonical reparses");
    assert_eq!(reparsed.base_mva(), 100.0);
    assert_eq!(reparsed.buses().len(), built.buses().len());
}

#[test]
fn handles_nan_inf() {
    let src = r"
mpc.baseMVA = 100;
mpc.bus = [
    1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
];
mpc.branch = [
    1 1 0.01 0.05 0.02 0 0 0 0 0 1 NaN Inf;
];
";
    let net = parse_mpc(src).expect("NaN/Inf should parse");
    assert!(net.branches()[0].angmin.is_nan());
    assert!(net.branches()[0].angmax.is_infinite());
}

#[test]
fn mixed_model_gencost_drops_padding_and_writes_rectangular() {
    // A gencost matrix that mixes piecewise (model 1) and polynomial (model 2)
    // rows is padded with trailing zeros to stay rectangular. The parser must take
    // each row's own values (2·ncost / ncost), not the padding, and the canonical
    // writer must pad back so the emitted matrix is rectangular (PowerModels and
    // MATPOWER both reject a ragged one). This mirrors t_case9_dcline's gencost.
    let src = r"
mpc.baseMVA = 100;
mpc.bus = [
    1 3 0 0 0 0 1 1 0 345 1 1.1 0.9;
    2 2 0 0 0 0 1 1 0 345 1 1.1 0.9;
];
mpc.branch = [
    1 2 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;
];
mpc.gen = [
    1 0 0 300 -300 1 100 1 250 10 0 0 0 0 0 0 0 0 0 0 0;
    2 0 0 300 -300 1 100 1 250 10 0 0 0 0 0 0 0 0 0 0 0;
];
mpc.gencost = [
    1 0 0 3 0 0 100 2500 200 5500;
    2 0 0 2 24.035 -403.5 0 0 0 0;
];
";
    let net = parse_mpc(src).expect("parse mixed gencost");
    let c0 = net.generators()[0].cost.as_ref().unwrap();
    let c1 = net.generators()[1].cost.as_ref().unwrap();
    // Piecewise: 2·ncost = 6 breakpoint values; polynomial: ncost = 2 coefficients
    // (the four padding zeros are dropped, not read as degree-2..5 terms).
    assert_eq!((c0.model, c0.coeffs.len()), (1, 6));
    assert_eq!((c1.model, c1.coeffs.len()), (2, 2));
    assert!((c1.coeffs[0] - 24.035).abs() < 1e-9 && (c1.coeffs[1] + 403.5).abs() < 1e-9);

    // Canonical write pads both gencost rows to the same width.
    let mut built = net.clone();
    *built.source_format_mut() = SourceFormat::InMemory;
    let text = write_matpower(&built);
    let rows: Vec<usize> = text
        .lines()
        .skip_while(|l| !l.contains("mpc.gencost"))
        .skip(1)
        .take_while(|l| !l.contains("];"))
        .map(|l| l.matches('\t').count())
        .collect();
    assert_eq!(rows.len(), 2);
    assert_eq!(rows[0], rows[1], "gencost rows must be equal width");
    // And it re-parses to the same trimmed costs.
    let reparsed = parse_mpc(&text).expect("canonical mixed gencost reparses");
    assert_eq!(
        reparsed.generators()[0].cost.as_ref().unwrap().coeffs.len(),
        6
    );
    assert_eq!(
        reparsed.generators()[1].cost.as_ref().unwrap().coeffs.len(),
        2
    );
}

#[test]
fn piecewise_gencost_constructor_counts_breakpoints() {
    let mut generator = Generator::new(BusId(1));
    generator.cost = Some(GenCost::new(1, 0.0, 0.0, vec![0.0, 0.0, 1.0, 1.0]));
    let mut net = BalancedNetwork::in_memory(
        "pwl_cost_constructor",
        100.0,
        vec![
            Bus::new(BusId(1), BusType::Ref, 230.0),
            Bus::new(BusId(2), BusType::Pq, 230.0),
        ],
        vec![Branch::new(BusId(1), BusId(2), 0.01, 0.1)],
    );
    *net.generators_mut() = vec![generator];
    let cost = net.generators()[0].cost.as_ref().unwrap();
    assert_eq!(cost.ncost, 2);
    assert_eq!(cost.coeffs, vec![0.0, 0.0, 1.0, 1.0]);

    let restored = crate::network::serde_round_trip(&net);
    let restored_cost = restored.generators()[0].cost.as_ref().unwrap();
    assert_eq!(restored_cost.ncost, 2);
    assert_eq!(restored_cost.coeffs, vec![0.0, 0.0, 1.0, 1.0]);

    let text = write_matpower(&net);
    let reparsed = parse_mpc(&text).expect("constructor PWL cost should reparse");
    let reparsed_cost = reparsed.generators()[0].cost.as_ref().unwrap();
    assert_eq!(reparsed_cost.model, 1);
    assert_eq!(reparsed_cost.ncost, 2);
    assert_eq!(reparsed_cost.coeffs, vec![0.0, 0.0, 1.0, 1.0]);
}

#[test]
fn dclinecost_must_cover_every_dcline() {
    // `toggle_dcline` requires one cost row per dcline; a short block is a
    // malformed file, not a partial assignment.
    let text = "function mpc = c\nmpc.version = '2';\nmpc.baseMVA = 100;\n\
        mpc.bus = [\n\t1\t3\t0\t0\t0\t0\t1\t1\t0\t345\t1\t1.1\t0.9;\n\t2\t1\t0\t0\t0\t0\t1\t1\t0\t345\t1\t1.1\t0.9;\n];\n\
        mpc.branch = [\n\t1\t2\t0.01\t0.1\t0\t0\t0\t0\t0\t0\t1\t-360\t360;\n];\n\
        mpc.dcline = [\n\t1\t2\t1\t10\t9.5\t0\t0\t1\t1\t0\t10\t-10\t10\t-10\t10\t0\t0.05;\n\
        \t2\t1\t1\t5\t5\t0\t0\t1\t1\t0\t10\t0\t0\t0\t0\t0\t0;\n];\n\
        mpc.dclinecost = [\n\t2\t0\t0\t2\t7.3\t0;\n];\n";
    let err = parse_mpc(text).unwrap_err();
    assert!(
        matches!(
            err,
            crate::Error::DcLineCostCountMismatch {
                dclines: 2,
                dclinecost: 1
            }
        ),
        "got {err:?}"
    );
}

/// A bus name holding a control character cannot ride a MATLAB single-quoted
/// literal: the literal cannot span a line, so the written file stops being
/// loadable by MATPOWER itself. powerio reading its own output back proves
/// nothing here, so the assertion is on the emitted text.
#[test]
fn a_bus_name_never_breaks_the_cell_array() {
    let mut net = BalancedNetwork::new("names", 100.0);
    let mut bus = Bus::new(BusId(1), BusType::Ref, 345.0);
    bus.name = Some("SUB A\nEVIL'X".to_string());
    net.buses_mut().push(bus);
    net.buses_mut().push(Bus::new(BusId(2), BusType::Pq, 345.0));
    net.branches_mut()
        .push(Branch::new(BusId(1), BusId(2), 0.01, 0.1));

    let text = write_matpower(&net);
    let names: Vec<&str> = text
        .lines()
        .skip_while(|l| !l.contains("bus_name"))
        .skip(1)
        .take_while(|l| !l.starts_with("};"))
        .collect();
    assert_eq!(names.len(), 2, "one entry per bus: {text}");
    assert_eq!(names[0], "\t'SUB A EVIL''X';");
    assert!(
        parse_mpc(&text).is_ok(),
        "the written case must still parse"
    );
}

/// MATPOWER's gen matrix is rectangular, so one generator stating capability
/// data grows the columns for every generator. The ones with nothing to say
/// pad with zeros, and a zero in `RAMP_10` states a unit that cannot ramp
/// rather than a unit that said nothing — a disclosure the readback cannot
/// tell from stated data, so the writer names it.
#[test]
fn zero_padded_capability_columns_are_declared() {
    let mut net = BalancedNetwork::new("caps", 100.0);
    net.buses_mut()
        .push(Bus::new(BusId(1), BusType::Ref, 345.0));
    net.buses_mut().push(Bus::new(BusId(2), BusType::Pv, 345.0));
    net.branches_mut()
        .push(Branch::new(BusId(1), BusId(2), 0.01, 0.1));
    let mut stated = Generator::new(BusId(1));
    stated.caps[0] = Some(12.5);
    net.generators_mut().push(stated);
    net.generators_mut().push(Generator::new(BusId(2)));

    let warnings = crate::format::emit_value_text(&net, crate::format::TargetFormat::Matpower)
        .unwrap()
        .render_diagnostics();
    assert!(
        warnings.iter().any(|w| w.contains("columns 11-21")),
        "the zero padding must be declared: {warnings:?}"
    );

    // A network where no generator states caps keeps the 10-column row and
    // says nothing.
    let mut plain = net.clone();
    plain.generators_mut()[0].caps = [None; 11];
    let warnings = crate::format::emit_value_text(&plain, crate::format::TargetFormat::Matpower)
        .unwrap()
        .render_diagnostics();
    assert!(
        !warnings.iter().any(|w| w.contains("columns 11-21")),
        "nothing to disclose when no generator states caps: {warnings:?}"
    );
}

/// A MATPOWER bus row states one demand and one shunt with no status of their
/// own, so an out of service element has no spelling there. Folding its value
/// into the row states an idle element as live load: a solver reading the
/// result sees demand the source said was off.
#[test]
fn an_out_of_service_load_does_not_become_live_demand() {
    let mut net = BalancedNetwork::new("oos", 100.0);
    net.buses_mut()
        .push(Bus::new(BusId(1), BusType::Ref, 345.0));
    net.buses_mut().push(Bus::new(BusId(2), BusType::Pq, 345.0));
    net.branches_mut()
        .push(Branch::new(BusId(1), BusId(2), 0.01, 0.1));
    let mut idle = crate::network::Load::new(BusId(2), 90.0, 30.0);
    idle.in_service = false;
    net.loads_mut().push(idle);
    net.loads_mut()
        .push(crate::network::Load::new(BusId(2), 10.0, 5.0));

    let conversion =
        crate::format::emit_value_text(&net, crate::format::TargetFormat::Matpower).unwrap();
    let back = parse_mpc(&conversion.text).unwrap();
    let demand: f64 = back.loads().iter().map(|l| l.p).sum();
    assert!(
        (demand - 10.0).abs() < 1e-9,
        "only the in-service load may reach the bus row, got {demand} MW"
    );
    assert!(
        conversion
            .render_diagnostics()
            .iter()
            .any(|w| w.contains("out of service load(s) dropped")),
        "the dropped demand must be named: {:?}",
        conversion.render_diagnostics()
    );
}

#[test]
fn areas_survive_the_canonical_round_trip() {
    let mut net = parse_mpc(CASE_TINY).unwrap();
    *net.source_format_mut() = SourceFormat::InMemory;
    net.areas_mut().push(crate::network::Area {
        slack_bus: Some(BusId(1)),
        ..crate::network::Area::new(7)
    });
    net.areas_mut().push(crate::network::Area::new(9));

    let text = write_matpower(&net);
    assert!(text.contains("mpc.areas"), "areas block missing:\n{text}");
    let back = parse_mpc(&text).unwrap();
    assert_eq!(back.areas().len(), 2);
    assert_eq!(back.areas()[0].number, 7);
    assert_eq!(back.areas()[0].slack_bus, Some(BusId(1)));
    assert_eq!(back.areas()[1].number, 9);
    assert_eq!(back.areas()[1].slack_bus, None, "refbus 0 reads as none");
}

#[test]
fn area_metadata_matpower_cannot_hold_is_a_declared_drop() {
    let mut net = parse_mpc(CASE_TINY).unwrap();
    *net.source_format_mut() = SourceFormat::InMemory;
    net.areas_mut().push(crate::network::Area {
        name: Some("west".into()),
        net_interchange: 12.5,
        uid: Some("area-west".into()),
        area_type: Some("ControlArea".into()),
        ..crate::network::Area::new(1)
    });

    let conversion =
        crate::format::emit_value_text(&net, crate::format::TargetFormat::Matpower).unwrap();
    assert!(
        conversion.render_diagnostics().iter().any(|w| {
            w.contains(
                "area record(s) carry a name, source identity, classification, or interchange data",
            )
        }),
        "the fields mpc.areas cannot hold must be declared: {:?}",
        conversion.render_diagnostics()
    );
}

#[test]
fn indexed_matlab_mutations_emit_span_qualified_diagnostics() {
    let src = "mpc.baseMVA = 10;\n\
mpc.bus = [\n\
  1 3 0 0 0 0 1 1 0 12.66 1 1.1 0.9;\n\
];\n\
mpc.branch = [\n\
  1 1 0.01 0.05 0.02 0 0 0 0 0 1 -360 360;\n\
];\n\
mpc.branch(:, [BR_R BR_X]) = mpc.branch(:, [BR_R BR_X]) / 2; % scale impedance\n\
mpc.bus(:, [PD, QD]) = mpc.bus(:, [PD, QD]) / 1e3;\n";
    let mut warnings = crate::collect::Diagnostics::new();
    warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
    super::parse_matpower_source(src, None, &mut warnings).expect("literal case parses");

    let records = warnings.records();
    assert_eq!(records.len(), 2);
    assert!(records.iter().all(|record| {
        record.code() == "READ.MATPOWER.INDEXED_ASSIGNMENT"
            && record.severity() == powerio_core::DiagnosticSeverity::Warning
    }));

    let branch_line = "mpc.branch(:, [BR_R BR_X]) = mpc.branch(:, [BR_R BR_X]) / 2;";
    let bus_line = "mpc.bus(:, [PD, QD]) = mpc.bus(:, [PD, QD]) / 1e3;";
    let spans: Vec<_> = records
        .iter()
        .map(|record| {
            let span = &record.spans()[0];
            (span.byte_start(), span.byte_end())
        })
        .collect();
    assert_eq!(spans[0], byte_range(src, branch_line));
    assert_eq!(spans[1], byte_range(src, bus_line));
}

#[test]
fn indexed_assignment_diagnostic_ignores_comments_and_literal_assignments() {
    let src = "mpc.baseMVA = 100;\n\
% mpc.bus(:, [PD, QD]) = mpc.bus(:, [PD, QD]) / 1e3;\n\
mpc.bus = [1 3 0 0 0 0 1 1 0 345 1 1.1 0.9];\n\
mpc.branch = [1 1 0.01 0.05 0.02 0 0 0 0 0 1 -360 360];\n";
    let mut warnings = crate::collect::Diagnostics::new();
    super::parse_matpower_source(src, None, &mut warnings).expect("literal case parses");
    assert!(
        warnings.is_empty(),
        "unexpected diagnostics: {:?}",
        warnings.lines()
    );
}

#[test]
fn indexed_assignment_scanner_handles_nested_index_expressions_and_comparisons() {
    let mutation = "mpc.bus(find(mpc.bus(:, 3) > 0), 3) = 0;";
    let src = format!("{CASE_TINY}\n{mutation}\nmpc.bus(:, 3) == 0;\n");
    let mut warnings = crate::collect::Diagnostics::new();
    warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
    super::parse_matpower_source(&src, None, &mut warnings).expect("literal case parses");
    let records = warnings.records();
    assert_eq!(records.len(), 1, "{:?}", warnings.lines());
    assert_eq!(records[0].code(), "READ.MATPOWER.INDEXED_ASSIGNMENT");
    assert_eq!(
        records[0].severity(),
        powerio_core::DiagnosticSeverity::Warning
    );
    let span = &records[0].spans()[0];
    assert_eq!(
        (span.byte_start(), span.byte_end()),
        byte_range(&src, mutation)
    );
}

#[test]
fn indexed_assignment_scanner_handles_ellipsis_continuation_and_statement_boundaries() {
    let continued = "mpc.bus(:, ...\n [3 4]) = mpc.bus(:, [3 4]) / 1000;";
    let same_line = "scale = 1000; mpc.bus(:, [3 4]) = mpc.bus(:, [3 4]) / scale;";
    let comma_separated = "scale = 1000, mpc.bus(:, [3 4]) = mpc.bus(:, [3 4]) / scale;";
    for mutation in [continued, same_line, comma_separated] {
        let src = format!("{CASE_TINY}\n{mutation}\n");
        let mut warnings = crate::collect::Diagnostics::new();
        warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
        super::warn_indexed_assignments(&src, &mut warnings);
        let records = warnings.records();
        assert_eq!(records.len(), 1, "{mutation:?}: {:?}", warnings.lines());
        assert_eq!(records[0].code(), "READ.MATPOWER.INDEXED_ASSIGNMENT");
        assert_eq!(
            records[0].severity(),
            powerio_core::DiagnosticSeverity::Warning
        );
        let span = &records[0].spans()[0];
        let offending_statement = if mutation.starts_with("scale =") {
            "mpc.bus(:, [3 4]) = mpc.bus(:, [3 4]) / scale;"
        } else {
            mutation
        };
        assert_eq!(
            (span.byte_start(), span.byte_end()),
            byte_range(&src, offending_statement),
            "span should cover the original offending statement: {mutation:?}"
        );
    }
}

#[test]
fn indexed_assignment_scanner_ignores_block_comments_and_quoted_literals() {
    let src = format!(
        "{CASE_TINY}\n%{{\nmpc.bus(:, [3 4]) = 0;\n%}}\nmpc.bus_name = {{'mpc.bus(:, [3 4]) = 0;'}};\n"
    );
    let mut warnings = crate::collect::Diagnostics::new();
    super::parse_matpower_source(&src, None, &mut warnings).expect("literal case parses");
    assert!(
        warnings.is_empty(),
        "inactive/commented or quoted text must not warn: {:?}",
        warnings.lines()
    );
}

#[test]
fn indexed_assignment_scanner_finds_only_assignments_at_statement_start() {
    let mutation = "mpc.bus(:, [3 4]) = mpc.bus(:, [3 4]) / 1000;";
    let src = format!("{CASE_TINY}\nif true, mpc.bus(:, [3 4]) == 0; end\n{mutation}\n");
    let mut warnings = crate::collect::Diagnostics::new();
    warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
    super::parse_matpower_source(&src, None, &mut warnings).expect("literal case parses");
    let records = warnings.records();
    assert_eq!(records.len(), 1, "{:?}", warnings.lines());
    let span = &records[0].spans()[0];
    assert_eq!(
        (span.byte_start(), span.byte_end()),
        byte_range(&src, mutation)
    );
}

#[test]
fn indexed_assignment_scanner_preserves_transpose_context_and_spans() {
    for (prefix, mutation) in [
        ("x = [1 2].';\n", "mpc.bus(:, [3 4]) = 0;"),
        ("x = [1 2]';\n", "mpc.bus(:, [3 4]) = 0;"),
        ("", "mpc.bus([1 2].', 3) = [0 0].';"),
    ] {
        let src = format!("{CASE_TINY}\n{prefix}{mutation}\n");
        assert_indexed_warning_span(&src, mutation);
    }
}

#[test]
fn indexed_assignment_scanner_skips_continuation_trivia_without_parsing_its_tail() {
    for mutation in [
        "mpc.bus(:, [3 4]) ... % conversion\n = mpc.bus(:, [3 4]) / 1000;",
        "mpc.bus(:, [3 4]) ... ignored ) ' ; ,\r\n = 0;",
        "mpc.bus ... ignored ) ' ; ,\n (:, [3 4]) = 0;",
        "mpc.bus(find(mpc.bus(:, 3) ... ignored ) ' ; ,\n > 0), 3) = 0;",
        "mpc.bus(:, [3 4]) = ... ignored ) ' ; ,\n 0;",
    ] {
        let src = format!("{CASE_TINY}\n{mutation}\n");
        assert_indexed_warning_span(&src, mutation);
    }
}

#[test]
fn indexed_assignment_scanner_keeps_comparisons_and_quoted_continuations_inactive() {
    let src = format!(
        "{CASE_TINY}\nmpc.bus(:, 3) ... % comparison\n == 0;\n\
         label = '... mpc.bus(:, 3) = 0;';\n\
         other = \"... mpc.bus(:, 3) = 0;\";\n"
    );
    let mut warnings = crate::collect::Diagnostics::new();
    super::parse_matpower_source(&src, None, &mut warnings).unwrap();
    assert!(warnings.is_empty(), "{:?}", warnings.lines());
}

#[test]
fn indexed_assignment_scanner_stops_spans_at_top_level_commas() {
    for mutation in ["mpc.bus(:, 3) = 0,", "mpc.bus(:, [3, 4]) = [0, 0],"] {
        let src = format!("{CASE_TINY}\n{mutation} unrelated = 1;\n");
        assert_indexed_warning_span(&src, mutation);
    }
}

fn assert_indexed_warning_span(source: &str, mutation: &str) {
    let mut warnings = crate::collect::Diagnostics::new();
    warnings.locate_in(powerio_core::SourceId::new("/input").unwrap(), 0);
    super::warn_indexed_assignments(source, &mut warnings);
    let records = warnings.records();
    assert_eq!(records.len(), 1, "{mutation:?}: {:?}", warnings.lines());
    assert_eq!(records[0].code(), "READ.MATPOWER.INDEXED_ASSIGNMENT");
    assert_eq!(
        records[0].severity(),
        powerio_core::DiagnosticSeverity::Warning
    );
    let span = &records[0].spans()[0];
    assert_eq!(
        (span.byte_start(), span.byte_end()),
        byte_range(source, mutation),
        "span must cover only the original mutation: {mutation:?}",
    );
}

#[test]
fn case33bw_applies_post_literal_unit_conversions() {
    let src = include_str!("../../../../tests/data/case33bw.m");
    let mut warnings = crate::collect::Diagnostics::new();
    let net = super::parse_matpower_source(src, None, &mut warnings).unwrap();
    assert!((net.loads().iter().map(|load| load.p).sum::<f64>() - 3.715).abs() < 1e-12);
    assert!((net.loads().iter().map(|load| load.q).sum::<f64>() - 2.3).abs() < 1e-12);
    let zbase = 12.66_f64.powi(2) / 10.0;
    assert!((net.branches()[0].r - 0.0922 / zbase).abs() < 1e-14);
    assert!((net.branches()[0].x - 0.0470 / zbase).abs() < 1e-14);
    assert!(warnings.is_empty());
}