oxigrid 0.1.2

Pure Rust Energy Systems Simulation & Optimization Library
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
//! Three-phase unbalanced continuation power flow (CPF) solver.
//!
//! Traces P-V curves under unbalanced loading conditions and finds voltage
//! collapse points for each phase independently using a predictor-corrector
//! scheme with Gauss-Seidel per-phase correction.
//!
//! # Example
//! ```rust,ignore
//! use oxigrid::powerflow::unbalanced_continuation::*;
//!
//! let cfg = UnbalancedCpfConfig::default();
//! let mut cpf = UnbalancedCpf::new(cfg);
//! cpf.add_bus(ThreePhaseBus { bus_id: 0, bus_type: CpfBusType::Slack, .. });
//! cpf.add_bus(ThreePhaseBus { bus_id: 1, bus_type: CpfBusType::PQ, .. });
//! cpf.add_branch(ThreePhaseBranch { from_bus: 0, to_bus: 1, .. });
//! let result = cpf.solve()?;
//! ```

// ── public types ────────────────────────────────────────────────────────────

/// Bus type classification for the unbalanced CPF solver.
///
/// Uses a distinct name (`CpfBusType`) to avoid collision with
/// `crate::network::bus::BusType`.
#[derive(Debug, Clone, PartialEq)]
pub enum CpfBusType {
    /// Slack (reference) bus: voltage magnitude and angle fixed at 1.0∠0°.
    Slack,
    /// PQ bus: active and reactive load are specified; both V and θ are free.
    PQ,
    /// PV bus: voltage magnitude is regulated to `v_setpoint_pu`; angle is free.
    PV {
        /// Regulated voltage setpoint (per-unit).
        v_setpoint_pu: f64,
    },
}

/// Three-phase bus data for the unbalanced CPF solver.
#[derive(Debug, Clone)]
pub struct ThreePhaseBus {
    /// Bus index (0-based).
    pub bus_id: usize,
    /// Bus classification.
    pub bus_type: CpfBusType,
    /// Base active loads per phase \[Pa, Pb, Pc\] (MW).
    pub p_load_mw: [f64; 3],
    /// Base reactive loads per phase \[Qa, Qb, Qc\] (MVAR).
    pub q_load_mvar: [f64; 3],
    /// Generation per phase \[Pa, Pb, Pc\] (MW); zero for load buses.
    pub p_gen_mw: [f64; 3],
}

/// Three-phase branch for the unbalanced CPF solver.
///
/// Mutual coupling between phases is represented by scalar `r_mutual_pu` /
/// `x_mutual_pu` terms applied to all off-phase pairs of the same branch.
#[derive(Debug, Clone)]
pub struct ThreePhaseBranch {
    /// Sending-end bus index (0-based).
    pub from_bus: usize,
    /// Receiving-end bus index (0-based).
    pub to_bus: usize,
    /// Per-phase series resistance \[ra, rb, rc\] (pu).
    pub r_pu: [f64; 3],
    /// Per-phase series reactance \[xa, xb, xc\] (pu).
    pub x_pu: [f64; 3],
    /// Mutual resistance coupling between phase pairs (pu).
    pub r_mutual_pu: f64,
    /// Mutual reactance coupling between phase pairs (pu).
    pub x_mutual_pu: f64,
}

/// Load-scaling strategy applied as λ increases.
#[derive(Debug, Clone)]
pub enum LoadScalingModel {
    /// All three phases scale by the same factor λ.
    Uniform,
    /// Each phase can scale independently (currently all three scale by λ —
    /// extend via custom λ vectors if needed).
    PhaseIndependent,
    /// Phase A scales by λ; phases B and C scale by `λ * ratio` (ratio ∈ (0,1\]).
    PhaseADominant {
        /// Scaling ratio for phases B and C relative to A (typically < 1.0).
        ratio: f64,
    },
}

/// Configuration for the unbalanced continuation power flow solver.
#[derive(Debug, Clone)]
pub struct UnbalancedCpfConfig {
    /// Maximum load factor to trace (default 3.0).
    pub lambda_max: f64,
    /// Initial predictor step size Δλ (default 0.05).
    pub lambda_step: f64,
    /// Minimum allowed step size (default 1e-4).
    pub lambda_min_step: f64,
    /// Gauss-Seidel convergence tolerance on |ΔV| (default 1e-5).
    pub v_tolerance: f64,
    /// Maximum Gauss-Seidel iterations per CPF step (default 20).
    pub max_iter_per_step: usize,
    /// Load-scaling strategy.
    pub load_model: LoadScalingModel,
    /// Voltage threshold below which collapse is declared (default 0.5 pu).
    pub collapse_voltage_pu: f64,
}

impl Default for UnbalancedCpfConfig {
    fn default() -> Self {
        Self {
            lambda_max: 3.0,
            lambda_step: 0.05,
            lambda_min_step: 1e-4,
            v_tolerance: 1e-5,
            max_iter_per_step: 20,
            load_model: LoadScalingModel::Uniform,
            collapse_voltage_pu: 0.5,
        }
    }
}

/// One point on a P-V curve (all three phases).
#[derive(Debug, Clone)]
pub struct PvPoint {
    /// Load-scaling factor λ at this point.
    pub lambda: f64,
    /// Phase-A bus voltage magnitude (pu), averaged across non-slack buses.
    pub v_a: f64,
    /// Phase-B bus voltage magnitude (pu), averaged across non-slack buses.
    pub v_b: f64,
    /// Phase-C bus voltage magnitude (pu), averaged across non-slack buses.
    pub v_c: f64,
    /// Whether Gauss-Seidel converged at this λ.
    pub converged: bool,
    /// Total three-phase active power consumed (MW) at this point.
    pub active_power_total_mw: f64,
}

/// Voltage collapse (nose) point for one phase.
#[derive(Debug, Clone)]
pub struct CollapsePoint {
    /// Phase index: 0 = A, 1 = B, 2 = C.
    pub phase: u8,
    /// Critical loading factor λ at the nose point.
    pub lambda_nose: f64,
    /// Voltage magnitude at the nose point (pu).
    pub v_nose_pu: f64,
    /// Index of the weakest bus (0-based).
    pub bus_id: usize,
    /// Total active power at the nose point (MW).
    pub p_nose_mw: f64,
}

/// Full result returned by [`UnbalancedCpf::solve`].
#[derive(Debug, Clone)]
pub struct UnbalancedCpfResult {
    /// Traced P-V curve points in order of increasing λ.
    pub pv_curve: Vec<PvPoint>,
    /// Detected nose (collapse) points, one per phase that collapsed.
    pub collapse_points: Vec<CollapsePoint>,
    /// Index of the phase (0/1/2) that collapses at the lowest λ.
    pub critical_phase: u8,
    /// Loading margin: λ_nose − λ_base (or λ_max − 1 if no collapse detected).
    pub loading_margin: f64,
    /// Voltage stability index per bus per phase: `vsi[bus][phase]` ∈ \[0, 1\].
    pub vsi: Vec<[f64; 3]>,
    /// Total number of predictor-corrector steps taken.
    pub n_steps: usize,
    /// `true` when the solver successfully bracketed the nose point.
    pub converged_to_nose: bool,
}

// ── solver ───────────────────────────────────────────────────────────────────

