powerio-prob 0.11.3

Problem instance builders for power system analysis and optimization.
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
//! Multiconductor operating points over one shared
//! [`MulticonductorNetwork`] handle, with per terminal addressing.

use std::sync::Arc;

use powerio_core::{Error, TimePoint, TimeSeries};
use powerio_dist::MulticonductorNetwork;

use super::{
    OperatingPointColumns, OperatingPointFlags, OperatingPointValues, QuantityLayout,
    SharedColumns, dense_quantity, sparse_quantity,
};
use crate::diagnostics::codes;

/// Multiconductor quantity names. Terminal quantities are keyed
/// `bus_id/terminal`; per element phase quantities `element_name/terminal`;
/// whole element quantities by the element's name. Names keep the case the
/// source supplied.
const TERMINAL_VOLTAGE_MAGNITUDE: &str = "terminal_voltage_magnitude";
const TERMINAL_VOLTAGE_ANGLE: &str = "terminal_voltage_angle";
pub(crate) const LOAD_ACTIVE_POWER: &str = "load_active_power";
pub(crate) const LOAD_REACTIVE_POWER: &str = "load_reactive_power";
pub(crate) const GENERATOR_ACTIVE_POWER: &str = "generator_active_power";
pub(crate) const GENERATOR_REACTIVE_POWER: &str = "generator_reactive_power";
pub(crate) const SWITCH_CLOSED: &str = "switch_closed";
const TRANSFORMER_TAP: &str = "transformer_tap";
const CAPACITOR_STEPS: &str = "capacitor_steps";

/// A numeric multiconductor operating point quantity.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MulticonductorOperatingPointQuantity {
    TerminalVoltageMagnitude,
    TerminalVoltageAngle,
    LoadActivePower,
    LoadReactivePower,
    GeneratorActivePower,
    GeneratorReactivePower,
    TransformerTap,
    CapacitorSteps,
}

impl MulticonductorOperatingPointQuantity {
    /// The stable PowerIO IR spelling.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::TerminalVoltageMagnitude => TERMINAL_VOLTAGE_MAGNITUDE,
            Self::TerminalVoltageAngle => TERMINAL_VOLTAGE_ANGLE,
            Self::LoadActivePower => LOAD_ACTIVE_POWER,
            Self::LoadReactivePower => LOAD_REACTIVE_POWER,
            Self::GeneratorActivePower => GENERATOR_ACTIVE_POWER,
            Self::GeneratorReactivePower => GENERATOR_REACTIVE_POWER,
            Self::TransformerTap => TRANSFORMER_TAP,
            Self::CapacitorSteps => CAPACITOR_STEPS,
        }
    }
}

/// A boolean multiconductor operating point quantity.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MulticonductorOperatingPointFlag {
    SwitchClosed,
}

impl MulticonductorOperatingPointFlag {
    /// The stable PowerIO IR spelling.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::SwitchClosed => SWITCH_CLOSED,
        }
    }
}

pub use super::OperatingPoint;

impl OperatingPoint<MulticonductorNetwork> {
    /// Rebind this point to an edited network with the same component and
    /// terminal identity layouts.
    pub(crate) fn rebind_network(mut self, network: MulticonductorNetwork) -> Result<Self, Error> {
        let layout = MulticonductorOperatingPointBuilder::new(network.clone(), Vec::new());
        for quantity in self.columns.quantities.keys() {
            let expected: Vec<String> = layout
                .layout_for(quantity)?
                .order()
                .map(str::to_owned)
                .collect();
            let actual: Vec<&str> = self
                .identity_order(quantity)
                .expect("the quantity came from this point")
                .collect();
            if actual.len() != expected.len()
                || actual
                    .iter()
                    .zip(&expected)
                    .any(|(left, right)| *left != right)
            {
                return Err(Error::new(
                    &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                    format!(
                        "{quantity}: edited network changes the initial point's component identity order"
                    ),
                ));
            }
        }
        self.network = network;
        Ok(self)
    }

