Skip to content

Commit c509044

Browse files
committed
refactored is_update_independent
Signed-off-by: Santiago Figueroa Manrique <santiago.figueroa.manrique@alliander.com>
1 parent 5f94b0c commit c509044

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/update.hpp

+25-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
#include "../all_components.hpp"
1010
#include "../common/iterator_like_concepts.hpp"
11+
#include "../main_model_utils.hpp"
12+
13+
#include <map>
1114

1215
namespace power_grid_model::main_core {
1316

@@ -229,7 +232,28 @@ UpdateCompProperties check_component_independence(ConstDataset const& update_dat
229232
return properties;
230233
}
231234

232-
void validate_update_data_independence(UpdateCompProperties const& comp) {
235+
template <typename... ComponentType> using ComponentFlags = std::array<bool, sizeof...(ComponentType)>;
236+
237+
template <class... ComponentType>
238+
ComponentFlags<ComponentType...>
239+
is_update_independent(ConstDataset const& update_data,
240+
std::map<std::string, Idx, std::less<>> const& relevant_component_count_map) {
241+
ComponentFlags<ComponentType...> result{};
242+
size_t idx{};
243+
utils::run_functor_with_all_types_return_void<ComponentType...>(
244+
[&result, &relevant_component_count_map, &update_data, &idx]<typename CompType>() {
245+
Idx n_component{};
246+
auto it = relevant_component_count_map.find(CompType::name);
247+
if (it != relevant_component_count_map.end()) {
248+
n_component = it->second;
249+
}
250+
result[idx] = check_component_independence<CompType>(update_data, n_component).is_independent();
251+
++idx;
252+
});
253+
return result;
254+
}
255+
256+
inline void validate_update_data_independence(UpdateCompProperties const& comp) {
233257
if (comp.is_empty_component()) {
234258
return; // empty dataset is still supported
235259
}

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

+5-19
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
139139

140140
using OwnedUpdateDataset = std::tuple<std::vector<typename ComponentType::UpdateType>...>;
141141

142-
using UpdateCompIndependence = std::array<main_core::UpdateCompProperties, n_types>;
143-
using ComponentFlags = std::array<bool, n_types>;
142+
using ComponentFlags = std::array<bool, n_types>; // TODO: (figueroa1395) also in update.hpp ish
144143
using ComponentCountInBase = std::pair<std::string, Idx>;
145144

146145
// TODO: (figueroa1395) probably move to common.hpp or somewhere else
@@ -581,7 +580,10 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
581580

582581
// cache component update order where possible.
583582
// the order for a cacheable (independent) component by definition is the same across all scenarios
584-
auto const is_independent = is_update_independent(update_data);
583+
// TODO: (figueroa1395): don't use string lookup, it is slow
584+
std::map<std::string, Idx, std::less<>> const relevant_component_count_map = all_component_count();
585+
auto const is_independent =
586+
main_core::is_update_independent<ComponentType...>(update_data, relevant_component_count_map);
585587
all_scenarios_sequence = get_sequence_idx_map(update_data, 0, is_independent);
586588

587589
return [&base_model, &exceptions, &infos, &calculation_fn, &result_data, &update_data,
@@ -750,22 +752,6 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
750752
// TODO: (figueroa1395) may want to move this to update.hpp or make something similar there
751753
template <class Component> using UpdateType = typename Component::UpdateType;
752754

753-
UpdateCompIndependence check_components_independence(ConstDataset const& update_data) const {
754-
// check and return indenpendence of all components
755-
return main_core::utils::run_functor_with_all_types_return_array<ComponentType...>(
756-
[this, &update_data]<typename CT>() {
757-
auto const n_component = this->component_count<CT>();
758-
return main_core::check_component_independence<CT>(update_data, n_component);
759-
});
760-
}
761-
762-
ComponentFlags is_update_independent(ConstDataset const& update_data) {
763-
ComponentFlags result;
764-
std::ranges::transform(check_components_independence(update_data), result.begin(),
765-
[](auto const& comp) { return comp.is_independent(); });
766-
return result;
767-
}
768-
769755
// Calculate with optimization, e.g., automatic tap changer
770756
template <calculation_type_tag calculation_type, symmetry_tag sym> auto calculate(Options const& options) {
771757
auto const calculator = [this, &options] {

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_utils.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ template <class... Types, class Functor> constexpr auto run_functor_with_all_typ
1919
return std::array { functor.template operator()<Types>()... };
2020
}
2121

22-
} // namespace power_grid_model::main_core::utils
22+
} // namespace power_grid_model::main_core::utils
23+
// TODO: (figueroa1395) move to main_core under utils.hpp

0 commit comments

Comments
 (0)