Skip to content

Commit

Permalink
PropagatedContingency refactor
Browse files Browse the repository at this point in the history
Add switches set

Signed-off-by: Florian Dupuy <[email protected]>
  • Loading branch information
flo-dup committed May 5, 2021
1 parent 178bc21 commit 52a9085
Showing 1 changed file with 68 additions and 60 deletions.
128 changes: 68 additions & 60 deletions src/main/java/com/powsybl/openloadflow/util/PropagatedContingency.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
Expand All @@ -30,9 +27,11 @@ public class PropagatedContingency {

private final int index;

private final Set<String> branchIdsToOpen = new HashSet<>();
private final Set<String> branchIdsToOpen;

private final Set<String> hvdcIdsToOpen = new HashSet<>();
private final Set<Switch> switchesToOpen;

private final Set<String> hvdcIdsToOpen;

public Contingency getContingency() {
return contingency;
Expand All @@ -50,77 +49,86 @@ public Set<String> getHvdcIdsToOpen() {
return hvdcIdsToOpen;
}

public PropagatedContingency(Contingency contingency, int index) {
this.contingency = contingency;
public PropagatedContingency(Contingency contingency, int index, Set<String> branchIdsToOpen, Set<String> hvdcIdsToOpen,
Set<Switch> switchesToOpen, Set<Terminal> terminalsToDisconnect) {
this.contingency = Objects.requireNonNull(contingency);
this.index = index;
}
this.branchIdsToOpen = Objects.requireNonNull(branchIdsToOpen);
this.hvdcIdsToOpen = Objects.requireNonNull(hvdcIdsToOpen);
this.switchesToOpen = Objects.requireNonNull(switchesToOpen);

public static List<PropagatedContingency> createListForSensitivityAnalysis(Network network, List<Contingency> contingencies) {
// Sensitivity analysis works in bus view, hence
// - it cannot deal with contingencies whose propagation encounters a coupler
// - it does not need the set of switches to open
return createList(network, contingencies, new HashSet<>(), true);
}
for (Switch sw : switchesToOpen) {
branchIdsToOpen.add(sw.getId());
}

public static List<PropagatedContingency> createListForSecurityAnalysis(Network network, List<Contingency> contingencies, Set<Switch> allSwitchesToOpen) {
// Security analysis works in bus breaker view, hence needs to know all switches to retain. Couplers are not a problem.
return createList(network, contingencies, allSwitchesToOpen, false);
for (Terminal terminal : terminalsToDisconnect) {
if (terminal.getConnectable() instanceof Branch) {
branchIdsToOpen.add(terminal.getConnectable().getId());
}
}
}

private static List<PropagatedContingency> createList(Network network, List<Contingency> contingencies, Set<Switch> allSwitchesToOpen,
boolean removeContingenciesEncounteringCouplers) {
public static List<PropagatedContingency> createListForSensitivityAnalysis(Network network, List<Contingency> contingencies) {
List<PropagatedContingency> propagatedContingencies = new ArrayList<>();
for (int index = 0; index < contingencies.size(); index++) {
Contingency contingency = contingencies.get(index);
PropagatedContingency propagatedContingency = new PropagatedContingency(contingency, index);

Set<Switch> switchesToOpen = new HashSet<>();
Set<Terminal> terminalsToDisconnect = new HashSet<>();
for (ContingencyElement element : contingency.getElements()) {
switch (element.getType()) {
case BRANCH:
case LINE:
case TWO_WINDINGS_TRANSFORMER:
// branch check is done inside branch tripping
new BranchTripping(element.getId(), null)
.traverse(network, null, switchesToOpen, terminalsToDisconnect);
propagatedContingency.getBranchIdsToOpen().add(element.getId());
break;
case HVDC_LINE:
HvdcLine hvdcLine = network.getHvdcLine(element.getId());
if (hvdcLine == null) {
throw new PowsyblException("HVDC line '" + element.getId() + "' not found");
}
propagatedContingency.getHvdcIdsToOpen().add(element.getId());
break;
default:
//TODO: support all kinds of contingencies
throw new UnsupportedOperationException("TODO");
}
}

if (!removeContingenciesEncounteringCouplers || switchesToOpen.stream().noneMatch(PropagatedContingency::isCoupler)) {
PropagatedContingency propagatedContingency = PropagatedContingency.create(network, contingency, index);
if (!propagatedContingency.containsCouplerToOpen()) {
propagatedContingencies.add(propagatedContingency);

for (Switch sw : switchesToOpen) {
propagatedContingency.getBranchIdsToOpen().add(sw.getId());
allSwitchesToOpen.add(sw);
}

for (Terminal terminal : terminalsToDisconnect) {
if (terminal.getConnectable() instanceof Branch) {
propagatedContingency.getBranchIdsToOpen().add(terminal.getConnectable().getId());
}
}

} else {
// Sensitivity analysis works in bus view, it cannot deal (yet) with contingencies whose propagation encounters a coupler
LOGGER.error("Contingency '{}' removed from list, as a coupler switch has been encountered while propagating the contingency", contingency.getId());
}
}
return propagatedContingencies;
}

public static List<PropagatedContingency> createListForSecurityAnalysis(Network network, List<Contingency> contingencies, Set<Switch> allSwitchesToOpen) {
List<PropagatedContingency> propagatedContingencies = new ArrayList<>();
for (int index = 0; index < contingencies.size(); index++) {
Contingency contingency = contingencies.get(index);
PropagatedContingency propagatedContingency = PropagatedContingency.create(network, contingency, index);
propagatedContingencies.add(propagatedContingency);
allSwitchesToOpen.addAll(propagatedContingency.switchesToOpen);
}
return propagatedContingencies;
}

private static PropagatedContingency create(Network network, Contingency contingency, int index) {
Set<Switch> switchesToOpen = new HashSet<>();
Set<Terminal> terminalsToDisconnect = new HashSet<>();
Set<String> branchIdsToOpen = new HashSet<>();
Set<String> hvdcIdsToOpen = new HashSet<>();
for (ContingencyElement element : contingency.getElements()) {
switch (element.getType()) {
case BRANCH:
case LINE:
case TWO_WINDINGS_TRANSFORMER:
// branch check is done inside branch tripping
new BranchTripping(element.getId(), null)
.traverse(network, null, switchesToOpen, terminalsToDisconnect);
branchIdsToOpen.add(element.getId());
break;
case HVDC_LINE:
HvdcLine hvdcLine = network.getHvdcLine(element.getId());
if (hvdcLine == null) {
throw new PowsyblException("HVDC line '" + element.getId() + "' not found");
}
hvdcIdsToOpen.add(element.getId());
break;
default:
//TODO: support all kinds of contingencies
throw new UnsupportedOperationException("TODO");
}
}

return new PropagatedContingency(contingency, index, branchIdsToOpen, hvdcIdsToOpen, switchesToOpen, terminalsToDisconnect);
}

private boolean containsCouplerToOpen() {
return switchesToOpen.stream().anyMatch(PropagatedContingency::isCoupler);
}

private static boolean isCoupler(Switch s) {
VoltageLevel.NodeBreakerView nbv = s.getVoltageLevel().getNodeBreakerView();
Connectable<?> c1 = nbv.getTerminal1(s.getId()).getConnectable();
Expand Down

0 comments on commit 52a9085

Please sign in to comment.