Skip to content

Apply storage losses to a shared stock's starting stock in its stored state of charge - #2838

Merged
Flix6x merged 2 commits into
mainfrom
fix/2837-shared-stock-soc-decay
Nov 6, 2026
Merged

Flix6x merged 2 commits into
mainfrom
fix/2837-shared-stock-soc-decay

Conversation

@Flix6x

@Flix6x Flix6x commented Nov 6, 2026

Copy link
Copy Markdown
Member

Description

Fixes the bookkeeping described in #2837.

For a shared stock (several flex-model entries on one state-of-charge sensor), StorageScheduler._build_soc_schedule integrated each device's contribution from zero, with the storage efficiency, and then added the starting stock on top without any decay.
The solver (group_stock_balance in linear_optimization.py) decays the whole stock, starting stock included.
So the stored state of charge drifted above the solver's stock by s0 * (1 - prod(eta_1..eta_k)) after k steps, could exceed soc-max, and fed that drift back into the next schedule's soc-at-start.

  • The starting stock is now integrated along with the first device's contribution (initial_stock=soc_at_start[d0] if d == d0 else 0), and the separate initial_stock + in the sum is gone.
  • All contributions now use the group's storage efficiency, taken from its first device.

Why this matches the solver exactly

group_stock_balance runs one recursion per stock group, stock[j] = a * stock[j-1] + b * change[j], with (a, b) = loss_coefficients(efficiency of the group's first device), starting from the group's initial stock, where change[j] sums the members' converted flows and stock deltas.
integrate_time_series runs the same recursion (apply_stock_changes_and_losses with how="linear", which loss_coefficients mirrors), per device, with each device's own up/down conversion efficiencies.
That recursion is linear in the initial stock and the changes, so the sum of per-device runs (one of them carrying the starting stock) equals one run over the summed changes, as long as all runs use the same storage efficiency.

Different storage efficiencies within a group: members cannot differ.
StorageScheduler resolves one storage efficiency per shared stock and assigns it to all members (raising if several entries define one),
and prepare_scheduling_problem raises if devices in one stock group have different efficiency columns.
Still, I took the efficiency from the first device for all contributions rather than from each device, so the bookkeeping reads the same value the solver reads (m.device_efficiency[d0, j]), and stays in sync should that validation ever loosen.
Per-device conversion efficiencies (charging/discharging) do differ between members, and stay per device, as in the solver's _stock_change_at.

Other consumers

  • soc_schedule_mwh (the MWh series per device) comes from the same soc_mwh, so the check of unresolved soc-minima/soc-maxima (_compute_unresolved_targets) now uses the decayed stock, too. Previously, a shared stock could report a minimum as met with an overstated SoC.
  • The soc-minima/maxima-only path (no SoC sensor) cannot hit the shared-stock branch: stock groups with more than one member are keyed by a shared state-of-charge sensor. Single-device stocks were already correct and are unchanged.
  • The % unit conversion is downstream of soc_mwh and unchanged.

How to test

Two regression tests in flexmeasures/data/models/planning/tests/test_storage.py:

  • test_shared_stock_soc_decays_like_single_device_stock: two idle devices share a stock (90% storage efficiency per 15 minutes, starting at 0.8 MWh); its stored SoC must equal 0.8 * 0.9**k, the single-device path's SoC, and the solver's own stock (read from the Pyomo model's group_stock).
  • test_shared_stock_soc_with_flows_matches_solver_stock: two devices with different charging/discharging efficiencies move a shared stock (99% storage efficiency) to two SoC targets; its stored SoC must equal the solver's stock at every step and hit the targets.

Both fail without the fix (the first stored a flat 0.8 MWh, the second drifted up to 0.06 MWh above the solver's stock), and pass with it.
The tests pin the Pyomo backend (appsi_highs), because only the Pyomo model exposes the solver's stock per group; the fix itself is backend-independent.
The planning test package passes locally (392 passed, 3 xfailed).

Question

This bug is in released versions (1.0.1 and 1.1.0). Should it be backported to 1.1.x?
I added the changelog entry under v1.2.0 for now.

Further Improvements

None planned.

Related Items

#2837


  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, this PR does not contain non-free code or data.
  • Added changelog item in documentation/changelog.rst

🤖 Generated with Claude Code

https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf

Flix6x and others added 2 commits November 6, 2026 21:39
A shared stock (two flex-model entries on one state-of-charge sensor) with a storage efficiency below 1 should store the same decaying state of charge as a single-device stock,
and the stock the solver modelled, with and without flows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
…hedule

The shared-stock branch of StorageScheduler._build_soc_schedule integrated each device's contribution from zero and added the starting stock on top,
so the starting stock never decayed, while the solver's group_stock_balance decays the whole stock.
Integrate the starting stock along with the first device's contribution instead,
and use the group's storage efficiency (that of its first device) for all contributions, as the solver does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UBBvKZkzbfEPKctwTN8SDf
Signed-off-by: F.N. Claessen <claessen@seita.nl>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 flexmeasures | 🛠️ Build #35707702 | 📁 Comparing 716510f against latest (b4dcf3b)

  🔍 Preview build  

3 files changed
± changelog.html
± _autosummary/flexmeasures.data.models.planning.storage.html
± api/v3_0.html

@Flix6x Flix6x added bug Something isn't working Scheduling labels Nov 6, 2026
@Flix6x Flix6x added this to the 1.0.3 milestone Nov 6, 2026
@Flix6x
Flix6x merged commit cdb57a7 into main Nov 6, 2026
13 checks passed
@Flix6x
Flix6x deleted the fix/2837-shared-stock-soc-decay branch November 6, 2026 22:34
Flix6x added a commit that referenced this pull request Nov 6, 2026
…stock in its stored state of charge (#2838)

(cherry picked from commit cdb57a7, leaving out its changelog entry)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v2AH6FAvKrAHvqWV3D
Signed-off-by: F.N. Claessen <felix@seita.nl>
Flix6x added a commit that referenced this pull request Nov 6, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DWv6v2AH6FAvKrAHvqWV3D
Signed-off-by: F.N. Claessen <felix@seita.nl>
@Flix6x Flix6x self-assigned this Nov 11, 2026
Flix6x added a commit that referenced this pull request Nov 11, 2026
The v1.1.1 patch release backports #2855, #2861, #2838, #2829, #2851, #2845, #2859, #2850 and #2860 to the 1.1.x branch.
Move their changelog bullets out of the v1.2.0 section into a new v1.1.1 section, worded and ordered as on 1.1.x,
so the same fixes are not listed as unreleased once 1.1.1 ships.
The bullet for #2861 takes the 1.1.x wording, which adds that Python 3.10 keeps the earlier Pillow, dnspython and filelock.
The API change log needs no split: v3.0-35 holds only #2859, which ships in v1.1.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Flix6x added a commit that referenced this pull request Nov 23, 2026
The security releases v1.1.2 and v1.0.3 ship the fixes merged here.
Add their sections between v1.2.0 and v1.1.1, worded as on the 1.1.x and
1.0.x branches (v1.0.3 also lists the backport of #2838, as on 1.0.x), so
the same fixes are not listed as unreleased once they ship.
The API change log gets v3.0-36 for these changes, and v3.0-35 gets the
date it shipped with v1.1.1. The CLI change log gets a v1.1.2 section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: F.N. Claessen <felix@seita.nl>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Scheduling

Projects

None yet

1 participant