From 157cf994c74b84e136f854516617265355c78819 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 15 May 2024 14:53:40 +0200 Subject: [PATCH 01/39] Added transformer regulator sanity check Signed-off-by: Jerry Guo --- .../include/power_grid_model/common/common.hpp | 8 ++++++++ .../include/power_grid_model/common/exception.hpp | 7 +++++++ .../include/power_grid_model/main_core/input.hpp | 11 +++++++++++ .../include/power_grid_model/main_model.hpp | 1 + 4 files changed, 27 insertions(+) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp index 8dd154758..12e3cdfba 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp @@ -30,6 +30,14 @@ struct Idx2D { friend constexpr bool operator==(Idx2D x, Idx2D y) = default; }; +struct Idx2DHash { + std::size_t operator()(const Idx2D& idx) const { + std::size_t h1 = std::hash{}(idx.group); + std::size_t h2 = std::hash{}(idx.pos); + return h1 ^ (h2 << 1); + } +}; + struct symmetric_t {}; struct asymmetric_t {}; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp index dfd21a4e4..9c59ca5b7 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp @@ -118,6 +118,13 @@ class InvalidRegulatedObject : public PowerGridError { } }; +class DuplicatedRegulatedObject : public PowerGridError { + public: + DuplicatedRegulatedObject() { + append_msg("There are objects regulated by more than one regulator. Maximum one regulator is allowed."); + } +}; + class AutomaticTapCalculationError : public PowerGridError { public: AutomaticTapCalculationError(ID id) { diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp index e2fc86ae0..8f8290bad 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp @@ -9,6 +9,8 @@ #include "../all_components.hpp" +#include + namespace power_grid_model::main_core { constexpr std::array const branch3_sides = {Branch3Side::side_1, Branch3Side::side_2, @@ -22,6 +24,8 @@ template Component, class ComponentContainer, std::forw inline void add_component(MainModelState& state, ForwardIterator begin, ForwardIterator end, double system_frequency) { reserve_component(state, std::distance(begin, end)); + // do sanity check on the transformer tap regulator + std::vector regulated_objects; // loop to add component for (auto it = begin; it != end; ++it) { auto const& input = *it; @@ -96,6 +100,7 @@ inline void add_component(MainModelState& state, ForwardIter emplace_component(state, id, input); } else if constexpr (std::derived_from) { Idx2D const regulated_object_idx = get_component_idx_by_id(state, input.regulated_object); + regulated_objects.push_back(regulated_object_idx); ID const regulated_terminal = [&input, &state, ®ulated_object_idx] { using enum ControlSide; @@ -158,6 +163,12 @@ inline void add_component(MainModelState& state, ForwardIter emplace_component(state, id, input, regulated_object_type, u_rated); } } + // Make sure that each regulated object has at most one regulator + std::unordered_set unique_regulated_objects(regulated_objects.begin(), regulated_objects.end()); + if (unique_regulated_objects.size() != regulated_objects.size()) { + // There are duplicates + throw DuplicatedRegulatedObject(); + } } } // namespace power_grid_model::main_core diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp index f237721fd..b0ab7a3b4 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp @@ -670,6 +670,7 @@ class MainModelImpl, ComponentLis OptimizerType::no_optimization, OptimizerStrategy::any, calculate_power_flow_(err_tol, max_iter), [this](ConstDataset update_data) { this->update_component(update_data); }) ->optimize(state_, calculation_method); + // if (opt ) return MathOutput>{.solver_output = std::move(result_pf), .optimizer_output = {}}; } From 412e43bf440f9c899561afec0413bb62d47705ee Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 15 May 2024 15:14:05 +0200 Subject: [PATCH 02/39] const Signed-off-by: Jerry Guo --- .../include/power_grid_model/common/common.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp index 12e3cdfba..2d1c80893 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp @@ -32,8 +32,8 @@ struct Idx2D { struct Idx2DHash { std::size_t operator()(const Idx2D& idx) const { - std::size_t h1 = std::hash{}(idx.group); - std::size_t h2 = std::hash{}(idx.pos); + const std::size_t h1 = std::hash{}(idx.group); + const std::size_t h2 = std::hash{}(idx.pos); return h1 ^ (h2 << 1); } }; From 497c78de3f3041bbb2e7f103d8b445a1a46543c8 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 15 May 2024 15:26:10 +0200 Subject: [PATCH 03/39] one couldn't be too const Signed-off-by: Jerry Guo --- .../include/power_grid_model/main_core/input.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp index 8f8290bad..fe087b528 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp @@ -164,7 +164,7 @@ inline void add_component(MainModelState& state, ForwardIter } } // Make sure that each regulated object has at most one regulator - std::unordered_set unique_regulated_objects(regulated_objects.begin(), regulated_objects.end()); + const std::unordered_set unique_regulated_objects(regulated_objects.begin(), regulated_objects.end()); if (unique_regulated_objects.size() != regulated_objects.size()) { // There are duplicates throw DuplicatedRegulatedObject(); From a126c8a50e9115bb6f641820c8d391ee91b466b7 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 15 May 2024 15:36:04 +0200 Subject: [PATCH 04/39] nor is format Signed-off-by: Jerry Guo --- .../include/power_grid_model/main_core/input.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp index fe087b528..55fd7bda6 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp @@ -164,7 +164,8 @@ inline void add_component(MainModelState& state, ForwardIter } } // Make sure that each regulated object has at most one regulator - const std::unordered_set unique_regulated_objects(regulated_objects.begin(), regulated_objects.end()); + const std::unordered_set unique_regulated_objects(regulated_objects.begin(), + regulated_objects.end()); if (unique_regulated_objects.size() != regulated_objects.size()) { // There are duplicates throw DuplicatedRegulatedObject(); From dab428dd5b29957a7e402af9c885b40c4df23b67 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Thu, 16 May 2024 16:12:15 +0200 Subject: [PATCH 05/39] made optimization type and strategy configurable Signed-off-by: Jerry Guo --- .../include/power_grid_model/common/exception.hpp | 4 ++-- .../include/power_grid_model/main_core/input.hpp | 2 +- .../include/power_grid_model/main_model.hpp | 15 +++++++++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp index 9c59ca5b7..6b005fd82 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/common/exception.hpp @@ -118,9 +118,9 @@ class InvalidRegulatedObject : public PowerGridError { } }; -class DuplicatedRegulatedObject : public PowerGridError { +class DuplicativelyRegulatedObject : public PowerGridError { public: - DuplicatedRegulatedObject() { + DuplicativelyRegulatedObject() { append_msg("There are objects regulated by more than one regulator. Maximum one regulator is allowed."); } }; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp index 55fd7bda6..01bcddb36 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp @@ -168,7 +168,7 @@ inline void add_component(MainModelState& state, ForwardIter regulated_objects.end()); if (unique_regulated_objects.size() != regulated_objects.size()) { // There are duplicates - throw DuplicatedRegulatedObject(); + throw DuplicativelyRegulatedObject(); } } diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp index b0ab7a3b4..c22e759e7 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp @@ -91,6 +91,10 @@ class MainModelImpl, ComponentLis struct permanent_update_t : std::false_type {}; + struct OptmizationOptions { + OptimizerType optimizer_type{OptimizerType::no_optimization}; + OptimizerStrategy optimizer_strategy{OptimizerStrategy::any}; + }; // constructor with data explicit MainModelImpl(double system_frequency, ConstDataset const& input_data, Idx pos = 0) : system_frequency_{system_frequency} { @@ -665,12 +669,19 @@ class MainModelImpl, ComponentLis template auto calculate_power_flow(double err_tol, Idx max_iter, CalculationMethod calculation_method) { + const OptmizationOptions default_opt_options; + return calculate_power_flow(err_tol, max_iter, calculation_method, default_opt_options); + } + + template + auto calculate_power_flow(double err_tol, Idx max_iter, CalculationMethod calculation_method, + OptmizationOptions const& opt_options) { auto result_pf = optimizer::get_optimizer( - OptimizerType::no_optimization, OptimizerStrategy::any, calculate_power_flow_(err_tol, max_iter), + opt_options.optimizer_type, opt_options.optimizer_strategy, + calculate_power_flow_(err_tol, max_iter), [this](ConstDataset update_data) { this->update_component(update_data); }) ->optimize(state_, calculation_method); - // if (opt ) return MathOutput>{.solver_output = std::move(result_pf), .optimizer_output = {}}; } From 365ba60a8b5fa70630ac7f4a567e9b08b385a8c9 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Fri, 17 May 2024 17:31:21 +0200 Subject: [PATCH 06/39] getting transformer tap positions; msvc still complains Signed-off-by: Jerry Guo --- .../include/power_grid_model/main_model.hpp | 1 + .../optimizer/tap_position_optimizer.hpp | 38 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp index c22e759e7..82da3e118 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp @@ -683,6 +683,7 @@ class MainModelImpl, ComponentLis [this](ConstDataset update_data) { this->update_component(update_data); }) ->optimize(state_, calculation_method); return MathOutput>{.solver_output = std::move(result_pf), .optimizer_output = {}}; + // return result_pf; } // Single load flow calculation, propagating the results to result_data diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 5cf263e40..643d09384 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -36,6 +36,7 @@ using main_core::get_component; using TrafoGraphIdx = Idx; using EdgeWeight = int64_t; using RankedTransformerGroups = std::vector>; +using TransformerTapPositionResult = std::vector>; constexpr auto infty = std::numeric_limits::max(); constexpr Idx2D unregulated_idx = {-1, -1}; @@ -583,6 +584,31 @@ void add_tap_regulator_output(State const& state, } } +template +inline void get_transformer_tap_positions(State const& state, TransformerTapPositionResult& transformer_tap_positions) { + (get_transformer_tap_positions(state, transformer_tap_positions), ...); +} + +template Component, class ComponentContainer> + requires main_core::model_component_state_c +constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, + TransformerTapPositionResult& transformer_tap_positions) { + for (auto const& transformer : state.components.template citer()) { + transformer_tap_positions.push_back( + std::pair{static_cast(transformer.id()), static_cast(transformer.tap_pos())}); + } +} + +template Component, class ComponentContainer> + requires main_core::model_component_state_c +constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, + TransformerTapPositionResult& transformer_tap_positions) { + for (auto const& transformer : state.components.template citer()) { + transformer_tap_positions.push_back( + std::pair{static_cast(transformer.id()), static_cast(transformer.tap_pos())}); + } +} + template class TapPositionOptimizerImpl; template @@ -619,10 +645,18 @@ class TapPositionOptimizerImpl, StateCalculator, auto const order = regulator_mapping(state, TransformerRanker{}(state)); auto const cache = this->cache_states(order); - auto optimal_output = optimize(state, order, method); + auto solver_output = optimize(state, order, method); update_state(cache); - return optimal_output; + TransformerTapPositionResult transformer_tap_positions; + get_transformer_tap_positions(state, transformer_tap_positions); + + //using SolverOutputType = decltype(solver_output)::value_type; + //return MathOutput{ + // .solver_output = std::move(solver_output), + // .optimizer_output = std::move(transformer_tap_positions) + //}; + return solver_output; } constexpr auto get_strategy() const { return strategy_; } From bbd15f7bf5aaed16ebe60af4d719c9dff5f6f40c Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Fri, 17 May 2024 17:31:47 +0200 Subject: [PATCH 07/39] format Signed-off-by: Jerry Guo --- .../optimizer/tap_position_optimizer.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 643d09384..abaf8e327 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -651,11 +651,11 @@ class TapPositionOptimizerImpl, StateCalculator, TransformerTapPositionResult transformer_tap_positions; get_transformer_tap_positions(state, transformer_tap_positions); - //using SolverOutputType = decltype(solver_output)::value_type; - //return MathOutput{ - // .solver_output = std::move(solver_output), - // .optimizer_output = std::move(transformer_tap_positions) - //}; + // using SolverOutputType = decltype(solver_output)::value_type; + // return MathOutput{ + // .solver_output = std::move(solver_output), + // .optimizer_output = std::move(transformer_tap_positions) + // }; return solver_output; } From cbfada6048c290c68af859af4e7d4f9bb88b1f16 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Sat, 18 May 2024 15:48:04 +0200 Subject: [PATCH 08/39] the recursion as complained is impossible; werror suppressed Signed-off-by: Jerry Guo --- .../include/power_grid_model/main_model.hpp | 3 +- .../optimizer/tap_position_optimizer.hpp | 29 +++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp index 5ef3299ba..edeea43b7 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp @@ -684,8 +684,7 @@ class MainModelImpl, ComponentLis } template - auto calculate_power_flow(Options const& options, - OptmizationOptions const& opt_options) { + auto calculate_power_flow(Options const& options, OptmizationOptions const& opt_options) { auto result_pf = optimizer::get_optimizer( opt_options.optimizer_type, opt_options.optimizer_strategy, diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index abaf8e327..f3b901c55 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -584,11 +584,6 @@ void add_tap_regulator_output(State const& state, } } -template -inline void get_transformer_tap_positions(State const& state, TransformerTapPositionResult& transformer_tap_positions) { - (get_transformer_tap_positions(state, transformer_tap_positions), ...); -} - template Component, class ComponentContainer> requires main_core::model_component_state_c constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, @@ -609,6 +604,28 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState +inline void get_transformer_tap_positions(State const& state, TransformerTapPositionResult& transformer_tap_positions) { + using ComponentContainerType = typename State::ComponentContainer; + (get_transformer_tap_positions(state, transformer_tap_positions), ...); +} +#ifdef _MSC_VER +// MSVC compiler +#pragma warning(pop) +#elif defined(__GNUC__) || defined(__clang__) +// GCC or Clang compilers +#pragma GCC diagnostic pop +#endif + template class TapPositionOptimizerImpl; template @@ -649,7 +666,7 @@ class TapPositionOptimizerImpl, StateCalculator, update_state(cache); TransformerTapPositionResult transformer_tap_positions; - get_transformer_tap_positions(state, transformer_tap_positions); + get_transformer_tap_positions(state, transformer_tap_positions); // using SolverOutputType = decltype(solver_output)::value_type; // return MathOutput{ From a8990b2148b6fde960126e1e85fad356a414b109 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Sun, 19 May 2024 14:12:07 +0200 Subject: [PATCH 09/39] [skip ci] attempt fixing idx2d Signed-off-by: Jerry Guo --- .../optimizer/tap_position_optimizer.hpp | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index f3b901c55..fa70f0d84 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -584,23 +584,16 @@ void add_tap_regulator_output(State const& state, } } -template Component, class ComponentContainer> - requires main_core::model_component_state_c -constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, - TransformerTapPositionResult& transformer_tap_positions) { - for (auto const& transformer : state.components.template citer()) { - transformer_tap_positions.push_back( - std::pair{static_cast(transformer.id()), static_cast(transformer.tap_pos())}); - } -} - -template Component, class ComponentContainer> - requires main_core::model_component_state_c +template + requires main_core::model_component_state_c && + std::disjunction_v, + std::is_base_of> constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, TransformerTapPositionResult& transformer_tap_positions) { + constexpr auto group_index = state.components.template get_type_idx(); for (auto const& transformer : state.components.template citer()) { transformer_tap_positions.push_back( - std::pair{static_cast(transformer.id()), static_cast(transformer.tap_pos())}); + std::pair{Idx2D{group_index, transformer.id()}, static_cast(transformer.tap_pos())}); } } From ff639dcf859b38d151946b8ba8be129c47549f24 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Sun, 19 May 2024 14:36:46 +0200 Subject: [PATCH 10/39] gcc tamed Signed-off-by: Jerry Guo --- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index fa70f0d84..20ce5f51d 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -604,7 +604,7 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState inline void get_transformer_tap_positions(State const& state, TransformerTapPositionResult& transformer_tap_positions) { From 74eee6eb68cb6e82f323de8e983677117c8542f5 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Sun, 19 May 2024 14:46:13 +0200 Subject: [PATCH 11/39] clang likes `template` in a specific way Signed-off-by: Jerry Guo --- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 20ce5f51d..b92d0eddd 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -590,7 +590,7 @@ template std::is_base_of> constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, TransformerTapPositionResult& transformer_tap_positions) { - constexpr auto group_index = state.components.template get_type_idx(); + constexpr auto group_index = ComponentContainer::template get_type_idx(); for (auto const& transformer : state.components.template citer()) { transformer_tap_positions.push_back( std::pair{Idx2D{group_index, transformer.id()}, static_cast(transformer.tap_pos())}); From cee42753ccf3ff51be61bf1a99bd700fda337844 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Tue, 21 May 2024 12:34:42 +0200 Subject: [PATCH 12/39] [skip ci] resolve comments Signed-off-by: Jerry Guo --- .../include/power_grid_model/common/common.hpp | 4 ++-- .../include/power_grid_model/main_core/input.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp index 2d1c80893..41e24e3b7 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/common/common.hpp @@ -32,8 +32,8 @@ struct Idx2D { struct Idx2DHash { std::size_t operator()(const Idx2D& idx) const { - const std::size_t h1 = std::hash{}(idx.group); - const std::size_t h2 = std::hash{}(idx.pos); + size_t const h1 = std::hash{}(idx.group); + size_t const h2 = std::hash{}(idx.pos); return h1 ^ (h2 << 1); } }; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp index 01bcddb36..c604880b8 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/input.hpp @@ -168,7 +168,7 @@ inline void add_component(MainModelState& state, ForwardIter regulated_objects.end()); if (unique_regulated_objects.size() != regulated_objects.size()) { // There are duplicates - throw DuplicativelyRegulatedObject(); + throw DuplicativelyRegulatedObject{}; } } From 3550b7cb2b4d0ff84c5c4a334385438e2427db48 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Tue, 21 May 2024 15:36:34 +0200 Subject: [PATCH 13/39] [skip ci] only for compilations, tweak mathoutput to include more Signed-off-by: Jerry Guo --- .../calculation_parameters.hpp | 12 ++-- .../power_grid_model/main_core/output.hpp | 39 ++++++----- .../include/power_grid_model/main_model.hpp | 62 +++++++---------- .../optimizer/base_optimizer.hpp | 13 ++-- .../optimizer/tap_position_optimizer.hpp | 41 +++--------- tests/cpp_unit_tests/test_optimizer.cpp | 14 ++-- .../test_tap_position_optimizer.cpp | 67 ++++++++++--------- 7 files changed, 114 insertions(+), 134 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index a036f893d..5c2a4660f 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -130,6 +130,8 @@ struct TransformerTapRegulatorOptimizerOutput { IntS tap_pos{na_IntS}; }; +using TransformerTapPositionResult = std::vector>; + // from side, to side // in case of indices for math model, -1 means the branch is not connected to that side using BranchIdx = std::array; @@ -269,7 +271,7 @@ template struct SolverOutput { std::vector> source; std::vector> shunt; std::vector> load_gen; - std::vector transformer_tap_regulator; + // std::vector transformer_tap_regulator; }; template struct ShortCircuitSolverOutput { @@ -325,13 +327,15 @@ static_assert(short_circuit_solver_output_type>); struct OptimizerOutput { - std::vector transformer_tap_regulator; + // std::vector transformer_tap_regulator; + TransformerTapPositionResult transformer_tap_positions; }; -template struct MathOutput { +template struct MathOutput { using SolverOutputType = T; - std::vector solver_output; + SolverOutputType solver_output; + // std::vector solver_output; OptimizerOutput optimizer_output; }; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index 5eb2dca80..f8665de47 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -350,6 +350,13 @@ inline auto output_result(Component const& transformer_tap_regulator, std::vector const& /* solver_output */, Idx const /* obj_seq */) { return transformer_tap_regulator.get_null_sc_output(); } +// +//// entry point for output_result +// template Component, typename SolverOutputType> +// constexpr auto output_result(Component const& component, MathOutput const& math_output, +// Idx2D math_id) { +// return output_result(component, math_output.solver_output, math_id); +// } // output base component template Component, class ComponentContainer, solver_output_type SolverOutputType, @@ -361,10 +368,10 @@ template Component, class ComponentContainer, solver_ou } -> std::convertible_to>; } constexpr ResIt output_result(MainModelState const& state, - std::vector const& solver_output, ResIt res_it) { + MathOutput> const& math_output, ResIt res_it) { return detail::produce_output( - state, res_it, [&solver_output](Component const& component, Idx2D math_id) { - return output_result(component, solver_output, math_id); + state, res_it, [&math_output](Component const& component, Idx2D math_id) { + return output_result(component, math_output.solver_output, math_id); }); } template Component, class ComponentContainer, solver_output_type SolverOutputType, @@ -377,10 +384,10 @@ template Component, class ComponentContainer, solver_ou } -> std::convertible_to>; } constexpr ResIt output_result(MainModelState const& state, - std::vector const& solver_output, ResIt res_it) { + MathOutput> const& math_output, ResIt res_it) { return detail::produce_output( - state, res_it, [&state, &solver_output](Component const& component, Idx2D const math_id) { - return output_result(component, state, solver_output, math_id); + state, res_it, [&state, &math_output](Component const& component, Idx2D const math_id) { + return output_result(component, state, math_output.solver_output, math_id); }); } template Component, class ComponentContainer, solver_output_type SolverOutputType, @@ -393,10 +400,10 @@ template Component, class ComponentContainer, solver_ou } -> std::convertible_to>; } constexpr ResIt output_result(MainModelState const& state, - std::vector const& solver_output, ResIt res_it) { + MathOutput> const& math_output, ResIt res_it) { return detail::produce_output( - state, res_it, [&state, &solver_output](Component const& component, Idx const obj_seq) { - return output_result(component, state, solver_output, obj_seq); + state, res_it, [&state, &math_output](Component const& component, Idx const obj_seq) { + return output_result(component, state, math_output.solver_output, obj_seq); }); } template Component, class ComponentContainer, solver_output_type SolverOutputType, @@ -409,10 +416,10 @@ template Component, class ComponentContainer, solver_ou } -> std::convertible_to>; } constexpr ResIt output_result(MainModelState const& state, - std::vector const& solver_output, ResIt res_it) { + MathOutput> const& math_output, ResIt res_it) { return detail::produce_output( - state, res_it, [&solver_output](Component const& component, Idx2DBranch3 const& math_id) { - return output_result(component, solver_output, math_id); + state, res_it, [&math_output](Component const& component, Idx2DBranch3 const& math_id) { + return output_result(component, math_output.solver_output, math_id); }); } @@ -421,10 +428,10 @@ template Component, class ComponentContainer, solver_ou std::forward_iterator ResIt> requires model_component_state_c constexpr ResIt output_result(MainModelState const& state, - std::vector const& solver_output, ResIt res_it) { - res_it = output_result(state, solver_output, res_it); - res_it = output_result(state, solver_output, res_it); - res_it = output_result(state, solver_output, res_it); + MathOutput> const& math_output, ResIt res_it) { + res_it = output_result(state, math_output, res_it); + res_it = output_result(state, math_output, res_it); + res_it = output_result(state, math_output, res_it); return res_it; } diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp index a1a12b020..c0769af70 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp @@ -91,11 +91,6 @@ class MainModelImpl, ComponentLis struct permanent_update_t : std::false_type {}; - struct OptmizationOptions { - OptimizerType optimizer_type{OptimizerType::no_optimization}; - OptimizerStrategy optimizer_strategy{OptimizerStrategy::any}; - }; - struct Options { static constexpr Idx sequential = -1; @@ -683,20 +678,11 @@ class MainModelImpl, ComponentLis } template auto calculate_power_flow(Options const& options) { - const OptmizationOptions default_opt_options; - return calculate_power_flow(options, default_opt_options); - } - - template - auto calculate_power_flow(Options const& options, OptmizationOptions const& opt_options) { - auto result_pf = - optimizer::get_optimizer( - options.optimizer_type, options.optimizer_strategy, - calculate_power_flow_(options.err_tol, options.max_iter), - [this](ConstDataset update_data) { this->update_component(update_data); }) - ->optimize(state_, options.calculation_method); - return MathOutput>{.solver_output = std::move(result_pf), .optimizer_output = {}}; - // return result_pf; + return optimizer::get_optimizer( + options.optimizer_type, options.optimizer_strategy, + calculate_power_flow_(options.err_tol, options.max_iter), + [this](ConstDataset update_data) { this->update_component(update_data); }) + ->optimize(state_, options.calculation_method); } // Single load flow calculation, propagating the results to result_data @@ -728,9 +714,10 @@ class MainModelImpl, ComponentLis // Single state estimation calculation, returning math output results template auto calculate_state_estimation(Options const& options) { - return MathOutput>{.solver_output = calculate_state_estimation_( - options.err_tol, options.max_iter)(state_, options.calculation_method), - .optimizer_output = {}}; + return MathOutput>>{ + .solver_output = + calculate_state_estimation_(options.err_tol, options.max_iter)(state_, options.calculation_method), + .optimizer_output = {}}; } // Single state estimation calculation, propagating the results to result_data @@ -762,7 +749,7 @@ class MainModelImpl, ComponentLis // Single short circuit calculation, returning short circuit math output results template auto calculate_short_circuit(Options const& options) { - return MathOutput>{ + return MathOutput>>{ .solver_output = calculate_short_circuit_(options.short_circuit_voltage_scaling)( state_, options.calculation_method), .optimizer_output = {}}; @@ -795,30 +782,29 @@ class MainModelImpl, ComponentLis } template - requires solver_output_type + requires solver_output_type ResIt output_result(MathOutputType const& math_output, ResIt res_it) const { assert(construction_complete_); - return main_core::output_result(state_, math_output.solver_output, res_it); + return main_core::output_result(state_, math_output, res_it); } template - void output_result(MathOutput const& math_output, Dataset const& result_data, Idx pos = 0) { - using OutputFunc = void (*)(MainModelImpl & x, MathOutput const& math_output, + void output_result(MathOutput> const& math_output, Dataset const& result_data, + Idx pos = 0) { + using OutputFunc = void (*)(MainModelImpl & x, MathOutput> const& math_output, MutableDataPointer const& data_ptr, Idx position); - static constexpr std::array get_result{[](MainModelImpl& model, - MathOutput const& math_output_, - MutableDataPointer const& data_ptr, - Idx position) { - auto const begin = - data_ptr - .get_iterators< - std::conditional_t, + static constexpr std::array get_result{ + [](MainModelImpl& model, MathOutput> const& math_output_, + MutableDataPointer const& data_ptr, Idx position) { + auto const begin = data_ptr + .get_iterators, typename ComponentType::template OutputType, typename ComponentType::ShortCircuitOutputType>>(position) - .first; - model.output_result(math_output_, begin); - }...}; + .first; + model.output_result(math_output_, begin); + }...}; Timer const t_output(calculation_info_, 3000, "Produce output"); for (ComponentEntry const& entry : AllComponents::component_index_map) { diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp index 23d5e2ad9..a3e215706 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp @@ -48,7 +48,7 @@ class BaseOptimizer { BaseOptimizer& operator=(BaseOptimizer&&) noexcept = default; virtual ~BaseOptimizer() = default; - virtual auto optimize(State const& state, CalculationMethod method) -> ResultType = 0; + virtual auto optimize(State const& state, CalculationMethod method) -> MathOutput = 0; template Optimizer, typename... Args> requires std::constructible_from @@ -64,8 +64,11 @@ concept optimizer_c = requires(Optimizer optimizer, typename Optimizer::State const& state, CalculationMethod method) { { optimizer.optimize(state, method) - } -> std::same_as< - detail::state_calculator_result_t>; + } -> // std::same_as>; + + std::same_as>>; }; template @@ -78,7 +81,9 @@ class NoOptimizer : public detail::BaseOptimizer { NoOptimizer(Calculator func) : func_{std::move(func)} {} - auto optimize(State const& state, CalculationMethod method) -> ResultType final { return func_(state, method); } + auto optimize(State const& state, CalculationMethod method) -> MathOutput final { + return {.solver_output = func_(state, method)}; + } private: Calculator func_; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index b92d0eddd..be085b182 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -36,7 +36,7 @@ using main_core::get_component; using TrafoGraphIdx = Idx; using EdgeWeight = int64_t; using RankedTransformerGroups = std::vector>; -using TransformerTapPositionResult = std::vector>; +// using TransformerTapPositionResult = std::vector>; constexpr auto infty = std::numeric_limits::max(); constexpr Idx2D unregulated_idx = {-1, -1}; @@ -561,29 +561,6 @@ inline void create_tap_regulator_output(State const& state, std::vector -inline void add_tap_regulator_output(State const& state, TransformerTapRegulator const& regulator, IntS tap_pos, - std::vector& solver_output) { - Idx2D const& math_id = - get_math_id(state, get_topology_index(state, regulator.id())); - solver_output[math_id.group].transformer_tap_regulator[math_id.pos] = {tap_pos}; -} - -template - requires(main_core::component_container_c && ...) -void add_tap_regulator_output(State const& state, - std::vector>> const& regulator_order, - std::vector& solver_output) { - create_tap_regulator_output(state, solver_output); - - for (auto const& same_rank_regulators : regulator_order) { - for (auto const& regulator : same_rank_regulators) { - add_tap_regulator_output(state, regulator.regulator.get(), regulator.transformer.tap_pos(), solver_output); - } - } -} - template requires main_core::model_component_state_c && std::disjunction_v, @@ -651,22 +628,22 @@ class TapPositionOptimizerImpl, StateCalculator, TapPositionOptimizerImpl(Calculator calculator, StateUpdater updater, OptimizerStrategy strategy) : calculate_{std::move(calculator)}, update_{std::move(updater)}, strategy_{strategy} {} - auto optimize(State const& state, CalculationMethod method) -> ResultType final { + auto optimize(State const& state, CalculationMethod method) -> MathOutput final { + auto const order = regulator_mapping(state, TransformerRanker{}(state)); auto const cache = this->cache_states(order); auto solver_output = optimize(state, order, method); update_state(cache); + using SolverOutputType = decltype(solver_output)::value_type; + TransformerTapPositionResult transformer_tap_positions; get_transformer_tap_positions(state, transformer_tap_positions); - // using SolverOutputType = decltype(solver_output)::value_type; - // return MathOutput{ - // .solver_output = std::move(solver_output), - // .optimizer_output = std::move(transformer_tap_positions) - // }; - return solver_output; + return {.solver_output = {std::move(solver_output)}, + .optimizer_output = {std::move(transformer_tap_positions)}}; + // return math_res; } constexpr auto get_strategy() const { return strategy_; } @@ -725,8 +702,6 @@ class TapPositionOptimizerImpl, StateCalculator, } } - add_tap_regulator_output(state, regulator_order, result); - return result; } diff --git a/tests/cpp_unit_tests/test_optimizer.cpp b/tests/cpp_unit_tests/test_optimizer.cpp index 39d8982f4..723260538 100644 --- a/tests/cpp_unit_tests/test_optimizer.cpp +++ b/tests/cpp_unit_tests/test_optimizer.cpp @@ -9,8 +9,8 @@ TEST_CASE("Test construct no-op optimizer") { for (auto method : calculation_methods) { CAPTURE(method); auto optimizer = NoOptimizer{mock_state_calculator}; - CHECK(optimizer.optimize({}, method).x == 1); - CHECK(optimizer.optimize({}, method).x == 1); + CHECK(optimizer.optimize({}, method).solver_output.x == 1); + CHECK(optimizer.optimize({}, method).solver_output.x == 1); } } @@ -24,7 +24,7 @@ TEST_CASE("Test construct tap position optimizer") { CAPTURE(strategy_method.method); auto optimizer = TapPositionOptimizer{ stub_steady_state_state_calculator, stub_const_dataset_update, strategy_method.strategy}; - CHECK(optimizer.optimize(empty_state, strategy_method.method).empty()); + CHECK(optimizer.optimize(empty_state, strategy_method.method).solver_output.empty()); } } SUBCASE("asymmetric") { @@ -33,7 +33,7 @@ TEST_CASE("Test construct tap position optimizer") { CAPTURE(strategy_method.method); auto optimizer = TapPositionOptimizer{ stub_steady_state_state_calculator, stub_const_dataset_update, strategy_method.strategy}; - CHECK(optimizer.optimize(empty_state, strategy_method.method).empty()); + CHECK(optimizer.optimize(empty_state, strategy_method.method).solver_output.empty()); } } } @@ -51,7 +51,7 @@ TEST_CASE("Test get optimizer") { CAPTURE(strategy_method.method); auto optimizer = get_optimizer(no_optimization, strategy_method.strategy, mock_state_calculator, stub_update); - CHECK(optimizer->optimize(empty_state, strategy_method.method).x == 1); + CHECK(optimizer->optimize(empty_state, strategy_method.method).solver_output.x == 1); } } @@ -76,7 +76,7 @@ TEST_CASE("Test get optimizer") { CAPTURE(strategy_method.strategy); CAPTURE(strategy_method.method); auto optimizer = get_instance(no_optimization, strategy_method.strategy); - CHECK(optimizer->optimize(empty_state, strategy_method.method).empty()); + CHECK(optimizer->optimize(empty_state, strategy_method.method).solver_output.empty()); } } SUBCASE("Automatic tap adjustment") { @@ -92,7 +92,7 @@ TEST_CASE("Test get optimizer") { StubState empty_state{}; empty_state.components.set_construction_complete(); - CHECK(optimizer->optimize(empty_state, strategy_method.method).empty()); + CHECK(optimizer->optimize(empty_state, strategy_method.method).solver_output.empty()); } } } diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 8f2af44a3..d93ee4ce5 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -328,38 +328,40 @@ class MockSolverOutput : public SolverOutput { }; }; -template - requires main_core::main_model_state_c> -inline void create_tap_regulator_output(main_core::MainModelState const& /* state */, - std::vector>& /* solver_output */) {} - -template - requires main_core::main_model_state_c> -inline void add_tap_regulator_output(main_core::MainModelState const& /* state */, - TransformerTapRegulator const& regulator, IntS tap_pos, - std::vector>& solver_output) { - REQUIRE(solver_output.size() > 0); - - // state consistency - CHECK(solver_output.front().state_tap_positions[regulator.regulated_object()] == tap_pos); - - // add to output - REQUIRE(!solver_output.front().output_tap_positions.contains(regulator.id())); - solver_output.front().output_tap_positions[regulator.id()] = tap_pos; -} +// re write this to transformer_tap_positions test + +// template +// requires main_core::main_model_state_c> +// inline void create_tap_regulator_output(main_core::MainModelState const& /* state */, +// std::vector>& /* solver_output */) {} + +// template +// requires main_core::main_model_state_c> +// inline void add_tap_regulator_output(main_core::MainModelState const& /* state */, +// TransformerTapRegulator const& regulator, IntS tap_pos, +// std::vector>& solver_output) { +// REQUIRE(solver_output.size() > 0); +// +// // state consistency +// CHECK(solver_output.front().state_tap_positions[regulator.regulated_object()] == tap_pos); +// +// // add to output +// REQUIRE(!solver_output.front().output_tap_positions.contains(regulator.id())); +// solver_output.front().output_tap_positions[regulator.id()] = tap_pos; +// } template requires main_core::main_model_state_c> -inline IntS get_state_tap_pos(std::vector> const& solver_output, ID id) { - REQUIRE(solver_output.size() > 0); - return solver_output.front().state_tap_positions.at(id); +inline IntS get_state_tap_pos(MathOutput> const& math_output, ID id) { + REQUIRE(math_output.solver_output.size() > 0); + return math_output.solver_output.front().state_tap_positions.at(id); } template requires main_core::main_model_state_c> -inline IntS get_output_tap_pos(std::vector> const& solver_output, ID id) { - REQUIRE(solver_output.size() > 0); - return solver_output.front().output_tap_positions.at(id); +inline IntS get_output_tap_pos(MathOutput> const& math_output, ID id) { + REQUIRE(math_output.solver_output.size() > 0); + return math_output.solver_output.front().output_tap_positions.at(id); } template @@ -598,8 +600,8 @@ TEST_CASE("Test Tap position optimizer") { state.components.set_construction_complete(); auto optimizer = get_optimizer(OptimizerStrategy::any); auto result = optimizer.optimize(state, CalculationMethod::default_method); - CHECK(result.size() == 1); - CHECK(result[0].method == CalculationMethod::default_method); + CHECK(result.solver_output.size() == 1); + CHECK(result.solver_output[0].method == CalculationMethod::default_method); } SUBCASE("Calculation method") { @@ -616,8 +618,8 @@ TEST_CASE("Test Tap position optimizer") { auto optimizer = get_optimizer(strategy_method.strategy); auto result = optimizer.optimize(state, strategy_method.method); - CHECK(result.size() == 1); - CHECK(result[0].method == strategy_method.method); + CHECK(result.solver_output.size() == 1); + CHECK(result.solver_output[0].method == strategy_method.method); } } @@ -965,9 +967,10 @@ TEST_CASE("Test Tap position optimizer") { auto const result = optimizer.optimize(state, CalculationMethod::default_method); // correctness - CHECK(result.size() == 1); - check_a(get_state_tap_pos(result, state_a.id), strategy); - check_b(get_state_tap_pos(result, state_b.id), strategy); + CHECK(result.solver_output.size() == 1); + // need rewriting + // check_a(get_state_tap_pos(result, state_a.id), strategy); + // check_b(get_state_tap_pos(result, state_b.id), strategy); // reset CHECK(transformer_a.tap_pos() == initial_a); From 43b3e828885c02e538bc08965a9937f30a6cf7ee Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 08:42:13 +0200 Subject: [PATCH 14/39] add typename Signed-off-by: Jerry Guo --- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index be085b182..727147027 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -636,7 +636,7 @@ class TapPositionOptimizerImpl, StateCalculator, auto solver_output = optimize(state, order, method); update_state(cache); - using SolverOutputType = decltype(solver_output)::value_type; + using SolverOutputType = typename decltype(solver_output)::value_type; TransformerTapPositionResult transformer_tap_positions; get_transformer_tap_positions(state, transformer_tap_positions); From 494942b32bc6e808e02aa45b898c7010be43e86a Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 08:51:54 +0200 Subject: [PATCH 15/39] clean up a bit before tests Signed-off-by: Jerry Guo --- .../power_grid_model/calculation_parameters.hpp | 11 +++++------ .../power_grid_model/optimizer/base_optimizer.hpp | 7 ++----- .../optimizer/tap_position_optimizer.hpp | 5 +---- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index 5c2a4660f..800b7ef73 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -126,11 +126,12 @@ struct TransformerTapRegulatorCalcParam { IntS status{}; }; -struct TransformerTapRegulatorOptimizerOutput { - IntS tap_pos{na_IntS}; +struct TransformerTapPosition { + Idx2D index; + IntS tap_position; + TransformerTapPosition(Idx2D _index, IntS _tap_position) : index(_index), tap_position(_tap_position) {} }; - -using TransformerTapPositionResult = std::vector>; +using TransformerTapPositionResult = std::vector; // from side, to side // in case of indices for math model, -1 means the branch is not connected to that side @@ -271,7 +272,6 @@ template struct SolverOutput { std::vector> source; std::vector> shunt; std::vector> load_gen; - // std::vector transformer_tap_regulator; }; template struct ShortCircuitSolverOutput { @@ -327,7 +327,6 @@ static_assert(short_circuit_solver_output_type>); struct OptimizerOutput { - // std::vector transformer_tap_regulator; TransformerTapPositionResult transformer_tap_positions; }; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp index a3e215706..2d2adca6c 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp @@ -64,11 +64,8 @@ concept optimizer_c = requires(Optimizer optimizer, typename Optimizer::State const& state, CalculationMethod method) { { optimizer.optimize(state, method) - } -> // std::same_as>; - - std::same_as>>; + } -> std::same_as>>; }; template diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 727147027..3fd4c72ce 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -36,7 +36,6 @@ using main_core::get_component; using TrafoGraphIdx = Idx; using EdgeWeight = int64_t; using RankedTransformerGroups = std::vector>; -// using TransformerTapPositionResult = std::vector>; constexpr auto infty = std::numeric_limits::max(); constexpr Idx2D unregulated_idx = {-1, -1}; @@ -570,7 +569,7 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState(); for (auto const& transformer : state.components.template citer()) { transformer_tap_positions.push_back( - std::pair{Idx2D{group_index, transformer.id()}, static_cast(transformer.tap_pos())}); + TransformerTapPosition{Idx2D{group_index, transformer.id()}, static_cast(transformer.tap_pos())}); } } @@ -636,8 +635,6 @@ class TapPositionOptimizerImpl, StateCalculator, auto solver_output = optimize(state, order, method); update_state(cache); - using SolverOutputType = typename decltype(solver_output)::value_type; - TransformerTapPositionResult transformer_tap_positions; get_transformer_tap_positions(state, transformer_tap_positions); From 41bd26c9abfb72b84787e0ca8ffbc6b206a5a2a0 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 11:46:34 +0200 Subject: [PATCH 16/39] [skip ci] tests. to be debug Signed-off-by: Jerry Guo --- .../test_tap_position_optimizer.cpp | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index d93ee4ce5..7dbca026d 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -23,7 +23,7 @@ using TestComponentContainer = ThreeWindingTransformer, TransformerTapRegulator, Source>; using TestState = main_core::MainModelState; -TransformerInput get_transformer(ID id, ID from, ID to, BranchSide tap_side) { +TransformerInput get_transformer(ID id, ID from, ID to, BranchSide tap_side, IntS tap_pos = na_IntS) { return TransformerInput{.id = id, .from_node = from, .to_node = to, @@ -40,7 +40,7 @@ TransformerInput get_transformer(ID id, ID from, ID to, BranchSide tap_side) { .winding_to = WindingType::wye_n, .clock = 0, .tap_side = tap_side, - .tap_pos = na_IntS, + .tap_pos = tap_pos, .tap_min = na_IntS, .tap_max = na_IntS, .tap_nom = na_IntS, @@ -55,7 +55,7 @@ TransformerInput get_transformer(ID id, ID from, ID to, BranchSide tap_side) { .x_grounding_to = nan}; } -ThreeWindingTransformerInput get_transformer3w(ID id, ID node_1, ID node_2, ID node_3) { +ThreeWindingTransformerInput get_transformer3w(ID id, ID node_1, ID node_2, ID node_3, IntS tap_pos = 0) { return ThreeWindingTransformerInput{ .id = id, .node_1 = node_1, @@ -84,7 +84,7 @@ ThreeWindingTransformerInput get_transformer3w(ID id, ID node_1, ID node_2, ID n .clock_12 = 0, .clock_13 = 0, .tap_side = Branch3Side::side_1, - .tap_pos = 0, + .tap_pos = tap_pos, .tap_min = 0, .tap_max = 0, .tap_nom = 0, @@ -978,6 +978,60 @@ TEST_CASE("Test Tap position optimizer") { } } } + + SUBCASE("transformer tap position retrieval") { + std::vector transformers{ + test::get_transformer(11, 0, 1, BranchSide::from, 0), test::get_transformer(12, 0, 1, BranchSide::from, -1), + test::get_transformer(13, 5, 7, BranchSide::from, 1), test::get_transformer(14, 2, 3, BranchSide::from, -2), + test::get_transformer(15, 8, 9, BranchSide::from, 2)}; + main_core::add_component(state, transformers.begin(), transformers.end(), 50.0); + + std::vector transformers3w{test::get_transformer3w(16, 0, 4, 5, 3)}; + main_core::add_component(state, transformers3w.begin(), transformers3w.end(), 50.0); + + TransformerTapPositionResult transformer_tap_positions; + power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< + Transformer, ThreeWindingTransformer, MockState>(state, transformer_tap_positions); + // CHECK(); + } + + SUBCASE("transformer duplicatively regulated") { + main_core::emplace_component( + state, 1, MockTransformerState{.id = 1, .math_id = {.group = 0, .pos = 0}}); + main_core::emplace_component( + state, 2, MockTransformerState{.id = 2, .math_id = {.group = 0, .pos = 1}}); + + std::vector bad_regulators{ + TransformerTapRegulatorInput{.id = 3, + .regulated_object = 1, + .status = 1, + .control_side = ControlSide::side_1, + .u_set = 0.0, + .u_band = 0.0, + .line_drop_compensation_r = 0.0, + .line_drop_compensation_x = 0.0}, + TransformerTapRegulatorInput{.id = 4, + .regulated_object = 2, + .status = 1, + .control_side = ControlSide::side_2, + .u_set = 0.0, + .u_band = 0.0, + .line_drop_compensation_r = 0.0, + .line_drop_compensation_x = 0.0}, + TransformerTapRegulatorInput{.id = 5, + .regulated_object = 2, + .status = 1, + .control_side = ControlSide::side_2, + .u_set = 0.0, + .u_band = 0.0, + .line_drop_compensation_r = 0.0, + .line_drop_compensation_x = 0.0}, + }; + + CHECK_THROWS_AS(main_core::add_component(state, bad_regulators.begin(), + bad_regulators.end(), 50.0), + DuplicativelyRegulatedObject); + } } } // namespace power_grid_model From b76a18e30b3bdfb2fd6401a8f5dadb25ed40fdc5 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 14:58:58 +0200 Subject: [PATCH 17/39] test in this pr passes. onto debug other 4 Signed-off-by: Jerry Guo --- .../test_tap_position_optimizer.cpp | 116 ++++++++++-------- 1 file changed, 62 insertions(+), 54 deletions(-) diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 7dbca026d..007dc4284 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -41,9 +41,9 @@ TransformerInput get_transformer(ID id, ID from, ID to, BranchSide tap_side, Int .clock = 0, .tap_side = tap_side, .tap_pos = tap_pos, - .tap_min = na_IntS, - .tap_max = na_IntS, - .tap_nom = na_IntS, + .tap_min = std::numeric_limits::min(), + .tap_max = std::numeric_limits::max(), + .tap_nom = 0, .tap_size = nan, .uk_min = nan, .uk_max = nan, @@ -85,8 +85,8 @@ ThreeWindingTransformerInput get_transformer3w(ID id, ID node_1, ID node_2, ID n .clock_13 = 0, .tap_side = Branch3Side::side_1, .tap_pos = tap_pos, - .tap_min = 0, - .tap_max = 0, + .tap_min = std::numeric_limits::min(), + .tap_max = std::numeric_limits::max(), .tap_nom = 0, .tap_size = nan, .uk_12_min = nan, @@ -328,28 +328,6 @@ class MockSolverOutput : public SolverOutput { }; }; -// re write this to transformer_tap_positions test - -// template -// requires main_core::main_model_state_c> -// inline void create_tap_regulator_output(main_core::MainModelState const& /* state */, -// std::vector>& /* solver_output */) {} - -// template -// requires main_core::main_model_state_c> -// inline void add_tap_regulator_output(main_core::MainModelState const& /* state */, -// TransformerTapRegulator const& regulator, IntS tap_pos, -// std::vector>& solver_output) { -// REQUIRE(solver_output.size() > 0); -// -// // state consistency -// CHECK(solver_output.front().state_tap_positions[regulator.regulated_object()] == tap_pos); -// -// // add to output -// REQUIRE(!solver_output.front().output_tap_positions.contains(regulator.id())); -// solver_output.front().output_tap_positions[regulator.id()] = tap_pos; -// } - template requires main_core::main_model_state_c> inline IntS get_state_tap_pos(MathOutput> const& math_output, ID id) { @@ -978,48 +956,38 @@ TEST_CASE("Test Tap position optimizer") { } } } +} - SUBCASE("transformer tap position retrieval") { - std::vector transformers{ - test::get_transformer(11, 0, 1, BranchSide::from, 0), test::get_transformer(12, 0, 1, BranchSide::from, -1), - test::get_transformer(13, 5, 7, BranchSide::from, 1), test::get_transformer(14, 2, 3, BranchSide::from, -2), - test::get_transformer(15, 8, 9, BranchSide::from, 2)}; - main_core::add_component(state, transformers.begin(), transformers.end(), 50.0); - - std::vector transformers3w{test::get_transformer3w(16, 0, 4, 5, 3)}; - main_core::add_component(state, transformers3w.begin(), transformers3w.end(), 50.0); - - TransformerTapPositionResult transformer_tap_positions; - power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< - Transformer, ThreeWindingTransformer, MockState>(state, transformer_tap_positions); - // CHECK(); - } - +TEST_CASE("Test tap position optmizer I/O") { SUBCASE("transformer duplicatively regulated") { - main_core::emplace_component( - state, 1, MockTransformerState{.id = 1, .math_id = {.group = 0, .pos = 0}}); - main_core::emplace_component( - state, 2, MockTransformerState{.id = 2, .math_id = {.group = 0, .pos = 1}}); + test::TestState state_mini; + std::vector nodes{{0, 150e3}, {1, 10e3}, {2, 10e3}, {3, 10e3}}; + main_core::add_component(state_mini, nodes.begin(), nodes.end(), 50.0); + + std::vector transformers{test::get_transformer(4, 0, 1, BranchSide::from, 0), + test::get_transformer(5, 1, 2, BranchSide::from, -1), + test::get_transformer(6, 2, 3, BranchSide::from, 1)}; + main_core::add_component(state_mini, transformers.begin(), transformers.end(), 50.0); std::vector bad_regulators{ - TransformerTapRegulatorInput{.id = 3, - .regulated_object = 1, + TransformerTapRegulatorInput{.id = 7, + .regulated_object = 4, .status = 1, .control_side = ControlSide::side_1, .u_set = 0.0, .u_band = 0.0, .line_drop_compensation_r = 0.0, .line_drop_compensation_x = 0.0}, - TransformerTapRegulatorInput{.id = 4, - .regulated_object = 2, + TransformerTapRegulatorInput{.id = 8, + .regulated_object = 5, .status = 1, .control_side = ControlSide::side_2, .u_set = 0.0, .u_band = 0.0, .line_drop_compensation_r = 0.0, .line_drop_compensation_x = 0.0}, - TransformerTapRegulatorInput{.id = 5, - .regulated_object = 2, + TransformerTapRegulatorInput{.id = 9, + .regulated_object = 5, .status = 1, .control_side = ControlSide::side_2, .u_set = 0.0, @@ -1028,10 +996,50 @@ TEST_CASE("Test Tap position optimizer") { .line_drop_compensation_x = 0.0}, }; - CHECK_THROWS_AS(main_core::add_component(state, bad_regulators.begin(), + CHECK_THROWS_AS(main_core::add_component(state_mini, bad_regulators.begin(), bad_regulators.end(), 50.0), DuplicativelyRegulatedObject); } + + SUBCASE("transformer tap position retrieval") { + // Minimum grid for only transformer tap position optimizer I/O + test::TestState state_; + std::vector nodes{{0, 150e3}, {1, 10e3}, {2, 10e3}, {3, 10e3}, {4, 10e3}, + {5, 50e3}, {6, 10e3}, {7, 10e3}, {8, 10e3}, {9, 10e3}}; + main_core::add_component(state_, nodes.begin(), nodes.end(), 50.0); + + std::vector transformers{ + test::get_transformer(11, 0, 1, BranchSide::from, 0), test::get_transformer(12, 0, 1, BranchSide::from, -1), + test::get_transformer(13, 5, 7, BranchSide::from, 1), test::get_transformer(14, 2, 3, BranchSide::from, -2), + test::get_transformer(15, 8, 9, BranchSide::from, 2)}; + main_core::add_component(state_, transformers.begin(), transformers.end(), 50.0); + + std::vector transformers3w{test::get_transformer3w(16, 0, 4, 5, 3)}; + main_core::add_component(state_, transformers3w.begin(), transformers3w.end(), 50.0); + + state_.components.set_construction_complete(); + + TransformerTapPositionResult transformer_tap_positions; + power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions( + state_, transformer_tap_positions); + std::vector expected_tap_positions{ + {{3, 11}, 0}, {{3, 12}, -1}, {{3, 13}, 1}, {{3, 14}, -2}, {{3, 15}, 2}, {{4, 16}, 3}, + }; + auto check_tap_positions_match = [](const std::vector& vec1, + const std::vector& vec2) { + if (vec1.size() != vec2.size()) { + return false; + } + for (size_t i = 0; i < vec1.size(); ++i) { + if (vec1[i].index != vec2[i].index || vec1[i].tap_position != vec2[i].tap_position) { + return false; + } + } + return true; + }; + CHECK(check_tap_positions_match(transformer_tap_positions, expected_tap_positions)); + } } } // namespace power_grid_model From b3ed691c6f9fe76771c895b12163ef40ee4e85b9 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 16:11:45 +0200 Subject: [PATCH 18/39] test debug Signed-off-by: Jerry Guo --- .../optimizer/tap_position_optimizer.hpp | 33 +++---------------- tests/cpp_unit_tests/test_optimizer.cpp | 1 + .../test_tap_position_optimizer.cpp | 30 ++++++----------- 3 files changed, 16 insertions(+), 48 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 3fd4c72ce..0c0957bbc 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -560,10 +560,8 @@ inline void create_tap_regulator_output(State const& state, std::vector - requires main_core::model_component_state_c && - std::disjunction_v, - std::is_base_of> +template + requires main_core::model_component_state_c constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, TransformerTapPositionResult& transformer_tap_positions) { constexpr auto group_index = ComponentContainer::template get_type_idx(); @@ -573,28 +571,6 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState -inline void get_transformer_tap_positions(State const& state, TransformerTapPositionResult& transformer_tap_positions) { - using ComponentContainerType = typename State::ComponentContainer; - (get_transformer_tap_positions(state, transformer_tap_positions), ...); -} -#ifdef _MSC_VER -// MSVC compiler -#pragma warning(pop) -#elif defined(__GNUC__) || defined(__clang__) -// GCC or Clang compilers -#pragma GCC diagnostic pop -#endif - template class TapPositionOptimizerImpl; template @@ -636,11 +612,12 @@ class TapPositionOptimizerImpl, StateCalculator, update_state(cache); TransformerTapPositionResult transformer_tap_positions; - get_transformer_tap_positions(state, transformer_tap_positions); + (get_transformer_tap_positions(state, + transformer_tap_positions), + ...); return {.solver_output = {std::move(solver_output)}, .optimizer_output = {std::move(transformer_tap_positions)}}; - // return math_res; } constexpr auto get_strategy() const { return strategy_; } diff --git a/tests/cpp_unit_tests/test_optimizer.cpp b/tests/cpp_unit_tests/test_optimizer.cpp index 723260538..bcf86cb39 100644 --- a/tests/cpp_unit_tests/test_optimizer.cpp +++ b/tests/cpp_unit_tests/test_optimizer.cpp @@ -24,6 +24,7 @@ TEST_CASE("Test construct tap position optimizer") { CAPTURE(strategy_method.method); auto optimizer = TapPositionOptimizer{ stub_steady_state_state_calculator, stub_const_dataset_update, strategy_method.strategy}; + auto res = optimizer.optimize(empty_state, strategy_method.method); CHECK(optimizer.optimize(empty_state, strategy_method.method).solver_output.empty()); } } diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 007dc4284..855aae987 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -328,20 +328,6 @@ class MockSolverOutput : public SolverOutput { }; }; -template - requires main_core::main_model_state_c> -inline IntS get_state_tap_pos(MathOutput> const& math_output, ID id) { - REQUIRE(math_output.solver_output.size() > 0); - return math_output.solver_output.front().state_tap_positions.at(id); -} - -template - requires main_core::main_model_state_c> -inline IntS get_output_tap_pos(MathOutput> const& math_output, ID id) { - REQUIRE(math_output.solver_output.size() > 0); - return math_output.solver_output.front().output_tap_positions.at(id); -} - template using MockStateCalculator = std::vector> (*)( main_core::MainModelState const& state, CalculationMethod method); @@ -554,6 +540,7 @@ TEST_CASE("Test Tap position optimizer") { using MockTransformerRanker = test::MockTransformerRanker; constexpr auto tap_sides = std::array{ControlSide::side_1, ControlSide::side_2, ControlSide::side_3}; + constexpr auto transformer_group_index = MockContainer::template get_type_idx(); MockState state; @@ -946,9 +933,10 @@ TEST_CASE("Test Tap position optimizer") { // correctness CHECK(result.solver_output.size() == 1); - // need rewriting - // check_a(get_state_tap_pos(result, state_a.id), strategy); - // check_b(get_state_tap_pos(result, state_b.id), strategy); + // need rewriting: add function to extract tap pos from result for states_.id + Idx2D const math_id_a{4, 0}; + // check_a((result, state_a.id), strategy); + // check_b((result, state_b.id), strategy); // reset CHECK(transformer_a.tap_pos() == initial_a); @@ -1020,9 +1008,11 @@ TEST_CASE("Test tap position optmizer I/O") { state_.components.set_construction_complete(); TransformerTapPositionResult transformer_tap_positions; - power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions( - state_, transformer_tap_positions); + // using power_grid_model::optimizer::tap_position_optimizer; + power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< + Transformer, test::TestState::ComponentContainer>(state_, transformer_tap_positions); + power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< + ThreeWindingTransformer, test::TestState::ComponentContainer>(state_, transformer_tap_positions); std::vector expected_tap_positions{ {{3, 11}, 0}, {{3, 12}, -1}, {{3, 13}, 1}, {{3, 14}, -2}, {{3, 15}, 2}, {{4, 16}, 3}, }; From 3e799bb406a7b13f0a132f004d9c0c0ed1d70def Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 17:42:46 +0200 Subject: [PATCH 19/39] [skip ci] first version out of draft Signed-off-by: Jerry Guo --- .../power_grid_model/calculation_parameters.hpp | 1 - .../power_grid_model/optimizer/base_optimizer.hpp | 2 +- .../cpp_unit_tests/test_tap_position_optimizer.cpp | 14 +++++++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index 800b7ef73..56c16db06 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -334,7 +334,6 @@ template struct MathOutput { using SolverOutputType = T; SolverOutputType solver_output; - // std::vector solver_output; OptimizerOutput optimizer_output; }; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp index 2d2adca6c..8126a7c7b 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/base_optimizer.hpp @@ -79,7 +79,7 @@ class NoOptimizer : public detail::BaseOptimizer { NoOptimizer(Calculator func) : func_{std::move(func)} {} auto optimize(State const& state, CalculationMethod method) -> MathOutput final { - return {.solver_output = func_(state, method)}; + return {.solver_output = func_(state, method), .optimizer_output = {}}; } private: diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 855aae987..2e1703a95 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -931,12 +931,20 @@ TEST_CASE("Test Tap position optimizer") { auto optimizer = get_optimizer(strategy); auto const result = optimizer.optimize(state, CalculationMethod::default_method); + auto get_tap_position = [&](const Idx2D& idx) { + for (const auto& tap_position : result.optimizer_output.transformer_tap_positions) { + if (tap_position.index == idx) { + return tap_position.tap_position; + } + } + return std::numeric_limits::quiet_NaN(); + }; + // correctness CHECK(result.solver_output.size() == 1); - // need rewriting: add function to extract tap pos from result for states_.id Idx2D const math_id_a{4, 0}; - // check_a((result, state_a.id), strategy); - // check_b((result, state_b.id), strategy); + check_a(get_tap_position(Idx2D{transformer_group_index, state_a.id}), strategy); + check_b(get_tap_position(Idx2D{transformer_group_index, state_b.id}), strategy); // reset CHECK(transformer_a.tap_pos() == initial_a); From a0d136863614127c9c725e4b41f3849c037377da Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 21:29:07 +0200 Subject: [PATCH 20/39] final test debugged, code cleaned Signed-off-by: Jerry Guo --- .../include/power_grid_model/main_core/output.hpp | 7 ------- .../test_tap_position_optimizer.cpp | 15 +++++---------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index f8665de47..934dd722f 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -350,13 +350,6 @@ inline auto output_result(Component const& transformer_tap_regulator, std::vector const& /* solver_output */, Idx const /* obj_seq */) { return transformer_tap_regulator.get_null_sc_output(); } -// -//// entry point for output_result -// template Component, typename SolverOutputType> -// constexpr auto output_result(Component const& component, MathOutput const& math_output, -// Idx2D math_id) { -// return output_result(component, math_output.solver_output, math_id); -// } // output base component template Component, class ComponentContainer, solver_output_type SolverOutputType, diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 2e1703a95..aee469590 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -931,20 +931,15 @@ TEST_CASE("Test Tap position optimizer") { auto optimizer = get_optimizer(strategy); auto const result = optimizer.optimize(state, CalculationMethod::default_method); - auto get_tap_position = [&](const Idx2D& idx) { - for (const auto& tap_position : result.optimizer_output.transformer_tap_positions) { - if (tap_position.index == idx) { - return tap_position.tap_position; - } - } - return std::numeric_limits::quiet_NaN(); + auto get_state_tap_pos = [&](const ID id) { + REQUIRE(result.solver_output.size() > 0); + return result.solver_output.front().state_tap_positions.at(id); }; // correctness CHECK(result.solver_output.size() == 1); - Idx2D const math_id_a{4, 0}; - check_a(get_tap_position(Idx2D{transformer_group_index, state_a.id}), strategy); - check_b(get_tap_position(Idx2D{transformer_group_index, state_b.id}), strategy); + check_a(get_state_tap_pos(state_a.id), strategy); + check_b(get_state_tap_pos(state_b.id), strategy); // reset CHECK(transformer_a.tap_pos() == initial_a); From f0fdba02d9ff5cae10d741787c2be2fb44b7d8da Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 21:32:31 +0200 Subject: [PATCH 21/39] unused var Signed-off-by: Jerry Guo --- tests/cpp_unit_tests/test_tap_position_optimizer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index aee469590..8c7a30930 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -540,8 +540,7 @@ TEST_CASE("Test Tap position optimizer") { using MockTransformerRanker = test::MockTransformerRanker; constexpr auto tap_sides = std::array{ControlSide::side_1, ControlSide::side_2, ControlSide::side_3}; - constexpr auto transformer_group_index = MockContainer::template get_type_idx(); - + MockState state; auto const updater = [&state](ConstDataset const& update_dataset) { From 8c83ea8b4e5abcb4578508cc1dfcfd6aa5e9e655 Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 21:37:11 +0200 Subject: [PATCH 22/39] format Signed-off-by: Jerry Guo --- tests/cpp_unit_tests/test_tap_position_optimizer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 8c7a30930..0a9b9750b 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -540,7 +540,7 @@ TEST_CASE("Test Tap position optimizer") { using MockTransformerRanker = test::MockTransformerRanker; constexpr auto tap_sides = std::array{ControlSide::side_1, ControlSide::side_2, ControlSide::side_3}; - + MockState state; auto const updater = [&state](ConstDataset const& update_dataset) { From f7353e802fc6b660210ce7f43da2343fb10bdd4a Mon Sep 17 00:00:00 2001 From: Jerry Guo Date: Wed, 22 May 2024 22:06:39 +0200 Subject: [PATCH 23/39] sonar cloud Signed-off-by: Jerry Guo --- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 2 +- tests/cpp_unit_tests/test_tap_position_optimizer.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 0c0957bbc..2a41dd0d5 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -566,7 +566,7 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState(); for (auto const& transformer : state.components.template citer()) { - transformer_tap_positions.push_back( + transformer_tap_positions.emplace_back( TransformerTapPosition{Idx2D{group_index, transformer.id()}, static_cast(transformer.tap_pos())}); } } diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 0a9b9750b..00ef0636c 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -1010,7 +1010,6 @@ TEST_CASE("Test tap position optmizer I/O") { state_.components.set_construction_complete(); TransformerTapPositionResult transformer_tap_positions; - // using power_grid_model::optimizer::tap_position_optimizer; power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< Transformer, test::TestState::ComponentContainer>(state_, transformer_tap_positions); power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< From 199c0404f14e61d86e1ec1a7f2ef4a35eed26d7a Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 13:51:48 +0200 Subject: [PATCH 24/39] Update power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp Signed-off-by: Martijn Govers Signed-off-by: Martijn Govers --- .../include/power_grid_model/calculation_parameters.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index 56c16db06..10b89a634 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -127,9 +127,8 @@ struct TransformerTapRegulatorCalcParam { }; struct TransformerTapPosition { - Idx2D index; - IntS tap_position; - TransformerTapPosition(Idx2D _index, IntS _tap_position) : index(_index), tap_position(_tap_position) {} + Idx2D index{}; + IntS tap_position{}; }; using TransformerTapPositionResult = std::vector; From 59fa97075e0afba07ab5ed8085bdcda910b13da1 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 15:32:15 +0200 Subject: [PATCH 25/39] output tap pos before restoring state from cache Signed-off-by: Martijn Govers --- .../optimizer/tap_position_optimizer.hpp | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 2a41dd0d5..8818a918b 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -608,16 +608,10 @@ class TapPositionOptimizerImpl, StateCalculator, auto const order = regulator_mapping(state, TransformerRanker{}(state)); auto const cache = this->cache_states(order); - auto solver_output = optimize(state, order, method); + auto result = optimize(state, order, method); update_state(cache); - TransformerTapPositionResult transformer_tap_positions; - (get_transformer_tap_positions(state, - transformer_tap_positions), - ...); - - return {.solver_output = {std::move(solver_output)}, - .optimizer_output = {std::move(transformer_tap_positions)}}; + return result; } constexpr auto get_strategy() const { return strategy_; } @@ -628,12 +622,24 @@ class TapPositionOptimizerImpl, StateCalculator, initialize(regulator_order); if (auto result = iterate_with_fallback(state, regulator_order, method); strategy_ == OptimizerStrategy::any) { - return result; + return produce_output(state, std::move(result)); } // refine solution exploit_neighborhood(regulator_order); - return iterate_with_fallback(state, regulator_order, method); + return produce_output(state, iterate_with_fallback(state, regulator_order, method)); + } + + auto produce_output(State const& state, ResultType solver_output) { + TransformerTapPositionResult transformer_tap_positions; + + // TODO(mgovers): only output the transformers that are regulated + (get_transformer_tap_positions(state, + transformer_tap_positions), + ...); + + return {.solver_output = {std::move(solver_output)}, + .optimizer_output = {std::move(transformer_tap_positions)}}; } auto iterate_with_fallback(State const& state, From fca6acffeb0b259087d13149a12896ea864ea695 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 15:33:49 +0200 Subject: [PATCH 26/39] sonar cloud Signed-off-by: Martijn Govers --- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 8818a918b..eb77d2494 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -566,8 +566,8 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState(); for (auto const& transformer : state.components.template citer()) { - transformer_tap_positions.emplace_back( - TransformerTapPosition{Idx2D{group_index, transformer.id()}, static_cast(transformer.tap_pos())}); + transformer_tap_positions.emplace_back(Idx2D{group_index, transformer.id()}, + narrow_cast(transformer.tap_pos())); } } @@ -618,7 +618,7 @@ class TapPositionOptimizerImpl, StateCalculator, private: auto optimize(State const& state, std::vector> const& regulator_order, - CalculationMethod method) const -> ResultType { + CalculationMethod method) const -> MathOutput { initialize(regulator_order); if (auto result = iterate_with_fallback(state, regulator_order, method); strategy_ == OptimizerStrategy::any) { @@ -630,7 +630,7 @@ class TapPositionOptimizerImpl, StateCalculator, return produce_output(state, iterate_with_fallback(state, regulator_order, method)); } - auto produce_output(State const& state, ResultType solver_output) { + auto produce_output(State const& state, ResultType solver_output) const -> MathOutput { TransformerTapPositionResult transformer_tap_positions; // TODO(mgovers): only output the transformers that are regulated From 92943953f07363d1b637d49661cb19cd88b95b29 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 16:05:11 +0200 Subject: [PATCH 27/39] output transformer tap regulator Signed-off-by: Martijn Govers --- .../component/transformer_tap_regulator.hpp | 8 +++- .../power_grid_model/main_core/output.hpp | 42 ++++++++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp index 08d590c3f..9a6a46fed 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp @@ -56,7 +56,11 @@ class TransformerTapRegulator : public Regulator { return update_data; } - TransformerTapRegulatorOutput get_output(IntS const& tap_pos) const { + constexpr TransformerTapRegulatorOutput get_null_output() const { + return {.id = id(), .energized = 0, .tap_pos = na_IntS}; + } + + constexpr TransformerTapRegulatorOutput get_output(IntS const& tap_pos) const { TransformerTapRegulatorOutput output{}; output.id = id(); output.energized = static_cast(energized(true)); @@ -77,7 +81,7 @@ class TransformerTapRegulator : public Regulator { // getter ControlSide control_side() const { return control_side_; } - RegulatorShortCircuitOutput get_null_sc_output() const { return {.id = id(), .energized = 0}; } + RegulatorShortCircuitOutput get_null_output() const { return {.id = id(), .energized = 0}; } private: // transformer tap regulator parameters diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index 934dd722f..dc52ff66d 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -335,19 +335,25 @@ inline auto output_result(Component const& fault, MainModelState Component, class ComponentContainer, steady_state_solver_output_type SolverOutputType> requires model_component_state_c -constexpr auto output_result(Component const& /* transformer_tap_regulator */, - MainModelState const& /* state */, - std::vector const& /* solver_output */, Idx const /* obj_seq */) { - // TODO: this function is not implemented - using sym = typename SolverOutputType::sym; - return typename TransformerTapRegulator::OutputType{}; +constexpr auto output_result(Component const& transformer_tap_regulator, + MainModelState const& state, + MathOutput> const& math_output, Idx const /* obj_seq */) { + if (!transformer_tap_regulator.status()) { + return transformer_tap_regulator.get_null_output(); + } + for (const auto& [index, tap_pos] : math_output.optimizer_output.transformer_tap_positions) { + if (transformer_tap_regulator.regulated_object() == state.components.template get_item(index).id()) { + return transformer_tap_regulator.get_output(tap_pos); + } + } + return transformer_tap_regulator.get_null_output(); } template Component, class ComponentContainer, short_circuit_solver_output_type SolverOutputType> requires model_component_state_c -inline auto output_result(Component const& transformer_tap_regulator, - MainModelState const& /* state */, - std::vector const& /* solver_output */, Idx const /* obj_seq */) { +constexpr auto +output_result(Component const& transformer_tap_regulator, MainModelState const& /* state */, + MathOutput> const& /* math_output */, Idx const /* obj_seq */) { return transformer_tap_regulator.get_null_sc_output(); } @@ -415,9 +421,25 @@ constexpr ResIt output_result(MainModelState const& state, return output_result(component, math_output.solver_output, math_id); }); } +template Component, class ComponentContainer, typename SolverOutputType, + std::forward_iterator ResIt> + requires model_component_state_c && + requires(Component const& component, MainModelState const& state, + MathOutput const& math_output) { + { + output_result(component, state, math_output) + } -> std::convertible_to>; + } +constexpr ResIt output_result(MainModelState const& state, + MathOutput const& math_output, ResIt res_it) { + return detail::produce_output( + state, res_it, [&state, &math_output](Component const& component, Idx const obj_seq) { + return output_result(component, state, math_output, obj_seq); + }); +} // output source, load_gen, shunt individually -template Component, class ComponentContainer, solver_output_type SolverOutputType, +template Component, class ComponentContainer, solver_output_type SolverOutputType, std::forward_iterator ResIt> requires model_component_state_c constexpr ResIt output_result(MainModelState const& state, From 8f6104c41ec028ec9b529d099a50f81f53371230 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 16:34:39 +0200 Subject: [PATCH 28/39] output tap position Signed-off-by: Martijn Govers --- .../component/transformer_tap_regulator.hpp | 3 +-- .../include/power_grid_model/main_core/output.hpp | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp index 9a6a46fed..25ccde98f 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp @@ -59,7 +59,6 @@ class TransformerTapRegulator : public Regulator { constexpr TransformerTapRegulatorOutput get_null_output() const { return {.id = id(), .energized = 0, .tap_pos = na_IntS}; } - constexpr TransformerTapRegulatorOutput get_output(IntS const& tap_pos) const { TransformerTapRegulatorOutput output{}; output.id = id(); @@ -67,6 +66,7 @@ class TransformerTapRegulator : public Regulator { output.tap_pos = tap_pos; return output; } + constexpr RegulatorShortCircuitOutput get_null_sc_output() const { return {.id = id(), .energized = 0}; } template TransformerTapRegulatorCalcParam calc_param() const { TransformerTapRegulatorCalcParam param{}; @@ -81,7 +81,6 @@ class TransformerTapRegulator : public Regulator { // getter ControlSide control_side() const { return control_side_; } - RegulatorShortCircuitOutput get_null_output() const { return {.id = id(), .energized = 0}; } private: // transformer tap regulator parameters diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index dc52ff66d..aa419ade7 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -425,9 +425,9 @@ template Component, class ComponentContainer, typename std::forward_iterator ResIt> requires model_component_state_c && requires(Component const& component, MainModelState const& state, - MathOutput const& math_output) { + MathOutput const& math_output, Idx const obj_seq) { { - output_result(component, state, math_output) + output_result(component, state, math_output, obj_seq) } -> std::convertible_to>; } constexpr ResIt output_result(MainModelState const& state, @@ -439,7 +439,7 @@ constexpr ResIt output_result(MainModelState const& state, } // output source, load_gen, shunt individually -template Component, class ComponentContainer, solver_output_type SolverOutputType, +template Component, class ComponentContainer, solver_output_type SolverOutputType, std::forward_iterator ResIt> requires model_component_state_c constexpr ResIt output_result(MainModelState const& state, From 8fd28fc5ba3ba6173ce68fbb6b2d562426f9702b Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 17:06:16 +0200 Subject: [PATCH 29/39] pos is an ID??? Signed-off-by: Martijn Govers --- .../include/power_grid_model/main_core/output.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index aa419ade7..1828d6111 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -342,7 +342,7 @@ constexpr auto output_result(Component const& transformer_tap_regulator, return transformer_tap_regulator.get_null_output(); } for (const auto& [index, tap_pos] : math_output.optimizer_output.transformer_tap_positions) { - if (transformer_tap_regulator.regulated_object() == state.components.template get_item(index).id()) { + if (transformer_tap_regulator.regulated_object() == index.pos) { // TODO(mgovers): pos should not act as an ID return transformer_tap_regulator.get_output(tap_pos); } } From 34f59b3281c80ea24a2126e4737d10432e1d1078 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 17:18:35 +0200 Subject: [PATCH 30/39] remove constexpr Signed-off-by: Martijn Govers --- .../power_grid_model/component/transformer_tap_regulator.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp index 25ccde98f..109d974d9 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_tap_regulator.hpp @@ -59,7 +59,7 @@ class TransformerTapRegulator : public Regulator { constexpr TransformerTapRegulatorOutput get_null_output() const { return {.id = id(), .energized = 0, .tap_pos = na_IntS}; } - constexpr TransformerTapRegulatorOutput get_output(IntS const& tap_pos) const { + TransformerTapRegulatorOutput get_output(IntS const& tap_pos) const { TransformerTapRegulatorOutput output{}; output.id = id(); output.energized = static_cast(energized(true)); From 05e3d272636400d78a1f584f09c326abc2be8232 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Thu, 23 May 2024 17:35:51 +0200 Subject: [PATCH 31/39] fix clang Signed-off-by: Martijn Govers --- .../include/power_grid_model/main_core/output.hpp | 2 +- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index 1828d6111..d4ebafca8 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -336,7 +336,7 @@ template Component, class ComponentC steady_state_solver_output_type SolverOutputType> requires model_component_state_c constexpr auto output_result(Component const& transformer_tap_regulator, - MainModelState const& state, + MainModelState const& /* state */, MathOutput> const& math_output, Idx const /* obj_seq */) { if (!transformer_tap_regulator.status()) { return transformer_tap_regulator.get_null_output(); diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index eb77d2494..00bd0a348 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -566,8 +566,7 @@ constexpr void get_transformer_tap_positions(main_core::MainModelState(); for (auto const& transformer : state.components.template citer()) { - transformer_tap_positions.emplace_back(Idx2D{group_index, transformer.id()}, - narrow_cast(transformer.tap_pos())); + transformer_tap_positions.push_back({Idx2D{group_index, transformer.id()}, transformer.tap_pos()}); } } From c224f12d7edadda4d68614c3efa255e3e95d8d49 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 08:42:30 +0200 Subject: [PATCH 32/39] only output regulated tap positions Signed-off-by: Martijn Govers --- .../calculation_parameters.hpp | 2 +- .../power_grid_model/main_core/output.hpp | 14 +++++----- .../optimizer/tap_position_optimizer.hpp | 28 +++++++------------ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index 10b89a634..e8fb41a67 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -127,7 +127,7 @@ struct TransformerTapRegulatorCalcParam { }; struct TransformerTapPosition { - Idx2D index{}; + ID transformer{}; IntS tap_position{}; }; using TransformerTapPositionResult = std::vector; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index d4ebafca8..93fbfd23e 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -338,13 +338,13 @@ template Component, class ComponentC constexpr auto output_result(Component const& transformer_tap_regulator, MainModelState const& /* state */, MathOutput> const& math_output, Idx const /* obj_seq */) { - if (!transformer_tap_regulator.status()) { - return transformer_tap_regulator.get_null_output(); - } - for (const auto& [index, tap_pos] : math_output.optimizer_output.transformer_tap_positions) { - if (transformer_tap_regulator.regulated_object() == index.pos) { // TODO(mgovers): pos should not act as an ID - return transformer_tap_regulator.get_output(tap_pos); - } + if (auto const it = std::ranges::find_if( + math_output.optimizer_output.transformer_tap_positions, + [regulated_object = transformer_tap_regulator.regulated_object()](auto const& transformer_tap_pos) { + return transformer_tap_pos.transformer == regulated_object; + }); + it != std::end(math_output.optimizer_output.transformer_tap_positions)) { + return transformer_tap_regulator.get_output(it->tap_position); } return transformer_tap_regulator.get_null_output(); } diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 00bd0a348..07c1bb7be 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -560,16 +560,6 @@ inline void create_tap_regulator_output(State const& state, std::vector - requires main_core::model_component_state_c -constexpr void get_transformer_tap_positions(main_core::MainModelState const& state, - TransformerTapPositionResult& transformer_tap_positions) { - constexpr auto group_index = ComponentContainer::template get_type_idx(); - for (auto const& transformer : state.components.template citer()) { - transformer_tap_positions.push_back({Idx2D{group_index, transformer.id()}, transformer.tap_pos()}); - } -} - template class TapPositionOptimizerImpl; template @@ -603,7 +593,6 @@ class TapPositionOptimizerImpl, StateCalculator, : calculate_{std::move(calculator)}, update_{std::move(updater)}, strategy_{strategy} {} auto optimize(State const& state, CalculationMethod method) -> MathOutput final { - auto const order = regulator_mapping(state, TransformerRanker{}(state)); auto const cache = this->cache_states(order); @@ -621,21 +610,24 @@ class TapPositionOptimizerImpl, StateCalculator, initialize(regulator_order); if (auto result = iterate_with_fallback(state, regulator_order, method); strategy_ == OptimizerStrategy::any) { - return produce_output(state, std::move(result)); + return produce_output(regulator_order, std::move(result)); } // refine solution exploit_neighborhood(regulator_order); - return produce_output(state, iterate_with_fallback(state, regulator_order, method)); + return produce_output(regulator_order, iterate_with_fallback(state, regulator_order, method)); } - auto produce_output(State const& state, ResultType solver_output) const -> MathOutput { + auto produce_output(std::vector> const& regulator_order, + ResultType solver_output) const -> MathOutput { TransformerTapPositionResult transformer_tap_positions; - // TODO(mgovers): only output the transformers that are regulated - (get_transformer_tap_positions(state, - transformer_tap_positions), - ...); + for (auto const& sub_order : regulator_order) { + for (auto const& regulator : sub_order) { + auto const& transformer = regulator.transformer; + transformer_tap_positions.push_back({transformer.id(), transformer.tap_pos()}); + } + } return {.solver_output = {std::move(solver_output)}, .optimizer_output = {std::move(transformer_tap_positions)}}; From 6d448b0e2d29388aca9c38210ead3a637830a321 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 08:58:11 +0200 Subject: [PATCH 33/39] remove deprecated test Signed-off-by: Martijn Govers --- .../test_tap_position_optimizer.cpp | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 00ef0636c..8f5cbb6c3 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -990,47 +990,6 @@ TEST_CASE("Test tap position optmizer I/O") { bad_regulators.end(), 50.0), DuplicativelyRegulatedObject); } - - SUBCASE("transformer tap position retrieval") { - // Minimum grid for only transformer tap position optimizer I/O - test::TestState state_; - std::vector nodes{{0, 150e3}, {1, 10e3}, {2, 10e3}, {3, 10e3}, {4, 10e3}, - {5, 50e3}, {6, 10e3}, {7, 10e3}, {8, 10e3}, {9, 10e3}}; - main_core::add_component(state_, nodes.begin(), nodes.end(), 50.0); - - std::vector transformers{ - test::get_transformer(11, 0, 1, BranchSide::from, 0), test::get_transformer(12, 0, 1, BranchSide::from, -1), - test::get_transformer(13, 5, 7, BranchSide::from, 1), test::get_transformer(14, 2, 3, BranchSide::from, -2), - test::get_transformer(15, 8, 9, BranchSide::from, 2)}; - main_core::add_component(state_, transformers.begin(), transformers.end(), 50.0); - - std::vector transformers3w{test::get_transformer3w(16, 0, 4, 5, 3)}; - main_core::add_component(state_, transformers3w.begin(), transformers3w.end(), 50.0); - - state_.components.set_construction_complete(); - - TransformerTapPositionResult transformer_tap_positions; - power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< - Transformer, test::TestState::ComponentContainer>(state_, transformer_tap_positions); - power_grid_model::optimizer::tap_position_optimizer::get_transformer_tap_positions< - ThreeWindingTransformer, test::TestState::ComponentContainer>(state_, transformer_tap_positions); - std::vector expected_tap_positions{ - {{3, 11}, 0}, {{3, 12}, -1}, {{3, 13}, 1}, {{3, 14}, -2}, {{3, 15}, 2}, {{4, 16}, 3}, - }; - auto check_tap_positions_match = [](const std::vector& vec1, - const std::vector& vec2) { - if (vec1.size() != vec2.size()) { - return false; - } - for (size_t i = 0; i < vec1.size(); ++i) { - if (vec1[i].index != vec2[i].index || vec1[i].tap_position != vec2[i].tap_position) { - return false; - } - } - return true; - }; - CHECK(check_tap_positions_match(transformer_tap_positions, expected_tap_positions)); - } } } // namespace power_grid_model From 39e45019c32c295581e6cbfefb5bd69073e84daa Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 09:36:14 +0200 Subject: [PATCH 34/39] check for optimal output of auto tap pos Signed-off-by: Martijn Govers --- .../optimizer/tap_position_optimizer.hpp | 8 ------- .../test_tap_position_optimizer.cpp | 22 ++++++++++++++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 07c1bb7be..19594761d 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -552,14 +552,6 @@ template struct NodeState { } }; -template -inline void create_tap_regulator_output(State const& state, std::vector& solver_output) { - for (Idx const group : boost::counting_range(Idx{0}, static_cast(solver_output.size()))) { - solver_output[group].transformer_tap_regulator.resize(state.math_topology[group]->n_transformer_tap_regulator(), - {.tap_pos = na_IntS}); - } -} - template class TapPositionOptimizerImpl; template diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index 8f5cbb6c3..e72c90957 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -930,16 +930,32 @@ TEST_CASE("Test Tap position optimizer") { auto optimizer = get_optimizer(strategy); auto const result = optimizer.optimize(state, CalculationMethod::default_method); - auto get_state_tap_pos = [&](const ID id) { - REQUIRE(result.solver_output.size() > 0); + auto const get_state_tap_pos = [&](const ID id) { + REQUIRE(!result.solver_output.empty()); return result.solver_output.front().state_tap_positions.at(id); }; + auto const get_output_tap_pos = [&](const ID id) { + REQUIRE(!result.optimizer_output.transformer_tap_positions.empty()); + auto const it = std::ranges::find_if(result.optimizer_output.transformer_tap_positions, + [id](auto const& x) { return x.transformer == id; }); + REQUIRE(it != std::end(result.optimizer_output.transformer_tap_positions)); + CHECK(it->transformer == id); + return it->tap_position; + }; - // correctness + // check optimal state CHECK(result.solver_output.size() == 1); check_a(get_state_tap_pos(state_a.id), strategy); check_b(get_state_tap_pos(state_b.id), strategy); + // check optimal output + if (state_a.rank != MockTransformerState::unregulated) { + check_a(get_output_tap_pos(state_a.id), strategy); + } + if (state_b.rank != MockTransformerState::unregulated) { + check_b(get_output_tap_pos(state_b.id), strategy); + } + // reset CHECK(transformer_a.tap_pos() == initial_a); CHECK(transformer_b.tap_pos() == initial_b); From 4fd52b673c02dfd275458643e6210c5e8d0427e7 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 09:55:07 +0200 Subject: [PATCH 35/39] resolve comments Signed-off-by: Martijn Govers --- .../include/power_grid_model/calculation_parameters.hpp | 2 +- .../include/power_grid_model/main_core/output.hpp | 2 +- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 3 ++- tests/cpp_unit_tests/test_tap_position_optimizer.cpp | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index e8fb41a67..93c21302e 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -127,7 +127,7 @@ struct TransformerTapRegulatorCalcParam { }; struct TransformerTapPosition { - ID transformer{}; + ID transformer_id{}; IntS tap_position{}; }; using TransformerTapPositionResult = std::vector; diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp index 93fbfd23e..2e6948d4c 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/main_core/output.hpp @@ -341,7 +341,7 @@ constexpr auto output_result(Component const& transformer_tap_regulator, if (auto const it = std::ranges::find_if( math_output.optimizer_output.transformer_tap_positions, [regulated_object = transformer_tap_regulator.regulated_object()](auto const& transformer_tap_pos) { - return transformer_tap_pos.transformer == regulated_object; + return transformer_tap_pos.transformer_id == regulated_object; }); it != std::end(math_output.optimizer_output.transformer_tap_positions)) { return transformer_tap_regulator.get_output(it->tap_position); diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 19594761d..f29c4db80 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -617,7 +617,8 @@ class TapPositionOptimizerImpl, StateCalculator, for (auto const& sub_order : regulator_order) { for (auto const& regulator : sub_order) { auto const& transformer = regulator.transformer; - transformer_tap_positions.push_back({transformer.id(), transformer.tap_pos()}); + transformer_tap_positions.push_back( + {.transformer_id = transformer.id(), .tap_position = transformer.tap_pos()}); } } diff --git a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp index e72c90957..38650af05 100644 --- a/tests/cpp_unit_tests/test_tap_position_optimizer.cpp +++ b/tests/cpp_unit_tests/test_tap_position_optimizer.cpp @@ -937,9 +937,9 @@ TEST_CASE("Test Tap position optimizer") { auto const get_output_tap_pos = [&](const ID id) { REQUIRE(!result.optimizer_output.transformer_tap_positions.empty()); auto const it = std::ranges::find_if(result.optimizer_output.transformer_tap_positions, - [id](auto const& x) { return x.transformer == id; }); + [id](auto const& x) { return x.transformer_id == id; }); REQUIRE(it != std::end(result.optimizer_output.transformer_tap_positions)); - CHECK(it->transformer == id); + CHECK(it->transformer_id == id); return it->tap_position; }; From 07e4c13e1315e96d1755dc9af29e31559e99469b Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 10:46:21 +0200 Subject: [PATCH 36/39] add tests for main core tap regulator output Signed-off-by: Martijn Govers --- tests/cpp_unit_tests/CMakeLists.txt | 1 + tests/cpp_unit_tests/test_load_gen.cpp | 2 +- .../cpp_unit_tests/test_main_core_output.cpp | 88 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/cpp_unit_tests/test_main_core_output.cpp diff --git a/tests/cpp_unit_tests/CMakeLists.txt b/tests/cpp_unit_tests/CMakeLists.txt index ed9806ff9..836fe4f5b 100644 --- a/tests/cpp_unit_tests/CMakeLists.txt +++ b/tests/cpp_unit_tests/CMakeLists.txt @@ -36,6 +36,7 @@ set(PROJECT_SOURCES "test_transformer_tap_regulator.cpp" "test_optimizer.cpp" "test_tap_position_optimizer.cpp" + "test_main_core_output.cpp" ) add_executable(power_grid_model_unit_tests ${PROJECT_SOURCES}) diff --git a/tests/cpp_unit_tests/test_load_gen.cpp b/tests/cpp_unit_tests/test_load_gen.cpp index 0c05f2b17..8ec035adf 100644 --- a/tests/cpp_unit_tests/test_load_gen.cpp +++ b/tests/cpp_unit_tests/test_load_gen.cpp @@ -450,4 +450,4 @@ TEST_CASE_TEMPLATE("Test load generator", LoadGeneratorType, SymLoad, AsymLoad, } } } -} // namespace power_grid_model \ No newline at end of file +} // namespace power_grid_model diff --git a/tests/cpp_unit_tests/test_main_core_output.cpp b/tests/cpp_unit_tests/test_main_core_output.cpp new file mode 100644 index 000000000..b21469b13 --- /dev/null +++ b/tests/cpp_unit_tests/test_main_core_output.cpp @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#include +#include +#include + +#include + +namespace power_grid_model::main_core { +namespace test { +namespace {} // namespace +} // namespace test + +TEST_CASE("Test main core output") { + SUBCASE("TransformerTapRegulator") { + using ComponentContainer = Container, TransformerTapRegulator>; + using State = MainModelState; + using SymOutput = MathOutput>>; + using AsymOutput = MathOutput>>; + + State state; + emplace_component( + state, 0, TransformerTapRegulatorInput{.id = 0, .regulated_object = 2}, ComponentType::test, 10e3); + emplace_component( + state, 1, TransformerTapRegulatorInput{.id = 1, .regulated_object = 3}, ComponentType::test, 10e3); + state.components.set_construction_complete(); + + auto comp_topo = std::make_shared(); + comp_topo->regulated_object_idx = {2, 3}; + state.comp_topo = std::make_shared(std::move(*comp_topo)); + + std::vector output(state.components.template size()); + + SUBCASE("No regulation") { + SUBCASE("Symmetric") { + output_result(state, SymOutput{}, std::begin(output)); + } + SUBCASE("Asymmetric") { + output_result(state, AsymOutput{}, std::begin(output)); + } + CHECK(output[0].id == 0); + CHECK(output[0].energized == 0); + CHECK(output[0].tap_pos == na_IntS); + CHECK(output[1].id == 1); + CHECK(output[1].energized == 0); + CHECK(output[1].tap_pos == na_IntS); + } + SUBCASE("One regulated") { + OptimizerOutput optimizer_output{.transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}}}; + SUBCASE("Symmetric") { + output_result( + state, SymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + } + SUBCASE("Asymmetric") { + output_result( + state, AsymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + } + CHECK(output[0].id == 0); + CHECK(output[0].energized == 0); + CHECK(output[0].tap_pos == na_IntS); + CHECK(output[1].id == 1); + CHECK(output[1].energized == 1); + CHECK(output[1].tap_pos == 1); + } + SUBCASE("Two regulated") { + OptimizerOutput optimizer_output{.transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}, + {.transformer_id = 4, .tap_position = 2}, + {.transformer_id = 2, .tap_position = 3}}}; + SUBCASE("Symmetric") { + output_result( + state, SymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + } + SUBCASE("Asymmetric") { + output_result( + state, AsymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + } + CHECK(output[0].id == 0); + CHECK(output[0].energized == 1); + CHECK(output[0].tap_pos == 3); + CHECK(output[1].id == 1); + CHECK(output[1].energized == 1); + CHECK(output[1].tap_pos == 1); + } + } +} +} // namespace power_grid_model::main_core From b3132d61709cf530de2c32e7d4432280d74d4bd7 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 10:49:17 +0200 Subject: [PATCH 37/39] resolve comments Signed-off-by: Martijn Govers --- .../include/power_grid_model/calculation_parameters.hpp | 4 ++-- .../power_grid_model/optimizer/tap_position_optimizer.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp index 93c21302e..0b0809e38 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp @@ -130,7 +130,7 @@ struct TransformerTapPosition { ID transformer_id{}; IntS tap_position{}; }; -using TransformerTapPositionResult = std::vector; +using TransformerTapPositionOutput = std::vector; // from side, to side // in case of indices for math model, -1 means the branch is not connected to that side @@ -326,7 +326,7 @@ static_assert(short_circuit_solver_output_type>); struct OptimizerOutput { - TransformerTapPositionResult transformer_tap_positions; + TransformerTapPositionOutput transformer_tap_positions; }; template struct MathOutput { diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index f29c4db80..88357a913 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -612,7 +612,7 @@ class TapPositionOptimizerImpl, StateCalculator, auto produce_output(std::vector> const& regulator_order, ResultType solver_output) const -> MathOutput { - TransformerTapPositionResult transformer_tap_positions; + TransformerTapPositionOutput transformer_tap_positions; for (auto const& sub_order : regulator_order) { for (auto const& regulator : sub_order) { From 591a56f60aba50f7d9ff8403f283f6b431800667 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 11:09:57 +0200 Subject: [PATCH 38/39] fix gcc Signed-off-by: Martijn Govers --- tests/cpp_unit_tests/test_main_core_output.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/cpp_unit_tests/test_main_core_output.cpp b/tests/cpp_unit_tests/test_main_core_output.cpp index b21469b13..fc04cadb5 100644 --- a/tests/cpp_unit_tests/test_main_core_output.cpp +++ b/tests/cpp_unit_tests/test_main_core_output.cpp @@ -51,11 +51,13 @@ TEST_CASE("Test main core output") { OptimizerOutput optimizer_output{.transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}}}; SUBCASE("Symmetric") { output_result( - state, SymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + state, SymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, + std::begin(output)); } SUBCASE("Asymmetric") { output_result( - state, AsymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + state, AsymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, + std::begin(output)); } CHECK(output[0].id == 0); CHECK(output[0].energized == 0); @@ -70,11 +72,13 @@ TEST_CASE("Test main core output") { {.transformer_id = 2, .tap_position = 3}}}; SUBCASE("Symmetric") { output_result( - state, SymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + state, SymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, + std::begin(output)); } SUBCASE("Asymmetric") { output_result( - state, AsymOutput{.optimizer_output = std::move(optimizer_output)}, std::begin(output)); + state, AsymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, + std::begin(output)); } CHECK(output[0].id == 0); CHECK(output[0].energized == 1); From 6a0d61479e71f7015e598d4d668aac7a5f3c7c0d Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Fri, 24 May 2024 11:41:59 +0200 Subject: [PATCH 39/39] resolve sonar cloud Signed-off-by: Martijn Govers --- .../optimizer/tap_position_optimizer.hpp | 2 +- .../cpp_unit_tests/test_main_core_output.cpp | 26 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp index 88357a913..4d5bfc25e 100644 --- a/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp +++ b/power_grid_model_c/power_grid_model/include/power_grid_model/optimizer/tap_position_optimizer.hpp @@ -336,7 +336,7 @@ template class TransformerWrapper { constexpr auto index() const { return index_; } constexpr auto topology_index() const { return topology_index_; } - IntS id() const { + ID id() const { return apply([](auto const& t) { return t.id(); }); } IntS tap_pos() const { diff --git a/tests/cpp_unit_tests/test_main_core_output.cpp b/tests/cpp_unit_tests/test_main_core_output.cpp index fc04cadb5..bb657eeb5 100644 --- a/tests/cpp_unit_tests/test_main_core_output.cpp +++ b/tests/cpp_unit_tests/test_main_core_output.cpp @@ -9,10 +9,6 @@ #include namespace power_grid_model::main_core { -namespace test { -namespace {} // namespace -} // namespace test - TEST_CASE("Test main core output") { SUBCASE("TransformerTapRegulator") { using ComponentContainer = Container, TransformerTapRegulator>; @@ -48,16 +44,15 @@ TEST_CASE("Test main core output") { CHECK(output[1].tap_pos == na_IntS); } SUBCASE("One regulated") { - OptimizerOutput optimizer_output{.transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}}}; + OptimizerOutput const optimizer_output{ + .transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}}}; SUBCASE("Symmetric") { output_result( - state, SymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, - std::begin(output)); + state, SymOutput{.solver_output = {}, .optimizer_output = optimizer_output}, std::begin(output)); } SUBCASE("Asymmetric") { output_result( - state, AsymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, - std::begin(output)); + state, AsymOutput{.solver_output = {}, .optimizer_output = optimizer_output}, std::begin(output)); } CHECK(output[0].id == 0); CHECK(output[0].energized == 0); @@ -67,18 +62,17 @@ TEST_CASE("Test main core output") { CHECK(output[1].tap_pos == 1); } SUBCASE("Two regulated") { - OptimizerOutput optimizer_output{.transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}, - {.transformer_id = 4, .tap_position = 2}, - {.transformer_id = 2, .tap_position = 3}}}; + OptimizerOutput const optimizer_output{ + .transformer_tap_positions = {{.transformer_id = 3, .tap_position = 1}, + {.transformer_id = 4, .tap_position = 2}, + {.transformer_id = 2, .tap_position = 3}}}; SUBCASE("Symmetric") { output_result( - state, SymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, - std::begin(output)); + state, SymOutput{.solver_output = {}, .optimizer_output = optimizer_output}, std::begin(output)); } SUBCASE("Asymmetric") { output_result( - state, AsymOutput{.solver_output = {}, .optimizer_output = std::move(optimizer_output)}, - std::begin(output)); + state, AsymOutput{.solver_output = {}, .optimizer_output = optimizer_output}, std::begin(output)); } CHECK(output[0].id == 0); CHECK(output[0].energized == 1);