CmguiDeformedSolutionsWriter.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 
00030 #include "CmguiDeformedSolutionsWriter.hpp"
00031 
00032 template<unsigned DIM>
00033 CmguiDeformedSolutionsWriter<DIM>::CmguiDeformedSolutionsWriter(std::string outputDirectory,
00034                                                                 std::string baseName,
00035                                                                 QuadraticMesh<DIM>& rQuadraticMesh)
00036     : CmguiMeshWriter<DIM, DIM>(outputDirectory, baseName),
00037       mpQuadraticMesh(&rQuadraticMesh),
00038       mFinalCounter(0)
00039 {
00040 }
00041 
00042 template<unsigned DIM>
00043 void CmguiDeformedSolutionsWriter<DIM>::WriteInitialMesh()
00044 {
00045     std::string saved_base_name = this->mBaseName;
00046     this->mBaseName = this->mBaseName + "_0";
00047     this->WriteFilesUsingMesh(*mpQuadraticMesh);
00048     this->mBaseName = saved_base_name;
00049 }
00050 
00051 template<unsigned DIM>
00052 void CmguiDeformedSolutionsWriter<DIM>::WriteDeformationPositions(std::vector<c_vector<double,DIM> >& rDeformedPositions,
00053                                                                   unsigned counter)
00054 {
00055     if(mpQuadraticMesh->GetNumNodes() != rDeformedPositions.size() )
00056     {
00057         EXCEPTION("The size of rDeformedPositions does not match the number of nodes in the mesh");
00058     }
00059 
00060     mFinalCounter = counter;
00061     std::stringstream node_file_name_stringstream;
00062     node_file_name_stringstream <<  this->mBaseName << "_" << counter << ".exnode";
00063 
00064     out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name_stringstream.str());
00065 
00066     this->WriteNodeFileHeader(p_node_file);
00067 
00068     // Write each node's data
00069     for (unsigned index=0; index<mpQuadraticMesh->GetNumVertices(); index++)
00070     {
00071         *p_node_file << "Node:\t" << index+1 << "\t";
00072 
00073         for (unsigned i=0; i<DIM; i++)
00074         {
00075             *p_node_file << rDeformedPositions[index](i) << "\t";
00076         }
00077         *p_node_file << "\n";
00078     }
00079     p_node_file->close();
00080 }
00081 
00082 template<unsigned DIM>
00083 void CmguiDeformedSolutionsWriter<DIM>::WriteCmguiScript()
00084 {
00085     out_stream p_script_file = this->mpOutputFileHandler->OpenOutputFile("LoadSolutions.com");
00086     *p_script_file << "#\n# Cmgui script automatically generated by Chaste\n#\n"
00087                    << "for ($i=0; $i<=" << mFinalCounter << "; $i++) { \n"
00088                    << "  gfx read node " << this->mBaseName << "_$i time $i\n"
00089                    << "}\n"
00090                    << "gfx read ele " << this->mBaseName << "_0 generate_faces_and_lines\n"
00091                    << "gfx cr win\n\n";
00092     p_script_file->close();
00093 }
00094 
00095 template<unsigned DIM>
00096 void CmguiDeformedSolutionsWriter<DIM>::ConvertOutput(std::string inputDirectory,
00097                                                       std::string inputFileBaseName,
00098                                                       unsigned finalCounter)
00099 {
00100     // write the mesh to <inputFileBaseName>_0.exnode and <inputFileBaseName>_0.exelem
00101     WriteInitialMesh();
00102 
00103     std::vector<c_vector<double,DIM> > deformed_position(mpQuadraticMesh->GetNumNodes(), zero_vector<double>(DIM));
00104 
00105     for(unsigned i=1; i<=finalCounter; i++) //not i=0
00106     {
00107         std::stringstream in_file_stream;
00108         in_file_stream << inputDirectory << "/" << inputFileBaseName << "_" << i << ".nodes";
00109 
00110         std::ifstream ifs(in_file_stream.str().c_str());
00111         if (!ifs.is_open())
00112         {
00113             EXCEPTION("Could not open file: " + in_file_stream.str());
00114         }
00115 
00116         // the file into deformed_position
00117         double data;
00118         for(unsigned index=0; index<mpQuadraticMesh->GetNumNodes(); index++)
00119         {
00120             for(unsigned j=0; j<DIM; j++)
00121             {
00122                 ifs >> data;
00123                 if(ifs.fail())
00124                 {
00125                     std::stringstream error_message;
00126                     error_message << "Error occurred when reading file " << in_file_stream.str()
00127                                   << ". Expected " << mpQuadraticMesh->GetNumNodes() << " rows and "
00128                                   << DIM << " columns";
00129                     EXCEPTION(error_message.str());
00130                 }
00131                 deformed_position[index](j) = data;
00132             }
00133         }
00134 
00135         ifs.close();
00136 
00137         // convert
00138         WriteDeformationPositions(deformed_position, i);
00139     }
00140 
00141     WriteCmguiScript();
00142 }
00143 
00144 
00145 template class CmguiDeformedSolutionsWriter<2>;
00146 template class CmguiDeformedSolutionsWriter<3>;

Generated by  doxygen 1.6.2