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 all 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 4 (expected 3).
-->
<antlr4.version>4.10.1</antlr4.version>
<!-- FIXME: powsybl-ws-commons modules'version is overloaded in the dependencies section
The overloads and this property below have to be removed at next powsybl-ws-dependencies.version upgrade -->
<powsybl-ws-commons.version>1.17.0</powsybl-ws-commons.version>
</properties>

<build>
Expand Down Expand Up @@ -136,6 +139,8 @@
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
<!-- FIXME: to be removed at next powsybl-ws-dependencies upgrade -->
<version>${powsybl-ws-commons.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static com.powsybl.ws.commons.computation.utils.ComputationResultUtils.getViolationLocationId;
import static org.gridsuite.loadflow.server.service.LoadFlowService.COMPUTATION_TYPE;

/**
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(getViolationLocationId(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
Expand Up @@ -11,10 +11,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.powsybl.commons.report.ReportNode;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.TwoSides;
import com.powsybl.iidm.network.VariantManagerConstants;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;
Expand All @@ -23,6 +20,7 @@
import com.powsybl.network.store.client.NetworkStoreService;
import com.powsybl.network.store.client.PreloadingStrategy;
import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl;
import com.powsybl.security.BusBreakerViolationLocation;
import com.powsybl.security.LimitViolation;
import com.powsybl.security.LimitViolationType;
import com.powsybl.security.Security;
Expand Down Expand Up @@ -99,6 +97,10 @@ public class LoadFlowControllerTest {

private static final int TIMEOUT = 1000;

private static final List<LimitViolation> LIMIT_VIOLATIONS_WITH_LOCATION = List.of(
new LimitViolation("VLGEN", "", LimitViolationType.LOW_VOLTAGE, "limit1", 60, 1500, 0.7F, 1300, ThreeSides.TWO, new BusBreakerViolationLocation(List.of("NHV1"))),
new LimitViolation("VLGEN", "", LimitViolationType.HIGH_VOLTAGE, "limit2", 300, 900, 0.7F, 1000, ThreeSides.ONE, new BusBreakerViolationLocation(List.of("NHV2"))));

@Autowired
private OutputDestination output;
@Autowired
Expand Down Expand Up @@ -145,8 +147,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 Expand Up @@ -792,4 +794,47 @@ private static final class LimitViolationsMock {
new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, TwoSides.ONE),
new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, TwoSides.TWO));
}

@Test
public void testGetLimitViolationsVoltage() throws Exception {
((Bus) network.getIdentifiable("NHV1")).setV(380.0).getVoltageLevel().setLowVoltageLimit(400.0).setHighVoltageLimit(450.0);
((Bus) network.getIdentifiable("NHV2")).setV(380.0).getVoltageLevel().setLowVoltageLimit(300.0).setHighVoltageLimit(350.0);

LoadFlow.Runner runner = Mockito.mock(LoadFlow.Runner.class);
try (MockedStatic<LoadFlow> loadFlowMockedStatic = Mockito.mockStatic(LoadFlow.class);
MockedStatic<Security> securityMockedStatic = Mockito.mockStatic(Security.class)) {
loadFlowMockedStatic.when(() -> LoadFlow.find(any())).thenReturn(runner);
securityMockedStatic.when(() -> Security.checkLimitsDc(any(), any(), anyDouble())).thenReturn(LIMIT_VIOLATIONS_WITH_LOCATION);

Mockito.when(runner.runAsync(eq(network), eq(VARIANT_2_ID), eq(executionService.getComputationManager()),
any(LoadFlowParameters.class), any(ReportNode.class)))
.thenReturn(CompletableFuture.completedFuture(LoadFlowResultMock.RESULT));

MvcResult result = mockMvc.perform(post(
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=LoadFlow&receiver=me&variantId=" + VARIANT_2_ID + "&parametersUuid=" + PARAMETERS_UUID + "&limitReduction=0.7", NETWORK_UUID)
.header(HEADER_USER_ID, "userId"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));

Message<byte[]> resultMessage = output.receive(1000, "loadflow.result");
assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid"));
assertEquals("me", resultMessage.getHeaders().get("receiver"));

// get loadflow limit violations
result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/limit-violations", RESULT_UUID))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
List<LimitViolationInfos> limitViolations = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() {
});
assertEquals(2, limitViolations.size());
// check that the subject id is equal to the Bus Id
assertEquals("VLHV1_0", limitViolations.get(0).getSubjectId());
assertEquals("VLHV2_0", limitViolations.get(1).getSubjectId());
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.gridsuite.loadflow.server;

import com.powsybl.security.LimitViolationType;
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;
Expand Down Expand Up @@ -88,4 +88,5 @@ void getLimitViolationsInfosWhenCalledReturnsCorrectData() {
assertEquals(LimitViolationsMock.limitViolationEntities.size(), result.size());
verify(limitViolationRepository, times(1)).findAll(any(Specification.class), eq(sort));
}

}
Loading