Hdf5ToXdmfConverter.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2014, 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 "Hdf5ToXdmfConverter.hpp"
00037 
00038 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 Hdf5ToXdmfConverter<ELEMENT_DIM, SPACE_DIM>::Hdf5ToXdmfConverter(const FileFinder& rInputDirectory,
00040                                                                  const std::string& rFileBaseName,
00041                                                                  AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>* pMesh)
00042     : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "xdmf_output", 0u),
00043       XdmfMeshWriter<ELEMENT_DIM,SPACE_DIM>("TestHdf5ToXdmfConverter/xdmf_output", rFileBaseName, false /* Not cleaning directory*/)
00044 {
00046 
00047     // Set number of time steps
00048     std::vector<double> time_values = this->mpReader->GetUnlimitedDimensionValues();
00049     unsigned num_timesteps = time_values.size();
00050     this->mNumberOfTimePoints = num_timesteps;
00051     // Set time step size
00052     if (num_timesteps > 1)
00053     {
00054         this->mTimeStep = time_values[1] - time_values[0];
00055     }
00056     // Write
00057     this->WriteFilesUsingMesh(*pMesh);
00058 }
00059 
00060 #ifndef _MSC_VER
00061 
00062 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00063 void Hdf5ToXdmfConverter<ELEMENT_DIM, SPACE_DIM>::AddDataOnNodes(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* pGridElement,
00064                                                                  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* pDomDocument,
00065                                                                  unsigned timeStep)
00066 {
00067     // Use xerces namespace for convenience
00068     XERCES_CPP_NAMESPACE_USE
00069 
00070     unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
00071 
00072     // Loop over variables
00073     for (unsigned var_index=0; var_index<this->mNumVariables; var_index++)
00074     {
00075         std::string variable_name = this->mpReader->GetVariableNames()[var_index];
00076 
00077         /*
00078          * e.g. <Attribute Center="Node" Name="V">
00079          */
00080         DOMElement* p_attr_element =  pDomDocument->createElement(X("Attribute"));
00081         p_attr_element->setAttribute(X("Name"), X(variable_name));
00082         p_attr_element->setAttribute(X("Center"), X("Node"));
00083         pGridElement->appendChild(p_attr_element);
00084 
00085         /*
00086          * e.g. <DataItem Dimensions="1 12 1" ItemType="HyperSlab">
00087          */
00088         DOMElement* p_hype_element =  pDomDocument->createElement(X("DataItem"));
00089         p_hype_element->setAttribute(X("ItemType"), X("HyperSlab"));
00090         std::stringstream dim_stream;
00091 
00092         // First index is time value, second is number of nodes, third is variable index
00094         /* DistributedVectorFactory* p_factory = AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::mpMesh->GetDistributedVectorFactory();
00095         dim_stream << "1 " << p_factory->GetHigh()-p_factory->GetLow() << " 1"; */
00096         unsigned num_nodes = AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::mpMesh->GetNumNodes();
00097         dim_stream << "1 " << num_nodes << " 1";
00098         p_hype_element->setAttribute(X("Dimensions"), X(dim_stream.str()));
00099         p_attr_element->appendChild(p_hype_element);
00100 
00101         /*
00102          * e.g. <DataItem Dimensions="3 3" Format="XML">
00103          */
00104         DOMElement* p_xml_element =  pDomDocument->createElement(X("DataItem"));
00105         p_xml_element->setAttribute(X("Format"), X("XML"));
00106         p_xml_element->setAttribute(X("Dimensions"), X("3 3"));
00107         p_hype_element->appendChild(p_xml_element);
00108 
00109         /*
00110          * e.g. 0 0 0 1 1 1 1 12 1
00111          */
00112         std::stringstream XMLStream;
00113         XMLStream << timeStep << " 0 " << var_index << " ";
00114         XMLStream << "1 1 1 ";
00115         /* XMLStream << "1 " << p_factory->GetHigh()-p_factory->GetLow() << " 1"; */
00116         XMLStream << "1 " << num_nodes << " 1";
00117         DOMText* p_xml_text = pDomDocument->createTextNode(X(XMLStream.str()));
00118         p_xml_element->appendChild(p_xml_text);
00119 
00120         /*
00121          * e.g. <DataItem Dimensions="2 12 2" Format="HDF" NumberType="Float" Precision="8">
00122          */
00123         DOMElement* p_hdf_element =  pDomDocument->createElement(X("DataItem"));
00124         p_hdf_element->setAttribute(X("Format"), X("HDF"));
00125         p_hdf_element->setAttribute(X("NumberType"), X("Float"));
00126         p_hdf_element->setAttribute(X("Precision"), X("8"));
00127         std::stringstream hdf_dims_stream;
00128         /* hdf_dims_stream << num_timesteps << " " << p_factory->GetHigh()-p_factory->GetLow() << " " << this->mNumVariables; */
00129         hdf_dims_stream << num_timesteps << " " << num_nodes << " " << this->mNumVariables;
00130         p_hdf_element->setAttribute(X("Dimensions"), X(hdf_dims_stream.str()));
00131         p_hype_element->appendChild(p_hdf_element);
00132 
00133         /*
00134          * e.g. ../cube_2mm_12_elements.h5:/Data
00135          */
00136         std::stringstream HDFStream;
00137         HDFStream << "../" << AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::mFileBaseName << ".h5:/Data";
00138         DOMText* p_hdf_text = pDomDocument->createTextNode(X(HDFStream.str()));
00139         p_hdf_element->appendChild(p_hdf_text);
00140     }
00141 }
00142 
00143 #endif
00144 
00146 // Explicit instantiation
00148 
00149 template class Hdf5ToXdmfConverter<1,1>;
00150 template class Hdf5ToXdmfConverter<1,2>;
00151 template class Hdf5ToXdmfConverter<2,2>;
00152 template class Hdf5ToXdmfConverter<1,3>;
00153 template class Hdf5ToXdmfConverter<2,3>;
00154 template class Hdf5ToXdmfConverter<3,3>;

Generated by  doxygen 1.6.2