Skip to main content

frequenz_microgrid_component_graph/
component_category.rs

1// License: MIT
2// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
4//! This module defines the `ComponentCategory` enum, which represents the
5//! category of a component.
6
7use crate::ComponentGraphConfig;
8use crate::graph_traits::Node;
9use std::fmt::Display;
10
11/// Represents the type of an inverter.
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub enum InverterType {
14    Unspecified,
15    Pv,
16    Battery,
17    Hybrid,
18}
19
20impl Display for InverterType {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            InverterType::Unspecified => write!(f, "Unspecified"),
24            InverterType::Pv => write!(f, "Pv"),
25            InverterType::Battery => write!(f, "Battery"),
26            InverterType::Hybrid => write!(f, "Hybrid"),
27        }
28    }
29}
30
31/// Represents the type of a battery.
32#[derive(Clone, Copy, Debug, PartialEq)]
33pub enum BatteryType {
34    Unspecified,
35    LiIon,
36    NaIon,
37}
38
39impl Display for BatteryType {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            BatteryType::Unspecified => write!(f, "Unspecified"),
43            BatteryType::LiIon => write!(f, "LiIon"),
44            BatteryType::NaIon => write!(f, "NaIon"),
45        }
46    }
47}
48
49/// Represents the type of an EV charger.
50#[derive(Clone, Copy, Debug, PartialEq)]
51pub enum EvChargerType {
52    Unspecified,
53    Ac,
54    Dc,
55    Hybrid,
56}
57
58impl Display for EvChargerType {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        match self {
61            EvChargerType::Unspecified => write!(f, "Unspecified"),
62            EvChargerType::Ac => write!(f, "AC"),
63            EvChargerType::Dc => write!(f, "DC"),
64            EvChargerType::Hybrid => write!(f, "Hybrid"),
65        }
66    }
67}
68
69/// Represents the category of a component.
70///
71/// Values of the underlying generated `ComponentCategory` and `ComponentType` types
72/// need to be converted to this type, so that they can be used in the
73/// `ComponentGraph`.
74#[derive(Clone, Copy, Debug, PartialEq)]
75pub enum ComponentCategory {
76    Unspecified,
77    GridConnectionPoint,
78    Meter,
79    Inverter(InverterType),
80    Converter,
81    Battery(BatteryType),
82    EvCharger(EvChargerType),
83    Breaker,
84    Precharger,
85    Chp,
86    Electrolyzer,
87    PowerTransformer,
88    Hvac,
89    Plc,
90    CryptoMiner,
91    StaticTransferSwitch,
92    UninterruptiblePowerSupply,
93    CapacitorBank,
94    WindTurbine,
95    SteamBoiler,
96}
97
98impl Display for ComponentCategory {
99    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100        match self {
101            ComponentCategory::Unspecified => write!(f, "Unspecified"),
102            ComponentCategory::GridConnectionPoint => write!(f, "GridConnectionPoint"),
103            ComponentCategory::Meter => write!(f, "Meter"),
104            ComponentCategory::Battery(battery_type) => write!(f, "Battery({battery_type})"),
105            ComponentCategory::Inverter(inverter_type) => write!(f, "{inverter_type}Inverter"),
106            ComponentCategory::EvCharger(ev_charger_type) => {
107                write!(f, "EVCharger({ev_charger_type})")
108            }
109            ComponentCategory::Converter => write!(f, "Converter"),
110            ComponentCategory::CryptoMiner => write!(f, "CryptoMiner"),
111            ComponentCategory::Electrolyzer => write!(f, "Electrolyzer"),
112            ComponentCategory::Chp => write!(f, "CHP"),
113            ComponentCategory::Precharger => write!(f, "Precharger"),
114            ComponentCategory::Hvac => write!(f, "HVAC"),
115            ComponentCategory::Breaker => write!(f, "Breaker"),
116            ComponentCategory::PowerTransformer => write!(f, "PowerTransformer"),
117            ComponentCategory::Plc => write!(f, "PLC"),
118            ComponentCategory::StaticTransferSwitch => write!(f, "StaticTransferSwitch"),
119            ComponentCategory::UninterruptiblePowerSupply => {
120                write!(f, "UninterruptiblePowerSupply")
121            }
122            ComponentCategory::CapacitorBank => write!(f, "CapacitorBank"),
123            ComponentCategory::WindTurbine => write!(f, "WindTurbine"),
124            ComponentCategory::SteamBoiler => write!(f, "SteamBoiler"),
125        }
126    }
127}
128
129impl ComponentCategory {
130    /// Returns `true` if this category is a *pass-through*: a component
131    /// that has no specific handling in the graph and should be treated
132    /// as transparent by validators and formula generators.
133    ///
134    /// Pass-through nodes can sit anywhere in the chain between handled
135    /// categories. They don't generate or consume their own measurement,
136    /// and neighbor relationships across them are evaluated by walking
137    /// past them as if they weren't present.
138    ///
139    /// `Unspecified` is a pass-through so that a component of this category
140    /// with no connections is exempt from the connectedness check and gets the
141    /// pass-through warning. It can't sit in a chain: graph construction
142    /// rejects an `Unspecified` component that has connections.
143    pub(crate) fn is_passthrough(self) -> bool {
144        use ComponentCategory as C;
145        matches!(
146            self,
147            C::Unspecified
148                | C::Converter
149                | C::Breaker
150                | C::Precharger
151                | C::Electrolyzer
152                | C::PowerTransformer
153                | C::Hvac
154                | C::Plc
155                | C::CryptoMiner
156                | C::StaticTransferSwitch
157                | C::UninterruptiblePowerSupply
158                | C::CapacitorBank
159        )
160    }
161}
162
163/// Predicates for checking the component category of a `Node`.
164pub(crate) trait CategoryPredicates: Node {
165    fn is_unspecified(&self) -> bool {
166        self.category() == ComponentCategory::Unspecified
167    }
168
169    fn is_grid(&self) -> bool {
170        self.category() == ComponentCategory::GridConnectionPoint
171    }
172
173    fn is_meter(&self) -> bool {
174        self.category() == ComponentCategory::Meter
175    }
176
177    fn is_battery(&self) -> bool {
178        matches!(self.category(), ComponentCategory::Battery(_))
179    }
180
181    fn is_inverter(&self) -> bool {
182        matches!(self.category(), ComponentCategory::Inverter(_))
183    }
184
185    fn is_battery_inverter(&self, config: &ComponentGraphConfig) -> bool {
186        match self.category() {
187            ComponentCategory::Inverter(InverterType::Battery) => true,
188            ComponentCategory::Inverter(InverterType::Unspecified) => {
189                config.allow_unspecified_inverters
190            }
191            _ => false,
192        }
193    }
194
195    fn is_pv_inverter(&self) -> bool {
196        self.category() == ComponentCategory::Inverter(InverterType::Pv)
197    }
198
199    fn is_hybrid_inverter(&self) -> bool {
200        self.category() == ComponentCategory::Inverter(InverterType::Hybrid)
201    }
202
203    fn is_unspecified_inverter(&self, config: &ComponentGraphConfig) -> bool {
204        match self.category() {
205            ComponentCategory::Inverter(InverterType::Unspecified) => {
206                !config.allow_unspecified_inverters
207            }
208            _ => false,
209        }
210    }
211
212    fn is_ev_charger(&self) -> bool {
213        matches!(self.category(), ComponentCategory::EvCharger(_))
214    }
215
216    fn is_chp(&self) -> bool {
217        self.category() == ComponentCategory::Chp
218    }
219
220    fn is_wind_turbine(&self) -> bool {
221        self.category() == ComponentCategory::WindTurbine
222    }
223
224    fn is_steam_boiler(&self) -> bool {
225        self.category() == ComponentCategory::SteamBoiler
226    }
227
228    /// Returns `true` if the component provides telemetry data, and so can be a
229    /// measurement source in a formula.
230    ///
231    /// Determined by the component's [operational mode][Node::operational_mode].
232    /// A component that does not provide telemetry is still used to classify the
233    /// meter that measures it (e.g. as a PV meter or a CHP meter).
234    fn provides_telemetry(&self) -> bool {
235        self.operational_mode().provides_telemetry()
236    }
237}
238
239/// Implement the `CategoryPredicates` trait for all types that implement the
240/// `Node` trait.
241impl<T: Node> CategoryPredicates for T {}
242
243#[cfg(test)]
244mod tests {
245    use super::*;
246
247    /// Locks the pass-through classification. If a category is added or
248    /// re-classified, this test forces an explicit decision rather than
249    /// silently inheriting the default.
250    #[test]
251    fn test_is_passthrough_classification() {
252        // Pass-through categories: no specific validation rule, no formula
253        // contribution; transparent to traversal. `Unspecified` has one rule:
254        // it can't have connections.
255        for c in [
256            ComponentCategory::Unspecified,
257            ComponentCategory::Converter,
258            ComponentCategory::Breaker,
259            ComponentCategory::Precharger,
260            ComponentCategory::Electrolyzer,
261            ComponentCategory::PowerTransformer,
262            ComponentCategory::Hvac,
263            ComponentCategory::Plc,
264            ComponentCategory::CryptoMiner,
265            ComponentCategory::StaticTransferSwitch,
266            ComponentCategory::UninterruptiblePowerSupply,
267            ComponentCategory::CapacitorBank,
268        ] {
269            assert!(c.is_passthrough(), "{c} should be a pass-through");
270        }
271
272        // Handled or special-cased categories.
273        for c in [
274            ComponentCategory::GridConnectionPoint,
275            ComponentCategory::Meter,
276            ComponentCategory::Inverter(InverterType::Battery),
277            ComponentCategory::Inverter(InverterType::Pv),
278            ComponentCategory::Inverter(InverterType::Hybrid),
279            ComponentCategory::Inverter(InverterType::Unspecified),
280            ComponentCategory::Battery(BatteryType::LiIon),
281            ComponentCategory::Battery(BatteryType::NaIon),
282            ComponentCategory::Battery(BatteryType::Unspecified),
283            ComponentCategory::EvCharger(EvChargerType::Ac),
284            ComponentCategory::EvCharger(EvChargerType::Dc),
285            ComponentCategory::EvCharger(EvChargerType::Hybrid),
286            ComponentCategory::EvCharger(EvChargerType::Unspecified),
287            ComponentCategory::Chp,
288            ComponentCategory::WindTurbine,
289        ] {
290            assert!(!c.is_passthrough(), "{c} should not be a pass-through");
291        }
292    }
293}