diff --git a/.clang-tidy b/.clang-tidy index 37e1ae0..2ff94f7 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -6,24 +6,34 @@ Checks: ' -*, boost-*, bugprone-*, +-bugprone-easily-swappable-parameters, cert-*, clang-analyzer-*, concurrency-*, cppcoreguidelines-*, +-cppcoreguidelines-avoid-c-arrays, -cppcoreguidelines-owning-memory, +-cppcoreguidelines-avoid-magic-numbers, +-cppcoreguidelines-pro-bounds-pointer-arithmetic, +-cppcoreguidelines-pro-type-reinterpret-cast, darwin-*, hiccp-*, llvm-*, -llvm-header-guard, google-*, -google-build-using-namespace, +-google-explicit-constructor misc-*, +-misc-non-private-member-variables-in-classes, modernize-*, +-modernize-avoid-c-arrays, -modernize-use-trailing-return-type, +-modernize-use-nodiscard, performance-*, portability-*, readability-*, -readability-identifier-length, +-readability-magic-numbers, ' WarningsAsErrors: '*' diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index a164e99..c8c21ff 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -45,7 +45,9 @@ jobs: - uses: actions/checkout@v4 with: submodules: 'true' - + - name: Initialize and update submodules + run: | + git submodule update --init --recursive - name: Install packages run: | sudo apt-get update diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9c97046..bc7735a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -80,7 +80,9 @@ jobs: - uses: actions/checkout@v4 with: submodules: 'true' - + - name: Initialize and update submodules + run: | + git submodule update --init --recursive - name: Install packages run: | sudo apt-get update diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 3d7c624..3e9548e 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -32,6 +32,9 @@ jobs: with: fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis submodules: 'true' + - name: Initialize and update submodules + run: | + git submodule update --init --recursive - name: Install packages run: | sudo apt-get update diff --git a/.gitmodules b/.gitmodules index 571de00..cfe160c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MPL-2.0 -[submodule "power-grid-model"] - path = power-grid-model +[submodule "deps/power-grid-model"] + path = deps/power-grid-model url = git@github.com:PowerGridModel/power-grid-model.git + branch = main diff --git a/CMakeLists.txt b/CMakeLists.txt index 5621d69..4a6c3eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,11 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) include(GNUInstallDirs) -add_subdirectory("power-grid-model") +find_package(Eigen3 CONFIG REQUIRED) +find_package(Boost REQUIRED) +find_package(nlohmann_json CONFIG REQUIRED) + +add_subdirectory("deps") # add C library add_subdirectory("power_grid_model_io_native_c") diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt new file mode 100644 index 0000000..e3d333d --- /dev/null +++ b/deps/CMakeLists.txt @@ -0,0 +1,6 @@ +# SPDX-FileCopyrightText: Contributors to the Power Grid Model project +# +# SPDX-License-Identifier: MPL-2.0 + +add_subdirectory("power-grid-model") + diff --git a/deps/power-grid-model b/deps/power-grid-model new file mode 160000 index 0000000..a05b88a --- /dev/null +++ b/deps/power-grid-model @@ -0,0 +1 @@ +Subproject commit a05b88a326583ecfad5bd72d5a2f99ae352d1907 diff --git a/power-grid-model b/power-grid-model deleted file mode 160000 index 5603232..0000000 --- a/power-grid-model +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 560323280a537ab5e71dfefc5c8e694c8f4e0256 diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/CMakeLists.txt b/power_grid_model_io_native_c/power_grid_model_io_native/CMakeLists.txt index 483e2ba..ed03d50 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/CMakeLists.txt +++ b/power_grid_model_io_native_c/power_grid_model_io_native/CMakeLists.txt @@ -4,6 +4,6 @@ add_library(power_grid_model_io_native INTERFACE) -target_link_libraries(power_grid_model_io_native INTERFACE power_grid_model) - target_include_directories(power_grid_model_io_native INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include") + +target_link_libraries(power_grid_model_io_native INTERFACE power_grid_model) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp new file mode 100644 index 0000000..4c104ca --- /dev/null +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -0,0 +1,128 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#pragma once +#ifndef POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP +#define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP + +#include +#include + +#include + +class PgmVnfConverter { + public: + PgmVnfConverter(char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr); + + // Public member functions + void parse_vnf_file(); + power_grid_model::ConstDataset const* convert_input(power_grid_model::ConstDataset const* dataset); + + private: + // Private attributes + char* f_file_buffer; + power_grid_model::WritableDataset* + deserialized_data; // this type because it is generated by a deserializer type structure + + // Private setters/getters + void set_file_buffer(char* file_buffer); + + void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data); + + char* get_file_buffer(); + + power_grid_model::WritableDataset* get_deserialized_data(); + + // Private member functions + void convert_node_input(); + void convert_line_input(); + void convert_sources_input(); + void convert_sym_loads_input(); + void convert_shunts_input(); + void convert_transformer_input(); + void convert_sym_gens_input(); + void convert_links_input(); +}; + +inline PgmVnfConverter::PgmVnfConverter(char* buffer, power_grid_model::WritableDataset* data) + : f_file_buffer(buffer), deserialized_data(data) { + using namespace std::string_literals; + using power_grid_model::ExperimentalFeature; + throw ExperimentalFeature{"PGM_VNF_converter", ExperimentalFeature::TypeValuePair{.name = "PGM_VNF_conversion", + .value = std::to_string(1)}}; +} + +inline void PgmVnfConverter::parse_vnf_file() { + // the function should use a deserializer type structure + // will be implemented later +} + +inline power_grid_model::ConstDataset const* +PgmVnfConverter::convert_input(power_grid_model::ConstDataset const* /*dataset*/) { + convert_node_input(); + convert_line_input(); + convert_sources_input(); + convert_sym_loads_input(); + convert_shunts_input(); + convert_transformer_input(); + convert_sym_gens_input(); + convert_links_input(); + + // then return the buffer + // return pgm_input_data; + // for now. + power_grid_model::ConstDataset* fake_data = nullptr; + return fake_data; +} + +inline void PgmVnfConverter::set_file_buffer(char* file_buffer) { this->f_file_buffer = file_buffer; } + +inline void PgmVnfConverter::set_deserialized_data(power_grid_model::WritableDataset* data) { + this->deserialized_data = data; +} + +inline char* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } + +inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_data() { return this->deserialized_data; } + +inline void PgmVnfConverter::convert_node_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_line_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_sources_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_sym_loads_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_shunts_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_transformer_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_sym_gens_input() { + // Implementation +} + +inline void PgmVnfConverter::convert_links_input() { + // Implementation +} + +inline void parse_vnf_file_wrapper(PgmVnfConverter* obj) { obj->parse_vnf_file(); } + +inline power_grid_model::ConstDataset const* convert_input_wrapper(PgmVnfConverter* obj, + power_grid_model::ConstDataset const* dataset) { + return obj->convert_input(dataset); +} + +#endif // POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/CMakeLists.txt b/power_grid_model_io_native_c/power_grid_model_io_native_c/CMakeLists.txt index 90cdf39..13ea773 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/CMakeLists.txt +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/CMakeLists.txt @@ -5,6 +5,7 @@ # C API library add_library(power_grid_model_io_native_c SHARED "src/handle.cpp" + "src/vnf_pgm_converter.cpp" ) target_include_directories(power_grid_model_io_native_c PUBLIC @@ -15,6 +16,7 @@ target_include_directories(power_grid_model_io_native_c PUBLIC set(PGM_IO_NATIVE_PUBLIC_HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/basics.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/handle.h" + "${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c/vnf_pgm_converter.h" "${CMAKE_CURRENT_SOURCE_DIR}/include/power_grid_model_io_native_c.h" ) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c.h b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c.h index 1aae595..6eecc9f 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c.h +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c.h @@ -18,5 +18,6 @@ #include "power_grid_model_io_native_c/basics.h" #include "power_grid_model_io_native_c/handle.h" +#include "power_grid_model_io_native_c/vnf_pgm_converter.h" #endif // POWER_GRID_MODEL_IO_NATIVE_C_H diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/basics.h b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/basics.h index 5a951aa..ca1b243 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/basics.h +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/basics.h @@ -64,6 +64,18 @@ typedef int32_t PGM_IO_ID; // */ // typedef struct PGM_IO_VnfConverter PGM_IO_VnfConverter; +/** + * @brief Opaque struct for the VnfConverter class. + * + */ +typedef struct PGM_IO_VnfConverter PGM_IO_VnfConverter; + +/** + * @brief Opaque struct for the ConstDataset. + * + */ +typedef struct PGM_IO_ConstDataset PGM_IO_ConstDataset; + /** * @brief Opaque struct for the handle class. * diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h new file mode 100644 index 0000000..d6fcb75 --- /dev/null +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#pragma once +#ifndef POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_H +#define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_H + +#include "basics.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Create the PGM_VNF_converter + * @param handle + * @param file_buffer A pointer to the null-terminated C string. + * @return The pointer to a PGM_VNF_converter instance. The instance must be freed by + * PGM_VNF_delete_Converter + */ +PGM_IO_API PGM_IO_VnfConverter* PGM_VNF_create_converter(PGM_IO_Handle* handle, char* file_buffer); + +/** + * @brief Retrieve the transformed input data from .vnf format to PGM format + * @param handle + * @param converter_ptr A pointer to a PGM_VNF_converter instace. + * @param dataset A pointer to the const dataset supplied by the user. + * @return The pointer to the const dataset instance supplied by the user which has been filled in. + */ +PGM_IO_API PGM_IO_ConstDataset const* PGM_VNF_get_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr, + PGM_IO_ConstDataset const* dataset); + +/** + * @brief Destroy the converter and free up the memory that was dedicated to it. + * @param converter_ptr A pointer to a PGM_VNF_converter instance. + */ +PGM_IO_API void PGM_VNF_delete_Converter(PGM_IO_VnfConverter* converter_ptr); + +#ifdef __cplusplus +} +#endif + +#endif // POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_H diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp new file mode 100644 index 0000000..6872acc --- /dev/null +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#define PGM_IO_DLL_EXPORTS + +#include + +#include "handle.hpp" +#include +#include + +#include + +using power_grid_model::ConstDataset; + +// TODO(Laurynas-Jagutis) add call_with_catch for these functions +PGM_IO_VnfConverter* PGM_VNF_create_converter(const PGM_IO_Handle* /*handle*/, char* file_buffer) { + auto* converter = new PgmVnfConverter(file_buffer); + parse_vnf_file_wrapper(converter); + return reinterpret_cast(converter); +} + +PGM_IO_ConstDataset const* PGM_VNF_get_input_data(const PGM_IO_Handle* /*handle*/, PGM_IO_VnfConverter* converter_ptr, + PGM_IO_ConstDataset const* dataset) { + auto* converter = reinterpret_cast(converter_ptr); + auto const* data = reinterpret_cast(dataset); + convert_input_wrapper(converter, data); + return reinterpret_cast(data); +} + +void PGM_VNF_delete_Converter(PGM_IO_VnfConverter* converter_ptr) { + auto* converter = reinterpret_cast(converter_ptr); + delete converter; +} diff --git a/pyproject.toml b/pyproject.toml index 344a17b..e0e55b2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,7 +97,7 @@ ignore_missing_imports = true show_column_numbers = true non_interactive = true install_types = true -exclude = "power-grid-model/" +exclude = "deps/power-grid-model/" # CI build options [tool.cibuildwheel] diff --git a/setup.py b/setup.py index 1a11294..03927ce 100644 --- a/setup.py +++ b/setup.py @@ -159,7 +159,8 @@ def generate_build_ext(pkg_dir: Path, pkg_name: str): pgm_io_c = Path("power_grid_model_io_native_c") pgm = Path("power_grid_model") pgm_c = Path("power_grid_model_c") - pgm_pkg_dir = pkg_dir / str(pgm).replace("_", "-") + deps = Path("deps") + pgm_pkg_dir = pkg_dir / deps / str(pgm).replace("_", "-") # include-folders include_dirs = [ diff --git a/sonar-project.properties b/sonar-project.properties index 54a12f8..3f3d9f8 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -20,6 +20,7 @@ sonar.sourceEncoding=UTF-8 sonar.issue.ignore.allfile=a1 sonar.issue.ignore.allfile.a1.fileRegexp='.*#include.*doctest\.h[>"].*' sonar.coverage.exclusions="tests/**/*" +sonar.coverage.exclusions=power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp,power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp sonar.cpd.exclusions="tests/**/*" sonar.cfamily.threads=1 sonar.coverageReportPaths=cpp_coverage.xml diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3d9b65b..3840887 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -7,3 +7,4 @@ find_package(doctest REQUIRED) include("${doctest_DIR}/doctest.cmake") add_subdirectory("c_api_tests") +add_subdirectory("cpp_unit_tests") diff --git a/tests/cpp_unit_tests/CMakeLists.txt b/tests/cpp_unit_tests/CMakeLists.txt new file mode 100644 index 0000000..167b33f --- /dev/null +++ b/tests/cpp_unit_tests/CMakeLists.txt @@ -0,0 +1,20 @@ +# SPDX-FileCopyrightText: Contributors to the Power Grid Model project +# +# SPDX-License-Identifier: MPL-2.0 + +set(PROJECT_SOURCES + "test_pgm_vnf_converter.cpp" + "test_entry_point.cpp" +) + +add_executable(power_grid_model_io_native_unit_tests ${PROJECT_SOURCES}) + +target_link_libraries(power_grid_model_io_native_unit_tests + PRIVATE + power_grid_model + power_grid_model_io_native + doctest::doctest + nlohmann_json nlohmann_json::nlohmann_json +) + +doctest_discover_tests(power_grid_model_io_native_unit_tests) diff --git a/tests/cpp_unit_tests/test_entry_point.cpp b/tests/cpp_unit_tests/test_entry_point.cpp new file mode 100644 index 0000000..020ee88 --- /dev/null +++ b/tests/cpp_unit_tests/test_entry_point.cpp @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +// main cpp file + +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN + +#include diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp new file mode 100644 index 0000000..21f12ce --- /dev/null +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#include + +#include + +namespace power_grid_model_io_native { + +TEST_CASE("Test converter constructor") { CHECK_THROWS_AS(PgmVnfConverter(), power_grid_model::ExperimentalFeature); }; + +} // namespace power_grid_model_io_native