powerio-prob 0.11.1

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
//! Operating points: alternate electrical assignments over an immutable
//! network handle.
//!
//! An [`OperatingPoint`] is a small owning handle: one cheap to clone network
//! handle, one shared column store, and one row index. Building a series
//! resolves every stable element identity once into a private layout; the
//! points share the layout and the columns, so retaining one point after
//! dropping its parent collection copies no network.
//!
//! Quantities include voltages, injections, in-service flags, switch
//! positions, tap positions, phase shifts, and capacitor
//! or regulator settings. Parameters, the equipment set, connectivity, time varying
//! bounds, availability, commitment, reserves, and costs are network or
//! calculation data, never operating point fields.
//!
//! Builders accept dense point major columns or sparse per point overrides
//! over one base assignment. Both forms have the same accessors.

pub(crate) mod balanced;
pub(crate) mod multiconductor;

pub use balanced::{
    BalancedOperatingPointBuilder, BalancedOperatingPointFlag, BalancedOperatingPointQuantity,
};
pub use multiconductor::{
    MulticonductorOperatingPointBuilder, MulticonductorOperatingPointFlag,
    MulticonductorOperatingPointQuantity,
};

/// One possibly partial alternate electrical assignment: a small owning handle over the
/// shared network and the series' shared columns. Cloning it or retaining it
/// after the parent series drops copies no table and no column.
#[derive(Clone, Debug)]
pub struct OperatingPoint<N> {
    pub(crate) network: N,
    pub(crate) columns: SharedColumns,
    pub(crate) index: usize,
}

impl<N> OperatingPoint<N> {
    /// The network whose equipment identities and defaults this point uses.
    pub fn network(&self) -> &N {
        &self.network
    }

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

