powerio-dist 0.5.0

Multiconductor distribution network model and lossless converters for OpenDSS, PMD JSON, and BMOPF JSON.
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
//! [`DistNetwork`] into strict BMOPF JSON.
//!
//! Output is schema valid wherever the schema permits the data.
//!
//! Numbers serialize through serde_json (shortest round trip form).
//! Nonfinite values cannot appear in JSON; they emit as 0 with a warning
//! naming the element and field.

use std::collections::{BTreeMap, BTreeSet};

use serde_json::{Map, Value, json};

use crate::convert::Conversion;
use crate::model::{
    Configuration, DistGenerator, DistLoadVoltageModel, DistNetwork, DistTransformer, Mat, Winding,
    WindingConn, n_winding_impedance_base, pair_keys,
};

/// The `$schema` stamped into every document's `meta`: the current BMOPF
/// schema URI used by BMOPFTools.
const BMOPF_SCHEMA_ID: &str =
    "https://raw.githubusercontent.com/frederikgeth/bmopf-report/main/schema/bmopf.json";

const RAW_BMOPF_TOP_LEVEL: &[&str] = &[
    "capacitor",
    "ibr",
    "control_profile",
    "dc_bus",
    "dc_line",
    "dc_load",
    "dc_source",
];

const TRANSFORMER_NO_LOAD_ALLOWED_EXTRAS: [&str; 4] =
    ["g_no_load", "b_no_load", "%noloadloss", "%imag"];
const TRANSFORMER_TWO_WINDING_ALLOWED_EXTRAS: [&str; 6] = [
    "tap_min",
    "tap_max",
    "g_no_load",
    "b_no_load",
    "%noloadloss",
    "%imag",
];

/// Writes the strict BMOPF document. Every field the schema cannot carry
/// is reported in the warnings.
///
/// # Panics
///
/// Never in practice: the document is maps, strings, and finite numbers,
/// which always serialize.
pub fn write_bmopf_json(net: &DistNetwork) -> Conversion {
    let mut w = Writer {
        warnings: Vec::new(),
        grounded: net
            .buses
            .iter()
            .map(|b| (b.id.to_ascii_lowercase(), b.grounded.clone()))
            .collect(),
    };
    let doc = w.document(net);
    Conversion {
        text: serde_json::to_string_pretty(&doc).expect("maps and finite numbers") + "\n",
        warnings: w.warnings,
    }
}

struct Writer {
    warnings: Vec<String>,
    grounded: BTreeMap<String, Vec<String>>,
}

impl Writer {
    fn warn(&mut self, msg: impl Into<String>) {
        self.warnings.push(msg.into());
    }

    /// Finite number guard (the jnum pattern): JSON has no Inf/NaN.
    fn num(&mut self, v: f64, what: &str) -> Value {
        if v.is_finite() {
            json!(v)
        } else {
            self.warn(format!("{what}: nonfinite value emitted as 0"));
            json!(0.0)
        }
    }

    fn nums(&mut self, vs: &[f64], what: &str) -> Value {
        Value::Array(vs.iter().map(|&v| self.num(v, what)).collect())
    }

    fn extras_dropped(&mut self, extras: &crate::model::Extras, what: &str) {
        for key in extras.keys() {
            // `bmopf_subtype` is reader bookkeeping; `conn` marks a delta shunt
            // whose geometry already lives in the off diagonal B matrix, so it
            // is preserved, not dropped.
            if key == "bmopf_subtype" || key == "conn" {
                continue;
            }
            self.warn(format!(
                "{what}: `{key}` has no place in the BMOPF schema; dropped from the output"
            ));
        }
    }

    /// Provenance + schema-vintage self-identification (the BMOPF `meta` object):
    /// "generated by powerio vX, targeting BMOPF schema vintage Y." Deterministic
    /// and round-trip stable — no timestamp, and nothing that depends on the
    /// immediate source format (which a round trip would change) — so canonical
    /// output is idempotent. The vintage lives in `$schema` (the canonical
    /// bmopf-report `$id`).
    fn meta() -> Value {
        json!({
            "$schema": BMOPF_SCHEMA_ID,
            "generator": {"tool": "powerio", "version": env!("CARGO_PKG_VERSION")},
        })
    }

    fn document(&mut self, net: &DistNetwork) -> Value {
        let mut doc = Map::new();
        if let Some(name) = &net.name {
            doc.insert("name".into(), json!(name));
        }
        doc.insert("meta".into(), Self::meta());

        let mut buses = Map::new();
        for b in &net.buses {
            let mut o = Map::new();
            o.insert("terminal_names".into(), json!(b.terminals));
            if !b.grounded.is_empty() {
                o.insert("perfectly_grounded_terminals".into(), json!(b.grounded));
            }
            if let Some(v) = b.v_min {
                o.insert("v_min".into(), Value::Array(vec![self.num(v, "bus v_min")]));
            }
            if let Some(v) = b.v_max {
                o.insert("v_max".into(), Value::Array(vec![self.num(v, "bus v_max")]));
            }
            for (key, bound) in [
                ("vpn_min", &b.vpn_min),
                ("vpn_max", &b.vpn_max),
                ("vpp_min", &b.vpp_min),
                ("vpp_max", &b.vpp_max),
                ("vsym_min", &b.vsym_min),
                ("vsym_max", &b.vsym_max),
            ] {
                if let Some(v) = bound {
                    o.insert(key.into(), self.nums(v, &format!("bus {key}")));
                }
            }
            // Coordinates and other extras have no bus fields in the schema.
            self.extras_dropped(&b.extras, &format!("bus {}", b.id));
            buses.insert(b.id.clone(), Value::Object(o));
        }
        doc.insert("bus".into(), Value::Object(buses));

        if !net.linecodes.is_empty() {
            let mut codes = Map::new();
            for c in &net.linecodes {
                let mut o = Map::new();
                // The schema requires R_series_1_1 and X_series_1_1; an
                // empty matrix would drop them and invalidate the output.
                let dim = c.r_series.len().max(c.x_series.len()).max(1);
                if c.r_series.is_empty() && c.x_series.is_empty() {
                    self.warn(format!(
                        "linecode {}: no series matrix; emitted as 1 conductor \
                         zero impedance",
                        c.name
                    ));
                } else if c.r_series.is_empty() || c.x_series.is_empty() {
                    self.warn(format!(
                        "linecode {}: R_series and X_series sizes disagree; the \
                         empty one emitted as zeros",
                        c.name
                    ));
                }
                self.required_matrix(&mut o, "R_series", &c.r_series, dim, &c.name);
                self.required_matrix(&mut o, "X_series", &c.x_series, dim, &c.name);
                self.flat_matrix(&mut o, "G_from", &c.g_from, &c.name);
                self.flat_matrix(&mut o, "G_to", &c.g_to, &c.name);
                self.flat_matrix(&mut o, "B_from", &c.b_from, &c.name);
                self.flat_matrix(&mut o, "B_to", &c.b_to, &c.name);
                if let Some(i_max) = &c.i_max {
                    o.insert("i_max".into(), self.nums(i_max, "linecode i_max"));
                }
                if let Some(s_max) = &c.s_max {
                    o.insert("s_max".into(), self.nums(s_max, "linecode s_max"));
                }
                self.extras_dropped(&c.extras, &format!("linecode {}", c.name));
                codes.insert(c.name.clone(), Value::Object(o));
            }
            doc.insert("linecode".into(), Value::Object(codes));
        }

        self.branches(net, &mut doc);
        self.injections(net, &mut doc);

        let transformers = self.transformers(net);
        if !transformers.is_empty() {
            doc.insert("transformer".into(), Value::Object(transformers));
        }

        self.untyped_bmopf_tables(net, &mut doc);

        for u in &net.untyped {
            if Self::is_emitted_untyped(u) {
                continue;
            }
            self.warn(format!(
                "{} {}: class is not represented in BMOPF; dropped from the output",
                u.class, u.name
            ));
        }
        self.prune_unreferenced_buses(&mut doc);
        Value::Object(doc)
    }

