Skip to content

Commit

Permalink
[IIDM v1.2] Add convenient methods to run through connectables of a n…
Browse files Browse the repository at this point in the history
…etwork (#126) (#147)

Signed-off-by: Sébastien LAIGRE <slaigre@silicom.fr>
Signed-off-by: Mathieu BAGUE <mathieu.bague@rte-france.com>
  • Loading branch information
sebalaig authored Aug 10, 2020
1 parent 2444190 commit bf6435b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
26 changes: 26 additions & 0 deletions include/powsybl/iidm/Identifiable.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2020, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef POWSYBL_IIDM_IDENTIFIABLE_HXX
#define POWSYBL_IIDM_IDENTIFIABLE_HXX

#include <powsybl/iidm/Identifiable.hpp>

namespace powsybl {

namespace iidm {

template <typename T>
bool Identifiable::isInstanceOf(const Identifiable& identifiable) {
return stdcxx::isInstanceOf<T>(identifiable);
}

} // namespace iidm

} // namespace powsybl

#endif // POWSYBL_IIDM_IDENTIFIABLE_HXX
9 changes: 9 additions & 0 deletions include/powsybl/iidm/Network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ class Network : public Container, public VariantManagerHolder {

const stdcxx::DateTime& getCaseDate() 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 = Connectable, typename = typename std::enable_if<std::is_base_of<Connectable, T>::value>::type>
stdcxx::const_range<T> getConnectables() const;

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

unsigned long getCountryCount() const;

const DanglingLine& getDanglingLine(const std::string& id) const;
Expand Down
28 changes: 28 additions & 0 deletions include/powsybl/iidm/Network.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

#include <powsybl/iidm/Network.hpp>

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>

#include <powsybl/stdcxx/instanceof.hpp>

namespace powsybl {

namespace iidm {
Expand All @@ -29,6 +34,29 @@ T& Network::get(const std::string& id) {
return m_networkIndex.get<T>(id);
}

template <typename T, typename>
unsigned long Network::getConnectableCount() const {
return boost::size(getConnectables<T>());
}

template <typename T, typename>
stdcxx::const_range<T> Network::getConnectables() const {
const auto& filter = [](const Identifiable& identifiable) {
return stdcxx::isInstanceOf<T>(identifiable);
};

return m_networkIndex.getAll<Identifiable, Identifiable>() | boost::adaptors::filtered(filter) | boost::adaptors::transformed(map<const T>);
}

template <typename T, typename>
stdcxx::range<T> Network::getConnectables() {
const auto& filter = [](const Identifiable& identifiable) {
return stdcxx::isInstanceOf<T>(identifiable);
};

return m_networkIndex.getAll<Identifiable, Identifiable>() | boost::adaptors::filtered(filter) | boost::adaptors::transformed(map<T>);
}

template <typename T, typename>
unsigned long Network::getObjectCount() const {
return m_networkIndex.getObjectCount<T>();
Expand Down
44 changes: 44 additions & 0 deletions test/iidm/NetworkTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#include <boost/test/unit_test.hpp>

#include <powsybl/iidm/Bus.hpp>
#include <powsybl/iidm/DanglingLine.hpp>
#include <powsybl/iidm/DanglingLineAdder.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>
#include <powsybl/iidm/Substation.hpp>
Expand Down Expand Up @@ -365,6 +368,47 @@ BOOST_AUTO_TEST_CASE(country) {
BOOST_CHECK_EQUAL(1, network.getCountryCount());
}

BOOST_AUTO_TEST_CASE(getConnectablesTest) {
Network network = createTestNetwork();
const Network& cNetwork = network;

BOOST_CHECK_EQUAL(2, boost::size(network.getConnectables<Load>()));
BOOST_CHECK_EQUAL(2, boost::size(cNetwork.getConnectables<Load>()));
BOOST_CHECK_EQUAL(2UL, network.getConnectableCount<Load>());

BOOST_CHECK_EQUAL(2, boost::size(network.getConnectables<Line>()));
BOOST_CHECK_EQUAL(2, boost::size(cNetwork.getConnectables<Line>()));
BOOST_CHECK_EQUAL(2UL, network.getConnectableCount<Line>());

BOOST_CHECK_EQUAL(1, boost::size(network.getConnectables<TieLine>()));
BOOST_CHECK_EQUAL(1, boost::size(cNetwork.getConnectables<TieLine>()));
BOOST_CHECK_EQUAL(1UL, network.getConnectableCount<TieLine>());

BOOST_CHECK_EQUAL(1, boost::size(network.getConnectables<DanglingLine>()));
BOOST_CHECK_EQUAL(1, boost::size(cNetwork.getConnectables<DanglingLine>()));
BOOST_CHECK_EQUAL(1UL, network.getConnectableCount<DanglingLine>());

BOOST_CHECK_EQUAL(1, boost::size(network.getConnectables<TwoWindingsTransformer>()));
BOOST_CHECK_EQUAL(1, boost::size(cNetwork.getConnectables<TwoWindingsTransformer>()));
BOOST_CHECK_EQUAL(1UL, network.getConnectableCount<TwoWindingsTransformer>());

BOOST_CHECK_EQUAL(3, boost::size(network.getConnectables<Injection>()));
BOOST_CHECK_EQUAL(3, boost::size(cNetwork.getConnectables<Injection>()));
BOOST_CHECK_EQUAL(3UL, network.getConnectableCount<Injection>());

BOOST_CHECK_EQUAL(6UL, boost::size(network.getConnectables()));
BOOST_CHECK_EQUAL(6UL, boost::size(cNetwork.getConnectables()));
BOOST_CHECK_EQUAL(6UL, network.getConnectableCount());

BOOST_CHECK_EQUAL(3UL, boost::size(network.getConnectables<Branch>()));
BOOST_CHECK_EQUAL(3UL, boost::size(cNetwork.getConnectables<Branch>()));
BOOST_CHECK_EQUAL(3UL, network.getConnectableCount<Branch>());

BOOST_CHECK_EQUAL(0, boost::size(network.getConnectables<ThreeWindingsTransformer>()));
BOOST_CHECK_EQUAL(0, boost::size(cNetwork.getConnectables<ThreeWindingsTransformer>()));
BOOST_CHECK_EQUAL(0UL, network.getConnectableCount<ThreeWindingsTransformer>());
}

BOOST_AUTO_TEST_CASE(branch) {
Network network = createTestNetwork();
const Network& cNetwork = network;
Expand Down

0 comments on commit bf6435b

Please sign in to comment.