From 7c4ea8f8e418fa8e790c4afa678bdcae5bb6fd87 Mon Sep 17 00:00:00 2001 From: Anne Tilloy Date: Mon, 12 Dec 2022 16:17:07 +0100 Subject: [PATCH 01/15] Update. Signed-off-by: Anne Tilloy --- pom.xml | 1 - .../powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index ef0b6c2bd1..df36f194da 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,6 @@ 5.8.2 1.6.3 1.0.12 - 5.1.0-RC1 diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index cd8822a790..c0b7bbce59 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -10,6 +10,7 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.commons.reporter.Reporter; import com.powsybl.commons.reporter.ReporterModel; +import com.powsybl.commons.test.TestUtil; import com.powsybl.computation.ComputationManager; import com.powsybl.contingency.*; import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory; @@ -57,6 +58,7 @@ import java.util.stream.Stream; import static com.powsybl.commons.test.TestUtil.normalizeLineSeparator; + import static java.lang.Double.NaN; import static java.util.Collections.emptySet; import static org.junit.jupiter.api.Assertions.*; @@ -1768,11 +1770,11 @@ void reportTest() throws IOException { runSecurityAnalysis(network, contingencies, Collections.emptyList(), new SecurityAnalysisParameters(), reporter); - String refLogExport = normalizeLineSeparator(new String(ByteStreams.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/saReport.txt"))), StandardCharsets.UTF_8)); + String refLogExport = TestUtil.normalizeLineSeparator(new String(ByteStreams.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/saReport.txt"))), StandardCharsets.UTF_8)); StringWriter writer = new StringWriter(); reporter.export(writer); - String logExport = normalizeLineSeparator(writer.toString()); + String logExport = TestUtil.normalizeLineSeparator(writer.toString()); assertEquals(refLogExport, logExport); } From 11db1fc1e9a6e5e01c7d960185519c6cc992ab40 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 10 Jan 2023 11:12:19 +0100 Subject: [PATCH 02/15] Update Signed-off-by: Geoffroy Jamgotchian --- .../powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index c0b7bbce59..41be724c84 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -10,7 +10,6 @@ import com.powsybl.commons.PowsyblException; import com.powsybl.commons.reporter.Reporter; import com.powsybl.commons.reporter.ReporterModel; -import com.powsybl.commons.test.TestUtil; import com.powsybl.computation.ComputationManager; import com.powsybl.contingency.*; import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory; @@ -1770,11 +1769,11 @@ void reportTest() throws IOException { runSecurityAnalysis(network, contingencies, Collections.emptyList(), new SecurityAnalysisParameters(), reporter); - String refLogExport = TestUtil.normalizeLineSeparator(new String(ByteStreams.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/saReport.txt"))), StandardCharsets.UTF_8)); + String refLogExport = normalizeLineSeparator(new String(ByteStreams.toByteArray(Objects.requireNonNull(getClass().getResourceAsStream("/saReport.txt"))), StandardCharsets.UTF_8)); StringWriter writer = new StringWriter(); reporter.export(writer); - String logExport = TestUtil.normalizeLineSeparator(writer.toString()); + String logExport = normalizeLineSeparator(writer.toString()); assertEquals(refLogExport, logExport); } From 165d75c4a2e504f22a9814a95b5d373600e98654 Mon Sep 17 00:00:00 2001 From: Guillaume Verger Date: Mon, 9 Jan 2023 15:39:17 +0100 Subject: [PATCH 03/15] Add nb of synchronous components in SA post contingency Signed-off-by: Guillaume Verger --- .../openloadflow/network/LfContingency.java | 9 ++++- .../network/impl/PropagatedContingency.java | 4 ++- .../openloadflow/sa/AcSecurityAnalysis.java | 4 ++- .../openloadflow/sa/DcSecurityAnalysis.java | 1 + .../sa/OpenSecurityAnalysisTest.java | 33 +++++++++++++++++-- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java index a6f57db9e1..9f7d8afdf1 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java @@ -29,6 +29,8 @@ public class LfContingency { private final int index; + private final int nbSynchronousComponents; + private final Set disabledBuses; private final Set disabledBranches; @@ -45,10 +47,11 @@ public class LfContingency { private double activePowerLoss = 0; - public LfContingency(String id, int index, Set disabledBuses, Set disabledBranches, Map shuntsShift, + public LfContingency(String id, int index, int nbSynchronousComponents, Set disabledBuses, Set disabledBranches, Map shuntsShift, Map busesLoadShift, Set lostGenerators, Set disabledHvdcs, Set originalPowerShiftIds) { this.id = Objects.requireNonNull(id); this.index = index; + this.nbSynchronousComponents = nbSynchronousComponents; this.disabledBuses = Objects.requireNonNull(disabledBuses); this.disabledBranches = Objects.requireNonNull(disabledBranches); this.disabledHvdcs = Objects.requireNonNull(disabledHvdcs); @@ -75,6 +78,10 @@ public int getIndex() { return index; } + public int getNbSynchronousComponents() { + return nbSynchronousComponents; + } + public Set getDisabledBuses() { return disabledBuses; } diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java index 2447b12c98..aebdc825fb 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java @@ -299,6 +299,7 @@ public Optional toLfContingency(LfNetwork network) { // add to contingency description buses and branches that won't be part of the main connected // component in post contingency state + int nbSynchronousComponents = connectivity.getNbConnectedComponents(); Set buses = connectivity.getVerticesRemovedFromMainComponent(); Set branches = new HashSet<>(connectivity.getEdgesRemovedFromMainComponent()); @@ -352,6 +353,7 @@ public Optional toLfContingency(LfNetwork network) { return Optional.empty(); } - return Optional.of(new LfContingency(contingency.getId(), index, buses, branches, shunts, busesLoadShift, generators, hvdcs, originalPowerShiftIds)); + return Optional.of(new LfContingency(contingency.getId(), index, nbSynchronousComponents, buses, branches, shunts, busesLoadShift, generators, hvdcs, originalPowerShiftIds)); + } } diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index e7123180f3..acd3c50bdb 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -247,12 +247,14 @@ private PostContingencyResult runPostContingencySimulation(LfNetwork network, Ac LOGGER.info("Post contingency '{}' simulation done on network {} in {} ms", lfContingency.getId(), network, stopwatch.elapsed(TimeUnit.MILLISECONDS)); + var connectivityResult = new ConnectivityResult(lfContingency.getNbSynchronousComponents() - 1, 0, 0, 0, Collections.emptySet()); + return new PostContingencyResult(contingency, status, new LimitViolationsResult(postContingencyLimitViolationManager.getLimitViolations()), postContingencyNetworkResult.getBranchResults(), postContingencyNetworkResult.getBusResults(), postContingencyNetworkResult.getThreeWindingsTransformerResults(), - new ConnectivityResult(0, 0, 0, 0, Collections.emptySet())); + connectivityResult); } private Optional runActionSimulation(LfNetwork network, AcLoadFlowContext context, OperatorStrategy operatorStrategy, diff --git a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java index 1801a8bfbe..0ac6caf71e 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java @@ -200,6 +200,7 @@ private List createPostContingencyResults(DcSecurityAnaly PostContingencyResult pcResult = new PostContingencyResult(contingency, PostContingencyComputationStatus.CONVERGED, new ArrayList<>(violations.values()), new ArrayList<>(postContingencyBranchResults.values()), Collections.emptyList(), Collections.emptyList(), new ConnectivityResult(0, 0, 0, 0, Collections.emptySet())); + context.getPostContingencyResultPerContingencyId().put(contingency.getId(), pcResult); postContingencyResults.add(pcResult); } diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index 41be724c84..fceb596324 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -2271,11 +2271,11 @@ void testLoadAction() { .collect(Collectors.toList()); List actions = List.of(new LoadActionBuilder().withId("action1").withLoadId("l4").withRelativeValue(false).withActivePowerValue(90).build(), - new LoadActionBuilder().withId("action2").withLoadId("l1").withRelativeValue(true).withActivePowerValue(50).build(), - new LoadActionBuilder().withId("action3").withLoadId("l2").withRelativeValue(true).withActivePowerValue(10).build()); + new LoadActionBuilder().withId("action2").withLoadId("l1").withRelativeValue(true).withActivePowerValue(50).build(), + new LoadActionBuilder().withId("action3").withLoadId("l2").withRelativeValue(true).withActivePowerValue(10).build()); List operatorStrategies = List.of(new OperatorStrategy("strategy1", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action1", "action2")), - new OperatorStrategy("strategy2", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action3"))); + new OperatorStrategy("strategy2", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action3"))); List monitors = createAllBranchesMonitors(network); @@ -2342,4 +2342,31 @@ void testDcEquationSystemUpdater() { SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, securityAnalysisParameters, operatorStrategies, actions, Reporter.NO_OP); assertFalse(result.getPostContingencyResults().isEmpty()); } + + void testConnectivityResultWhenNoSplit() throws IOException { + Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); + List monitors = createAllBranchesMonitors(network); + List contingencies = List.of(new Contingency("line", new BranchContingency("l12"))); + SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + + var postContingencyResult = result.getPostContingencyResults().get(0); + + assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); + + assertEquals(0, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + } + + @Test + void testConnectivityResultOnSplit() throws IOException { + Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); + List monitors = createAllBranchesMonitors(network); + List contingencies = List.of(new Contingency("line", new BranchContingency("l34"))); + SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + + PostContingencyResult postContingencyResult = result.getPostContingencyResults().get(0); + + assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); + + assertEquals(1, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + } } From 36078b593da5769b3fb3c151546b0a768d87ae37 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Wed, 11 Jan 2023 14:51:32 +0100 Subject: [PATCH 04/15] Use core main. Signed-off-by: Bertrand Rix --- .github/workflows/maven.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 9b265f43dc..0ebba62cd1 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -16,6 +16,12 @@ jobs: os: [ubuntu-latest, windows-latest, macos-latest] steps: + - name: Checkout powsybl-core sources + uses: actions/checkout@v1 + with: + repository: powsybl/powsybl-core + ref: refs/heads/main + - name: Checkout sources uses: actions/checkout@v1 @@ -24,6 +30,9 @@ jobs: with: java-version: 11 + - name: Build and install powsybl-core with Maven + run: mvn --batch-mode -DskipTests=true --file ../powsybl-core/pom.xml install + - name: Build with Maven run: mvn --batch-mode -Pjacoco install From 5c0f6aae42bdf21f6a83f3e77588966d377d4bd2 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Wed, 11 Jan 2023 17:04:48 +0100 Subject: [PATCH 05/15] Add separated disconnected generation and load active power and set of lost elements for the contingency. Signed-off-by: Bertrand Rix --- .../openloadflow/network/LfContingency.java | 36 ++++++++++++++++--- .../network/impl/PropagatedContingency.java | 1 - .../openloadflow/sa/AcSecurityAnalysis.java | 6 +++- .../sa/OpenSecurityAnalysisTest.java | 4 +++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java index 9f7d8afdf1..33129d73a4 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java @@ -45,7 +45,11 @@ public class LfContingency { private final Set lostGenerators; - private double activePowerLoss = 0; + private double disconnectedLoadActivePower; + + private double disconnectedGenerationActivePower; + + private Set disconnectedElements; public LfContingency(String id, int index, int nbSynchronousComponents, Set disabledBuses, Set disabledBranches, Map shuntsShift, Map busesLoadShift, Set lostGenerators, Set disabledHvdcs, Set originalPowerShiftIds) { @@ -59,15 +63,25 @@ public LfContingency(String id, int index, int nbSynchronousComponents, Set(); + for (LfBus bus : disabledBuses) { - activePowerLoss += bus.getGenerationTargetP() - bus.getLoadTargetP(); + disconnectedLoadActivePower += bus.getLoadTargetP(); + disconnectedGenerationActivePower += bus.getGenerationTargetP(); + disconnectedElements.addAll(bus.getGenerators().stream().map(LfGenerator::getId).collect(Collectors.toList())); + disconnectedElements.addAll(bus.getAggregatedLoads().getOriginalIds()); } for (Map.Entry e : busesLoadShift.entrySet()) { - activePowerLoss -= e.getValue().getActive(); + disconnectedLoadActivePower += e.getValue().getActive(); } for (LfGenerator generator : lostGenerators) { - activePowerLoss += generator.getTargetP(); + disconnectedGenerationActivePower += generator.getTargetP(); + disconnectedElements.add(generator.getId()); } + disconnectedElements.addAll(originalPowerShiftIds); + disconnectedElements.addAll(disabledBranches.stream().map(LfBranch::getId).collect(Collectors.toList())); } public String getId() { @@ -102,8 +116,20 @@ public Set getLostGenerators() { return lostGenerators; } + public Set getDisconnectedElements() { + return disconnectedElements; + } + public double getActivePowerLoss() { - return activePowerLoss; + return disconnectedGenerationActivePower - disconnectedLoadActivePower; + } + + public double getDisconnectedLoadActivePower() { + return disconnectedLoadActivePower; + } + + public double getDisconnectedGenerationActivePower() { + return disconnectedGenerationActivePower; } public void apply(LoadFlowParameters.BalanceType balanceType) { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java index aebdc825fb..819fc70cd9 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java @@ -354,6 +354,5 @@ public Optional toLfContingency(LfNetwork network) { } return Optional.of(new LfContingency(contingency.getId(), index, nbSynchronousComponents, buses, branches, shunts, busesLoadShift, generators, hvdcs, originalPowerShiftIds)); - } } diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index acd3c50bdb..c58dd13c8f 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -31,6 +31,7 @@ import com.powsybl.openloadflow.network.impl.PropagatedContingency; import com.powsybl.openloadflow.network.util.ActivePowerDistribution; import com.powsybl.openloadflow.network.util.PreviousValueVoltageInitializer; +import com.powsybl.openloadflow.util.PerUnit; import com.powsybl.openloadflow.util.Reports; import com.powsybl.security.*; import com.powsybl.security.action.Action; @@ -43,6 +44,7 @@ import java.util.*; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; /** * @author Geoffroy Jamgotchian @@ -247,7 +249,9 @@ private PostContingencyResult runPostContingencySimulation(LfNetwork network, Ac LOGGER.info("Post contingency '{}' simulation done on network {} in {} ms", lfContingency.getId(), network, stopwatch.elapsed(TimeUnit.MILLISECONDS)); - var connectivityResult = new ConnectivityResult(lfContingency.getNbSynchronousComponents() - 1, 0, 0, 0, Collections.emptySet()); + var connectivityResult = new ConnectivityResult(lfContingency.getNbSynchronousComponents() - 1, + 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, + lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElements()); return new PostContingencyResult(contingency, status, new LimitViolationsResult(postContingencyLimitViolationManager.getLimitViolations()), diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index fceb596324..20d889cb53 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -2368,5 +2368,9 @@ void testConnectivityResultOnSplit() throws IOException { assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); assertEquals(1, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + assertEquals(3.0, postContingencyResult.getConnectivityResult().getDisconnectedLoadActivePower()); + assertEquals(2.0, postContingencyResult.getConnectivityResult().getDisconnectedGenerationActivePower()); + assertTrue(postContingencyResult.getConnectivityResult().getDisconnectedElements().containsAll( + List.of("d4", "d5", "g6", "l46", "l34", "l45", "l56"))); } } From 072a2ea9b1b6beaec99d7c596468cc48a6f9b9a3 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Thu, 12 Jan 2023 16:52:49 +0100 Subject: [PATCH 06/15] Remove unused import and add a test for two synchronous component creation. Signed-off-by: Bertrand Rix --- .../openloadflow/sa/AcSecurityAnalysis.java | 1 - .../openloadflow/sa/OpenSecurityAnalysisTest.java | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index c58dd13c8f..d58e28abb1 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -44,7 +44,6 @@ import java.util.*; import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; /** * @author Geoffroy Jamgotchian diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index 20d889cb53..9e89bc6aa0 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -2373,4 +2373,18 @@ void testConnectivityResultOnSplit() throws IOException { assertTrue(postContingencyResult.getConnectivityResult().getDisconnectedElements().containsAll( List.of("d4", "d5", "g6", "l46", "l34", "l45", "l56"))); } + + @Test + void testConnectivityResultOnSplitThreeCC() throws IOException { + Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBus(); + List monitors = createAllBranchesMonitors(network); + List contingencies = List.of(new Contingency("line", new BranchContingency("l34"), new BranchContingency("l45"))); + SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + + PostContingencyResult postContingencyResult = result.getPostContingencyResults().get(0); + + assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); + + assertEquals(2, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + } } From dbaa62d2abb06bc9540808350c2e58cf7d868a0b Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Tue, 17 Jan 2023 21:17:35 +0100 Subject: [PATCH 07/15] Fix merge Signed-off-by: Geoffroy Jamgotchian --- .github/workflows/maven.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 0ebba62cd1..9b265f43dc 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -16,12 +16,6 @@ jobs: os: [ubuntu-latest, windows-latest, macos-latest] steps: - - name: Checkout powsybl-core sources - uses: actions/checkout@v1 - with: - repository: powsybl/powsybl-core - ref: refs/heads/main - - name: Checkout sources uses: actions/checkout@v1 @@ -30,9 +24,6 @@ jobs: with: java-version: 11 - - name: Build and install powsybl-core with Maven - run: mvn --batch-mode -DskipTests=true --file ../powsybl-core/pom.xml install - - name: Build with Maven run: mvn --batch-mode -Pjacoco install From e4c7036af4b5b88a4ffe2aea9de3642ae56c038f Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Fri, 20 Jan 2023 09:59:31 +0100 Subject: [PATCH 08/15] Rebase on main and move the count of created synchronous component in toLfContingency. Signed-off-by: Bertrand Rix --- .../powsybl/openloadflow/network/LfContingency.java | 10 +++++----- .../network/impl/PropagatedContingency.java | 4 ++-- .../powsybl/openloadflow/sa/AcSecurityAnalysis.java | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java index 33129d73a4..b8a9abd84e 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java @@ -29,7 +29,7 @@ public class LfContingency { private final int index; - private final int nbSynchronousComponents; + private final int createdSynchronousComponentsCount; private final Set disabledBuses; @@ -51,11 +51,11 @@ public class LfContingency { private Set disconnectedElements; - public LfContingency(String id, int index, int nbSynchronousComponents, Set disabledBuses, Set disabledBranches, Map shuntsShift, + public LfContingency(String id, int index, int createdSynchronousComponentsCount, Set disabledBuses, Set disabledBranches, Map shuntsShift, Map busesLoadShift, Set lostGenerators, Set disabledHvdcs, Set originalPowerShiftIds) { this.id = Objects.requireNonNull(id); this.index = index; - this.nbSynchronousComponents = nbSynchronousComponents; + this.createdSynchronousComponentsCount = createdSynchronousComponentsCount; this.disabledBuses = Objects.requireNonNull(disabledBuses); this.disabledBranches = Objects.requireNonNull(disabledBranches); this.disabledHvdcs = Objects.requireNonNull(disabledHvdcs); @@ -92,8 +92,8 @@ public int getIndex() { return index; } - public int getNbSynchronousComponents() { - return nbSynchronousComponents; + public int getCreatedSynchronousComponentsCount() { + return createdSynchronousComponentsCount; } public Set getDisabledBuses() { diff --git a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java index 819fc70cd9..ff845c71f2 100644 --- a/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/impl/PropagatedContingency.java @@ -299,7 +299,7 @@ public Optional toLfContingency(LfNetwork network) { // add to contingency description buses and branches that won't be part of the main connected // component in post contingency state - int nbSynchronousComponents = connectivity.getNbConnectedComponents(); + int createdSynchronousComponents = connectivity.getNbConnectedComponents() - 1; Set buses = connectivity.getVerticesRemovedFromMainComponent(); Set branches = new HashSet<>(connectivity.getEdgesRemovedFromMainComponent()); @@ -353,6 +353,6 @@ public Optional toLfContingency(LfNetwork network) { return Optional.empty(); } - return Optional.of(new LfContingency(contingency.getId(), index, nbSynchronousComponents, buses, branches, shunts, busesLoadShift, generators, hvdcs, originalPowerShiftIds)); + return Optional.of(new LfContingency(contingency.getId(), index, createdSynchronousComponents, buses, branches, shunts, busesLoadShift, generators, hvdcs, originalPowerShiftIds)); } } diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index d58e28abb1..24aeb3bd72 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -248,7 +248,7 @@ private PostContingencyResult runPostContingencySimulation(LfNetwork network, Ac LOGGER.info("Post contingency '{}' simulation done on network {} in {} ms", lfContingency.getId(), network, stopwatch.elapsed(TimeUnit.MILLISECONDS)); - var connectivityResult = new ConnectivityResult(lfContingency.getNbSynchronousComponents() - 1, + var connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElements()); From 2cb4e709b5798a0d7e5817966e0a1649d46d8292 Mon Sep 17 00:00:00 2001 From: Anne Tilloy Date: Tue, 24 Jan 2023 11:16:16 +0100 Subject: [PATCH 09/15] Clean. Signed-off-by: Anne Tilloy --- .../openloadflow/network/LfContingency.java | 21 +++++++++++-------- .../openloadflow/sa/AcSecurityAnalysis.java | 5 ++--- .../sa/OpenSecurityAnalysisTest.java | 15 +++++++------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java index b8a9abd84e..96f919a6fd 100644 --- a/src/main/java/com/powsybl/openloadflow/network/LfContingency.java +++ b/src/main/java/com/powsybl/openloadflow/network/LfContingency.java @@ -49,7 +49,7 @@ public class LfContingency { private double disconnectedGenerationActivePower; - private Set disconnectedElements; + private Set disconnectedElementIds; public LfContingency(String id, int index, int createdSynchronousComponentsCount, Set disabledBuses, Set disabledBranches, Map shuntsShift, Map busesLoadShift, Set lostGenerators, Set disabledHvdcs, Set originalPowerShiftIds) { @@ -65,23 +65,26 @@ public LfContingency(String id, int index, int createdSynchronousComponentsCount this.originalPowerShiftIds = Objects.requireNonNull(originalPowerShiftIds); this.disconnectedLoadActivePower = 0.0; this.disconnectedGenerationActivePower = 0.0; - this.disconnectedElements = new HashSet<>(); + this.disconnectedElementIds = new HashSet<>(); for (LfBus bus : disabledBuses) { disconnectedLoadActivePower += bus.getLoadTargetP(); disconnectedGenerationActivePower += bus.getGenerationTargetP(); - disconnectedElements.addAll(bus.getGenerators().stream().map(LfGenerator::getId).collect(Collectors.toList())); - disconnectedElements.addAll(bus.getAggregatedLoads().getOriginalIds()); + disconnectedElementIds.addAll(bus.getGenerators().stream().map(LfGenerator::getId).collect(Collectors.toList())); + disconnectedElementIds.addAll(bus.getAggregatedLoads().getOriginalIds()); + bus.getControllerShunt().ifPresent(shunt -> disconnectedElementIds.addAll(shunt.getOriginalIds())); + bus.getShunt().ifPresent(shunt -> disconnectedElementIds.addAll(shunt.getOriginalIds())); } for (Map.Entry e : busesLoadShift.entrySet()) { disconnectedLoadActivePower += e.getValue().getActive(); } for (LfGenerator generator : lostGenerators) { disconnectedGenerationActivePower += generator.getTargetP(); - disconnectedElements.add(generator.getId()); + disconnectedElementIds.add(generator.getId()); } - disconnectedElements.addAll(originalPowerShiftIds); - disconnectedElements.addAll(disabledBranches.stream().map(LfBranch::getId).collect(Collectors.toList())); + disconnectedElementIds.addAll(originalPowerShiftIds); + disconnectedElementIds.addAll(disabledBranches.stream().map(LfBranch::getId).collect(Collectors.toList())); + // FIXME: shuntsShift has to be included in the disconnected elements. } public String getId() { @@ -116,8 +119,8 @@ public Set getLostGenerators() { return lostGenerators; } - public Set getDisconnectedElements() { - return disconnectedElements; + public Set getDisconnectedElementIds() { + return disconnectedElementIds; } public double getActivePowerLoss() { diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index 24aeb3bd72..1cfad1d9a0 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -248,9 +248,8 @@ private PostContingencyResult runPostContingencySimulation(LfNetwork network, Ac LOGGER.info("Post contingency '{}' simulation done on network {} in {} ms", lfContingency.getId(), network, stopwatch.elapsed(TimeUnit.MILLISECONDS)); - var connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), - 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, - lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElements()); + var connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, + lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElementIds()); return new PostContingencyResult(contingency, status, new LimitViolationsResult(postContingencyLimitViolationManager.getLimitViolations()), diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index 9e89bc6aa0..3a5fe1f6f0 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -2271,11 +2271,11 @@ void testLoadAction() { .collect(Collectors.toList()); List actions = List.of(new LoadActionBuilder().withId("action1").withLoadId("l4").withRelativeValue(false).withActivePowerValue(90).build(), - new LoadActionBuilder().withId("action2").withLoadId("l1").withRelativeValue(true).withActivePowerValue(50).build(), - new LoadActionBuilder().withId("action3").withLoadId("l2").withRelativeValue(true).withActivePowerValue(10).build()); + new LoadActionBuilder().withId("action2").withLoadId("l1").withRelativeValue(true).withActivePowerValue(50).build(), + new LoadActionBuilder().withId("action3").withLoadId("l2").withRelativeValue(true).withActivePowerValue(10).build()); List operatorStrategies = List.of(new OperatorStrategy("strategy1", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action1", "action2")), - new OperatorStrategy("strategy2", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action3"))); + new OperatorStrategy("strategy2", ContingencyContext.specificContingency("l2"), new AnyViolationCondition(), List.of("action3"))); List monitors = createAllBranchesMonitors(network); @@ -2343,7 +2343,7 @@ void testDcEquationSystemUpdater() { assertFalse(result.getPostContingencyResults().isEmpty()); } - void testConnectivityResultWhenNoSplit() throws IOException { + void testConnectivityResultWhenNoSplit() { Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); List monitors = createAllBranchesMonitors(network); List contingencies = List.of(new Contingency("line", new BranchContingency("l12"))); @@ -2357,7 +2357,7 @@ void testConnectivityResultWhenNoSplit() throws IOException { } @Test - void testConnectivityResultOnSplit() throws IOException { + void testConnectivityResultOnSplit() { Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); List monitors = createAllBranchesMonitors(network); List contingencies = List.of(new Contingency("line", new BranchContingency("l34"))); @@ -2370,12 +2370,11 @@ void testConnectivityResultOnSplit() throws IOException { assertEquals(1, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); assertEquals(3.0, postContingencyResult.getConnectivityResult().getDisconnectedLoadActivePower()); assertEquals(2.0, postContingencyResult.getConnectivityResult().getDisconnectedGenerationActivePower()); - assertTrue(postContingencyResult.getConnectivityResult().getDisconnectedElements().containsAll( - List.of("d4", "d5", "g6", "l46", "l34", "l45", "l56"))); + assertTrue(postContingencyResult.getConnectivityResult().getDisconnectedElements().containsAll(List.of("d4", "d5", "g6", "l46", "l34", "l45", "l56"))); } @Test - void testConnectivityResultOnSplitThreeCC() throws IOException { + void testConnectivityResultOnSplitThreeCC() { Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBus(); List monitors = createAllBranchesMonitors(network); List contingencies = List.of(new Contingency("line", new BranchContingency("l34"), new BranchContingency("l45"))); From b8e7c7709d9f8c1c910d12ea8e0196c83624bdb9 Mon Sep 17 00:00:00 2001 From: Anne Tilloy Date: Tue, 24 Jan 2023 12:08:11 +0100 Subject: [PATCH 10/15] Fix indentation. Signed-off-by: Anne Tilloy --- .../com/powsybl/openloadflow/sa/AcSecurityAnalysis.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java index 1cfad1d9a0..7d9473ca4e 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java @@ -248,8 +248,10 @@ private PostContingencyResult runPostContingencySimulation(LfNetwork network, Ac LOGGER.info("Post contingency '{}' simulation done on network {} in {} ms", lfContingency.getId(), network, stopwatch.elapsed(TimeUnit.MILLISECONDS)); - var connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, - lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElementIds()); + var connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, + lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, + lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, + lfContingency.getDisconnectedElementIds()); return new PostContingencyResult(contingency, status, new LimitViolationsResult(postContingencyLimitViolationManager.getLimitViolations()), From 1b65db9560264c6a19d555a6ee95b812974da701 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Tue, 24 Jan 2023 14:09:00 +0100 Subject: [PATCH 11/15] Rework the DCSecurityAnalysis to create post contingency results when everything needed is available. Signed-off-by: Bertrand Rix --- .../openloadflow/sa/DcSecurityAnalysis.java | 108 ++++++++++-------- .../sa/OpenSecurityAnalysisTest.java | 42 ++++++- 2 files changed, 94 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java index 0ac6caf71e..34071c2ded 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java @@ -28,6 +28,7 @@ import com.powsybl.openloadflow.network.impl.Networks; import com.powsybl.openloadflow.network.impl.PropagatedContingency; import com.powsybl.openloadflow.sensi.OpenSensitivityAnalysisProvider; +import com.powsybl.openloadflow.util.PerUnit; import com.powsybl.security.*; import com.powsybl.security.action.Action; import com.powsybl.security.detectors.DefaultLimitViolationDetector; @@ -51,7 +52,10 @@ private static class DcSecurityAnalysisContext { private final List contingencies; private final DefaultLimitViolationDetector detector; private final double dcPowerFactor; - private final Map postContingencyResultPerContingencyId = new HashMap<>(); + + private final List postContingencyResults = new ArrayList<>(); + + private final List operatorStrategyResults = new ArrayList<>(); public DcSecurityAnalysisContext(SecurityAnalysisParameters saParameters, List contingencyList, @@ -94,8 +98,12 @@ DefaultLimitViolationDetector getDetector() { return dcPowerFactor; } - Map getPostContingencyResultPerContingencyId() { - return postContingencyResultPerContingencyId; + List getPostContingencyResults() { + return postContingencyResults; + } + + List getOperatorStrategyResults() { + return operatorStrategyResults; } } @@ -133,12 +141,8 @@ SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParamete .run(network, workingVariantId, context.getFactors(), context.getContingencies(), variableSets, sensitivityAnalysisParameters, computationManager, Reporter.NO_OP); PreContingencyResult preContingencyResult = createPreContingencyResults(context, result); - - List postContingencyResults = createPostContingencyResults(context, result); - - List operatorStrategyResult = createOperatorStrategyResults(context, operatorStrategies, actions); - - return new SecurityAnalysisReport(new SecurityAnalysisResult(preContingencyResult, postContingencyResults, operatorStrategyResult)); + computePostContingencyAndOperatorStrategyResults(context, result, operatorStrategies, actions); + return new SecurityAnalysisReport(new SecurityAnalysisResult(preContingencyResult, context.getPostContingencyResults(), context.getOperatorStrategyResults())); } private PreContingencyResult createPreContingencyResults(DcSecurityAnalysisContext context, SensitivityAnalysisResult result) { @@ -164,11 +168,13 @@ private PreContingencyResult createPreContingencyResults(DcSecurityAnalysisConte return new PreContingencyResult(LoadFlowResult.ComponentResult.Status.CONVERGED, limitViolations, branchResults, Collections.emptyList(), Collections.emptyList()); } - private List createPostContingencyResults(DcSecurityAnalysisContext context, SensitivityAnalysisResult res) { + private void computePostContingencyAndOperatorStrategyResults(DcSecurityAnalysisContext context, SensitivityAnalysisResult res, List operatorStrategies, List actions) { - List postContingencyResults = new ArrayList<>(); + //Post contingency results from Sensitivity analysis + Map limitViolationPerContingency = new HashMap<>(); + Map> branchResultsPerContingency = new HashMap<>(); for (Contingency contingency : context.getContingencies()) { - Map postContingencyBranchResults = new HashMap<>(); + List postContingencyBranchResults = new ArrayList<>(); List values = res.getValues(contingency.getId()); Map, LimitViolation> violations = new HashMap<>(); double branchInContingencyP1 = Double.NaN; @@ -184,7 +190,7 @@ private List createPostContingencyResults(DcSecurityAnaly if (isBranchMonitored(branchId, contingency)) { BranchResult preContingencyBranchResult = context.getPreContingencyAllBranchResults().get(branchId); double flowTransfer = Double.isNaN(branchInContingencyP1) ? Double.NaN : (v.getFunctionReference() - preContingencyBranchResult.getP1()) / branchInContingencyP1; - postContingencyBranchResults.put(branchId, new BranchResult(branchId, v.getFunctionReference(), Double.NaN, i1, + postContingencyBranchResults.add(new BranchResult(branchId, v.getFunctionReference(), Double.NaN, i1, -v.getFunctionReference(), Double.NaN, i2, flowTransfer)); } context.getDetector().checkActivePower(branch, Branch.Side.ONE, Math.abs(v.getFunctionReference()), violation -> violations.put(Pair.of(violation.getSubjectId(), violation.getSide()), violation)); @@ -198,16 +204,10 @@ private List createPostContingencyResults(DcSecurityAnaly } }); - PostContingencyResult pcResult = new PostContingencyResult(contingency, PostContingencyComputationStatus.CONVERGED, new ArrayList<>(violations.values()), - new ArrayList<>(postContingencyBranchResults.values()), Collections.emptyList(), Collections.emptyList(), new ConnectivityResult(0, 0, 0, 0, Collections.emptySet())); - - context.getPostContingencyResultPerContingencyId().put(contingency.getId(), pcResult); - postContingencyResults.add(pcResult); + //Do not create the postContingencyResult now, save the needed data and create them later when ConnectivityResults are available + limitViolationPerContingency.put(contingency.getId(), new LimitViolationsResult(new ArrayList<>(violations.values()))); + branchResultsPerContingency.put(contingency.getId(), postContingencyBranchResults); } - return postContingencyResults; - } - - private List createOperatorStrategyResults(DcSecurityAnalysisContext context, List operatorStrategies, List actions) { OpenLoadFlowParameters parametersExt = OpenLoadFlowParameters.get(context.getParameters().getLoadFlowParameters()); Set allSwitchesToOpen = new HashSet<>(); @@ -230,10 +230,11 @@ private List createOperatorStrategyResults(DcSecurityAna dcParameters.getNetworkParameters().setBreakers(breakers); try (LfNetworkList lfNetworks = Networks.load(network, dcParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, Reporter.NO_OP)) { - return lfNetworks.getLargest().filter(LfNetwork::isValid) - .map(largestNetwork -> runActionSimulations(context, largestNetwork, dcParameters, propagatedContingencies, - operatorStrategies, actionsById, neededActions)) - .orElse(Collections.emptyList()); + Optional largestNetwork = lfNetworks.getLargest(); + if (largestNetwork.isPresent() && largestNetwork.get().isValid()) { + contingencyAndActionSimulation(context, largestNetwork.get(), dcParameters, propagatedContingencies, + operatorStrategies, actionsById, neededActions, limitViolationPerContingency, branchResultsPerContingency); + } } } @@ -254,48 +255,55 @@ private static double currentActivePower(double activePower, double voltage, dou return 1000 * activePower / (Math.sqrt(3) * cosPhi * voltage); } - private List runActionSimulations(DcSecurityAnalysisContext context, LfNetwork lfNetwork, DcLoadFlowParameters parameters, + private void contingencyAndActionSimulation(DcSecurityAnalysisContext context, LfNetwork lfNetwork, DcLoadFlowParameters parameters, List propagatedContingencies, List operatorStrategies, - Map actionsById, Set neededActions) { - + Map actionsById, Set neededActions, + Map limitViolationPerContingency, + Map> postContingencyBranchResults) { // Run initial load flow and save state DcLoadFlowContext lfContext = new DcLoadFlowContext(lfNetwork, parameters); new DcLoadFlowEngine(lfContext).run(); NetworkState networkState = NetworkState.save(lfNetwork); - - OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters()); - boolean createResultExtension = openSecurityAnalysisParameters.isCreateResultExtension(); - var preContingencyLimitViolationManager = new LimitViolationManager(); preContingencyLimitViolationManager.detectViolations(lfNetwork); + Iterator contingencyIt = propagatedContingencies.iterator(); + Map> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions); Map lfActionById = createLfActions(lfNetwork, neededActions, network, false); - Iterator contingencyIt = propagatedContingencies.iterator(); + OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters()); - List operatorStrategyResults = new ArrayList<>(); while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) { PropagatedContingency propagatedContingency = contingencyIt.next(); - List operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId()); - - if (operatorStrategiesForThisContingency == null) { - break; - } - for (OperatorStrategy operatorStrategy : operatorStrategiesForThisContingency) { - var postContingencyResult = context.getPostContingencyResultPerContingencyId().get(propagatedContingency.getContingency().getId()); - if (checkCondition(operatorStrategy, postContingencyResult.getLimitViolationsResult())) { - propagatedContingency.toLfContingency(lfNetwork) + propagatedContingency.toLfContingency(lfNetwork) .ifPresent(lfContingency -> { lfContingency.apply(context.getParameters().getLoadFlowParameters().getBalanceType()); - OperatorStrategyResult result = runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(), - lfActionById, createResultExtension, lfContingency, parameters.getBalanceType()); - operatorStrategyResults.add(result); + ConnectivityResult connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, + lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElementIds()); + + LimitViolationsResult violationsResult = limitViolationPerContingency.get(propagatedContingency.getContingency().getId()); + //Create the contingency result + context.getPostContingencyResults().add(new PostContingencyResult(propagatedContingency.getContingency(), PostContingencyComputationStatus.CONVERGED, violationsResult, + postContingencyBranchResults.get(propagatedContingency.getContingency().getId()), Collections.emptyList(), Collections.emptyList(), connectivityResult)); + + List operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId()); + if (operatorStrategiesForThisContingency != null) { + for (OperatorStrategy operatorStrategy : operatorStrategiesForThisContingency) { + if (checkCondition(operatorStrategy, violationsResult)) { + NetworkState networkStatePostContingency = NetworkState.save(lfNetwork); + //Iterate on the action for this contingency + //Now do we have to run an action ? + context.getOperatorStrategyResults().add( + runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(), + lfActionById, openSecurityAnalysisParameters.isCreateResultExtension(), lfContingency, parameters.getBalanceType())); + + networkStatePostContingency.restore(); + } + } + } networkState.restore(); }); - } - } } - return operatorStrategyResults; } @Override diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index 3a5fe1f6f0..e7ad7c8b52 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -2347,12 +2347,20 @@ void testConnectivityResultWhenNoSplit() { Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); List monitors = createAllBranchesMonitors(network); List contingencies = List.of(new Contingency("line", new BranchContingency("l12"))); - SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + SecurityAnalysisParameters parameters = new SecurityAnalysisParameters(); + //Test AC + parameters.getLoadFlowParameters().setDc(false); + SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, parameters, Reporter.NO_OP); var postContingencyResult = result.getPostContingencyResults().get(0); - assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); + assertEquals(0, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + //Test DC + parameters.getLoadFlowParameters().setDc(true); + result = runSecurityAnalysis(network, contingencies, monitors, parameters, Reporter.NO_OP); + postContingencyResult = result.getPostContingencyResults().get(0); + assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); assertEquals(0, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); } @@ -2361,12 +2369,26 @@ void testConnectivityResultOnSplit() { Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); List monitors = createAllBranchesMonitors(network); List contingencies = List.of(new Contingency("line", new BranchContingency("l34"))); - SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + SecurityAnalysisParameters parameters = new SecurityAnalysisParameters(); - PostContingencyResult postContingencyResult = result.getPostContingencyResults().get(0); + //Test AC + parameters.getLoadFlowParameters().setDc(false); + SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + PostContingencyResult postContingencyResult = result.getPostContingencyResults().get(0); assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); + assertEquals(1, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + assertEquals(3.0, postContingencyResult.getConnectivityResult().getDisconnectedLoadActivePower()); + assertEquals(2.0, postContingencyResult.getConnectivityResult().getDisconnectedGenerationActivePower()); + assertTrue(postContingencyResult.getConnectivityResult().getDisconnectedElements().containsAll( + List.of("d4", "d5", "g6", "l46", "l34", "l45", "l56"))); + //Test DC + parameters.getLoadFlowParameters().setDc(true); + + result = runSecurityAnalysis(network, contingencies, monitors, parameters, Reporter.NO_OP); + postContingencyResult = result.getPostContingencyResults().get(0); + assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); assertEquals(1, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); assertEquals(3.0, postContingencyResult.getConnectivityResult().getDisconnectedLoadActivePower()); assertEquals(2.0, postContingencyResult.getConnectivityResult().getDisconnectedGenerationActivePower()); @@ -2378,12 +2400,20 @@ void testConnectivityResultOnSplitThreeCC() { Network network = ConnectedComponentNetworkFactory.createThreeCcLinkedByASingleBus(); List monitors = createAllBranchesMonitors(network); List contingencies = List.of(new Contingency("line", new BranchContingency("l34"), new BranchContingency("l45"))); - SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, new SecurityAnalysisParameters(), Reporter.NO_OP); + SecurityAnalysisParameters parameters = new SecurityAnalysisParameters(); + //Test AC + parameters.getLoadFlowParameters().setDc(false); + SecurityAnalysisResult result = runSecurityAnalysis(network, contingencies, monitors, parameters, Reporter.NO_OP); PostContingencyResult postContingencyResult = result.getPostContingencyResults().get(0); - assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); + assertEquals(2, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); + //Test DC + parameters.getLoadFlowParameters().setDc(true); + runSecurityAnalysis(network, contingencies, monitors, parameters, Reporter.NO_OP); + postContingencyResult = result.getPostContingencyResults().get(0); + assertSame(PostContingencyComputationStatus.CONVERGED, postContingencyResult.getStatus()); assertEquals(2, postContingencyResult.getConnectivityResult().getCreatedSynchronousComponentCount()); } } From d422c84ef249af2ed701177df30332c8adb02bff Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Wed, 25 Jan 2023 14:22:59 +0100 Subject: [PATCH 12/15] Remove shunt compensator support in disable change. Signed-off-by: Bertrand Rix --- .../openloadflow/dc/equations/DcEquationSystemUpdater.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java index ea44144213..0acfd83fbb 100644 --- a/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java @@ -45,7 +45,6 @@ public void onDisableChange(LfElement element, boolean disabled) { break; case BRANCH: case HVDC: - case SHUNT_COMPENSATOR: // nothing to do. break; default: From 051ed7d4ed9ed38c06eb9d226cd7ac6f13f04fce Mon Sep 17 00:00:00 2001 From: Anne Tilloy Date: Thu, 26 Jan 2023 10:28:46 +0100 Subject: [PATCH 13/15] Fix merge. Signed-off-by: Anne Tilloy --- pom.xml | 1 + .../openloadflow/dc/equations/DcEquationSystemUpdater.java | 1 + .../com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java | 1 + 3 files changed, 3 insertions(+) diff --git a/pom.xml b/pom.xml index df36f194da..ef0b6c2bd1 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ 5.8.2 1.6.3 1.0.12 + 5.1.0-RC1 diff --git a/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java b/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java index 0acfd83fbb..ea44144213 100644 --- a/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java +++ b/src/main/java/com/powsybl/openloadflow/dc/equations/DcEquationSystemUpdater.java @@ -45,6 +45,7 @@ public void onDisableChange(LfElement element, boolean disabled) { break; case BRANCH: case HVDC: + case SHUNT_COMPENSATOR: // nothing to do. break; default: diff --git a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java index e7ad7c8b52..d44ff05d39 100644 --- a/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sa/OpenSecurityAnalysisTest.java @@ -2343,6 +2343,7 @@ void testDcEquationSystemUpdater() { assertFalse(result.getPostContingencyResults().isEmpty()); } + @Test void testConnectivityResultWhenNoSplit() { Network network = ConnectedComponentNetworkFactory.createTwoComponentWithGeneratorAndLoad(); List monitors = createAllBranchesMonitors(network); From cd1c7683df1bed3c51290ed013c7dac5b41d7861 Mon Sep 17 00:00:00 2001 From: Anne Tilloy Date: Thu, 26 Jan 2023 14:38:02 +0100 Subject: [PATCH 14/15] Proposal. Signed-off-by: Anne Tilloy --- .../openloadflow/sa/DcSecurityAnalysis.java | 144 +++++++++++------- 1 file changed, 89 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java index 34071c2ded..0f3b330b1b 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java @@ -52,10 +52,9 @@ private static class DcSecurityAnalysisContext { private final List contingencies; private final DefaultLimitViolationDetector detector; private final double dcPowerFactor; - - private final List postContingencyResults = new ArrayList<>(); - - private final List operatorStrategyResults = new ArrayList<>(); + private final Map limitViolationsPerContingencyId = new LinkedHashMap<>(); + private final Map> branchResultsPerContingencyId = new LinkedHashMap<>(); + private final Map connectivityResultPerContingencyId = new LinkedHashMap<>(); public DcSecurityAnalysisContext(SecurityAnalysisParameters saParameters, List contingencyList, @@ -98,12 +97,16 @@ DefaultLimitViolationDetector getDetector() { return dcPowerFactor; } - List getPostContingencyResults() { - return postContingencyResults; + Map getLimitViolationsPerContingencyId() { + return limitViolationsPerContingencyId; } - List getOperatorStrategyResults() { - return operatorStrategyResults; + Map> getBranchResultsPerContingencyId() { + return branchResultsPerContingencyId; + } + + Map getConnectivityResultPerContingencyId() { + return connectivityResultPerContingencyId; } } @@ -141,8 +144,19 @@ SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParamete .run(network, workingVariantId, context.getFactors(), context.getContingencies(), variableSets, sensitivityAnalysisParameters, computationManager, Reporter.NO_OP); PreContingencyResult preContingencyResult = createPreContingencyResults(context, result); - computePostContingencyAndOperatorStrategyResults(context, result, operatorStrategies, actions); - return new SecurityAnalysisReport(new SecurityAnalysisResult(preContingencyResult, context.getPostContingencyResults(), context.getOperatorStrategyResults())); + + preparePostContingencyResults(context, result); + + List operatorStrategyResult = createOperatorStrategyResults(context, operatorStrategies, actions); + + List postContingencyResults = new ArrayList<>(); + for (Contingency contingency : contingencies) { + postContingencyResults.add(new PostContingencyResult(contingency, PostContingencyComputationStatus.CONVERGED, + context.getLimitViolationsPerContingencyId().get(contingency.getId()), context.getBranchResultsPerContingencyId().get(contingency.getId()), + Collections.emptyList(), Collections.emptyList(), context.getConnectivityResultPerContingencyId().get(contingency.getId()))); + } + + return new SecurityAnalysisReport(new SecurityAnalysisResult(preContingencyResult, postContingencyResults, operatorStrategyResult)); } private PreContingencyResult createPreContingencyResults(DcSecurityAnalysisContext context, SensitivityAnalysisResult result) { @@ -168,13 +182,10 @@ private PreContingencyResult createPreContingencyResults(DcSecurityAnalysisConte return new PreContingencyResult(LoadFlowResult.ComponentResult.Status.CONVERGED, limitViolations, branchResults, Collections.emptyList(), Collections.emptyList()); } - private void computePostContingencyAndOperatorStrategyResults(DcSecurityAnalysisContext context, SensitivityAnalysisResult res, List operatorStrategies, List actions) { + private void preparePostContingencyResults(DcSecurityAnalysisContext context, SensitivityAnalysisResult res) { - //Post contingency results from Sensitivity analysis - Map limitViolationPerContingency = new HashMap<>(); - Map> branchResultsPerContingency = new HashMap<>(); for (Contingency contingency : context.getContingencies()) { - List postContingencyBranchResults = new ArrayList<>(); + Map postContingencyBranchResults = new HashMap<>(); List values = res.getValues(contingency.getId()); Map, LimitViolation> violations = new HashMap<>(); double branchInContingencyP1 = Double.NaN; @@ -190,7 +201,7 @@ private void computePostContingencyAndOperatorStrategyResults(DcSecurityAnalysis if (isBranchMonitored(branchId, contingency)) { BranchResult preContingencyBranchResult = context.getPreContingencyAllBranchResults().get(branchId); double flowTransfer = Double.isNaN(branchInContingencyP1) ? Double.NaN : (v.getFunctionReference() - preContingencyBranchResult.getP1()) / branchInContingencyP1; - postContingencyBranchResults.add(new BranchResult(branchId, v.getFunctionReference(), Double.NaN, i1, + postContingencyBranchResults.put(branchId, new BranchResult(branchId, v.getFunctionReference(), Double.NaN, i1, -v.getFunctionReference(), Double.NaN, i2, flowTransfer)); } context.getDetector().checkActivePower(branch, Branch.Side.ONE, Math.abs(v.getFunctionReference()), violation -> violations.put(Pair.of(violation.getSubjectId(), violation.getSide()), violation)); @@ -204,10 +215,12 @@ private void computePostContingencyAndOperatorStrategyResults(DcSecurityAnalysis } }); - //Do not create the postContingencyResult now, save the needed data and create them later when ConnectivityResults are available - limitViolationPerContingency.put(contingency.getId(), new LimitViolationsResult(new ArrayList<>(violations.values()))); - branchResultsPerContingency.put(contingency.getId(), postContingencyBranchResults); + context.getLimitViolationsPerContingencyId().put(contingency.getId(), new LimitViolationsResult(new ArrayList<>(violations.values()), Collections.emptyList())); + context.getBranchResultsPerContingencyId().put(contingency.getId(), new ArrayList<>(postContingencyBranchResults.values())); } + } + + private List createOperatorStrategyResults(DcSecurityAnalysisContext context, List operatorStrategies, List actions) { OpenLoadFlowParameters parametersExt = OpenLoadFlowParameters.get(context.getParameters().getLoadFlowParameters()); Set allSwitchesToOpen = new HashSet<>(); @@ -230,11 +243,10 @@ private void computePostContingencyAndOperatorStrategyResults(DcSecurityAnalysis dcParameters.getNetworkParameters().setBreakers(breakers); try (LfNetworkList lfNetworks = Networks.load(network, dcParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, Reporter.NO_OP)) { - Optional largestNetwork = lfNetworks.getLargest(); - if (largestNetwork.isPresent() && largestNetwork.get().isValid()) { - contingencyAndActionSimulation(context, largestNetwork.get(), dcParameters, propagatedContingencies, - operatorStrategies, actionsById, neededActions, limitViolationPerContingency, branchResultsPerContingency); - } + return lfNetworks.getLargest().filter(LfNetwork::isValid) + .map(largestNetwork -> runActionSimulations(context, largestNetwork, dcParameters, propagatedContingencies, + operatorStrategies, actionsById, neededActions) + ).orElse(Collections.emptyList()); } } @@ -255,54 +267,76 @@ private static double currentActivePower(double activePower, double voltage, dou return 1000 * activePower / (Math.sqrt(3) * cosPhi * voltage); } - private void contingencyAndActionSimulation(DcSecurityAnalysisContext context, LfNetwork lfNetwork, DcLoadFlowParameters parameters, + private List runActionSimulations(DcSecurityAnalysisContext context, LfNetwork lfNetwork, DcLoadFlowParameters parameters, List propagatedContingencies, List operatorStrategies, - Map actionsById, Set neededActions, - Map limitViolationPerContingency, - Map> postContingencyBranchResults) { + Map actionsById, Set neededActions) { + // Run initial load flow and save state DcLoadFlowContext lfContext = new DcLoadFlowContext(lfNetwork, parameters); new DcLoadFlowEngine(lfContext).run(); NetworkState networkState = NetworkState.save(lfNetwork); + + OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters()); + boolean createResultExtension = openSecurityAnalysisParameters.isCreateResultExtension(); + var preContingencyLimitViolationManager = new LimitViolationManager(); preContingencyLimitViolationManager.detectViolations(lfNetwork); - Iterator contingencyIt = propagatedContingencies.iterator(); - Map> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions); Map lfActionById = createLfActions(lfNetwork, neededActions, network, false); - OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters()); + Iterator contingencyIt = propagatedContingencies.iterator(); + List operatorStrategyResults = new ArrayList<>(); while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) { PropagatedContingency propagatedContingency = contingencyIt.next(); - propagatedContingency.toLfContingency(lfNetwork) + List operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId()); + + if (operatorStrategiesForThisContingency == null) { + break; + } + for (OperatorStrategy operatorStrategy : operatorStrategiesForThisContingency) { + if (checkCondition(operatorStrategy, context.getLimitViolationsPerContingencyId().get(propagatedContingency.getContingency().getId()))) { + propagatedContingency.toLfContingency(lfNetwork) .ifPresent(lfContingency -> { lfContingency.apply(context.getParameters().getLoadFlowParameters().getBalanceType()); - ConnectivityResult connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, - lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, lfContingency.getDisconnectedElementIds()); - - LimitViolationsResult violationsResult = limitViolationPerContingency.get(propagatedContingency.getContingency().getId()); - //Create the contingency result - context.getPostContingencyResults().add(new PostContingencyResult(propagatedContingency.getContingency(), PostContingencyComputationStatus.CONVERGED, violationsResult, - postContingencyBranchResults.get(propagatedContingency.getContingency().getId()), Collections.emptyList(), Collections.emptyList(), connectivityResult)); - - List operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId()); - if (operatorStrategiesForThisContingency != null) { - for (OperatorStrategy operatorStrategy : operatorStrategiesForThisContingency) { - if (checkCondition(operatorStrategy, violationsResult)) { - NetworkState networkStatePostContingency = NetworkState.save(lfNetwork); - //Iterate on the action for this contingency - //Now do we have to run an action ? - context.getOperatorStrategyResults().add( - runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(), - lfActionById, openSecurityAnalysisParameters.isCreateResultExtension(), lfContingency, parameters.getBalanceType())); - - networkStatePostContingency.restore(); - } - } - } + OperatorStrategyResult result = runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(), + lfActionById, createResultExtension, lfContingency, parameters.getBalanceType()); + operatorStrategyResults.add(result); networkState.restore(); }); + } + } + } + + completeConnectivityResults(context, lfNetwork, propagatedContingencies, networkState); + + return operatorStrategyResults; + } + + private void completeConnectivityResults(DcSecurityAnalysisContext context, LfNetwork lfNetwork, + List propagatedContingencies, + NetworkState networkState) { + + // some connectivity results have not been built yet and we have to. + // after some contingencies, no operator strategies have been applied. + // we apply the contingency on the network just to complete the connectivity results. + Iterator contingencyIt = propagatedContingencies.iterator(); + while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) { + PropagatedContingency propagatedContingency = contingencyIt.next(); + context.getConnectivityResultPerContingencyId().computeIfAbsent(propagatedContingency.getContingency().getId(), id -> + propagatedContingency.toLfContingency(lfNetwork) + .map(lfContingency -> { + lfContingency.apply(context.getParameters().getLoadFlowParameters().getBalanceType()); + // we build the connectivity result linked to this contingency by opportunity. + ConnectivityResult connectivityResult = new ConnectivityResult(lfContingency.getCreatedSynchronousComponentsCount(), 0, + lfContingency.getDisconnectedLoadActivePower() * PerUnit.SB, + lfContingency.getDisconnectedGenerationActivePower() * PerUnit.SB, + lfContingency.getDisconnectedElementIds()); + networkState.restore(); + return connectivityResult; + }) + .orElse(new ConnectivityResult(0, 0, 0, 0, Collections.emptySet())) + ); } } From b317e4369bfaca27c6d8d5064657cbf1cc0747ab Mon Sep 17 00:00:00 2001 From: Anne Tilloy Date: Fri, 27 Jan 2023 09:58:00 +0100 Subject: [PATCH 15/15] Fix. Signed-off-by: Anne Tilloy --- .../java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java index 0f3b330b1b..be0e2780cf 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java @@ -245,8 +245,8 @@ private List createOperatorStrategyResults(DcSecurityAna try (LfNetworkList lfNetworks = Networks.load(network, dcParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, Reporter.NO_OP)) { return lfNetworks.getLargest().filter(LfNetwork::isValid) .map(largestNetwork -> runActionSimulations(context, largestNetwork, dcParameters, propagatedContingencies, - operatorStrategies, actionsById, neededActions) - ).orElse(Collections.emptyList()); + operatorStrategies, actionsById, neededActions)) + .orElse(Collections.emptyList()); } }