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 EV charger formula for the graph.
68    pub fn ev_charger_formula(
69        &self,
70        ev_charger_ids: Option<BTreeSet<u64>>,
71    ) -> Result<AggregationFormula, Error> {
72        generators::ev_charger::EVChargerFormulaBuilder::try_new(self, ev_charger_ids)?.build()
73    }
74
75    /// Returns the formula for a specific component by its ID.
76    pub fn component_formula(&self, component_id: u64) -> Result<AggregationFormula, Error> {
77        Ok(Expr::component(component_id).into())
78    }
79
80    /// Returns the grid coalesce formula for the graph.
81    ///
82    /// This formula is used for non-aggregating metrics like AC voltage or
83    /// frequency.
84    ///
85    /// The formula is a `COALESCE` expression that includes all meters,
86    /// PV inverters, and battery inverters that are directly connected to the
87    /// grid.
88    pub fn grid_coalesce_formula(&self) -> Result<CoalesceFormula, Error> {
89        generators::grid_coalesce::GridCoalesceFormulaBuilder::try_new(self)?.build()
90    }
91
92    /// Returns the battery AC coalesce formula for the given components.
93    ///
94    /// This formula is used for non-aggregating metrics like AC voltage or
95    /// frequency.
96    ///
97    /// The formula is a `COALESCE` expression that includes all the specified
98    /// battery meters and corresponding inverters.
99    ///
100    /// When the `battery_ids` parameter is `None`, it will include all the
101    /// battery meters and inverters in the graph.
102    pub fn battery_ac_coalesce_formula(
103        &self,
104        battery_ids: Option<BTreeSet<u64>>,
105    ) -> Result<CoalesceFormula, Error> {
106        generators::battery_ac_coalesce::BatteryAcCoalesceFormulaBuilder::try_new(
107            self,
108            battery_ids,
109        )?
110        .build()
111    }
112
113    /// Returns the PV AC coalesce formula for the given components.
114    ///
115    /// This formula is used for non-aggregating metrics like AC voltage or
116    /// frequency.
117    ///
118    /// The formula is a `COALESCE` expression that includes all the specified
119    /// PV meters and corresponding inverters.
120    ///
121    /// When the `pv_inverter_ids` parameter is `None`, it will include all the
122    /// PV meters and inverters in the graph.
123    pub fn pv_ac_coalesce_formula(
124        &self,
125        pv_inverter_ids: Option<BTreeSet<u64>>,
126    ) -> Result<CoalesceFormula, Error> {
127        generators::pv_ac_coalesce::PVAcCoalesceFormulaBuilder::try_new(self, pv_inverter_ids)?
128            .build()
129    }
130
131    /// Returns the AC coalesce formula for a specific component by its ID.
132    pub fn component_ac_coalesce_formula(
133        &self,
134        component_id: u64,
135    ) -> Result<CoalesceFormula, Error> {
136        Ok(Expr::component(component_id).into())
137    }
138}