    /// Iterate one numeric quantity in stable component identity order.
    #[must_use]
    pub fn values(
        &self,
        quantity: MulticonductorOperatingPointQuantity,
    ) -> Option<OperatingPointValues<'_>> {
        self.iter_values(quantity.name())
    }

    /// Iterate one boolean quantity in stable component identity order.
    #[must_use]
    pub fn flags(
        &self,
        quantity: MulticonductorOperatingPointFlag,
    ) -> Option<OperatingPointFlags<'_>> {
        self.iter_flags(quantity.name())
    }

    /// Voltage magnitude at one bus terminal in volts, keyed
    /// `bus_id/terminal`.
    #[must_use]
    pub fn terminal_voltage_magnitude(&self, bus: &str, terminal: &str) -> Option<f64> {
        self.value_pair(TERMINAL_VOLTAGE_MAGNITUDE, bus, terminal)
    }

    /// Voltage angle at one bus terminal in radians.
    #[must_use]
    pub fn terminal_voltage_angle(&self, bus: &str, terminal: &str) -> Option<f64> {
        self.value_pair(TERMINAL_VOLTAGE_ANGLE, bus, terminal)
    }

    /// Load active power on one conductor in watts, keyed
    /// `load_name/terminal`.
    #[must_use]
    pub fn load_active_power(&self, load: &str, terminal: &str) -> Option<f64> {
        self.value_pair(LOAD_ACTIVE_POWER, load, terminal)
    }

    /// Load reactive power on one conductor in vars.
    #[must_use]
    pub fn load_reactive_power(&self, load: &str, terminal: &str) -> Option<f64> {
        self.value_pair(LOAD_REACTIVE_POWER, load, terminal)
    }

    /// Generator active power on one conductor in watts.
    #[must_use]
    pub fn generator_active_power(&self, generator: &str, terminal: &str) -> Option<f64> {
        self.value_pair(GENERATOR_ACTIVE_POWER, generator, terminal)
    }

    /// Generator reactive power on one conductor in vars.
    #[must_use]
    pub fn generator_reactive_power(&self, generator: &str, terminal: &str) -> Option<f64> {
        self.value_pair(GENERATOR_REACTIVE_POWER, generator, terminal)
    }

    /// Whether the named switch is closed at this point.
    #[must_use]
    pub fn switch_closed(&self, switch: &str) -> Option<bool> {
        self.value_single(SWITCH_CLOSED, switch)
            .map(|value| value != 0.0)
    }

    /// The named transformer's regulator tap position at this point.
    #[must_use]
    pub fn transformer_tap(&self, transformer: &str) -> Option<f64> {
        self.value_single(TRANSFORMER_TAP, transformer)
    }

    /// The named capacitor's engaged step count at this point.
    #[must_use]
    pub fn capacitor_steps(&self, capacitor: &str) -> Option<f64> {
        self.value_single(CAPACITOR_STEPS, capacitor)
    }

    fn value_pair(&self, quantity: &'static str, element: &str, terminal: &str) -> Option<f64> {
        self.columns
            .quantities
            .get(quantity)?
            .value(self.index, &format!("{element}/{terminal}"))
    }

    fn value_single(&self, quantity: &'static str, element: &str) -> Option<f64> {
        self.columns
            .quantities
            .get(quantity)?
            .value(self.index, element)
    }
}

/// Bulk constructor for a multiconductor operating point series. Identities
/// resolve once against the network's stable order: bus terminals in bus
/// table order with each bus's terminal order, load conductors in load table
/// order with each terminal map's order, and named elements in their table
/// order.
#[derive(Debug)]
pub struct MulticonductorOperatingPointBuilder {
    network: MulticonductorNetwork,
    time_points: Vec<TimePoint>,
    quantities: Vec<(&'static str, ColumnsInput)>,
}

#[derive(Debug)]
enum ColumnsInput {
    Dense(Vec<f64>),
    Sparse {
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    },
}

impl MulticonductorOperatingPointBuilder {
    #[must_use]
    pub fn new(network: MulticonductorNetwork, time_points: Vec<TimePoint>) -> Self {
        Self {
            network,
            time_points,
            quantities: Vec::new(),
        }
    }

    /// Start a builder for one operating point.
    #[must_use]
    pub fn for_point(network: MulticonductorNetwork) -> Self {
        Self::new(network, Vec::new())
    }

    fn dense(mut self, quantity: &'static str, values: Vec<f64>) -> Self {
        self.quantities
            .push((quantity, ColumnsInput::Dense(values)));
        self
    }

