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

Linopy Integration #351

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Conversation

GbotemiB
Copy link

@GbotemiB GbotemiB commented Jun 24, 2024

Closes # (if applicable).

Changes proposed in this Pull Request

Integrate linopy following PyPSA/pypsa-eur#625 and pypsa-meets-earth/pypsa-earth#796

Checklist

  • 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 envs/environment.docs.yaml.
  • Changes in configuration options are added in all of config.default.yaml, config.tutorial.yaml, and test/config.test1.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.

@GbotemiB GbotemiB marked this pull request as draft June 24, 2024 21:48
@ekatef
Copy link
Collaborator

ekatef commented Jun 27, 2024

Hello @GbotemiB!

Thanks a lot for opening the PR! Currently, CI is failing, and it looks like the reason is the recent environment troubles we experienced in PyPSA-Earth. Those troubles have been fixed recently with this PR and merged into Linopy PR in PyPSA-Earth. Could you please change the update the commit on which your PR relays? I think, this one should work fine.

@GbotemiB
Copy link
Author

Hi @ekatef,

Thanks for looking into this. I will review the changes and revert.

@ekatef
Copy link
Collaborator

ekatef commented Jun 27, 2024

Hi @ekatef,

Thanks for looking into this. I will review the changes and revert.

Hey @GbotemiB, no worries and no need to revert! 🙂 All good, and we just need to include the recent changes form PyPSA-Earth upstream.

if co2_stores.empty or ("Store", "e") not in n.variables.index:
if co2_stores.empty: # or ("Store", "e") not in n.variables.index:
Copy link
Collaborator

@ekatef ekatef Jun 27, 2024

Choose a reason for hiding this comment

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

Just for better understanding: what has been the reason of commenting-out the second part of the conditon?

Copy link
Author

Choose a reason for hiding this comment

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

The variables method no longer exists in the network, which is why I commented it out. I also noticed something in pypsa-eur implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You mean n.variables component, right?

Agree that some adjustment may be needed, but we have to avoid losses in functionality. It would be great to understand the purpose of having ("Store", "e") not in n.variables.index condition here, check if it's still needed and find a way to re-implement it. May be a good point to discuss with the cross-sectoral team.

Copy link
Author

Choose a reason for hiding this comment

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

I don't know the reason or the functionality. But, I checked similar implementations in pypsa-eur, before the linopy integration, and after the integration, and it seems to have been done in the same way.

scripts/solve_network.py Outdated Show resolved Hide resolved
@ekatef
Copy link
Collaborator

ekatef commented Jun 27, 2024

Thanks a lot @GbotemiB! The update has worked 🎉
CI is failing currently but the reason is that some remote source is unavailable (urllib.error.HTTPError: HTTP Error 503: Service Unavailable).

The PR itself looks great! Is it ready for review?

@GbotemiB
Copy link
Author

@ekatef, the PR is ready for review. I didn't mark it for review because the Linopy PR in pypsa-earth is still in draft.

I have investigated the error in the CI. It relates to prepare_transport_data_input:download_number_of_vehicles.py which is supposed to access this link here, but at the moment the site is currently not reachable. This should be the reason for the error.

@GbotemiB GbotemiB marked this pull request as ready for review June 27, 2024 17:23
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.

Hello :D
Just adding few cents :)
Great @GbotemiB :D


mem: 30000 #memory in MB; 20 GB enough for 50+B+I+H2; 100 GB for 181+B+I+H2
walltime: "12:00:00"
Copy link
Member

Choose a reason for hiding this comment

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

In pypsa-eur, the standard parameter is "runtime", may be good to revise?

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @davide-f, I will check it out.

Comment on lines 337 to 341
lhs = (
(1 * link_p.loc[heat_ext]),
(1 * link_p.loc[electric_ext]),
(1 * link_p_nom.loc[electric_ext]),
)
Copy link
Member

Choose a reason for hiding this comment

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

