From 307cc690971b7c4a7f5f0474fc1c859804692fa6 Mon Sep 17 00:00:00 2001 From: p-arvy Date: Mon, 8 Jul 2024 19:03:10 +0200 Subject: [PATCH] Merge treatment of override for contingency breaking connectivity or not Signed-off-by: p-arvy --- .../sensi/DcSensitivityAnalysis.java | 96 +++++++------------ 1 file changed, 33 insertions(+), 63 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sensi/DcSensitivityAnalysis.java index 2bdbcac76b..112ea64977 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, boolean hasRhsChangedDueToConnectivityBreak) { + SensitivityResultWriter resultWriter, ReportNode reportNode, Set partialDisabledBranches, Collection slackConnectedComponent) { List> factors = validFactorHolder.getFactorsForContingency(contingency.getContingency().getId()); Collection contingencyElements = contingency.getBranchIdsToOpen().keySet().stream() .filter(element -> !elementsToReconnect.contains(element)) @@ -332,17 +332,27 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo LfContingency lfContingency = contingency.toLfContingency(lfNetwork).orElse(null); DenseMatrix newFactorStates = factorStates; List newParticipatingElements = participatingElements; - boolean participatingElementsChanged = false; - boolean rhsChanged = false; + 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 if (lfContingency != null) { lfContingency.apply(lfParameters.getBalanceType()); - participatingElementsChanged = isDistributedSlackOnGenerators(lfParameters) && !contingency.getGeneratorIdsToLose().isEmpty() - || isDistributedSlackOnLoads(lfParameters) && !contingency.getLoadIdsToLoose().isEmpty(); + // TODO : verify if we need rhsChanged AND participatingElementsChanged + if (lfParameters.isDistributedSlack()) { + rhsChanged = participatingElements.stream().anyMatch(element -> disabledBuses.contains(element.getLfBus())); + } 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 @@ -364,7 +374,7 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo } // we need to recompute the factor states because the rhs has changed or - if (participatingElementsChanged || rhsChanged || hasRhsChangedDueToConnectivityBreak) { + if (participatingElementsChanged || rhsChanged) { newFactorStates = calculateFactorStates(loadFlowContext, factorGroups, newParticipatingElements); } @@ -384,61 +394,9 @@ private void calculateSensitivityValuesForAContingency(DcLoadFlowContext loadFlo contingency, resultWriter, disabledNetwork); networkState.restore(); - if (participatingElementsChanged || rhsChanged || hasRhsChangedDueToConnectivityBreak) { + if (participatingElementsChanged || rhsChanged) { 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) { @@ -602,16 +560,28 @@ 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(), false); + connectivityBreakAnalysisResults.contingencyElementByBranch(), Collections.emptySet(), participatingElements, Collections.emptySet(), resultWriter, reportNode, Collections.emptySet(), + lfNetwork.getBuses()); } LOGGER.info("Processing contingencies with connectivity break"); // process contingencies with connectivity break for (ConnectivityBreakAnalysis.ConnectivityAnalysisResult connectivityAnalysisResult : connectivityBreakAnalysisResults.connectivityAnalysisResults()) { - processContingenciesBreakingConnectivity(connectivityAnalysisResult, loadFlowContext, lfParameters, lfParametersExt, - validFactorHolder, factorGroups, participatingElements, connectivityBreakAnalysisResults.contingencyElementByBranch(), - flowStates, factorsStates, connectivityBreakAnalysisResults.contingenciesStates(), resultWriter, reportNode); + 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()); } }