CmguiDeformedSolutionsWriter.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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++)
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
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
00138 WriteDeformationPositions(deformed_position, i);
00139 }
00140
00141 WriteCmguiScript();
00142 }
00143
00144
00145 template class CmguiDeformedSolutionsWriter<2>;
00146 template class CmguiDeformedSolutionsWriter<3>;