frequenz-microgrid-component-graph 0.5.0

A library for representing the components of a microgrid and the connections between them as a Directed Acyclic Graph (DAG).
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
// License: MIT
// Copyright © 2024 Frequenz Energy-as-a-Service GmbH

//! Methods for checking the roles of meters in a [`ComponentGraph`].

use crate::{ComponentGraph, Edge, Error, Node, component_category::CategoryPredicates};

/// Meter role identification.
impl<N, E> ComponentGraph<N, E>
where
    N: Node,
    E: Edge,
{
    /// Returns true if the node is a PV meter.
    ///
    /// A meter is identified as a PV meter if:
    ///   - it has atleast one successor,
    ///   - all its successors are PV inverters.
    pub fn is_pv_meter(&self, component_id: u64) -> Result<bool, Error> {
        let mut has_successors = false;
        Ok(self.component(component_id)?.is_meter()
            && self.successors(component_id)?.all(|n| {
                has_successors = true;
                n.is_pv_inverter()
            })
            && has_successors)
    }

    /// Returns true if the node is a battery meter.
    ///
    /// A meter is identified as a battery meter if
    ///   - it has atleast one successor,
    ///   - all its successors are battery inverters.
    pub fn is_battery_meter(&self, component_id: u64) -> Result<bool, Error> {
        let mut has_successors = false;
        Ok(self.component(component_id)?.is_meter()
            && self.successors(component_id)?.all(|n| {
                has_successors = true;
                n.is_battery_inverter(&self.config)
            })
            && has_successors)
    }

    /// Returns true if the node is an EV charger meter.
    ///
    /// A meter is identified as an EV charger meter if
    ///   - it has atleast one successor,
    ///   - all its successors are EV chargers.
    pub fn is_ev_charger_meter(&self, component_id: u64) -> Result<bool, Error> {
        let mut has_successors = false;
        Ok(self.component(component_id)?.is_meter()
            && self.successors(component_id)?.all(|n| {
                has_successors = true;
                n.is_ev_charger()
            })
            && has_successors)
    }

    /// Returns true if the node is a CHP meter.
    ///
    /// A meter is identified as a CHP meter if
    ///   - has atleast one successor,
    ///   - all its successors are CHPs.
    pub fn is_chp_meter(&self, component_id: u64) -> Result<bool, Error> {
        let mut has_successors = false;
        Ok(self.component(component_id)?.is_meter()
            && self.successors(component_id)?.all(|n| {
                has_successors = true;
                n.is_chp()
            })
            && has_successors)
    }

    /// Returns true if the node is a Wind Turbine meter.
    ///
    /// A meter is identified as a Wind Turbine meter if
    ///   - has atleast one successor
    ///   - all its successors are Wind Turbines.
    pub fn is_wind_turbine_meter(&self, component_id: u64) -> Result<bool, Error> {
        let mut has_successors = false;
        Ok(self.component(component_id)?.is_meter()
            && self.successors(component_id)?.all(|n| {
                has_successors = true;
                n.is_wind_turbine()
            })
            && has_successors)
    }

    /// Returns true if the node is a steam boiler meter.
    ///
    /// A meter is identified as a steam boiler meter if
    ///   - it has at least one successor
    ///   - all its successors are steam boilers.
    pub fn is_steam_boiler_meter(&self, component_id: u64) -> Result<bool, Error> {
        let mut has_successors = false;
        Ok(self.component(component_id)?.is_meter()
            && self.successors(component_id)?.all(|n| {
                has_successors = true;
                n.is_steam_boiler()
            })
            && has_successors)
    }

    /// Returns true if the node is a component meter.
    ///
    /// A meter is a component meter if it is one of the following:
    ///  - a PV meter,
    ///  - a battery meter,
    ///  - an EV charger meter,
    ///  - a CHP meter,
    ///  - a Wind Turbine meter,
    ///  - a Steam Boiler meter.
    pub fn is_component_meter(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_pv_meter(component_id)?
            || self.is_battery_meter(component_id)?
            || self.is_ev_charger_meter(component_id)?
            || self.is_chp_meter(component_id)?
            || self.is_wind_turbine_meter(component_id)?
            || self.is_steam_boiler_meter(component_id)?)
    }

    /// Returns true if the node is part of a battery chain.
    ///
    /// A component is part of a battery chain if it is one of the following:
    ///  - a battery meter,
    ///  - a battery inverter,
    ///  - a battery.
    pub fn is_battery_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_battery_meter(component_id)? || {
            let component = self.component(component_id)?;
            component.is_battery() || component.is_battery_inverter(&self.config)
        })
    }

    /// Returns true if the node is part of a PV chain.
    ///
    /// A component is part of a PV chain if it is one of the following:
    ///  - a PV meter,
    ///  - a PV inverter.
    pub fn is_pv_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_pv_meter(component_id)? || self.component(component_id)?.is_pv_inverter())
    }

    /// Returns true if the node is part of a CHP chain.
    ///
    /// A component is part of a CHP chain if it is one of the following:
    ///  - a CHP meter,
    ///  - a CHP.
    pub fn is_chp_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_chp_meter(component_id)? || self.component(component_id)?.is_chp())
    }

    /// Returns true if the node is part of an EV charger chain.
    ///
    /// A component is part of an EV charger chain if it is one of the following:
    ///  - an EV charger meter,
    ///  - an EV charger.
    pub fn is_ev_charger_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(
            self.is_ev_charger_meter(component_id)?
                || self.component(component_id)?.is_ev_charger(),
        )
    }

    /// Returns true if the node is part of a Wind Turbine chain.
    ///
    /// A component is part of a Wind Turbine chain if it is one of the following:
    /// - a Wind Turbine meter,
    /// - a Wind Turbine.
    pub fn is_wind_turbine_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_wind_turbine_meter(component_id)?
            || self.component(component_id)?.is_wind_turbine())
    }

    /// Returns true if the node is part of a steam boiler chain.
    ///
    /// A component is part of a steam boiler chain if it is one of the following:
    /// - a steam boiler meter,
    /// - a steam boiler.
    pub fn is_steam_boiler_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_steam_boiler_meter(component_id)?
            || self.component(component_id)?.is_steam_boiler())
    }

    /// Returns true if the node is part of a component chain.
    ///
    /// A component is part of a component chain if it is part of one of the
    /// following:
    ///  - a battery chain,
    ///  - a PV chain,
    ///  - an EV charger chain,
    ///  - a CHP chain,
    ///  - a Wind Turbine chain,
    ///  - a steam boiler chain.
    pub fn is_component_chain(&self, component_id: u64) -> Result<bool, Error> {
        Ok(self.is_battery_chain(component_id)?
            || self.is_pv_chain(component_id)?
            || self.is_ev_charger_chain(component_id)?
            || self.is_chp_chain(component_id)?
            || self.is_wind_turbine_chain(component_id)?
            || self.is_steam_boiler_chain(component_id)?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ComponentCategory;
    use crate::ComponentGraphConfig;
    use crate::InverterType;
    use crate::component_category::BatteryType;
    use crate::component_category::EvChargerType;
    use crate::error::Error;
    use crate::graph::test_utils::{TestComponent, TestConnection};

    fn nodes_and_edges() -> (Vec<TestComponent>, Vec<TestConnection>) {
        let components = vec![
            TestComponent::new(1, ComponentCategory::GridConnectionPoint),
            TestComponent::new(2, ComponentCategory::Meter),
            TestComponent::new(3, ComponentCategory::Meter),
            TestComponent::new(4, ComponentCategory::Inverter(InverterType::Battery)),
            TestComponent::new(5, ComponentCategory::Battery(BatteryType::NaIon)),
            TestComponent::new(6, ComponentCategory::Meter),
            TestComponent::new(7, ComponentCategory::Inverter(InverterType::Battery)),
            TestComponent::new(8, ComponentCategory::Battery(BatteryType::Unspecified)),
            TestComponent::new(9, ComponentCategory::Meter),
            TestComponent::new(10, ComponentCategory::Inverter(InverterType::Pv)),
            TestComponent::new(11, ComponentCategory::Inverter(InverterType::Pv)),
            TestComponent::new(12, ComponentCategory::Meter),
            TestComponent::new(13, ComponentCategory::Chp),
            TestComponent::new(14, ComponentCategory::Meter),
            TestComponent::new(15, ComponentCategory::Chp),
            TestComponent::new(16, ComponentCategory::Inverter(InverterType::Pv)),
            TestComponent::new(17, ComponentCategory::Inverter(InverterType::Battery)),
            TestComponent::new(18, ComponentCategory::Battery(BatteryType::LiIon)),
            TestComponent::new(19, ComponentCategory::Meter),
            TestComponent::new(20, ComponentCategory::SteamBoiler),
        ];
        let connections = vec![
            // Single Grid meter
            TestConnection::new(1, 2),
            // Battery chain
            TestConnection::new(2, 3),
            TestConnection::new(3, 4),
            TestConnection::new(4, 5),
            // Battery chain
            TestConnection::new(2, 6),
            TestConnection::new(6, 7),
            TestConnection::new(7, 8),
            // Solar chain
            TestConnection::new(2, 9),
            TestConnection::new(9, 10),
            TestConnection::new(9, 11),
            // CHP chain
            TestConnection::new(2, 12),
            TestConnection::new(12, 13),
            // Mixed chain
            TestConnection::new(2, 14),
            TestConnection::new(14, 15),
            TestConnection::new(14, 16),
            TestConnection::new(14, 17),
            TestConnection::new(17, 18),
            // Steam boiler chain
            TestConnection::new(2, 19),
            TestConnection::new(19, 20),
        ];

        (components, connections)
    }

    fn with_multiple_grid_meters() -> (Vec<TestComponent>, Vec<TestConnection>) {
        let (mut components, mut connections) = nodes_and_edges();

        // Add a meter to the grid without successors
        components.push(TestComponent::new(21, ComponentCategory::Meter));
        connections.push(TestConnection::new(1, 21));

        // Add a meter to the grid that has a battery meter and a PV meter as
        // successors.
        components.push(TestComponent::new(22, ComponentCategory::Meter));
        connections.push(TestConnection::new(1, 22));

        // battery chain
        components.push(TestComponent::new(23, ComponentCategory::Meter));
        components.push(TestComponent::new(
            24,
            ComponentCategory::Inverter(InverterType::Battery),
        ));
        components.push(TestComponent::new(
            25,
            ComponentCategory::Battery(BatteryType::Unspecified),
        ));
        connections.push(TestConnection::new(22, 23));
        connections.push(TestConnection::new(23, 24));
        connections.push(TestConnection::new(24, 25));

        // pv chain
        components.push(TestComponent::new(26, ComponentCategory::Meter));
        components.push(TestComponent::new(
            27,
            ComponentCategory::Inverter(InverterType::Pv),
        ));
        connections.push(TestConnection::new(22, 26));
        connections.push(TestConnection::new(26, 27));

        // steam boiler chain
        components.push(TestComponent::new(28, ComponentCategory::Meter));
        components.push(TestComponent::new(29, ComponentCategory::SteamBoiler));
        connections.push(TestConnection::new(22, 28));
        connections.push(TestConnection::new(28, 29));

        (components, connections)
    }

    fn without_grid_meters() -> (Vec<TestComponent>, Vec<TestConnection>) {
        let (mut components, mut connections) = nodes_and_edges();

        // Add an EV charger meter to the grid, then none of the meters
        // connected to the grid should be detected as grid meters.
        components.push(TestComponent::new(21, ComponentCategory::Meter));
        components.push(TestComponent::new(
            22,
            ComponentCategory::EvCharger(EvChargerType::Ac),
        ));
        connections.push(TestConnection::new(1, 21));
        connections.push(TestConnection::new(21, 22));

        (components, connections)
    }

    fn find_matching_components(
        components: Vec<TestComponent>,
        connections: Vec<TestConnection>,
        filter: impl Fn(&ComponentGraph<TestComponent, TestConnection>, u64) -> Result<bool, Error>,
    ) -> Result<Vec<u64>, Error> {
        let config = ComponentGraphConfig::default();

        let graph = ComponentGraph::try_new(components.clone(), connections.clone(), config)?;

        let mut found_meters = vec![];
        for comp in graph.components() {
            if filter(&graph, comp.component_id())? {
                found_meters.push(comp.component_id());
            }
        }

        Ok(found_meters)
    }

    #[test]
    fn test_is_pv_meter() -> Result<(), Error> {
        let (components, connections) = nodes_and_edges();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_pv_meter)?,
            vec![9],
        );

        let (components, connections) = with_multiple_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_pv_meter)?,
            vec![9, 26],
        );

        let (components, connections) = without_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_pv_meter)?,
            vec![9],
        );

        Ok(())
    }

    #[test]
    fn test_is_battery_meter() -> Result<(), Error> {
        let (components, connections) = nodes_and_edges();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_battery_meter)?,
            vec![3, 6],
        );

        let (components, connections) = with_multiple_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_battery_meter)?,
            vec![3, 6, 23],
        );

        let (components, connections) = without_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_battery_meter)?,
            vec![3, 6],
        );

        Ok(())
    }

    #[test]
    fn test_is_chp_meter() -> Result<(), Error> {
        let (components, connections) = nodes_and_edges();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_chp_meter)?,
            vec![12],
        );

        let (components, connections) = with_multiple_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_chp_meter)?,
            vec![12],
        );

        let (components, connections) = without_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_chp_meter)?,
            vec![12],
        );

        Ok(())
    }

    #[test]
    fn test_is_ev_charger_meter() -> Result<(), Error> {
        let (components, connections) = nodes_and_edges();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_ev_charger_meter)?,
            vec![],
        );

        let (components, connections) = with_multiple_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_ev_charger_meter)?,
            vec![],
        );

        let (components, connections) = without_grid_meters();
        assert_eq!(
            find_matching_components(components, connections, ComponentGraph::is_ev_charger_meter)?,
            vec![21],
        );

        Ok(())
    }

    #[test]
    fn test_is_steam_boiler_meter() -> Result<(), Error> {
        let (components, connections) = nodes_and_edges();
        assert_eq!(
            find_matching_components(
                components,
                connections,
                ComponentGraph::is_steam_boiler_meter
            )?,
            vec![19],
        );

        let (components, connections) = with_multiple_grid_meters();
        assert_eq!(
            find_matching_components(
                components,
                connections,
                ComponentGraph::is_steam_boiler_meter
            )?,
            vec![19, 28],
        );

        let (components, connections) = without_grid_meters();
        assert_eq!(
            find_matching_components(
                components,
                connections,
                ComponentGraph::is_steam_boiler_meter
            )?,
            vec![19],
        );

        Ok(())
    }
}