-
Notifications
You must be signed in to change notification settings - Fork 584
fix: expressions returning zero for non-extendable generators #1451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure thing, I'll do that
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