Skip to content

Commit

Permalink
Fix error in arguments order DecomposedFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
caioluke committed May 22, 2024
1 parent d5a6d1d commit 9858634
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 83 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DecomposedFlow {
static final String PST_COLUMN_NAME = "PST Flow";
static final String INTERNAL_COLUMN_NAME = "Internal Flow";

protected DecomposedFlow(String branchId, String contingencyId, Country country1, Country country2, double acMaxFlow, double acReferenceFlow, double dcReferenceFlow, double allocatedFlow, double xNodeFlow, double pstFlow, double internalFlow, Map<String, Double> loopFlowsMap) {
protected DecomposedFlow(String branchId, String contingencyId, Country country1, Country country2, double acReferenceFlow, double acMaxFlow, double dcReferenceFlow, double allocatedFlow, double xNodeFlow, double pstFlow, double internalFlow, Map<String, Double> loopFlowsMap) {
this.branchId = branchId;
this.contingencyId = contingencyId;
this.country1 = country1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 Sebastien Murgey {@literal <sebastien.murgey at rte-france.com>}
* @author Hugo Schindler {@literal <hugo.schindler at rte-france.com>}
* @author Guillaume Verger {@literal <guillaume.verger at artelys.com>}
* @author Caio Luke {@literal <caio.luke at artelys.com>}
*/
public final class FlowComputerUtils {

private FlowComputerUtils() {
// empty constructor
}

public static Map<String, Double> calculateAcReferenceFlows(Collection<Branch> xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) {
if (loadFlowServiceAcResult.fallbackHasBeenActivated()) {
return xnecList.stream().collect(Collectors.toMap(Identifiable::getId, branch -> Double.NaN));
}
return getReferenceFlow(xnecList);
}

public static Map<String, Double> calculateAcMaxFlows(Collection<Branch> xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) {
if (loadFlowServiceAcResult.fallbackHasBeenActivated()) {
return xnecList.stream().collect(Collectors.toMap(Identifiable::getId, branch -> Double.NaN));
}
return getMaxFlow(xnecList);
}

public static Map<String, Double> getReferenceFlow(Collection<Branch> xnecList) {
return xnecList.stream()
.collect(Collectors.toMap(
Identifiable::getId,
FlowComputerUtils::getReferenceP
));
}

public static Map<String, Double> getMaxFlow(Collection<Branch> xnecList) {
return xnecList.stream()
.collect(Collectors.toMap(
Identifiable::getId,
branch -> Math.max(Math.abs(branch.getTerminal1().getP()), Math.abs(branch.getTerminal2().getP()))
));
}

private static double getReferenceP(Branch branch) {
return branch.getTerminal1().getP();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private LoadFlowRunningService.Result runAcLoadFlow(Network network) {
}

private void saveAcReferenceFlow(FlowDecompositionResults.PerStateBuilder flowDecompositionResultBuilder, Set<Branch> xnecList, LoadFlowRunningService.Result loadFlowServiceAcResult) {
Map<String, Double> acReferenceFlows = new AcReferenceFlowComputer().run(xnecList, loadFlowServiceAcResult);
Map<String, Double> acReferenceFlows = FlowComputerUtils.calculateAcReferenceFlows(xnecList, loadFlowServiceAcResult);
flowDecompositionResultBuilder.saveAcReferenceFlow(acReferenceFlows);
}

Expand All @@ -193,10 +193,6 @@ private Map<Country, Double> getZonesNetPosition(Network network) {
return netPositionComputer.run(network);
}

private Map<String, Double> getBranchReferenceFlows(Set<Branch> branches) {
return new ReferenceFlowComputer().run(branches);
}

private void compensateLosses(Network network) {
if (parameters.isLossesCompensationEnabled()) {
lossesCompensator.run(network);
Expand All @@ -216,7 +212,7 @@ private SparseMatrixWithIndexesTriplet getNodalInjectionsMatrix(Network network,
}

private void saveDcReferenceFlow(FlowDecompositionResults.PerStateBuilder flowDecompositionResultBuilder, Set<Branch> xnecList) {
flowDecompositionResultBuilder.saveDcReferenceFlow(getBranchReferenceFlows(xnecList));
flowDecompositionResultBuilder.saveDcReferenceFlow(FlowComputerUtils.getReferenceFlow(xnecList));
}

private SensitivityAnalyser getSensitivityAnalyser(Network network, NetworkMatrixIndexes networkMatrixIndexes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public void computedAcFlows(Network network, Result loadFlowServiceAcResult) {
return;
}

Map<String, Double> acFlows =
new AcReferenceFlowComputer().run(network.getBranchStream().toList(), loadFlowServiceAcResult);
Map<String, Double> acFlows = FlowComputerUtils.calculateAcReferenceFlows(network.getBranchStream().toList(), loadFlowServiceAcResult);

for (FlowDecompositionObserver o : observers) {
o.computedAcFlows(acFlows);
Expand All @@ -100,7 +99,7 @@ public void computedDcFlows(Network network) {
return;
}

Map<String, Double> dcFlows = new ReferenceFlowComputer().run(network.getBranchStream().toList());
Map<String, Double> dcFlows = FlowComputerUtils.getReferenceFlow(network.getBranchStream().toList());

for (FlowDecompositionObserver o : observers) {
o.computedDcFlows(dcFlows);
Expand Down Expand Up @@ -144,6 +143,6 @@ private void sendMatrix(MatrixNotification notification, SparseMatrixWithIndexes

@FunctionalInterface
private interface MatrixNotification {
public void sendMatrix(FlowDecompositionObserver o, Map<String, Map<String, Double>> matrix);
void sendMatrix(FlowDecompositionObserver o, Map<String, Map<String, Double>> matrix);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ void checkFlowDecompositionWorksOnCgmesFile() {
.setEnableLossesCompensation(FlowDecompositionParameters.ENABLE_LOSSES_COMPENSATION)
.setLossesCompensationEpsilon(FlowDecompositionParameters.DISABLE_LOSSES_COMPENSATION_EPSILON)
.setSensitivityEpsilon(FlowDecompositionParameters.DISABLE_SENSITIVITY_EPSILON)
.setRescaleEnabled(FlowDecompositionParameters.ENABLE_RESCALED_RESULTS);
.setRescaleMode(FlowDecompositionParameters.RescaleMode.RELU);
String xnecId = "044cd006-c766-11e1-8775-005056c00008";
XnecProvider xnecProvider = XnecProviderByIds.builder().addNetworkElementsOnBasecase(Set.of(xnecId)).build();
FlowDecompositionComputer flowDecompositionComputer = new FlowDecompositionComputer(flowDecompositionParameters);
FlowDecompositionResults flowDecompositionResults = flowDecompositionComputer.run(xnecProvider, network);
assertNotNull(flowDecompositionResults.getDecomposedFlowMap().get(xnecId));
assertEquals(1, flowDecompositionResults.getDecomposedFlowMap().size());
TestUtils.assertCoherenceTotalFlow(flowDecompositionParameters.isRescaleEnabled(), flowDecompositionResults);
TestUtils.assertCoherenceTotalFlow(flowDecompositionParameters.getRescaleMode(), flowDecompositionResults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ private DecomposedFlow getDecomposedFlow(double acReferenceFlow, double dcRefere
loopFlows.put(NetworkUtil.getLoopFlowIdFromCountry(Country.ES), 700.);
Country country1 = Country.FR;
Country country2 = Country.FR;
return new DecomposedFlow("", "", country1, country2, acReferenceFlow, dcReferenceFlow, allocatedFlow, 0, pstFlow, internalFlow, loopFlows);
return new DecomposedFlow("", "", country1, country2, acReferenceFlow, acReferenceFlow, dcReferenceFlow, allocatedFlow, 0, pstFlow, internalFlow, loopFlows);
}

private DecomposedFlow getRescaledFlow(double acReferenceFlow, double dcReferenceFlow) {
DecomposedFlow decomposedFlow = getDecomposedFlow(acReferenceFlow, dcReferenceFlow);
assertEquals(Math.abs(dcReferenceFlow), decomposedFlow.getTotalFlow(), EPSILON);

return DecomposedFlowsRescaler.rescale(decomposedFlow);
return DecomposedFlowsRescalerReLU.rescale(decomposedFlow);
}

@Test
Expand Down Expand Up @@ -136,29 +136,29 @@ void testAcerNormalizationWithPositiveAbsoluteSmallerReferenceFlows() {
@Test
void testNormalizationWithFlowDecompositionResultsWithPstNetwork() {
String networkFileName = "NETWORK_PST_FLOW_WITH_COUNTRIES.uct";
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.ENABLE_RESCALED_RESULTS);
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.RescaleMode.RELU);
}

@Test
void testNoNormalizationWithFlowDecompositionResultsWithPstNetwork() {
String networkFileName = "NETWORK_PST_FLOW_WITH_COUNTRIES.uct";
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.DISABLE_RESCALED_RESULTS);
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.RescaleMode.NONE);
}

static void testNormalizationWithFlowDecompositionResults(String networkFileName, boolean enableRescaledResults) {
static void testNormalizationWithFlowDecompositionResults(String networkFileName, FlowDecompositionParameters.RescaleMode rescaleMode) {
Network network = TestUtils.importNetwork(networkFileName);

FlowDecompositionParameters flowDecompositionParameters = new FlowDecompositionParameters()
.setEnableLossesCompensation(FlowDecompositionParameters.ENABLE_LOSSES_COMPENSATION)
.setLossesCompensationEpsilon(FlowDecompositionParameters.DISABLE_LOSSES_COMPENSATION_EPSILON)
.setSensitivityEpsilon(FlowDecompositionParameters.DISABLE_SENSITIVITY_EPSILON)
.setRescaleEnabled(enableRescaledResults);
.setRescaleMode(rescaleMode);

FlowDecompositionComputer flowDecompositionComputer = new FlowDecompositionComputer(flowDecompositionParameters);
XnecProvider xnecProvider = new XnecProviderAllBranches();
FlowDecompositionResults flowDecompositionResults = flowDecompositionComputer.run(xnecProvider, network);

TestUtils.assertCoherenceTotalFlow(enableRescaledResults, flowDecompositionResults);
TestUtils.assertCoherenceTotalFlow(rescaleMode, flowDecompositionResults);
}

@Test
Expand All @@ -171,7 +171,7 @@ void testRescalingDoesNotOccurWhenAcDiverge() {
.setEnableLossesCompensation(FlowDecompositionParameters.ENABLE_LOSSES_COMPENSATION)
.setLossesCompensationEpsilon(FlowDecompositionParameters.DISABLE_LOSSES_COMPENSATION_EPSILON)
.setSensitivityEpsilon(FlowDecompositionParameters.DISABLE_SENSITIVITY_EPSILON)
.setRescaleEnabled(FlowDecompositionParameters.ENABLE_RESCALED_RESULTS);
.setRescaleMode(FlowDecompositionParameters.RescaleMode.RELU);

FlowDecompositionComputer flowDecompositionComputer = new FlowDecompositionComputer(flowDecompositionParameters);
XnecProvider xnecProvider = XnecProviderByIds.builder().addNetworkElementsOnBasecase(Set.of(xnecId)).build();
Expand Down

0 comments on commit 9858634

Please sign in to comment.