$extrastylesheet
mesh_data_tetgen_support.C
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 // C++ includes
00021 #include <fstream>
00022 
00023 // Local includes
00024 #include "libmesh/mesh_data.h"
00025 
00026 namespace libMesh
00027 {
00028 
00029 
00030 
00031 
00032 //---------------------------------------------------------------------
00033 // MeshDate TetGen support function
00034 void MeshData::read_tetgen (const std::string& name)
00035 {
00036   std::string name_node, name_ele, dummy;
00037   std::string desc = name;
00038 
00039 
00040   // Check name for *.node or *.ele extension.
00041   // Set std::istream for node_stream and ele_stream.
00042   if (name.rfind(".node") < name.size())
00043     {
00044       name_node = name;
00045       dummy     = name;
00046       std::size_t position = dummy.rfind(".node");
00047       name_ele     = dummy.replace(position, 5, ".ele");
00048       desc.erase(position);
00049     }
00050   else if (name.rfind(".ele") < name.size())
00051     {
00052       name_ele = name;
00053       dummy    = name;
00054       std::size_t position = dummy.rfind(".ele");
00055       name_node    = dummy.replace(position, 4, ".node");
00056       desc.erase(position);
00057     }
00058   else
00059     libmesh_error_msg("ERROR: Unrecognized file name: " << name);
00060 
00061   // Set the streams from which to read in.
00062   std::ifstream node_stream (name_node.c_str());
00063   std::ifstream ele_stream  (name_ele.c_str());
00064 
00065   if ( !node_stream.good() || !ele_stream.good() )
00066     libmesh_error_msg("ERROR: One or both Input file(s) not good.\n"  \
00067                       << "Error opening files "                       \
00068                       << name_node                                    \
00069                       << " and "                                      \
00070                       << name_ele);
00071 
00072 
00073   // Set the descriptive name.
00074   // TetGen won't give a name, so we use the filename.
00075   this->_data_descriptor = desc;
00076 
00077 
00078   //--------------------------------------------------
00079   // Read in the data associated with the nodes.
00080   {
00081     unsigned int n_node=0, f_n_id=0, nAttri=0, BoundMark=0;
00082     Real dummy_val=0.0;
00083     std::vector<Number> AttriValue;
00084 
00085     // Read the parameters from the node_stream.
00086     node_stream >> n_node     // Read the number of nodes
00087                 >> dummy_val  // Read the dimension
00088                 >> nAttri     // Read the number of attributes
00089                 >> BoundMark; // (0 or 1) boundary markers are in the stream or not.
00090 
00091     // Resize the values vector.
00092     AttriValue.resize(nAttri);
00093 
00094     for (unsigned int i=0; i<n_node; i++)
00095       {
00096         node_stream >> f_n_id;
00097 
00098 
00099         // Read the nodal coordinates for this node into dummy_val,
00100         // since we don't need them.
00101         for (unsigned int j=0; j<3; j++)
00102           node_stream >> dummy_val;
00103 
00104         // Read the attributes from the stream.
00105         for (unsigned int j=0; j<nAttri; j++)
00106           node_stream >> AttriValue[j];
00107 
00108         // Read boundary marker if BoundaryMarker=1.
00109         if (BoundMark == 1)
00110           node_stream >> dummy_val;
00111 
00112         // For the foreign node id locate the Node*.
00113         const Node* node = foreign_id_to_node(f_n_id);
00114 
00115         // Insert this node and the values in our _node_data.
00116         _node_data.insert (std::make_pair(node, AttriValue));
00117       }
00118   }
00119 
00120 
00121   //--------------------------------------------------
00122   // Read in the data associated with the elements.
00123   {
00124     unsigned int n_elem, f_e_id, n_nodes, nAttri=0;
00125     Real dummy_val=0.0;
00126     std::vector<Number> AttriValue;
00127 
00128     // Read the parameters from the ele_stream.
00129     ele_stream >> n_elem   // Read the number of tetrahedrons
00130                >> n_nodes  // Read the points per tetrahedron
00131                >> nAttri;  // Read the number of attributes
00132 
00133     // Resize the values vector.
00134     AttriValue.resize(nAttri);
00135 
00136     for (unsigned int i=0; i<n_elem; i++)
00137       {
00138         ele_stream >> f_e_id;
00139 
00140         // For the number of nodes for this element read them into dummy_val,
00141         // since we don't need them.
00142         for (unsigned int n=0; n<n_nodes; n++)
00143           ele_stream >> dummy_val;
00144 
00145         // Read the attributes from the stream.
00146         for (unsigned int j=0; j<nAttri; j++)
00147           ele_stream >> AttriValue[j];
00148 
00149         // For the foreign elem id locate the Elem*.
00150         const Elem* elem = foreign_id_to_elem(f_e_id);
00151 
00152         // Insert this elem and the values in our _elem_data.
00153         _elem_data.insert (std::make_pair(elem, AttriValue));
00154       }
00155   }
00156 
00157   //--------------------------------------------------
00158   // Finished reading.  Now ready for use.
00159   this->_node_data_closed = true;
00160   this->_elem_data_closed = true;
00161 
00162   node_stream.close();
00163   ele_stream.close();
00164 }
00165 
00166 } // namespace libMesh