    fn is_emitted_untyped(u: &crate::model::UntypedObject) -> bool {
        RAW_BMOPF_TOP_LEVEL.contains(&u.class.as_str()) || u.class.starts_with("transformer.")
    }

    fn untyped_bmopf_tables(&mut self, net: &DistNetwork, doc: &mut Map<String, Value>) {
        for u in &net.untyped {
            let Some(value) = raw_bmopf_value(u) else {
                self.warn(format!(
                    "{} {}: untyped BMOPF object could not be parsed as JSON; dropped from the output",
                    u.class, u.name
                ));
                continue;
            };
            if RAW_BMOPF_TOP_LEVEL.contains(&u.class.as_str()) {
                doc.entry(u.class.clone())
                    .or_insert_with(|| Value::Object(Map::new()))
                    .as_object_mut()
                    .expect("BMOPF tables are objects")
                    .insert(u.name.clone(), value);
            } else if let Some(subtype) = u.class.strip_prefix("transformer.") {
                doc.entry("transformer")
                    .or_insert_with(|| Value::Object(Map::new()))
                    .as_object_mut()
                    .expect("transformer table is an object")
                    .entry(subtype.to_string())
                    .or_insert_with(|| Value::Object(Map::new()))
                    .as_object_mut()
                    .expect("transformer subtype table is an object")
                    .insert(u.name.clone(), value);
            }
        }
    }

    fn prune_unreferenced_buses(&mut self, doc: &mut Map<String, Value>) {
        let mut refs = BTreeMap::new();
        for (key, value) in doc.iter() {
            if key != "bus" {
                collect_bus_usage(value, &mut refs);
            }
        }
        let Some(buses) = doc.get_mut("bus").and_then(Value::as_object_mut) else {
            return;
        };
        let ids: Vec<String> = buses.keys().cloned().collect();
        for id in ids {
            let Some(used) = refs.get(&id) else {
                buses.remove(&id);
                self.warn(format!(
                    "bus {id}: no emitted BMOPF element references this bus; dropped from the output"
                ));
                continue;
            };
            let Some(bus) = buses.get_mut(&id).and_then(Value::as_object_mut) else {
                continue;
            };
            prune_string_array(
                bus,
                "terminal_names",
                used,
                &mut self.warnings,
                &format!("bus {id}"),
            );
            prune_string_array(
                bus,
                "perfectly_grounded_terminals",
                used,
                &mut self.warnings,
                &format!("bus {id}"),
            );
            if matches!(
                bus.get("perfectly_grounded_terminals"),
                Some(Value::Array(terms)) if terms.is_empty()
            ) {
                bus.remove("perfectly_grounded_terminals");
            }
        }
    }

    /// Lines and switches.
    fn branches(&mut self, net: &DistNetwork, doc: &mut Map<String, Value>) {
        if !net.lines.is_empty() {
            let mut lines = Map::new();
            for l in &net.lines {
                let mut o = Map::new();
                o.insert("length".into(), self.num(l.length, "line length"));
                o.insert("linecode".into(), json!(l.linecode));
                o.insert("bus_from".into(), json!(l.bus_from));
                o.insert("bus_to".into(), json!(l.bus_to));
                o.insert("terminal_map_from".into(), json!(l.terminal_map_from));
                o.insert("terminal_map_to".into(), json!(l.terminal_map_to));
                self.extras_dropped(&l.extras, &format!("line {}", l.name));
                lines.insert(l.name.clone(), Value::Object(o));
            }
            doc.insert("line".into(), Value::Object(lines));
        }
        if !net.switches.is_empty() {
            let mut switches = Map::new();
            for s in &net.switches {
                let mut o = Map::new();
                o.insert("bus_from".into(), json!(s.bus_from));
                o.insert("bus_to".into(), json!(s.bus_to));
                o.insert("terminal_map_from".into(), json!(s.terminal_map_from));
                o.insert("terminal_map_to".into(), json!(s.terminal_map_to));
                o.insert("open_switch".into(), json!(s.open));
                if let Some(i_max) = &s.i_max {
                    o.insert("i_max".into(), self.nums(i_max, "switch i_max"));
                }
                self.extras_dropped(&s.extras, &format!("switch {}", s.name));
                switches.insert(s.name.clone(), Value::Object(o));
            }
            doc.insert("switch".into(), Value::Object(switches));
        }
    }

