Skip to content

Commit 50ae81a

Browse files
committed
Test some derivatives connected
Signed-off-by: Didier Vidal <[email protected]>
1 parent e711711 commit 50ae81a

10 files changed

+96
-120
lines changed

src/main/java/com/powsybl/openloadflow/ac/equations/AbstractBranchAcFlowEquationTerm.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ protected AbstractBranchAcFlowEquationTerm(LfBranch branch, AcVectorEngine acVec
4242
}
4343
}
4444

45-
public abstract void updateVectorSuppliers();
46-
4745
protected double b1() {
4846
return acVectorEnginee.b1[branchNum];
4947
}

src/main/java/com/powsybl/openloadflow/ac/equations/AbstractClosedBranchAcFlowEquationTerm.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222
import java.util.Objects;
23+
import java.util.function.DoubleSupplier;
2324

2425
import static com.powsybl.openloadflow.network.PiModel.A2;
2526

@@ -114,19 +115,19 @@ public void setEquation(Equation<AcVariableType, AcEquationType> equation) {
114115
}
115116
}
116117

117-
@Override
118-
public void updateVectorSuppliers() {
119-
// if a1 is not a variable, and cannot be changed as an input, store it, otherwise use a supplier
120-
if (a1Var != null || isArrayPiModel) {
121-
acVectorEnginee.a1Supplier[element.getNum()] = () -> a1();
118+
public DoubleSupplier getR1Supplier() {
119+
if (r1Var != null || isArrayPiModel) {
120+
return () -> r1();
122121
} else {
123-
acVectorEnginee.a1[element.getNum()] = a1;
122+
return null;
124123
}
125-
// if r1 is not a variable, and cannot be changed as an input, store it, otherwise use a supplier
126-
if (r1Var != null || isArrayPiModel) {
127-
acVectorEnginee.r1Supplier[element.getNum()] = () -> r1();
124+
}
125+
126+
public DoubleSupplier getA1Supplier() {
127+
if (a1Var != null || isArrayPiModel) {
128+
return () -> a1();
128129
} else {
129-
acVectorEnginee.r1[element.getNum()] = r1;
130+
return null;
130131
}
131132
}
132133

@@ -150,14 +151,14 @@ protected double ph2() {
150151
return sv.get(ph2Var.getRow());
151152
}
152153

153-
protected double r1() {
154+
public double r1() {
154155
// TODO: Remove test on var row - should not be called if term is inactive
155156
return r1Var != null && r1Var.getRow() >= 0 ? sv.get(r1Var.getRow()) :
156157
isArrayPiModel ? element.getPiModel().getR1() : r1;
157158
// to avoid memory cache miss we don't load the piModel if not necessary
158159
}
159160

160-
protected double a1() {
161+
public double a1() {
161162
// TODO remove test >0 - should not be called if term is inactive
162163
return a1Var != null && a1Var.getRow() >= 0 ? sv.get(a1Var.getRow()) :
163164
isArrayPiModel ? element.getPiModel().getA1() : a1;

src/main/java/com/powsybl/openloadflow/ac/equations/AbstractOpenSide1BranchAcFlowEquationTerm.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ protected AbstractOpenSide1BranchAcFlowEquationTerm(LfBranch branch, AcVariableT
2828
variables = List.of(variableSet.getVariable(bus.getNum(), variableType));
2929
}
3030

31-
@Override
32-
public void updateVectorSuppliers() {
33-
// Do nothing -- no vectorization implemented for now
34-
}
35-
3631
protected static double shunt(double y, double cosKsi, double sinKsi, double g1, double b1) {
3732
return (g1 + y * sinKsi) * (g1 + y * sinKsi) + (-b1 + y * cosKsi) * (-b1 + y * cosKsi);
3833
}

src/main/java/com/powsybl/openloadflow/ac/equations/ClosedBranchSide2ActiveFlowEquationTerm.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.powsybl.openloadflow.ac.equations.vector.AcVectorEngine;
1111
import com.powsybl.openloadflow.equations.Variable;
1212
import com.powsybl.openloadflow.equations.VariableSet;
13+
import com.powsybl.openloadflow.equations.VectorEngine;
1314
import com.powsybl.openloadflow.network.LfBranch;
1415
import com.powsybl.openloadflow.network.LfBus;
1516
import com.powsybl.openloadflow.util.Fortescue;
@@ -36,18 +37,22 @@ public ClosedBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, LfBu
3637
}
3738

3839
@Override
39-
public void updateVectorSuppliers() {
40-
super.updateVectorSuppliers();
41-
acVectorEnginee.vecToP2[element.getNum()] = ClosedBranchSide2ActiveFlowEquationTerm::vec2p2;
42-
acVectorEnginee.busDpDvVecToVal[acVectorEnginee.bus2D1PerLoc[element.getNum()]] = ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dv1;
43-
acVectorEnginee.busDpDvVecToVal[acVectorEnginee.bus2D2PerLoc[element.getNum()]] = ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dv2;
44-
acVectorEnginee.busDpDphVecToVal[acVectorEnginee.bus2D1PerLoc[element.getNum()]] = ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dph1;
45-
acVectorEnginee.busDpDphVecToVal[acVectorEnginee.bus2D2PerLoc[element.getNum()]] = ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dph2;
46-
}
47-
48-
@Override
49-
public boolean isVectorized(Variable<AcVariableType> v) {
50-
return v.getType() == AcVariableType.BUS_V || v.getType() == AcVariableType.BUS_PHI;
40+
public VectorEngine.VecToVal getVecToVal(Variable<AcVariableType> v) {
41+
if (v == v1Var) {
42+
return ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dv1;
43+
}
44+
if (v == v2Var) {
45+
return ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dv2;
46+
}
47+
if (v == ph1Var) {
48+
return ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dph1;
49+
}
50+
if (v == ph2Var) {
51+
return ClosedBranchSide2ActiveFlowEquationTerm::vec2dp2dph2;
52+
}
53+
return null;
54+
// TODO return for eval
55+
// acVectorEnginee.vecToP2[element.getNum()] = ClosedBranchSide2ActiveFlowEquationTerm::vec2p2;}
5156
}
5257

5358
@Override

src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ActiveFlowEquationTerm.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public OpenBranchSide2ActiveFlowEquationTerm(LfBranch branch, LfBus bus1, Variab
2828
v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V);
2929
}
3030

31-
@Override
32-
public void updateVectorSuppliers() {
33-
// Do nothing for now
34-
}
35-
3631
private double v1() {
3732
return sv.get(v1Var.getRow());
3833
}

src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2CurrentMagnitudeEquationTerm.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ public OpenBranchSide2CurrentMagnitudeEquationTerm(LfBranch branch, LfBus bus1,
3838
}
3939
}
4040

41-
@Override
42-
public void updateVectorSuppliers() {
43-
// Do nothing for now. No vectorization supported
44-
}
45-
4641
private double v1() {
4742
return sv.get(v1Var.getRow());
4843
}

src/main/java/com/powsybl/openloadflow/ac/equations/OpenBranchSide2ReactiveFlowEquationTerm.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ public OpenBranchSide2ReactiveFlowEquationTerm(LfBranch branch, LfBus bus1, Vari
2828
v1Var = variableSet.getVariable(bus1.getNum(), AcVariableType.BUS_V);
2929
}
3030

31-
@Override
32-
public void updateVectorSuppliers() {
33-
// Do nothing for now
34-
}
35-
3631
private double v1() {
3732
return sv.get(v1Var.getRow());
3833
}

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

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public class AcVectorEngine implements StateVectorListener, EquationSystemListen
5353
private final ArrayList<AbstractClosedBranchAcFlowEquationTerm> supplyingTerms = new ArrayList<>();
5454
private boolean suppliersValid = false;
5555
public final double[] a1;
56-
public final DoubleSupplier[] a1Supplier;
5756
public final double[] r1;
58-
public final DoubleSupplier[] r1Supplier;
5957

6058
// variables
6159
public final Variable<AcVariableType>[] v1Var;
@@ -88,6 +86,7 @@ public class AcVectorEngine implements StateVectorListener, EquationSystemListen
8886
private Variable<AcVariableType>[] variablesPerEquation;
8987
private int[] matrixIndexPerVariableAndEquation;
9088
private int[] termsByVariableAndEquationIndex;
89+
private VecToVal[] vecToValByVariableAndEquation;
9190
private int[] termCountByVariableAndEquation;
9291
private EquationTerm<AcVariableType, AcEquationType>[] termsByVariableAndEquation;
9392
private int[] termStatusByVariableAndEquationsIndex;
@@ -98,12 +97,9 @@ public class AcVectorEngine implements StateVectorListener, EquationSystemListen
9897

9998
// term replicated data
10099
private boolean[] termActiveStatus;
101-
102-
public interface VecToVal {
103-
double value(double v1, double v2, double sinKsi, double sinTheta2, double cosTheta2,
104-
double b1, double b2, double g1, double g2, double y,
105-
double g12, double b12, double a1, double r1);
106-
}
100+
private int[] termBranchNum;
101+
public DoubleSupplier[] a1TermSupplier;
102+
public DoubleSupplier[] r1TermSupplier;
107103

108104
public AcVectorEngine(LfNetwork network, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
109105
this.equationSystem = equationSystem;
@@ -124,10 +120,8 @@ public AcVectorEngine(LfNetwork network, EquationSystem<AcVariableType, AcEquati
124120
networkDataInitialized = new boolean[branchCount];
125121

126122
a1 = new double[branchCount];
127-
a1Supplier = new DoubleSupplier[branchCount];
128123

129124
r1 = new double[branchCount];
130-
r1Supplier = new DoubleSupplier[branchCount];
131125

132126
v1Var = new Variable[branchCount];
133127
v1 = new double[branchCount];
@@ -179,7 +173,7 @@ public AcVectorEngine(LfNetwork network, EquationSystem<AcVariableType, AcEquati
179173
public void onStateUpdate() {
180174
// disconnected for now - does not accelerate because of need to update the suppliers (can be modified)
181175
// Arrays.fill(p2Valid, false);
182-
// updateVariables();
176+
updateVariables();
183177
// vecToP2();
184178
}
185179

@@ -230,21 +224,6 @@ public void addSupplyingTerm(AbstractClosedBranchAcFlowEquationTerm t) {
230224
equationDataValid = false;
231225
}
232226

233-
private void updateSuppliers() {
234-
Arrays.fill(r1, Double.NaN);
235-
Arrays.fill(r1Supplier, null);
236-
Arrays.fill(a1, Double.NaN);
237-
Arrays.fill(a1Supplier, null);
238-
Arrays.fill(vecToP2, null);
239-
Arrays.fill(busDpDvVecToVal, null);
240-
Arrays.fill(busDpDphVecToVal, null);
241-
supplyingTerms.stream()
242-
.filter(AbstractEquationTerm::isActive)
243-
.filter(t -> t.getEquation().isActive())
244-
.forEach(AbstractClosedBranchAcFlowEquationTerm::updateVectorSuppliers);
245-
suppliersValid = true;
246-
}
247-
248227
private void updateVariables() {
249228
StateVector stateVector = equationSystem.getStateVector();
250229
for (int i = 0; i < v1Var.length; i++) {
@@ -256,63 +235,25 @@ private void updateVariables() {
256235
}
257236

258237
private void vecToP2() {
238+
// TODO: Update with new structure
259239
for (int i = 0; i < vecToP2.length; i++) {
260-
double a1Evaluated = a1Supplier[i] == null ? a1[i] : a1Supplier[i].getAsDouble();
261-
double r1Evaluated = r1Supplier[i] == null ? r1[i] : r1Supplier[i].getAsDouble();
240+
// double a1Evaluated = a1Supplier[i] == null ? a1[i] : a1Supplier[i].getAsDouble();
241+
// double r1Evaluated = r1Supplier[i] == null ? r1[i] : r1Supplier[i].getAsDouble();
262242
double sinKsi = FastMath.sin(ksi[i]);
263-
double theta2 = AbstractClosedBranchAcFlowEquationTerm.theta2(ksi[i], ph1[i], a1Evaluated, ph2[i]);
243+
double theta2 = AbstractClosedBranchAcFlowEquationTerm.theta2(ksi[i], ph1[i], /*a1Evaluated*/ Double.NaN, ph2[i]);
264244
double sinTheta2 = FastMath.sin(theta2);
265245
double cosTheta2 = FastMath.cos(theta2);
266246
// TODO - est-ce qu'on se sert de tout ?
267247
if (vecToP2[i] != null) {
268248
// All dp2 functions should be available then
269249
p2[i] = vecToP2[i].value(v1[i], v2[i], sinKsi, sinTheta2, cosTheta2,
270-
b1[i], b2[i], g1[i], g2[i], y[i], g12[i], b12[i],
271-
a1Evaluated, r1Evaluated);
250+
b1[i], b2[i], g1[i], g2[i], y[i], g12[i], b12[i], Double.NaN, Double.NaN);
251+
// a1Evaluated, r1Evaluated);
272252
p2Valid[i] = true;
273253
}
274254
}
275255
}
276256

277-
private void vectToDP2() {
278-
if (!suppliersValid) {
279-
updateSuppliers();
280-
}
281-
Arrays.fill(busDpDv, 0);
282-
Arrays.fill(busDpDph, 0);
283-
for (int i = 0; i < vecToP2.length; i++) {
284-
double a1Evaluated = a1Supplier[i] == null ? a1[i] : a1Supplier[i].getAsDouble();
285-
double r1Evaluated = r1Supplier[i] == null ? r1[i] : r1Supplier[i].getAsDouble();
286-
double sinKsi = FastMath.sin(ksi[i]);
287-
double theta2 = AbstractClosedBranchAcFlowEquationTerm.theta2(ksi[i], ph1[i], a1Evaluated, ph2[i]);
288-
double sinTheta2 = FastMath.sin(theta2);
289-
double cosTheta2 = FastMath.cos(theta2);
290-
if (vecToP2[i] != null) {
291-
// All dp2 functions should be available then
292-
if (busDpDvVecToVal[bus2D1PerLoc[i]] != null) {
293-
busDpDv[bus2D1PerLoc[i]] = busDpDvVecToVal[bus2D1PerLoc[i]].value(v1[i], v2[i], sinKsi, sinTheta2, cosTheta2,
294-
b1[i], b2[i], g1[i], g2[i], y[i], g12[i], b12[i],
295-
a1Evaluated, r1Evaluated);
296-
}
297-
if (busDpDvVecToVal[bus2D2PerLoc[i]] != null) {
298-
busDpDv[bus2D2PerLoc[i]] = busDpDvVecToVal[bus2D2PerLoc[i]].value(v1[i], v2[i], sinKsi, sinTheta2, cosTheta2,
299-
b1[i], b2[i], g1[i], g2[i], y[i], g12[i], b12[i],
300-
a1Evaluated, r1Evaluated);
301-
}
302-
if (busDpDphVecToVal[bus2D1PerLoc[i]] != null) {
303-
busDpDph[bus2D1PerLoc[i]] = busDpDphVecToVal[bus2D1PerLoc[i]].value(v1[i], v2[i], sinKsi, sinTheta2, cosTheta2,
304-
b1[i], b2[i], g1[i], g2[i], y[i], g12[i], b12[i],
305-
a1Evaluated, r1Evaluated);
306-
}
307-
if (busDpDphVecToVal[bus2D2PerLoc[i]] != null) {
308-
busDpDph[bus2D2PerLoc[i]] = busDpDphVecToVal[bus2D2PerLoc[i]].value(v1[i], v2[i], sinKsi, sinTheta2, cosTheta2,
309-
b1[i], b2[i], g1[i], g2[i], y[i], g12[i], b12[i],
310-
a1Evaluated, r1Evaluated);
311-
}
312-
}
313-
}
314-
}
315-
316257
private void initEquationData() {
317258
// reset all term vector index
318259
if (termsByVariableAndEquation != null) {
@@ -338,6 +279,9 @@ private void initEquationData() {
338279
index += 1;
339280
}
340281
termActiveStatus = new boolean[termCount];
282+
termBranchNum = new int[termCount];
283+
a1TermSupplier = new DoubleSupplier[termCount];
284+
r1TermSupplier = new DoubleSupplier[termCount];
341285
variablePerEquationIndex = new int[equationCount];
342286
variablesPerEquation = new Variable[variableIndexSize];
343287
matrixIndexPerVariableAndEquation = new int[variableIndexSize];
@@ -352,6 +296,13 @@ private void initEquationData() {
352296
for (EquationTerm<AcVariableType, AcEquationType> t : e.getTerms()) {
353297
t.setVectorIndex(indexForTermStatus);
354298
termActiveStatus[indexForTermStatus] = t.isActive();
299+
if (t instanceof AbstractClosedBranchAcFlowEquationTerm brTerm) {
300+
termBranchNum[indexForTermStatus] = brTerm.getElementNum();
301+
a1TermSupplier[indexForTermStatus] = brTerm.getA1Supplier();
302+
r1TermSupplier[indexForTermStatus] = brTerm.getR1Supplier();
303+
r1[brTerm.getElementNum()] = brTerm.r1();
304+
a1[brTerm.getElementNum()] = brTerm.a1();
305+
}
355306
indexForTermStatus += 1;
356307
}
357308
variablePerEquationIndex[indexEq] = indexVar;
@@ -369,9 +320,26 @@ private void initEquationData() {
369320
}
370321
termsByVariableAndEquation = termsByVariableAndEquationList.toArray(new EquationTerm[0]);
371322
termStatusByVariableAndEquationsIndex = new int[termsByVariableAndEquation.length];
323+
vecToValByVariableAndEquation = new VecToVal[termsByVariableAndEquation.length];
372324
for (int i = 0; i < termsByVariableAndEquation.length; i++) {
373325
termStatusByVariableAndEquationsIndex[i] = termsByVariableAndEquation[i].getVectorIndex();
374326
}
327+
for (int eqIndex = 0; eqIndex < equationActiveStatus.length; eqIndex++) {
328+
if (equationActiveStatus[eqIndex]) {
329+
int varEnd = variablePerEquationIndex[eqIndex] + variableCountPerEquation[eqIndex];
330+
for (int varIndex = variablePerEquationIndex[eqIndex]; varIndex < varEnd; varIndex++) {
331+
Variable<AcVariableType> v = variablesPerEquation[varIndex];
332+
int termEnd = termsByVariableAndEquationIndex[varIndex] + termCountByVariableAndEquation[varIndex];
333+
for (int termIndex = termsByVariableAndEquationIndex[varIndex];
334+
termIndex < termEnd;
335+
termIndex++) {
336+
if (termActiveStatus[termStatusByVariableAndEquationsIndex[termIndex]]) {
337+
vecToValByVariableAndEquation[termIndex] = termsByVariableAndEquation[termIndex].getVecToVal(v);
338+
}
339+
}
340+
}
341+
}
342+
}
375343
equationDataValid = true;
376344
}
377345

@@ -395,7 +363,11 @@ public void der(boolean update, Matrix matrix) {
395363
termIndex < termEnd;
396364
termIndex++) {
397365
if (termActiveStatus[termStatusByVariableAndEquationsIndex[termIndex]]) {
398-
value += callTermDer(termIndex, v);
366+
if (vecToValByVariableAndEquation[termIndex] != null) {
367+
value += callVecToVar(termIndex);
368+
} else {
369+
value += callTermDer(termIndex, v);
370+
}
399371
}
400372
}
401373
if (update) {
@@ -409,8 +381,22 @@ public void der(boolean update, Matrix matrix) {
409381
}
410382
}
411383

412-
// Isolate for profileing
384+
// Isolate for profiling
413385
private double callTermDer(int termIndex, Variable<AcVariableType> v) {
414386
return termsByVariableAndEquation[termIndex].der(v);
415387
}
388+
389+
private double callVecToVar(int termIndex) {
390+
int termStatusIndex = termStatusByVariableAndEquationsIndex[termIndex];
391+
int branchNum = termBranchNum[termStatusIndex];
392+
double a1Evaluated = a1TermSupplier[termStatusIndex] == null ? a1[branchNum] : a1TermSupplier[termStatusIndex].getAsDouble();
393+
double r1Evaluated = r1TermSupplier[termStatusIndex] == null ? r1[branchNum] : r1TermSupplier[termStatusIndex].getAsDouble();
394+
double sinKsi = FastMath.sin(ksi[branchNum]);
395+
double theta2 = AbstractClosedBranchAcFlowEquationTerm.theta2(ksi[branchNum], ph1[branchNum], a1Evaluated, ph2[branchNum]);
396+
double sinTheta2 = FastMath.sin(theta2);
397+
double cosTheta2 = FastMath.cos(theta2);
398+
return vecToValByVariableAndEquation[termIndex].value(v1[branchNum], v2[branchNum], sinKsi, sinTheta2, cosTheta2,
399+
b1[branchNum], b2[branchNum], g1[branchNum], g2[branchNum], y[branchNum], g12[branchNum], b12[branchNum],
400+
a1Evaluated, r1Evaluated);
401+
}
416402
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ default EquationTerm<V, E> minus() {
233233
return multiply(-1);
234234
}
235235

236-
default boolean isVectorized(Variable<V> v) {
237-
return false;
236+
default VectorEngine.VecToVal getVecToVal(Variable<V> v) {
237+
return null;
238238
}
239239

240240
default int getVectorIndex(Variable<V> v) {

0 commit comments

Comments
 (0)