Skip to content
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

Fix overlapping bus regions when alternative clustering is selected #1287

Merged

Conversation

choiHenry
Copy link
Contributor

Closes # (if applicable).

Changes proposed in this Pull Request

Checklist

  • I consent to the release of this PR's code under the AGPLv3 license and non-code contributions under CC0-1.0 and CC-BY-4.0.
  • I tested my contribution locally and it seems to work fine.
  • Code and workflow changes are sufficiently documented.
  • Newly introduced dependencies are added to envs/environment.yaml and doc/requirements.txt.
  • Changes in configuration options are added in all of config.default.yaml and config.tutorial.yaml.
  • Add a test config or line additions to test/ (note tests are changing the config.tutorial.yaml)
  • Changes in configuration options are also documented in doc/configtables/*.csv and line references are adjusted in doc/configuration.rst and doc/tutorial.rst.
  • A note for the release notes doc/release_notes.rst is amended in the format of previous release notes, including reference to the requested PR.

Copy link
Member

@davide-f davide-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for the PR :)
I have proposed two minor comments related. This is a great improvement! :)
Please add a bugfix in release_note :)

In your run, could you check if in the solved network hydro technologies are available?

An easy way is to do:

n = pypsa.Network("pypsa-earth/results/networks/...")
n.statistics()

There you can see if the hydro technologies do have a positive energy dispatch?
We had reports of potential issues and the reason is the following.
While for wind/solar technology the selection of the node is not so relevant, for hydro technology it is not because they are not as distributed as solar or wind.

Note, a possible fix for the hydro issue could be available and ready to review here:
#1249

#1249 is planned for review and probably the 2 PR together can solve the alternative_clustering issues :) [review and testing is needed]

Comment on lines 263 to 267
isolated_buses = n.buses[~non_isolated_buses].index
# drop isolated buses
onshore_regions = onshore_regions[~onshore_regions.name.isin(isolated_buses)]
# drop duplicates based on shape_id
onshore_regions = onshore_regions.drop_duplicates("shape_id")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isolated_buses = n.buses[~non_isolated_buses].index
# drop isolated buses
onshore_regions = onshore_regions[~onshore_regions.name.isin(isolated_buses)]
# drop duplicates based on shape_id
onshore_regions = onshore_regions.drop_duplicates("shape_id")
non_isolated_regions = onshore_regions[~onshore_regions.name.isin(isolated_buses)]
isolated_regions = onshore_regions[onshore_regions.name.isin(isolated_buses)]
# Combine regions while prioritizing non-isolated ones
onshore_regions = (
pd.concat([non_isolated_regions, isolated_regions])
.drop_duplicates("shape_id", keep="first")
)

Could you check if this works? this also allows isolated nodes to be kept if necessary. It builds on your spirit.
The logic is that non_isolated_nodes have priority over isolated ones and isolated may be kept if no other bus is available there.
Could you check if it works?

Copy link
Contributor Author

@choiHenry choiHenry Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @davide-f. I checked and this works! I think it could add some stablility.
So this code prefers non-isolated bus but isolated one could represent a region if the region got only isolated ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The edited code be like

    if snakemake.params.alternative_clustering:
        # determine isolated buses
        n.determine_network_topology()
        non_isolated_buses = n.buses.duplicated(subset=["sub_network"], keep=False)
        isolated_buses = n.buses[~non_isolated_buses].index
        non_isolated_regions = onshore_regions[~onshore_regions.name.isin(isolated_buses)]
        isolated_regions = onshore_regions[onshore_regions.name.isin(isolated_buses)]
        
        # Combine regions while prioritizing non-isolated ones
        onshore_regions = (
            pd.concat([non_isolated_regions, isolated_regions])
            .drop_duplicates("shape_id", keep="first")
        )
        
        if len(onshore_regions) < len(gadm_country):
            logger.warning(
                f"The number of remaining of buses are less than the number of administrative clusters suggested!"
            )

logger.error(
f"The number of remaining of buses are less than the number of administrative clusters suggested!"
)
raise ValueError("Insufficient buses to match administrative clusters!")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind making it a logger.warning instead?
I understand the reason but a hard error may have a hard impact

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I was not sure whether I need to put logger.warning or logger.error. Thanks.

@choiHenry
Copy link
Contributor Author

choiHenry commented Jan 9, 2025

Many thanks for the PR :) I have proposed two minor comments related. This is a great improvement! :) Please add a bugfix in release_note :)

In your run, could you check if in the solved network hydro technologies are available?

An easy way is to do:

n = pypsa.Network("pypsa-earth/results/networks/...")
n.statistics()

There you can see if the hydro technologies do have a positive energy dispatch? We had reports of potential issues and the reason is the following. While for wind/solar technology the selection of the node is not so relevant, for hydro technology it is not because they are not as distributed as solar or wind.

Note, a possible fix for the hydro issue could be available and ready to review here: #1249

#1249 is planned for review and probably the 2 PR together can solve the alternative_clustering issues :) [review and testing is needed]

As you said @davide-f, when I checked using modified build_bus_regions rule alone, the solved network shows no "Run of River" technology.
To solve this problem, also as @davide-f noted, I proceed to merge #1249 and run again.
The run of river popped up in the solved network.

I have to say #1249 and this PR should be used together.

Screenshot 2025-01-09 at 7 46 35 PM

@davide-f
Copy link
Member

davide-f commented Jan 9, 2025

Hello @choiHenry :D
Do you mind pushing the commits you solved? :D happy to merge this PR soon :)

@choiHenry choiHenry force-pushed the AC_drop_overlapping_bus_regions branch from 5c92667 to 1eb3b34 Compare January 10, 2025 01:54
@choiHenry
Copy link
Contributor Author

choiHenry commented Jan 10, 2025

@davide-f I pushed the commit! Thanks :)

@davide-f davide-f merged commit 1f79db0 into pypsa-meets-earth:main Jan 10, 2025
4 of 5 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.

2 participants