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
//! Restoration sequencing: parallel path discovery, load-block ordering,
//! and SAIDI/SAIFI/ENS reliability metrics.

use crate::network::topology::PowerNetwork;
use crate::optimize::restoration::black_start::{EnergizationPath, LoadBlock, RestorationPlan};
use std::collections::{HashSet, VecDeque};

// ─────────────────────────────────────────────────────────────────────────────
// RestorationSequencer
// ─────────────────────────────────────────────────────────────────────────────

/// Computes parallel energisation opportunities and orders load blocks for
/// optimal restoration priority.
pub struct RestorationSequencer {
    /// Maximum number of simultaneously active energisation paths.
    pub max_parallel_paths: usize,
}

impl RestorationSequencer {
    /// Construct a sequencer allowing up to `max_parallel_paths` concurrent paths.
    pub fn new(max_parallel_paths: usize) -> Self {
        Self {
            max_parallel_paths: max_parallel_paths.max(1),
        }
    }

    // ── Public API ──────────────────────────────────────────────────────────

    /// Discover groups of energisation paths that can be executed in parallel.
    ///
    /// Two paths are *electrically independent* when they share no common
    /// intermediate bus — i.e., they form disjoint sub-trees rooted at
    /// distinct black-start buses.
    ///
    /// Returns a `Vec` of groups; each group is a `Vec<EnergizationPath>` that
    /// can be energised simultaneously.
    pub fn compute_parallel_paths(
        &self,
        network: &PowerNetwork,
        black_start_buses: &[usize],
    ) -> Vec<Vec<EnergizationPath>> {
        // One parallel group per black-start bus (radial sub-tree BFS)
        let mut groups: Vec<Vec<EnergizationPath>> = Vec::new();

        for &bs_bus in black_start_buses {
            let subtree_paths = self.bfs_subtree(network, bs_bus);
            if !subtree_paths.is_empty() {
                groups.push(subtree_paths);
            }
        }

        // Limit group count to max_parallel_paths
        groups.truncate(self.max_parallel_paths);
        groups
    }