    /// Loads, generators, shunts, and the voltage sources.
    fn injections(&mut self, net: &DistNetwork, doc: &mut Map<String, Value>) {
        let mut loads = Map::new();
        for l in &net.loads {
            let mut o = Map::new();
            o.insert("configuration".into(), json!(config_str(l.configuration)));
            o.insert("p_nom".into(), self.nums(&l.p_nom, "load p_nom"));
            o.insert("q_nom".into(), self.nums(&l.q_nom, "load q_nom"));
            o.insert("bus".into(), json!(l.bus));
            o.insert("terminal_map".into(), json!(l.terminal_map));
            self.load_voltage_model(&mut o, &l.voltage_model, &format!("load {}", l.name));
            self.extras_dropped(&l.extras, &format!("load {}", l.name));
            loads.insert(l.name.clone(), Value::Object(o));
        }
        let mut gens = Map::new();
        for g in &net.generators {
            gens.insert(g.name.clone(), self.generator(g));
        }
        if !loads.is_empty() {
            doc.insert("load".into(), Value::Object(loads));
        }
        if !gens.is_empty() {
            doc.insert("generator".into(), Value::Object(gens));
        }
        if !net.shunts.is_empty() {
            let mut shunts = Map::new();
            for s in &net.shunts {
                let mut o = Map::new();
                o.insert("bus".into(), json!(s.bus));
                o.insert("terminal_map".into(), json!(s.terminal_map));
                // The schema requires G_1_1 and B_1_1.
                let dim = s.g.len().max(s.b.len()).max(1);
                if s.g.is_empty() && s.b.is_empty() {
                    self.warn(format!(
                        "shunt {}: no admittance matrix; emitted as 1 conductor \
                         zero admittance",
                        s.name
                    ));
                } else if s.g.is_empty() || s.b.is_empty() {
                    self.warn(format!(
                        "shunt {}: G and B sizes disagree; the empty one emitted \
                         as zeros",
                        s.name
                    ));
                }
                self.required_matrix(&mut o, "G", &s.g, dim, &s.name);
                self.required_matrix(&mut o, "B", &s.b, dim, &s.name);
                self.extras_dropped(&s.extras, &format!("shunt {}", s.name));
                shunts.insert(s.name.clone(), Value::Object(o));
            }
            doc.insert("shunt".into(), Value::Object(shunts));
        }
        let mut sources = Map::new();
        if net.sources.is_empty() {
            self.warn("network has no voltage source; BMOPF requires exactly one");
        }
        for (i, vs) in net.sources.iter().enumerate() {
            if i > 0 {
                self.warn(format!(
                    "voltage source {}: the BMOPF formulation expects exactly one source; \
                     this network has {}",
                    vs.name,
                    net.sources.len()
                ));
            }
            let mut o = Map::new();
            o.insert(
                "v_magnitude".into(),
                self.nums(&vs.v_magnitude, "voltage_source v_magnitude"),
            );
            o.insert(
                "v_angle".into(),
                self.nums(&vs.v_angle, "voltage_source v_angle"),
            );
            o.insert("bus".into(), json!(vs.bus));
            o.insert("terminal_map".into(), json!(vs.terminal_map));
            let mut extras = vs.extras.clone();
            if let Some(cost) = extras.remove("cost") {
                o.insert("cost".into(), cost);
            }
            self.extras_dropped(&extras, &format!("voltage source {}", vs.name));
            sources.insert(vs.name.clone(), Value::Object(o));
        }
        doc.insert("voltage_source".into(), Value::Object(sources));
    }

    fn load_voltage_model(
        &mut self,
        o: &mut Map<String, Value>,
        model: &DistLoadVoltageModel,
        what: &str,
    ) {
        match model {
            DistLoadVoltageModel::ConstantPower { v_nom } => {
                o.insert("model".into(), json!("constant_power"));
                if !v_nom.is_empty() {
                    o.insert("v_nom".into(), self.nums(v_nom, &format!("{what} v_nom")));
                }
            }
            DistLoadVoltageModel::ConstantCurrent { v_nom } => {
                o.insert("model".into(), json!("constant_current"));
                o.insert("v_nom".into(), self.nums(v_nom, &format!("{what} v_nom")));
            }
            DistLoadVoltageModel::ConstantImpedance { v_nom } => {
                o.insert("model".into(), json!("constant_impedance"));
                o.insert("v_nom".into(), self.nums(v_nom, &format!("{what} v_nom")));
            }
            DistLoadVoltageModel::Zip {
                v_nom,
                alpha_z,
                alpha_i,
                alpha_p,
                beta_z,
                beta_i,
                beta_p,
            } => {
                o.insert("model".into(), json!("zip"));
                o.insert("v_nom".into(), self.nums(v_nom, &format!("{what} v_nom")));
                o.insert(
                    "alpha_z".into(),
                    self.nums(alpha_z, &format!("{what} alpha_z")),
                );
                o.insert(
                    "alpha_i".into(),
                    self.nums(alpha_i, &format!("{what} alpha_i")),
                );
                o.insert(
                    "alpha_p".into(),
                    self.nums(alpha_p, &format!("{what} alpha_p")),
                );
                o.insert(
                    "beta_z".into(),
                    self.nums(beta_z, &format!("{what} beta_z")),
                );
                o.insert(
                    "beta_i".into(),
                    self.nums(beta_i, &format!("{what} beta_i")),
                );
                o.insert(
                    "beta_p".into(),
                    self.nums(beta_p, &format!("{what} beta_p")),
                );
            }
            DistLoadVoltageModel::Exponential {
                v_nom,
                gamma_p,
                gamma_q,
            } => {
                o.insert("model".into(), json!("exponential"));
                o.insert("v_nom".into(), self.nums(v_nom, &format!("{what} v_nom")));
                o.insert(
                    "gamma_p".into(),
                    self.nums(gamma_p, &format!("{what} gamma_p")),
                );
                o.insert(
                    "gamma_q".into(),
                    self.nums(gamma_q, &format!("{what} gamma_q")),
                );
            }
        }
    }

