Skip to content

Commit 7390ab1

Browse files
Get and store voltage profile (#44)
* Get and store voltage profile * Set explicit name for foreign key in bus voltage table * Add primary composite key in bus voltages table Signed-off-by: Franck LECUYER <[email protected]>
1 parent 09e4eaa commit 7390ab1

File tree

10 files changed

+126
-7
lines changed

10 files changed

+126
-7
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.voltageinit.server.dto;
8+
9+
import lombok.AllArgsConstructor;
10+
import lombok.Getter;
11+
import lombok.NoArgsConstructor;
12+
13+
/**
14+
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
15+
*/
16+
@Getter
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
public class BusVoltage {
20+
private String busId;
21+
22+
private double v;
23+
24+
private double angle;
25+
}

src/main/java/org/gridsuite/voltageinit/server/dto/VoltageInitResult.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,7 @@ public class VoltageInitResult {
3131

3232
private List<ReactiveSlack> reactiveSlacks;
3333

34+
private List<BusVoltage> busVoltages;
35+
3436
private UUID modificationsGroupUuid;
3537
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.voltageinit.server.entities;
8+
9+
import jakarta.persistence.Column;
10+
import jakarta.persistence.Embeddable;
11+
import lombok.AllArgsConstructor;
12+
import lombok.Getter;
13+
import lombok.NoArgsConstructor;
14+
15+
/**
16+
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
17+
*/
18+
@Getter
19+
@AllArgsConstructor
20+
@NoArgsConstructor
21+
@Embeddable
22+
public class BusVoltageEmbeddable {
23+
24+
@Column
25+
private String busId;
26+
27+
@Column
28+
private double v;
29+
30+
@Column
31+
private double angle;
32+
}

src/main/java/org/gridsuite/voltageinit/server/entities/VoltageInitResultEntity.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import jakarta.persistence.Column;
1717
import jakarta.persistence.ElementCollection;
1818
import jakarta.persistence.Entity;
19+
import jakarta.persistence.ForeignKey;
1920
import jakarta.persistence.Id;
21+
import jakarta.persistence.Index;
2022
import jakarta.persistence.Table;
2123

2224
import lombok.AllArgsConstructor;
@@ -49,6 +51,10 @@ public class VoltageInitResultEntity {
4951
@CollectionTable
5052
private List<ReactiveSlackEmbeddable> reactiveSlacks;
5153

54+
@ElementCollection
55+
@CollectionTable(foreignKey = @ForeignKey(name = "voltageInitResultEntity_busVoltages_fk1"), indexes = {@Index(name = "voltageInitResultEntity_busVoltages_idx1", columnList = "voltage_init_result_entity_result_uuid")})
56+
private List<BusVoltageEmbeddable> busVoltages;
57+
5258
@Column
5359
private UUID modificationsGroupUuid;
5460
}

src/main/java/org/gridsuite/voltageinit/server/repository/VoltageInitResultRepository.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
*/
77
package org.gridsuite.voltageinit.server.repository;
88

9+
import com.powsybl.iidm.network.Bus;
10+
import org.gridsuite.voltageinit.server.entities.BusVoltageEmbeddable;
911
import org.gridsuite.voltageinit.server.entities.GlobalStatusEntity;
1012
import org.gridsuite.voltageinit.server.entities.ReactiveSlackEmbeddable;
1113
import org.gridsuite.voltageinit.server.entities.VoltageInitResultEntity;
14+
import org.jgrapht.alg.util.Pair;
1215
import org.springframework.stereotype.Repository;
1316
import org.springframework.transaction.annotation.Transactional;
1417

@@ -37,12 +40,25 @@ public VoltageInitResultRepository(GlobalStatusRepository globalStatusRepository
3740
this.resultRepository = resultRepository;
3841
}
3942

40-
private static VoltageInitResultEntity toVoltageInitResultEntity(UUID resultUuid, OpenReacResult result, UUID modificationsGroupUuid) {
43+
private static VoltageInitResultEntity toVoltageInitResultEntity(UUID resultUuid, OpenReacResult result, Map<String, Bus> networkBuses, UUID modificationsGroupUuid) {
4144
Map<String, String> indicators = result.getIndicators();
4245
List<ReactiveSlackEmbeddable> reactiveSlacks = result.getReactiveSlacks().stream().map(rs ->
4346
new ReactiveSlackEmbeddable(rs.getBusId(), rs.getSlack()))
4447
.collect(Collectors.toList());
45-
return new VoltageInitResultEntity(resultUuid, ZonedDateTime.now(), indicators, reactiveSlacks, modificationsGroupUuid);
48+
Map<String, Pair<Double, Double>> voltageProfile = result.getVoltageProfile();
49+
List<BusVoltageEmbeddable> busVoltages = voltageProfile.entrySet().stream()
50+
.map(vp -> {
51+
Bus b = networkBuses.get(vp.getKey());
52+
if (b != null) {
53+
return new BusVoltageEmbeddable(vp.getKey(),
54+
vp.getValue().getFirst() * b.getVoltageLevel().getNominalV(),
55+
Math.toDegrees(vp.getValue().getSecond()));
56+
} else {
57+
return null;
58+
}
59+
}
60+
).filter(Objects::nonNull).toList();
61+
return new VoltageInitResultEntity(resultUuid, ZonedDateTime.now(), indicators, reactiveSlacks, busVoltages, modificationsGroupUuid);
4662
}
4763

4864
@Transactional
@@ -90,17 +106,17 @@ public Optional<VoltageInitResultEntity> find(UUID resultUuid) {
90106
}
91107

92108
@Transactional
93-
public void insert(UUID resultUuid, OpenReacResult result, UUID modificationsGroupUuid, String status) {
109+
public void insert(UUID resultUuid, OpenReacResult result, Map<String, Bus> networkBuses, UUID modificationsGroupUuid, String status) {
94110
Objects.requireNonNull(resultUuid);
95111
if (result != null) {
96-
resultRepository.save(toVoltageInitResultEntity(resultUuid, result, modificationsGroupUuid));
112+
resultRepository.save(toVoltageInitResultEntity(resultUuid, result, networkBuses, modificationsGroupUuid));
97113
}
98114
globalStatusRepository.save(toStatusEntity(resultUuid, status));
99115
}
100116

101117
@Transactional
102118
public void insertErrorResult(UUID resultUuid, Map<String, String> errorIndicators) {
103119
Objects.requireNonNull(resultUuid);
104-
resultRepository.save(new VoltageInitResultEntity(resultUuid, ZonedDateTime.now(), errorIndicators, List.of(), null));
120+
resultRepository.save(new VoltageInitResultEntity(resultUuid, ZonedDateTime.now(), errorIndicators, List.of(), List.of(), null));
105121
}
106122
}

src/main/java/org/gridsuite/voltageinit/server/service/VoltageInitService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import com.powsybl.network.store.client.NetworkStoreService;
1010

11+
import org.gridsuite.voltageinit.server.dto.BusVoltage;
1112
import org.gridsuite.voltageinit.server.dto.ReactiveSlack;
1213
import org.gridsuite.voltageinit.server.dto.VoltageInitResult;
1314
import org.gridsuite.voltageinit.server.dto.VoltageInitStatus;
@@ -73,7 +74,10 @@ private static VoltageInitResult fromEntity(VoltageInitResultEntity resultEntity
7374
List<ReactiveSlack> reactiveSlacks = resultEntity.getReactiveSlacks().stream()
7475
.map(slack -> new ReactiveSlack(slack.getBusId(), slack.getSlack()))
7576
.toList();
76-
return new VoltageInitResult(resultEntity.getResultUuid(), resultEntity.getWriteTimeStamp(), sortedIndicators, reactiveSlacks, resultEntity.getModificationsGroupUuid());
77+
List<BusVoltage> busVoltages = resultEntity.getBusVoltages().stream()
78+
.map(bv -> new BusVoltage(bv.getBusId(), bv.getV(), bv.getAngle()))
79+
.toList();
80+
return new VoltageInitResult(resultEntity.getResultUuid(), resultEntity.getWriteTimeStamp(), sortedIndicators, reactiveSlacks, busVoltages, resultEntity.getModificationsGroupUuid());
7781
}
7882

7983
public void deleteResult(UUID resultUuid) {

src/main/java/org/gridsuite/voltageinit/server/service/VoltageInitWorkerService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.powsybl.commons.reporter.Reporter;
1313
import com.powsybl.commons.reporter.ReporterModel;
1414
import com.powsybl.commons.reporter.TypedValue;
15+
import com.powsybl.iidm.network.Bus;
1516
import com.powsybl.iidm.network.Network;
1617
import com.powsybl.iidm.network.VariantManagerConstants;
1718
import com.powsybl.network.store.client.NetworkStoreService;
@@ -40,6 +41,7 @@
4041
import java.util.concurrent.locks.Lock;
4142
import java.util.concurrent.locks.ReentrantLock;
4243
import java.util.function.Consumer;
44+
import java.util.function.Function;
4345
import java.util.stream.Collectors;
4446

4547
import static org.gridsuite.voltageinit.server.service.NotificationService.CANCEL_MESSAGE;
@@ -211,8 +213,9 @@ public Consumer<Message<String>> consumeRun() {
211213

212214
if (openReacResult != null) { // result available
213215
UUID modificationsGroupUuid = networkModificationService.createVoltageInitModificationGroup(network, openReacResult);
216+
Map<String, Bus> networkBuses = network.getBusView().getBusStream().collect(Collectors.toMap(Bus::getId, Function.identity()));
214217
voltageInitObserver.observe("results.save", () ->
215-
resultRepository.insert(resultContext.getResultUuid(), openReacResult, modificationsGroupUuid, openReacResult.getStatus().name()));
218+
resultRepository.insert(resultContext.getResultUuid(), openReacResult, networkBuses, modificationsGroupUuid, openReacResult.getStatus().name()));
216219
LOGGER.info("Status : {}", openReacResult.getStatus());
217220
LOGGER.info("Reactive slacks : {}", openReacResult.getReactiveSlacks());
218221
LOGGER.info("Indicators : {}", openReacResult.getIndicators());
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
2+
<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">
3+
<changeSet author="lecuyerfra (generated)" id="1711115162726-1">
4+
<createTable tableName="voltage_init_result_entity_bus_voltages">
5+
<column name="voltage_init_result_entity_result_uuid" type="UUID">
6+
<constraints nullable="false" primaryKey="true" primaryKeyName="result_uuid_bus_idPK"/>
7+
</column>
8+
<column name="angle" type="FLOAT(53)"/>
9+
<column name="bus_id" type="VARCHAR(255)">
10+
<constraints nullable="false" primaryKey="true" primaryKeyName="result_uuid_bus_idPK"/>
11+
</column>
12+
<column name="v" type="FLOAT(53)"/>
13+
</createTable>
14+
</changeSet>
15+
<changeSet author="lecuyerfra (generated)" id="1711115162726-2">
16+
<createIndex indexName="voltageInitResultEntity_busVoltages_idx1" tableName="voltage_init_result_entity_bus_voltages">
17+
<column name="voltage_init_result_entity_result_uuid"/>
18+
</createIndex>
19+
</changeSet>
20+
<changeSet author="lecuyerfra (generated)" id="1711115162726-3">
21+
<addForeignKeyConstraint baseColumnNames="voltage_init_result_entity_result_uuid" baseTableName="voltage_init_result_entity_bus_voltages" constraintName="voltageInitResultEntity_busVoltages_fk1" deferrable="false" initiallyDeferred="false" referencedColumnNames="result_uuid" referencedTableName="voltage_init_result" validate="true"/>
22+
</changeSet>
23+
</databaseChangeLog>

src/main/resources/db/changelog/db.changelog-master.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ databaseChangeLog:
1515
- include:
1616
file: changesets/changelog_20231103T151738Z.xml
1717
relativeToChangelogFile: true
18+
- include:
19+
file: changesets/changelog_20240322T134547Z.xml
20+
relativeToChangelogFile: true

src/test/java/org/gridsuite/voltageinit/server/VoltageInitControllerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.powsybl.openreac.parameters.input.OpenReacParameters;
2828
import com.powsybl.openreac.parameters.output.OpenReacResult;
2929
import com.powsybl.openreac.parameters.output.OpenReacStatus;
30+
import com.powsybl.openreac.parameters.output.ReactiveSlackOutput;
3031
import lombok.SneakyThrows;
3132
import okhttp3.HttpUrl;
3233
import okhttp3.mockwebserver.Dispatcher;
@@ -163,6 +164,10 @@ private OpenReacResult buildOpenReacResult() {
163164
Map<String, Pair<Double, Double>> voltageProfile = openReacAmplIOFiles.getVoltageProfileOutput().getVoltageProfile();
164165
voltageProfile.put("NHV2_NLOAD_busId1", Pair.of(100., 100.));
165166
voltageProfile.put("SHUNT_1_busId1", Pair.of(100., 100.));
167+
voltageProfile.put("VLHV1_0", Pair.of(100., 100.));
168+
voltageProfile.put("VLGEN_0", Pair.of(100., 100.));
169+
170+
openReacAmplIOFiles.getReactiveSlackOutput().getSlacks().add(new ReactiveSlackOutput.ReactiveSlack("NGEN", "VLGEN", 10.));
166171

167172
openReacResult = new OpenReacResult(OpenReacStatus.OK, openReacAmplIOFiles, INDICATORS);
168173
return openReacResult;

0 commit comments

Comments
 (0)