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     // The condition below allows the writer to cope with a NodesOnlyMesh
00113     if (num_elements == 0)
00114     {
00115         *p_element_file << 0 << "\t";
00116         *p_element_file << 0 << "\t";
00117         *p_element_file << 0;
00118         if (this->mFilesAreBinary)
00119         {
00120             *p_element_file << "\tBIN\n";
00121         }
00122         else
00123         {
00124             *p_element_file << "\n";
00125         }
00126         p_element_file->close();
00127     }
00128     else
00129     {
00130         ElementData element_data = this->GetNextElement();
00131     
00132         unsigned nodes_per_element = element_data.NodeIndices.size();
00133         if (nodes_per_element != ELEMENT_DIM+1)
00134         {
00135             // Check that this is a quadratic mesh
00136             assert(ELEMENT_DIM == SPACE_DIM);
00137             assert(nodes_per_element == (ELEMENT_DIM+1)*(ELEMENT_DIM+2)/2);
00138          }
00139     
00140         *p_element_file << num_elements << "\t";
00141         *p_element_file << nodes_per_element << "\t";
00142         *p_element_file << num_attr;
00143         if (this->mFilesAreBinary)
00144         {
00145             *p_element_file << "\tBIN\n";
00146         }
00147         else
00148         {
00149             *p_element_file << "\n";
00150         }
00151     
00152         // Write each element's data
00153         for (unsigned item_num=0; item_num<num_elements; item_num++)
00154         {
00155             // if item_num==0 we will already got the element above (in order to
00156             // get the number of nodes per element
00157             if (item_num>0)
00158             {
00159                 element_data = this->GetNextElement();
00160             }
00161     
00162             WriteItem(p_element_file, item_num, element_data.NodeIndices, element_data.AttributeValue);
00163         }
00164         *p_element_file << comment << "\n";
00165         p_element_file->close();
00166     }
00167 
00168     // Write boundary face file
00169     std::string face_file_name = this->mBaseName;
00170 
00171     if (ELEMENT_DIM == 1)
00172     {
00173         // In 1-D there is no boundary file.  It's trivial to calculate
00174         return;
00175     }
00176     else if (ELEMENT_DIM == 2)
00177     {
00178         face_file_name = face_file_name + ".edge";
00179     }
00180     else
00181     {
00182         face_file_name = face_file_name + ".face";
00183     }
00184     out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00185 
00186     // Write the boundary face header
00187     if (num_elements != 0)
00188     {
00189         unsigned num_faces = this->GetNumBoundaryFaces();
00190     
00191         *p_face_file << num_faces << "\t";
00192         *p_face_file << max_bdy_marker;
00193         if (this->mFilesAreBinary)
00194         {
00195             *p_face_file << "\tBIN\n";
00196         }
00197         else
00198         {
00199             *p_face_file << "\n";
00200         }
00201     
00202         // Write each face's data
00203         default_marker = UINT_MAX;
00204         for (unsigned item_num=0; item_num<num_faces; item_num++)
00205         {
00206             ElementData face_data = this->GetNextBoundaryElement();
00207             WriteItem(p_face_file, item_num, face_data.NodeIndices, default_marker);
00208         }
00209         *p_face_file << comment << "\n";
00210         p_face_file->close();
00211     
00212         if( this->GetNumCableElements() > 0)
00213         {
00214             // Write cable element file
00215             std::string cable_element_file_name = this->mBaseName + ".cable";
00216             out_stream p_cable_element_file = this->mpOutputFileHandler->OpenOutputFile(cable_element_file_name);
00217         
00218             // Write the cable element header
00219             unsigned num_cable_elements = this->GetNumCableElements();
00220             num_attr = 1u; // We have a single region code
00221         
00222             *p_cable_element_file << num_cable_elements << "\t";
00223             *p_cable_element_file << 2 << "\t";
00224             *p_cable_element_file << num_attr;
00225             if (this->mFilesAreBinary)
00226             {
00227                 *p_cable_element_file << "\tBIN\n";
00228             }
00229             else
00230             {
00231                 *p_cable_element_file << "\n";
00232             }
00233         
00234             // Write each element's data
00235             for (unsigned item_num=0; item_num<num_cable_elements; item_num++)
00236             {
00237                 ElementData cable_element_data = this->GetNextCableElement();
00238                 WriteItem(p_cable_element_file, item_num, cable_element_data.NodeIndices, cable_element_data.AttributeValue);
00239             }
00240             *p_cable_element_file << comment;
00241             p_cable_element_file->close();
00242         }
00243     }
00244 }
00245 
00246 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00247 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteElementsAsFaces()
00248 {
00249     std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00250 
00251     std::string element_file_name = this->mBaseName;
00252     if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
00253     {
00254         element_file_name = element_file_name + ".edge";
00255     }
00256     else if (ELEMENT_DIM == 2 && SPACE_DIM == 3)
00257     {
00258         element_file_name = element_file_name + ".face";
00259     }
00260 
00261     out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00262 
00263     // Write the element header
00264     unsigned num_elements = this->GetNumElements();
00265     assert(SPACE_DIM != ELEMENT_DIM);
00266     unsigned num_attr = 0;
00267 
00268     *p_element_file << num_elements << "\t";
00269     //*p_element_file << nodes_per_element << "\t";
00270     *p_element_file << num_attr;
00271     if (this->mFilesAreBinary)
00272     {
00273         *p_element_file << "\tBIN\n";
00274     }
00275     else
00276     {
00277         *p_element_file << "\n";
00278     }
00279 
00280     // Write each element's data
00281     for (unsigned item_num=0; item_num<num_elements; item_num++)
00282     {
00283          WriteItem(p_element_file, item_num, this->GetNextElement().NodeIndices);
00284     }
00285     *p_element_file << comment << "\n";
00286     p_element_file->close();
00287 
00288 }
00289 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00290 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFacesAsEdges()
00291 {
00292     std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00293 
00294     if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
00295     {
00296         return;
00297     }
00298 
00299     assert(SPACE_DIM == 3 && ELEMENT_DIM == 2);
00300 
00301     std::string face_file_name = this->mBaseName;
00302     face_file_name = face_file_name + ".edge";
00303 
00304     out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00305 
00306     // Write the boundary face header
00307     unsigned num_faces = this->GetNumBoundaryFaces();
00308 
00309     unsigned max_bdy_marker = 0;
00310     unsigned default_marker = UINT_MAX;
00311 
00312     *p_face_file << num_faces << "\t";
00313     *p_face_file << max_bdy_marker;
00314     if (this->mFilesAreBinary)
00315     {
00316         *p_face_file << "\tBIN\n";
00317     }
00318     else
00319     {
00320         *p_face_file << "\n";
00321     }
00322     // Write each face's data
00323     for (unsigned item_num=0; item_num<num_faces; item_num++)
00324     {
00325         ElementData face_data = this->GetNextBoundaryElement();
00326         WriteItem(p_face_file, item_num, face_data.NodeIndices, default_marker);
00327     }
00328     *p_face_file << comment << "\n";
00329     p_face_file->close();
00330 }
00331 
00332 
00333 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00334 template<class T>
00335 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteItem(out_stream &pFile, unsigned itemNumber,
00336                                                     const std::vector<T> &dataPacket, unsigned attribute)
00337 {
00338     if (this->mFilesAreBinary)
00339     {
00340         //No item numbers
00341         //Write raw data out of std::vector into the file
00342         pFile->write((char*)&dataPacket[0], dataPacket.size()*sizeof(T));
00343         //Write raw attribute
00344         if (attribute != UINT_MAX)
00345         {
00346             pFile->write((char*) &attribute, sizeof(attribute));
00347         }
00348     }
00349     else
00350     {
00351         *pFile << itemNumber;
00352         for (unsigned i=0; i<dataPacket.size(); i++)
00353         {
00354             *pFile << "\t" << dataPacket[i];
00355         }
00356         if (attribute != UINT_MAX)
00357         {
00358             *pFile << "\t" << attribute;
00359         }
00360         *pFile << "\n";
00361     }
00362 }
00364 // Explicit instantiation
00366 
00367 template class TrianglesMeshWriter<1,1>;
00368 template class TrianglesMeshWriter<1,2>;
00369 template class TrianglesMeshWriter<1,3>;
00370 template class TrianglesMeshWriter<2,2>;
00371 template class TrianglesMeshWriter<2,3>;
00372 template class TrianglesMeshWriter<3,3>;
00373 
00378 template void TrianglesMeshWriter<1, 1>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00379 template void TrianglesMeshWriter<1, 1>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00380 template void TrianglesMeshWriter<1, 2>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00381 template void TrianglesMeshWriter<1, 2>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00382 template void TrianglesMeshWriter<1, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00383 template void TrianglesMeshWriter<1, 3>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00384 template void TrianglesMeshWriter<2, 2>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00385 template void TrianglesMeshWriter<2, 2>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00386 template void TrianglesMeshWriter<2, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00387 template void TrianglesMeshWriter<2, 3>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );
00388 template void TrianglesMeshWriter<3, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00389 template void TrianglesMeshWriter<3, 3>::WriteItem(out_stream &, unsigned, const std::vector<double>   &, unsigned );

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