/// Three-phase unbalanced continuation power flow solver.
///
/// Uses a predictor-corrector scheme:
/// * **Predictor**: increment λ by `config.lambda_step` and use the previous
///   voltage solution as the starting guess.
/// * **Corrector**: Gauss-Seidel iterations on the per-phase admittance system.
/// * **Step-size adaptation**: halve Δλ when GS fails to converge; double when
///   it converges in fewer than 5 iterations.
pub struct UnbalancedCpf {
    /// Buses in the network (order defines the 0-based index).
    pub buses: Vec<ThreePhaseBus>,
    /// Branches in the network.
    pub branches: Vec<ThreePhaseBranch>,
    /// Solver configuration.
    pub config: UnbalancedCpfConfig,
}

impl UnbalancedCpf {
    /// Create a new solver with the given configuration and empty network.
    pub fn new(config: UnbalancedCpfConfig) -> Self {
        Self {
            buses: Vec::new(),
            branches: Vec::new(),
            config,
        }
    }

    /// Append a bus to the network.
    pub fn add_bus(&mut self, bus: ThreePhaseBus) {
        self.buses.push(bus);
    }

    /// Append a branch to the network.
    pub fn add_branch(&mut self, branch: ThreePhaseBranch) {
        self.branches.push(branch);
    }

    // ── internal helpers ────────────────────────────────────────────────────

    /// Return the per-phase load scaling multiplier for `phase` at factor `lambda`.
    fn phase_lambda(&self, phase: usize, lambda: f64) -> f64 {
        match &self.config.load_model {
            LoadScalingModel::Uniform | LoadScalingModel::PhaseIndependent => lambda,
            LoadScalingModel::PhaseADominant { ratio } => {
                if phase == 0 {
                    lambda
                } else {
                    lambda * ratio
                }
            }
        }
    }

    /// Build the per-phase real (G) and imaginary (B) admittance matrices.
    ///
    /// Returns `(G, B)` where `G[i][j]` and `B[i][j]` are the (i,j) entries
    /// of the nodal admittance matrix for the requested `phase` (0=A, 1=B, 2=C).
    ///
    /// Mutual coupling between phases is *not* included here — it is handled
    /// as a current-injection correction in [`Self::gauss_seidel_phase`].
    fn build_admittance_per_phase(&self, phase: usize) -> (Vec<Vec<f64>>, Vec<Vec<f64>>) {
        let n = self.buses.len();
        let mut g = vec![vec![0.0_f64; n]; n];
        let mut b = vec![vec![0.0_f64; n]; n];

        for br in &self.branches {
            let i = br.from_bus;
            let j = br.to_bus;
            if i >= n || j >= n {
                continue;
            }
            let r = br.r_pu[phase];
            let x = br.x_pu[phase];
            let denom = r * r + x * x;
            if denom < 1e-18 {
                continue;
            }
            let g_br = r / denom;
            let b_br = -x / denom;

            // Off-diagonal
            g[i][j] -= g_br;
            g[j][i] -= g_br;
            b[i][j] -= b_br;
            b[j][i] -= b_br;

            // Diagonal
            g[i][i] += g_br;
            g[j][j] += g_br;
            b[i][i] += b_br;
            b[j][j] += b_br;
        }

        (g, b)
    }

    /// Compute per-bus power injections `(P_inj, Q_inj)` for `phase` given
    /// voltage magnitudes `v` and angles `theta` (radians).
    #[allow(dead_code)]
    fn compute_power_injection(&self, phase: usize, v: &[f64], theta: &[f64]) -> Vec<(f64, f64)> {
        let n = self.buses.len();
        let (g, b) = self.build_admittance_per_phase(phase);
        let mut injections = vec![(0.0_f64, 0.0_f64); n];

        for j in 0..n {
            let mut p = 0.0_f64;
            let mut q = 0.0_f64;
            for k in 0..n {
                let d_theta = theta[j] - theta[k];
                p += v[j] * v[k] * (g[j][k] * d_theta.cos() + b[j][k] * d_theta.sin());
                q += v[j] * v[k] * (g[j][k] * d_theta.sin() - b[j][k] * d_theta.cos());
            }
            injections[j] = (p, q);
        }
        injections
    }

