88
99namespace 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, ¶meter_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}
0 commit comments