Skip to content

Commit 2f11f0d

Browse files
Update battery pool SoC calculation (#557)
Fix the SoC calculation such that it never exceeds 100%.
2 parents 3eae96b + 83888c5 commit 2f11f0d

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828

2929
- Fix formatting issue for `Quantity` objects with zero values.
3030
- Fix formatting isuse for `Quantity` when the base value is float.inf or float.nan.
31+
- Fix clamping to 100% for the battery pool SoC scaling calculation.
3132

3233
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->

src/frequenz/sdk/timeseries/battery_pool/_metric_calculator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ def calculate(
424424
soc_scaled = (
425425
(soc - soc_lower_bound) / (soc_upper_bound - soc_lower_bound) * 100
426426
)
427-
soc_scaled = max(soc_scaled, 0)
427+
# we are clamping here because the SoC might be out of bounds
428+
soc_scaled = min(max(soc_scaled, 0), 100)
428429
timestamp = max(timestamp, metrics.timestamp)
429430
used_capacity_x100 += usable_capacity_x100 * soc_scaled
430431
total_capacity_x100 += usable_capacity_x100

src/frequenz/sdk/timeseries/battery_pool/battery_pool.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,17 @@ def consumption_power(self) -> FormulaEngine[Power]:
347347
def soc(self) -> MetricAggregator[Sample[Percentage]]:
348348
"""Fetch the normalized average weighted-by-capacity SoC values for the pool.
349349
350-
The values are normalized to the 0-100% range.
350+
The values are normalized to the 0-100% range and clamped if the SoC is out of
351+
bounds.
351352
352353
Average soc is calculated with the formula:
353354
```
354355
working_batteries: Set[BatteryData] # working batteries from the battery pool
355356
356-
soc_scaled = max(
357+
soc_scaled = min(max(
357358
0,
358359
(soc - soc_lower_bound) / (soc_upper_bound - soc_lower_bound) * 100,
359-
)
360+
), 100)
360361
used_capacity = sum(
361362
battery.usable_capacity * battery.soc_scaled
362363
for battery in working_batteries

0 commit comments

Comments
 (0)