Hdf5ToTxtConverter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
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 "Hdf5ToTxtConverter.hpp"
00031 #include "PetscTools.hpp"
00032 #include "Exception.hpp"
00033 #include "ReplicatableVector.hpp"
00034 #include "DistributedVector.hpp"
00035 #include "DistributedVectorFactory.hpp"
00036 #include "DistributedTetrahedralMesh.hpp"
00037 #include "Warnings.hpp"
00038 
00039 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00040 Hdf5ToTxtConverter<ELEMENT_DIM, SPACE_DIM>::Hdf5ToTxtConverter(std::string inputDirectory,
00041                                                                std::string fileBaseName,
00042                                                                AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>* pMesh)
00043     : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(inputDirectory, fileBaseName, pMesh, "txt_output")
00044 {
00045     // Make sure that we are never trying to write from an incomplete data HDF5 file
00046     assert(this->mpReader->GetNumberOfRows() == pMesh->GetNumNodes());
00047 
00048     std::string output_directory = inputDirectory + "/" + this->mRelativeSubdirectory;
00049     OutputFileHandler handler(output_directory);
00050 
00051     unsigned num_nodes = pMesh->GetNumNodes();
00052     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00053 
00054     DistributedVectorFactory* p_factory = pMesh->GetDistributedVectorFactory();
00055     Vec data = p_factory->CreateVec();
00056     ReplicatableVector repl_data(num_nodes);
00057 
00058     // Loop over time steps
00059     for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00060     {
00061         // Loop over variables
00062         for (unsigned var_index=0; var_index<this->mNumVariables; var_index++)
00063         {
00064             std::string variable_name = this->mpReader->GetVariableNames()[var_index];
00065 
00066             // Create a .txt file for this time step and this variable
00067             std::stringstream file_name;
00068             file_name << fileBaseName << "_" << variable_name << "_" << time_step << ".txt";
00069             out_stream p_file = handler.OpenOutputFile(file_name.str());
00070 
00071             this->mpReader->GetVariableOverNodes(data, variable_name, time_step);
00072             repl_data.ReplicatePetscVector(data);
00073 
00074             assert(repl_data.GetSize() == num_nodes);
00075 
00076             if (PetscTools::AmMaster())
00077             {
00078                 for (unsigned i=0; i<num_nodes; i++)
00079                 {
00080                     *p_file << repl_data[i] << "\n";
00081                 }
00082             }
00083             p_file->close();
00084         }
00085     }
00086 
00087     // Tidy up
00088     VecDestroy(data);
00089 }
00090 
00092 // Explicit instantiation
00094 
00095 template class Hdf5ToTxtConverter<1,1>;
00096 template class Hdf5ToTxtConverter<1,2>;
00097 template class Hdf5ToTxtConverter<2,2>;
00098 template class Hdf5ToTxtConverter<1,3>;
00099 template class Hdf5ToTxtConverter<2,3>;
00100 template class Hdf5ToTxtConverter<3,3>;
Generated on Thu Dec 22 13:00:17 2011 for Chaste by  doxygen 1.6.3