    fn generator(&mut self, g: &DistGenerator) -> Value {
        let mut o = Map::new();
        // BMOPF generators carry bounds and cost, no dispatch setpoint: a
        // fixed injection becomes pinned bounds. Explicit source bounds win
        // over the setpoint, which then has nowhere to go.
        let what = format!("generator {}", g.name);
        for (key_lo, key_hi, lo, hi, nom) in [
            ("p_min", "p_max", &g.p_min, &g.p_max, &g.p_nom),
            ("q_min", "q_max", &g.q_min, &g.q_max, &g.q_nom),
        ] {
            if lo.is_some() || hi.is_some() {
                // Pinned bounds ARE the setpoint; only a setpoint that
                // differs from the bounds has nowhere to go.
                let pinned = lo.as_deref() == Some(nom) && hi.as_deref() == Some(nom);
                if !nom.is_empty() && !nom.iter().all(|&v| v == 0.0) && !pinned {
                    self.warn(format!(
                        "{what}: explicit {key_lo}/{key_hi} bounds win over the setpoint, \
                         which has no BMOPF field"
                    ));
                }
                if let Some(v) = lo {
                    o.insert(key_lo.into(), self.nums(v, key_lo));
                }
                if let Some(v) = hi {
                    o.insert(key_hi.into(), self.nums(v, key_hi));
                }
            } else if !nom.is_empty() {
                // A fixed injection becomes pinned bounds.
                o.insert(key_lo.into(), self.nums(nom, key_lo));
                o.insert(key_hi.into(), self.nums(nom, key_hi));
            }
        }
        // BMOPF generation cost is per phase conductor; powerio carries a single
        // value, so broadcast the scalar to one entry per phase.
        let n_phase = if g.p_nom.is_empty() {
            g.terminal_map.len().max(1)
        } else {
            g.p_nom.len()
        };
        let cost = g.cost.unwrap_or_else(|| {
            self.warnings.push(format!(
                "{what}: no generation cost in the source; emitted cost 0"
            ));
            0.0
        });
        o.insert(
            "cost".into(),
            self.nums(&vec![cost; n_phase], "generator cost"),
        );
        o.insert("bus".into(), json!(g.bus));
        o.insert("configuration".into(), json!(config_str(g.configuration)));
        o.insert("terminal_map".into(), json!(g.terminal_map));
        if g.configuration == Configuration::Delta {
            self.warn(format!(
                "{what}: the BMOPF formulation covers WYE generators; DELTA emitted as written"
            ));
        }
        self.extras_dropped(&g.extras, &what);
        Value::Object(o)
    }

    /// Transformers keyed by subtype; wye-wye three phase units decompose
    /// into one single_phase entry per phase, the convention the public
    /// example networks use.
    fn transformers(&mut self, net: &DistNetwork) -> Map<String, Value> {
        let mut by_subtype: Map<String, Value> = Map::new();
        let insert = |sub: &str, name: String, v: Value, map: &mut Map<String, Value>| {
            map.entry(sub.to_string())
                .or_insert_with(|| Value::Object(Map::new()))
                .as_object_mut()
                .expect("subtype maps are objects")
                .insert(name, v);
        };
        for t in &net.transformers {
            match classify(t) {
                Kind::SinglePhase => {
                    if t.windings.iter().any(|w| w.conn == WindingConn::Delta) {
                        // An open wye / open delta leg. The single_phase shape
                        // carries the terminals and impedance faithfully, but
                        // has no field for the wye/delta connection, so a
                        // consumer that models the subtype literally reads it
                        // as a wye-wye unit. Flag it; the line to line topology
                        // survives in the terminal map.
                        self.warn(format!(
                            "transformer {}: single phase wye/delta emitted as single_phase; \
                             the wye/delta connection is not encoded in the subtype, only the \
                             line to line terminal map",
                            t.name
                        ));
                    }
                    let v = self.two_winding(t, &t.windings[0], &t.windings[1], 1.0, true, true);
                    insert("single_phase", t.name.clone(), v, &mut by_subtype);
                }
                Kind::SinglePhaseShape(sub) => {
                    let v = self.two_winding(t, &t.windings[0], &t.windings[1], 1.0, true, true);
                    insert(sub, t.name.clone(), v, &mut by_subtype);
                }
                Kind::CenterTap => {
                    let v = self.center_tap(t);
                    insert("center_tap", t.name.clone(), v, &mut by_subtype);
                }
                Kind::WyeDelta => {
                    let v = self.three_phase(t, 0);
                    insert("wye_delta", t.name.clone(), v, &mut by_subtype);
                }
                Kind::DeltaWye => {
                    let v = self.three_phase(t, 1);
                    insert("delta_wye", t.name.clone(), v, &mut by_subtype);
                }
                Kind::WyeWye3 => {
                    for (k, v) in self.decompose_wye_wye(t) {
                        insert("single_phase", k, v, &mut by_subtype);
                    }
                }
                Kind::NWinding => {
                    let v = self.n_winding(t);
                    insert("n_winding", t.name.clone(), v, &mut by_subtype);
                }
                Kind::Unsupported(why) => {
                    self.warn(format!(
                        "transformer {}: {why}; not representable in the four BMOPF \
                         subtypes, dropped from the output",
                        t.name
                    ));
                }
            }
        }
        by_subtype
    }

