From 066c09172c1e0c82771b254ff9ab4586627de2b8 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 9 Dec 2024 21:16:54 +0100 Subject: [PATCH 1/6] check conserved quantities --- test/MHD/OrszagTang3D/setup.cpp | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/MHD/OrszagTang3D/setup.cpp b/test/MHD/OrszagTang3D/setup.cpp index 8e7205c42..ceb508ab3 100644 --- a/test/MHD/OrszagTang3D/setup.cpp +++ b/test/MHD/OrszagTang3D/setup.cpp @@ -11,10 +11,48 @@ generators on different architectures. Output* myOutput; int outnum; + +void CheckConservation(DataBlock &data) { + static real firstCall{true}; + static std::array consArray; + data.hydro->ConvertPrimToCons(); + auto Uc = data.hydro->Uc; + auto dV = data.dV; + for(int nv = 0 ; nv < DefaultPhysics::nvar ; nv++) { + real cons = 0; + idefix_reduce("Timestep_reduction", + data.beg[KDIR], data.end[KDIR], + data.beg[JDIR], data.end[JDIR], + data.beg[IDIR], data.end[IDIR], + KOKKOS_LAMBDA (int k, int j, int i, real &c) { + c += dV(k,j,i)*Uc(nv,k,j,i); + }, + Kokkos::Sum(cons)); + #ifdef WITH_MPI + MPI_Allreduce(MPI_IN_PLACE, &cons, 1, realMPI, MPI_SUM, MPI_COMM_WORLD); + #endif + if(firstCall) { + consArray[nv] = cons; + } else { + real err = std::fabs((consArray[nv]-cons)); + if(err>1e-13) { + std::stringstream str; + str << "Quantity " << data.hydro->VcName[nv] << " is not conserved" << std::endl; + std::cout << "Error on " << data.hydro->VcName[nv] << " is " << err << std::endl; + std::cout << "Original=" << consArray[nv] << " New=" << cons << std::endl; + IDEFIX_ERROR(str); + } + } + } + firstCall=false; +} // Analysis function // This analysis checks that the restart routines are performing as they should void Analysis(DataBlock& data) { + idfx::cout << "Analysis: conservation check..." << std::endl; + CheckConservation(data); + idfx::cout << "Analysis: conservation check successful." << std::endl; idfx::cout << "Analysis: Checking restart routines" << std::endl; From 47729132c900622671a5d0ad790ce834f6f7bf64 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 9 Dec 2024 22:33:50 +0100 Subject: [PATCH 2/6] add conservation check in OT --- test/MHD/OrszagTang/idefix-hll.ini | 1 + .../MHD/OrszagTang/idefix-hlld-arithmetic.ini | 1 + test/MHD/OrszagTang/idefix-hlld-hll.ini | 1 + test/MHD/OrszagTang/idefix-hlld-hlld.ini | 1 + test/MHD/OrszagTang/idefix-hlld-uct0.ini | 1 + test/MHD/OrszagTang/idefix-hlld.ini | 1 + test/MHD/OrszagTang/idefix-tvdlf.ini | 1 + test/MHD/OrszagTang/idefix.ini | 1 + test/MHD/OrszagTang/setup.cpp | 44 ++++++++++++++++++- 9 files changed, 50 insertions(+), 2 deletions(-) diff --git a/test/MHD/OrszagTang/idefix-hll.ini b/test/MHD/OrszagTang/idefix-hll.ini index 4f13fd972..e1d2e8915 100644 --- a/test/MHD/OrszagTang/idefix-hll.ini +++ b/test/MHD/OrszagTang/idefix-hll.ini @@ -18,6 +18,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini b/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini index 8a656d7b3..b0af4b349 100644 --- a/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini +++ b/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini @@ -19,6 +19,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-hll.ini b/test/MHD/OrszagTang/idefix-hlld-hll.ini index b396e9917..0ccc549ce 100644 --- a/test/MHD/OrszagTang/idefix-hlld-hll.ini +++ b/test/MHD/OrszagTang/idefix-hlld-hll.ini @@ -19,6 +19,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-hlld.ini b/test/MHD/OrszagTang/idefix-hlld-hlld.ini index 5e158b939..b76d3e06b 100644 --- a/test/MHD/OrszagTang/idefix-hlld-hlld.ini +++ b/test/MHD/OrszagTang/idefix-hlld-hlld.ini @@ -19,6 +19,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-uct0.ini b/test/MHD/OrszagTang/idefix-hlld-uct0.ini index 23646b96f..0177ee791 100644 --- a/test/MHD/OrszagTang/idefix-hlld-uct0.ini +++ b/test/MHD/OrszagTang/idefix-hlld-uct0.ini @@ -19,6 +19,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld.ini b/test/MHD/OrszagTang/idefix-hlld.ini index cf6d5cbfd..457e73b62 100644 --- a/test/MHD/OrszagTang/idefix-hlld.ini +++ b/test/MHD/OrszagTang/idefix-hlld.ini @@ -18,6 +18,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix-tvdlf.ini b/test/MHD/OrszagTang/idefix-tvdlf.ini index ec9ffe85e..517e6b7dc 100644 --- a/test/MHD/OrszagTang/idefix-tvdlf.ini +++ b/test/MHD/OrszagTang/idefix-tvdlf.ini @@ -18,6 +18,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/idefix.ini b/test/MHD/OrszagTang/idefix.ini index 0e7421843..835972691 100644 --- a/test/MHD/OrszagTang/idefix.ini +++ b/test/MHD/OrszagTang/idefix.ini @@ -18,6 +18,7 @@ X2-beg periodic X2-end periodic [Output] +analysis 0.1 vtk 0.5 dmp 0.5 log 100 diff --git a/test/MHD/OrszagTang/setup.cpp b/test/MHD/OrszagTang/setup.cpp index 50c981c18..b337ff6bf 100644 --- a/test/MHD/OrszagTang/setup.cpp +++ b/test/MHD/OrszagTang/setup.cpp @@ -9,13 +9,53 @@ generators on different architectures. **/ /*********************************************/ - +void CheckConservation(DataBlock &data) { + static real firstCall{true}; + static std::array consArray; + data.hydro->ConvertPrimToCons(); + auto Uc = data.hydro->Uc; + auto dV = data.dV; + //idfx::cout << "Analysis: checking conserved quantities..." << std::endl; + #ifdef SINGLE_PRECISION + const real threshold = 1e-4; + #else + const real threshold = 1e-13; + #endif + for(int nv = 0 ; nv < DefaultPhysics::nvar ; nv++) { + real cons = 0; + idefix_reduce("Conserved quantity reduction", + data.beg[KDIR], data.end[KDIR], + data.beg[JDIR], data.end[JDIR], + data.beg[IDIR], data.end[IDIR], + KOKKOS_LAMBDA (int k, int j, int i, real &c) { + c += dV(k,j,i)*Uc(nv,k,j,i); + }, + Kokkos::Sum(cons)); + #ifdef WITH_MPI + MPI_Allreduce(MPI_IN_PLACE, &cons, 1, realMPI, MPI_SUM, MPI_COMM_WORLD); + #endif + if(firstCall) { + consArray[nv] = cons; + } else { + real err = std::fabs((consArray[nv]-cons)); + if(err>threshold) { + std::stringstream str; + str << "Quantity " << data.hydro->VcName[nv] << " is not conserved" << std::endl; + std::cout << "Error on " << data.hydro->VcName[nv] << " is " << err << std::endl; + std::cout << "Original=" << consArray[nv] << " New=" << cons << std::endl; + IDEFIX_ERROR(str); + } + } + } + firstCall=false; + //idfx::cout << "Analysis: done." << std::endl; +} // Default constructor // Initialisation routine. Can be used to allocate // Arrays or variables which are used later on Setup::Setup(Input &input, Grid &grid, DataBlock &data, Output &output) { - + output.EnrollAnalysis(&CheckConservation); } // This routine initialize the flow From 18ccbce923388b246880ce75fe8481372f1d2f54 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 1 Sep 2026 14:14:00 +0000 Subject: [PATCH 3/6] [pre-commit.ci lite] apply automatic fixes --- test/MHD/OrszagTang/idefix-hll.ini | 8 ++++---- test/MHD/OrszagTang/idefix-hlld-arithmetic.ini | 8 ++++---- test/MHD/OrszagTang/idefix-hlld-hll.ini | 8 ++++---- test/MHD/OrszagTang/idefix-hlld-hlld.ini | 8 ++++---- test/MHD/OrszagTang/idefix-hlld-uct0.ini | 8 ++++---- test/MHD/OrszagTang/idefix-hlld.ini | 8 ++++---- test/MHD/OrszagTang/idefix-tvdlf.ini | 8 ++++---- test/MHD/OrszagTang/idefix.ini | 8 ++++---- 8 files changed, 32 insertions(+), 32 deletions(-) diff --git a/test/MHD/OrszagTang/idefix-hll.ini b/test/MHD/OrszagTang/idefix-hll.ini index e1d2e8915..707f35605 100644 --- a/test/MHD/OrszagTang/idefix-hll.ini +++ b/test/MHD/OrszagTang/idefix-hll.ini @@ -18,7 +18,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini b/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini index b0af4b349..0c62cfcc5 100644 --- a/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini +++ b/test/MHD/OrszagTang/idefix-hlld-arithmetic.ini @@ -19,7 +19,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-hll.ini b/test/MHD/OrszagTang/idefix-hlld-hll.ini index 0ccc549ce..26ae64973 100644 --- a/test/MHD/OrszagTang/idefix-hlld-hll.ini +++ b/test/MHD/OrszagTang/idefix-hlld-hll.ini @@ -19,7 +19,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-hlld.ini b/test/MHD/OrszagTang/idefix-hlld-hlld.ini index b76d3e06b..8f82d6f8e 100644 --- a/test/MHD/OrszagTang/idefix-hlld-hlld.ini +++ b/test/MHD/OrszagTang/idefix-hlld-hlld.ini @@ -19,7 +19,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld-uct0.ini b/test/MHD/OrszagTang/idefix-hlld-uct0.ini index 0177ee791..b0d637d74 100644 --- a/test/MHD/OrszagTang/idefix-hlld-uct0.ini +++ b/test/MHD/OrszagTang/idefix-hlld-uct0.ini @@ -19,7 +19,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix-hlld.ini b/test/MHD/OrszagTang/idefix-hlld.ini index 457e73b62..449d941f4 100644 --- a/test/MHD/OrszagTang/idefix-hlld.ini +++ b/test/MHD/OrszagTang/idefix-hlld.ini @@ -18,7 +18,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix-tvdlf.ini b/test/MHD/OrszagTang/idefix-tvdlf.ini index 517e6b7dc..62cf6d280 100644 --- a/test/MHD/OrszagTang/idefix-tvdlf.ini +++ b/test/MHD/OrszagTang/idefix-tvdlf.ini @@ -18,7 +18,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 diff --git a/test/MHD/OrszagTang/idefix.ini b/test/MHD/OrszagTang/idefix.ini index 835972691..2ecc050ec 100644 --- a/test/MHD/OrszagTang/idefix.ini +++ b/test/MHD/OrszagTang/idefix.ini @@ -18,7 +18,7 @@ X2-beg periodic X2-end periodic [Output] -analysis 0.1 -vtk 0.5 -dmp 0.5 -log 100 +analysis 0.1 +vtk 0.5 +dmp 0.5 +log 100 From a1b97d218e11caa45653439a328c94fc1d068bdc Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Tue, 1 Sep 2026 16:14:33 +0200 Subject: [PATCH 4/6] fix linter --- test/MHD/OrszagTang3D/idefix.ini | 7 ++++--- test/MHD/OrszagTang3D/setup.cpp | 10 +--------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/test/MHD/OrszagTang3D/idefix.ini b/test/MHD/OrszagTang3D/idefix.ini index 1021bd335..5551b23f8 100644 --- a/test/MHD/OrszagTang3D/idefix.ini +++ b/test/MHD/OrszagTang3D/idefix.ini @@ -22,6 +22,7 @@ X3-beg periodic X3-end periodic [Output] -vtk 0.2 -dmp 0.2 -log 10 +vtk 0.2 +dmp 0.2 +log 10 +analysis 0.1 diff --git a/test/MHD/OrszagTang3D/setup.cpp b/test/MHD/OrszagTang3D/setup.cpp index dc7199ead..bebdbe45d 100644 --- a/test/MHD/OrszagTang3D/setup.cpp +++ b/test/MHD/OrszagTang3D/setup.cpp @@ -46,19 +46,11 @@ void CheckConservation(DataBlock &data) { } firstCall=false; } -// Analysis function -// This analysis checks that the restart routines are performing as they should -void Analysis(DataBlock& data) { - idfx::cout << "Analysis: conservation check..." << std::endl; - - CheckConservation(data); - idfx::cout << "Analysis: conservation check successful." << std::endl; - -} // Initialisation routine. Can be used to allocate // Arrays or variables which are used later on Setup::Setup(Input &input, Grid &grid, DataBlock &data, Output &output) { + output.EnrollAnalysis(&CheckConservation); } // This routine initialize the flow From 787c3dc2e32b4cfbf893453eac9dd4ac74be3507 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Tue, 1 Sep 2026 21:17:08 +0200 Subject: [PATCH 5/6] fix tolerance on conservation in single precision --- test/MHD/OrszagTang3D/setup.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/MHD/OrszagTang3D/setup.cpp b/test/MHD/OrszagTang3D/setup.cpp index bebdbe45d..b4aea929d 100644 --- a/test/MHD/OrszagTang3D/setup.cpp +++ b/test/MHD/OrszagTang3D/setup.cpp @@ -18,9 +18,15 @@ void CheckConservation(DataBlock &data) { data.hydro->ConvertPrimToCons(); auto Uc = data.hydro->Uc; auto dV = data.dV; + //idfx::cout << "Analysis: checking conserved quantities..." << std::endl; + #ifdef SINGLE_PRECISION + const real threshold = 1e-4; + #else + const real threshold = 1e-13; + #endif for(int nv = 0 ; nv < DefaultPhysics::nvar ; nv++) { real cons = 0; - idefix_reduce("Timestep_reduction", + idefix_reduce("Conserved quantity reduction", data.beg[KDIR], data.end[KDIR], data.beg[JDIR], data.end[JDIR], data.beg[IDIR], data.end[IDIR], @@ -35,7 +41,7 @@ void CheckConservation(DataBlock &data) { consArray[nv] = cons; } else { real err = std::fabs((consArray[nv]-cons)); - if(err>1e-13) { + if(err>threshold) { std::stringstream str; str << "Quantity " << data.hydro->VcName[nv] << " is not conserved" << std::endl; std::cout << "Error on " << data.hydro->VcName[nv] << " is " << err << std::endl; @@ -45,6 +51,7 @@ void CheckConservation(DataBlock &data) { } } firstCall=false; + //idfx::cout << "Analysis: done." << std::endl; } // Initialisation routine. Can be used to allocate From 3d52bd69ac1d9c63a09c5d630c208a490dfb1e8d Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Wed, 2 Sep 2026 09:28:24 +0200 Subject: [PATCH 6/6] increase tolerance (again) on single precision --- test/MHD/OrszagTang3D/setup.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MHD/OrszagTang3D/setup.cpp b/test/MHD/OrszagTang3D/setup.cpp index b4aea929d..abbc78534 100644 --- a/test/MHD/OrszagTang3D/setup.cpp +++ b/test/MHD/OrszagTang3D/setup.cpp @@ -20,7 +20,7 @@ void CheckConservation(DataBlock &data) { auto dV = data.dV; //idfx::cout << "Analysis: checking conserved quantities..." << std::endl; #ifdef SINGLE_PRECISION - const real threshold = 1e-4; + const real threshold = 2e-4; #else const real threshold = 1e-13; #endif