Skip to content

Commit

Permalink
Merge pull request #828 from PowerGridModel/feature/optional-id-refac…
Browse files Browse the repository at this point in the history
…tor-part2

Optional ID structural refactor
  • Loading branch information
figueroa1395 authored Nov 25, 2024
2 parents cf7cdf6 + 1f5bb5f commit 02f9fd4
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 356 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
//
// SPDX-License-Identifier: MPL-2.0

#pragma once

#include "../all_components.hpp"
#include "../container.hpp"

#include <array>
#include <vector>

namespace power_grid_model::main_core::utils {

template <class... ComponentTypes> constexpr size_t n_types = sizeof...(ComponentTypes);
template <class CompType, class... ComponentTypes>
constexpr size_t index_of_component = container_impl::get_cls_pos_v<CompType, ComponentTypes...>;

template <class... ComponentTypes> using SequenceIdx = std::array<std::vector<Idx2D>, n_types<ComponentTypes...>>;
template <class... ComponentTypes> using ComponentFlags = std::array<bool, n_types<ComponentTypes...>>;

// run functors with all component types
template <class... Types, class Functor> constexpr void run_functor_with_all_types_return_void(Functor functor) {
(functor.template operator()<Types>(), ...);
}
template <class... Types, class Functor> constexpr auto run_functor_with_all_types_return_array(Functor functor) {
return std::array { functor.template operator()<Types>()... };
}

} // namespace power_grid_model::main_core::utils
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ constexpr Idx get_component_group_idx(MainModelState<ComponentContainer> const&

template <typename ComponentType, class ComponentContainer>
requires model_component_state_c<MainModelState, ComponentContainer, ComponentType>
inline Idx get_component_sequence(MainModelState<ComponentContainer> const& state, auto const& id_or_index) {
inline Idx get_component_sequence_idx(MainModelState<ComponentContainer> const& state, auto const& id_or_index) {
return state.components.template get_seq<ComponentType>(id_or_index);
}

Expand Down Expand Up @@ -97,19 +97,19 @@ constexpr auto get_component_citer(MainModelState<ComponentContainer> const& sta
template <std::derived_from<Branch> ComponentType, class ComponentContainer>
requires model_component_state_c<MainModelState, ComponentContainer, ComponentType>
constexpr auto get_topology_index(MainModelState<ComponentContainer> const& state, auto const& id_or_index) {
return get_component_sequence<Branch>(state, id_or_index);
return get_component_sequence_idx<Branch>(state, id_or_index);
}

template <std::derived_from<Branch3> ComponentType, class ComponentContainer>
requires model_component_state_c<MainModelState, ComponentContainer, ComponentType>
constexpr auto get_topology_index(MainModelState<ComponentContainer> const& state, auto const& id_or_index) {
return get_component_sequence<Branch3>(state, id_or_index);
return get_component_sequence_idx<Branch3>(state, id_or_index);
}

template <std::derived_from<Regulator> ComponentType, class ComponentContainer>
requires model_component_state_c<MainModelState, ComponentContainer, ComponentType>
constexpr auto get_topology_index(MainModelState<ComponentContainer> const& state, auto const& id_or_index) {
return get_component_sequence<Regulator>(state, id_or_index);
return get_component_sequence_idx<Regulator>(state, id_or_index);
}

template <std::derived_from<Branch> ComponentType, class ComponentContainer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ template <std::same_as<Branch> Component, class ComponentContainer>
constexpr void register_topology_components(MainModelState<ComponentContainer> const& state,
ComponentTopology& comp_topo) {
detail::register_topo_components<Component>(state, comp_topo.branch_node_idx, [&state](Branch const& branch) {
return BranchIdx{get_component_sequence<Node>(state, branch.from_node()),
get_component_sequence<Node>(state, branch.to_node())};
return BranchIdx{get_component_sequence_idx<Node>(state, branch.from_node()),
get_component_sequence_idx<Node>(state, branch.to_node())};
});
}

Expand All @@ -50,9 +50,9 @@ template <std::same_as<Branch3> Component, class ComponentContainer>
constexpr void register_topology_components(MainModelState<ComponentContainer> const& state,
ComponentTopology& comp_topo) {
detail::register_topo_components<Component>(state, comp_topo.branch3_node_idx, [&state](Branch3 const& branch3) {
return Branch3Idx{get_component_sequence<Node>(state, branch3.node_1()),
get_component_sequence<Node>(state, branch3.node_2()),
get_component_sequence<Node>(state, branch3.node_3())};
return Branch3Idx{get_component_sequence_idx<Node>(state, branch3.node_1()),
get_component_sequence_idx<Node>(state, branch3.node_2()),
get_component_sequence_idx<Node>(state, branch3.node_3())};
});
}

Expand All @@ -61,7 +61,7 @@ template <std::same_as<Source> Component, class ComponentContainer>
constexpr void register_topology_components(MainModelState<ComponentContainer> const& state,
ComponentTopology& comp_topo) {
detail::register_topo_components<Component>(state, comp_topo.source_node_idx, [&state](Source const& source) {
return get_component_sequence<Node>(state, source.node());
return get_component_sequence_idx<Node>(state, source.node());
});
}

Expand All @@ -70,7 +70,7 @@ template <std::same_as<Shunt> Component, class ComponentContainer>
constexpr void register_topology_components(MainModelState<ComponentContainer> const& state,
ComponentTopology& comp_topo) {
detail::register_topo_components<Component>(state, comp_topo.shunt_node_idx, [&state](Shunt const& shunt) {
return get_component_sequence<Node>(state, shunt.node());
return get_component_sequence_idx<Node>(state, shunt.node());
});
}

Expand All @@ -80,7 +80,7 @@ constexpr void register_topology_components(MainModelState<ComponentContainer> c
ComponentTopology& comp_topo) {
detail::register_topo_components<Component>(
state, comp_topo.load_gen_node_idx,
[&state](GenericLoadGen const& load_gen) { return get_component_sequence<Node>(state, load_gen.node()); });
[&state](GenericLoadGen const& load_gen) { return get_component_sequence_idx<Node>(state, load_gen.node()); });

detail::register_topo_components<Component>(state, comp_topo.load_gen_type,
[](GenericLoadGen const& load_gen) { return load_gen.type(); });
Expand All @@ -92,7 +92,7 @@ constexpr void register_topology_components(MainModelState<ComponentContainer> c
ComponentTopology& comp_topo) {
detail::register_topo_components<Component>(
state, comp_topo.voltage_sensor_node_idx, [&state](GenericVoltageSensor const& voltage_sensor) {
return get_component_sequence<Node>(state, voltage_sensor.measured_object());
return get_component_sequence_idx<Node>(state, voltage_sensor.measured_object());
});
}

Expand All @@ -110,21 +110,21 @@ constexpr void register_topology_components(MainModelState<ComponentContainer> c
case branch_from:
[[fallthrough]];
case branch_to:
return get_component_sequence<Branch>(state, measured_object);
return get_component_sequence_idx<Branch>(state, measured_object);
case source:
return get_component_sequence<Source>(state, measured_object);
return get_component_sequence_idx<Source>(state, measured_object);
case shunt:
return get_component_sequence<Shunt>(state, measured_object);
return get_component_sequence_idx<Shunt>(state, measured_object);
case load:
[[fallthrough]];
case generator:
return get_component_sequence<GenericLoadGen>(state, measured_object);
return get_component_sequence_idx<GenericLoadGen>(state, measured_object);
case branch3_1:
case branch3_2:
case branch3_3:
return get_component_sequence<Branch3>(state, measured_object);
return get_component_sequence_idx<Branch3>(state, measured_object);
case node:
return get_component_sequence<Node>(state, measured_object);
return get_component_sequence_idx<Node>(state, measured_object);
default:
throw MissingCaseForEnumError("Power sensor idx to seq transformation",
power_sensor.get_terminal_type());
Expand All @@ -144,9 +144,9 @@ constexpr void register_topology_components(MainModelState<ComponentContainer> c
state, comp_topo.regulated_object_idx, [&state](Regulator const& regulator) {
switch (regulator.regulated_object_type()) {
case ComponentType::branch:
return get_component_sequence<Branch>(state, regulator.regulated_object());
return get_component_sequence_idx<Branch>(state, regulator.regulated_object());
case ComponentType::branch3:
return get_component_sequence<Branch3>(state, regulator.regulated_object());
return get_component_sequence_idx<Branch3>(state, regulator.regulated_object());
default:
throw MissingCaseForEnumError("Regulator idx to seq transformation", regulator.regulated_object_type());
}
Expand Down
Loading

0 comments on commit 02f9fd4

Please sign in to comment.