Conversation
The curtailment(), capacity(), and capex() methods in the optimization expressions module now correctly handle networks with only non-extendable generators. Previously, these methods raised KeyError when accessing optimization variables that don't exist for fixed capacities. Uses comp.fixed property and comp._operational_attrs for cleaner code. Closes #1316
f0193ac to
f9f8340
Compare
Irieo
approved these changes
Dec 1, 2025
Contributor
Irieo
left a comment
There was a problem hiding this comment.
@FabianHofmann This works. I tried cases with mixed capacity (ext & non-est), things like max_hours for storage units. Don't see any issues but for an xarray warning which I mention above.
lkstrp
reviewed
Dec 2, 2025
pypsa/optimization/expressions.py
Outdated
| query = f"~{nominal_attrs[c]}_extendable" | ||
| capacity = capacity + n.c[c].static.query(query)["p_nom"] | ||
| costs = n.c[c].static[cost_attribute][capacity.indexes["name"]] | ||
| comp = n.c[c] |
Member
There was a problem hiding this comment.
The internal convention we started is c for pypsa.Component and component as parameter name to pass any string or pypsa.Component. I think we can just also rename c: str here since this is internal. I will just adjust
Contributor
Author
There was a problem hiding this comment.
sure thing, I'll do that
a-buntjer
pushed a commit
to a-buntjer/PyPSA
that referenced
this pull request
Dec 4, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
curtailment(),capacity(), andcapex()expressions returning zero for networks with only non-extendable generators.Closes #1316
Root Cause
The methods accessed optimization model variables directly (
n.model.variables[f"{c}-{attr}"]). For non-extendable generators, this variable doesn't exist since there's nothing to optimize. The@pass_none_if_keyerrordecorator caught the KeyError and returned None, resulting in zero expressions.Solution
LinearExpressionfor non-extendable components when the variable doesn't existcomp.fixedproperty andcomp._operational_attrs["nom"]for cleaner, more idiomatic codenominal_attrsimportTest Plan