-
Notifications
You must be signed in to change notification settings - Fork 46
Clean up main model: migrate out "prepare calculation" related stuff #1168
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
c8bfaa5
ff6b631
5d985b7
0ef04c6
7ce6e95
a9ae335
4f0d589
9b1fa1e
66e5449
7c1cab5
cf50d51
4513b6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
| // | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "topology.hpp" | ||
|
|
||
| #include "common/common.hpp" | ||
|
|
||
| #include "math_solver/math_solver_dispatch.hpp" | ||
|
|
||
| #include "main_core/main_model_type.hpp" | ||
| #include "main_core/math_state.hpp" | ||
| #include "main_core/topology.hpp" | ||
| #include "main_core/y_bus.hpp" | ||
|
|
||
| namespace power_grid_model { | ||
| struct SolverPreparationContext { | ||
| main_core::MathState math_state; | ||
| Idx n_math_solvers{0}; | ||
|
||
| MathSolverDispatcher const* math_solver_dispatcher; | ||
| }; | ||
|
|
||
| template <class ModelType> | ||
| requires(main_core::is_main_model_type_v<ModelType>) | ||
| struct StatusCheckingContext { | ||
| bool is_topology_up_to_date{false}; | ||
| bool last_updated_calculation_symmetry_mode{false}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't like this name with the fact that it's a bool. Maybe you can make it an enum? or otherwise rename to something along the lines of this is basically an invariant, namely a mapping of the previous calculation type to the next. maybe you can solve it using a setter (instead of doing the checking at the user side), e.g. class StatusCheckingContext {
template <symmetry_tag sym> set_most_recent_calculation_symmetry() {
last_updated_calculation_was_symmetric_ = is_symmetric_v<sym>;
}
template <symmetry_tag sym> is_same_calculation_symmetry() {
return last_updated_calculation_was_symmetric_ == is_symmetric_v<sym>;
}
};You can also use note that using |
||
| typename ModelType::SequenceIdx parameter_changed_components{}; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it feels a little bit confusing to me that the status and the cache are part of the same struct called Maybe the name is just not clear enough. Maybe something along the lines of That said, maybe the confusing part is not the name of the type but the fact that one thing tells something about the cache validity while the other about the context in which it is valid? maybe that means that they shouldn't be part of the same class in the first place? |
||
| struct IsParameterUpToDateHelper { | ||
| bool sym{false}; | ||
| bool asym{false}; | ||
| } is_parameter_up_to_date{}; | ||
| }; | ||
|
|
||
| namespace detail { | ||
| template <class ModelType> | ||
| void reset_solvers(typename ModelType::MainModelState& state, SolverPreparationContext& solver_context, | ||
| StatusCheckingContext<ModelType>& status_context) { | ||
| status_context.is_topology_up_to_date = false; | ||
| status_context.is_parameter_up_to_date.sym = false; | ||
| status_context.is_parameter_up_to_date.asym = false; | ||
| solver_context.n_math_solvers = 0; | ||
| main_core::clear(solver_context.math_state); | ||
| state.math_topology.clear(); | ||
| state.topo_comp_coup.reset(); | ||
| state.comp_coup = {}; | ||
| } | ||
|
|
||
| template <class ModelType> | ||
| void rebuild_topology(typename ModelType::MainModelState& state, SolverPreparationContext& solver_context, | ||
| StatusCheckingContext<ModelType>& status_context) { | ||
| // clear old solvers | ||
| reset_solvers(state, solver_context, status_context); | ||
| ComponentConnections const comp_conn = main_core::construct_components_connections<ModelType>(state.components); | ||
| // re build | ||
| Topology topology{*state.comp_topo, comp_conn}; | ||
| std::tie(state.math_topology, state.topo_comp_coup) = topology.build_topology(); | ||
| solver_context.n_math_solvers = static_cast<Idx>(state.math_topology.size()); | ||
| status_context.is_topology_up_to_date = true; | ||
| status_context.is_parameter_up_to_date.sym = false; | ||
| status_context.is_parameter_up_to_date.asym = false; | ||
| } | ||
| } // namespace detail | ||
|
|
||
| template <symmetry_tag sym, class ModelType> | ||
| bool& is_parameter_up_to_date( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i prefer to make this a member function |
||
| typename StatusCheckingContext<ModelType>::IsParameterUpToDateHelper& is_parameter_up_to_date) { | ||
| if constexpr (is_symmetric_v<sym>) { | ||
| return is_parameter_up_to_date.sym; | ||
| } else { | ||
| return is_parameter_up_to_date.asym; | ||
| } | ||
| } | ||
|
|
||
| template <symmetry_tag sym, class ModelType> | ||
| void prepare_solvers(typename ModelType::MainModelState& state, SolverPreparationContext& solver_context, | ||
| StatusCheckingContext<ModelType>& status_context) { | ||
| std::vector<MathSolverProxy<sym>>& solvers = main_core::get_solvers<sym>(solver_context.math_state); | ||
| // rebuild topology if needed | ||
| if (!status_context.is_topology_up_to_date) { | ||
| detail::rebuild_topology(state, solver_context, status_context); | ||
| } | ||
| main_core::prepare_y_bus<sym, ModelType>(state, solver_context.n_math_solvers, solver_context.math_state); | ||
|
|
||
| if (solver_context.n_math_solvers != static_cast<Idx>(solvers.size())) { | ||
| assert(solvers.empty()); | ||
| assert(solver_context.n_math_solvers == static_cast<Idx>(state.math_topology.size())); | ||
| assert(solver_context.n_math_solvers == | ||
| static_cast<Idx>(main_core::get_y_bus<sym>(solver_context.math_state).size())); | ||
|
|
||
| solvers.clear(); | ||
| solvers.reserve(solver_context.n_math_solvers); | ||
| std::ranges::transform(state.math_topology, std::back_inserter(solvers), | ||
| [&solver_context](auto const& math_topo) { | ||
| return MathSolverProxy<sym>{solver_context.math_solver_dispatcher, math_topo}; | ||
| }); | ||
| for (Idx idx = 0; idx < solver_context.n_math_solvers; ++idx) { | ||
| main_core::get_y_bus<sym>(solver_context.math_state)[idx].register_parameters_changed_callback( | ||
| [solver = std::ref(solvers[idx])](bool changed) { solver.get().get().parameters_changed(changed); }); | ||
| } | ||
| } else if (!is_parameter_up_to_date<sym, ModelType>(status_context.is_parameter_up_to_date)) { | ||
| std::vector<MathModelParam<sym>> const math_params = | ||
| main_core::get_math_param<sym>(state, solver_context.n_math_solvers); | ||
| std::vector<MathModelParamIncrement> const math_param_increments = | ||
| main_core::get_math_param_increment<ModelType>(state, solver_context.n_math_solvers, | ||
| status_context.parameter_changed_components); | ||
| if (status_context.last_updated_calculation_symmetry_mode == is_symmetric_v<sym>) { | ||
| main_core::update_y_bus(solver_context.math_state, math_params, math_param_increments); | ||
| } else { | ||
| main_core::update_y_bus(solver_context.math_state, math_params); | ||
| } | ||
| } | ||
| // else do nothing, set everything up to date | ||
| is_parameter_up_to_date<sym, ModelType>(status_context.is_parameter_up_to_date) = true; | ||
| std::ranges::for_each(status_context.parameter_changed_components, [](auto& comps) { comps.clear(); }); | ||
| status_context.last_updated_calculation_symmetry_mode = is_symmetric_v<sym>; | ||
| } | ||
| } // namespace power_grid_model | ||
Uh oh!
There was an error while loading. Please reload this page.