frequenz_microgrid_component_graph/graph/
formulas.rs

1// License: MIT
2// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
4//! Methods for building formulas for various microgrid metrics.
5
6use std::collections::BTreeSet;
7
8use crate::ComponentGraph;
9use crate::Edge;
10use crate::Error;
11use crate::Node;
12
13mod expr;
14mod fallback;
15mod formula;
16mod generators;
17mod traversal;
18
19use expr::Expr;
20pub use formula::{AggregationFormula, CoalesceFormula, Formula};
21
22/// Formulas for various microgrid metrics.
23impl<N, E> ComponentGraph<N, E>
24where
25    N: Node,
26    E: Edge,
27{
28    /// Returns the consumer formula for the graph.
29    pub fn consumer_formula(&self) -> Result<AggregationFormula, Error> {
30        generators::consumer::ConsumerFormulaBuilder::try_new(self)?.build()
31    }
32
33    /// Returns the grid formula for the graph.
34    pub fn grid_formula(&self) -> Result<AggregationFormula, Error> {
35        generators::grid::GridFormulaBuilder::try_new(self)?.build()
36    }
37
38    /// Returns the producer formula for the graph.
39    pub fn producer_formula(&self) -> Result<AggregationFormula, Error> {
40        generators::producer::ProducerFormulaBuilder::try_new(self)?.build()
41    }
42
43    /// Returns the battery formula with the given battery IDs.
44    ///
45    /// If `battery_ids` is `None`, the formula will contain all batteries in
46    /// the graph.
47    pub fn battery_formula(
48        &self,
49        battery_ids: Option<BTreeSet<u64>>,
50    ) -> Result<AggregationFormula, Error> {
51        generators::battery::BatteryFormulaBuilder::try_new(self, battery_ids)?.build()
52    }
53
54    /// Returns the CHP formula for the graph.
55    pub fn chp_formula(&self, chp_ids: Option<BTreeSet<u64>>) -> Result<AggregationFormula, Error> {
56        generators::chp::CHPFormulaBuilder::try_new(self, chp_ids)?.build()
57    }
58
59    /// Returns the PV formula for the graph.
60    pub fn pv_formula(
61        &self,
62        pv_inverter_ids: Option<BTreeSet<u64>>,
63    ) -> Result<AggregationFormula, Error> {
64        generators::pv::PVFormulaBuilder::try_new(self, pv_inverter_ids)?.build()
65    }
66
67    /// Returns the wind_turbine formula for the graph.
68    pub fn wind_turbine_formula(
69        &self,
70        wind_turbine_ids: Option<BTreeSet<u64>>,
71    ) -> Result<AggregationFormula, Error> {
72        generators::wind_turbine::WindTurbineFormulaBuilder::try_new(self, wind_turbine_ids)?
73            .build()
74    }
75
76    /// Returns the EV charger formula for the graph.
77    pub fn ev_charger_formula(
78        &self,
79        ev_charger_ids: Option<BTreeSet<u64>>,
80    ) -> Result<AggregationFormula, Error> {
81        generators::ev_charger::EVChargerFormulaBuilder::try_new(self, ev_charger_ids)?.build()
82    }
83
84    /// Returns the formula for a specific component by its ID.
85    pub fn component_formula(&self, component_id: u64) -> Result<AggregationFormula, Error> {
86        Ok(Expr::component(component_id).into())
87    }
88
89    /// Returns the grid coalesce formula for the graph.
90    ///
91    /// This formula is used for non-aggregating metrics like AC voltage or
92    /// frequency.
93    ///
94    /// The formula is a `COALESCE` expression that includes all meters,
95    /// PV inverters, and battery inverters that are directly connected to the
96    /// grid.
97    pub fn grid_coalesce_formula(&self) -> Result<CoalesceFormula, Error> {
98        generators::grid_coalesce::GridCoalesceFormulaBuilder::try_new(self)?.build()
99    }
100
101    /// Returns the battery AC coalesce formula for the given components.
102    ///
103    /// This formula is used for non-aggregating metrics like AC voltage or
104    /// frequency.
105    ///
106    /// The formula is a `COALESCE` expression that includes all the specified
107    /// battery meters and corresponding inverters.
108    ///
109    /// When the `battery_ids` parameter is `None`, it will include all the
110    /// battery meters and inverters in the graph.
111    pub fn battery_ac_coalesce_formula(
112        &self,
113        battery_ids: Option<BTreeSet<u64>>,
114    ) -> Result<CoalesceFormula, Error> {
115        generators::battery_ac_coalesce::BatteryAcCoalesceFormulaBuilder::try_new(
116            self,
117            battery_ids,
118        )?
119        .build()
120    }
121
122    /// Returns the PV AC coalesce formula for the given components.
123    ///
124    /// This formula is used for non-aggregating metrics like AC voltage or
125    /// frequency.
126    ///
127    /// The formula is a `COALESCE` expression that includes all the specified
128    /// PV meters and corresponding inverters.
129    ///
130    /// When the `pv_inverter_ids` parameter is `None`, it will include all the
131    /// PV meters and inverters in the graph.
132    pub fn pv_ac_coalesce_formula(
133        &self,
134        pv_inverter_ids: Option<BTreeSet<u64>>,
135    ) -> Result<CoalesceFormula, Error> {
136        generators::pv_ac_coalesce::PVAcCoalesceFormulaBuilder::try_new(self, pv_inverter_ids)?
137            .build()
138    }
139
140    /// Returns the AC coalesce formula for a specific component by its ID.
141    pub fn component_ac_coalesce_formula(
142        &self,
143        component_id: u64,
144    ) -> Result<CoalesceFormula, Error> {
145        Ok(Expr::component(component_id).into())
146    }
147}