fix: preserve per-period sizes in transform.fix_sizes() for multi-period models#696
Conversation
…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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
Changesfix_sizes() Per-Period Size Preservation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Fixes #695
Problem
flow_system.transform.fix_sizes()crashed withon 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 aperioddimension (one size per period unlesslinked_periodsis used), so.item()fails. The same applies to models with ascenariodimension.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_sizealready accepts per-period/per-scenario data (it is fitted withdims=['period', 'scenario']), so no further changes are needed downstream.Verification
test_fix_sizes_preserves_per_period_sizesintests/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 theoptimizefixture.tests/test_math/test_multi_period.py: 33/33 pass.fix_sizes({'Boiler(heat)': 100})) still works as before.tests/test_math/test_flow_invest.py(invest retirement / non-mandatory tests) occur onmainwithout this change and are unrelated.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests