Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/IO/NetgenMeshWriter.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 writer class definition
10 : : \details Netgen mesh writer class definition. Only supports tetrahedra.
11 : : */
12 : : // *****************************************************************************
13 : :
14 : : #include <iomanip>
15 : : #include <ostream>
16 : : #include <vector>
17 : : #include <algorithm>
18 : : #include <iterator>
19 : : #include <utility>
20 : : #include <cstddef>
21 : :
22 : : #include "Types.hpp"
23 : : #include "UnsMesh.hpp"
24 : : #include "Exception.hpp"
25 : : #include "NetgenMeshWriter.hpp"
26 : :
27 : : using tk::NetgenMeshWriter;
28 : :
29 : : void
30 : 5 : NetgenMeshWriter::writeMesh( const UnsMesh& mesh )
31 : : // *****************************************************************************
32 : : // Public interface for writing Netgen mesh
33 : : //! \param[in] mesh Unstructured mesh object
34 : : // *****************************************************************************
35 : : {
36 : 5 : writeNodes( mesh );
37 : 5 : writeElements( mesh );
38 : 5 : }
39 : :
40 : : void
41 : 5 : NetgenMeshWriter::writeNodes( const UnsMesh& mesh )
42 : : // *****************************************************************************
43 : : // Write nodes
44 : : //! \param[in] mesh Unstructured mesh object
45 : : // *****************************************************************************
46 : : {
47 : : // Write out number of nodes
48 : 5 : m_outFile << mesh.nnode() << std::endl;
49 : :
50 : : // Write node coordinates: x-coord y-coord z-coord
51 : 5 : m_outFile << std::setprecision(6) << std::fixed;
52 [ + + ]: 17637 : for ( std::size_t i=0; i<mesh.nnode(); ++i ) {
53 : 17632 : m_outFile << '\t' << mesh.x()[i]
54 : 17632 : << '\t' << mesh.y()[i]
55 : 17632 : << '\t' << mesh.z()[i] << std::endl;
56 : : }
57 : 5 : }
58 : :
59 : : void
60 : 5 : NetgenMeshWriter::writeElements( const UnsMesh& mesh )
61 : : // *****************************************************************************
62 : : // Write elements, i.e., connectivity
63 : : //! \param[in] mesh Unstructured mesh object
64 : : // *****************************************************************************
65 : : {
66 [ - + ]: 6 : if (mesh.tetinpoel().empty()) return;
67 : :
68 : : // Make sure tetrahedron element connectivity starts with zero
69 [ + - ][ - + ]: 15 : Assert( *std::minmax_element( begin(mesh.tetinpoel()),
[ - - ][ - - ]
[ - - ]
70 : : end(mesh.tetinpoel()) ).first == 0,
71 : : "tetrahedron node ids should start from zero" );
72 : :
73 : : // Get number of tetrahedra in mesh
74 : 5 : auto n = mesh.tetinpoel().size()/4;
75 : :
76 : : // Write out number of tetrahedra
77 [ + - ][ + - ]: 5 : m_outFile << n << std::endl;
78 : :
79 : : // Create empty tag vector
80 : 5 : std::vector< std::vector< int > > tg;
81 [ + - ]: 5 : tg.resize( n );
82 [ + - ][ + + ]: 93851 : for (auto& t : tg) t.push_back( 1 );
83 : :
84 : : // Write out tetrehadra element tags and connectivity
85 [ + + ]: 93851 : for (std::size_t i=0; i<n; ++i) {
86 : : // tag n[1-4]
87 [ + - ]: 93846 : m_outFile << '\t' << tg[i][0]
88 [ + - ][ + - ]: 93846 : << '\t' << mesh.tetinpoel()[i*4+3]+1
[ + - ]
89 [ + - ][ + - ]: 93846 : << '\t' << mesh.tetinpoel()[i*4+0]+1
90 [ + - ][ + - ]: 93846 : << '\t' << mesh.tetinpoel()[i*4+1]+1
91 [ + - ][ + - ]: 93846 : << '\t' << mesh.tetinpoel()[i*4+2]+1 << std::endl;
[ + - ]
92 : : }
93 : :
94 [ + + ]: 5 : if (mesh.triinpoel().empty()) return;
95 : :
96 : : // Make sure triangle element connectivity starts with zero
97 [ + - ][ - + ]: 12 : Assert( *std::minmax_element( begin(mesh.triinpoel()),
[ - - ][ - - ]
[ - - ]
98 : : end(mesh.triinpoel()) ).first == 0,
99 : : "triangle node ids should start from zero" );
100 : :
101 : : // Get number of triangles in mesh
102 : 4 : n = mesh.triinpoel().size()/3;
103 : :
104 : : // Write out number of triangles
105 [ + - ][ + - ]: 4 : m_outFile << n << std::endl;
106 : :
107 : : // Create empty tag vector if there is no tag
108 : 4 : tg.clear();
109 [ + - ]: 4 : tg.resize( n );
110 [ + - ][ + + ]: 7576 : for (auto& t : tg) t.push_back( 1 );
111 : :
112 : : // Write out triangle element tags and connectivity
113 [ + + ]: 7576 : for (std::size_t i=0; i<n; ++i) {
114 : : // tag n[1-4]
115 [ + - ]: 7572 : m_outFile << '\t' << tg[i][0]
116 [ + - ][ + - ]: 7572 : << '\t' << mesh.triinpoel()[i*3+0]+1
[ + - ]
117 [ + - ][ + - ]: 7572 : << '\t' << mesh.triinpoel()[i*3+1]+1
118 [ + - ][ + - ]: 7572 : << '\t' << mesh.triinpoel()[i*3+2]+1 << std::endl;
[ + - ]
119 : : }
120 [ + + ]: 5 : }
|