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

include node id in loadflow result #147

Merged
merged 18 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -43,6 +43,7 @@
import java.util.function.Consumer;

import static org.gridsuite.loadflow.server.service.LoadFlowService.COMPUTATION_TYPE;
import static org.gridsuite.loadflow.server.utils.LoadflowResultsUtils.getIdFromViolation;

/**
* @author Anis Touri <anis.touri at rte-france.com>
Expand Down Expand Up @@ -190,9 +191,9 @@ protected List<LimitViolationInfos> calculateOverloadLimitViolations(List<LimitV
return limitViolationInfos;
}

public static LimitViolationInfos toLimitViolationInfos(LimitViolation violation) {
public static LimitViolationInfos toLimitViolationInfos(LimitViolation violation, Network network) {
return LimitViolationInfos.builder()
.subjectId(violation.getSubjectId())
.subjectId(getIdFromViolation(violation, network))
.actualOverloadDuration(violation.getAcceptableDuration())
.upComingOverloadDuration(violation.getAcceptableDuration())
.limit(violation.getLimit())
Expand Down Expand Up @@ -222,7 +223,7 @@ private List<LimitViolationInfos> getLimitViolations(Network network, LoadFlowRu

}
return violations.stream()
.map(LoadFlowWorkerService::toLimitViolationInfos).toList();
.map(limitViolation -> toLimitViolationInfos(limitViolation, network)).toList();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.gridsuite.loadflow.server.utils;

import com.powsybl.iidm.network.*;
import com.powsybl.security.BusBreakerViolationLocation;
import com.powsybl.security.LimitViolation;
import com.powsybl.security.NodeBreakerViolationLocation;
import com.powsybl.security.ViolationLocation;

import java.util.*;

import static com.powsybl.security.ViolationLocation.Type.NODE_BREAKER;

public final class LoadflowResultsUtils {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why all methods are public
It's used only by LoadFlowWorkerService

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 code has been moved to powsybl-ws-commons , to avoid duplicating it for security-analysis-server


private LoadflowResultsUtils() {
}

public static String getIdFromViolation(LimitViolation limitViolation, Network network) {
Copy link
Contributor

Choose a reason for hiding this comment

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

getViolationLocationId ?

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

Optional<ViolationLocation> violationLocation = limitViolation.getViolationLocation();
if (violationLocation.isEmpty()) {
return limitViolation.getSubjectId();
}

ViolationLocation location = violationLocation.get();
if (location.getType() == NODE_BREAKER) {
NodeBreakerViolationLocation nodeBreakerViolationLocation = (NodeBreakerViolationLocation) location;
return getBusIdOrVlIdNodeBreaker(nodeBreakerViolationLocation, network);
} else {
BusBreakerViolationLocation busBreakerViolationLocation = (BusBreakerViolationLocation) location;
return getBusIdOrVlIdBusBreaker(busBreakerViolationLocation, network, limitViolation.getSubjectId());
}
}

public static String getBusIdOrVlIdNodeBreaker(NodeBreakerViolationLocation nodeBreakerViolationLocation, Network network) {
VoltageLevel vl = network.getVoltageLevel(nodeBreakerViolationLocation.getVoltageLevelId());

List<String> busIds = nodeBreakerViolationLocation.getNodes().stream()
.map(node -> vl.getNodeBreakerView().getTerminal(node))
.filter(Objects::nonNull)
.map(Terminal::getConnectable)
.filter(t -> t.getType() == IdentifiableType.BUSBAR_SECTION)
.map(busBar -> ((BusbarSection) busBar).getTerminal().getBusView().getBus())
.filter(Objects::nonNull)
.map(Bus::getId)
.distinct()
.toList();
Copy link
Contributor

Choose a reason for hiding this comment

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

In nodebreaker we want busbar sections ids and not bus ids

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 in powsybl-ws-commons


return formatNodeId(busIds, nodeBreakerViolationLocation.getVoltageLevelId());
}

public static String formatNodeId(List<String> nodesIds, String subjectId) {
if (nodesIds.size() == 1) {
return nodesIds.get(0);
} else if (nodesIds.isEmpty()) {
return subjectId;
} else {
return subjectId + " (" + String.join(", ", nodesIds) + " )";
}
}

public static String getBusIdOrVlIdBusBreaker(BusBreakerViolationLocation busBreakerViolationLocation, Network network, String subjectId) {
List<String> busBreakerIds = busBreakerViolationLocation
.getBusBreakerView(network)
.getBusStream()
.map(Identifiable::getId)
.distinct().toList();
return formatNodeId(busBreakerIds, subjectId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ private static void assertLimitViolationsEquals(List<LimitViolation> limitViolat
assertEquals(limitViolationsDto.get(i).getValue(), limitViolations.get(i).getValue(), 0.01);
assertEquals(limitViolationsDto.get(i).getSide(), limitViolations.get(i).getSide() != null ? limitViolations.get(i).getSide().name() : "");
assertEquals(limitViolationsDto.get(i).getLimitType(), limitViolations.get(i).getLimitType());
assertEquals(limitViolationsDto.get(i).getActualOverloadDuration(), LoadFlowWorkerService.calculateActualOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i)), network));
assertEquals(limitViolationsDto.get(i).getUpComingOverloadDuration(), LoadFlowWorkerService.calculateUpcomingOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i))));
assertEquals(limitViolationsDto.get(i).getActualOverloadDuration(), LoadFlowWorkerService.calculateActualOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i), network), network));
assertEquals(limitViolationsDto.get(i).getUpComingOverloadDuration(), LoadFlowWorkerService.calculateUpcomingOverloadDuration(LoadFlowWorkerService.toLimitViolationInfos(limitViolations.get(i), network)));
assertEquals(limitViolationsDto.get(i).getOverload(), (limitViolations.get(i).getValue() / limitViolations.get(i).getLimit()) * 100, 0.01);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.gridsuite.loadflow.server;

import com.powsybl.security.LimitViolationType;
import com.powsybl.iidm.network.Network;
import com.powsybl.security.*;
import org.gridsuite.loadflow.server.dto.LimitViolationInfos;
import org.gridsuite.loadflow.server.entities.LimitViolationEntity;
import org.gridsuite.loadflow.server.entities.LoadFlowResultEntity;
import org.gridsuite.loadflow.server.repositories.LimitViolationRepository;
import org.gridsuite.loadflow.server.service.LoadFlowService;
import org.gridsuite.loadflow.server.utils.LoadflowResultsUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -15,8 +17,10 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.gridsuite.loadflow.server.Networks.createNetwork;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -88,4 +92,62 @@ void getLimitViolationsInfosWhenCalledReturnsCorrectData() {
assertEquals(LimitViolationsMock.limitViolationEntities.size(), result.size());
verify(limitViolationRepository, times(1)).findAll(any(Specification.class), eq(sort));
}

@Test
void testFormatNodeIdSingleNode() {
String result = LoadflowResultsUtils.formatNodeId(List.of("Node1"), "Subject1");
assertEquals("Node1", result);
}

@Test
void testFormatNodeIdEmptyNodes() {
String result = LoadflowResultsUtils.formatNodeId(List.of(), "Subject1");
assertEquals("Subject1", result);
}

@Test
void testFormatNodeIdMultipleNodes() {
String result = LoadflowResultsUtils.formatNodeId(List.of("Node1", "Node2"), "Subject1");
assertEquals("Subject1 (Node1, Node2 )", result);
}

@Test
void testGetIdFromViolationWithBusBreaker() {
// Setup
Network network = createNetwork("", true);
BusBreakerViolationLocation busBreakerLocation = new BusBreakerViolationLocation(List.of("NGEN"));
LimitViolation limitViolation = mock(LimitViolation.class);
when(limitViolation.getViolationLocation()).thenReturn(Optional.of(busBreakerLocation));

assertEquals("NGEN", LoadflowResultsUtils.getIdFromViolation(limitViolation, network));
}

@Test
void testGetIdFromViolationWithNoViolationLocation() {
// Setup
Network network = mock(Network.class);
LimitViolation limitViolation = mock(LimitViolation.class);

when(limitViolation.getViolationLocation()).thenReturn(Optional.empty());
when(limitViolation.getSubjectId()).thenReturn("SubjectId");

assertEquals("SubjectId", LoadflowResultsUtils.getIdFromViolation(limitViolation, network));
}

@Test
void testGetIdFromViolationWithNodeBreaker() {
// Create a real network instead of mocking it
Network network = createNetwork("testNetwork", true);

NodeBreakerViolationLocation nodeBreakerViolationLocation = mock(NodeBreakerViolationLocation.class);
when(nodeBreakerViolationLocation.getType()).thenReturn(ViolationLocation.Type.NODE_BREAKER);
when(nodeBreakerViolationLocation.getVoltageLevelId()).thenReturn("VL1");

LimitViolation limitViolation = mock(LimitViolation.class);
when(limitViolation.getViolationLocation()).thenReturn(Optional.of(nodeBreakerViolationLocation));
when(limitViolation.getSubjectId()).thenReturn("SubjectId");

String result = LoadflowResultsUtils.getIdFromViolation(limitViolation, network);
assertEquals("VL1", result);
}
}