    /// Gauss-Seidel power-flow iteration for a single `phase`.
    ///
    /// Returns a vector of converged voltage magnitudes (pu) for each bus,
    /// or an `Err` if the maximum iteration count is exceeded or `Y_jj = 0`.
    ///
    /// * Slack bus: held at 1.0 pu, 0° throughout.
    /// * PV bus: magnitude clamped to `v_setpoint_pu` after each update.
    /// * PQ bus: voltage updated freely.
    fn gauss_seidel_phase(
        &self,
        phase: usize,
        lambda: f64,
        v_init: &[f64],
    ) -> Result<Vec<f64>, String> {
        let n = self.buses.len();
        if n == 0 {
            return Err("Network has no buses".to_string());
        }

        let (g, b) = self.build_admittance_per_phase(phase);
        let lam = self.phase_lambda(phase, lambda);

        // Compute scheduled net injections (generation – load·λ) in pu (base=1 MW)
        let mut p_sched: Vec<f64> = self
            .buses
            .iter()
            .map(|bus| bus.p_gen_mw[phase] - bus.p_load_mw[phase] * lam)
            .collect();
        let mut q_sched: Vec<f64> = self
            .buses
            .iter()
            .map(|bus| -bus.q_load_mvar[phase] * lam)
            .collect();

        // Slack bus: override
        for (idx, bus) in self.buses.iter().enumerate() {
            if bus.bus_type == CpfBusType::Slack {
                p_sched[idx] = 0.0;
                q_sched[idx] = 0.0;
            }
        }

        // Rectangular voltage: V = e + j*f
        let mut e: Vec<f64> = v_init.to_vec();
        let mut f: Vec<f64> = vec![0.0_f64; n];

        // Fix slack bus
        for (idx, bus) in self.buses.iter().enumerate() {
            if bus.bus_type == CpfBusType::Slack {
                e[idx] = 1.0;
                f[idx] = 0.0;
            }
        }

        // Mutual coupling: build correction currents from other phases.
        // We model it as a fixed current injection perturbation from V ≈ 1∠0.
        let mut i_real_mutual = vec![0.0_f64; n];
        let mut i_imag_mutual = vec![0.0_f64; n];
        for br in &self.branches {
            let fi = br.from_bus;
            let ti = br.to_bus;
            if fi >= n || ti >= n {
                continue;
            }
            let rm = br.r_mutual_pu;
            let xm = br.x_mutual_pu;
            let denom = rm * rm + xm * xm;
            if denom < 1e-18 {
                continue;
            }
            // Approximate mutual current contribution as small perturbation
            // (simplified: treat adjacent-phase voltage ≈ 1∠(±120°))
            let angle_offset = if phase == 0 {
                2.0 * std::f64::consts::PI / 3.0
            } else {
                -2.0 * std::f64::consts::PI / 3.0
            };
            let v_other_e = angle_offset.cos();
            let v_other_f = angle_offset.sin();
            // Current from mutual: I_m = Y_m * V_other; Y_m = 1/(r_m+j*x_m)
            let g_m = rm / denom;
            let b_m = -xm / denom;
            let i_e = g_m * v_other_e - b_m * v_other_f;
            let i_f = b_m * v_other_e + g_m * v_other_f;
            // This coupling subtracts from the net injection at both ends
            i_real_mutual[fi] -= i_e;
            i_imag_mutual[fi] -= i_f;
            i_real_mutual[ti] += i_e;
            i_imag_mutual[ti] += i_f;
        }

        for iter in 0..self.config.max_iter_per_step {
            let mut max_dv = 0.0_f64;

            for j in 0..n {
                if self.buses[j].bus_type == CpfBusType::Slack {
                    e[j] = 1.0;
                    f[j] = 0.0;
                    continue;
                }

                let v2 = e[j] * e[j] + f[j] * f[j];
                if v2 < 1e-12 {
                    // Avoid division by zero — reset to small value
                    e[j] = 0.1;
                    f[j] = 0.0;
                }
                let v2 = e[j] * e[j] + f[j] * f[j];

                // Compute I_j = (P - jQ) / conj(V) = (P-jQ)(e-jf)/v2
                let p_j = p_sched[j];
                let q_j = q_sched[j];
                // I_j (rectangular): numerator = (P-jQ)*(e-jf)
                //   real: P*e - Q*f
                //   imag: -P*f - Q*e ... wait: (P-jQ)*(e+jf)/v^2 for I=(P+jQ)/conj(V)
                // Using: I = (P + jQ) / conj(V) = (P+jQ)*(e+jf) / v^2
                let i_real_j = (p_j * e[j] - q_j * f[j]) / v2 + i_real_mutual[j];
                let i_imag_j = (p_j * f[j] + q_j * e[j]) / v2 + i_imag_mutual[j];

                // Sum Y_jk * V_k for k != j
                let mut sum_e = 0.0_f64;
                let mut sum_f = 0.0_f64;
                for k in 0..n {
                    if k == j {
                        continue;
                    }
                    // Y_jk = g_jk + j*b_jk; V_k = e_k + j*f_k
                    // Y_jk * V_k: real = g*e - b*f; imag = g*f + b*e
                    sum_e += g[j][k] * e[k] - b[j][k] * f[k];
                    sum_f += g[j][k] * f[k] + b[j][k] * e[k];
                }

                // V_j_new = (I_j - sum) / Y_jj
                let y_jj_g = g[j][j];
                let y_jj_b = b[j][j];
                let y_jj2 = y_jj_g * y_jj_g + y_jj_b * y_jj_b;
                if y_jj2 < 1e-18 {
                    // Isolated bus — skip update
                    continue;
                }
                let num_e = i_real_j - sum_e;
                let num_f = i_imag_j - sum_f;
                // Division by (g + jb): multiply by conj (g - jb)
                let e_new = (num_e * y_jj_g + num_f * y_jj_b) / y_jj2;
                let f_new = (num_f * y_jj_g - num_e * y_jj_b) / y_jj2;

                // For PV bus: clamp magnitude
                let (e_upd, f_upd) = match &self.buses[j].bus_type {
                    CpfBusType::PV { v_setpoint_pu } => {
                        let mag = (e_new * e_new + f_new * f_new).sqrt();
                        if mag < 1e-9 {
                            (*v_setpoint_pu, 0.0)
                        } else {
                            (e_new / mag * v_setpoint_pu, f_new / mag * v_setpoint_pu)
                        }
                    }
                    _ => (e_new, f_new),
                };

                let dv = ((e_upd - e[j]).powi(2) + (f_upd - f[j]).powi(2)).sqrt();
                max_dv = max_dv.max(dv);
                e[j] = e_upd;
                f[j] = f_upd;
            }

            if max_dv < self.config.v_tolerance {
                // Converged
                let v_mag: Vec<f64> = (0..n).map(|j| (e[j] * e[j] + f[j] * f[j]).sqrt()).collect();
                return Ok(v_mag);
            }

            let _ = iter; // suppress unused warning
        }

        // Return best-effort result with a warning embedded in Err
        Err(format!(
            "Gauss-Seidel did not converge for phase {} at lambda={:.4} within {} iterations",
            phase, lambda, self.config.max_iter_per_step
        ))
    }

    /// Solve the three-phase power flow at load factor `lambda`.
    ///
    /// Returns voltage magnitudes `v[bus][phase]` (pu).
    #[allow(clippy::needless_range_loop)]
    fn solve_power_flow_at(&self, lambda: f64) -> Result<Vec<[f64; 3]>, String> {
        let n = self.buses.len();
        let init: Vec<f64> = vec![1.0_f64; n];
        let mut v_per_phase: Vec<[f64; 3]> = vec![[1.0_f64; 3]; n];

        for ph in 0..3_usize {
            let v = self.gauss_seidel_phase(ph, lambda, &init)?;
            for (i, &vi) in v.iter().enumerate() {
                v_per_phase[i][ph] = vi;
            }
        }
        Ok(v_per_phase)
    }

    /// Detect the nose (voltage collapse) point for one `phase` from the
    /// traced P-V curve.
    ///
    /// The nose is identified as the last converged point before either:
    /// * the minimum phase voltage drops below `collapse_voltage_pu`, or
    /// * the per-step voltage drop accelerates strongly (second-derivative sign
    ///   change indicates turning of the P-V curve).
    fn detect_nose_point(&self, pv_curve: &[PvPoint]) -> Option<CollapsePoint> {
        if pv_curve.len() < 3 {
            return None;
        }

        // Find first point where ANY phase drops below collapse threshold
        let mut nose_idx: Option<usize> = None;
        let mut nose_phase: u8 = 0;

        for (idx, pt) in pv_curve.iter().enumerate() {
            let voltages = [pt.v_a, pt.v_b, pt.v_c];
            for (ph, &v) in voltages.iter().enumerate() {
                if v < self.config.collapse_voltage_pu && nose_idx.is_none() {
                    nose_idx = Some(idx.saturating_sub(1));
                    nose_phase = ph as u8;
                }
            }
            if nose_idx.is_some() {
                break;
            }
        }

        // Also detect via strong dV/dλ second derivative sign change
        if nose_idx.is_none() && pv_curve.len() >= 5 {
            for i in 2..pv_curve.len() {
                let pt0 = &pv_curve[i - 2];
                let pt1 = &pv_curve[i - 1];
                let pt2 = &pv_curve[i];
                let dl01 = pt1.lambda - pt0.lambda;
                let dl12 = pt2.lambda - pt1.lambda;
                if dl01 < 1e-12 || dl12 < 1e-12 {
                    continue;
                }
                let vols = [
                    (pt0.v_a, pt1.v_a, pt2.v_a, 0u8),
                    (pt0.v_b, pt1.v_b, pt2.v_b, 1u8),
                    (pt0.v_c, pt1.v_c, pt2.v_c, 2u8),
                ];
                for (v0, v1, v2, ph) in vols {
                    let dv1 = (v1 - v0) / dl01;
                    let dv2 = (v2 - v1) / dl12;
                    // Accelerating drop: second derivative < -2.0 (pu/pu²)
                    let d2v = (dv2 - dv1) / ((dl01 + dl12) / 2.0);
                    if d2v < -2.0 && nose_idx.is_none() {
                        nose_idx = Some(i.saturating_sub(1));
                        nose_phase = ph;
                    }
                }
                if nose_idx.is_some() {
                    break;
                }
            }
        }

        let idx = nose_idx?;
        let pt = pv_curve.get(idx)?;
        let v_nose = match nose_phase {
            0 => pt.v_a,
            1 => pt.v_b,
            _ => pt.v_c,
        };

        // Identify weakest bus as the one with lowest voltage at nose λ
        // (approximation: use bus 0 index for weakest since we only store
        //  aggregate voltages in PvPoint; full per-bus tracking below)
        let weakest_bus = self
            .buses
            .iter()
            .enumerate()
            .filter(|(_, b)| b.bus_type != CpfBusType::Slack)
            .map(|(i, _)| i)
            .next()
            .unwrap_or(0);

        Some(CollapsePoint {
            phase: nose_phase,
            lambda_nose: pt.lambda,
            v_nose_pu: v_nose,
            bus_id: weakest_bus,
            p_nose_mw: pt.active_power_total_mw,
        })
    }

