Chaste Release::3.1
MeshalyzerMeshWriter.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "MeshalyzerMeshWriter.hpp"
00037 #include "Version.hpp"
00038 
00039 // We need these two includes for the node/element/face iterators to compile
00040 #include "AbstractTetrahedralMesh.hpp"
00041 #include "DistributedTetrahedralMesh.hpp"
00042 
00043 
00045 // Implementation
00047 
00048 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::MeshalyzerMeshWriter(const std::string &rDirectory,
00050         const std::string &rBaseName,
00051         const bool &rCleanDirectory,
00052         const bool &rSetCoolGraphics)
00053         : AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, rCleanDirectory)
00054 {
00055    /* if (ELEMENT_DIM != SPACE_DIM)
00056     {
00057         EXCEPTION("ELEMENT_DIM must be equal to SPACE_DIM");
00058     }*/
00059 
00060     if (rSetCoolGraphics)
00061     {
00062         this->mIndexFromZero = false;
00063         this->mWriteMetaFile = true;
00064     }
00065     else
00066     {
00067         this->mIndexFromZero = true;
00068         this->mWriteMetaFile = false;
00069     }
00070 }
00071 
00072 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00073 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFiles()
00074 {
00075     std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00076 
00077     //Write node file
00078     out_stream p_node_file = OpenNodeFile();
00079 
00080     //Write the node header
00081     unsigned num_nodes = this->GetNumNodes();
00082     *p_node_file << num_nodes << "\n";
00083 
00084     // Write each node's data
00085     for (unsigned item_num=0; item_num<num_nodes; item_num++)
00086     {
00087         std::vector<double> current_item = this->GetNextNode(); //this->mNodeData[item_num];
00088         for (unsigned i=0; i<SPACE_DIM; i++)
00089         {
00090             *p_node_file << current_item[i] << "\t";
00091         }
00092         if (SPACE_DIM==2)
00093         {
00094             *p_node_file << 0 << "\t";
00095         }
00096         if (SPACE_DIM==1)
00097         {
00098             *p_node_file << 0 << "\t" << 0 << "\t";
00099         }
00100         *p_node_file << "\n";
00101 
00102     }
00103     *p_node_file << comment;
00104     p_node_file->close();
00105 
00106     // Write element file
00107     std::string element_file_name;
00108 
00109     if (ELEMENT_DIM == 3)
00110     {
00111         element_file_name = this->mBaseName + ".tetras";
00112     }
00113     else if (ELEMENT_DIM == 2)
00114     {
00115         element_file_name = this->mBaseName + ".tri";
00116     }
00117     else //ELEMENT_DIM == 1
00118     {
00119         element_file_name = this->mBaseName + ".cnnx";
00120     }
00121 
00122     out_stream p_element_file = OpenElementFile();
00123 
00124     // Write the element header
00125     unsigned num_elements = this->GetNumElements();
00126 
00127     *p_element_file << num_elements << "\n";
00128 
00129     // Write each element's data
00130     unsigned nodes_per_element = ELEMENT_DIM+1;
00131     for (unsigned item_num=0; item_num<num_elements; item_num++)
00132     {
00133         ElementData element_data = this->GetNextElement();
00134 
00135         std::vector<unsigned> current_item = element_data.NodeIndices;
00136         for (unsigned i=0; i<nodes_per_element; i++)
00137         {
00138             if (this->mIndexFromZero)
00139             {
00140                 *p_element_file << current_item[i] << "\t";
00141             }
00142             else
00143             {
00144                 *p_element_file << current_item[i]+1 << "\t";
00145             }
00146         }
00147 
00148         *p_element_file << element_data.AttributeValue << "\n";
00149     }
00150     *p_element_file << comment;
00151     p_element_file->close();
00152 
00153     if (ELEMENT_DIM==3)
00154     {
00155         // Write boundary face file
00156         out_stream p_face_file = OpenFaceFile();
00157 
00158         // Write the boundary face header
00159         unsigned num_faces = this->GetNumBoundaryFaces();
00160 
00161         *p_face_file << num_faces << "\n";
00162 
00163         // Write each face's data
00164         double material_property = 0.0;
00165         for (unsigned item_num=0; item_num<num_faces; item_num++)
00166         {
00167             ElementData current_item = this->GetNextBoundaryElement();
00168             for (unsigned i=0; i<ELEMENT_DIM; i++)
00169             {
00170                 if (this->mIndexFromZero)
00171                 {
00172                     *p_face_file << current_item.NodeIndices[i] << "\t";
00173                 }
00174                 else
00175                 {
00176                     *p_face_file << current_item.NodeIndices[i]+1 <<"\t";
00177                 }
00178             }
00179             *p_face_file << material_property << "\n";
00180         }
00181         *p_face_file << comment;
00182         p_face_file->close();
00183 
00184         WriteMetaFile();
00185     }
00186 }
00187 
00188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00189 MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::~MeshalyzerMeshWriter()
00190 {
00191 }
00192 
00193 
00194 
00195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00196 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteMetaFile()
00197 {
00198     if (this->mWriteMetaFile)
00199     {
00200         std::string meta_file_name = this->mBaseName + ".cg_in";
00201         out_stream p_meta_file = this->mpOutputFileHandler->OpenOutputFile(meta_file_name);
00202 
00203         *p_meta_file << "1\n" << "0\n";
00204         std::string face_file_name = this->mBaseName + ".tri";
00205         *p_meta_file << face_file_name <<"\n";
00206         std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00207         *p_meta_file << comment;
00208         p_meta_file->close();
00209     }
00210 }
00211 
00212 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00213 std::ios_base::openmode MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetOpenMode(bool append)
00214 {
00215     std::ios_base::openmode mode = std::ios::out;
00216     if (append)
00217     {
00218         mode |= std::ios::app; // Note: bitwise OR operation
00219     }
00220     else
00221     {
00222         mode |= std::ios::trunc;
00223     }
00224     return mode;
00225 }
00226 
00227 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00228 out_stream MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::OpenNodeFile(bool append)
00229 {
00230     std::string node_file_name = this->mBaseName + ".pts";
00231     return this->mpOutputFileHandler->OpenOutputFile(node_file_name, GetOpenMode(append));
00232 }
00233 
00234 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00235 out_stream MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::OpenElementFile(bool append)
00236 {
00237     std::string element_file_name;
00238 
00239     if (ELEMENT_DIM == 3)
00240     {
00241         element_file_name = this->mBaseName + ".tetras";
00242     }
00243     else if (ELEMENT_DIM == 2)
00244     {
00245         element_file_name = this->mBaseName + ".tri";
00246     }
00247     else //ELEMENT_DIM == 1
00248     {
00249         element_file_name = this->mBaseName + ".cnnx";
00250     }
00251 
00252     return this->mpOutputFileHandler->OpenOutputFile(element_file_name, GetOpenMode(append));
00253 }
00254 
00255 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00256 out_stream MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::OpenFaceFile(bool append)
00257 {
00258     std::string face_file_name = this->mBaseName + ".tri";
00259     return this->mpOutputFileHandler->OpenOutputFile(face_file_name, GetOpenMode(append));
00260 }
00261 
00262 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00263 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::CreateFilesWithHeaders()
00264 {
00265     /*
00266      *  Node file
00267      */
00268     out_stream p_node_file = OpenNodeFile();
00269 
00270     //Write the node header
00271     unsigned num_nodes = this->GetNumNodes();
00272     *p_node_file << num_nodes << "\n";
00273 
00274     p_node_file->close();
00275 
00276     /*
00277      *  Element file
00278      */
00279     // Write element file
00280     out_stream p_element_file = OpenElementFile();
00281 
00282     // Write the element header
00283     unsigned num_elements = this->GetNumElements();
00284     *p_element_file << num_elements << "\n";
00285 
00286     p_element_file->close();
00287 
00288     /*
00289      * Face file
00290      */
00291     if (ELEMENT_DIM==3)
00292     {
00293         // Write boundary face file
00294         out_stream p_face_file = OpenFaceFile();
00295 
00296         // Write the boundary face header
00297         unsigned num_faces = this->GetNumBoundaryFaces();
00298         *p_face_file << num_faces << "\n";
00299 
00300         p_face_file->close();
00301 
00302         WriteMetaFile();
00303     }
00304 
00305 }
00306 
00307 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00308 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::AppendLocalDataToFiles()
00309 {
00310     out_stream p_node_file = OpenNodeFile(true);
00311 
00312     typedef typename AbstractMesh<ELEMENT_DIM,SPACE_DIM>::NodeIterator NodeIterType;
00313 
00314     for (NodeIterType iter = this->mpDistributedMesh->GetNodeIteratorBegin();
00315          iter != this->mpDistributedMesh->GetNodeIteratorEnd();
00316          ++iter)
00317     {
00318         const c_vector<double, SPACE_DIM>& r_current_item = iter->rGetLocation();
00319         for (unsigned i=0; i<SPACE_DIM; i++)
00320         {
00321             *p_node_file << r_current_item[i] << "\t";
00322         }
00323         if (SPACE_DIM==2)
00324         {
00325             *p_node_file << 0 << "\t";
00326         }
00327         if (SPACE_DIM==1)
00328         {
00329             *p_node_file << 0 << "\t" << 0 << "\t";
00330         }
00331         *p_node_file << "\n";
00332     }
00333     p_node_file->close();
00334 
00335     out_stream p_element_file = OpenElementFile(true);
00336 
00337     typedef typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ElementIterator ElemIterType;
00338 
00339     for (ElemIterType iter = this->mpDistributedMesh->GetElementIteratorBegin();
00340          iter != this->mpDistributedMesh->GetElementIteratorEnd();
00341          ++iter)
00342     {
00343         if ( this->mpDistributedMesh->CalculateDesignatedOwnershipOfElement(iter->GetIndex()))
00344         {
00345             for (unsigned i=0; i<this->mNodesPerElement; i++)
00346             {
00347                 if (this->mIndexFromZero)
00348                 {
00349                     *p_element_file << iter->GetNodeGlobalIndex(i) << "\t";
00350                 }
00351                 else
00352                 {
00353                     *p_element_file << iter->GetNodeGlobalIndex(i)+1 << "\t";
00354                 }
00355             }
00356 
00357             *p_element_file << iter->GetAttribute() << "\n";
00358         }
00359     }
00360     p_element_file->close();
00361 
00362 
00363     if (ELEMENT_DIM == 3)
00364     {
00365         out_stream p_face_file = OpenFaceFile(true);
00366 
00367         typedef typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::BoundaryElementIterator BoundaryElemIterType;
00368 
00369         for (BoundaryElemIterType iter = this->mpDistributedMesh->GetBoundaryElementIteratorBegin();
00370              iter != this->mpDistributedMesh->GetBoundaryElementIteratorEnd();
00371              ++iter)
00372         {
00373             if ( this->mpDistributedMesh->CalculateDesignatedOwnershipOfBoundaryElement((*iter)->GetIndex()))
00374             {
00375                 for (unsigned i=0; i<ELEMENT_DIM; i++)
00376                 {
00377                     if (this->mIndexFromZero)
00378                     {
00379                         *p_face_file << (*iter)->GetNodeGlobalIndex(i) << "\t";
00380                     }
00381                     else
00382                     {
00383                         *p_face_file << (*iter)->GetNodeGlobalIndex(i)+1 << "\t";
00384                     }
00385                 }
00386 
00387                 *p_face_file << (*iter)->GetAttribute() << "\n";
00388             }
00389         }
00390         p_face_file->close();
00391     }
00392 }
00393 
00394 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00395 void MeshalyzerMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesFooter()
00396 {
00397     std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00398 
00399     out_stream p_node_file = OpenNodeFile(true);
00400     *p_node_file << comment;
00401     p_node_file->close();
00402 
00403     out_stream p_element_file = OpenElementFile(true);
00404     *p_element_file << comment;
00405     p_element_file->close();
00406 
00407     if (ELEMENT_DIM == 3)
00408     {
00409         out_stream p_face_file = OpenFaceFile(true);
00410         *p_face_file << comment;
00411         p_face_file->close();
00412     }
00413 }
00414 
00415 
00417 // Explicit instantiation
00419 
00420 template class MeshalyzerMeshWriter<1,1>;
00421 template class MeshalyzerMeshWriter<1,2>;
00422 template class MeshalyzerMeshWriter<1,3>;
00423 template class MeshalyzerMeshWriter<2,2>;
00424 template class MeshalyzerMeshWriter<2,3>;
00425 template class MeshalyzerMeshWriter<3,3>;