MeshalyzerMeshWriter.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 _MESHALYZERMESHWRITER_HPP_
00031 #define _MESHALYZERMESHWRITER_HPP_
00032 
00033 #include "AbstractMeshWriter.hpp"
00034 
00035 
00036 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00037 class MeshalyzerMeshWriter : public AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>
00038 {
00039 public:
00040     MeshalyzerMeshWriter(const std::string &rDirectory,
00041                          const std::string &rBaseName,
00042                          const bool &rCleanDirectory=true,
00043                          const bool &rSetCoolGraphics=false);
00044     void WriteFiles();
00045     virtual ~MeshalyzerMeshWriter();
00046 
00047 };
00048 
00049 
00050 
00051 
00052 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00053 MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::MeshalyzerMeshWriter(const std::string &rDirectory,
00054         const std::string &rBaseName,
00055         const bool &rCleanDirectory,
00056         const bool &rSetCoolGraphics)
00057         : AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, rCleanDirectory)
00058 {
00059     if (ELEMENT_DIM != SPACE_DIM)
00060     {
00061         EXCEPTION("ELEMENT_DIM must be equal to SPACE_DIM");
00062     }
00063 
00064     if (rSetCoolGraphics)
00065     {
00066         this->mIndexFromZero=false;
00067         this->mWriteMetaFile=true;
00068     }
00069     else
00070     {
00071         this->mIndexFromZero=true;
00072         this->mWriteMetaFile=false;
00073     }
00074 }
00075 
00076 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00077 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFiles()
00078 {
00079     //std::string comment="#Generated by Chaste mesh file writer";
00080 
00081     //Write node file
00082     std::string node_file_name = this->mBaseName+".pts";
00083     out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00084 
00085     //Write the node header
00086     unsigned num_nodes = this->GetNumNodes();
00087     *p_node_file << num_nodes << "\n";
00088 
00089     //Write each node's data
00090     for (unsigned item_num=0; item_num<num_nodes; item_num++)
00091     {
00092         std::vector<double> current_item = this->mNodeData[item_num];
00093         for (unsigned i=0;i<SPACE_DIM;i++)
00094         {
00095             *p_node_file << current_item[i] << "\t";
00096         }
00097         if (SPACE_DIM==2)
00098         {
00099             *p_node_file << 0 << "\t";
00100         }
00101         if (SPACE_DIM==1)
00102         {
00103             *p_node_file << 0 << "\t" << 0 << "\t";
00104         }
00105         *p_node_file << "\n";
00106 
00107     }
00108     p_node_file->close();
00109 
00110     //Write Element file
00111     std::string element_file_name;
00112 
00113     if (SPACE_DIM == 3)
00114     {
00115         element_file_name = this->mBaseName+".tetra";
00116     }
00117     else // SPACE_DIM == 1 or 2
00118     {
00119         element_file_name = this->mBaseName+".tri";
00120     }
00121 
00122 
00123     out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00124 
00125     //Write the element header
00126     unsigned num_elements = this->GetNumElements();
00127 
00128     *p_element_file << num_elements << "\n";
00129 
00130     //Write each element's data
00131     unsigned nodes_per_element = ELEMENT_DIM+1;
00132     for (unsigned item_num=0; item_num<num_elements; item_num++)
00133     {
00134         std::vector<unsigned> current_item = this->mElementData[item_num];
00135         for (unsigned i=0;i<nodes_per_element;i++)
00136         {
00137             if (this->mIndexFromZero)
00138             {
00139                 *p_element_file << current_item[i] << "\t";
00140             }
00141             else
00142             {
00143                 *p_element_file << current_item[i]+1 << "\t";
00144             }
00145         }
00146         *p_element_file << "\n";
00147 
00148     }
00149     p_element_file->close();
00150 
00151     if (SPACE_DIM==3)
00152     {
00153         //Write boundary face file
00154         std::string face_file_name = this->mBaseName+".tri";
00155         out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00156 
00157         //Write the boundary face header
00158         unsigned num_faces = this->GetNumBoundaryFaces();
00159 
00160         *p_face_file<< num_faces << "\n";
00161 
00162         //Write each face's data
00163         double material_property= 0.0;
00164         for (unsigned item_num=0; item_num<num_faces; item_num++)
00165         {
00166             std::vector<unsigned> current_item = this->mBoundaryFaceData[item_num];
00167             for (unsigned i=0;i<ELEMENT_DIM;i++)
00168             {
00169                 if (this->mIndexFromZero)
00170                 {
00171                     *p_face_file << current_item[i] << "\t";
00172                 }
00173                 else
00174                 {
00175                     *p_face_file << current_item[i]+1 <<"\t";
00176                 }
00177             }
00178             *p_face_file << material_property << "\n";
00179         }
00180         p_face_file->close();
00181 
00182         if (this->mWriteMetaFile)
00183         {
00184             std::string meta_file_name = this->mBaseName+".cg_in";
00185             out_stream p_meta_file = this->mpOutputFileHandler->OpenOutputFile(meta_file_name);
00186 
00187             *p_meta_file << "1\n" << "0\n";
00188             *p_meta_file << face_file_name <<"\n";
00189             p_meta_file->close();
00190         }
00191     }
00192 }
00193 
00194 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00195 MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::~MeshalyzerMeshWriter()
00196 {}
00197 
00198 #endif //_MESHALYZERMESHWRITER_HPP_

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