diff --git a/balances-adjustment/src/main/java/com/powsybl/balances_adjustment/util/CountryArea.java b/balances-adjustment/src/main/java/com/powsybl/balances_adjustment/util/CountryArea.java index d6630a43..8c95de16 100644 --- a/balances-adjustment/src/main/java/com/powsybl/balances_adjustment/util/CountryArea.java +++ b/balances-adjustment/src/main/java/com/powsybl/balances_adjustment/util/CountryArea.java @@ -121,20 +121,24 @@ private boolean isAreaBorder(HvdcLine hvdcLine) { } private double getLeavingFlow(DanglingLine danglingLine) { - return danglingLine.getTerminal().isConnected() ? -danglingLine.getBoundary().getP() : 0; + return danglingLine.getTerminal().isConnected() ? zeroIfNan(-danglingLine.getBoundary().getP()) : 0; } private double getLeavingFlow(Line line) { - double flowSide1 = line.getTerminal1().isConnected() && !Double.isNaN(line.getTerminal1().getP()) ? line.getTerminal1().getP() : 0; - double flowSide2 = line.getTerminal2().isConnected() && !Double.isNaN(line.getTerminal2().getP()) ? line.getTerminal2().getP() : 0; + double flowSide1 = line.getTerminal1().isConnected() ? zeroIfNan(line.getTerminal1().getP()) : 0; + double flowSide2 = line.getTerminal2().isConnected() ? zeroIfNan(line.getTerminal2().getP()) : 0; double directFlow = (flowSide1 - flowSide2) / 2; return countries.contains(line.getTerminal1().getVoltageLevel().getSubstation().map(Substation::getNullableCountry).orElse(null)) ? directFlow : -directFlow; } private double getLeavingFlow(HvdcLine hvdcLine) { - double flowSide1 = hvdcLine.getConverterStation1().getTerminal().isConnected() && !Double.isNaN(hvdcLine.getConverterStation1().getTerminal().getP()) ? hvdcLine.getConverterStation1().getTerminal().getP() : 0; - double flowSide2 = hvdcLine.getConverterStation2().getTerminal().isConnected() && !Double.isNaN(hvdcLine.getConverterStation2().getTerminal().getP()) ? hvdcLine.getConverterStation2().getTerminal().getP() : 0; + double flowSide1 = hvdcLine.getConverterStation1().getTerminal().isConnected() ? zeroIfNan(hvdcLine.getConverterStation1().getTerminal().getP()) : 0; + double flowSide2 = hvdcLine.getConverterStation2().getTerminal().isConnected() ? zeroIfNan(hvdcLine.getConverterStation2().getTerminal().getP()) : 0; double directFlow = (flowSide1 - flowSide2) / 2; return countries.contains(hvdcLine.getConverterStation1().getTerminal().getVoltageLevel().getSubstation().map(Substation::getNullableCountry).orElse(null)) ? directFlow : -directFlow; } + + private static double zeroIfNan(double aPossiblyNanValue) { + return Double.isNaN(aPossiblyNanValue) ? 0 : aPossiblyNanValue; + } } diff --git a/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/CountryAreaTest.java b/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/CountryAreaTest.java index b2d826e9..8bc7a4ca 100644 --- a/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/CountryAreaTest.java +++ b/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/CountryAreaTest.java @@ -129,4 +129,31 @@ void testLeavingFlowToCountryWithTieLines() { assertEquals(0, countryAreaCH.getLeavingFlowToCountry(countryAreaDE), EPSILON); assertEquals(-699, countryAreaIT.getLeavingFlowToCountry(countryAreaAT), EPSILON); } + + @Test + void testNetPositionIsZeroWhenDanglingLineBorderPIsNaN() { + Network testNetwork = Network.read("testCaseNanInNetPositionComputation.uct", NetworkAreaTest.class.getResourceAsStream("/testCaseNanInNetPositionComputation.uct")); + NetworkAreaFactory countryAreaCH = new CountryAreaFactory(Country.CH); + NetworkAreaFactory countryAreaIT = new CountryAreaFactory(Country.IT); + assertEquals(0, countryAreaCH.create(testNetwork).getNetPosition(), 1e-3); + assertEquals(0, countryAreaIT.create(testNetwork).getNetPosition(), 1e-3); + } + + @Test + void testNetPositionPartOfLineIsZeroWhenLineIsDisconnected() { + Network network = Network.read("testCaseSpecialDevices.xiidm", getClass().getResourceAsStream("/testCaseSpecialDevices.xiidm")); + network.getLine("LINE_FR_ES").getTerminal1().disconnect(); + network.getLine("LINE_FR_ES").getTerminal2().disconnect(); + assertEquals(50, countryAreaFactoryFR.create(network).getNetPosition(), 1e-3); + assertEquals(-50, countryAreaFactoryES.create(network).getNetPosition(), 1e-3); + } + + @Test + void testNetPositionPartOfHvdcIsZeroWhenHvdcIsDisconnected() { + Network network = Network.read("testCaseSpecialDevices.xiidm", getClass().getResourceAsStream("/testCaseSpecialDevices.xiidm")); + network.getHvdcLine("HVDC_FR_ES").getConverterStation1().getTerminal().disconnect(); + network.getHvdcLine("HVDC_FR_ES").getConverterStation2().getTerminal().disconnect(); + assertEquals(50, countryAreaFactoryFR.create(network).getNetPosition(), 1e-3); + assertEquals(-50, countryAreaFactoryES.create(network).getNetPosition(), 1e-3); + } } diff --git a/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/NetworkAreaTest.java b/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/NetworkAreaTest.java index ff7f29e0..5a464f12 100644 --- a/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/NetworkAreaTest.java +++ b/balances-adjustment/src/test/java/com/powsybl/balances_adjustment/util/NetworkAreaTest.java @@ -8,7 +8,6 @@ import com.powsybl.iidm.network.Country; import com.powsybl.iidm.network.Network; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -18,24 +17,12 @@ */ class NetworkAreaTest { - private Network testNetwork1; - private NetworkAreaFactory countryAreaFR; - - private NetworkAreaFactory voltageLevelsArea1; - - @BeforeEach - void setUp() { - testNetwork1 = Network.read("testCase.xiidm", NetworkAreaTest.class.getResourceAsStream("/testCase.xiidm")); - - voltageLevelsArea1 = new VoltageLevelsAreaFactory("FFR1AA1", "FFR3AA1"); - - countryAreaFR = new CountryAreaFactory(Country.FR); - - } - @Test void testGetNetPosition() { - assertEquals(countryAreaFR.create(testNetwork1).getNetPosition(), voltageLevelsArea1.create(testNetwork1).getNetPosition(), 1e-3); + Network testNetwork = Network.read("testCase.xiidm", NetworkAreaTest.class.getResourceAsStream("/testCase.xiidm")); + NetworkAreaFactory voltageLevelsArea = new VoltageLevelsAreaFactory("FFR1AA1", "FFR3AA1"); + NetworkAreaFactory countryAreaFR = new CountryAreaFactory(Country.FR); + assertEquals(countryAreaFR.create(testNetwork).getNetPosition(), voltageLevelsArea.create(testNetwork).getNetPosition(), 1e-3); } } diff --git a/balances-adjustment/src/test/resources/testCaseNanInNetPositionComputation.uct b/balances-adjustment/src/test/resources/testCaseNanInNetPositionComputation.uct new file mode 100644 index 00000000..31fc9d56 --- /dev/null +++ b/balances-adjustment/src/test/resources/testCaseNanInNetPositionComputation.uct @@ -0,0 +1,12 @@ +##C 2007.05.01 +Manually made test case to fix NaN issue in net position computation +##N +##ZCH +S 21 0 0 0.00000 0.00000 0.00000 0.00000 +##ZIT +I 22 0 0 0.00000 0.00000 0.00000 0.00000 +##ZXX +X_S_I_21 0 0 0.00000 0.00000 0.00000 0.00000 +##L +S 21 X_S_I_21 1 8 2.0000 10.000 100.0000 9999 +X_S_I_21 I 22 1 0 2.0000 10.000 100.0000 9999