This one cannot work.
It feels to me that probably we need to slightly change the CI options to enable more of these functions to actually be tested.
Moreover, pay attention on signs, it seems something has went wrong here

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @davide-f for catching this. I will revise the implementation.

vars_final_co2_stored = get_var(n, "Store", "e").loc[sns[-1], co2_stores]
vars_final_co2_stored = n.model["Store-e"].loc[sns[-1], co2_stores]
Copy link
Collaborator

Choose a reason for hiding this comment

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

As a general comment, indexing in linopy may be a bit delicate. My observation is that there may be no one-to-one correspondence with old functions. Some testing may be needed, especially there is no exact match with PyPSA-Eur.

Copy link
Author

Choose a reason for hiding this comment

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

Do you have any ideas on how we can test the functionalities?

from my end, I think we can test the original functionalities alongside the new ones, and compare the results.

lhs = linexpr((1, vars_final_co2_stored)).sum()
lhs = (1 * vars_final_co2_stored).sum()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure we need to multiply by 1 here and in all similar fragments.

My understanding is that a linopy equivalent of linexpr((1, vars_final_co2_stored)).sum() is vars_final_co2_stored.sum(). But that is not necessarily 100% correct understanding, and happy to discuss. Do you have any ideas on that? Have you tested these parts?

Copy link
Author

@GbotemiB GbotemiB Jul 3, 2024

Choose a reason for hiding this comment

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

You are right. The 1 multiple is redundant. I did a bit of debugging to verify the expression already. However, I am open to more ways to test these implementations.

@ekatef
Copy link
Collaborator

ekatef commented Jul 3, 2024

Hello @GbotemiB!

Very nice and quite comprehensive work! Thank you for addressing the review comments. I think, now we have a good basic linopy-based implementation both for pypsa-earth and pypsa-earth-sec. The next step must be a good testing, as linopy is quite a powerful tool which should be used in a mindful way. Along with PyPSA-Eur implementation, there are great examples on linopy usage which can be very helpful to get the main principles. I have also collected some observations on linopy syntax which may be relevant for our PRs: see this comment.

Absolutely agree with @davide-f that CI doesn't catch the points which are crucial for functionality of both linopy PRs. Which mean that we can't fully relate on CI in this case, and some a manual testing is necessary. My feeling is that the following tests are needed:

  1. Execute some extra-functionality functions. In particular, add_chp_constraints(n) looks quite complicated, and it's local testing is highly desirable.
  2. Check that the objective didn't change due to this PR. Or didn't change significantly, at the very least :)
  3. Backward compatibility is respected: PR runs also with the old environment.

@GbotemiB does the plan sound reasonable for you?

@GbotemiB
Copy link
Author

GbotemiB commented Jul 3, 2024

  1. Execute some extra-functionality functions. In particular, add_chp_constraints(n) looks quite complicated, and it's local testing is highly desirable.
  2. Check that the objective didn't change due to this PR. Or didn't change significantly, at the very least :)
  3. Backward compatibility is respected: PR runs also with the old environment.
    @GbotemiB does the plan sound reasonable for you?

@ekatef Sounds perfect. I think in terms of priority, 1 should come ontop.

@GbotemiB
Copy link
Author

GbotemiB commented Jul 3, 2024

  • Check that the objective didn't change due to this PR. Or didn't change significantly, at the very least :)
  1. Execute some extra-functionality functions. In particular, add_chp_constraints(n) looks quite complicated, and it's local testing is highly desirable.
  2. Check that the objective didn't change due to this PR. Or didn't change significantly, at the very least :)
  3. Backward compatibility is respected: PR runs also with the old environment.
    @GbotemiB does the plan sound reasonable for you?

@ekatef Sounds perfect. I think in terms of priority, 1 should come on top.

I should also mention here that in the original pypsa-earth-sec, the test config results in nan as objective value. I think this is one of the things that CI doesn't catch.

