mesh_data_tetgen_support.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 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  {
60  libMesh::err << "ERROR: Unrecognized file name: "
61  << name << std::endl;
62  libmesh_error();
63  }
64 
65  // Set the streams from which to read in.
66  std::ifstream node_stream (name_node.c_str());
67  std::ifstream ele_stream (name_ele.c_str());
68 
69  if ( !node_stream.good() || !ele_stream.good() )
70  {
71  libMesh::err << "ERROR: One or both Input file(s) not good." << std::endl
72  << "Error checking files "
73  << name_node << " and "
74  << name_ele << std::endl;
75  libmesh_error();
76  }
77 
78 
79  // Set the descriptive name.
80  // TetGen won't give a name, so we use the filename.
81  this->_data_descriptor = desc;
82 
83 
84  //--------------------------------------------------
85  // Read in the data associated with the nodes.
86  {
87  unsigned int n_node=0, f_n_id=0, nAttri=0, BoundMark=0;
88  Real dummy_val=0.0;
89  std::vector<Number> AttriValue;
90 
91  // Read the parameters from the node_stream.
92  node_stream >> n_node // Read the number of nodes
93  >> dummy_val // Read the dimension
94  >> nAttri // Read the number of attributes
95  >> BoundMark; // (0 or 1) boundary markers are in the stream or not.
96 
97  // Resize the values vector.
98  AttriValue.resize(nAttri);
99 
100  for (unsigned int i=0; i<n_node; i++)
101  {
102  node_stream >> f_n_id;
103 
104 
105  // Read the nodal coordinates for this node into dummy_val,
106  // since we don't need them.
107  for (unsigned int j=0; j<3; j++)
108  node_stream >> dummy_val;
109 
110  // Read the attributes from the stream.
111  for (unsigned int j=0; j<nAttri; j++)
112  node_stream >> AttriValue[j];
113 
114  // Read boundary marker if BoundaryMarker=1.
115  if (BoundMark == 1)
116  node_stream >> dummy_val;
117 
118  // For the foreign node id locate the Node*.
119  const Node* node = foreign_id_to_node(f_n_id);
120 
121  // Insert this node and the values in our _node_data.
122  _node_data.insert (std::make_pair(node, AttriValue));
123  }
124  }
125 
126 
127  //--------------------------------------------------
128  // Read in the data associated with the elements.
129  {
130  unsigned int n_elem, f_e_id, n_nodes, nAttri=0;
131  Real dummy_val=0.0;
132  std::vector<Number> AttriValue;
133 
134  // Read the parameters from the ele_stream.
135  ele_stream >> n_elem // Read the number of tetrahedrons
136  >> n_nodes // Read the points per tetrahedron
137  >> nAttri; // Read the number of attributes
138 
139  // Resize the values vector.
140  AttriValue.resize(nAttri);
141 
142  for (unsigned int i=0; i<n_elem; i++)
143  {
144  ele_stream >> f_e_id;
145 
146  // For the number of nodes for this element read them into dummy_val,
147  // since we don't need them.
148  for (unsigned int n=0; n<n_nodes; n++)
149  ele_stream >> dummy_val;
150 
151  // Read the attributes from the stream.
152  for (unsigned int j=0; j<nAttri; j++)
153  ele_stream >> AttriValue[j];
154 
155  // For the foreign elem id locate the Elem*.
156  const Elem* elem = foreign_id_to_elem(f_e_id);
157 
158  // Insert this elem and the values in our _elem_data.
159  _elem_data.insert (std::make_pair(elem, AttriValue));
160  }
161  }
162 
163  //--------------------------------------------------
164  // Finished reading. Now ready for use.
165  this->_node_data_closed = true;
166  this->_elem_data_closed = true;
167 
168  node_stream.close();
169  ele_stream.close();
170 }
171 
172 } // namespace libMesh

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo