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 nad layout instability #490

Merged
merged 8 commits into from
Jan 30, 2023
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
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);
Comment on lines +78 to +80
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do better to avoid creating those sets. Maybe by using traverseVoltageLevels at the cost of changing its signature. Or maybe by enriching VoltageLevelFilter API, like in #491. But let's do that later in a separate PR

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