Skip to content

Commit e6728db

Browse files
committed
move construct topology
Signed-off-by: Nitish Bharambe <[email protected]>
1 parent ce25a1c commit e6728db

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,46 @@ constexpr void register_topology_components(MainModelState<ComponentContainer> c
191191
});
192192
}
193193

194+
ComponentTopology construct_topology(main_model_state_c auto const& state) {
195+
ComponentTopology comp_topo;
196+
register_topology_components<Node>(state, comp_topo);
197+
register_topology_components<Branch>(state, comp_topo);
198+
register_topology_components<Branch3>(state, comp_topo);
199+
register_topology_components<Source>(state, comp_topo);
200+
register_topology_components<Shunt>(state, comp_topo);
201+
register_topology_components<GenericLoadGen>(state, comp_topo);
202+
register_topology_components<GenericVoltageSensor>(state, comp_topo);
203+
register_topology_components<GenericPowerSensor>(state, comp_topo);
204+
register_topology_components<GenericCurrentSensor>(state, comp_topo);
205+
register_topology_components<Regulator>(state, comp_topo);
206+
return comp_topo;
207+
}
208+
209+
ComponentConnections construct_components_connections(main_model_state_c auto const& state) {
210+
ComponentConnections comp_conn;
211+
comp_conn.branch_connected.resize(state.comp_topo->branch_node_idx.size());
212+
comp_conn.branch_phase_shift.resize(state.comp_topo->branch_node_idx.size());
213+
comp_conn.branch3_connected.resize(state.comp_topo->branch3_node_idx.size());
214+
comp_conn.branch3_phase_shift.resize(state.comp_topo->branch3_node_idx.size());
215+
comp_conn.source_connected.resize(state.comp_topo->source_node_idx.size());
216+
std::transform(state.components.template citer<Branch>().begin(), state.components.template citer<Branch>().end(),
217+
comp_conn.branch_connected.begin(), [](Branch const& branch) {
218+
return BranchConnected{static_cast<IntS>(branch.from_status()),
219+
static_cast<IntS>(branch.to_status())};
220+
});
221+
std::transform(state.components.template citer<Branch>().begin(), state.components.template citer<Branch>().end(),
222+
comp_conn.branch_phase_shift.begin(), [](Branch const& branch) { return branch.phase_shift(); });
223+
std::transform(state.components.template citer<Branch3>().begin(), state.components.template citer<Branch3>().end(),
224+
comp_conn.branch3_connected.begin(), [](Branch3 const& branch3) {
225+
return Branch3Connected{static_cast<IntS>(branch3.status_1()),
226+
static_cast<IntS>(branch3.status_2()),
227+
static_cast<IntS>(branch3.status_3())};
228+
});
229+
std::transform(state.components.template citer<Branch3>().begin(), state.components.template citer<Branch3>().end(),
230+
comp_conn.branch3_phase_shift.begin(), [](Branch3 const& branch3) { return branch3.phase_shift(); });
231+
std::transform(state.components.template citer<Source>().begin(), state.components.template citer<Source>().end(),
232+
comp_conn.source_connected.begin(), [](Source const& source) { return source.status(); });
233+
return comp_conn;
234+
}
235+
194236
} // namespace power_grid_model::main_core

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
314314
construction_complete_ = true;
315315
#endif // !NDEBUG
316316
state_.components.set_construction_complete();
317-
construct_topology();
318-
}
319-
320-
void construct_topology() {
321-
ComponentTopology comp_topo;
322-
main_core::register_topology_components<Node>(state_, comp_topo);
323-
main_core::register_topology_components<Branch>(state_, comp_topo);
324-
main_core::register_topology_components<Branch3>(state_, comp_topo);
325-
main_core::register_topology_components<Source>(state_, comp_topo);
326-
main_core::register_topology_components<Shunt>(state_, comp_topo);
327-
main_core::register_topology_components<GenericLoadGen>(state_, comp_topo);
328-
main_core::register_topology_components<GenericVoltageSensor>(state_, comp_topo);
329-
main_core::register_topology_components<GenericPowerSensor>(state_, comp_topo);
330-
main_core::register_topology_components<GenericCurrentSensor>(state_, comp_topo);
331-
main_core::register_topology_components<Regulator>(state_, comp_topo);
332-
state_.comp_topo = std::make_shared<ComponentTopology const>(std::move(comp_topo));
317+
state_.comp_topo = std::make_shared<ComponentTopology const>(main_core::construct_topology(state_));
333318
}
334319

335320
void reset_solvers() {
@@ -652,33 +637,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
652637
assert(construction_complete_);
653638
// clear old solvers
654639
reset_solvers();
655-
// get connection info
656-
ComponentConnections comp_conn;
657-
comp_conn.branch_connected.resize(state_.comp_topo->branch_node_idx.size());
658-
comp_conn.branch_phase_shift.resize(state_.comp_topo->branch_node_idx.size());
659-
comp_conn.branch3_connected.resize(state_.comp_topo->branch3_node_idx.size());
660-
comp_conn.branch3_phase_shift.resize(state_.comp_topo->branch3_node_idx.size());
661-
comp_conn.source_connected.resize(state_.comp_topo->source_node_idx.size());
662-
std::transform(
663-
state_.components.template citer<Branch>().begin(), state_.components.template citer<Branch>().end(),
664-
comp_conn.branch_connected.begin(), [](Branch const& branch) {
665-
return BranchConnected{static_cast<IntS>(branch.from_status()), static_cast<IntS>(branch.to_status())};
666-
});
667-
std::transform(state_.components.template citer<Branch>().begin(),
668-
state_.components.template citer<Branch>().end(), comp_conn.branch_phase_shift.begin(),
669-
[](Branch const& branch) { return branch.phase_shift(); });
670-
std::transform(
671-
state_.components.template citer<Branch3>().begin(), state_.components.template citer<Branch3>().end(),
672-
comp_conn.branch3_connected.begin(), [](Branch3 const& branch3) {
673-
return Branch3Connected{static_cast<IntS>(branch3.status_1()), static_cast<IntS>(branch3.status_2()),
674-
static_cast<IntS>(branch3.status_3())};
675-
});
676-
std::transform(state_.components.template citer<Branch3>().begin(),
677-
state_.components.template citer<Branch3>().end(), comp_conn.branch3_phase_shift.begin(),
678-
[](Branch3 const& branch3) { return branch3.phase_shift(); });
679-
std::transform(state_.components.template citer<Source>().begin(),
680-
state_.components.template citer<Source>().end(), comp_conn.source_connected.begin(),
681-
[](Source const& source) { return source.status(); });
640+
ComponentConnections comp_conn = main_core::construct_components_connections(state_);
682641
// re build
683642
Topology topology{*state_.comp_topo, comp_conn};
684643
std::tie(state_.math_topology, state_.topo_comp_coup) = topology.build_topology();

0 commit comments

Comments
 (0)