From 5bb714171c0594abb648e97ed9ea635617d1a65d Mon Sep 17 00:00:00 2001 From: BOUHOURS Antoine Date: Thu, 5 Sep 2024 11:35:09 +0200 Subject: [PATCH] Add support for multithread with OpenLoadFlow in security analysis * Copy network to iidm-impl while multithread variant access is not supported in the network-store * SecurityAnalysisParameters.load() in any case to retrieve specific parameters set in the PlatformConfig (like threadCount) --- pom.xml | 11 ++++++++ .../SecurityAnalysisParametersService.java | 9 +++--- .../SecurityAnalysisWorkerService.java | 28 ++++++++++++++++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index df8ad38e..e4bfcc31 100644 --- a/pom.xml +++ b/pom.xml @@ -85,6 +85,11 @@ + + com.powsybl + powsybl-open-loadflow + 1.13.0-SNAPSHOT + @@ -195,6 +200,12 @@ micrometer-registry-prometheus runtime + + + com.powsybl + powsybl-iidm-impl + runtime + diff --git a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisParametersService.java b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisParametersService.java index 03270a42..849de6c9 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisParametersService.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisParametersService.java @@ -77,14 +77,13 @@ public SecurityAnalysisRunContext createRunContext(UUID networkUuid, String vari } public SecurityAnalysisParametersDTO toSecurityAnalysisParameters(SecurityAnalysisParametersEntity entity) { - SecurityAnalysisParameters securityAnalysisParameters; + SecurityAnalysisParameters securityAnalysisParameters = SecurityAnalysisParameters.load(); List> limitReductions = new ArrayList<>(); if (entity == null) { - securityAnalysisParameters = SecurityAnalysisParameters.load() - // the default values are overloaded - .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)); + // the default values are overloaded + 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)); } else { - securityAnalysisParameters = new SecurityAnalysisParameters().setIncreasedViolationsParameters(getIncreasedViolationsParameters(entity.getFlowProportionalThreshold(), entity.getLowVoltageProportionalThreshold(), entity.getLowVoltageAbsoluteThreshold(), entity.getHighVoltageProportionalThreshold(), entity.getHighVoltageAbsoluteThreshold())); + securityAnalysisParameters.setIncreasedViolationsParameters(getIncreasedViolationsParameters(entity.getFlowProportionalThreshold(), entity.getLowVoltageProportionalThreshold(), entity.getLowVoltageAbsoluteThreshold(), entity.getHighVoltageProportionalThreshold(), entity.getHighVoltageAbsoluteThreshold())); limitReductions = entity.toLimitReductionsValues(); } diff --git a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java index 92c473be..759dd1a6 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java @@ -16,11 +16,11 @@ import com.powsybl.iidm.criteria.duration.IntervalTemporaryDurationCriterion; import com.powsybl.iidm.criteria.duration.LimitDurationCriterion; import com.powsybl.iidm.criteria.duration.PermanentDurationCriterion; -import com.powsybl.iidm.network.LimitType; -import com.powsybl.iidm.network.Network; -import com.powsybl.iidm.network.VariantManagerConstants; +import com.powsybl.iidm.network.*; +import com.powsybl.iidm.serde.NetworkSerDe; import com.powsybl.loadflow.LoadFlowResult; import com.powsybl.network.store.client.NetworkStoreService; +import com.powsybl.network.store.client.PreloadingStrategy; import com.powsybl.security.*; import com.powsybl.security.limitreduction.LimitReduction; import com.powsybl.ws.commons.LogUtils; @@ -42,6 +42,7 @@ import java.util.Objects; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Function; @@ -92,6 +93,11 @@ public SecurityAnalysisResult run(SecurityAnalysisRunContext runContext) { } } + @Override + protected PreloadingStrategy getNetworkPreloadingStrategy() { + return PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW; + } + @Override protected String getComputationType() { return COMPUTATION_TYPE; @@ -108,6 +114,20 @@ protected CompletableFuture getCompletableFuture(Securit .toList(); List limitReductions = createLimitReductions(runContext); + Network network = runContext.getNetwork(); + // FIXME: Remove this part when multithread variant access is implemented in the network-store + if (runContext.getProvider().equals("OpenLoadFlow")) { + long startTime = System.nanoTime(); + Network originalNetwork = runContext.getNetwork(); + String originalVariant = originalNetwork.getVariantManager().getWorkingVariantId(); + originalNetwork.getVariantManager().setWorkingVariant(variantId); + + network = NetworkSerDe.copy(originalNetwork, NetworkFactory.find("Default")); + network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, variantId); + LOGGER.info("Network copied to iidm-impl in {} ms", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)); + originalNetwork.getVariantManager().setWorkingVariant(originalVariant); + } + SecurityAnalysisRunParameters runParameters = new SecurityAnalysisRunParameters() .setSecurityAnalysisParameters(runContext.getParameters().securityAnalysisParameters()) .setComputationManager(executionService.getComputationManager()) @@ -116,7 +136,7 @@ protected CompletableFuture getCompletableFuture(Securit .setReportNode(runContext.getReportNode()); return securityAnalysisRunner.runAsync( - runContext.getNetwork(), + network, variantId, n -> contingencies, runParameters)