@@ -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}
0 commit comments