Skip to main content

frequenz_microgrid_component_graph/
config.rs

1// License: MIT
2// Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3
4//! This module contains the configuration options for the `ComponentGraph`.
5
6/// Configuration options for the `ComponentGraph`.
7#[derive(Clone, Debug, Default)]
8pub struct ComponentGraphConfig {
9    /// Whether to allow validation errors on components.  When this is `true`,
10    /// the graph will be built even if there are validation errors on
11    /// components.
12    pub(crate) allow_component_validation_failures: bool,
13
14    /// Whether to allow unconnected components in the graph, that are not
15    /// reachable from the root.
16    pub(crate) allow_unconnected_components: bool,
17
18    /// Whether to allow untyped inverters in the graph.  When this is `true`,
19    /// inverters that have `InverterType::Unspecified` will be assumed to be
20    /// Battery inverters.
21    pub(crate) allow_unspecified_inverters: bool,
22
23    /// Whether to disable fallback components in generated formulas.  When this
24    /// is `true`, the formulas will not include fallback components.
25    pub(crate) disable_fallback_components: bool,
26
27    /// Meters with successors can still have loads not represented in the
28    /// component graph.  These are called phantom loads.
29    ///
30    /// When this is `true`, phantom loads are included in formulas by excluding
31    /// the measurements of successor meters from the measurements of their
32    /// predecessor meters.
33    ///
34    /// When `false`, the consumer formula instead measures whole lines — the
35    /// grid reading, or the topmost reporting meters — and subtracts the
36    /// battery, PV, CHP, EV charger, wind turbine and steam boiler chains
37    /// those readings cover.
38    pub(crate) include_phantom_loads_in_consumer_formula: bool,
39
40    /// Default policy for the per-category "component" formulas.
41    ///
42    /// When `false` (the default), the component measurement is the primary
43    /// source and the meter measurement is the fallback for the per-
44    /// category formulas (`battery_formula`, `chp_formula`, `pv_formula`,
45    /// `wind_turbine_formula`, `ev_charger_formula`, `steam_boiler_formula`).
46    /// When `true`, the meter is primary and the component is the fallback.
47    ///
48    /// Per-formula overrides live in [`formula_overrides`][Self::formula_overrides].
49    ///
50    /// Has no effect on `grid_formula`, `consumer_formula`,
51    /// `producer_formula`, or any of the coalesce formulas.
52    pub(crate) prefer_meters_in_component_formulas: bool,
53
54    /// Per-formula overrides for the meter/component preference; see
55    /// [`FormulaOverrides`].
56    pub(crate) formula_overrides: FormulaOverrides,
57}
58
59impl ComponentGraphConfig {
60    /// Effective "prefer meters" setting for [`ComponentGraph::pv_formula`][cg].
61    ///
62    /// [cg]: crate::ComponentGraph::pv_formula
63    pub(crate) fn prefer_meters_in_pv_formula(&self) -> bool {
64        self.formula_overrides
65            .prefer_meters_in_pv_formula
66            .unwrap_or(self.prefer_meters_in_component_formulas)
67    }
68
69    /// Effective "prefer meters" setting for [`ComponentGraph::battery_formula`][cg].
70    ///
71    /// [cg]: crate::ComponentGraph::battery_formula
72    pub(crate) fn prefer_meters_in_battery_formula(&self) -> bool {
73        self.formula_overrides
74            .prefer_meters_in_battery_formula
75            .unwrap_or(self.prefer_meters_in_component_formulas)
76    }
77
78    /// Effective "prefer meters" setting for [`ComponentGraph::chp_formula`][cg].
79    ///
80    /// [cg]: crate::ComponentGraph::chp_formula
81    pub(crate) fn prefer_meters_in_chp_formula(&self) -> bool {
82        self.formula_overrides
83            .prefer_meters_in_chp_formula
84            .unwrap_or(self.prefer_meters_in_component_formulas)
85    }
86
87    /// Effective "prefer meters" setting for [`ComponentGraph::ev_charger_formula`][cg].
88    ///
89    /// [cg]: crate::ComponentGraph::ev_charger_formula
90    pub(crate) fn prefer_meters_in_ev_charger_formula(&self) -> bool {
91        self.formula_overrides
92            .prefer_meters_in_ev_charger_formula
93            .unwrap_or(self.prefer_meters_in_component_formulas)
94    }
95
96    /// Effective "prefer meters" setting for [`ComponentGraph::wind_turbine_formula`][cg].
97    ///
98    /// [cg]: crate::ComponentGraph::wind_turbine_formula
99    pub(crate) fn prefer_meters_in_wind_turbine_formula(&self) -> bool {
100        self.formula_overrides
101            .prefer_meters_in_wind_turbine_formula
102            .unwrap_or(self.prefer_meters_in_component_formulas)
103    }
104
105    /// Effective "prefer meters" setting for [`ComponentGraph::steam_boiler_formula`][cg].
106    ///
107    /// [cg]: crate::ComponentGraph::steam_boiler_formula
108    pub(crate) fn prefer_meters_in_steam_boiler_formula(&self) -> bool {
109        self.formula_overrides
110            .prefer_meters_in_steam_boiler_formula
111            .unwrap_or(self.prefer_meters_in_component_formulas)
112    }
113
114    /// Returns a [`ComponentGraphConfigBuilder`] initialised with all
115    /// options set to their default values.
116    pub fn builder() -> ComponentGraphConfigBuilder {
117        ComponentGraphConfigBuilder::new()
118    }
119}
120
121/// Builder for [`ComponentGraphConfig`].
122///
123/// Each method sets the corresponding option and returns `self`, so calls
124/// can be chained. Call [`build`][Self::build] to obtain the final
125/// `ComponentGraphConfig`.
126#[derive(Clone, Debug)]
127pub struct ComponentGraphConfigBuilder {
128    inner: ComponentGraphConfig,
129}
130
131impl ComponentGraphConfigBuilder {
132    /// Creates a new builder with all options set to their default values.
133    #[allow(clippy::new_without_default)]
134    pub fn new() -> Self {
135        Self {
136            inner: ComponentGraphConfig::default(),
137        }
138    }
139
140    /// When `true`, the graph is built even if per-component validation
141    /// rules fail; failures are reported as `tracing::warn!` instead of
142    /// returning an error.
143    pub fn allow_component_validation_failures(mut self, value: bool) -> Self {
144        self.inner.allow_component_validation_failures = value;
145        self
146    }
147
148    /// When `true`, components that are not reachable from the root are
149    /// permitted; otherwise the graph fails to build.
150    pub fn allow_unconnected_components(mut self, value: bool) -> Self {
151        self.inner.allow_unconnected_components = value;
152        self
153    }
154
155    /// When `true`, inverters with `InverterType::Unspecified` are
156    /// treated as battery inverters instead of being rejected.
157    pub fn allow_unspecified_inverters(mut self, value: bool) -> Self {
158        self.inner.allow_unspecified_inverters = value;
159        self
160    }
161
162    /// When `true`, generated formulas omit fallback components.
163    pub fn disable_fallback_components(mut self, value: bool) -> Self {
164        self.inner.disable_fallback_components = value;
165        self
166    }
167
168    /// Controls how the consumer formula handles meters with successors,
169    /// which can carry loads not represented in the graph (phantom loads).
170    ///
171    /// When `true`, phantom loads are included by subtracting successor
172    /// meter measurements from their predecessor meter's measurements.
173    /// When `false`, the consumer formula instead measures whole lines — the
174    /// grid reading, or the topmost reporting meters — and subtracts the
175    /// battery, PV, CHP, EV charger, wind turbine and steam boiler chains
176    /// those readings cover.
177    pub fn include_phantom_loads_in_consumer_formula(mut self, value: bool) -> Self {
178        self.inner.include_phantom_loads_in_consumer_formula = value;
179        self
180    }
181
182    /// Sets the global meter-vs-component source preference for the
183    /// per-category formulas. See the field-level docs on
184    /// [`ComponentGraphConfig`] for the exact list of affected formulas.
185    pub fn prefer_meters_in_component_formulas(mut self, value: bool) -> Self {
186        self.inner.prefer_meters_in_component_formulas = value;
187        self
188    }
189
190    /// Sets the per-formula overrides for the meter/component preference.
191    /// Each override, when `Some(_)`, takes precedence over
192    /// [`prefer_meters_in_component_formulas`][Self::prefer_meters_in_component_formulas]
193    /// for that formula.
194    pub fn formula_overrides(mut self, overrides: FormulaOverrides) -> Self {
195        self.inner.formula_overrides = overrides;
196        self
197    }
198
199    /// Consumes the builder and returns the resulting [`ComponentGraphConfig`].
200    pub fn build(self) -> ComponentGraphConfig {
201        self.inner
202    }
203}
204
205/// Per-formula overrides for the meter/component preference in the
206/// per-category formulas.
207///
208/// Each field is `None` by default, meaning the corresponding formula
209/// follows the global `prefer_meters_in_component_formulas` setting on
210/// [`ComponentGraphConfig`]. Setting an entry to `Some(true)` forces
211/// the meter as primary for that formula; `Some(false)` forces the
212/// component.
213///
214/// Construct via [`FormulaOverrides::builder`] or
215/// [`FormulaOverrides::default`].
216#[derive(Clone, Default, Debug)]
217pub struct FormulaOverrides {
218    pub(crate) prefer_meters_in_pv_formula: Option<bool>,
219    pub(crate) prefer_meters_in_battery_formula: Option<bool>,
220    pub(crate) prefer_meters_in_chp_formula: Option<bool>,
221    pub(crate) prefer_meters_in_ev_charger_formula: Option<bool>,
222    pub(crate) prefer_meters_in_wind_turbine_formula: Option<bool>,
223    pub(crate) prefer_meters_in_steam_boiler_formula: Option<bool>,
224}
225
226impl FormulaOverrides {
227    /// Returns a [`FormulaOverridesBuilder`] with no overrides set.
228    pub fn builder() -> FormulaOverridesBuilder {
229        FormulaOverridesBuilder::new()
230    }
231}
232
233/// Builder for [`FormulaOverrides`].
234#[derive(Clone, Debug)]
235pub struct FormulaOverridesBuilder {
236    inner: FormulaOverrides,
237}
238
239impl FormulaOverridesBuilder {
240    /// Creates a new builder with no overrides set.
241    #[allow(clippy::new_without_default)]
242    pub fn new() -> Self {
243        Self {
244            inner: FormulaOverrides::default(),
245        }
246    }
247
248    /// Override the meter/component preference for
249    /// [`ComponentGraph::pv_formula`][cg].
250    ///
251    /// [cg]: crate::ComponentGraph::pv_formula
252    pub fn prefer_meters_in_pv_formula(mut self, value: bool) -> Self {
253        self.inner.prefer_meters_in_pv_formula = Some(value);
254        self
255    }
256
257    /// Override the meter/component preference for
258    /// [`ComponentGraph::battery_formula`][cg].
259    ///
260    /// [cg]: crate::ComponentGraph::battery_formula
261    pub fn prefer_meters_in_battery_formula(mut self, value: bool) -> Self {
262        self.inner.prefer_meters_in_battery_formula = Some(value);
263        self
264    }
265
266    /// Override the meter/component preference for
267    /// [`ComponentGraph::chp_formula`][cg].
268    ///
269    /// [cg]: crate::ComponentGraph::chp_formula
270    pub fn prefer_meters_in_chp_formula(mut self, value: bool) -> Self {
271        self.inner.prefer_meters_in_chp_formula = Some(value);
272        self
273    }
274
275    /// Override the meter/component preference for
276    /// [`ComponentGraph::ev_charger_formula`][cg].
277    ///
278    /// [cg]: crate::ComponentGraph::ev_charger_formula
279    pub fn prefer_meters_in_ev_charger_formula(mut self, value: bool) -> Self {
280        self.inner.prefer_meters_in_ev_charger_formula = Some(value);
281        self
282    }
283
284    /// Override the meter/component preference for
285    /// [`ComponentGraph::wind_turbine_formula`][cg].
286    ///
287    /// [cg]: crate::ComponentGraph::wind_turbine_formula
288    pub fn prefer_meters_in_wind_turbine_formula(mut self, value: bool) -> Self {
289        self.inner.prefer_meters_in_wind_turbine_formula = Some(value);
290        self
291    }
292
293    /// Override the meter/component preference for
294    /// [`ComponentGraph::steam_boiler_formula`][cg].
295    ///
296    /// [cg]: crate::ComponentGraph::steam_boiler_formula
297    pub fn prefer_meters_in_steam_boiler_formula(mut self, value: bool) -> Self {
298        self.inner.prefer_meters_in_steam_boiler_formula = Some(value);
299        self
300    }
301
302    /// Consumes the builder and returns the resulting [`FormulaOverrides`].
303    pub fn build(self) -> FormulaOverrides {
304        self.inner
305    }
306}