Skip to content

Commit e711711

Browse files
committed
Vectorize equation status
Signed-off-by: Didier Vidal <[email protected]>
1 parent ea1084b commit e711711

File tree

5 files changed

+58
-33
lines changed

5 files changed

+58
-33
lines changed

src/main/java/com/powsybl/openloadflow/ac/equations/vector/AcVectorEngine.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public class AcVectorEngine implements StateVectorListener, EquationSystemListen
8383

8484
// indexes to compute derivatives
8585
private boolean equationDataValid;
86-
private Equation<AcVariableType, AcEquationType>[] equations;
8786
private int[] variableCountPerEquation;
8887
private int[] variablePerEquationIndex;
8988
private Variable<AcVariableType>[] variablesPerEquation;
@@ -93,7 +92,11 @@ public class AcVectorEngine implements StateVectorListener, EquationSystemListen
9392
private EquationTerm<AcVariableType, AcEquationType>[] termsByVariableAndEquation;
9493
private int[] termStatusByVariableAndEquationsIndex;
9594

96-
// terma replicated data
95+
// equation replicated data
96+
private boolean[] equationActiveStatus;
97+
private int[] equationColumn;
98+
99+
// term replicated data
97100
private boolean[] termActiveStatus;
98101

99102
public interface VecToVal {
@@ -190,7 +193,14 @@ public void onEquationChange(Equation equation, EquationEventType eventType) {
190193
break;
191194
case EQUATION_ACTIVATED:
192195
case EQUATION_DEACTIVATED:
193-
// nothing to do
196+
if (equation.getVectorIndex() >= 0) {
197+
equationActiveStatus[equation.getVectorIndex()] = equation.isActive();
198+
}
199+
case EQUATION_COLUMN_CHANGED:
200+
if (equation.getVectorIndex() >= 0) {
201+
equationColumn[equation.getVectorIndex()] = equation.getColumn();
202+
}
203+
break;
194204
}
195205
}
196206

