Skip to content

Commit 9bc80f1

Browse files
committed
Add disable_fallback_components config option
When this is `true`, formulas will be generated without considering fallback components. Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
1 parent 79e1e14 commit 9bc80f1

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ pub struct ComponentGraphConfig {
1919
/// inverters that have `InverterType::Unspecified` will be assumed to be
2020
/// Battery inverters.
2121
pub 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 disable_fallback_components: bool,
2226
}

src/graph/formulas/fallback.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ where
4545
{
4646
fn generate(&self, mut component_ids: BTreeSet<u64>) -> Result<Expr, Error> {
4747
let mut formula = None::<Expr>;
48+
if self.graph.config.disable_fallback_components {
49+
while let Some(component_id) = component_ids.pop_first() {
50+
formula = Self::add_to_option(formula, Expr::component(component_id));
51+
}
52+
return formula.ok_or(Error::internal("No components to generate formula."));
53+
}
4854
while let Some(component_id) = component_ids.pop_first() {
4955
if let Some(expr) = self.meter_fallback(component_id)? {
5056
formula = Self::add_to_option(formula, expr);
@@ -169,7 +175,7 @@ where
169175

170176
#[cfg(test)]
171177
mod tests {
172-
use crate::{graph::test_utils::ComponentGraphBuilder, Error};
178+
use crate::{graph::test_utils::ComponentGraphBuilder, ComponentGraphConfig, Error};
173179

174180
#[test]
175181
fn test_meter_fallback() -> Result<(), Error> {
@@ -197,6 +203,19 @@ mod tests {
197203
let expr = graph.fallback_expr(vec![3], true)?;
198204
assert_eq!(expr.to_string(), "COALESCE(#2, #3, 0.0)");
199205

206+
let graph = builder.build(Some(ComponentGraphConfig {
207+
disable_fallback_components: true,
208+
..Default::default()
209+
}))?;
210+
let expr = graph.fallback_expr(vec![1, 2], false)?;
211+
assert_eq!(expr.to_string(), "#1 + #2");
212+
213+
let expr = graph.fallback_expr(vec![1, 2], true)?;
214+
assert_eq!(expr.to_string(), "#1 + #2");
215+
216+
let expr = graph.fallback_expr(vec![3], true)?;
217+
assert_eq!(expr.to_string(), "#3");
218+
200219
// Add a battery meter with three inverter and three batteries
201220
let meter_bat_chain = builder.meter_bat_chain(3, 3);
202221
builder.connect(grid_meter, meter_bat_chain);
@@ -241,6 +260,22 @@ mod tests {
241260
"COALESCE(#2, #3, 0.0) + COALESCE(#7, 0.0) + COALESCE(#8, 0.0)"
242261
);
243262

263+
let graph = builder.build(Some(ComponentGraphConfig {
264+
disable_fallback_components: true,
265+
..Default::default()
266+
}))?;
267+
let expr = graph.fallback_expr(vec![3, 5], false)?;
268+
assert_eq!(expr.to_string(), "#3 + #5");
269+
270+
let expr = graph.fallback_expr(vec![2, 5], true)?;
271+
assert_eq!(expr.to_string(), "#2 + #5");
272+
273+
let expr = graph.fallback_expr(vec![2, 6, 7, 8], true)?;
274+
assert_eq!(expr.to_string(), "#2 + #6 + #7 + #8");
275+
276+
let expr = graph.fallback_expr(vec![2, 7, 8], true)?;
277+
assert_eq!(expr.to_string(), "#2 + #7 + #8");
278+
244279
let meter = builder.meter();
245280
let chp = builder.chp();
246281
let pv_inverter = builder.solar_inverter();

src/graph/formulas/generators/consumer.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ where
126126
#[cfg(test)]
127127
mod tests {
128128
use super::*;
129-
use crate::graph::test_utils::ComponentGraphBuilder;
129+
use crate::{graph::test_utils::ComponentGraphBuilder, ComponentGraphConfig};
130130

131131
#[test]
132132
fn test_zero_consumers() -> Result<(), Error> {
@@ -230,6 +230,23 @@ mod tests {
230230
"COALESCE(MAX(0.0, #11 - #8 - #9 - #10), 0.0)"
231231
)
232232
);
233+
let graph = builder.build(Some(ComponentGraphConfig {
234+
disable_fallback_components: true,
235+
..Default::default()
236+
}))?;
237+
let formula = graph.consumer_formula()?;
238+
assert_eq!(
239+
formula,
240+
concat!(
241+
// difference of grid meter from all its suceessors (without fallbacks)
242+
"MAX(0.0, #1 - #2 - #5 - #11) + ",
243+
// difference of battery meter from battery inverter and pv
244+
// meter from the two pv inverters.
245+
"COALESCE(MAX(0.0, #2 - #3), 0.0) + COALESCE(MAX(0.0, #5 - #6 - #7), 0.0) + ",
246+
// difference of "mixed" meter from its successors.
247+
"COALESCE(MAX(0.0, #11 - #8 - #9 - #10), 0.0)"
248+
)
249+
);
233250

234251
// add a battery chain to the grid meter and a dangling meter to the grid.
235252
let meter_bat_chain = builder.meter_bat_chain(1, 1);

0 commit comments

Comments
 (0)