AbstractCachedMeshReader.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 #ifndef ABSTRACTCACHEDMESHREADER_HPP_
00030 #define ABSTRACTCACHEDMESHREADER_HPP_
00031 
00032 
00048 #include <vector>
00049 #include <string>
00050 #include <fstream>
00051 #include <iostream>
00052 #include <sstream>
00053 #include "Exception.hpp"
00054 #include "AbstractMeshReader.hpp"
00055 
00056 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00057 class AbstractCachedMeshReader : public AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> 
00058 {
00059 protected:
00060     unsigned mNumNodeAttributes; 
00061     unsigned mMaxNodeBdyMarker; 
00062     unsigned mNumElementNodes; 
00063     unsigned mNumElementAttributes; 
00064     unsigned mMaxFaceBdyMarker; 
00066     std::vector<std::string> mNodeRawData;  
00067     std::vector<std::string> mElementRawData;  
00068     std::vector<std::string> mFaceRawData;  
00070     std::vector< std::vector<double> > mNodeData; 
00071     std::vector< std::vector<unsigned> > mElementData; 
00072     std::vector< std::vector<unsigned> > mFaceData; 
00074     std::vector< std::vector<double> >::iterator mpNodeIterator; 
00075     std::vector< std::vector<unsigned> >::iterator mpElementIterator; 
00076     std::vector< std::vector<unsigned> >::iterator mpFaceIterator; 
00078     bool mIndexFromZero; 
00080     std::vector<std::string> GetRawDataFromFile(std::string fileName); 
00083 public:
00084     AbstractCachedMeshReader() 
00085     {
00086         mNumNodeAttributes = 0;
00087         mMaxNodeBdyMarker = 0;
00088         mNumElementNodes = 0;
00089         mNumElementAttributes = 0;
00090         mMaxFaceBdyMarker = 0;
00091 
00092         // We have initialized all numeric variables to zero
00093 
00094         mIndexFromZero = false; // Initially assume that nodes are not numbered from zero
00095     }
00096     virtual ~AbstractCachedMeshReader()
00097     {}
00098 
00099 
00100     unsigned GetNumElements() const
00101     {
00102         return mElementData.size();
00103     } 
00104     unsigned GetNumNodes() const
00105     {
00106         return mNodeData.size();
00107     } 
00108     unsigned GetNumFaces() const
00109     {
00110         return mFaceData.size();
00111     } 
00112     unsigned GetNumEdges() const
00113     {
00114         return mFaceData.size();
00115     }    
00117     unsigned GetMaxNodeIndex(); 
00118     unsigned GetMinNodeIndex(); 
00120     std::vector<double> GetNextNode(); 
00121     void Reset(); 
00122     ElementData GetNextElementData(); 
00123     ElementData GetNextEdgeData(); 
00124     ElementData GetNextFaceData(); 
00125 };
00126 
00127 
00137 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00138 std::vector<std::string> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetRawDataFromFile(std::string fileName)
00139 {
00140     // Open raw data file
00141 
00142     std::vector<std::string> RawDataFromFile;
00143     std::ifstream dataFile(fileName.c_str());
00144 
00145     // Checks that input file has been opened correctly. If not throws an
00146     // exception that should be caught by the user.
00147     if (!dataFile.is_open())
00148     {
00149         EXCEPTION("Could not open data file "+fileName+" .");
00150     }
00151 
00152     // Read each line in turn
00153     std::string RawLineFromFile;
00154     getline(dataFile, RawLineFromFile);
00155 
00156     while (dataFile)
00157     {
00158         //Remove comments (everything from a hash to the end of the line)
00159         //If there is no hash, then hashLocation = string::npos = -1 = 4294967295 = UINT_MAX
00160         //(so it works with unsigneds but is a little nasty)
00161         long hash_location=RawLineFromFile.find('#',0);
00162         if (hash_location >= 0)
00163         {
00164             RawLineFromFile=RawLineFromFile.substr(0,hash_location);
00165         }
00166         //Remove blank lines.  This is unnecessary, since the tokenizer will
00167         //ignore blank lines anyway.
00168         long not_blank_location=RawLineFromFile.find_first_not_of(" \t",0);
00169         if (not_blank_location >= 0)
00170         {
00171             RawDataFromFile.push_back(RawLineFromFile);
00172         }
00173 
00174         // Move onto next line
00175         getline(dataFile, RawLineFromFile);
00176     }
00177 
00178     dataFile.close(); // Closes the data file
00179     return(RawDataFromFile);
00180 }
00181 
00182 
00183 
00188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00189 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMaxNodeIndex()
00190 {
00191     //Initialize an interator for the vector of nodes
00192     std::vector<std::vector<unsigned> >::iterator the_iterator;
00193 
00194     unsigned max_node_index = 0; // Nice if it were negative
00195 
00196     for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00197     {
00198         std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
00199 
00200         for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00201         {
00202             if ( indices[i] >  max_node_index)
00203             {
00204                 max_node_index = indices[i];
00205             }
00206         }
00207     }
00208 
00209     return max_node_index;
00210 }
00211 
00212 
00213 
00218 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00219 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMinNodeIndex()
00220 {
00221     //Initialize an interator for the vector of nodes
00222     std::vector<std::vector<unsigned> >::iterator the_iterator;
00223 
00224     unsigned min_node_index = UINT_MAX; // A large integer
00225 
00226     for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00227     {
00228         std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
00229 
00230         for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00231         {
00232             if (indices[i] < min_node_index)
00233             {
00234                 min_node_index = indices[i];
00235             }
00236         }
00237     }
00238 
00239     return min_node_index;
00240 }
00241 
00242 
00247 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00248 std::vector<double> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextNode()
00249 {
00250     // Checks that there are still some nodes left to read. If not throws an
00251     // exception that must be caught by the user.
00252     if (mpNodeIterator == mNodeData.end())
00253     {
00254         EXCEPTION("All nodes already got");
00255     }
00256 
00257     std::vector<double> next_node = *mpNodeIterator;
00258 
00259     mpNodeIterator++;
00260 
00261     return next_node;
00262 }
00263 
00264 
00265 
00271 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00272 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00273 {
00274     // Checks that there are still some elements left to read. If not throws an
00275     // exception that must be caught by the user.
00276     if (mpElementIterator == mElementData.end())
00277     {
00278         EXCEPTION("All elements already got");
00279     }
00280 
00281     ElementData ret;
00282     ret.NodeIndices = *mpElementIterator;
00283     ret.AttributeValue = 0;
00284 
00285     mpElementIterator++;
00286 
00287     return ret;
00288 }
00289 
00290 
00291 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00292 void AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::Reset()
00293 {
00294     mpElementIterator = mElementData.begin();
00295     mpFaceIterator = mFaceData.begin();
00296     mpNodeIterator = mNodeData.begin();
00297 }
00298 
00299 
00307 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00308 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextFaceData()
00309 {
00310     // Checks that there are still some faces left to read. If not throws an
00311     // exception that must be caught by the user.
00312     if (mpFaceIterator == mFaceData.end())
00313     {
00314         EXCEPTION("All faces (or edges) already got");
00315     }
00316 
00317     ElementData ret;
00318     ret.NodeIndices = *mpFaceIterator;
00319     ret.AttributeValue = 0;
00320 
00321     mpFaceIterator++;
00322 
00323     return ret;
00324 }
00325 
00326 
00327 
00335 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00336 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextEdgeData()
00337 {
00338     return GetNextFaceData();
00339 }
00340 
00341 
00342 #endif /*ABSTRACTCACHEDMESHREADER_HPP_*/

Generated on Wed Mar 18 12:51:52 2009 for Chaste by  doxygen 1.5.5