Hdf5ToVtkConverter.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 "UblasCustomFunctions.hpp"
00037 #include "Hdf5ToVtkConverter.hpp"
00038 #include "PetscTools.hpp"
00039 #include "Exception.hpp"
00040 #include "ReplicatableVector.hpp"
00041 #include "DistributedVector.hpp"
00042 #include "DistributedVectorFactory.hpp"
00043 #include "VtkMeshWriter.hpp"
00044 #include "GenericMeshReader.hpp"
00045 #include "DistributedTetrahedralMesh.hpp"
00046 #include "Warnings.hpp"
00047
00048 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 Hdf5ToVtkConverter<ELEMENT_DIM, SPACE_DIM>::Hdf5ToVtkConverter(const FileFinder& rInputDirectory,
00050 const std::string& rFileBaseName,
00051 AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>* pMesh,
00052 bool parallelVtk,
00053 bool usingOriginalNodeOrdering)
00054 : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "vtk_output",0u)
00055 {
00056 #ifdef CHASTE_VTK // Requires "sudo aptitude install libvtk5-dev" or similar
00057
00058
00059 FileFinder test_output("", RelativeTo::ChasteTestOutput);
00060 std::string output_directory = rInputDirectory.GetRelativePath(test_output) + "/" + this->mRelativeSubdirectory;
00061
00062 VtkMeshWriter<ELEMENT_DIM,SPACE_DIM> vtk_writer(output_directory, rFileBaseName, false);
00063
00064 DistributedVectorFactory* p_factory = pMesh->GetDistributedVectorFactory();
00065
00066
00067 assert(this->mpReader->GetNumberOfRows() == pMesh->GetNumNodes());
00068
00069 DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* p_distributed_mesh = dynamic_cast<DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>*>(pMesh);
00070
00071 unsigned num_nodes = pMesh->GetNumNodes();
00072 if (parallelVtk)
00073 {
00074
00075 if (p_distributed_mesh == NULL)
00076 {
00077 WARNING("Can only write parallel VTK from a DistributedTetrahedralMesh - writing sequential VTK instead");
00078 parallelVtk = false;
00079 }
00080
00081
00082 if (usingOriginalNodeOrdering)
00083 {
00084 WARNING("Can't write parallel VTK (pvtu) files with original ordering - writing sequential VTK instead");
00085 parallelVtk = false;
00086 }
00087
00088
00089 if (parallelVtk)
00090 {
00091 vtk_writer.SetParallelFiles(*pMesh);
00092 num_nodes = p_distributed_mesh->GetNumLocalNodes();
00093 }
00094 }
00095
00096 Vec data = p_factory->CreateVec();
00097
00098 do
00099 {
00100
00101 assert(this->mpReader->GetNumberOfRows() == pMesh->GetNumNodes());
00102
00103 unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00104
00105
00106 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00107 {
00108
00109 for (unsigned variable=0; variable<this->mNumVariables; variable++)
00110 {
00111 std::string variable_name = this->mpReader->GetVariableNames()[variable];
00112
00113
00114 this->mpReader->GetVariableOverNodes(data, variable_name, time_step);
00115
00116 std::vector<double> data_for_vtk;
00117 data_for_vtk.resize(num_nodes);
00118 std::ostringstream variable_point_data_name;
00119 variable_point_data_name << variable_name << "_" << std::setw(6) << std::setfill('0') << time_step;
00120
00121 if (parallelVtk)
00122 {
00123
00124 double *p_data;
00125 VecGetArray(data, &p_data);
00126 for (unsigned index=0; index<num_nodes; index++)
00127 {
00128 data_for_vtk[index] = p_data[index];
00129 }
00130 VecRestoreArray(data, &p_data);
00131 }
00132 else
00133 {
00134
00135 ReplicatableVector repl_data(data);
00136 for (unsigned index=0; index<num_nodes; index++)
00137 {
00138 data_for_vtk[index] = repl_data[index];
00139 }
00140 }
00141
00142 vtk_writer.AddPointData(variable_point_data_name.str(), data_for_vtk);
00143 }
00144 }
00145 }
00146 while ( this->MoveOntoNextDataset() );
00147
00148
00149 PetscTools::Destroy(data);
00150
00151
00152 if (!usingOriginalNodeOrdering)
00153 {
00154 vtk_writer.WriteFilesUsingMesh(*(this->mpMesh));
00155 }
00156 else
00157 {
00158
00160
00161 std::string original_file = this->mpMesh->GetMeshFileBaseName();
00162 std::auto_ptr<AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> > p_original_mesh_reader
00163 = GenericMeshReader<ELEMENT_DIM, SPACE_DIM>(original_file);
00164 vtk_writer.WriteFilesUsingMeshReader(*p_original_mesh_reader);
00165 }
00166 #endif //CHASTE_VTK
00167 }
00168
00170
00172
00173 template class Hdf5ToVtkConverter<1,1>;
00174 template class Hdf5ToVtkConverter<1,2>;
00175 template class Hdf5ToVtkConverter<2,2>;
00176 template class Hdf5ToVtkConverter<1,3>;
00177 template class Hdf5ToVtkConverter<2,3>;
00178 template class Hdf5ToVtkConverter<3,3>;