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 
00032 // We need these two includes for the node/element/face iterators to compile
00033 #include "AbstractTetrahedralMesh.hpp"
00034 #include "DistributedTetrahedralMesh.hpp"
00035 
00036 
00038 // Implementation
00040 
00041 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00042 MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::MeshalyzerMeshWriter(const std::string &rDirectory,
00043         const std::string &rBaseName,
00044         const bool &rCleanDirectory,
00045         const bool &rSetCoolGraphics)
00046         : AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, rCleanDirectory)
00047 {
00048    /* if (ELEMENT_DIM != SPACE_DIM)
00049     {
00050         EXCEPTION("ELEMENT_DIM must be equal to SPACE_DIM");
00051     }*/
00052 
00053     if (rSetCoolGraphics)
00054     {
00055         this->mIndexFromZero = false;
00056         this->mWriteMetaFile = true;
00057     }
00058     else
00059     {
00060         this->mIndexFromZero = true;
00061         this->mWriteMetaFile = false;
00062     }
00063 }
00064 
00065 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00066 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFiles()
00067 {
00068     std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00069     
00070     //Write node file
00071     out_stream p_node_file = OpenNodeFile();
00072 
00073     //Write the node header
00074     unsigned num_nodes = this->GetNumNodes();
00075     *p_node_file << num_nodes << "\n";
00076 
00077     // Write each node's data
00078     for (unsigned item_num=0; item_num<num_nodes; item_num++)
00079     {
00080         std::vector<double> current_item = this->GetNextNode(); //this->mNodeData[item_num];
00081         for (unsigned i=0; i<SPACE_DIM; i++)
00082         {
00083             *p_node_file << current_item[i] << "\t";
00084         }
00085         if (SPACE_DIM==2)
00086         {
00087             *p_node_file << 0 << "\t";
00088         }
00089         if (SPACE_DIM==1)
00090         {
00091             *p_node_file << 0 << "\t" << 0 << "\t";
00092         }
00093         *p_node_file << "\n";
00094 
00095     }
00096     *p_node_file << comment;
00097     p_node_file->close();
00098 
00099     // Write element file
00100     std::string element_file_name;
00101 
00102     if (ELEMENT_DIM == 3)
00103     {
00104         element_file_name = this->mBaseName + ".tetras";
00105     }
00106     else if (ELEMENT_DIM == 2)
00107     {
00108         element_file_name = this->mBaseName + ".tri";
00109     }
00110     else //ELEMENT_DIM == 1
00111     {
00112         element_file_name = this->mBaseName + ".cnnx";
00113     }
00114 
00115     out_stream p_element_file = OpenElementFile();
00116 
00117     // Write the element header
00118     unsigned num_elements = this->GetNumElements();
00119 
00120     *p_element_file << num_elements << "\n";
00121 
00122     // Write each element's data
00123     unsigned nodes_per_element = ELEMENT_DIM+1;
00124     for (unsigned item_num=0; item_num<num_elements; item_num++)
00125     {
00126         ElementData element_data = this->GetNextElement();
00127 
00128         std::vector<unsigned> current_item = element_data.NodeIndices;
00129         for (unsigned i=0; i<nodes_per_element; i++)
00130         {
00131             if (this->mIndexFromZero)
00132             {
00133                 *p_element_file << current_item[i] << "\t";
00134             }
00135             else
00136             {
00137                 *p_element_file << current_item[i]+1 << "\t";
00138             }
00139         }
00140 
00141         *p_element_file << element_data.AttributeValue << "\n";
00142     }
00143     *p_element_file << comment;
00144     p_element_file->close();
00145 
00146     if (ELEMENT_DIM==3)
00147     {
00148         // Write boundary face file
00149         out_stream p_face_file = OpenFaceFile();
00150 
00151         // Write the boundary face header
00152         unsigned num_faces = this->GetNumBoundaryFaces();
00153 
00154         *p_face_file << num_faces << "\n";
00155 
00156         // Write each face's data
00157         double material_property = 0.0;
00158         for (unsigned item_num=0; item_num<num_faces; item_num++)
00159         {
00160             std::vector<unsigned> current_item = this->mBoundaryFaceData[item_num];
00161             for (unsigned i=0; i<ELEMENT_DIM; i++)
00162             {
00163                 if (this->mIndexFromZero)
00164                 {
00165                     *p_face_file << current_item[i] << "\t";
00166                 }
00167                 else
00168                 {
00169                     *p_face_file << current_item[i]+1 <<"\t";
00170                 }
00171             }
00172             *p_face_file << material_property << "\n";
00173         }
00174         *p_face_file << comment;
00175         p_face_file->close();
00176 
00177         WriteMetaFile();
00178     }
00179 }
00180 
00181 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00182 MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::~MeshalyzerMeshWriter()
00183 {
00184 }
00185 
00186 
00187 
00188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00189 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteMetaFile()
00190 {
00191     if (this->mWriteMetaFile)
00192     {
00193         std::string meta_file_name = this->mBaseName + ".cg_in";
00194         out_stream p_meta_file = this->mpOutputFileHandler->OpenOutputFile(meta_file_name);
00195 
00196         *p_meta_file << "1\n" << "0\n";        
00197         std::string face_file_name = this->mBaseName + ".tri";        
00198         *p_meta_file << face_file_name <<"\n";
00199         std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();           
00200         *p_meta_file << comment;
00201         p_meta_file->close();
00202     }
00203 }
00204 
00205 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00206 std::ios_base::openmode MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetOpenMode(bool append)
00207 {
00208     std::ios_base::openmode mode = std::ios::out;
00209     if (append)
00210     {
00211         mode |= std::ios::app; // Note: bitwise OR operation
00212     }
00213     else
00214     {
00215         mode |= std::ios::trunc;
00216     }
00217     return mode;
00218 }
00219 
00220 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00221 out_stream MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::OpenNodeFile(bool append)
00222 {
00223     std::string node_file_name = this->mBaseName + ".pts";
00224     return this->mpOutputFileHandler->OpenOutputFile(node_file_name, GetOpenMode(append));
00225 }
00226 
00227 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00228 out_stream MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::OpenElementFile(bool append)
00229 {
00230     std::string element_file_name;
00231 
00232     if (ELEMENT_DIM == 3)
00233     {
00234         element_file_name = this->mBaseName + ".tetras";
00235     }
00236     else if (ELEMENT_DIM == 2)
00237     {
00238         element_file_name = this->mBaseName + ".tri";
00239     }
00240     else //ELEMENT_DIM == 1
00241     {
00242         element_file_name = this->mBaseName + ".cnnx";
00243     }
00244 
00245     return this->mpOutputFileHandler->OpenOutputFile(element_file_name, GetOpenMode(append));
00246 }
00247 
00248 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00249 out_stream MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::OpenFaceFile(bool append)
00250 {
00251     std::string face_file_name = this->mBaseName + ".tri";
00252     return this->mpOutputFileHandler->OpenOutputFile(face_file_name, GetOpenMode(append));
00253 }
00254 
00255 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00256 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::CreateFilesWithHeaders()
00257 {
00258     /*
00259      *  Node file
00260      */
00261     out_stream p_node_file = OpenNodeFile();
00262 
00263     //Write the node header
00264     unsigned num_nodes = this->GetNumNodes();
00265     *p_node_file << num_nodes << "\n";
00266 
00267     p_node_file->close();
00268 
00269     /*
00270      *  Element file
00271      */
00272     // Write element file
00273     out_stream p_element_file = OpenElementFile();
00274 
00275     // Write the element header
00276     unsigned num_elements = this->GetNumElements();
00277     *p_element_file << num_elements << "\n";
00278     
00279     p_element_file->close();
00280 
00281     /*
00282      * Face file 
00283      */
00284     if (ELEMENT_DIM==3)
00285     {
00286         // Write boundary face file
00287         out_stream p_face_file = OpenFaceFile();
00288 
00289         // Write the boundary face header
00290         unsigned num_faces = this->GetNumBoundaryFaces();
00291         *p_face_file << num_faces << "\n";
00292 
00293         p_face_file->close();
00294 
00295         WriteMetaFile();
00296     }
00297 
00298 }
00299     
00300 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00301 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::AppendLocalDataToFiles()
00302 {
00303     out_stream p_node_file = OpenNodeFile(true);    
00304     
00305     typedef typename AbstractMesh<ELEMENT_DIM,SPACE_DIM>::NodeIterator NodeIterType;
00306     
00307     for (NodeIterType iter = this->mpParallelMesh->GetNodeIteratorBegin();
00308          iter != this->mpParallelMesh->GetNodeIteratorEnd();
00309          ++iter)
00310     {
00311         const c_vector<double, SPACE_DIM>& r_current_item = iter->rGetLocation();
00312         for (unsigned i=0; i<SPACE_DIM; i++)
00313         {
00314             *p_node_file << r_current_item[i] << "\t";
00315         }
00316         if (SPACE_DIM==2)
00317         {
00318             *p_node_file << 0 << "\t";
00319         }
00320         if (SPACE_DIM==1)
00321         {
00322             *p_node_file << 0 << "\t" << 0 << "\t";
00323         }
00324         *p_node_file << "\n";
00325     }
00326     p_node_file->close();
00327 
00328     out_stream p_element_file = OpenElementFile(true);    
00329 
00330     typedef typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ElementIterator ElemIterType;
00331 
00332     for (ElemIterType iter = this->mpParallelMesh->GetElementIteratorBegin();
00333          iter != this->mpParallelMesh->GetElementIteratorEnd();
00334          ++iter)
00335     {
00336         if ( this->mpParallelMesh->CalculateDesignatedOwnershipOfElement(iter->GetIndex()))
00337         {            
00338             for (unsigned i=0; i<this->mNodesPerElement; i++)
00339             {
00340                 if (this->mIndexFromZero)
00341                 {
00342                     *p_element_file << iter->GetNodeGlobalIndex(i) << "\t";
00343                 }
00344                 else
00345                 {
00346                     *p_element_file << iter->GetNodeGlobalIndex(i)+1 << "\t";
00347                 }
00348             }
00349     
00350             *p_element_file << iter->GetRegion() << "\n";
00351         }
00352     }
00353     p_element_file->close();   
00354 
00355 
00356     if (ELEMENT_DIM == 3)
00357     {
00358         out_stream p_face_file = OpenFaceFile(true);    
00359     
00360         typedef typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::BoundaryElementIterator BoundaryElemIterType;
00361     
00362         for (BoundaryElemIterType iter = this->mpParallelMesh->GetBoundaryElementIteratorBegin();
00363              iter != this->mpParallelMesh->GetBoundaryElementIteratorEnd();
00364              ++iter)
00365         {
00366             if ( this->mpParallelMesh->CalculateDesignatedOwnershipOfBoundaryElement((*iter)->GetIndex()))
00367             {            
00368                 for (unsigned i=0; i<ELEMENT_DIM; i++)
00369                 {
00370                     if (this->mIndexFromZero)
00371                     {
00372                         *p_face_file << (*iter)->GetNodeGlobalIndex(i) << "\t";
00373                     }
00374                     else
00375                     {
00376                         *p_face_file << (*iter)->GetNodeGlobalIndex(i)+1 << "\t";
00377                     }
00378                 }
00379         
00380                 *p_face_file << (*iter)->GetRegion() << "\n";
00381             }
00382         }
00383         p_face_file->close();
00384     }   
00385 }
00386 
00387 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00388 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesFooter()
00389 {
00390     std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00391     
00392     out_stream p_node_file = OpenNodeFile(true);
00393     *p_node_file << comment;
00394     p_node_file->close();
00395 
00396     out_stream p_element_file = OpenElementFile(true);
00397     *p_element_file << comment;
00398     p_element_file->close();
00399 
00400     if (ELEMENT_DIM == 3)
00401     {
00402         out_stream p_face_file = OpenFaceFile(true);
00403         *p_face_file << comment;
00404         p_face_file->close();
00405     }            
00406 }
00407 
00408 
00410 // Explicit instantiation
00412 
00413 template class MeshalyzerMeshWriter<1,1>;
00414 template class MeshalyzerMeshWriter<1,2>;
00415 template class MeshalyzerMeshWriter<1,3>;
00416 template class MeshalyzerMeshWriter<2,2>;
00417 template class MeshalyzerMeshWriter<2,3>;
00418 template class MeshalyzerMeshWriter<3,3>;

Generated on Mon Nov 1 12:35:24 2010 for Chaste by  doxygen 1.5.5