surge-network 0.1.8

Surge network — canonical power-system domain model
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
// SPDX-License-Identifier: LicenseRef-PolyForm-Noncommercial-1.0.0
//! Canonical HVDC domain model.
//!
//! This module is the only public HVDC surface for [`crate::network::Network`].
//! Source-format specific records stay out of the canonical network model.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};

pub use crate::network::dc_line::{LccConverterTerminal, LccHvdcControlMode, LccHvdcLink};
pub use crate::network::dc_network_types::{DcBranch, DcBus, DcConverterStation};
pub use crate::network::vsc_dc_line::{
    VscConverterAcControlMode, VscConverterTerminal, VscHvdcControlMode, VscHvdcLink,
};

/// A point-to-point HVDC link.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "technology", rename_all = "snake_case")]
pub enum HvdcLink {
    Lcc(LccHvdcLink),
    Vsc(VscHvdcLink),
}

impl HvdcLink {
    /// Stable user-facing name for the link.
    pub fn name(&self) -> &str {
        match self {
            Self::Lcc(link) => &link.name,
            Self::Vsc(link) => &link.name,
        }
    }

    /// Whether the link is blocked / out of service.
    pub fn is_blocked(&self) -> bool {
        match self {
            Self::Lcc(link) => link.mode == LccHvdcControlMode::Blocked,
            Self::Vsc(link) => link.mode == VscHvdcControlMode::Blocked,
        }
    }

    pub fn as_lcc(&self) -> Option<&LccHvdcLink> {
        match self {
            Self::Lcc(link) => Some(link),
            Self::Vsc(_) => None,
        }
    }

    pub fn as_lcc_mut(&mut self) -> Option<&mut LccHvdcLink> {
        match self {
            Self::Lcc(link) => Some(link),
            Self::Vsc(_) => None,
        }
    }

    pub fn as_vsc(&self) -> Option<&VscHvdcLink> {
        match self {
            Self::Lcc(_) => None,
            Self::Vsc(link) => Some(link),
        }
    }

    pub fn as_vsc_mut(&mut self) -> Option<&mut VscHvdcLink> {
        match self {
            Self::Lcc(_) => None,
            Self::Vsc(link) => Some(link),
        }
    }
}

/// Converter role within an LCC DC grid.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum LccDcConverterRole {
    #[default]
    Rectifier,
    Inverter,
}

/// Canonical LCC converter record for explicit DC-grid topology.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LccDcConverter {
    /// Stable converter identifier within the enclosing DC grid.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub id: String,
    /// DC bus number this converter connects to.
    pub dc_bus: u32,
    /// AC bus number this converter connects to.
    pub ac_bus: u32,
    /// Number of 6-pulse bridges.
    #[serde(alias = "num_bridges")]
    pub n_bridges: u32,
    /// Maximum firing / extinction angle in degrees.
    pub alpha_max_deg: f64,
    /// Minimum firing / extinction angle in degrees.
    pub alpha_min_deg: f64,
    /// Minimum extinction angle in inverter mode.
    pub gamma_min_deg: f64,
    /// Commutating resistance per bridge in ohms.
    pub commutation_resistance_ohm: f64,
    /// Commutating reactance per bridge in ohms.
    pub commutation_reactance_ohm: f64,
    /// Converter transformer rated AC voltage on the network side.
    pub base_voltage_kv: f64,
    /// Transformer turns ratio.
    pub turns_ratio: f64,
    /// Off-nominal tap ratio.
    pub tap_ratio: f64,
    /// Maximum tap ratio.
    pub tap_max: f64,
    /// Minimum tap ratio.
    pub tap_min: f64,
    /// Tap step size.
    pub tap_step: f64,
    /// Scheduled power (MW) or current (kA) setpoint.
    pub scheduled_setpoint: f64,
    /// Share of total DC power assigned to this converter.
    pub power_share_percent: f64,
    /// Current margin percentage.
    pub current_margin_percent: f64,
    /// Rectifier or inverter role.
    pub role: LccDcConverterRole,
    /// Converter in-service flag.
    pub in_service: bool,
}

impl Default for LccDcConverter {
    fn default() -> Self {
        Self {
            id: String::new(),
            dc_bus: 0,
            ac_bus: 0,
            n_bridges: 1,
            alpha_max_deg: 90.0,
            alpha_min_deg: 5.0,
            gamma_min_deg: 15.0,
            commutation_resistance_ohm: 0.0,
            commutation_reactance_ohm: 0.0,
            base_voltage_kv: 0.0,
            turns_ratio: 1.0,
            tap_ratio: 1.0,
            tap_max: 1.1,
            tap_min: 0.9,
            tap_step: 0.00625,
            scheduled_setpoint: 0.0,
            power_share_percent: 0.0,
            current_margin_percent: 0.0,
            role: LccDcConverterRole::Rectifier,
            in_service: true,
        }
    }
}

