Skip to content

Commit fbbaa5e

Browse files
committed
decouple topology from state
Signed-off-by: Nitish Bharambe <[email protected]>
1 parent 8597a46 commit fbbaa5e

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/container_fwd.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ concept component_container_c = requires(ContainerType const& c, ID id) {
1919
{ c.template get_item<ComponentType>(id) } -> std::convertible_to<ComponentType const&>;
2020
};
2121

22+
// TODO merge with component_container_c
2223
template <typename ContainerType, typename ComponentType>
2324
concept extended_component_container_c =
2425
component_container_c<ContainerType, ComponentType> && requires(ContainerType const& c, Idx2D const& idx2d) {

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/topology.hpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#pragma once
66

77
#include "container_queries.hpp"
8-
#include "state.hpp"
98

109
#include "../all_components.hpp"
1110

@@ -185,46 +184,52 @@ constexpr void register_topology_components(ComponentContainer const& components
185184
[](Regulator const& regulator) { return regulator.regulated_object_type(); });
186185
}
187186

188-
ComponentTopology construct_topology(main_model_state_c auto const& state) {
187+
template <typename ComponentContainer>
188+
requires multi_extended_component_container_c<ComponentContainer, Branch, Branch3, Source, Shunt, GenericLoadGen,
189+
GenericVoltageSensor, GenericPowerSensor, GenericCurrentSensor,
190+
Regulator>
191+
ComponentTopology construct_topology(ComponentContainer const& components) {
189192
ComponentTopology comp_topo;
190-
register_topology_components<Node>(state.components, comp_topo);
191-
register_topology_components<Branch>(state.components, comp_topo);
192-
register_topology_components<Branch3>(state.components, comp_topo);
193-
register_topology_components<Source>(state.components, comp_topo);
194-
register_topology_components<Shunt>(state.components, comp_topo);
195-
register_topology_components<GenericLoadGen>(state.components, comp_topo);
196-
register_topology_components<GenericVoltageSensor>(state.components, comp_topo);
197-
register_topology_components<GenericPowerSensor>(state.components, comp_topo);
198-
register_topology_components<GenericCurrentSensor>(state.components, comp_topo);
199-
register_topology_components<Regulator>(state.components, comp_topo);
193+
register_topology_components<Node>(components, comp_topo);
194+
register_topology_components<Branch>(components, comp_topo);
195+
register_topology_components<Branch3>(components, comp_topo);
196+
register_topology_components<Source>(components, comp_topo);
197+
register_topology_components<Shunt>(components, comp_topo);
198+
register_topology_components<GenericLoadGen>(components, comp_topo);
199+
register_topology_components<GenericVoltageSensor>(components, comp_topo);
200+
register_topology_components<GenericPowerSensor>(components, comp_topo);
201+
register_topology_components<GenericCurrentSensor>(components, comp_topo);
202+
register_topology_components<Regulator>(components, comp_topo);
200203
return comp_topo;
201204
}
202205

203-
ComponentConnections construct_components_connections(main_model_state_c auto const& state) {
206+
template <typename ComponentContainer>
207+
requires multi_extended_component_container_c<ComponentContainer, Branch, Branch3, Source>
208+
ComponentConnections construct_components_connections(ComponentContainer const& components) {
204209
ComponentConnections comp_conn;
205-
comp_conn.branch_connected.resize(get_component_size<Branch>(state.components));
206-
comp_conn.branch_phase_shift.resize(get_component_size<Branch>(state.components));
207-
comp_conn.branch3_connected.resize(get_component_size<Branch3>(state.components));
208-
comp_conn.branch3_phase_shift.resize(get_component_size<Branch3>(state.components));
209-
comp_conn.source_connected.resize(get_component_size<Source>(state.components));
210+
comp_conn.branch_connected.resize(get_component_size<Branch>(components));
211+
comp_conn.branch_phase_shift.resize(get_component_size<Branch>(components));
212+
comp_conn.branch3_connected.resize(get_component_size<Branch3>(components));
213+
comp_conn.branch3_phase_shift.resize(get_component_size<Branch3>(components));
214+
comp_conn.source_connected.resize(get_component_size<Source>(components));
210215
std::ranges::transform(
211-
state.components.template citer<Branch>(), comp_conn.branch_connected.begin(), [](Branch const& branch) {
216+
components.template citer<Branch>(), comp_conn.branch_connected.begin(), [](Branch const& branch) {
212217
return BranchConnected{static_cast<IntS>(branch.from_status()), static_cast<IntS>(branch.to_status())};
213218
});
214219

215-
std::ranges::transform(state.components.template citer<Branch>(), comp_conn.branch_phase_shift.begin(),
220+
std::ranges::transform(components.template citer<Branch>(), comp_conn.branch_phase_shift.begin(),
216221
[](Branch const& branch) { return branch.phase_shift(); });
217222

218223
std::ranges::transform(
219-
state.components.template citer<Branch3>(), comp_conn.branch3_connected.begin(), [](Branch3 const& branch3) {
224+
components.template citer<Branch3>(), comp_conn.branch3_connected.begin(), [](Branch3 const& branch3) {
220225
return Branch3Connected{static_cast<IntS>(branch3.status_1()), static_cast<IntS>(branch3.status_2()),
221226
static_cast<IntS>(branch3.status_3())};
222227
});
223228

224-
std::ranges::transform(state.components.template citer<Branch3>(), comp_conn.branch3_phase_shift.begin(),
229+
std::ranges::transform(components.template citer<Branch3>(), comp_conn.branch3_phase_shift.begin(),
225230
[](Branch3 const& branch3) { return branch3.phase_shift(); });
226231

227-
std::ranges::transform(state.components.template citer<Source>(), comp_conn.source_connected.begin(),
232+
std::ranges::transform(components.template citer<Source>(), comp_conn.source_connected.begin(),
228233
[](Source const& source) { return source.status(); });
229234
return comp_conn;
230235
}

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
314314
construction_complete_ = true;
315315
#endif // !NDEBUG
316316
state_.components.set_construction_complete();
317-
state_.comp_topo = std::make_shared<ComponentTopology const>(main_core::construct_topology(state_));
317+
state_.comp_topo = std::make_shared<ComponentTopology const>(main_core::construct_topology(state_.components));
318318
}
319319

320320
void reset_solvers() {
@@ -637,7 +637,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
637637
assert(construction_complete_);
638638
// clear old solvers
639639
reset_solvers();
640-
ComponentConnections const comp_conn = main_core::construct_components_connections(state_);
640+
ComponentConnections const comp_conn = main_core::construct_components_connections(state_.components);
641641
// re build
642642
Topology topology{*state_.comp_topo, comp_conn};
643643
std::tie(state_.math_topology, state_.topo_comp_coup) = topology.build_topology();

0 commit comments

Comments
 (0)