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

Generated on Mon Apr 18 11:35:35 2011 for Chaste by  doxygen 1.5.5