LifeV
MeshWriter.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 Writes the mesh in medit format
30 
31  This file contains the namespace MeshWriters. There a method to write meshes in format MEDIT (ASCII) is implemented.
32 
33  @author Paolo Crosetto <crosetto@iacspc70.epfl.ch>
34  @date 31 May 2011
35 
36 
37  */
38 
39 #ifndef MESHWRITER_H
40 #define MESHWRITER_H 1
41 
42 #include <lifev/core/LifeV.hpp>
43 #include <lifev/core/mesh/ElementShapes.hpp>
44 
45 namespace LifeV
46 {
47 
48 //! MeshWriter - Short description of the class
49 /*!
50  @author Paolo Crosetto
51 
52  This file contains the namespace MeshWriters. There a method to write meshes in format MEDIT (ASCII) is implemented.
53  The files in MEDIT format have the extension .mesh and can be read by several softwares (like MEDIT, from INRIA, or GMSH),
54  The method writeMeshMedit implemented here works in serial and only for 3D meshes.
55  */
56 namespace MeshWriter
57 {
58 
59 template <typename Mesh>
60 void writeMeshMedit (std::string fname, Mesh& mesh )
61 {
62  std::ofstream ofile ( fname.c_str() );
63 
64  ASSERT ( ofile, "Error: Output file cannot be open" );
65 
66  ofile << "MeshVersionFormatted 1\n";
67  ofile << "Dimension 3\n";
68  ofile << "\n";
69  ofile << "Vertices\n";
70 
71  UInt nVertices = mesh.numVertices();
72  ofile << nVertices << "\n";
73 
74  for ( UInt i = 0; i < nVertices; ++i )
75  {
76  ofile << mesh.pointList ( i ).x() << " "
77  << mesh.pointList ( i ).y() << " "
78  << mesh.pointList ( i ).z() << " "
79  << mesh.pointList ( i ).markerID() << "\n";
80  }
81  ofile << "\n";
82 
83  typedef typename Mesh::faceShape_Type faceShape_Type;
84 
85  switch ( faceShape_Type::S_shape )
86  {
87  case QUAD:
88  ofile << "Quadrilaterals\n";
89  break;
90  case TRIANGLE:
91  ofile << "Triangles\n";
92  break;
93  default:
94  ERROR_MSG ( "BdShape not implement in MEDIT writer" );
95  }
96 
97  UInt nBdF = mesh. numBFaces();
98  ofile << nBdF << "\n";
99 
100  UInt nVerticesPerFace = faceShape_Type::S_numVertices;
101 
102 
103  for ( UInt k = 0; k < nBdF; ++k )
104  {
105  for ( UInt i = 0; i < nVerticesPerFace; ++i )
106  {
107  ofile << mesh.boundaryFace ( k ).point ( i ).id() + 1
108  << " ";
109  }
110  ofile << mesh.boundaryFace ( k ).markerID() << "\n";
111  }
112  ofile << "\n";
113 
114  typedef typename Mesh::volumeShape_Type volumeShape_Type;
115 
116  switch ( volumeShape_Type::S_shape )
117  {
118  case HEXA:
119  ofile << "Hexaedra\n";
120  break;
121  case TETRA:
122  ofile << "Tetrahedra\n";
123  break;
124  default:
125  ERROR_MSG ( "Shape not implement in MEDIT writer" );
126  }
127 
128  UInt nElements = mesh.numVolumes();
129  ofile << nElements << "\n";
130 
131  UInt nVerticesPerElement = volumeShape_Type::S_numVertices;
132 
133  UInt ielem;
134 
135  for ( UInt k = 0; k < nElements; ++k )
136  {
137  for ( UInt i = 0; i < nVerticesPerElement; ++i )
138  {
139  ielem = mesh.volume ( k ).point ( i ).localId();
140 
141  ofile << ielem + 1
142  << " ";
143  }
144  ofile << mesh.volume ( k ).markerID() << "\n";
145  }
146  ofile.close();
147 }
148 
149 } // Namespace MeshWriter
150 } // Namespace LifeV
151 
152 #endif /* MESHWRITER_H */
void writeMeshMedit(std::string fname, Mesh &mesh)
Definition: MeshWriter.hpp:60
#define ERROR_MSG(A)
Definition: LifeAssert.hpp:69
#define ASSERT(X, A)
Definition: LifeAssert.hpp:90
MeshWriter - Short description of the class.
Definition: MeshWriter.hpp:56
uint32_type UInt
generic unsigned integer (used mainly for addressing)
Definition: LifeV.hpp:191