Hdf5ToCmguiConverter.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "Hdf5ToCmguiConverter.hpp"
00037 #include "CmguiMeshWriter.hpp"
00038 #include "UblasCustomFunctions.hpp"
00039 #include "HeartConfig.hpp"
00040 #include "PetscTools.hpp"
00041 #include "Exception.hpp"
00042 #include "ReplicatableVector.hpp"
00043 #include "DistributedVector.hpp"
00044 #include "DistributedVectorFactory.hpp"
00045 #include "Version.hpp"
00046 #include "GenericMeshReader.hpp"
00047 
00048 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00049 void Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::Write(std::string type)
00050 {
00051     out_stream p_file = out_stream(NULL);
00052 
00053     unsigned num_nodes = this->mpReader->GetNumberOfRows();
00054     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00055 
00056     DistributedVectorFactory factory(num_nodes);
00057 
00058     Vec data = factory.CreateVec();//for V
00059     Vec data_phie = factory.CreateVec();//for phi_e
00060     Vec data_second_cell = factory.CreateVec();//for the V of the second cell, used in extended bidomain problems.
00061 
00062     for (unsigned time_step=0; time_step<num_timesteps; time_step++)
00063     {
00064         // Create the file for this time step
00065         std::stringstream time_step_string;
00066 
00067         // unsigned to string
00068         time_step_string << time_step;
00069         if (PetscTools::AmMaster())
00070         {
00071             p_file = this->mpOutputFileHandler->OpenOutputFile(this->mFileBaseName + "_" + time_step_string.str() + ".exnode");
00072 
00073             // Check how many digits are to be output in the solution (0 goes to default value of digits)
00074             if (this->mPrecision != 0)
00075             {
00076                p_file->precision(this->mPrecision);
00077             }
00078         }
00079 
00080         std::vector<ReplicatableVector*> all_data;
00081         unsigned num_vars = this->mpReader->GetVariableNames().size();
00082         for (unsigned var=0; var<num_vars; var++)
00083         {
00084             // Read the data for this time step
00085             this->mpReader->GetVariableOverNodes(data, this->mpReader->GetVariableNames()[var], time_step);
00086             ReplicatableVector* p_repl_data = new ReplicatableVector(data);
00087             assert(p_repl_data->GetSize()==num_nodes);
00088             all_data.push_back(p_repl_data);
00089         }
00090 
00091         if (PetscTools::AmMaster())
00092         {
00093             // Write provenance info
00094             std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00095             *p_file << comment;
00096             // The header first
00097             *p_file << "Group name: " << this->mFileBaseName << "\n";
00098             *p_file << "#Fields=" << num_vars << "\n";
00099             for (unsigned var=0; var<num_vars; var++)
00100             {
00101                 *p_file << " " << var+1 << ") " <<this->mpReader->GetVariableNames()[var]<< " , field, rectangular cartesian, #Components=1" << "\n" << "x.  Value index=1, #Derivatives=0, #Versions=1"<<"\n";
00102                 if (var != num_vars-1)
00103                 {
00104                     *p_file << "\n";
00105                 }
00106             }
00107 
00108             // Write the data
00109             for (unsigned i=0; i<num_nodes; i++)
00110             {
00111                 // cmgui counts nodes from 1
00112                 *p_file << "Node: "<< i+1 << "\n";
00113                 for (unsigned var=0; var<num_vars; var++)
00114                 {
00115                     *p_file  << (*(all_data[var]))[i] << "\n";
00116                 }
00117             }
00118         }
00119 
00120         for (unsigned var=0; var<num_vars; var++)
00121         {
00122            delete all_data[var];
00123         }
00124     }
00125     PetscTools::Destroy(data);
00126     PetscTools::Destroy(data_phie);
00127     PetscTools::Destroy(data_second_cell);
00128 
00129     if (PetscTools::AmMaster())
00130     {
00131         p_file->close();
00132     }
00133 }
00134 
00135 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00136 Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::Hdf5ToCmguiConverter(const FileFinder& rInputDirectory,
00137                                                                   const std::string& rFileBaseName,
00138                                                                   AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* pMesh,
00139                                                                   bool hasBath,
00140                                                                   unsigned precision)
00141     : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "cmgui_output", precision)
00142 {
00144     while (this->mDatasetNames[this->mOpenDatasetIndex] != "Data")
00145     {
00146         bool next_open = this->MoveOntoNextDataset();
00147         UNUSED_OPT(next_open);
00148         assert(next_open);
00149     }
00150 
00151     // Write the node data out
00152     Write("");
00153 
00154     // Write mesh in a suitable form for cmgui
00155     std::string output_directory =  HeartConfig::Instance()->GetOutputDirectory() + "/cmgui_output";
00156 
00157     CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM> cmgui_mesh_writer(output_directory, HeartConfig::Instance()->GetOutputFilenamePrefix(), false);
00158 
00159     // Used to inform the mesh of the data names
00160     std::vector<std::string> field_names = this->mpReader->GetVariableNames();
00161     cmgui_mesh_writer.SetAdditionalFieldNames(field_names);
00162     if (hasBath)
00163     {
00164         std::vector<std::string> names;
00165         names.push_back("tissue");
00166         names.push_back("bath");
00167         cmgui_mesh_writer.SetRegionNames(names);
00168     }
00169 
00170     // Normally the in-memory mesh is converted:
00171     if (HeartConfig::Instance()->GetOutputUsingOriginalNodeOrdering() == false || !this->mpMesh->IsMeshOnDisk())
00172     {
00173         cmgui_mesh_writer.WriteFilesUsingMesh(*(this->mpMesh), false);
00174     }
00175     else
00176     {
00177         // In this case we expect the mesh to have been read in from file
00179         // Note that the next line will throw if the mesh has not been read from file
00180         std::string original_file=this->mpMesh->GetMeshFileBaseName();
00181         std::auto_ptr<AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> > p_original_mesh_reader
00182             = GenericMeshReader<ELEMENT_DIM, SPACE_DIM>(original_file);
00183         cmgui_mesh_writer.WriteFilesUsingMeshReader(*p_original_mesh_reader);
00184     }
00185 
00186     WriteCmguiScript();
00187     PetscTools::Barrier("Hdf5ToCmguiConverter");
00188 }
00189 
00190 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00191 void Hdf5ToCmguiConverter<ELEMENT_DIM,SPACE_DIM>::WriteCmguiScript()
00192 {
00193     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00194     assert(this->mpReader->GetVariableNames().size() > 0); // seg fault guard
00195     std::string variable_name = this->mpReader->GetVariableNames()[0];
00196 
00197     if (PetscTools::AmMaster())
00198     {
00199         out_stream p_script_file = this->mpOutputFileHandler->OpenOutputFile("LoadSolutions.com");
00200 
00201         // Write provenance info, note the # instead of ! because this is - essentially - a PERL script that Cmgui interprets
00202         std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00203         *p_script_file << comment;
00204 
00205         *p_script_file << "# Read the mesh \n"
00206                        << "gfx read node "<<HeartConfig::Instance()->GetOutputFilenamePrefix()<<".exnode \n"
00207                        << "gfx read elem "<<HeartConfig::Instance()->GetOutputFilenamePrefix()<<".exelem \n" // note the mesh file name is taken from HeartConfig...
00208                        << "gfx define faces egroup "<<HeartConfig::Instance()->GetOutputFilenamePrefix()<<"\n"
00209                        << "# Create a window \n"
00210                        << "gfx cre win 1 \n"
00211                        << "# Modify the scene (obtained by gfx list g_element XXXX commands) to visualize first var on lines and nodes \n"
00212                        << "gfx modify g_element "<< HeartConfig::Instance()->GetOutputFilenamePrefix()<<" general clear circle_discretization 6 default_coordinate coordinates element_discretization \"4*4*4\" native_discretization none; \n"
00213                        << "gfx modify g_element "<< HeartConfig::Instance()->GetOutputFilenamePrefix()<<" lines select_on material default data "<<variable_name<<" spectrum default selected_material default_selected; \n"
00214                        << "gfx modify g_element "<< HeartConfig::Instance()->GetOutputFilenamePrefix()<<" node_points glyph point general size \"1*1*1\" centre 0,0,0 font default select_on material default data "<<variable_name<<" spectrum default selected_material default_selected; \n"
00215                        << "# Load the data \n"
00216                        << "for ($i=0; $i<" << num_timesteps << "; $i++) { \n"
00217                        << "    gfx read node " << this->mFileBaseName << "_$i.exnode time $i\n" // ...while the data file from mFileBaseName...
00218                        << "}\n";
00219         p_script_file->close();
00220     }
00221 }
00222 
00224 // Explicit instantiation
00226 
00227 template class Hdf5ToCmguiConverter<1,1>;
00228 template class Hdf5ToCmguiConverter<1,2>;
00229 template class Hdf5ToCmguiConverter<2,2>;
00230 template class Hdf5ToCmguiConverter<1,3>;
00231 template class Hdf5ToCmguiConverter<2,3>;
00232 template class Hdf5ToCmguiConverter<3,3>;

Generated by  doxygen 1.6.2