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

Generated on Wed Mar 18 12:51:52 2009 for Chaste by  doxygen 1.5.5