]> SALOME platform Git repositories - tools/solverlab.git/blob - CDMATH/mesh/inc/Mesh.hxx
Salome HOME
Improved Mesh class, especially when meshDim=1 (graphs)
[tools/solverlab.git] / CDMATH / mesh / inc / Mesh.hxx
1 /*
2  * mesh.hxx
3  *
4  *  Created on: 22 janv. 2012
5  *      Authors: CDMAT
6  */
7
8 #ifndef MESH_HXX_
9 #define MESH_HXX_
10
11 #include "MEDCouplingUMesh.hxx"
12 #include "MEDCouplingIMesh.hxx"
13 #include "MEDCouplingFieldDouble.hxx"
14
15 /**
16  * Mesh class is defined by
17  * - case 1: file name of mesh med file (general unstructured)
18  * - case 2: 1D cartesian, xmin and xmax and number of cells
19  * - case 3: 2D cartesian, xmin, xmax, ymin and ymax and numbers of cells in x direction and y direction
20  * - case 4: 3D cartesian, xmin, xmax, ymin, ymax, zmin and zmax and numbers of cells in x direction, y direction and z direction
21  * - case 5: 2D regular triangular mesh
22  * - case 6: 3D regular hexahedral mesh
23  * - case 7: 1D unstructured
24  */
25
26 namespace MEDCoupling
27 {
28 class MEDFileUMesh;
29 class MEDCouplingMesh;
30 class MEDCouplingIMesh;
31 class MEDCouplingUMesh;
32 class DataArrayIdType;
33 }
34 namespace ParaMEDMEM
35 {
36 class DataArrayIdType;
37 }
38 #include <MCAuto.hxx>
39 #include "NormalizedGeometricTypes"
40
41 class Node;
42 class Cell;
43 class Face;
44
45 typedef enum
46   {
47     CELLS = 0,
48     NODES = 1,
49     FACES = 2,
50   } EntityType;
51
52 #include <vector>
53 #include <string>
54 #include <map>
55
56 class Mesh
57 {
58
59 public: //----------------------------------------------------------------
60         /**
61          * \brief default constructor
62          */
63         Mesh ( void ) ;
64
65         /**
66          * \brief constructor with data to load a general unstructured mesh
67          * @param filename name of mesh file
68          * @param meshLevel : relative mesh dimension : 0->cells, 1->Faces etc
69          */
70         Mesh ( const std::string filename, int meshLevel=0 ) ;
71
72         /**
73          * \brief constructor with data for a regular 1D grid 
74          * @param xmin : minimum x
75          * @param xmax : maximum x
76          * @param nx : Number of cells in x direction
77          */
78         Mesh( double xmin, double xmax, int nx, std::string meshName="MESH1D_Regular_Grid" ) ;
79
80         /**
81          * \brief constructor with data for an unstructured 1D mesh
82          * @param points : abscissas of the mesh nodes
83          */
84         Mesh( std::vector<double> points, std::string meshName="MESH1D_unstructured" ) ;
85
86         /**
87          * \brief constructor with data for a regular 2D grid 
88          * @param xmin : minimum x
89          * @param xmax : maximum x
90          * @param ymin : minimum y
91          * @param ymax : maximum y
92          * @param nx : Number of cells in x direction
93          * @param ny : Number of cells in y direction
94      * @param split_to_triangles_policy : each rectangle will be split into 2 triangles with orientation of the cut depending if value is 0 or 1
95          */
96         Mesh( double xmin, double xmax, int nx, double ymin, double ymax, int ny, int split_to_triangles_policy=-1, std::string meshName="MESH2D_Regular_Rectangle_Grid") ;
97
98         /**
99          * \brief constructor with data for a regular 3D grid 
100          * @param xmin : minimum x
101          * @param xmax : maximum x
102          * @param ymin : minimum y
103          * @param ymax : maximum y
104          * @param zmin : minimum z
105          * @param zmax : maximum z
106          * @param nx : Number of cells in x direction
107          * @param ny : Number of cells in y direction
108          * @param nz : Number of cells in z direction
109      * @param split_to_tetrahedra_policy : each cuboid will be split into 5 tetrahedra if value is 0 or 6 tetrahedra if the value is 1
110          */
111         Mesh( double xmin, double xmax, int nx, double ymin, double ymax, int ny, double zmin, double zmax, int nz, int split_to_tetrahedra_policy=-1, std::string meshName="MESH3D_Regular_Cuboid_Grid") ;
112
113         Mesh( const MEDCoupling::MEDCouplingIMesh* mesh ) ;
114         Mesh( const MEDCoupling::MEDCouplingUMesh* mesh ) ;
115
116         /**
117          * \brief constructor with data
118          * @param filename : file name of mesh med file
119          * @param meshLevel : relative mesh dimension : 0->cells, 1->Faces etc
120          */
121         void readMeshMed( const std::string filename, int meshLevel=0 ) ;
122
123         /**
124          * \brief constructor by copy
125          * @param mesh : The Mesh object to be copied
126          */
127         Mesh ( const Mesh & mesh ) ;
128
129         /**
130          * \brief destructor
131          */
132         ~Mesh( void ) ;
133
134         /**
135          * \brief return mesh name
136          * @return _name
137          */
138         std::string getName( void ) const ;
139
140         /**
141          * \brief return Space dimension
142          * @return _spaceDim
143          */
144         int getSpaceDimension( void ) const ;
145
146         /**
147          * \brief Mesh dimension
148          * @return _meshDim
149          */
150         int getMeshDimension( void ) const ;
151
152         /**
153          * \brief return The nodes in this mesh
154          * @return _nodes
155          */
156         Node* getNodes ( void ) const ;
157
158         /**
159          * \brief return The cells in this mesh
160          * @return _vertices
161          */
162         Cell* getCells ( void ) const ;
163
164         /**
165          * \brief return the faces in this mesh
166          * @return _vertices
167          */
168         Face* getFaces ( void ) const ;
169
170         /**
171          * \brief return the number of nodes in this mesh
172          * @return _numberOfNodes
173          */
174         int getNumberOfNodes ( void )  const ;
175
176         /**
177          * \brief return the number of faces in this mesh
178          * @return _numberOfFaces
179          */
180         int getNumberOfFaces ( void )  const ;
181
182         /**
183          * \brief return the number of cells in this mesh
184          * @return _numberOfCells
185          */
186         int getNumberOfCells ( void )  const ;
187
188         /**
189          * \brief return the number of edges in this mesh
190          * @return _numberOfEdges
191          */
192         int getNumberOfEdges ( void )  const ;
193
194         /**
195          * \brief return the cell i in this mesh
196          * @return _cells[i]
197          */
198         Cell& getCell ( int i )  const ;
199
200         /**
201          * return The face i in this mesh
202          * @return _faces[i]
203          */
204         Face& getFace ( int i )  const ;
205
206         /**
207          * \brief return The node i in this mesh
208          * @return _nodes[i]
209          */
210         Node& getNode ( int i )  const ;
211
212         /**
213          * \brief return number of cell in x direction (structured mesh)
214          * @return _nX
215          */
216         int getNx( void )  const ;
217
218         /**
219          * \brief return number of cell in y direction (structured mesh)
220          * @return _nY
221          */
222         int getNy( void )  const ;
223
224         /**
225          * \brief return number of cell in z direction (structured mesh)
226          * @return _nZ
227          */
228         int getNz( void )  const ;
229
230         double getXMin( void )  const ;
231
232         double getXMax( void )  const ;
233
234         double getYMin( void )  const ;
235
236         double getYMax( void )  const ;
237
238         double getZMin( void )  const ;
239
240         double getZMax( void )  const ;
241
242         std::vector<double> getDXYZ() const ;// for structured meshes
243
244         std::vector<mcIdType> getCellGridStructure() const;// for structured meshes
245
246         /**
247          * \brief surcharge operator =
248          * @param mesh : The Mesh object to be copied
249          */
250         const Mesh& operator= ( const Mesh& mesh ) ;
251
252         /**
253          * \brief return the mesh MEDCoupling
254          * @return _mesh
255          */
256         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingMesh> getMEDCouplingMesh ( void )  const ;
257
258         /**
259          * \brief return the skin surrounding the mesh
260          */
261         Mesh getBoundaryMesh ( void )  const ;
262
263         /**
264          * \brief return a group surrounding the mesh
265          */
266         Mesh getBoundaryGroupMesh ( std::string groupName, int nth_group_match = 0 )  const ;
267
268         /**
269          * \brief return the list of face group names
270          * return _faceGroupNames
271          */
272         std::vector<std::string> getNameOfFaceGroups( void )  const ;
273
274         /**
275          * \brief return the list of node group names
276          * return _nodeGroupNames
277          */
278         std::vector<std::string> getNameOfNodeGroups( void )  const ;
279
280         /**
281          * \brief return the list of face groups
282          * @return _faceGroups
283          */
284         std::vector<MEDCoupling::MEDCouplingUMesh *> getFaceGroups( void )  const ;
285
286         /**
287          * \brief return the list of node groups
288          * @return _nodeGroups
289          */
290         std::vector<MEDCoupling::DataArrayIdType *> getNodeGroups( void )  const ;
291
292     /*
293      * Functions to extract boundary nodes and faces Ids
294      */
295      /**
296       *  \brief return the list of boundary faces Ids
297       *  @return _boundaryFaceIds
298       */
299     std::vector< int > getBoundaryFaceIds() const;
300     /**
301      * \brief list of boundary nodes Ids
302      * @return _boundaryNodeIds
303      */
304     std::vector< int > getBoundaryNodeIds() const;
305     /*
306      * Functions to extract group nodes and faces ids
307      */
308      /** 
309       * @return list of face group Ids
310       */
311     std::vector< int > getFaceGroupIds(std::string groupName, bool isBoundaryGroup=true) const;
312     /**
313      * @return list of node group Ids
314      * */
315     std::vector< int > getNodeGroupIds(std::string groupName, bool isBoundaryGroup=true) const;
316  
317         /**
318          * \brief write mesh in the VTK format
319          */
320         void writeVTK ( const std::string fileName ) const ;
321
322         /**
323          * \brief write mesh in the MED format
324          */
325         void writeMED ( const std::string fileName ) const ;
326
327         void setGroupAtPlan(double value, int direction, double eps, std::string groupName, bool isBoundaryGroup=true) ;
328
329         void setGroupAtFaceByCoords(double x, double y, double z, double eps, std::string groupName, bool isBoundaryGroup=true) ;
330
331         void setGroupAtNodeByCoords(double x, double y, double z, double eps, std::string groupName, bool isBoundaryGroup=true) ;
332
333         void setFaceGroupByIds(std::vector< int > faceIds, std::string groupName) ;
334
335         void setNodeGroupByIds(std::vector< int > nodeIds, std::string groupName) ;
336
337         /*
338      * Functions to manage periodic boundary condition in square/cubic geometries 
339      */
340     void setPeriodicFaces(bool check_groups= false, bool use_central_inversion=false) ;
341     int getIndexFacePeriodic(int indexFace, bool check_groups= false, bool use_central_inversion=false);
342     void setBoundaryNodesFromFaces();
343     std::map<int,int> getIndexFacePeriodic( void ) const;
344     bool isIndexFacePeriodicSet() const ;
345     
346         bool isBorderNode(int nodeid) const ;
347         bool isBorderFace(int faceid) const ;
348         
349         bool isTriangular() const ;
350         bool isTetrahedral() const ;
351         bool isQuadrangular() const ;
352         bool isHexahedral() const ;
353     bool isStructured() const ;
354
355         // epsilon used in mesh comparisons
356         double getComparisonEpsilon() const {return _epsilon;};
357         void setComparisonEpsilon(double epsilon){ _epsilon=epsilon;};
358     // Quick comparison of two meshes to see if they are identical with high probability (three cells are compared)
359     void checkFastEquivalWith( Mesh m) const { return getMEDCouplingMesh()->checkFastEquivalWith(m.getMEDCouplingMesh(),_epsilon);};
360     // Deep comparison of two meshes to see if they are identical Except for their names and units
361     bool isEqualWithoutConsideringStr( Mesh m) const { return getMEDCouplingMesh()->isEqualWithoutConsideringStr(m.getMEDCouplingMesh(),_epsilon);};
362
363     std::vector< std::string > getElementTypesNames() const ;
364         /**
365          * \brief Compute the minimum value over all cells of the ratio cell perimeter/cell vaolume
366          */
367     double minRatioVolSurf() const;
368     
369         /**
370          * \brief Compute the maximum number of neighbours around an element (cells around a cell or nodes around a node)
371          */
372     int getMaxNbNeighbours(EntityType type) const;
373     
374     /** 
375      * \brief Delete the medcoupling mesh to save memory space
376      */
377     void deleteMEDCouplingUMesh();
378     
379     /** 
380      * \brief Returns true iff an unstructured mesh has been loaded
381      */
382      bool meshNotDeleted() const {return _meshNotDeleted;}
383     
384 private: //----------------------------------------------------------------
385
386         MEDCoupling::MEDCouplingUMesh*  setMesh( void ) ;
387         void setGroups( const MEDCoupling::MEDFileUMesh* medmesh, MEDCoupling::MEDCouplingUMesh*  mu) ;//Read all face and node group
388         void addNewFaceGroup( const MEDCoupling::MEDCouplingUMesh *m);//adds one face group in the vectors _faceGroups, _faceGroupNames and _faceGroupIds
389         
390         /*
391          * The MEDCoupling mesh
392          */
393         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingMesh> _mesh;
394
395         bool _meshNotDeleted;
396         
397     std::string _name;
398     
399         /**
400          * \brief Space dimension
401          */
402         int _spaceDim ;
403
404         /**
405          * \brief Mesh dimension
406          */
407         int _meshDim ;
408     
409     /*
410      * Structured mesh parameters
411      */
412
413     bool _isStructured;
414     
415         double _xMin;
416
417         double _xMax;
418
419         double _yMin;
420
421         double _yMax;
422
423         double _zMin;
424
425         double _zMax;
426
427         std::vector<mcIdType> _nxyz;
428
429         std::vector<double> _dxyz;
430         /*
431          * The nodes in this mesh.
432          */
433         Node *_nodes;
434
435         /*
436          * The number of nodes in this mesh.
437          */
438         int _numberOfNodes;
439
440         /*
441          * The faces in this mesh.
442          */
443         Face *_faces;
444
445         /*
446          * The numbers of faces in this mesh.
447          */
448         int _numberOfFaces;
449
450         /*
451          * The cells in this mesh.
452          */
453         Cell *_cells;
454
455         /*
456          * The number of cells in this mesh.
457          */
458         int _numberOfCells;
459
460         /*
461          * The number of edges in this mesh.
462          */
463         int _numberOfEdges;//Useful to deduce the number of non zero coefficients in the finite element matrix 
464
465         /*
466          * The names of face groups.
467          */
468         std::vector<std::string> _faceGroupNames;
469
470         /*
471          * The names of node groups.
472          */
473         std::vector<std::string> _nodeGroupNames;
474
475         /*
476          * The list of face groups.
477          */
478         std::vector<MEDCoupling::MEDCouplingUMesh *> _faceGroups;
479         /*
480          * The list of node groups.
481          */
482         std::vector<MEDCoupling::DataArrayIdType *> _nodeGroups;
483         
484         /*
485          * The list of face id in each face groups.
486          */
487         std::vector< std::vector<int> > _faceGroupsIds;
488         
489         /*
490          * The list of node id in each node groups.
491          */
492         std::vector< std::vector<int> > _nodeGroupsIds;
493         
494         /*
495          * Elements types (SEG2, TRI3, QUAD4, HEXA6 ...)
496          */
497         std::vector< INTERP_KERNEL::NormalizedCellType > _eltsTypes;//List of cell types contained in the mesh
498         std::vector< std::string > _eltsTypesNames;//List of cell types contained in the mesh
499     std::vector< INTERP_KERNEL::NormalizedCellType > getElementTypes() const;    
500     
501     /*
502      * Tools to manage periodic boundary conditions in square/cube geometries
503      */
504      bool _indexFacePeriodicSet;
505      std::map<int,int> _indexFacePeriodicMap;
506     
507     /* List of boundary faces*/
508     std::vector< int > _boundaryFaceIds;
509     /* List of boundary nodes*/
510     std::vector< int > _boundaryNodeIds;
511     /* Boundary mesh */
512     MEDCoupling::MEDCouplingUMesh * _boundaryMesh;
513     
514     double _epsilon;
515 };
516
517 #endif /* MESH_HXX_ */