LifeV
MeshData.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 File containing a class for handling spatial discretization.
30 
31  @author M.A. Fernandez
32  @date 01-2003
33  @author Cristiano Malossi<cristiano.malossi@epfl.ch>
34  @date 06-2009
35  @author Gilles Fourestey
36  @date 06-2010
37  @contributor Lucia Mirabella <lucia.mirabell@gmail.com>
38  @contributor Gwenol Grandperrin <gwenol.grandperrin@epfl.ch>
39  @maintainer Lucia Mirabella <lucia.mirabell@gmail.com>
40 
41  */
42 
43 #ifndef MESHDATA_H
44 #define MESHDATA_H
45 
46 #include <string>
47 #include <ostream>
48 
49 #include <Teuchos_ParameterList.hpp>
50 
51 #include <lifev/core/LifeV.hpp>
52 #include <lifev/core/filter/GetPot.hpp>
53 #include <lifev/core/filter/ImporterMesh2D.hpp>
54 #include <lifev/core/mesh/RegionMesh.hpp>
55 #include <lifev/core/filter/ImporterMesh3D.hpp>
56 #include <lifev/core/mesh/RegionMesh3DStructured.hpp>
57 #include <lifev/core/filter/ParserINRIAMesh.hpp>
58 #include <lifev/core/filter/ParserGmsh.hpp>
59 #include <lifev/core/mesh/ConvertBareMesh.hpp>
60 
61 namespace LifeV
62 {
63 
64 //! MeshData - class for handling spatial discretization.
65 /*!
66  @author M.A. Fernandez
67  @author Cristiano Malossi
68 
69  The class is a container for mesh information.
70  */
71 
72 class MeshData
73 {
74 public:
75 
76  //! @name Constructors & Destructor
77  //@{
78 
79  //! Empty Constructor
80  MeshData();
81 
82  //! Constructor
83  /*!
84  @param dataFile data file
85  @param section the section in the data file
86  */
87  MeshData ( const GetPot& dataFile, const std::string& section = "space_discretization" );
88 
89  //! Copy constructor
90  /*!
91  */
92  MeshData ( const MeshData& meshData );
93 
94  //! Virtual destructor
95  virtual ~MeshData() {};
96 
97  //@}
98 
99 
100  //! @name Methods
101  //@{
102 
103  //! Read the dataFile and set all the members
104  /*!
105  @param dataFile data file
106  @param section file section
107  */
108  void setup ( const GetPot& dataFile, const std::string& section );
109 
110  //! Set all members using a Teuchos ParameterList
111  /*!
112  @param meshParameters Teuchos ParameterList containing:
113  - meshDir [string] location of mesh file
114  - meshFile [string] the name of the mesh file
115  - order [string] the order of the mesh (P1, P2)
116  - verbose [bool] verbosity level
117  */
118  void setup ( const Teuchos::ParameterList& meshParameters);
119 
120  //! Display the values
121  virtual void showMe ( std::ostream& output = std::cout ) const;
122 
123  //@}
124 
125 
126  //! @name Set Methods
127  //@{
128 
129  void setMeshDir ( const std::string& dir )
130  {
131  M_meshDir = dir;
132  }
133  void setMeshFile ( const std::string& file )
134  {
135  M_meshFile = file;
136  }
137  void setMeshType ( const std::string& type )
138  {
139  M_meshType = type;
140  }
141  void setMOrder ( const std::string& order )
142  {
143  M_order = order;
144  }
145  void setVerbose ( const bool& isVerbose )
146  {
147  M_verbose = isVerbose;
148  }
149 
150  //@}
151 
152 
153  //! @name Get Methods
154  //@{
155 
156  const std::string& meshDir() const
157  {
158  return M_meshDir;
159  }
160  const std::string& meshFile() const
161  {
162  return M_meshFile;
163  }
164  const std::string& meshType() const
165  {
166  return M_meshType;
167  }
168  const std::string& mOrder() const
169  {
170  return M_order;
171  }
172  const bool& verbose() const
173  {
174  return M_verbose;
175  }
176 
177  //@}
178 
179 private:
180 
181  std::string M_meshDir; //!< mesh directory
182  std::string M_meshFile; //!< mesh file
183  std::string M_meshType; //!< mesh type
184  std::string M_order; //!< mesh type
185 
186  bool M_verbose; //!< verbose output?
187 };
188 
189 template <typename MC>
190 void readMesh ( RegionMesh<LinearTriangle, MC>& mesh, const MeshData& data )
191 {
192  if ( data.verbose() )
193  {
194  std::cout << "\nBuilding mesh ... ";
195  }
196 
197 
198  if ( data.meshType() == ".msh" )
199  {
200  std::ifstream ifile;
201  ifile.open ( ( data.meshDir() + data.meshFile() ).c_str() );
202  ASSERT (ifile.is_open(), "Error! Unable to read mesh file.\n");
203 
204  // Checking whether it is Gmsh or FreeFem mesh format
205  if (ifile.get() == '$')
206  {
207  // Gmsh files start with '$MeshFormat'
208  ifile.close();
209  BareMesh<LinearTriangle> bareMesh;
210  MeshIO::ReadGmshFile (data.meshDir() + data.meshFile(), bareMesh, 0, data.verbose() );
211  convertBareMesh ( bareMesh, mesh, data.verbose() );
212  }
213  else
214  {
215  // If not Gmsh format, it must be FreeFem
216  ifile.close();
217  readFreeFemFile ( mesh, data.meshDir() + data.meshFile(), 1, data.verbose() );
218  }
219  }
220  else
221  {
222  ERROR_MSG ( "Sorry, this mesh file can not be loaded" );
223  }
224 
225  //Update Edges
226  mesh.updateElementFacets (true);
227 
228  if ( data.verbose() )
229  {
230  std::cout << "mesh read.\n" << std::endl;
231  }
232 }
233 
234 template <typename GEOSHAPE, typename MC>
235 void readMesh ( RegionMesh<GEOSHAPE, MC>& mesh, const MeshData& data )
236 {
237  if ( data.verbose() )
238  {
239  std::cout << "\nBuilding mesh ... ";
240  }
241 
242  bool updateEdgesAndFaces (true);
243 
244  if ( data.meshType() == ".mesh" )
245  {
246  BareMesh<GEOSHAPE> bareMesh;
247  MeshIO::ReadINRIAMeshFile ( bareMesh, data.meshDir() + data.meshFile(), 1, data.verbose() );
248  convertBareMesh ( bareMesh, mesh, data.verbose() );
249  // readINRIAMeshFile( mesh, data.meshDir() + data.meshFile(), 1, data.verbose() );
250  }
251  else if ( data.meshType() == ".m++" )
252  {
253  readMppFile ( mesh, data.meshDir() + data.meshFile(), 1, data.verbose() );
254  }
255  else if ( data.meshType() == ".msh" )
256  {
257  readGmshFile ( mesh, data.meshDir() + data.meshFile(), 1 );
258  }
259  else if ( data.meshType() == ".vol" )
260  {
261  readNetgenMesh ( mesh, data.meshDir() + data.meshFile(), 1, data.verbose() );
262  }
263  else
264  {
265  ERROR_MSG ( "Sorry, this mesh file can not be loaded" );
266  }
267 
268  //Update Edges & Faces
269  if (updateEdgesAndFaces)
270  {
271  mesh.updateElementRidges ( true, data.verbose() );
272  mesh.updateElementFacets ( true, data.verbose() );
273  }
274 
275  if ( data.verbose() )
276  {
277  std::cout << "mesh read.\n" << std::endl;
278  }
279 }
280 
281 
282 
283 } // namespace LifeV
284 
285 #endif /* MESHDATA_H */
void setMeshDir(const std::string &dir)
Definition: MeshData.hpp:129
MeshData(const MeshData &meshData)
Copy constructor.
Definition: MeshData.cpp:69
const std::string & meshFile() const
Definition: MeshData.hpp:160
A Geometric Shape.
virtual ~MeshData()
Virtual destructor.
Definition: MeshData.hpp:95
bool M_verbose
verbose output?
Definition: MeshData.hpp:186
void setMeshType(const std::string &type)
Definition: MeshData.hpp:137
const std::string & meshType() const
Definition: MeshData.hpp:164
#define ERROR_MSG(A)
Definition: LifeAssert.hpp:69
void setMeshFile(const std::string &file)
Definition: MeshData.hpp:133
std::string M_meshDir
mesh directory
Definition: MeshData.hpp:181
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
MeshData(const GetPot &dataFile, const std::string &section="space_discretization")
Constructor.
Definition: MeshData.cpp:59
std::string M_meshType
mesh type
Definition: MeshData.hpp:183
const std::string & mOrder() const
Definition: MeshData.hpp:168
Class for 3D, 2D and 1D Mesh.
Definition: RegionMesh.hpp:87
void readMesh(RegionMesh< LinearTriangle, MC > &mesh, const MeshData &data)
Definition: MeshData.hpp:190
MeshData - class for handling spatial discretization.
Definition: MeshData.hpp:72
const std::string & meshDir() const
Definition: MeshData.hpp:156
void setup(const GetPot &dataFile, const std::string &section)
Read the dataFile and set all the members.
Definition: MeshData.cpp:81
const bool & verbose() const
Definition: MeshData.hpp:172
MeshData()
Empty Constructor.
Definition: MeshData.cpp:51
virtual void showMe(std::ostream &output=std::cout) const
Display the values.
Definition: MeshData.cpp:108
std::string M_order
mesh type
Definition: MeshData.hpp:184
void readMesh(RegionMesh< GEOSHAPE, MC > &mesh, const MeshData &data)
Definition: MeshData.hpp:235
void setMOrder(const std::string &order)
Definition: MeshData.hpp:141
void setup(const Teuchos::ParameterList &meshParameters)
Set all members using a Teuchos ParameterList.
Definition: MeshData.cpp:91
A struct for a bare mesh.
Definition: BareMesh.hpp:53
void setVerbose(const bool &isVerbose)
Definition: MeshData.hpp:145
std::string M_meshFile
mesh file
Definition: MeshData.hpp:182