@davide-f davide-f mentioned this pull request Jul 4, 2024
18 tasks
@GbotemiB
Copy link
Author

Hello @GbotemiB!

Very nice and quite comprehensive work! Thank you for addressing the review comments. I think, now we have a good basic linopy-based implementation both for pypsa-earth and pypsa-earth-sec. The next step must be a good testing, as linopy is quite a powerful tool which should be used in a mindful way. Along with PyPSA-Eur implementation, there are great examples on linopy usage which can be very helpful to get the main principles. I have also collected some observations on linopy syntax which may be relevant for our PRs: see this comment.

Absolutely agree with @davide-f that CI doesn't catch the points which are crucial for functionality of both linopy PRs. Which mean that we can't fully relate on CI in this case, and some a manual testing is necessary. My feeling is that the following tests are needed:

  1. Execute some extra-functionality functions. In particular, add_chp_constraints(n) looks quite complicated, and it's local testing is highly desirable.
  2. Check that the objective didn't change due to this PR. Or didn't change significantly, at the very least :)
  3. Backward compatibility is respected: PR runs also with the old environment.

@GbotemiB does the plan sound reasonable for you?

  1. About testing the implementation of add_chp_constraints(n), I confirmed that the implementation Is now just as it is in pypsa-eur. I havent found a way to test it, I have a plan to run the script in debug mode and test it that way.
  2. I have confirmed that the objective didn't change due to this PR. I ran the CI locally which returns a nan for the objective value. I am already in the process of investigating why this happened with @finozzifa.
  3. The current PR can't run with the old environment because of the latest version of pypsa.

@GbotemiB GbotemiB force-pushed the linopy-dev branch 2 times, most recently from dd3102c to b4c4b74 Compare July 15, 2024 14:36
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.

Few comments; great @GbotemiB! :D
As a comment, do you feel like by simply changing few parameters in the config test yaml, the functions you modified could be included in the CI?
That may ease the process (if we also fix the CI, but that may not be done here)

Snakefile Outdated
Comment on lines 192 to 191
clustered_pop_layout="resources/population_shares/pop_layout_elec_s{simpl}_{clusters}_{planning_horizons}.csv",
clustered_pop_layout="resources/population_shares/pop_layout_elec_s{simpl}_{clusters}.csv",
Copy link
Member

Choose a reason for hiding this comment

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

Please, check the main, probaly one merge conflict didn't go well

Copy link
Member

Choose a reason for hiding this comment

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

this applies for changes below too

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for catching this. I have resolved the merge conflict.

Comment on lines 163 to 164
h2_network_cap = n.model["Link-p_nom"]
lhs = (h2_network_cap.loc[h2_network.index] * h2_network.length).sum()
Copy link
Member

Choose a reason for hiding this comment

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

Please, check the merge conflict; an if-case has been dropped unfortunately

@GbotemiB
Copy link
Author

Few comments; great @GbotemiB! :D As a comment, do you feel like by simply changing few parameters in the config test yaml, the functions you modified could be included in the CI? That may ease the process (if we also fix the CI, but that may not be done here)

Hi @davide-f, could you please clarify what you mean here?
Do you mean making changes to the config test file?

@davide-f
Copy link
Member

Hi @davide-f, could you please clarify what you mean here? Do you mean making changes to the config test file?

Some functions in the solve_network are triggered by options in the config file, some of them may be off by default which means that they are not tested.
Not a priority, but worth mentioning if it may help the revision

@GbotemiB
Copy link
Author

Hi @davide-f, could you please clarify what you mean here? Do you mean making changes to the config test file?

Some functions in the solve_network are triggered by options in the config file, some of them may be off by default which means that they are not tested. Not a priority, but worth mentioning if it may help the revision

The option to test the add_chp_contraints in the test config is already set to true. The issue is that the solve_network script doesn't call the function. I have included it in my PR.

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.

None yet

3 participants