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
/// IEEE Common Data Format (CDF) parser.
///
/// Parses the fixed-column IEEE CDF text format used by classic power systems
/// test cases (IEEE 14, 30, 57, 118, 300-bus systems from the original data files).
///
/// # Format specification
/// The IEEE CDF format uses fixed-column widths:
/// - Title card: columns 2-9 (date), 31-37 (MVA base), 39-42 (year)
/// - Bus data card: 16 columns of fixed-width fields
/// - Branch data card: 21 columns of fixed-width fields
///
/// # Reference
/// IEEE Committee Report, "Common Format for Exchange of Solved Load Flow Data",
/// IEEE Trans. Power Apparatus & Systems, PAS-92(6), 1973.
use crate::error::{OxiGridError, Result};
use crate::network::branch::Branch;
use crate::network::bus::{Bus, BusType};
use crate::network::topology::{Generator, PowerNetwork};
use crate::units::{Power, ReactivePower, Voltage};

/// Parse an IEEE CDF file and return a PowerNetwork.
pub fn parse_ieee_cdf_file(path: &str) -> Result<PowerNetwork> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| OxiGridError::ParseError(format!("Failed to read {path}: {e}")))?;
    parse_ieee_cdf_string(&content)
}

/// Parse IEEE CDF format from a string.
pub fn parse_ieee_cdf_string(content: &str) -> Result<PowerNetwork> {
    let mut lines = content.lines().peekable();

    // Title card: MVA base in columns 31-37 (1-indexed)
    let title = lines
        .next()
        .ok_or_else(|| OxiGridError::ParseError("Empty file".into()))?;
    let base_mva = parse_base_mva_cdf(title)?;

    let mut buses: Vec<Bus> = Vec::new();
    let mut branches: Vec<Branch> = Vec::new();
    let mut generators: Vec<Generator> = Vec::new();

    let mut section = CdfSection::None;

    for line in lines {
        // Section markers
        if line.starts_with("BUS DATA FOLLOWS") {
            section = CdfSection::Bus;
            continue;
        }
        if line.starts_with("BRANCH DATA FOLLOWS") {
            section = CdfSection::Branch;
            continue;
        }
        if line.starts_with("LOSS ZONES") || line.starts_with("INTERCHANGE DATA") {
            section = CdfSection::Skip;
            continue;
        }
        if line.starts_with("TIE LINES") || line.starts_with("END OF DATA") {
            break;
        }
        // Skip separator lines (-999... pattern)
        if line.trim_start().starts_with("-999") || line.trim().is_empty() {
            section = CdfSection::None;
            continue;
        }

        match section {
            CdfSection::Bus => {
                if let Some(b) = parse_cdf_bus(line)? {
                    // Create generator record for PV/Slack buses with generation
                    let pg = parse_f64_col(line, 59, 67).unwrap_or(0.0);
                    let qg = parse_f64_col(line, 67, 75).unwrap_or(0.0);
                    let qmax = parse_f64_col(line, 90, 98).unwrap_or(9999.0);
                    let qmin = parse_f64_col(line, 98, 106).unwrap_or(-9999.0);
                    if pg != 0.0 || b.bus_type != BusType::PQ {
                        generators.push(Generator {
                            bus_id: b.id,
                            pg,
                            qg,
                            qmax,
                            qmin,
                            vg: b.vm,
                            mbase: base_mva,
                            status: true,
                            pmax: pg.max(0.0) * 2.0 + 100.0,
                            pmin: 0.0,
                        });
                    }
                    buses.push(b);
                }
            }
            CdfSection::Branch => {
                if let Some(br) = parse_cdf_branch(line)? {
                    branches.push(br);
                }
            }
            CdfSection::Skip | CdfSection::None => {}
        }
    }

    if buses.is_empty() {
        return Err(OxiGridError::ParseError(
            "No bus data found in IEEE CDF file".into(),
        ));
    }

    let mut network = PowerNetwork::new(base_mva);
    network.buses = buses;
    network.branches = branches;
    network.generators = generators;
    network.validate()?;
    Ok(network)
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum CdfSection {
    None,
    Bus,
    Branch,
    Skip,
}

/// Parse base MVA from the CDF title card (columns 31-37, 0-indexed 30-36).
fn parse_base_mva_cdf(title: &str) -> Result<f64> {
    // MVA base is in columns 31-37 (1-indexed) = chars 30..37
    let chars: Vec<char> = title.chars().collect();
    let end = chars.len().min(37);
    let start = end.min(30);
    if start >= end {
        // Try parsing from anywhere in the title if short line
        for word in title.split_whitespace() {
            if let Ok(v) = word.parse::<f64>() {
                if v > 0.0 {
                    return Ok(v);
                }
            }
        }
        return Ok(100.0); // default
    }
    let mva_str: String = chars[start..end].iter().collect();
    mva_str
        .trim()
        .parse::<f64>()
        .map_err(|_| OxiGridError::ParseError(format!("Cannot parse MVA base from: '{mva_str}'")))
}

/// Parse a CDF bus data card (fixed-column format).
///
/// Field layout (1-indexed columns):
///  1-4   Bus number
///  6-17  Bus name
///  19    Load flow area
///  20-23 Loss zone
///  25    Type (0=PQ, 1=PQ_gen, 2=PV, 3=Slack)
///  27-33 Final voltage (p.u.)
///  34-40 Final angle (degrees)
///  41-49 Load MW
///  50-58 Load MVAr
///  59-67 Generation MW
///  68-75 Generation MVAr
///  77-83 Base kV
///  ...
fn parse_cdf_bus(line: &str) -> Result<Option<Bus>> {
    let line = line.trim_end();
    if line.len() < 25 {
        return Ok(None);
    }

    let bus_id = parse_i32_col(line, 0, 4)? as usize;
    let name = if line.len() >= 17 {
        line[5..17.min(line.len())].trim().to_string()
    } else {
        format!("Bus {bus_id}")
    };

    let bus_type_code = parse_i32_col(line, 24, 26).unwrap_or(0);
    let bus_type = match bus_type_code {
        3 => BusType::Slack,
        2 => BusType::PV,
        _ => BusType::PQ,
    };

    let vm = parse_f64_col(line, 27, 33).unwrap_or(1.0);
    let va_deg = parse_f64_col(line, 33, 40).unwrap_or(0.0);
    let pd = parse_f64_col(line, 40, 49).unwrap_or(0.0);
    let qd = parse_f64_col(line, 49, 58).unwrap_or(0.0);
    let base_kv = parse_f64_col(line, 76, 83).unwrap_or(0.0);
    let gs = parse_f64_col(line, 106, 114).unwrap_or(0.0);
    let bs = parse_f64_col(line, 114, 122).unwrap_or(0.0);

    let bus = Bus {
        id: bus_id,
        name,
        bus_type,
        base_kv: Voltage(base_kv),
        vm: vm.max(0.5),
        va: va_deg.to_radians(),
        pd: Power(pd),
        qd: ReactivePower(qd),
        gs,
        bs,
        zone: None,
    };
    Ok(Some(bus))
}

/// Parse a CDF branch data card.
///
/// Field layout (1-indexed):
///  1-4   Tap bus number
///  6-9   Z bus number
///  ...   resistance, reactance, line charging, tap ratio, angle
fn parse_cdf_branch(line: &str) -> Result<Option<Branch>> {
    let line = line.trim_end();
    if line.len() < 40 {
        return Ok(None);
    }

    let from_bus = parse_i32_col(line, 0, 4)? as usize;
    let to_bus = parse_i32_col(line, 5, 9)? as usize;

    if from_bus == 0 || to_bus == 0 {
        return Ok(None);
    }

    let r = parse_f64_col(line, 19, 29).unwrap_or(0.0);
    let x = parse_f64_col(line, 29, 40).unwrap_or(0.001);
    let b = parse_f64_col(line, 40, 50).unwrap_or(0.0);
    let rate_a = parse_f64_col(line, 50, 56).unwrap_or(0.0);
    let rate_b = parse_f64_col(line, 56, 62).unwrap_or(0.0);
    let rate_c = parse_f64_col(line, 62, 68).unwrap_or(0.0);
    let tap = parse_f64_col(line, 76, 83).unwrap_or(0.0);
    let shift = parse_f64_col(line, 83, 90).unwrap_or(0.0);
    let status_code = parse_i32_col(line, 18, 19).unwrap_or(1);

    Ok(Some(Branch {
        from_bus,
        to_bus,
        r,
        x: if x.abs() < 1e-8 { 0.001 } else { x },
        b,
        rate_a,
        rate_b,
        rate_c,
        tap,
        shift,
        status: status_code != 0,
    }))
}

/// Extract a substring by 0-indexed column range and parse as f64.
fn parse_f64_col(line: &str, start: usize, end: usize) -> Option<f64> {
    let bytes = line.as_bytes();
    let end = end.min(bytes.len());
    if start >= end {
        return None;
    }
    // Safety: we only work with ASCII
    let s = std::str::from_utf8(&bytes[start..end]).ok()?.trim();
    if s.is_empty() {
        None
    } else {
        s.parse::<f64>().ok()
    }
}

/// Extract a substring by 0-indexed column range and parse as i32.
fn parse_i32_col(line: &str, start: usize, end: usize) -> Result<i32> {
    let bytes = line.as_bytes();
    let end = end.min(bytes.len());
    if start >= end {
        return Ok(0);
    }
    let s = std::str::from_utf8(&bytes[start..end])
        .map_err(|_| OxiGridError::ParseError("UTF-8 error".into()))?
        .trim();
    if s.is_empty() {
        return Ok(0);
    }
    s.parse::<i32>()
        .map_err(|_| OxiGridError::ParseError(format!("Cannot parse int from '{s}'")))
}

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

    const SAMPLE_CDF: &str = "\
 08/19/93 UW ARCHIVE           100.0  1962 W IEEE 14 Bus Test Case
BUS DATA FOLLOWS                            G  B
   1 Bus 1     HV  1  1  3 1.060  0.000  0.000  0.000  232.4  -16.9   132.0  1  1.060    0.000    0.000    0.000
   2 Bus 2     HV  1  1  2 1.045 -4.986  21.70   12.70  40.0   42.4   132.0  1  1.045    0.000  300.000 -300.000
   3 Bus 3     HV  1  1  0 1.010 -12.725  94.20   19.00   0.0    0.0   132.0  1  0.000    0.000    0.000    0.000
-999
BRANCH DATA FOLLOWS                         R          X          B    CONT  NAME  RATE1  RATE2  RATE3
   1   2  1  1 1  1 .01938    .05917     .05280
   1   3  1  1 1  1 .05403    .22304     .04920
-999
END OF DATA
";

    #[test]
    fn test_parse_cdf_sample() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).unwrap();
        assert_eq!(net.base_mva, 100.0);
        assert_eq!(net.bus_count(), 3);
        assert_eq!(net.branch_count(), 2);
    }

    #[test]
    fn test_parse_cdf_bus_types() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).unwrap();
        let b1 = &net.buses[0];
        let b2 = &net.buses[1];
        let b3 = &net.buses[2];
        assert_eq!(b1.bus_type, BusType::Slack, "Bus 1 should be Slack");
        assert_eq!(b2.bus_type, BusType::PV, "Bus 2 should be PV");
        assert_eq!(b3.bus_type, BusType::PQ, "Bus 3 should be PQ");
    }

    #[test]
    fn test_parse_cdf_branch_impedance() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).unwrap();
        let br = &net.branches[0]; // bus 1→2
        assert!(br.r > 0.0, "Branch resistance should be positive");
        assert!(br.x > 0.0, "Branch reactance should be positive");
    }

    #[test]
    fn test_parse_f64_col() {
        let line = "   1.060  0.000  ";
        let v = parse_f64_col(line, 3, 8);
        assert!(v.is_some());
        assert!((v.unwrap() - 1.060).abs() < 1e-4);
    }

    #[test]
    fn test_base_mva_parse() {
        let title = " 08/19/93 UW ARCHIVE           100.0  1962";
        let mva = parse_base_mva_cdf(title).unwrap();
        assert!((mva - 100.0).abs() < 1e-6);
    }

    #[test]
    fn test_empty_input_returns_error() {
        let result = parse_ieee_cdf_string("");
        assert!(result.is_err(), "empty input should return Err");
    }

    #[test]
    fn test_no_bus_section_returns_error() {
        let input =
            " 08/19/93 UW ARCHIVE           100.0  1962 W IEEE 14 Bus Test Case\nEND OF DATA\n";
        let result = parse_ieee_cdf_string(input);
        assert!(
            result.is_err(),
            "input without BUS DATA FOLLOWS section should return Err"
        );
    }

    #[test]
    fn test_bus_vm_values() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).expect("SAMPLE_CDF should parse successfully");
        let b1 = &net.buses[0];
        let b2 = &net.buses[1];
        let b3 = &net.buses[2];
        assert!(
            (b1.vm - 1.060).abs() < 1e-3,
            "Bus 1 vm should be ≈1.060, got {}",
            b1.vm
        );
        assert!(
            (b2.vm - 1.045).abs() < 1e-3,
            "Bus 2 vm should be ≈1.045, got {}",
            b2.vm
        );
        assert!(
            (b3.vm - 1.010).abs() < 1e-3,
            "Bus 3 vm should be ≈1.010, got {}",
            b3.vm
        );
    }

    #[test]
    fn test_bus_va_angle_bus2() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).expect("SAMPLE_CDF should parse successfully");
        let b2 = &net.buses[1];
        assert!(
            b2.va < 0.0,
            "Bus 2 va should be negative (was -4.986°), got {} rad",
            b2.va
        );
    }

    #[test]
    fn test_bus_3_load_pd() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).expect("SAMPLE_CDF should parse successfully");
        let b3 = &net.buses[2];
        assert!(
            (b3.pd.0 - 94.2).abs() < 1e-3,
            "Bus 3 pd should be ≈94.2 MW, got {}",
            b3.pd.0
        );
    }

    #[test]
    fn test_generators_created_for_pv_and_slack() {
        let net = parse_ieee_cdf_string(SAMPLE_CDF).expect("SAMPLE_CDF should parse successfully");
        assert!(
            net.generators.len() >= 2,
            "expected at least 2 generators (PV + Slack), got {}",
            net.generators.len()
        );
    }

    #[test]
    fn test_parse_f64_col_whitespace_returns_none() {
        let result = parse_f64_col("     ", 0, 5);
        assert!(result.is_none(), "all-whitespace slice should return None");
    }
}