Salome HOME
Load interpolation matrix from the outside
[tools/medcoupling.git] / src / INTERP_KERNELTest / Interpolation3DTest.cxx
index a538c6fdbbe0ee89c89fc969a944f586844e89a2..c0349a5ffc1d71ae67c440dc6d41b29543492341 100644 (file)
@@ -1,36 +1,37 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include "Interpolation3DTest.hxx"
-#include "MEDMEM_Mesh.hxx"
 
-#include <iostream>
+#include "MEDFileMesh.hxx"
+#include "MEDCouplingUMesh.hxx"
+#include "MEDCouplingFieldDouble.hxx"
+
 #include <map>
-#include <vector>
 #include <cmath>
+#include <vector>
+#include <iostream>
 #include <algorithm>
 
 #include "VectorUtils.hxx"
 
-#include "MEDMEM_Field.hxx"
-#include "MEDMEM_Support.hxx"
-
-// levels : 
+// levels :
 // 1 - titles and volume results
 // 2 - symmetry / diagonal results and intersection matrix output
 // 3 - empty
 
 //#define VOL_PREC 1.0e-6
 
-using namespace MEDMEM;
-using namespace std;
+using namespace MEDCoupling;
 using namespace INTERP_KERNEL;
-using namespace MED_EN;
 
 double Interpolation3DTest::sumRow(const IntersectionMatrix& m, int i) const
 {
@@ -53,7 +52,7 @@ double Interpolation3DTest::sumRow(const IntersectionMatrix& m, int i) const
     {
       if(iter->count(i) != 0.0)
         {
-          map<int, double>::const_iterator iter2 = iter->find(i);
+          std::map<int, double>::const_iterator iter2 = iter->find(i);
           vol += iter2->second;
         }
     }
@@ -64,7 +63,7 @@ double Interpolation3DTest::sumCol(const IntersectionMatrix& m, int i) const
 {
   double vol = 0.0;
   const std::map<int, double>& col = m[i];
-  for(map<int, double>::const_iterator iter = col.begin() ; iter != col.end() ; ++iter)
+  for(std::map<int, double>::const_iterator iter = col.begin() ; iter != col.end() ; ++iter)
     {
       vol += std::fabs(iter->second);
     }
@@ -72,28 +71,25 @@ double Interpolation3DTest::sumCol(const IntersectionMatrix& m, int i) const
 }
 
 
-void Interpolation3DTest::getVolumes(MESH& mesh, double* tab) const
+void Interpolation3DTest::getVolumes(MEDCoupling::MEDCouplingUMesh& mesh, double *tab) const
 {
-  SUPPORT *sup=new SUPPORT(&mesh,"dummy",MED_CELL);
-  FIELD<double>* f=mesh.getVolume(sup);
-  const double *tabS=f->getValue();
-  std::copy(tabS,tabS+mesh.getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS),tab)
-    delete sup;
+  MCAuto<MEDCouplingFieldDouble> vol=mesh->getMeasureField(true);
+  std::copy(vol->getArray()->begin(),vol->getArray()->end(),tab);
 }
 
 double Interpolation3DTest::sumVolume(const IntersectionMatrix& m) const
 {
-  
-  vector<double> volumes;
+
+  std::vector<double> volumes;
   for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
     {
-      for(map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
+      for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
         {
           volumes.push_back(iter2->second);
           //    vol += std::abs(iter2->second);
         }
     }
-  
+
   // sum in ascending order to avoid rounding errors
 
   sort(volumes.begin(), volumes.end());
@@ -102,15 +98,15 @@ double Interpolation3DTest::sumVolume(const IntersectionMatrix& m) const
   return vol;
 }
 
-bool Interpolation3DTest::testVolumes(const IntersectionMatrix& m,  MESH& sMesh,  MESH& tMesh) const
+bool Interpolation3DTest::testVolumes(const IntersectionMatrix& m,  MEDCouplingUMesh& sMesh,  MEDCouplingUMesh& tMesh) const
 {
   bool ok = true;
 
   // source elements
-  double* sVol = new double[sMesh.getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS)];
+  double* sVol = new double[sMesh.getNumberOfCells()];
   getVolumes(sMesh, sVol);
 
-  for(int i = 0; i < sMesh.getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS); ++i)
+  for(int i = 0; i < sMesh.getNumberOfCells(); ++i)
     {
       const double sum_row = sumRow(m, i+1);
       if(!epsilonEqualRelative(sum_row, sVol[i], VOL_PREC))
@@ -122,9 +118,9 @@ bool Interpolation3DTest::testVolumes(const IntersectionMatrix& m,  MESH& sMesh,
     }
 
   // target elements
-  double* tVol = new double[tMesh.getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS)];
+  double* tVol = new double[tMesh.getNumberOfCells()];
   getVolumes(tMesh, tVol);
