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