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 #include "CmguiDeformedSolutionsWriter.hpp"
00030 #include "Exception.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
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
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
00077
00078
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
00095
00096
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
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
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++)
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
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 EXCEPTION("Error occurred when reading file " << in_file_stream.str()
00203 << ". Expected " << mpQuadraticMesh->GetNumNodes() << " rows and "
00204 << DIM << " columns");
00205 }
00206 deformed_position[index](j) = data;
00207 }
00208 }
00209
00210 ifs.close();
00211
00212
00213 WriteDeformationPositions(deformed_position, i);
00214 }
00215
00216 WriteCmguiScript();
00217 }
00218
00219
00220 template class CmguiDeformedSolutionsWriter<2>;
00221 template class CmguiDeformedSolutionsWriter<3>;