TrianglesMeshWriter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 "TrianglesMeshWriter.hpp"
00030 #include "Version.hpp"
00031 
00032 #include <cassert>
00033 
00035 // Implementation
00037 
00038 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::TrianglesMeshWriter(
00040     const std::string &rDirectory,
00041     const std::string &rBaseName,
00042     const bool clearOutputDir)
00043         : AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, clearOutputDir),
00044           mFilesAreBinary(false)
00045 {
00046 }
00047 
00048 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::~TrianglesMeshWriter()
00050 {
00051 }
00052 
00053 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00054 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetWriteFilesAsBinary()
00055 {
00056     mFilesAreBinary=true;
00057 }
00058 
00059 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00060 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFiles()
00061 {
00062     std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00063 
00064     // Write node file
00065     std::string node_file_name = this->mBaseName + ".node";
00066     out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00067 
00068     // Write the node header
00069     unsigned num_attr = 0;
00070     unsigned max_bdy_marker = 0;
00071     unsigned num_nodes = this->GetNumNodes();
00072 
00073     *p_node_file << num_nodes << "\t";
00074     *p_node_file << SPACE_DIM << "\t";
00075     *p_node_file << num_attr << "\t";
00076     *p_node_file << max_bdy_marker;
00077     if (mFilesAreBinary)
00078     {
00079         *p_node_file << "\tBIN\n";
00080     }
00081     else
00082     {
00083         *p_node_file << "\n";
00084     }
00085 
00086     *p_node_file << std::setprecision(20);
00087 
00088     // Write each node's data
00089     unsigned default_marker = UINT_MAX;
00090     for (unsigned item_num=0; item_num<num_nodes; item_num++)
00091     {
00092         WriteItem(p_node_file, item_num, this->GetNextNode(), default_marker);
00093     }
00094     *p_node_file << comment << "\n";
00095     p_node_file->close();
00096 
00097     if (ELEMENT_DIM < SPACE_DIM)
00098     {
00099         WriteElementsAsFaces();
00100         WriteFacesAsEdges();
00101         return;
00102     }
00103 
00104     // Write element file
00105     std::string element_file_name = this->mBaseName + ".ele";
00106     out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00107 
00108     // Write the element header
00109     unsigned num_elements = this->GetNumElements();
00110     num_attr = 1u; // We have a single region code
00111 
00112     ElementData element_data = this->GetNextElement();
00113 
00114     unsigned nodes_per_element = element_data.NodeIndices.size();
00115     if (nodes_per_element != ELEMENT_DIM+1)
00116     {
00117         // Check that this is a quadratic mesh
00118         assert(ELEMENT_DIM == SPACE_DIM);
00119         assert(nodes_per_element == (ELEMENT_DIM+1)*(ELEMENT_DIM+2)/2);
00120      }
00121 
00122     *p_element_file << num_elements << "\t";
00123     *p_element_file << nodes_per_element << "\t";
00124     *p_element_file << num_attr;
00125     if (mFilesAreBinary)
00126     {
00127         *p_element_file << "\tBIN\n";
00128     }
00129     else
00130     {
00131         *p_element_file << "\n";
00132     }
00133 
00134     // Write each element's data
00135     for (unsigned item_num=0; item_num<num_elements; item_num++)
00136     {
00137         // if item_num==0 we will already got the element above (in order to
00138         // get the number of nodes per element
00139         if (item_num>0)
00140         {
00141             element_data = this->GetNextElement();
00142         }
00143 
00144         WriteItem(p_element_file, item_num, element_data.NodeIndices, element_data.AttributeValue);
00145     }
00146     *p_element_file << comment << "\n";
00147     p_element_file->close();
00148 
00149     // Write boundary face file
00150     std::string face_file_name = this->mBaseName;
00151 
00152     if (SPACE_DIM == 1)
00153     {
00154         // In 1-D there is no boundary file.  It's trivial to calculate
00155         return;
00156     }
00157     else if (SPACE_DIM == 2)
00158     {
00159         face_file_name = face_file_name + ".edge";
00160     }
00161     else
00162     {
00163         face_file_name = face_file_name + ".face";
00164     }
00165     out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00166 
00167     // Write the boundary face header
00168     unsigned num_faces = this->GetNumBoundaryFaces();
00169 
00170     *p_face_file << num_faces << "\t";
00171     *p_face_file << max_bdy_marker;
00172     if (mFilesAreBinary)
00173     {
00174         *p_face_file << "\tBIN\n";
00175     }
00176     else
00177     {
00178         *p_face_file << "\n";
00179     }
00180 
00181     // Write each face's data
00182     default_marker = UINT_MAX;
00183     for (unsigned item_num=0; item_num<num_faces; item_num++)
00184     {
00185         WriteItem(p_face_file, item_num, this->mBoundaryFaceData[item_num], default_marker);
00186     }
00187     *p_face_file << comment << "\n";
00188     p_face_file->close();
00189 }
00190 
00191 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00192 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteElementsAsFaces()
00193 {
00194     std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00195 
00196     std::string element_file_name = this->mBaseName;
00197     if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
00198     {
00199         element_file_name = element_file_name + ".edge";
00200     }
00201     else if (ELEMENT_DIM == 2 && SPACE_DIM == 3)
00202     {
00203         element_file_name = element_file_name + ".face";
00204     }
00205 
00206     out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00207 
00208     // Write the element header
00209     unsigned num_elements = this->GetNumElements();
00210     assert(SPACE_DIM != ELEMENT_DIM);
00211     unsigned num_attr = 0;
00212 
00213     *p_element_file << num_elements << "\t";
00214     //*p_element_file << nodes_per_element << "\t";
00215     *p_element_file << num_attr;
00216     if (mFilesAreBinary)
00217     {
00218         *p_element_file << "\tBIN\n";
00219     }
00220     else
00221     {
00222         *p_element_file << "\n";
00223     }
00224 
00225     // Write each element's data
00226     for (unsigned item_num=0; item_num<num_elements; item_num++)
00227     {
00228          WriteItem(p_element_file, item_num, this->GetNextElement().NodeIndices);
00229     }
00230     *p_element_file << comment << "\n";
00231     p_element_file->close();
00232 
00233 }
00234 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00235 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFacesAsEdges()
00236 {
00237     std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00238 
00239     if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
00240     {
00241         return;
00242     }
00243 
00244     assert(SPACE_DIM == 3 && ELEMENT_DIM == 2);
00245 
00246     std::string face_file_name = this->mBaseName;
00247     face_file_name = face_file_name + ".edge";
00248 
00249     out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00250 
00251     // Write the boundary face header
00252     unsigned num_faces = this->GetNumBoundaryFaces();
00253 
00254     unsigned max_bdy_marker = 0;
00255     unsigned default_marker = UINT_MAX;
00256 
00257     *p_face_file << num_faces << "\t";
00258     *p_face_file << max_bdy_marker;
00259     if (mFilesAreBinary)
00260     {
00261         *p_face_file << "\tBIN\n";
00262     }
00263     else
00264     {
00265         *p_face_file << "\n";
00266     }
00267     // Write each face's data
00268     for (unsigned item_num=0; item_num<num_faces; item_num++)
00269     {
00270         WriteItem(p_face_file, item_num, this->mBoundaryFaceData[item_num], default_marker);
00271     }
00272     *p_face_file << comment << "\n";
00273     p_face_file->close();
00274 }
00275 
00276 
00277 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00278 template<class T>
00279 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteItem(out_stream &pFile, unsigned itemNumber,
00280                                                     const std::vector<T> &dataPacket, unsigned attribute)
00281 {
00282     if (mFilesAreBinary)
00283     {
00284         //No item numbers
00285         //Write raw data out of std::vector into the file
00286         pFile->write((char*)&dataPacket[0], dataPacket.size()*sizeof(T));
00287         //Write raw attribute
00288         if (attribute != UINT_MAX)
00289         {
00290             pFile->write((char*) &attribute, sizeof(attribute));
00291         }
00292     }
00293     else
00294     {
00295         *pFile << itemNumber;
00296         for (unsigned i=0; i<dataPacket.size(); i++)
00297         {
00298             *pFile << "\t" << dataPacket[i];
00299         }
00300         if (attribute != UINT_MAX)
00301         {
00302             *pFile << "\t" << attribute;
00303         }
00304         *pFile << "\n";
00305     }
00306 }
00308 // Explicit instantiation
00310 
00311 template class TrianglesMeshWriter<1,1>;
00312 template class TrianglesMeshWriter<1,2>;
00313 template class TrianglesMeshWriter<1,3>;
00314 template class TrianglesMeshWriter<2,2>;
00315 template class TrianglesMeshWriter<2,3>;
00316 template class TrianglesMeshWriter<3,3>;
00317 
00322 template void TrianglesMeshWriter<1, 1>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00323 template void TrianglesMeshWriter<1, 1>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00324 template void TrianglesMeshWriter<1, 2>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00325 template void TrianglesMeshWriter<1, 2>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00326 template void TrianglesMeshWriter<1, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00327 template void TrianglesMeshWriter<1, 3>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00328 template void TrianglesMeshWriter<2, 2>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00329 template void TrianglesMeshWriter<2, 2>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00330 template void TrianglesMeshWriter<2, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00331 template void TrianglesMeshWriter<2, 3>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00332 template void TrianglesMeshWriter<3, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00333 template void TrianglesMeshWriter<3, 3>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );

Generated on Mon Nov 1 12:35:24 2010 for Chaste by  doxygen 1.5.5