    fn sparse(
        mut self,
        quantity: &'static str,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.quantities
            .push((quantity, ColumnsInput::Sparse { base, changes }));
        self
    }

    fn sparse_flags(
        self,
        quantity: &'static str,
        base: Vec<bool>,
        changes: Vec<Vec<(String, bool)>>,
    ) -> Self {
        self.sparse(
            quantity,
            encode_flags(base),
            changes
                .into_iter()
                .map(|point| {
                    point
                        .into_iter()
                        .map(|(identity, value)| (identity, encode_flag(value)))
                        .collect()
                })
                .collect(),
        )
    }

    /// Dense terminal voltage magnitudes: one value per bus terminal in bus
    /// table order (each bus's terminals in its stated order), point major.
    #[must_use]
    pub fn terminal_voltage_magnitudes(self, values: Vec<f64>) -> Self {
        self.dense(TERMINAL_VOLTAGE_MAGNITUDE, values)
    }

    #[must_use]
    pub fn terminal_voltage_angles(self, values: Vec<f64>) -> Self {
        self.dense(TERMINAL_VOLTAGE_ANGLE, values)
    }

    /// Dense per conductor load active powers: one value per load terminal in
    /// load table order (each load's terminal map order), point major.
    #[must_use]
    pub fn load_active_powers(self, values: Vec<f64>) -> Self {
        self.dense(LOAD_ACTIVE_POWER, values)
    }

    #[must_use]
    pub fn load_reactive_powers(self, values: Vec<f64>) -> Self {
        self.dense(LOAD_REACTIVE_POWER, values)
    }

    #[must_use]
    pub fn generator_active_powers(self, values: Vec<f64>) -> Self {
        self.dense(GENERATOR_ACTIVE_POWER, values)
    }

    #[must_use]
    pub fn generator_reactive_powers(self, values: Vec<f64>) -> Self {
        self.dense(GENERATOR_REACTIVE_POWER, values)
    }

    #[must_use]
    pub fn switch_closed(self, values: Vec<bool>) -> Self {
        self.dense(SWITCH_CLOSED, encode_flags(values))
    }

    #[must_use]
    pub fn transformer_taps(self, values: Vec<f64>) -> Self {
        self.dense(TRANSFORMER_TAP, values)
    }

    #[must_use]
    pub fn capacitor_steps(self, values: Vec<f64>) -> Self {
        self.dense(CAPACITOR_STEPS, values)
    }

    /// Sparse terminal voltage magnitudes: one base row plus per point
    /// overrides keyed `bus_id/terminal`.
    #[must_use]
    pub fn sparse_terminal_voltage_magnitudes(
        self,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.sparse(TERMINAL_VOLTAGE_MAGNITUDE, base, changes)
    }

    #[must_use]
    pub fn sparse_terminal_voltage_angles(
        self,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.sparse(TERMINAL_VOLTAGE_ANGLE, base, changes)
    }

    /// Sparse per conductor load active powers: one base row plus per point
    /// overrides keyed `load_name/terminal`.
    #[must_use]
    pub fn sparse_load_active_powers(
        self,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.sparse(LOAD_ACTIVE_POWER, base, changes)
    }

    #[must_use]
    pub fn sparse_load_reactive_powers(
        self,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.sparse(LOAD_REACTIVE_POWER, base, changes)
    }

    #[must_use]
    pub fn sparse_generator_active_powers(
        self,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.sparse(GENERATOR_ACTIVE_POWER, base, changes)
    }

    #[must_use]
    pub fn sparse_generator_reactive_powers(
        self,
        base: Vec<f64>,
        changes: Vec<Vec<(String, f64)>>,
    ) -> Self {
        self.sparse(GENERATOR_REACTIVE_POWER, base, changes)
    }

    #[must_use]
    pub fn sparse_switch_closed(self, base: Vec<bool>, changes: Vec<Vec<(String, bool)>>) -> Self {
        self.sparse_flags(SWITCH_CLOSED, base, changes)
    }

    #[must_use]
    pub fn sparse_transformer_taps(self, base: Vec<f64>, changes: Vec<Vec<(String, f64)>>) -> Self {
        self.sparse(TRANSFORMER_TAP, base, changes)
    }

