From dc4cc11ec470aec93524988f344d1a1c7dfd1421 Mon Sep 17 00:00:00 2001 From: Geoffroy Jamgotchian Date: Fri, 17 Feb 2023 21:15:43 +0100 Subject: [PATCH] Fix missing DC context close Signed-off-by: Geoffroy Jamgotchian --- .../openloadflow/sa/DcSecurityAnalysis.java | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java index 3e4c5fee43..ff3cf89303 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java @@ -278,45 +278,47 @@ private List runActionSimulations(DcSecurityAnalysisCont Map actionsById, Set neededActions) { // Run initial load flow and save state - DcLoadFlowContext lfContext = new DcLoadFlowContext(lfNetwork, parameters); - new DcLoadFlowEngine(lfContext).run(); - NetworkState networkState = NetworkState.save(lfNetwork); + try (DcLoadFlowContext lfContext = new DcLoadFlowContext(lfNetwork, parameters)) { - OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters()); - boolean createResultExtension = openSecurityAnalysisParameters.isCreateResultExtension(); + new DcLoadFlowEngine(lfContext).run(); + NetworkState networkState = NetworkState.save(lfNetwork); - var preContingencyLimitViolationManager = new LimitViolationManager(); - preContingencyLimitViolationManager.detectViolations(lfNetwork); + OpenSecurityAnalysisParameters openSecurityAnalysisParameters = OpenSecurityAnalysisParameters.getOrDefault(context.getParameters()); + boolean createResultExtension = openSecurityAnalysisParameters.isCreateResultExtension(); - Map> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions); - Map lfActionById = createLfActions(lfNetwork, neededActions, network, false); - Iterator contingencyIt = propagatedContingencies.iterator(); + var preContingencyLimitViolationManager = new LimitViolationManager(); + preContingencyLimitViolationManager.detectViolations(lfNetwork); - List operatorStrategyResults = new ArrayList<>(); - while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) { - PropagatedContingency propagatedContingency = contingencyIt.next(); - List operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId()); + Map> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions); + Map lfActionById = createLfActions(lfNetwork, neededActions, network, false); + Iterator contingencyIt = propagatedContingencies.iterator(); - if (operatorStrategiesForThisContingency == null) { - break; - } - for (OperatorStrategy operatorStrategy : operatorStrategiesForThisContingency) { - if (checkCondition(operatorStrategy, context.getLimitViolationsPerContingencyId().get(propagatedContingency.getContingency().getId()))) { - propagatedContingency.toLfContingency(lfNetwork) - .ifPresent(lfContingency -> { - lfContingency.apply(context.getParameters().getLoadFlowParameters().getBalanceType()); - OperatorStrategyResult result = runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(), - lfActionById, createResultExtension, lfContingency, parameters.getBalanceType()); - operatorStrategyResults.add(result); - networkState.restore(); - }); + List operatorStrategyResults = new ArrayList<>(); + while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) { + PropagatedContingency propagatedContingency = contingencyIt.next(); + List operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId()); + + if (operatorStrategiesForThisContingency == null) { + break; + } + for (OperatorStrategy operatorStrategy : operatorStrategiesForThisContingency) { + if (checkCondition(operatorStrategy, context.getLimitViolationsPerContingencyId().get(propagatedContingency.getContingency().getId()))) { + propagatedContingency.toLfContingency(lfNetwork) + .ifPresent(lfContingency -> { + lfContingency.apply(context.getParameters().getLoadFlowParameters().getBalanceType()); + OperatorStrategyResult result = runActionSimulation(lfNetwork, lfContext, operatorStrategy, preContingencyLimitViolationManager, context.getParameters().getIncreasedViolationsParameters(), + lfActionById, createResultExtension, lfContingency, parameters.getBalanceType()); + operatorStrategyResults.add(result); + networkState.restore(); + }); + } } } - } - completeConnectivityResults(context, lfNetwork, propagatedContingencies, networkState); + completeConnectivityResults(context, lfNetwork, propagatedContingencies, networkState); - return operatorStrategyResults; + return operatorStrategyResults; + } } private void completeConnectivityResults(DcSecurityAnalysisContext context, LfNetwork lfNetwork,