Skip to content
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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)
Expand Down
104 changes: 59 additions & 45 deletions src/dataBlock/dataBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,50 +121,10 @@ DataBlock::DataBlock(Grid &grid, Input &input) {

this->states["current"] = StateContainer();

// Initialize the Dump object
this->dump = std::make_unique<Dump>(input, this);

// Initialize the VTK object
this->vtk = std::make_unique<Vtk>(input, this, "data");

// Init XDMF objects for HDF5 outputs
#ifdef WITH_HDF5
this->xdmf= std::make_unique<Xdmf>(input,this);
#endif


// Initialize the hydro object attached to this datablock
this->hydro = std::make_unique<Fluid<DefaultPhysics>>(grid, input, this);

// Initialise Fargo if needed
if(input.CheckBlock("Fargo")) {
this->fargo = std::make_unique<Fargo>(input, DefaultPhysics::nvar, this);
this->haveFargo = true;
}

// initialise planets if needed
if(input.CheckBlock("Planet")) {
this->planetarySystem = std::make_unique<PlanetarySystem>(input, this);
this->haveplanetarySystem = true;
}

// Initialise gravity if needed (automatically if planets are present)
if(input.CheckBlock("Gravity") || haveplanetarySystem) {
this->gravity = std::make_unique<Gravity>(input, this);
this->haveGravity = true;
}

// Initialise dust grains if needed
if(input.CheckBlock("Dust")) {
haveDust = true;
int nSpecies = input.Get<int>("Dust","nSpecies",0);
for(int i = 0 ; i < nSpecies ; i++) {
dust.emplace_back(std::make_unique<Fluid<DustPhysics>>(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();
}
Expand Down Expand Up @@ -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<Dump>(input, this);

// Initialize the VTK object
this->vtk = std::make_unique<Vtk>(input, this, "data");

// Init XDMF objects for HDF5 outputs
#ifdef WITH_HDF5
this->xdmf= std::make_unique<Xdmf>(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<Fluid<DefaultPhysics>>(grid, input, this);

// Initialise Fargo if needed
if(input.CheckBlock("Fargo")) {
this->fargo = std::make_unique<Fargo>(input, DefaultPhysics::nvar, this);
this->haveFargo = true;
}

// initialise planets if needed
if(input.CheckBlock("Planet")) {
this->planetarySystem = std::make_unique<PlanetarySystem>(input, this);
this->haveplanetarySystem = true;
}

// Initialise gravity if needed (automatically if planets are present)
if(input.CheckBlock("Gravity") || haveplanetarySystem) {
this->gravity = std::make_unique<Gravity>(input, this);
this->haveGravity = true;
}

// Initialise dust grains if needed
if(input.CheckBlock("Dust")) {
haveDust = true;
int nSpecies = input.Get<int>("Dust","nSpecies",0);
for(int i = 0 ; i < nSpecies ; i++) {
dust.emplace_back(std::make_unique<Fluid<DustPhysics>>(grid, input, this, i));
}
}
}

// Recompute magnetic fields from vector potential in dedicated fluids
void DataBlock::DeriveVectorPotential() {
if constexpr(DefaultPhysics::mhd) {
Expand Down
5 changes: 4 additions & 1 deletion src/dataBlock/dataBlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 31 additions & 25 deletions src/dataBlock/dataBlockHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<IdefixHostArray4D<real>>(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) {
Expand Down Expand Up @@ -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<IdefixHostArray4D<real>>(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()");
Expand Down
4 changes: 3 additions & 1 deletion src/dataBlock/dataBlockHost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ real randm(void) {
return static_cast<real>(static_cast<double>(q) / static_cast<double>(m));
}

void safeExit(int retCode) {
__attribute__((weak)) void safeExit(int retCode) {
if(retCode != 0) {
#ifdef WITH_MPI
MPI_Abort(MPI_COMM_WORLD,retCode);
Expand Down
7 changes: 7 additions & 0 deletions src/gtest-idfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// includes
#include <string>
#include <format>
#if __has_include(<filesystem>)
#include <filesystem> // NOLINT [build/c++17]
namespace fs = std::filesystem;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/gtest-idfx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down
37 changes: 37 additions & 0 deletions src/tests/testUnitTestApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ***********************************************************************************
// Idefix MHD astrophysical code
//
// Source file src/tests/testUnitTestApp.hpp
//
// Last modified : 10/2026
//
// Copyright(C) by :
// - Sébastien Valat <sebastien.valat@univ-grenoble-alpes.fr> (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);
}
Loading
Loading