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

Improve docs and tests for PTDF calculation in Flow Decomposition #159

Merged
merged 9 commits into from
Oct 10, 2024

Conversation

caioluke
Copy link
Member

@caioluke caioluke commented Sep 4, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce?

Enhancement

What is the current behavior?

Lack of clarity in signed PTDF calculation.

What is the new behavior (if this is a feature change)?
Improve docs and tests about PTDF calculation respecting the flow sign convention.

Does this PR introduce a breaking change or deprecate an API?

  • Yes
  • No

Signed-off-by: Caio Luke <caioluke97@gmail.com>
@caioluke caioluke requested a review from OpenSuze September 4, 2024 12:47
Copy link
Contributor

@OpenSuze OpenSuze left a comment

Choose a reason for hiding this comment

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

Thank you for your PR !

I have a lot of questions !

  1. Do you have any test to highlight this behavior ?
  2. Could you document the reason of the sign flipping in the documentation ?
  3. Is it an issue with any rescaler that are based on a terminal ?
  4. Could you refactor this code to avoid comments ?
  5. Could you provide something like an empty contingency to trigger the fixZeroFunctionReference instead of duplicating code ?
  6. Why are they not rescaling when there is no contingency ?
  7. Should this not be a PR in powsybl-open-loadflow ?

@caioluke
Copy link
Member Author

caioluke commented Sep 6, 2024

Thanks for your reply :)

I have a test case but the data is confidential... I will try to come up with a minimal case

@caioluke caioluke self-assigned this Sep 30, 2024
@caioluke
Copy link
Member Author

@OpenSuze I managed to create a minimal case where the sign instability presents itself.

It showed up in a different scenario from what I had previously described.
That's why I updated the PR's description.

However, I don't have a fix in mind for this one... Could you have a look and see if you can find anything?

var ptdfs1 = report1.ptdfs.forBaseCase();
var ptdfs2 = report2.ptdfs.forBaseCase();

// Ensure that ptdf signs are the same with or without loss compensation
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain why you want to keep the same ptdf sign with or without loss compensation please ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I expect the same PTDF results with or without loss compensation.

In this case the PTDF changes sign (+1 becomes -1) and to avoid thresholding I thought it would be simpler to only compare signs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I understand your test but I'm wondering why does it matter to keep the same pdtf value before or after loss compensation ?

From my point of view, loss compensation allows to to DC computations that are closer to AC computations without paying AC computation costs. So having slightly different PTDF between DC-loss-compensated PTDF and AC PTDF is expected.

Futhermore, I don't understand why this test might improve sign stability ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Keeping the same PTDF sign matters to have similar (not the same due to losses) allocated flows in flow decomposition results.

Here, PTDFs are not sligthly different, they are as different as they can be (+1 and -1). I'm betting it's a sign issue somewhere.

My test doesn't improve sign stability, it's just a minimal example showing there is one.

I haven't found out why this happens...

Copy link
Contributor

Choose a reason for hiding this comment

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

I have investigated your issue.

The sign difference of the PTDF comes from the fact that we change the sign of the PTDF value (see SensitivityAnalyser line 93).

If we remove this sign modification, then there is no more issue with your test.
However, this sign modification is here to ensure that burdening flows are positive and relieving flows negative without having to consider the reference flow direction.
This sign modification is only here for easier understanding of the flow decomposition results.
More details are available here

Futhermore, it could be a great idea to refactor the SensitivityAnalyser line 93 to extract a static function with a good name such as respectFlowSignConvention

    static double respectFlowSignConvention(double ptdfValue, double referenceFlow) {
        return referenceFlow < 0 ? -ptdfValue : ptdfValue;
    }

With this in mind, I think the best choice is to acknowledge this sign convention and change your test.

assertEquals(respectFlowSignConvention(ptdfValue1, dcFlow1.get(branchId)), respectFlowSignConvention(ptdfValue2, dcFlow2.get(branchId)), 1E-9);

This solution uses the fact that respectFlowSignConvention is an involution (maybe we should test that as well).
Maybe we should document the sign convention in the PTDF observer part also.

Thanks to @phiedw and @wangjer for their help.

Thank you in advance,

Hugo

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you @OpenSuze @phiedw and @wangjer for this detailed answer!

The behaviour is much clearer to me now :)

I have updated the test and the docs accordingly.

Signed-off-by: Caio Luke <caioluke97@gmail.com>
# Conflicts:
#	flow-decomposition/src/test/java/com/powsybl/flow_decomposition/FlowDecompositionObserverTest.java
Signed-off-by: Caio Luke <caioluke97@gmail.com>
Signed-off-by: Caio Luke <caioluke97@gmail.com>
Copy link

sonarqubecloud bot commented Oct 9, 2024

@caioluke caioluke changed the title Fix PTDF sign instability Improve docs and tests for PTDF calculation in Flow Decomposition Oct 9, 2024
Copy link
Contributor

@OpenSuze OpenSuze left a comment

Choose a reason for hiding this comment

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

Thank you for this PR !

@phiedw phiedw merged commit 9b05615 into main Oct 10, 2024
7 checks passed
@phiedw phiedw deleted the fix_ptdf_signs branch October 10, 2024 07:58
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.

3 participants