Skip to content

Commit 662e355

Browse files
authored
Merge pull request #1078 from PowerGridModel/feature/move-prepare-logic
Clean-up main model: move prepare calculation logic
2 parents 87b9b96 + 8f9c7b8 commit 662e355

File tree

2 files changed

+310
-279
lines changed

2 files changed

+310
-279
lines changed
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
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 "state_queries.hpp"
8+
9+
#include "../calculation_parameters.hpp"
10+
11+
#include <concepts>
12+
#include <vector>
13+
14+
namespace power_grid_model::main_core {
15+
constexpr Idx isolated_component{-1};
16+
constexpr Idx not_connected{-1};
17+
18+
namespace detail {
19+
template <calculation_input_type CalcInputType>
20+
inline auto calculate_param(auto const& c, auto const&... extra_args)
21+
requires requires {
22+
{ c.calc_param(extra_args...) };
23+
}
24+
{
25+
return c.calc_param(extra_args...);
26+
}
27+
28+
template <calculation_input_type CalcInputType>
29+
inline auto calculate_param(auto const& c, auto const&... extra_args)
30+
requires requires {
31+
{ c.template calc_param<typename CalcInputType::sym>(extra_args...) };
32+
}
33+
{
34+
return c.template calc_param<typename CalcInputType::sym>(extra_args...);
35+
}
36+
37+
/** This is a heavily templated member function because it operates on many different variables of many
38+
*different types, but the essence is ever the same: filling one member (vector) of the calculation calc_input
39+
*struct (soa) with the right calculation symmetric or asymmetric calculation parameters, in the same order as
40+
*the corresponding component are stored in the component topology. There is one such struct for each sub graph
41+
* / math model and all of them are filled within the same function call (i.e. Notice that calc_input is a
42+
*vector).
43+
*
44+
* 1. For each component, check if it should be included.
45+
* By default, all component are included, except for some cases, like power sensors. For power sensors, the
46+
* list of component contains all power sensors, but the preparation should only be done for one type of
47+
*power sensors at a time. Therefore, `included` will be a lambda function, such as:
48+
*
49+
* [this](Idx i) { return state_.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::source;
50+
*}
51+
*
52+
* 2. Find the original component in the topology and retrieve its calculation parameters.
53+
*
54+
* 3. Fill the calculation parameters of the right math model.
55+
*
56+
* @tparam CalcStructOut
57+
* The calculation input type (soa) for the desired calculation (e.g. PowerFlowInput<sym> or
58+
*StateEstimationInput<sym>).
59+
*
60+
* @tparam CalcParamOut
61+
* The data type for the desired calculation for the given ComponentIn (e.g. SourceCalcParam<sym> or
62+
* VoltageSensorCalcParam<sym>).
63+
*
64+
* @tparam comp_vect
65+
* The (pre-allocated and resized) vector of CalcParamOuts which shall be filled with component specific
66+
* calculation parameters. Note that this is a pointer to member
67+
*
68+
* @tparam ComponentIn
69+
* The component type for which we are collecting calculation parameters
70+
*
71+
* @tparam PredicateIn
72+
* The lambda function type. The actual type depends on the captured variables, and will be
73+
*automatically deduced.
74+
*
75+
* @param component[in]
76+
* The vector of component math indices to consider (e.g. state_.topo_comp_coup->source).
77+
* When idx.group = -1, the original component is not assigned to a math model, so we can skip it.
78+
*
79+
* @param calc_input[out]
80+
* Although this variable is called `input`, it is actually the output of this function, it stored
81+
*the calculation parameters for each math model, for each component of type ComponentIn.
82+
*
83+
* @param include
84+
* A lambda function (Idx i -> bool) which returns true if the component at Idx i should be included.
85+
* The default lambda `include_all` always returns `true`.
86+
*/
87+
template <calculation_input_type CalcStructOut, typename CalcParamOut,
88+
std::vector<CalcParamOut>(CalcStructOut::*comp_vect), class ComponentIn,
89+
std::invocable<Idx> PredicateIn = IncludeAll>
90+
requires std::convertible_to<std::invoke_result_t<PredicateIn, Idx>, bool>
91+
void prepare_input(main_model_state_c auto const& state, std::vector<Idx2D> const& components,
92+
std::vector<CalcStructOut>& calc_input, PredicateIn include = include_all) {
93+
for (Idx i = 0, n = narrow_cast<Idx>(components.size()); i != n; ++i) {
94+
if (include(i)) {
95+
Idx2D const math_idx = components[i];
96+
if (math_idx.group != isolated_component) {
97+
auto const& component = get_component_by_sequence<ComponentIn>(state, i);
98+
CalcStructOut& math_model_input = calc_input[math_idx.group];
99+
std::vector<CalcParamOut>& math_model_input_vect = math_model_input.*comp_vect;
100+
math_model_input_vect[math_idx.pos] = calculate_param<CalcStructOut>(component);
101+
}
102+
}
103+
}
104+
}
105+
106+
template <calculation_input_type CalcStructOut, typename CalcParamOut,
107+
std::vector<CalcParamOut>(CalcStructOut::*comp_vect), class ComponentIn,
108+
std::invocable<Idx> PredicateIn = IncludeAll>
109+
requires std::convertible_to<std::invoke_result_t<PredicateIn, Idx>, bool>
110+
void prepare_input(main_model_state_c auto const& state, std::vector<Idx2D> const& components,
111+
std::vector<CalcStructOut>& calc_input, std::invocable<ComponentIn const&> auto extra_args,
112+
PredicateIn include = include_all) {
113+
for (Idx i = 0, n = narrow_cast<Idx>(components.size()); i != n; ++i) {
114+
if (include(i)) {
115+
Idx2D const math_idx = components[i];
116+
if (math_idx.group != isolated_component) {
117+
auto const& component = get_component_by_sequence<ComponentIn>(state, i);
118+
CalcStructOut& math_model_input = calc_input[math_idx.group];
119+
std::vector<CalcParamOut>& math_model_input_vect = math_model_input.*comp_vect;
120+
math_model_input_vect[math_idx.pos] = calculate_param<CalcStructOut>(component, extra_args(component));
121+
}
122+
}
123+
}
124+
}
125+
126+
template <symmetry_tag sym, IntSVector(StateEstimationInput<sym>::*component), class Component>
127+
void prepare_input_status(main_model_state_c auto const& state, std::vector<Idx2D> const& objects,
128+
std::vector<StateEstimationInput<sym>>& input) {
129+
for (Idx i = 0, n = narrow_cast<Idx>(objects.size()); i != n; ++i) {
130+
Idx2D const math_idx = objects[i];
131+
if (math_idx.group == isolated_component) {
132+
continue;
133+
}
134+
(input[math_idx.group].*component)[math_idx.pos] =
135+
main_core::get_component_by_sequence<Component>(state, i).status();
136+
}
137+
}
138+
} // namespace detail
139+
140+
template <symmetry_tag sym>
141+
std::vector<PowerFlowInput<sym>> prepare_power_flow_input(main_model_state_c auto const& state, Idx n_math_solvers) {
142+
using detail::prepare_input;
143+
144+
std::vector<PowerFlowInput<sym>> pf_input(n_math_solvers);
145+
for (Idx i = 0; i != n_math_solvers; ++i) {
146+
pf_input[i].s_injection.resize(state.math_topology[i]->n_load_gen());
147+
pf_input[i].source.resize(state.math_topology[i]->n_source());
148+
}
149+
prepare_input<PowerFlowInput<sym>, DoubleComplex, &PowerFlowInput<sym>::source, Source>(
150+
state, state.topo_comp_coup->source, pf_input);
151+
152+
prepare_input<PowerFlowInput<sym>, ComplexValue<sym>, &PowerFlowInput<sym>::s_injection, GenericLoadGen>(
153+
state, state.topo_comp_coup->load_gen, pf_input);
154+
155+
return pf_input;
156+
}
157+
158+
template <symmetry_tag sym>
159+
std::vector<StateEstimationInput<sym>> prepare_state_estimation_input(main_model_state_c auto const& state,
160+
Idx n_math_solvers) {
161+
using detail::prepare_input;
162+
using detail::prepare_input_status;
163+
164+
std::vector<StateEstimationInput<sym>> se_input(n_math_solvers);
165+
166+
for (Idx i = 0; i != n_math_solvers; ++i) {
167+
se_input[i].shunt_status.resize(state.math_topology[i]->n_shunt());
168+
se_input[i].load_gen_status.resize(state.math_topology[i]->n_load_gen());
169+
se_input[i].source_status.resize(state.math_topology[i]->n_source());
170+
se_input[i].measured_voltage.resize(state.math_topology[i]->n_voltage_sensor());
171+
se_input[i].measured_source_power.resize(state.math_topology[i]->n_source_power_sensor());
172+
se_input[i].measured_load_gen_power.resize(state.math_topology[i]->n_load_gen_power_sensor());
173+
se_input[i].measured_shunt_power.resize(state.math_topology[i]->n_shunt_power_power_sensor());
174+
se_input[i].measured_branch_from_power.resize(state.math_topology[i]->n_branch_from_power_sensor());
175+
se_input[i].measured_branch_to_power.resize(state.math_topology[i]->n_branch_to_power_sensor());
176+
se_input[i].measured_bus_injection.resize(state.math_topology[i]->n_bus_power_sensor());
177+
se_input[i].measured_branch_from_current.resize(state.math_topology[i]->n_branch_from_current_sensor());
178+
se_input[i].measured_branch_to_current.resize(state.math_topology[i]->n_branch_to_current_sensor());
179+
}
180+
181+
prepare_input_status<sym, &StateEstimationInput<sym>::shunt_status, Shunt>(state, state.topo_comp_coup->shunt,
182+
se_input);
183+
prepare_input_status<sym, &StateEstimationInput<sym>::load_gen_status, GenericLoadGen>(
184+
state, state.topo_comp_coup->load_gen, se_input);
185+
prepare_input_status<sym, &StateEstimationInput<sym>::source_status, Source>(state, state.topo_comp_coup->source,
186+
se_input);
187+
188+
prepare_input<StateEstimationInput<sym>, VoltageSensorCalcParam<sym>, &StateEstimationInput<sym>::measured_voltage,
189+
GenericVoltageSensor>(state, state.topo_comp_coup->voltage_sensor, se_input);
190+
prepare_input<StateEstimationInput<sym>, PowerSensorCalcParam<sym>,
191+
&StateEstimationInput<sym>::measured_source_power, GenericPowerSensor>(
192+
state, state.topo_comp_coup->power_sensor, se_input,
193+
[&state](Idx i) { return state.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::source; });
194+
prepare_input<StateEstimationInput<sym>, PowerSensorCalcParam<sym>,
195+
&StateEstimationInput<sym>::measured_load_gen_power, GenericPowerSensor>(
196+
state, state.topo_comp_coup->power_sensor, se_input, [&state](Idx i) {
197+
return state.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::load ||
198+
state.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::generator;
199+
});
200+
prepare_input<StateEstimationInput<sym>, PowerSensorCalcParam<sym>,
201+
&StateEstimationInput<sym>::measured_shunt_power, GenericPowerSensor>(
202+
state, state.topo_comp_coup->power_sensor, se_input,
203+
[&state](Idx i) { return state.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::shunt; });
204+
prepare_input<StateEstimationInput<sym>, PowerSensorCalcParam<sym>,
205+
&StateEstimationInput<sym>::measured_branch_from_power, GenericPowerSensor>(
206+
state, state.topo_comp_coup->power_sensor, se_input, [&state](Idx i) {
207+
using enum MeasuredTerminalType;
208+
return state.comp_topo->power_sensor_terminal_type[i] == branch_from ||
209+
// all branch3 sensors are at from side in the mathematical model
210+
state.comp_topo->power_sensor_terminal_type[i] == branch3_1 ||
211+
state.comp_topo->power_sensor_terminal_type[i] == branch3_2 ||
212+
state.comp_topo->power_sensor_terminal_type[i] == branch3_3;
213+
});
214+
prepare_input<StateEstimationInput<sym>, PowerSensorCalcParam<sym>,
215+
&StateEstimationInput<sym>::measured_branch_to_power, GenericPowerSensor>(
216+
state, state.topo_comp_coup->power_sensor, se_input,
217+
[&state](Idx i) { return state.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::branch_to; });
218+
prepare_input<StateEstimationInput<sym>, PowerSensorCalcParam<sym>,
219+
&StateEstimationInput<sym>::measured_bus_injection, GenericPowerSensor>(
220+
state, state.topo_comp_coup->power_sensor, se_input,
221+
[&state](Idx i) { return state.comp_topo->power_sensor_terminal_type[i] == MeasuredTerminalType::node; });
222+
223+
prepare_input<StateEstimationInput<sym>, CurrentSensorCalcParam<sym>,
224+
&StateEstimationInput<sym>::measured_branch_from_current, GenericCurrentSensor>(
225+
state, state.topo_comp_coup->current_sensor, se_input, [&state](Idx i) {
226+
using enum MeasuredTerminalType;
227+
return state.comp_topo->current_sensor_terminal_type[i] == branch_from ||
228+
// all branch3 sensors are at from side in the mathematical model
229+
state.comp_topo->current_sensor_terminal_type[i] == branch3_1 ||
230+
state.comp_topo->current_sensor_terminal_type[i] == branch3_2 ||
231+
state.comp_topo->current_sensor_terminal_type[i] == branch3_3;
232+
});
233+
prepare_input<StateEstimationInput<sym>, CurrentSensorCalcParam<sym>,
234+
&StateEstimationInput<sym>::measured_branch_to_current, GenericCurrentSensor>(
235+
state, state.topo_comp_coup->current_sensor, se_input, [&state](Idx i) {
236+
return state.comp_topo->current_sensor_terminal_type[i] == MeasuredTerminalType::branch_to;
237+
});
238+
239+
return se_input;
240+
}
241+
242+
template <symmetry_tag sym>
243+
std::vector<ShortCircuitInput> prepare_short_circuit_input(main_model_state_c auto const& state,
244+
ComponentToMathCoupling& comp_coup, Idx n_math_solvers,
245+
ShortCircuitVoltageScaling voltage_scaling) {
246+
using detail::prepare_input;
247+
248+
// TODO(mgovers) split component mapping from actual preparing
249+
std::vector<IdxVector> topo_fault_indices(state.math_topology.size());
250+
std::vector<IdxVector> topo_bus_indices(state.math_topology.size());
251+
252+
for (Idx fault_idx{0}; fault_idx < state.components.template size<Fault>(); ++fault_idx) {
253+
auto const& fault = state.components.template get_item_by_seq<Fault>(fault_idx);
254+
if (fault.status()) {
255+
auto const node_idx = state.components.template get_seq<Node>(fault.get_fault_object());
256+
auto const topo_bus_idx = state.topo_comp_coup->node[node_idx];
257+
258+
if (topo_bus_idx.group >= 0) { // Consider non-isolated objects only
259+
topo_fault_indices[topo_bus_idx.group].push_back(fault_idx);
260+
topo_bus_indices[topo_bus_idx.group].push_back(topo_bus_idx.pos);
261+
}
262+
}
263+
}
264+
265+
auto fault_coup = std::vector<Idx2D>(state.components.template size<Fault>(),
266+
Idx2D{.group = isolated_component, .pos = not_connected});
267+
std::vector<ShortCircuitInput> sc_input(n_math_solvers);
268+
269+
for (Idx i = 0; i != n_math_solvers; ++i) {
270+
auto map = build_dense_mapping(topo_bus_indices[i], state.math_topology[i]->n_bus());
271+
272+
for (Idx reordered_idx{0}; reordered_idx < static_cast<Idx>(map.reorder.size()); ++reordered_idx) {
273+
fault_coup[topo_fault_indices[i][map.reorder[reordered_idx]]] = Idx2D{.group = i, .pos = reordered_idx};
274+
}
275+
276+
sc_input[i].fault_buses = {from_dense, std::move(map.indvector), state.math_topology[i]->n_bus()};
277+
sc_input[i].faults.resize(state.components.template size<Fault>());
278+
sc_input[i].source.resize(state.math_topology[i]->n_source());
279+
}
280+
281+
comp_coup = ComponentToMathCoupling{.fault = std::move(fault_coup)};
282+
283+
prepare_input<ShortCircuitInput, FaultCalcParam, &ShortCircuitInput::faults, Fault>(
284+
state, comp_coup.fault, sc_input, [&state](Fault const& fault) {
285+
return state.components.template get_item<Node>(fault.get_fault_object()).u_rated();
286+
});
287+
prepare_input<ShortCircuitInput, DoubleComplex, &ShortCircuitInput::source, Source>(
288+
state, state.topo_comp_coup->source, sc_input, [&state, voltage_scaling](Source const& source) {
289+
return std::pair{state.components.template get_item<Node>(source.node()).u_rated(), voltage_scaling};
290+
});
291+
292+
return sc_input;
293+
}
294+
} // namespace power_grid_model::main_core

0 commit comments

Comments
 (0)