Hdf5ToVtkConverter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
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 
00040 
00041 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00042 Hdf5ToVtkConverter<ELEMENT_DIM, SPACE_DIM>::Hdf5ToVtkConverter(std::string inputDirectory,
00043                           std::string fileBaseName,
00044                           AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM> *pMesh) :
00045                     AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(inputDirectory, fileBaseName, pMesh, "vtk_output")
00046 {
00047 #ifdef CHASTE_VTK
00048 // Requires  "sudo aptitude install libvtk5-dev" or similar
00049 
00050     VtkMeshWriter<ELEMENT_DIM,SPACE_DIM> vtk_writer(HeartConfig::Instance()->GetOutputDirectory() + "/vtk_output", fileBaseName, false);
00051 
00052     unsigned num_nodes = this->mpReader->GetNumberOfRows();
00053     DistributedVectorFactory factory(num_nodes);
00055     Vec data = factory.CreateVec();//for V
00056 
00057     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00058 
00059     // Loop over time steps
00060     for (unsigned time_step=0; time_step<num_timesteps; time_step++) //num_timesteps; time_step++)
00061     {
00062         // Loop over variables
00063         for( unsigned variable = 0; variable < this->mNumVariables; variable++ )
00064         {
00065             std::string variable_name = this->mpReader->GetVariableNames()[variable];
00066 
00067             this->mpReader->GetVariableOverNodes(data, variable_name, time_step); // Gets variable at this time step from HDF5 archive
00068             ReplicatableVector repl_data(data);
00069             std::vector<double> data_for_vtk;
00070             data_for_vtk.resize(num_nodes);
00071             for (unsigned index=0; index<num_nodes; index++)
00072             {
00073                 data_for_vtk[index]  = repl_data[index];
00074             }
00075 
00076             std::ostringstream variable_point_data_name;
00077             variable_point_data_name << variable_name << "_" << std::setw(6) << std::setfill('0') << time_step;
00078 
00079             if (PetscTools::AmMaster())
00080             {
00081                 // Add V into the node "point" data
00082                 vtk_writer.AddPointData(variable_point_data_name.str(), data_for_vtk);
00083             }
00084         }
00085     }
00086     VecDestroy(data);
00087 
00088     // Normally the in-memory mesh is converted:
00089     if (HeartConfig::Instance()->GetOutputUsingOriginalNodeOrdering() == false)
00090     {
00091         vtk_writer.WriteFilesUsingMesh( *(this->mpMesh) );
00092     }
00093     else
00094     {
00095         //In this case we expect the mesh to have been read in from file
00097         //Note that the next line will throw if the mesh has not been read from file
00098         std::string original_file=this->mpMesh->GetMeshFileBaseName();
00099         GenericMeshReader<ELEMENT_DIM, SPACE_DIM> original_mesh_reader(original_file);
00100         vtk_writer.WriteFilesUsingMeshReader(original_mesh_reader);
00101     }
00102 
00103 #endif //CHASTE_VTK
00104 
00105 }
00106 
00107 
00109 // Explicit instantiation
00111 
00112 template class Hdf5ToVtkConverter<1,1>;
00113 template class Hdf5ToVtkConverter<1,2>;
00114 template class Hdf5ToVtkConverter<2,2>;
00115 template class Hdf5ToVtkConverter<1,3>;
00116 template class Hdf5ToVtkConverter<2,3>;
00117 template class Hdf5ToVtkConverter<3,3>;

Generated on Mon Nov 1 12:35:16 2010 for Chaste by  doxygen 1.5.5