Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Light refactor in NetworkGraphBuilder (#47)
Browse files Browse the repository at this point in the history
* Factorizing code in NetworkGraphBuilder
* Add getOpposite(side) utility method
* Update unit test

Signed-off-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
flo-dup authored Jan 25, 2022
1 parent a7caa94 commit 8228d67
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 78 deletions.
99 changes: 37 additions & 62 deletions src/main/java/com/powsybl/nad/build/iidm/NetworkGraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.powsybl.iidm.network.*;
import com.powsybl.nad.build.GraphBuilder;
import com.powsybl.nad.model.*;
import com.powsybl.nad.utils.iidm.IidmUtils;

import java.util.Objects;
import java.util.function.Predicate;
Expand Down Expand Up @@ -74,21 +75,11 @@ public VisitorBuilder(Graph graph) {

@Override
public void visitLine(Line line, Branch.Side side) {
// check if the edge was not already added (at the other side of the line)
if (graph.containsEdge(line.getId())) {
return;
}

addEdge(line, side, BranchEdge.LINE_EDGE);
}

@Override
public void visitTwoWindingsTransformer(TwoWindingsTransformer twt, Branch.Side side) {
// check if the edge was not already added (at the other side of the transformer)
if (graph.containsEdge(twt.getId())) {
return;
}

addEdge(twt, side, BranchEdge.TWO_WT_EDGE);
}

Expand All @@ -99,35 +90,12 @@ public void visitThreeWindingsTransformer(ThreeWindingsTransformer twt, ThreeWin
return;
}

ThreeWindingsTransformer.Side otherSide1;
ThreeWindingsTransformer.Side otherSide2;
if (side == ThreeWindingsTransformer.Side.ONE) {
otherSide1 = ThreeWindingsTransformer.Side.TWO;
otherSide2 = ThreeWindingsTransformer.Side.THREE;
} else if (side == ThreeWindingsTransformer.Side.TWO) {
otherSide1 = ThreeWindingsTransformer.Side.ONE;
otherSide2 = ThreeWindingsTransformer.Side.THREE;
} else {
otherSide1 = ThreeWindingsTransformer.Side.ONE;
otherSide2 = ThreeWindingsTransformer.Side.TWO;
}

Terminal terminalA = twt.getTerminal(side);
Terminal terminalB = twt.getTerminal(otherSide1);
Terminal terminalC = twt.getTerminal(otherSide2);

VoltageLevelNode vlNodeA = graph.getVoltageLevelNode(terminalA.getVoltageLevel().getId())
.orElseThrow(() -> new PowsyblException("Cannot add three-windings transformer, its voltage level is unknown"));
VoltageLevelNode vlNodeB = getOrCreateVoltageLevelNode(terminalB);
VoltageLevelNode vlNodeC = getOrCreateVoltageLevelNode(terminalC);

String twtId = twt.getId();
String twtName = twt.getNameOrId();
ThreeWtNode tn = new ThreeWtNode(idProvider.createId(twt), twtId, twtName);
ThreeWtNode tn = new ThreeWtNode(idProvider.createId(twt), twt.getId(), twt.getNameOrId());
graph.addNode(tn);
graph.addEdge(vlNodeA, getBusInnerNode(terminalA, vlNodeA), tn, new ThreeWtEdge(idProvider.createId(get3wtLeg(twt, side)), twtId, twtName, iidmSideToSide(side), vlNodeA.isVisible()));
graph.addEdge(vlNodeB, getBusInnerNode(terminalB, vlNodeB), tn, new ThreeWtEdge(idProvider.createId(get3wtLeg(twt, otherSide1)), twtId, twtName, iidmSideToSide(otherSide1), vlNodeB.isVisible()));
graph.addEdge(vlNodeC, getBusInnerNode(terminalC, vlNodeC), tn, new ThreeWtEdge(idProvider.createId(get3wtLeg(twt, otherSide2)), twtId, twtName, iidmSideToSide(otherSide2), vlNodeC.isVisible()));

for (ThreeWindingsTransformer.Side s : getSidesArray(side)) {
addThreeWtEdge(twt, tn, s);
}
}

@Override
Expand All @@ -148,16 +116,21 @@ public void visitHvdcConverterStation(HvdcConverterStation<?> converterStation)
}

