Skip to content

Commit 8dede6b

Browse files
authored
Merge pull request #855 from PowerGridModel/feature/deserialized-api-tests
Feature/deserialized api tests
2 parents df078ff + 336e1af commit 8dede6b

File tree

12 files changed

+640
-607
lines changed

12 files changed

+640
-607
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repos:
1616
hooks:
1717
- id: black-jupyter
1818
- repo: https://github.com/pre-commit/mirrors-mypy
19-
rev: v1.11.2
19+
rev: v1.13.0
2020
hooks:
2121
- id: mypy
2222
additional_dependencies:

power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/dataset.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
#include "handle.hpp"
1212

1313
#include "power_grid_model_c/dataset.h"
14+
1415
namespace power_grid_model_cpp {
16+
class ComponentTypeNotFound : public PowerGridError {
17+
public:
18+
ComponentTypeNotFound(std::string const& component)
19+
: PowerGridError{[&]() {
20+
using namespace std::string_literals;
21+
return "ComponentType"s + component + " not found"s;
22+
}()} {}
23+
ComponentTypeNotFound(std::string_view component) : ComponentTypeNotFound{std::string{component}} {}
24+
};
1525

1626
class DatasetInfo {
1727

@@ -43,6 +53,16 @@ class DatasetInfo {
4353
return handle_.call_with(PGM_dataset_info_total_elements, info_, component_idx);
4454
}
4555

56+
Idx component_idx(std::string_view component) const {
57+
Idx const n_comp = n_components();
58+
for (Idx idx = 0; idx < n_comp; ++idx) {
59+
if (component_name(idx) == component) {
60+
return idx;
61+
}
62+
}
63+
throw ComponentTypeNotFound{component};
64+
}
65+
4666
private:
4767
Handle handle_{};
4868
RawDatasetInfo const* info_;
@@ -171,6 +191,16 @@ class DatasetConst {
171191
detail::UniquePtr<RawConstDataset, &PGM_destroy_dataset_const> dataset_;
172192
DatasetInfo info_;
173193
};
194+
195+
struct OwningMemory {
196+
std::vector<Buffer> buffers;
197+
std::vector<std::vector<Idx>> indptrs;
198+
};
199+
200+
struct OwningDataset {
201+
DatasetMutable dataset;
202+
OwningMemory storage{};
203+
};
174204
} // namespace power_grid_model_cpp
175205

176206
#endif // POWER_GRID_MODEL_CPP_DATASET_HPP

power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/handle.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class PowerGridBatchError : public PowerGridError {
4040
std::string error_message;
4141
};
4242

43-
PowerGridBatchError(std::string const& message, std::vector<FailedScenario> failed_scenarios_c)
44-
: PowerGridError{message}, failed_scenarios_{std::move(failed_scenarios_c)} {}
43+
PowerGridBatchError(std::string message, std::vector<FailedScenario> failed_scenarios_c)
44+
: PowerGridError{std::move(message)}, failed_scenarios_{std::move(failed_scenarios_c)} {}
4545
Idx error_code() const noexcept override { return PGM_batch_error; }
4646
std::vector<FailedScenario> const& failed_scenarios() const { return failed_scenarios_; }
4747

power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/serialization.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "basics.hpp"
1010
#include "dataset.hpp"
1111
#include "handle.hpp"
12+
#include "meta_data.hpp"
1213

1314
#include "power_grid_model_c/serialization.h"
1415

@@ -90,6 +91,33 @@ class Serializer {
9091
power_grid_model_cpp::Handle handle_{};
9192
detail::UniquePtr<RawSerializer, &PGM_destroy_serializer> serializer_;
9293
};
94+
95+
inline OwningDataset create_owning_dataset(DatasetWritable& writable_dataset) {
96+
auto const& info = writable_dataset.get_info();
97+
bool const is_batch = info.is_batch();
98+
Idx const batch_size = info.batch_size();
99+
auto const& dataset_name = info.name();
100+
DatasetMutable dataset_mutable{dataset_name, is_batch, batch_size};
101+
OwningMemory storage{};
102+
103+
for (Idx component_idx{}; component_idx < info.n_components(); ++component_idx) {
104+
auto const& component_name = info.component_name(component_idx);
105+
auto const& component_meta = MetaData::get_component_by_name(dataset_name, component_name);
106+
Idx const component_size = info.component_total_elements(component_idx);
107+
Idx const elements_per_scenario = info.component_elements_per_scenario(component_idx);
108+
109+
auto& current_indptr = storage.indptrs.emplace_back(elements_per_scenario < 0 ? batch_size + 1 : 0);
110+
if (!current_indptr.empty()) {
111+
current_indptr.at(0) = 0;
112+
current_indptr.at(batch_size) = component_size;
113+
}
114+
Idx* const indptr = current_indptr.empty() ? nullptr : current_indptr.data();
115+
auto& current_buffer = storage.buffers.emplace_back(component_meta, component_size);
116+
writable_dataset.set_buffer(component_name, indptr, current_buffer);
117+
dataset_mutable.add_buffer(component_name, elements_per_scenario, component_size, indptr, current_buffer);
118+
}
119+
return OwningDataset{.dataset = std::move(dataset_mutable), .storage = std::move(storage)};
120+
}
93121
} // namespace power_grid_model_cpp
94122

95123
#endif // POWER_GRID_MODEL_CPP_SERIALIZATION_HPP

