Hdf5ToMeshalyzerConverter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 "Hdf5ToMeshalyzerConverter.hpp"
00035 #include "PetscTools.hpp"
00036 #include "Exception.hpp"
00037 #include "OutputFileHandler.hpp"
00038 #include "ReplicatableVector.hpp"
00039 #include "DistributedVector.hpp"
00040 #include "DistributedVectorFactory.hpp"
00041 
00042 void Hdf5ToMeshalyzerConverter::Write(std::string type)
00043 {
00044     assert(type=="V" || type=="Phi_e");
00045 
00046     out_stream p_file=out_stream(NULL);
00047     OutputFileHandler output_file_handler(HeartConfig::Instance()->GetOutputDirectory() + "/output", false);
00048     if (PetscTools::AmMaster())
00049     {
00050         p_file = output_file_handler.OpenOutputFile(mFileBaseName + "_" + type + ".dat");
00051     }
00052 
00053     unsigned num_nodes = mpReader->GetNumberOfRows();
00054     unsigned num_timesteps = mpReader->GetUnlimitedDimensionValues().size();
00055 
00056     DistributedVectorFactory factory(num_nodes);
00057 
00058     Vec data = factory.CreateVec();
00059     for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00060     {
00061         mpReader->GetVariableOverNodes(data, type, time_step);
00062         ReplicatableVector repl_data(data);
00063 
00064         assert(repl_data.size()==num_nodes);
00065 
00066         if(PetscTools::AmMaster())
00067         {
00068             for(unsigned i=0; i<num_nodes; i++)
00069             {
00070                 *p_file << repl_data[i] << "\n";
00071             }
00072         }
00073     }
00074     VecDestroy(data);
00075     if(PetscTools::AmMaster())
00076     {
00077         p_file->close();
00078     }
00079 }
00080 
00081 
00082 Hdf5ToMeshalyzerConverter::Hdf5ToMeshalyzerConverter(std::string inputDirectory,
00083                           std::string fileBaseName)
00084 {
00085     // store dir and filenames, and create a reader
00086     mFileBaseName = fileBaseName;
00087     mpReader = new Hdf5DataReader(inputDirectory, mFileBaseName);
00088 
00089     // check the data file read has one or two variables (ie V; or V and PhiE)
00090     std::vector<std::string> variable_names = mpReader->GetVariableNames();
00091     if((variable_names.size()==0) || (variable_names.size()>2))
00092     {
00093         delete mpReader;
00094         EXCEPTION("Data has zero or more than two variables - doesn't appear to be mono or bidomain");
00095     }
00096 
00097     // if one variable, a monodomain problem
00098     if(variable_names.size()==1)
00099     {
00100         if(variable_names[0]!="V")
00101         {
00102             delete mpReader;
00103             EXCEPTION("One variable, but it is not called 'V'");
00104         }
00105 
00106         Write("V");
00107     }
00108 
00109     // if two variable, a bidomain problem
00110     if(variable_names.size()==2)
00111     {
00112         if(variable_names[0]!="V" || variable_names[1]!="Phi_e")
00113         {
00114             delete mpReader;
00115             EXCEPTION("Two variables, but they are not called 'V' and 'Phi_e'");
00116         }
00117 
00118         Write("V");
00119         Write("Phi_e");
00120     }
00121 
00122     if (PetscTools::AmMaster())
00123     {
00124         //Note that we don't want the child processes to create
00125         //a fresh directory if it doesn't already exist
00126         OutputFileHandler output_file_handler(HeartConfig::Instance()->GetOutputDirectory() + "/output", false);
00127         
00128         out_stream p_file = output_file_handler.OpenOutputFile(mFileBaseName + "_times.info");
00129         unsigned num_timesteps = mpReader->GetUnlimitedDimensionValues().size();
00130         *p_file << "Number of timesteps "<<num_timesteps<<"\n";
00131         *p_file << "timestep "<<HeartConfig::Instance()->GetPrintingTimeStep()<<"\n";
00132         double first_timestep=mpReader->GetUnlimitedDimensionValues().front();
00133         *p_file << "First timestep "<<first_timestep<<"\n";
00134         double last_timestep=mpReader->GetUnlimitedDimensionValues().back();
00135         *p_file << "Last timestep "<<last_timestep<<"\n";
00136 
00137         p_file->close();
00138 
00139     }
00140 
00141     MPI_Barrier(PETSC_COMM_WORLD);
00142 
00143 
00144 }
00145 
00146 Hdf5ToMeshalyzerConverter::~Hdf5ToMeshalyzerConverter()
00147 {
00148     delete mpReader;
00149 }

Generated on Tue Aug 4 16:10:22 2009 for Chaste by  doxygen 1.5.5