From 948ae8210e7623dcb7b98f66e3b6257a9f411198 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 9 Aug 2023 22:34:36 +0200 Subject: [PATCH 1/5] Fix too large dense matrix allocation issue Signed-off-by: Geoffroy Jamgotchian --- .../sensi/AbstractSensitivityAnalysis.java | 10 +++++++++- .../openloadflow/sensi/AcSensitivityAnalysis.java | 5 ----- .../openloadflow/sensi/DcSensitivityAnalysis.java | 9 ++++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java index 6207a19809..be9e3a1a7e 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java @@ -628,7 +628,15 @@ protected List getParticipatingElements(Collection } protected DenseMatrix initFactorsRhs(EquationSystem equationSystem, SensitivityFactorGroupList factorsGroups, Map participationByBus) { - DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), factorsGroups.getList().size()); + // otherwise, defining the rhs matrix will result in integer overflow + int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); + int factorsGroupCount = factorsGroups.getList().size(); + if (factorsGroupCount >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { + throw new PowsyblException("So many factors groups (" + factorsGroupCount + + ") is not allowed for a system with " + equationCount + " equations"); + } + + DenseMatrix rhs = new DenseMatrix(equationCount, factorsGroupCount); fillRhsSensitivityVariable(factorsGroups, rhs, participationByBus); return rhs; } diff --git a/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java index 6c2c596adb..45d6d7bd58 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysis.java @@ -279,11 +279,6 @@ public void analyse(Network network, List contingencies, // we make the assumption that we ran a loadflow before, and thus this jacobian is the right one - // otherwise, defining the rhs matrix will result in integer overflow - if (factorGroups.getList().size() >= Integer.MAX_VALUE / (context.getEquationSystem().getIndex().getSortedEquationsToSolve().size() * Double.BYTES)) { - throw new PowsyblException("Too many factors!"); - } - // initialize right hand side from valid factors DenseMatrix factorsStates = initFactorsRhs(context.getEquationSystem(), factorGroups, slackParticipationByBus); // this is the rhs for the moment diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index 6d8390c79b..e5578aa11d 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -425,7 +425,14 @@ protected static void fillRhsContingency(LfNetwork lfNetwork, EquationSystem equationSystem, Collection contingencyElements) { - DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), contingencyElements.size()); + // otherwise, defining the rhs matrix will result in integer overflow + int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); + if (contingencyElements.size() >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { + throw new PowsyblException("So many contingency elements (" + contingencyElements.size() + + ") is not allowed for a system with " + equationCount + " equations"); + } + + DenseMatrix rhs = new DenseMatrix(equationCount, contingencyElements.size()); fillRhsContingency(lfNetwork, equationSystem, contingencyElements, rhs); return rhs; } From b610e507fab84b344190e434597e1590fffa534e Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 9 Aug 2023 23:21:19 +0200 Subject: [PATCH 2/5] Add unit test Signed-off-by: Geoffroy Jamgotchian --- .../sensi/AbstractSensitivityAnalysis.java | 10 ++--- .../sensi/DcSensitivityAnalysis.java | 4 +- .../sensi/dc/DcSensitivityAnalysisTest.java | 41 +++++++++++++++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java index be9e3a1a7e..fe08e0cdd6 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java @@ -60,7 +60,7 @@ protected AbstractSensitivityAnalysis(MatrixFactory matrixFactory, GraphConnecti this.parameters = Objects.requireNonNull(parameters); } - interface LfSensitivityFactor & Quantity, E extends Enum & Quantity> { + public interface LfSensitivityFactor & Quantity, E extends Enum & Quantity> { enum Status { VALID, @@ -414,7 +414,7 @@ public boolean isVariableInContingency(PropagatedContingency contingency) { } } - interface SensitivityFactorGroup & Quantity, E extends Enum & Quantity> { + public interface SensitivityFactorGroup & Quantity, E extends Enum & Quantity> { List> getFactors(); @@ -574,7 +574,7 @@ boolean updateConnectivityWeights(Set nonConnectedBuses) { } } - protected static class SensitivityFactorGroupList & Quantity, E extends Enum & Quantity> { + public static class SensitivityFactorGroupList & Quantity, E extends Enum & Quantity> { private final List> list; @@ -627,7 +627,7 @@ protected List getParticipatingElements(Collection return participatingElements; } - protected DenseMatrix initFactorsRhs(EquationSystem equationSystem, SensitivityFactorGroupList factorsGroups, Map participationByBus) { + public static & Quantity, E extends Enum & Quantity> DenseMatrix initFactorsRhs(EquationSystem equationSystem, SensitivityFactorGroupList factorsGroups, Map participationByBus) { // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); int factorsGroupCount = factorsGroups.getList().size(); @@ -641,7 +641,7 @@ protected DenseMatrix initFactorsRhs(EquationSystem equationSystem, Sensit return rhs; } - protected void fillRhsSensitivityVariable(SensitivityFactorGroupList factorGroups, Matrix rhs, Map participationByBus) { + protected static & Quantity, E extends Enum & Quantity> void fillRhsSensitivityVariable(SensitivityFactorGroupList factorGroups, Matrix rhs, Map participationByBus) { for (SensitivityFactorGroup factorGroup : factorGroups.getList()) { factorGroup.fillRhs(rhs, participationByBus); } diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index e5578aa11d..363b6ef6a1 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -58,7 +58,7 @@ public class DcSensitivityAnalysis extends AbstractSensitivityAnalysis equationSystem, Collection contingencyElements) { + public static DenseMatrix initContingencyRhs(LfNetwork lfNetwork, EquationSystem equationSystem, Collection contingencyElements) { // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); if (contingencyElements.size() >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { diff --git a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java index ae6e987311..2beaec9d08 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java @@ -15,20 +15,22 @@ import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import com.powsybl.iidm.network.test.PhaseShifterTestCaseFactory; import com.powsybl.loadflow.LoadFlowParameters; -import com.powsybl.openloadflow.network.BoundaryFactory; -import com.powsybl.openloadflow.network.FourBusNetworkFactory; -import com.powsybl.openloadflow.network.HvdcNetworkFactory; -import com.powsybl.openloadflow.network.NodeBreakerNetworkFactory; +import com.powsybl.openloadflow.dc.equations.DcEquationType; +import com.powsybl.openloadflow.dc.equations.DcVariableType; +import com.powsybl.openloadflow.equations.Equation; +import com.powsybl.openloadflow.equations.EquationSystem; +import com.powsybl.openloadflow.equations.EquationSystemIndex; +import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.network.impl.PropagatedContingency; +import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysis; import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; +import com.powsybl.openloadflow.sensi.DcSensitivityAnalysis; import com.powsybl.openloadflow.util.LoadFlowAssert; import com.powsybl.sensitivity.*; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.CompletionException; import java.util.stream.Collectors; @@ -1008,4 +1010,27 @@ void testWithTieLines2() { assertTrue(e.getCause() instanceof PowsyblException); assertEquals("Branch, tie line, dangling line or leg of 'h1' not found", e.getCause().getMessage()); } + + @Test + void testTooManyFactorsAndContingencies() { + EquationSystem equationSystem = Mockito.mock(EquationSystem.class); + EquationSystemIndex equationSystemIndex = Mockito.mock(EquationSystemIndex.class); + Mockito.when(equationSystem.getIndex()).thenReturn(equationSystemIndex); + List> equations = Mockito.mock(List.class); + Mockito.when(equations.size()).thenReturn(100000); + Mockito.when(equationSystemIndex.getSortedEquationsToSolve()).thenReturn(equations); + AbstractSensitivityAnalysis.SensitivityFactorGroupList factorsGroups = Mockito.mock(AbstractSensitivityAnalysis.SensitivityFactorGroupList.class); + List> factorGroupList = Mockito.mock(List.class); + Mockito.when(factorGroupList.size()).thenReturn(3333333); + Mockito.when(factorsGroups.getList()).thenReturn(factorGroupList); + Map participationByBus = Collections.emptyMap(); + PowsyblException e = assertThrows(PowsyblException.class, () -> AbstractSensitivityAnalysis.initFactorsRhs(equationSystem, factorsGroups, participationByBus)); + assertEquals("So many factors groups (3333333) is not allowed for a system with 100000 equations", e.getMessage()); + + LfNetwork network = Mockito.mock(LfNetwork.class); + List contingencyElements = Mockito.mock(List.class); + Mockito.when(contingencyElements.size()).thenReturn(999999); + e = assertThrows(PowsyblException.class, () -> DcSensitivityAnalysis.initContingencyRhs(network, equationSystem, contingencyElements)); + assertEquals("So many contingency elements (999999) is not allowed for a system with 100000 equations", e.getMessage()); + } } From 9a14d707f259964a5a59df548a0f28ad72e5af86 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Wed, 9 Aug 2023 23:26:04 +0200 Subject: [PATCH 3/5] Clean Signed-off-by: Geoffroy Jamgotchian --- .../sensi/AbstractSensitivityAnalysis.java | 8 ++++---- .../openloadflow/sensi/DcSensitivityAnalysis.java | 4 ++-- .../AcSensitivityAnalysisContingenciesTest.java | 13 +++++++++---- .../{ac => }/AcSensitivityAnalysisReportTest.java | 3 +-- .../sensi/{ac => }/AcSensitivityAnalysisTest.java | 4 +--- .../DcSensitivityAnalysisContingenciesTest.java | 4 +--- .../sensi/{dc => }/DcSensitivityAnalysisTest.java | 10 +++++----- 7 files changed, 23 insertions(+), 23 deletions(-) rename src/test/java/com/powsybl/openloadflow/sensi/{ac => }/AcSensitivityAnalysisContingenciesTest.java (99%) rename src/test/java/com/powsybl/openloadflow/sensi/{ac => }/AcSensitivityAnalysisReportTest.java (96%) rename src/test/java/com/powsybl/openloadflow/sensi/{ac => }/AcSensitivityAnalysisTest.java (99%) rename src/test/java/com/powsybl/openloadflow/sensi/{dc => }/DcSensitivityAnalysisContingenciesTest.java (99%) rename src/test/java/com/powsybl/openloadflow/sensi/{dc => }/DcSensitivityAnalysisTest.java (99%) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java index fe08e0cdd6..7b92250508 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java @@ -60,7 +60,7 @@ protected AbstractSensitivityAnalysis(MatrixFactory matrixFactory, GraphConnecti this.parameters = Objects.requireNonNull(parameters); } - public interface LfSensitivityFactor & Quantity, E extends Enum & Quantity> { + interface LfSensitivityFactor & Quantity, E extends Enum & Quantity> { enum Status { VALID, @@ -414,7 +414,7 @@ public boolean isVariableInContingency(PropagatedContingency contingency) { } } - public interface SensitivityFactorGroup & Quantity, E extends Enum & Quantity> { + interface SensitivityFactorGroup & Quantity, E extends Enum & Quantity> { List> getFactors(); @@ -574,7 +574,7 @@ boolean updateConnectivityWeights(Set nonConnectedBuses) { } } - public static class SensitivityFactorGroupList & Quantity, E extends Enum & Quantity> { + protected static class SensitivityFactorGroupList & Quantity, E extends Enum & Quantity> { private final List> list; @@ -627,7 +627,7 @@ protected List getParticipatingElements(Collection return participatingElements; } - public static & Quantity, E extends Enum & Quantity> DenseMatrix initFactorsRhs(EquationSystem equationSystem, SensitivityFactorGroupList factorsGroups, Map participationByBus) { + static & Quantity, E extends Enum & Quantity> DenseMatrix initFactorsRhs(EquationSystem equationSystem, SensitivityFactorGroupList factorsGroups, Map participationByBus) { // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); int factorsGroupCount = factorsGroups.getList().size(); diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index 363b6ef6a1..21c89ca443 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -58,7 +58,7 @@ public class DcSensitivityAnalysis extends AbstractSensitivityAnalysis equationSystem, Collection contingencyElements) { + static DenseMatrix initContingencyRhs(LfNetwork lfNetwork, EquationSystem equationSystem, Collection contingencyElements) { // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); if (contingencyElements.size() >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { diff --git a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisContingenciesTest.java b/src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisContingenciesTest.java similarity index 99% rename from src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisContingenciesTest.java rename to src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisContingenciesTest.java index 589d5f6fa1..e9d1c419a6 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisContingenciesTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisContingenciesTest.java @@ -4,24 +4,29 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package com.powsybl.openloadflow.sensi.ac; +package com.powsybl.openloadflow.sensi; import com.powsybl.commons.PowsyblException; import com.powsybl.commons.reporter.Reporter; import com.powsybl.contingency.*; -import com.powsybl.iidm.network.*; +import com.powsybl.iidm.network.Branch; +import com.powsybl.iidm.network.Line; +import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.StaticVarCompensator; import com.powsybl.iidm.network.extensions.HvdcAngleDroopActivePowerControlAdder; import com.powsybl.iidm.network.extensions.StandbyAutomatonAdder; import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import com.powsybl.loadflow.LoadFlowParameters; import com.powsybl.openloadflow.OpenLoadFlowParameters; import com.powsybl.openloadflow.network.*; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; import com.powsybl.openloadflow.util.LoadFlowAssert; import com.powsybl.sensitivity.*; import org.junit.jupiter.api.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletionException; import java.util.stream.Collectors; import java.util.stream.Stream; diff --git a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisReportTest.java b/src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisReportTest.java similarity index 96% rename from src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisReportTest.java rename to src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisReportTest.java index 2335557513..a182fac549 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisReportTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisReportTest.java @@ -4,7 +4,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package com.powsybl.openloadflow.sensi.ac; +package com.powsybl.openloadflow.sensi; import com.powsybl.commons.reporter.ReporterModel; import com.powsybl.computation.local.LocalComputationManager; @@ -14,7 +14,6 @@ import com.powsybl.loadflow.LoadFlowParameters; import com.powsybl.openloadflow.OpenLoadFlowParameters; import com.powsybl.openloadflow.network.EurostagFactory; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; import com.powsybl.sensitivity.SensitivityAnalysisParameters; import com.powsybl.sensitivity.SensitivityFactor; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisTest.java similarity index 99% rename from src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisTest.java rename to src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisTest.java index 7b238a10a6..9c94135e8a 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/ac/AcSensitivityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/AcSensitivityAnalysisTest.java @@ -4,7 +4,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package com.powsybl.openloadflow.sensi.ac; +package com.powsybl.openloadflow.sensi; import com.powsybl.commons.PowsyblException; import com.powsybl.contingency.Contingency; @@ -17,8 +17,6 @@ import com.powsybl.loadflow.LoadFlowParameters; import com.powsybl.openloadflow.OpenLoadFlowParameters; import com.powsybl.openloadflow.network.*; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysis; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; import com.powsybl.openloadflow.util.LoadFlowAssert; import com.powsybl.sensitivity.*; import org.junit.jupiter.api.Test; diff --git a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisContingenciesTest.java b/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisContingenciesTest.java similarity index 99% rename from src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisContingenciesTest.java rename to src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisContingenciesTest.java index 59c086e99c..9d55d5e261 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisContingenciesTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisContingenciesTest.java @@ -4,7 +4,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package com.powsybl.openloadflow.sensi.dc; +package com.powsybl.openloadflow.sensi; import com.powsybl.commons.PowsyblException; import com.powsybl.commons.config.PlatformConfig; @@ -18,8 +18,6 @@ import com.powsybl.loadflow.LoadFlowParameters; import com.powsybl.openloadflow.OpenLoadFlowParameters; import com.powsybl.openloadflow.network.*; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.openloadflow.sensi.OpenSensitivityAnalysisParameters; import com.powsybl.openloadflow.util.DebugUtil; import com.powsybl.openloadflow.util.LoadFlowAssert; import com.powsybl.sensitivity.*; diff --git a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java similarity index 99% rename from src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java rename to src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java index 2beaec9d08..e04309bb3a 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/dc/DcSensitivityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java @@ -4,7 +4,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -package com.powsybl.openloadflow.sensi.dc; +package com.powsybl.openloadflow.sensi; import com.powsybl.commons.PowsyblException; import com.powsybl.contingency.BranchContingency; @@ -22,15 +22,15 @@ import com.powsybl.openloadflow.equations.EquationSystemIndex; import com.powsybl.openloadflow.network.*; import com.powsybl.openloadflow.network.impl.PropagatedContingency; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysis; -import com.powsybl.openloadflow.sensi.AbstractSensitivityAnalysisTest; -import com.powsybl.openloadflow.sensi.DcSensitivityAnalysis; import com.powsybl.openloadflow.util.LoadFlowAssert; import com.powsybl.sensitivity.*; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.CompletionException; import java.util.stream.Collectors; From 146ab096ae0a0ac76a1b58da5c06380e846305f9 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Sun, 13 Aug 2023 21:53:39 +0200 Subject: [PATCH 4/5] Fix Signed-off-by: Geoffroy Jamgotchian --- .../com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index cf74ac27e3..9473b5f01a 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -58,7 +58,7 @@ public class DcSensitivityAnalysis extends AbstractSensitivityAnalysis equationSystem, Collection contingencyElements) { + static DenseMatrix initContingencyRhs(LfNetwork lfNetwork, EquationSystem equationSystem, Collection contingencyElements) { // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); if (contingencyElements.size() >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { From dd3ea36d68b89727f19b2afe0ac339989b21a26f Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Mon, 14 Aug 2023 10:51:31 +0200 Subject: [PATCH 5/5] Improve message Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/sensi/AbstractSensitivityAnalysis.java | 7 ++++--- .../powsybl/openloadflow/sensi/DcSensitivityAnalysis.java | 7 ++++--- .../openloadflow/sensi/DcSensitivityAnalysisTest.java | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java index 3b565162c6..3a3238b9b0 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/AbstractSensitivityAnalysis.java @@ -631,9 +631,10 @@ static & Quantity, E extends Enum & Quantity> DenseMatrix // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); int factorsGroupCount = factorsGroups.getList().size(); - if (factorsGroupCount >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { - throw new PowsyblException("So many factors groups (" + factorsGroupCount - + ") is not allowed for a system with " + equationCount + " equations"); + int maxFactorsGroups = Integer.MAX_VALUE / (equationCount * Double.BYTES); + if (factorsGroupCount > maxFactorsGroups) { + throw new PowsyblException("Too many factors groups " + factorsGroupCount + + ", maximum is " + maxFactorsGroups + " for a system with " + equationCount + " equations"); } DenseMatrix rhs = new DenseMatrix(equationCount, factorsGroupCount); diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index 9473b5f01a..b7378a5bc9 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -429,9 +429,10 @@ private static void fillRhsContingency(LfNetwork lfNetwork, EquationSystem equationSystem, Collection contingencyElements) { // otherwise, defining the rhs matrix will result in integer overflow int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size(); - if (contingencyElements.size() >= Integer.MAX_VALUE / (equationCount * Double.BYTES)) { - throw new PowsyblException("So many contingency elements (" + contingencyElements.size() - + ") is not allowed for a system with " + equationCount + " equations"); + int maxContingencyElements = Integer.MAX_VALUE / (equationCount * Double.BYTES); + if (contingencyElements.size() > maxContingencyElements) { + throw new PowsyblException("Too many contingency elements " + contingencyElements.size() + + ", maximum is " + maxContingencyElements + " for a system with " + equationCount + " equations"); } DenseMatrix rhs = new DenseMatrix(equationCount, contingencyElements.size()); diff --git a/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java b/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java index e04309bb3a..222fb2126f 100644 --- a/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java +++ b/src/test/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysisTest.java @@ -1025,12 +1025,12 @@ void testTooManyFactorsAndContingencies() { Mockito.when(factorsGroups.getList()).thenReturn(factorGroupList); Map participationByBus = Collections.emptyMap(); PowsyblException e = assertThrows(PowsyblException.class, () -> AbstractSensitivityAnalysis.initFactorsRhs(equationSystem, factorsGroups, participationByBus)); - assertEquals("So many factors groups (3333333) is not allowed for a system with 100000 equations", e.getMessage()); + assertEquals("Too many factors groups 3333333, maximum is 2684 for a system with 100000 equations", e.getMessage()); LfNetwork network = Mockito.mock(LfNetwork.class); List contingencyElements = Mockito.mock(List.class); Mockito.when(contingencyElements.size()).thenReturn(999999); e = assertThrows(PowsyblException.class, () -> DcSensitivityAnalysis.initContingencyRhs(network, equationSystem, contingencyElements)); - assertEquals("So many contingency elements (999999) is not allowed for a system with 100000 equations", e.getMessage()); + assertEquals("Too many contingency elements 999999, maximum is 2684 for a system with 100000 equations", e.getMessage()); } }