Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache invalidation issue when updating network state #867

Merged
merged 4 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/com/powsybl/openloadflow/NetworkCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static class Entry extends DefaultNetworkListener {

private List<AcLoadFlowContext> contexts;

private boolean pause = false;

public Entry(Network network, LoadFlowParameters parameters) {
Objects.requireNonNull(network);
this.networkRef = new WeakReference<>(network);
Expand Down Expand Up @@ -73,6 +75,10 @@ public LoadFlowParameters getParameters() {
return parameters;
}

public void setPause(boolean pause) {
this.pause = pause;
}

private void reset() {
if (contexts != null) {
for (AcLoadFlowContext context : contexts) {
Expand Down Expand Up @@ -200,7 +206,7 @@ private static void updateSwitch(boolean open, LfNetwork lfNetwork, LfBranch lfB

@Override
public void onUpdate(Identifiable identifiable, String attribute, String variantId, Object oldValue, Object newValue) {
if (contexts == null) {
if (contexts == null || pause) {
return;
}
boolean done = false;
Expand Down
44 changes: 29 additions & 15 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ private GraphConnectivityFactory<LfBus, LfBranch> getConnectivityFactory(OpenLoa
: connectivityFactory;
}

private void updateAcState(Network network, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt,
AcLoadFlowResult result, AcLoadFlowParameters acParameters) {
if (parametersExt.isNetworkCacheEnabled()) {
NetworkCache.INSTANCE.findEntry(network).orElseThrow().setPause(true);
}
try {
// update network state
if (result.isOk() || parametersExt.isAlwaysUpdateNetwork()) {
var updateParameters = new LfNetworkStateUpdateParameters(parameters.isUseReactiveLimits(),
parameters.isWriteSlackBus(),
parameters.isPhaseShifterRegulationOn(),
parameters.isTransformerVoltageControlOn(),
parameters.isDistributedSlack() && (parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD || parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD) && parametersExt.isLoadPowerFactorConstant(),
parameters.isDc(),
acParameters.getNetworkParameters().isBreakers(),
parametersExt.getReactivePowerDispatchMode());
result.getNetwork().updateState(updateParameters);

// zero or low impedance branch flows computation
computeZeroImpedanceFlows(result.getNetwork(), LoadFlowModel.AC);
}
} finally {
if (parametersExt.isNetworkCacheEnabled()) {
NetworkCache.INSTANCE.findEntry(network).orElseThrow().setPause(false);
}
}
}

private LoadFlowResult runAc(Network network, LoadFlowParameters parameters, OpenLoadFlowParameters parametersExt, Reporter reporter) {
GraphConnectivityFactory<LfBus, LfBranch> selectedConnectivityFactory = getConnectivityFactory(parametersExt);
AcLoadFlowParameters acParameters = OpenLoadFlowParameters.createAcParameters(network, parameters, parametersExt, matrixFactory, selectedConnectivityFactory);
Expand Down Expand Up @@ -142,21 +170,7 @@ private LoadFlowResult runAc(Network network, LoadFlowParameters parameters, Ope

List<LoadFlowResult.ComponentResult> componentResults = new ArrayList<>(results.size());
for (AcLoadFlowResult result : results) {
// update network state
if (result.isOk() || parametersExt.isAlwaysUpdateNetwork()) {
var updateParameters = new LfNetworkStateUpdateParameters(parameters.isUseReactiveLimits(),
parameters.isWriteSlackBus(),
parameters.isPhaseShifterRegulationOn(),
parameters.isTransformerVoltageControlOn(),
parameters.isDistributedSlack() && (parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_LOAD || parameters.getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD) && parametersExt.isLoadPowerFactorConstant(),
parameters.isDc(),
acParameters.getNetworkParameters().isBreakers(),
parametersExt.getReactivePowerDispatchMode());
result.getNetwork().updateState(updateParameters);

// zero or low impedance branch flows computation
computeZeroImpedanceFlows(result.getNetwork(), LoadFlowModel.AC);
}
updateAcState(network, parameters, parametersExt, result, acParameters);

LoadFlowResult.ComponentResult.Status status = convertStatus(result);
// FIXME a null slack bus ID should be allowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.openloadflow.ac;

import com.powsybl.iidm.network.Bus;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VariantManagerConstants;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.iidm.network.test.FourSubstationsNodeBreakerFactory;
Expand All @@ -20,6 +21,7 @@
import com.powsybl.openloadflow.network.EurostagFactory;
import com.powsybl.openloadflow.network.NodeBreakerNetworkFactory;
import com.powsybl.openloadflow.network.ShuntNetworkFactory;
import com.powsybl.openloadflow.network.VoltageControlNetworkFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -270,7 +272,7 @@ void testShunt3() {
assertEquals(1, shunt.getSectionCount());

shunt.setSectionCount(1);
assertNull(NetworkCache.INSTANCE.findEntry(network).orElseThrow().getContexts()); // cache has been invalidated
assertNotNull(NetworkCache.INSTANCE.findEntry(network).orElseThrow().getContexts());
}

@Test
Expand Down Expand Up @@ -414,4 +416,22 @@ void testUpdateNetworkFix() {
assertFalse(Double.isNaN(bus.getAngle()));
}
}

@Test
void fixCacheInvalidationWhenUpdatingTapPosition() {
Network network = VoltageControlNetworkFactory.createNetworkWithT2wt();
var t2wt = network.getTwoWindingsTransformer("T2wT");
t2wt.getRatioTapChanger()
.setTargetDeadband(0)
.setRegulating(true)
.setTapPosition(0)
.setRegulationTerminal(t2wt.getTerminal2())
.setTargetV(34.0);

parameters.setTransformerVoltageControlOn(true);

LoadFlowResult result = loadFlowRunner.run(network, parameters);
assertTrue(result.isOk());
assertNotNull(NetworkCache.INSTANCE.findEntry(network).orElseThrow().getContexts());
}
}