From 1a0e329ecd2a5562fc2f5211186dae87f04ffec4 Mon Sep 17 00:00:00 2001 From: CHEN Roxane Date: Thu, 12 Dec 2024 14:52:25 +0100 Subject: [PATCH] Fixed sensi computation in parallel Signed-off-by: CHEN Roxane --- .../InterTemporalSensitivityAnalysis.java | 22 +++++++++++-- .../InterTemporalSensitivityAnalysisTest.java | 32 ++++++++++--------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/ra-optimisation/search-tree-rao/src/main/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysis.java b/ra-optimisation/search-tree-rao/src/main/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysis.java index 0e3f239cc9..b1d657db0e 100644 --- a/ra-optimisation/search-tree-rao/src/main/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysis.java +++ b/ra-optimisation/search-tree-rao/src/main/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysis.java @@ -8,6 +8,7 @@ package com.powsybl.openrao.searchtreerao.castor.algorithm; import com.powsybl.iidm.network.Network; +import com.powsybl.openrao.commons.OpenRaoException; import com.powsybl.openrao.commons.TemporalData; import com.powsybl.openrao.commons.TemporalDataImpl; import com.powsybl.openrao.data.crac.api.Crac; @@ -20,11 +21,13 @@ import com.powsybl.openrao.searchtreerao.commons.SensitivityComputer; import com.powsybl.openrao.searchtreerao.commons.ToolProvider; import com.powsybl.openrao.searchtreerao.result.api.LoadFlowAndSensitivityResult; +import org.jgrapht.alg.util.Pair; import java.time.OffsetDateTime; import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; /** * @author Thomas Bouquet {@literal } @@ -44,7 +47,20 @@ public InterTemporalSensitivityAnalysis(InterTemporalRaoInput input, RaoParamete } public TemporalData runInitialSensitivityAnalysis() throws InterruptedException { - return new TemporalDataImpl<>(input.getTimestampsToRun().stream().collect(Collectors.toMap(Function.identity(), this::runForTimestamp))); + ForkJoinPool timestampPool = new ForkJoinPool(input.getTimestampsToRun().size()); + List>> tasks = input.getTimestampsToRun().stream().map(timestamp -> + timestampPool.submit(() -> Pair.of(timestamp, runForTimestamp(timestamp))) + ).toList(); + Map loadFlowAndSensitivityResultPerTimestamp = new HashMap<>(); + for (ForkJoinTask> task : tasks) { + try { + Pair taskResult = task.get(); + loadFlowAndSensitivityResultPerTimestamp.put(taskResult.getFirst(), taskResult.getSecond()); + } catch (ExecutionException e) { + throw new OpenRaoException(e); + } + } + return new TemporalDataImpl<>(loadFlowAndSensitivityResultPerTimestamp); } Map>> getRangeActionsPerTimestamp() { diff --git a/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysisTest.java b/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysisTest.java index 34a872e46c..44ba5d29fe 100644 --- a/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysisTest.java +++ b/ra-optimisation/search-tree-rao/src/test/java/com/powsybl/openrao/searchtreerao/castor/algorithm/InterTemporalSensitivityAnalysisTest.java @@ -52,15 +52,17 @@ class InterTemporalSensitivityAnalysisTest { @BeforeEach void setUp() throws IOException { - Network network = Network.read("12Nodes_2_pst.uct", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/network/12Nodes_2_pst.uct")); + Network network1 = Network.read("12Nodes_2_pst.uct", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/network/12Nodes_2_pst.uct")); + Network network2 = Network.read("12Nodes_2_pst.uct", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/network/12Nodes_2_pst.uct")); + Network network3 = Network.read("12Nodes_2_pst.uct", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/network/12Nodes_2_pst.uct")); - crac1 = Crac.read("small-crac-2pst-1600.json", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/crac/small-crac-2pst-1600.json"), network); - crac2 = Crac.read("small-crac-2pst-1700.json", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/crac/small-crac-2pst-1700.json"), network); - crac3 = Crac.read("small-crac-2pst-1800.json", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/crac/small-crac-2pst-1800.json"), network); + crac1 = Crac.read("small-crac-2pst-1600.json", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/crac/small-crac-2pst-1600.json"), network1); + crac2 = Crac.read("small-crac-2pst-1700.json", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/crac/small-crac-2pst-1700.json"), network2); + crac3 = Crac.read("small-crac-2pst-1800.json", InterTemporalSensitivityAnalysisTest.class.getResourceAsStream("/crac/small-crac-2pst-1800.json"), network3); - RaoInput raoInput1 = RaoInput.build(network, crac1).build(); - RaoInput raoInput2 = RaoInput.build(network, crac2).build(); - RaoInput raoInput3 = RaoInput.build(network, crac3).build(); + RaoInput raoInput1 = RaoInput.build(network1, crac1).build(); + RaoInput raoInput2 = RaoInput.build(network2, crac2).build(); + RaoInput raoInput3 = RaoInput.build(network3, crac3).build(); InterTemporalRaoInput input = new InterTemporalRaoInput(new TemporalDataImpl<>(Map.of(timestamp1, raoInput1, timestamp2, raoInput2, timestamp3, raoInput3))); RaoParameters parameters = new RaoParameters(); @@ -72,20 +74,20 @@ void setUp() throws IOException { @Test void getRangeActionsPerTimestamp() { assertEquals( - Map.of(timestamp1, Set.of(crac1.getRangeAction("pstBe - 1600")), - timestamp2, Set.of(crac1.getRangeAction("pstBe - 1600"), crac2.getRangeAction("pstBe - 1700")), - timestamp3, Set.of(crac1.getRangeAction("pstBe - 1600"), crac2.getRangeAction("pstBe - 1700"), crac3.getRangeAction("pstBe - 1800"), crac3.getRangeAction("pstDe - 1800"))), - sensitivityAnalysis.getRangeActionsPerTimestamp()); + Map.of(timestamp1, Set.of(crac1.getRangeAction("pstBe - 1600")), + timestamp2, Set.of(crac1.getRangeAction("pstBe - 1600"), crac2.getRangeAction("pstBe - 1700")), + timestamp3, Set.of(crac1.getRangeAction("pstBe - 1600"), crac2.getRangeAction("pstBe - 1700"), crac3.getRangeAction("pstBe - 1800"), crac3.getRangeAction("pstDe - 1800"))), + sensitivityAnalysis.getRangeActionsPerTimestamp()); } @Test void getFlowCnecsPerTimestamp() { assertEquals( - Map.of(timestamp1, Set.of(crac1.getFlowCnec("cnecDeNlPrev - 1600"), crac1.getFlowCnec("cnecDeNlOut - 1600")), - timestamp2, Set.of(crac2.getFlowCnec("cnecDeNlPrev - 1700"), crac2.getFlowCnec("cnecDeNlOut - 1700")), - timestamp3, Set.of(crac3.getFlowCnec("cnecDeNlPrev - 1800"), crac3.getFlowCnec("cnecDeNlOut - 1800"))), - sensitivityAnalysis.getFlowCnecsPerTimestamp()); + Map.of(timestamp1, Set.of(crac1.getFlowCnec("cnecDeNlPrev - 1600"), crac1.getFlowCnec("cnecDeNlOut - 1600")), + timestamp2, Set.of(crac2.getFlowCnec("cnecDeNlPrev - 1700"), crac2.getFlowCnec("cnecDeNlOut - 1700")), + timestamp3, Set.of(crac3.getFlowCnec("cnecDeNlPrev - 1800"), crac3.getFlowCnec("cnecDeNlOut - 1800"))), + sensitivityAnalysis.getFlowCnecsPerTimestamp()); }