Skip to content

Commit 83f6815

Browse files
authored
Merge pull request #1135 from PowerGridModel/feature/main-model-refactor-clean-templates-2
Cleanup main model: Finish migrating to main model type
2 parents 6f2387e + 8cc863d commit 83f6815

File tree

12 files changed

+98
-93
lines changed

12 files changed

+98
-93
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/all_components.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,9 @@ using AllComponents =
3232
ComponentList<Node, Line, AsymLine, Link, GenericBranch, Transformer, ThreeWindingTransformer, Shunt, Source,
3333
SymGenerator, AsymGenerator, SymLoad, AsymLoad, SymPowerSensor, AsymPowerSensor, SymVoltageSensor,
3434
AsymVoltageSensor, SymCurrentSensor, AsymCurrentSensor, Fault, TransformerTapRegulator>;
35+
36+
using AllExtraRetrievableTypes =
37+
ExtraRetrievableTypes<Base, Node, Branch, Branch3, Appliance, GenericLoadGen, GenericLoad, GenericGenerator,
38+
GenericPowerSensor, GenericVoltageSensor, GenericCurrentSensor, Regulator>;
39+
3540
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/common/component_list.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ template <typename T, typename... Ts>
1616
requires is_in_list_c<T, Ts...>
1717
struct IsInList<T, ComponentList<Ts...>> : std::true_type {};
1818

19+
// type traits associated with the container
20+
template <class... T> struct ExtraRetrievableTypes {};
21+
1922
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/container.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// container for multiple components
88

99
#include "common/common.hpp"
10+
#include "common/component_list.hpp"
1011
#include "common/exception.hpp"
1112
#include "common/iterator_facade.hpp"
1213
#include "container_fwd.hpp"
@@ -336,8 +337,6 @@ class Container<RetrievableTypes<GettableTypes...>, StorageableTypes...> {
336337
template <supported_type_c<GettableTypes...> Gettable> auto citer() const { return iter<Gettable>(); }
337338
};
338339

339-
// type traits to instantiate container
340-
template <class... T> struct ExtraRetrievableTypes;
341340
// with no extra types, default all vector value types
342341
template <class... T> struct container_trait {
343342
using type = Container<RetrievableTypes<T...>, T...>;
@@ -349,8 +348,6 @@ template <class... TR, class... T> struct container_trait<ExtraRetrievableTypes<
349348

350349
} // namespace container_impl
351350

352-
template <class... T> using ExtraRetrievableTypes = container_impl::ExtraRetrievableTypes<T...>;
353-
354351
template <class... T> using Container = typename container_impl::container_trait<T...>::type;
355352

