From fc6d0b4b0a3b848589ce2a0eceac72d56a92e8f6 Mon Sep 17 00:00:00 2001 From: Bertrand Rix Date: Fri, 14 Oct 2022 15:35:05 +0200 Subject: [PATCH] Remove useless reset of variant ID, fixing exception when trying to access an uninitialized variant id from a child thread. Signed-off-by: Bertrand Rix --- .../sa/AbstractSecurityAnalysis.java | 7 +- .../OpenSensitivityAnalysisProvider.java | 101 +++++++++--------- 2 files changed, 49 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java b/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java index f8b27cb9d9..a4503c87d8 100644 --- a/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java +++ b/src/main/java/com/powsybl/openloadflow/sa/AbstractSecurityAnalysis.java @@ -58,13 +58,8 @@ public CompletableFuture 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()); } diff --git a/src/main/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProvider.java b/src/main/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProvider.java index 65ad71aedd..77377c40ce 100644 --- a/src/main/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProvider.java +++ b/src/main/java/com/powsybl/openloadflow/sensi/OpenSensitivityAnalysisProvider.java @@ -147,64 +147,59 @@ public CompletableFuture 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 allSwitchesToOpen = new HashSet<>(); - List 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 allSwitchesToOpen = new HashSet<>(); + List 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()); }