Skip to content

Commit 887cf99

Browse files
Merge pull request #578 from PowerGridModel/feature/container-improvements
Feature/container + main core improvements
2 parents b9d6393 + 1c9c202 commit 887cf99

File tree

12 files changed

+243
-142
lines changed

12 files changed

+243
-142
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/calculation_parameters.hpp

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,6 @@ template <symmetry_tag sym_type> struct BranchCalcParam {
3535
ComplexTensor<sym> const& ytt() const { return value[3]; }
3636
};
3737

38-
struct TransformerTapRegulatorCalcParam {
39-
double u_set{};
40-
double u_band{};
41-
DoubleComplex z_compensation{};
42-
IntS status{};
43-
};
44-
4538
template <symmetry_tag sym_type> struct BranchMathOutput {
4639
using sym = sym_type;
4740

@@ -117,15 +110,28 @@ template <symmetry_tag sym_type> struct PowerSensorCalcParam {
117110

118111
template <typename T>
119112
concept sensor_calc_param_type =
120-
std::same_as<T, VoltageSensorCalcParam<symmetric_t>> || std::same_as<T, VoltageSensorCalcParam<asymmetric_t>> ||
121-
std::same_as<T, PowerSensorCalcParam<symmetric_t>> || std::same_as<T, PowerSensorCalcParam<asymmetric_t>>;
113+
std::derived_from<T, VoltageSensorCalcParam<symmetric_t>> ||
114+
std::derived_from<T, VoltageSensorCalcParam<asymmetric_t>> ||
115+
std::derived_from<T, PowerSensorCalcParam<symmetric_t>> || std::derived_from<T, PowerSensorCalcParam<asymmetric_t>>;
122116

123117
static_assert(sensor_calc_param_type<VoltageSensorCalcParam<symmetric_t>>);
124118
static_assert(sensor_calc_param_type<VoltageSensorCalcParam<asymmetric_t>>);
125119
static_assert(sensor_calc_param_type<PowerSensorCalcParam<symmetric_t>>);
126120
static_assert(sensor_calc_param_type<PowerSensorCalcParam<asymmetric_t>>);
127121

128-
// from, to side
122+
struct TransformerTapRegulatorCalcParam {
123+
double u_set{};
124+
double u_band{};
125+
DoubleComplex z_compensation{};
126+
IntS status{};
127+
};
128+
template <symmetry_tag sym_type> struct TransformerTapRegulatorMathOutput {
129+
using sym = sym_type;
130+
131+
IntS tap_pos{na_IntS};
132+
};
133+
134+
// from side, to side
129135
// in case of indices for math model, -1 means the branch is not connected to that side
130136
using BranchIdx = std::array<Idx, 2>;
131137
// node 0, 1, 2 side
@@ -221,7 +227,7 @@ struct ShortCircuitInput {
221227

222228
template <typename T>
223229
concept symmetric_calculation_input_type =
224-
std::same_as<T, PowerFlowInput<symmetric_t>> || std::same_as<T, StateEstimationInput<symmetric_t>>;
230+
std::derived_from<T, PowerFlowInput<symmetric_t>> || std::derived_from<T, StateEstimationInput<symmetric_t>>;
225231

226232
static_assert(symmetric_calculation_input_type<PowerFlowInput<symmetric_t>>);
227233
static_assert(symmetric_calculation_input_type<StateEstimationInput<symmetric_t>>);
@@ -231,8 +237,8 @@ static_assert(!symmetric_calculation_input_type<ShortCircuitInput>);
231237

232238
template <typename T>
233239
concept asymmetric_calculation_input_type =
234-
std::same_as<T, PowerFlowInput<asymmetric_t>> || std::same_as<T, StateEstimationInput<asymmetric_t>> ||
235-
std::same_as<T, ShortCircuitInput>;
240+
std::derived_from<T, PowerFlowInput<asymmetric_t>> || std::derived_from<T, StateEstimationInput<asymmetric_t>> ||
241+
std::derived_from<T, ShortCircuitInput>;
236242

237243
static_assert(!asymmetric_calculation_input_type<PowerFlowInput<symmetric_t>>);
238244
static_assert(!asymmetric_calculation_input_type<StateEstimationInput<symmetric_t>>);
@@ -249,7 +255,10 @@ static_assert(calculation_input_type<PowerFlowInput<asymmetric_t>>);
249255
static_assert(calculation_input_type<StateEstimationInput<asymmetric_t>>);
250256
static_assert(calculation_input_type<ShortCircuitInput>);
251257

258+
struct math_output_t {};
259+
252260
template <symmetry_tag sym_type> struct MathOutput {
261+
using type = math_output_t;
253262
using sym = sym_type;
254263

255264
std::vector<ComplexValue<sym>> u;
@@ -258,9 +267,11 @@ template <symmetry_tag sym_type> struct MathOutput {
258267
std::vector<ApplianceMathOutput<sym>> source;
259268
std::vector<ApplianceMathOutput<sym>> shunt;
260269
std::vector<ApplianceMathOutput<sym>> load_gen;
270+
std::vector<TransformerTapRegulatorMathOutput<sym>> transformer_tap_regulator;
261271
};
262272

263273
template <symmetry_tag sym_type> struct ShortCircuitMathOutput {
274+
using type = math_output_t;
264275
using sym = sym_type;
265276

266277
std::vector<ComplexValue<sym>> u_bus;
@@ -271,26 +282,31 @@ template <symmetry_tag sym_type> struct ShortCircuitMathOutput {
271282
};
272283

273284
template <typename T>
274-
concept symmetric_math_output_type =
275-
std::same_as<T, MathOutput<symmetric_t>> || std::same_as<T, ShortCircuitMathOutput<symmetric_t>>;
285+
concept math_output_type = std::derived_from<typename T::type, math_output_t>;
286+
287+
static_assert(math_output_type<MathOutput<symmetric_t>>);
288+
static_assert(math_output_type<MathOutput<asymmetric_t>>);
289+
static_assert(math_output_type<ShortCircuitMathOutput<symmetric_t>>);
290+
static_assert(math_output_type<ShortCircuitMathOutput<asymmetric_t>>);
291+
292+
template <typename T>
293+
concept symmetric_math_output_type = math_output_type<T> && is_symmetric_v<typename T::sym>;
276294

277295
static_assert(symmetric_math_output_type<MathOutput<symmetric_t>>);
278296
static_assert(!symmetric_math_output_type<MathOutput<asymmetric_t>>);
279297
static_assert(symmetric_math_output_type<ShortCircuitMathOutput<symmetric_t>>);
280298
static_assert(!symmetric_math_output_type<ShortCircuitMathOutput<asymmetric_t>>);
281299

282300
template <typename T>
283-
concept asymmetric_math_output_type =
284-
std::same_as<T, MathOutput<asymmetric_t>> || std::same_as<T, ShortCircuitMathOutput<asymmetric_t>>;
301+
concept asymmetric_math_output_type = math_output_type<T> && is_asymmetric_v<typename T::sym>;
285302

286303
static_assert(!asymmetric_math_output_type<MathOutput<symmetric_t>>);
287304
static_assert(asymmetric_math_output_type<MathOutput<asymmetric_t>>);
288305
static_assert(!asymmetric_math_output_type<ShortCircuitMathOutput<symmetric_t>>);
289306
static_assert(asymmetric_math_output_type<ShortCircuitMathOutput<asymmetric_t>>);
290307

291308
template <typename T>
292-
concept steady_state_math_output_type =
293-
std::same_as<T, MathOutput<symmetric_t>> || std::same_as<T, MathOutput<asymmetric_t>>;
309+
concept steady_state_math_output_type = math_output_type<T> && std::derived_from<T, MathOutput<typename T::sym>>;
294310

295311
static_assert(steady_state_math_output_type<MathOutput<symmetric_t>>);
296312
static_assert(steady_state_math_output_type<MathOutput<asymmetric_t>>);
@@ -299,22 +315,13 @@ static_assert(!steady_state_math_output_type<ShortCircuitMathOutput<asymmetric_t
299315

300316
template <typename T>
301317
concept short_circuit_math_output_type =
302-
std::same_as<T, ShortCircuitMathOutput<symmetric_t>> || std::same_as<T, ShortCircuitMathOutput<asymmetric_t>>;
318+
math_output_type<T> && std::derived_from<T, ShortCircuitMathOutput<typename T::sym>>;
303319

304320
static_assert(!short_circuit_math_output_type<MathOutput<symmetric_t>>);
305321
static_assert(!short_circuit_math_output_type<MathOutput<asymmetric_t>>);
306322
static_assert(short_circuit_math_output_type<ShortCircuitMathOutput<symmetric_t>>);
307323
static_assert(short_circuit_math_output_type<ShortCircuitMathOutput<asymmetric_t>>);
308324

309-
template <typename T>
310-
concept math_output_type = (symmetric_math_output_type<T> || asymmetric_math_output_type<T>) &&
311-
(steady_state_math_output_type<T> || short_circuit_math_output_type<T>);
312-
313-
static_assert(math_output_type<MathOutput<symmetric_t>>);
314-
static_assert(math_output_type<MathOutput<asymmetric_t>>);
315-
static_assert(math_output_type<ShortCircuitMathOutput<symmetric_t>>);
316-
static_assert(math_output_type<ShortCircuitMathOutput<asymmetric_t>>);
317-
318325
// component indices at physical model side
319326
// from, to node indices for branches
320327
// node1, node2, node3 indices for 3-way branches
@@ -328,12 +335,13 @@ struct ComponentTopology {
328335
IdxVector load_gen_node_idx;
329336
std::vector<LoadGenType> load_gen_type;
330337
IdxVector voltage_sensor_node_idx;
331-
IdxVector power_sensor_object_idx; // the index is relative to branch, source, shunt, or load_gen
338+
IdxVector power_sensor_object_idx; // the index is relative to branch, source, shunt or load_gen
332339
std::vector<MeasuredTerminalType> power_sensor_terminal_type;
333-
IdxVector regulated_object_idx; // the index is relative to branch
340+
std::vector<ComponentType> regulator_type;
341+
IdxVector regulated_object_idx; // the index is relative to branch or branch3
334342
std::vector<ComponentType> regulated_object_type;
335343

336-
inline Idx n_node_total() const { return n_node + static_cast<Idx>(branch3_node_idx.size()); }
344+
Idx n_node_total() const { return n_node + static_cast<Idx>(branch3_node_idx.size()); }
337345
};
338346

339347
// connection property
@@ -392,14 +400,4 @@ struct TopologicalComponentToMathCoupling {
392400
std::vector<Idx2D> regulator;
393401
};
394402

395-
// change of update cause topology and param change, or just param change
396-
struct UpdateChange {
397-
bool topo{};
398-
bool param{};
399-
400-
friend constexpr UpdateChange operator||(UpdateChange const& x, UpdateChange const& y) {
401-
return UpdateChange{x.topo || y.topo, x.param || y.param};
402-
}
403-
};
404-
405403
} // namespace power_grid_model

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ template <typename T>
3737
concept symmetry_tag = std::derived_from<T, symmetric_t> || std::derived_from<T, asymmetric_t>;
3838

3939
template <symmetry_tag T> constexpr bool is_symmetric_v = std::derived_from<T, symmetric_t>;
40+
template <symmetry_tag T> constexpr bool is_asymmetric_v = std::derived_from<T, asymmetric_t>;
4041

4142
template <symmetry_tag T> using other_symmetry_t = std::conditional_t<is_symmetric_v<T>, asymmetric_t, symmetric_t>;
4243

power_grid_model_c/power_grid_model/include/power_grid_model/common/enum.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ enum class ComponentType : IntS {
5858
branch3 = 9,
5959
fault = 10,
6060
regulator = 11,
61-
transformer_tap_regulator = 12
61+
transformer_tap_regulator = 12,
62+
test = -128 // any stub or mock may use this. do not use this in production
6263
};
6364

6465
// DO NOT change the order of enumerations

power_grid_model_c/power_grid_model/include/power_grid_model/component/base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#pragma once
66

7+
#include "component.hpp"
8+
79
#include "../auxiliary/input.hpp"
810
#include "../auxiliary/output.hpp"
911
#include "../auxiliary/update.hpp"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
#pragma once
6+
7+
#include "../common/common.hpp"
8+
#include "../common/enum.hpp"
9+
10+
#include <concepts>
11+
#include <string_view>
12+
13+
namespace power_grid_model {
14+
15+
// change of update cause topology and param change, or just param change
16+
struct UpdateChange {
17+
bool topo{};
18+
bool param{};
19+
20+
friend constexpr UpdateChange operator||(UpdateChange const& x, UpdateChange const& y) {
21+
return UpdateChange{x.topo || y.topo, x.param || y.param};
22+
}
23+
};
24+
25+
template <typename T>
26+
concept component_c = requires(T t, T const& ct, typename T::UpdateType u, typename T::UpdateType const& cu) {
27+
typename T::InputType;
28+
typename T::UpdateType;
29+
30+
{ T::name } -> std::convertible_to<std::string_view>;
31+
{ ct.math_model_type() } -> std::convertible_to<ComponentType>;
32+
33+
{ ct.id() } -> std::same_as<ID>;
34+
35+
{ t.update(cu) } -> std::same_as<UpdateChange>;
36+
{ ct.inverse(u) } -> std::same_as<typename T::UpdateType>;
37+
};
38+
39+
} // namespace power_grid_model

power_grid_model_c/power_grid_model/include/power_grid_model/component/transformer_utils.hpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@ concept enum_c = std::is_enum_v<T>;
1515
} // namespace detail
1616

1717
template <typename T>
18-
concept transformer_c = requires(T const& t, typename T::UpdateType u, typename T::SideType s) {
19-
typename T::UpdateType;
20-
typename T::SideType;
21-
22-
{ T::name } -> std::convertible_to<std::string_view>;
23-
{ t.math_model_type() } -> std::convertible_to<ComponentType>;
24-
25-
{ t.id() } -> std::same_as<ID>;
26-
{ t.node(s) } -> std::same_as<ID>;
27-
{ t.status(s) } -> std::convertible_to<bool>;
28-
29-
{ t.tap_side() } -> std::same_as<typename T::SideType>;
30-
{ t.tap_pos() } -> std::convertible_to<IntS>;
31-
{ t.tap_min() } -> std::convertible_to<IntS>;
32-
{ t.tap_max() } -> std::convertible_to<IntS>;
33-
{ t.tap_nom() } -> std::convertible_to<IntS>;
34-
35-
{ t.inverse(u) } -> std::same_as<typename T::UpdateType>;
36-
};
18+
concept transformer_c = component_c<T> && requires(T const& t, typename T::UpdateType u, typename T::SideType s) {
19+
{ t.node(s) } -> std::same_as<ID>;
20+
{ t.status(s) } -> std::convertible_to<bool>;
21+
22+
{ t.tap_side() } -> std::same_as<typename T::SideType>;
23+
{ t.tap_pos() } -> std::convertible_to<IntS>;
24+
{ t.tap_min() } -> std::convertible_to<IntS>;
25+
{ t.tap_max() } -> std::convertible_to<IntS>;
26+
{ t.tap_nom() } -> std::convertible_to<IntS>;
27+
};
3728

3829
constexpr double tap_adjust_impedance(double tap_pos, double tap_min, double tap_max, double tap_nom, double xk,
3930
double xk_min, double xk_max) {

0 commit comments

Comments
 (0)