From 8ed1d663823ad483104e3c15b076275d2ff28ff5 Mon Sep 17 00:00:00 2001 From: Sophie Frasnedo Date: Sun, 29 Jan 2023 22:27:24 +0100 Subject: [PATCH 1/4] create invisible nodes from the start and not on the fly Signed-off-by: Sophie Frasnedo --- .../nad/build/iidm/NetworkGraphBuilder.java | 26 +++++++++++-------- .../nad/build/iidm/VoltageLevelFilter.java | 18 ++++++++++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/NetworkGraphBuilder.java b/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/NetworkGraphBuilder.java index d67da3987..f8e2f95cd 100644 --- a/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/NetworkGraphBuilder.java +++ b/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/NetworkGraphBuilder.java @@ -45,12 +45,17 @@ public NetworkGraphBuilder(Network network) { @Override public Graph buildGraph() { Graph graph = new Graph(); - List voltageLevels = network.getVoltageLevelStream() + List 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 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; } @@ -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); @@ -160,7 +164,7 @@ 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); @@ -168,8 +172,7 @@ private void addThreeWtEdge(Graph graph, ThreeWindingsTransformer twt, ThreeWtNo 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); @@ -184,9 +187,10 @@ private BusNode getBusNode(Graph graph, Terminal terminal) { return graph.getBusNode(connectableBusA.getId()); } - private VoltageLevelNode getOrCreateInvisibleVoltageLevelNode(Graph graph, Terminal terminal) { + private VoltageLevelNode getVoltageLevelNode(Graph graph, Terminal terminal) { VoltageLevel vl = terminal.getVoltageLevel(); - return graph.getVoltageLevelNode(vl.getId()).orElseGet(() -> addVoltageLevelGraphNode(vl, graph, false)); + 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) { diff --git a/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/VoltageLevelFilter.java b/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/VoltageLevelFilter.java index a261200a3..9461473dd 100644 --- a/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/VoltageLevelFilter.java +++ b/network-area-diagram/src/main/java/com/powsybl/nad/build/iidm/VoltageLevelFilter.java @@ -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 @@ -29,6 +27,10 @@ public VoltageLevelFilter(Set voltageLevels) { this.voltageLevels = voltageLevels; } + private Set getVoltageLevels() { + return voltageLevels; + } + @Override public boolean test(VoltageLevel voltageLevel) { return voltageLevels.contains(voltageLevel); @@ -71,6 +73,14 @@ public static VoltageLevelFilter createVoltageLevelsFilter(Network network, List return createVoltageLevelsDepthFilter(network, voltageLevelIds, 0); } + public static List getNextDepthVoltageLevels(Network network, List voltageLevels) { + List voltageLevelIds = voltageLevels.stream().map(VoltageLevel::getId).collect(Collectors.toList()); + VoltageLevelFilter voltageLevelFilter = createVoltageLevelsDepthFilter(network, voltageLevelIds, 1); + Set voltageLevelSet = new HashSet<>(voltageLevelFilter.getVoltageLevels()); + voltageLevelSet.removeAll(voltageLevels.stream().collect(Collectors.toSet())); + return new ArrayList<>(voltageLevelSet.stream().collect(Collectors.toList())); + } + private static void traverseVoltageLevels(Set voltageLevelsDepth, int depth, Set visitedVoltageLevels) { if (depth < 0) { return; From 6429604af11049734c818a2092e55a40d25e05d6 Mon Sep 17 00:00:00 2001 From: Sophie Frasnedo Date: Sun, 29 Jan 2023 22:28:03 +0100 Subject: [PATCH 2/4] update test references Signed-off-by: Sophie Frasnedo --- .../src/test/resources/3wt_partial.svg | 48 +- .../test/resources/IEEE_118_bus_partial.svg | 498 +++++----- .../IEEE_118_bus_partial_non_connected.svg | 910 +++++++++--------- 3 files changed, 728 insertions(+), 728 deletions(-) diff --git a/network-area-diagram/src/test/resources/3wt_partial.svg b/network-area-diagram/src/test/resources/3wt_partial.svg index b502d34e2..f63b9ae26 100644 --- a/network-area-diagram/src/test/resources/3wt_partial.svg +++ b/network-area-diagram/src/test/resources/3wt_partial.svg @@ -1,5 +1,5 @@ - +