Skip to content

Commit

Permalink
Adapt code to new SA API
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Dupuy <[email protected]>
  • Loading branch information
flo-dup committed May 19, 2021
1 parent 1741b32 commit f80c3c2
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class OpenSecurityAnalysis implements SecurityAnalysis {
public class OpenSecurityAnalysis {

private static final Logger LOGGER = LoggerFactory.getLogger(OpenSecurityAnalysis.class);

Expand All @@ -74,31 +74,29 @@ public OpenSecurityAnalysis(Network network, LimitViolationDetector detector, Li
this.connectivityProvider = Objects.requireNonNull(connectivityProvider);
}

@Override
public void addInterceptor(SecurityAnalysisInterceptor interceptor) {
interceptors.add(Objects.requireNonNull(interceptor));
}

@Override
public boolean removeInterceptor(SecurityAnalysisInterceptor interceptor) {
return interceptors.remove(Objects.requireNonNull(interceptor));
}

@Override
public CompletableFuture<SecurityAnalysisResult> run(String workingVariantId, SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider) {
public CompletableFuture<SecurityAnalysisReport> run(String workingVariantId, SecurityAnalysisParameters securityAnalysisParameters,
ContingenciesProvider contingenciesProvider) {
Objects.requireNonNull(workingVariantId);
Objects.requireNonNull(securityAnalysisParameters);
Objects.requireNonNull(contingenciesProvider);
return CompletableFuture.supplyAsync(() -> {
String oldWorkingVariantId = network.getVariantManager().getWorkingVariantId();
network.getVariantManager().setWorkingVariant(workingVariantId);
SecurityAnalysisResult result = runSync(securityAnalysisParameters, contingenciesProvider);
SecurityAnalysisReport result = runSync(securityAnalysisParameters, contingenciesProvider);
network.getVariantManager().setWorkingVariant(oldWorkingVariantId);
return result;
});
}

SecurityAnalysisResult runSync(SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider) {
SecurityAnalysisReport runSync(SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider) {
Stopwatch stopwatch = Stopwatch.createStarted();

LoadFlowParameters lfParameters = securityAnalysisParameters.getLoadFlowParameters();
Expand Down Expand Up @@ -132,7 +130,7 @@ SecurityAnalysisResult runSync(SecurityAnalysisParameters securityAnalysisParame
stopwatch.stop();
LOGGER.info("Security analysis done in {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));

return result;
return new SecurityAnalysisReport(result);
}

List<LfNetwork> createNetworks(Set<Switch> allSwitchesToOpen, AcLoadFlowParameters acParameters) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2021, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.openloadflow.sa;

import com.google.auto.service.AutoService;
import com.powsybl.computation.ComputationManager;
import com.powsybl.contingency.ContingenciesProvider;
import com.powsybl.iidm.network.Network;
import com.powsybl.math.matrix.DenseMatrixFactory;
import com.powsybl.math.matrix.MatrixFactory;
import com.powsybl.math.matrix.SparseMatrixFactory;
import com.powsybl.openloadflow.graph.EvenShiloachGraphDecrementalConnectivity;
import com.powsybl.openloadflow.graph.GraphDecrementalConnectivity;
import com.powsybl.openloadflow.graph.NaiveGraphDecrementalConnectivity;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.util.PowsyblOpenLoadFlowVersion;
import com.powsybl.security.*;
import com.powsybl.security.interceptors.SecurityAnalysisInterceptor;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

/**
* @author Florian Dupuy <florian.dupuy at rte-france.com>
*/
@AutoService(SecurityAnalysisProvider.class)
public class OpenSecurityAnalysisProvider implements SecurityAnalysisProvider {

final MatrixFactory matrixFactory;

final Supplier<GraphDecrementalConnectivity<LfBus>> connectivityProvider;

public OpenSecurityAnalysisProvider(MatrixFactory matrixFactory, Supplier<GraphDecrementalConnectivity<LfBus>> connectivityProvider) {
this.matrixFactory = matrixFactory;
this.connectivityProvider = connectivityProvider;
}

public OpenSecurityAnalysisProvider() {
this(new DenseMatrixFactory(), () -> new NaiveGraphDecrementalConnectivity<>(LfBus::getNum));
}

@Override
public CompletableFuture<SecurityAnalysisReport> run(Network network, String workingVariantId, LimitViolationDetector limitViolationDetector,
LimitViolationFilter limitViolationFilter, ComputationManager computationManager,
SecurityAnalysisParameters securityAnalysisParameters, ContingenciesProvider contingenciesProvider,
List<SecurityAnalysisInterceptor> interceptors) {
OpenSecurityAnalysis osa = new OpenSecurityAnalysis(network, limitViolationDetector, limitViolationFilter,
new SparseMatrixFactory(), EvenShiloachGraphDecrementalConnectivity::new);
interceptors.forEach(osa::addInterceptor);
return osa.run(workingVariantId, securityAnalysisParameters, contingenciesProvider);
}

@Override
public String getName() {
return "OpenSecurityAnalysis";
}

@Override
public String getVersion() {
return new PowsyblOpenLoadFlowVersion().toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
import com.powsybl.contingency.Contingency;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.test.FourSubstationsNodeBreakerFactory;
import com.powsybl.math.matrix.DenseMatrixFactory;
import com.powsybl.openloadflow.graph.NaiveGraphDecrementalConnectivity;
import com.powsybl.openloadflow.network.LfBus;
import com.powsybl.openloadflow.network.LfNetwork;
import com.powsybl.openloadflow.network.MostMeshedSlackBusSelector;
import com.powsybl.openloadflow.util.LfContingency;
import com.powsybl.openloadflow.util.PropagatedContingency;
import com.powsybl.security.LimitViolationFilter;
import com.powsybl.security.detectors.DefaultLimitViolationDetector;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -54,7 +59,8 @@ void test() throws IOException {
List<LfNetwork> lfNetworks = LfNetwork.load(network, new MostMeshedSlackBusSelector());
assertEquals(2, lfNetworks.size());

OpenSecurityAnalysis sa = new OpenSecurityAnalysisFactory().create(network, null, 0);
OpenSecurityAnalysis sa = new OpenSecurityAnalysis(network, new DefaultLimitViolationDetector(),
new LimitViolationFilter(), new DenseMatrixFactory(), () -> new NaiveGraphDecrementalConnectivity<>(LfBus::getNum));

String branchId = "LINE_S3S4";
Contingency contingency = new Contingency(branchId, new BranchContingency(branchId));
Expand Down
Loading

0 comments on commit f80c3c2

Please sign in to comment.