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 network variant removal issue #637

Merged
merged 4 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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.network.impl;

import com.powsybl.openloadflow.network.LfNetwork;

import java.util.List;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public interface LfNetworkList extends AutoCloseable {

List<LfNetwork> getList();

@Override
void close();
}
80 changes: 59 additions & 21 deletions src/main/java/com/powsybl/openloadflow/network/impl/Networks.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,74 @@ public static List<LfNetwork> load(Network network, LfNetworkParameters paramete
return LfNetwork.load(network, new LfNetworkLoaderImpl(), parameters);
}

public static List<LfNetwork> load(Network network, SlackBusSelector slackBusSelector, Reporter reporter) {
return LfNetwork.load(network, new LfNetworkLoaderImpl(), slackBusSelector, reporter);
}

public static List<LfNetwork> load(Network network, LfNetworkParameters parameters, Reporter reporter) {
return LfNetwork.load(network, new LfNetworkLoaderImpl(), parameters, reporter);
}

public static List<LfNetwork> load(Network network, LfNetworkParameters networkParameters,
Set<Switch> switchesToOpen, Set<Switch> switchesToClose, Reporter reporter) {
static class LfNetworkListImpl implements LfNetworkList {

private final List<LfNetwork> list;

LfNetworkListImpl(List<LfNetwork> list) {
this.list = Objects.requireNonNull(list);
}

@Override
public List<LfNetwork> getList() {
return list;
}

@Override
public void close() {
// nothing to clean
}
}

static class LfNetworkListVariantCleanupImpl implements LfNetworkList {

private final Network network;

private final String workingVariantId;

private final String tmpVariantId;

private final List<LfNetwork> list;

LfNetworkListVariantCleanupImpl(Network network, String workingVariantId, String tmpVariantId, List<LfNetwork> list) {
this.network = Objects.requireNonNull(network);
this.workingVariantId = Objects.requireNonNull(workingVariantId);
this.tmpVariantId = Objects.requireNonNull(tmpVariantId);
this.list = Objects.requireNonNull(list);
}

public List<LfNetwork> getList() {
return list;
}

@Override
public void close() {
network.getVariantManager().removeVariant(tmpVariantId);
network.getVariantManager().setWorkingVariant(workingVariantId);
}
}

public static LfNetworkList load(Network network, LfNetworkParameters networkParameters,
Set<Switch> switchesToOpen, Set<Switch> switchesToClose, Reporter reporter) {
if (switchesToOpen.isEmpty() && switchesToClose.isEmpty()) {
return load(network, networkParameters, reporter);
return new LfNetworkListImpl(load(network, networkParameters, reporter));
} else {
String tmpVariantId = "olf-tmp-" + UUID.randomUUID();
String variantId = network.getVariantManager().getWorkingVariantId();
String workingVariantId = network.getVariantManager().getWorkingVariantId();
network.getVariantManager().cloneVariant(network.getVariantManager().getWorkingVariantId(), tmpVariantId);
network.getVariantManager().setWorkingVariant(tmpVariantId);
try {
network.getSwitchStream().filter(sw -> sw.getVoltageLevel().getTopologyKind() == TopologyKind.NODE_BREAKER)
.forEach(sw -> sw.setRetained(false));
switchesToOpen.stream().filter(sw -> sw.getVoltageLevel().getTopologyKind() == TopologyKind.NODE_BREAKER)
.forEach(sw -> sw.setRetained(true));
switchesToClose.stream().filter(sw -> sw.getVoltageLevel().getTopologyKind() == TopologyKind.NODE_BREAKER)
.forEach(sw -> sw.setRetained(true));
switchesToClose.forEach(sw -> sw.setOpen(false)); // in order to be present in the network.
return load(network, networkParameters, reporter);
} finally {
network.getVariantManager().removeVariant(tmpVariantId);
network.getVariantManager().setWorkingVariant(variantId);
}
network.getSwitchStream().filter(sw -> sw.getVoltageLevel().getTopologyKind() == TopologyKind.NODE_BREAKER)
.forEach(sw -> sw.setRetained(false));
switchesToOpen.stream().filter(sw -> sw.getVoltageLevel().getTopologyKind() == TopologyKind.NODE_BREAKER)
.forEach(sw -> sw.setRetained(true));
switchesToClose.stream().filter(sw -> sw.getVoltageLevel().getTopologyKind() == TopologyKind.NODE_BREAKER)
.forEach(sw -> sw.setRetained(true));
switchesToClose.forEach(sw -> sw.setOpen(false)); // in order to be present in the network.
return new LfNetworkListVariantCleanupImpl(network, workingVariantId, tmpVariantId, load(network, networkParameters, reporter));
}
}
}
38 changes: 20 additions & 18 deletions src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.powsybl.openloadflow.ac.outerloop.AcloadFlowEngine;
import com.powsybl.openloadflow.graph.GraphConnectivityFactory;
import com.powsybl.openloadflow.network.*;
import com.powsybl.openloadflow.network.impl.LfNetworkList;
import com.powsybl.openloadflow.network.impl.Networks;
import com.powsybl.openloadflow.network.impl.PropagatedContingency;
import com.powsybl.openloadflow.network.util.ActivePowerDistribution;
Expand Down Expand Up @@ -91,28 +92,29 @@ SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParamete
AcLoadFlowParameters acParameters = OpenLoadFlowParameters.createAcParameters(network, lfParameters, lfParametersExt, matrixFactory, connectivityFactory, saReporter, breakers, false);

// create networks including all necessary switches
List<LfNetwork> lfNetworks = Networks.load(network, acParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, saReporter);

// run simulation on largest network
SecurityAnalysisResult result;
if (lfNetworks.isEmpty()) {
result = createNoResult();
} else {
LfNetwork largestNetwork = lfNetworks.get(0);
if (largestNetwork.isValid()) {
Map<String, LfAction> lfActionsById = createLfActions(largestNetwork, actions);
Map<String, List<OperatorStrategy>> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies);
result = runSimulations(largestNetwork, propagatedContingencies, acParameters, securityAnalysisParameters, operatorStrategiesByContingencyId, lfActionsById, allSwitchesToClose);
} else {
try (LfNetworkList lfNetworks = Networks.load(network, acParameters.getNetworkParameters(), allSwitchesToOpen, allSwitchesToClose, saReporter)) {

// run simulation on largest network
SecurityAnalysisResult result;
if (lfNetworks.getList().isEmpty()) {
result = createNoResult();
} else {
LfNetwork largestNetwork = lfNetworks.getList().get(0);
if (largestNetwork.isValid()) {
Map<String, LfAction> lfActionsById = createLfActions(largestNetwork, actions);
Map<String, List<OperatorStrategy>> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies);
result = runSimulations(largestNetwork, propagatedContingencies, acParameters, securityAnalysisParameters, operatorStrategiesByContingencyId, lfActionsById, allSwitchesToClose);
} else {
result = createNoResult();
}
}
}

stopwatch.stop();
LOGGER.info("Security analysis {} in {} ms", Thread.currentThread().isInterrupted() ? "cancelled" : "done",
stopwatch.elapsed(TimeUnit.MILLISECONDS));
stopwatch.stop();
LOGGER.info("Security analysis {} in {} ms", Thread.currentThread().isInterrupted() ? "cancelled" : "done",
stopwatch.elapsed(TimeUnit.MILLISECONDS));

return new SecurityAnalysisReport(result);
return new SecurityAnalysisReport(result);
}
}

public static void distributedMismatch(LfNetwork network, double mismatch, LoadFlowParameters loadFlowParameters,
Expand Down
Loading