/// Canonical DC-grid converter.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "technology", rename_all = "snake_case")]
pub enum DcConverter {
    Lcc(LccDcConverter),
    Vsc(DcConverterStation),
}

impl From<LccDcConverter> for DcConverter {
    fn from(value: LccDcConverter) -> Self {
        Self::Lcc(value)
    }
}

impl From<DcConverterStation> for DcConverter {
    fn from(value: DcConverterStation) -> Self {
        Self::Vsc(value)
    }
}

impl DcConverter {
    pub fn id(&self) -> &str {
        match self {
            Self::Lcc(converter) => &converter.id,
            Self::Vsc(converter) => &converter.id,
        }
    }

    pub fn id_mut(&mut self) -> &mut String {
        match self {
            Self::Lcc(converter) => &mut converter.id,
            Self::Vsc(converter) => &mut converter.id,
        }
    }

    pub fn ac_bus(&self) -> u32 {
        match self {
            Self::Lcc(converter) => converter.ac_bus,
            Self::Vsc(converter) => converter.ac_bus,
        }
    }

    pub fn dc_bus(&self) -> u32 {
        match self {
            Self::Lcc(converter) => converter.dc_bus,
            Self::Vsc(converter) => converter.dc_bus,
        }
    }

    pub fn is_in_service(&self) -> bool {
        match self {
            Self::Lcc(converter) => converter.in_service,
            Self::Vsc(converter) => converter.status,
        }
    }

    pub fn is_lcc(&self) -> bool {
        matches!(self, Self::Lcc(_))
    }

    pub fn as_lcc(&self) -> Option<&LccDcConverter> {
        match self {
            Self::Lcc(converter) => Some(converter),
            Self::Vsc(_) => None,
        }
    }

    pub fn as_lcc_mut(&mut self) -> Option<&mut LccDcConverter> {
        match self {
            Self::Lcc(converter) => Some(converter),
            Self::Vsc(_) => None,
        }
    }

    pub fn as_vsc(&self) -> Option<&DcConverterStation> {
        match self {
            Self::Lcc(_) => None,
            Self::Vsc(converter) => Some(converter),
        }
    }

    pub fn as_vsc_mut(&mut self) -> Option<&mut DcConverterStation> {
        match self {
            Self::Lcc(_) => None,
            Self::Vsc(converter) => Some(converter),
        }
    }

    pub fn ac_bus_mut(&mut self) -> &mut u32 {
        match self {
            Self::Lcc(converter) => &mut converter.ac_bus,
            Self::Vsc(converter) => &mut converter.ac_bus,
        }
    }

    pub fn dc_bus_mut(&mut self) -> &mut u32 {
        match self {
            Self::Lcc(converter) => &mut converter.dc_bus,
            Self::Vsc(converter) => &mut converter.dc_bus,
        }
    }
}

/// Explicit DC-grid topology.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DcGrid {
    /// Stable grid identifier within the network.
    pub id: u32,
    /// Optional user-facing name when the source format provides one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// DC buses in this grid.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub buses: Vec<DcBus>,
    /// DC-grid converters in this grid.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub converters: Vec<DcConverter>,
    /// DC branches in this grid.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub branches: Vec<DcBranch>,
}