-  for(int i = 0; i < tMesh.getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS); ++i)
+  for(int i = 0; i < tMesh.getNumberOfCells(); ++i)
     {
       const double sum_col = sumCol(m, i);
       if(!epsilonEqualRelative(sum_col, tVol[i], VOL_PREC))
@@ -146,7 +142,7 @@ bool Interpolation3DTest::areCompatitable(const IntersectionMatrix& m1, const In
   int i = 0;
   for(IntersectionMatrix::const_iterator iter = m1.begin() ; iter != m1.end() ; ++iter)
     {
-      for(map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
+      for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
         {
           int j = iter2->first;
           if(m2.at(j-1).count(i+1) == 0)
@@ -167,7 +163,7 @@ bool Interpolation3DTest::areCompatitable(const IntersectionMatrix& m1, const In
     }
   return compatitable;
 }
-      
+
 bool Interpolation3DTest::testSymmetric(const IntersectionMatrix& m1, const IntersectionMatrix& m2) const
 {
 
@@ -181,13 +177,13 @@ bool Interpolation3DTest::testSymmetric(const IntersectionMatrix& m1, const Inte
 
   for(IntersectionMatrix::const_iterator iter = m1.begin() ; iter != m1.end() ; ++iter)
     {
-      for(map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
+      for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
         {
           int j = iter2->first;
           const double v1 = iter2->second;
           //if(m2[j - 1].count(i+1) > 0)
           //  {
-          map<int, double> theMap =  m2.at(j-1);
+          std::map<int, double> theMap =  m2.at(j-1);
           const double v2 = theMap[i + 1];
           if(v1 != v2)
             {
@@ -215,7 +211,7 @@ bool Interpolation3DTest::testDiagonal(const IntersectionMatrix& m) const
   bool isDiagonal = true;
   for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
     {
-      for(map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
+      for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
         {
           int j = iter2->first;
           const double vol = iter2->second;
@@ -241,14 +237,14 @@ bool Interpolation3DTest::testDiagonal(const IntersectionMatrix& m) const
 void Interpolation3DTest::dumpIntersectionMatrix(const IntersectionMatrix& m) const
 {
   int i = 0;
-  std::cout << "Intersection matrix is " << endl;
+  std::cout << "Intersection matrix is " << std::endl;
   for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
     {
-      for(map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
+      for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
         {
-    
-          std::cout << "V(" << i << ", " << iter2->first << ") = " << iter2->second << endl;
-    
+
+          std::cout << "V(" << i << ", " << iter2->first << ") = " << iter2->second << std::endl;
+
         }
       ++i;
     }
@@ -263,18 +259,25 @@ void Interpolation3DTest::setUp()
 void Interpolation3DTest::tearDown()
 {
   delete interpolator;
-} 
+}
 
 void Interpolation3DTest::calcIntersectionMatrix(const char* mesh1path, const char* mesh1, const char* mesh2path, const char* mesh2, IntersectionMatrix& m) const
 {
-  const string dataBaseDir = getenv("MED_ROOT_DIR");
-  const string dataDir = dataBaseDir + "/share/salome/resources/med/";
+  string dataDir = "";
+  if ( getenv("MEDCOUPLING_ROOT_DIR") ) {
+    dataDir = getenv("MEDCOUPLING_ROOT_DIR");
+    dataDir += "/share/resources/med/";
+  }
+  else {
+    dataDir = get_current_dir_name();
+    dataDir += "/../../resources/";
+  }
 
   LOG(1, std::endl << "=== -> intersecting src = " << mesh1 << ", target = " << mesh2 );
 
   LOG(5, "Loading " << mesh1 << " from " << mesh1path);
   MESH sMesh(MED_DRIVER, dataDir+mesh1path, mesh1);
-  
+
   LOG(5, "Loading " << mesh2 << " from " << mesh2path);
   MESH tMesh(MED_DRIVER, dataDir+mesh2path, mesh2);
 
@@ -288,7 +291,7 @@ void Interpolation3DTest::calcIntersectionMatrix(const char* mesh1path, const ch
     }
 
   LOG(1, "Intersection calculation done. " << std::endl );
-  
+
 }
 
 void Interpolation3DTest::intersectMeshes(const char* mesh1path, const char* mesh1, const char* mesh2path, const char* mesh2, const double correctVol, const double prec, bool doubleTest) const
@@ -304,7 +307,7 @@ void Interpolation3DTest::intersectMeshes(const char* mesh1path, const char* mes
   IntersectionMatrix matrix1;
   calcIntersectionMatrix(mesh1path, mesh1, mesh2path, mesh2, matrix1);
 
-#if LOG_LEVEL >= 2 
+#if LOG_LEVEL >= 2
   dumpIntersectionMatrix(matrix1);
 #endif
 
@@ -324,14 +327,14 @@ void Interpolation3DTest::intersectMeshes(const char* mesh1path, const char* mes
     }
   else
     {
-      
+
       IntersectionMatrix matrix2;
-      calcIntersectionMatrix(mesh2path, mesh2, mesh1path, mesh1, matrix2);    
+      calcIntersectionMatrix(mesh2path, mesh2, mesh1path, mesh1, matrix2);
 
 #if LOG_LEVEL >= 2
       dumpIntersectionMatrix(matrix2);
 #endif
-      
+
       const double vol2 = sumVolume(matrix2);
 
       LOG(1, "vol1 =  " << vol1 << ", vol2 = " << vol2 << ", correctVol = " << correctVol );