Hdf5ToCmguiConverter.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 
00033 #include "Hdf5ToCmguiConverter.hpp"
00034 #include "CmguiMeshWriter.hpp"
00035 #include "UblasCustomFunctions.hpp"
00036 #include "HeartConfig.hpp"
00037 #include "PetscTools.hpp"
00038 #include "Exception.hpp"
00039 #include "ReplicatableVector.hpp"
00040 #include "DistributedVector.hpp"
00041 #include "DistributedVectorFactory.hpp"
00042 #include "Version.hpp"
00043 
00044 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00045 void Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::Write(std::string type)
00046 {
00047     assert(type=="Mono" || type=="Bi");
00048     out_stream p_file=out_stream(NULL);
00049 
00050     unsigned num_nodes = this->mpReader->GetNumberOfRows();
00051     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00052 
00053     DistributedVectorFactory factory(num_nodes);
00054 
00055     Vec data = factory.CreateVec();//for V
00056     Vec data_phie = factory.CreateVec();//for phi_e
00057 
00058     for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00059     {
00060         //create the file for this time step
00061         std::stringstream time_step_string;
00062         //unsigned to string
00063         time_step_string << time_step;
00064         if (PetscTools::AmMaster())
00065         {
00066             p_file = this->mpOutputFileHandler->OpenOutputFile(this->mFileBaseName + "_" + time_step_string.str() + ".exnode");
00067         }
00068 
00069         //read the data for this time step
00070         this->mpReader->GetVariableOverNodes(data, "V", time_step);
00071         ReplicatableVector repl_data(data);
00072         assert(repl_data.GetSize()==num_nodes);
00073 
00074         //get the data for phie only if needed
00075         ReplicatableVector repl_data_phie;
00076         if (type=="Bi")
00077         {
00078             repl_data_phie.Resize(num_nodes);
00079             this->mpReader->GetVariableOverNodes(data_phie, "Phi_e", time_step);
00080             repl_data_phie.ReplicatePetscVector(data_phie);
00081         }
00082 
00083         if(PetscTools::AmMaster())
00084         {
00085             //write provenance info
00086             std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00087             *p_file << comment;
00088             //The header first
00089             *p_file << "Group name: " << this->mFileBaseName << "\n";
00090 
00091             //we need two fields for bidomain and one only for monodomain
00092             if(type=="Mono")
00093             {
00094                *p_file << "#Fields=1" << "\n" << " 1) " << "V , field, rectangular cartesian, #Components=1" << "\n" << "x.  Value index=1, #Derivatives=0, #Versions=1"<<"\n";
00095             }
00096             else
00097             {
00098                *p_file << "#Fields=2" << "\n" << " 1) " << "V , field, rectangular cartesian, #Components=1" << "\n" << "x.  Value index=1, #Derivatives=0, #Versions=1"<<"\n";
00099                 //the details of the second field
00100                *p_file << "\n" << " 2) " << "Phi_e , field, rectangular cartesian, #Components=1" << "\n" << "x.  Value index=1, #Derivatives=0, #Versions=1"<<"\n";
00101             }
00102 
00103             //write the data
00104             for(unsigned i=0; i<num_nodes; i++)
00105             {
00106                 //cmgui counts nodes from 1
00107                 *p_file << "Node: "<< i+1 << "\n" << repl_data[i] << "\n";
00108                 //if it is a bidomain simulation, write the data for phie
00109                 if (type=="Bi")
00110                 {
00111                     *p_file <<  repl_data_phie[i] << "\n";
00112                 }
00113             }
00114         }
00115     }
00116     VecDestroy(data);
00117     VecDestroy(data_phie);
00118 
00119     if(PetscTools::AmMaster())
00120     {
00121         p_file->close();
00122     }
00123 }
00124 
00125 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00126 Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::Hdf5ToCmguiConverter(std::string inputDirectory,
00127                           std::string fileBaseName,
00128                           AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM> *pMesh) :
00129                     AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(inputDirectory, fileBaseName, pMesh, "cmgui_output")
00130 {
00131     //Used to inform the mesh of the data names
00132     std::vector<std::string> field_names;
00133     field_names.push_back("V");
00134     Write("Mono");
00135     if(this->mNumVariables==2)
00136     {
00137         Write("Bi");
00138         field_names.push_back("Phi_e");
00139     }
00140 
00141     //Write mesh in a suitable form for cmgui
00142     std::string output_directory =  HeartConfig::Instance()->GetOutputDirectory() + "/cmgui_output";
00143     CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM> cmgui_mesh_writer(output_directory, HeartConfig::Instance()->GetOutputFilenamePrefix(), false);
00144     cmgui_mesh_writer.SetAdditionalFieldNames(field_names);
00145     cmgui_mesh_writer.WriteFilesUsingMesh(*(this->mpMesh));
00146 
00147     PetscTools::Barrier("Hdf5ToCmguiConverter");
00148 }
00149 
00151 // Explicit instantiation
00153 
00154 template class Hdf5ToCmguiConverter<1,1>;
00155 template class Hdf5ToCmguiConverter<1,2>;
00156 template class Hdf5ToCmguiConverter<2,2>;
00157 template class Hdf5ToCmguiConverter<1,3>;
00158 template class Hdf5ToCmguiConverter<2,3>;
00159 template class Hdf5ToCmguiConverter<3,3>;

Generated by  doxygen 1.6.2