From 279bc71ba9c65afa4d9edcbde8b2a739f14a3ff6 Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Mon, 8 Feb 2016 09:20:05 +0100
Subject: [PATCH] Refactoring finite difference test.

---
 .../fdm/tnlCentralFiniteDifference.h          |   2 +-
 .../fdm/tnlForwardFiniteDifference.h          |   2 +-
 .../operators/fdm/tnlFiniteDifferencesTest.h  | 109 +++---------------
 .../operators/tnlPDEOperatorEocTest.h         |  14 ++-
 4 files changed, 29 insertions(+), 98 deletions(-)

diff --git a/src/operators/fdm/tnlCentralFiniteDifference.h b/src/operators/fdm/tnlCentralFiniteDifference.h
index 2e563a23a0..342113fa94 100644
--- a/src/operators/fdm/tnlCentralFiniteDifference.h
+++ b/src/operators/fdm/tnlCentralFiniteDifference.h
@@ -49,7 +49,7 @@ class tnlCentralFiniteDifference< tnlGrid< Dimensions, MeshReal, MeshDevice, Mes
       typedef Real RealType;
       typedef MeshDevice DeviceType;
       typedef Index IndexType;
-      typedef tnlExactDifference< XDifference, YDifference, ZDifference > ExactOperatorType;
+      typedef tnlExactDifference< Dimensions, XDifference, YDifference, ZDifference > ExactOperatorType;
             
       //static constexpr int getMeshDimensions() { return Dimensions; }
       
diff --git a/src/operators/fdm/tnlForwardFiniteDifference.h b/src/operators/fdm/tnlForwardFiniteDifference.h
index 237e395716..f687848a0e 100644
--- a/src/operators/fdm/tnlForwardFiniteDifference.h
+++ b/src/operators/fdm/tnlForwardFiniteDifference.h
@@ -50,7 +50,7 @@ class tnlForwardFiniteDifference< tnlGrid< Dimensions, MeshReal, MeshDevice, Mes
       typedef Real RealType;
       typedef MeshDevice DeviceType;
       typedef Index IndexType;
-      typedef tnlExactDifference< XDifference, YDifference, ZDifference > ExactOperatorType;
+      typedef tnlExactDifference< Dimensions, XDifference, YDifference, ZDifference > ExactOperatorType;
       
       static constexpr int getMeshDimensions() { return Dimensions; }
       
diff --git a/tests/unit-tests/operators/fdm/tnlFiniteDifferencesTest.h b/tests/unit-tests/operators/fdm/tnlFiniteDifferencesTest.h
index cdba506d48..ce49dc5600 100644
--- a/tests/unit-tests/operators/fdm/tnlFiniteDifferencesTest.h
+++ b/tests/unit-tests/operators/fdm/tnlFiniteDifferencesTest.h
@@ -30,70 +30,15 @@
 #include "../tnlPDEOperatorEocTestResult.h"
 #include <functions/tnlExpBumpFunction.h>
 
-template< typename MeshType,
-          typename RealType,
-          int XDifference,
-          int YDifference,
-          int ZDifference,
-          typename IndexType,   
-          typename TestFunction >
-class tnlPDEOperatorEocTestResult< 
-   tnlForwardFiniteDifference< MeshType, XDifference, YDifference, ZDifference, RealType, IndexType >, TestFunction >
-{
-   public:
-      static RealType getL1Eoc() 
-      {
-         if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 1.0;
-      };
-      static RealType getL1Tolerance() { return ( RealType ) 0.05; };
-
-      static RealType getL2Eoc()
-      { 
-         if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 1.0;
-      };
-      static RealType getL2Tolerance() { return ( RealType ) 0.05; };
-
-      static RealType getMaxEoc()
-      {
-         if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 1.0; 
-      };
-      static RealType getMaxTolerance() { return ( RealType ) 0.05; };
-};
-
-template< typename MeshType,
-          typename RealType,
-          int XDifference,
-          int YDifference,
-          int ZDifference,
-          typename IndexType,   
-          typename TestFunction >
-class tnlPDEOperatorEocTestResult< 
-   tnlBackwardFiniteDifference< MeshType, XDifference, YDifference, ZDifference, RealType, IndexType >, TestFunction >
+template< typename ApproximateOperator >
+class tnlFinitDifferenceEocTestResults
 {
    public:
-      static RealType getL1Eoc() 
-      {
-         if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 1.0;
-      };
-      static RealType getL1Tolerance() { return ( RealType ) 0.05; };
-
-      static RealType getL2Eoc()
-      { 
-         if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 1.0;
-      };
-      static RealType getL2Tolerance() { return ( RealType ) 0.05; };
-
-      static RealType getMaxEoc()
-      {
-         if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 1.0; 
-      };
-      static RealType getMaxTolerance() { return ( RealType ) 0.05; };
+        
+      typedef typename ApproximateOperator::RealType RealType;
+      
+      const RealType  eoc[ 3 ] =       { 1.0,  1.0,  1.0 };
+      const RealType  tolerance[ 3 ] = { 0.05, 0.05, 0.05 };      
 
 };
 
@@ -102,42 +47,22 @@ template< typename MeshType,
           int XDifference,
           int YDifference,
           int ZDifference,
