CmguiDeformedSolutionsWriter.cpp

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

Generated by  doxygen 1.6.2