diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4c329051deea92b7501df80cfdc3a84d4a04865d..d4221e3ad2aa24bc5b0d0a82c389503c663358f1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -162,6 +162,7 @@ if(PUGS_HAS_HDF5) list(APPEND checkpointing_TESTS test_checkpointing_Array.cpp test_checkpointing_HFTypes.cpp + test_checkpointing_ItemArray.cpp test_checkpointing_ItemValue.cpp test_checkpointing_OStream.cpp test_checkpointing_IBoundaryDescriptor.cpp diff --git a/tests/test_checkpointing_ItemArray.cpp b/tests/test_checkpointing_ItemArray.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2a844e0333cab64adde0d6c48a197ac3d4032cb --- /dev/null +++ b/tests/test_checkpointing_ItemArray.cpp @@ -0,0 +1,82 @@ +#include <catch2/catch_test_macros.hpp> +#include <catch2/matchers/catch_matchers_all.hpp> + +#include <utils/Messenger.hpp> + +#include <language/utils/DataHandler.hpp> +#include <language/utils/EmbeddedData.hpp> +#include <mesh/Mesh.hpp> +#include <mesh/MeshVariant.hpp> +#include <utils/checkpointing/ReadItemArray.hpp> +#include <utils/checkpointing/WriteItemArray.hpp> + +#include <MeshDataBaseForTests.hpp> + +#include <filesystem> + +// clazy:excludeall=non-pod-global-static + +TEST_CASE("checkpointing_ItemArray", "[utils/checkpointing]") +{ + std::string tmp_dirname; + { + { + if (parallel::rank() == 0) { + tmp_dirname = [&]() -> std::string { + std::string temp_filename = std::filesystem::temp_directory_path() / "pugs_checkpointing_XXXXXX"; + return std::string{mkdtemp(&temp_filename[0])}; + }(); + } + parallel::broadcast(tmp_dirname, 0); + } + std::filesystem::path path = tmp_dirname; + const std::string filename = path / "checkpoint.h5"; + + HighFive::FileAccessProps fapl; + fapl.add(HighFive::MPIOFileAccess{MPI_COMM_WORLD, MPI_INFO_NULL}); + fapl.add(HighFive::MPIOCollectiveMetadata{}); + HighFive::File file = HighFive::File(filename, HighFive::File::Truncate, fapl); + + SECTION("ItemArray") + { + HighFive::Group checkpoint_group = file.createGroup("checkpoint_group"); + HighFive::Group useless_group; + + auto mesh_2d = MeshDataBaseForTests::get().hybrid2DMesh()->get<Mesh<2>>(); + + CellArray<double> item_array{mesh_2d->connectivity(), 3}; + item_array.fill(0); + + for (CellId cell_id = 0; cell_id < mesh_2d->numberOfCells(); ++cell_id) { + for (size_t i = 0; i < 3; ++i) { + item_array[cell_id][i] = (1. * std::rand()) / (1. / 3 * RAND_MAX); + } + } + + checkpointing::write(checkpoint_group, "item_array", item_array); + + file.flush(); + + auto is_same = [](const auto& a, const auto& b) { + bool same = true; + for (size_t i = 0; i < a.numberOfRows(); ++i) { + for (size_t j = 0; j < a.numberOfColumns(); ++j) { + if (a[i][j] != b[i][j]) { + same = false; + } + } + } + return parallel::allReduceAnd(same); + }; + + CellArray<double> read_item_array = + checkpointing::readItemArray<double, ItemType::cell>(checkpoint_group, "item_array", mesh_2d->connectivity()); + REQUIRE(is_same(item_array.tableView(), read_item_array.tableView())); + } + } + + parallel::barrier(); + if (parallel::rank() == 0) { + std::filesystem::remove_all(std::filesystem::path{tmp_dirname}); + } +}