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 
00030 #include <vector>
00031 
00032 #include "UblasCustomFunctions.hpp"
00033 #include "HeartConfig.hpp"
00034 #include "Hdf5ToVtkConverter.hpp"
00035 #include "PetscTools.hpp"
00036 #include "Exception.hpp"
00037 #include "ReplicatableVector.hpp"
00038 #include "DistributedVector.hpp"
00039 #include "DistributedVectorFactory.hpp"
00040 #include "VtkWriter.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                     AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(inputDirectory, fileBaseName, pMesh, "vtk_output")
00047 {
00048 #ifdef CHASTE_VTK
00049 // Requires  "sudo aptitude install libvtk5-dev" or similar
00050 
00051     VtkWriter<ELEMENT_DIM,SPACE_DIM> vtk_writer(HeartConfig::Instance()->GetOutputDirectory() + "/vtk_output", fileBaseName, false);
00052 
00053     unsigned num_nodes = this->mpReader->GetNumberOfRows();
00054     DistributedVectorFactory factory(num_nodes);
00056     Vec data = factory.CreateVec();//for V
00057 
00058     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00059 
00060     // Loop over time steps
00061     for (unsigned time_step=0; time_step<num_timesteps; time_step++) //num_timesteps; time_step++)
00062     {
00063         this->mpReader->GetVariableOverNodes(data, "V", time_step); // Gets V at this time step from HDF5 archive
00064         ReplicatableVector repl_data(data);
00065         std::vector<double> v_for_vtk;
00066         v_for_vtk.resize(num_nodes);
00067         for (unsigned index=0; index<num_nodes; index++)
00068         {
00069             v_for_vtk[index]  = repl_data[index];
00070         }
00071 
00072         std::ostringstream V_point_data_name;
00073         V_point_data_name << "V_" << std::setw(6) << std::setfill('0') << time_step;
00074 
00075         if (PetscTools::AmMaster())
00076         {
00077             // Add V into the node "point" data
00078             vtk_writer.AddPointData(V_point_data_name.str(), v_for_vtk);
00079         }
00080         if(this->mNumVariables==2)
00081         {
00082             this->mpReader->GetVariableOverNodes(data, "Phi_e", time_step); // Gets Phi at this time step from HDF5 archive
00083             ReplicatableVector repl_phi(data);
00084             std::vector<double> phi_for_vtk;
00085             phi_for_vtk.resize(num_nodes);
00086             for (unsigned index=0; index<num_nodes; index++)
00087             {
00088                 phi_for_vtk[index]  = repl_phi[index];
00089             }
00090 
00091             std::ostringstream Phi_point_data_name;
00092             Phi_point_data_name << "Phi_e_" << std::setw(6) << std::setfill('0') << time_step;
00093 
00094             if (PetscTools::AmMaster())
00095             {
00096                 // Add Phi into the node "point" data
00097                 vtk_writer.AddPointData(Phi_point_data_name.str(), phi_for_vtk);
00098             }
00099         }
00100     }
00101     VecDestroy(data);
00102     vtk_writer.WriteFilesUsingMesh( *(this->mpMesh) );
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 by  doxygen 1.6.2