    /// Compute the per-bus voltage stability index (VSI) for one `phase`.
    ///
    /// Returns `vsi[bus]` ∈ \[0, 1\] where 1 = fully stable and 0 = at
    /// collapse.  Uses a proximity-to-collapse heuristic:
    /// `VSI(j) = (|V_j| - V_collapse) / (1 - V_collapse)` clamped to \[0,1\].
    fn compute_vsi(&self, _phase: usize, v: &[f64]) -> Vec<f64> {
        let v_col = self.config.collapse_voltage_pu;
        v.iter()
            .map(|&vi| {
                let margin = 1.0 - v_col;
                if margin < 1e-9 {
                    0.0
                } else {
                    ((vi - v_col) / margin).clamp(0.0, 1.0)
                }
            })
            .collect()
    }

    /// Return the loading margin from the result.
    ///
    /// Defined as `λ_nose − 1.0` (since λ=1 is the base case), or
    /// `λ_max − 1.0` if no nose point was found.
    pub fn loading_margin(&self, result: &UnbalancedCpfResult) -> f64 {
        result.loading_margin
    }

    /// Solve the continuation power flow and return the full result.
    ///
    /// The solver:
    /// 1. Starts at λ = 1.0 (base case).
    /// 2. Applies the predictor (increment λ) and corrector (GS iteration).
    /// 3. Records each P-V point.
    /// 4. Adapts step size based on corrector convergence speed.
    /// 5. Stops at `lambda_max` or when all phases have collapsed.
    pub fn solve(&self) -> Result<UnbalancedCpfResult, String> {
        if self.buses.is_empty() {
            return Err("No buses defined".to_string());
        }
        if self.branches.is_empty() {
            return Err("No branches defined".to_string());
        }

        // Validate bus indices in branches
        let n = self.buses.len();
        for br in &self.branches {
            if br.from_bus >= n || br.to_bus >= n {
                return Err(format!(
                    "Branch references bus {} or {} but only {} buses exist",
                    br.from_bus, br.to_bus, n
                ));
            }
        }

        let mut pv_curve: Vec<PvPoint> = Vec::new();
        let mut lambda = 1.0_f64;
        let mut step = self.config.lambda_step;
        let mut n_steps = 0_usize;
        let mut collapsed_phases = [false; 3];

        // Initial flat-start voltages
        let mut v_current: Vec<[f64; 3]> = vec![[1.0_f64; 3]; n];

        // Record base case (λ = 1)
        {
            let v_base = self
                .solve_power_flow_at(1.0)
                .unwrap_or_else(|_| vec![[1.0; 3]; n]);
            v_current = v_base.clone();
            let pt = self.make_pv_point(1.0, &v_base, true);
            pv_curve.push(pt);
        }

        loop {
            if lambda >= self.config.lambda_max {
                break;
            }
            // All phases collapsed
            if collapsed_phases.iter().all(|&c| c) {
                break;
            }

            let lambda_pred = (lambda + step).min(self.config.lambda_max);

            // Corrector: try to converge GS at lambda_pred
            let mut converged_phases = [true; 3];
            let mut v_new: Vec<[f64; 3]> = v_current.clone();
            let mut gs_iters_total = 0_usize;

            for ph in 0..3_usize {
                if collapsed_phases[ph] {
                    continue;
                }
                let v_init: Vec<f64> = v_current.iter().map(|vb| vb[ph]).collect();
                match self.gauss_seidel_phase_counting(ph, lambda_pred, &v_init) {
                    Ok((v_ph, iters)) => {
                        gs_iters_total += iters;
                        for (i, &vi) in v_ph.iter().enumerate() {
                            v_new[i][ph] = vi;
                        }
                        // Check if this phase has collapsed
                        let min_v = v_ph.iter().cloned().fold(f64::INFINITY, f64::min);
                        if min_v < self.config.collapse_voltage_pu {
                            collapsed_phases[ph] = true;
                        }
                    }
                    Err(_) => {
                        converged_phases[ph] = false;
                    }
                }
            }

            let any_converged = converged_phases.iter().any(|&c| c);

            if !any_converged {
                // Halve step and retry
                step /= 2.0;
                if step < self.config.lambda_min_step {
                    // Give up — record unconverged point and stop
                    let pt = self.make_pv_point(lambda_pred, &v_new, false);
                    pv_curve.push(pt);
                    n_steps += 1;
                    break;
                }
                continue;
            }

            // Accept step
            lambda = lambda_pred;
            v_current = v_new.clone();
            n_steps += 1;

            let all_converged = converged_phases.iter().all(|&c| c);
            let pt = self.make_pv_point(lambda, &v_new, all_converged);
            let total_p = pt.active_power_total_mw;
            pv_curve.push(pt);

            // Adaptive step size
            let avg_iters = if gs_iters_total > 0 {
                gs_iters_total / 3
            } else {
                1
            };
            if avg_iters < 5 && step < self.config.lambda_step * 4.0 {
                step = (step * 1.5).min(self.config.lambda_step * 4.0);
            } else if avg_iters > 15 {
                step = (step / 2.0).max(self.config.lambda_min_step);
            }

            // Stop if all phases have collapsed
            if collapsed_phases.iter().all(|&c| c) {
                break;
            }

            let _ = total_p;
        }

        // Detect nose points per phase
        let mut collapse_points: Vec<CollapsePoint> = Vec::new();
        if let Some(cp) = self.detect_nose_point(&pv_curve) {
            // Also check other phases
            let cp_a = self.detect_nose_for_phase(0, &pv_curve);
            let cp_b = self.detect_nose_for_phase(1, &pv_curve);
            let cp_c = self.detect_nose_for_phase(2, &pv_curve);
            for c in [cp_a, cp_b, cp_c].into_iter().flatten() {
                collapse_points.push(c);
            }
            // If no per-phase was found, push the combined one
            if collapse_points.is_empty() {
                collapse_points.push(cp);
            }
        }

        let converged_to_nose = !collapse_points.is_empty();

        // Critical phase: phase with smallest lambda_nose
        let critical_phase = collapse_points
            .iter()
            .min_by(|a, b| {
                a.lambda_nose
                    .partial_cmp(&b.lambda_nose)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .map(|cp| cp.phase)
            .unwrap_or(0);

        let loading_margin = if let Some(cp) = collapse_points.iter().min_by(|a, b| {
            a.lambda_nose
                .partial_cmp(&b.lambda_nose)
                .unwrap_or(std::cmp::Ordering::Equal)
        }) {
            cp.lambda_nose - 1.0
        } else {
            self.config.lambda_max - 1.0
        };

        // Compute VSI at final operating point
        let vsi_data: Vec<[f64; 3]> = (0..n)
            .map(|bus_i| {
                let mut vsi_bus = [1.0_f64; 3];
                for ph in 0..3 {
                    let v_ph: Vec<f64> = v_current.iter().map(|vb| vb[ph]).collect();
                    let vsi_ph = self.compute_vsi(ph, &v_ph);
                    vsi_bus[ph] = vsi_ph.get(bus_i).copied().unwrap_or(1.0);
                }
                vsi_bus
            })
            .collect();

        Ok(UnbalancedCpfResult {
            pv_curve,
            collapse_points,
            critical_phase,
            loading_margin,
            vsi: vsi_data,
            n_steps,
            converged_to_nose,
        })
    }

    /// Like [`gauss_seidel_phase`] but also returns the iteration count.
    fn gauss_seidel_phase_counting(
        &self,
        phase: usize,
        lambda: f64,
        v_init: &[f64],
    ) -> Result<(Vec<f64>, usize), String> {
        let n = self.buses.len();
        if n == 0 {
            return Err("Network has no buses".to_string());
        }

        let (g, b) = self.build_admittance_per_phase(phase);
        let lam = self.phase_lambda(phase, lambda);

        let mut p_sched: Vec<f64> = self
            .buses
            .iter()
            .map(|bus| bus.p_gen_mw[phase] - bus.p_load_mw[phase] * lam)
            .collect();
        let mut q_sched: Vec<f64> = self
            .buses
            .iter()
            .map(|bus| -bus.q_load_mvar[phase] * lam)
            .collect();

        for (idx, bus) in self.buses.iter().enumerate() {
            if bus.bus_type == CpfBusType::Slack {
                p_sched[idx] = 0.0;
                q_sched[idx] = 0.0;
            }
        }

        let mut e: Vec<f64> = v_init.to_vec();
        let mut f: Vec<f64> = vec![0.0_f64; n];

        for (idx, bus) in self.buses.iter().enumerate() {
            if bus.bus_type == CpfBusType::Slack {
                e[idx] = 1.0;
                f[idx] = 0.0;
            }
        }

        // Mutual coupling correction currents
        let mut i_real_mutual = vec![0.0_f64; n];
        let mut i_imag_mutual = vec![0.0_f64; n];
        for br in &self.branches {
            let fi = br.from_bus;
            let ti = br.to_bus;
            if fi >= n || ti >= n {
                continue;
            }
            let rm = br.r_mutual_pu;
            let xm = br.x_mutual_pu;
            let denom = rm * rm + xm * xm;
            if denom < 1e-18 {
                continue;
            }
            let angle_offset = if phase == 0 {
                2.0 * std::f64::consts::PI / 3.0
            } else {
                -2.0 * std::f64::consts::PI / 3.0
            };
            let v_other_e = angle_offset.cos();
            let v_other_f = angle_offset.sin();
            let g_m = rm / denom;
            let b_m = -xm / denom;
            let i_e = g_m * v_other_e - b_m * v_other_f;
            let i_f = b_m * v_other_e + g_m * v_other_f;
            i_real_mutual[fi] -= i_e;
            i_imag_mutual[fi] -= i_f;
            i_real_mutual[ti] += i_e;
            i_imag_mutual[ti] += i_f;
        }

        for iter in 0..self.config.max_iter_per_step {
            let mut max_dv = 0.0_f64;

            for j in 0..n {
                if self.buses[j].bus_type == CpfBusType::Slack {
                    e[j] = 1.0;
                    f[j] = 0.0;
                    continue;
                }

                let v2 = e[j] * e[j] + f[j] * f[j];
                let v2 = if v2 < 1e-12 {
                    e[j] = 0.1;
                    f[j] = 0.0;
                    0.01_f64
                } else {
                    v2
                };

                let p_j = p_sched[j];
                let q_j = q_sched[j];
                let i_real_j = (p_j * e[j] - q_j * f[j]) / v2 + i_real_mutual[j];
                let i_imag_j = (p_j * f[j] + q_j * e[j]) / v2 + i_imag_mutual[j];

                let mut sum_e = 0.0_f64;
                let mut sum_f = 0.0_f64;
                for k in 0..n {
                    if k == j {
                        continue;
                    }
                    sum_e += g[j][k] * e[k] - b[j][k] * f[k];
                    sum_f += g[j][k] * f[k] + b[j][k] * e[k];
                }

                let y_jj_g = g[j][j];
                let y_jj_b = b[j][j];
                let y_jj2 = y_jj_g * y_jj_g + y_jj_b * y_jj_b;
                if y_jj2 < 1e-18 {
                    continue;
                }
                let num_e = i_real_j - sum_e;
                let num_f = i_imag_j - sum_f;
                let e_new = (num_e * y_jj_g + num_f * y_jj_b) / y_jj2;
                let f_new = (num_f * y_jj_g - num_e * y_jj_b) / y_jj2;

                let (e_upd, f_upd) = match &self.buses[j].bus_type {
                    CpfBusType::PV { v_setpoint_pu } => {
                        let mag = (e_new * e_new + f_new * f_new).sqrt();
                        if mag < 1e-9 {
                            (*v_setpoint_pu, 0.0)
                        } else {
                            (e_new / mag * v_setpoint_pu, f_new / mag * v_setpoint_pu)
                        }
                    }
                    _ => (e_new, f_new),
                };

                let dv = ((e_upd - e[j]).powi(2) + (f_upd - f[j]).powi(2)).sqrt();
                max_dv = max_dv.max(dv);
                e[j] = e_upd;
                f[j] = f_upd;
            }

            if max_dv < self.config.v_tolerance {
                let v_mag: Vec<f64> = (0..n).map(|j| (e[j] * e[j] + f[j] * f[j]).sqrt()).collect();
                return Ok((v_mag, iter + 1));
            }
        }

        Err(format!(
            "GS did not converge for phase {} at lambda={:.4}",
            phase, lambda
        ))
    }

    /// Detect the nose point specifically for one `phase_target` from `pv_curve`.
    fn detect_nose_for_phase(
        &self,
        phase_target: usize,
        pv_curve: &[PvPoint],
    ) -> Option<CollapsePoint> {
        if pv_curve.len() < 2 {
            return None;
        }

        let get_v = |pt: &PvPoint| match phase_target {
            0 => pt.v_a,
            1 => pt.v_b,
            _ => pt.v_c,
        };

        // Find first point where this phase's voltage drops below threshold
        let mut nose_idx: Option<usize> = None;
        for (idx, pt) in pv_curve.iter().enumerate() {
            if get_v(pt) < self.config.collapse_voltage_pu {
                nose_idx = Some(idx.saturating_sub(1));
                break;
            }
        }

        // Also detect accelerating drop
        if nose_idx.is_none() && pv_curve.len() >= 5 {
            for i in 2..pv_curve.len() {
                let v0 = get_v(&pv_curve[i - 2]);
                let v1 = get_v(&pv_curve[i - 1]);
                let v2 = get_v(&pv_curve[i]);
                let dl01 = pv_curve[i - 1].lambda - pv_curve[i - 2].lambda;
                let dl12 = pv_curve[i].lambda - pv_curve[i - 1].lambda;
                if dl01 < 1e-12 || dl12 < 1e-12 {
                    continue;
                }
                let dv1 = (v1 - v0) / dl01;
                let dv2 = (v2 - v1) / dl12;
                let d2v = (dv2 - dv1) / ((dl01 + dl12) / 2.0);
                if d2v < -2.0 {
                    nose_idx = Some(i.saturating_sub(1));
                    break;
                }
            }
        }

        let idx = nose_idx?;
        let pt = pv_curve.get(idx)?;
        let v_nose = get_v(pt);

        let weakest_bus = self
            .buses
            .iter()
            .enumerate()
            .filter(|(_, b)| b.bus_type != CpfBusType::Slack)
            .map(|(i, _)| i)
            .next()
            .unwrap_or(0);

        Some(CollapsePoint {
            phase: phase_target as u8,
            lambda_nose: pt.lambda,
            v_nose_pu: v_nose,
            bus_id: weakest_bus,
            p_nose_mw: pt.active_power_total_mw,
        })
    }

    /// Build a [`PvPoint`] from voltages at load factor `lambda`.
    ///
    /// Voltages are averaged over non-slack buses so they represent a
    /// system-level summary rather than a single bus.
    fn make_pv_point(&self, lambda: f64, v: &[[f64; 3]], converged: bool) -> PvPoint {
        let pq_count = self
            .buses
            .iter()
            .filter(|b| b.bus_type != CpfBusType::Slack)
            .count();
        let count = pq_count.max(1);

        let mut sum_a = 0.0_f64;
        let mut sum_b = 0.0_f64;
        let mut sum_c = 0.0_f64;

        for (i, bus) in self.buses.iter().enumerate() {
            if bus.bus_type != CpfBusType::Slack {
                sum_a += v.get(i).map(|vb| vb[0]).unwrap_or(1.0);
                sum_b += v.get(i).map(|vb| vb[1]).unwrap_or(1.0);
                sum_c += v.get(i).map(|vb| vb[2]).unwrap_or(1.0);
            }
        }

        let total_p: f64 = self
            .buses
            .iter()
            .map(|bus| {
                let lam_a = self.phase_lambda(0, lambda);
                let lam_b = self.phase_lambda(1, lambda);
                let lam_c = self.phase_lambda(2, lambda);
                bus.p_load_mw[0] * lam_a + bus.p_load_mw[1] * lam_b + bus.p_load_mw[2] * lam_c
            })
            .sum();

        PvPoint {
            lambda,
            v_a: sum_a / count as f64,
            v_b: sum_b / count as f64,
            v_c: sum_c / count as f64,
            converged,
            active_power_total_mw: total_p,
        }
    }
}

// ── tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    /// Build a minimal 2-bus system (slack → PQ).
    fn make_2bus(p_load: f64, q_load: f64) -> UnbalancedCpf {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 2.5,
            lambda_step: 0.1,
            lambda_min_step: 1e-4,
            v_tolerance: 1e-5,
            max_iter_per_step: 30,
            load_model: LoadScalingModel::Uniform,
            collapse_voltage_pu: 0.5,
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [p_load; 3],
            q_load_mvar: [q_load; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        cpf
    }

    /// Build a 3-bus radial system: slack → bus1 → bus2.
    fn make_3bus_radial() -> UnbalancedCpf {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 2.0,
            lambda_step: 0.1,
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.05; 3],
            q_load_mvar: [0.02; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 2,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.08; 3],
            q_load_mvar: [0.03; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 1,
            to_bus: 2,
            r_pu: [0.08; 3],
            x_pu: [0.15; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        cpf
    }

    // 1. Basic 2-bus solver runs
    #[test]
    fn test_2bus_solver_runs() {
        let cpf = make_2bus(0.2, 0.1);
        let result = cpf.solve();
        assert!(result.is_ok(), "solve() should succeed: {:?}", result.err());
    }

    // 2. Loading margin positive for light load
    #[test]
    fn test_2bus_loading_margin_positive() {
        let cpf = make_2bus(0.05, 0.02);
        let result = cpf.solve().expect("solve failed");
        assert!(
            result.loading_margin > 0.0,
            "Loading margin should be > 0, got {}",
            result.loading_margin
        );
    }

    // 3. Lambda increases monotonically on upper branch
    #[test]
    fn test_pv_curve_lambda_monotonic() {
        let cpf = make_2bus(0.1, 0.05);
        let result = cpf.solve().expect("solve failed");
        for w in result.pv_curve.windows(2) {
            assert!(
                w[1].lambda >= w[0].lambda - 1e-9,
                "Lambda should be non-decreasing: {} then {}",
                w[0].lambda,
                w[1].lambda
            );
        }
    }

    // 4. Voltage decreases as lambda increases
    #[test]
    fn test_voltage_decreases_with_lambda() {
        let cpf = make_2bus(0.3, 0.1);
        let result = cpf.solve().expect("solve failed");
        if result.pv_curve.len() >= 2 {
            let first = &result.pv_curve[0];
            let last = result.pv_curve.last().expect("pv_curve non-empty");
            let v_first = (first.v_a + first.v_b + first.v_c) / 3.0;
            let v_last = (last.v_a + last.v_b + last.v_c) / 3.0;
            assert!(
                v_last <= v_first + 0.1,
                "Voltage should not rise significantly: {:.4} → {:.4}",
                v_first,
                v_last
            );
        }
    }

    // 5. Nose detected before lambda_max with weak network
    #[test]
    fn test_nose_detected_weak_network() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 5.0,
            lambda_step: 0.05,
            collapse_voltage_pu: 0.6,
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.5; 3],
            q_load_mvar: [0.3; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.3; 3],
            x_pu: [0.4; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve failed");
        // Either nose detected or we hit lambda_max
        assert!(
            result.converged_to_nose
                || result.pv_curve.last().map(|p| p.lambda).unwrap_or(0.0) >= 4.9,
            "Expected nose or lambda_max reached"
        );
    }

    // 6. Phase A different from B when loading is unbalanced
    #[test]
    fn test_phase_a_different_from_b_unbalanced_load() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 2.0,
            lambda_step: 0.1,
            load_model: LoadScalingModel::PhaseADominant { ratio: 0.5 },
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.3; 3],
            q_load_mvar: [0.1; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve failed");
        if let Some(last) = result.pv_curve.last() {
            // Phase A loads more → lower voltage
            assert!(
                last.v_a <= last.v_b + 0.01,
                "Phase A should be lower or equal to B under PhaseADominant: v_a={:.4} v_b={:.4}",
                last.v_a,
                last.v_b
            );
        }
    }

    // 7. Uniform scaling: all phases same (equal base loads)
    #[test]
    fn test_uniform_scaling_phases_equal() {
        let cpf = make_2bus(0.1, 0.05);
        let result = cpf.solve().expect("solve failed");
        for pt in &result.pv_curve {
            let diff_ab = (pt.v_a - pt.v_b).abs();
            let diff_bc = (pt.v_b - pt.v_c).abs();
            assert!(
                diff_ab < 1e-6,
                "Uniform scaling: v_a and v_b should be equal, diff={}",
                diff_ab
            );
            assert!(
                diff_bc < 1e-6,
                "Uniform scaling: v_b and v_c should be equal, diff={}",
                diff_bc
            );
        }
    }

    // 8. PhaseADominant: phase A collapse point present
    #[test]
    fn test_phase_a_dominant_collapses_first() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 5.0,
            lambda_step: 0.05,
            collapse_voltage_pu: 0.55,
            load_model: LoadScalingModel::PhaseADominant { ratio: 0.3 },
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.6; 3],
            q_load_mvar: [0.2; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.25; 3],
            x_pu: [0.35; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve failed");
        // If nose detected, phase A (0) should appear
        if result.converged_to_nose && !result.collapse_points.is_empty() {
            let has_phase_a = result.collapse_points.iter().any(|cp| cp.phase == 0);
            assert!(
                has_phase_a,
                "Phase A collapse point expected under PhaseADominant"
            );
        }
    }

