Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@gmail.com>
  • Loading branch information
geofjamg committed Jun 1, 2022
1 parent 37a9594 commit 6f8c934
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@ public Set<LfGenerator> getParticipatingGeneratorsToBeRemoved() {
}

public Set<LfBus> getLoadAndGeneratorBuses() {
Set<LfBus> affectedBuses = new HashSet<>();
Set<LfBus> buses = new HashSet<>();
for (var e : busesLoadShift.entrySet()) {
LfBus bus = e.getKey();
if (bus != null) {
affectedBuses.add(bus);
buses.add(bus);
}
}
for (LfGenerator generator : generators) {
LfBus bus = generator.getBus();
if (bus != null) {
affectedBuses.add(bus);
buses.add(bus);
}
}
return affectedBuses;
return buses;
}

public void writeJson(Writer writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,11 @@ public void analyse(Network network, List<PropagatedContingency> contingencies,
Set<LfBus> slackConnectedComponent;
if (lfContingency.getDisabledBuses().isEmpty()) {
// contingency not breaking connectivity
LOGGER.info("Contingency {} without loss of connectivity", lfContingency.getId());
LOGGER.debug("Contingency {} without loss of connectivity", lfContingency.getId());
slackConnectedComponent = new HashSet<>(lfNetwork.getBuses());
} else {
// contingency breaking connectivity
LOGGER.info("Contingency {} with loss of connectivity", lfContingency.getId());
LOGGER.debug("Contingency {} with loss of connectivity", lfContingency.getId());
// we check if factors are still in the main component
slackConnectedComponent = new HashSet<>(lfNetwork.getBuses()).stream().filter(Predicate.not(lfContingency.getDisabledBuses()::contains)).collect(Collectors.toSet());
setPredefinedResults(contingencyFactors, slackConnectedComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,22 +565,22 @@ public void calculateContingencySensitivityValues(PropagatedContingency continge
// if we have a contingency including the loss of a DC line or a generator or a load
// save base state for later restoration after each contingency
NetworkState networkState = NetworkState.save(lfNetwork);
Optional<LfContingency> lfContingency = contingency.toLfContingency(lfNetwork, true);
LfContingency lfContingency = contingency.toLfContingency(lfNetwork, true).orElse(null);
DenseMatrix newFactorStates = factorStates;
List<ParticipatingElement> newParticipatingElements = participatingElements;
boolean participatingElementsChanged = false;
boolean rhsChanged = false;
if (lfContingency.isPresent()) {
lfContingency.get().apply(lfParameters.getBalanceType());
if (lfContingency != null) {
lfContingency.apply(lfParameters.getBalanceType());
participatingElementsChanged = (isDistributedSlackOnGenerators(lfParameters) && !contingency.getGeneratorIdsToLose().isEmpty())
|| (isDistributedSlackOnLoads(lfParameters) && !contingency.getLoadIdsToShift().isEmpty());
if (hasMultiVariables) {
Set<LfBus> affectedBuses = lfContingency.get().getLoadAndGeneratorBuses();
Set<LfBus> affectedBuses = lfContingency.getLoadAndGeneratorBuses();
rhsChanged = rescaleGlsk(factorGroups, affectedBuses);
}
if (participatingElementsChanged) {
if (isDistributedSlackOnGenerators(lfParameters)) {
Set<LfGenerator> participatingGeneratorsToRemove = lfContingency.get().getParticipatingGeneratorsToBeRemoved();
Set<LfGenerator> participatingGeneratorsToRemove = lfContingency.getParticipatingGeneratorsToBeRemoved();
// deep copy of participatingElements, removing the participating LfGeneratorImpl whose targetP has been set to 0
Set<LfGenerator> finalParticipatingGeneratorsToRemove = participatingGeneratorsToRemove;
newParticipatingElements = participatingElements.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
import com.powsybl.iidm.network.extensions.LoadDetailAdder;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.iidm.network.test.FourSubstationsNodeBreakerFactory;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.math.matrix.DenseMatrixFactory;
import com.powsybl.math.matrix.MatrixFactory;
import com.powsybl.openloadflow.OpenLoadFlowParameters;
import com.powsybl.openloadflow.OpenLoadFlowProvider;
import com.powsybl.openloadflow.graph.EvenShiloachGraphDecrementalConnectivityFactory;
import com.powsybl.openloadflow.graph.GraphDecrementalConnectivityFactory;
import com.powsybl.openloadflow.network.*;
import com.powsybl.openloadflow.util.LoadFlowAssert;
import com.powsybl.security.*;
import com.powsybl.security.detectors.DefaultLimitViolationDetector;
import com.powsybl.security.monitor.StateMonitor;
import com.powsybl.security.results.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

Expand All @@ -44,6 +48,22 @@
*/
class OpenSecurityAnalysisTest {

private ComputationManager computationManager;

private OpenSecurityAnalysisProvider securityAnalysisProvider;

private OpenLoadFlowProvider loadFlowProvider;

@BeforeEach
void setUp() {
computationManager = Mockito.mock(ComputationManager.class);
Mockito.when(computationManager.getExecutor()).thenReturn(ForkJoinPool.commonPool());
MatrixFactory matrixFactory = new DenseMatrixFactory();
GraphDecrementalConnectivityFactory<LfBus, LfBranch> connectivityFactory = new EvenShiloachGraphDecrementalConnectivityFactory<>();
securityAnalysisProvider = new OpenSecurityAnalysisProvider(matrixFactory, connectivityFactory);
loadFlowProvider = new OpenLoadFlowProvider(matrixFactory, connectivityFactory);
}

private static Network createNodeBreakerNetwork() {
Network network = NodeBreakerNetworkFactory.create();

Expand Down Expand Up @@ -83,23 +103,25 @@ private static Network createNodeBreakerNetwork() {
return network;
}

private LoadFlowResult runLoadFlow(Network network, LoadFlowParameters parameters) {
return loadFlowProvider.run(network, computationManager, network.getVariantManager().getWorkingVariantId(), parameters)
.join();
}

/**
* Runs a security analysis with default parameters + most meshed slack bus selection
*/
private static SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, List<StateMonitor> monitors,
LoadFlowParameters lfParameters) {
private SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, List<StateMonitor> monitors,
LoadFlowParameters lfParameters) {
SecurityAnalysisParameters securityAnalysisParameters = new SecurityAnalysisParameters();
securityAnalysisParameters.setLoadFlowParameters(lfParameters);
return runSecurityAnalysis(network, contingencies, monitors, securityAnalysisParameters);
}

private static SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, List<StateMonitor> monitors,
SecurityAnalysisParameters saParameters) {
private SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, List<StateMonitor> monitors,
SecurityAnalysisParameters saParameters) {
ContingenciesProvider provider = n -> contingencies;
var saProvider = new OpenSecurityAnalysisProvider(new DenseMatrixFactory(), new EvenShiloachGraphDecrementalConnectivityFactory<>());
var computationManager = Mockito.mock(ComputationManager.class);
Mockito.when(computationManager.getExecutor()).thenReturn(ForkJoinPool.commonPool());
SecurityAnalysisReport report = saProvider.run(network,
SecurityAnalysisReport report = securityAnalysisProvider.run(network,
network.getVariantManager().getWorkingVariantId(),
new DefaultLimitViolationDetector(),
new LimitViolationFilter(),
Expand All @@ -112,19 +134,19 @@ private static SecurityAnalysisResult runSecurityAnalysis(Network network, List<
return report.getResult();
}

private static SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, List<StateMonitor> monitors) {
private SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, List<StateMonitor> monitors) {
return runSecurityAnalysis(network, contingencies, monitors, new LoadFlowParameters());
}

private static SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, LoadFlowParameters loadFlowParameters) {
private SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies, LoadFlowParameters loadFlowParameters) {
return runSecurityAnalysis(network, contingencies, Collections.emptyList(), loadFlowParameters);
}

private static SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies) {
private SecurityAnalysisResult runSecurityAnalysis(Network network, List<Contingency> contingencies) {
return runSecurityAnalysis(network, contingencies, Collections.emptyList());
}

private static SecurityAnalysisResult runSecurityAnalysis(Network network) {
private SecurityAnalysisResult runSecurityAnalysis(Network network) {
return runSecurityAnalysis(network, Collections.emptyList(), Collections.emptyList());
}

Expand Down Expand Up @@ -1371,7 +1393,7 @@ void testContingencyOnHvdcLcc() {

network.getHvdcLine("hvdc34").getConverterStation1().getTerminal().disconnect();
network.getHvdcLine("hvdc34").getConverterStation2().getTerminal().disconnect();
LoadFlow.run(network, parameters);
runLoadFlow(network, parameters);

PreContingencyResult preContingencyResult = result.getPreContingencyResult();
assertEquals(1.360, preContingencyResult.getPreContingencyBranchResult("l12").getP1(), LoadFlowAssert.DELTA_POWER);
Expand Down Expand Up @@ -1406,7 +1428,7 @@ void testContingencyOnHvdcVsc() {

network.getHvdcLine("hvdc34").getConverterStation1().getTerminal().disconnect();
network.getHvdcLine("hvdc34").getConverterStation2().getTerminal().disconnect();
LoadFlow.run(network, parameters);
runLoadFlow(network, parameters);

PreContingencyResult preContingencyResult = result.getPreContingencyResult();
assertEquals(1.25, preContingencyResult.getPreContingencyBranchResult("l12").getP1(), LoadFlowAssert.DELTA_POWER);
Expand Down

0 comments on commit 6f8c934

Please sign in to comment.