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

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