|
20 | 20 | import org.slf4j.Logger; |
21 | 21 | import org.slf4j.LoggerFactory; |
22 | 22 |
|
23 | | -import java.util.ArrayList; |
24 | | -import java.util.Arrays; |
25 | | -import java.util.Collection; |
26 | | -import java.util.List; |
| 23 | +import java.util.*; |
27 | 24 | import java.util.function.DoubleSupplier; |
28 | 25 | import java.util.stream.IntStream; |
29 | 26 |
|
@@ -67,6 +64,8 @@ public class AcVectorEngine implements StateVectorListener, EquationSystemListen |
67 | 64 |
|
68 | 65 | // indexes to compute derivatives |
69 | 66 | private boolean equationDataValid; |
| 67 | + private boolean equationOrderValid; |
| 68 | + private int[] sortedEquationIndexArray; |
70 | 69 | private int[] variableCountPerEquation; |
71 | 70 | private int[] variablePerEquationIndex; |
72 | 71 | private Variable<AcVariableType>[] variablesPerEquation; |
@@ -179,6 +178,7 @@ public void onEquationChange(Equation equation, EquationEventType eventType) { |
179 | 178 | if (equation.getVectorIndex() >= 0) { |
180 | 179 | equationColumn[equation.getVectorIndex()] = equation.getColumn(); |
181 | 180 | } |
| 181 | + equationOrderValid = false; |
182 | 182 | break; |
183 | 183 | } |
184 | 184 | } |
@@ -231,6 +231,7 @@ private void initEquationData() { |
231 | 231 |
|
232 | 232 | Collection<Equation<AcVariableType, AcEquationType>> equationList = equationSystem.getEquations(); |
233 | 233 | int equationCount = equationList.size(); |
| 234 | + sortedEquationIndexArray = new int[equationCount]; |
234 | 235 | variableCountPerEquation = new int[equationCount]; |
235 | 236 | equationActiveStatus = new boolean[equationCount]; |
236 | 237 | equationColumn = new int[equationCount]; |
@@ -384,20 +385,33 @@ private void initEquationData() { |
384 | 385 | equationDataValid = true; |
385 | 386 | } |
386 | 387 |
|
| 388 | + private void sortEquations() { |
| 389 | + Iterator<Integer> it = IntStream.range(0, equationColumn.length).boxed() |
| 390 | + .sorted((i1, i2) -> equationColumn[i1] - equationColumn[i2]).iterator(); |
| 391 | + int i = 0; |
| 392 | + while (it.hasNext()) { |
| 393 | + sortedEquationIndexArray[i] = it.next(); |
| 394 | + i += 1; |
| 395 | + } |
| 396 | + equationOrderValid = true; |
| 397 | + } |
| 398 | + |
387 | 399 | @Override |
388 | 400 | public void der(boolean update, Matrix matrix) { |
389 | 401 |
|
390 | 402 | if (!equationDataValid) { |
391 | 403 | initEquationData(); |
| 404 | + equationOrderValid = false; |
| 405 | + } |
| 406 | + |
| 407 | + if (!equationOrderValid) { |
| 408 | + sortEquations(); |
392 | 409 | } |
393 | 410 |
|
394 | 411 | updateVariables(); // do not depend on listener call order |
395 | 412 |
|
396 | 413 | derSortedTerms(); |
397 | 414 |
|
398 | | - int[] sortedEquationIndexArray = IntStream.range(0, equationActiveStatus.length).boxed() |
399 | | - .sorted((i1, i2) -> equationColumn[i1] - equationColumn[i2]) |
400 | | - .mapToInt(i -> i).toArray(); |
401 | 415 | for (int sortedEqIndex = 0; sortedEqIndex < equationActiveStatus.length; sortedEqIndex++) { |
402 | 416 | int eqIndex = sortedEquationIndexArray[sortedEqIndex]; |
403 | 417 | if (equationActiveStatus[eqIndex]) { |
|
0 commit comments