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

AC security analysis cancellation #491

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager

@Override
public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager computationManager, String workingVariantId, LoadFlowParameters parameters, Reporter reporter) {
Objects.requireNonNull(network);
Objects.requireNonNull(computationManager);
Objects.requireNonNull(workingVariantId);
Objects.requireNonNull(parameters);
Objects.requireNonNull(reporter);

LOGGER.info("Version: {}", new PowsyblOpenLoadFlowVersion());

Expand All @@ -228,7 +231,7 @@ public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager
LOGGER.info(Markers.PERFORMANCE_MARKER, "Load flow ran in {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));

return result;
});
}, computationManager.getExecutor());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.openloadflow.sa;

import com.powsybl.computation.CompletableFutureTask;
import com.powsybl.computation.ComputationManager;
import com.powsybl.contingency.ContingenciesProvider;
import com.powsybl.iidm.network.Branch;
Expand Down Expand Up @@ -80,15 +81,15 @@ public CompletableFuture<SecurityAnalysisReport> run(String workingVariantId, Se
Objects.requireNonNull(workingVariantId);
Objects.requireNonNull(securityAnalysisParameters);
Objects.requireNonNull(contingenciesProvider);
return CompletableFuture.supplyAsync(() -> {
return CompletableFutureTask.runAsync(() -> {
String oldWorkingVariantId = network.getVariantManager().getWorkingVariantId();
network.getVariantManager().setWorkingVariant(workingVariantId);
try {
return runSync(workingVariantId, securityAnalysisParameters, contingenciesProvider, computationManager);
} finally {
network.getVariantManager().setWorkingVariant(oldWorkingVariantId);
}
});
}, computationManager.getExecutor());
}

abstract SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ SecurityAnalysisReport runSync(String workingVariantId, SecurityAnalysisParamete
SecurityAnalysisResult result = runSimulations(largestNetwork, propagatedContingencies, acParameters, securityAnalysisParameters);

stopwatch.stop();
LOGGER.info("Security analysis done in {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
LOGGER.info("Security analysis {} in {} ms", Thread.currentThread().isInterrupted() ? "cancelled" : "done",
stopwatch.elapsed(TimeUnit.MILLISECONDS));

return new SecurityAnalysisReport(result);
}
Expand Down Expand Up @@ -136,7 +137,7 @@ private SecurityAnalysisResult runSimulations(LfNetwork network, List<Propagated
// start a simulation for each of the contingency
Iterator<PropagatedContingency> contingencyIt = propagatedContingencies.iterator();
GraphDecrementalConnectivity<LfBus> connectivity = network.getConnectivity();
while (contingencyIt.hasNext()) {
while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) {
PropagatedContingency propagatedContingency = contingencyIt.next();
propagatedContingency.toLfContingency(network, connectivity, true)
.ifPresent(lfContingency -> { // only process contingencies that impact the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.powsybl.security.monitor.StateMonitor;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
Expand All @@ -47,13 +48,25 @@ public CompletableFuture<SecurityAnalysisReport> run(Network network, String wor
LimitViolationFilter limitViolationFilter, ComputationManager computationManager,
SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider,
List<SecurityAnalysisInterceptor> interceptors, List<StateMonitor> stateMonitors) {
Objects.requireNonNull(network);
Objects.requireNonNull(workingVariantId);
Objects.requireNonNull(limitViolationDetector);
Objects.requireNonNull(limitViolationFilter);
Objects.requireNonNull(computationManager);
Objects.requireNonNull(securityAnalysisParameters);
Objects.requireNonNull(contingenciesProvider);
Objects.requireNonNull(interceptors);
Objects.requireNonNull(stateMonitors);

AbstractSecurityAnalysis securityAnalysis;
if (securityAnalysisParameters.getLoadFlowParameters().isDc()) {
securityAnalysis = new DcSecurityAnalysis(network, limitViolationDetector, limitViolationFilter, matrixFactory, connectivityFactory, stateMonitors);
} else {
securityAnalysis = new AcSecurityAnalysis(network, limitViolationDetector, limitViolationFilter, matrixFactory, connectivityFactory, stateMonitors);
}

interceptors.forEach(securityAnalysis::addInterceptor);

return securityAnalysis.run(workingVariantId, securityAnalysisParameters, contingenciesProvider, computationManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public CompletableFuture<Void> run(Network network,
Objects.requireNonNull(sensitivityAnalysisParameters);
Objects.requireNonNull(factorReader);
Objects.requireNonNull(valueWriter);
Objects.requireNonNull(computationManager);
Objects.requireNonNull(reporter);

return CompletableFuture.runAsync(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.openloadflow.ac;

import com.powsybl.commons.reporter.ReporterModel;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.SlackTerminal;
import com.powsybl.iidm.network.extensions.SlackTerminalAdder;
Expand Down Expand Up @@ -273,7 +274,7 @@ void noGeneratorTest() {
network.getGenerator("GEN").getTerminal().disconnect();

ReporterModel reporter = new ReporterModel("unitTest", "");
LoadFlowResult result = loadFlowRunner.run(network, VariantManagerConstants.INITIAL_VARIANT_ID, null, parameters, reporter);
LoadFlowResult result = loadFlowRunner.run(network, VariantManagerConstants.INITIAL_VARIANT_ID, LocalComputationManager.getDefault(), parameters, reporter);
assertFalse(result.isOk());
assertEquals(1, result.getComponentResults().size());
assertEquals(LoadFlowResult.ComponentResult.Status.FAILED, result.getComponentResults().get(0).getStatus());
Expand Down