Chaste Release::3.1
AbstractCachedMeshReader.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "AbstractCachedMeshReader.hpp"
00037 #include "Exception.hpp"
00038 
00039 #include <fstream>
00040 
00042 // Implementation
00044 
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::AbstractCachedMeshReader()
00047     : mNumNodeAttributes(0),
00048       mMaxNodeBdyMarker(0),
00049       mNumElementNodes(0),
00050       mNumElementAttributes(0),
00051       mMaxFaceBdyMarker(0),
00052       mIndexFromZero(false) // Initially assume that nodes are not numbered from zero
00053 {
00054     // We have initialized all numeric variables to zero
00055 }
00056 
00057 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00058 std::vector<std::string> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetRawDataFromFile(
00059         const std::string& rFileName)
00060 {
00061     // Open raw data file
00062 
00063     std::vector<std::string> raw_data;
00064     std::ifstream data_file(rFileName.c_str());
00065 
00066     // Checks that input file has been opened correctly. If not throws an
00067     // exception that should be caught by the user.
00068     if (!data_file.is_open())
00069     {
00070         EXCEPTION("Could not open data file " + rFileName);
00071     }
00072 
00073     // Read each line in turn
00074     std::string raw_line;
00075     getline(data_file, raw_line);
00076 
00077     while (data_file)
00078     {
00079         // Remove comments (everything from a hash to the end of the line)
00080         // If there is no hash, then hashLocation = string::npos = -1 = 4294967295 = UINT_MAX
00081         // (so it works with unsigneds but is a little nasty)
00082         long hash_location = raw_line.find('#', 0);
00083         if (hash_location >= 0)
00084         {
00085             raw_line = raw_line.substr(0, hash_location);
00086         }
00087         // Remove blank lines.  This is unnecessary, since the tokenizer will
00088         // ignore blank lines anyway.
00089         long not_blank_location = raw_line.find_first_not_of(" \t", 0);
00090         if (not_blank_location >= 0)
00091         {
00092             raw_data.push_back(raw_line);
00093         }
00094 
00095         // Move onto next line
00096         getline(data_file, raw_line);
00097     }
00098 
00099     data_file.close(); // Closes the data file
00100     return raw_data;
00101 }
00102 
00103 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00104 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMaxNodeIndex()
00105 {
00106     // Initialize an interator for the vector of nodes
00107     std::vector<std::vector<unsigned> >::iterator the_iterator;
00108 
00109     unsigned max_node_index = 0; // Nice if it were negative
00110 
00111     for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00112     {
00113         std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
00114 
00115         for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00116         {
00117             if ( indices[i] >  max_node_index)
00118             {
00119                 max_node_index = indices[i];
00120             }
00121         }
00122     }
00123 
00124     return max_node_index;
00125 }
00126 
00127 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00128 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetMinNodeIndex()
00129 {
00130     // Initialize an interator for the vector of nodes
00131     std::vector<std::vector<unsigned> >::iterator the_iterator;
00132 
00133     unsigned min_node_index = UINT_MAX; // A large integer
00134 
00135     for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
00136     {
00137         std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
00138 
00139         for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
00140         {
00141             if (indices[i] < min_node_index)
00142             {
00143                 min_node_index = indices[i];
00144             }
00145         }
00146     }
00147 
00148     return min_node_index;
00149 }
00150 
00151 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00152 std::vector<double> AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextNode()
00153 {
00154     // Checks that there are still some nodes left to read. If not throws an
00155     // exception that must be caught by the user.
00156     if (mpNodeIterator == mNodeData.end())
00157     {
00158         EXCEPTION("All nodes already got");
00159     }
00160 
00161     std::vector<double> next_node = *mpNodeIterator;
00162 
00163     mpNodeIterator++;
00164 
00165     return next_node;
00166 }
00167 
00168 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00169 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextElementData()
00170 {
00171     // Checks that there are still some elements left to read. If not throws an
00172     // exception that must be caught by the user.
00173     if (mpElementIterator == mElementData.end())
00174     {
00175         EXCEPTION("All elements already got");
00176     }
00177 
00178     ElementData ret;
00179     ret.NodeIndices = *mpElementIterator;
00180     ret.AttributeValue = 0;
00181 
00182     mpElementIterator++;
00183 
00184     return ret;
00185 }
00186 
00187 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00188 void AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::Reset()
00189 {
00190     mpElementIterator = mElementData.begin();
00191     mpFaceIterator = mFaceData.begin();
00192     mpNodeIterator = mNodeData.begin();
00193 }
00194 
00195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00196 ElementData AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNextFaceData()
00197 {
00198     // Checks that there are still some faces left to read. If not throws an
00199     // exception that must be caught by the user.
00200     if (mpFaceIterator == mFaceData.end())
00201     {
00202         EXCEPTION("All faces (or edges) already got");
00203     }
00204 
00205     ElementData ret;
00206     ret.NodeIndices = *mpFaceIterator;
00207     ret.AttributeValue = 0;
00208 
00209     mpFaceIterator++;
00210 
00211     return ret;
00212 }
00213 
00214 
00215 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00216 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumElements() const
00217 {
00218     return mElementData.size();
00219 }
00220 
00221 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00222 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumNodes() const
00223 {
00224     return mNodeData.size();
00225 }
00226 
00227 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00228 unsigned AbstractCachedMeshReader<ELEMENT_DIM, SPACE_DIM>::GetNumFaces() const
00229 {
00230     return mFaceData.size();
00231 }
00232 
00233 
00235 // Explicit instantiation
00237 
00238 template class AbstractCachedMeshReader<1,1>;
00239 template class AbstractCachedMeshReader<1,2>;
00240 template class AbstractCachedMeshReader<1,3>;
00241 template class AbstractCachedMeshReader<2,2>;
00242 template class AbstractCachedMeshReader<2,3>;
00243 template class AbstractCachedMeshReader<3,3>;