diff --git a/pom.xml b/pom.xml index e7bf4ee1..4b598fcd 100644 --- a/pom.xml +++ b/pom.xml @@ -207,6 +207,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..0e100078 100644 --- a/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java +++ b/src/main/java/org/gridsuite/securityanalysis/server/service/SecurityAnalysisWorkerService.java @@ -18,9 +18,12 @@ import com.powsybl.iidm.criteria.duration.PermanentDurationCriterion; import com.powsybl.iidm.network.LimitType; import com.powsybl.iidm.network.Network; +import com.powsybl.iidm.network.NetworkFactory; import com.powsybl.iidm.network.VariantManagerConstants; +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 +45,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 +96,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 +117,22 @@ 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")); + if (!variantId.equals(VariantManagerConstants.INITIAL_VARIANT_ID)) { + 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 +141,7 @@ protected CompletableFuture getCompletableFuture(Securit .setReportNode(runContext.getReportNode()); return securityAnalysisRunner.runAsync( - runContext.getNetwork(), + network, variantId, n -> contingencies, runParameters) diff --git a/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisControllerTest.java b/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisControllerTest.java index 93b28428..258ae503 100644 --- a/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisControllerTest.java +++ b/src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisControllerTest.java @@ -166,9 +166,9 @@ void setUp() throws Exception { network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_2_ID); network.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_3_ID); - given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.COLLECTION)).willReturn(network); + given(networkStoreService.getNetwork(NETWORK_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).willReturn(network); - when(networkStoreService.getNetwork(NETWORK_STOP_UUID, PreloadingStrategy.COLLECTION)).thenAnswer(invocation -> { + when(networkStoreService.getNetwork(NETWORK_STOP_UUID, PreloadingStrategy.ALL_COLLECTIONS_NEEDED_FOR_BUS_VIEW)).thenAnswer(invocation -> { //Needed so the stop call doesn't arrive too late Network network1 = new NetworkFactoryImpl().createNetwork("other", "test"); network1.getVariantManager().cloneVariant(VariantManagerConstants.INITIAL_VARIANT_ID, VARIANT_TO_STOP_ID);