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                                                                 CmguiMeshWriteType writeType)
00037     : CmguiMeshWriter<DIM, DIM>(outputDirectory, baseName),
00038       mpQuadraticMesh(&rQuadraticMesh),
00039       mFinalCounter(0)
00040 {
00041     
00042     mNumNodesToUse = mpQuadraticMesh->GetNumVertices();
00043 
00044     if(writeType==WRITE_QUADRATIC_MESH)
00045     {
00046         mNumNodesToUse = mpQuadraticMesh->GetNumNodes();
00047         
00048         switch(DIM)
00049         {
00050             //WriteCmguiScript Commented as CmguiDeformedSolutionsWriter is meant to correspond to
00051             // output of nonlinear elasticity problems - 2d or 3d only, and there is
00052             // no explicit instantiation of this class in 1d.
00053             //case 1:
00054             //{
00055             //   this->mElementFileHeader = CmguiElementFileHeader1DQuadratic;
00056             //    this->mCoordinatesFileHeader = CmguiCoordinatesFileHeader1DQuadratic;
00057             //    this->mAdditionalFieldHeader = CmguiAdditionalFieldHeader1DQuadratic;
00058             //    this->mNumNodesPerElement = 3;
00059             //    this->mReordering.resize(this->mNumNodesPerElement);
00060             //    unsigned reordering[6] = {0,2,1};
00061             //    for(unsigned i=0; i<3; i++)
00062             //    {
00063             //        this->mReordering[i] = reordering[i];
00064             //    }
00065             //    break;
00066             //};
00067             
00068             case 2:
00069             {
00070                 this->mElementFileHeader = CmguiElementFileHeader2DQuadratic;
00071                 this->mCoordinatesFileHeader = CmguiCoordinatesFileHeader2DQuadratic;
00072                 this->mAdditionalFieldHeader = CmguiAdditionalFieldHeader2DQuadratic;
00073                 this->mNumNodesPerElement = 6;
00074                 this->mReordering.resize(this->mNumNodesPerElement);
00075         
00076                 // Go from Chaste(tetgen ordering) (see example comments in
00077                 // QuadraticBasisFunction::ComputeBasisFunction() to CMGUI ordering
00078                 // ("psi1 increasing, then psi1 increasing")
00079                 unsigned reordering[6] = {0,5,1,4,3,2};
00080                 for(unsigned i=0; i<6; i++)
00081                 {
00082                     this->mReordering[i] = reordering[i];
00083                 }
00084                 break;
00085             }
00086             case 3:
00087             {
00088                 this->mElementFileHeader = CmguiElementFileHeader3DQuadratic;
00089                 this->mCoordinatesFileHeader = CmguiCoordinatesFileHeader3DQuadratic;
00090                 this->mAdditionalFieldHeader = CmguiAdditionalFieldHeader3DQuadratic;
00091                 this->mNumNodesPerElement = 10;
00092                 this->mReordering.resize(this->mNumNodesPerElement);
00093 
00094                 // Go from Chaste(tetgen ordering) (see example comments in
00095                 // QuadraticBasisFunction::ComputeBasisFunction() to CMGUI ordering
00096                 // ("psi1 increasing, then psi2 increasing, then psi3 increasing")
00097                 unsigned reordering[10] = {0,4,1,6,5,2,7,8,9,3};
00098                 for(unsigned i=0; i<10; i++)
00099                 {
00100                     this->mReordering[i] = reordering[i];
00101                 }
00102                 break;
00103             }
00104             default:
00105             {
00106                 NEVER_REACHED;
00107             }
00108         }
00109     }
00110 }
00111 
00112 template<unsigned DIM>
00113 void CmguiDeformedSolutionsWriter<DIM>::WriteInitialMesh()
00114 {
00115     std::string saved_base_name = this->mBaseName;
00116     this->mBaseName = this->mBaseName + "_0";
00117     this->WriteFilesUsingMesh(*mpQuadraticMesh);
00118     this->mBaseName = saved_base_name;
00119 }
00120 
00121 template<unsigned DIM>
00122 void CmguiDeformedSolutionsWriter<DIM>::WriteDeformationPositions(std::vector<c_vector<double,DIM> >& rDeformedPositions,
00123                                                                   unsigned counter)
00124 {
00125     if(mpQuadraticMesh->GetNumNodes() != rDeformedPositions.size() )
00126     {
00127         EXCEPTION("The size of rDeformedPositions does not match the number of nodes in the mesh");
00128     }
00129 
00130     mFinalCounter = counter;
00131     std::stringstream node_file_name_stringstream;
00132     node_file_name_stringstream <<  this->mBaseName << "_" << counter << ".exnode";
00133 
00134     out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name_stringstream.str());
00135 
00136     this->WriteNodeFileHeader(p_node_file);
00137 
00138     // Write each node's data
00139     for (unsigned index=0; index<this->GetNumNodes(); index++)
00140     {
00141         *p_node_file << "Node:\t" << index+1 << "\t";
00142 
00143         for (unsigned i=0; i<DIM; i++)
00144         {
00145             *p_node_file << rDeformedPositions[index](i) << "\t";
00146         }
00147         *p_node_file << "\n";
00148     }
00149     p_node_file->close();
00150 }
00151 
00152 template<unsigned DIM>
00153 void CmguiDeformedSolutionsWriter<DIM>::WriteCmguiScript(std::string fieldBaseName)
00154 {
00155     std::string field_string = "";
00156     if(fieldBaseName!="")
00157     {
00158         field_string = " gfx read node " + fieldBaseName + "_$i time $i\n";
00159     }
00160     
00161     out_stream p_script_file = this->mpOutputFileHandler->OpenOutputFile("LoadSolutions.com");
00162     *p_script_file << "#\n# Cmgui script automatically generated by Chaste\n#\n"
00163                    << "for ($i=0; $i<=" << mFinalCounter << "; $i++) { \n"
00164                    << "  gfx read node " << this->mBaseName << "_$i time $i\n"
00165                    << field_string
00166                    << "}\n"
00167                    << "gfx read ele " << this->mBaseName << "_0 generate_faces_and_lines\n"
00168                    << "gfx cr win\n\n";
00169     p_script_file->close();
00170 }
00171 
00172 template<unsigned DIM>
00173 void CmguiDeformedSolutionsWriter<DIM>::ConvertOutput(std::string inputDirectory,
00174                                                       std::string inputFileBaseName,
00175                                                       unsigned finalCounter)
00176 {
00177     // write the mesh to <inputFileBaseName>_0.exnode and <inputFileBaseName>_0.exelem
00178     WriteInitialMesh();
00179 
00180     std::vector<c_vector<double,DIM> > deformed_position(mpQuadraticMesh->GetNumNodes(), zero_vector<double>(DIM));
00181 
00182     for(unsigned i=1; i<=finalCounter; i++) //not i=0
00183     {
00184         std::stringstream in_file_stream;
00185         in_file_stream << inputDirectory << "/" << inputFileBaseName << "_" << i << ".nodes";
00186 
00187         std::ifstream ifs(in_file_stream.str().c_str());
00188         if (!ifs.is_open())
00189         {
00190             EXCEPTION("Could not open file: " + in_file_stream.str());
00191         }
00192 
00193         // the file into deformed_position
00194         double data;
00195         for(unsigned index=0; index<mpQuadraticMesh->GetNumNodes(); index++)
00196         {
00197             for(unsigned j=0; j<DIM; j++)
00198             {
00199                 ifs >> data;
00200                 if(ifs.fail())
00201                 {
00202                     std::stringstream error_message;
00203                     error_message << "Error occurred when reading file " << in_file_stream.str()
00204                                   << ". Expected " << mpQuadraticMesh->GetNumNodes() << " rows and "
00205                                   << DIM << " columns";
00206                     EXCEPTION(error_message.str());
00207                 }
00208                 deformed_position[index](j) = data;
00209             }
00210         }
00211 
00212         ifs.close();
00213 
00214         // convert
00215         WriteDeformationPositions(deformed_position, i);
00216     }
00217 
00218     WriteCmguiScript();
00219 }
00220 
00221 
00222 template class CmguiDeformedSolutionsWriter<2>;
00223 template class CmguiDeformedSolutionsWriter<3>;

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