Salome HOME
dada7e03d4bcaf0b6028fad2f36153ca4b789cbd
[tools/medcoupling.git] / src / MEDPartitioner / MEDPARTITIONER_Utils.hxx
1 // Copyright (C) 2007-2015  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 #ifndef __MEDPARTITIONER_UTILS_HXX__
21 #define __MEDPARTITIONER_UTILS_HXX__
22
23 #include "MEDPARTITIONER.hxx"
24
25 #include "MEDCouplingUMesh.hxx"
26 #include "BBTree.txx"
27
28 #include <string>
29 #include <vector>
30 #include <map>
31
32 //# define LOCALIZED(message) #message , __FILE__ , __FUNCTION__ , __LINE__
33
34 namespace MEDPARTITIONER
35 {
36   MEDPARTITIONER_EXPORT std::string Trim(const std::string& s,const std::string& drop);
37   MEDPARTITIONER_EXPORT std::string IntToStr(const int i);
38   MEDPARTITIONER_EXPORT std::string DoubleToStr(const double i);
39   MEDPARTITIONER_EXPORT int StrToInt(const std::string& s);
40   MEDPARTITIONER_EXPORT double StrToDouble(const std::string& s);
41   MEDPARTITIONER_EXPORT bool TestArg(const char *arg, const char *argExpected, std::string& argValue);
42   MEDPARTITIONER_EXPORT std::vector<int> CreateRandomSize(const int size);
43   MEDPARTITIONER_EXPORT void RandomizeAdj(int* xadj, int* adjncy, std::vector<int>& ran, std::vector<int>& vx, std::vector<int>& va);
44   MEDPARTITIONER_EXPORT void TestRandomize();
45                        
46   MEDPARTITIONER_EXPORT std::string ReprVectorOfString(const std::vector<std::string>& vec);
47   MEDPARTITIONER_EXPORT std::string ReprVectorOfString(const std::vector<std::string>& vec, const std::string separator);
48   MEDPARTITIONER_EXPORT std::string ReprMapOfStringInt(const std::map<std::string,int>& mymap);
49   MEDPARTITIONER_EXPORT std::string ReprMapOfStringVectorOfString(const std::map< std::string,std::vector<std::string> >& mymap);
50   MEDPARTITIONER_EXPORT std::string ReprFieldDescriptions(const std::vector<std::string>& vec,const  std::string separator);
51   
52   MEDPARTITIONER_EXPORT std::string SerializeFromString(const std::string& s);
53   MEDPARTITIONER_EXPORT std::string SerializeFromVectorOfString(const std::vector<std::string>& vec);
54   MEDPARTITIONER_EXPORT std::vector<std::string> DeserializeToVectorOfString(const std::string& str);
55   MEDPARTITIONER_EXPORT std::string EraseTagSerialized(const std::string& fromStr, const std::string& tag);
56   
57   MEDPARTITIONER_EXPORT std::vector<std::string> VectorizeFromMapOfStringInt(const std::map<std::string,int>& mymap);
58   MEDPARTITIONER_EXPORT std::map<std::string,int> DevectorizeToMapOfStringInt(const std::vector<std::string>& vec);
59   
60   MEDPARTITIONER_EXPORT std::vector<std::string> VectorizeFromMapOfStringVectorOfString(const std::map< std::string,std::vector<std::string> >& mymap);
61   MEDPARTITIONER_EXPORT std::map< std::string,std::vector<std::string> > DevectorizeToMapOfStringVectorOfString(const std::vector<std::string>& vec);
62   
63   MEDPARTITIONER_EXPORT std::vector<std::string> SelectTagsInVectorOfString(const std::vector<std::string>& vec, const std::string tag);
64   MEDPARTITIONER_EXPORT std::vector<std::string> DeleteDuplicatesInVectorOfString(const std::vector<std::string>& vec);
65   MEDPARTITIONER_EXPORT std::map< std::string,std::vector<std::string> > DeleteDuplicatesInMapOfStringVectorOfString(const std::map< std::string,std::vector<std::string> >& mymap);
66   
67   MEDPARTITIONER_EXPORT std::string Cle1ToStr(const std::string& s, const int inew);
68   MEDPARTITIONER_EXPORT void Cle1ToData(const std::string& cle, std::string& s, int& inew);
69   
70   MEDPARTITIONER_EXPORT std::string Cle2ToStr(const std::string& s,const int inew,const int iold);
71   MEDPARTITIONER_EXPORT void Cle2ToData(const std::string& cle, std::string& s, int& inew, int& iold);
72   
73   MEDPARTITIONER_EXPORT std::string ExtractFromDescription(const std::string& description,const  std::string& tag);
74   MEDPARTITIONER_EXPORT void FieldDescriptionToData(const std::string& description,
75                               int& idomain, std::string& fileName, std::string& meshName, std::string& fieldName,
76                               int& typeField, int& DT, int& IT);
77   MEDPARTITIONER_EXPORT void FieldShortDescriptionToData(const std::string& description,
78                                    std::string& fieldName, int& typeField, int& entity, int& DT, int& IT);
79   
80   MEDCoupling::DataArrayInt *CreateDataArrayIntFromVector(const std::vector<int>& v);
81   MEDCoupling::DataArrayInt *CreateDataArrayIntFromVector(const std::vector<int>& v, const int nbComponents);
82   MEDCoupling::DataArrayDouble *CreateDataArrayDoubleFromVector(const std::vector<double>& v);
83   
84   MEDCoupling::MEDCouplingUMesh *CreateEmptyMEDCouplingUMesh();
85
86   std::vector<std::string> BrowseFieldDouble(const MEDCoupling::MEDCouplingFieldDouble* fd);
87   std::vector<std::string> BrowseAllFields(const std::string& myfile);
88   std::vector<std::string> BrowseAllFieldsOnMesh(const std::string& myfile, const std::string& mymesh, const int idomain);
89   std::vector<std::string> GetInfosOfField(const char *fileName, const char *meshName, const int idomain );
90
91 #ifdef HAVE_MPI
92   //not adviced, interblocking, use sendAndReceive
93   //void SendVectorOfString(const std::vector<std::string>& vec, const int target);
94   //std::vector<std::string> RecvVectorOfString(const int source);
95   //TODO void sendRecvVectorOfString(const std::vector<std::string>& vec, const int source, const int target);
96   MEDPARTITIONER_EXPORT std::vector<std::string> SendAndReceiveVectorOfString(const std::vector<std::string>& vec, const int source, const int target);
97   MEDPARTITIONER_EXPORT std::vector<std::string> AllgathervVectorOfString(const std::vector<std::string>& vec);
98   
99   void SendDoubleVec(const std::vector<double>& vec, const int target);
100   std::vector<double> *RecvDoubleVec(const int source);
101   void RecvDoubleVec(std::vector<double>& vec, const int source);
102     
103   void SendIntVec(const std::vector<int>& vec, const int target);
104   std::vector<int>* RecvIntVec(int source);
105   void RecvIntVec(std::vector<int>& vec, const int source);
106   
107   void SendDataArrayInt(const MEDCoupling::DataArrayInt* da, const int target);
108   MEDCoupling::DataArrayInt *RecvDataArrayInt(const int source);
109   void SendDataArrayDouble(const MEDCoupling::DataArrayDouble* da, const int target);
110   MEDCoupling::DataArrayDouble *RecvDataArrayDouble(const int source);
111
112   void TestVectorOfStringMpi();
113   void TestMapOfStringIntMpi();
114   void TestMapOfStringVectorOfStringMpi();
115   void TestDataArrayMpi();
116   void TestPersistantMpi0To1(int taille, int nb);
117   void TestPersistantMpiRing(int taille, int nb);
118   void TestPersistantMpiRingOnCommSplit(int taille, int nb);
119 #endif
120
121   class MEDPARTITIONER_EXPORT MyGlobals
122   {
123   public :
124     static int _Verbose;  //0 to 1000 over 200 is debug
125     static int _Rank;
126     static int _World_Size;
127     static int _Randomize;
128     static int _Atomize;
129     static int _Creates_Boundary_Faces;
130     static int _Create_Joints;
131     static int _Is0verbose; //trace cout if rank 0 and verbose
132     static std::vector<std::string> _File_Names;    //on [iold]
133     static std::vector<std::string> _Mesh_Names;    //on [iold]
134     static std::vector<std::string> _Field_Descriptions;
135     /*! used for descriptions of components of fields for example...*/
136     static std::vector<std::string> _General_Informations;
137   };
138
139
140
141   /*!
142    * \brief Class encapsulating BBTree of dimension given at construction and
143    *        providing all features of BBTree
144    */
145   class BBTreeOfDim
146   {
147     void * _tree;
148     void (BBTreeOfDim::*_PgetElementsAroundPoint)( const double* coordsPtr,
149                                                    std::vector<int>& elems ) const;
150     void (BBTreeOfDim::*_PgetIntersectingElems)( const double* bb,
151                                                  std::vector<int>& elems ) const;
152
153     template< int dim>
154     void _getElementsAroundPoint( const double* coordsPtr,
155                                   std::vector<int>& elems ) const
156     {
157       ((BBTree<dim,int>*)_tree)->getElementsAroundPoint( coordsPtr, elems );
158     }
159     template< int dim>
160     void _getIntersectingElems(const double* bb,
161                                std::vector<int>& elems) const
162     {
163       ((BBTree<dim,int>*)_tree)->getIntersectingElems( bb, elems );
164     }
165   public:
166
167     BBTreeOfDim( int           dim,
168                  const double* bbs,
169                  int*          elems,
170                  int           level,
171                  int           nbelems,
172                  double        epsilon=1e-12);
173     ~BBTreeOfDim();
174     void getElementsAroundPoint(const double* coordsPtr, std::vector<int>& elems ) const;
175     void getIntersectingElems  (const double* bb,        std::vector<int>& elems)  const;
176   };
177 }
178 #endif