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