diff --git a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/util/TieLineUtil.java b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/util/TieLineUtil.java index 6a2c536fc95..3ee299cd67e 100644 --- a/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/util/TieLineUtil.java +++ b/iidm/iidm-api/src/main/java/com/powsybl/iidm/network/util/TieLineUtil.java @@ -273,4 +273,15 @@ private static boolean zeroImpedanceLine(BranchAdmittanceMatrix adm) { return adm.y21().getReal() == 0.0 && adm.y22().getImaginary() == 0.0; } } + + /** + *

Retrieve, if it exists, the dangling line paired to the given one.

+ * + * @param danglingLine a dangling line + * @return an Optional containing the dangling line paired to the given one + */ + public static Optional getPairedDanglingLine(DanglingLine danglingLine) { + return danglingLine.getTieLine().map(t -> + t.getDanglingLine1() == danglingLine ? t.getDanglingLine2() : t.getDanglingLine1()); + } } diff --git a/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractTieLineTest.java b/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractTieLineTest.java index 1f0c52c0067..054c86167e7 100644 --- a/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractTieLineTest.java +++ b/iidm/iidm-tck/src/test/java/com/powsybl/iidm/network/tck/AbstractTieLineTest.java @@ -12,10 +12,12 @@ import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory; import com.powsybl.iidm.network.test.NoEquipmentNetworkFactory; import com.powsybl.iidm.network.util.SV; +import com.powsybl.iidm.network.util.TieLineUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -91,6 +93,10 @@ public void testTieLineAdder() { assertFalse(network.getDanglingLines(DanglingLineFilter.PAIRED).iterator().hasNext()); assertEquals(List.of(dl1, dl2), network.getDanglingLines()); + // test paired dangling line retrieval - For unpaired dangling lines + assertFalse(TieLineUtil.getPairedDanglingLine(dl1).isPresent()); + assertFalse(TieLineUtil.getPairedDanglingLine(dl2).isPresent()); + TieLineAdder adder = network.newTieLine().setId("testTie") .setName("testNameTie") .setDanglingLine1(dl1.getId()) @@ -116,7 +122,6 @@ public void testTieLineAdder() { assertEquals(0.0285, tieLine.getB2(), tol); DanglingLine danglingLine1 = tieLine.getDanglingLine1(); - DanglingLine danglingLine2 = tieLine.getDanglingLine2(); // Check notification on DanglingLine changes @@ -160,20 +165,28 @@ public void testTieLineAdder() { double v2 = 380.0; double angle1 = -1e-4; double angle2 = -1.7e-3; - tieLine.getDanglingLine1().getTerminal().setP(p1).setQ(q1).getBusView().getBus().setV(v1).setAngle(angle1); - tieLine.getDanglingLine2().getTerminal().setP(p2).setQ(q2).getBusView().getBus().setV(v2).setAngle(angle2); + danglingLine1.getTerminal().setP(p1).setQ(q1).getBusView().getBus().setV(v1).setAngle(angle1); + danglingLine2.getTerminal().setP(p2).setQ(q2).getBusView().getBus().setV(v2).setAngle(angle2); // test boundaries values SV expectedSV1 = new SV(p1, q1, v1, angle1, Branch.Side.ONE); SV expectedSV2 = new SV(p2, q2, v2, angle2, Branch.Side.TWO); - assertEquals(expectedSV1.otherSideP(tieLine.getDanglingLine1(), true), tieLine.getDanglingLine1().getBoundary().getP(), 0.0d); - assertEquals(expectedSV1.otherSideQ(tieLine.getDanglingLine1(), true), tieLine.getDanglingLine1().getBoundary().getQ(), 0.0d); - assertEquals(expectedSV2.otherSideP(tieLine.getDanglingLine2(), true), tieLine.getDanglingLine2().getBoundary().getP(), 0.0d); - assertEquals(expectedSV2.otherSideQ(tieLine.getDanglingLine2(), true), tieLine.getDanglingLine2().getBoundary().getQ(), 0.0d); - assertEquals(expectedSV1.otherSideU(tieLine.getDanglingLine1(), true), tieLine.getDanglingLine1().getBoundary().getV(), 0.0d); - assertEquals(expectedSV1.otherSideA(tieLine.getDanglingLine1(), true), tieLine.getDanglingLine1().getBoundary().getAngle(), 0.0d); - assertEquals(expectedSV2.otherSideU(tieLine.getDanglingLine2(), true), tieLine.getDanglingLine2().getBoundary().getV(), 0.0d); - assertEquals(expectedSV2.otherSideA(tieLine.getDanglingLine2(), true), tieLine.getDanglingLine2().getBoundary().getAngle(), 0.0d); + assertEquals(expectedSV1.otherSideP(danglingLine1, true), danglingLine1.getBoundary().getP(), 0.0d); + assertEquals(expectedSV1.otherSideQ(danglingLine1, true), danglingLine1.getBoundary().getQ(), 0.0d); + assertEquals(expectedSV2.otherSideP(danglingLine2, true), danglingLine2.getBoundary().getP(), 0.0d); + assertEquals(expectedSV2.otherSideQ(danglingLine2, true), danglingLine2.getBoundary().getQ(), 0.0d); + assertEquals(expectedSV1.otherSideU(danglingLine1, true), danglingLine1.getBoundary().getV(), 0.0d); + assertEquals(expectedSV1.otherSideA(danglingLine1, true), danglingLine1.getBoundary().getAngle(), 0.0d); + assertEquals(expectedSV2.otherSideU(danglingLine2, true), danglingLine2.getBoundary().getV(), 0.0d); + assertEquals(expectedSV2.otherSideA(danglingLine2, true), danglingLine2.getBoundary().getAngle(), 0.0d); + + // test paired dangling line retrieval - For paired dangling lines + Optional otherSide1 = TieLineUtil.getPairedDanglingLine(danglingLine1); + assertTrue(otherSide1.isPresent()); + assertEquals(danglingLine2, otherSide1.orElseThrow()); + Optional otherSide2 = TieLineUtil.getPairedDanglingLine(danglingLine2); + assertTrue(otherSide2.isPresent()); + assertEquals(danglingLine1, otherSide2.orElseThrow()); } @Test