-          typename IndexType,   
-          typename TestFunction >
-class tnlPDEOperatorEocTestResult< 
-   tnlCentralFiniteDifference< MeshType, XDifference, YDifference, ZDifference, RealType, IndexType >, TestFunction >
+          typename IndexType >
+class tnlFinitDifferenceEocTestResults< tnlCentralFiniteDifference< MeshType, XDifference, YDifference, ZDifference, RealType, IndexType > >
 {
    public:
-      static RealType getL1Eoc() 
-      {
-         //if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 2.0;
-      };
-      static RealType getL1Tolerance() { return ( RealType ) 0.05; };
-
-      static RealType getL2Eoc()
-      { 
-         //if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 2.0;
-      };
-      static RealType getL2Tolerance() { return ( RealType ) 0.05; };
-
-      static RealType getMaxEoc()
-      {
-         //if( XDifference < 2 && YDifference < 2 && ZDifference < 2 )
-            return ( RealType ) 2.0; 
-      };
-      static RealType getMaxTolerance() { return ( RealType ) 0.05; };
-
+      
+      const RealType  eoc[ 3 ] =       { 2.0,  2.0,  2.0 };
+      const RealType  tolerance[ 3 ] = { 0.05, 0.05, 0.05 };         
 };
 
-
 template< typename ApproximateOperator,
           typename TestFunction,
           bool write = false,
           bool verbose = false >
 class tnlFiniteDifferenceTest
-   : public tnlPDEOperatorEocTest< ApproximateOperator, TestFunction > 
+   : public tnlPDEOperatorEocTest< ApproximateOperator, TestFunction >,
+     public tnlFinitDifferenceEocTestResults< ApproximateOperator >
 {
    public:
       
@@ -149,8 +74,6 @@ class tnlFiniteDifferenceTest
       
       const IndexType coarseMeshSize[ 3 ] = { 1024, 256, 64 };
       
-      const RealType  eoc[ 3 ] =       { 2.0,  2.0,  2.0 };
-      const RealType  tolerance[ 3 ] = { 0.05, 0.05, 0.05 };      
    
       static tnlString getType()
       { 
@@ -181,7 +104,7 @@ class tnlFiniteDifferenceTest
          RealType coarseErrors[ 3 ], fineErrors[ 3 ];
          this->getApproximationError( coarseMeshSize[ MeshType::getDimensionsCount() - 1 ], coarseErrors );
          this->getApproximationError( 2 * coarseMeshSize[ MeshType::getDimensionsCount() - 1 ], fineErrors );
-         this->checkEoc( coarseErrors, fineErrors, eoc, tolerance, verbose );                            
+         this->checkEoc( coarseErrors, fineErrors, this->eoc, this->tolerance, verbose );                            
       }
       
    protected:
@@ -278,7 +201,7 @@ bool setDifferenceOrder()
 bool test()
 {
    const bool writeFunctions( false );
-   const bool verbose( false );
+   const bool verbose( true );
    if( ! setDifferenceOrder< double, tnlHost, int, double, int, 64, writeFunctions, verbose >() )
       return false;
 #ifdef HAVE_CUDA
diff --git a/tests/unit-tests/operators/tnlPDEOperatorEocTest.h b/tests/unit-tests/operators/tnlPDEOperatorEocTest.h
index 44484f3d80..acda81875e 100644
--- a/tests/unit-tests/operators/tnlPDEOperatorEocTest.h
+++ b/tests/unit-tests/operators/tnlPDEOperatorEocTest.h
@@ -22,6 +22,14 @@
 #include "tnlPDEOperatorEocTestFunctionSetter.h"
 #include "tnlApproximationError.h"
 
+#ifdef HAVE_CPPUNIT
+#include <cppunit/TestSuite.h>
+#include <cppunit/TestResult.h>
+#include <cppunit/TestCaller.h>
+#include <cppunit/TestCase.h>
+#endif
+
+
 template< typename ApproximateOperator,
           typename TestFunction >
 class tnlPDEOperatorEocTest
@@ -79,7 +87,7 @@ class tnlPDEOperatorEocTest
           */
          RealType maxEoc = log( coarse[ 0 ] / fine[ 0 ] ) / log( 2.0 );
          if( verbose )
-            std::cout << "Max error EOC = " << maxEoc << std::endl;
+            std::cout << "Max error EOC = " << maxEoc << " expected " << eoc[ 0 ] << std::endl;
 #ifdef HAVE_CPPUNIT
          CPPUNIT_ASSERT( fabs( maxEoc - eoc[ 0 ] ) < tolerance[ 0 ] );
 #endif
@@ -89,7 +97,7 @@ class tnlPDEOperatorEocTest
           */
          RealType l1Eoc = log( coarse[ 1 ] / fine[ 1 ] ) / log( 2.0 );
          if( verbose )
-            std::cout << "L1 error EOC = " << l1Eoc << std::endl;         
+            std::cout << "L1 error EOC = " << l1Eoc << " expected " << eoc[ 1 ]  << std::endl;         
 #ifdef HAVE_CPPUNIT
          CPPUNIT_ASSERT( fabs( l1Eoc - eoc[ 1 ] ) < tolerance[ 1 ] );
 #endif         
@@ -99,7 +107,7 @@ class tnlPDEOperatorEocTest
           */
          RealType l2Eoc = log( coarse[ 2 ] / fine[ 2 ] ) / log( 2.0 );
          if( verbose )
-            std::cout << "L2 error EOC = " << l2Eoc << std::endl;
+            std::cout << "L2 error EOC = " << l2Eoc << " expected " << eoc[ 2 ] << std::endl;
 #ifdef HAVE_CPPUNIT         
          CPPUNIT_ASSERT( fabs( l2Eoc - eoc[ 2 ] ) < tolerance[ 2 ] );
 #endif         
-- 
GitLab