PottsMeshReader.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "PottsMeshReader.hpp"
00030 #include "Exception.hpp"
00031 
00032 #include <sstream>
00033 
00034 template<unsigned SPACE_DIM>
00035 PottsMeshReader<SPACE_DIM>::PottsMeshReader(std::string pathBaseName)
00036     : mFilesBaseName(pathBaseName),
00037       mIndexFromZero(false), // initially assume that nodes are not numbered from zero
00038       mNumNodes(0),
00039       mNumElements(0),
00040       mNodesRead(0),
00041       mElementsRead(0),
00042       mNumElementAttributes(0)
00043 {
00044     OpenFiles();
00045     ReadHeaders();
00046 }
00047 
00048 template<unsigned SPACE_DIM>
00049 unsigned PottsMeshReader<SPACE_DIM>::GetNumElements() const
00050 {
00051     return mNumElements;
00052 }
00053 
00054 template<unsigned SPACE_DIM>
00055 unsigned PottsMeshReader<SPACE_DIM>::GetNumNodes() const
00056 {
00057     return mNumNodes;
00058 }
00059 
00060 template<unsigned SPACE_DIM>
00061 unsigned PottsMeshReader<SPACE_DIM>::GetNumElementAttributes() const
00062 {
00063     return mNumElementAttributes;
00064 }
00065 
00066 template<unsigned SPACE_DIM>
00067 ElementData PottsMeshReader<SPACE_DIM>::GetNextFaceData()
00068 {
00070     ElementData ret;
00071     ret.NodeIndices = std::vector<unsigned>();
00072     ret.AttributeValue = 0;
00073     return ret;
00074 }
00075 
00076 template<unsigned SPACE_DIM>
00077 unsigned PottsMeshReader<SPACE_DIM>::GetNumFaces() const
00078 {
00080     return 0;
00081 }
00082 
00083 template<unsigned SPACE_DIM>
00084 void PottsMeshReader<SPACE_DIM>::Reset()
00085 {
00086     CloseFiles();
00087     OpenFiles();
00088     ReadHeaders();
00089 
00090     mNodesRead = 0;
00091     mElementsRead = 0;
00092 }
00093 
00094 template<unsigned SPACE_DIM>
00095 std::vector<double> PottsMeshReader<SPACE_DIM>::GetNextNode()
00096 {
00097     std::vector<double> node_data;
00098 
00099     std::string buffer;
00100     GetNextLineFromStream(mNodesFile, buffer);
00101 
00102     std::stringstream buffer_stream(buffer);
00103 
00104     unsigned index;
00105     buffer_stream >> index;
00106 
00107     unsigned offset = mIndexFromZero ? 0 : 1;
00108     if (index != mNodesRead + offset)
00109     {
00110         EXCEPTION("Data for node " << mNodesRead << " missing");
00111     }
00112 
00113     double node_value;
00114     for (unsigned i=0; i<SPACE_DIM+1; i++)
00115     {
00116         buffer_stream >> node_value;
00117         node_data.push_back(node_value);
00118     }
00119 
00120     mNodesRead++;
00121     return node_data;
00122 }
00123 
00124 template<unsigned SPACE_DIM>
00125 ElementData PottsMeshReader<SPACE_DIM>::GetNextElementData()
00126 {
00127     // Create data structure for this element
00128     ElementData element_data;
00129 
00130     std::string buffer;
00131     GetNextLineFromStream(mElementsFile, buffer);
00132 
00133     std::stringstream buffer_stream(buffer);
00134 
00135     unsigned element_index;
00136     buffer_stream >> element_index;
00137 
00138     unsigned offset = mIndexFromZero ? 0 : 1;
00139     if (element_index != mElementsRead + offset)
00140     {
00141         EXCEPTION("Data for element " << mElementsRead << " missing");
00142     }
00143 
00144     unsigned num_nodes_in_element;
00145     buffer_stream >> num_nodes_in_element;
00146 
00147     // Store node indices owned by this element
00148     unsigned node_index;
00149     for (unsigned i=0; i<num_nodes_in_element; i++)
00150     {
00151         buffer_stream >> node_index;
00152         element_data.NodeIndices.push_back(node_index - offset);
00153     }
00154 
00155     if (mNumElementAttributes > 0)
00156     {
00157         assert(mNumElementAttributes==1);
00158 
00159         unsigned attribute_value;
00160         buffer_stream >> attribute_value;
00161         element_data.AttributeValue = attribute_value;
00162     }
00163     else
00164     {
00165         element_data.AttributeValue = 0;
00166     }
00167 
00168     mElementsRead++;
00169     return element_data;
00170 }
00171 
00172 template<unsigned SPACE_DIM>
00173 void PottsMeshReader<SPACE_DIM>::OpenFiles()
00174 {
00175     OpenNodeFile();
00176     OpenElementsFile();
00177 }
00178 
00179 template<unsigned SPACE_DIM>
00180 void PottsMeshReader<SPACE_DIM>::OpenNodeFile()
00181 {
00182     // Nodes definition
00183     std::string file_name = mFilesBaseName + ".node";
00184     mNodesFile.open(file_name.c_str());
00185     if (!mNodesFile.is_open())
00186     {
00187         EXCEPTION("Could not open data file: " + file_name);
00188     }
00189 }
00190 
00191 template<unsigned SPACE_DIM>
00192 void PottsMeshReader<SPACE_DIM>::OpenElementsFile()
00193 {
00194     // Elements definition
00195     std::string file_name;
00196     file_name = mFilesBaseName + ".cell";
00197 
00198     mElementsFile.open(file_name.c_str());
00199     if (!mElementsFile.is_open())
00200     {
00201         EXCEPTION("Could not open data file: " + file_name);
00202     }
00203 }
00204 
00205 template<unsigned SPACE_DIM>
00206 void PottsMeshReader<SPACE_DIM>::ReadHeaders()
00207 {
00208     std::string buffer;
00209 
00210     GetNextLineFromStream(mNodesFile, buffer);
00211     std::stringstream buffer_stream(buffer);
00212     buffer_stream >> mNumNodes >> mNumNodeAttributes;
00213 
00214     // Get the next line to see if nodes are indexed from zero or not
00215     GetNextLineFromStream(mNodesFile, buffer);
00216     std::stringstream node_buffer_stream(buffer);
00217 
00218     unsigned first_index;
00219     node_buffer_stream >> first_index;
00220     assert(first_index == 0 || first_index == 1);
00221     mIndexFromZero = (first_index == 0);
00222 
00223     // Close, reopen, skip header
00224     mNodesFile.close();
00225     OpenNodeFile();
00226     GetNextLineFromStream(mNodesFile, buffer);
00227 
00228     GetNextLineFromStream(mElementsFile, buffer);
00229     std::stringstream element_buffer_stream(buffer);
00230 
00231     element_buffer_stream >> mNumElements >> mNumElementAttributes;
00232 }
00233 
00234 template<unsigned SPACE_DIM>
00235 void PottsMeshReader<SPACE_DIM>::CloseFiles()
00236 {
00237     mNodesFile.close();
00238     mElementsFile.close();
00239 }
00240 
00241 template<unsigned SPACE_DIM>
00242 void PottsMeshReader<SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
00243 {
00244     bool line_is_blank;
00245 
00246     do
00247     {
00248         getline(fileStream, rawLine);
00249 
00250         if (fileStream.eof())
00251         {
00252             EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
00253         }
00254 
00255         // Get rid of any comment
00256         rawLine = rawLine.substr(0,rawLine.find('#', 0));
00257 
00258         line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
00259     }
00260     while (line_is_blank);
00261 }
00262 
00264 // Explicit instantiation
00266 
00267 template class PottsMeshReader<1>;
00268 template class PottsMeshReader<2>;
00269 template class PottsMeshReader<3>;
Generated on Thu Dec 22 13:00:04 2011 for Chaste by  doxygen 1.6.3