CmguiMeshWriter.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 #include "Exception.hpp"
00029 #include "CmguiMeshWriter.hpp"
00030 #include "Version.hpp"
00031 
00033 // Implementation
00035 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00036 CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::CmguiMeshWriter(const std::string &rDirectory,
00037                                                         const std::string &rBaseName,
00038                                                         bool cleanDirectory)
00039         : AbstractTetrahedralMeshWriter<ELEMENT_DIM,SPACE_DIM>(rDirectory, rBaseName, cleanDirectory)
00040 {
00041     this->mIndexFromZero = false;
00042     mGroupName = this->mBaseName;
00043 }
00044 
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 void CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::WriteFiles()
00047 {
00049     // Write the exnode file
00051     std::string node_file_name = this->mBaseName + ".exnode";
00052     out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00053 
00054     WriteNodeFileHeader(p_node_file);
00055 
00056     // Write each node's data
00057     for (unsigned item_num=0; item_num<this->GetNumNodes(); item_num++)
00058     {
00059         std::vector<double> current_item = this->GetNextNode();
00060 
00061         *p_node_file << "Node:\t" << item_num+1 << "\t";
00062         for (unsigned i=0; i<ELEMENT_DIM; i++)
00063         {
00064             *p_node_file << current_item[i] << "\t";
00065         }
00066 
00067         *p_node_file << "\n";
00068     }
00069     p_node_file->close();
00070 
00072     // Write the exlem file
00074     std::string elem_file_name = this->mBaseName + ".exelem";
00075     out_stream p_elem_file = this->mpOutputFileHandler->OpenOutputFile(elem_file_name);
00076     
00077     // Write the elem header
00078     
00079     //write provenance info
00080     std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00081     *p_elem_file << comment;
00082 
00083     *p_elem_file << "Group name: " << mGroupName << "\n";
00084     switch (ELEMENT_DIM)
00085     {
00086         case 1:
00087         {
00088             *p_elem_file << CmguiElementFileHeader1D;
00089             break;
00090         }
00091         case 2:
00092         {
00093             *p_elem_file << CmguiElementFileHeader2D;
00094             break;
00095         }
00096         case 3:
00097         {
00098             *p_elem_file << CmguiElementFileHeader3D;
00099             break;
00100         }
00101         default:
00102         {
00103             NEVER_REACHED;
00104         }
00105     }
00106 
00107 
00108     //now we need to figure out how many additional fields we have
00109     unsigned number_of_fields = mAdditionalFieldNames.size();
00110     std::stringstream string_of_number_of_fields;
00111     //we write the number of additional fields + 1 because the coordinates field gets written anyway
00112     string_of_number_of_fields << number_of_fields+1;
00113     //and write accordingly the total number of fields
00114     *p_elem_file << " #Fields="<<string_of_number_of_fields.str()<<"\n";
00115 
00116     //first field (the coordinates field is fixed and always there
00117     switch (ELEMENT_DIM)
00118     {
00119         case 1:
00120         {
00121             *p_elem_file << CmguiCoordinatesFileHeader1D;
00122             break;
00123         }
00124         case 2:
00125         {
00126             *p_elem_file << CmguiCoordinatesFileHeader2D;
00127             break;
00128         }
00129         case 3:
00130         {
00131             *p_elem_file << CmguiCoordinatesFileHeader3D;
00132             break;
00133         }
00134         default:
00135         {
00136             NEVER_REACHED;
00137         }
00138     }
00139 
00140 
00141     //now write the specification for each additional field
00142     for (unsigned i = 0; i <  number_of_fields; i++)
00143     {
00144         //unsigned to string
00145         std::stringstream i_string;
00146         i_string << i+2;
00147         *p_elem_file<<i_string.str()<<")  "<<mAdditionalFieldNames[i]<<" ,";
00148         switch (ELEMENT_DIM)
00149         {
00150             case 1:
00151             {
00152                 *p_elem_file << CmguiAdditonalFieldHeader1D;
00153                 break;
00154             }
00155             case 2:
00156             {
00157                 *p_elem_file << CmguiAdditonalFieldHeader2D;
00158                 break;
00159             }
00160             case 3:
00161             {
00162                 *p_elem_file << CmguiAdditonalFieldHeader3D;
00163                 break;
00164             }
00165             default:
00166             {
00167                 NEVER_REACHED;
00168             }
00169         }
00170 
00171     }
00172 
00173     // Write each elements's data
00174     for (unsigned item_num=0; item_num<this->GetNumElements(); item_num++)
00175     {
00176         std::vector<unsigned> current_element = this->GetNextElement().NodeIndices;
00177 
00178         *p_elem_file << "Element:\t" << item_num+1 << " 0 0 Nodes:\t";
00179         for (unsigned i=0; i<(ELEMENT_DIM+1); i++)
00180         {
00181             *p_elem_file << current_element[i]+1 << "\t";
00182         }
00183 
00184         *p_elem_file << "\n";
00185     }
00186     p_elem_file->close();
00187 }
00188 
00189 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00190 void CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::SetAdditionalFieldNames(std::vector<std::string>& rFieldNames)
00191 {
00192     mAdditionalFieldNames = rFieldNames;
00193 }
00194 
00195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00196 void CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::WriteNodeFileHeader(out_stream& rpNodeFile)
00197 {
00198     //write provenance info
00199     std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00200     *rpNodeFile << comment;
00201     
00202     // Write the node header
00203     *rpNodeFile << "Group name: " << this->mGroupName << "\n";
00204     switch (SPACE_DIM)
00205     {
00206         case 1:
00207         {
00208             *rpNodeFile << CmguiNodeFileHeader1D;
00209             break;
00210         }
00211         case 2:
00212         {
00213             *rpNodeFile << CmguiNodeFileHeader2D;
00214             break;
00215         }
00216         case 3:
00217         {
00218             *rpNodeFile << CmguiNodeFileHeader3D;
00219             break;
00220         }
00221         default:
00222         {
00223             NEVER_REACHED;
00224         }
00225     }
00226 }
00227 
00228 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00229 bool CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::CompareCmguiFiles(std::string& rPath1, std::string& rPath2)
00230 {
00231     std::string compare_command = "diff --ignore-matching-lines=\"! \" ";
00232     compare_command += rPath1;
00233     compare_command += " ";
00234     compare_command += rPath2;
00235 
00236     //Compare the new test file with one from the repository
00237     if (system(compare_command.c_str()) == 0)
00238     {
00239         return true;
00240     }
00241     else
00242     {
00243         return false;
00244     }
00245 }
00246 
00248 // Explicit instantiation
00250 
00251 template class CmguiMeshWriter<1,1>;
00252 template class CmguiMeshWriter<1,2>;
00253 template class CmguiMeshWriter<1,3>;
00254 template class CmguiMeshWriter<2,2>;
00255 template class CmguiMeshWriter<2,3>;
00256 template class CmguiMeshWriter<3,3>;

Generated by  doxygen 1.6.2