tests/cpp_unit_tests/test_math_solver_common.hpp

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

55
// In this unit test the powerflow solvers are tested
66

7+
#pragma once
8+
79
#include <power_grid_model/calculation_parameters.hpp>
810
#include <power_grid_model/common/exception.hpp>
911
#include <power_grid_model/common/three_phase_tensor.hpp>

tests/cpp_unit_tests/test_math_solver_pf.hpp

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

55
// In this unit test the powerflow solvers are tested
66

7+
#pragma once
8+
79
#include "test_math_solver_common.hpp"
810

911
#include <power_grid_model/common/calculation_info.hpp>

tests/cpp_unit_tests/test_math_solver_se.hpp

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

55
// In this unit test the powerflow solvers are tested
66

7+
#pragma once
8+
79
#include "test_math_solver_common.hpp"
810

911
#include <power_grid_model/common/calculation_info.hpp>

tests/cpp_unit_tests/test_optimizer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5+
#pragma once
6+
57
#include <power_grid_model/auxiliary/meta_data.hpp>
68
#include <power_grid_model/auxiliary/meta_gen/gen_getters.hpp>
79
#include <power_grid_model/container.hpp>

tests/cpp_validation_tests/test_validation.cpp

Lines changed: 15 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define PGM_ENABLE_EXPERIMENTAL
66

7-
#include "power_grid_model_cpp.hpp"
7+
#include <power_grid_model_cpp.hpp>
88

99
#include <doctest/doctest.h>
1010
#include <nlohmann/json.hpp>
@@ -35,15 +35,6 @@ class UnsupportedValidationCase : public PowerGridError {
3535
}()} {}
3636
};
3737

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-
4738
using nlohmann::json;
4839

4940
auto read_file(std::filesystem::path const& path) {
@@ -60,72 +51,26 @@ auto read_json(std::filesystem::path const& path) {
6051
return j;
6152
}
6253

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-
10454
OwningDataset create_result_dataset(OwningDataset const& input, std::string const& dataset_name, bool is_batch = false,
10555
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();
10857

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{}};
11359

11460
for (Idx component_idx{}; component_idx != input_info.n_components(); ++component_idx) {
11561
auto const& component_name = input_info.component_name(component_idx);
11662
auto const& component_meta = MetaData::get_component_by_name(dataset_name, component_name);
11763
Idx const component_elements_per_scenario = input_info.component_elements_per_scenario(component_idx);
11864
Idx const component_size = input_info.component_total_elements(component_idx);
11965

120-
auto& current_indptr = owning_dataset.storage.indptrs.emplace_back(
66+
auto& current_indptr = result.storage.indptrs.emplace_back(
12167
input_info.component_elements_per_scenario(component_idx) < 0 ? batch_size + 1 : 0);
12268
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);
12672
}
127-
owning_dataset.const_dataset = owning_dataset.dataset.value();
128-
return owning_dataset;
73+
return result;
12974
}
13075

13176
OwningDataset load_dataset(std::filesystem::path const& path) {
@@ -259,19 +204,13 @@ void assert_result(OwningDataset const& owning_result, OwningDataset const& owni
259204
std::map<std::string, double, std::less<>> atol, double rtol) {
260205
using namespace std::string_literals;
261206

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};
266208
auto const& result_info = result.get_info();
267209
auto const& result_name = result_info.name();
268210
Idx const result_batch_size = result_info.batch_size();
269211
auto const& storage = owning_result.storage;
270212

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;
275214
auto const& reference_result_info = reference_result.get_info();
276215
auto const& reference_result_name = reference_result_info.name();
277216
auto const& reference_storage = owning_reference_result.storage;
@@ -574,8 +513,8 @@ void validate_single_case(CaseParam const& param) {
574513

575514
// create and run model
576515
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);
579518

580519
// check results
581520
assert_result(result, validation_case.output.value(), param.atol, param.rtol);
@@ -586,21 +525,20 @@ void validate_batch_case(CaseParam const& param) {
586525
execute_test(param, [&]() {
587526
auto const output_prefix = get_output_type(param.calculation_type, param.sym);
588527
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();
590529
Idx const batch_size = info.batch_size();
591530
auto const batch_result =
592531
create_result_dataset(validation_case.output_batch.value(), output_prefix, true, batch_size);
593532

594533
// create model
595-
Model model{50.0, validation_case.input.const_dataset.value()};
534+
Model model{50.0, validation_case.input.dataset};
596535

597536
// check results after whole update is finished
598537
for (Idx const threading : {-1, 0, 1, 2}) {
599538
CAPTURE(threading);
600539
// set options and run
601540
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);
604542

605543
// check results
606544
assert_result(batch_result, validation_case.output_batch.value(), param.atol, param.rtol);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
#pragma once
6+
7+
#include <power_grid_model_cpp/serialization.hpp>
8+
9+
namespace power_grid_model_cpp_test {
10+
inline power_grid_model_cpp::OwningDataset load_dataset(std::string const& json_string) {
11+
power_grid_model_cpp::Deserializer deserializer{json_string, PGM_json};
12+
auto& writable_dataset = deserializer.get_dataset();
13+
auto owning_dataset = power_grid_model_cpp::create_owning_dataset(writable_dataset);
14+
deserializer.parse_to_buffer();
15+
return owning_dataset;
16+
}
17+
} // namespace power_grid_model_cpp_test

0 commit comments

Comments
 (0)