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 <vector>
00031
00032 #include "UblasCustomFunctions.hpp"
00033 #include "HeartConfig.hpp"
00034 #include "Hdf5ToMeshalyzerConverter.hpp"
00035 #include "PetscTools.hpp"
00036 #include "Exception.hpp"
00037 #include "OutputFileHandler.hpp"
00038 #include "ReplicatableVector.hpp"
00039 #include "DistributedVector.hpp"
00040 #include "DistributedVectorFactory.hpp"
00041
00042 void Hdf5ToMeshalyzerConverter::Write(std::string type)
00043 {
00044 assert(type=="V" || type=="Phi_e");
00045
00046 out_stream p_file=out_stream(NULL);
00047 OutputFileHandler output_file_handler(HeartConfig::Instance()->GetOutputDirectory() + "/output", false);
00048 if (PetscTools::AmMaster())
00049 {
00050 p_file = output_file_handler.OpenOutputFile(mFileBaseName + "_" + type + ".dat");
00051 }
00052
00053 unsigned num_nodes = mpReader->GetNumberOfRows();
00054 unsigned num_timesteps = mpReader->GetUnlimitedDimensionValues().size();
00055
00056 DistributedVectorFactory factory(num_nodes);
00057
00058 Vec data = factory.CreateVec();
00059 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00060 {
00061 mpReader->GetVariableOverNodes(data, type, time_step);
00062 ReplicatableVector repl_data(data);
00063
00064 assert(repl_data.size()==num_nodes);
00065
00066 if(PetscTools::AmMaster())
00067 {
00068 for(unsigned i=0; i<num_nodes; i++)
00069 {
00070 *p_file << repl_data[i] << "\n";
00071 }
00072 }
00073 }
00074 VecDestroy(data);
00075 if(PetscTools::AmMaster())
00076 {
00077 p_file->close();
00078 }
00079 }
00080
00081
00082 Hdf5ToMeshalyzerConverter::Hdf5ToMeshalyzerConverter(std::string inputDirectory,
00083 std::string fileBaseName)
00084 {
00085
00086 mFileBaseName = fileBaseName;
00087 mpReader = new Hdf5DataReader(inputDirectory, mFileBaseName);
00088
00089
00090 std::vector<std::string> variable_names = mpReader->GetVariableNames();
00091 if((variable_names.size()==0) || (variable_names.size()>2))
00092 {
00093 delete mpReader;
00094 EXCEPTION("Data has zero or more than two variables - doesn't appear to be mono or bidomain");
00095 }
00096
00097
00098 if(variable_names.size()==1)
00099 {
00100 if(variable_names[0]!="V")
00101 {
00102 delete mpReader;
00103 EXCEPTION("One variable, but it is not called 'V'");
00104 }
00105
00106 Write("V");
00107 }
00108
00109
00110 if(variable_names.size()==2)
00111 {
00112 if(variable_names[0]!="V" || variable_names[1]!="Phi_e")
00113 {
00114 delete mpReader;
00115 EXCEPTION("Two variables, but they are not called 'V' and 'Phi_e'");
00116 }
00117
00118 Write("V");
00119 Write("Phi_e");
00120 }
00121
00122 if (PetscTools::AmMaster())
00123 {
00124
00125
00126 OutputFileHandler output_file_handler(HeartConfig::Instance()->GetOutputDirectory() + "/output", false);
00127
00128 out_stream p_file = output_file_handler.OpenOutputFile(mFileBaseName + "_times.info");
00129 unsigned num_timesteps = mpReader->GetUnlimitedDimensionValues().size();
00130 *p_file << "Number of timesteps "<<num_timesteps<<"\n";
00131 *p_file << "timestep "<<HeartConfig::Instance()->GetPrintingTimeStep()<<"\n";
00132 double first_timestep=mpReader->GetUnlimitedDimensionValues().front();
00133 *p_file << "First timestep "<<first_timestep<<"\n";
00134 double last_timestep=mpReader->GetUnlimitedDimensionValues().back();
00135 *p_file << "Last timestep "<<last_timestep<<"\n";
00136
00137 p_file->close();
00138
00139 }
00140
00141 MPI_Barrier(PETSC_COMM_WORLD);
00142
00143
00144 }
00145
00146 Hdf5ToMeshalyzerConverter::~Hdf5ToMeshalyzerConverter()
00147 {
00148 delete mpReader;
00149 }