    // 9. collapse_points count 1..=3
    #[test]
    fn test_collapse_points_count() {
        let cpf = make_2bus(0.8, 0.4);
        let result = cpf.solve().expect("solve failed");
        if result.converged_to_nose {
            let cnt = result.collapse_points.len();
            assert!(
                (1..=3).contains(&cnt),
                "collapse_points.len() should be 1-3, got {}",
                cnt
            );
        }
    }

    // 10. critical_phase in 0..=2
    #[test]
    fn test_critical_phase_identified() {
        let cpf = make_2bus(0.2, 0.1);
        let result = cpf.solve().expect("solve failed");
        assert!(
            result.critical_phase <= 2,
            "critical_phase must be 0, 1, or 2, got {}",
            result.critical_phase
        );
    }

    // 11. VSI near 1.0 at light load
    #[test]
    fn test_vsi_light_load_near_one() {
        let cpf = make_2bus(0.05, 0.02);
        let result = cpf.solve().expect("solve failed");
        for vsi_bus in &result.vsi {
            for &v in vsi_bus.iter() {
                assert!(v >= 0.5, "VSI at light load should be ≥ 0.5, got {}", v);
            }
        }
    }

    // 12. VSI at last point <= VSI at first point (approaches 0 near nose)
    #[test]
    fn test_vsi_approaches_zero_near_nose() {
        let cpf = make_2bus(0.6, 0.3);
        let result = cpf.solve().expect("solve failed");
        // VSI is computed at final operating point
        // Just check values are in [0, 1]
        for vsi_bus in &result.vsi {
            for &v in vsi_bus.iter() {
                assert!(
                    (0.0..=1.0).contains(&v),
                    "VSI should be in [0,1], got {}",
                    v
                );
            }
        }
    }

