Skip to content

Commit

Permalink
Remove deprecated isOk() for LF results, force connected component to…
Browse files Browse the repository at this point in the history
… main (#167)

* Loadflow for flow decomposition is only launched on the main island
* Remove methods isOk() for LoadFlowResult and use the updated version isFullyConverged()

Signed-off-by: Caio Luke <[email protected]>
  • Loading branch information
caioluke authored Oct 15, 2024
1 parent 7deeced commit 77dd7b7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.sensitivity.SensitivityAnalysis;
import com.powsybl.sensitivity.SensitivityVariableType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Map;
Expand All @@ -28,8 +30,10 @@
*/
public class FlowDecompositionComputer {

private static final Logger LOGGER = LoggerFactory.getLogger(FlowDecompositionComputer.class);
static final String DEFAULT_LOAD_FLOW_PROVIDER = "OpenLoadFlow";
static final String DEFAULT_SENSITIVITY_ANALYSIS_PROVIDER = "OpenLoadFlow";
public static final LoadFlowParameters.ConnectedComponentMode MAIN_CONNECTED_COMPONENT = LoadFlowParameters.ConnectedComponentMode.MAIN;
private final LoadFlowParameters loadFlowParameters;
private final FlowDecompositionParameters parameters;
private final LoadFlowRunningService loadFlowRunningService;
Expand All @@ -46,7 +50,12 @@ public FlowDecompositionComputer(FlowDecompositionParameters flowDecompositionPa
LoadFlowParameters loadFlowParameters,
String loadFlowProvider, String sensitivityAnalysisProvider) {
this.parameters = flowDecompositionParameters;
this.loadFlowParameters = loadFlowParameters;
this.loadFlowParameters = loadFlowParameters.copy();
if (!MAIN_CONNECTED_COMPONENT.equals(this.loadFlowParameters.getConnectedComponentMode())) {
LOGGER.warn("Flow decomposition is currently available only on the main synchronous component. Changing connected component mode from {} to MAIN.",
this.loadFlowParameters.getConnectedComponentMode());
this.loadFlowParameters.setConnectedComponentMode(MAIN_CONNECTED_COMPONENT);
}
this.loadFlowRunningService = new LoadFlowRunningService(LoadFlow.find(loadFlowProvider));
this.sensitivityAnalysisRunner = SensitivityAnalysis.find(sensitivityAnalysisProvider);
this.lossesCompensator = parameters.isLossesCompensationEnabled() ? new LossesCompensator(parameters) : null;
Expand Down Expand Up @@ -273,4 +282,8 @@ private void computePstFlows(Network network,
SparseMatrixWithIndexesCSC pstFlowMatrix = pstFlowComputer.run(network, networkMatrixIndexes, psdfMatrix);
flowDecompositionResultBuilder.savePstFlowMatrix(pstFlowMatrix);
}

protected LoadFlowParameters getLoadFlowParameters() {
return this.loadFlowParameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public interface FlowDecompositionObserver {
/**
* Called when the PTDF matrix is computed (for base case or contingency)
*
* @param pdtfMatrix the matrix of ptdf indexed by (line, node)
* @param ptdfMatrix the matrix of ptdf indexed by (line, node)
*/
void computedPtdfMatrix(Map<String, Map<String, Double>> pdtfMatrix);
void computedPtdfMatrix(Map<String, Map<String, Double>> ptdfMatrix);

/**
* Called when the PSDF matrix is computed (for base case or contingency)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ class LoadFlowRunningService {
Result runAcLoadflow(Network network, LoadFlowParameters loadFlowParameters, boolean isDcFallbackEnabledAfterAcDivergence) {
LoadFlowParameters acEnforcedParameters = enforceAcLoadFlowCalculation(loadFlowParameters);
LoadFlowResult acLoadFlowResult = runner.run(network, acEnforcedParameters);
if (!acLoadFlowResult.isOk() && isDcFallbackEnabledAfterAcDivergence) {
if (!acLoadFlowResult.isFullyConverged() && isDcFallbackEnabledAfterAcDivergence) {
LOGGER.warn("AC loadflow divergence. Running DC loadflow as fallback procedure.");
return runDcLoadflow(network, loadFlowParameters)
.setFallbackHasBeenActivated(FALLBACK_HAS_BEEN_ACTIVATED);
}
if (!acLoadFlowResult.isOk()) {
throw new PowsyblException("AC loadfow divergence without fallback procedure enabled.");
if (!acLoadFlowResult.isFullyConverged()) {
throw new PowsyblException("AC loadflow divergence without fallback procedure enabled.");
}
return new Result(acLoadFlowResult, FALLBACK_HAS_NOT_BEEN_ACTIVATED);
}

Result runDcLoadflow(Network network, LoadFlowParameters loadFlowParameters) {
LoadFlowParameters dcEnforcedParameters = enforceDcLoadFlowCalculation(loadFlowParameters);
LoadFlowResult dcLoadFlowResult = runner.run(network, dcEnforcedParameters);
if (!dcLoadFlowResult.isOk()) {
throw new PowsyblException("DC loadfow divergence.");
if (!dcLoadFlowResult.isFullyConverged()) {
throw new PowsyblException("DC loadflow divergence.");
}
return new Result(dcLoadFlowResult, FALLBACK_HAS_NOT_BEEN_ACTIVATED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ public void computedNodalInjectionsMatrix(Map<String, Map<String, Double>> nodal
}

@Override
public void computedPtdfMatrix(Map<String, Map<String, Double>> pdtfMatrix) {
public void computedPtdfMatrix(Map<String, Map<String, Double>> ptdfMatrix) {
addEvent(Event.COMPUTED_PTDF_MATRIX);
this.ptdfs.put(currentContingency, pdtfMatrix);
this.ptdfs.put(currentContingency, ptdfMatrix);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ void testFlowDecompositionOnHvdcNetwork() {
assertTrue(flowDecompositionResults.getZoneSet().contains(Country.NL));
}

@Test
void testConnectedComponentModeChangesFromAllToMain() {
LoadFlowParameters loadFlowParameters = new LoadFlowParameters().setConnectedComponentMode(LoadFlowParameters.ConnectedComponentMode.ALL);
FlowDecompositionComputer flowDecompositionComputer = new FlowDecompositionComputer(new FlowDecompositionParameters(), loadFlowParameters);
// lfParameters inside flow decomposition changed from all to main
assertEquals(FlowDecompositionComputer.MAIN_CONNECTED_COMPONENT, flowDecompositionComputer.getLoadFlowParameters().getConnectedComponentMode());
// original lfParameters didn't change
assertEquals(LoadFlowParameters.ConnectedComponentMode.ALL, loadFlowParameters.getConnectedComponentMode());
}

private static FlowDecompositionResults runFlowDecomposition(Network network, XnecProvider xnecProvider) {
FlowDecompositionParameters flowDecompositionParameters = new FlowDecompositionParameters()
.setEnableLossesCompensation(FlowDecompositionParameters.ENABLE_LOSSES_COMPENSATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class LoadFlowFallbackTests {

public static final String FALLBACK_MESSAGE = "AC loadfow divergence without fallback procedure enabled";
public static final String FALLBACK_MESSAGE = "AC loadflow divergence without fallback procedure enabled.";

@Test
void testIntegrationOfDisabledFallbackOnNetworkThatDoesNotConvergeInAc() {
Expand All @@ -38,7 +38,7 @@ void testIntegrationOfDisabledFallbackOnNetworkThatDoesNotConvergeInAc() {
XnecProvider xnecProvider = XnecProviderByIds.builder().addNetworkElementsOnBasecase(Set.of("UNUSED")).build();
Executable flowComputerExecutable = () -> flowComputer.run(xnecProvider, network);
Exception exception = assertThrows(PowsyblException.class, flowComputerExecutable, FALLBACK_MESSAGE);
assertEquals("AC loadfow divergence without fallback procedure enabled.", exception.getMessage());
assertEquals(FALLBACK_MESSAGE, exception.getMessage());
}

@Test
Expand All @@ -48,7 +48,7 @@ void testLoadFlowServiceWhenLoadFlowConvergeInACWithFallbackActivated() {
LoadFlowRunningService loadFlowRunningService = new LoadFlowRunningService(LoadFlow.find());
LoadFlowRunningService.Result loadFlowResult = loadFlowRunningService.runAcLoadflow(
network, new LoadFlowParameters(), LoadFlowRunningService.FALLBACK_HAS_BEEN_ACTIVATED);
assertTrue(loadFlowResult.getLoadFlowResult().isOk());
assertTrue(loadFlowResult.getLoadFlowResult().isFullyConverged());
assertFalse(loadFlowResult.fallbackHasBeenActivated());
}

Expand All @@ -59,7 +59,7 @@ void testLoadFlowServiceWhenLoadFlowConvergeInACWithoutFallbackActivated() {
LoadFlowRunningService loadFlowRunningService = new LoadFlowRunningService(LoadFlow.find());
LoadFlowRunningService.Result loadFlowResult = loadFlowRunningService.runAcLoadflow(
network, new LoadFlowParameters(), LoadFlowRunningService.FALLBACK_HAS_NOT_BEEN_ACTIVATED);
assertTrue(loadFlowResult.getLoadFlowResult().isOk());
assertTrue(loadFlowResult.getLoadFlowResult().isFullyConverged());
assertFalse(loadFlowResult.fallbackHasBeenActivated());
}

Expand All @@ -70,7 +70,7 @@ void testLoadFlowServiceWhenLoadFlowDoesNotConvergeInACWithFallbackActivated() {
LoadFlowRunningService loadFlowRunningService = new LoadFlowRunningService(LoadFlow.find());
LoadFlowRunningService.Result loadFlowResult = loadFlowRunningService.runAcLoadflow(
network, new LoadFlowParameters(), LoadFlowRunningService.FALLBACK_HAS_BEEN_ACTIVATED);
assertTrue(loadFlowResult.getLoadFlowResult().isOk());
assertTrue(loadFlowResult.getLoadFlowResult().isFullyConverged());
assertTrue(loadFlowResult.fallbackHasBeenActivated());
}

Expand All @@ -82,6 +82,6 @@ void testLoadFlowServiceWhenLoadFlowDoesNotConvergeInACWithoutFallback() {
Executable loadFlowRunningServiceExecutable = () -> loadFlowRunningService.runAcLoadflow(
network, new LoadFlowParameters(), LoadFlowRunningService.FALLBACK_HAS_NOT_BEEN_ACTIVATED);
Exception exception = assertThrows(PowsyblException.class, loadFlowRunningServiceExecutable, FALLBACK_MESSAGE);
assertEquals("AC loadfow divergence without fallback procedure enabled.", exception.getMessage());
assertEquals(FALLBACK_MESSAGE, exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void testThatReferenceNodalInjectionsAreWellComputedOnLoopFlowNetwork() {
private static Map<String, Map<String, Double>> getNodalInjections(String networkFileName) {
Network network = importNetwork(networkFileName);
LoadFlowResult loadFlowResult = LoadFlow.run(network);
if (!loadFlowResult.isOk()) {
if (!loadFlowResult.isFullyConverged()) {
LoadFlow.run(network, LoadFlowParameters.load().setDc(true));
}
AutoGlskProvider glskProvider = new AutoGlskProvider();
Expand All @@ -111,7 +111,7 @@ private static Map<String, Map<String, Double>> getNodalInjections(String networ
private static Map<String, Double> getReferenceNodalInjections(String networkFileName) {
Network network = importNetwork(networkFileName);
LoadFlowResult loadFlowResult = LoadFlow.run(network);
if (!loadFlowResult.isOk()) {
if (!loadFlowResult.isFullyConverged()) {
LoadFlow.run(network, LoadFlowParameters.load().setDc(true));
}
List<Branch> xnecList = network.getBranchStream().collect(Collectors.toList());
Expand Down

0 comments on commit 77dd7b7

Please sign in to comment.