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 support for multithread with OpenLoadFlow in security analysis #140

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-open-loadflow</artifactId>
<version>1.13.0</version>
</dependency>

<!-- imports -->
<dependency>
Expand Down Expand Up @@ -195,6 +200,12 @@
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
<!-- FIXME: Remove this runtime dependency when variant multithread access is implemented in the network-store -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-iidm-impl</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ public SecurityAnalysisRunContext createRunContext(UUID networkUuid, String vari
}

public SecurityAnalysisParametersDTO toSecurityAnalysisParameters(SecurityAnalysisParametersEntity entity) {
SecurityAnalysisParameters securityAnalysisParameters;
SecurityAnalysisParameters securityAnalysisParameters = SecurityAnalysisParameters.load();
List<List<Double>> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -108,6 +117,22 @@ protected CompletableFuture<SecurityAnalysisResult> getCompletableFuture(Securit
.toList();
List<LimitReduction> 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())
Expand All @@ -116,7 +141,7 @@ protected CompletableFuture<SecurityAnalysisResult> getCompletableFuture(Securit
.setReportNode(runContext.getReportNode());

return securityAnalysisRunner.runAsync(
runContext.getNetwork(),
network,
variantId,
n -> contingencies,
runParameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ public 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);
Expand Down
Loading