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

Generated by  doxygen 1.6.2