|
| 1 | +// License: MIT |
| 2 | +// Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +//! This module contains the methods for generating producer formulas. |
| 5 | +
|
| 6 | +use std::collections::BTreeSet; |
| 7 | + |
| 8 | +use crate::component_category::CategoryPredicates; |
| 9 | +use crate::graph::formulas::AggregationFormula; |
| 10 | +use crate::graph::formulas::expr::Expr; |
| 11 | +use crate::graph::formulas::fallback::FallbackExpr; |
| 12 | +use crate::{ComponentGraph, Edge, Error, Node}; |
| 13 | + |
| 14 | +pub(crate) struct WindTurbineFormulaBuilder<'a, N, E> |
| 15 | +where |
| 16 | + N: Node, |
| 17 | + E: Edge, |
| 18 | +{ |
| 19 | + graph: &'a ComponentGraph<N, E>, |
| 20 | + wind_turbine_ids: BTreeSet<u64>, |
| 21 | +} |
| 22 | + |
| 23 | +impl<'a, N, E> WindTurbineFormulaBuilder<'a, N, E> |
| 24 | +where |
| 25 | + N: Node, |
| 26 | + E: Edge, |
| 27 | +{ |
| 28 | + pub fn try_new( |
| 29 | + graph: &'a ComponentGraph<N, E>, |
| 30 | + wind_turbine_ids: Option<BTreeSet<u64>>, |
| 31 | + ) -> Result<Self, Error> { |
| 32 | + let wind_turbine_ids = if let Some(wind_turbine_ids) = wind_turbine_ids { |
| 33 | + wind_turbine_ids |
| 34 | + } else { |
| 35 | + graph.find_all( |
| 36 | + graph.root_id, |
| 37 | + |node| node.is_wind_turbine(), |
| 38 | + petgraph::Direction::Outgoing, |
| 39 | + false, |
| 40 | + )? |
| 41 | + }; |
| 42 | + Ok(Self { |
| 43 | + graph, |
| 44 | + wind_turbine_ids, |
| 45 | + }) |
| 46 | + } |
| 47 | + |
| 48 | + /// Generates the wind_turbine formula. |
| 49 | + /// |
| 50 | + /// This is the sum of all wind_turbines in the graph. If the wind_turbine_ids are provided, |
| 51 | + /// only the wind_turbines with the given ids are included in the formula. |
| 52 | + pub fn build(self) -> Result<AggregationFormula, Error> { |
| 53 | + if self.wind_turbine_ids.is_empty() { |
| 54 | + return Ok(AggregationFormula::new(Expr::number(0.0))); |
| 55 | + } |
| 56 | + |
| 57 | + for id in &self.wind_turbine_ids { |
| 58 | + if !self.graph.component(*id)?.is_wind_turbine() { |
| 59 | + return Err(Error::invalid_component(format!( |
| 60 | + "Component with id {id} is not a wind turbine." |
| 61 | + ))); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + FallbackExpr { |
| 66 | + prefer_meters: false, |
| 67 | + meter_fallback_for_meters: false, |
| 68 | + } |
| 69 | + .generate(self.graph, self.wind_turbine_ids.clone()) |
| 70 | + .map(AggregationFormula::new) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use std::collections::BTreeSet; |
| 77 | + |
| 78 | + use crate::{Error, graph::test_utils::ComponentGraphBuilder}; |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn test_wind_turbine_formula() -> Result<(), Error> { |
| 82 | + let mut builder = ComponentGraphBuilder::new(); |
| 83 | + let grid = builder.grid(); |
| 84 | + |
| 85 | + let grid_meter = builder.meter(); |
| 86 | + builder.connect(grid, grid_meter); |
| 87 | + |
| 88 | + let graph = builder.build(None)?; |
| 89 | + let formula = graph.wind_turbine_formula(None)?.to_string(); |
| 90 | + assert_eq!(formula, "0.0"); |
| 91 | + |
| 92 | + // Add a wind_turbine meter with one wind turbine. |
| 93 | + let meter_wind_turbine_chain = builder.meter_wind_turbine_chain(1); |
| 94 | + builder.connect(grid_meter, meter_wind_turbine_chain); |
| 95 | + |
| 96 | + assert_eq!(grid_meter.component_id(), 1); |
| 97 | + assert_eq!(meter_wind_turbine_chain.component_id(), 2); |
| 98 | + let graph = builder.build(None)?; |
| 99 | + let formula = graph.wind_turbine_formula(None)?.to_string(); |
| 100 | + assert_eq!(formula, "COALESCE(#3, #2, 0.0)"); |
| 101 | + |
| 102 | + // Add a battery meter with one battery inverter and two batteries. |
| 103 | + let meter_bat_chain = builder.meter_bat_chain(1, 2); |
| 104 | + builder.connect(grid_meter, meter_bat_chain); |
| 105 | + |
| 106 | + assert_eq!(meter_bat_chain.component_id(), 4); |
| 107 | + |
| 108 | + let graph = builder.build(None)?; |
| 109 | + let formula = graph.wind_turbine_formula(None)?.to_string(); |
| 110 | + assert_eq!(formula, "COALESCE(#3, #2, 0.0)"); |
| 111 | + |
| 112 | + // Add a wind_turbine meter with two wind_turbine inverters. |
| 113 | + let meter_wind_turbine_chain = builder.meter_wind_turbine_chain(2); |
| 114 | + builder.connect(grid_meter, meter_wind_turbine_chain); |
| 115 | + |
| 116 | + assert_eq!(meter_wind_turbine_chain.component_id(), 8); |
| 117 | + |
| 118 | + let graph = builder.build(None)?; |
| 119 | + let formula = graph.wind_turbine_formula(None)?.to_string(); |
| 120 | + assert_eq!( |
| 121 | + formula, |
| 122 | + concat!( |
| 123 | + "COALESCE(#3, #2, 0.0) + ", |
| 124 | + "COALESCE(#10 + #9, #8, COALESCE(#10, 0.0) + COALESCE(#9, 0.0))" |
| 125 | + ) |
| 126 | + ); |
| 127 | + |
| 128 | + let formula = graph |
| 129 | + .wind_turbine_formula(Some(BTreeSet::from([10, 3])))? |
| 130 | + .to_string(); |
| 131 | + assert_eq!(formula, "COALESCE(#3, #2, 0.0) + COALESCE(#10, 0.0)"); |
| 132 | + |
| 133 | + // add a meter direct to the grid with three wind_turbine inverters |
| 134 | + let meter_wind_turbine_chain = builder.meter_wind_turbine_chain(3); |
| 135 | + builder.connect(grid, meter_wind_turbine_chain); |
| 136 | + |
| 137 | + assert_eq!(meter_wind_turbine_chain.component_id(), 11); |
| 138 | + let graph = builder.build(None)?; |
| 139 | + let formula = graph.wind_turbine_formula(None)?.to_string(); |
| 140 | + assert_eq!( |
| 141 | + formula, |
| 142 | + concat!( |
| 143 | + "COALESCE(#3, #2, 0.0) + ", |
| 144 | + "COALESCE(#10 + #9, #8, COALESCE(#10, 0.0) + COALESCE(#9, 0.0)) + ", |
| 145 | + "COALESCE(", |
| 146 | + "#14 + #13 + #12, ", |
| 147 | + "#11, ", |
| 148 | + "COALESCE(#14, 0.0) + COALESCE(#13, 0.0) + COALESCE(#12, 0.0)", |
| 149 | + ")" |
| 150 | + ), |
| 151 | + ); |
| 152 | + |
| 153 | + let formula = graph |
| 154 | + .wind_turbine_formula(Some(BTreeSet::from([3, 9, 10, 12, 13])))? |
| 155 | + .to_string(); |
| 156 | + assert_eq!( |
| 157 | + formula, |
| 158 | + concat!( |
| 159 | + "COALESCE(#3, #2, 0.0) + ", |
| 160 | + "COALESCE(#10 + #9, #8, COALESCE(#10, 0.0) + COALESCE(#9, 0.0)) + ", |
| 161 | + "COALESCE(#12, 0.0) + ", |
| 162 | + "COALESCE(#13, 0.0)" |
| 163 | + ) |
| 164 | + ); |
| 165 | + |
| 166 | + let formula = graph |
| 167 | + .wind_turbine_formula(Some(BTreeSet::from([3, 9, 10, 12, 13, 14])))? |
| 168 | + .to_string(); |
| 169 | + assert_eq!( |
| 170 | + formula, |
| 171 | + concat!( |
| 172 | + "COALESCE(#3, #2, 0.0) + ", |
| 173 | + "COALESCE(#10 + #9, #8, COALESCE(#10, 0.0) + COALESCE(#9, 0.0)) + ", |
| 174 | + "COALESCE(", |
| 175 | + "#14 + #13 + #12, ", |
| 176 | + "#11, ", |
| 177 | + "COALESCE(#14, 0.0) + COALESCE(#13, 0.0) + COALESCE(#12, 0.0)", |
| 178 | + ")" |
| 179 | + ) |
| 180 | + ); |
| 181 | + |
| 182 | + let formula = graph |
| 183 | + .wind_turbine_formula(Some(BTreeSet::from([10, 14])))? |
| 184 | + .to_string(); |
| 185 | + assert_eq!(formula, "COALESCE(#10, 0.0) + COALESCE(#14, 0.0)"); |
| 186 | + |
| 187 | + // Failure cases: |
| 188 | + let formula = graph.wind_turbine_formula(Some(BTreeSet::from([8]))); |
| 189 | + assert_eq!( |
| 190 | + formula.unwrap_err().to_string(), |
| 191 | + "InvalidComponent: Component with id 8 is not a wind turbine." |
| 192 | + ); |
| 193 | + |
| 194 | + Ok(()) |
| 195 | + } |
| 196 | +} |
0 commit comments