1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "Interpolation3DTest.hxx"
22 #include "MEDFileMesh.hxx"
23 #include "MEDCouplingUMesh.hxx"
24 #include "MEDCouplingFieldDouble.hxx"
32 #include "VectorUtils.hxx"
35 // 1 - titles and volume results
36 // 2 - symmetry / diagonal results and intersection matrix output
43 //#define VOL_PREC 1.0e-6
45 using namespace ParaMEDMEM;
46 using namespace INTERP_KERNEL;
48 double Interpolation3DTest::sumRow(const IntersectionMatrix& m, int i) const
51 for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
53 if(iter->count(i) != 0.0)
55 std::map<int, double>::const_iterator iter2 = iter->find(i);
62 double Interpolation3DTest::sumCol(const IntersectionMatrix& m, int i) const
65 const std::map<int, double>& col = m[i];
66 for(std::map<int, double>::const_iterator iter = col.begin() ; iter != col.end() ; ++iter)
68 vol += std::fabs(iter->second);
74 void Interpolation3DTest::getVolumes(ParaMEDMEM::MEDCouplingUMesh& mesh, double *tab) const
76 MEDCouplingAutoRefCountObjectPtr<MEDCouplingFieldDouble> vol=mesh->getMeasureField(true);
77 std::copy(vol->getArray()->begin(),vol->getArray()->end(),tab);
80 double Interpolation3DTest::sumVolume(const IntersectionMatrix& m) const
83 std::vector<double> volumes;
84 for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
86 for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
88 volumes.push_back(iter2->second);
89 // vol += std::abs(iter2->second);
93 // sum in ascending order to avoid rounding errors
95 sort(volumes.begin(), volumes.end());
96 const double vol = accumulate(volumes.begin(), volumes.end(), 0.0);
101 bool Interpolation3DTest::testVolumes(const IntersectionMatrix& m, MEDCouplingUMesh& sMesh, MEDCouplingUMesh& tMesh) const
106 double* sVol = new double[sMesh.getNumberOfCells()];
107 getVolumes(sMesh, sVol);
109 for(int i = 0; i < sMesh.getNumberOfCells(); ++i)
111 const double sum_row = sumRow(m, i+1);
112 if(!epsilonEqualRelative(sum_row, sVol[i], VOL_PREC))
114 LOG(1, "Source volume inconsistent : vol of cell " << i << " = " << sVol[i] << " but the row sum is " << sum_row );
117 LOG(1, "diff = " <<sum_row - sVol[i] );
121 double* tVol = new double[tMesh.getNumberOfCells()];
122 getVolumes(tMesh, tVol);
123 for(int i = 0; i < tMesh.getNumberOfCells(); ++i)
125 const double sum_col = sumCol(m, i);
126 if(!epsilonEqualRelative(sum_col, tVol[i], VOL_PREC))
128 LOG(1, "Target volume inconsistent : vol of cell " << i << " = " << tVol[i] << " but the col sum is " << sum_col);
131 LOG(1, "diff = " <<sum_col - tVol[i] );
139 bool Interpolation3DTest::areCompatitable(const IntersectionMatrix& m1, const IntersectionMatrix& m2) const
141 bool compatitable = true;
143 for(IntersectionMatrix::const_iterator iter = m1.begin() ; iter != m1.end() ; ++iter)
145 for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
147 int j = iter2->first;
148 if(m2.at(j-1).count(i+1) == 0)
150 if(!epsilonEqual(iter2->second, 0.0, VOL_PREC))
152 LOG(2, "V1( " << i << ", " << j << ") exists, but V2( " << j - 1 << ", " << i + 1 << ") " << " does not " );
153 LOG(2, "(" << i << ", " << j << ") fails");
154 compatitable = false;
162 LOG(1, "*** matrices are not compatitable");
167 bool Interpolation3DTest::testSymmetric(const IntersectionMatrix& m1, const IntersectionMatrix& m2) const
171 bool isSymmetric = true;
173 LOG(1, "Checking symmetry src - target" );
174 isSymmetric = isSymmetric & areCompatitable(m1, m2) ;
175 LOG(1, "Checking symmetry target - src" );
176 isSymmetric = isSymmetric & areCompatitable(m2, m1);
178 for(IntersectionMatrix::const_iterator iter = m1.begin() ; iter != m1.end() ; ++iter)
180 for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
182 int j = iter2->first;
183 const double v1 = iter2->second;
184 //if(m2[j - 1].count(i+1) > 0)
186 std::map<int, double> theMap = m2.at(j-1);
187 const double v2 = theMap[i + 1];
190 LOG(2, "V1( " << i << ", " << j << ") = " << v1 << " which is different from V2( " << j - 1 << ", " << i + 1 << ") = " << v2 << " | diff = " << v1 - v2 );
191 if(!epsilonEqualRelative(v1, v2, VOL_PREC))
193 LOG(2, "(" << i << ", " << j << ") fails");
202 LOG(1, "*** matrices are not symmetric");
207 bool Interpolation3DTest::testDiagonal(const IntersectionMatrix& m) const
209 LOG(1, "Checking if matrix is diagonal" );
211 bool isDiagonal = true;
212 for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
214 for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
216 int j = iter2->first;
217 const double vol = iter2->second;
218 if(vol != 0.0 && (i != j))
220 LOG(2, "V( " << i - 1 << ", " << j << ") = " << vol << " which is not zero" );
221 if(!epsilonEqual(vol, 0.0, VOL_PREC))
223 LOG(2, "(" << i << ", " << j << ") fails");
232 LOG(1, "*** matrix is not diagonal");
237 void Interpolation3DTest::dumpIntersectionMatrix(const IntersectionMatrix& m) const
240 std::cout << "Intersection matrix is " << std::endl;
241 for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
243 for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
246 std::cout << "V(" << i << ", " << iter2->first << ") = " << iter2->second << std::endl;
251 std::cout << "Sum of volumes = " << sumVolume(m) << std::endl;
254 void Interpolation3DTest::setUp()
256 interpolator = new Interpolation3D();
259 void Interpolation3DTest::tearDown()
264 void Interpolation3DTest::calcIntersectionMatrix(const char* mesh1path, const char* mesh1, const char* mesh2path, const char* mesh2, IntersectionMatrix& m) const
267 if ( getenv("MEDCOUPLING_ROOT_DIR") ) {
268 dataDir = getenv("MEDCOUPLING_ROOT_DIR");
269 dataDir += "/share/resources/med/";
272 dataDir = get_current_dir_name();
273 dataDir += "/../../resources/";
276 LOG(1, std::endl << "=== -> intersecting src = " << mesh1 << ", target = " << mesh2 );
278 LOG(5, "Loading " << mesh1 << " from " << mesh1path);
279 MESH sMesh(MED_DRIVER, dataDir+mesh1path, mesh1);
281 LOG(5, "Loading " << mesh2 << " from " << mesh2path);
282 MESH tMesh(MED_DRIVER, dataDir+mesh2path, mesh2);
284 m = interpolator->interpolateMeshes(sMesh, tMesh);
286 // if reflexive, check volumes
287 if(strcmp(mesh1path,mesh2path) == 0)
289 const bool row_and_col_sums_ok = testVolumes(m, sMesh, tMesh);
290 CPPUNIT_ASSERT_EQUAL_MESSAGE("Row or column sums incorrect", true, row_and_col_sums_ok);
293 LOG(1, "Intersection calculation done. " << std::endl );
297 void Interpolation3DTest::intersectMeshes(const char* mesh1path, const char* mesh1, const char* mesh2path, const char* mesh2, const double correctVol, const double prec, bool doubleTest) const
299 LOG(1, std::endl << std::endl << "=============================" );
302 const string path1 = string(mesh1path) + string(mesh1);
303 const string path2 = string(mesh2path) + string(mesh2);
305 const bool isTestReflexive = (path1.compare(path2) == 0);
307 IntersectionMatrix matrix1;
308 calcIntersectionMatrix(mesh1path, mesh1, mesh2path, mesh2, matrix1);
311 dumpIntersectionMatrix(matrix1);
314 std::cout.precision(16);
316 const double vol1 = sumVolume(matrix1);
320 LOG(1, "vol = " << vol1 <<" correctVol = " << correctVol );
321 CPPUNIT_ASSERT_DOUBLES_EQUAL(correctVol, vol1, prec * std::max(correctVol, vol1));
325 CPPUNIT_ASSERT_EQUAL_MESSAGE("Reflexive test failed", true, testDiagonal(matrix1));
331 IntersectionMatrix matrix2;
332 calcIntersectionMatrix(mesh2path, mesh2, mesh1path, mesh1, matrix2);
335 dumpIntersectionMatrix(matrix2);
338 const double vol2 = sumVolume(matrix2);
340 LOG(1, "vol1 = " << vol1 << ", vol2 = " << vol2 << ", correctVol = " << correctVol );
342 CPPUNIT_ASSERT_DOUBLES_EQUAL(correctVol, vol1, prec * std::max(vol1, correctVol));
343 CPPUNIT_ASSERT_DOUBLES_EQUAL(correctVol, vol2, prec * std::max(vol2, correctVol));
344 CPPUNIT_ASSERT_DOUBLES_EQUAL(vol1, vol2, prec * std::max(vol1, vol2));
345 CPPUNIT_ASSERT_EQUAL_MESSAGE("Symmetry test failed", true, testSymmetric(matrix1, matrix2));