Hdf5ToMeshalyzerConverter.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 "Hdf5ToMeshalyzerConverter.hpp"
00030 #include "MeshalyzerMeshWriter.hpp"
00031 #include "GenericMeshReader.hpp"
00032 #include "UblasCustomFunctions.hpp"
00033 #include "HeartConfig.hpp"
00034 #include "PetscTools.hpp"
00035 #include "Exception.hpp"
00036 #include "ReplicatableVector.hpp"
00037 #include "DistributedVector.hpp"
00038 #include "DistributedVectorFactory.hpp"
00039 #include "Version.hpp"
00040 
00041 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00042 void Hdf5ToMeshalyzerConverter<ELEMENT_DIM,SPACE_DIM>::Write(std::string type)
00043 {
00044     out_stream p_file = out_stream(NULL);
00045     if (PetscTools::AmMaster())
00046     {
00047         p_file = this->mpOutputFileHandler->OpenOutputFile(this->mFileBaseName + "_" + type + ".dat");
00048 
00049         // Check how many digits are to be output in the solution (0 goes to default value of digits)
00050         unsigned num_digits = HeartConfig::Instance()->GetVisualizerOutputPrecision();
00051         if (num_digits != 0)
00052         {
00053            p_file->precision(num_digits);
00054         }
00055     }
00056 
00057     unsigned num_nodes = this->mpReader->GetNumberOfRows();
00058     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00059 
00060     DistributedVectorFactory factory(num_nodes);
00061 
00062     Vec data = factory.CreateVec();
00063     ReplicatableVector repl_data(num_nodes);
00064     for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00065     {
00066         this->mpReader->GetVariableOverNodes(data, type, time_step);
00067         repl_data.ReplicatePetscVector(data);
00068 
00069         assert(repl_data.GetSize() == num_nodes);
00070 
00071         if (PetscTools::AmMaster())
00072         {
00073             for (unsigned i=0; i<num_nodes; i++)
00074             {
00075                 *p_file << repl_data[i] << "\n";
00076             }
00077         }
00078     }
00079     VecDestroy(data);
00080     if (PetscTools::AmMaster())
00081     {
00082         std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00083         *p_file << comment;
00084         p_file->close();
00085     }
00086 }
00087 
00088 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00089 Hdf5ToMeshalyzerConverter<ELEMENT_DIM,SPACE_DIM>::Hdf5ToMeshalyzerConverter(std::string inputDirectory,
00090                                                                             std::string fileBaseName,
00091                                                                             AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* pMesh,
00092                                                                             bool usingOriginalNodeOrdering)
00093     : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(inputDirectory, fileBaseName, pMesh, "output")
00094 {
00095     std::vector<std::string> variable_names = this->mpReader->GetVariableNames();
00096     for (unsigned i=0; i<variable_names.size(); i++)
00097     {
00098         Write(variable_names[i]);
00099     }
00100 
00101     // Write mesh in a suitable form for meshalyzer
00102     std::string output_directory = inputDirectory + "/" + this->mRelativeSubdirectory;
00103     MeshalyzerMeshWriter<ELEMENT_DIM,SPACE_DIM> mesh_writer(output_directory, fileBaseName+"_mesh", false);
00104 
00105     // Normal case is that the in-memory mesh is converted
00106     if (!usingOriginalNodeOrdering)
00107     {
00108         // The second argument tells the writer to not follow original element ordering for performance reasons.
00109         mesh_writer.WriteFilesUsingMesh(*(this->mpMesh), false);
00110     }
00111     else
00112     {
00113         // In this case we expect the mesh to have been read in from file
00115         // Note that the next line will throw if the mesh has not been read from file
00116         std::string original_file = this->mpMesh->GetMeshFileBaseName();
00117         std::auto_ptr<AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> > p_original_mesh_reader
00118             = GenericMeshReader<ELEMENT_DIM, SPACE_DIM>(original_file);
00119         mesh_writer.WriteFilesUsingMeshReader(*p_original_mesh_reader);
00120     }
00121     PetscTools::Barrier("Hdf5ToMeshalyzerConverter");
00122 }
00123 
00125 // Explicit instantiation
00127 
00128 template class Hdf5ToMeshalyzerConverter<1,1>;
00129 template class Hdf5ToMeshalyzerConverter<1,2>;
00130 template class Hdf5ToMeshalyzerConverter<2,2>;
00131 template class Hdf5ToMeshalyzerConverter<1,3>;
00132 template class Hdf5ToMeshalyzerConverter<2,3>;
00133 template class Hdf5ToMeshalyzerConverter<3,3>;
Generated on Thu Dec 22 13:00:06 2011 for Chaste by  doxygen 1.6.3