Skip to content

Commit

Permalink
Fix missing DC context close (#732)
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <geoffroy.jamgotchian@gmail.com>
  • Loading branch information
geofjamg authored Feb 17, 2023
1 parent c2c59bb commit 76746ba
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions src/main/java/com/powsybl/openloadflow/sa/DcSecurityAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,45 +278,47 @@ private List<OperatorStrategyResult> runActionSimulations(DcSecurityAnalysisCont
Map<String, Action> actionsById, Set<Action> 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<String, List<OperatorStrategy>> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions);
Map<String, LfAction> lfActionById = createLfActions(lfNetwork, neededActions, network, false);
Iterator<PropagatedContingency> contingencyIt = propagatedContingencies.iterator();
var preContingencyLimitViolationManager = new LimitViolationManager();
preContingencyLimitViolationManager.detectViolations(lfNetwork);

List<OperatorStrategyResult> operatorStrategyResults = new ArrayList<>();
while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) {
PropagatedContingency propagatedContingency = contingencyIt.next();
List<OperatorStrategy> operatorStrategiesForThisContingency = operatorStrategiesByContingencyId.get(propagatedContingency.getContingency().getId());
Map<String, List<OperatorStrategy>> operatorStrategiesByContingencyId = indexOperatorStrategiesByContingencyId(propagatedContingencies, operatorStrategies, actionsById, neededActions);
Map<String, LfAction> lfActionById = createLfActions(lfNetwork, neededActions, network, false);
Iterator<PropagatedContingency> 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<OperatorStrategyResult> operatorStrategyResults = new ArrayList<>();
while (contingencyIt.hasNext() && !Thread.currentThread().isInterrupted()) {
PropagatedContingency propagatedContingency = contingencyIt.next();
List<OperatorStrategy> 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,
Expand Down

0 comments on commit 76746ba

Please sign in to comment.