private void addEdge(Branch<?> branch, Branch.Side side, String edgeType) {
// check if the edge was not already added (at the other side of the transformer)
if (graph.containsEdge(branch.getId())) {
return;
}

Terminal terminalA = branch.getTerminal(side);
Terminal terminalB = branch.getTerminal(side == Branch.Side.ONE ? Branch.Side.TWO : Branch.Side.ONE);
Terminal terminalB = branch.getTerminal(IidmUtils.getOpposite(side));

addEdge(terminalA, terminalB, branch, edgeType, side == Branch.Side.TWO);
}

private void addEdge(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 = getOrCreateVoltageLevelNode(terminalB);
VoltageLevelNode vlNodeB = getOrCreateInvisibleVoltageLevelNode(terminalB);

BusInnerNode busNodeA = getBusInnerNode(terminalA, vlNodeA);
BusInnerNode busNodeB = getBusInnerNode(terminalB, vlNodeB);
Expand All @@ -170,22 +143,36 @@ private void addEdge(Terminal terminalA, Terminal terminalB, Identifiable<?> ide
}
}

private BusInnerNode getBusInnerNode(Terminal terminal, VoltageLevelNode vlNode) {
Bus connectableBusA = terminal.getBusView().getConnectableBus();
return connectableBusA != null ? vlNode.getBusInnerNode(connectableBusA.getId()) : null;
private void addThreeWtEdge(ThreeWindingsTransformer twt, ThreeWtNode tn, ThreeWindingsTransformer.Side side) {
Terminal terminal = twt.getTerminal(side);
VoltageLevelNode vlNode = getOrCreateInvisibleVoltageLevelNode(terminal);
ThreeWtEdge edge = new ThreeWtEdge(idProvider.createId(IidmUtils.get3wtLeg(twt, side)),
twt.getId(), twt.getNameOrId(), IidmUtils.getThreeWtEdgeSideFromIidmSide(side), vlNode.isVisible());
graph.addEdge(vlNode, getBusInnerNode(terminal, vlNode), tn, edge);
}

private ThreeWindingsTransformer.Leg get3wtLeg(ThreeWindingsTransformer twt, ThreeWindingsTransformer.Side side) {
if (side == ThreeWindingsTransformer.Side.ONE) {
return twt.getLeg1();
} else if (side == ThreeWindingsTransformer.Side.TWO) {
return twt.getLeg2();
private ThreeWindingsTransformer.Side[] getSidesArray(ThreeWindingsTransformer.Side sideA) {
ThreeWindingsTransformer.Side sideB;
ThreeWindingsTransformer.Side sideC;
if (sideA == ThreeWindingsTransformer.Side.ONE) {
sideB = ThreeWindingsTransformer.Side.TWO;
sideC = ThreeWindingsTransformer.Side.THREE;
} else if (sideA == ThreeWindingsTransformer.Side.TWO) {
sideB = ThreeWindingsTransformer.Side.ONE;
sideC = ThreeWindingsTransformer.Side.THREE;
} else {
return twt.getLeg3();
sideB = ThreeWindingsTransformer.Side.ONE;
sideC = ThreeWindingsTransformer.Side.TWO;
}
return new ThreeWindingsTransformer.Side[] {sideA, sideB, sideC};
}

private VoltageLevelNode getOrCreateVoltageLevelNode(Terminal terminal) {
private BusInnerNode getBusInnerNode(Terminal terminal, VoltageLevelNode vlNode) {
Bus connectableBusA = terminal.getBusView().getConnectableBus();
return connectableBusA != null ? vlNode.getBusInnerNode(connectableBusA.getId()) : null;
}

private VoltageLevelNode getOrCreateInvisibleVoltageLevelNode(Terminal terminal) {
VoltageLevel vl = terminal.getVoltageLevel();
return graph.getVoltageLevelNode(vl.getId()).orElseGet(() -> createInvisibleVoltageLevelNode(vl));
}
Expand All @@ -195,17 +182,5 @@ private VoltageLevelNode createInvisibleVoltageLevelNode(VoltageLevel vl) {
graph.addNode(invisibleVlNode);
return invisibleVlNode;
}

private ThreeWtEdge.Side iidmSideToSide(ThreeWindingsTransformer.Side side) {
switch (Objects.requireNonNull(side)) {
case ONE:
return ThreeWtEdge.Side.ONE;
case TWO:
return ThreeWtEdge.Side.TWO;
case THREE:
return ThreeWtEdge.Side.THREE;
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.nad.utils.iidm.IidmUtils;

import java.util.HashSet;
import java.util.Objects;
Expand Down Expand Up @@ -96,8 +97,7 @@ public void visitThreeWindingsTransformer(ThreeWindingsTransformer twt, ThreeWin
}

private void visitBranch(Branch<?> branch, Branch.Side side) {
Branch.Side otherSide = side == Branch.Side.ONE ? Branch.Side.TWO : Branch.Side.ONE;
visitTerminal(branch.getTerminal(otherSide));
visitTerminal(branch.getTerminal(IidmUtils.getOpposite(side)));
}

private void visitTerminal(Terminal terminal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.*;
import com.powsybl.nad.utils.iidm.IidmUtils;
import com.powsybl.nad.model.*;
import com.powsybl.nad.svg.EdgeInfo;
import com.powsybl.nad.svg.LabelProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.nad.utils.iidm.IidmUtils;
import com.powsybl.nad.model.*;
import com.powsybl.nad.svg.AbstractStyleProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.iidm.network.*;
import com.powsybl.nad.model.BusInnerNode;
import com.powsybl.nad.model.Node;
import com.powsybl.nad.utils.iidm.IidmUtils;

import java.util.*;

Expand Down Expand Up @@ -77,7 +78,7 @@ private void findConnectedBuses(Bus bus, Set<Bus> visitedBus) {
bus.visitConnectedEquipments(new DefaultTopologyVisitor() {
@Override
public void visitLine(Line line, Branch.Side side) {
Terminal t = line.getTerminal(side == Branch.Side.ONE ? Branch.Side.TWO : Branch.Side.ONE);
Terminal t = line.getTerminal(IidmUtils.getOpposite(side));
Bus otherBus = t.getBusView().getBus();
if (otherBus != null && !visitedBus.contains(otherBus)) {
findConnectedBuses(otherBus, visitedBus);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.powsybl.nad.svg.iidm;
package com.powsybl.nad.utils.iidm;

import com.powsybl.iidm.network.*;
import com.powsybl.nad.model.BranchEdge;
Expand All @@ -21,6 +21,20 @@ public static Terminal getTerminalFromEdge(Network network, BranchEdge edge, Bra
}
}

public static ThreeWindingsTransformer.Leg get3wtLeg(ThreeWindingsTransformer twt, ThreeWindingsTransformer.Side side) {
if (side == ThreeWindingsTransformer.Side.ONE) {
return twt.getLeg1();
} else if (side == ThreeWindingsTransformer.Side.TWO) {
return twt.getLeg2();
} else {
return twt.getLeg3();
}
}

public static Branch.Side getOpposite(Branch.Side side) {
return side == Branch.Side.ONE ? Branch.Side.TWO : Branch.Side.ONE;
}

public static Branch.Side getIidmSideFromBranchEdgeSide(BranchEdge.Side side) {
return Objects.requireNonNull(side) == BranchEdge.Side.ONE ? Branch.Side.ONE : Branch.Side.TWO;
}
Expand All @@ -40,4 +54,17 @@ public static ThreeWindingsTransformer.Side getIidmSideFromThreeWtEdgeSide(Three
}
return null;
}

public static ThreeWtEdge.Side getThreeWtEdgeSideFromIidmSide(ThreeWindingsTransformer.Side side) {
switch (Objects.requireNonNull(side)) {
case ONE:
return ThreeWtEdge.Side.ONE;
case TWO:
return ThreeWtEdge.Side.TWO;
case THREE:
return ThreeWtEdge.Side.THREE;
}
return null;
}

}
24 changes: 12 additions & 12 deletions 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.

0 comments on commit 8228d67

Please sign in to comment.