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 plot for hourly dispatch in sample_network_analysis notebook #95

Closed
mt7180 opened this issue Nov 25, 2024 · 2 comments · Fixed by #96
Closed

fix plot for hourly dispatch in sample_network_analysis notebook #95

mt7180 opened this issue Nov 25, 2024 · 2 comments · Fixed by #96

Comments

@mt7180
Copy link
Contributor

mt7180 commented Nov 25, 2024

As also mentioned in the Youtube Tutorial #1, the stacked plot for hourly dispatch in the sample_network_analysis notebook has greater demand than generation. That's because:

  • the power coming from the hydro-storage is not recognised
  • the power from/ to the H2 and battery store is not separated for stacking positive/ negative numbers and just adds up with plt.stackplot.
  • when taking into account the H2 (and battery, which is not used in this scenario) store itself:
    • H2.where(H2 > 0, 0), : H2 discharging : p at H2 store is without loss, but grid supply is reduced behind link!
    • H2.where(H2 < 0, 0), : H2 charging : power at H2 store reduced due to link (charging) efficiency, but grid withdrawal (before link) is higher!
    • -> It would be clearer to show the direct withdrawal from / supply to the grid

When doing so, generation and demand areas are balanced (see the black withdrawal line)

The following updated code gives the diagram below (and additionally omits dealing with the color_map manually) :

...
hydro = n.storage_units_t.p.loc[first_date:second_date].T.groupby(n.storage_units.carrier).sum().loc['hydro']
H2_electrolysis = n.links_t.p0.loc[first_date:second_date].T.groupby(n.links.carrier).sum().loc['H2 electrolysis']*-1
H2_fuel_cell = n.links_t.p1.loc[first_date:second_date].T.groupby(n.links.carrier).sum().loc['H2 fuel cell']*-1
battery_charge = n.links_t.p0.loc[first_date:second_date].T.groupby(n.links.carrier).sum().loc['battery charger']*-1
battery_discharge = n.links_t.p1.loc[first_date:second_date].T.groupby(n.links.carrier).sum().loc['battery discharger']*-1

# names used for labels
demand.name = 'demand'
H2_electrolysis.name = "H2"
H2_fuel_cell.name = "H2"
battery_charge.name = 'battery'
battery_discharge.name = 'battery'

plt.stackplot(
    CCGT.index,
    supply:=(hydro.where(hydro > 0, 0), CCGT, OCGT, Onwind, battery_discharge, solar, H2_fuel_cell), 
    labels=[key.name for key in supply],
    colors=[n.carriers.color.get(source.name, default="gray") for source in supply], 
    zorder = 99
)

plt.stackplot(
    CCGT.index,
    withdrawal:=(demand, battery_charge, H2_electrolysis, hydro.where(hydro < 0, 0)),
    labels=[source.name for source in withdrawal],
    colors=[n.carriers.color.get(key.name, default="red") for key in withdrawal], 
    zorder = 1
)
plt.plot(sum(withdrawal).abs(), color = "black", label='abs(withdrawal)', linestyle='dashed', zorder=100)

handles, labels = ax.get_legend_handles_labels()
unique_labels = dict(zip(labels, handles))  # remove duplicates

....

image

Additionally, there are some minor issues with the notebook:

  • from _helpers import sets_path_to_root is no longer available (and not needed) and raises an error
  • .groupby(n.generators.carrier, axis=1) raises: FutureWarning: DataFrame.groupby with axis=1 is deprecated. Do frame.T.groupby(...) without axis instead.. This can be fixed with: .T.groupby(n.storage_units.carrier). in combination with .loc[...]
  • n.statistics._parent raises an AttributeError for newer versions of pypsa (just printing n returns the same information)
    (By the way: why is the pypsa version fixed to 0.24 in the environment.yaml for pypsa-earth, is there a specific reason?)

Maybe it would also be nice to add a link to the tutorial video mentioned above :)

Please let me know if I should open a PR.

@ekatef
Copy link
Member

ekatef commented Nov 25, 2024

Hello @mt7180, thank you so much for sharing your work! Looks like a very nice improvement of the documentation 😄

I confirm that the sample_network_analysis notebook got a bit outdated. Also, it has been created for a particular case where hydro didn't play a significant role, while having a more general approach would be highly appreciated!

Also, that it absolutely makes sense to add a link to the youtube tutorial.

We would love to have the implementation you suggest! The PR on that would be very warmly welcome 🙏🏽

@ekatef
Copy link
Member

ekatef commented Nov 25, 2024

(By the way: why is the pypsa version fixed to 0.24 in the environment.yaml for pypsa-earth, is there a specific reason?)

Ahh, regarding the PyPSA version, 0.25 restriction has been needed to avoid introducing breaking changes into the model. We have a PR which introduced the changes needed to run the latest version of PyPSA, but we aim first to release a stable version of the model with cross-sectoral capabilities. Actually, that is a great moment to communicate is more actively 😄 Thank for raising the question in the Support channel in Discord!

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 a pull request may close this issue.

2 participants