Skip to content

Commit 776d3c2

Browse files
authored
Merge pull request #1092 from PowerGridModel/feture/refactor-topology-main-model
Clean-up main model: move topology related construction
2 parents 791a4ca + 79c1da4 commit 776d3c2

File tree

12 files changed

+238
-171
lines changed

12 files changed

+238
-171
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/container.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "common/common.hpp"
1010
#include "common/exception.hpp"
1111
#include "common/iterator_facade.hpp"
12+
#include "container_fwd.hpp"
1213

1314
#include <boost/range.hpp>
1415

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
#pragma once
6+
7+
#include "common/common.hpp"
8+
9+
#include <concepts>
10+
11+
namespace power_grid_model::common {
12+
13+
namespace detail {
14+
template <typename ContainerType, typename RetrievableType>
15+
concept single_component_container_c = requires(ContainerType const& c, ID id, Idx2D idx2d) {
16+
{ c.template citer<RetrievableType>().begin() } -> std::forward_iterator;
17+
{ c.template citer<RetrievableType>().end() } -> std::forward_iterator;
18+
{ *(c.template citer<RetrievableType>().begin()) } -> std::same_as<RetrievableType const&>;
19+
{
20+
c.template citer<RetrievableType>().end()
21+
} -> std::same_as<decltype(c.template citer<RetrievableType>().begin())>;
22+
{ c.template get_item<RetrievableType>(id) } -> std::convertible_to<RetrievableType const&>;
23+
{ c.template size<RetrievableType>() } -> std::same_as<Idx>;
24+
{ c.template get_seq<RetrievableType>(idx2d) } -> std::same_as<Idx>;
25+
};
26+
} // namespace detail
27+
28+
template <typename ContainerType, typename... RetrievableType>
29+
concept component_container_c = (detail::single_component_container_c<ContainerType, RetrievableType> && ...);
30+
31+
} // namespace power_grid_model::common
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
#pragma once
6+
7+
#include "../container.hpp"
8+
9+
namespace power_grid_model::main_core {
10+
11+
// TODO Reconfirm if there is duplication with state_queries and if we can remove either one of them
12+
13+
template <typename ComponentType, class ComponentContainer>
14+
requires common::component_container_c<ComponentContainer, ComponentType>
15+
constexpr auto get_component_size(ComponentContainer const& components) {
16+
return components.template size<ComponentType>();
17+
}
18+
19+
template <typename ComponentType, class ComponentContainer>
20+
requires common::component_container_c<ComponentContainer, ComponentType>
21+
inline Idx get_component_sequence_idx(ComponentContainer const& components, auto const& id_or_index) {
22+
return components.template get_seq<ComponentType>(id_or_index);
23+
}
24+
25+
} // namespace power_grid_model::main_core

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/state.hpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,9 @@ template <class CompContainer> struct MainModelState {
2828
template <class StateType>
2929
concept main_model_state_c = std::same_as<StateType, MainModelState<typename StateType::ComponentContainer>>;
3030

31-
template <typename ContainerType, typename ComponentType>
32-
concept component_container_c = requires(ContainerType const& c, ID id) {
33-
{ c.template citer<ComponentType>().begin() } -> std::forward_iterator;
34-
{ c.template citer<ComponentType>().end() } -> std::forward_iterator;
35-
{ *(c.template citer<ComponentType>().begin()) } -> std::same_as<ComponentType const&>;
36-
{ *(c.template citer<ComponentType>().end()) } -> std::same_as<ComponentType const&>;
37-
{ c.template get_item<ComponentType>(id) } -> std::convertible_to<ComponentType const&>;
38-
};
39-
4031
template <template <typename T> class StateType, typename ContainerType, typename ComponentType>
4132
concept model_component_state_c =
42-
component_container_c<typename StateType<ContainerType>::ComponentContainer, ComponentType> &&
33+
common::component_container_c<typename StateType<ContainerType>::ComponentContainer, ComponentType> &&
4334
std::same_as<StateType<ContainerType>, MainModelState<ContainerType>>;
4435

4536
} // namespace power_grid_model::main_core

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/state_queries.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../all_components.hpp"
1010

