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

Add actual and upcoming overload : limitViolationInfos #74

Merged
merged 10 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -29,7 +29,9 @@ public class LimitViolationInfos {

private String limitName;

private Integer acceptableDuration;
private Integer actualOverloadDuration;

private Integer upComingOverloadDuration;

private Double value;

Expand All @@ -38,15 +40,16 @@ public class LimitViolationInfos {
private LimitViolationType limitType;

public LimitViolationEmbeddable toEmbeddable() {
return new LimitViolationEmbeddable(subjectId, limit, limitName, acceptableDuration, value, side, limitType);
return new LimitViolationEmbeddable(subjectId, limit, limitName, actualOverloadDuration, upComingOverloadDuration, value, side, limitType);
}

public static LimitViolationInfos toLimitViolationInfos(LimitViolationEmbeddable limitViolationEmbeddable) {
return LimitViolationInfos.builder()
.subjectId(limitViolationEmbeddable.getSubjectId())
.limit(limitViolationEmbeddable.getLimit())
.limitName(limitViolationEmbeddable.getLimitName())
.acceptableDuration(limitViolationEmbeddable.getAcceptableDuration())
.actualOverloadDuration(limitViolationEmbeddable.getActualOverload())
.upComingOverloadDuration(limitViolationEmbeddable.getUpComingOverload())
.value(limitViolationEmbeddable.getValue())
.side(limitViolationEmbeddable.getSide())
.limitType(limitViolationEmbeddable.getLimitType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public class LimitViolationEmbeddable {
private String limitName;

@Column
private Integer acceptableDuration;
private Integer actualOverload;

@Column
private Integer upComingOverload;

@Column(name = "value_")
private Double value;
Expand All @@ -45,4 +48,5 @@ public class LimitViolationEmbeddable {
@Column
@Enumerated(EnumType.STRING)
private LimitViolationType limitType;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.ReporterModel;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VariantManagerConstants;
import com.powsybl.iidm.network.*;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.network.store.client.NetworkStoreService;
import com.powsybl.network.store.client.PreloadingStrategy;
import com.powsybl.security.LimitViolation;
import com.powsybl.security.LimitViolationType;
import com.powsybl.security.Security;
import org.apache.commons.lang3.StringUtils;
import org.gridsuite.loadflow.server.dto.LimitViolationInfos;
Expand Down Expand Up @@ -50,7 +50,6 @@ public class LoadFlowWorkerService {

private static final Logger LOGGER = LoggerFactory.getLogger(LoadFlowWorkerService.class);
private static final String LOAD_FLOW_TYPE_REPORT = "LoadFlow";

private Lock lockRunAndCancelLF = new ReentrantLock();

private ObjectMapper objectMapper;
Expand Down Expand Up @@ -143,6 +142,59 @@ private void cleanLoadFlowResultsAndPublishCancel(UUID resultUuid, String receiv
notificationService.publishStop(resultUuid, receiver);
}

private static LoadingLimits.TemporaryLimit getBranchLowerLimitViolation(Branch<?> branch, LimitViolationInfos violationInfo) {
dbraquart marked this conversation as resolved.
Show resolved Hide resolved
// limits are returned from the store by DESC duration / ASC value
Optional<CurrentLimits> currentLimits = violationInfo.getSide().equals(Branch.Side.ONE.name()) ? branch.getCurrentLimits1() : branch.getCurrentLimits2();
if (!currentLimits.isPresent() || violationInfo.getValue() < currentLimits.get().getPermanentLimit()) {
return null;
} else {
Optional<LoadingLimits.TemporaryLimit> nextTemporaryLimit = currentLimits.get().getTemporaryLimits().stream()
.filter(tl -> violationInfo.getValue() < tl.getValue())
.findFirst();
if (nextTemporaryLimit.isPresent()) {
return nextTemporaryLimit.get();
}
}
return null;
}

public static Integer calculateUpcomingOverloadDuration(LimitViolationInfos limitViolationInfo) {
if (limitViolationInfo.getValue() < limitViolationInfo.getLimit()) {
return limitViolationInfo.getUpComingOverloadDuration();
}
return null;
}

public static Integer calculateActualOverloadDuration(LimitViolationInfos limitViolationInfo, Network network) {
if (limitViolationInfo.getValue() > limitViolationInfo.getLimit()) {
return limitViolationInfo.getActualOverloadDuration();
} else {
String equipmentId = limitViolationInfo.getSubjectId();
LoadingLimits.TemporaryLimit tempLimit = null;

Line line = network.getLine(equipmentId);
if (line != null) {
tempLimit = getBranchLowerLimitViolation(line, limitViolationInfo);
} else {
TwoWindingsTransformer twoWindingsTransformer = network.getTwoWindingsTransformer(equipmentId);
if (twoWindingsTransformer != null) {
tempLimit = getBranchLowerLimitViolation(twoWindingsTransformer, limitViolationInfo);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider also the three windings transformer

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 checked with PO and Techlead, we will not consider three windings transformer

}
return (tempLimit != null) ? tempLimit.getAcceptableDuration() : null;
}
}

private List<LimitViolationInfos> calculateOverloadLimitViolations(List<LimitViolationInfos> limitViolationInfos, Network network) {
for (LimitViolationInfos violationInfo : limitViolationInfos) {
if (violationInfo.getLimitName() != null && violationInfo.getLimitType() == LimitViolationType.CURRENT) {
violationInfo.setActualOverloadDuration(calculateActualOverloadDuration(violationInfo, network));
violationInfo.setUpComingOverloadDuration(calculateUpcomingOverloadDuration(violationInfo));
}
}
return limitViolationInfos;
}

private void cancelLoadFlowAsync(LoadFlowCancelContext cancelContext) {
lockRunAndCancelLF.lock();
try {
Expand All @@ -162,7 +214,8 @@ private void cancelLoadFlowAsync(LoadFlowCancelContext cancelContext) {
public static LimitViolationInfos toLimitViolationInfos(LimitViolation violation) {
return LimitViolationInfos.builder()
.subjectId(violation.getSubjectId())
.acceptableDuration(violation.getAcceptableDuration())
.actualOverloadDuration(violation.getAcceptableDuration())
.upComingOverloadDuration(violation.getAcceptableDuration())
dbraquart marked this conversation as resolved.
Show resolved Hide resolved
.limit(violation.getLimit())
.limitName(violation.getLimitName())
.value(violation.getValue())
Expand Down Expand Up @@ -198,7 +251,8 @@ public Consumer<Message<String>> consumeRun() {
LOGGER.info("Just run in {}s", TimeUnit.NANOSECONDS.toSeconds(nanoTime - startTime.getAndSet(nanoTime)));

List<LimitViolationInfos> limitViolationInfos = getLimitViolations(network, resultContext.getRunContext());
resultRepository.insert(resultContext.getResultUuid(), result, LoadFlowService.computeLoadFlowStatus(result), limitViolationInfos);
List<LimitViolationInfos> limitViolationsWithCalculatedOverload = calculateOverloadLimitViolations(limitViolationInfos, network);
resultRepository.insert(resultContext.getResultUuid(), result, LoadFlowService.computeLoadFlowStatus(result), limitViolationsWithCalculatedOverload);
long finalNanoTime = System.nanoTime();
LOGGER.info("Stored in {}s", TimeUnit.NANOSECONDS.toSeconds(finalNanoTime - startTime.getAndSet(finalNanoTime)));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="souissimai (generated)" id="1688572305291-1">
<renameColumn tableName="limit_violation"
newColumnName="actual_overload"
oldColumnName="acceptable_duration"
/>
</changeSet>
<changeSet author="souissimai (generated)" id="1700045593643-2">
<addColumn tableName="limit_violation">
<column name="up_coming_overload" type="integer"/>
</addColumn>
</changeSet>
</databaseChangeLog>
3 changes: 3 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ databaseChangeLog:
- include:
file: changesets/changelog_20231003T105819Z.xml
relativeToChangelogFile: true
- include:
file: changesets/changelog_20231115T10528Z.xml
relativeToChangelogFile: true
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gridsuite.loadflow.server.dto.LimitViolationInfos;
import org.gridsuite.loadflow.server.dto.LoadFlowParametersInfos;
import org.gridsuite.loadflow.server.dto.LoadFlowStatus;
import org.gridsuite.loadflow.server.service.LoadFlowWorkerService;
import org.gridsuite.loadflow.server.service.NotificationService;
import org.gridsuite.loadflow.server.service.ReportService;
import org.gridsuite.loadflow.server.service.UuidGeneratorService;
Expand Down Expand Up @@ -100,9 +101,11 @@ private static final class LoadFlowResultMock {
}

private static final class LimitViolationsMock {
static List<LimitViolation> limitViolations = List.of(new LimitViolation("lineId1", "lineName1", LimitViolationType.CURRENT, "limit1", 60, 200, 0.7F, 150, Branch.Side.ONE),
new LimitViolation("lineId2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 100, 0.7F, 80, Branch.Side.TWO),
new LimitViolation("genId1", "genName1", LimitViolationType.HIGH_VOLTAGE, "limit3", 120, 500, 0.7F, 370, null));
static List<LimitViolation> limitViolations = List.of(
new LimitViolation("NHV1_NHV2_1", "lineName1", LimitViolationType.CURRENT, "limit1", 60, 1500, 0.7F, 1300, Branch.Side.TWO),
new LimitViolation("NHV1_NHV2_1", "lineName1", LimitViolationType.CURRENT, "limit1", 60, 1500, 0.7F, 1000, Branch.Side.TWO),
new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, Branch.Side.ONE),
new LimitViolation("NHV1_NHV2_2", "lineName2", LimitViolationType.CURRENT, "limit2", 300, 900, 0.7F, 1000, Branch.Side.TWO));
}

@Autowired
Expand Down Expand Up @@ -141,21 +144,21 @@ private static void assertResultsEquals(LoadFlowResult result, org.gridsuite.loa
assertEquals(componentResultsDto.get(i).getSlackBusId(), componentResults.get(i).getSlackBusId());
assertEquals(componentResultsDto.get(i).getSlackBusActivePowerMismatch(), componentResults.get(i).getSlackBusActivePowerMismatch(), 0.01);
assertEquals(componentResultsDto.get(i).getDistributedActivePower(), componentResults.get(i).getDistributedActivePower(), 0.01);

}
}

private static void assertLimitViolationsEquals(List<LimitViolation> limitViolations, List<LimitViolationInfos> limitViolationsDto) {
private static void assertLimitViolationsEquals(List<LimitViolation> limitViolations, List<LimitViolationInfos> limitViolationsDto, Network network) {
assertEquals(limitViolations.size(), limitViolationsDto.size());

for (int i = 0; i < limitViolationsDto.size(); i++) {
assertEquals(limitViolationsDto.get(i).getSubjectId(), limitViolations.get(i).getSubjectId());
assertEquals(limitViolationsDto.get(i).getLimit(), limitViolations.get(i).getLimit(), 0.01);
assertEquals(limitViolationsDto.get(i).getLimitName(), limitViolations.get(i).getLimitName());
assertEquals(Optional.ofNullable(limitViolationsDto.get(i).getAcceptableDuration()), Optional.ofNullable(limitViolations.get(i).getAcceptableDuration()));
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))));
}
}

Expand All @@ -164,7 +167,7 @@ public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

// network store service mocking
network = EurostagTutorialExample1Factory.createWithMoreGenerators(new NetworkFactoryImpl());
network = EurostagTutorialExample1Factory.createWithFixedCurrentLimits(new NetworkFactoryImpl());
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_1_ID);
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID);
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_3_ID);
Expand Down Expand Up @@ -286,7 +289,7 @@ public void testGetLimitViolations() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
List<LimitViolationInfos> limitViolations = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<List<LimitViolationInfos>>() { });
assertLimitViolationsEquals(LimitViolationsMock.limitViolations, limitViolations);
assertLimitViolationsEquals(LimitViolationsMock.limitViolations, limitViolations, network);
}
}

Expand Down
Loading