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