LifeV
MeshLoadingUtility.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3  *******************************************************************************
4 
5  Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6  Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8  This file is part of LifeV.
9 
10  LifeV is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  LifeV is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23  *******************************************************************************
24  */
25 //@HEADER
26 
27 /*!
28  @file
29  @brief Base utilities operating on meshes
30 
31  @contributor Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
32  @maintainer Simone Rossi <simone.rossi@epfl.ch>
33 
34  This file contains a set of base utilities used to test mesh entities or
35  operate on them
36  */
37 
38 #ifndef MESHLOADINGUTILITY_H
39 #define MESHLOADINGUTILITY_H 1
40 
41 #pragma GCC diagnostic ignored "-Wunused-variable"
42 #pragma GCC diagnostic ignored "-Wunused-parameter"
43 
44 #include <algorithm>
45 #include <iterator>
46 
47 #include <boost/numeric/ublas/matrix.hpp>
48 #include <boost/numeric/ublas/io.hpp>
49 
50 #pragma GCC diagnostic warning "-Wunused-variable"
51 #pragma GCC diagnostic warning "-Wunused-parameter"
52 
53 
54 #include <lifev/core/util/LifeChrono.hpp>
55 #include <lifev/core/util/Displayer.hpp>
56 #include <lifev/core/mesh/MeshPartitioner.hpp>
57 #include <lifev/core/filter/PartitionIO.hpp>
58 #include <lifev/core/mesh/MeshUtility.hpp>
59 #include <lifev/core/mesh/RegionMesh3DStructured.hpp>
60 #include <lifev/core/mesh/MeshData.hpp>
61 #include <lifev/core/mesh/RegionMesh.hpp>
62 
63 
64 namespace LifeV
65 {
66 
67 
68 namespace MeshUtility
69 {
70 
71 //! Print informations about the mesh
72 /*!
73  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
74  */
75 template <typename RegionMeshType>
76 void printMeshInfos ( std::shared_ptr<RegionMeshType > mesh )
77 {
78 #ifdef HAVE_MPI
79  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
80 #else
81  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_SerialComm );
82 #endif
83  Displayer displayer ( Comm );
84  MeshUtility::MeshStatistics::meshSize meshSize = MeshUtility::MeshStatistics::computeSize ( *mesh );
85  displayer.leaderPrint ( "Mesh size (max): ", meshSize.maxH, "\n" );
86  displayer.leaderPrint ( "Mesh size (min): ", meshSize.minH, "\n" );
87  displayer.leaderPrint ( "Mesh size (av.): ", meshSize.meanH, "\n" );
88 }
89 
90 //! setup and get the mesh data
91 /*!
92  @param meshName name of the mesh file
93  @param resourcesPath path to the mesh folder
94  @param meshOrder order of the mesh elements
95 */
96 /*!
97  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
98  */
99 inline MeshData
100 getMeshData ( const std::string& meshName,
101  const std::string& resourcesPath = "./",
102  const std::string& meshOrder = "P1")
103 {
104  MeshData meshData;
105  meshData.setMeshDir ( resourcesPath );
106  meshData.setMeshFile ( meshName );
107  meshData.setMeshType ( ".mesh" );
108  meshData.setMOrder ( meshOrder );
109  meshData.setVerbose ( false );
110  return meshData;
111 }
112 
113 
114 //! Read and partitioned a *.mesh file
115 /*!
116  @param meshLocal The partitioned mesh that we want to generate
117  @param meshFull The non partitioned mesh that we want to keep
118  @param meshName name of the mesh file
119  @param resourcesPath path to the mesh folder
120  @param meshOrder order of the mesh
121 */
122 /*!
123  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
124  */
125 template< typename RegionMeshType>
126 void loadMesh ( std::shared_ptr< RegionMeshType >& meshLocal,
127  std::shared_ptr< RegionMeshType >& meshFull,
128  const std::string& meshName,
129  const std::string& resourcesPath,
130  const std::string& meshOrder )
131 {
132 #ifdef HAVE_MPI
133  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
134 #else
135  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_SerialComm );
136 #endif
137  Displayer displayer ( Comm );
138 
139  LifeChrono meshReadChrono;
140  meshReadChrono.start();
141  std::shared_ptr<RegionMeshType > fullMesh ( new RegionMeshType );
142  readMesh (*fullMesh, getMeshData (meshName, resourcesPath, meshOrder ) );
143  MeshUtility::printMeshInfos ( fullMesh );
144  meshReadChrono.stop();
145  displayer.leaderPrint ("Loading time: ", meshReadChrono.diff(), " s.\n");
146 
147  LifeChrono meshPartChrono;
148  meshPartChrono.start();
149  MeshPartitioner< RegionMeshType > meshPartitioner ( fullMesh, Comm );
150  meshLocal = meshPartitioner.meshPartition();
151  meshPartChrono.stop();
152  displayer.leaderPrint ("Partitioning time: ", meshPartChrono.diff(), " s.\n");
153  if ( meshFull )
154  {
155  meshFull = fullMesh;
156  }
157  else
158  {
159  fullMesh.reset(); //Freeing the global mesh to save memory
160  }
161 }
162 
163 //! Read and partitioned a *.mesh file
164 /*!
165  @param meshLocal The partitioned mesh that we want to generate
166  @param meshName name of the mesh file
167  @param resourcesPath path to the mesh folder
168 */
169 /*!
170  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
171  */
172 template< typename RegionMeshType>
173 void loadPartitionedMesh ( std::shared_ptr< RegionMeshType >& meshLocal,
174  const std::string& meshName,
175  const std::string& resourcesPath )
176 {
177 #ifdef HAVE_MPI
178  std::shared_ptr<Epetra_MpiComm> Comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
179 #else
180  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_SerialComm );
181 #endif
182  Displayer displayer ( Comm );
183 
184  LifeChrono meshReadChrono;
185  meshReadChrono.start();
186 #ifdef LIFEV_HAS_HDF5
187  PartitionIO< RegionMeshType > partitionIO ( ( resourcesPath + meshName ).data(), Comm);
188  partitionIO.read (meshLocal);
189 #else
190  ASSERT (false, "You must compile LifeV with HDF5 to load partitioned meshes");
191 #endif
192  meshReadChrono.stop();
193  displayer.leaderPrint ("Loading time: ", meshReadChrono.diff(), " s.\n");
194 }
195 
196 //! Read and partitioned a *.mesh file
197 /*!
198  @param meshLocal The partitioned mesh that we want to generate
199  @param meshFull The non partitioned mesh that we want to keep
200  @param isPartitioned boolean to say if the mesh should be partitioned or just loaded
201  @param meshName name of the mesh file
202  @param resourcesPath path to the mesh folder
203  @param meshOrder order of the mesh
204 */
205 /*!
206  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
207  */
208 template< typename RegionMeshType>
209 void loadMesh ( std::shared_ptr< RegionMeshType >& meshLocal,
210  std::shared_ptr< RegionMeshType >& meshFull ,
211  const std::string& meshName,
212  const std::string& resourcesPath = "./",
213  bool isPartitioned = false,
214  const std::string& meshOrder = "P1" )
215 {
216  if (isPartitioned)
217  {
218  loadPartitionedMesh ( meshLocal, meshName, resourcesPath );
219  }
220  else
221  {
222  loadMesh ( meshLocal, meshFull, meshName, resourcesPath, meshOrder );
223  }
224 }
225 
226 
227 //! Read and partitioned a *.mesh file
228 /*!
229  @param meshLocal The partitioned mesh that we want to generate
230  @param isPartitioned boolean to say if the mesh should be partitioned or just loaded
231  @param meshName name of the mesh file
232  @param resourcesPath path to the mesh folder
233  @param meshOrder order of the mesh
234 */
235 /*!
236  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
237  */
238 template< typename RegionMeshType>
239 void loadMesh ( std::shared_ptr< RegionMeshType >& meshLocal,
240  const std::string& meshName,
241  const std::string& resourcesPath = "./",
242  bool isPartitioned = false,
243  const std::string& meshOrder = "P1" )
244 {
245  std::shared_ptr< RegionMeshType > tmpMeshFull;
246  loadMesh ( meshLocal, tmpMeshFull, meshName, resourcesPath, isPartitioned, meshOrder );
247 }
248 
249 //! Build a mesh from a partitioned mesh
250 /*!
251  @param mesh The mesh that we want to generate
252  @param regionFlag Flag of the region
253  @param m Number of elements along the ( length, width, height )
254  @param l length of the mesh ( length, width, height )
255  @param t translation of the mesh along the (x,y,z)-axis
256  @param verbose Verbose mode enabled/disabled
257 */
258 /*!
259  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
260  */
261 template< typename RegionMeshType >
262 void loadStructuredMesh ( std::shared_ptr< RegionMeshType >& mesh,
263  std::shared_ptr< RegionMeshType >& meshFull,
264  markerID_Type regionFlag,
265  const std::vector<UInt>& m,
266  bool verbose = false,
267  const std::vector<Real>& l = std::vector<Real> (3, 1),
268  const std::vector<Real>& t = std::vector<Real> (3, 0) )
269 {
270 #ifdef HAVE_MPI
271  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_MpiComm ( MPI_COMM_WORLD ) );
272 #else
273  std::shared_ptr<Epetra_Comm> Comm ( new Epetra_SerialComm );
274 #endif
275  Displayer displayer ( Comm );
276 
277  LifeChrono meshBuildChrono;
278  meshBuildChrono.start();
279  std::shared_ptr< RegionMeshType > fullMesh ( new RegionMeshType ( Comm ) );
280  if ( m.size() == 1)
281  {
282  //TODO structured mesh in 1D
283  }
284  else if ( m.size() == 2)
285  {
286  //TODO structured mesh in 2D
287  }
288  else
289  {
290  regularMesh3D ( *fullMesh,
291  regionFlag,
292  m[0], m[1], m[2],
293  verbose,
294  l[0], l[1], l[2],
295  t[0], t[1], t[2] );
296  }
297  MeshUtility::printMeshInfos ( fullMesh );
298  meshBuildChrono.stop();
299  displayer.leaderPrint ("Building time: ", meshBuildChrono.diff(), " s.\n");
300 
301  LifeChrono meshPartChrono;
302  meshPartChrono.start();
303  MeshPartitioner< RegionMeshType > meshPartitioner ( fullMesh, Comm );
304  mesh = meshPartitioner.meshPartition();
305  meshPartChrono.stop();
306  displayer.leaderPrint ("Partitioning time: ", meshPartChrono.diff(), " s.\n");
307  if ( meshFull )
308  {
309  meshFull = fullMesh;
310  }
311  else
312  {
313  fullMesh.reset(); //Freeing the global mesh to save memory
314  }
315 }
316 
317 //! Build a mesh from a partitioned mesh
318 /*!
319  @param mesh The mesh that we want to generate
320  @param regionFlag Flag of the region
321  @param m Number of elements along the ( length, width, height )
322  @param l length of the mesh ( length, width, height )
323  @param t translation of the mesh along the (x,y,z)-axis
324  @param verbose Verbose mode enabled/disabled
325 */
326 /*!
327  @author Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
328  */
329 template< typename RegionMeshType>
330 void loadStructuredMesh ( std::shared_ptr< RegionMeshType >& mesh,
331  markerID_Type regionFlag,
332  const std::vector<UInt> m,
333  bool verbose = false,
334  const std::vector<Real>& l = std::vector<Real> (3, 1),
335  const std::vector<Real>& t = std::vector<Real> (3, 0) )
336 {
337  std::shared_ptr< RegionMeshType > tmpMeshFull;
338  loadStructuredMesh ( mesh, tmpMeshFull, regionFlag, m, verbose, l, t );
339 }
340 
341 
342 
343 
344 
345 } // namespace MeshUtility
346 
347 } // namespace LifeV
348 
349 #endif /* MESHLOADINGUTILITY_H */
void setMeshDir(const std::string &dir)
Definition: MeshData.hpp:129
void printMeshInfos(std::shared_ptr< RegionMeshType > mesh)
Print informations about the mesh.
void loadPartitionedMesh(std::shared_ptr< RegionMeshType > &meshLocal, const std::string &meshName, const std::string &resourcesPath)
Read and partitioned a *.mesh file.
void setMeshFile(const std::string &file)
Definition: MeshData.hpp:133
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
void loadMesh(std::shared_ptr< RegionMeshType > &meshLocal, const std::string &meshName, const std::string &resourcesPath="./", bool isPartitioned=false, const std::string &meshOrder="P1")
Read and partitioned a *.mesh file.
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
MeshData getMeshData(const std::string &meshName, const std::string &resourcesPath="./", const std::string &meshOrder="P1")
setup and get the mesh data
void loadStructuredMesh(std::shared_ptr< RegionMeshType > &mesh, markerID_Type regionFlag, const std::vector< UInt > m, bool verbose=false, const std::vector< Real > &l=std::vector< Real >(3, 1), const std::vector< Real > &t=std::vector< Real >(3, 0))
Build a mesh from a partitioned mesh.
void setMOrder(const std::string &order)
Definition: MeshData.hpp:141
void setVerbose(const bool &isVerbose)
Definition: MeshData.hpp:145