frequenz_microgrid_component_graph/
graph.rs

1// License: MIT
2// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
4//! A graph representation of the electrical components that are part of a
5//! microgrid, and the connections between them.
6
7mod creation;
8mod meter_roles;
9mod retrieval;
10mod validation;
11
12mod formulas;
13pub mod iterators;
14
15use crate::{ComponentGraphConfig, Edge, Node};
16pub use formulas::{AggregationFormula, CoalesceFormula, Formula};
17use petgraph::graph::{DiGraph, NodeIndex};
18use std::collections::HashMap;
19
20/// `Node`s stored in a `DiGraph` instance can be addressed with `NodeIndex`es.
21///
22/// `NodeIndexMap` stores the corresponding `NodeIndex` for any `component_id`, so
23/// that Nodes in the `DiGraph` can be retrieved from their `component_id`s.
24pub(crate) type NodeIndexMap = HashMap<u64, NodeIndex>;
25
26/// `Edge`s are not stored in the `DiGraph` instance, so we need to store them
27/// separately.
28///
29/// `EdgeMap` can be used to lookup the `Edge` for any pair of source and
30/// destination `NodeIndex` values.
31pub(crate) type EdgeMap<E> = HashMap<(NodeIndex, NodeIndex), E>;
32
33/// A graph representation of the electrical components of a microgrid and the
34/// connections between them.
35pub struct ComponentGraph<N, E>
36where
37    N: Node,
38    E: Edge,
39{
40    graph: DiGraph<N, ()>,
41    node_indices: NodeIndexMap,
42    root_id: u64,
43    edges: EdgeMap<E>,
44    config: ComponentGraphConfig,
45}
46
47/// Implement `Clone` for `ComponentGraph` when the `Node`s and `Edge`s
48/// implement `Clone`.
49impl<N, E> Clone for ComponentGraph<N, E>
50where
51    N: Node + Clone,
52    E: Edge + Clone,
53{
54    fn clone(&self) -> Self {
55        Self {
56            graph: self.graph.clone(),
57            node_indices: self.node_indices.clone(),
58            root_id: self.root_id,
59            edges: self.edges.clone(),
60            config: self.config.clone(),
61        }
62    }
63}
64
65#[cfg(test)]
66mod test_utils;