diff --git a/power_grid_model_c/power_grid_model_c/src/buffer.cpp b/power_grid_model_c/power_grid_model_c/src/buffer.cpp index bc9881754..30d4c1a99 100644 --- a/power_grid_model_c/power_grid_model_c/src/buffer.cpp +++ b/power_grid_model_c/power_grid_model_c/src/buffer.cpp @@ -17,6 +17,7 @@ namespace { using namespace power_grid_model; +using meta_data::MetaAttribute; using meta_data::RawDataConstPtr; using meta_data::RawDataPtr; using power_grid_model_c::call_with_catch; diff --git a/power_grid_model_c/power_grid_model_c/src/dataset.cpp b/power_grid_model_c/power_grid_model_c/src/dataset.cpp index acd3617cc..5b1115b2f 100644 --- a/power_grid_model_c/power_grid_model_c/src/dataset.cpp +++ b/power_grid_model_c/power_grid_model_c/src/dataset.cpp @@ -15,6 +15,7 @@ #include #include +namespace { using namespace power_grid_model; using namespace power_grid_model::meta_data; using power_grid_model_c::call_with_catch; @@ -27,6 +28,7 @@ using power_grid_model_c::safe_ptr_maybe_nullptr; using power_grid_model_c::safe_str_view; using power_grid_model_c::to_c_bool; using power_grid_model_c::to_c_size; +} // namespace // dataset info diff --git a/power_grid_model_c/power_grid_model_c/src/model.cpp b/power_grid_model_c/power_grid_model_c/src/model.cpp index 6e8378003..6fdc67792 100644 --- a/power_grid_model_c/power_grid_model_c/src/model.cpp +++ b/power_grid_model_c/power_grid_model_c/src/model.cpp @@ -156,7 +156,7 @@ class BadCalculationRequest : public PowerGridError { explicit BadCalculationRequest(std::string msg) : PowerGridError{std::move(msg)} {} }; -void calculate_single_batch_dimension_impl(MainModel& model, PGM_Options const& opt, +void calculate_single_batch_dimension_impl(MainModel& model, MainModel::Options const& options, MutableDataset const& output_dataset, ConstDataset const* batch_dataset) { // check dataset integrity if ((batch_dataset != nullptr) && (!batch_dataset->is_batch() || !output_dataset.is_batch())) { @@ -168,13 +168,6 @@ void calculate_single_batch_dimension_impl(MainModel& model, PGM_Options const& ? safe_ptr_get(batch_dataset) : ConstDataset{false, 1, "update", output_dataset.meta_data()}; - check_calculate_valid_options(opt); - auto const options = extract_calculation_options(opt); - - if (opt.experimental_features == PGM_experimental_features_disabled) { - check_no_experimental_features_used(model, options); - } - model.calculate(options, output_dataset, exported_update_dataset); } @@ -241,29 +234,33 @@ class MDBatchExceptionHandler : public power_grid_model_c::DefaultExceptionHandl Idx get_batch_dimension(ConstDataset const* batch_dataset) { Idx dimension = 0; - while (batch_dataset != nullptr) { + ConstDataset const* safe_batch_dataset = safe_ptr_maybe_nullptr(batch_dataset); + while (safe_batch_dataset != nullptr) { ++dimension; - batch_dataset = batch_dataset->get_next_cartesian_product_dimension(); + safe_batch_dataset = + safe_ptr_maybe_nullptr(safe_ptr_get(safe_batch_dataset).get_next_cartesian_product_dimension()); } return dimension; } Idx get_stride_size(ConstDataset const* batch_dataset) { Idx size = 1; - ConstDataset const* current = batch_dataset->get_next_cartesian_product_dimension(); + ConstDataset const* current = + safe_ptr_maybe_nullptr(safe_ptr_get(batch_dataset).get_next_cartesian_product_dimension()); while (current != nullptr) { - size *= current->batch_size(); - current = current->get_next_cartesian_product_dimension(); + auto const& safe_current = safe_ptr_get(current); + size *= safe_current.batch_size(); + current = safe_current.get_next_cartesian_product_dimension(); } return size; } // run calculation -void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt, MutableDataset const& output_dataset, +void calculate_multi_dimensional_impl(MainModel& model, MainModel::Options const& options, MutableDataset const& output_dataset, ConstDataset const* batch_dataset) { // for dimension < 2 (one-time or 1D batch), call implementation directly if (auto const batch_dimension = get_batch_dimension(batch_dataset); batch_dimension < 2) { - calculate_single_batch_dimension_impl(model, opt, output_dataset, batch_dataset); + calculate_single_batch_dimension_impl(model, options, output_dataset, batch_dataset); return; } @@ -280,7 +277,7 @@ void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt, // a new handle call_with_catch( &local_handle, - [&model, &opt, &output_dataset, &safe_batch_dataset, i, stride_size] { + [&model, &options, &output_dataset, &safe_batch_dataset, i, stride_size] { // create sliced datasets for the rest of dimensions ConstDataset const single_update_dataset = safe_batch_dataset.get_individual_scenario(i); MutableDataset const sliced_output_dataset = @@ -293,7 +290,7 @@ void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt, local_model.update_components(single_update_dataset); // recursive call - calculate_multi_dimensional_impl(local_model, opt, sliced_output_dataset, + calculate_multi_dimensional_impl(local_model, options, sliced_output_dataset, safe_batch_dataset.get_next_cartesian_product_dimension()); }, MDBatchExceptionHandler{i * stride_size, stride_size}); @@ -305,6 +302,18 @@ void calculate_multi_dimensional_impl(MainModel& model, PGM_Options const& opt, } } +void calculate_impl(MainModel& model, PGM_Options const& options, MutableDataset const& output_dataset, + ConstDataset const* batch_dataset) { + check_calculate_valid_options(options); + auto const extracted_options = extract_calculation_options(options); + + if (options.experimental_features == PGM_experimental_features_disabled) { + check_no_experimental_features_used(model, extracted_options); + } + + calculate_multi_dimensional_impl(model, extracted_options, output_dataset, batch_dataset); +} + } // namespace // run calculation @@ -313,7 +322,7 @@ void PGM_calculate(PGM_Handle* handle, PGM_PowerGridModel* model, PGM_Options co call_with_catch( handle, [model, opt, output_dataset, batch_dataset] { - calculate_multi_dimensional_impl(safe_ptr_get(cast_to_cpp(model)), safe_ptr_get(opt), + calculate_impl(safe_ptr_get(cast_to_cpp(model)), safe_ptr_get(opt), safe_ptr_get(cast_to_cpp(output_dataset)), safe_ptr_maybe_nullptr(cast_to_cpp(batch_dataset))); }, diff --git a/power_grid_model_c/power_grid_model_c/src/serialization.cpp b/power_grid_model_c/power_grid_model_c/src/serialization.cpp index a98ffd87a..0547d9b2e 100644 --- a/power_grid_model_c/power_grid_model_c/src/serialization.cpp +++ b/power_grid_model_c/power_grid_model_c/src/serialization.cpp @@ -37,6 +37,13 @@ struct SerializationExceptionHandler : public power_grid_model_c::DefaultExcepti constexpr SerializationExceptionHandler serialization_exception_handler{}; } // namespace +struct PGM_Serializer : public power_grid_model::meta_data::Serializer { + using Serializer::Serializer; +}; +struct PGM_Deserializer : public power_grid_model::meta_data::Deserializer { + using Deserializer::Deserializer; +}; + PGM_Deserializer* PGM_create_deserializer_from_binary_buffer(PGM_Handle* handle, char const* data, PGM_Idx size, PGM_Idx serialization_format) { return call_with_catch(