Skip to content

fix: preserve per-period sizes in transform.fix_sizes() for multi-period models#696

Merged
FBumann merged 1 commit into
mainfrom
fix/fix-sizes-multi-period
Jun 1, 2026
Merged

fix: preserve per-period sizes in transform.fix_sizes() for multi-period models#696
FBumann merged 1 commit into
mainfrom
fix/fix-sizes-multi-period

Conversation

@FBumann
Copy link
Copy Markdown
Member

@FBumann FBumann commented Jun 1, 2026

Fixes #695

Problem

flow_system.transform.fix_sizes() crashed with

ValueError: can only convert an array of size 1 to a Python scalar

on any multi-period model, because it collapsed every solution size to a Python scalar via float(sizes[size_var].item()). In multi-period models, investment sizes carry a period dimension (one size per period unless linked_periods is used), so .item() fails. The same applies to models with a scenario dimension.

This broke the documented two-stage workflow (size on a reduced model → fix_sizes() → dispatch at full resolution) for multi-period systems, e.g. the multi-period clustering notebook.

Fix

Keep sizes that have period/scenario dimensions as DataArrays so each coordinate retains its own fixed size; only collapse 0-d arrays to plain floats. InvestParameters.fixed_size already accepts per-period/per-scenario data (it is fitted with dims=['period', 'scenario']), so no further changes are needed downstream.

Verification

  • New regression test test_fix_sizes_preserves_per_period_sizes in tests/test_math/test_multi_period.py: a 2-period sizing problem with per-period demand peaks (50 / 80) is solved, sizes are fixed, and the dispatch stage must reproduce the same per-period sizes [50, 80] and objective (1700) — including through NetCDF save/reload roundtrips via the optimize fixture.
  • tests/test_math/test_multi_period.py: 33/33 pass.
  • Scalar dict input (fix_sizes({'Boiler(heat)': 100})) still works as before.
  • Pre-existing failures in tests/test_math/test_flow_invest.py (invest retirement / non-mandatory tests) occur on main without this change and are unrelated.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Improved multi-period and multi-scenario optimization to correctly preserve period and scenario-specific investment sizes during sizing operations, preventing loss of granular optimization results.
  • Tests

    • Added regression test verifying that per-period investment sizes and objectives are maintained across optimization stages in multi-period workflows.

…iod models

fix_sizes() collapsed every size to a Python scalar via float(...item()),
which raises ValueError on any FlowSystem with multiple periods (or
scenarios), since investment sizes then carry a period dimension.

Keep sizes with period/scenario dimensions as DataArrays so each
coordinate retains its own fixed size; only collapse 0-d arrays to
floats. InvestParameters.fixed_size already supports per-period data.

Fixes #695

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@FBumann FBumann enabled auto-merge (squash) June 1, 2026 13:03
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 1, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 18ef7878-af69-44f8-98a3-8c1d83b91808

📥 Commits

Reviewing files that changed from the base of the PR and between a825371 and 36fc283.

📒 Files selected for processing (2)
  • flixopt/transform_accessor.py
  • tests/test_math/test_multi_period.py

📝 Walkthrough

Walkthrough

TransformAccessor.fix_sizes() is updated to preserve period and scenario dimensions in size arrays instead of collapsing them to Python scalars. The docstring clarifies this behavior, the implementation conditionally handles scalar vs. non-scalar inputs, and a regression test validates per-period size preservation in a two-stage multi-period optimization workflow.

Changes

fix_sizes() Per-Period Size Preservation

Layer / File(s) Summary
fix_sizes() documentation and implementation
flixopt/transform_accessor.py
Docstring updated to clarify that period/scenario dimensions in input sizes are preserved. Implementation changes from always collapsing sizes via .item() to conditionally: scalars are converted to float, non-scalar arrays are passed through as fixed_value objects.
Multi-period regression test for fix_sizes()
tests/test_math/test_multi_period.py
New test test_fix_sizes_preserves_per_period_sizes validates a two-stage workflow: stage 1 sizes/optimizes a multi-period model with period-dependent demand, then stage 2 applies fix_sizes() and re-optimizes dispatch, asserting per-period sizes and objective are preserved.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Poem

A rabbit hops through periods bright,
With sizes saved, dimension-wise,
No collapse to scalar's plight,
Each phase gets its own size prize,
Multi-stage dreams take their flight! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main change: fixing preserve per-period sizes in the fix_sizes() function for multi-period models, which directly matches the core change in the pull request.
Description check ✅ Passed The PR description comprehensively covers the problem, fix, and verification sections with specific examples and test results, though it does not strictly follow the template's checkbox format.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/fix-sizes-multi-period

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@FBumann FBumann merged commit 54cc04a into main Jun 1, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] transform.fix_sizes() fails on multi-period models

1 participant