Skip to content

KeyError: 'name-p' in add_power_limits constraint #134

@pworschischek-aggmag

Description

@pworschischek-aggmag

Version Checks (indicate both or one)

  • I have confirmed this bug exists on the lastest release of PyPSA-DE.

  • I have confirmed this bug exists on the current master branch of PyPSA-DE.

Issue Description

The PyPSA-AT workflow currently fails to apply the custom_extra_functionailty constraint during add_power_limits. See traceback below. I suspect This is a PyPSA v1.0 regression. The same reproducible example succeeds without errors using PyPSA v0.35.2. The root cause is the index.name attribute holding "name" instead of the expected Link or Line strings. A change introduced in v1.0.

var_key = f"{idx.name}-{'s' if idx.name == 'Line' else 'p'}"  # FixMe: idx.name is "name" since PyPSA v1.0
var = n.model[var_key].sel({idx.name: idx})  # FixMe: model dimension stays "name"
# suggested PR
var_key = f"{idx.name}-{'s' if idx.name == 'Line' else 'p'}"  # Fix: setting index.name to Link or Line outside of closure
var = n.model[var_key].sel({"name": idx})  # Fix: hard coded model dimension

Explicitly setting the idx.name to "Link" or "Line" fixes the indexing in n.model[var_key]. The model dimension stays "name" however, so in idx.name needs to be replaced by "name" during variable selection.

# Traceback from snakemake logs
INFO:additional_functionality:Adding constraint on electricity import/export from/to DE to be < 15000.0 MW
ERROR:root:Uncaught exception
Traceback (most recent call last):
  File "/IdeaProjects/pypsa-at/.snakemake/scripts/tmp0dg1j2ca.solve_network.py", line 1439, in <module>
    solve_network(
  File "/IdeaProjects/pypsa-at/.snakemake/scripts/tmp0dg1j2ca.solve_network.py", line 1364, in solve_network
    status, condition = n.optimize(**kwargs)
                        ^^^^^^^^^^^^^^^^^^^^
  File "/IdeaProjects/pypsa-at/.pixi/envs/default/lib/python3.12/site-packages/pypsa/optimization/optimize.py", line 473, in __call__
    extra_functionality(n, sns)
  File "/IdeaProjects/pypsa-at/.snakemake/scripts/tmp0dg1j2ca.solve_network.py", line 1248, in extra_functionality
    custom_extra_functionality(n, snapshots, snakemake)  # pylint: disable=E0601
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/IdeaProjects/pypsa-at/scripts/pypsa-de/additional_functionality.py", line 806, in additional_functionality
    add_power_limits(n, investment_year, constraints["limits_power_max"])
  File "/IdeaProjects/pypsa-at/scripts/pypsa-de/additional_functionality.py", line 175, in add_power_limits
    incoming_lines_aux_pos, incoming_lines_aux_neg = add_pos_neg_aux_variables(
                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/IdeaProjects/pypsa-at/scripts/pypsa-de/additional_functionality.py", line 128, in add_pos_neg_aux_variables
    var = n.model[var_key].sel({idx.name: idx})
          ~~~~~~~^^^^^^^^^
  File "/IdeaProjects/pypsa-at/.pixi/envs/default/lib/python3.12/site-packages/linopy/model.py", line 360, in __getitem__
    return self.variables[key]
           ~~~~~~~~~~~~~~^^^^^
  File "/IdeaProjects/pypsa-at/.pixi/envs/default/lib/python3.12/site-packages/linopy/variables.py", line 1194, in __getitem__
    return self.data[names]
           ~~~~~~~~~^^^^^^^
KeyError: 'name-p'

Unfortunately, the workflow runs into another error after solving and during n.optimize.assign_duals(assign_all_duals) in optimize.py line 478. I do not know enough about the specifics at this point, so I thought I'd raise the issue here and hope someone can either explain to me how to tackle this, or simply go ahead and fix this.

Reproducible Example

Connect your debugger to solve_network.py and set breakpoints.


# scripts/solve_network.py
if __name__ == "__main__":
    if "snakemake" not in globals():
        from scripts._helpers import mock_snakemake

        snakemake = mock_snakemake(
            "solve_sector_network_myopic",
            run="KN2040"
            opts="",
            clusters="27",
            configfiles="config/config.de.yaml",
            sector_opts="none",
            planning_horizons="2020",
        )

Expected Behavior

The constraints are applied as expected and the workflow succeeds without errors

Installed Versions

Details PyPSA-DE main 92787a4, PyPSA v1.0.0, pixi package manager

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageIssue that has not been reviewed by the maintainer

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions