From 7b24b258921d21dddad08efbf682c9fcee342a59 Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Tue, 24 Nov 2026 15:09:38 +0100 Subject: [PATCH 1/8] test: refactor DataBlock to be able to make unit tests for Column and make a first example (still not finished) --- src/CMakeLists.txt | 3 + src/dataBlock/dataBlock.cpp | 104 ++++++++++++++----------- src/dataBlock/dataBlock.hpp | 5 +- src/gtest-idfx.hpp | 20 +++++ src/tests/testUnitTestApp.cpp | 42 ++++++++++ src/unitTestApp.cpp | 111 +++++++++++++++++++++++++++ src/unitTestApp.hpp | 69 +++++++++++++++++ src/utils/CMakeLists.txt | 2 + src/utils/arrayHelpers.hpp | 74 ++++++++++++++++++ src/utils/tests/testArrayHelpers.cpp | 63 +++++++++++++++ src/utils/tests/testColumn.cpp | 48 ++++++++++++ 11 files changed, 495 insertions(+), 46 deletions(-) create mode 100644 src/tests/testUnitTestApp.cpp create mode 100644 src/unitTestApp.cpp create mode 100644 src/unitTestApp.hpp create mode 100644 src/utils/arrayHelpers.hpp create mode 100644 src/utils/tests/testArrayHelpers.cpp create mode 100644 src/utils/tests/testColumn.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c801a06c..599ea72b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,6 +47,8 @@ target_sources(idefix-core PRIVATE ${CMAKE_CURRENT_LIST_DIR}/setup.cpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/units.hpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/units.cpp + PRIVATE ${CMAKE_CURRENT_LIST_DIR}/unitTestApp.hpp + PRIVATE ${CMAKE_CURRENT_LIST_DIR}/unitTestApp.cpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/timeIntegrator.hpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/timeIntegrator.cpp ) @@ -56,6 +58,7 @@ if (Idefix_GTEST) target_sources(unittests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testKokkos.cpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testArrays.cpp + PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testUnitTestApp.cpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/gtest-idfx.cpp ) if (Idefix_MPI) diff --git a/src/dataBlock/dataBlock.cpp b/src/dataBlock/dataBlock.cpp index 3440faca..2e3060dc 100644 --- a/src/dataBlock/dataBlock.cpp +++ b/src/dataBlock/dataBlock.cpp @@ -29,7 +29,7 @@ #include "xdmf.hpp" #endif -DataBlock::DataBlock(Grid &grid, Input &input) { +DataBlock::DataBlock(Grid &grid, Input &input, bool init_phys) { idfx::pushRegion("DataBlock::DataBlock"); this->mygrid=&grid; @@ -121,50 +121,10 @@ DataBlock::DataBlock(Grid &grid, Input &input) { this->states["current"] = StateContainer(); - // Initialize the Dump object - this->dump = std::make_unique(input, this); - - // Initialize the VTK object - this->vtk = std::make_unique(input, this, "data"); - - // Init XDMF objects for HDF5 outputs - #ifdef WITH_HDF5 - this->xdmf= std::make_unique(input,this); - #endif - - - // Initialize the hydro object attached to this datablock - this->hydro = std::make_unique>(grid, input, this); - - // Initialise Fargo if needed - if(input.CheckBlock("Fargo")) { - this->fargo = std::make_unique(input, DefaultPhysics::nvar, this); - this->haveFargo = true; - } - - // initialise planets if needed - if(input.CheckBlock("Planet")) { - this->planetarySystem = std::make_unique(input, this); - this->haveplanetarySystem = true; - } - - // Initialise gravity if needed (automatically if planets are present) - if(input.CheckBlock("Gravity") || haveplanetarySystem) { - this->gravity = std::make_unique(input, this); - this->haveGravity = true; - } - - // Initialise dust grains if needed - if(input.CheckBlock("Dust")) { - haveDust = true; - int nSpecies = input.Get("Dust","nSpecies",0); - for(int i = 0 ; i < nSpecies ; i++) { - dust.emplace_back(std::make_unique>(grid, input, this, i)); - } - } - // Register variables that need to be saved in case of restart dump - dump->RegisterVariable(&t, "time"); - dump->RegisterVariable(&dt, "dt"); + // Ini storage & physics + this->InitStorage(input); + if (init_phys) + this->InitPhysics(grid, input); idfx::popRegion(); } @@ -373,6 +333,60 @@ real DataBlock::ComputeTimestep() { return(dt); } +/** + * @brief Initialise the IO storage part of the datablock. + * + * Initialise the IO storage part of the datablock. + */ +void DataBlock::InitStorage(Input & input) { + // Initialize the Dump object + this->dump = std::make_unique(input, this); + + // Initialize the VTK object + this->vtk = std::make_unique(input, this, "data"); + + // Init XDMF objects for HDF5 outputs + #ifdef WITH_HDF5 + this->xdmf= std::make_unique(input,this); + #endif + + // Register variables that need to be saved in case of restart dump + dump->RegisterVariable(&t, "time"); + dump->RegisterVariable(&dt, "dt"); +} + +void DataBlock::InitPhysics(Grid & grid, Input & input) { + // Initialize the hydro object attached to this datablock + this->hydro = std::make_unique>(grid, input, this); + + // Initialise Fargo if needed + if(input.CheckBlock("Fargo")) { + this->fargo = std::make_unique(input, DefaultPhysics::nvar, this); + this->haveFargo = true; + } + + // initialise planets if needed + if(input.CheckBlock("Planet")) { + this->planetarySystem = std::make_unique(input, this); + this->haveplanetarySystem = true; + } + + // Initialise gravity if needed (automatically if planets are present) + if(input.CheckBlock("Gravity") || haveplanetarySystem) { + this->gravity = std::make_unique(input, this); + this->haveGravity = true; + } + + // Initialise dust grains if needed + if(input.CheckBlock("Dust")) { + haveDust = true; + int nSpecies = input.Get("Dust","nSpecies",0); + for(int i = 0 ; i < nSpecies ; i++) { + dust.emplace_back(std::make_unique>(grid, input, this, i)); + } + } +} + // Recompute magnetic fields from vector potential in dedicated fluids void DataBlock::DeriveVectorPotential() { if constexpr(DefaultPhysics::mhd) { diff --git a/src/dataBlock/dataBlock.hpp b/src/dataBlock/dataBlock.hpp index 6d322e7e..08c3c7d4 100644 --- a/src/dataBlock/dataBlock.hpp +++ b/src/dataBlock/dataBlock.hpp @@ -132,9 +132,12 @@ class DataBlock { #endif - DataBlock(Grid &, Input &); ///< init from a Grid object + DataBlock(Grid &, Input &, bool init_phys = true); ///< init from a Grid object explicit DataBlock(SubGrid *); ///< init a minimal datablock for a subgrid + void InitStorage(Input &); ///< initialise the IO storage part of the datablock + void InitPhysics(Grid &, Input &); ///< initialise the physics part of the datablock + void ExtractSubdomain(); ///< initialise datablock sub-domain according to domain decomp. void MakeGeometry(); ///< Compute geometrical terms void DumpToFile(std::string); ///< Dump current datablock to a file for inspection diff --git a/src/gtest-idfx.hpp b/src/gtest-idfx.hpp index dbf09fed..489dd611 100644 --- a/src/gtest-idfx.hpp +++ b/src/gtest-idfx.hpp @@ -80,6 +80,7 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_1D_ARRAYS(array, matcher, array2) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -107,6 +108,7 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_1D_ARRAYS_ARGS(array, that, array2, ...) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -159,6 +161,8 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_2D_ARRAYS(array, matcher, array2) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ + ASSERT_EQ(array.extent(1), array2.extent(1)) << " Array dimension 1"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -189,6 +193,8 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_2D_ARRAYS_ARGS(array, matcher, array2, ...) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ + ASSERT_EQ(array.extent(1), array2.extent(1)) << " Array dimension 1"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -245,6 +251,9 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_3D_ARRAYS(array, matcher, array2) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ + ASSERT_EQ(array.extent(1), array2.extent(1)) << " Array dimension 1"; \ + ASSERT_EQ(array.extent(3), array2.extent(3)) << " Array dimension 3"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -277,6 +286,9 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_3D_ARRAYS_ARGS(array, matcher, array2, ...) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ + ASSERT_EQ(array.extent(1), array2.extent(1)) << " Array dimension 1"; \ + ASSERT_EQ(array.extent(3), array2.extent(3)) << " Array dimension 3"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -337,6 +349,10 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_4D_ARRAYS(array, matcher, array2) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ + ASSERT_EQ(array.extent(1), array2.extent(1)) << " Array dimension 1"; \ + ASSERT_EQ(array.extent(3), array2.extent(3)) << " Array dimension 3"; \ + ASSERT_EQ(array.extent(4), array2.extent(4)) << " Array dimension 4"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ @@ -371,6 +387,10 @@ std::string idfx_get_data_file_path(const std::string & sourceFile, const std::s #define ASSERT_THAT_4D_ARRAYS_ARGS(array, matcher, array2, ...) \ do { \ Kokkos::fence(); \ + ASSERT_EQ(array.extent(0), array2.extent(0)) << " Array dimension 0"; \ + ASSERT_EQ(array.extent(1), array2.extent(1)) << " Array dimension 1"; \ + ASSERT_EQ(array.extent(3), array2.extent(3)) << " Array dimension 3"; \ + ASSERT_EQ(array.extent(4), array2.extent(4)) << " Array dimension 4"; \ auto array##_host = Kokkos::create_mirror_view(array); \ auto array2##_host = Kokkos::create_mirror_view(array2); \ Kokkos::deep_copy(array##_host, array); \ diff --git a/src/tests/testUnitTestApp.cpp b/src/tests/testUnitTestApp.cpp new file mode 100644 index 00000000..c4d57d5d --- /dev/null +++ b/src/tests/testUnitTestApp.cpp @@ -0,0 +1,42 @@ +// *********************************************************************************** +// Idefix MHD astrophysical code +// +// Source file src/tests/testUnitTestApp.hpp +// +// Last modified : 10/2026 +// +// Copyright(C) by : +// - Sébastien Valat (IPAG/UGA/CNRS - 2026) +// and other code contributors +// +// Licensed under CeCILL 2.1 License, see COPYING for more information +// *********************************************************************************** + +// headers +#include "idefix.hpp" +#include "arrays.hpp" +#include "gtest-idfx.hpp" +#include "unitTestApp.hpp" + +TEST(UnitTestApp, constructor) { + //ini idefix app + idfx::UnitTestApp app(8,8,8); + + //extract one pointer for use + DataBlock & data = *app.dataBlock; + + //checks about mesh with ghost cells + ASSERT_EQ(data.np_tot[IDIR], 12); + ASSERT_EQ(data.np_tot[JDIR], 12); + ASSERT_EQ(data.np_tot[KDIR], 12); + + //checks about internal mesh + ASSERT_EQ(data.np_int[IDIR], 8); + ASSERT_EQ(data.np_int[JDIR], 8); + ASSERT_EQ(data.np_int[KDIR], 8); + + //checks about cell size + ASSERT_EQ(data.dx[IDIR][0], 1.0); + ASSERT_EQ(data.dx[JDIR][0], 1.0); + ASSERT_EQ(data.dx[KDIR][0], 1.0); +} diff --git a/src/unitTestApp.cpp b/src/unitTestApp.cpp new file mode 100644 index 00000000..7dc633ed --- /dev/null +++ b/src/unitTestApp.cpp @@ -0,0 +1,111 @@ +// *********************************************************************************** +// Idefix MHD astrophysical code +// +// Source file src/unitTestApp.cpp +// +// Last modified : 11/2026 +// +// Copyright(C) by : +// - Sébastien Valat (IPAG/UGA/CNRS - 2026) +// and other code contributors +// +// Licensed under CeCILL 2.1 License, see COPYING for more information +// *********************************************************************************** + +#include "unitTestApp.hpp" +//unix +#include +//std c +#include +//std c++ +#include +#include +#include +#include +//idefix +#include "units.hpp" +#include "fluid.hpp" + +namespace idfx { + +TempConfigFile::TempConfigFile(const std::list & lines) { + //tmp name + char tmpname[] = "/tmp/idefix-ut-config-XXXXXXX"; + + //open + this->fp = fdopen(mkstemp(tmpname), "w"); + this->fname = tmpname; + + //write + for (const auto & it : lines) { + fputs(it.c_str(), this->fp); + fputs("\n", this->fp); + } + + //flush + fflush(this->fp); +} + +TempConfigFile::~TempConfigFile(void) { + fclose(this->fp); + unlink(this->fname.c_str()); +} + +const std::string & TempConfigFile::getFilePath(void) const { + return this->fname; +} + +UnitTestApp::UnitTestApp(size_t s1, size_t s2, size_t s3, float cell_size) { + //init input and components + this->InitInput(s1, s2, s3, cell_size); + this->InitComponents(); +} + +void UnitTestApp::InitInput(size_t s1, size_t s2, size_t s3, float cell_size) { + //create config file + TempConfigFile configFile({ + "[Grid]", + std::format("X1-grid 1 0.0 {} u {}", s1, static_cast(s1)*cell_size), + std::format("X2-grid 1 0.0 {} u {}", s2, static_cast(s2)*cell_size), + std::format("X3-grid 1 0.0 {} u {}", s3, static_cast(s3)*cell_size), + "", + "[Boundary]", + "X1-beg periodic", + "X1-end periodic", + "X2-beg periodic", + "X2-end periodic", + "X3-beg periodic", + "X3-end periodic", + }); + + //build args + char* argc[] = {strdup("idefix"), strdup("-i"), strdup(configFile.getFilePath().c_str()), nullptr}; + + //load it as Input object + this->input = std::make_shared(3, argc); + + //free args + free(argc[0]); + free(argc[1]); + free(argc[2]); +} + +void UnitTestApp::InitComponents() { + //ini components + idfx::units.Init(*input); + + // Allocate the grid on device + this->grid = std::make_shared(*input); + + // Allocate the grid image on host + this->gridHost = std::make_shared(*grid); + + // Actually make the grid on host and sync it on the device + this->gridHost->MakeGrid(*input); + this->gridHost->SyncToDevice(); + + // instantiate required objects. + this->dataBlock = std::make_shared(*grid, *input, false); +} + +} // namespace idfx diff --git a/src/unitTestApp.hpp b/src/unitTestApp.hpp new file mode 100644 index 00000000..97f31d1a --- /dev/null +++ b/src/unitTestApp.hpp @@ -0,0 +1,69 @@ +// *********************************************************************************** +// Idefix MHD astrophysical code +// +// Source file src/unitTestApp.hpp +// +// Last modified : 11/2026 +// +// Copyright(C) by : +// - Sébastien Valat (IPAG/UGA/CNRS - 2026) +// and other code contributors +// +// Licensed under CeCILL 2.1 License, see COPYING for more information +// *********************************************************************************** + +#ifndef UNITTESTAPP_HPP_ +#define UNITTESTAPP_HPP_ + +//std c +#include +//std c++ +#include +#include +#include +//idefix +#include "idefix.hpp" +#include "dataBlock.hpp" +#include "grid.hpp" +#include "gridHost.hpp" + +namespace idfx { + +/** + * @brief Create a temporary text file. + * + * Create a temporary texte file, store the given data in, and delete + * it when destroyed. This is used for unit tests. + */ +class TempConfigFile { + public: + explicit TempConfigFile(const std::list & lines); + ~TempConfigFile(void); + const std::string & getFilePath(void) const; + private: + std::FILE * fp{nullptr}; + std::string fname; +}; + +/** + * @brief Handle all the components of Idefix for unit tests. + * + * Construct all the Idefix components except physics to perform + * unit tests on the Idefix classes. + */ +class UnitTestApp { + public: + UnitTestApp(size_t s1, size_t s2, size_t s3, float cell_size = 1.0); + public: + std::shared_ptr input; + std::shared_ptr grid; + std::shared_ptr gridHost; + std::shared_ptr dataBlock; + private: + void InitInput(size_t s1, size_t s2, size_t s3, float cell_size); + void InitComponents(void); +}; + +} // namespace idfx + +#endif //UNITTESTAPP_HPP_ diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index f5276557..ce8775ba 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -26,5 +26,7 @@ target_sources(idefix-core if (Idefix_GTEST) target_sources(unittests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testLookupTable.cpp + PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testColumn.cpp + PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testArrayHelpers.cpp ) endif() diff --git a/src/utils/arrayHelpers.hpp b/src/utils/arrayHelpers.hpp new file mode 100644 index 00000000..3befee03 --- /dev/null +++ b/src/utils/arrayHelpers.hpp @@ -0,0 +1,74 @@ +// *********************************************************************************** +// Idefix MHD astrophysical code +// +// Source file src/utils/tests/testColumn.hpp +// +// Last modified : 10/2026 +// +// Copyright(C) by : +// - Sébastien Valat (IPAG/UGA/CNRS - 2026) +// and other code contributors +// +// Licensed under CeCILL 2.1 License, see COPYING for more information +// *********************************************************************************** + +#ifndef UTILS_ARRAYHELPERS_HPP_ +#define UTILS_ARRAYHELPERS_HPP_ + +// headers +#include +#include "idefix.hpp" +#include "arrays.hpp" +#include "gtest-idfx.hpp" +#include "unitTestApp.hpp" +#include "column.hpp" + +/** + * @brief Uniformaly fill the given array with the given value. + * + * Uniformaly fill the given array with the given value. + * + * @param array The array to fill, being a 1D, 2D, 3D or 3D Kokkos array. + * @param value The value to assign to each cells. + */ +template +void ArrayUniformFill(T & array, const U value) { + if constexpr (array.rank() == 1) { + idefix_for("ArrayUniformFill.1D", 0, array.extent(0), + KOKKOS_LAMBDA(int i) { + array(i) = value; + } + ); + } else if constexpr (array.rank() == 2) { + idefix_for("ArrayUniformFill.2D", + 0, array.extent(0), + 0, array.extent(1), + KOKKOS_LAMBDA(int j, int i) { + array(j, i) = value; + } + ); + } else if constexpr (array.rank() == 3) { + idefix_for("ArrayUniformFill.3D", + 0, array.extent(0), + 0, array.extent(1), + 0, array.extent(2), + KOKKOS_LAMBDA(int k, int j, int i) { + array(k, j, i) = value; + } + ); + } else if constexpr (array.rank() == 4) { + idefix_for("ArrayUniformFill.3D", + 0, array.extent(0), + 0, array.extent(1), + 0, array.extent(2), + 0, array.extent(3), + KOKKOS_LAMBDA(int l, int k, int j, int i) { + array(l, k, j, i) = value; + } + ); + } else { + IDEFIX_ERROR("Unsupported number of dimensions !"); + } +} + +#endif //UTILS_ARRAYHELPERS_HPP_ diff --git a/src/utils/tests/testArrayHelpers.cpp b/src/utils/tests/testArrayHelpers.cpp new file mode 100644 index 00000000..3d0aa4e8 --- /dev/null +++ b/src/utils/tests/testArrayHelpers.cpp @@ -0,0 +1,63 @@ +// *********************************************************************************** +// Idefix MHD astrophysical code +// +// Source file src/utils/tests/testArrayHelpers.hpp +// +// Last modified : 10/2026 +// +// Copyright(C) by : +// - Sébastien Valat (IPAG/UGA/CNRS - 2026) +// and other code contributors +// +// Licensed under CeCILL 2.1 License, see COPYING for more information +// *********************************************************************************** + +// headers +#include "idefix.hpp" +#include "arrays.hpp" +#include "arrayHelpers.hpp" +#include "gtest-idfx.hpp" + +TEST(ArrayHelpers, ArrayUniformFill_1D) { + //array + IdefixArray1D array("array", 10); + + //fill with uniform + ArrayUniformFill(array, 5.0); + + //check + ASSERT_THAT_1D_ARRAY(array, testing::Eq, 5.0); +} + +TEST(ArrayHelpers, ArrayUniformFill_2D) { + //array + IdefixArray2D array("array", 10, 10); + + //fill with uniform + ArrayUniformFill(array, 5.0); + + //check + ASSERT_THAT_2D_ARRAY(array, testing::Eq, 5.0); +} + +TEST(ArrayHelpers, ArrayUniformFill_3D) { + //array + IdefixArray3D array("array", 10, 10, 10); + + //fill with uniform + ArrayUniformFill(array, 5.0); + + //check + ASSERT_THAT_3D_ARRAY(array, testing::Eq, 5.0); +} + +TEST(ArrayHelpers, ArrayUniformFill_4D) { + //array + IdefixArray4D array("array", 10, 10, 10, 10); + + //fill with uniform + ArrayUniformFill(array, 5.0); + + //check + ASSERT_THAT_4D_ARRAY(array, testing::Eq, 5.0); +} diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp new file mode 100644 index 00000000..0435f0d2 --- /dev/null +++ b/src/utils/tests/testColumn.cpp @@ -0,0 +1,48 @@ +// *********************************************************************************** +// Idefix MHD astrophysical code +// +// Source file src/utils/tests/testColumn.hpp +// +// Last modified : 10/2026 +// +// Copyright(C) by : +// - Sébastien Valat (IPAG/UGA/CNRS - 2026) +// and other code contributors +// +// Licensed under CeCILL 2.1 License, see COPYING for more information +// *********************************************************************************** + +// headers +#include "idefix.hpp" +#include "arrays.hpp" +#include "gtest-idfx.hpp" +#include "unitTestApp.hpp" +#include "column.hpp" +#include "arrayHelpers.hpp" + +TEST(UtIdefixEnv, ComputeColumn_X1_Right) { + //ini idefix app + idfx::UnitTestApp app(8, 8, 8, 1.0); + + //extract one pointer for use + DataBlock & data = *app.dataBlock; + + //spawn an array + const int MYVAR = 0; + const int VARS = 1; + IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //fill + ArrayUniformFill(vars, 1.0); + + //build expected array + IdefixArray3D expected = IdefixArray3D("expected", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //create column object & use + Column column(IDIR, -1, &data); + column.ComputeColumn(vars, MYVAR); + IdefixArray3D result = column.GetColumn(); + + //check + ASSERT_THAT_3D_ARRAYS(result, testing::Eq, expected); +} From 0216502fe245e057a62a8442b903382da6073fa4 Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Tue, 24 Nov 2026 17:57:01 +0100 Subject: [PATCH 2/8] test: implement unit test for Column class (in single MPI rank mode) --- src/tests/testUnitTestApp.cpp | 5 - src/unitTestApp.cpp | 6 +- src/utils/tests/testColumn.cpp | 182 +++++++++++++++++++++++++++++---- 3 files changed, 165 insertions(+), 28 deletions(-) diff --git a/src/tests/testUnitTestApp.cpp b/src/tests/testUnitTestApp.cpp index c4d57d5d..66b7f6ee 100644 --- a/src/tests/testUnitTestApp.cpp +++ b/src/tests/testUnitTestApp.cpp @@ -34,9 +34,4 @@ TEST(UnitTestApp, constructor) { ASSERT_EQ(data.np_int[IDIR], 8); ASSERT_EQ(data.np_int[JDIR], 8); ASSERT_EQ(data.np_int[KDIR], 8); - - //checks about cell size - ASSERT_EQ(data.dx[IDIR][0], 1.0); - ASSERT_EQ(data.dx[JDIR][0], 1.0); - ASSERT_EQ(data.dx[KDIR][0], 1.0); } diff --git a/src/unitTestApp.cpp b/src/unitTestApp.cpp index 7dc633ed..aa7e5b79 100644 --- a/src/unitTestApp.cpp +++ b/src/unitTestApp.cpp @@ -65,9 +65,9 @@ void UnitTestApp::InitInput(size_t s1, size_t s2, size_t s3, float cell_size) { //create config file TempConfigFile configFile({ "[Grid]", - std::format("X1-grid 1 0.0 {} u {}", s1, static_cast(s1)*cell_size), - std::format("X2-grid 1 0.0 {} u {}", s2, static_cast(s2)*cell_size), - std::format("X3-grid 1 0.0 {} u {}", s3, static_cast(s3)*cell_size), + std::format("X1-grid 1 1.0 {} u {}", s1, 1 + static_cast(s1)*cell_size), //R + std::format("X2-grid 1 0.0 {} u 3.1415926", s2, static_cast(s2)*cell_size), //teta + std::format("X3-grid 1 0.0 {} u {}", s3, 2 * 3.1415926), //phi "", "[Boundary]", "X1-beg periodic", diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp index 0435f0d2..e88c3e33 100644 --- a/src/utils/tests/testColumn.cpp +++ b/src/utils/tests/testColumn.cpp @@ -20,29 +20,171 @@ #include "column.hpp" #include "arrayHelpers.hpp" -TEST(UtIdefixEnv, ComputeColumn_X1_Right) { - //ini idefix app - idfx::UnitTestApp app(8, 8, 8, 1.0); +// only applies when being 3D + Spherical +#if DIMENSIONS == 3 && GEOMETRY == SPHERICAL + // The test fill a 3D var with constant value and applies + // Colum on it. Along theta it should have constant increase + TEST(Column, ComputeColumn_4D_const_increase_along_theta) { + //ini idefix app + idfx::UnitTestApp app(8, 8, 8, 1.0); - //extract one pointer for use - DataBlock & data = *app.dataBlock; + //extract one pointer for use + DataBlock & data = *app.dataBlock; - //spawn an array - const int MYVAR = 0; - const int VARS = 1; - IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + //spawn an array + const int MYVAR = 0; + const int VARS = 1; + IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); - //fill - ArrayUniformFill(vars, 1.0); + //fill with const values + ArrayUniformFill(vars, 3.0); - //build expected array - IdefixArray3D expected = IdefixArray3D("expected", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + //create column object & use + Column column(JDIR, 1, &data);//teta + column.ComputeColumn(vars, MYVAR); + IdefixArray3D result = column.GetColumn(); - //create column object & use - Column column(IDIR, -1, &data); - column.ComputeColumn(vars, MYVAR); - IdefixArray3D result = column.GetColumn(); + //copy to ease checking + IdefixHostArray3D tmp("result_host", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + Kokkos::deep_copy(tmp, result); - //check - ASSERT_THAT_3D_ARRAYS(result, testing::Eq, expected); -} + //should be const increase along theta except having 0 on ghosts + for (int i = 2 ; i < data.np_tot[IDIR] - 2 ; i++) { + for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) { + double prev = 0.0; + for (int j = 0 ; j < data.np_tot[JDIR] ; j++) { + if (j < 2 || j >= 10) { + ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; + } else if (j == 2) { + prev = tmp(k,j,i); + ASSERT_NE(0, tmp(k,j,i)) << "First non ghost"; + } else { + ASSERT_DOUBLE_EQ(prev * (j-1), tmp(k,j,i)) << "Shound have constant increase along theta"; + } + } + } + } + } + + // The test fill a 3D var with constant value and applies + // Colum on it. Along phi it should have constant increase + TEST(Column, ComputeColumn_4D_const_increase_along_phi) { + //ini idefix app + idfx::UnitTestApp app(8, 8, 8, 1.0); + + //extract one pointer for use + DataBlock & data = *app.dataBlock; + + //spawn an array + const int MYVAR = 0; + const int VARS = 1; + IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //fill with const values + ArrayUniformFill(vars, 3.0); + + //create column object & use + Column column(KDIR, 1, &data);//phi + column.ComputeColumn(vars, MYVAR); + IdefixArray3D result = column.GetColumn(); + + //copy on host to ease comare + IdefixHostArray3D tmp("result_host", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + Kokkos::deep_copy(tmp, result); + + //should be const increase along phi except having 0 on ghost + for (int i = 2 ; i < data.np_tot[IDIR] - 2 ; i++) { + for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) { + double prev = 0.0; + for (int k = 0 ; k < data.np_tot[KDIR] ; k++) { + if (k < 2 || k >= 10) { + ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; + } else if (k == 2) { + prev = tmp(k,j,i); + ASSERT_NE(0, tmp(k,j,i)) << "First non ghost"; + } else { + ASSERT_DOUBLE_EQ(prev * (k-1), tmp(k,j,i)) << "Shound have constant increase along phi"; + } + } + } + } + } + + // The test fill a 3D var with constant value and applies + // Colum on it. Along R it should have the pre-defined values + // + being constant over theta & phi + TEST(Column, ComputeColumn_4D_along_r_and_const_along_theta_phi) { + //ini idefix app + idfx::UnitTestApp app(8, 8, 8, 1.0); + + //extract one pointer for use + DataBlock & data = *app.dataBlock; + + //spawn an array + const int MYVAR = 0; + const int VARS = 1; + IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //fill with const values + ArrayUniformFill(vars, 3.0); + + //create column object & use + Column column(IDIR, 1, &data);//r + column.ComputeColumn(vars, MYVAR); + IdefixArray3D result = column.GetColumn(); + + //copy on host to each checking + IdefixHostArray3D tmp("result_host", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + Kokkos::deep_copy(tmp, result); + + //should increase along R and be constant over phi/theta + double expected_along_r[] = { + 0, 0, + 2.8, 5.7230769230769241, 8.6830769230769249, 11.658686679174485, + 14.642293236551534, 17.630528530669181, 20.621678973147059, 23.61478242142292, + 0, 0 + }; + for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) + for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) + for (int i = 0 ; i < data.np_tot[IDIR] ; i++) + ASSERT_DOUBLE_EQ(expected_along_r[i], tmp(k,j,i)) << "Shound have constant along phi & theta"; + } + + // The test fill a 3D var with constant value and applies + // Colum on it. Along R it should have the pre-defined values + // + being constant over theta & phi + TEST(Column, ComputeColumn_3D_along_r_and_const_along_theta_phi) { + //ini idefix app + idfx::UnitTestApp app(8, 8, 8, 1.0); + + //extract one pointer for use + DataBlock & data = *app.dataBlock; + + //spawn an array + IdefixArray3D var = IdefixArray3D("myvar", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //fill with const values + ArrayUniformFill(var, 3.0); + + //create column object & use + Column column(IDIR, 1, &data);//r + column.ComputeColumn(var); + IdefixArray3D result = column.GetColumn(); + + //copy on host to each checking + IdefixHostArray3D tmp("result_host", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + Kokkos::deep_copy(tmp, result); + + //should increase along R and be constant over phi/theta + double expected_along_r[] = { + 0, 0, + 2.8, 5.7230769230769241, 8.6830769230769249, 11.658686679174485, + 14.642293236551534, 17.630528530669181, 20.621678973147059, 23.61478242142292, + 0, 0 + }; + for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) + for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) + for (int i = 0 ; i < data.np_tot[IDIR] ; i++) + ASSERT_DOUBLE_EQ(expected_along_r[i], tmp(k,j,i)) << "Shound have constant along phi & theta"; + } +#endif //DIMENSION == 3 && GEOMETRY == SPHERICAL From a2b8341edaca424739f9ecdf313b90ba18fa3e21 Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Tue, 24 Nov 2026 18:15:38 +0100 Subject: [PATCH 3/8] test: improve Column test, cover more cases and make safeExit() throwing an exception in case of error for unit tests (not to abort the whole test suite) --- src/global.cpp | 2 +- src/gtest-idfx.cpp | 7 +++++++ src/unitTestApp.cpp | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/global.cpp b/src/global.cpp index 2f449250..85bde2c7 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -174,7 +174,7 @@ real randm(void) { return static_cast(static_cast(q) / static_cast(m)); } -void safeExit(int retCode) { +__attribute__((weak)) void safeExit(int retCode) { if(retCode != 0) { #ifdef WITH_MPI MPI_Abort(MPI_COMM_WORLD,retCode); diff --git a/src/gtest-idfx.cpp b/src/gtest-idfx.cpp index 2390b027..bccab1d8 100644 --- a/src/gtest-idfx.cpp +++ b/src/gtest-idfx.cpp @@ -14,6 +14,7 @@ // includes #include +#include #if __has_include() #include // NOLINT [build/c++17] namespace fs = std::filesystem; @@ -112,6 +113,12 @@ class IdefixEnvironment : public ::testing::Environment { } }; +// Override safeExit to throw an exception instead of exiting. +// So the unit test suite can continue. +void idfx::safeExit(int retCode) { + throw std::runtime_error(std::format("idfx::safeExit: exit with retCode={} !", retCode)); +} + // setup advanded main for tests using MPI / Kokkos int main(int argc, char* argv[]) { bool initKokkosBeforeMPI = false; diff --git a/src/unitTestApp.cpp b/src/unitTestApp.cpp index aa7e5b79..788b0fa6 100644 --- a/src/unitTestApp.cpp +++ b/src/unitTestApp.cpp @@ -65,9 +65,21 @@ void UnitTestApp::InitInput(size_t s1, size_t s2, size_t s3, float cell_size) { //create config file TempConfigFile configFile({ "[Grid]", - std::format("X1-grid 1 1.0 {} u {}", s1, 1 + static_cast(s1)*cell_size), //R - std::format("X2-grid 1 0.0 {} u 3.1415926", s2, static_cast(s2)*cell_size), //teta - std::format("X3-grid 1 0.0 {} u {}", s3, 2 * 3.1415926), //phi + #if GEOMETRY == CARTESIAN + std::format("X1-grid 1 0.0 {} u {}", s1, static_cast(s1)*cell_size), + std::format("X2-grid 1 0.0 {} u {}", s2, static_cast(s2)*cell_size), + std::format("X3-grid 1 0.0 {} u {}", s3, static_cast(s1)*cell_size), + #elif GEOMETRY == SPHERICAL + std::format("X1-grid 1 1.0 {} u {}", s1, 1 + static_cast(s1)*cell_size), //R + std::format("X2-grid 1 0.0 {} u {}", s2, 3.1415926), //theta + std::format("X3-grid 1 0.0 {} u {}", s3, 2 * 3.1415926), //phi + #elif GEOMETRY == POLAR + std::format("X1-grid 1 1.0 {} u {}", s1, 1 + static_cast(s1)*cell_size), //R + std::format("X2-grid 1 0.0 {} u {}", s2, 2* 3.1415926), //theta + #else //GEOMETRY + #warning "Using unsupported geometry for the unit test object, some tests will fail !" + "# UNSUPPORTED GEOMETRY" + #endif //GEOMETRY "", "[Boundary]", "X1-beg periodic", From 85a7327ce7b539b23232f1259491f1888ed068e2 Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Thu, 26 Nov 2026 11:56:24 +0100 Subject: [PATCH 4/8] test: roughly estimate the values expected for the sphical 3D case in column --- src/utils/tests/testColumn.cpp | 82 +++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp index e88c3e33..f99b7385 100644 --- a/src/utils/tests/testColumn.cpp +++ b/src/utils/tests/testColumn.cpp @@ -56,8 +56,16 @@ if (j < 2 || j >= 10) { ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; } else if (j == 2) { + const real r = data.x[0](i); + const real dtheta = 3.1415926/8.0; + //const real dV = r * r * sin(theta) * dr * dtheta * dphi; + //const real avgA = r * sin(theta) * dr * dphi; + //const real expected = 3.0 * dV / avgA; + const real expected = 3.0 * r * dtheta; + ASSERT_NEAR(expected, tmp(k,j,i), 0.15) << "First non ghost with single contribution"; + + //remind the real value to check the constant increase prev = tmp(k,j,i); - ASSERT_NE(0, tmp(k,j,i)) << "First non ghost"; } else { ASSERT_DOUBLE_EQ(prev * (j-1), tmp(k,j,i)) << "Shound have constant increase along theta"; } @@ -100,8 +108,16 @@ if (k < 2 || k >= 10) { ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; } else if (k == 2) { + const real r = data.x[0](i); + const real theta = data.x[1](j); + const real dphi = 2*3.1415926/8.0; + //const real dV = r * r * sin(theta) * dr * dtheta * dphi; + //const real avgA = r * dr * dtheta; + //const real expected = 3.0 * dV / avgA; + const real expected = 3.0 * r * sin(theta) * dphi; + ASSERT_NEAR(expected, tmp(k,j,i), 0.11) << "First non ghost with single contribution"; + //remind the real value to check the constant increase prev = tmp(k,j,i); - ASSERT_NE(0, tmp(k,j,i)) << "First non ghost"; } else { ASSERT_DOUBLE_EQ(prev * (k-1), tmp(k,j,i)) << "Shound have constant increase along phi"; } @@ -138,16 +154,27 @@ Kokkos::deep_copy(tmp, result); //should increase along R and be constant over phi/theta - double expected_along_r[] = { - 0, 0, - 2.8, 5.7230769230769241, 8.6830769230769249, 11.658686679174485, - 14.642293236551534, 17.630528530669181, 20.621678973147059, 23.61478242142292, - 0, 0 - }; - for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) - for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) - for (int i = 0 ; i < data.np_tot[IDIR] ; i++) - ASSERT_DOUBLE_EQ(expected_along_r[i], tmp(k,j,i)) << "Shound have constant along phi & theta"; + for (int i = 0 ; i < data.np_tot[IDIR] ; i++) { + real prev = 0.0; + for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) { + for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) { + if (i < 2 || i >= 10) { + ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; + } else if (prev == 0.0) { + const real dr = 1.0; + //const real dV = r * r * sin(theta) * dr * dtheta * dphi; + //const real avgA = r * r * sin(theta) * dtheta * dphi; + //const real expected = (i-1) * dV / avgA + const real expected = 3.0 * dr * (i-1); + ASSERT_NEAR(expected, tmp(k,j,i), 0.4) << "First non ghost with single contribution"; + //remind the real value to check the constant value over theta phi + prev = tmp(k,j,i); + } else { + ASSERT_DOUBLE_EQ(prev, tmp(k,j,i)) << "Shound have constant increase along theta phi"; + } + } + } + } } // The test fill a 3D var with constant value and applies @@ -176,15 +203,26 @@ Kokkos::deep_copy(tmp, result); //should increase along R and be constant over phi/theta - double expected_along_r[] = { - 0, 0, - 2.8, 5.7230769230769241, 8.6830769230769249, 11.658686679174485, - 14.642293236551534, 17.630528530669181, 20.621678973147059, 23.61478242142292, - 0, 0 - }; - for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) - for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) - for (int i = 0 ; i < data.np_tot[IDIR] ; i++) - ASSERT_DOUBLE_EQ(expected_along_r[i], tmp(k,j,i)) << "Shound have constant along phi & theta"; + for (int i = 0 ; i < data.np_tot[IDIR] ; i++) { + real prev = 0.0; + for (int j = 2 ; j < data.np_tot[JDIR] - 2 ; j++) { + for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) { + if (i < 2 || i >= 10) { + ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; + } else if (prev == 0.0) { + const real dr = 1.0; + //const real dV = r * r * sin(theta) * dr * dtheta * dphi; + //const real avgA = r * r * sin(theta) * dtheta * dphi; + //const real expected = (i-1) * dV / avgA + const real expected = 3.0 * dr * (i-1); + ASSERT_NEAR(expected, tmp(k,j,i), 0.4) << "First non ghost with single contribution"; + //remind the real value to check the constant value over theta phi + prev = tmp(k,j,i); + } else { + ASSERT_DOUBLE_EQ(prev, tmp(k,j,i)) << "Should have constant value over phi / theta"; + } + } + } + } } #endif //DIMENSION == 3 && GEOMETRY == SPHERICAL From 05ecb425a840ad2110f60adba3e7f45b31c05fdc Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Thu, 26 Nov 2026 12:06:22 +0100 Subject: [PATCH 5/8] test: remove unneeded uniformFillArray() which can be done with Kokkos::deep_copy(array, const_scalar_value) --- src/utils/CMakeLists.txt | 1 - src/utils/arrayHelpers.hpp | 74 ---------------------------- src/utils/tests/testArrayHelpers.cpp | 63 ----------------------- src/utils/tests/testColumn.cpp | 9 ++-- 4 files changed, 4 insertions(+), 143 deletions(-) delete mode 100644 src/utils/arrayHelpers.hpp delete mode 100644 src/utils/tests/testArrayHelpers.cpp diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index ce8775ba..0741df89 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -27,6 +27,5 @@ if (Idefix_GTEST) target_sources(unittests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testLookupTable.cpp PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testColumn.cpp - PRIVATE ${CMAKE_CURRENT_LIST_DIR}/tests/testArrayHelpers.cpp ) endif() diff --git a/src/utils/arrayHelpers.hpp b/src/utils/arrayHelpers.hpp deleted file mode 100644 index 3befee03..00000000 --- a/src/utils/arrayHelpers.hpp +++ /dev/null @@ -1,74 +0,0 @@ -// *********************************************************************************** -// Idefix MHD astrophysical code -// -// Source file src/utils/tests/testColumn.hpp -// -// Last modified : 10/2026 -// -// Copyright(C) by : -// - Sébastien Valat (IPAG/UGA/CNRS - 2026) -// and other code contributors -// -// Licensed under CeCILL 2.1 License, see COPYING for more information -// *********************************************************************************** - -#ifndef UTILS_ARRAYHELPERS_HPP_ -#define UTILS_ARRAYHELPERS_HPP_ - -// headers -#include -#include "idefix.hpp" -#include "arrays.hpp" -#include "gtest-idfx.hpp" -#include "unitTestApp.hpp" -#include "column.hpp" - -/** - * @brief Uniformaly fill the given array with the given value. - * - * Uniformaly fill the given array with the given value. - * - * @param array The array to fill, being a 1D, 2D, 3D or 3D Kokkos array. - * @param value The value to assign to each cells. - */ -template -void ArrayUniformFill(T & array, const U value) { - if constexpr (array.rank() == 1) { - idefix_for("ArrayUniformFill.1D", 0, array.extent(0), - KOKKOS_LAMBDA(int i) { - array(i) = value; - } - ); - } else if constexpr (array.rank() == 2) { - idefix_for("ArrayUniformFill.2D", - 0, array.extent(0), - 0, array.extent(1), - KOKKOS_LAMBDA(int j, int i) { - array(j, i) = value; - } - ); - } else if constexpr (array.rank() == 3) { - idefix_for("ArrayUniformFill.3D", - 0, array.extent(0), - 0, array.extent(1), - 0, array.extent(2), - KOKKOS_LAMBDA(int k, int j, int i) { - array(k, j, i) = value; - } - ); - } else if constexpr (array.rank() == 4) { - idefix_for("ArrayUniformFill.3D", - 0, array.extent(0), - 0, array.extent(1), - 0, array.extent(2), - 0, array.extent(3), - KOKKOS_LAMBDA(int l, int k, int j, int i) { - array(l, k, j, i) = value; - } - ); - } else { - IDEFIX_ERROR("Unsupported number of dimensions !"); - } -} - -#endif //UTILS_ARRAYHELPERS_HPP_ diff --git a/src/utils/tests/testArrayHelpers.cpp b/src/utils/tests/testArrayHelpers.cpp deleted file mode 100644 index 3d0aa4e8..00000000 --- a/src/utils/tests/testArrayHelpers.cpp +++ /dev/null @@ -1,63 +0,0 @@ -// *********************************************************************************** -// Idefix MHD astrophysical code -// -// Source file src/utils/tests/testArrayHelpers.hpp -// -// Last modified : 10/2026 -// -// Copyright(C) by : -// - Sébastien Valat (IPAG/UGA/CNRS - 2026) -// and other code contributors -// -// Licensed under CeCILL 2.1 License, see COPYING for more information -// *********************************************************************************** - -// headers -#include "idefix.hpp" -#include "arrays.hpp" -#include "arrayHelpers.hpp" -#include "gtest-idfx.hpp" - -TEST(ArrayHelpers, ArrayUniformFill_1D) { - //array - IdefixArray1D array("array", 10); - - //fill with uniform - ArrayUniformFill(array, 5.0); - - //check - ASSERT_THAT_1D_ARRAY(array, testing::Eq, 5.0); -} - -TEST(ArrayHelpers, ArrayUniformFill_2D) { - //array - IdefixArray2D array("array", 10, 10); - - //fill with uniform - ArrayUniformFill(array, 5.0); - - //check - ASSERT_THAT_2D_ARRAY(array, testing::Eq, 5.0); -} - -TEST(ArrayHelpers, ArrayUniformFill_3D) { - //array - IdefixArray3D array("array", 10, 10, 10); - - //fill with uniform - ArrayUniformFill(array, 5.0); - - //check - ASSERT_THAT_3D_ARRAY(array, testing::Eq, 5.0); -} - -TEST(ArrayHelpers, ArrayUniformFill_4D) { - //array - IdefixArray4D array("array", 10, 10, 10, 10); - - //fill with uniform - ArrayUniformFill(array, 5.0); - - //check - ASSERT_THAT_4D_ARRAY(array, testing::Eq, 5.0); -} diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp index f99b7385..b974694a 100644 --- a/src/utils/tests/testColumn.cpp +++ b/src/utils/tests/testColumn.cpp @@ -18,7 +18,6 @@ #include "gtest-idfx.hpp" #include "unitTestApp.hpp" #include "column.hpp" -#include "arrayHelpers.hpp" // only applies when being 3D + Spherical #if DIMENSIONS == 3 && GEOMETRY == SPHERICAL @@ -37,7 +36,7 @@ IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); //fill with const values - ArrayUniformFill(vars, 3.0); + Kokkos::deep_copy(vars, 3.0); //create column object & use Column column(JDIR, 1, &data);//teta @@ -89,7 +88,7 @@ IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); //fill with const values - ArrayUniformFill(vars, 3.0); + Kokkos::deep_copy(vars, 3.0); //create column object & use Column column(KDIR, 1, &data);//phi @@ -142,7 +141,7 @@ IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); //fill with const values - ArrayUniformFill(vars, 3.0); + Kokkos::deep_copy(vars, 3.0); //create column object & use Column column(IDIR, 1, &data);//r @@ -191,7 +190,7 @@ IdefixArray3D var = IdefixArray3D("myvar", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); //fill with const values - ArrayUniformFill(var, 3.0); + Kokkos::deep_copy(var, 3.0); //create column object & use Column column(IDIR, 1, &data);//r From 8f4b177b8116dd5302e52fe2bbc6de0aef84b025 Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Thu, 26 Nov 2026 14:35:49 +0100 Subject: [PATCH 6/8] test: add DataBlockHost to the UnitTestApp class and use it in column test to support GPU + fix rounding acceptance in testColumn.cpp --- src/dataBlock/dataBlockHost.cpp | 56 ++++++++++++++++++--------------- src/dataBlock/dataBlockHost.hpp | 4 ++- src/unitTestApp.cpp | 1 + src/unitTestApp.hpp | 2 ++ src/utils/tests/testColumn.cpp | 20 ++++++------ 5 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/dataBlock/dataBlockHost.cpp b/src/dataBlock/dataBlockHost.cpp index 0c3293b6..9e8447f1 100644 --- a/src/dataBlock/dataBlockHost.cpp +++ b/src/dataBlock/dataBlockHost.cpp @@ -20,7 +20,7 @@ #include "dataBlockHost.hpp" #include "fluid.hpp" -DataBlockHost::DataBlockHost(DataBlock& datain) { +DataBlockHost::DataBlockHost(DataBlock& datain, bool init_physics) { idfx::pushRegion("DataBlockHost::DataBlockHost(DataBlock)"); // copy the dataBlock object for later use @@ -58,31 +58,9 @@ DataBlockHost::DataBlockHost(DataBlock& datain) { // TO BE COMPLETED... dV = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->dV); - Vc = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Vc); - Uc = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Uc); - InvDt = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->InvDt); -#if MHD == YES - Vs = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Vs); - this->haveCurrent = data->hydro->haveCurrent; - if(data->hydro->haveCurrent) { - J = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->J); - } - #ifdef EVOLVE_VECTOR_POTENTIAL - Ve = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Ve); - #endif - - D_EXPAND( Ex3 = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->emf->ez); , - , - Ex1 = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->emf->ex); - Ex2 = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->emf->ey); ) -#endif - if(haveDust) { - dustVc = std::vector>(data->dust.size()); - for(int i = 0 ; i < data->dust.size() ; i++) { - dustVc[i] = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->dust[i]->Vc); - } - } + if (init_physics) + this->IniPhysics(); // if grid coarsening is enabled if(data->haveGridCoarsening) { @@ -115,6 +93,34 @@ DataBlockHost::DataBlockHost(DataBlock& datain) { idfx::popRegion(); } +void DataBlockHost::IniPhysics() { + Vc = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Vc); + Uc = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Uc); + InvDt = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->InvDt); + + #if MHD == YES + Vs = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Vs); + this->haveCurrent = data->hydro->haveCurrent; + if(data->hydro->haveCurrent) { + J = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->J); + } + #ifdef EVOLVE_VECTOR_POTENTIAL + Ve = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->Ve); + #endif + + D_EXPAND( Ex3 = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->emf->ez); , + , + Ex1 = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->emf->ex); + Ex2 = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->hydro->emf->ey); ) +#endif + if(haveDust) { + dustVc = std::vector>(data->dust.size()); + for(int i = 0 ; i < data->dust.size() ; i++) { + dustVc[i] = Kokkos::create_mirror_view(Kokkos::HostSpace(), data->dust[i]->Vc); + } + } +} + // Synchronisation routines of Data (*Only*) void DataBlockHost::SyncToDevice() { idfx::pushRegion("DataBlockHost::SyncToDevice()"); diff --git a/src/dataBlock/dataBlockHost.hpp b/src/dataBlock/dataBlockHost.hpp index 0bf4ccd8..a3700fba 100644 --- a/src/dataBlock/dataBlockHost.hpp +++ b/src/dataBlock/dataBlockHost.hpp @@ -87,11 +87,13 @@ class DataBlockHost { real dt; ///< Current timestep real t; ///< Current time - explicit DataBlockHost(DataBlock &); ///< Constructor from a device datablock + explicit DataBlockHost(DataBlock &, bool init_physics = true); ///< Constructor from a device datablock ///< (NB: does not sync any data) DataBlockHost() = default; ///< Default constructor + void IniPhysics(); ///< Init the physics part of the host data block + // The Planetary system (actually a copy from the dataBlock) bool haveplanetarySystem{false}; PlanetarySystem* planetarySystem; diff --git a/src/unitTestApp.cpp b/src/unitTestApp.cpp index 788b0fa6..2e691c1d 100644 --- a/src/unitTestApp.cpp +++ b/src/unitTestApp.cpp @@ -118,6 +118,7 @@ void UnitTestApp::InitComponents() { // instantiate required objects. this->dataBlock = std::make_shared(*grid, *input, false); + this->dataBlockHost = std::make_shared(*(this->dataBlock), false); } } // namespace idfx diff --git a/src/unitTestApp.hpp b/src/unitTestApp.hpp index 97f31d1a..1ed2df99 100644 --- a/src/unitTestApp.hpp +++ b/src/unitTestApp.hpp @@ -24,6 +24,7 @@ //idefix #include "idefix.hpp" #include "dataBlock.hpp" +#include "dataBlockHost.hpp" #include "grid.hpp" #include "gridHost.hpp" @@ -59,6 +60,7 @@ class UnitTestApp { std::shared_ptr grid; std::shared_ptr gridHost; std::shared_ptr dataBlock; + std::shared_ptr dataBlockHost; private: void InitInput(size_t s1, size_t s2, size_t s3, float cell_size); void InitComponents(void); diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp index b974694a..d99a316d 100644 --- a/src/utils/tests/testColumn.cpp +++ b/src/utils/tests/testColumn.cpp @@ -66,7 +66,7 @@ //remind the real value to check the constant increase prev = tmp(k,j,i); } else { - ASSERT_DOUBLE_EQ(prev * (j-1), tmp(k,j,i)) << "Shound have constant increase along theta"; + ASSERT_NEAR(prev * (j-1), tmp(k,j,i),10e-14) << "Shound have constant increase along theta"; } } } @@ -80,7 +80,7 @@ idfx::UnitTestApp app(8, 8, 8, 1.0); //extract one pointer for use - DataBlock & data = *app.dataBlock; + DataBlockHost & data = *app.dataBlockHost; //spawn an array const int MYVAR = 0; @@ -91,7 +91,7 @@ Kokkos::deep_copy(vars, 3.0); //create column object & use - Column column(KDIR, 1, &data);//phi + Column column(KDIR, 1, app.dataBlock.get());//phi column.ComputeColumn(vars, MYVAR); IdefixArray3D result = column.GetColumn(); @@ -118,7 +118,7 @@ //remind the real value to check the constant increase prev = tmp(k,j,i); } else { - ASSERT_DOUBLE_EQ(prev * (k-1), tmp(k,j,i)) << "Shound have constant increase along phi"; + ASSERT_NEAR(prev * (k-1), tmp(k,j,i), 10e-14) << "Shound have constant increase along phi"; } } } @@ -133,7 +133,7 @@ idfx::UnitTestApp app(8, 8, 8, 1.0); //extract one pointer for use - DataBlock & data = *app.dataBlock; + DataBlockHost & data = *app.dataBlockHost; //spawn an array const int MYVAR = 0; @@ -144,7 +144,7 @@ Kokkos::deep_copy(vars, 3.0); //create column object & use - Column column(IDIR, 1, &data);//r + Column column(IDIR, 1, app.dataBlock.get());//r column.ComputeColumn(vars, MYVAR); IdefixArray3D result = column.GetColumn(); @@ -169,7 +169,7 @@ //remind the real value to check the constant value over theta phi prev = tmp(k,j,i); } else { - ASSERT_DOUBLE_EQ(prev, tmp(k,j,i)) << "Shound have constant increase along theta phi"; + ASSERT_NEAR(prev, tmp(k,j,i), 10e-14) << "Shound have constant increase along theta phi"; } } } @@ -184,7 +184,7 @@ idfx::UnitTestApp app(8, 8, 8, 1.0); //extract one pointer for use - DataBlock & data = *app.dataBlock; + DataBlockHost & data = *app.dataBlockHost; //spawn an array IdefixArray3D var = IdefixArray3D("myvar", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); @@ -193,7 +193,7 @@ Kokkos::deep_copy(var, 3.0); //create column object & use - Column column(IDIR, 1, &data);//r + Column column(IDIR, 1, app.dataBlock.get());//r column.ComputeColumn(var); IdefixArray3D result = column.GetColumn(); @@ -218,7 +218,7 @@ //remind the real value to check the constant value over theta phi prev = tmp(k,j,i); } else { - ASSERT_DOUBLE_EQ(prev, tmp(k,j,i)) << "Should have constant value over phi / theta"; + ASSERT_NEAR(prev, tmp(k,j,i), 10e-14) << "Should have constant value over phi / theta"; } } } From a402091d62e814a29aa9b16389fed8db6f6ec80d Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Thu, 26 Nov 2026 19:19:31 +0100 Subject: [PATCH 7/8] test: oops forgot to add a DataBlockHost in the first test --- src/utils/tests/testColumn.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp index d99a316d..17b21fc9 100644 --- a/src/utils/tests/testColumn.cpp +++ b/src/utils/tests/testColumn.cpp @@ -28,7 +28,7 @@ idfx::UnitTestApp app(8, 8, 8, 1.0); //extract one pointer for use - DataBlock & data = *app.dataBlock; + DataBlockHost & data = *app.dataBlockHost; //spawn an array const int MYVAR = 0; @@ -39,7 +39,7 @@ Kokkos::deep_copy(vars, 3.0); //create column object & use - Column column(JDIR, 1, &data);//teta + Column column(JDIR, 1, app.dataBlock.get());//teta column.ComputeColumn(vars, MYVAR); IdefixArray3D result = column.GetColumn(); From 38437785426aa05d12229a31cb6c23c6b5a53a1c Mon Sep 17 00:00:00 2001 From: Sebastien Valat Date: Fri, 27 Nov 2026 13:51:07 +0100 Subject: [PATCH 8/8] test: add a Column test with its second option at -1 (inverse) --- src/utils/tests/testColumn.cpp | 61 +++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp index 17b21fc9..713c8734 100644 --- a/src/utils/tests/testColumn.cpp +++ b/src/utils/tests/testColumn.cpp @@ -66,7 +66,60 @@ //remind the real value to check the constant increase prev = tmp(k,j,i); } else { - ASSERT_NEAR(prev * (j-1), tmp(k,j,i),10e-14) << "Shound have constant increase along theta"; + ASSERT_NEAR(prev * (j-1), tmp(k,j,i),10e-14) << "Shound have constant increase along theta : j=" << j; + } + } + } + } + } + + // The test fill a 3D var with constant value and applies + // Colum on it. Along theta it should have constant increase + // following axis with option -1 + TEST(Column, ComputeColumn_4D_const_increase_along_theta_inverse_way) { + //ini idefix app + idfx::UnitTestApp app(8, 8, 8, 1.0); + + //extract one pointer for use + DataBlockHost & data = *app.dataBlockHost; + + //spawn an array + const int MYVAR = 0; + const int VARS = 1; + IdefixArray4D vars = IdefixArray4D("myvar", VARS, data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //fill with const values + Kokkos::deep_copy(vars, 3.0); + + //create column object & use + Column column(JDIR, -1, app.dataBlock.get());//teta + column.ComputeColumn(vars, MYVAR); + IdefixArray3D result = column.GetColumn(); + + //copy to ease checking + IdefixHostArray3D tmp("result_host", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + Kokkos::deep_copy(tmp, result); + + //should be const increase along theta except having 0 on ghosts + for (int i = 2 ; i < data.np_tot[IDIR] - 2 ; i++) { + for (int k = 2 ; k < data.np_tot[KDIR] - 2 ; k++) { + double prev = 0.0; + for (int j = data.np_tot[JDIR] - 1 ; j >= 0 ; j--) { + if (j < 2 || j >= 10) { + ASSERT_EQ(0, tmp(k,j,i)) << "Ghost"; + } else if (j == 9) { + const real r = data.x[0](i); + const real dtheta = 3.1415926/8.0; + //const real dV = r * r * sin(theta) * dr * dtheta * dphi; + //const real avgA = r * sin(theta) * dr * dphi; + //const real expected = 3.0 * dV / avgA; + const real expected = 3.0 * r * dtheta; + ASSERT_NEAR(expected, tmp(k,j,i), 0.15) << "First non ghost with single contribution"; + + //remind the real value to check the constant increase + prev = tmp(k,j,i); + } else { + ASSERT_NEAR(prev * (data.np_tot[JDIR] - j - 2), tmp(k,j,i),10e-14) << "Shound have constant increase along theta : j=" << j; } } } @@ -118,7 +171,7 @@ //remind the real value to check the constant increase prev = tmp(k,j,i); } else { - ASSERT_NEAR(prev * (k-1), tmp(k,j,i), 10e-14) << "Shound have constant increase along phi"; + ASSERT_NEAR(prev * (k-1), tmp(k,j,i), 10e-14) << "Shound have constant increase along phi : k=" << k; } } } @@ -169,7 +222,7 @@ //remind the real value to check the constant value over theta phi prev = tmp(k,j,i); } else { - ASSERT_NEAR(prev, tmp(k,j,i), 10e-14) << "Shound have constant increase along theta phi"; + ASSERT_NEAR(prev, tmp(k,j,i), 10e-14) << "Shound have constant increase along theta phi : k=" << k; } } } @@ -218,7 +271,7 @@ //remind the real value to check the constant value over theta phi prev = tmp(k,j,i); } else { - ASSERT_NEAR(prev, tmp(k,j,i), 10e-14) << "Should have constant value over phi / theta"; + ASSERT_NEAR(prev, tmp(k,j,i), 10e-14) << "Should have constant value over phi / theta k=" << k; } } }