Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a check to avoid a nan when calculating the leaving flow from danglingline #120

Merged
merged 5 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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