TrianglesMeshWriter.cpp

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 #include "TrianglesMeshWriter.hpp"
00030 #include <cassert>
00031 
00033 // Implementation
00035 
00036 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00037 TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::TrianglesMeshWriter(
00038     const std::string &rDirectory,
00039     const std::string &rBaseName,
00040     const bool clearOutputDir)
00041         : AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, clearOutputDir)
00042 {
00043 }
00044 
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::~TrianglesMeshWriter()
00047 {
00048 }
00049 
00050 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00051 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFiles()
00052 {
00053     std::string comment = "#Generated by Chaste mesh file writer";
00054 
00055     // Write node file
00056     std::string node_file_name = this->mBaseName + ".node";
00057     out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00058 
00059     // Write the node header
00060     unsigned num_attr = 0;
00061     unsigned max_bdy_marker = 0;
00062     unsigned num_nodes = this->GetNumNodes();
00063 
00064     *p_node_file << num_nodes << "\t";
00065     *p_node_file << SPACE_DIM << "\t";
00066     *p_node_file << num_attr << "\t";
00067     *p_node_file << max_bdy_marker << "\n";
00068     *p_node_file << std::setprecision(20);
00069 
00070     // Write each node's data
00071     unsigned default_marker = 0;
00072     for (unsigned item_num=0; item_num<num_nodes; item_num++)
00073     {
00074         std::vector<double> current_item = this->mNodeData[item_num];
00075         *p_node_file << item_num;
00076         for (unsigned i=0; i<SPACE_DIM; i++)
00077         {
00078             *p_node_file << "\t" << current_item[i];
00079         }
00080         *p_node_file << "\t" << default_marker << "\n";
00081 
00082     }
00083     *p_node_file << comment << "\n";
00084     p_node_file->close();
00085 
00086     if (ELEMENT_DIM < SPACE_DIM)
00087     {
00088         WriteElementsAsFaces();
00089         WriteFacesAsEdges();
00090         return;
00091     }
00092 
00093     // Write element file
00094     std::string element_file_name = this->mBaseName + ".ele";
00095     out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00096 
00097     // Write the element header
00098     unsigned num_elements = this->GetNumElements();
00099     unsigned nodes_per_element = ELEMENT_DIM+1;
00100 
00101     *p_element_file << num_elements << "\t";
00102     *p_element_file << nodes_per_element << "\t";
00103     *p_element_file << num_attr << "\n";
00104 
00105     // Write each element's data
00106     for (unsigned item_num=0; item_num<num_elements; item_num++)
00107     {
00108         std::vector<unsigned> current_item = this->mElementData[item_num];
00109         *p_element_file << item_num;
00110         for (unsigned i=0; i<nodes_per_element; i++)
00111         {
00112             *p_element_file << "\t" << current_item[i];
00113         }
00114         *p_element_file << "\n";
00115 
00116     }
00117     *p_element_file << comment << "\n";
00118     p_element_file->close();
00119 
00120     // Write boundary face file
00121     std::string face_file_name = this->mBaseName;
00122 
00123     if (SPACE_DIM == 1)
00124     {
00125         // In 1-D there is no boundary file.  It's trivial to calculate
00126         return;
00127     }
00128     else if (SPACE_DIM == 2)
00129     {
00130         face_file_name = face_file_name + ".edge";
00131     }
00132     else
00133     {
00134         face_file_name = face_file_name + ".face";
00135     }
00136     out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00137 
00138     // Write the boundary face header
00139     unsigned num_faces = this->GetNumBoundaryFaces();
00140 
00141     *p_face_file << num_faces << "\t";
00142     *p_face_file << max_bdy_marker << "\n";
00143 
00144     // Write each face's data
00145     for (unsigned item_num=0; item_num<num_faces; item_num++)
00146     {
00147         std::vector<unsigned> current_item = this->mBoundaryFaceData[item_num];
00148         *p_face_file << item_num;
00149         for (unsigned i=0; i<ELEMENT_DIM; i++)
00150         {
00151             *p_face_file << "\t" << current_item[i];
00152         }
00153         *p_face_file << "\t" << default_marker << "\n";
00154 
00155     }
00156     *p_face_file << comment << "\n";
00157     p_face_file->close();
00158 }
00159 
00160 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00161 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteElementsAsFaces()
00162 {
00163     std::string comment = "#Generated by Chaste mesh file writer";
00164 
00165     std::string element_file_name = this->mBaseName;
00166     if (ELEMENT_DIM == 1 && SPACE_DIM == 2)
00167     {
00168         element_file_name = element_file_name + ".edge";
00169     }
00170     else if (ELEMENT_DIM == 2 && SPACE_DIM == 3)
00171     {
00172         element_file_name = element_file_name + ".face";
00173     }
00174     else
00175     {
00176         // This is ignored in coverage:
00177         // Since we can't yet read line element meshes in 3D, we won't have anything to write
00178 #define COVERAGE_IGNORE
00179         EXCEPTION("Can only write 1D/2D elements in 2D/3D space.");
00180 #undef COVERAGE_IGNORE
00181     }
00182 
00183     out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00184 
00185     // Write the element header
00186     unsigned num_elements = this->GetNumElements();
00187     unsigned nodes_per_element = ELEMENT_DIM+1;
00188     unsigned num_attr = 0;
00189 
00190     *p_element_file << num_elements << "\t";
00191     *p_element_file << nodes_per_element << "\t";
00192     *p_element_file << num_attr << "\n";
00193 
00194     // Write each element's data
00195     for (unsigned item_num=0; item_num<num_elements; item_num++)
00196     {
00197         std::vector<unsigned> current_item = this->mElementData[item_num];
00198         *p_element_file << item_num;
00199         for (unsigned i=0; i<nodes_per_element; i++)
00200         {
00201             *p_element_file << "\t" << current_item[i];
00202         }
00203         *p_element_file << "\n";
00204 
00205     }
00206     *p_element_file << comment << "\n";
00207     p_element_file->close();
00208 
00209 }
00210 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00211 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFacesAsEdges()
00212 {
00213     std::string comment = "#Generated by Chaste mesh file writer";
00214 
00215     if (ELEMENT_DIM == 1 && SPACE_DIM == 2)
00216     {
00217         return;
00218     }
00219 
00220     //Gcov is confused by this assertion
00221 #define COVERAGE_IGNORE
00222     assert(SPACE_DIM == 3 && ELEMENT_DIM == 2);
00223 #undef COVERAGE_IGNORE
00224 
00225     std::string face_file_name = this->mBaseName;
00226     face_file_name = face_file_name + ".edge";
00227 
00228     out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00229 
00230     // Write the boundary face header
00231     unsigned num_faces = this->GetNumBoundaryFaces();
00232 
00233     unsigned max_bdy_marker = 0;
00234     unsigned default_marker = 0;
00235 
00236     *p_face_file << num_faces << "\t";
00237     *p_face_file << max_bdy_marker << "\n";
00238 
00239     // Write each face's data
00240     for (unsigned item_num=0; item_num<num_faces; item_num++)
00241     {
00242         std::vector<unsigned> current_item = this->mBoundaryFaceData[item_num];
00243         *p_face_file << item_num;
00244         for (unsigned i=0; i<ELEMENT_DIM; i++)
00245         {
00246             *p_face_file << "\t" << current_item[i];
00247         }
00248         *p_face_file << "\t" << default_marker << "\n";
00249 
00250     }
00251     *p_face_file << comment << "\n";
00252     p_face_file->close();
00253 }
00254 
00256 // Explicit instantiation
00258 
00259 template class TrianglesMeshWriter<1,1>;
00260 template class TrianglesMeshWriter<1,2>;
00261 template class TrianglesMeshWriter<1,3>;
00262 template class TrianglesMeshWriter<2,2>;
00263 template class TrianglesMeshWriter<2,3>;
00264 template class TrianglesMeshWriter<3,3>;

Generated on Tue Aug 4 16:10:24 2009 for Chaste by  doxygen 1.5.5