mesh_data_tetgen_support.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2016 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // C++ includes
21 #include <fstream>
22 
23 // Local includes
24 #include "libmesh/mesh_data.h"
25 
26 namespace libMesh
27 {
28 
29 
30 
31 
32 //---------------------------------------------------------------------
33 // MeshDate TetGen support function
34 void MeshData::read_tetgen (const std::string & name)
35 {
36  std::string name_node, name_ele, dummy;
37  std::string desc = name;
38 
39 
40  // Check name for *.node or *.ele extension.
41  // Set std::istream for node_stream and ele_stream.
42  if (name.rfind(".node") < name.size())
43  {
44  name_node = name;
45  dummy = name;
46  std::size_t position = dummy.rfind(".node");
47  name_ele = dummy.replace(position, 5, ".ele");
48  desc.erase(position);
49  }
50  else if (name.rfind(".ele") < name.size())
51  {
52  name_ele = name;
53  dummy = name;
54  std::size_t position = dummy.rfind(".ele");
55  name_node = dummy.replace(position, 4, ".node");
56  desc.erase(position);
57  }
58  else
59  libmesh_error_msg("ERROR: Unrecognized file name: " << name);
60 
61  // Set the streams from which to read in.
62  std::ifstream node_stream (name_node.c_str());
63  std::ifstream ele_stream (name_ele.c_str());
64 
65  if ( !node_stream.good() || !ele_stream.good() )
66  libmesh_error_msg("ERROR: One or both Input file(s) not good.\n" \
67  << "Error opening files " \
68  << name_node \
69  << " and " \
70  << name_ele);
71 
72 
73  // Set the descriptive name.
74  // TetGen won't give a name, so we use the filename.
75  this->_data_descriptor = desc;
76 
77 
78  //--------------------------------------------------
79  // Read in the data associated with the nodes.
80  {
81  unsigned int n_node=0, f_n_id=0, nAttri=0, BoundMark=0;
82  Real dummy_val=0.0;
83  std::vector<Number> AttriValue;
84 
85  // Read the parameters from the node_stream.
86  node_stream >> n_node // Read the number of nodes
87  >> dummy_val // Read the dimension
88  >> nAttri // Read the number of attributes
89  >> BoundMark; // (0 or 1) boundary markers are in the stream or not.
90 
91  // Resize the values vector.
92  AttriValue.resize(nAttri);
93 
94  for (unsigned int i=0; i<n_node; i++)
95  {
96  node_stream >> f_n_id;
97 
98 
99  // Read the nodal coordinates for this node into dummy_val,
100  // since we don't need them.
101  for (unsigned int j=0; j<3; j++)
102  node_stream >> dummy_val;
103 
104  // Read the attributes from the stream.
105  for (unsigned int j=0; j<nAttri; j++)
106  node_stream >> AttriValue[j];
107 
108  // Read boundary marker if BoundaryMarker=1.
109  if (BoundMark == 1)
110  node_stream >> dummy_val;
111 
112  // For the foreign node id locate the Node *.
113  const Node * node = foreign_id_to_node(f_n_id);
114 
115  // Insert this node and the values in our _node_data.
116  _node_data.insert (std::make_pair(node, AttriValue));
117  }
118  }
119 
120 
121  //--------------------------------------------------
122  // Read in the data associated with the elements.
123  {
124  unsigned int n_elem, f_e_id, n_nodes, nAttri=0;
125  Real dummy_val=0.0;
126  std::vector<Number> AttriValue;
127 
128  // Read the parameters from the ele_stream.
129  ele_stream >> n_elem // Read the number of tetrahedrons
130  >> n_nodes // Read the points per tetrahedron
131  >> nAttri; // Read the number of attributes
132 
133  // Resize the values vector.
134  AttriValue.resize(nAttri);
135 
136  for (unsigned int i=0; i<n_elem; i++)
137  {
138  ele_stream >> f_e_id;
139 
140  // For the number of nodes for this element read them into dummy_val,
141  // since we don't need them.
142  for (unsigned int n=0; n<n_nodes; n++)
143  ele_stream >> dummy_val;
144 
145  // Read the attributes from the stream.
146  for (unsigned int j=0; j<nAttri; j++)
147  ele_stream >> AttriValue[j];
148 
149  // For the foreign elem id locate the Elem *.
150  const Elem * elem = foreign_id_to_elem(f_e_id);
151 
152  // Insert this elem and the values in our _elem_data.
153  _elem_data.insert (std::make_pair(elem, AttriValue));
154  }
155  }
156 
157  //--------------------------------------------------
158  // Finished reading. Now ready for use.
159  this->_node_data_closed = true;
160  this->_elem_data_closed = true;
161 
162  node_stream.close();
163  ele_stream.close();
164 }
165 
166 } // namespace libMesh
std::string name(const ElemQuality q)
Definition: elem_quality.C:39
A geometric point in (x,y,z) space associated with a DOF.
Definition: node.h:54
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Definition: mesh_tools.C:669
The base class for all geometric element types.
Definition: elem.h:92
const dof_id_type n_nodes
Definition: tecplot_io.C:67
const Elem * foreign_id_to_elem(const unsigned int fid) const
Definition: mesh_data.C:434
bool _elem_data_closed
Definition: mesh_data.h:599
std::string _data_descriptor
Definition: mesh_data.h:515
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
const Node * foreign_id_to_node(const unsigned int fid) const
Definition: mesh_data.C:369
std::map< const Node *, std::vector< Number > > _node_data
Definition: mesh_data.h:525
void read_tetgen(const std::string &name)
std::map< const Elem *, std::vector< Number > > _elem_data
Definition: mesh_data.h:549
bool _node_data_closed
Definition: mesh_data.h:581