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 /// [Pass-through components](crate#pass-through-components) never have to
151 /// be reachable.
152 pub fn allow_unconnected_components(mut self, value: bool) -> Self {
153 self.inner.allow_unconnected_components = value;
154 self
155 }
156
157 /// When `true`, inverters with `InverterType::Unspecified` are
158 /// treated as battery inverters instead of being rejected.
159 pub fn allow_unspecified_inverters(mut self, value: bool) -> Self {
160 self.inner.allow_unspecified_inverters = value;
161 self
162 }
163
164 /// When `true`, generated formulas omit fallback components.
165 pub fn disable_fallback_components(mut self, value: bool) -> Self {
166 self.inner.disable_fallback_components = value;
167 self
168 }
169
170 /// Controls how the consumer formula handles meters with successors,
171 /// which can carry loads not represented in the graph (phantom loads).
172 ///
173 /// When `true`, phantom loads are included by subtracting successor
174 /// meter measurements from their predecessor meter's measurements.
175 /// When `false`, the consumer formula instead measures whole lines — the
176 /// grid reading, or the topmost reporting meters — and subtracts the
177 /// battery, PV, CHP, EV charger, wind turbine and steam boiler chains
178 /// those readings cover.
179 pub fn include_phantom_loads_in_consumer_formula(mut self, value: bool) -> Self {
180 self.inner.include_phantom_loads_in_consumer_formula = value;
181 self
182 }
183
184 /// Sets the global meter-vs-component source preference for the
185 /// per-category formulas. See the field-level docs on
186 /// [`ComponentGraphConfig`] for the exact list of affected formulas.
187 pub fn prefer_meters_in_component_formulas(mut self, value: bool) -> Self {
188 self.inner.prefer_meters_in_component_formulas = value;
189 self
190 }
191
192 /// Sets the per-formula overrides for the meter/component preference.
193 /// Each override, when `Some(_)`, takes precedence over
194 /// [`prefer_meters_in_component_formulas`][Self::prefer_meters_in_component_formulas]
195 /// for that formula.
196 pub fn formula_overrides(mut self, overrides: FormulaOverrides) -> Self {
197 self.inner.formula_overrides = overrides;
198 self
199 }
200
201 /// Consumes the builder and returns the resulting [`ComponentGraphConfig`].
202 pub fn build(self) -> ComponentGraphConfig {
203 self.inner
204 }
205}
206
207/// Per-formula overrides for the meter/component preference in the
208/// per-category formulas.
209///
210/// Each field is `None` by default, meaning the corresponding formula
211/// follows the global `prefer_meters_in_component_formulas` setting on
212/// [`ComponentGraphConfig`]. Setting an entry to `Some(true)` forces
213/// the meter as primary for that formula; `Some(false)` forces the
214/// component.
215///
216/// Construct via [`FormulaOverrides::builder`] or
217/// [`FormulaOverrides::default`].
218#[derive(Clone, Default, Debug)]
219pub struct FormulaOverrides {
220 pub(crate) prefer_meters_in_pv_formula: Option<bool>,
221 pub(crate) prefer_meters_in_battery_formula: Option<bool>,
222 pub(crate) prefer_meters_in_chp_formula: Option<bool>,
223 pub(crate) prefer_meters_in_ev_charger_formula: Option<bool>,
224 pub(crate) prefer_meters_in_wind_turbine_formula: Option<bool>,
225 pub(crate) prefer_meters_in_steam_boiler_formula: Option<bool>,
226}
227
228impl FormulaOverrides {
229 /// Returns a [`FormulaOverridesBuilder`] with no overrides set.
230 pub fn builder() -> FormulaOverridesBuilder {
231 FormulaOverridesBuilder::new()
232 }
233}
234
235/// Builder for [`FormulaOverrides`].
236#[derive(Clone, Debug)]
237pub struct FormulaOverridesBuilder {
238 inner: FormulaOverrides,
239}
240
241impl FormulaOverridesBuilder {
242 /// Creates a new builder with no overrides set.
243 #[allow(clippy::new_without_default)]
244 pub fn new() -> Self {
245 Self {
246 inner: FormulaOverrides::default(),
247 }
248 }
249
250 /// Override the meter/component preference for
251 /// [`ComponentGraph::pv_formula`][cg].
252 ///
253 /// [cg]: crate::ComponentGraph::pv_formula
254 pub fn prefer_meters_in_pv_formula(mut self, value: bool) -> Self {
255 self.inner.prefer_meters_in_pv_formula = Some(value);
256 self
257 }
258
259 /// Override the meter/component preference for
260 /// [`ComponentGraph::battery_formula`][cg].
261 ///
262 /// [cg]: crate::ComponentGraph::battery_formula
263 pub fn prefer_meters_in_battery_formula(mut self, value: bool) -> Self {
264 self.inner.prefer_meters_in_battery_formula = Some(value);
265 self
266 }
267
268 /// Override the meter/component preference for
269 /// [`ComponentGraph::chp_formula`][cg].
270 ///
271 /// [cg]: crate::ComponentGraph::chp_formula
272 pub fn prefer_meters_in_chp_formula(mut self, value: bool) -> Self {
273 self.inner.prefer_meters_in_chp_formula = Some(value);
274 self
275 }
276
277 /// Override the meter/component preference for
278 /// [`ComponentGraph::ev_charger_formula`][cg].
279 ///
280 /// [cg]: crate::ComponentGraph::ev_charger_formula
281 pub fn prefer_meters_in_ev_charger_formula(mut self, value: bool) -> Self {
282 self.inner.prefer_meters_in_ev_charger_formula = Some(value);
283 self
284 }
285
286 /// Override the meter/component preference for
287 /// [`ComponentGraph::wind_turbine_formula`][cg].
288 ///
289 /// [cg]: crate::ComponentGraph::wind_turbine_formula
290 pub fn prefer_meters_in_wind_turbine_formula(mut self, value: bool) -> Self {
291 self.inner.prefer_meters_in_wind_turbine_formula = Some(value);
292 self
293 }
294
295 /// Override the meter/component preference for
296 /// [`ComponentGraph::steam_boiler_formula`][cg].
297 ///
298 /// [cg]: crate::ComponentGraph::steam_boiler_formula
299 pub fn prefer_meters_in_steam_boiler_formula(mut self, value: bool) -> Self {
300 self.inner.prefer_meters_in_steam_boiler_formula = Some(value);
301 self
302 }
303
304 /// Consumes the builder and returns the resulting [`FormulaOverrides`].
305 pub fn build(self) -> FormulaOverrides {
306 self.inner
307 }
308}