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 "UblasCustomFunctions.hpp"
00030 #include "HeartConfig.hpp"
00031 #include "Hdf5ToVtkConverter.hpp"
00032 #include "PetscTools.hpp"
00033 #include "Exception.hpp"
00034 #include "ReplicatableVector.hpp"
00035 #include "DistributedVector.hpp"
00036 #include "DistributedVectorFactory.hpp"
00037 #include "VtkMeshWriter.hpp"
00038 #include "GenericMeshReader.hpp"
00039 #include "DistributedTetrahedralMesh.hpp"
00040 #include "Warnings.hpp"
00041
00042 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00043 Hdf5ToVtkConverter<ELEMENT_DIM, SPACE_DIM>::Hdf5ToVtkConverter(std::string inputDirectory,
00044 std::string fileBaseName,
00045 AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM> *pMesh,
00046 bool parallelVtk) :
00047 AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(inputDirectory, fileBaseName, pMesh, "vtk_output")
00048 {
00049 #ifdef CHASTE_VTK
00050
00051
00052 VtkMeshWriter<ELEMENT_DIM,SPACE_DIM> vtk_writer(HeartConfig::Instance()->GetOutputDirectory() + "/vtk_output", fileBaseName, false);
00053
00054 DistributedVectorFactory *p_factory = pMesh->GetDistributedVectorFactory();
00055
00056
00057 assert(this->mpReader->GetNumberOfRows() == pMesh->GetNumNodes());
00058
00059 DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* p_distributed_mesh = dynamic_cast<DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* >(pMesh);
00060
00061 unsigned num_nodes = pMesh->GetNumNodes();
00062 if (parallelVtk)
00063 {
00064
00065 if (p_distributed_mesh == NULL)
00066 {
00067 WARNING("Can only write parallel VTK from a DistributedTetrahedralMesh - writing sequential VTK instead");
00068 parallelVtk = false;
00069 }
00070
00071
00072 if (HeartConfig::Instance()->GetOutputUsingOriginalNodeOrdering())
00073 {
00074 WARNING("Can't write parallel VTK (pvtu) files with original ordering - writing sequential VTK instead");
00075 parallelVtk = false;
00076 }
00077
00078
00079 if (parallelVtk)
00080 {
00081 vtk_writer.SetParallelFiles(*pMesh);
00082 num_nodes = p_distributed_mesh->GetNumLocalNodes();
00083 }
00084 }
00085
00086 Vec data = p_factory->CreateVec();
00087
00088 unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00089
00090
00091 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00092 {
00093
00094 for( unsigned variable = 0; variable < this->mNumVariables; variable++ )
00095 {
00096 std::string variable_name = this->mpReader->GetVariableNames()[variable];
00097
00098 this->mpReader->GetVariableOverNodes(data, variable_name, time_step);
00099
00100 std::vector<double> data_for_vtk;
00101 data_for_vtk.resize(num_nodes);
00102 std::ostringstream variable_point_data_name;
00103 variable_point_data_name << variable_name << "_" << std::setw(6) << std::setfill('0') << time_step;
00104
00105 if (parallelVtk)
00106 {
00107
00108 double *p_data;
00109 VecGetArray(data, &p_data);
00110 for (unsigned index=0; index<num_nodes; index++)
00111 {
00112 data_for_vtk[index] = p_data[index];
00113 }
00114 VecRestoreArray(data, &p_data);
00115 }
00116 else
00117 {
00118
00119 ReplicatableVector repl_data(data);
00120 for (unsigned index=0; index<num_nodes; index++)
00121 {
00122 data_for_vtk[index] = repl_data[index];
00123 }
00124 }
00125
00126 vtk_writer.AddPointData(variable_point_data_name.str(), data_for_vtk);
00127 }
00128 }
00129 VecDestroy(data);
00130
00131
00132 if (HeartConfig::Instance()->GetOutputUsingOriginalNodeOrdering() == false)
00133 {
00134 vtk_writer.WriteFilesUsingMesh( *(this->mpMesh) );
00135 }
00136 else
00137 {
00138
00140
00141 std::string original_file=this->mpMesh->GetMeshFileBaseName();
00142 GenericMeshReader<ELEMENT_DIM, SPACE_DIM> original_mesh_reader(original_file);
00143 vtk_writer.WriteFilesUsingMeshReader(original_mesh_reader);
00144 }
00145
00146 #endif //CHASTE_VTK
00147
00148 }
00149
00150
00152
00154
00155 template class Hdf5ToVtkConverter<1,1>;
00156 template class Hdf5ToVtkConverter<1,2>;
00157 template class Hdf5ToVtkConverter<2,2>;
00158 template class Hdf5ToVtkConverter<1,3>;
00159 template class Hdf5ToVtkConverter<2,3>;
00160 template class Hdf5ToVtkConverter<3,3>;