Skip to content

Commit b3c8f52

Browse files
committed
final implementation
Signed-off-by: Santiago Figueroa Manrique <figueroa1395@gmail.com>
1 parent 0ff8f0d commit b3c8f52

2 files changed

Lines changed: 47 additions & 28 deletions

File tree

power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/dataset.hpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@
2424
#include <vector>
2525

2626
namespace power_grid_model_cpp {
27-
namespace detail {
28-
inline std::set<std::string, std::less<>> get_irrelevant_components(PGM_CalculationType calculation_type) {
29-
using namespace std::string_literals;
30-
31-
if (calculation_type == PGM_power_flow || calculation_type == PGM_state_estimation) {
32-
return {"fault"s};
33-
}
34-
return {};
35-
}
36-
} // namespace detail
3727
class ComponentTypeNotFound : public PowerGridError {
3828
public:
3929
ComponentTypeNotFound(std::string const& component)
@@ -144,8 +134,8 @@ class DatasetWritable {
144134
class DatasetMutable {
145135
public:
146136
explicit DatasetMutable(std::string const& dataset, bool is_batch, Idx batch_size)
147-
: dataset_{handle_.call_with(PGM_create_dataset_mutable, dataset.c_str(), (is_batch ? Idx{1} : Idx{0}),
148-
batch_size)},
137+
: dataset_{
138+
handle_.call_with(PGM_create_dataset_mutable, dataset.c_str(), (is_batch ? Idx{1} : Idx{0}), batch_size)},
149139
info_{handle_.call_with(PGM_dataset_mutable_get_info, get())} {}
150140

151141
RawMutableDataset const* get() const { return dataset_.get(); }
@@ -183,8 +173,8 @@ class DatasetMutable {
183173
class DatasetConst {
184174
public:
185175
explicit DatasetConst(std::string const& dataset, bool is_batch, Idx batch_size)
186-
: dataset_{handle_.call_with(PGM_create_dataset_const, dataset.c_str(), (is_batch ? Idx{1} : Idx{0}),
187-
batch_size)},
176+
: dataset_{
177+
handle_.call_with(PGM_create_dataset_const, dataset.c_str(), (is_batch ? Idx{1} : Idx{0}), batch_size)},
188178
info_{handle_.call_with(PGM_dataset_const_get_info, get())} {}
189179

190180
DatasetConst(DatasetWritable const& writable_dataset)
@@ -279,6 +269,28 @@ inline std::string get_output_type(PGM_CalculationType calculation_type, bool sy
279269
return "asym_output"s;
280270
}
281271

272+
inline std::set<std::string, std::less<>> get_irrelevant_components(PGM_CalculationType calculation_type) {
273+
using namespace std::string_literals;
274+
275+
if (calculation_type == PGM_power_flow) {
276+
return {"sym_voltage_sensor"s,
277+
"sym_current_sensor"s,
278+
"sym_power_sensor"s,
279+
"asym_voltage_sensor"s,
280+
"asym_current_sensor"s,
281+
"asym_power_sensor"s,
282+
"fault"s};
283+
}
284+
if (calculation_type == PGM_state_estimation) {
285+
return {"fault"s, "transformer_tap_regulator"s, "voltage_regulator"s};
286+
}
287+
if (calculation_type == PGM_short_circuit) {
288+
return {"sym_voltage_sensor"s, "sym_current_sensor"s, "sym_power_sensor"s, "asym_voltage_sensor"s,
289+
"asym_current_sensor"s, "asym_power_sensor"s, "transformer_tap_regulator"s, "voltage_regulator"s};
290+
}
291+
return {};
292+
}
293+
282294
struct OwningDataset {
283295
DatasetMutable dataset;
284296
OwningMemory storage{};
@@ -333,7 +345,7 @@ struct OwningDataset {
333345
: dataset{get_output_type(calculation_type, sym), is_batch, batch_size} {
334346
DatasetInfo const& ref_info = ref_dataset.dataset.get_info();
335347
bool const enable_filters = !output_component_attribute_filters.empty();
336-
auto const irrelevant_components = detail::get_irrelevant_components(calculation_type);
348+
auto const irrelevant_components = get_irrelevant_components(calculation_type);
337349

338350
auto const contains_irrelevant_component = [&irrelevant_components](std::string const& component) {
339351
return irrelevant_components.find(component) != irrelevant_components.end();

tests/native_api_tests/test_api_dataset.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,36 @@ TEST_CASE("Test get_output_type") {
6969
}
7070
}
7171

72-
// faults are treated specially because they are added to the topology at runtime (not model construction)
73-
// and only if the calculation type is short circuit
74-
// for power flow and state estimation they are never in the topology, so we shouldn't allocate buffers for them in the
75-
// output dataset
7672
TEST_CASE("Test get_irrelevant_components") {
7773
using namespace std::string_literals;
7874

7975
SUBCASE("Power flow") {
80-
auto const component_list = std::set<std::string, std::less<>>{"fault"s};
81-
CHECK(component_list == detail::get_irrelevant_components(PGM_power_flow));
76+
auto const component_list = std::set<std::string, std::less<>>{"sym_voltage_sensor"s,
77+
"sym_current_sensor"s,
78+
"sym_power_sensor"s,
79+
"asym_voltage_sensor"s,
80+
"asym_current_sensor"s,
81+
"asym_power_sensor"s,
82+
"fault"s};
83+
CHECK(component_list == get_irrelevant_components(PGM_power_flow));
8284
}
8385
SUBCASE("State estimation") {
84-
auto const component_list = std::set<std::string, std::less<>>{"fault"s};
85-
CHECK(component_list == detail::get_irrelevant_components(PGM_state_estimation));
86+
auto const component_list =
87+
std::set<std::string, std::less<>>{"fault"s, "transformer_tap_regulator"s, "voltage_regulator"s};
88+
CHECK(component_list == get_irrelevant_components(PGM_state_estimation));
8689
}
8790

8891
SUBCASE("Short circuit") {
8992

90-
auto const component_list = std::set<std::string, std::less<>>{};
91-
CHECK(component_list == detail::get_irrelevant_components(PGM_short_circuit));
93+
auto const component_list = std::set<std::string, std::less<>>{
94+
"sym_voltage_sensor"s, "sym_current_sensor"s, "sym_power_sensor"s, "asym_voltage_sensor"s,
95+
"asym_current_sensor"s, "asym_power_sensor"s, "transformer_tap_regulator"s, "voltage_regulator"s};
96+
CHECK(component_list == get_irrelevant_components(PGM_short_circuit));
9297
}
9398
}
9499

95100
TEST_CASE("OwningDataset - filter irrelevant components") {
101+
using namespace std::string_literals;
96102
auto const input_dataset = load_dataset(json_data);
97103
auto options = Options{};
98104
Model model{50.0, input_dataset.dataset};
@@ -113,7 +119,7 @@ TEST_CASE("OwningDataset - filter irrelevant components") {
113119

114120
CHECK_NOTHROW(model.calculate(options, output_dataset.dataset));
115121
auto const& info = output_dataset.dataset.get_info();
116-
check_irrelevant_components(info, {"fault"});
122+
check_irrelevant_components(info, {"fault", "sym_power_sensor", "sym_voltage_sensor", "asym_current_sensor"});
117123
}
118124

119125
SUBCASE("State estimation filters out faults") {
@@ -123,7 +129,7 @@ TEST_CASE("OwningDataset - filter irrelevant components") {
123129

124130
CHECK_NOTHROW(model.calculate(options, output_dataset.dataset));
125131
auto const& info = output_dataset.dataset.get_info();
126-
check_irrelevant_components(info, {"fault"});
132+
check_irrelevant_components(info, {"fault", "voltage_regulator"});
127133
}
128134

129135
SUBCASE("Short circuit filters out sensors") {
@@ -133,7 +139,8 @@ TEST_CASE("OwningDataset - filter irrelevant components") {
133139

134140
CHECK_NOTHROW(model.calculate(options, output_dataset.dataset));
135141
auto const& info = output_dataset.dataset.get_info();
136-
check_irrelevant_components(info, {});
142+
check_irrelevant_components(
143+
info, {"sym_power_sensor", "sym_voltage_sensor", "asym_current_sensor", "voltage_regulator"});
137144
}
138145
}
139146

0 commit comments

Comments
 (0)