1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266 | // *****************************************************************************
/*!
\file src/IO/GmshMeshReader.cpp
\copyright 2012-2015 J. Bakosi,
2016-2018 Los Alamos National Security, LLC.,
2019-2021 Triad National Security, LLC.,
2022-2025 J. Bakosi
All rights reserved. See the LICENSE file for details.
\brief Gmsh mesh reader class definition
\details Gmsh mesh reader class definition. Currently, this class supports
line, triangle, tetrahedron, and point Gmsh element types.
*/
// *****************************************************************************
#include <limits><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <array><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cmath><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstddef><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <istream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <utility><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "UnsMesh.hpp"<--- Include file: "UnsMesh.hpp" not found.
#include "GmshMeshReader.hpp"
#include "GmshMeshIO.hpp"
#include "Reorder.hpp"<--- Include file: "Reorder.hpp" not found.
#include "PrintUtil.hpp"
using tk::GmshMeshReader;
void
GmshMeshReader::readMesh( UnsMesh& mesh )
// *****************************************************************************
// Public interface for read a Gmsh mesh from file
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
// Read in mandatory "$MeshFormat" section
readMeshFormat();
// Keep reading in sections until end of file. These sections can be in
// arbitrary order, hence a while loop.
while ( !m_inFile.eof() ) {
std::string s;
getline( m_inFile, s );
if ( s == "$Nodes" )
readNodes( mesh );
else if ( s == "$Elements" )
readElements( mesh );
}
}
void
GmshMeshReader::readMeshFormat()
// *****************************************************************************
// Read mandatory "$MeshFormat--$EndMeshFormat" section
// *****************************************************************************
{
using tk::operator<<;
std::string s;
// Read in beginning of header: $MeshFormat
getline( m_inFile, s );
ErrChk( s == "$MeshFormat",
std::string("Unsupported mesh format '") + s + "' in file " +
m_filename );
// Read in "version-number file-type data-size"
int type;
m_inFile >> m_version >> type >> m_datasize;
if (type == 0 )
m_type = GmshFileType::ASCII;
else if (type == 1 )
m_type = GmshFileType::BINARY;
ErrChk( ( fabs(m_version-2.2) < std::numeric_limits<tk::real>::epsilon() ||
fabs(m_version-2.0) < std::numeric_limits<tk::real>::epsilon() ),
std::string("Unsupported mesh version '") << m_version <<
"' in file " << m_filename );
ErrChk( ( m_type == GmshFileType::ASCII || m_type == GmshFileType::BINARY ),
std::string("Unsupported mesh type '") << m_type << "' in file " <<
m_filename );
ErrChk( m_datasize == sizeof(tk::real),
std::string("Unsupported mesh datasize '") << m_datasize <<
"' in file " << m_filename );
getline( m_inFile, s ); // finish reading the line
// if file is binary, binary-read in binary "one"
if ( isBinary() ) {
int one;
m_inFile.read( reinterpret_cast<char*>(&one), sizeof(int) );
#ifdef __bg__
one = tk::swap_endian< int >( one );
#endif
ErrChk( one == 1, "Endianness does not match in file " + m_filename );
getline( m_inFile, s ); // finish reading the line
}
// Read in end of header: $EndMeshFormat
getline( m_inFile, s );
ErrChk( s == "$EndMeshFormat",
"'$EndMeshFormat' keyword is missing in file " + m_filename );
}
void
GmshMeshReader::readNodes( UnsMesh& mesh )
// *****************************************************************************
// Read "$Nodes--$EndNodes" section
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
// Read in number of nodes in this node set
std::size_t nnode;
m_inFile >> nnode;
ErrChk( nnode > 0,
"Number of nodes must be greater than zero in file " + m_filename );
std::string s;
if (isBinary()) getline( m_inFile, s ); // finish reading the line
// Read in node ids and coordinates: node-number x-coord y-coord z-coord
for ( std::size_t i=0; i<nnode; ++i ) {
int id;
std::array< tk::real, 3 > coord;
if (isASCII()) {
m_inFile >> id >> coord[0] >> coord[1] >> coord[2];
} else {
m_inFile.read( reinterpret_cast<char*>(&id), sizeof(int) );
#ifdef __bg__
id = tk::swap_endian< int >( id );
#endif
m_inFile.read( reinterpret_cast<char*>(coord.data()), 3*sizeof(double) );
#ifdef __bg__
coord[0] = tk::swap_endian< double >( coord[0] );
coord[1] = tk::swap_endian< double >( coord[1] );
coord[2] = tk::swap_endian< double >( coord[2] );
#endif
}
mesh.x().push_back( coord[0] );
mesh.y().push_back( coord[1] );
mesh.z().push_back( coord[2] );
}
getline( m_inFile, s ); // finish reading the last line
// Read in end of header: $EndNodes
getline( m_inFile, s );
ErrChk( s == "$EndNodes",
"'$EndNodes' keyword is missing in file" + m_filename );
}
void
GmshMeshReader::readElements( UnsMesh& mesh )
// *****************************************************************************
// Read "$Elements--$EndElements" section
//! \param[in] mesh Unstructured mesh object
// *****************************************************************************
{
using tk::operator<<;
std::string s;
// Read in number of elements in this element set
int nel;
m_inFile >> nel;
ErrChk( nel > 0, "Number of elements must be greater than zero in file " +
m_filename );
getline( m_inFile, s ); // finish reading the last line
// Read in element ids, tags, and element connectivity (node list)
int n=1;
for (int i=0; i<nel; i+=n) {
int id, elmtype, ntags;
if (isASCII()) {
// elm-number elm-type number-of-tags < tag > ... node-number-list
m_inFile >> id >> elmtype >> ntags;
} else {
// elm-type num-of-elm-follow number-of-tags
m_inFile.read( reinterpret_cast<char*>(&elmtype), sizeof(int) );
m_inFile.read( reinterpret_cast<char*>(&n), sizeof(int) );
m_inFile.read( reinterpret_cast<char*>(&ntags), sizeof(int) );
#ifdef __bg__
elmtype = tk::swap_endian< int >( elmtype );
n = tk::swap_endian< int >( n );
ntags = tk::swap_endian< int >( ntags );
#endif
}
// Find element type, throw exception if not supported
const auto it = m_elemNodes.find( elmtype );
ErrChk( it != m_elemNodes.end(),
std::string("Unsupported element type ") << elmtype <<
" in mesh file: " << m_filename );
for (int e=0; e<n; ++e) {
// Read element id if binary
if (isBinary()) {
m_inFile.read( reinterpret_cast<char*>(&id), sizeof(int) );
#ifdef __bg__
id = tk::swap_endian< int >( id );
#endif
}
// Read and ignore element tags
std::vector< int > tags( static_cast<std::size_t>(ntags), 0 );
if (isASCII()) {
for (std::size_t j=0; j<static_cast<std::size_t>(ntags); j++)
m_inFile >> tags[j];
} else {
m_inFile.read(
reinterpret_cast<char*>(tags.data()),
static_cast<std::streamsize>(
static_cast<std::size_t>(ntags) * sizeof(int) ) );
#ifdef __bg__
for (auto& t : tags) t = tk::swap_endian< int >( t );
#endif
}
// Read and add element node list (i.e. connectivity)
std::size_t nnode = static_cast< std::size_t >( it->second );
std::vector< std::size_t > nodes( nnode, 0 );
if (isASCII()) {
for (std::size_t j=0; j<nnode; j++)
m_inFile >> nodes[j];
} else {
std::vector< int > nds( nnode, 0 );
m_inFile.read(
reinterpret_cast< char* >( nds.data() ),
static_cast< std::streamsize >( nnode * sizeof(int) ) );
#ifdef __bg__
for (auto& j : nds) j = tk::swap_endian< int >( j );
#endif
for (std::size_t j=0; j<nnode; j++)
nodes[j] = static_cast< std::size_t >( nds[j] );
}
// Put in element connectivity for different types of elements
switch ( elmtype ) {
case GmshElemType::TRI:
for (const auto& j : nodes) mesh.triinpoel().push_back( j );
break;
case GmshElemType::TET:
for (const auto& j : nodes) mesh.tetinpoel().push_back( j );
break;
case GmshElemType::PNT:
break; // ignore 1-node 'point element' type
default: Throw( std::string("Unsupported element type ") << elmtype <<
" in mesh file: " << m_filename );
}
}
}
getline( m_inFile, s ); // finish reading the last line
// Shift node IDs to start from zero (gmsh likes one-based node ids)
shiftToZero( mesh.triinpoel() );
shiftToZero( mesh.tetinpoel() );
// Read in end of header: $EndNodes
getline( m_inFile, s );
ErrChk( s == "$EndElements",
"'$EndElements' keyword is missing in file" + m_filename );
}
|