    /// Order load blocks for sequential pickup given the available net headroom.
    ///
    /// Strategy:
    /// 1. Non-deferrable blocks (priority 1) first.
    /// 2. Among blocks at the same priority level, choose those whose
    ///    cold-load pickup demand fits within the headroom first.
    /// 3. Ties broken by block_id for determinism.
    ///
    /// Returns a `Vec` of block IDs in the recommended restoration order.
    pub fn order_load_blocks(
        &self,
        blocks: &[LoadBlock],
        available_mw: f64,
        reserve_mw: f64,
    ) -> Vec<usize> {
        let headroom = (available_mw - reserve_mw).max(0.0);
        let mut remaining_headroom = headroom;
        let mut ordered: Vec<usize> = Vec::new();

        // Group by priority, then within each group pick greedily by demand fit
        let mut priorities: Vec<usize> = blocks.iter().map(|b| b.priority).collect();
        priorities.sort_unstable();
        priorities.dedup();

        for prio in priorities {
            let mut tier: Vec<&LoadBlock> = blocks.iter().filter(|b| b.priority == prio).collect();
            // Sort within tier: non-deferrable first, then by ascending demand
            tier.sort_by(|a, b| {
                let a_nd = !a.can_defer as u8;
                let b_nd = !b.can_defer as u8;
                b_nd.cmp(&a_nd)
                    .then_with(|| {
                        a.base_demand_mw
                            .partial_cmp(&b.base_demand_mw)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .then(a.block_id.cmp(&b.block_id))
            });

            for block in tier {
                // Cold-load pickup demand at t=0 after restore
                let clp = block.base_demand_mw * block.cold_load_pickup_factor;
                if clp <= remaining_headroom || !block.can_defer {
                    ordered.push(block.block_id);
                    remaining_headroom = (remaining_headroom - clp).max(0.0);
                }
            }
        }

        ordered
    }

    // ── Private helpers ─────────────────────────────────────────────────────

    /// BFS from `root_bus` to enumerate all reachable energisation paths in the
    /// network sub-tree (one path per reachable bus).
    fn bfs_subtree(&self, network: &PowerNetwork, root_bus: usize) -> Vec<EnergizationPath> {
        let mut paths: Vec<EnergizationPath> = Vec::new();
        let mut visited: HashSet<usize> = HashSet::new();
        // (current_bus, path_so_far, accumulated_km)
        let mut queue: VecDeque<(usize, Vec<usize>, f64)> = VecDeque::new();

        visited.insert(root_bus);
        queue.push_back((root_bus, Vec::new(), 0.0));

        while let Some((current, branch_path, dist)) = queue.pop_front() {
            for (bi, branch) in network.branches.iter().enumerate() {
                if !branch.status {
                    continue;
                }
                let neighbor = if branch.from_bus == current {
                    branch.to_bus
                } else if branch.to_bus == current {
                    branch.from_bus
                } else {
                    continue;
                };

                if visited.contains(&neighbor) {
                    continue;
                }
                visited.insert(neighbor);

                let z_pu = (branch.r * branch.r + branch.x * branch.x).sqrt();
                let length_km = (z_pu / 0.3).max(0.5);
                let new_dist = dist + length_km;
                let mut new_path = branch_path.clone();
                new_path.push(bi);

                let charging: f64 = new_path
                    .iter()
                    .map(|&idx| network.branches[idx].b * 0.5 * 100.0)
                    .sum();

                paths.push(EnergizationPath {
                    from_bus: root_bus,
                    to_bus: neighbor,
                    branch_sequence: new_path.clone(),
                    total_length_km: new_dist,
                    charging_current_mvar: charging,
                    can_energize_at_t: 0.0,
                });

                queue.push_back((neighbor, new_path, new_dist));
            }
        }
        paths
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// RestorationMetrics: SAIDI / SAIFI / ENS
// ─────────────────────────────────────────────────────────────────────────────

/// Tracks reliability impact of a restoration event.
pub struct RestorationMetrics {
    /// Total number of customers affected by the blackout.
    pub total_customers: usize,
    /// Clock time at which the blackout began \[min\].
    pub outage_start_min: f64,
}

impl RestorationMetrics {
    /// Construct metrics for an event that started at `outage_start_min` and
    /// affects `total_customers` customers.
    pub fn new(total_customers: usize, outage_start_min: f64) -> Self {
        Self {
            total_customers,
            outage_start_min,
        }
    }

    /// System Average Interruption Duration Index \[customer-minutes / customer\].
    ///
    /// ```text
    /// SAIDI = Σ_i (customers_i × interruption_duration_i) / total_customers
    /// ```
    ///
    /// `customers_per_block[i]` is the customer count for block `i` (0-based,
    /// matching `RestorationPlan::steps` load-block ordering).
    pub fn compute_saidi(&self, plan: &RestorationPlan, customers_per_block: &[usize]) -> f64 {
        use crate::optimize::restoration::black_start::RestorationAction;

        if self.total_customers == 0 {
            return 0.0;
        }

        let mut sum = 0.0_f64;
        for step in &plan.steps {
            if let RestorationAction::PickupLoadBlock { block_id, .. } = &step.action {
                let customers = customers_per_block.get(*block_id).copied().unwrap_or(0);
                let duration_min = step.time_min - self.outage_start_min;
                sum += (customers as f64) * duration_min.max(0.0);
            }
        }
        sum / (self.total_customers as f64)
    }

    /// System Average Interruption Frequency Index \[interruptions / customer\].
    ///
    /// For a single black-start event every customer experiences exactly one
    /// interruption, so SAIFI = total interrupted customers / total customers.
    ///
    /// `customers_per_block[i]` is the customer count for block `i`.
    pub fn compute_saifi(&self, plan: &RestorationPlan, customers_per_block: &[usize]) -> f64 {
        use crate::optimize::restoration::black_start::RestorationAction;

        if self.total_customers == 0 {
            return 0.0;
        }

        let mut interrupted = 0usize;
        for step in &plan.steps {
            if let RestorationAction::PickupLoadBlock { block_id, .. } = &step.action {
                interrupted += customers_per_block.get(*block_id).copied().unwrap_or(0);
            }
        }
        interrupted as f64 / self.total_customers as f64
    }

    /// Energy Not Supplied \[MWh\].
    ///
    /// ```text
    /// ENS = Σ_i (P_base_i × outage_duration_i)
    /// ```
    ///
    /// where `outage_duration_i` is measured from `outage_start_min` to the
    /// step time at which block `i` is restored.
    pub fn compute_ens(&self, plan: &RestorationPlan, blocks: &[LoadBlock]) -> f64 {
        use crate::optimize::restoration::black_start::RestorationAction;

        let mut ens = 0.0_f64;
        for step in &plan.steps {
            if let RestorationAction::PickupLoadBlock { block_id, .. } = &step.action {
                if let Some(block) = blocks.iter().find(|b| b.block_id == *block_id) {
                    let duration_h = (step.time_min - self.outage_start_min).max(0.0) / 60.0;
                    ens += block.base_demand_mw * duration_h;
                }
            }
        }
        // Blocks not restored: add full plan duration
        for block in blocks {
            let restored = plan.steps.iter().any(|s| {
                matches!(&s.action, RestorationAction::PickupLoadBlock { block_id, .. }
                    if *block_id == block.block_id)
            });
            if !restored {
                let duration_h = (plan.total_time_min - self.outage_start_min).max(0.0) / 60.0;
                ens += block.base_demand_mw * duration_h;
            }
        }
        ens
    }
}

#[cfg(test)]
mod tests {
    use super::{RestorationMetrics, RestorationSequencer};
    use crate::network::bus::{Bus, BusType};
    use crate::network::topology::PowerNetwork;
    use crate::optimize::restoration::black_start::{
        LoadBlock, RestorationAction, RestorationPlan, RestorationStep,
    };

    fn make_load_block(
        block_id: usize,
        demand_mw: f64,
        priority: usize,
        can_defer: bool,
    ) -> LoadBlock {
        LoadBlock {
            block_id,
            buses: vec![block_id + 10],
            base_demand_mw: demand_mw,
            cold_load_pickup_factor: 1.5,
            cold_load_decay_min: 30.0,
            priority,
            can_defer,
        }
    }

    #[test]
    fn test_order_load_blocks_returns_nonempty() {
        let sequencer = RestorationSequencer::new(3);
        let blocks = vec![
            make_load_block(0, 20.0, 1, false),
            make_load_block(1, 10.0, 2, true),
            make_load_block(2, 15.0, 2, true),
        ];
        let result = sequencer.order_load_blocks(&blocks, 100.0, 10.0);
        assert!(!result.is_empty(), "result should be non-empty");
        assert!(
            result.contains(&0),
            "non-deferrable block 0 must be included"
        );
    }

    #[test]
    fn test_order_load_blocks_priority_ordering() {
        let sequencer = RestorationSequencer::new(3);
        let blocks = vec![
            make_load_block(0, 20.0, 1, false),
            make_load_block(1, 10.0, 2, true),
            make_load_block(2, 15.0, 2, true),
        ];
        let result = sequencer.order_load_blocks(&blocks, 100.0, 0.0);
        let pos0 = result
            .iter()
            .position(|&id| id == 0)
            .expect("block 0 must be in result");
        let pos1 = result.iter().position(|&id| id == 1);
        let pos2 = result.iter().position(|&id| id == 2);
        if let Some(p1) = pos1 {
            assert!(pos0 < p1, "priority-1 block 0 must come before block 1");
        }
        if let Some(p2) = pos2 {
            assert!(pos0 < p2, "priority-1 block 0 must come before block 2");
        }
    }

    #[test]
    fn test_order_load_blocks_respects_headroom() {
        let sequencer = RestorationSequencer::new(3);
        // block0: 80MW * 1.5 CLP = 120MW required — exceeds headroom of 20MW
        // block1: 5MW  * 1.5 CLP =  7.5MW required — fits within 20MW
        // both are deferrable, so block0 should be excluded
        let blocks = vec![
            make_load_block(0, 80.0, 1, true),
            make_load_block(1, 5.0, 1, true),
        ];
        let result = sequencer.order_load_blocks(&blocks, 20.0, 0.0);
        assert!(
            result.contains(&1),
            "block 1 (7.5 MW CLP) must fit within 20 MW headroom"
        );
        assert!(
            !result.contains(&0),
            "block 0 (120 MW CLP) must not fit within 20 MW headroom"
        );
    }

    #[test]
    fn test_compute_parallel_paths_empty_network() {
        let sequencer = RestorationSequencer::new(4);
        let mut net = PowerNetwork::new(100.0);
        net.buses.push(Bus::new(1, BusType::Slack));
        let groups = sequencer.compute_parallel_paths(&net, &[1]);
        assert!(groups.is_empty(), "no branches means no energization paths");
    }

    #[test]
    fn test_metrics_saidi_saifi_ens() {
        let metrics = RestorationMetrics::new(100, 0.0);
        let blocks = vec![
            make_load_block(0, 20.0, 1, false),
            make_load_block(1, 10.0, 2, true),
        ];
        let plan = RestorationPlan {
            steps: vec![
                RestorationStep {
                    step_id: 1,
                    time_min: 30.0,
                    action: RestorationAction::PickupLoadBlock {
                        block_id: 0,
                        actual_mw: 20.0,
                    },
                    available_generation_mw: 50.0,
                    connected_load_mw: 20.0,
                    frequency_hz: 50.0,
                    notes: String::new(),
                },
                RestorationStep {
                    step_id: 2,
                    time_min: 60.0,
                    action: RestorationAction::PickupLoadBlock {
                        block_id: 1,
                        actual_mw: 10.0,
                    },
                    available_generation_mw: 50.0,
                    connected_load_mw: 30.0,
                    frequency_hz: 50.0,
                    notes: String::new(),
                },
            ],
            total_time_min: 120.0,
            restored_load_pct: 1.0,
            n_black_start_units_used: 1,
            critical_loads_restored_min: 30.0,
            feasible: true,
            bottlenecks: vec![],
        };
        let customers_per_block = [50usize, 50usize];
        let saidi = metrics.compute_saidi(&plan, &customers_per_block);
        let saifi = metrics.compute_saifi(&plan, &customers_per_block);
        let ens = metrics.compute_ens(&plan, &blocks);
        assert!(saidi > 0.0, "SAIDI must be positive");
        assert!(
            (saifi - 1.0).abs() < 1e-9,
            "SAIFI must equal 1.0 (all 100 customers interrupted, 100/100)"
        );
        assert!(ens > 0.0, "ENS must be positive");
    }

    #[test]
    fn test_sequencer_new_clamps_to_one() {
        let sequencer = RestorationSequencer::new(0);
        assert_eq!(
            sequencer.max_parallel_paths, 1,
            "new(0) must clamp max_parallel_paths to 1"
        );
    }

    #[test]
    fn test_order_load_blocks_empty_input() {
        let sequencer = RestorationSequencer::new(3);
        let result = sequencer.order_load_blocks(&[], 100.0, 10.0);
        assert!(
            result.is_empty(),
            "empty blocks slice must produce empty result"
        );
    }

    #[test]
    fn test_order_load_blocks_nondeferrable_always_included() {
        let sequencer = RestorationSequencer::new(3);
        // CLP = 200.0 * 1.5 = 300 MW, far exceeds headroom of 10 MW
        let blocks = vec![make_load_block(0, 200.0, 1, false)];
        let result = sequencer.order_load_blocks(&blocks, 15.0, 5.0);
        assert!(
            result.contains(&0),
            "non-deferrable block must be included regardless of CLP vs headroom"
        );
    }

    #[test]
    fn test_order_load_blocks_reserve_reduces_headroom() {
        let sequencer = RestorationSequencer::new(3);
        // headroom = 50 - 40 = 10 MW
        // block 0: deferrable, CLP = 10.0 * 1.5 = 15 MW → excluded (15 > 10)
        // block 1: deferrable, CLP =  3.0 * 1.5 =  4.5 MW → included (4.5 ≤ 10)
        let blocks = vec![
            make_load_block(0, 10.0, 1, true),
            make_load_block(1, 3.0, 1, true),
        ];
        let result = sequencer.order_load_blocks(&blocks, 50.0, 40.0);
        assert!(
            result.contains(&1),
            "block 1 (4.5 MW CLP) must fit within 10 MW headroom"
        );
        assert!(
            !result.contains(&0),
            "block 0 (15 MW CLP) must be excluded when headroom is only 10 MW"
        );
    }

    #[test]
    fn test_compute_parallel_paths_limits_groups() {
        use crate::network::branch::Branch;

        let sequencer = RestorationSequencer::new(2);
        let mut net = PowerNetwork::new(100.0);
        // Three black-start buses (1, 3, 5) each with one neighbour (2, 4, 6)
        for id in [1, 2, 3, 4, 5, 6] {
            net.buses.push(Bus::new(id, BusType::Slack));
        }
        net.branches.push(Branch {
            from_bus: 1,
            to_bus: 2,
            r: 0.1,
            x: 0.2,
            b: 0.01,
            rate_a: 100.0,
            rate_b: 100.0,
            rate_c: 100.0,
            tap: 1.0,
            shift: 0.0,
            status: true,
        });
        net.branches.push(Branch {
            from_bus: 3,
            to_bus: 4,
            r: 0.1,
            x: 0.2,
            b: 0.01,
            rate_a: 100.0,
            rate_b: 100.0,
            rate_c: 100.0,
            tap: 1.0,
            shift: 0.0,
            status: true,
        });
        net.branches.push(Branch {
            from_bus: 5,
            to_bus: 6,
            r: 0.1,
            x: 0.2,
            b: 0.01,
            rate_a: 100.0,
            rate_b: 100.0,
            rate_c: 100.0,
            tap: 1.0,
            shift: 0.0,
            status: true,
        });

        let groups = sequencer.compute_parallel_paths(&net, &[1, 3, 5]);
        assert!(
            groups.len() <= 2,
            "result must be clamped to max_parallel_paths=2, got {}",
            groups.len()
        );
    }

    #[test]
    fn test_metrics_zero_customers_returns_zero() {
        let metrics = RestorationMetrics::new(0, 0.0);
        let blocks = vec![make_load_block(0, 20.0, 1, false)];
        let plan = RestorationPlan {
            steps: vec![RestorationStep {
                step_id: 1,
                time_min: 30.0,
                action: RestorationAction::PickupLoadBlock {
                    block_id: 0,
                    actual_mw: 20.0,
                },
                available_generation_mw: 50.0,
                connected_load_mw: 20.0,
                frequency_hz: 50.0,
                notes: String::new(),
            }],
            total_time_min: 60.0,
            restored_load_pct: 1.0,
            n_black_start_units_used: 1,
            critical_loads_restored_min: 30.0,
            feasible: true,
            bottlenecks: vec![],
        };
        let customers_per_block = [100usize];
        assert!(
            (metrics.compute_saidi(&plan, &customers_per_block)).abs() < 1e-9,
            "SAIDI must be 0.0 when total_customers == 0"
        );
        assert!(
            (metrics.compute_saifi(&plan, &customers_per_block)).abs() < 1e-9,
            "SAIFI must be 0.0 when total_customers == 0"
        );
        // ENS is independent of customer count; block 0 restored at t=30 min
        // → duration_h = 0.5, ENS = 20 MW * 0.5 h = 10 MWh
        assert!(
            (metrics.compute_ens(&plan, &blocks) - 10.0_f64).abs() < 1e-9,
            "ENS must equal 10 MWh for the one restored block"
        );
    }

    #[test]
    fn test_metrics_ens_unrestored_block() {
        // outage starts at t=0, plan runs 120 min
        // block 0 restored at t=30, block 1 never restored
        let metrics = RestorationMetrics::new(100, 0.0);
        let blocks = vec![
            make_load_block(0, 20.0, 1, false), // 20 MW
            make_load_block(1, 10.0, 2, true),  // 10 MW — unrestored
        ];
        let plan = RestorationPlan {
            steps: vec![RestorationStep {
                step_id: 1,
                time_min: 30.0,
                action: RestorationAction::PickupLoadBlock {
                    block_id: 0,
                    actual_mw: 20.0,
                },
                available_generation_mw: 50.0,
                connected_load_mw: 20.0,
                frequency_hz: 50.0,
                notes: String::new(),
            }],
            total_time_min: 120.0,
            restored_load_pct: 0.5,
            n_black_start_units_used: 1,
            critical_loads_restored_min: 30.0,
            feasible: true,
            bottlenecks: vec![],
        };
        let ens = metrics.compute_ens(&plan, &blocks);
        // block 0 restored at t=30: ENS contribution = 20 MW * (30/60) h = 10 MWh
        // block 1 unrestored:       ENS contribution = 10 MW * (120/60) h = 20 MWh
        // total = 30 MWh
        let expected = 10.0_f64 + 20.0_f64;
        assert!(
            (ens - expected).abs() < 1e-9,
            "ENS must include unrestored block contribution: expected {expected}, got {ens}"
        );
    }
}