1111
namespace power_grid_model::main_core {
12+
1213
template <typename ComponentType, class ComponentContainer>
1314
requires model_component_state_c<MainModelState, ComponentContainer, ComponentType>
1415
inline Idx get_component_type_index(MainModelState<ComponentContainer> const& state) {

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/topology.hpp

Lines changed: 145 additions & 101 deletions
Large diffs are not rendered by default.

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
314314
construction_complete_ = true;
315315
#endif // !NDEBUG
316316
state_.components.set_construction_complete();
317-
construct_topology();
318-
}
319-
320-
void construct_topology() {
321-
ComponentTopology comp_topo;
322-
main_core::register_topology_components<Node>(state_, comp_topo);
323-
main_core::register_topology_components<Branch>(state_, comp_topo);
324-
main_core::register_topology_components<Branch3>(state_, comp_topo);
325-
main_core::register_topology_components<Source>(state_, comp_topo);
326-
main_core::register_topology_components<Shunt>(state_, comp_topo);
327-
main_core::register_topology_components<GenericLoadGen>(state_, comp_topo);
328-
main_core::register_topology_components<GenericVoltageSensor>(state_, comp_topo);
329-
main_core::register_topology_components<GenericPowerSensor>(state_, comp_topo);
330-
main_core::register_topology_components<GenericCurrentSensor>(state_, comp_topo);
331-
main_core::register_topology_components<Regulator>(state_, comp_topo);
332-
state_.comp_topo = std::make_shared<ComponentTopology const>(std::move(comp_topo));
317+
state_.comp_topo = std::make_shared<ComponentTopology const>(main_core::construct_topology(state_.components));
333318
}
334319

335320
void reset_solvers() {
@@ -653,33 +638,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
653638
assert(construction_complete_);
654639
// clear old solvers
655640
reset_solvers();
656-
// get connection info
657-
ComponentConnections comp_conn;
658-
comp_conn.branch_connected.resize(state_.comp_topo->branch_node_idx.size());
659-
comp_conn.branch_phase_shift.resize(state_.comp_topo->branch_node_idx.size());
660-
comp_conn.branch3_connected.resize(state_.comp_topo->branch3_node_idx.size());
661-
comp_conn.branch3_phase_shift.resize(state_.comp_topo->branch3_node_idx.size());
662-
comp_conn.source_connected.resize(state_.comp_topo->source_node_idx.size());
663-
std::transform(
664-
state_.components.template citer<Branch>().begin(), state_.components.template citer<Branch>().end(),
665-
comp_conn.branch_connected.begin(), [](Branch const& branch) {
666-
return BranchConnected{static_cast<IntS>(branch.from_status()), static_cast<IntS>(branch.to_status())};
667-
});
668-
std::transform(state_.components.template citer<Branch>().begin(),
669-
state_.components.template citer<Branch>().end(), comp_conn.branch_phase_shift.begin(),
670-
[](Branch const& branch) { return branch.phase_shift(); });
671-
std::transform(
672-
state_.components.template citer<Branch3>().begin(), state_.components.template citer<Branch3>().end(),
673-
comp_conn.branch3_connected.begin(), [](Branch3 const& branch3) {
674-
return Branch3Connected{static_cast<IntS>(branch3.status_1()), static_cast<IntS>(branch3.status_2()),
675-
static_cast<IntS>(branch3.status_3())};
676-
});
677-
std::transform(state_.components.template citer<Branch3>().begin(),
678-
state_.components.template citer<Branch3>().end(), comp_conn.branch3_phase_shift.begin(),
679-
[](Branch3 const& branch3) { return branch3.phase_shift(); });
680-
std::transform(state_.components.template citer<Source>().begin(),
681-
state_.components.template citer<Source>().end(), comp_conn.source_connected.begin(),
682-
[](Source const& source) { return source.status(); });
641+
ComponentConnections const comp_conn = main_core::construct_components_connections(state_.components);
683642
// re build
684643
Topology topology{*state_.comp_topo, comp_conn};
685644
std::tie(state_.math_topology, state_.topo_comp_coup) = topology.build_topology();

power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/optimizer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ constexpr auto get_optimizer(OptimizerType optimizer_type, OptimizerStrategy str
2929
case automatic_tap_adjustment:
3030
if constexpr (detail::steady_state_calculator_c<StateCalculator, State> &&
3131
std::invocable<std::remove_cvref_t<StateUpdater>, ConstDataset const&> &&
32-
main_core::component_container_c<typename State::ComponentContainer, TransformerTapRegulator>) {
32+
common::component_container_c<typename State::ComponentContainer, TransformerTapRegulator>) {
3333
return BaseOptimizer::template make_shared<TapPositionOptimizer<StateCalculator, StateUpdater, State>>(
3434
std::move(calculator), std::move(updater), strategy, meta_data, search);
3535
}

power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ template <transformer_c... TransformerTypes> struct TapRegulatorRef {
471471
};
472472

473473
template <typename State>
474-
requires main_core::component_container_c<typename State::ComponentContainer, TransformerTapRegulator>
474+
requires common::component_container_c<typename State::ComponentContainer, TransformerTapRegulator>
475475
TransformerTapRegulator const& find_regulator(State const& state, ID regulated_object) {
476476
auto const regulators = get_component_citer<TransformerTapRegulator>(state);
477477

@@ -503,7 +503,7 @@ template <typename... Ts> struct transformer_types_s<std::tuple<std::tuple<Ts...
503503
template <typename... Ts> using transformer_types_t = typename transformer_types_s<std::tuple<Ts...>>::type;
504504

505505
template <transformer_c... TransformerTypes, typename State>
506-
requires(main_core::component_container_c<typename State::ComponentContainer, TransformerTypes> && ...)
506+
requires(common::component_container_c<typename State::ComponentContainer, TransformerTypes> && ...)
507507
inline TapRegulatorRef<TransformerTypes...> regulator_mapping(State const& state, Idx2D const& transformer_index) {
508508
using ResultType = TapRegulatorRef<TransformerTypes...>;
509509
using IsType = bool (*)(Idx2D const&);
@@ -536,7 +536,7 @@ inline TapRegulatorRef<TransformerTypes...> regulator_mapping(State const& state
536536
}
537537

538538
template <transformer_c... TransformerTypes, typename State>
539-
requires(main_core::component_container_c<typename State::ComponentContainer, TransformerTypes> && ...)
539+
requires(common::component_container_c<typename State::ComponentContainer, TransformerTypes> && ...)
540540
inline auto regulator_mapping(State const& state, std::vector<Idx2D> const& order) {
541541
std::vector<TapRegulatorRef<TransformerTypes...>> result;
542542
result.reserve(order.size());
@@ -549,7 +549,7 @@ inline auto regulator_mapping(State const& state, std::vector<Idx2D> const& orde
549549
}
550550

551551
template <transformer_c... TransformerTypes, typename State>
552-
requires(main_core::component_container_c<typename State::ComponentContainer, TransformerTypes> && ...)
552+
requires(common::component_container_c<typename State::ComponentContainer, TransformerTypes> && ...)
553553
inline auto regulator_mapping(State const& state, RankedTransformerGroups const& order) {
554554
std::vector<std::vector<TapRegulatorRef<TransformerTypes...>>> result;
555555
result.reserve(order.size());
@@ -598,15 +598,15 @@ inline auto i_pu(std::vector<SolverOutputType> const& solver_output, Idx2DBranch
598598

599599
template <component_c ComponentType, typename... RegulatedTypes, typename State,
600600
steady_state_solver_output_type SolverOutputType>
601-
requires main_core::component_container_c<typename State::ComponentContainer, ComponentType>
601+
requires common::component_container_c<typename State::ComponentContainer, ComponentType>
602602
inline auto i_pu_controlled_node(TapRegulatorRef<RegulatedTypes...> const& regulator, State const& state,
603603
std::vector<SolverOutputType> const& solver_output) {
604604
auto const& branch_math_id = get_math_id<ComponentType>(state, regulator.transformer.topology_index());
605605
return i_pu<ComponentType>(solver_output, branch_math_id, regulator.regulator.get().control_side());
606606
}
607607

608608
template <transformer_c ComponentType, typename State, steady_state_solver_output_type SolverOutputType>
609-
requires main_core::component_container_c<typename State::ComponentContainer, ComponentType> &&
609+
requires common::component_container_c<typename State::ComponentContainer, ComponentType> &&
610610
requires(State const& state, Idx const i) {
611611
{ get_branch_nodes<ComponentType>(state, i)[i] } -> std::convertible_to<Idx>;
612612
}
@@ -619,15 +619,15 @@ inline auto u_pu(State const& state, std::vector<SolverOutputType> const& solver
619619

620620
template <component_c ComponentType, typename... RegulatedTypes, typename State,
621621
steady_state_solver_output_type SolverOutputType>
622-
requires main_core::component_container_c<typename State::ComponentContainer, ComponentType>
622+
requires common::component_container_c<typename State::ComponentContainer, ComponentType>
623623
inline auto u_pu_controlled_node(TapRegulatorRef<RegulatedTypes...> const& regulator, State const& state,
624624
std::vector<SolverOutputType> const& solver_output) {
625625
return u_pu<ComponentType>(state, solver_output, regulator.transformer.topology_index(),
626626
regulator.regulator.get().control_side());
627627
}
628628

629629
template <component_c ComponentType, typename... RegulatedTypes, typename State>
630-
requires main_core::component_container_c<typename State::ComponentContainer, ComponentType>
630+
requires common::component_container_c<typename State::ComponentContainer, ComponentType>
631631
inline bool is_regulated_transformer_connected(TapRegulatorRef<RegulatedTypes...> const& regulator,
632632
State const& state) {
633633
auto const controlled_node_idx = get_topo_node<ComponentType>(state, regulator.transformer.topology_index(),
@@ -702,7 +702,7 @@ class RankIteration {
702702
template <typename... T> class TapPositionOptimizerImpl;
703703
template <transformer_c... TransformerTypes, typename StateCalculator, typename StateUpdater_, typename State_,
704704
typename TransformerRanker_>
705-
requires(main_core::component_container_c<typename State_::ComponentContainer, TransformerTypes> && ...) &&
705+
requires(common::component_container_c<typename State_::ComponentContainer, TransformerTypes> && ...) &&
706706
detail::steady_state_calculator_c<StateCalculator, State_> &&
707707
std::invocable<std::remove_cvref_t<StateUpdater_>, ConstDataset const&> &&
708708
requires(TransformerRanker_ const& ranker, State_ const& state) {

tests/cpp_unit_tests/test_container.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ TEST_CASE("Test component container") {
223223
CHECK(const_container.get_id_by_idx(Idx2D{2, 0}) == 3);
224224
}
225225
#endif // NDEBUG
226+
227+
SUBCASE("Component Container concept") {
228+
static_assert(common::component_container_c<CompContainer, C>);
229+
static_assert(common::component_container_c<CompContainer, C1>);
230+
static_assert(common::component_container_c<CompContainer, C2>);
231+
static_assert(common::component_container_c<CompContainer, C, C1>);
232+
static_assert(common::component_container_c<CompContainer, C1, C2>);
233+
static_assert(common::component_container_c<CompContainer, C, C1, C2>);
234+
static_assert(common::component_container_c<CompContainer2, C>);
235+
static_assert(common::component_container_c<CompContainer2, C1>);
236+
static_assert(common::component_container_c<CompContainer2, C2>);
237+
static_assert(common::component_container_c<CompContainer2, C, C1>);
238+
static_assert(common::component_container_c<CompContainer2, C1, C2>);
239+
static_assert(common::component_container_c<CompContainer2, C, C1, C2>);
240+
}
226241
}
227242

228243
} // namespace power_grid_model

0 commit comments

Comments
 (0)