Skip to content
Open
Show file tree
Hide file tree
Changes from 20 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 @@ -8,40 +8,82 @@

import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Switch;
import com.powsybl.iidm.network.SwitchKind;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.math.graph.TraverseResult;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Slimane Amar <slimane.amar at rte-france.com>
*/
public class BusbarSectionFinderTraverser implements Terminal.TopologyTraverser {
Copy link
Contributor

Choose a reason for hiding this comment

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

Move those changes in another PR. So the review can be done with #288. And the changes in the tests will be reviewed with it

Copy link
Contributor

Choose a reason for hiding this comment

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

done


private final boolean onlyConnectedBbs;

private String firstTraversedBbsId;

public BusbarSectionFinderTraverser(boolean onlyConnectedBbs) {
this.onlyConnectedBbs = onlyConnectedBbs;
}
private final List<BusbarCandidate> busbarCandidates = new ArrayList<>();
private final Set<String> visitedTerminals = new HashSet<>();
private static final int MAX_VISITED = 50;

@Override
public TraverseResult traverse(Terminal terminal, boolean connected) {
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
firstTraversedBbsId = terminal.getConnectable().getId();
String terminalId = terminal.getConnectable().getId();
if (visitedTerminals.contains(terminalId)) {
return TraverseResult.TERMINATE_PATH;
}
visitedTerminals.add(terminalId);
if (visitedTerminals.size() > MAX_VISITED) {
return TraverseResult.TERMINATE_TRAVERSER;
}

// If a busbar section is found, add it as a candidate
if (terminal.getConnectable().getType() == IdentifiableType.BUSBAR_SECTION) {
busbarCandidates.add(new BusbarCandidate(terminalId, connected));
// CONTINUE to explore other paths to other busbars
return TraverseResult.CONTINUE;
}
return TraverseResult.CONTINUE;
}

@Override
public TraverseResult traverse(Switch aSwitch) {
if (onlyConnectedBbs && aSwitch.isOpen()) {
return TraverseResult.TERMINATE_PATH;
if (visitedTerminals.size() > MAX_VISITED) {
return TraverseResult.TERMINATE_TRAVERSER;
}

// KEY: Open disconnectors end this path but not the overall traversal
// They block access to this busbar but not to the others
if (aSwitch.isOpen() && aSwitch.getKind() == SwitchKind.DISCONNECTOR) {
return TraverseResult.TERMINATE_PATH; // Ends this path, not the whole traversal
}
return TraverseResult.CONTINUE;
}

public String getFirstTraversedBbsId() {
return firstTraversedBbsId;
public String getBusbarWithClosedDisconnector() {
// Search for a connected busbar (disconnector closed)
for (BusbarCandidate candidate : busbarCandidates) {
if (candidate.isConnected()) {
return candidate.getId();
}
}

// If none is connected, return the first one found (fallback)
if (!busbarCandidates.isEmpty()) {
return busbarCandidates.getFirst().getId();
}
return null;
}

@Getter
private static class BusbarCandidate {
private final String id;
private final boolean connected;

public BusbarCandidate(String id, boolean connected) {
this.id = id;
this.connected = connected;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.network.map.dto.definition.extension;

import com.fasterxml.jackson.annotation.JsonInclude;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.network.map.dto.definition.voltagelevel;

import com.powsybl.iidm.network.ThreeSides;
import org.gridsuite.network.map.dto.definition.extension.ConnectablePositionInfos;

/**
* @author Etienne Lesot <etienne.lesot at rte-france.com>
*/
public record FeederBayInfos(String busbarSectionId, ConnectablePositionInfos connectablePositionInfos, ThreeSides connectionSide) { }
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ public class VoltageLevelFormInfos extends ElementInfosWithProperties {

@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, List<String>> busBarSectionInfos;

@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, List<FeederBayInfos>> feederBaysInfos;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.gridsuite.network.map.dto.ElementInfos;
import org.gridsuite.network.map.dto.InfoTypeParameters;
import org.gridsuite.network.map.dto.definition.busbarsection.BusBarSectionFormInfos;
import org.gridsuite.network.map.dto.definition.voltagelevel.FeederBayInfos;
import org.gridsuite.network.map.dto.definition.voltagelevel.VoltageLevelFormInfos;
import org.gridsuite.network.map.dto.definition.voltagelevel.VoltageLevelMapInfos;
import org.gridsuite.network.map.dto.definition.voltagelevel.VoltageLevelTabInfos;
Expand Down Expand Up @@ -106,14 +107,52 @@ static VoltageLevelFormInfos toFormInfos(Identifiable<?> identifiable) {
builder.isRetrievedBusbarSections(vlTopologyInfos.isRetrievedBusbarSections());
builder.isBusbarSectionPositionFound(vlTopologyInfos.isBusbarSectionPositionFound());
builder.busBarSectionInfos(vlTopologyInfos.getBusBarSectionInfosGrouped());
builder.feederBaysInfos(getFeederBaysInfos(voltageLevel));
}

builder.identifiableShortCircuit(ExtensionUtils.toIdentifiableShortCircuit(voltageLevel));

return builder.build();
}

static VoltageLevelMapInfos toMapInfos(Identifiable<?> identifiable) {
private static Map<String, List<FeederBayInfos>> getFeederBaysInfos(VoltageLevel voltageLevel) {
Copy link
Contributor

Choose a reason for hiding this comment

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

    private static Map<String, List<FeederBayInfos>> getFeederBaysInfos(VoltageLevel voltageLevel) {
        Map<String, List<FeederBayInfos>> feederBayInfos = new HashMap<>();
        voltageLevel.getConnectableStream()
            .filter(connectable -> !(connectable instanceof BusbarSection))
            .forEach(connectable -> {
                List<FeederBayInfos> connections = new ArrayList<>();
                connectable.getTerminals().forEach(obj -> {
                    Terminal terminal = (Terminal) obj;
                    if (terminal.getVoltageLevel().getId().equals(voltageLevel.getId())) {
                        connections.add(
                                new FeederBayInfos(
                                    getBusOrBusbarSection(terminal),
                                    getConnectablePosition(connectable, getConnectableSide(terminal).map(side -> side.getNum()).orElse(0)),
                                    getConnectableSide(terminal).orElse(null)
                                )
                        );
                    }
                });
                feederBayInfos.put(connectable.getId(), connections);
            });
        return feederBayInfos;
    }

Copy link
Contributor

Choose a reason for hiding this comment

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

something like this to be more succinct ?

Copy link
Contributor

Choose a reason for hiding this comment

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

done

Map<String, List<FeederBayInfos>> connections = new HashMap<>();
voltageLevel.getConnectableStream()
.filter(connectable -> !(connectable instanceof BusbarSection))
.forEach(connectable -> {
switch (connectable) {
case Injection<?> injection -> connections.put(injection.getId(), List.of(new FeederBayInfos(getBusOrBusbarSection(injection.getTerminal()),
toMapConnectablePosition(injection, 0), null)));
case Branch<?> branch -> {
List<FeederBayInfos> branchConnections = new ArrayList<>();
if (branch.getTerminal1().getVoltageLevel().getId().equals(voltageLevel.getId())) {
branchConnections.add(new FeederBayInfos(getBusOrBusbarSection(branch.getTerminal1()), toMapConnectablePosition(branch, 1), ThreeSides.ONE));
}
if (branch.getTerminal2().getVoltageLevel().getId().equals(voltageLevel.getId())) {
branchConnections.add(new FeederBayInfos(getBusOrBusbarSection(branch.getTerminal2()), toMapConnectablePosition(branch, 2), ThreeSides.TWO));
}
connections.put(branch.getId(), branchConnections);
}
case ThreeWindingsTransformer threeWindingsTransformer -> {
List<FeederBayInfos> threeWTConnections = new ArrayList<>();
if (threeWindingsTransformer.getLeg1().getTerminal().getVoltageLevel().getId().equals(voltageLevel.getId())) {
threeWTConnections.add(new FeederBayInfos(getBusOrBusbarSection(threeWindingsTransformer.getLeg1().getTerminal()), toMapConnectablePosition(threeWindingsTransformer, 1), ThreeSides.ONE));
}
if (threeWindingsTransformer.getLeg2().getTerminal().getVoltageLevel().getId().equals(voltageLevel.getId())) {
threeWTConnections.add(new FeederBayInfos(getBusOrBusbarSection(threeWindingsTransformer.getLeg2().getTerminal()), toMapConnectablePosition(threeWindingsTransformer, 2), ThreeSides.TWO));
}
if (threeWindingsTransformer.getLeg3().getTerminal().getVoltageLevel().getId().equals(voltageLevel.getId())) {
threeWTConnections.add(new FeederBayInfos(getBusOrBusbarSection(threeWindingsTransformer.getLeg3().getTerminal()), toMapConnectablePosition(threeWindingsTransformer, 3), ThreeSides.THREE));
}
connections.put(threeWindingsTransformer.getId(), threeWTConnections);
}
default -> throw new IllegalArgumentException("connectable type: " + connectable.getClass() + " not supported");
}
});
return connections;
}

protected static VoltageLevelMapInfos toMapInfos(Identifiable<?> identifiable) {
VoltageLevel voltageLevel = (VoltageLevel) identifiable;
return VoltageLevelMapInfos.builder()
.id(voltageLevel.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
package org.gridsuite.network.map.dto.utils;

import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.math.graph.TraversalType;
import lombok.NonNull;
import org.gridsuite.network.map.dto.common.ReactiveCapabilityCurveMapData;
import org.gridsuite.network.map.dto.common.TapChangerData;
import org.gridsuite.network.map.dto.common.TapChangerStepData;
import org.gridsuite.network.map.dto.definition.extension.BusbarSectionFinderTraverser;
import org.springframework.lang.NonNull;
import org.gridsuite.network.map.dto.definition.extension.ConnectablePositionInfos;

import java.util.Collection;
import java.util.List;
Expand All @@ -39,6 +41,39 @@ public static void setIfNotNan(@NonNull final DoubleConsumer setter, final doubl
}
}

private static ConnectablePosition.Feeder getFeederInfos(Identifiable<?> identifiable, int index) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This code should use ThreeSides

Suggested change
private static ConnectablePosition.Feeder getFeederInfos(Identifiable<?> identifiable, int index) {
private static ConnectablePosition.Feeder getFeederInfos(Identifiable<?> identifiable, ThreeSides side) {

Copy link
Contributor

Choose a reason for hiding this comment

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

With the index we can handle injections and branches, but the ThreeSides type cannot manage that

var connectablePosition = identifiable.getExtension(ConnectablePosition.class);
if (connectablePosition == null) {
return null;
}

switch (index) {
case 0:
return connectablePosition.getFeeder();
case 1:
return connectablePosition.getFeeder1();
case 2:
return connectablePosition.getFeeder2();
default:
throw new IllegalArgumentException("Invalid feeder index: " + index);
}
}

public static ConnectablePositionInfos toMapConnectablePosition(Identifiable<?> identifiable, int index) {
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
public static ConnectablePositionInfos toMapConnectablePosition(Identifiable<?> identifiable, int index) {
public static ConnectablePositionInfos getConnectablePosition(Identifiable<?> identifiable, ThreeSides side) {

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with the renaming, but not with changing int index to ThreeSides side, because we need to specify the index to retrieve the feeder

ConnectablePosition.Feeder feeder = getFeederInfos(identifiable, index);
return convertFeederToConnectablePositionInfos(feeder);
}

public static ConnectablePositionInfos convertFeederToConnectablePositionInfos(ConnectablePosition.Feeder feeder) {
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
public static ConnectablePositionInfos convertFeederToConnectablePositionInfos(ConnectablePosition.Feeder feeder) {
public static ConnectablePositionInfos buildConnectablePositionInfos(ConnectablePosition.Feeder feeder) {

Copy link
Contributor

Choose a reason for hiding this comment

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

done

ConnectablePositionInfos.ConnectablePositionInfosBuilder builder = ConnectablePositionInfos.builder();
if (feeder != null) {
builder.connectionDirection(feeder.getDirection() == null ? null : feeder.getDirection())
.connectionPosition(feeder.getOrder().orElse(null))
.connectionName(feeder.getName().orElse(null));
}
return builder.build();
}

public static String getBusOrBusbarSection(Terminal terminal) {
if (terminal.getVoltageLevel().getTopologyKind().equals(TopologyKind.BUS_BREAKER)) {
if (terminal.isConnected()) {
Expand All @@ -47,9 +82,10 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

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

Move those changes in another PR. So the review can be done with #288. And the changes in the tests will be reviewed with it

Copy link
Contributor

Choose a reason for hiding this comment

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

done

BusbarSectionFinderTraverser finder = new BusbarSectionFinderTraverser();
terminal.traverse(finder, TraversalType.BREADTH_FIRST);
return finder.getBusbarWithClosedDisconnector();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static ConnectablePositionInfos toMapConnectablePosition(@NonNull final I
case 0 -> connectablePosition.getFeeder();
case 1 -> connectablePosition.getFeeder1();
case 2 -> connectablePosition.getFeeder2();
case 3 -> connectablePosition.getFeeder3();
default -> throw new IllegalArgumentException("Invalid feeder index: " + index);
})
.ifPresent(feeder -> builder
Expand Down
Loading