Salome HOME
c7cdab566853ba1938a306811dbf31e1024ae913
[tools/medcoupling.git] / src / INTERP_KERNELTest / PerfTest.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "Interpolation3D.hxx"
21 #include "Interpolation3D.txx"
22 #include "MeshTestToolkit.txx"
23 #include "Log.hxx"
24 #include "VectorUtils.hxx"
25 #include "TestInterpKernelUtils.hxx"
26
27 #include "MEDCouplingNormalizedUnstructuredMesh.hxx"
28
29 #include <cassert>
30 #include <string>
31
32 /**
33  * \file PerfTest.cxx
34  * Test program which takes two meshes and calculates their intersection matrix.
35  *
36  * USAGE : PerfTest mesh1 mesh2
37  *         where mesh1 and mesh2 are the names of two meshes located in
38  *         the files mesh1.med, mesh2.med in {$MEDCOUPLING_ROOT_DIR}/share/resources/med/
39  *
40  */
41
42 namespace INTERP_TEST
43 {
44   /**
45    * \brief Specialization of MeshTestToolkit for the purposes of performance testing.
46    *
47    */
48   class PerfTestToolkit : public MeshTestToolkit<3,3>
49   {
50
51   public:
52
53     /**
54      * Calculates the intersection matrix for two meshes.
55      * Outputs the names of the meshes intersected, the number of elements in each mesh,
56      * the number of matrix elements and the number of non-zero matrix elements, etc.
57      * These values help to determine how well the filtering algorithm is working.
58      *
59      * @param  mesh1path   the path to the file containing the source mesh, relative to {$MEDCOUPLING_ROOT_DIR}/share/resources/med/
60      * @param  mesh1       the name of the source mesh
61      * @param  mesh2path   the path to the file containing the target mesh, relative to {$MEDCOUPLING_ROOT_DIR}/share/resources/med/
62      * @param  mesh2       the name of the target mesh
63      * @param  m           intersection matrix in which to store the result of the intersection
64      */
65     void calcIntersectionMatrix(const char* mesh1path, const char* mesh1, const char* mesh2path, const char* mesh2, IntersectionMatrix& m)
66     {
67       LOG(1, std::endl << "=== -> intersecting src = " << mesh1 << ", target = " << mesh2 );
68
69       LOG(5, "Loading " << mesh1 << " from " << mesh1path);
70       MCAuto<MEDFileUMesh> sMeshML=MEDFileUMesh::New(INTERP_TEST::getResourceFile(mesh1path).c_str(),mesh1);
71       MCAuto<MEDCouplingUMesh> sMesh=sMeshML->getMeshAtLevel(0);
72
73
74       LOG(5, "Loading " << mesh2 << " from " << mesh2path);
75       MCAuto<MEDFileUMesh> tMeshML=MEDFileUMesh::New(INTERP_TEST::getResourceFile(mesh2path).c_str(),mesh2);
76       MCAuto<MEDCouplingUMesh> tMesh=tMeshML->getMeshAtLevel(0);
77
78       MEDCouplingNormalizedUnstructuredMesh<3,3> sMesh_wrapper(sMesh);
79       MEDCouplingNormalizedUnstructuredMesh<3,3> tMesh_wrapper(tMesh);
80
81       Interpolation3D interpolator;
82       interpolator.interpolateMeshes(sMesh_wrapper, tMesh_wrapper,m,"P0P0");
83
84       std::pair<int, int> eff = countNumberOfMatrixEntries(m);
85 //      LOG(1, eff.first << " of " << numTargetElems * numSrcElems << " intersections calculated : ratio = "
86 //          << double(eff.first) / double(numTargetElems * numSrcElems));
87       LOG(1, eff.second << " non-zero elements of " << eff.first << " total : filter efficiency = "
88           << double(eff.second) / double(eff.first));
89
90       LOG(1, "Intersection calculation done. " << std::endl );
91
92     }
93
94     /**
95      * Counts the number of elements in an intersection matrix, and the number of these which are non-zero.
96      *
97      * @param m  the intersection matrix
98      * @return  pair<int, int> containing as its first element the number of elements in m and as its second element the
99      *                         number these which are non-zero
100      */
101     std::pair<int,int> countNumberOfMatrixEntries(const IntersectionMatrix& m)
102     {
103
104       int numElems = 0;
105       int numNonZero = 0;
106       for(IntersectionMatrix::const_iterator iter = m.begin() ; iter != m.end() ; ++iter)
107         {
108           numElems += iter->size();
109           for(std::map<int, double>::const_iterator iter2 = iter->begin() ; iter2 != iter->end() ; ++iter2)
110             {
111               if(!INTERP_KERNEL::epsilonEqual(iter2->second, 0.0, VOL_PREC))
112                 {
113                   ++numNonZero;
114                 }
115             }
116         }
117       return std::make_pair(numElems, numNonZero);
118     }
119
120   };
121 }
122
123 /**
124  * Main method of the program.
125  * Intersects the meshes and outputs some information about the calculation as well as the
126  * intersection matrix on std::cout.
127  *
128  * @param argc  number of arguments given to the program (should be 3, the user giving 2 mesh names)
129  * @param argv  vector to the arguments as strings.
130  */
131 int main(int argc, char** argv)
132 {
133   using INTERP_TEST::PerfTestToolkit;
134
135   assert(argc == 3);
136
137   // load meshes
138   const std::string mesh1 = argv[1];
139   const std::string mesh2 = argv[2];
140
141   const std::string mesh1path = mesh1 + ".med";
142   const std::string mesh2path = mesh2 + ".med";
143
144   IntersectionMatrix m;
145
146   PerfTestToolkit testTools;
147
148   testTools.calcIntersectionMatrix(mesh1path.c_str(), mesh1.c_str(), mesh2path.c_str(), mesh2.c_str(), m);
149
150   testTools.dumpIntersectionMatrix(m);
151
152   return 0;
153
154 }
155