356353
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/job_adapter.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace power_grid_model {
1616

1717
template <class MainModel> class JobAdapter;
1818

19-
template <class MainModel> class JobAdapter : public JobInterface<JobAdapter<MainModel>> {
19+
template <class MainModel> class JobAdapter : public JobInterface {
2020
public:
2121
using ModelType = typename MainModel::ImplType;
2222

@@ -66,10 +66,7 @@ template <class MainModel> class JobAdapter : public JobInterface<JobAdapter<Mai
6666
~JobAdapter() { model_copy_.reset(); }
6767

6868
private:
69-
// Grant the CRTP base (JobInterface<JobAdapter>) access to
70-
// JobAdapter's private members. This allows the base class template
71-
// to call derived-class implementation details as part of the CRTP pattern.
72-
friend class JobInterface<JobAdapter>;
69+
friend class JobInterface;
7370

7471
std::unique_ptr<MainModel> model_copy_;
7572
std::reference_wrapper<MainModel> model_reference_;

power_grid_model_c/power_grid_model/include/power_grid_model/job_dispatch.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ namespace power_grid_model {
1717

1818
class JobDispatch {
1919
public:
20-
static constexpr Idx sequential{-1};
21-
2220
template <typename Adapter, typename ResultDataset, typename UpdateDataset>
23-
requires std::is_base_of_v<JobInterface<Adapter>, Adapter>
21+
requires std::is_base_of_v<JobInterface, Adapter>
2422
static BatchParameter batch_calculation(Adapter& adapter, ResultDataset const& result_data,
2523
UpdateDataset const& update_data, Idx threading,
2624
common::logging::MultiThreadedLogger& log) {

power_grid_model_c/power_grid_model/include/power_grid_model/job_interface.hpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,60 @@
1414
#include <utility>
1515

1616
namespace power_grid_model {
17-
template <typename Adapter> class JobInterface {
17+
class JobInterface {
1818
public:
1919
// the multiple NOSONARs are used to avoid the complaints about the unnamed concepts
20-
template <typename ResultDataset>
21-
void calculate(ResultDataset const& result_data, Idx pos, Logger& logger)
22-
requires requires(Adapter& adapter) { // NOSONAR
23-
{ adapter.calculate_impl(result_data, pos, logger) } -> std::same_as<void>;
20+
template <typename Self, typename ResultDataset>
21+
void calculate(this Self&& self, ResultDataset const& result_data, Idx pos, Logger& logger)
22+
requires requires { // NOSONAR
23+
{ std::forward<Self>(self).calculate_impl(result_data, pos, logger) } -> std::same_as<void>;
2424
}
2525
{
26-
static_cast<Adapter*>(this)->calculate_impl(result_data, pos, logger);
26+
return std::forward<Self>(self).calculate_impl(result_data, pos, logger);
2727
}
28-
template <typename ResultDataset> void calculate(ResultDataset const& result_data, Logger& logger) {
29-
calculate(result_data, Idx{}, logger);
28+
29+
template <typename Self, typename ResultDataset>
30+
void calculate(this Self&& self, ResultDataset const& result_data, Logger& logger) {
31+
std::forward<Self>(self).calculate(result_data, Idx{}, logger);
3032
}
3133

32-
void cache_calculate(Logger& logger)
33-
requires requires(Adapter& adapter) { // NOSONAR
34-
{ adapter.cache_calculate_impl(logger) } -> std::same_as<void>;
34+
template <typename Self>
35+
void cache_calculate(this Self&& self, Logger& logger)
36+
requires requires { // NOSONAR
37+
{ std::forward<Self>(self).cache_calculate_impl(logger) } -> std::same_as<void>;
3538
}
3639
{
37-
static_cast<Adapter*>(this)->cache_calculate_impl(logger);
40+
return std::forward<Self>(self).cache_calculate_impl(logger);
3841
}
3942

40-
template <typename UpdateDataset>
41-
void prepare_job_dispatch(UpdateDataset const& update_data)
42-
requires requires(Adapter& adapter) { // NOSONAR
43-
{ adapter.prepare_job_dispatch_impl(update_data) } -> std::same_as<void>;
43+
template <typename Self, typename UpdateDataset>
44+
void prepare_job_dispatch(this Self&& self, UpdateDataset const& update_data)
45+
requires requires { // NOSONAR
46+
{ std::forward<Self>(self).prepare_job_dispatch_impl(update_data) } -> std::same_as<void>;
4447
}
4548
{
46-
static_cast<Adapter*>(this)->prepare_job_dispatch_impl(update_data);
49+
return std::forward<Self>(self).prepare_job_dispatch_impl(update_data);
4750
}
4851

49-
template <typename UpdateDataset>
50-
void setup(UpdateDataset const& update_data, Idx scenario_idx)
51-
requires requires(Adapter& adapter) { // NOSONAR
52-
{ adapter.setup_impl(update_data, scenario_idx) } -> std::same_as<void>;
52+
template <typename Self, typename UpdateDataset>
53+
void setup(this Self&& self, UpdateDataset const& update_data, Idx scenario_idx)
54+
requires requires { // NOSONAR
55+
{ std::forward<Self>(self).setup_impl(update_data, scenario_idx) } -> std::same_as<void>;
5356
}
5457
{
55-
static_cast<Adapter*>(this)->setup_impl(update_data, scenario_idx);
58+
return std::forward<Self>(self).setup_impl(update_data, scenario_idx);
5659
}
5760

58-
void winddown()
59-
requires requires(Adapter& adapter) { // NOSONAR
60-
{ adapter.winddown_impl() } -> std::same_as<void>;
61+
template <typename Self>
62+
void winddown(this Self&& self)
63+
requires requires { // NOSONAR
64+
{ std::forward<Self>(self).winddown_impl() } -> std::same_as<void>;
6165
}
6266
{
63-
static_cast<Adapter*>(this)->winddown_impl();
67+
return std::forward<Self>(self).winddown_impl();
6468
}
6569

66-
private:
67-
friend Adapter;
70+
protected:
6871
JobInterface() = default;
6972
JobInterface(const JobInterface& /*other*/) = default;
7073
JobInterface& operator=(const JobInterface& /*other*/) = default;

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/core_utils.hpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
#pragma once
66

7-
#include "../container.hpp"
8-
#include "state.hpp"
9-
107
#include <array>
118
#include <vector>
129
namespace power_grid_model::main_core::utils {
@@ -20,26 +17,9 @@ constexpr void run_functor_with_tuple_index_return_void(Functor&& functor, std::
2017

2118
} // namespace detail
2219

20+
constexpr Idx sequential{-1};
2321
constexpr Idx invalid_index{-1};
2422

25-
/////////////////// To remove ///////////////////
26-
27-
template <class... ComponentTypes> constexpr size_t n_types = sizeof...(ComponentTypes);
28-
template <class CompType, class... ComponentTypes>
29-
constexpr size_t index_of_component = container_impl::get_cls_pos_v<CompType, ComponentTypes...>;
30-
31-
template <class... ComponentTypes> using SequenceIdx = std::array<std::vector<Idx2D>, n_types<ComponentTypes...>>;
32-
template <class... ComponentTypes> using ComponentFlags = std::array<bool, n_types<ComponentTypes...>>;
33-
34-
// run functors with all component types
35-
template <class... Types, class Functor> constexpr void run_functor_with_all_types_return_void(Functor functor) {
36-
(functor.template operator()<Types>(), ...);
37-
}
38-
template <class... Types, class Functor> constexpr auto run_functor_with_all_types_return_array(Functor functor) {
39-
return std::array { functor.template operator()<Types>()... };
40-
}
41-
/////////////////// To remove ///////////////////
42-
4323
template <typename Tuple, class Functor> constexpr void run_functor_with_tuple_return_void(Functor&& functor) {
4424
detail::run_functor_with_tuple_index_return_void<Tuple>(std::forward<Functor>(functor),
4525
std::make_index_sequence<std::tuple_size_v<Tuple>>{});

power_grid_model_c/power_grid_model/include/power_grid_model/main_core/main_model_type.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ class MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
7878
using ComponentTypesTuple = std::tuple<ComponentType...>;
7979

8080
static constexpr size_t n_types = sizeof...(ComponentType);
81-
// TODO Should not have to go via container_impl.
8281
template <class CompType>
8382
static constexpr size_t index_of_component = container_impl::get_cls_pos_v<CompType, ComponentType...>;
8483

8584
private:
85+
// This tuple may contain duplicate types. eg. Node.
8686
static constexpr auto all_types_tuple_v_ =
8787
std::tuple<std::type_identity<ComponentType>..., std::type_identity<ExtraRetrievableType>...>{};
88-
// TODO: Would making a unique be necessary? We have Node mentioned as a ExtraRetrievableType and ComponentType so
89-
// summing up is possible but maybe not appropriate. Would it be better to handle them both separately? using
9088

9189
static constexpr auto topology_types_tuple_v_ =
9290
std::tuple<std::type_identity<Node>, std::type_identity<Branch>, std::type_identity<Branch3>,
@@ -121,4 +119,11 @@ class MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
121119
}
122120
};
123121

122+
template <typename T> struct is_main_model_type : std::false_type {};
123+
124+
template <typename... ETs, typename... CTs>
125+
struct is_main_model_type<MainModelType<ExtraRetrievableTypes<ETs...>, ComponentList<CTs...>>> : std::true_type {};
126+
127+
template <typename T> inline constexpr bool is_main_model_type_v = is_main_model_type<T>::value;
128+
124129
} // namespace power_grid_model::main_core

power_grid_model_c/power_grid_model/include/power_grid_model/main_model.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ namespace power_grid_model {
1818

1919
class MainModel {
2020
private:
21-
using Impl = MainModelImpl<
22-
ExtraRetrievableTypes<Base, Node, Branch, Branch3, Appliance, GenericLoadGen, GenericLoad, GenericGenerator,
23-
GenericPowerSensor, GenericVoltageSensor, GenericCurrentSensor, Regulator>,
24-
AllComponents>;
21+
using Impl = MainModelImpl<main_core::MainModelType<AllExtraRetrievableTypes, AllComponents>>;
2522

2623
public:
2724
using Options = MainModelOptions;

power_grid_model_c/power_grid_model/include/power_grid_model/main_model_impl.hpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,12 @@ decltype(auto) calculation_type_symmetry_func_selector(CalculationType calculati
116116
calculation_symmetry, std::forward<Functor>(f), std::forward<Args>(args)...);
117117
}
118118

119-
// main model implementation template
120-
template <class T, class U> class MainModelImpl;
121-
122-
template <class... ExtraRetrievableType, class... ComponentType>
123-
class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentList<ComponentType...>> {
119+
template <class ModelType>
120+
requires(main_core::is_main_model_type_v<ModelType>)
121+
class MainModelImpl {
124122

125123
private:
126-
using ModelType =
127-
main_core::MainModelType<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentList<ComponentType...>>;
128124
// internal type traits
129-
// container class
130125
using ComponentContainer = typename ModelType::ComponentContainer;
131126
using MainModelState = typename ModelType::MainModelState;
132127

@@ -138,7 +133,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
138133

139134
static constexpr Idx isolated_component{main_core::isolated_component};
140135
static constexpr Idx not_connected{main_core::not_connected};
141-
static constexpr Idx sequential{JobDispatch::sequential};
136+
static constexpr Idx sequential{main_core::utils::sequential};
142137

143138
public:
144139
using ImplType = ModelType;
@@ -481,7 +476,7 @@ class MainModelImpl<ExtraRetrievableTypes<ExtraRetrievableType...>, ComponentLis
481476
faults, [](Fault const& fault) { return fault.get_fault_type() == FaultType::three_phase; });
482477
options.calculation_symmetry =
483478
is_three_phase ? CalculationSymmetry::symmetric : CalculationSymmetry::asymmetric;
484-
};
479+
}
485480

486481
calculation_type_symmetry_func_selector(
487482
options.calculation_type, options.calculation_symmetry,

0 commit comments

Comments
 (0)