    /// Shared single_phase / center_tap shape. `to_scale` rescales the to
    /// side ratings (used by the wye-wye decomposition).
    fn two_winding(
        &mut self,
        t: &DistTransformer,
        from: &Winding,
        to: &Winding,
        s_scale: f64,
        emit_no_load: bool,
        warn_extras: bool,
    ) -> Value {
        let s = from.s_rating * s_scale;
        let zb_from = from.v_ref * from.v_ref / s;
        let zb_to = to.v_ref * to.v_ref / s;
        let mut o = Map::new();
        o.insert("bus_from".into(), json!(from.bus));
        o.insert("bus_to".into(), json!(to.bus));
        o.insert("s_rating".into(), self.num(s, "transformer s_rating"));
        o.insert(
            "v_nom_from".into(),
            self.num(from.v_ref, "transformer v_nom_from"),
        );
        o.insert(
            "v_nom_to".into(),
            self.num(to.v_ref, "transformer v_nom_to"),
        );
        o.insert(
            "r_series_from".into(),
            self.num(from.r_pct / 100.0 * zb_from, "transformer r_series_from"),
        );
        o.insert(
            "r_series_to".into(),
            self.num(to.r_pct / 100.0 * zb_to, "transformer r_series_to"),
        );
        // The whole leakage reactance rides on the from side, the
        // convention the public example uses.
        if t.xsc_pct.is_empty() {
            self.warn(format!(
                "transformer {}: xsc_pct is empty; emitted x_series_from=0",
                t.name
            ));
        }
        let xhl = t.xsc_pct.first().copied().unwrap_or(0.0);
        o.insert(
            "x_series_from".into(),
            self.num(xhl / 100.0 * zb_from, "transformer x_series_from"),
        );
        o.insert("x_series_to".into(), json!(0.0));
        o.insert("terminal_map_from".into(), json!(from.terminal_map));
        o.insert("terminal_map_to".into(), json!(to.terminal_map));
        self.transformer_tap_fields(&mut o, t, from);
        if emit_no_load {
            self.transformer_no_load_fields(&mut o, t, from, s);
        }
        if warn_extras {
            self.transformer_extras_dropped(t, &TRANSFORMER_TWO_WINDING_ALLOWED_EXTRAS);
        }
        o.into()
    }

    fn center_tap(&mut self, t: &DistTransformer) -> Value {
        // The split secondary collapses to one to side winding: voltage is
        // the full 240 V across the outer terminals, the center tap is the
        // shared terminal, listed last.
        let from = &t.windings[0];
        let (w2, w3) = (&t.windings[1], &t.windings[2]);
        let common = w2
            .terminal_map
            .iter()
            .find(|term| w3.terminal_map.contains(term))
            .cloned()
            .unwrap_or_default();
        let mut hots: Vec<String> = Vec::new();
        for term in w2.terminal_map.iter().chain(&w3.terminal_map) {
            if *term != common && !hots.contains(term) {
                hots.push(term.clone());
            }
        }
        // Percent resistance does not transfer to the doubled voltage:
        // each winding's impedance base is its own v^2/s (PMD eng2math
        // builds zbase per winding from vnom^2/snom). Convert each half to
        // ohms on its own base, sum the series path, and express the total
        // on the base two_winding gives the combined winding,
        // v_new^2/from.s_rating. Equal ratings make the s factors exactly
        // 1, leaving the plain v^2 weighting. The leakage reactance needs
        // no such move: two_winding applies xsc_pct at the from side,
        // whose base the collapse does not touch.
        let v_new = w2.v_ref + w3.v_ref;
        let r_pct_new = (w2.r_pct * w2.v_ref * w2.v_ref * (from.s_rating / w2.s_rating)
            + w3.r_pct * w3.v_ref * w3.v_ref * (from.s_rating / w3.s_rating))
            / (v_new * v_new);
        let to = Winding {
            bus: w2.bus.clone(),
            terminal_map: {
                let mut m = hots;
                m.push(common);
                m
            },
            conn: WindingConn::Wye,
            v_ref: v_new,
            s_rating: from.s_rating,
            r_pct: r_pct_new,
            tap: 1.0,
        };
        self.warn(format!(
            "transformer {}: center tap secondary collapsed to one winding; the \
             xht/xlt impedance split is not representable and was dropped",
            t.name
        ));
        if w2.s_rating.to_bits() != from.s_rating.to_bits()
            || w3.s_rating.to_bits() != from.s_rating.to_bits()
        {
            self.warn(format!(
                "transformer {}: center tap half winding s_ratings ({}, {}) differ \
                 from the primary's {}; the collapsed winding keeps the primary \
                 rating, the half ratings only survive in the resistance conversion",
                t.name, w2.s_rating, w3.s_rating, from.s_rating
            ));
        }
        self.two_winding(t, from, &to, 1.0, true, true)
    }

    /// `wye_delta` / `delta_wye`: one series impedance in ohms on the wye
    /// side. `wye_idx` names which winding is the wye one.
    fn three_phase(&mut self, t: &DistTransformer, wye_idx: usize) -> Value {
        let from = &t.windings[0];
        let to = &t.windings[1];
        let wye = &t.windings[wye_idx];
        let s = from.s_rating;
        let zb_wye = wye.v_ref * wye.v_ref / s;
        let mut o = Map::new();
        o.insert("bus_from".into(), json!(from.bus));
        o.insert("bus_to".into(), json!(to.bus));
        o.insert("s_rating".into(), self.num(s, "transformer s_rating"));
        o.insert(
            "v_nom_from".into(),
            self.num(from.v_ref, "transformer v_nom_from"),
        );
        o.insert(
            "v_nom_to".into(),
            self.num(to.v_ref, "transformer v_nom_to"),
        );
        o.insert(
            "r_series".into(),
            self.num(
                (from.r_pct + to.r_pct) / 100.0 * zb_wye,
                "transformer r_series",
            ),
        );
        if t.xsc_pct.is_empty() {
            self.warn(format!(
                "transformer {}: xsc_pct is empty; emitted x_series=0",
                t.name
            ));
        }
        let xhl = t.xsc_pct.first().copied().unwrap_or(0.0);
        o.insert(
            "x_series".into(),
            self.num(xhl / 100.0 * zb_wye, "transformer x_series"),
        );
        o.insert("terminal_map_from".into(), json!(from.terminal_map));
        o.insert("terminal_map_to".into(), json!(to.terminal_map));
        self.transformer_tap_fields(&mut o, t, from);
        self.transformer_no_load_fields(&mut o, t, from, s);
        self.transformer_extras_dropped(t, &TRANSFORMER_TWO_WINDING_ALLOWED_EXTRAS);
        o.into()
    }

