Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing DC context close #732

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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