4
4
5
5
#define PGM_ENABLE_EXPERIMENTAL
6
6
7
- #include " power_grid_model_cpp.hpp"
7
+ #include < power_grid_model_cpp.hpp>
8
8
9
9
#include < doctest/doctest.h>
10
10
#include < nlohmann/json.hpp>
@@ -35,15 +35,6 @@ class UnsupportedValidationCase : public PowerGridError {
35
35
}()} {}
36
36
};
37
37
38
- class OptionalNotInitialized : public PowerGridError {
39
- public:
40
- OptionalNotInitialized (std::string const & object)
41
- : PowerGridError{[&]() {
42
- using namespace std ::string_literals;
43
- return " Optional " s + object + " object not initialized" s;
44
- }()} {}
45
- };
46
-
47
38
using nlohmann::json;
48
39
49
40
auto read_file (std::filesystem::path const & path) {
@@ -60,72 +51,26 @@ auto read_json(std::filesystem::path const& path) {
60
51
return j;
61
52
}
62
53
63
- struct OwningMemory {
64
- std::vector<Buffer> buffers;
65
- std::vector<std::vector<Idx>> indptrs;
66
- };
67
-
68
- struct OwningDataset {
69
- std::optional<DatasetMutable> dataset;
70
- std::optional<DatasetConst> const_dataset;
71
- OwningMemory storage{};
72
- };
73
-
74
- OwningDataset create_owning_dataset (DatasetWritable& writable_dataset) {
75
- auto const & info = writable_dataset.get_info ();
76
- bool const is_batch = info.is_batch ();
77
- Idx const batch_size = info.batch_size ();
78
- auto const & dataset_name = info.name ();
79
- OwningDataset owning_dataset{.dataset {DatasetMutable{dataset_name, is_batch, batch_size}},
80
- .const_dataset = std::nullopt};
81
-
82
- for (Idx component_idx{}; component_idx < info.n_components (); ++component_idx) {
83
- auto const & component_name = info.component_name (component_idx);
84
- auto const & component_meta = MetaData::get_component_by_name (dataset_name, component_name);
85
- Idx const component_elements_per_scenario = info.component_elements_per_scenario (component_idx);
86
- Idx const component_size = info.component_total_elements (component_idx);
87
-
88
- auto & current_indptr = owning_dataset.storage .indptrs .emplace_back (
89
- info.component_elements_per_scenario (component_idx) < 0 ? batch_size + 1 : 0 );
90
- if (!current_indptr.empty ()) {
91
- current_indptr.at (0 ) = 0 ;
92
- current_indptr.at (batch_size) = component_size;
93
- }
94
- Idx* const indptr = current_indptr.empty () ? nullptr : current_indptr.data ();
95
- auto & current_buffer = owning_dataset.storage .buffers .emplace_back (component_meta, component_size);
96
- writable_dataset.set_buffer (component_name, indptr, current_buffer);
97
- owning_dataset.dataset .value ().add_buffer (component_name, component_elements_per_scenario, component_size,
98
- indptr, current_buffer);
99
- }
100
- owning_dataset.const_dataset = writable_dataset;
101
- return owning_dataset;
102
- }
103
-
104
54
OwningDataset create_result_dataset (OwningDataset const & input, std::string const & dataset_name, bool is_batch = false ,
105
55
Idx batch_size = 1 ) {
106
- OwningDataset owning_dataset{.dataset {DatasetMutable{dataset_name, is_batch, batch_size}},
107
- .const_dataset = std::nullopt};
56
+ DatasetInfo const & input_info = input.dataset .get_info ();
108
57
109
- if (!input.const_dataset .has_value ()) {
110
- throw OptionalNotInitialized (" DatasetConst" );
111
- }
112
- DatasetInfo const & input_info = input.const_dataset .value ().get_info ();
58
+ OwningDataset result{.dataset = DatasetMutable{dataset_name, is_batch, batch_size}, .storage {}};
113
59
114
60
for (Idx component_idx{}; component_idx != input_info.n_components (); ++component_idx) {
115
61
auto const & component_name = input_info.component_name (component_idx);
116
62
auto const & component_meta = MetaData::get_component_by_name (dataset_name, component_name);
117
63
Idx const component_elements_per_scenario = input_info.component_elements_per_scenario (component_idx);
118
64
Idx const component_size = input_info.component_total_elements (component_idx);
119
65
120
- auto & current_indptr = owning_dataset .storage .indptrs .emplace_back (
66
+ auto & current_indptr = result .storage .indptrs .emplace_back (
121
67
input_info.component_elements_per_scenario (component_idx) < 0 ? batch_size + 1 : 0 );
122
68
Idx const * const indptr = current_indptr.empty () ? nullptr : current_indptr.data ();
123
- auto & current_buffer = owning_dataset .storage .buffers .emplace_back (component_meta, component_size);
124
- owning_dataset .dataset .value (). add_buffer (component_name, component_elements_per_scenario, component_size,
125
- indptr, current_buffer);
69
+ auto & current_buffer = result .storage .buffers .emplace_back (component_meta, component_size);
70
+ result .dataset .add_buffer (component_name, component_elements_per_scenario, component_size, indptr ,
71
+ current_buffer);
126
72
}
127
- owning_dataset.const_dataset = owning_dataset.dataset .value ();
128
- return owning_dataset;
73
+ return result;
129
74
}
130
75
131
76
OwningDataset load_dataset (std::filesystem::path const & path) {
@@ -259,19 +204,13 @@ void assert_result(OwningDataset const& owning_result, OwningDataset const& owni
259
204
std::map<std::string, double , std::less<>> atol, double rtol) {
260
205
using namespace std ::string_literals;
261
206
262
- if (!owning_result.const_dataset .has_value ()) {
263
- throw OptionalNotInitialized (" DatasetConst" );
264
- }
265
- DatasetConst const & result = owning_result.const_dataset .value ();
207
+ DatasetConst const result{owning_result.dataset };
266
208
auto const & result_info = result.get_info ();
267
209
auto const & result_name = result_info.name ();
268
210
Idx const result_batch_size = result_info.batch_size ();
269
211
auto const & storage = owning_result.storage ;
270
212
271
- if (!owning_reference_result.const_dataset .has_value ()) {
272
- throw OptionalNotInitialized (" DatasetConst" );
273
- }
274
- DatasetConst const & reference_result = owning_reference_result.const_dataset .value ();
213
+ DatasetConst const & reference_result = owning_reference_result.dataset ;
275
214
auto const & reference_result_info = reference_result.get_info ();
276
215
auto const & reference_result_name = reference_result_info.name ();
277
216
auto const & reference_storage = owning_reference_result.storage ;
@@ -574,8 +513,8 @@ void validate_single_case(CaseParam const& param) {
574
513
575
514
// create and run model
576
515
auto const & options = get_options (param);
577
- Model model{50.0 , validation_case.input .const_dataset . value () };
578
- model.calculate (options, result.dataset . value () );
516
+ Model model{50.0 , validation_case.input .dataset };
517
+ model.calculate (options, result.dataset );
579
518
580
519
// check results
581
520
assert_result (result, validation_case.output .value (), param.atol , param.rtol );
@@ -586,21 +525,20 @@ void validate_batch_case(CaseParam const& param) {
586
525
execute_test (param, [&]() {
587
526
auto const output_prefix = get_output_type (param.calculation_type , param.sym );
588
527
auto const validation_case = create_validation_case (param, output_prefix);
589
- auto const & info = validation_case.update_batch .value ().const_dataset . value () .get_info ();
528
+ auto const & info = validation_case.update_batch .value ().dataset .get_info ();
590
529
Idx const batch_size = info.batch_size ();
591
530
auto const batch_result =
592
531
create_result_dataset (validation_case.output_batch .value (), output_prefix, true , batch_size);
593
532
594
533
// create model
595
- Model model{50.0 , validation_case.input .const_dataset . value () };
534
+ Model model{50.0 , validation_case.input .dataset };
596
535
597
536
// check results after whole update is finished
598
537
for (Idx const threading : {-1 , 0 , 1 , 2 }) {
599
538
CAPTURE (threading);
600
539
// set options and run
601
540
auto const & options = get_options (param, threading);
602
- model.calculate (options, batch_result.dataset .value (),
603
- validation_case.update_batch .value ().const_dataset .value ());
541
+ model.calculate (options, batch_result.dataset , validation_case.update_batch .value ().dataset );
604
542
605
543
// check results
606
544
assert_result (batch_result, validation_case.output_batch .value (), param.atol , param.rtol );
0 commit comments