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