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

[IIDM V1.2] Removed duplicates in VoltageLevel::getConnectables (#46) #143

Merged
merged 5 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 3 additions & 5 deletions include/powsybl/iidm/VoltageLevel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,13 @@ class VoltageLevel : public Container, public MultiVariantObject {
template <typename T, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
stdcxx::Reference<T> getConnectable(const std::string& id);

template <typename T, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
unsigned long getConnectableCount() const;

template <typename T = Connectable, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
unsigned long getConnectableCount() const;

template <typename T, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
template <typename T = Connectable, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
stdcxx::const_range<T> getConnectables() const;

template <typename T, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
template <typename T = Connectable, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
stdcxx::range<T> getConnectables();

unsigned long getDanglingLineCount() const;
Expand Down
10 changes: 4 additions & 6 deletions include/powsybl/iidm/VoltageLevel.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <powsybl/iidm/PhaseTapChanger.hpp>
#include <powsybl/iidm/RatioTapChanger.hpp>
#include <powsybl/iidm/ThreeWindingsTransformer.hpp>
#include <powsybl/iidm/util/DistinctPredicate.hpp>
#include <powsybl/stdcxx/instanceof.hpp>
#include <powsybl/stdcxx/memory.hpp>

Expand Down Expand Up @@ -70,20 +71,17 @@ stdcxx::Reference<T> VoltageLevel::getConnectable(const std::string& id) {

template <typename T, typename>
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<T>(terminal);
});
return boost::size(getConnectables<T>());
}

template <typename T, typename>
stdcxx::const_range<T> VoltageLevel::getConnectables() const {
return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf<T>) | boost::adaptors::transformed(Terminal::map<const T>);
return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf<T>) | boost::adaptors::transformed(Terminal::map<const T>) | boost::adaptors::filtered(DistinctPredicate());
}

template <typename T, typename>
stdcxx::range<T> VoltageLevel::getConnectables() {
return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf<T>) | boost::adaptors::transformed(Terminal::map<T>);
return getTerminals() | boost::adaptors::filtered(Terminal::isInstanceOf<T>) | boost::adaptors::transformed(Terminal::map<T>) | boost::adaptors::filtered(DistinctPredicate());
}

} // namespace iidm
Expand Down
12 changes: 2 additions & 10 deletions src/iidm/Substation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ThreeWindingsTransformer>();
}
return res / 3UL;
return boost::size(getThreeWindingsTransformers());
}

stdcxx::const_range<ThreeWindingsTransformer> Substation::getThreeWindingsTransformers() const {
Expand Down Expand Up @@ -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<TwoWindingsTransformer>();
}
return res / 2UL;
return boost::size(getTwoWindingsTransformers());
}

stdcxx::const_range<TwoWindingsTransformer> Substation::getTwoWindingsTransformers() const {
Expand Down
7 changes: 0 additions & 7 deletions src/iidm/VoltageLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ stdcxx::range<Battery> VoltageLevel::getBatteries() {
return getConnectables<Battery>();
}

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<DanglingLine>();
}
Expand Down
58 changes: 58 additions & 0 deletions test/iidm/VoltageLevelTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

#include <boost/test/unit_test.hpp>

#include <powsybl/iidm/Bus.hpp>
#include <powsybl/iidm/Generator.hpp>
#include <powsybl/iidm/Injection.hpp>
#include <powsybl/iidm/Line.hpp>
#include <powsybl/iidm/LineAdder.hpp>
#include <powsybl/iidm/Load.hpp>
#include <powsybl/iidm/LoadAdder.hpp>
#include <powsybl/iidm/Network.hpp>
Expand All @@ -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) {
Expand Down Expand Up @@ -145,6 +193,16 @@ BOOST_AUTO_TEST_CASE(integrity) {
POWSYBL_ASSERT_REF_FALSE(vl.getConnectable<Generator>("LOAD1"));
}

BOOST_AUTO_TEST_CASE(getConnectablesCheckUnique) {
const Network network = createLineTestNetwork2();
const VoltageLevel& vl = network.getVoltageLevel("VL1");
BOOST_CHECK_EQUAL(1UL, boost::size(vl.getConnectables<Line>()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you check that vl.getConnectableCount() also return 1?

Copy link
Contributor Author

@sebalaig sebalaig Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it fails... I have to patch getConnectableCount() also

BOOST_CHECK_EQUAL(1UL, vl.getConnectableCount<Line>());

BOOST_CHECK_EQUAL(1UL, boost::size(vl.getConnectables()));
BOOST_CHECK_EQUAL(1UL, vl.getConnectableCount());
}

BOOST_AUTO_TEST_SUITE_END()

} // namespace iidm
Expand Down