Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 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 @@ -6,42 +6,123 @@
*/
package org.gridsuite.network.map.dto.definition.extension;

import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Switch;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.math.graph.TraverseResult;
import com.powsybl.iidm.network.*;

import java.util.*;

/**
* @author Slimane Amar <slimane.amar at rte-france.com>
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
*/
public class BusbarSectionFinderTraverser implements Terminal.TopologyTraverser {
// TODO : to remove when this class is available in network-store
public final class BusbarSectionFinderTraverser {

private BusbarSectionFinderTraverser() {
throw new UnsupportedOperationException();
}

private final boolean onlyConnectedBbs;
private record NodePath(int node, List<SwitchInfo> pathSwitches, SwitchInfo lastSwitch) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private record NodePath(int node, List<SwitchInfo> pathSwitches, SwitchInfo lastSwitch) { }
private record Path(int startNode, List<SwitchInfo> traversedSwitches, SwitchInfo lastSwitch) { }

Something like this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The term 'Path' is a generic name
it is ok for startNode and traversedSwitches


private String firstTraversedBbsId;
public record SwitchInfo(String id, SwitchKind kind, boolean isOpen) { }
Copy link
Contributor

Choose a reason for hiding this comment

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

SwitchKind kind is not used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used it at the beginning for logging and debugging purposes
to be removed


public BusbarSectionFinderTraverser(boolean onlyConnectedBbs) {
this.onlyConnectedBbs = onlyConnectedBbs;
public record BusbarSectionResult(String busbarSectionId, int depth, SwitchInfo lastSwitch) { }

public static String findBusbarSectionId(Terminal terminal) {
BusbarSectionResult result = getBusbarSectionResult(terminal);
return result != null ? result.busbarSectionId() : null;
}

@Override
public TraverseResult traverse(Terminal terminal, boolean connected) {
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
firstTraversedBbsId = terminal.getConnectable().getId();
return TraverseResult.TERMINATE_TRAVERSER;
public static BusbarSectionResult getBusbarSectionResult(Terminal terminal) {
VoltageLevel.NodeBreakerView view = terminal.getVoltageLevel().getNodeBreakerView();
int startNode = terminal.getNodeBreakerView().getNode();
List<BusbarSectionResult> allResults = searchAllBusbars(view, startNode);
if (allResults.isEmpty()) {
return null;
}
return TraverseResult.CONTINUE;
return selectBestBusbar(allResults);
}

@Override
public TraverseResult traverse(Switch aSwitch) {
if (onlyConnectedBbs && aSwitch.isOpen()) {
return TraverseResult.TERMINATE_PATH;
private static BusbarSectionResult selectBestBusbar(List<BusbarSectionResult> results) {
List<BusbarSectionResult> withoutSwitch = results.stream().filter(r -> r.lastSwitch() == null).toList();
if (!withoutSwitch.isEmpty()) {
return withoutSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null);
}
List<BusbarSectionResult> withClosedSwitch = results.stream().filter(r -> r.lastSwitch() != null && !r.lastSwitch().isOpen()).toList();
if (!withClosedSwitch.isEmpty()) {
return withClosedSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null);
}
List<BusbarSectionResult> withOpenSwitch = results.stream().filter(r -> r.lastSwitch() != null && r.lastSwitch().isOpen()).toList();
if (!withOpenSwitch.isEmpty()) {
return withOpenSwitch.stream().min(Comparator.comparingInt(BusbarSectionResult::depth)).orElse(null);
}
return results.getFirst();
Copy link
Contributor

Choose a reason for hiding this comment

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

can be empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case is handled by returning result.busbarSectionId() when result is not null, and otherwise the Id of the first busbar section in the terminal’s voltage level

}

private static List<BusbarSectionResult> searchAllBusbars(VoltageLevel.NodeBreakerView view, int startNode) {
List<BusbarSectionResult> results = new ArrayList<>();
Set<Integer> visited = new HashSet<>();
Queue<NodePath> queue = new LinkedList<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Queue<NodePath> queue = new LinkedList<>();
Queue<NodePath> pathsToVisit = new LinkedList<>();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nodePathsToVisit maybe ?

queue.offer(new NodePath(startNode, new ArrayList<>(), null));
while (!queue.isEmpty()) {
NodePath currentNodePath = queue.poll();
if (!hasNotBeenVisited(currentNodePath.node(), visited)) {
continue;
}
visited.add(currentNodePath.node());
Optional<BusbarSectionResult> busbarSectionResult = tryCreateBusbarResult(view, currentNodePath);
if (busbarSectionResult.isPresent()) {
results.add(busbarSectionResult.get());
} else {
exploreAdjacentNodes(view, currentNodePath, visited, queue);
}
}
return results;
}

private static boolean hasNotBeenVisited(int node, Set<Integer> visited) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static boolean hasNotBeenVisited(int node, Set<Integer> visited) {
private static boolean hasBeenVisited(int node, Set<Integer> visited) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

return !visited.contains(node);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return !visited.contains(node);
return visited.contains(node);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

}

private static Optional<BusbarSectionResult> tryCreateBusbarResult(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static Optional<BusbarSectionResult> tryCreateBusbarResult(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) {
private static Optional<BusbarSectionResult> getDirectlyConnectedBusbarSection(VoltageLevel.NodeBreakerView view, NodePath currentNodePath) {

Something like this ? Here, we check if a busbar section is directly connected to the node and return it if yes

Copy link
Contributor

Choose a reason for hiding this comment

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

something better than getDirectlyConnectedBusbarSection() to be found

Copy link
Contributor Author

Choose a reason for hiding this comment

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

findBusbarSectionAtNode maybe ?

Optional<Terminal> nodeTerminal = view.getOptionalTerminal(currentNodePath.node());
if (nodeTerminal.isEmpty()) {
return Optional.empty();
}
Terminal term = nodeTerminal.get();
if (term.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
String busbarSectionId = term.getConnectable().getId();
int depth = currentNodePath.pathSwitches().size();
SwitchInfo lastSwitch = currentNodePath.lastSwitch();
return Optional.of(new BusbarSectionResult(busbarSectionId, depth, lastSwitch));
}
return Optional.empty();
}

private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> queue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> queue) {
private static void exploreAdjacentNodes(VoltageLevel.NodeBreakerView view, NodePath currentNodePath, Set<Integer> visited, Queue<NodePath> pathsToVisit) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

view.getSwitchStream().forEach(sw -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not efficient. Maybe we can do something better

int node1 = view.getNode1(sw.getId());
int node2 = view.getNode2(sw.getId());
Optional<Integer> nextNode = getNextNodeIfAdjacent(currentNodePath.node(), node1, node2);
if (nextNode.isPresent() && !visited.contains(nextNode.get())) {
NodePath newPath = createNodePath(currentNodePath, sw, nextNode.get());
queue.offer(newPath);
}
});
}

private static Optional<Integer> getNextNodeIfAdjacent(int currentNode, int node1, int node2) {
if (node1 == currentNode) {
return Optional.of(node2);
}
if (node2 == currentNode) {
return Optional.of(node1);
}
return TraverseResult.CONTINUE;
return Optional.empty();
}

public String getFirstTraversedBbsId() {
return firstTraversedBbsId;
private static NodePath createNodePath(NodePath currentNodePath, Switch sw, int nextNode) {
List<SwitchInfo> newPathSwitches = new ArrayList<>(currentNodePath.pathSwitches());
SwitchInfo switchInfo = new SwitchInfo(sw.getId(), sw.getKind(), sw.isOpen());
newPathSwitches.add(switchInfo);
return new NodePath(nextNode, newPathSwitches, switchInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.math.graph.TraversalType;
import org.gridsuite.network.map.dto.common.ReactiveCapabilityCurveMapData;
import org.gridsuite.network.map.dto.common.TapChangerData;
import org.gridsuite.network.map.dto.common.TapChangerStepData;
Expand Down Expand Up @@ -92,9 +91,8 @@ public static String getBusOrBusbarSection(Terminal terminal) {
return terminal.getBusBreakerView().getConnectableBus().getId();
}
} else {
final BusbarSectionFinderTraverser connectedBusbarSectionFinder = new BusbarSectionFinderTraverser(terminal.isConnected());
terminal.traverse(connectedBusbarSectionFinder, TraversalType.BREADTH_FIRST);
return connectedBusbarSectionFinder.getFirstTraversedBbsId();
// NODE_BREAKER: explore all paths and choose the busbar with the closed disconnector
return BusbarSectionFinderTraverser.findBusbarSectionId(terminal);
}
}

Expand Down
Loading
Loading