    fn n_winding(&mut self, t: &DistTransformer) -> Value {
        let s = t.windings.first().map_or(f64::NAN, |w| w.s_rating);
        if t.windings
            .iter()
            .any(|w| w.s_rating.to_bits() != s.to_bits())
        {
            self.warn(format!(
                "transformer {}: n_winding BMOPF carries one s_rating; emitted the first winding rating",
                t.name
            ));
        }
        let mut o = Map::new();
        o.insert("s_rating".into(), self.num(s, "transformer s_rating"));
        let windings: Vec<Value> = t
            .windings
            .iter()
            .map(|w| {
                let mut wj = Map::new();
                wj.insert("bus".into(), json!(w.bus));
                wj.insert("terminal_map".into(), json!(w.terminal_map));
                wj.insert(
                    "v_nom".into(),
                    self.num(n_winding_bmopf_v_nom(w), "transformer winding v_nom"),
                );
                wj.insert(
                    "configuration".into(),
                    json!(match w.conn {
                        WindingConn::Wye => "WYE",
                        WindingConn::Delta => "DELTA",
                    }),
                );
                let zbase = n_winding_base(w, s).unwrap_or(f64::NAN);
                wj.insert(
                    "r_winding".into(),
                    self.num(w.r_pct / 100.0 * zbase, "transformer winding r_winding"),
                );
                Value::Object(wj)
            })
            .collect();
        o.insert("windings".into(), Value::Array(windings));
        let base_z = t
            .windings
            .first()
            .and_then(|w| n_winding_base(w, s))
            .unwrap_or(f64::NAN);
        let mut x_sc = Map::new();
        for (idx, (i, j)) in pair_keys(t.windings.len()).into_iter().enumerate() {
            let x_pct = t.xsc_pct.get(idx).copied().unwrap_or_else(|| {
                self.warn(format!(
                    "transformer {}: missing x_sc for winding pair {}_{}; emitted 0",
                    t.name,
                    i + 1,
                    j + 1
                ));
                0.0
            });
            x_sc.insert(
                format!("{}_{}", i + 1, j + 1),
                self.num(x_pct / 100.0 * base_z, "transformer x_sc"),
            );
        }
        o.insert("x_sc".into(), Value::Object(x_sc));
        if let Some(first) = t.windings.first() {
            self.transformer_no_load_fields(&mut o, t, first, s);
        }
        self.taps_dropped(t);
        self.transformer_extras_dropped(t, &TRANSFORMER_NO_LOAD_ALLOWED_EXTRAS);
        o.into()
    }

    /// A three phase wye-wye unit becomes one single_phase entry per phase
    /// (`name_1`..), each at line to neutral voltage and a third of the
    /// rating. That keeps the impedance base v^2/s, so the percent values
    /// carry over unchanged. The public IEEE13 example records the line to
    /// line voltage on its decomposed units instead; both are self
    /// consistent, they differ in the v_ref convention.
    fn decompose_wye_wye(&mut self, t: &DistTransformer) -> Vec<(String, Value)> {
        let mut out = Vec::new();
        let (from, to) = (&t.windings[0], &t.windings[1]);
        let sqrt3 = 3f64.sqrt();
        for k in 0..t.phases {
            let per = |w: &Winding| {
                let neutral = w.terminal_map.last().cloned().unwrap_or_default();
                Winding {
                    bus: w.bus.clone(),
                    terminal_map: vec![w.terminal_map[k].clone(), neutral],
                    conn: WindingConn::Wye,
                    v_ref: w.v_ref / sqrt3,
                    s_rating: w.s_rating / 3.0,
                    r_pct: w.r_pct,
                    tap: w.tap,
                }
            };
            let f = per(from);
            let to_1 = per(to);
            let mut t1 = t.clone();
            t1.windings = vec![f.clone(), to_1.clone()];
            let v = self.two_winding(&t1, &f, &to_1, 1.0, false, false);
            out.push((format!("{}_{}", t.name, k + 1), v));
        }
        self.warn(format!(
            "transformer {}: three phase wye-wye decomposed into {} single_phase units",
            t.name, t.phases
        ));
        self.transformer_extras_dropped(t, &TRANSFORMER_TWO_WINDING_ALLOWED_EXTRAS);
        out
    }

    fn taps_dropped(&mut self, t: &DistTransformer) {
        for w in &t.windings {
            if (w.tap - 1.0).abs() > 1e-12 {
                self.warn(format!(
                    "transformer {}: off nominal tap {} has no BMOPF field; dropped",
                    t.name, w.tap
                ));
            }
        }
    }

    fn transformer_tap_fields(
        &mut self,
        o: &mut Map<String, Value>,
        t: &DistTransformer,
        from: &Winding,
    ) {
        if (from.tap - 1.0).abs() > 1e-12 || t.extras.contains_key("tap") {
            o.insert("tap".into(), self.num(from.tap, "transformer tap"));
        }
        for key in ["tap_min", "tap_max"] {
            if let Some(v) = extras_number(&t.extras, key) {
                o.insert(key.into(), self.num(v, &format!("transformer {key}")));
            }
        }
        for w in t.windings.iter().skip(1) {
            if (w.tap - 1.0).abs() > 1e-12 {
                self.warn(format!(
                    "transformer {}: non-from-side tap {} has no BMOPF field; dropped",
                    t.name, w.tap
                ));
            }
        }
    }