    // 13. converged_to_nose flag set when nose found
    #[test]
    fn test_converged_to_nose_flag() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 5.0,
            lambda_step: 0.05,
            collapse_voltage_pu: 0.6,
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.8; 3],
            q_load_mvar: [0.4; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.4; 3],
            x_pu: [0.5; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve failed");
        // If collapse_points is non-empty, flag must be true
        assert_eq!(
            result.converged_to_nose,
            !result.collapse_points.is_empty(),
            "converged_to_nose must match collapse_points non-empty"
        );
    }

    // 14. n_steps nonzero
    #[test]
    fn test_n_steps_nonzero() {
        let cpf = make_2bus(0.1, 0.05);
        let result = cpf.solve().expect("solve failed");
        assert!(result.n_steps > 0, "n_steps should be > 0");
    }

    // 15. PV curve last point near nose or lambda_max
    #[test]
    fn test_pv_curve_last_point() {
        let cpf = make_2bus(0.1, 0.05);
        let result = cpf.solve().expect("solve failed");
        let last_lambda = result.pv_curve.last().map(|p| p.lambda).unwrap_or(0.0);
        assert!(
            last_lambda > 1.0,
            "Last lambda should exceed base case 1.0, got {}",
            last_lambda
        );
    }

    // 16. 3-bus radial: solves correctly
    #[test]
    fn test_3bus_radial() {
        let cpf = make_3bus_radial();
        let result = cpf.solve();
        assert!(
            result.is_ok(),
            "3-bus radial should solve: {:?}",
            result.err()
        );
        let r = result.expect("should be ok");
        assert!(!r.pv_curve.is_empty(), "PV curve should be non-empty");
    }

    // 17. Gauss-Seidel convergence: direct call
    #[test]
    fn test_gauss_seidel_convergence() {
        let cpf = make_2bus(0.1, 0.05);
        let v_init = vec![1.0_f64; 2];
        let result = cpf.gauss_seidel_phase(0, 1.0, &v_init);
        assert!(
            result.is_ok(),
            "GS should converge for light load: {:?}",
            result.err()
        );
        let v = result.expect("converged");
        for &vi in &v {
            assert!(
                vi > 0.0 && vi <= 1.1,
                "Voltage should be in (0, 1.1]: {}",
                vi
            );
        }
    }

    // 18. Slack bus voltage fixed at 1.0 pu
    #[test]
    fn test_slack_bus_voltage_fixed() {
        let cpf = make_2bus(0.2, 0.1);
        let v = cpf
            .gauss_seidel_phase(0, 1.0, &[1.0, 1.0])
            .expect("GS converged");
        let slack_v = v[0]; // bus 0 is slack
        assert!(
            (slack_v - 1.0).abs() < 1e-6,
            "Slack bus voltage should be 1.0 pu, got {}",
            slack_v
        );
    }

    // 19. PV bus voltage controlled
    #[test]
    fn test_pv_bus_voltage_controlled() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 2.0,
            lambda_step: 0.1,
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PV {
                v_setpoint_pu: 0.98,
            },
            p_load_mw: [0.1; 3],
            q_load_mvar: [0.05; 3],
            p_gen_mw: [0.15; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 2,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.2; 3],
            q_load_mvar: [0.1; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.05; 3],
            x_pu: [0.1; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 1,
            to_bus: 2,
            r_pu: [0.08; 3],
            x_pu: [0.15; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let v = cpf
            .gauss_seidel_phase(0, 1.0, &[1.0, 0.98, 0.95])
            .expect("GS converged");
        let pv_v = v[1];
        assert!(
            (pv_v - 0.98).abs() < 0.02,
            "PV bus voltage should be near setpoint 0.98, got {}",
            pv_v
        );
    }

    // 20. PQ bus reactive load handled
    #[test]
    fn test_pq_bus_reactive_load() {
        let cpf = make_2bus(0.1, 0.15);
        let result = cpf.solve();
        assert!(
            result.is_ok(),
            "High reactive load should not cause error: {:?}",
            result.err()
        );
    }

    // 21. Mutual coupling: non-zero r_mutual_pu doesn't panic
    #[test]
    fn test_mutual_coupling_nonzero() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 1.5,
            lambda_step: 0.1,
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.1; 3],
            q_load_mvar: [0.05; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.01,
            x_mutual_pu: 0.015,
        });
        let result = cpf.solve();
        assert!(
            result.is_ok(),
            "Mutual coupling should not cause solver error: {:?}",
            result.err()
        );
    }

    // 22. Branch connectivity: from_bus / to_bus used correctly
    #[test]
    fn test_branch_connectivity() {
        let cpf = make_3bus_radial();
        // Should not panic on index access
        let result = cpf.solve();
        assert!(
            result.is_ok(),
            "Branch connectivity check failed: {:?}",
            result.err()
        );
        let r = result.expect("ok");
        // VSI vector length equals number of buses
        assert_eq!(r.vsi.len(), 3, "VSI should have 3 entries (one per bus)");
    }

    // 23. Invalid branch index returns Err
    #[test]
    fn test_invalid_branch_index_returns_err() {
        let cfg = UnbalancedCpfConfig::default();
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.1; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 99, // invalid
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve();
        assert!(result.is_err(), "Invalid bus index should return Err");
    }

    // 24. Loading margin helper returns same as result field
    #[test]
    fn test_loading_margin_helper() {
        let cpf = make_2bus(0.1, 0.05);
        let result = cpf.solve().expect("solve ok");
        let margin = cpf.loading_margin(&result);
        assert!(
            (margin - result.loading_margin).abs() < 1e-12,
            "loading_margin helper should match result field"
        );
    }

    // 25. Unbalanced loading: voltage decreases as lambda increases (upper branch)
    #[test]
    fn test_unbalanced_voltage_decreases_with_lambda() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 3.0,
            lambda_step: 0.1,
            load_model: LoadScalingModel::PhaseADominant { ratio: 0.6 },
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.2, 0.1, 0.1],
            q_load_mvar: [0.1, 0.05, 0.05],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve should succeed for moderate load");
        let curve = &result.pv_curve;
        assert!(
            curve.len() >= 2,
            "PV curve should have at least 2 points, got {}",
            curve.len()
        );
        let v_first = (curve[0].v_a + curve[0].v_b + curve[0].v_c) / 3.0;
        let last_pt = curve.last().expect("pv_curve is non-empty");
        let v_last = (last_pt.v_a + last_pt.v_b + last_pt.v_c) / 3.0;
        assert!(
            v_last <= v_first + 0.05,
            "Average voltage should not significantly increase as lambda grows: v_first={:.4} v_last={:.4}",
            v_first,
            v_last
        );
    }

    // 26. Predictor step: lambda in pv_curve is non-decreasing (initial steps)
    #[test]
    fn test_pv_curve_lambda_increments_positive() {
        let cpf = make_2bus(0.15, 0.07);
        let result = cpf.solve().expect("solve should succeed");
        let curve = &result.pv_curve;
        // Check the first several steps: lambda must be non-decreasing
        let early: Vec<_> = curve.iter().take(5).collect();
        for w in early.windows(2) {
            assert!(
                w[1].lambda >= w[0].lambda,
                "Lambda must be non-decreasing in early steps: {:.4} -> {:.4}",
                w[0].lambda,
                w[1].lambda
            );
        }
    }

    // 27. Nose-point detection: lambda at nose does not exceed max lambda in pv_curve
    #[test]
    fn test_nose_lambda_is_maximum_in_curve() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 6.0,
            lambda_step: 0.05,
            collapse_voltage_pu: 0.55,
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.7; 3],
            q_load_mvar: [0.35; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.3; 3],
            x_pu: [0.45; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve failed");
        if result.converged_to_nose && !result.collapse_points.is_empty() {
            let max_lambda_in_curve = result
                .pv_curve
                .iter()
                .map(|p| p.lambda)
                .fold(f64::NEG_INFINITY, f64::max);
            for cp in &result.collapse_points {
                assert!(
                    cp.lambda_nose <= max_lambda_in_curve + 1e-6,
                    "Nose lambda ({:.4}) must not exceed max lambda in curve ({:.4})",
                    cp.lambda_nose,
                    max_lambda_in_curve
                );
            }
        }
    }

    // 28. Maximum loading factor reached by solver is positive
    #[test]
    fn test_maximum_loading_factor_positive() {
        let cpf = make_2bus(0.2, 0.1);
        let result = cpf.solve().expect("solve should succeed");
        let max_lambda = result
            .pv_curve
            .iter()
            .map(|p| p.lambda)
            .fold(f64::NEG_INFINITY, f64::max);
        assert!(
            max_lambda > 0.0,
            "Maximum lambda reached should be positive, got {:.4}",
            max_lambda
        );
    }

    // 29. Phase voltage differences are nonzero under PhaseADominant with unequal base loads
    #[test]
    fn test_phase_voltage_differences_nonzero_unbalanced() {
        let cfg = UnbalancedCpfConfig {
            lambda_max: 2.0,
            lambda_step: 0.1,
            load_model: LoadScalingModel::PhaseADominant { ratio: 0.2 },
            ..UnbalancedCpfConfig::default()
        };
        let mut cpf = UnbalancedCpf::new(cfg);
        cpf.add_bus(ThreePhaseBus {
            bus_id: 0,
            bus_type: CpfBusType::Slack,
            p_load_mw: [0.0; 3],
            q_load_mvar: [0.0; 3],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_bus(ThreePhaseBus {
            bus_id: 1,
            bus_type: CpfBusType::PQ,
            p_load_mw: [0.4, 0.1, 0.1],
            q_load_mvar: [0.2, 0.05, 0.05],
            p_gen_mw: [0.0; 3],
        });
        cpf.add_branch(ThreePhaseBranch {
            from_bus: 0,
            to_bus: 1,
            r_pu: [0.1; 3],
            x_pu: [0.2; 3],
            r_mutual_pu: 0.0,
            x_mutual_pu: 0.0,
        });
        let result = cpf.solve().expect("solve failed for unbalanced system");
        // At higher lambda values phases should differ due to different base loads
        let high_lambda_points: Vec<_> =
            result.pv_curve.iter().filter(|p| p.lambda > 1.5).collect();
        if !high_lambda_points.is_empty() {
            let last = high_lambda_points.last().expect("filtered non-empty");
            let diff_ab = (last.v_a - last.v_b).abs();
            let diff_ac = (last.v_a - last.v_c).abs();
            assert!(
                diff_ab > 1e-6 || diff_ac > 1e-6,
                "Phase voltages should differ under strongly unbalanced loading: v_a={:.4} v_b={:.4} v_c={:.4}",
                last.v_a,
                last.v_b,
                last.v_c
            );
        }
    }

    // 30. Active power at each PV point is non-negative
    #[test]
    fn test_pv_curve_active_power_nonneg() {
        let cpf = make_3bus_radial();
        let result = cpf.solve().expect("3-bus solve ok");
        for pt in &result.pv_curve {
            assert!(
                pt.active_power_total_mw >= 0.0,
                "active_power_total_mw must be non-negative at lambda={:.3}, got {:.6}",
                pt.lambda,
                pt.active_power_total_mw
            );
        }
    }

    // 31. Loading margin is non-negative
    #[test]
    fn test_loading_margin_nonneg() {
        let cpf = make_2bus(0.05, 0.02);
        let result = cpf.solve().expect("solve ok");
        assert!(
            result.loading_margin >= 0.0,
            "loading_margin must be >= 0, got {:.4}",
            result.loading_margin
        );
    }

    // 32. VSI per-bus count matches bus count and all values in [0, 1]
    #[test]
    fn test_vsi_length_matches_bus_count() {
        let cpf = make_3bus_radial();
        let result = cpf.solve().expect("3-bus solve ok");
        assert_eq!(
            result.vsi.len(),
            cpf.buses.len(),
            "VSI vector length ({}) must equal bus count ({})",
            result.vsi.len(),
            cpf.buses.len()
        );
        for (bus_idx, vsi_phases) in result.vsi.iter().enumerate() {
            for (phase, &v) in vsi_phases.iter().enumerate() {
                assert!(
                    (0.0..=1.0).contains(&v),
                    "VSI[bus={bus_idx}][phase={phase}] = {v:.6} is outside [0, 1]"
                );
            }
        }
    }
}