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

Remove reset of variant ID in security and sensitivity analysis #623

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ public CompletableFuture<SecurityAnalysisReport> run(String workingVariantId, Se
Objects.requireNonNull(securityAnalysisParameters);
Objects.requireNonNull(contingenciesProvider);
return CompletableFutureTask.runAsync(() -> {
String oldWorkingVariantId = network.getVariantManager().getWorkingVariantId();
network.getVariantManager().setWorkingVariant(workingVariantId);
try {
return runSync(workingVariantId, securityAnalysisParameters, contingenciesProvider, computationManager);
} finally {
network.getVariantManager().setWorkingVariant(oldWorkingVariantId);
}
return runSync(workingVariantId, securityAnalysisParameters, contingenciesProvider, computationManager);
}, computationManager.getExecutor());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,64 +147,59 @@ public CompletableFuture<Void> run(Network network,
Objects.requireNonNull(reporter);

return CompletableFuture.runAsync(() -> {
String oldWorkingVariantId = network.getVariantManager().getWorkingVariantId();
network.getVariantManager().setWorkingVariant(workingVariantId);
try {
Reporter sensiReporter = Reports.createSensitivityAnalysis(reporter, network.getId());

LoadFlowParameters lfParameters = sensitivityAnalysisParameters.getLoadFlowParameters();
OpenLoadFlowParameters lfParametersExt = OpenLoadFlowParameters.get(lfParameters);
OpenSensitivityAnalysisParameters sensitivityAnalysisParametersExt = getSensitivityAnalysisParametersExtension(sensitivityAnalysisParameters);

// We only support switch contingency for the moment. Contingency propagation is not supported yet.
// Contingency propagation leads to numerous zero impedance branches, that are managed as min impedance
// branches in sensitivity analysis. It could lead to issues with voltage controls in AC analysis.
Set<Switch> allSwitchesToOpen = new HashSet<>();
List<PropagatedContingency> propagatedContingencies = PropagatedContingency.createList(network, contingencies, allSwitchesToOpen, false,
sensitivityAnalysisParameters.getLoadFlowParameters().getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD,
sensitivityAnalysisParameters.getLoadFlowParameters().isHvdcAcEmulation() && !sensitivityAnalysisParameters.getLoadFlowParameters().isDc(),
false);

SensitivityFactorReader decoratedFactorReader = factorReader;

// debugging
if (sensitivityAnalysisParametersExt.getDebugDir() != null) {
Path debugDir = PlatformConfig.defaultConfig().getConfigDir()
.map(dir -> dir.getFileSystem().getPath(sensitivityAnalysisParametersExt.getDebugDir()))
.orElseThrow(() -> new PowsyblException("Cannot write to debug directory as no configuration directory has been defined"));
String dateStr = DateTime.now().toString(DATE_TIME_FORMAT);

NetworkXml.write(network, debugDir.resolve("network-" + dateStr + ".xiidm"));

ObjectWriter objectWriter = createObjectMapper()
.writerWithDefaultPrettyPrinter();
try {
try (BufferedWriter writer = Files.newBufferedWriter(debugDir.resolve("contingencies-" + dateStr + ".json"), StandardCharsets.UTF_8)) {
ContingencyList contingencyList = new DefaultContingencyList("default", contingencies);
objectWriter.writeValue(writer, contingencyList);
}

try (BufferedWriter writer = Files.newBufferedWriter(debugDir.resolve("variable-sets-" + dateStr + ".json"), StandardCharsets.UTF_8)) {
objectWriter.writeValue(writer, variableSets);
}

try (BufferedWriter writer = Files.newBufferedWriter(debugDir.resolve("parameters-" + dateStr + ".json"), StandardCharsets.UTF_8)) {
objectWriter.writeValue(writer, sensitivityAnalysisParameters);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
Reporter sensiReporter = Reports.createSensitivityAnalysis(reporter, network.getId());

LoadFlowParameters lfParameters = sensitivityAnalysisParameters.getLoadFlowParameters();
OpenLoadFlowParameters lfParametersExt = OpenLoadFlowParameters.get(lfParameters);
OpenSensitivityAnalysisParameters sensitivityAnalysisParametersExt = getSensitivityAnalysisParametersExtension(sensitivityAnalysisParameters);

// We only support switch contingency for the moment. Contingency propagation is not supported yet.
// Contingency propagation leads to numerous zero impedance branches, that are managed as min impedance
// branches in sensitivity analysis. It could lead to issues with voltage controls in AC analysis.
Set<Switch> allSwitchesToOpen = new HashSet<>();
List<PropagatedContingency> propagatedContingencies = PropagatedContingency.createList(network, contingencies, allSwitchesToOpen, false,
sensitivityAnalysisParameters.getLoadFlowParameters().getBalanceType() == LoadFlowParameters.BalanceType.PROPORTIONAL_TO_CONFORM_LOAD,
sensitivityAnalysisParameters.getLoadFlowParameters().isHvdcAcEmulation() && !sensitivityAnalysisParameters.getLoadFlowParameters().isDc(),
false);

SensitivityFactorReader decoratedFactorReader = factorReader;

// debugging
if (sensitivityAnalysisParametersExt.getDebugDir() != null) {
Path debugDir = PlatformConfig.defaultConfig().getConfigDir()
.map(dir -> dir.getFileSystem().getPath(sensitivityAnalysisParametersExt.getDebugDir()))
.orElseThrow(() -> new PowsyblException("Cannot write to debug directory as no configuration directory has been defined"));
String dateStr = DateTime.now().toString(DATE_TIME_FORMAT);

NetworkXml.write(network, debugDir.resolve("network-" + dateStr + ".xiidm"));

ObjectWriter objectWriter = createObjectMapper()
.writerWithDefaultPrettyPrinter();
try {
try (BufferedWriter writer = Files.newBufferedWriter(debugDir.resolve("contingencies-" + dateStr + ".json"), StandardCharsets.UTF_8)) {
ContingencyList contingencyList = new DefaultContingencyList("default", contingencies);
objectWriter.writeValue(writer, contingencyList);
}

decoratedFactorReader = new SensitivityFactoryJsonRecorder(factorReader, debugDir.resolve("factors-" + dateStr + ".json"));
}
try (BufferedWriter writer = Files.newBufferedWriter(debugDir.resolve("variable-sets-" + dateStr + ".json"), StandardCharsets.UTF_8)) {
objectWriter.writeValue(writer, variableSets);
}

if (lfParameters.isDc()) {
dcSensitivityAnalysis.analyse(network, propagatedContingencies, variableSets, lfParameters, lfParametersExt, decoratedFactorReader, resultWriter, sensiReporter, allSwitchesToOpen);
} else {
acSensitivityAnalysis.analyse(network, propagatedContingencies, variableSets, lfParameters, lfParametersExt, decoratedFactorReader, resultWriter, sensiReporter, allSwitchesToOpen);
try (BufferedWriter writer = Files.newBufferedWriter(debugDir.resolve("parameters-" + dateStr + ".json"), StandardCharsets.UTF_8)) {
objectWriter.writeValue(writer, sensitivityAnalysisParameters);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} finally {
network.getVariantManager().setWorkingVariant(oldWorkingVariantId);

decoratedFactorReader = new SensitivityFactoryJsonRecorder(factorReader, debugDir.resolve("factors-" + dateStr + ".json"));
}

if (lfParameters.isDc()) {
dcSensitivityAnalysis.analyse(network, propagatedContingencies, variableSets, lfParameters, lfParametersExt, decoratedFactorReader, resultWriter, sensiReporter, allSwitchesToOpen);
} else {
acSensitivityAnalysis.analyse(network, propagatedContingencies, variableSets, lfParameters, lfParametersExt, decoratedFactorReader, resultWriter, sensiReporter, allSwitchesToOpen);
}
}, computationManager.getExecutor());
}
Expand Down