From 96ff8a6ad55986e5df4b55210f22d4f54b2a59b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Laigre?= Date: Wed, 12 Aug 2020 14:56:29 +0200 Subject: [PATCH] [IIDM V1.2] Removed duplicates in VoltageLevel::getConnectables (#46) (#143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Sébastien LAIGRE --- include/powsybl/iidm/VoltageLevel.hpp | 8 ++-- include/powsybl/iidm/VoltageLevel.hxx | 10 ++--- src/iidm/Substation.cpp | 12 +----- src/iidm/VoltageLevel.cpp | 7 ---- test/iidm/VoltageLevelTest.cpp | 58 +++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 28 deletions(-) diff --git a/include/powsybl/iidm/VoltageLevel.hpp b/include/powsybl/iidm/VoltageLevel.hpp index ec8de3e36..5d6792307 100644 --- a/include/powsybl/iidm/VoltageLevel.hpp +++ b/include/powsybl/iidm/VoltageLevel.hpp @@ -85,15 +85,13 @@ class VoltageLevel : public Container, public MultiVariantObject { template ::value>::type> stdcxx::Reference getConnectable(const std::string& id); - template ::value>::type> - unsigned long getConnectableCount() const; - + template ::value>::type> unsigned long getConnectableCount() const; - template ::value>::type> + template ::value>::type> stdcxx::const_range getConnectables() const; - template ::value>::type> + template ::value>::type> stdcxx::range getConnectables(); unsigned long getDanglingLineCount() const; diff --git a/include/powsybl/iidm/VoltageLevel.hxx b/include/powsybl/iidm/VoltageLevel.hxx index a5651db41..c322fd072 100644 --- a/include/powsybl/iidm/VoltageLevel.hxx +++ b/include/powsybl/iidm/VoltageLevel.hxx @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -70,20 +71,17 @@ stdcxx::Reference VoltageLevel::getConnectable(const std::string& id) { template unsigned long VoltageLevel::getConnectableCount() const { - const auto& terminals = getTerminals(); - return std::count_if(std::begin(terminals), std::end(terminals), [](const Terminal& terminal) { - return Terminal::isInstanceOf(terminal); - }); + return boost::size(getConnectables()); } template stdcxx::const_range VoltageLevel::getConnectables() const { - return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf) | boost::adaptors::transformed(Terminal::map); + return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf) | boost::adaptors::transformed(Terminal::map) | boost::adaptors::filtered(DistinctPredicate()); } template stdcxx::range VoltageLevel::getConnectables() { - return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf) | boost::adaptors::transformed(Terminal::map); + return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf) | boost::adaptors::transformed(Terminal::map) | boost::adaptors::filtered(DistinctPredicate()); } } // namespace iidm diff --git a/src/iidm/Substation.cpp b/src/iidm/Substation.cpp index a74c8ffe7..889fbccdf 100644 --- a/src/iidm/Substation.cpp +++ b/src/iidm/Substation.cpp @@ -54,11 +54,7 @@ Network& Substation::getNetwork() { } unsigned long Substation::getThreeWindingsTransformerCount() const { - unsigned long res = 0UL; - for (const auto& it : m_voltageLevels) { - res += it.get().getConnectableCount(); - } - return res / 3UL; + return boost::size(getThreeWindingsTransformers()); } stdcxx::const_range Substation::getThreeWindingsTransformers() const { @@ -86,11 +82,7 @@ const std::string& Substation::getTso() const { } unsigned long Substation::getTwoWindingsTransformerCount() const { - unsigned long res = 0UL; - for (const auto& it : m_voltageLevels) { - res += it.get().getConnectableCount(); - } - return res / 2UL; + return boost::size(getTwoWindingsTransformers()); } stdcxx::const_range Substation::getTwoWindingsTransformers() const { diff --git a/src/iidm/VoltageLevel.cpp b/src/iidm/VoltageLevel.cpp index 7e56edffe..56aa64e34 100644 --- a/src/iidm/VoltageLevel.cpp +++ b/src/iidm/VoltageLevel.cpp @@ -53,13 +53,6 @@ stdcxx::range VoltageLevel::getBatteries() { return getConnectables(); } -unsigned long VoltageLevel::getConnectableCount() const { - const auto& terminals = getTerminals(); - return std::count_if(std::begin(terminals), std::end(terminals), [](const Terminal& terminal) { - return terminal.getConnectable(); - }); -} - unsigned long VoltageLevel::getDanglingLineCount() const { return getConnectableCount(); } diff --git a/test/iidm/VoltageLevelTest.cpp b/test/iidm/VoltageLevelTest.cpp index 4994d3bb9..0b334c4ff 100644 --- a/test/iidm/VoltageLevelTest.cpp +++ b/test/iidm/VoltageLevelTest.cpp @@ -7,8 +7,11 @@ #include +#include #include #include +#include +#include #include #include #include @@ -26,6 +29,51 @@ namespace powsybl { namespace iidm { +Network createLineTestNetwork2() { + Network network("test", "test"); + Substation& substation = network.newSubstation() + .setId("S1") + .setName("S1_NAME") + .setCountry(Country::FR) + .setTso("TSO") + .add(); + + VoltageLevel& vl1 = substation.newVoltageLevel() + .setId("VL1") + .setName("VL1_NAME") + .setTopologyKind(TopologyKind::BUS_BREAKER) + .setNominalVoltage(380.0) + .setLowVoltageLimit(340.0) + .setHighVoltageLimit(420.0) + .add(); + + Bus& vl1Bus1 = vl1.getBusBreakerView().newBus() + .setId("VL1_BUS1") + .add(); + + Bus& vl1Bus2 = vl1.getBusBreakerView().newBus() + .setId("VL1_BUS2") + .add(); + + network.newLine() + .setId("VL1_VL3") + .setVoltageLevel1(vl1.getId()) + .setBus1(vl1Bus1.getId()) + .setConnectableBus1(vl1Bus1.getId()) + .setVoltageLevel2(vl1.getId()) + .setBus2(vl1Bus2.getId()) + .setConnectableBus2(vl1Bus2.getId()) + .setR(3.0) + .setX(33.0) + .setG1(1.0) + .setB1(0.2) + .setG2(2.0) + .setB2(0.4) + .add(); + + return network; +} + BOOST_AUTO_TEST_SUITE(VoltageLevelTestSuite) BOOST_AUTO_TEST_CASE(constructor) { @@ -145,6 +193,16 @@ BOOST_AUTO_TEST_CASE(integrity) { POWSYBL_ASSERT_REF_FALSE(vl.getConnectable("LOAD1")); } +BOOST_AUTO_TEST_CASE(getConnectablesCheckUnique) { + const Network network = createLineTestNetwork2(); + const VoltageLevel& vl = network.getVoltageLevel("VL1"); + BOOST_CHECK_EQUAL(1UL, boost::size(vl.getConnectables())); + BOOST_CHECK_EQUAL(1UL, vl.getConnectableCount()); + + BOOST_CHECK_EQUAL(1UL, boost::size(vl.getConnectables())); + BOOST_CHECK_EQUAL(1UL, vl.getConnectableCount()); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace iidm