From 1326c8ac675c514a47044658bc892cc7897d5e66 Mon Sep 17 00:00:00 2001
From: Beatrix Becker <beatrix.becker@iws.uni-stuttgart.de>
Date: Wed, 2 Oct 2019 17:34:39 +0200
Subject: [PATCH] [constraintsolver tests] make them failable DUNE_THROW if
 error to reference solution gets too high

---
 .../immiscibleflash/test_immiscibleflash.cc   | 37 ++++++++++++-------
 test/material/ncpflash/test_ncpflash.cc       | 36 +++++++++++-------
 2 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/test/material/immiscibleflash/test_immiscibleflash.cc b/test/material/immiscibleflash/test_immiscibleflash.cc
index 7aa67760d0..6608553dd1 100644
--- a/test/material/immiscibleflash/test_immiscibleflash.cc
+++ b/test/material/immiscibleflash/test_immiscibleflash.cc
@@ -44,6 +44,8 @@
 #include <dumux/material/fluidmatrixinteractions/2p/regularizedbrookscorey.hh>
 #include <dumux/material/fluidmatrixinteractions/2p/efftoabslaw.hh>
 
+#include <dune/common/exceptions.hh>
+
 template <class Scalar, class FluidState>
 void checkSame(const FluidState &fsRef, const FluidState &fsFlash)
 {
@@ -57,28 +59,35 @@ void checkSame(const FluidState &fsRef, const FluidState &fsFlash)
         using std::abs;
         error = 1 - fsRef.pressure(phaseIdx)/fsFlash.pressure(phaseIdx);
         if (abs(error) > 1e-6) {
-            std::cout << "pressure error phase " << phaseIdx << ": "
-                      << fsFlash.pressure(phaseIdx)  << " flash vs "
-                      << fsRef.pressure(phaseIdx) << " reference"
-                      << " error=" << error << "\n";
+            DUNE_THROW(Dune::Exception, "Mismatch to reference. Pressure of phase "
+            << phaseIdx << " calculated by flash is: "
+            << fsFlash.pressure(phaseIdx)  << " vs "
+            << fsRef.pressure(phaseIdx) << " calculated as reference."
+            << " Error = " << error << "\n");
         }
 
         // check the saturations
         error = fsRef.saturation(phaseIdx) - fsFlash.saturation(phaseIdx);
-        if (abs(error) > 1e-6)
-            std::cout << "saturation error phase " << phaseIdx << ": "
-                      << fsFlash.saturation(phaseIdx) << " flash vs "
-                      << fsRef.saturation(phaseIdx) << " reference"
-                      << " error=" << error << "\n";
+        if (abs(error) > 1e-6) {
+            DUNE_THROW(Dune::Exception, "Mismatch to reference. Saturation of phase "
+            << phaseIdx << " calculated by flash is: "
+            << fsFlash.saturation(phaseIdx)  << " vs "
+            << fsRef.saturation(phaseIdx) << " calculated as reference."
+            << " Error = " << error << "\n");
+        }
+
 
         // check the compositions
         for (int compIdx = 0; compIdx < numComponents; ++ compIdx) {
             error = fsRef.moleFraction(phaseIdx, compIdx) - fsFlash.moleFraction(phaseIdx, compIdx);
-            if (abs(error) > 1e-6)
-                std::cout << "composition error phase " << phaseIdx << ", component " << compIdx << ": "
-                          << fsFlash.moleFraction(phaseIdx, compIdx) << " flash vs "
-                          << fsRef.moleFraction(phaseIdx, compIdx) << " reference"
-                          << " error=" << error << "\n";
+            if (abs(error) > 1e-6) {
+                DUNE_THROW(Dune::Exception, "Mismatch to reference. Mole fraction of component "
+                << compIdx << " in phase "
+                << phaseIdx << " calculated by flash is: "
+                << fsFlash.moleFraction(phaseIdx, compIdx) << " vs "
+                << fsRef.moleFraction(phaseIdx, compIdx) << " calculated as reference."
+                << " Error = " << error << "\n");
+            }
         }
     }
 }
diff --git a/test/material/ncpflash/test_ncpflash.cc b/test/material/ncpflash/test_ncpflash.cc
index 6a930a873a..d2e5001992 100644
--- a/test/material/ncpflash/test_ncpflash.cc
+++ b/test/material/ncpflash/test_ncpflash.cc
@@ -41,6 +41,8 @@
 #include <dumux/material/fluidmatrixinteractions/2p/regularizedbrookscorey.hh>
 #include <dumux/material/fluidmatrixinteractions/2p/efftoabslaw.hh>
 
+#include <dune/common/exceptions.hh>
+
 template <class Scalar, class FluidState>
 void checkSame(const FluidState &fsRef, const FluidState &fsFlash)
 {
@@ -54,28 +56,34 @@ void checkSame(const FluidState &fsRef, const FluidState &fsFlash)
         using std::abs;
         error = 1 - fsRef.pressure(phaseIdx)/fsFlash.pressure(phaseIdx);
         if (abs(error) > 1e-6) {
-            std::cout << "pressure error phase " << phaseIdx << ": "
-                      << fsFlash.pressure(phaseIdx)  << " flash vs "
-                      << fsRef.pressure(phaseIdx) << " reference"
-                      << " error=" << error << "\n";
+            DUNE_THROW(Dune::Exception, "Mismatch to reference. Pressure of phase "
+            << phaseIdx << " calculated by flash is: "
+            << fsFlash.pressure(phaseIdx)  << " vs "
+            << fsRef.pressure(phaseIdx) << " calculated as reference."
+            << " Error = " << error << "\n");
         }
 
         // check the saturations
         error = fsRef.saturation(phaseIdx) - fsFlash.saturation(phaseIdx);
-        if (abs(error) > 1e-6)
-            std::cout << "saturation error phase " << phaseIdx << ": "
-                      << fsFlash.saturation(phaseIdx) << " flash vs "
-                      << fsRef.saturation(phaseIdx) << " reference"
-                      << " error=" << error << "\n";
+        if (abs(error) > 1e-6) {
+            DUNE_THROW(Dune::Exception, "Mismatch to reference. Saturation of phase "
+            << phaseIdx << " calculated by flash is: "
+            << fsFlash.saturation(phaseIdx)  << " vs "
+            << fsRef.saturation(phaseIdx) << " calculated as reference."
+            << " Error = " << error << "\n");
+        }
 
         // check the compositions
         for (int compIdx = 0; compIdx < numComponents; ++ compIdx) {
             error = fsRef.moleFraction(phaseIdx, compIdx) - fsFlash.moleFraction(phaseIdx, compIdx);
-            if (abs(error) > 1e-6)
-                std::cout << "composition error phase " << phaseIdx << ", component " << compIdx << ": "
-                          << fsFlash.moleFraction(phaseIdx, compIdx) << " flash vs "
-                          << fsRef.moleFraction(phaseIdx, compIdx) << " reference"
-                          << " error=" << error << "\n";
+            if (abs(error) > 1e-6) {
+                DUNE_THROW(Dune::Exception, "Mismatch to reference. Mole fraction of component "
+                << compIdx << " in phase "
+                << phaseIdx << " calculated by flash is: "
+                << fsFlash.moleFraction(phaseIdx, compIdx) << " vs "
+                << fsRef.moleFraction(phaseIdx, compIdx) << " calculated as reference."
+                << " Error = " << error << "\n");
+            }
         }
     }
 }
-- 
GitLab