Salome HOME
Corrected missing data in copy constructor
[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 ids
264          * @return _faceGroupsIds
265          */
266         std::vector< std::vector<int> > getFaceGroups( void )  const ;
267
268         /**
269          * \brief return the list of node groups Ids
270          * @return _nodeGroupsIds
271          */
272         std::vector< std::vector<int> > getNodeGroups( void )  const ;
273
274         /**
275          * \brief return the list of face groups
276          * @return _faceGroups
277          */
278         std::vector<MEDCoupling::MEDCouplingUMesh *> getMEDCouplingFaceGroups( void )  const ;
279
280         /**
281          * \brief return the list of node groups
282          * @return _nodeGroups
283          */
284         std::vector<MEDCoupling::DataArrayIdType *> getMEDCouplingNodeGroups( void )  const ;
285
286     /**
287      * \brief Functions to extract boundary nodes and faces Ids
288      */
289      /**
290       *  \brief return the list of boundary faces Ids
291       *  @return _boundaryFaceIds
292       */
293     std::vector< int > getBoundaryFaceIds() const;
294     /**
295      * \brief list of boundary nodes Ids
296      * @return _boundaryNodeIds
297      */
298     std::vector< int > getBoundaryNodeIds() const;
299     /**
300      * \brief Functions to extract group nodes and faces ids
301      */
302      /** 
303       * @return list of face group Ids
304       */
305     std::vector< int > getFaceGroupIds(std::string groupName, bool isBoundaryGroup=true) const;
306     /**
307      * @return list of node group Ids
308      * */
309     std::vector< int > getNodeGroupIds(std::string groupName, bool isBoundaryGroup=true) const;
310  
311         /**
312          * \brief write mesh in the VTK format
313          */
314         void writeVTK ( const std::string fileName ) const ;
315
316         /**
317          * \brief write mesh in the MED format
318          */
319         void writeMED ( const std::string fileName, bool fromScratch = true ) const;
320
321         void setGroupAtPlan(double value, int direction, double eps, std::string groupName, bool isBoundaryGroup=true) ;
322
323         void setGroupAtFaceByCoords(double x, double y, double z, double eps, std::string groupName, bool isBoundaryGroup=true) ;
324
325         void setGroupAtNodeByCoords(double x, double y, double z, double eps, std::string groupName, bool isBoundaryGroup=true) ;
326
327         void setFaceGroupByIds(std::vector< int > faceIds, std::string groupName) ;
328
329         void setNodeGroupByIds(std::vector< int > nodeIds, std::string groupName) ;
330
331         /*
332      * Functions to manage periodic boundary condition in square/cubic geometries 
333      */
334     void setPeriodicFaces(bool check_groups= false, bool use_central_inversion=false) ;
335     int getIndexFacePeriodic(int indexFace, bool check_groups= false, bool use_central_inversion=false);
336     void setBoundaryNodesFromFaces();
337     std::map<int,int> getIndexFacePeriodic( void ) const;
338     bool isIndexFacePeriodicSet() const ;
339     
340         bool isBorderNode(int nodeid) const ;
341         bool isBorderFace(int faceid) const ;
342         
343         bool isTriangular() const ;
344         bool isTetrahedral() const ;
345         bool isQuadrangular() const ;
346         bool isHexahedral() const ;
347     bool isStructured() const ;
348
349         // epsilon used in mesh comparisons
350         double getComparisonEpsilon() const {return _epsilon;};
351         void setComparisonEpsilon(double epsilon){ _epsilon=epsilon;};
352     // Quick comparison of two meshes to see if they are identical with high probability (three cells are compared)
353     void checkFastEquivalWith( Mesh m) const { return getMEDCouplingMesh()->checkFastEquivalWith(m.getMEDCouplingMesh(),_epsilon);};
354     // Deep comparison of two meshes to see if they are identical Except for their names and units
355     bool isEqualWithoutConsideringStr( Mesh m) const { return getMEDCouplingMesh()->isEqualWithoutConsideringStr(m.getMEDCouplingMesh(),_epsilon);};
356
357     std::vector< std::string > getElementTypesNames() const ;
358         /**
359          * \brief Compute the minimum value over all cells of the ratio cell perimeter/cell volume
360          */
361     double minRatioVolSurf() const;
362     
363         /**
364          * \brief Compute the maximum number of neighbours around an element (cells around a cell or nodes around a node)
365          */
366     int getMaxNbNeighbours(EntityType type) const;
367     
368     /** 
369      * \brief Delete the medcoupling mesh to save memory space
370      */
371     void deleteMEDCouplingMesh();
372     
373     /** 
374      * \brief Returns true iff an unstructured mesh has been loaded
375      */
376      bool meshNotDeleted() const {return _meshNotDeleted;}
377     
378 private: //----------------------------------------------------------------
379
380         MEDCoupling::MEDCouplingUMesh*  setMesh( void ) ;
381         void setFaceGroups( const MEDCoupling::MEDFileUMesh* medmesh, MEDCoupling::MEDCouplingUMesh*  mu) ;//Read all face groups
382         void setNodeGroups( const MEDCoupling::MEDFileMesh*  medmesh, MEDCoupling::MEDCouplingUMesh*  mu) ;//Read all node groups
383         void addNewFaceGroup( const MEDCoupling::MEDCouplingUMesh *m);//adds one face group in the vectors _faceGroups, _faceGroupNames and _faceGroupIds
384         
385         /**
386          * \brief The MEDCoupling mesh
387          */
388         MEDCoupling::MCAuto<MEDCoupling::MEDCouplingMesh> _mesh;// This is either a MEDCouplingUMesh or a MEDCouplingStructuredMesh
389
390         bool _meshNotDeleted;
391         
392     std::string _name;
393     
394         /**
395          * \brief Space dimension
396          */
397         int _spaceDim ;
398
399         /**
400          * \brief Mesh dimension
401          */
402         int _meshDim ;
403     
404     /**
405      * \brief Signal a structured mesh
406      */
407         bool _isStructured;
408     /**
409      * \brief Number of cells in each direction (Structured meshes)
410      */
411         std::vector<mcIdType> _nxyz;
412
413         /**
414          * \brief The nodes in this mesh.
415          */
416         std::shared_ptr<Node> _nodes;
417
418         /**
419          * \brief The number of nodes in this mesh.
420          */
421         int _numberOfNodes;
422
423         /**
424          * \brief The faces in this mesh.
425          */
426         std::shared_ptr<Face> _faces;
427
428         /**
429          * \brief The numbers of faces in this mesh.
430          */
431         int _numberOfFaces;
432
433         /**
434          * \brief The cells in this mesh.
435          */
436         std::shared_ptr<Cell> _cells;
437
438         /**
439          * \brief The number of cells in this mesh.
440          */
441         int _numberOfCells;
442
443         /**
444          * \brief return The nodes in this mesh
445          * @return _nodes
446          */
447         std::shared_ptr<Node> getNodes ( void ) const ;
448
449         /**
450          * \brief return The cells in this mesh
451          * @return _vertices
452          */
453         std::shared_ptr<Cell> getCells ( void ) const ;
454
455         /**
456          * \brief return the faces in this mesh
457          * @return _vertices
458          */
459         std::shared_ptr<Face> getFaces ( void ) const ;
460
461         /**
462          * \brief The number of edges in this mesh.
463          */
464         int _numberOfEdges;//Useful to deduce the number of non zero coefficients in a finite element matrix 
465
466         /**
467          * \brief The names of face groups.
468          */
469         std::vector<std::string> _faceGroupNames;
470
471         /**
472          * \brief The names of node groups.
473          */
474         std::vector<std::string> _nodeGroupNames;
475
476         /**
477          * \brief The list of face groups.
478          */
479         std::vector<MEDCoupling::MEDCouplingUMesh *> _faceGroups;
480         /**
481          * \brief The list of node groups.
482          */
483         std::vector<MEDCoupling::DataArrayIdType *> _nodeGroups;
484         
485         /**
486          * \brief The list of face id in each face groups.
487          */
488         std::vector< std::vector<int> > _faceGroupsIds;
489         
490         /**
491          * \brief The list of node id in each node groups.
492          */
493         std::vector< std::vector<int> > _nodeGroupsIds;
494         
495         /**
496          * \brief Elements types (SEG2, TRI3, QUAD4, HEXA6 ...)
497          */
498         std::vector< INTERP_KERNEL::NormalizedCellType > _eltsTypes;//List of cell types contained in the mesh
499         std::vector< std::string > _eltsTypesNames;//List of cell types contained in the mesh
500     std::vector< INTERP_KERNEL::NormalizedCellType > getElementTypes() const;    
501     
502     /**
503      * \brief Tools to manage periodic boundary conditions in square/cube geometries
504      */
505      bool _indexFacePeriodicSet;
506      std::map<int,int> _indexFacePeriodicMap;
507     
508     /* List of boundary faces*/
509     std::vector< int > _boundaryFaceIds;
510     /* List of boundary nodes*/
511     std::vector< int > _boundaryNodeIds;
512     /* Boundary mesh */
513     MEDCoupling::MEDCouplingUMesh * _boundaryMesh;
514     
515     double _epsilon;
516 };
517
518 #endif /* MESH_HXX_ */