Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix too large dense matrix allocation issue #822

Merged
merged 7 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,22 @@ protected List<ParticipatingElement> getParticipatingElements(Collection<LfBus>
return participatingElements;
}

protected DenseMatrix initFactorsRhs(EquationSystem<V, E> equationSystem, SensitivityFactorGroupList<V, E> factorsGroups, Map<LfBus, Double> participationByBus) {
DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), factorsGroups.getList().size());
static <V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> DenseMatrix initFactorsRhs(EquationSystem<V, E> equationSystem, SensitivityFactorGroupList<V, E> factorsGroups, Map<LfBus, Double> participationByBus) {
// otherwise, defining the rhs matrix will result in integer overflow
int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size();
int factorsGroupCount = factorsGroups.getList().size();
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);
fillRhsSensitivityVariable(factorsGroups, rhs, participationByBus);
return rhs;
}

protected void fillRhsSensitivityVariable(SensitivityFactorGroupList<V, E> factorGroups, Matrix rhs, Map<LfBus, Double> participationByBus) {
protected static <V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> void fillRhsSensitivityVariable(SensitivityFactorGroupList<V, E> factorGroups, Matrix rhs, Map<LfBus, Double> participationByBus) {
for (SensitivityFactorGroup<V, E> factorGroup : factorGroups.getList()) {
factorGroup.fillRhs(rhs, participationByBus);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,6 @@ public void analyse(Network network, List<PropagatedContingency> 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class DcSensitivityAnalysis extends AbstractSensitivityAnalysis<DcVariabl

private static final double CONNECTIVITY_LOSS_THRESHOLD = 10e-7;

private static final class ComputedContingencyElement {
static final class ComputedContingencyElement {

private int contingencyIndex = -1; // index of the element in the rhs for +1-1
private int localIndex = -1; // local index of the element : index of the element in the matrix used in the setAlphas method
Expand Down Expand Up @@ -429,8 +429,16 @@ private static void fillRhsContingency(LfNetwork lfNetwork, EquationSystem<DcVar
}
}

private static DenseMatrix initContingencyRhs(LfNetwork lfNetwork, EquationSystem<DcVariableType, DcEquationType> equationSystem, Collection<ComputedContingencyElement> contingencyElements) {
DenseMatrix rhs = new DenseMatrix(equationSystem.getIndex().getSortedEquationsToSolve().size(), contingencyElements.size());
static DenseMatrix initContingencyRhs(LfNetwork lfNetwork, EquationSystem<DcVariableType, DcEquationType> equationSystem, Collection<ComputedContingencyElement> contingencyElements) {
// otherwise, defining the rhs matrix will result in integer overflow
int equationCount = equationSystem.getIndex().getSortedEquationsToSolve().size();
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());
fillRhsContingency(lfNetwork, equationSystem, contingencyElements, rhs);
return rhs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
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.util.LoadFlowAssert;
import com.powsybl.sensitivity.*;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -1007,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<DcVariableType, DcEquationType> equationSystem = Mockito.mock(EquationSystem.class);
EquationSystemIndex<DcVariableType, DcEquationType> equationSystemIndex = Mockito.mock(EquationSystemIndex.class);
Mockito.when(equationSystem.getIndex()).thenReturn(equationSystemIndex);
List<Equation<DcVariableType, DcEquationType>> equations = Mockito.mock(List.class);
Mockito.when(equations.size()).thenReturn(100000);
Mockito.when(equationSystemIndex.getSortedEquationsToSolve()).thenReturn(equations);
AbstractSensitivityAnalysis.SensitivityFactorGroupList<DcVariableType, DcEquationType> factorsGroups = Mockito.mock(AbstractSensitivityAnalysis.SensitivityFactorGroupList.class);
List<AbstractSensitivityAnalysis.SensitivityFactorGroup<DcVariableType, DcEquationType>> factorGroupList = Mockito.mock(List.class);
Mockito.when(factorGroupList.size()).thenReturn(3333333);
Mockito.when(factorsGroups.getList()).thenReturn(factorGroupList);
Map<LfBus, Double> participationByBus = Collections.emptyMap();
PowsyblException e = assertThrows(PowsyblException.class, () -> AbstractSensitivityAnalysis.initFactorsRhs(equationSystem, factorsGroups, participationByBus));
assertEquals("Too many factors groups 3333333, maximum is 2684 for a system with 100000 equations", e.getMessage());

LfNetwork network = Mockito.mock(LfNetwork.class);
List<DcSensitivityAnalysis.ComputedContingencyElement> contingencyElements = Mockito.mock(List.class);
Mockito.when(contingencyElements.size()).thenReturn(999999);
e = assertThrows(PowsyblException.class, () -> DcSensitivityAnalysis.initContingencyRhs(network, equationSystem, contingencyElements));
assertEquals("Too many contingency elements 999999, maximum is 2684 for a system with 100000 equations", e.getMessage());
}
}