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/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/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/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..66b7f6ee --- /dev/null +++ b/src/tests/testUnitTestApp.cpp @@ -0,0 +1,37 @@ +// *********************************************************************************** +// 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); +} diff --git a/src/unitTestApp.cpp b/src/unitTestApp.cpp new file mode 100644 index 00000000..2e691c1d --- /dev/null +++ b/src/unitTestApp.cpp @@ -0,0 +1,124 @@ +// *********************************************************************************** +// 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]", + #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", + "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); + this->dataBlockHost = std::make_shared(*(this->dataBlock), false); +} + +} // namespace idfx diff --git a/src/unitTestApp.hpp b/src/unitTestApp.hpp new file mode 100644 index 00000000..1ed2df99 --- /dev/null +++ b/src/unitTestApp.hpp @@ -0,0 +1,71 @@ +// *********************************************************************************** +// 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 "dataBlockHost.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; + std::shared_ptr dataBlockHost; + 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..0741df89 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -26,5 +26,6 @@ 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 ) endif() diff --git a/src/utils/tests/testColumn.cpp b/src/utils/tests/testColumn.cpp new file mode 100644 index 00000000..713c8734 --- /dev/null +++ b/src/utils/tests/testColumn.cpp @@ -0,0 +1,280 @@ +// *********************************************************************************** +// 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" + +// 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 + 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 = 0 ; j < data.np_tot[JDIR] ; j++) { + 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); + } else { + 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; + } + } + } + } + } + + // 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 + 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(KDIR, 1, app.dataBlock.get());//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) { + 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); + } else { + ASSERT_NEAR(prev * (k-1), tmp(k,j,i), 10e-14) << "Shound have constant increase along phi : k=" << k; + } + } + } + } + } + + // 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 + 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(IDIR, 1, app.dataBlock.get());//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 + 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_NEAR(prev, tmp(k,j,i), 10e-14) << "Shound have constant increase along theta phi : k=" << k; + } + } + } + } + } + + // 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 + DataBlockHost & data = *app.dataBlockHost; + + //spawn an array + IdefixArray3D var = IdefixArray3D("myvar", data.np_tot[KDIR], data.np_tot[JDIR], data.np_tot[IDIR]); + + //fill with const values + Kokkos::deep_copy(var, 3.0); + + //create column object & use + Column column(IDIR, 1, app.dataBlock.get());//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 + 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_NEAR(prev, tmp(k,j,i), 10e-14) << "Should have constant value over phi / theta k=" << k; + } + } + } + } + } +#endif //DIMENSION == 3 && GEOMETRY == SPHERICAL