AbstractMeshWriter.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 
00030 #ifndef _ABSTRACTMESHWRITER_HPP_
00031 #define _ABSTRACTMESHWRITER_HPP_
00032 
00033 #include "AbstractMesh.hpp"
00034 #include <vector>
00035 #include <string>
00036 #include <fstream>
00037 #include <iostream>
00038 #include <sstream>
00039 #include <iomanip>
00040 
00041 #include "Exception.hpp"
00042 #include "OutputFileHandler.hpp"
00043 #include "AbstractMeshReader.hpp"
00044 #include "NodeMap.hpp"
00045 
00046 
00047 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00048 class AbstractMeshWriter
00049 {
00050 protected:
00051     OutputFileHandler *mpOutputFileHandler; 
00052     std::string mBaseName; 
00054     std::vector< std::vector<double> > mNodeData; 
00055     std::vector< std::vector<unsigned> > mElementData; 
00056     std::vector< std::vector<unsigned> > mBoundaryFaceData; 
00058     std::vector< std::vector<double> >::iterator mpNodeIterator; 
00059     std::vector< std::vector<unsigned> >::iterator mpElementIterator; 
00060     std::vector< std::vector<unsigned> >::iterator mpBoundaryFaceIterator; 
00062     bool mIndexFromZero; 
00063     bool mWriteMetaFile;
00064 public:
00066     AbstractMeshWriter(const std::string &rDirectory,
00067                        const std::string &rBaseName,
00068                        const bool clearOutputDir=true)
00069             : mBaseName(rBaseName)
00070     {
00071         mpOutputFileHandler = new OutputFileHandler(rDirectory, clearOutputDir);
00072     }
00074     virtual ~AbstractMeshWriter()
00075     {
00076         delete mpOutputFileHandler;
00077     }
00078     std::string GetOutputDirectory(void);
00079 
00080     void SetNextNode(std::vector<double> nextNode);
00081     virtual void SetNextElement(std::vector<unsigned> nextElement);
00082     void SetNextBoundaryFace(std::vector<unsigned> nextFace);
00083     void SetNextBoundaryEdge(std::vector<unsigned> nextEdge);
00084     virtual void WriteFiles()=0;
00085     unsigned GetNumNodes()
00086     {
00087         return mNodeData.size();
00088     }
00089     unsigned GetNumElements()
00090     {
00091         return mElementData.size();
00092     }
00093     unsigned GetNumBoundaryFaces()
00094     {
00095         return mBoundaryFaceData.size();
00096     }
00097     unsigned GetNumBoundaryEdges()
00098     {
00099         return mBoundaryFaceData.size();
00100     }
00101     void WriteFilesUsingMesh(AbstractMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00102     void WriteFilesUsingMeshReader(AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader);
00103     void WriteFilesUsingMeshReader(AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader,
00104                                    std::vector<unsigned>& rNodePermutation);
00105 };
00106 
00107 
00108 
00109 
00113 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00114 std::string AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetOutputDirectory(void)
00115 {
00116     return mpOutputFileHandler->GetOutputDirectoryFullPath();
00117 }
00118 
00119 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00120 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextNode(std::vector<double> nextNode)
00121 {
00122     assert (nextNode.size() == SPACE_DIM);
00123     mNodeData.push_back(nextNode);
00124 }
00125 
00126 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00127 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextElement(std::vector<unsigned> nextElement)
00128 {
00129     assert (nextElement.size() == ELEMENT_DIM+1);
00130     mElementData.push_back(nextElement);
00131 }
00132 
00133 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00134 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextBoundaryFace(std::vector<unsigned> nextFace)
00135 {
00136     assert (nextFace.size() == ELEMENT_DIM);
00137     mBoundaryFaceData.push_back(nextFace);
00138 }
00139 
00140 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00141 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextBoundaryEdge(std::vector<unsigned> nextEdge)
00142 {
00143     SetNextBoundaryFace(nextEdge);
00144 }
00145 
00146 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00147 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMesh(
00148      AbstractMesh<ELEMENT_DIM,SPACE_DIM>& rMesh)
00149 {
00150     NodeMap node_map(rMesh.GetNumAllNodes());
00151     unsigned new_index=0;
00152     for (unsigned i=0; i<(unsigned)rMesh.GetNumAllNodes();i++)
00153     {
00154         Node<SPACE_DIM>* p_node = rMesh.GetNode(i);
00155 
00156         if (p_node->IsDeleted() == false)
00157         {
00158             std::vector<double> coords(SPACE_DIM);
00159             for (unsigned j=0; j<SPACE_DIM; j++)
00160             {
00161                 coords[j] = p_node->GetPoint()[j];
00162             }
00163             SetNextNode(coords);
00164             node_map.SetNewIndex(i,new_index++);
00165         }
00166         else
00167         {
00168             node_map.SetDeleted(i);
00169         }
00170     }
00171     assert(new_index==(unsigned)rMesh.GetNumNodes());
00172 
00173     // Get an iterator over the elements of the mesh
00174     typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::ElementIterator iter =
00175         rMesh.GetElementIteratorBegin();
00176 
00177     while (iter != rMesh.GetElementIteratorEnd())
00178     {
00179         if ((*iter)->IsDeleted() == false)
00180         {
00181             std::vector<unsigned> indices((*iter)->GetNumNodes());
00182             for (unsigned j=0; j<indices.size(); j++)
00183             {
00184                 unsigned old_index=(*iter)->GetNodeGlobalIndex(j);
00185                 indices[j] = node_map.GetNewIndex(old_index);
00186             }
00187             this->SetNextElement(indices);
00188         }
00189 
00190         iter++;
00191     }
00192 
00193     // Get a iterator over the boundary elements of the mesh
00194     typename AbstractMesh<ELEMENT_DIM, SPACE_DIM>::BoundaryElementIterator boundary_iter =
00195         rMesh.GetBoundaryElementIteratorBegin();
00196     while (boundary_iter != rMesh.GetBoundaryElementIteratorEnd())
00197     {
00198         if ((*boundary_iter)->IsDeleted() == false)
00199         {
00200             std::vector<unsigned> indices(ELEMENT_DIM);
00201             for (unsigned j=0; j<ELEMENT_DIM; j++)
00202             {
00203                 unsigned old_index=(*boundary_iter)->GetNodeGlobalIndex(j);
00204                 indices[j] = node_map.GetNewIndex(old_index);
00205             }
00206             SetNextBoundaryFace(indices);
00207         }
00208         boundary_iter++;
00209     }
00210     WriteFiles();
00211 }
00212 
00213 
00214 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00215 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMeshReader(
00216     AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader)
00217 {
00218     for (unsigned i=0; i<rMeshReader.GetNumNodes();i++)
00219     {
00220         SetNextNode(rMeshReader.GetNextNode());
00221     }
00222     for (unsigned i=0; i<rMeshReader.GetNumElements();i++)
00223     {
00224         this->SetNextElement(rMeshReader.GetNextElementData().NodeIndices);
00225     }
00226     for (unsigned i=0; i<rMeshReader.GetNumFaces();i++)
00227     {
00228         this->SetNextBoundaryFace(rMeshReader.GetNextFaceData().NodeIndices);
00229     }
00230     WriteFiles();
00231 }
00232 
00233 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00234 void AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMeshReader(
00235     AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader,
00236     std::vector<unsigned>& rNodePermutation)
00237 {
00238     if (rNodePermutation.size() == 0)
00239     {
00240        WriteFilesUsingMeshReader(rMeshReader);
00241     }
00242     else
00243     {
00244         mNodeData.resize(rMeshReader.GetNumNodes());        
00245         for (unsigned i=0; i<rMeshReader.GetNumNodes();i++)
00246         {
00247             assert(rNodePermutation[i] < rMeshReader.GetNumNodes());
00248             mNodeData[ rNodePermutation[i] ] = rMeshReader.GetNextNode();
00249         }
00250 
00251         for (unsigned i=0; i<rMeshReader.GetNumElements();i++)
00252         {
00253             ElementData element = rMeshReader.GetNextElementData();
00254             
00255             for(unsigned local_index=0; local_index<element.NodeIndices.size(); local_index++)
00256             {
00257                 unsigned old_index = element.NodeIndices[local_index];
00258                 element.NodeIndices[local_index] = rNodePermutation[old_index];
00259             }
00260             
00261             SetNextElement(element.NodeIndices);
00262         }
00263         
00264         for (unsigned i=0; i<rMeshReader.GetNumFaces();i++)
00265         {
00266             ElementData face = rMeshReader.GetNextFaceData();
00267             
00268             for(unsigned local_index=0; local_index<face.NodeIndices.size(); local_index++)
00269             {
00270                 unsigned old_index = face.NodeIndices[local_index];
00271                 face.NodeIndices[local_index] = rNodePermutation[old_index];
00272             }
00273             
00274             SetNextBoundaryFace(face.NodeIndices);
00275         }
00276         WriteFiles();
00277     }
00278 }
00279 
00280 #endif //_ABSTRACTMESHWRITER_HPP_

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