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

ptdf calculation #604

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open

ptdf calculation #604

wants to merge 37 commits into from

Conversation

Cellophil
Copy link
Contributor

No description provided.

@Cellophil Cellophil changed the title couple improvements ptdf calculation Apr 14, 2023
@Cellophil
Copy link
Contributor Author

I have the suspicion that small impedances cause numerical problems in the PTDF calculation - can someone confirm or check? Possibly a warning could be put in consistency check if the impedances are too far apart.
Apart from that I converted PTDF and BODF into DataFrames, and replaced the sparse solve by sparse inverse. I don't know why sparse solve was used, maybe there's a good reason, but inverse seems to be the dedicated function to do it.

@fneum
Copy link
Member

fneum commented Apr 15, 2023

Do you have a small example where you see numerical problems in the PTDF calculation, that could help us reproduce the behaviour?

Regarding the PR: I think using the scipy inversion function makes a lot of sense; also turning PTDF and BODF into DataFrames. But the tests fail because the code is not yet fully adapted to take DataFrames rather than numpy arrays for these.

@Cellophil
Copy link
Contributor Author

Cellophil commented Apr 15, 2023 via email

@Cellophil
Copy link
Contributor Author

Cellophil commented Apr 15, 2023 via email

@Cellophil
Copy link
Contributor Author

Cellophil commented Apr 18, 2023

Updates on my side:

  • tbh, I'm not so sure about the numerical issue anymore, possibly I had a hickup there
  • the various matrices calculated in sub_network rely on using the same bus and branch ordering in a number of places - probably in order to use scipy sparse matrices. Some functions use for loops a lot, maybe there's a speedup to be gained (incidence matrix for instance)
  • An equivalent of sparse scipy matrices could be set up in the form of Pandas Series with multiindexes. It's an extra step in the middle and probably a bit slower than the blank handling of numpy and scipy, but it would keep track of the index.
  • I'll try to set up a working version that accepts the result at the end and attaches the correct indexes. For now.
  • til pandas supports sparse matrices

@Cellophil
Copy link
Contributor Author

This is probably on me but I have problems with pytest - pandapower and it's numba dependency I think. Sorry for the failed checks, hope it starting to take form.

@Cellophil
Copy link
Contributor Author

Hi @fneum ,

I replaced the usage of BODF and PTDF is a dozen places; and I feel it really makes sense to continue with pandas instead of the annonymous numpy. Some further things should be cleaned up, I patched it in some places...
What I couldn't really check is p_branch_shift and p_bus_shift, I don't know whether that's covered by the test cases. And what it does.

Best,
Andreas

@Cellophil
Copy link
Contributor Author

@fneum One test fails concerning tkinter and some plotting library - it passes on my machine.

@codecov
Copy link

codecov bot commented Jun 22, 2023

Codecov Report

Attention: 5 lines in your changes are missing coverage. Please review.

Comparison is base (e3a38e7) 78.15% compared to head (b586369) 78.14%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #604      +/-   ##
==========================================
- Coverage   78.15%   78.14%   -0.01%     
==========================================
  Files          26       26              
  Lines        6756     6773      +17     
  Branches     1470     1473       +3     
==========================================
+ Hits         5280     5293      +13     
- Misses       1162     1164       +2     
- Partials      314      316       +2     
Files Coverage Δ
pypsa/components.py 79.43% <100.00%> (+0.03%) ⬆️
pypsa/opf.py 89.92% <100.00%> (+0.01%) ⬆️
pypsa/optimization/abstract.py 46.34% <100.00%> (ø)
pypsa/pf.py 83.90% <90.47%> (+0.12%) ⬆️
pypsa/contingency.py 78.34% <76.92%> (-1.40%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@fneum fneum left a comment

Choose a reason for hiding this comment

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

Sorry for taking a long time to get to this! This looks very good!

I had a few small questions. Maybe you can answer them.

Have you tested that the code changes yield same PTDF as before?

The one test failure is fine.

Final thing: Would you add a release note outlining the changes? I.e. BODF and PTDF are now dataframes and new utility function to build PTDF for whole network.

Comment on lines +1339 to +1341
def get_network_ptdf(network):
# convenience function

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a small docstring that this gives PTDF for whole network (not per subnetwork).

Comment on lines +1193 to +1195
sub_network.p_branch_shift = (
-b * phase_shift
) # np.multiply(-b, phase_shift, where=b != np.inf)
Copy link
Member

Choose a reason for hiding this comment

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

Could you briefly explain why where=b != np.inf is no longer needed?

And remove old code.

logger.warning(
"Warning! Some series impedances are zero - this will cause a singularity in LPF!"
)
b_diag = csr_matrix((b, (r_[: len(b)], r_[: len(b)])))

# old: b_diag = csr_matrix((b, (np.r_[: len(b)], np.r_[: len(b)])))
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
# old: b_diag = csr_matrix((b, (np.r_[: len(b)], np.r_[: len(b)])))

# susceptances
b = np.divide(1.0, z, out=np.full_like(z, np.inf), where=z != 0)
# old: b = np.divide(1.0, z, out=np.full_like(z, np.inf), where=z != 0)
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
# old: b = np.divide(1.0, z, out=np.full_like(z, np.inf), where=z != 0)

Comment on lines +1227 to +1228
# I = eye(n_pvpq)
# B_inverse = spsolve(csc_matrix(sub_network.B[1:, 1:]), I)
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
# I = eye(n_pvpq)
# B_inverse = spsolve(csc_matrix(sub_network.B[1:, 1:]), I)

@@ -24,10 +24,11 @@
from numpy import ones, r_
from numpy.linalg import norm
from pandas.api.types import is_list_like
from scipy.sparse import csc_matrix, csr_matrix, dok_matrix
from scipy.sparse import csc_matrix, csr_matrix, diags, dok_matrix, eye
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
from scipy.sparse import csc_matrix, csr_matrix, diags, dok_matrix, eye
from scipy.sparse import csc_matrix, csr_matrix, diags, dok_matrix

Comment on lines -1159 to +1160
z = np.concatenate(
[
(c.df.loc[c.ind, attribute]).values
for c in sub_network.iterate_components(network.passive_branch_components)
]
)
z = sub_network.branches()[attribute]
Copy link
Member

Choose a reason for hiding this comment

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

Oh, this looked very complicated beforehand.

@Cellophil
Copy link
Contributor Author

Hi @fneum , great I'll check it out next week - agreed code needs a little polishing. With ptdf index, I'll check it once again that it's correct.

@FabianHofmann
Copy link
Collaborator

Hey @Cellophil, gently pinging you. Do you think it is feasible for you to resolve the remaining issues?

@Cellophil
Copy link
Contributor Author

Cellophil commented Oct 10, 2023 via email

@FabianHofmann
Copy link
Collaborator

Hey Andreas, no worries, thanks for letting us know. The young ones have full priority :) We can also try to take it from here.

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