impl DcGrid {
    pub fn new(id: u32, name: Option<String>) -> Self {
        Self {
            id,
            name,
            buses: Vec::new(),
            converters: Vec::new(),
            branches: Vec::new(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.buses.is_empty() && self.converters.is_empty() && self.branches.is_empty()
    }

    pub fn find_bus(&self, bus_id: u32) -> Option<&DcBus> {
        self.buses.iter().find(|bus| bus.bus_id == bus_id)
    }

    pub fn find_bus_mut(&mut self, bus_id: u32) -> Option<&mut DcBus> {
        self.buses.iter_mut().find(|bus| bus.bus_id == bus_id)
    }

    pub fn bus_index_map(&self) -> HashMap<u32, usize> {
        self.buses
            .iter()
            .enumerate()
            .map(|(index, bus)| (bus.bus_id, index))
            .collect()
    }

    pub fn canonicalize_converter_ids(&mut self) {
        for (index, converter) in self.converters.iter_mut().enumerate() {
            let trimmed = converter.id().trim().to_string();
            if trimmed.is_empty() {
                *converter.id_mut() = format!("dc_grid_{}_converter_{}", self.id, index + 1);
            } else if trimmed != converter.id() {
                *converter.id_mut() = trimmed;
            }
        }
    }

    pub fn canonicalize_branch_ids(&mut self) {
        for (index, branch) in self.branches.iter_mut().enumerate() {
            let trimmed = branch.id.trim().to_string();
            if trimmed.is_empty() {
                branch.id = format!("dc_grid_{}_branch_{}", self.id, index + 1);
            } else if trimmed != branch.id {
                branch.id = trimmed;
            }
        }
    }
}

/// Canonical HVDC namespace on [`crate::network::Network`].
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HvdcModel {
    /// Point-to-point HVDC links.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub links: Vec<HvdcLink>,
    /// Explicit DC grids.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dc_grids: Vec<DcGrid>,
}

impl HvdcModel {
    pub fn is_empty(&self) -> bool {
        self.links.is_empty() && self.dc_grids.iter().all(DcGrid::is_empty)
    }

    pub fn has_point_to_point_links(&self) -> bool {
        !self.links.is_empty()
    }

    pub fn has_explicit_dc_topology(&self) -> bool {
        self.dc_grids.iter().any(|grid| !grid.is_empty())
    }

    pub fn push_link(&mut self, link: HvdcLink) {
        self.links.push(link);
    }

    pub fn push_lcc_link(&mut self, link: LccHvdcLink) {
        self.links.push(HvdcLink::Lcc(link));
    }

    pub fn push_vsc_link(&mut self, link: VscHvdcLink) {
        self.links.push(HvdcLink::Vsc(link));
    }

    pub fn ensure_dc_grid(&mut self, id: u32, name: Option<String>) -> &mut DcGrid {
        if let Some(index) = self.dc_grids.iter().position(|grid| grid.id == id) {
            let grid = &mut self.dc_grids[index];
            if grid.name.is_none() {
                grid.name = name;
            }
            return grid;
        }
        self.dc_grids.push(DcGrid::new(id, name));
        self.dc_grids.last_mut().expect("grid just inserted")
    }

    pub fn find_dc_grid(&self, id: u32) -> Option<&DcGrid> {
        self.dc_grids.iter().find(|grid| grid.id == id)
    }

    pub fn find_dc_grid_mut(&mut self, id: u32) -> Option<&mut DcGrid> {
        self.dc_grids.iter_mut().find(|grid| grid.id == id)
    }

    pub fn find_dc_grid_by_bus(&self, bus_id: u32) -> Option<&DcGrid> {
        self.dc_grids
            .iter()
            .find(|grid| grid.buses.iter().any(|bus| bus.bus_id == bus_id))
    }

    pub fn find_dc_grid_by_bus_mut(&mut self, bus_id: u32) -> Option<&mut DcGrid> {
        self.dc_grids
            .iter_mut()
            .find(|grid| grid.buses.iter().any(|bus| bus.bus_id == bus_id))
    }

    pub fn find_dc_bus(&self, bus_id: u32) -> Option<&DcBus> {
        self.dc_grids.iter().find_map(|grid| grid.find_bus(bus_id))
    }

    pub fn find_dc_bus_mut(&mut self, bus_id: u32) -> Option<&mut DcBus> {
        self.dc_grids
            .iter_mut()
            .find_map(|grid| grid.find_bus_mut(bus_id))
    }

    pub fn dc_bus_count(&self) -> usize {
        self.dc_grids.iter().map(|grid| grid.buses.len()).sum()
    }

    pub fn dc_converter_count(&self) -> usize {
        self.dc_grids.iter().map(|grid| grid.converters.len()).sum()
    }

    pub fn dc_branch_count(&self) -> usize {
        self.dc_grids.iter().map(|grid| grid.branches.len()).sum()
    }

    pub fn next_dc_grid_id(&self) -> u32 {
        self.dc_grids.iter().map(|grid| grid.id).max().unwrap_or(0) + 1
    }

    pub fn next_dc_bus_id(&self) -> u32 {
        self.dc_grids
            .iter()
            .flat_map(|grid| grid.buses.iter().map(|bus| bus.bus_id))
            .max()
            .unwrap_or(0)
            + 1
    }

    pub fn clear_dc_grids(&mut self) {
        self.dc_grids.clear();
    }

    pub fn canonicalize_converter_ids(&mut self) {
        for grid in &mut self.dc_grids {
            grid.canonicalize_converter_ids();
            grid.canonicalize_branch_ids();
        }
    }

    pub fn dc_buses(&self) -> impl Iterator<Item = &DcBus> {
        self.dc_grids.iter().flat_map(|grid| grid.buses.iter())
    }

    pub fn dc_buses_mut(&mut self) -> impl Iterator<Item = &mut DcBus> {
        self.dc_grids
            .iter_mut()
            .flat_map(|grid| grid.buses.iter_mut())
    }

    pub fn dc_converters(&self) -> impl Iterator<Item = &DcConverter> {
        self.dc_grids.iter().flat_map(|grid| grid.converters.iter())
    }

    pub fn dc_converters_mut(&mut self) -> impl Iterator<Item = &mut DcConverter> {
        self.dc_grids
            .iter_mut()
            .flat_map(|grid| grid.converters.iter_mut())
    }

    pub fn dc_branches(&self) -> impl Iterator<Item = &DcBranch> {
        self.dc_grids.iter().flat_map(|grid| grid.branches.iter())
    }

    pub fn dc_branches_mut(&mut self) -> impl Iterator<Item = &mut DcBranch> {
        self.dc_grids
            .iter_mut()
            .flat_map(|grid| grid.branches.iter_mut())
    }
}