From fc664241b571ea2867ed123c1e179ea891cc82df Mon Sep 17 00:00:00 2001 From: Guillaume Verger Date: Wed, 31 Jan 2024 12:13:13 +0100 Subject: [PATCH] refactor flow extraction Signed-off-by: Guillaume Verger --- .../AcReferenceFlowComputer.java | 30 +++++++++++++++++++ .../FlowDecompositionComputer.java | 16 +++------- .../FlowDecompositionObserverList.java | 13 ++------ .../ReferenceFlowComputer.java | 4 +-- 4 files changed, 39 insertions(+), 24 deletions(-) create mode 100644 flow-decomposition/src/main/java/com/powsybl/flow_decomposition/AcReferenceFlowComputer.java diff --git a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/AcReferenceFlowComputer.java b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/AcReferenceFlowComputer.java new file mode 100644 index 00000000..cb342b82 --- /dev/null +++ b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/AcReferenceFlowComputer.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * SPDX-License-Identifier: MPL-2.0 + */ +package com.powsybl.flow_decomposition; + +import com.powsybl.iidm.network.Branch; +import com.powsybl.iidm.network.Identifiable; + +import java.util.Collection; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author Guillaume Verger {@literal } + */ +class AcReferenceFlowComputer { + private static ReferenceFlowComputer flowComputer = new ReferenceFlowComputer(); + + Map run(Collection xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) { + if (loadFlowServiceAcResult.fallbackHasBeenActivated()) { + return xnecList.stream().collect(Collectors.toMap(Identifiable::getId, branch -> Double.NaN)); + } + + return flowComputer.run(xnecList); + } +} diff --git a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionComputer.java b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionComputer.java index e42b7afe..99d35078 100644 --- a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionComputer.java +++ b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionComputer.java @@ -9,7 +9,6 @@ import com.powsybl.flow_decomposition.glsk_provider.AutoGlskProvider; import com.powsybl.iidm.network.Branch; import com.powsybl.iidm.network.Country; -import com.powsybl.iidm.network.Identifiable; import com.powsybl.iidm.network.Network; import com.powsybl.loadflow.LoadFlow; import com.powsybl.loadflow.LoadFlowParameters; @@ -19,7 +18,6 @@ import java.util.ArrayList; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; /** * @author Sebastien Murgey {@literal } @@ -180,12 +178,7 @@ private LoadFlowRunningService.Result runAcLoadFlow(Network network) { } private void saveAcReferenceFlow(FlowDecompositionResults.PerStateBuilder flowDecompositionResultBuilder, Set xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) { - Map acReferenceFlows; - if (loadFlowServiceAcResult.fallbackHasBeenActivated()) { - acReferenceFlows = xnecList.stream().collect(Collectors.toMap(Identifiable::getId, branch -> Double.NaN)); - } else { - acReferenceFlows = getXnecReferenceFlows(xnecList); - } + Map acReferenceFlows = new AcReferenceFlowComputer().run(xnecList, loadFlowServiceAcResult); flowDecompositionResultBuilder.saveAcReferenceFlow(acReferenceFlows); } @@ -194,9 +187,8 @@ private Map getZonesNetPosition(Network network) { return netPositionComputer.run(network); } - private Map getXnecReferenceFlows(Set xnecList) { - ReferenceFlowComputer referenceFlowComputer = new ReferenceFlowComputer(); - return referenceFlowComputer.run(xnecList); + private Map getBranchReferenceFlows(Set branches) { + return new ReferenceFlowComputer().run(branches); } private void compensateLosses(Network network) { @@ -218,7 +210,7 @@ private SparseMatrixWithIndexesTriplet getNodalInjectionsMatrix(Network network, } private void saveDcReferenceFlow(FlowDecompositionResults.PerStateBuilder flowDecompositionResultBuilder, Set xnecList) { - flowDecompositionResultBuilder.saveDcReferenceFlow(getXnecReferenceFlows(xnecList)); + flowDecompositionResultBuilder.saveDcReferenceFlow(getBranchReferenceFlows(xnecList)); } private SensitivityAnalyser getSensitivityAnalyser(Network network, NetworkMatrixIndexes networkMatrixIndexes) { diff --git a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionObserverList.java b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionObserverList.java index 22713bbd..f15ef9a9 100644 --- a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionObserverList.java +++ b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/FlowDecompositionObserverList.java @@ -9,13 +9,11 @@ import com.powsybl.flow_decomposition.LoadFlowRunningService.Result; import com.powsybl.iidm.network.Country; -import com.powsybl.iidm.network.Identifiable; import com.powsybl.iidm.network.Network; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; /** * @author Guillaume Verger {@literal } @@ -88,13 +86,8 @@ public void computedAcFlows(Network network, Result loadFlowServiceAcResult) { return; } - Map acFlows; - if (loadFlowServiceAcResult.fallbackHasBeenActivated()) { - acFlows = network.getBranchStream().collect(Collectors.toMap(Identifiable::getId, branch -> Double.NaN)); - } else { - ReferenceFlowComputer flowComputer = new ReferenceFlowComputer(); - acFlows = flowComputer.run(network.getBranchStream().collect(Collectors.toSet())); - } + Map acFlows = + new AcReferenceFlowComputer().run(network.getBranchStream().toList(), loadFlowServiceAcResult); for (FlowDecompositionObserver o : observers) { o.computedAcFlows(acFlows); @@ -107,7 +100,7 @@ public void computedDcFlows(Network network) { } ReferenceFlowComputer flowComputer = new ReferenceFlowComputer(); - Map dcFlows = flowComputer.run(network.getBranchStream().collect(Collectors.toSet())); + Map dcFlows = flowComputer.run(network.getBranchStream().toList()); for (FlowDecompositionObserver o : observers) { o.computedDcFlows(dcFlows); diff --git a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/ReferenceFlowComputer.java b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/ReferenceFlowComputer.java index d1470fbe..68fb4061 100644 --- a/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/ReferenceFlowComputer.java +++ b/flow-decomposition/src/main/java/com/powsybl/flow_decomposition/ReferenceFlowComputer.java @@ -9,8 +9,8 @@ import com.powsybl.iidm.network.Branch; import com.powsybl.iidm.network.Identifiable; +import java.util.Collection; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; /** @@ -18,7 +18,7 @@ * @author Hugo Schindler {@literal } */ class ReferenceFlowComputer { - Map run(Set xnecList) { + Map run(Collection xnecList) { return xnecList.stream() .collect(Collectors.toMap( Identifiable::getId,