-
Notifications
You must be signed in to change notification settings - Fork 53
Main model refactoring: move calculation logic out of main model #1286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mgovers
wants to merge
12
commits into
main
Choose a base branch
from
feature/calculation-out-of-main-model
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−142
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fd7ecde
split calculation typing out of main model impl
mgovers 6908f46
move calculator implementation detail logic out of main model impl (wip)
mgovers fc1078a
Merge remote-tracking branch 'origin/main' into feature/calculation-o…
mgovers d6ac715
homogenize
mgovers 4f61d49
more homogenize
mgovers 6ca9d39
cleanup
mgovers 0713681
Merge branch 'main' into feature/calculation-out-of-main-model
mgovers 118fcae
use custom enumerate because views::enumerate not available on all pl…
mgovers 897fa99
use views enumerate overload when not provided
mgovers 5cddc0d
sonar
mgovers 2afbb9c
Merge branch 'main' into feature/calculation-out-of-main-model
mgovers 356b2dd
clang-tidy
mgovers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
power_grid_model_c/power_grid_model/include/power_grid_model/calculation.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org> | ||
| // | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "main_model_fwd.hpp" | ||
|
|
||
| #include "common/common.hpp" | ||
| #include "common/enum.hpp" | ||
|
|
||
| #include "main_core/calculation_input_preparation.hpp" | ||
| #include "main_core/main_model_type.hpp" | ||
|
|
||
| namespace power_grid_model { | ||
|
|
||
| struct calculation_type_t {}; | ||
| struct power_flow_t : calculation_type_t {}; | ||
| struct state_estimation_t : calculation_type_t {}; | ||
| struct short_circuit_t : calculation_type_t {}; | ||
|
|
||
| template <typename T> | ||
| concept calculation_type_tag = std::derived_from<T, calculation_type_t>; | ||
|
|
||
| template <class Functor, class... Args> | ||
| decltype(auto) calculation_symmetry_func_selector(CalculationSymmetry calculation_symmetry, Functor&& f, | ||
| Args&&... args) { | ||
| using enum CalculationSymmetry; | ||
|
|
||
| switch (calculation_symmetry) { | ||
| case symmetric: | ||
| return std::forward<Functor>(f).template operator()<symmetric_t>(std::forward<Args>(args)...); | ||
| case asymmetric: | ||
| return std::forward<Functor>(f).template operator()<asymmetric_t>(std::forward<Args>(args)...); | ||
| default: | ||
| throw MissingCaseForEnumError{"Calculation symmetry selector", calculation_symmetry}; | ||
| } | ||
| } | ||
|
|
||
| template <class Functor, class... Args> | ||
| decltype(auto) calculation_type_func_selector(CalculationType calculation_type, Functor&& f, Args&&... args) { | ||
| using enum CalculationType; | ||
|
|
||
| switch (calculation_type) { | ||
| case CalculationType::power_flow: | ||
| return std::forward<Functor>(f).template operator()<power_flow_t>(std::forward<Args>(args)...); | ||
| case CalculationType::state_estimation: | ||
| return std::forward<Functor>(f).template operator()<state_estimation_t>(std::forward<Args>(args)...); | ||
| case CalculationType::short_circuit: | ||
| return std::forward<Functor>(f).template operator()<short_circuit_t>(std::forward<Args>(args)...); | ||
| default: | ||
| throw MissingCaseForEnumError{"CalculationType", calculation_type}; | ||
| } | ||
| } | ||
|
|
||
| template <class Functor, class... Args> | ||
| decltype(auto) calculation_type_symmetry_func_selector(CalculationType calculation_type, | ||
| CalculationSymmetry calculation_symmetry, Functor&& f, | ||
| Args&&... args) { | ||
| calculation_type_func_selector( | ||
| calculation_type, | ||
| []<calculation_type_tag calculation_type, typename Functor_, typename... Args_>( | ||
| CalculationSymmetry calculation_symmetry_, Functor_&& f_, Args_&&... args_) { | ||
| calculation_symmetry_func_selector( | ||
| calculation_symmetry_, | ||
| []<symmetry_tag sym, typename SubFunctor, typename... SubArgs>(SubFunctor&& sub_f, | ||
| SubArgs&&... sub_args) { | ||
| std::forward<SubFunctor>(sub_f).template operator()<calculation_type, sym>( | ||
| std::forward<SubArgs>(sub_args)...); | ||
| }, | ||
| std::forward<Functor_>(f_), std::forward<Args_>(args_)...); | ||
| }, | ||
| calculation_symmetry, std::forward<Functor>(f), std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| template <typename T, typename sym> struct Calculator; | ||
| template <symmetry_tag sym> struct Calculator<power_flow_t, sym> { | ||
| template <typename State> | ||
| static auto preparer(State const& state, ComponentToMathCoupling& /*comp_coup*/, | ||
| MainModelOptions const& /*options*/) { | ||
| return [&state](Idx n_math_solvers) { return main_core::prepare_power_flow_input<sym>(state, n_math_solvers); }; | ||
| } | ||
| static auto solver(CalculationMethod calculation_method, MainModelOptions const& options, Logger& logger) { | ||
| return [calculation_method, err_tol = options.err_tol, max_iter = options.max_iter, | ||
| &logger](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus, PowerFlowInput<sym> const& input) { | ||
| return solver.get().run_power_flow(input, err_tol, max_iter, logger, calculation_method, y_bus); | ||
| }; | ||
| } | ||
| }; | ||
| template <symmetry_tag sym> struct Calculator<state_estimation_t, sym> { | ||
| template <typename State> | ||
| static auto preparer(State const& state, ComponentToMathCoupling& /*comp_coup*/, | ||
| MainModelOptions const& /*options*/) { | ||
| return [&state](Idx n_math_solvers) { | ||
| return main_core::prepare_state_estimation_input<sym>(state, n_math_solvers); | ||
| }; | ||
| } | ||
| static auto solver(CalculationMethod calculation_method, MainModelOptions const& options, Logger& logger) { | ||
| return [calculation_method, err_tol = options.err_tol, max_iter = options.max_iter, | ||
| &logger](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus, StateEstimationInput<sym> const& input) { | ||
| return solver.get().run_state_estimation(input, err_tol, max_iter, logger, calculation_method, y_bus); | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| template <symmetry_tag sym> struct Calculator<short_circuit_t, sym> { | ||
| template <typename State> | ||
| static auto preparer(State const& state, ComponentToMathCoupling& comp_coup, MainModelOptions const& options) { | ||
| return [&state, &comp_coup, voltage_scaling = options.short_circuit_voltage_scaling](Idx n_math_solvers) { | ||
| return main_core::prepare_short_circuit_input<sym>(state, comp_coup, n_math_solvers, voltage_scaling); | ||
| }; | ||
| } | ||
| static auto solver(CalculationMethod calculation_method, MainModelOptions const& /*options*/, Logger& logger) { | ||
| return [calculation_method, &logger](MathSolverProxy<sym>& solver, YBus<sym> const& y_bus, | ||
| ShortCircuitInput const& input) { | ||
| return solver.get().run_short_circuit(input, logger, calculation_method, y_bus); | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace power_grid_model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.