    fn transformer_no_load_fields(
        &mut self,
        o: &mut Map<String, Value>,
        t: &DistTransformer,
        from: &Winding,
        s: f64,
    ) {
        if let Some(v) = t.extras.get("g_no_load") {
            o.insert("g_no_load".into(), v.clone());
        } else if let Some(loss_pct) = extras_number(&t.extras, "%noloadloss") {
            if self.is_phase_to_phase_single_phase(from) {
                self.warn(format!(
                    "transformer {}: phase-to-phase %noloadloss cannot be represented as a BMOPF no-load shunt; dropped",
                    t.name
                ));
            } else {
                let v_stamp = no_load_voltage_base(from);
                if s.is_finite() && s > 0.0 && v_stamp.is_finite() && v_stamp > 0.0 {
                    let y_base = s / (v_stamp * v_stamp);
                    o.insert(
                        "g_no_load".into(),
                        self.num(loss_pct / 100.0 * y_base, "transformer g_no_load"),
                    );
                } else {
                    self.warn(format!(
                        "transformer {}: %noloadloss cannot be converted without a positive s_rating and v_nom_from",
                        t.name
                    ));
                }
            }
        }

        if let Some(v) = t.extras.get("b_no_load") {
            o.insert("b_no_load".into(), v.clone());
        } else if let Some(imag_pct) = extras_number(&t.extras, "%imag") {
            if self.is_phase_to_phase_single_phase(from) {
                self.warn(format!(
                    "transformer {}: phase-to-phase %imag cannot be represented as a BMOPF no-load shunt; dropped",
                    t.name
                ));
            } else {
                let v_stamp = no_load_voltage_base(from);
                if s.is_finite() && s > 0.0 && v_stamp.is_finite() && v_stamp > 0.0 {
                    let y_base = s / (v_stamp * v_stamp);
                    o.insert(
                        "b_no_load".into(),
                        self.num(imag_pct / 100.0 * y_base, "transformer b_no_load"),
                    );
                } else {
                    self.warn(format!(
                        "transformer {}: %imag cannot be converted without a positive s_rating and v_nom_from",
                        t.name
                    ));
                }
            }
        } else if !self.is_phase_to_phase_single_phase(from)
            && extras_number(&t.extras, "%noloadloss").is_some()
        {
            o.insert("b_no_load".into(), json!(0.0));
        }
    }

    fn is_phase_to_phase_single_phase(&self, winding: &Winding) -> bool {
        n_winding_phase_count(winding) == 1
            && !self
                .grounded
                .get(&winding.bus.to_ascii_lowercase())
                .is_some_and(|g| winding.terminal_map.iter().any(|t| g.contains(t)))
    }

    fn transformer_extras_dropped(&mut self, t: &DistTransformer, allowed: &[&str]) {
        for key in t.extras.keys() {
            if key == "bmopf_subtype" || key == "tap" || allowed.contains(&key.as_str()) {
                continue;
            }
            self.warn(format!(
                "transformer {}: `{key}` has no place in the BMOPF schema; dropped from the output",
                t.name
            ));
        }
    }

    /// Emits a matrix whose `_1_1` entry the schema requires; an empty one
    /// becomes `dim` by `dim` zeros so the required key exists.
    fn required_matrix(
        &mut self,
        o: &mut Map<String, Value>,
        prefix: &str,
        m: &Mat,
        dim: usize,
        name: &str,
    ) {
        if m.is_empty() {
            self.flat_matrix(o, prefix, &vec![vec![0.0; dim]; dim], name);
        } else {
            self.flat_matrix(o, prefix, m, name);
        }
    }

    fn flat_matrix(&mut self, o: &mut Map<String, Value>, prefix: &str, m: &Mat, name: &str) {
        for (i, row) in m.iter().enumerate() {
            for (j, &v) in row.iter().enumerate() {
                o.insert(
                    format!("{prefix}_{}_{}", i + 1, j + 1),
                    self.num(v, &format!("{name} {prefix}")),
                );
            }
        }
    }
}

fn collect_bus_usage(value: &Value, refs: &mut BTreeMap<String, BTreeSet<String>>) {
    match value {
        Value::Object(o) => {
            add_bus_usage(o, refs, "bus", "terminal_map");
            add_bus_usage(o, refs, "bus_from", "terminal_map_from");
            add_bus_usage(o, refs, "bus_to", "terminal_map_to");
            for value in o.values() {
                collect_bus_usage(value, refs);
            }
        }
        Value::Array(values) => {
            for value in values {
                collect_bus_usage(value, refs);
            }
        }
        _ => {}
    }
}

fn add_bus_usage(
    o: &Map<String, Value>,
    refs: &mut BTreeMap<String, BTreeSet<String>>,
    bus_key: &str,
    map_key: &str,
) {
    let Some(id) = o.get(bus_key).and_then(Value::as_str) else {
        return;
    };
    let entry = refs.entry(id.to_string()).or_default();
    if let Some(terms) = o.get(map_key).and_then(Value::as_array) {
        entry.extend(terms.iter().filter_map(Value::as_str).map(str::to_string));
    }
}

fn prune_string_array(
    o: &mut Map<String, Value>,
    key: &str,
    used: &BTreeSet<String>,
    warnings: &mut Vec<String>,
    what: &str,
) {
    let Some(Value::Array(values)) = o.get_mut(key) else {
        return;
    };
    let old = std::mem::take(values);
    let mut kept = Vec::new();
    let mut dropped = Vec::new();
    for value in old {
        if value.as_str().is_some_and(|s| used.contains(s)) {
            kept.push(value);
        } else {
            dropped.push(value);
        }
    }
    if !dropped.is_empty() {
        let names: Vec<String> = dropped
            .iter()
            .filter_map(Value::as_str)
            .map(str::to_string)
            .collect();
        warnings.push(format!(
            "{what}: `{key}` entries {names:?} are not referenced by emitted BMOPF elements; dropped from the output"
        ));
    }
    *values = kept;
}

