Hdf5ToCmguiConverter.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 "Hdf5ToCmguiConverter.hpp"
00037 #include "CmguiMeshWriter.hpp"
00038 #include "UblasCustomFunctions.hpp"
00039 #include "HeartConfig.hpp"
00040 #include "PetscTools.hpp"
00041 #include "Exception.hpp"
00042 #include "ReplicatableVector.hpp"
00043 #include "DistributedVector.hpp"
00044 #include "DistributedVectorFactory.hpp"
00045 #include "Version.hpp"
00046 #include "GenericMeshReader.hpp"
00047
00048 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 void Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::Write(std::string type)
00050 {
00051 out_stream p_file = out_stream(NULL);
00052
00053 unsigned num_nodes = this->mpReader->GetNumberOfRows();
00054 unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00055
00056 DistributedVectorFactory factory(num_nodes);
00057
00058 Vec data = factory.CreateVec();
00059 Vec data_phie = factory.CreateVec();
00060 Vec data_second_cell = factory.CreateVec();
00061
00062 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00063 {
00064
00065 std::stringstream time_step_string;
00066
00067
00068 time_step_string << time_step;
00069 if (PetscTools::AmMaster())
00070 {
00071 p_file = this->mpOutputFileHandler->OpenOutputFile(this->mFileBaseName + "_" + time_step_string.str() + ".exnode");
00072
00073
00074 if (this->mPrecision != 0)
00075 {
00076 p_file->precision(this->mPrecision);
00077 }
00078 }
00079
00080 std::vector<ReplicatableVector*> all_data;
00081 unsigned num_vars = this->mpReader->GetVariableNames().size();
00082 for (unsigned var=0; var<num_vars; var++)
00083 {
00084
00085 this->mpReader->GetVariableOverNodes(data, this->mpReader->GetVariableNames()[var], time_step);
00086 ReplicatableVector* p_repl_data = new ReplicatableVector(data);
00087 assert(p_repl_data->GetSize()==num_nodes);
00088 all_data.push_back(p_repl_data);
00089 }
00090
00091 if (PetscTools::AmMaster())
00092 {
00093
00094 std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00095 *p_file << comment;
00096
00097 *p_file << "Group name: " << this->mFileBaseName << "\n";
00098 *p_file << "#Fields=" << num_vars << "\n";
00099 for (unsigned var=0; var<num_vars; var++)
00100 {
00101 *p_file << " " << var+1 << ") " <<this->mpReader->GetVariableNames()[var]<< " , field, rectangular cartesian, #Components=1" << "\n" << "x. Value index=1, #Derivatives=0, #Versions=1"<<"\n";
00102 if (var != num_vars-1)
00103 {
00104 *p_file << "\n";
00105 }
00106 }
00107
00108
00109 for (unsigned i=0; i<num_nodes; i++)
00110 {
00111
00112 *p_file << "Node: "<< i+1 << "\n";
00113 for (unsigned var=0; var<num_vars; var++)
00114 {
00115 *p_file << (*(all_data[var]))[i] << "\n";
00116 }
00117 }
00118 }
00119
00120 for (unsigned var=0; var<num_vars; var++)
00121 {
00122 delete all_data[var];
00123 }
00124 }
00125 PetscTools::Destroy(data);
00126 PetscTools::Destroy(data_phie);
00127 PetscTools::Destroy(data_second_cell);
00128
00129 if (PetscTools::AmMaster())
00130 {
00131 p_file->close();
00132 }
00133 }
00134
00135 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00136 Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::Hdf5ToCmguiConverter(const FileFinder& rInputDirectory,
00137 const std::string& rFileBaseName,
00138 AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* pMesh,
00139 bool hasBath,
00140 unsigned precision)
00141 : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "cmgui_output", precision)
00142 {
00144 while (this->mDatasetNames[this->mOpenDatasetIndex] != "Data")
00145 {
00146 bool next_open = this->MoveOntoNextDataset();
00147 UNUSED_OPT(next_open);
00148 assert(next_open);
00149 }
00150
00151
00152 Write("");
00153
00154
00155 std::string output_directory = HeartConfig::Instance()->GetOutputDirectory() + "/cmgui_output";
00156
00157 CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM> cmgui_mesh_writer(output_directory, HeartConfig::Instance()->GetOutputFilenamePrefix(), false);
00158
00159
00160 std::vector<std::string> field_names = this->mpReader->GetVariableNames();
00161 cmgui_mesh_writer.SetAdditionalFieldNames(field_names);
00162 if (hasBath)
00163 {
00164 std::vector<std::string> names;
00165 names.push_back("tissue");
00166 names.push_back("bath");
00167 cmgui_mesh_writer.SetRegionNames(names);
00168 }
00169
00170
00171 if (HeartConfig::Instance()->GetOutputUsingOriginalNodeOrdering() == false || !this->mpMesh->IsMeshOnDisk())
00172 {
00173 cmgui_mesh_writer.WriteFilesUsingMesh(*(this->mpMesh), false);
00174 }
00175 else
00176 {
00177
00179
00180 std::string original_file=this->mpMesh->GetMeshFileBaseName();
00181 std::auto_ptr<AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> > p_original_mesh_reader
00182 = GenericMeshReader<ELEMENT_DIM, SPACE_DIM>(original_file);
00183 cmgui_mesh_writer.WriteFilesUsingMeshReader(*p_original_mesh_reader);
00184 }
00185
00186 WriteCmguiScript();
00187 PetscTools::Barrier("Hdf5ToCmguiConverter");
00188 }
00189
00190 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00191 void Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::WriteCmguiScript()
00192 {
00193 unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00194 assert(this->mpReader->GetVariableNames().size() > 0);
00195 std::string variable_name = this->mpReader->GetVariableNames()[0];
00196
00197 if (PetscTools::AmMaster())
00198 {
00199 out_stream p_script_file = this->mpOutputFileHandler->OpenOutputFile("LoadSolutions.com");
00200
00201
00202 std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00203 *p_script_file << comment;
00204
00205 *p_script_file << "# Read the mesh \n"
00206 << "gfx read node "<<HeartConfig::Instance()->GetOutputFilenamePrefix()<<".exnode \n"
00207 << "gfx read elem "<<HeartConfig::Instance()->GetOutputFilenamePrefix()<<".exelem \n"
00208 << "gfx define faces egroup "<<HeartConfig::Instance()->GetOutputFilenamePrefix()<<"\n"
00209 << "# Create a window \n"
00210 << "gfx cre win 1 \n"
00211 << "# Modify the scene (obtained by gfx list g_element XXXX commands) to visualize first var on lines and nodes \n"
00212 << "gfx modify g_element "<< HeartConfig::Instance()->GetOutputFilenamePrefix()<<" general clear circle_discretization 6 default_coordinate coordinates element_discretization \"4*4*4\" native_discretization none; \n"
00213 << "gfx modify g_element "<< HeartConfig::Instance()->GetOutputFilenamePrefix()<<" lines select_on material default data "<<variable_name<<" spectrum default selected_material default_selected; \n"
00214 << "gfx modify g_element "<< HeartConfig::Instance()->GetOutputFilenamePrefix()<<" node_points glyph point general size \"1*1*1\" centre 0,0,0 font default select_on material default data "<<variable_name<<" spectrum default selected_material default_selected; \n"
00215 << "# Load the data \n"
00216 << "for ($i=0; $i<" << num_timesteps << "; $i++) { \n"
00217 << " gfx read node " << this->mFileBaseName << "_$i.exnode time $i\n"
00218 << "}\n";
00219 p_script_file->close();
00220 }
00221 }
00222
00224
00226
00227 template class Hdf5ToCmguiConverter<1,1>;
00228 template class Hdf5ToCmguiConverter<1,2>;
00229 template class Hdf5ToCmguiConverter<2,2>;
00230 template class Hdf5ToCmguiConverter<1,3>;
00231 template class Hdf5ToCmguiConverter<2,3>;
00232 template class Hdf5ToCmguiConverter<3,3>;