    #[must_use]
    pub fn sparse_capacitor_steps(self, base: Vec<f64>, changes: Vec<Vec<(String, f64)>>) -> Self {
        self.sparse(CAPACITOR_STEPS, base, changes)
    }

    fn layout_for(&self, quantity: &'static str) -> Result<QuantityLayout, Error> {
        let network = &self.network;
        match quantity {
            TERMINAL_VOLTAGE_MAGNITUDE | TERMINAL_VOLTAGE_ANGLE => QuantityLayout::from_order(
                quantity,
                network.buses().iter().flat_map(|bus| {
                    bus.terminals
                        .iter()
                        .map(move |terminal| format!("{}/{terminal}", bus.id))
                }),
            ),
            LOAD_ACTIVE_POWER | LOAD_REACTIVE_POWER => QuantityLayout::from_order(
                quantity,
                network.loads().iter().flat_map(|load| {
                    load.terminal_map
                        .iter()
                        .map(move |terminal| format!("{}/{terminal}", load.name))
                }),
            ),
            GENERATOR_ACTIVE_POWER | GENERATOR_REACTIVE_POWER => QuantityLayout::from_order(
                quantity,
                network.generators().iter().flat_map(|generator| {
                    generator
                        .terminal_map
                        .iter()
                        .map(move |terminal| format!("{}/{terminal}", generator.name))
                }),
            ),
            SWITCH_CLOSED => QuantityLayout::from_order(
                quantity,
                network.switches().iter().map(|s| s.name.clone()),
            ),
            TRANSFORMER_TAP => QuantityLayout::from_order(
                quantity,
                network.transformers().iter().map(|t| t.name.clone()),
            ),
            CAPACITOR_STEPS => QuantityLayout::from_order(
                quantity,
                network.capacitors().iter().map(|c| c.name.clone()),
            ),
            _ => unreachable!("builder methods name registered quantities"),
        }
    }

    fn build_points(
        &self,
        point_count: usize,
    ) -> Result<Vec<OperatingPoint<MulticonductorNetwork>>, Error> {
        let mut quantities = std::collections::HashMap::new();
        for (quantity, input) in &self.quantities {
            let layout = self.layout_for(quantity)?;
            let built = match input {
                ColumnsInput::Dense(values) => {
                    dense_quantity(quantity, layout, point_count, values.clone())?
                }
                ColumnsInput::Sparse { base, changes } => {
                    sparse_quantity(quantity, layout, point_count, base.clone(), changes.clone())?
                }
            };
            if quantities.insert(*quantity, built).is_some() {
                return Err(Error::new(
                    &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                    format!("{quantity} was supplied twice"),
                ));
            }
        }
        let columns: SharedColumns = Arc::new(OperatingPointColumns {
            point_count,
            quantities,
        });
        Ok((0..point_count)
            .map(|index| OperatingPoint {
                network: self.network.clone(),
                columns: Arc::clone(&columns),
                index,
            })
            .collect())
    }

    /// Resolve every identity once, validate every column, and build the
    /// series. The points share the network handle and the columns; QSTS
    /// result import goes through here without cloning the network per
    /// sample.
    ///
    /// # Errors
    /// A shape mismatch, a duplicate or unknown identity, or an empty time
    /// axis.
    pub fn build(self) -> Result<TimeSeries<OperatingPoint<MulticonductorNetwork>>, Error> {
        let point_count = self.time_points.len();
        if point_count == 0 {
            return Err(Error::new(
                &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                "an operating point series needs at least one time point",
            ));
        }
        let points = self.build_points(point_count)?;
        TimeSeries::new(self.time_points, points)
    }

    /// Build one operating point.
    ///
    /// # Errors
    /// The builder does not contain exactly one point, or a quantity is invalid.
    pub fn build_point(self) -> Result<OperatingPoint<MulticonductorNetwork>, Error> {
        if self.time_points.len() > 1 {
            return Err(Error::new(
                &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                "a scalar operating point builder cannot contain several time points",
            ));
        }
        self.build_points(1)?.pop().ok_or_else(|| {
            Error::new(
                &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                "the scalar operating point builder produced no point",
            )
        })
    }
}

fn encode_flags(values: Vec<bool>) -> Vec<f64> {
    values.into_iter().map(encode_flag).collect()
}

fn encode_flag(value: bool) -> f64 {
    if value { 1.0 } else { 0.0 }
}