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
00031
00032
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
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
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
00091
00092
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
00109
00110
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
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
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++)
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
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
00253 WriteDeformationPositions(deformed_position, i);
00254 }
00255
00256 WriteCmguiScript();
00257 }
00258
00259
00260 template class CmguiDeformedSolutionsWriter<2>;
00261 template class CmguiDeformedSolutionsWriter<3>;