Skip to content

Commit

Permalink
[NAD] Fix layout instability (#490)
Browse files Browse the repository at this point in the history
* create invisible nodes from the start and not on the fly
* update test references

Signed-off-by: Sophie Frasnedo <sophie.frasnedo@rte-france.com>
  • Loading branch information
So-Fras authored Jan 30, 2023
1 parent 9a29ab3 commit d698b90
Show file tree
Hide file tree
Showing 5 changed files with 757 additions and 744 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ public NetworkGraphBuilder(Network network) {
@Override
public Graph buildGraph() {
Graph graph = new Graph();
List<VoltageLevel> voltageLevels = network.getVoltageLevelStream()
List<VoltageLevel> voltageLevelsVisible = network.getVoltageLevelStream()
.filter(voltageLevelFilter)
.sorted(Comparator.comparing(VoltageLevel::getId))
.collect(Collectors.toList());
voltageLevels.forEach(vl -> addVoltageLevelGraphNode(vl, graph, true));
voltageLevels.forEach(vl -> addGraphEdges(vl, graph));
List<VoltageLevel> voltageLevelsInvisible = VoltageLevelFilter.getNextDepthVoltageLevels(network, voltageLevelsVisible)
.stream()
.sorted(Comparator.comparing(VoltageLevel::getId))
.collect(Collectors.toList());
voltageLevelsVisible.forEach(vl -> addVoltageLevelGraphNode(vl, graph, true));
voltageLevelsInvisible.forEach(vl -> addVoltageLevelGraphNode(vl, graph, false));
voltageLevelsVisible.forEach(vl -> addGraphEdges(vl, graph));
return graph;
}

Expand Down Expand Up @@ -143,9 +148,8 @@ private void addEdge(Graph graph, Branch<?> branch, VoltageLevel vl, String edge
}

private void addEdge(Graph graph, Terminal terminalA, Terminal terminalB, Identifiable<?> identifiable, String edgeType, boolean terminalsInReversedOrder) {
VoltageLevelNode vlNodeA = graph.getVoltageLevelNode(terminalA.getVoltageLevel().getId())
.orElseThrow(() -> new PowsyblException("Cannot add edge, corresponding voltage level is unknown: '" + terminalA.getVoltageLevel().getId() + "'"));
VoltageLevelNode vlNodeB = getOrCreateInvisibleVoltageLevelNode(graph, terminalB);
VoltageLevelNode vlNodeA = getVoltageLevelNode(graph, terminalA);
VoltageLevelNode vlNodeB = getVoltageLevelNode(graph, terminalB);

BusNode busNodeA = getBusNode(graph, terminalA);
BusNode busNodeB = getBusNode(graph, terminalB);
Expand All @@ -160,16 +164,15 @@ private void addEdge(Graph graph, Terminal terminalA, Terminal terminalB, Identi

private void addThreeWtEdge(Graph graph, ThreeWindingsTransformer twt, ThreeWtNode tn, ThreeWindingsTransformer.Side side) {
Terminal terminal = twt.getTerminal(side);
VoltageLevelNode vlNode = getOrCreateInvisibleVoltageLevelNode(graph, terminal);
VoltageLevelNode vlNode = getVoltageLevelNode(graph, terminal);
ThreeWtEdge edge = new ThreeWtEdge(idProvider.createId(IidmUtils.get3wtLeg(twt, side)),
twt.getId(), twt.getNameOrId(), IidmUtils.getThreeWtEdgeSideFromIidmSide(side), vlNode.isVisible());
graph.addEdge(vlNode, getBusNode(graph, terminal), tn, edge);
}

private void addEdge(Graph graph, DanglingLine dl, BoundaryNode boundaryVlNode, BusNode boundaryBusNode) {
Terminal terminal = dl.getTerminal();
VoltageLevelNode vlNode = graph.getVoltageLevelNode(terminal.getVoltageLevel().getId())
.orElseThrow(() -> new PowsyblException("Cannot add edge, corresponding voltage level is unknown: '" + terminal.getVoltageLevel().getId() + "'"));
VoltageLevelNode vlNode = getVoltageLevelNode(graph, terminal);
BranchEdge edge = new BranchEdge(idProvider.createId(dl),
dl.getId(), dl.getNameOrId(), BranchEdge.DANGLING_LINE_EDGE);
graph.addEdge(vlNode, getBusNode(graph, terminal), boundaryVlNode, boundaryBusNode, edge);
Expand All @@ -184,9 +187,9 @@ private BusNode getBusNode(Graph graph, Terminal terminal) {
return graph.getBusNode(connectableBusA.getId());
}

private VoltageLevelNode getOrCreateInvisibleVoltageLevelNode(Graph graph, Terminal terminal) {
VoltageLevel vl = terminal.getVoltageLevel();
return graph.getVoltageLevelNode(vl.getId()).orElseGet(() -> addVoltageLevelGraphNode(vl, graph, false));
private VoltageLevelNode getVoltageLevelNode(Graph graph, Terminal terminal) {
return graph.getVoltageLevelNode(terminal.getVoltageLevel().getId())
.orElseThrow(() -> new PowsyblException("Cannot add edge, corresponding voltage level is unknown: '" + terminal.getVoltageLevel().getId() + "'"));
}

private ThreeWindingsTransformer.Side[] getSidesArray(ThreeWindingsTransformer.Side sideA) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import com.powsybl.iidm.network.*;
import com.powsybl.nad.utils.iidm.IidmUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
Expand All @@ -29,6 +27,10 @@ public VoltageLevelFilter(Set<VoltageLevel> voltageLevels) {
this.voltageLevels = voltageLevels;
}

private Set<VoltageLevel> getVoltageLevels() {
return voltageLevels;
}

@Override
public boolean test(VoltageLevel voltageLevel) {
return voltageLevels.contains(voltageLevel);
Expand Down Expand Up @@ -71,6 +73,14 @@ public static VoltageLevelFilter createVoltageLevelsFilter(Network network, List
return createVoltageLevelsDepthFilter(network, voltageLevelIds, 0);
}

public static Collection<VoltageLevel> getNextDepthVoltageLevels(Network network, List<VoltageLevel> voltageLevels) {
List<String> voltageLevelIds = voltageLevels.stream().map(VoltageLevel::getId).collect(Collectors.toList());
VoltageLevelFilter voltageLevelFilter = createVoltageLevelsDepthFilter(network, voltageLevelIds, 1);
Set<VoltageLevel> voltageLevelSet = new HashSet<>(voltageLevelFilter.getVoltageLevels());
voltageLevels.forEach(voltageLevelSet::remove);
return voltageLevelSet;
}

private static void traverseVoltageLevels(Set<VoltageLevel> voltageLevelsDepth, int depth, Set<VoltageLevel> visitedVoltageLevels) {
if (depth < 0) {
return;
Expand Down
48 changes: 24 additions & 24 deletions network-area-diagram/src/test/resources/3wt_partial.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d698b90

Please sign in to comment.