Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/IO/NetgenMeshReader.cpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.,
7 : : 2022-2025 J. Bakosi
8 : : All rights reserved. See the LICENSE file for details.
9 : : \brief Netgen mesh reader class definition
10 : : \details Netgen mesh reader class definition. Only supports tetrahedra.
11 : : */
12 : : // *****************************************************************************
13 : :
14 : : #include <array>
15 : : #include <istream>
16 : : #include <string>
17 : : #include <vector>
18 : : #include <cstddef>
19 : :
20 : : #include "Types.hpp"
21 : : #include "Exception.hpp"
22 : : #include "UnsMesh.hpp"
23 : : #include "Reorder.hpp"
24 : : #include "NetgenMeshReader.hpp"
25 : :
26 : : using tk::NetgenMeshReader;
27 : :
28 : : void
29 : 3 : NetgenMeshReader::readMesh( UnsMesh& mesh )
30 : : // *****************************************************************************
31 : : // Read Netgen mesh
32 : : //! \param[in] mesh Unstructured mesh object
33 : : // *****************************************************************************
34 : : {
35 : : // Read nodes
36 : 3 : readNodes( mesh );
37 : : // Read elements
38 : 3 : readElements( mesh );
39 : 3 : }
40 : :
41 : : void
42 : 3 : NetgenMeshReader::readNodes( UnsMesh& mesh )
43 : : // *****************************************************************************
44 : : // Read nodes
45 : : //! \param[in] mesh Unstructured mesh object
46 : : // *****************************************************************************
47 : : {
48 : : int nnode;
49 : 3 : m_inFile >> nnode;
50 [ + - ][ - - ]: 3 : ErrChk( nnode > 0,
[ - - ][ - - ]
51 : : "Number of nodes must be greater than zero in file " + m_filename );
52 : :
53 : : // Read in node coordinates: x-coord y-coord z-coord
54 [ + + ]: 1670 : for (int i=0; i<nnode; ++i) {
55 : : tk::real x, y, z;
56 : : m_inFile >> x >> y >> z;
57 : : mesh.x().push_back( x );
58 : : mesh.y().push_back( y );
59 : : mesh.z().push_back( z );
60 : : }
61 : :
62 : : std::string s;
63 [ + - ]: 3 : getline( m_inFile, s ); // finish reading the last line
64 : 3 : }
65 : :
66 : : void
67 : 3 : NetgenMeshReader::readElements( UnsMesh& mesh )
68 : : // *****************************************************************************
69 : : // Read element connectivity
70 : : //! \param[in] mesh Unstructured mesh object
71 : : // *****************************************************************************
72 : : {
73 : : int nel;
74 : :
75 : : // Read in number of tetrahedra
76 : 3 : m_inFile >> nel;
77 [ + - ]: 3 : if (!m_inFile.eof()) {
78 [ - + ][ - - ]: 3 : ErrChk( nel > 0, "Number of tetrahedra (volume elements) must be greater "
[ - - ][ - - ]
79 : : "than zero in file " + m_filename );
80 : : std::string s;
81 [ + - ]: 3 : getline( m_inFile, s ); // finish reading the last line
82 : :
83 : : // Read in tetrahedra element tags and connectivity
84 [ + + ]: 5457 : for (int i=0; i<nel; ++i) {
85 : : int tag;
86 : : std::array< std::size_t, 4 > n;
87 : : // tag n[1-4]
88 [ + - ]: 5454 : m_inFile >> tag >> n[3] >> n[0] >> n[1] >> n[2];
89 : : mesh.tetinpoel().push_back( n[0] );
90 : : mesh.tetinpoel().push_back( n[1] );
91 : : mesh.tetinpoel().push_back( n[2] );
92 : : mesh.tetinpoel().push_back( n[3] );
93 : : }
94 : :
95 : : // Shift node IDs to start from zero
96 [ + - ]: 3 : shiftToZero( mesh.tetinpoel() );
97 : : }
98 : :
99 : : // Read in number of triangles
100 : 3 : m_inFile >> nel;
101 [ + + ]: 3 : if (!m_inFile.eof()) {
102 [ - + ][ - - ]: 2 : ErrChk( nel > 0, "Number of triangles (surface elements) must be greater "
[ - - ][ - - ]
103 : : "than zero in file " + m_filename );
104 : : std::string s;
105 [ + - ]: 2 : getline( m_inFile, s ); // finish reading the last line
106 : :
107 : : // Read in triangle element tags and connectivity
108 [ + + ]: 2606 : for (int i=0; i<nel; ++i) {
109 : : int tag;
110 : : std::array< std::size_t, 3 > n;
111 : : // tag n[1-3]
112 [ + - ]: 2604 : m_inFile >> tag >> n[0] >> n[1] >> n[2];
113 : : mesh.triinpoel().push_back( n[0] );
114 : : mesh.triinpoel().push_back( n[1] );
115 : : mesh.triinpoel().push_back( n[2] );
116 : : }
117 : :
118 : : // Shift node IDs to start from zero
119 [ + - ]: 2 : shiftToZero( mesh.triinpoel() );
120 : : }
121 : 3 : }
|