    fn iter_values(&self, quantity: &'static str) -> Option<OperatingPointValues<'_>> {
        Some(OperatingPointValues {
            quantity: self.columns.quantities.get(quantity)?,
            point: self.index,
            column: 0,
        })
    }

    fn iter_flags(&self, quantity: &'static str) -> Option<OperatingPointFlags<'_>> {
        Some(OperatingPointFlags(self.iter_values(quantity)?))
    }

    pub(crate) fn identity_order(
        &self,
        quantity: &str,
    ) -> Option<impl ExactSizeIterator<Item = &str>> {
        Some(self.columns.quantities.get(quantity)?.layout.order())
    }
}

/// One operating point quantity in stable component identity order.
#[derive(Clone, Debug)]
pub struct OperatingPointValues<'a> {
    quantity: &'a Quantity,
    point: usize,
    column: usize,
}

impl<'a> Iterator for OperatingPointValues<'a> {
    type Item = (&'a str, f64);

    fn next(&mut self) -> Option<Self::Item> {
        let identity = self.quantity.layout.order.get(self.column)?;
        let value =
            self.quantity
                .storage
                .value(self.point, self.column, self.quantity.layout.len());
        self.column += 1;
        Some((identity.as_ref(), value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.quantity.layout.len() - self.column;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for OperatingPointValues<'_> {}

/// One operating point flag in stable component identity order.
#[derive(Clone, Debug)]
pub struct OperatingPointFlags<'a>(OperatingPointValues<'a>);

impl<'a> Iterator for OperatingPointFlags<'a> {
    type Item = (&'a str, bool);

    fn next(&mut self) -> Option<Self::Item> {
        self.0
            .next()
            .map(|(identity, value)| (identity, value != 0.0))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

impl ExactSizeIterator for OperatingPointFlags<'_> {}

use std::collections::HashMap;
use std::sync::Arc;

use powerio_core::Error;

use crate::diagnostics::codes;

/// One quantity's resolved column block: the identity order the network
/// tables define, with a hash lookup so keyed access never scans.
#[derive(Clone, Debug, Default)]
pub(crate) struct QuantityLayout {
    /// Column position by resolved identity.
    index: HashMap<Box<str>, u32>,
    /// The identities in stable network table order; `index` inverts this.
    order: Vec<Box<str>>,
}

impl QuantityLayout {
    pub(crate) fn from_order(
        quantity: &'static str,
        order: impl IntoIterator<Item = String>,
    ) -> Result<Self, Error> {
        let mut layout = Self::default();
        for identity in order {
            let column = u32::try_from(layout.order.len()).map_err(|_| {
                Error::new(
                    &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                    format!("{quantity}: more than u32::MAX elements"),
                )
            })?;
            if layout
                .index
                .insert(identity.clone().into_boxed_str(), column)
                .is_some()
            {
                return Err(Error::new(
                    &codes::BUILD_OPERATING_POINT_IDENTITY_UNKNOWN,
                    format!("{quantity}: duplicate element identity `{identity}`"),
                ));
            }
            layout.order.push(identity.into_boxed_str());
        }
        Ok(layout)
    }

    pub(crate) fn len(&self) -> usize {
        self.order.len()
    }

    pub(crate) fn column(&self, identity: &str) -> Option<usize> {
        self.index.get(identity).map(|column| *column as usize)
    }

    pub(crate) fn order(&self) -> impl ExactSizeIterator<Item = &str> {
        self.order.iter().map(AsRef::as_ref)
    }
}

/// One point's sparse overrides: `(column, value)` pairs sorted by column.
pub(crate) type PointChanges = Box<[(u32, f64)]>;

/// One quantity's values across every point: dense rows, or sparse overrides
/// over one base row. Both address `(point, column)`.
#[derive(Clone, Debug)]
pub(crate) enum QuantityStorage {
    Dense {
        /// `point_count * width` values, point major.
        values: Box<[f64]>,
    },
    Sparse {
        /// The base row every point starts from, `width` long.
        base: Box<[f64]>,
        /// Per point overrides, each sorted by column.
        changes: Box<[PointChanges]>,
    },
}

impl QuantityStorage {
    pub(crate) fn value(&self, point: usize, column: usize, width: usize) -> f64 {
        match self {
            Self::Dense { values } => values[point * width + column],
            Self::Sparse { base, changes } => {
                let column32 = column as u32;
                match changes[point].binary_search_by_key(&column32, |(c, _)| *c) {
                    Ok(found) => changes[point][found].1,
                    Err(_) => base[column],
                }
            }
        }
    }

    pub(crate) fn replace(
        &mut self,
        point: usize,
        column: usize,
        width: usize,
        replacement: f64,
    ) -> bool {
        let previous = self.value(point, column, width);
        if previous.to_bits() == replacement.to_bits() {
            return false;
        }
        match self {
            Self::Dense { values } => values[point * width + column] = replacement,
            Self::Sparse { base, changes } => {
                let column = column as u32;
                let row = &mut changes[point];
                let mut updated = row.to_vec();
                match updated.binary_search_by_key(&column, |(entry, _)| *entry) {
                    Ok(found) if replacement.to_bits() == base[column as usize].to_bits() => {
                        updated.remove(found);
                    }
                    Ok(found) => updated[found].1 = replacement,
                    Err(_) if replacement.to_bits() == base[column as usize].to_bits() => {}
                    Err(insert_at) => updated.insert(insert_at, (column, replacement)),
                }
                *row = updated.into_boxed_slice();
            }
        }
        true
    }
}

/// One named quantity: its identity layout and its storage.
#[derive(Clone, Debug)]
pub(crate) struct Quantity {
    pub(crate) layout: QuantityLayout,
    pub(crate) storage: QuantityStorage,
}

impl Quantity {
    pub(crate) fn value(&self, point: usize, identity: &str) -> Option<f64> {
        let column = self.layout.column(identity)?;
        Some(self.storage.value(point, column, self.layout.len()))
    }
}

/// The shared column store behind every point of one series.
#[derive(Clone, Debug)]
pub(crate) struct OperatingPointColumns {
    pub(crate) point_count: usize,
    pub(crate) quantities: HashMap<&'static str, Quantity>,
}

pub(crate) type SharedColumns = Arc<OperatingPointColumns>;

impl<N> OperatingPoint<N> {
    pub(crate) fn replace_value(
        &mut self,
        quantity_name: &'static str,
        layout: QuantityLayout,
        defaults: &[f64],
        identity: &str,
        replacement: f64,
    ) -> Result<bool, Error> {
        let columns = Arc::make_mut(&mut self.columns);
        if !columns.quantities.contains_key(quantity_name) {
            if defaults.len() != layout.len() {
                return Err(Error::new(
                    &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
                    format!(
                        "{quantity_name}: {} defaults supplied for {} components",
                        defaults.len(),
                        layout.len()
                    ),
                ));
            }
            let mut values = Vec::with_capacity(defaults.len() * columns.point_count);
            for _ in 0..columns.point_count {
                values.extend_from_slice(defaults);
            }
            columns.quantities.insert(
                quantity_name,
                dense_quantity(quantity_name, layout, columns.point_count, values)?,
            );
        }
        let quantity = columns
            .quantities
            .get_mut(quantity_name)
            .expect("the quantity was inserted above");
        let Some(column) = quantity.layout.column(identity) else {
            return Err(Error::new(
                &codes::BUILD_OPERATING_POINT_IDENTITY_UNKNOWN,
                format!("{quantity_name}: unknown component identity `{identity}`"),
            ));
        };
        Ok(quantity
            .storage
            .replace(self.index, column, quantity.layout.len(), replacement))
    }
}

/// Validates one dense column block against its layout.
pub(crate) fn dense_quantity(
    quantity: &'static str,
    layout: QuantityLayout,
    point_count: usize,
    values: Vec<f64>,
) -> Result<Quantity, Error> {
    let expected = point_count.checked_mul(layout.len()).ok_or_else(|| {
        Error::new(
            &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
            format!(
                "{quantity}: {point_count} points by {} elements exceeds addressable memory",
                layout.len()
            ),
        )
    })?;
    if values.len() != expected {
        return Err(Error::new(
            &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
            format!(
                "{quantity}: {} values supplied; {point_count} points by {} elements needs {expected}",
                values.len(),
                layout.len()
            ),
        ));
    }
    Ok(Quantity {
        layout,
        storage: QuantityStorage::Dense {
            values: values.into_boxed_slice(),
        },
    })
}

/// Validates one sparse column block: a base row plus per point keyed
/// overrides, resolved against the layout once.
pub(crate) fn sparse_quantity(
    quantity: &'static str,
    layout: QuantityLayout,
    point_count: usize,
    base: Vec<f64>,
    changes: Vec<Vec<(String, f64)>>,
) -> Result<Quantity, Error> {
    if base.len() != layout.len() {
        return Err(Error::new(
            &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
            format!(
                "{quantity}: base row has {} values; the layout has {} elements",
                base.len(),
                layout.len()
            ),
        ));
    }
    if changes.len() != point_count {
        return Err(Error::new(
            &codes::BUILD_OPERATING_POINT_SHAPE_MISMATCH,
            format!(
                "{quantity}: {} change sets supplied for {point_count} points",
                changes.len()
            ),
        ));
    }
    let mut resolved = Vec::with_capacity(point_count);
    for point in changes {
        let mut row: Vec<(u32, f64)> = Vec::with_capacity(point.len());
        for (identity, value) in point {
            let Some(column) = layout.column(&identity) else {
                return Err(Error::new(
                    &codes::BUILD_OPERATING_POINT_IDENTITY_UNKNOWN,
                    format!("{quantity}: unknown element identity `{identity}`"),
                ));
            };
            row.push((column as u32, value));
        }
        row.sort_unstable_by_key(|(column, _)| *column);
        row.dedup_by_key(|(column, _)| *column);
        resolved.push(row.into_boxed_slice());
    }
    Ok(Quantity {
        layout,
        storage: QuantityStorage::Sparse {
            base: base.into_boxed_slice(),
            changes: resolved.into_boxed_slice(),
        },
    })
}

/// The payload identity of one element row: its stated uid, or the
/// `{table}:{row}` value the stored document mints for a row without one.
/// Resolution never mutates the network.
pub(crate) fn row_identity(uid: Option<&str>, table: &str, row: usize) -> String {
    match uid {
        Some(uid) => uid.to_owned(),
        None => format!("{table}:{row}"),
    }
}