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