Skip to content

Commit

Permalink
Remove duplicated code in DecomposedFlowsRescaler, add test with prop…
Browse files Browse the repository at this point in the history
…ortional rescaler
  • Loading branch information
caioluke committed May 22, 2024
1 parent 9858634 commit 988f2ff
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2022, RTE (http://www.rte-france.com)
* 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/.
*/
package com.powsybl.flow_decomposition;

import com.powsybl.commons.PowsyblException;
import com.powsybl.iidm.network.Country;

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 Caio Luke {@literal <caio.luke at artelys.com>}
*/
final class DecomposedFlowsRescaler {

private static final double MIN_FLOW_TOLERANCE = 1E-6; // min flow in MW to rescale

private DecomposedFlowsRescaler() {
}

private static double reLU(double value) {
return value > 0 ? value : 0.;
}

private static double rescaleValue(double initialValue, double delta, double sumOfReLUFlows) {
return initialValue + delta * reLU(initialValue) / sumOfReLUFlows;
}

static DecomposedFlow rescale(DecomposedFlow decomposedFlow, FlowDecompositionParameters.RescaleMode rescaleMode) {
double acReferenceFlow = decomposedFlow.getAcReferenceFlow();

if (Double.isNaN(acReferenceFlow)) {
return decomposedFlow;
}

String branchId = decomposedFlow.getBranchId();
String contingencyId = decomposedFlow.getContingencyId();
Country country1 = decomposedFlow.getCountry1();
Country country2 = decomposedFlow.getCountry2();

double acMaxFlow = decomposedFlow.getAcMaxFlow();
double dcReferenceFlow = decomposedFlow.getDcReferenceFlow();
double allocatedFlow = decomposedFlow.getAllocatedFlow();
double xNodeFlow = decomposedFlow.getXNodeFlow();
double pstFlow = decomposedFlow.getPstFlow();
double internalFlow = decomposedFlow.getInternalFlow();
Map<String, Double> loopFlows = decomposedFlow.getLoopFlows();

switch (rescaleMode) {
case RELU -> {
double deltaToRescale = acReferenceFlow * Math.signum(acReferenceFlow) - decomposedFlow.getTotalFlow();
double sumOfReLUFlows = reLU(allocatedFlow) + reLU(pstFlow) + reLU(xNodeFlow) + loopFlows.values().stream().mapToDouble(DecomposedFlowsRescaler::reLU).sum() + reLU(internalFlow);

double rescaledAllocatedFlow = rescaleValue(allocatedFlow, deltaToRescale, sumOfReLUFlows);
double rescaledXNodeFlow = rescaleValue(xNodeFlow, deltaToRescale, sumOfReLUFlows);
double rescaledPstFlow = rescaleValue(pstFlow, deltaToRescale, sumOfReLUFlows);
double rescaleInternalFlow = rescaleValue(internalFlow, deltaToRescale, sumOfReLUFlows);
Map<String, Double> rescaledLoopFlows = loopFlows.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> rescaleValue(entry.getValue(), deltaToRescale, sumOfReLUFlows)));

return new DecomposedFlow(branchId, contingencyId, country1, country2, acReferenceFlow, acMaxFlow, dcReferenceFlow, rescaledAllocatedFlow, rescaledXNodeFlow, rescaledPstFlow, rescaleInternalFlow, rescaledLoopFlows);
}
case PROPORTIONAL -> {
// if dcReferenceFlow is too small, do not rescale
if (Math.abs(dcReferenceFlow) < MIN_FLOW_TOLERANCE) {
return decomposedFlow;
}
double rescaleFactor = Math.abs(acMaxFlow / dcReferenceFlow);

double rescaledAllocatedFlow = rescaleFactor * allocatedFlow;
double rescaledXNodeFlow = rescaleFactor * xNodeFlow;
double rescaledPstFlow = rescaleFactor * pstFlow;
double rescaleInternalFlow = rescaleFactor * internalFlow;
Map<String, Double> rescaledLoopFlows = loopFlows.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> rescaleFactor * entry.getValue()));

return new DecomposedFlow(branchId, contingencyId, country1, country2, acReferenceFlow, acMaxFlow, dcReferenceFlow, rescaledAllocatedFlow, rescaledXNodeFlow, rescaledPstFlow, rescaleInternalFlow, rescaledLoopFlows);
}
default -> throw new PowsyblException("Rescale mode not defined: " + rescaleMode);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,10 @@ private DecomposedFlow createDecomposedFlow(String branchId, Map<String, Double>
Country country2 = NetworkUtil.getTerminalCountry(xnecMap.get(branchId).getTerminal2());
double internalFlow = extractInternalFlow(loopFlowsMap, country1, country2);
DecomposedFlow decomposedFlow = new DecomposedFlow(branchId, contingencyId, country1, country2, acReferenceFlow.get(branchId), acMaxFlow.get(branchId), dcReferenceFlow.get(branchId), allocatedFlow, xNodeFlow, pstFlow, internalFlow, loopFlowsMap);
switch (rescaleMode) {
case RELU -> {
return DecomposedFlowsRescalerReLU.rescale(decomposedFlow);
}
case PROPORTIONAL -> {
return DecomposedFlowsRescalerProportional.rescale(decomposedFlow);
}
default -> {
return decomposedFlow;
}
if (rescaleMode.equals(FlowDecompositionParameters.RescaleMode.NONE)) {
return decomposedFlow;
} else {
return DecomposedFlowsRescaler.rescale(decomposedFlow, rescaleMode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private DecomposedFlow getRescaledFlow(double internalFlow, double acReferenceFl
DecomposedFlow decomposedFlow = getDecomposedFlow(internalFlow, acReferenceFlow, dcReferenceFlow);
assertEquals(Math.abs(dcReferenceFlow), decomposedFlow.getTotalFlow(), EPSILON);

return DecomposedFlowsRescalerReLU.rescale(decomposedFlow);
return DecomposedFlowsRescaler.rescale(decomposedFlow, FlowDecompositionParameters.RescaleMode.RELU);
}

private void checkRescaleAcReference(double acReferenceFlow, double dcReferenceFlow, DecomposedFlow rescaledFlow, double expectedAllocatedFlow, double expectedInternalFlow, double expectedPstFlow, double expectedLoopFlowBE, double expectedLoopFlowES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/**
* @author Sebastien Murgey {@literal <sebastien.murgey at rte-france.com>}
* @author Hugo Schindler {@literal <hugo.schindler at rte-france.com>}
* @author Caio Luke {@literal <caio.luke at artelys.com>}
*/
class RescalingTests {
private static final double EPSILON = 1e-5;
Expand Down Expand Up @@ -66,7 +67,7 @@ private DecomposedFlow getRescaledFlow(double acReferenceFlow, double dcReferenc
DecomposedFlow decomposedFlow = getDecomposedFlow(acReferenceFlow, dcReferenceFlow);
assertEquals(Math.abs(dcReferenceFlow), decomposedFlow.getTotalFlow(), EPSILON);

return DecomposedFlowsRescalerReLU.rescale(decomposedFlow);
return DecomposedFlowsRescaler.rescale(decomposedFlow, FlowDecompositionParameters.RescaleMode.RELU);
}

@Test
Expand Down Expand Up @@ -134,7 +135,7 @@ void testAcerNormalizationWithPositiveAbsoluteSmallerReferenceFlows() {
}

@Test
void testNormalizationWithFlowDecompositionResultsWithPstNetwork() {
void testReLUNormalizationWithFlowDecompositionResultsWithPstNetwork() {
String networkFileName = "NETWORK_PST_FLOW_WITH_COUNTRIES.uct";
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.RescaleMode.RELU);
}
Expand All @@ -145,6 +146,12 @@ void testNoNormalizationWithFlowDecompositionResultsWithPstNetwork() {
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.RescaleMode.NONE);
}

@Test
void testProportionalNormalizationWithFlowDecompositionResultsWithPstNetwork() {
String networkFileName = "NETWORK_PST_FLOW_WITH_COUNTRIES.uct";
testNormalizationWithFlowDecompositionResults(networkFileName, FlowDecompositionParameters.RescaleMode.PROPORTIONAL);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public static GlskDocument importGlskDocument(String glskFileName) {
public static void assertCoherenceTotalFlow(FlowDecompositionParameters.RescaleMode rescaleMode, FlowDecompositionResults flowDecompositionResults) {
for (String xnec : flowDecompositionResults.getDecomposedFlowMap().keySet()) {
DecomposedFlow decomposedFlow = flowDecompositionResults.getDecomposedFlowMap().get(xnec);
if (!rescaleMode.equals(FlowDecompositionParameters.RescaleMode.NONE)) {
assertEquals(Math.abs(decomposedFlow.getAcReferenceFlow()), Math.abs(decomposedFlow.getTotalFlow()), EPSILON);
} else {
assertEquals(Math.abs(decomposedFlow.getDcReferenceFlow()), Math.abs(decomposedFlow.getTotalFlow()), EPSILON);
switch (rescaleMode) {
case RELU -> assertEquals(Math.abs(decomposedFlow.getAcReferenceFlow()), Math.abs(decomposedFlow.getTotalFlow()), EPSILON);
case PROPORTIONAL -> assertEquals(Math.abs(decomposedFlow.getAcMaxFlow()), Math.abs(decomposedFlow.getTotalFlow()), EPSILON);
default -> assertEquals(Math.abs(decomposedFlow.getDcReferenceFlow()), Math.abs(decomposedFlow.getTotalFlow()), EPSILON);
}
}
}
Expand Down

0 comments on commit 988f2ff

Please sign in to comment.