Skip to content

Commit

Permalink
Fixed sensi computation in parallel
Browse files Browse the repository at this point in the history
Signed-off-by: CHEN Roxane <[email protected]>
  • Loading branch information
CHEN Roxane committed Dec 12, 2024
1 parent d14c18a commit 1a0e329
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <thomas.bouquet at rte-france.com>}
Expand All @@ -44,7 +47,20 @@ public InterTemporalSensitivityAnalysis(InterTemporalRaoInput input, RaoParamete
}

public TemporalData<LoadFlowAndSensitivityResult> runInitialSensitivityAnalysis() throws InterruptedException {
return new TemporalDataImpl<>(input.getTimestampsToRun().stream().collect(Collectors.toMap(Function.identity(), this::runForTimestamp)));
ForkJoinPool timestampPool = new ForkJoinPool(input.getTimestampsToRun().size());
List<ForkJoinTask<Pair<OffsetDateTime, LoadFlowAndSensitivityResult>>> tasks = input.getTimestampsToRun().stream().map(timestamp ->
timestampPool.submit(() -> Pair.of(timestamp, runForTimestamp(timestamp)))
).toList();
Map<OffsetDateTime, LoadFlowAndSensitivityResult> loadFlowAndSensitivityResultPerTimestamp = new HashMap<>();
for (ForkJoinTask<Pair<OffsetDateTime, LoadFlowAndSensitivityResult>> task : tasks) {
try {
Pair<OffsetDateTime, LoadFlowAndSensitivityResult> taskResult = task.get();
loadFlowAndSensitivityResultPerTimestamp.put(taskResult.getFirst(), taskResult.getSecond());
} catch (ExecutionException e) {
throw new OpenRaoException(e);
}
}
return new TemporalDataImpl<>(loadFlowAndSensitivityResultPerTimestamp);
}

Map<OffsetDateTime, Set<RangeAction<?>>> getRangeActionsPerTimestamp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());

}

Expand Down

0 comments on commit 1a0e329

Please sign in to comment.