-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
- Loading branch information
Showing
9 changed files
with
255 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/com/powsybl/openloadflow/equations/AbstractVector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) 2022, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.openloadflow.equations; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com> | ||
*/ | ||
public abstract class AbstractVector<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> implements Vector { | ||
|
||
protected final EquationSystem<V, E> equationSystem; | ||
|
||
private double[] array; | ||
|
||
private enum Status { | ||
VALID, | ||
VECTOR_INVALID, // structure has changed | ||
VALUES_INVALID // same structure but values have to be updated | ||
} | ||
|
||
private Status status = Status.VECTOR_INVALID; | ||
|
||
private final EquationSystemIndexListener<V, E> equationSystemIndexListener = new EquationSystemIndexListener<>() { | ||
|
||
@Override | ||
public void onEquationChange(Equation<V, E> equation, ChangeType changeType) { | ||
invalidateVector(); | ||
} | ||
|
||
@Override | ||
public void onVariableChange(Variable<V> variable, ChangeType changeType) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void onEquationTermChange(EquationTerm<V, E> term) { | ||
// nothing to do | ||
} | ||
}; | ||
|
||
protected AbstractVector(EquationSystem<V, E> equationSystem) { | ||
this.equationSystem = Objects.requireNonNull(equationSystem); | ||
equationSystem.getIndex().addListener(equationSystemIndexListener); | ||
} | ||
|
||
protected void invalidateValues() { | ||
if (status == Status.VALID) { | ||
status = Status.VALUES_INVALID; | ||
} | ||
} | ||
|
||
protected void invalidateVector() { | ||
status = Status.VECTOR_INVALID; | ||
} | ||
|
||
protected void validate() { | ||
status = Status.VALID; | ||
} | ||
|
||
@Override | ||
public double[] getArray() { | ||
switch (status) { | ||
case VECTOR_INVALID: | ||
array = createArray(); | ||
validate(); | ||
break; | ||
|
||
case VALUES_INVALID: | ||
updateArray(array); | ||
validate(); | ||
break; | ||
|
||
default: | ||
// nothing to do | ||
break; | ||
} | ||
return array; | ||
} | ||
|
||
protected abstract double[] createArray(); | ||
|
||
protected abstract void updateArray(double[] array); | ||
|
||
@Override | ||
public void minus(Vector other) { | ||
Objects.requireNonNull(other); | ||
Vectors.minus(getArray(), other.getArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/com/powsybl/openloadflow/equations/EquationVector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2022, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.openloadflow.equations; | ||
|
||
import com.google.common.base.Stopwatch; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.powsybl.openloadflow.util.Markers.PERFORMANCE_MARKER; | ||
|
||
/** | ||
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com> | ||
*/ | ||
public class EquationVector<V extends Enum<V> & Quantity, E extends Enum<E> & Quantity> extends AbstractVector<V, E> | ||
implements StateVectorListener { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(EquationVector.class); | ||
|
||
public EquationVector(EquationSystem<V, E> equationSystem) { | ||
super(equationSystem); | ||
equationSystem.getStateVector().addListener(this); | ||
} | ||
|
||
@Override | ||
public void onStateUpdate() { | ||
invalidateValues(); | ||
} | ||
|
||
@Override | ||
protected double[] createArray() { | ||
double[] array = new double[equationSystem.getIndex().getSortedEquationsToSolve().size()]; | ||
updateArray(array); | ||
return array; | ||
} | ||
|
||
@Override | ||
protected void updateArray(double[] array) { | ||
Stopwatch stopwatch = Stopwatch.createStarted(); | ||
|
||
var equations = equationSystem.getIndex().getSortedEquationsToSolve(); | ||
|
||
if (array.length != equations.size()) { | ||
throw new IllegalArgumentException("Bad equation vector length: " + array.length); | ||
} | ||
|
||
Arrays.fill(array, 0); // necessary? | ||
for (Equation<V, E> equation : equations) { | ||
array[equation.getColumn()] = equation.eval(); | ||
} | ||
|
||
LOGGER.debug(PERFORMANCE_MARKER, "Equation vector updated in {} us", stopwatch.elapsed(TimeUnit.MICROSECONDS)); | ||
} | ||
} |
Oops, something went wrong.