enum Kind {
    SinglePhase,
    /// Two windings already in the shared single_phase/center_tap shape,
    /// emitted under the named subtype.
    SinglePhaseShape(&'static str),
    CenterTap,
    WyeDelta,
    DeltaWye,
    WyeWye3,
    NWinding,
    Unsupported(String),
}

fn classify(t: &DistTransformer) -> Kind {
    // A network read from BMOPF records its subtype; trust it so writing
    // back reproduces the grouping (center tap reads as two windings).
    // An unknown or shape mismatched subtype falls through to the shape
    // based classification below.
    if let Some(sub) = t.extras.get("bmopf_subtype").and_then(|v| v.as_str()) {
        if t.windings.len() == 2 {
            match sub {
                "single_phase" => return Kind::SinglePhase,
                "center_tap" => return Kind::SinglePhaseShape("center_tap"),
                "wye_delta" => return Kind::WyeDelta,
                "delta_wye" => return Kind::DeltaWye,
                _ => {}
            }
        }
        if sub == "n_winding" && t.windings.len() >= 2 {
            return Kind::NWinding;
        }
    }
    let conns: Vec<WindingConn> = t.windings.iter().map(|w| w.conn).collect();
    match (t.phases, conns.as_slice()) {
        // single_phase covers the plain 1-phase wye-wye unit and both open
        // wye / open delta leg orientations (one delta winding wired line to
        // line). The single_phase shape holds the delta side: it carries two
        // phase terminals, no conn discriminator, and its line to line v_ref
        // makes the per winding impedance base v^2/s already right. The
        // pattern reads as the three pairs wye-wye, delta-wye, wye-delta.
        (
            1,
            [WindingConn::Wye | WindingConn::Delta, WindingConn::Wye]
            | [WindingConn::Wye, WindingConn::Delta],
        ) => Kind::SinglePhase,
        (1, [WindingConn::Wye, WindingConn::Wye, WindingConn::Wye]) => Kind::CenterTap,
        (3, [WindingConn::Wye, WindingConn::Delta]) => Kind::WyeDelta,
        (3, [WindingConn::Delta, WindingConn::Wye]) => Kind::DeltaWye,
        // The decomposition indexes terminal_map[phase] and takes the last
        // entry as the neutral; anything else is not safely decomposable.
        (3, [WindingConn::Wye, WindingConn::Wye])
            if t.windings
                .iter()
                .all(|w| w.terminal_map.len() == t.phases + 1) =>
        {
            Kind::WyeWye3
        }
        (3, [WindingConn::Wye, WindingConn::Wye]) => Kind::Unsupported(
            "three phase wye-wye whose terminal maps do not list each phase plus a neutral".into(),
        ),
        (_, _) if t.windings.len() >= 3 => Kind::NWinding,
        _ => Kind::Unsupported(format!(
            "{} phase with {} windings ({:?})",
            t.phases,
            t.windings.len(),
            conns
        )),
    }
}

fn raw_bmopf_value(u: &crate::model::UntypedObject) -> Option<Value> {
    let (_, text) = u.props.first()?;
    serde_json::from_str(text).ok()
}

fn extras_number(extras: &crate::model::Extras, key: &str) -> Option<f64> {
    let v = extras.get(key)?;
    v.as_f64()
        .or_else(|| v.as_i64().map(|v| v as f64))
        .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        .filter(|v| v.is_finite())
}

fn n_winding_phase_count(w: &Winding) -> usize {
    crate::model::n_winding_phase_count(w.conn, &w.terminal_map)
}

fn n_winding_bmopf_v_nom(w: &Winding) -> f64 {
    if w.conn == WindingConn::Wye && n_winding_phase_count(w) >= 2 {
        w.v_ref / 3f64.sqrt()
    } else {
        w.v_ref
    }
}

fn n_winding_base(w: &Winding, s: f64) -> Option<f64> {
    n_winding_impedance_base(n_winding_phase_count(w), n_winding_bmopf_v_nom(w), s)
}

fn no_load_voltage_base(from: &Winding) -> f64 {
    let phases = match from.conn {
        WindingConn::Wye => from.terminal_map.len().saturating_sub(1),
        WindingConn::Delta => from.terminal_map.len(),
    };
    if phases >= 3 {
        from.v_ref / 3f64.sqrt()
    } else {
        from.v_ref
    }
}

fn config_str(c: Configuration) -> &'static str {
    match c {
        Configuration::Wye => "WYE",
        Configuration::Delta => "DELTA",
        Configuration::SinglePhase => "SINGLE_PHASE",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bmopf::parse_bmopf_str;
    use crate::model::DistLoadVoltageModel;

    #[test]
    fn load_voltage_models_round_trip_through_bmopf() {
        let text = r#"{
            "bus": {
                "b1": {"terminal_names": ["1", "2", "3", "4"], "perfectly_grounded_terminals": ["4"]}
            },
            "voltage_source": {
                "source": {
                    "bus": "b1", "terminal_map": ["1", "2", "3", "4"],
                    "v_magnitude": [7200.0, 7200.0, 7200.0, 0.0],
                    "v_angle": [0.0, -120.0, 120.0, 0.0]
                }
            },
            "load": {
                "zip": {
                    "bus": "b1", "terminal_map": ["1", "2", "3", "4"],
                    "configuration": "WYE", "p_nom": [1.0, 2.0, 3.0], "q_nom": [0.1, 0.2, 0.3],
                    "model": "zip", "v_nom": [7200.0, 7200.0, 7200.0],
                    "alpha_z": [0.2, 0.2, 0.2], "alpha_i": [0.3, 0.3, 0.3], "alpha_p": [0.5, 0.5, 0.5],
                    "beta_z": [0.1, 0.1, 0.1], "beta_i": [0.4, 0.4, 0.4], "beta_p": [0.5, 0.5, 0.5]
                },
                "exp": {
                    "bus": "b1", "terminal_map": ["1", "2", "3", "4"],
                    "configuration": "WYE", "p_nom": [1.0, 1.0, 1.0], "q_nom": [0.0, 0.0, 0.0],
                    "model": "exponential", "v_nom": [7200.0, 7200.0, 7200.0],
                    "gamma_p": [1.2, 1.2, 1.2], "gamma_q": [2.1, 2.1, 2.1]
                }
            }
        }"#;
        let net = parse_bmopf_str(text).unwrap();
        let zip = net.loads.iter().find(|l| l.name == "zip").unwrap();
        let exp = net.loads.iter().find(|l| l.name == "exp").unwrap();
        assert!(matches!(
            &zip.voltage_model,
            DistLoadVoltageModel::Zip { alpha_z, .. } if alpha_z == &vec![0.2, 0.2, 0.2]
        ));
        assert!(matches!(
            &exp.voltage_model,
            DistLoadVoltageModel::Exponential { gamma_q, .. } if gamma_q == &vec![2.1, 2.1, 2.1]
        ));

        let out = write_bmopf_json(&net);
        assert!(out.warnings.is_empty(), "{:?}", out.warnings);
        let v: Value = serde_json::from_str(&out.text).unwrap();
        assert_eq!(
            v["load"]["zip"]["alpha_i"],
            serde_json::json!([0.3, 0.3, 0.3])
        );
        assert_eq!(
            v["load"]["exp"]["gamma_p"],
            serde_json::json!([1.2, 1.2, 1.2])
        );
    }
}