From 6ee4fc21aebcfcc3f4b7420c84ad25df54ba8e98 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Tue, 9 Jul 2024 14:23:43 +0200 Subject: [PATCH] Revert "Merge treatment of override for contingency breaking connectivity or not" This reverts commit 307cc690971b7c4a7f5f0474fc1c859804692fa6. --- .../sensi/DcSensitivityAnalysis.java | 96 ++++++++++++------- 1 file changed, 63 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index 112ea64977..2bdbcac76b 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java @@ -315,7 +315,7 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo SensitivityFactorGroupList factorGroups, DenseMatrix factorStates, DenseMatrix contingenciesStates, DenseMatrix flowStates, PropagatedContingency contingency, Map contingencyElementByBranch, Set disabledBuses, List participatingElements, Set elementsToReconnect, - SensitivityResultWriter resultWriter, ReportNode reportNode, Set partialDisabledBranches, Collection slackConnectedComponent) { + SensitivityResultWriter resultWriter, ReportNode reportNode, Set partialDisabledBranches, boolean hasRhsChangedDueToConnectivityBreak) { List> factors = validFactorHolder.getFactorsForContingency(contingency.getContingency().getId()); Collection contingencyElements = contingency.getBranchIdsToOpen().keySet().stream() .filter(element -> !elementsToReconnect.contains(element)) @@ -332,27 +332,17 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo LfContingency lfContingency = contingency.toLfContingency(lfNetwork).orElse(null); DenseMatrix newFactorStates = factorStates; List newParticipatingElements = participatingElements; - boolean participatingElementsChanged = false; // true if participating elements are changed due to contingency - boolean rhsChanged = false; // true if the disabled buses or the contingency change the slack distribution, or the GLSK + boolean participatingElementsChanged = false; + boolean rhsChanged = false; if (lfContingency != null) { lfContingency.apply(lfParameters.getBalanceType()); - // TODO : verify if we need rhsChanged AND participatingElementsChanged - if (lfParameters.isDistributedSlack()) { - rhsChanged = participatingElements.stream().anyMatch(element -> disabledBuses.contains(element.getLfBus())); - } + participatingElementsChanged = isDistributedSlackOnGenerators(lfParameters) && !contingency.getGeneratorIdsToLose().isEmpty() + || isDistributedSlackOnLoads(lfParameters) && !contingency.getLoadIdsToLoose().isEmpty(); if (factorGroups.hasMultiVariables()) { Set impactedBuses = lfContingency.getLoadAndGeneratorBuses(); impactedBuses.addAll(disabledBuses); rhsChanged = rescaleGlsk(factorGroups, impactedBuses); } - if (rhsChanged) { - newParticipatingElements = lfParameters.isDistributedSlack() - ? getParticipatingElements(slackConnectedComponent, lfParameters.getBalanceType(), lfParametersExt) // will also be used to recompute the loadflow - : Collections.emptyList(); - } - - participatingElementsChanged = isDistributedSlackOnGenerators(lfParameters) && !contingency.getGeneratorIdsToLose().isEmpty() - || isDistributedSlackOnLoads(lfParameters) && !contingency.getLoadIdsToLoose().isEmpty(); if (participatingElementsChanged) { if (isDistributedSlackOnGenerators(lfParameters)) { // deep copy of participatingElements, removing the participating LfGeneratorImpl whose targetP has been set to 0 @@ -374,7 +364,7 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo } // we need to recompute the factor states because the rhs has changed or - if (participatingElementsChanged || rhsChanged) { + if (participatingElementsChanged || rhsChanged || hasRhsChangedDueToConnectivityBreak) { newFactorStates = calculateFactorStates(loadFlowContext, factorGroups, newParticipatingElements); } @@ -394,9 +384,61 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo contingency, resultWriter, disabledNetwork); networkState.restore(); - if (participatingElementsChanged || rhsChanged) { + if (participatingElementsChanged || rhsChanged || hasRhsChangedDueToConnectivityBreak) { setBaseCaseSensitivityValues(factorGroups, factorStates); // we modified the rhs, we need to restore previous state } +// } + } + + private void processContingenciesBreakingConnectivity(ConnectivityBreakAnalysis.ConnectivityAnalysisResult connectivityAnalysisResult, DcLoadFlowContext loadFlowContext, + LoadFlowParameters lfParameters, OpenLoadFlowParameters lfParametersExt, + SensitivityFactorHolder validFactorHolder, + SensitivityFactorGroupList factorGroups, + List participatingElements, + Map contingencyElementByBranch, + DenseMatrix flowStates, DenseMatrix factorsStates, DenseMatrix contingenciesStates, + SensitivityResultWriter resultWriter, + ReportNode reportNode) { + + PropagatedContingency contingency = connectivityAnalysisResult.getPropagatedContingency(); + Set disabledBuses = connectivityAnalysisResult.getDisabledBuses(); + Set partialDisabledBranches = connectivityAnalysisResult.getPartialDisabledBranches(); + + // as we are processing a contingency with connectivity break, we have to reset active power flow of a hvdc line + // if one bus of the line is lost. + for (LfHvdc hvdc : loadFlowContext.getNetwork().getHvdcs()) { + if (Networks.isIsolatedBusForHvdc(hvdc.getBus1(), disabledBuses) ^ Networks.isIsolatedBusForHvdc(hvdc.getBus2(), disabledBuses)) { + contingency.getGeneratorIdsToLose().add(hvdc.getConverterStation1().getId()); + contingency.getGeneratorIdsToLose().add(hvdc.getConverterStation2().getId()); + } + } + + // null and unused if slack bus is not distributed + List participatingElementsForThisConnectivity = participatingElements; + boolean rhsChanged = false; // true if the disabled buses change the slack distribution, or the GLSK + if (lfParameters.isDistributedSlack()) { + rhsChanged = participatingElements.stream().anyMatch(element -> disabledBuses.contains(element.getLfBus())); + } + if (factorGroups.hasMultiVariables()) { + // some elements of the GLSK may not be in the connected component anymore, we recompute the injections + rhsChanged |= rescaleGlsk(factorGroups, disabledBuses); + } + + // we need to recompute the participating elements because the connectivity changed + if (rhsChanged) { + participatingElementsForThisConnectivity = lfParameters.isDistributedSlack() + ? getParticipatingElements(connectivityAnalysisResult.getSlackConnectedComponent(), lfParameters.getBalanceType(), lfParametersExt) // will also be used to recompute the loadflow + : Collections.emptyList(); + + // calcul de l'injection + } + + // calcul du flow + + calculateSensitivityValuesForAContingency(loadFlowContext, lfParametersExt, + validFactorHolder, factorGroups, factorsStates, contingenciesStates, flowStates, + contingency, contingencyElementByBranch, disabledBuses, participatingElementsForThisConnectivity, + connectivityAnalysisResult.getElementsToReconnect(), resultWriter, reportNode, partialDisabledBranches, rhsChanged); } protected void cleanContingencies(LfNetwork lfNetwork, List contingencies) { @@ -560,28 +602,16 @@ public void analyse(Network network, List contingencies, for (PropagatedContingency contingency : connectivityBreakAnalysisResults.nonBreakingConnectivityContingencies()) { calculateSensitivityValuesForAContingency(loadFlowContext, lfParametersExt, validFactorHolder, factorGroups, factorsStates, connectivityBreakAnalysisResults.contingenciesStates(), flowStates, contingency, - connectivityBreakAnalysisResults.contingencyElementByBranch(), Collections.emptySet(), participatingElements, Collections.emptySet(), resultWriter, reportNode, Collections.emptySet(), - lfNetwork.getBuses()); + connectivityBreakAnalysisResults.contingencyElementByBranch(), Collections.emptySet(), participatingElements, Collections.emptySet(), resultWriter, reportNode, Collections.emptySet(), false); } LOGGER.info("Processing contingencies with connectivity break"); // process contingencies with connectivity break for (ConnectivityBreakAnalysis.ConnectivityAnalysisResult connectivityAnalysisResult : connectivityBreakAnalysisResults.connectivityAnalysisResults()) { - Set disabledBuses = connectivityAnalysisResult.getDisabledBuses(); - PropagatedContingency contingency = connectivityAnalysisResult.getPropagatedContingency(); - - for (LfHvdc hvdc : loadFlowContext.getNetwork().getHvdcs()) { - if (Networks.isIsolatedBusForHvdc(hvdc.getBus1(), disabledBuses) ^ Networks.isIsolatedBusForHvdc(hvdc.getBus2(), disabledBuses)) { - contingency.getGeneratorIdsToLose().add(hvdc.getConverterStation1().getId()); - contingency.getGeneratorIdsToLose().add(hvdc.getConverterStation2().getId()); - } - } - - calculateSensitivityValuesForAContingency(loadFlowContext, lfParametersExt, - validFactorHolder, factorGroups, factorsStates, connectivityBreakAnalysisResults.contingenciesStates(), flowStates, - contingency, connectivityBreakAnalysisResults.contingencyElementByBranch(), disabledBuses, participatingElements, - connectivityAnalysisResult.getElementsToReconnect(), resultWriter, reportNode, connectivityAnalysisResult.getPartialDisabledBranches(), connectivityAnalysisResult.getSlackConnectedComponent()); + processContingenciesBreakingConnectivity(connectivityAnalysisResult, loadFlowContext, lfParameters, lfParametersExt, + validFactorHolder, factorGroups, participatingElements, connectivityBreakAnalysisResults.contingencyElementByBranch(), + flowStates, factorsStates, connectivityBreakAnalysisResults.contingenciesStates(), resultWriter, reportNode); } }