@@ -311,17 +321,20 @@ private void initEquationData() {
311321

312322
Collection<Equation<AcVariableType, AcEquationType>> equationList = equationSystem.getEquations();
313323
int equationCount = equationList.size();
314-
equations = new Equation[equationCount];
315324
variableCountPerEquation = new int[equationCount];
325+
equationActiveStatus = new boolean[equationCount];
326+
equationColumn = new int[equationCount];
316327
int index = 0;
317328
int variableIndexSize = 0;
318329
int termCount = 0;
319330
for (Equation<AcVariableType, AcEquationType> e : equationList) {
331+
e.setVectorIndex(index);
332+
equationActiveStatus[index] = e.isActive();
333+
equationColumn[index] = e.getColumn();
320334
int equationVariableCount = e.getVariableCount();
321335
variableIndexSize += equationVariableCount;
322336
variableCountPerEquation[index] = equationVariableCount;
323337
termCount += e.getTerms().size();
324-
this.equations[index] = e;
325338
index += 1;
326339
}
327340
termActiveStatus = new boolean[termCount];
@@ -364,13 +377,13 @@ private void initEquationData() {
364377

365378
@Override
366379
public void der(boolean update, Matrix matrix) {
367-
int[] sortedEquationIndexArray = IntStream.range(0, equations.length).boxed()
368-
.sorted((i1, i2) -> equations[i1].getColumn() - equations[i2].getColumn())
380+
int[] sortedEquationIndexArray = IntStream.range(0, equationActiveStatus.length).boxed()
381+
.sorted((i1, i2) -> equationColumn[i1] - equationColumn[i2])
369382
.mapToInt(i -> i).toArray();
370-
for (int sortedEqIndex = 0; sortedEqIndex < equations.length; sortedEqIndex++) {
383+
for (int sortedEqIndex = 0; sortedEqIndex < equationActiveStatus.length; sortedEqIndex++) {
371384
int eqIndex = sortedEquationIndexArray[sortedEqIndex];
372-
if (equations[eqIndex].isActive()) {
373-
int col = equations[eqIndex].getColumn();
385+
if (equationActiveStatus[eqIndex]) {
386+
int col = equationColumn[eqIndex];
374387
int varEnd = variablePerEquationIndex[eqIndex] + variableCountPerEquation[eqIndex];
375388
for (int varIndex = variablePerEquationIndex[eqIndex]; varIndex < varEnd; varIndex++) {
376389
Variable<AcVariableType> v = variablesPerEquation[varIndex];
@@ -382,20 +395,7 @@ public void der(boolean update, Matrix matrix) {
382395
termIndex < termEnd;
383396
termIndex++) {
384397
if (termActiveStatus[termStatusByVariableAndEquationsIndex[termIndex]]) {
385-
value += termsByVariableAndEquation[termIndex].der(v);
386-
}
387-
}
388-
boolean debug = false;
389-
if (debug) {
390-
double[] hack = new double[1];
391-
equations[eqIndex].der((var, val, matrixElement) -> {
392-
if (var == v) {
393-
hack[0] = val;
394-
}
395-
return 0;
396-
});
397-
if (value != hack[0]) {
398-
throw new IllegalStateException("different result");
398+
value += callTermDer(termIndex, v);
399399
}
400400
}
401401
if (update) {
@@ -408,4 +408,9 @@ public void der(boolean update, Matrix matrix) {
408408
}
409409
}
410410
}
411+
412+
// Isolate for profileing
413+
private double callTermDer(int termIndex, Variable<AcVariableType> v) {
414+
return termsByVariableAndEquation[termIndex].der(v);
415+
}
411416
}

src/main/java/com/powsybl/openloadflow/equations/Equation.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class Equation<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity
3030

3131
private int column = -1;
3232

33+
private int vectorIndex = -1;
34+
3335
/**
3436
* true if this equation term active, false otherwise
3537
*/
@@ -81,6 +83,18 @@ public int getColumn() {
8183

8284
public void setColumn(int column) {
8385
this.column = column;
86+
87+
if (equationSystem != null) {
88+
equationSystem.notifyEquationChange(this, EquationEventType.EQUATION_COLUMN_CHANGED);
89+
}
90+
}
91+
92+
public int getVectorIndex() {
93+
return vectorIndex;
94+
}
95+
96+
public void setVectorIndex(int vectorIndex) {
97+
this.vectorIndex = vectorIndex;
8498
}
8599

86100
public boolean isActive() {

src/main/java/com/powsybl/openloadflow/equations/EquationEventType.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (c) 2019, RTE (http://www.rte-france.com)
1+
/*
2+
* Copyright (c) 2019-2025, RTE (http://www.rte-france.com)
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -14,5 +14,6 @@ public enum EquationEventType {
1414
EQUATION_CREATED,
1515
EQUATION_REMOVED,
1616
EQUATION_ACTIVATED,
17-
EQUATION_DEACTIVATED;
17+
EQUATION_DEACTIVATED,
18+
EQUATION_COLUMN_CHANGED;
1819
}

src/main/java/com/powsybl/openloadflow/equations/EquationSystemIndex.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (c) 2022, RTE (http://www.rte-france.com)
1+
/*
2+
* Copyright (c) 2022-2025, RTE (http://www.rte-france.com)
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -160,6 +160,9 @@ public void onEquationChange(Equation<V, E> equation, EquationEventType eventTyp
160160
addEquation(equation);
161161
break;
162162

163+
case EQUATION_COLUMN_CHANGED:
164+
break;
165+
163166
default:
164167
throw new IllegalStateException("Event type not supported: " + eventType);
165168
}

src/test/java/com/powsybl/openloadflow/equations/EquationSystemTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (c) 2019, RTE (http://www.rte-france.com)
1+
/*
2+
* Copyright (c) 2019-2025, RTE (http://www.rte-france.com)
33
* This Source Code Form is subject to the terms of the Mozilla Public
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
@@ -53,8 +53,10 @@ void test() {
5353
equationSystem.addListener(new EquationSystemListener<>() {
5454
@Override
5555
public void onEquationChange(Equation<AcVariableType, AcEquationType> equation, EquationEventType eventType) {
56-
equations.add(equation);
57-
equationEventTypes.add(eventType);
56+
if (eventType != EquationEventType.EQUATION_COLUMN_CHANGED) {
57+
equations.add(equation);
58+
equationEventTypes.add(eventType);
59+
}
5860
}
5961

6062
@Override

0 commit comments

Comments
 (0)