Skip to content

Commit d30ae63

Browse files
authored
Merge pull request #1119 from PowerGridModel/feature/clean-main-core-ybus-templates
Cleanup main model: Clean up templates and variadics from y bus
2 parents 0d96300 + b78b0fe commit d30ae63

File tree

4 files changed

+64
-80
lines changed

4 files changed

+64
-80
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/main_model_type.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ class MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
112112
using SequenceIdxRefWrappers = std::array<std::reference_wrapper<std::vector<Idx2D> const>, n_types>;
113113
using ComponentFlags = std::array<bool, n_types>;
114114

115-
// Clean these 2. They are unused
116-
static constexpr auto branch_param_in_seq_map =
117-
std::array{index_of_component<Line>, index_of_component<Link>, index_of_component<Transformer>};
118-
static constexpr auto shunt_param_in_seq_map = std::array{index_of_component<Shunt>};
119-
120115
template <class Functor> static constexpr void run_functor_with_all_component_types_return_void(Functor&& functor) {
121116
(std::forward<Functor>(functor).template operator()<ComponentType>(), ...);
122117
}

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/y_bus.hpp

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,56 @@
88

99
namespace power_grid_model::main_core {
1010

11-
template <symmetry_tag sym, typename ComponentContainer, typename... ComponentType>
12-
void prepare_y_bus(MainModelState<ComponentContainer> const& state_, Idx n_math_solvers_, MathState& math_state_) {
11+
namespace detail {
12+
template <std::derived_from<Branch> ComponentType, typename ComponentContainer>
13+
constexpr void add_to_increment(std::vector<MathModelParamIncrement>& increments,
14+
MainModelState<ComponentContainer> const& state, Idx2D const& changed_component_idx) {
15+
Idx2D const math_idx =
16+
state.topo_comp_coup->branch[main_core::get_component_sequence_idx<Branch>(state, changed_component_idx)];
17+
if (math_idx.group == isolated_component) {
18+
return;
19+
}
20+
// assign parameters
21+
increments[math_idx.group].branch_param_to_change.push_back(math_idx.pos);
22+
}
23+
24+
template <std::derived_from<Branch3> ComponentType, typename ComponentContainer>
25+
constexpr void add_to_increment(std::vector<MathModelParamIncrement>& increments,
26+
MainModelState<ComponentContainer> const& state, Idx2D const& changed_component_idx) {
27+
Idx2DBranch3 const math_idx =
28+
state.topo_comp_coup->branch3[main_core::get_component_sequence_idx<Branch3>(state, changed_component_idx)];
29+
if (math_idx.group == isolated_component) {
30+
return;
31+
}
32+
// assign parameters, branch3 param consists of three branch parameters
33+
for (size_t branch2 = 0; branch2 < 3; ++branch2) {
34+
increments[math_idx.group].branch_param_to_change.push_back(math_idx.pos[branch2]);
35+
}
36+
}
37+
38+
template <std::same_as<Shunt> ComponentType, typename ComponentContainer>
39+
constexpr void add_to_increment(std::vector<MathModelParamIncrement>& increments,
40+
MainModelState<ComponentContainer> const& state, Idx2D const& changed_component_idx) {
41+
Idx2D const math_idx =
42+
state.topo_comp_coup->shunt[main_core::get_component_sequence_idx<Shunt>(state, changed_component_idx)];
43+
if (math_idx.group == isolated_component) {
44+
return;
45+
}
46+
// assign parameters
47+
increments[math_idx.group].shunt_param_to_change.push_back(math_idx.pos);
48+
}
49+
50+
// default implementation for other components, does nothing
51+
template <typename ComponentType, typename ComponentContainer>
52+
constexpr void add_to_increment(std::vector<MathModelParamIncrement> const& /* increments */,
53+
MainModelState<ComponentContainer> const& /* state */,
54+
Idx2D const& /* changed_component_idx */) {
55+
// default implementation is no-op
56+
}
57+
} // namespace detail
58+
59+
template <symmetry_tag sym, typename MainModelType>
60+
void prepare_y_bus(typename MainModelType::MainModelState const& state_, Idx n_math_solvers_, MathState& math_state_) {
1361
std::vector<YBus<sym>>& y_bus_vec = main_core::get_y_bus<sym>(math_state_);
1462
// also get the vector of other Y_bus (sym -> asym, or asym -> sym)
1563
std::vector<YBus<other_symmetry_t<sym>>>& other_y_bus_vec =
@@ -20,14 +68,6 @@ void prepare_y_bus(MainModelState<ComponentContainer> const& state_, Idx n_math_
2068
y_bus_vec.reserve(n_math_solvers_);
2169
auto math_params = get_math_param<sym>(state_, n_math_solvers_);
2270

23-
// Check the branch and shunt indices
24-
constexpr auto branch_param_in_seq_map =
25-
std::array{main_core::utils::index_of_component<Line, ComponentType...>,
26-
main_core::utils::index_of_component<Link, ComponentType...>,
27-
main_core::utils::index_of_component<Transformer, ComponentType...>};
28-
constexpr auto shunt_param_in_seq_map =
29-
std::array{main_core::utils::index_of_component<Shunt, ComponentType...>};
30-
3171
for (Idx i = 0; i != n_math_solvers_; ++i) {
3272
// construct from existing Y_bus structure if possible
3373
if (other_y_bus_exist) {
@@ -38,66 +78,25 @@ void prepare_y_bus(MainModelState<ComponentContainer> const& state_, Idx n_math_
3878
y_bus_vec.emplace_back(state_.math_topology[i],
3979
std::make_shared<MathModelParam<sym> const>(std::move(math_params[i])));
4080
}
41-
42-
y_bus_vec.back().set_branch_param_idx(
43-
IdxVector{branch_param_in_seq_map.begin(), branch_param_in_seq_map.end()});
44-
y_bus_vec.back().set_shunt_param_idx(
45-
IdxVector{shunt_param_in_seq_map.begin(), shunt_param_in_seq_map.end()});
4681
}
4782
}
4883
}
4984

50-
template <symmetry_tag sym, typename ComponentContainer, typename... ComponentType>
51-
static std::vector<MathModelParamIncrement> get_math_param_increment(
52-
MainModelState<ComponentContainer>& received_state, Idx n_math_solvers_,
53-
std::array<std::vector<Idx2D>, main_core::utils::n_types<ComponentType...>> const& parameter_changed_components_) {
54-
using AddToIncrement =
55-
void (*)(std::vector<MathModelParamIncrement>&, MainModelState<ComponentContainer> const&, Idx2D const&);
56-
57-
static constexpr std::array<AddToIncrement, main_core::utils::n_types<ComponentType...>> add_to_increments{
58-
[](std::vector<MathModelParamIncrement>& increments, MainModelState<ComponentContainer> const& state,
59-
Idx2D const& changed_component_idx) {
60-
if constexpr (std::derived_from<ComponentType, Branch>) {
61-
Idx2D const math_idx =
62-
state.topo_comp_coup
63-
->branch[main_core::get_component_sequence_idx<Branch>(state, changed_component_idx)];
64-
if (math_idx.group == isolated_component) {
65-
return;
66-
}
67-
// assign parameters
68-
increments[math_idx.group].branch_param_to_change.push_back(math_idx.pos);
69-
} else if constexpr (std::derived_from<ComponentType, Branch3>) {
70-
Idx2DBranch3 const math_idx =
71-
state.topo_comp_coup
72-
->branch3[main_core::get_component_sequence_idx<Branch3>(state, changed_component_idx)];
73-
if (math_idx.group == isolated_component) {
74-
return;
75-
}
76-
// assign parameters, branch3 param consists of three branch parameters
77-
for (size_t branch2 = 0; branch2 < 3; ++branch2) {
78-
increments[math_idx.group].branch_param_to_change.push_back(math_idx.pos[branch2]);
79-
}
80-
} else if constexpr (std::same_as<ComponentType, Shunt>) {
81-
Idx2D const math_idx =
82-
state.topo_comp_coup
83-
->shunt[main_core::get_component_sequence_idx<Shunt>(state, changed_component_idx)];
84-
if (math_idx.group == isolated_component) {
85-
return;
86-
}
87-
// assign parameters
88-
increments[math_idx.group].shunt_param_to_change.push_back(math_idx.pos);
89-
}
90-
}...};
85+
template <typename MainModelType>
86+
static std::vector<MathModelParamIncrement>
87+
get_math_param_increment(typename MainModelType::MainModelState const& state, Idx n_math_solvers_,
88+
typename MainModelType::SequenceIdx const& parameter_changed_components_) {
9189

9290
std::vector<MathModelParamIncrement> math_param_increment(n_math_solvers_);
9391

94-
for (size_t i = 0; i < main_core::utils::n_types<ComponentType...>; ++i) {
95-
auto const& changed_type_components = parameter_changed_components_[i];
96-
auto const& add_type_to_increment = add_to_increments[i];
97-
for (auto const& changed_component : changed_type_components) {
98-
add_type_to_increment(math_param_increment, received_state, changed_component);
99-
}
100-
}
92+
MainModelType::run_functor_with_all_component_types_return_void(
93+
[&math_param_increment, &state, &parameter_changed_components_]<typename CompType>() {
94+
static constexpr auto comp_index = MainModelType::template index_of_component<CompType>;
95+
for (auto const& changed_component : std::get<comp_index>(parameter_changed_components_)) {
96+
detail::add_to_increment<CompType, typename MainModelType::ComponentContainer>(
97+
math_param_increment, state, changed_component);
98+
}
99+
});
101100

102101
return math_param_increment;
103102
}

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
594594
if (!is_topology_up_to_date_) {
595595
rebuild_topology();
596596
}
597-
main_core::prepare_y_bus<sym, ComponentContainer, ComponentType...>(state_, n_math_solvers_, math_state_);
597+
main_core::prepare_y_bus<sym, ModelType>(state_, n_math_solvers_, math_state_);
598598

599599
if (n_math_solvers_ != static_cast<Idx>(solvers.size())) {
600600
assert(solvers.empty());
@@ -616,8 +616,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
616616
std::vector<MathModelParam<sym>> const math_params =
617617
main_core::get_math_param<sym>(state_, n_math_solvers_);
618618
std::vector<MathModelParamIncrement> const math_param_increments =
619-
main_core::get_math_param_increment<sym, ComponentContainer, ComponentType...>(
620-
state_, n_math_solvers_, parameter_changed_components_);
619+
main_core::get_math_param_increment<ModelType>(state_, n_math_solvers_, parameter_changed_components_);
621620
if (last_updated_calculation_symmetry_mode_ == is_symmetric_v<sym>) {
622621
main_core::update_y_bus(math_state_, math_params, math_param_increments);
623622
} else {

power_grid_model_c/power_grid_model/include/power_grid_model/math_solver/y_bus.hpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,6 @@ template <symmetry_tag sym> class YBus {
324324

325325
constexpr auto& get_y_bus_structure() const { return y_bus_struct_; }
326326

327-
void set_branch_param_idx(IdxVector branch_param_idx) { branch_param_idx_ = std::move(branch_param_idx); }
328-
void set_shunt_param_idx(IdxVector shunt_param_idx) { shunt_param_idx_ = std::move(shunt_param_idx); }
329-
IdxVector const& get_branch_param_idx() const { return branch_param_idx_; }
330-
IdxVector const& get_shunt_param_idx() const { return shunt_param_idx_; }
331-
332327
void update_admittance(std::shared_ptr<MathModelParam<sym> const> const& math_model_param) {
333328
// overwrite the old cached parameters
334329
math_model_param_ = math_model_param;
@@ -524,10 +519,6 @@ template <symmetry_tag sym> class YBus {
524519
// cache the math parameters
525520
std::shared_ptr<MathModelParam<sym> const> math_model_param_;
526521

527-
// cache the branch and shunt parameters in sequence_idx_map
528-
IdxVector branch_param_idx_;
529-
IdxVector shunt_param_idx_;
530-
531522
// map index between admittance entries and parameter entries
532523
std::vector<IdxVector> map_admittance_param_branch_;
533524
std::vector<IdxVector> map_admittance_param_shunt_;

0 commit comments

Comments
 (0)