powerio 0.11.0

Compiler infrastructure for power systems: parse, validate, transform, and emit grid models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! The dynamic Rust boundary for values produced by universal parsing.

use powerio_core::{Scenario, ScenarioSet, TimePoint, TimeSeries};
use powerio_dist::MulticonductorNetwork;
use powerio_prob::OperatingPoint;
use powerio_tx::{BalancedNetwork, GeoLayer};

/// A dynamically typed time series at the universal parser boundary.
///
/// Typed Rust code uses `TimeSeries<T>` directly. This wrapper preserves the
/// element type for empty collections and lets C and PowerIO IR report the
/// same structural type name without a flattened collection registry.
#[derive(Clone, Debug)]
pub struct PioTimeSeries {
    element_type: Box<str>,
    type_name: Box<str>,
    values: TimeSeries<PioValue>,
}

impl PioTimeSeries {
    fn from_typed<T>(element_type: &'static str, values: TimeSeries<T>) -> Self
    where
        T: Clone + Into<PioValue>,
    {
        let values = values.map_values(Into::into);
        debug_assert!(
            values
                .values()
                .iter()
                .all(|value| value.type_name() == element_type)
        );
        Self {
            element_type: element_type.into(),
            type_name: format!("powerio.TimeSeries<{element_type}>").into_boxed_str(),
            values,
        }
    }

    /// Construct the dynamic form from values that already crossed the
    /// universal parser boundary.
    ///
    /// Typed Rust code should use [`TimeSeries<T>`] and its ordinary
    /// [`From`] conversion. This constructor exists for dynamic language
    /// bindings. An empty input has no value from which to infer `T` and is
    /// rejected.
    pub fn from_values(
        time_points: Vec<TimePoint>,
        values: Vec<PioValue>,
    ) -> Result<Self, powerio_core::Error> {
        let Some(first) = values.first() else {
            return Err(powerio_core::Error::new(
                &crate::codes::VALIDATE_COLLECTION_EMPTY,
                "a time series needs at least one value to infer its element type",
            ));
        };
        let element_type = first.type_name().to_owned();
        if !is_time_series_element_type(&element_type) {
            return Err(powerio_core::Error::new(
                &crate::codes::VALIDATE_COLLECTION_ELEMENT_TYPE,
                format!("PowerIO IR does not define a time series of `{element_type}`"),
            ));
        }
        require_element_type(values.iter(), &element_type)?;
        let values = TimeSeries::new(time_points, values)?;
        Ok(Self {
            type_name: format!("powerio.TimeSeries<{element_type}>").into_boxed_str(),
            element_type: element_type.into_boxed_str(),
            values,
        })
    }

    #[must_use]
    pub fn element_type(&self) -> &str {
        &self.element_type
    }

    #[must_use]
    pub fn type_name(&self) -> &str {
        &self.type_name
    }

    #[must_use]
    pub fn time_points(&self) -> &[TimePoint] {
        self.values.time_points()
    }

    #[must_use]
    pub fn get(&self, index: usize) -> Option<&PioValue> {
        self.values.get(index)
    }

    /// Mutably borrow one entry through the collection's copy on write
    /// storage.
    pub fn get_mut(&mut self, index: usize) -> Option<&mut PioValue> {
        self.values.get_mut(index)
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = (&TimePoint, &PioValue)> {
        self.values.iter()
    }

    /// Iterate over mutable entries through one copy on write split.
    pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = (&TimePoint, &mut PioValue)> {
        self.values.iter_mut()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    pub(crate) fn values(&self) -> &TimeSeries<PioValue> {
        &self.values
    }
}

/// A dynamically typed scenario set at the universal parser boundary.
/// Typed Rust code uses `ScenarioSet<T>` directly.
#[derive(Clone, Debug)]
pub struct PioScenarioSet {
    element_type: Box<str>,
    type_name: Box<str>,
    values: ScenarioSet<PioValue>,
}

impl PioScenarioSet {
    fn from_typed<T>(element_type: &'static str, values: ScenarioSet<T>) -> Self
    where
        T: Clone + Into<PioValue>,
    {
        let values = values.map_values(Into::into);
        debug_assert!(
            values
                .iter()
                .all(|scenario| scenario.value().type_name() == element_type)
        );
        Self {
            element_type: element_type.into(),
            type_name: format!("powerio.ScenarioSet<{element_type}>").into_boxed_str(),
            values,
        }
    }

    /// Construct the dynamic form from scenarios that already crossed the
    /// universal parser boundary.
    ///
    /// Typed Rust code should use [`ScenarioSet<T>`] and its ordinary
    /// [`From`] conversion. An empty input has no value from which to infer
    /// `T` and is rejected.
    pub fn from_scenarios(scenarios: Vec<Scenario<PioValue>>) -> Result<Self, powerio_core::Error> {
        let Some(first) = scenarios.first() else {
            return Err(powerio_core::Error::new(
                &crate::codes::VALIDATE_COLLECTION_EMPTY,
                "a scenario set needs at least one value to infer its element type",
            ));
        };
        let element_type = first.value().type_name().to_owned();
        if !is_scenario_element_type(&element_type) {
            return Err(powerio_core::Error::new(
                &crate::codes::VALIDATE_COLLECTION_ELEMENT_TYPE,
                format!("PowerIO IR does not define a scenario set of `{element_type}`"),
            ));
        }
        require_element_type(scenarios.iter().map(Scenario::value), &element_type)?;
        let values = ScenarioSet::new(scenarios)?;
        Ok(Self {
            type_name: format!("powerio.ScenarioSet<{element_type}>").into_boxed_str(),
            element_type: element_type.into_boxed_str(),
            values,
        })
    }

    #[must_use]
    pub fn element_type(&self) -> &str {
        &self.element_type
    }

    #[must_use]
    pub fn type_name(&self) -> &str {
        &self.type_name
    }

    #[must_use]
    pub fn get(&self, id: &str) -> Option<&PioValue> {
        self.values.get(id)
    }

    /// Mutably borrow one entry by scenario ID through copy on write.
    pub fn get_mut(&mut self, id: &str) -> Option<&mut PioValue> {
        self.values.get_mut(id)
    }

    #[must_use]
    pub fn get_at(&self, position: usize) -> Option<&PioValue> {
        self.values.get_at(position)
    }

    /// Mutably borrow one entry by insertion position through copy on write.
    pub fn get_at_mut(&mut self, position: usize) -> Option<&mut PioValue> {
        self.values.get_at_mut(position)
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = &Scenario<PioValue>> {
        self.values.iter()
    }

    /// Iterate over mutable scenario entries through one copy on write split.
    pub fn iter_mut(&mut self) -> impl ExactSizeIterator<Item = &mut Scenario<PioValue>> {
        self.values.iter_mut()
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    pub(crate) fn values(&self) -> &ScenarioSet<PioValue> {
        &self.values
    }
}

const BALANCED_NETWORK_TYPE: &str = "powerio.BalancedNetwork";
const MULTICONDUCTOR_NETWORK_TYPE: &str = "powerio.MulticonductorNetwork";
const BALANCED_OPERATING_POINT_TYPE: &str = "powerio.OperatingPoint<powerio.BalancedNetwork>";
const MULTICONDUCTOR_OPERATING_POINT_TYPE: &str =
    "powerio.OperatingPoint<powerio.MulticonductorNetwork>";

fn is_time_series_element_type(type_name: &str) -> bool {
    matches!(
        type_name,
        BALANCED_NETWORK_TYPE
            | MULTICONDUCTOR_NETWORK_TYPE
            | BALANCED_OPERATING_POINT_TYPE
            | MULTICONDUCTOR_OPERATING_POINT_TYPE
    )
}

fn is_scenario_element_type(type_name: &str) -> bool {
    is_time_series_element_type(type_name)
        || matches!(
            type_name,
            "powerio.TimeSeries<powerio.BalancedNetwork>"
                | "powerio.TimeSeries<powerio.MulticonductorNetwork>"
                | "powerio.TimeSeries<powerio.OperatingPoint<powerio.BalancedNetwork>>"
                | "powerio.TimeSeries<powerio.OperatingPoint<powerio.MulticonductorNetwork>>"
        )
}

fn require_element_type<'a>(
    values: impl IntoIterator<Item = &'a PioValue>,
    element_type: &str,
) -> Result<(), powerio_core::Error> {
    if let Some(value) = values
        .into_iter()
        .find(|value| value.type_name() != element_type)
    {
        return Err(powerio_core::Error::new(
            &crate::codes::VALIDATE_COLLECTION_ELEMENT_TYPE,
            format!(
                "collection starts with `{element_type}` but also contains `{}`",
                value.type_name()
            ),
        ));
    }
    Ok(())
}

/// A value produced by PowerIO's universal parser or decoded from PowerIO IR.
/// Application values remain ordinary `PioModule<T>` values.
#[derive(Clone, Debug)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub enum PioValue {
    BalancedNetwork(BalancedNetwork),
    MulticonductorNetwork(MulticonductorNetwork),
    /// A standalone geographic document: element points and routes keyed by
    /// element identity, placed onto a network by
    /// [`crate::apply_geo_layer`].
    GeoLayer(GeoLayer),
    BalancedOperatingPoint(OperatingPoint<BalancedNetwork>),
    MulticonductorOperatingPoint(OperatingPoint<MulticonductorNetwork>),
    TimeSeries(PioTimeSeries),
    ScenarioSet(PioScenarioSet),
    DcPfInstance(powerio_prob::DcPfInstance),
    AcPfInstance(powerio_prob::AcPfInstance),
    DcOpfInstance(powerio_prob::DcOpfInstance),
    AcOpfInstance(powerio_prob::AcOpfInstance),
    McAcPfInstance(powerio_prob::McAcPfInstance),
    McAcOpfInstance(powerio_prob::McAcOpfInstance),
    AcScucInstance(powerio_prob::AcScucInstance),
    DcPfSolution(powerio_prob::DcPfSolution),
    AcPfSolution(powerio_prob::AcPfSolution),
    DcOpfSolution(powerio_prob::DcOpfSolution),
    AcOpfSolution(powerio_prob::AcOpfSolution),
    SocwrOpfSolution(powerio_prob::solution::SocwrOpfSolution),
    McAcPfSolution(powerio_prob::McAcPfSolution),
    McAcOpfSolution(powerio_prob::McAcOpfSolution),
    AcScucSolution(powerio_prob::AcScucSolution),
}

impl PioValue {
    /// Canonical structural type name used by C and PowerIO IR.
    #[must_use]
    pub fn type_name(&self) -> &str {
        match self {
            Self::BalancedNetwork(_) => "powerio.BalancedNetwork",
            Self::MulticonductorNetwork(_) => "powerio.MulticonductorNetwork",
            Self::GeoLayer(_) => "powerio.GeoLayer",
            Self::BalancedOperatingPoint(_) => "powerio.OperatingPoint<powerio.BalancedNetwork>",
            Self::MulticonductorOperatingPoint(_) => {
                "powerio.OperatingPoint<powerio.MulticonductorNetwork>"
            }
            Self::TimeSeries(series) => series.type_name(),
            Self::ScenarioSet(scenarios) => scenarios.type_name(),
            Self::DcPfInstance(_) => "powerio.DcPfInstance",
            Self::AcPfInstance(_) => "powerio.AcPfInstance",
            Self::DcOpfInstance(_) => "powerio.DcOpfInstance",
            Self::AcOpfInstance(_) => "powerio.AcOpfInstance",
            Self::McAcPfInstance(_) => "powerio.McAcPfInstance",
            Self::McAcOpfInstance(_) => "powerio.McAcOpfInstance",
            Self::AcScucInstance(_) => "powerio.AcScucInstance",
            Self::DcPfSolution(_) => "powerio.DcPfSolution",
            Self::AcPfSolution(_) => "powerio.AcPfSolution",
            Self::DcOpfSolution(_) => "powerio.DcOpfSolution",
            Self::AcOpfSolution(_) => "powerio.AcOpfSolution",
            Self::SocwrOpfSolution(_) => "powerio.SocwrOpfSolution",
            Self::McAcPfSolution(_) => "powerio.McAcPfSolution",
            Self::McAcOpfSolution(_) => "powerio.McAcOpfSolution",
            Self::AcScucSolution(_) => "powerio.AcScucSolution",
        }
    }
}

macro_rules! value_conversion {
    ($ty:ty, $variant:ident) => {
        impl From<$ty> for PioValue {
            fn from(value: $ty) -> Self {
                Self::$variant(value)
            }
        }
    };
}

impl From<BalancedNetwork> for PioValue {
    fn from(mut value: BalancedNetwork) -> Self {
        value.assign_missing_component_ids();
        Self::BalancedNetwork(value)
    }
}
value_conversion!(MulticonductorNetwork, MulticonductorNetwork);
value_conversion!(GeoLayer, GeoLayer);
value_conversion!(OperatingPoint<BalancedNetwork>, BalancedOperatingPoint);
value_conversion!(
    OperatingPoint<MulticonductorNetwork>,
    MulticonductorOperatingPoint
);
value_conversion!(powerio_prob::DcPfInstance, DcPfInstance);
value_conversion!(powerio_prob::AcPfInstance, AcPfInstance);
value_conversion!(powerio_prob::DcOpfInstance, DcOpfInstance);
value_conversion!(powerio_prob::AcOpfInstance, AcOpfInstance);
value_conversion!(powerio_prob::McAcPfInstance, McAcPfInstance);
value_conversion!(powerio_prob::McAcOpfInstance, McAcOpfInstance);
value_conversion!(powerio_prob::AcScucInstance, AcScucInstance);
value_conversion!(powerio_prob::DcPfSolution, DcPfSolution);
value_conversion!(powerio_prob::AcPfSolution, AcPfSolution);
value_conversion!(powerio_prob::DcOpfSolution, DcOpfSolution);
value_conversion!(powerio_prob::AcOpfSolution, AcOpfSolution);
value_conversion!(powerio_prob::solution::SocwrOpfSolution, SocwrOpfSolution);
value_conversion!(powerio_prob::McAcPfSolution, McAcPfSolution);
value_conversion!(powerio_prob::McAcOpfSolution, McAcOpfSolution);
value_conversion!(powerio_prob::AcScucSolution, AcScucSolution);

macro_rules! time_series_conversion {
    ($ty:ty, $name:literal) => {
        impl From<TimeSeries<$ty>> for PioValue {
            fn from(value: TimeSeries<$ty>) -> Self {
                Self::TimeSeries(PioTimeSeries::from_typed($name, value))
            }
        }
    };
}

time_series_conversion!(BalancedNetwork, "powerio.BalancedNetwork");
time_series_conversion!(MulticonductorNetwork, "powerio.MulticonductorNetwork");
time_series_conversion!(
    OperatingPoint<BalancedNetwork>,
    "powerio.OperatingPoint<powerio.BalancedNetwork>"
);
time_series_conversion!(
    OperatingPoint<MulticonductorNetwork>,
    "powerio.OperatingPoint<powerio.MulticonductorNetwork>"
);

macro_rules! scenario_set_conversion {
    ($ty:ty, $name:literal) => {
        impl From<ScenarioSet<$ty>> for PioValue {
            fn from(value: ScenarioSet<$ty>) -> Self {
                Self::ScenarioSet(PioScenarioSet::from_typed($name, value))
            }
        }
    };
}

scenario_set_conversion!(BalancedNetwork, "powerio.BalancedNetwork");
scenario_set_conversion!(MulticonductorNetwork, "powerio.MulticonductorNetwork");
scenario_set_conversion!(
    OperatingPoint<BalancedNetwork>,
    "powerio.OperatingPoint<powerio.BalancedNetwork>"
);
scenario_set_conversion!(
    OperatingPoint<MulticonductorNetwork>,
    "powerio.OperatingPoint<powerio.MulticonductorNetwork>"
);
scenario_set_conversion!(
    TimeSeries<BalancedNetwork>,
    "powerio.TimeSeries<powerio.BalancedNetwork>"
);
scenario_set_conversion!(
    TimeSeries<MulticonductorNetwork>,
    "powerio.TimeSeries<powerio.MulticonductorNetwork>"
);
scenario_set_conversion!(
    TimeSeries<OperatingPoint<BalancedNetwork>>,
    "powerio.TimeSeries<powerio.OperatingPoint<powerio.BalancedNetwork>>"
);
scenario_set_conversion!(
    TimeSeries<OperatingPoint<MulticonductorNetwork>>,
    "powerio.TimeSeries<powerio.OperatingPoint<powerio.MulticonductorNetwork>>"
);

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

    fn small_balanced() -> BalancedNetwork {
        use powerio_tx::{Bus, BusId, BusType};
        BalancedNetwork::in_memory(
            "facade",
            100.0,
            vec![Bus::new(BusId(1), BusType::Ref, 230.0)],
            Vec::new(),
        )
    }

    #[test]
    fn rust_uses_enum_matching_and_structural_names() {
        let value = PioValue::from(small_balanced());
        assert!(matches!(value, PioValue::BalancedNetwork(_)));
        assert_eq!(value.type_name(), "powerio.BalancedNetwork");
    }

    #[test]
    fn collections_keep_generic_structural_names() {
        let series = TimeSeries::new(
            vec![TimePoint::new("now", None).unwrap()],
            vec![small_balanced()],
        )
        .unwrap();
        let value = PioValue::from(series);
        let PioValue::TimeSeries(series) = &value else {
            unreachable!();
        };
        assert_eq!(series.len(), 1);
        assert_eq!(
            value.type_name(),
            "powerio.TimeSeries<powerio.BalancedNetwork>"
        );
    }

    #[test]
    fn scenario_sets_compose_with_time_series() {
        let series = TimeSeries::new(
            vec![TimePoint::new("now", None).unwrap()],
            vec![small_balanced()],
        )
        .unwrap();
        let set = ScenarioSet::new(vec![Scenario::new(
            powerio_core::ScenarioId::new("base").unwrap(),
            None,
            series,
        )])
        .unwrap();
        let value = PioValue::from(set);
        assert_eq!(
            value.type_name(),
            "powerio.ScenarioSet<powerio.TimeSeries<powerio.BalancedNetwork>>"
        );
    }
}