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

Refactor security analysis network loading #659

Merged
merged 2 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
69 changes: 53 additions & 16 deletions src/main/java/com/powsybl/openloadflow/network/impl/Networks.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
*/
package com.powsybl.openloadflow.network.impl;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.iidm.network.*;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.network.SlackBusSelector;
import com.powsybl.openloadflow.network.*;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.*;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand Down Expand Up @@ -110,24 +106,65 @@ public static List<LfNetwork> load(Network network, LfNetworkParameters paramete
return LfNetwork.load(network, new LfNetworkLoaderImpl(), parameters, reporter);
}

private static void retainAndCloseNecessarySwitches(Network network, Set<Switch> switchesToOpen, Set<Switch> switchesToClose) {
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.
}

private static void restoreInitialTopology(LfNetwork network, Set<Switch> allSwitchesToClose) {
var connectivity = network.getConnectivity();
connectivity.startTemporaryChanges();
allSwitchesToClose.stream().map(Identifiable::getId).forEach(id -> {
LfBranch branch = network.getBranchById(id);
connectivity.removeEdge(branch);
});
Set<LfBus> removedBuses = connectivity.getVerticesRemovedFromMainComponent();
removedBuses.forEach(bus -> bus.setDisabled(true));
Set<LfBranch> removedBranches = new HashSet<>(connectivity.getEdgesRemovedFromMainComponent());
// we should manage branches open at one side.
for (LfBus bus : removedBuses) {
bus.getBranches().stream().filter(b -> !b.isConnectedAtBothSides()).forEach(removedBranches::add);
}
removedBranches.forEach(branch -> branch.setDisabled(true));
}

public static LfNetworkList load(Network network, LfNetworkParameters networkParameters,
Set<Switch> switchesToOpen, Set<Switch> switchesToClose, Reporter reporter) {
if (switchesToOpen.isEmpty() && switchesToClose.isEmpty()) {
return new LfNetworkList(load(network, networkParameters, reporter));
} else {
if (!networkParameters.isBreakers()) {
throw new PowsyblException("LF networks have to be built from bus/breaker view");
}
// create a temporary working variant to build LF networks
String tmpVariantId = "olf-tmp-" + UUID.randomUUID();
String workingVariantId = network.getVariantManager().getWorkingVariantId();
network.getVariantManager().cloneVariant(network.getVariantManager().getWorkingVariantId(), tmpVariantId);
network.getVariantManager().setWorkingVariant(tmpVariantId);
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 LfNetworkList(load(network, networkParameters, reporter),
new LfNetworkList.VariantCleaner(network, workingVariantId, tmpVariantId));

// retain in topology all switches that could be open or close
// and close switches that could be closed during the simulation
retainAndCloseNecessarySwitches(network, switchesToOpen, switchesToClose);

List<LfNetwork> lfNetworks = load(network, networkParameters, reporter);

if (!switchesToClose.isEmpty()) {
for (LfNetwork lfNetwork : lfNetworks) {
// disable all buses and branches not connected to main component (because of switch to close)
restoreInitialTopology(lfNetwork, switchesToClose);
}
}

return new LfNetworkList(lfNetworks, new LfNetworkList.VariantCleaner(network, workingVariantId, tmpVariantId));
}
}
}
26 changes: 2 additions & 24 deletions src/main/java/com/powsybl/openloadflow/sa/AcSecurityAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.powsybl.computation.ComputationManager;
import com.powsybl.contingency.ContingenciesProvider;
import com.powsybl.contingency.Contingency;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Switch;
import com.powsybl.loadflow.LoadFlowParameters;
Expand Down Expand Up @@ -104,7 +103,7 @@ SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParamete

// run simulation on largest network
SecurityAnalysisResult result = lfNetworks.getLargest().filter(LfNetwork::isValid)
.map(largestNetwork -> runSimulations(largestNetwork, propagatedContingencies, acParameters, securityAnalysisParameters, operatorStrategies, actions, allSwitchesToClose))
.map(largestNetwork -> runSimulations(largestNetwork, propagatedContingencies, acParameters, securityAnalysisParameters, operatorStrategies, actions))
.orElse(createNoResult());

stopwatch.stop();
Expand Down Expand Up @@ -219,29 +218,9 @@ private static Map<String, List<OperatorStrategy>> indexOperatorStrategiesByCont
return operatorStrategiesByContingencyId;
}

private static void restoreInitialTopology(LfNetwork network, Set<Switch> allSwitchesToClose) {
if (allSwitchesToClose.isEmpty()) {
return;
}
var connectivity = network.getConnectivity();
connectivity.startTemporaryChanges();
allSwitchesToClose.stream().map(Identifiable::getId).forEach(id -> {
LfBranch branch = network.getBranchById(id);
connectivity.removeEdge(branch);
});
Set<LfBus> removedBuses = connectivity.getVerticesRemovedFromMainComponent();
removedBuses.forEach(bus -> bus.setDisabled(true));
Set<LfBranch> removedBranches = new HashSet<>(connectivity.getEdgesRemovedFromMainComponent());
// we should manage branches open at one side.
for (LfBus bus : removedBuses) {
bus.getBranches().stream().filter(b -> !b.isConnectedAtBothSides()).forEach(removedBranches::add);
}
removedBranches.forEach(branch -> branch.setDisabled(true));
}

private SecurityAnalysisResult runSimulations(LfNetwork network, List<PropagatedContingency> propagatedContingencies, AcLoadFlowParameters acParameters,
SecurityAnalysisParameters securityAnalysisParameters, List<OperatorStrategy> operatorStrategies,
List<Action> actions, Set<Switch> allSwitchesToClose) {
List<Action> actions) {
Map<String, Action> actionsById = indexActionsById(actions);
Set<Action> neededActions = new HashSet<>(actionsById.size());
Map<String, List<OperatorStrategy>> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions);
Expand All @@ -258,7 +237,6 @@ private SecurityAnalysisResult runSimulations(LfNetwork network, List<Propagated
network.setReporter(preContSimReporter);

// run pre-contingency simulation
restoreInitialTopology(network, allSwitchesToClose);
AcLoadFlowResult preContingencyLoadFlowResult = new AcloadFlowEngine(context)
.run();

Expand Down