Skip to content

Commit 67078bb

Browse files
authored
Add support for multithread with OpenLoadFlow in security analysis (#140)
1 parent d5f8bbe commit 67078bb

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@
207207
<artifactId>micrometer-registry-prometheus</artifactId>
208208
<scope>runtime</scope>
209209
</dependency>
210+
<!-- FIXME: Remove this runtime dependency when variant multithread access is implemented in the network-store -->
211+
<dependency>
212+
<groupId>com.powsybl</groupId>
213+
<artifactId>powsybl-iidm-impl</artifactId>
214+
<scope>runtime</scope>
215+
</dependency>
210216

211217
<!-- Test dependencies -->
212218
<dependency>

src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisParametersService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ public SecurityAnalysisRunContext createRunContext(UUID networkUuid, String vari
7777
}
7878

7979
public SecurityAnalysisParametersDTO toSecurityAnalysisParameters(SecurityAnalysisParametersEntity entity) {
80-
SecurityAnalysisParameters securityAnalysisParameters;
80+
SecurityAnalysisParameters securityAnalysisParameters = SecurityAnalysisParameters.load();
8181
List<List<Double>> limitReductions = new ArrayList<>();
8282
if (entity == null) {
83-
securityAnalysisParameters = SecurityAnalysisParameters.load()
84-
// the default values are overloaded
85-
.setIncreasedViolationsParameters(getIncreasedViolationsParameters(DEFAULT_FLOW_PROPORTIONAL_THRESHOLD, DEFAULT_LOW_VOLTAGE_PROPORTIONAL_THRESHOLD, DEFAULT_LOW_VOLTAGE_ABSOLUTE_THRESHOLD, DEFAULT_HIGH_VOLTAGE_PROPORTIONAL_THRESHOLD, DEFAULT_HIGH_VOLTAGE_ABSOLUTE_THRESHOLD));
83+
// the default values are overloaded
84+
securityAnalysisParameters.setIncreasedViolationsParameters(getIncreasedViolationsParameters(DEFAULT_FLOW_PROPORTIONAL_THRESHOLD, DEFAULT_LOW_VOLTAGE_PROPORTIONAL_THRESHOLD, DEFAULT_LOW_VOLTAGE_ABSOLUTE_THRESHOLD, DEFAULT_HIGH_VOLTAGE_PROPORTIONAL_THRESHOLD, DEFAULT_HIGH_VOLTAGE_ABSOLUTE_THRESHOLD));
8685
} else {
87-
securityAnalysisParameters = new SecurityAnalysisParameters().setIncreasedViolationsParameters(getIncreasedViolationsParameters(entity.getFlowProportionalThreshold(), entity.getLowVoltageProportionalThreshold(), entity.getLowVoltageAbsoluteThreshold(), entity.getHighVoltageProportionalThreshold(), entity.getHighVoltageAbsoluteThreshold()));
86+
securityAnalysisParameters.setIncreasedViolationsParameters(getIncreasedViolationsParameters(entity.getFlowProportionalThreshold(), entity.getLowVoltageProportionalThreshold(), entity.getLowVoltageAbsoluteThreshold(), entity.getHighVoltageProportionalThreshold(), entity.getHighVoltageAbsoluteThreshold()));
8887
limitReductions = entity.toLimitReductionsValues();
8988
}
9089

src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
import com.powsybl.iidm.criteria.duration.PermanentDurationCriterion;
1919
import com.powsybl.iidm.network.LimitType;
2020
import com.powsybl.iidm.network.Network;
21+
import com.powsybl.iidm.network.NetworkFactory;
2122
import com.powsybl.iidm.network.VariantManagerConstants;
23+
import com.powsybl.iidm.serde.NetworkSerDe;
2224
import com.powsybl.loadflow.LoadFlowResult;
2325
import com.powsybl.network.store.client.NetworkStoreService;
26+
import com.powsybl.network.store.client.PreloadingStrategy;
2427
import com.powsybl.security.*;
2528
import com.powsybl.security.limitreduction.LimitReduction;
2629
import com.powsybl.ws.commons.LogUtils;
@@ -42,6 +45,7 @@
4245
import java.util.Objects;
4346
import java.util.UUID;
4447
import java.util.concurrent.CompletableFuture;
48+
import java.util.concurrent.TimeUnit;
4549
import java.util.concurrent.atomic.AtomicReference;
4650
import java.util.function.Consumer;
4751
import java.util.function.Function;
@@ -92,6 +96,11 @@ public SecurityAnalysisResult run(SecurityAnalysisRunContext runContext) {
9296
}
9397
}
9498

99+
@Override
100+
protected PreloadingStrategy getNetworkPreloadingStrategy() {
101+
return PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW;
102+
}
103+
95104
@Override
96105
protected String getComputationType() {
97106
return COMPUTATION_TYPE;
@@ -108,6 +117,22 @@ protected CompletableFuture<SecurityAnalysisResult> getCompletableFuture(Securit
108117
.toList();
109118
List<LimitReduction> limitReductions = createLimitReductions(runContext);
110119

120+
Network network = runContext.getNetwork();
121+
// FIXME: Remove this part when multithread variant access is implemented in the network-store
122+
if (runContext.getProvider().equals("OpenLoadFlow")) {
123+
long startTime = System.nanoTime();
124+
Network originalNetwork = runContext.getNetwork();
125+
String originalVariant = originalNetwork.getVariantManager().getWorkingVariantId();
126+
originalNetwork.getVariantManager().setWorkingVariant(variantId);
127+
128+
network = NetworkSerDe.copy(originalNetwork, NetworkFactory.find("Default"));
129+
if (!variantId.equals(VariantManagerConstants.INITIAL_VARIANT_ID)) {
130+
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, variantId);
131+
}
132+
LOGGER.info("Network copied to iidm-impl in {} ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime));
133+
originalNetwork.getVariantManager().setWorkingVariant(originalVariant);
134+
}
135+
111136
SecurityAnalysisRunParameters runParameters = new SecurityAnalysisRunParameters()
112137
.setSecurityAnalysisParameters(runContext.getParameters().securityAnalysisParameters())
113138
.setComputationManager(executionService.getComputationManager())
@@ -116,7 +141,7 @@ protected CompletableFuture<SecurityAnalysisResult> getCompletableFuture(Securit
116141
.setReportNode(runContext.getReportNode());
117142

118143
return securityAnalysisRunner.runAsync(
119-
runContext.getNetwork(),
144+
network,
120145
variantId,
121146
n -> contingencies,
122147
runParameters)

src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisControllerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ void setUp() throws Exception {
166166
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID);
167167
network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_3_ID);
168168

169-
given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.COLLECTION)).willReturn(network);
169+
given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willReturn(network);
170170

171-
when(networkStoreService.getNetwork(NETWORK_STOP_UUID, PreloadingStrategy.COLLECTION)).thenAnswer(invocation -> {
171+
when(networkStoreService.getNetwork(NETWORK_STOP_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).thenAnswer(invocation -> {
172172
//Needed so the stop call doesn't arrive too late
173173
Network network1 = new NetworkFactoryImpl().createNetwork("other", "test");
174174
network1.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_TO_STOP_ID);

0 commit comments

Comments
 (0)