MeshalyzerMeshWriter.cpp

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

Generated by  doxygen 1.6.2