00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00064 std::string node_file_name = this->mBaseName+".node";
00065 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00066
00067
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
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
00102 std::string element_file_name = this->mBaseName+".ele";
00103 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00104
00105
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
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
00129 std::string face_file_name = this->mBaseName;
00130
00131 if (SPACE_DIM == 1)
00132 {
00133
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
00147 unsigned num_faces = this->GetNumBoundaryFaces();
00148
00149 *p_face_file<< num_faces << "\t";
00150 *p_face_file<< max_bdy_marker<< "\n";
00151
00152
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
00185
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
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
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
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
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
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_