AbstractCachedMeshReader.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 "AbstractCachedMeshReader.hpp"
00030 #include "Exception.hpp"
00031 
00032 #include <fstream>
00033 
00035 // Implementation
00037 
00038 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::AbstractCachedMeshReader()
00040     : mNumNodeAttributes(0),
00041       mMaxNodeBdyMarker(0),
00042       mNumElementNodes(0),
00043       mNumElementAttributes(0),
00044       mMaxFaceBdyMarker(0),
00045       mIndexFromZero(false) // Initially assume that nodes are not numbered from zero
00046 {
00047     // We have initialized all numeric variables to zero
00048 }
00049 
00050 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00051 std::vector<std::string> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetRawDataFromFile(
00052         const std::string& rFileName)
00053 {
00054     // Open raw data file
00055 
00056     std::vector<std::string> raw_data;
00057     std::ifstream data_file(rFileName.c_str());
00058 
00059     // Checks that input file has been opened correctly. If not throws an
00060     // exception that should be caught by the user.
00061     if (!data_file.is_open())
00062     {
00063         EXCEPTION("Could not open data file " + rFileName);
00064     }
00065 
00066     // Read each line in turn
00067     std::string raw_line;
00068     getline(data_file, raw_line);
00069 
00070     while (data_file)
00071     {
00072         // Remove comments (everything from a hash to the end of the line)
00073         // If there is no hash, then hashLocation = string::npos = -1 = 4294967295 = UINT_MAX
00074         // (so it works with unsigneds but is a little nasty)
00075         long hash_location = raw_line.find('#', 0);
00076         if (hash_location >= 0)
00077         {
00078             raw_line = raw_line.substr(0, hash_location);
00079         }
00080         // Remove blank lines.  This is unnecessary, since the tokenizer will
00081         // ignore blank lines anyway.
00082         long not_blank_location = raw_line.find_first_not_of(" \t", 0);
00083         if (not_blank_location >= 0)
00084         {
00085             raw_data.push_back(raw_line);
00086         }
00087 
00088         // Move onto next line
00089         getline(data_file, raw_line);
00090     }
00091 
00092     data_file.close(); // Closes the data file
00093     return raw_data;
00094 }
00095 
00096 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00097 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMaxNodeIndex()
00098 {
00099     // Initialize an interator for the vector of nodes
00100     std::vector<std::vector<unsigned> >::iterator the_iterator;
00101 
00102     unsigned max_node_index = 0; // Nice if it were negative
00103 
00104     for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00105     {
00106         std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
00107 
00108         for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00109         {
00110             if ( indices[i] >  max_node_index)
00111             {
00112                 max_node_index = indices[i];
00113             }
00114         }
00115     }
00116 
00117     return max_node_index;
00118 }
00119 
00120 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00121 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMinNodeIndex()
00122 {
00123     // Initialize an interator for the vector of nodes
00124     std::vector<std::vector<unsigned> >::iterator the_iterator;
00125 
00126     unsigned min_node_index = UINT_MAX; // A large integer
00127 
00128     for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00129     {
00130         std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
00131 
00132         for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00133         {
00134             if (indices[i] < min_node_index)
00135             {
00136                 min_node_index = indices[i];
00137             }
00138         }
00139     }
00140 
00141     return min_node_index;
00142 }
00143 
00144 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00145 std::vector<double> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextNode()
00146 {
00147     // Checks that there are still some nodes left to read. If not throws an
00148     // exception that must be caught by the user.
00149     if (mpNodeIterator == mNodeData.end())
00150     {
00151         EXCEPTION("All nodes already got");
00152     }
00153 
00154     std::vector<double> next_node = *mpNodeIterator;
00155 
00156     mpNodeIterator++;
00157 
00158     return next_node;
00159 }
00160 
00161 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00162 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00163 {
00164     // Checks that there are still some elements left to read. If not throws an
00165     // exception that must be caught by the user.
00166     if (mpElementIterator == mElementData.end())
00167     {
00168         EXCEPTION("All elements already got");
00169     }
00170 
00171     ElementData ret;
00172     ret.NodeIndices = *mpElementIterator;
00173     ret.AttributeValue = 0;
00174 
00175     mpElementIterator++;
00176 
00177     return ret;
00178 }
00179 
00180 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00181 void AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::Reset()
00182 {
00183     mpElementIterator = mElementData.begin();
00184     mpFaceIterator = mFaceData.begin();
00185     mpNodeIterator = mNodeData.begin();
00186 }
00187 
00188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00189 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextFaceData()
00190 {
00191     // Checks that there are still some faces left to read. If not throws an
00192     // exception that must be caught by the user.
00193     if (mpFaceIterator == mFaceData.end())
00194     {
00195         EXCEPTION("All faces (or edges) already got");
00196     }
00197 
00198     ElementData ret;
00199     ret.NodeIndices = *mpFaceIterator;
00200     ret.AttributeValue = 0;
00201 
00202     mpFaceIterator++;
00203 
00204     return ret;
00205 }
00206 
00207 
00208 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00209 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumElements() const
00210 {
00211     return mElementData.size();
00212 }
00213 
00214 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00215 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumNodes() const
00216 {
00217     return mNodeData.size();
00218 }
00219 
00220 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00221 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumFaces() const
00222 {
00223     return mFaceData.size();
00224 }
00225 
00226 
00228 // Explicit instantiation
00230 
00231 template class AbstractCachedMeshReader<1,1>;
00232 template class AbstractCachedMeshReader<1,2>;
00233 template class AbstractCachedMeshReader<1,3>;
00234 template class AbstractCachedMeshReader<2,2>;
00235 template class AbstractCachedMeshReader<2,3>;
00236 template class AbstractCachedMeshReader<3,3>;

Generated on Tue May 31 14:31:48 2011 for Chaste by  doxygen 1.5.5