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

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