Chaste  Release::3.4
Hdf5ToXdmfConverter.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "Hdf5ToXdmfConverter.hpp"
37 
38 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
40  const std::string& rFileBaseName,
42  : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "xdmf_output", 0u),
43  XdmfMeshWriter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory.GetRelativePath(FileFinder("", RelativeTo::ChasteTestOutput)) + "/xdmf_output",
44  rFileBaseName, false /* Not cleaning directory*/)
45 {
46  // Set number of time steps
47  std::vector<double> time_values = this->mpReader->GetUnlimitedDimensionValues();
48  unsigned num_timesteps = time_values.size();
49  this->mNumberOfTimePoints = num_timesteps;
50  // Set time step size
51  if (num_timesteps > 1)
52  {
53  this->mTimeStep = time_values[1] - time_values[0];
54  }
55  // Write
56  this->WriteFilesUsingMesh(*pMesh);
57 }
58 
59 #ifndef _MSC_VER
60 
61 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
62 void Hdf5ToXdmfConverter<ELEMENT_DIM, SPACE_DIM>::AddDataOnNodes(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement* pGridElement,
63  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* pDomDocument,
64  unsigned timeStep)
65 {
66  // Use xerces namespace for convenience
67  XERCES_CPP_NAMESPACE_USE
68 
69  unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
70 
71  // Loop over variables
72  for (unsigned var_index=0; var_index<this->mNumVariables; var_index++)
73  {
74  std::string variable_name = this->mpReader->GetVariableNames()[var_index];
75 
76  /*
77  * e.g. <Attribute Center="Node" Name="V">
78  */
79  DOMElement* p_attr_element = pDomDocument->createElement(X("Attribute"));
80  p_attr_element->setAttribute(X("Name"), X(variable_name));
81  p_attr_element->setAttribute(X("Center"), X("Node"));
82  pGridElement->appendChild(p_attr_element);
83 
84  /*
85  * e.g. <DataItem Dimensions="1 12 1" ItemType="HyperSlab">
86  */
87  DOMElement* p_hype_element = pDomDocument->createElement(X("DataItem"));
88  p_hype_element->setAttribute(X("ItemType"), X("HyperSlab"));
89  std::stringstream dim_stream;
90 
91  // First index is time value, second is number of nodes, third is variable index
93  /* DistributedVectorFactory* p_factory = AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::mpMesh->GetDistributedVectorFactory();
94  dim_stream << "1 " << p_factory->GetHigh()-p_factory->GetLow() << " 1"; */
95  unsigned num_nodes = AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::mpMesh->GetNumNodes();
96  dim_stream << "1 " << num_nodes << " 1";
97  p_hype_element->setAttribute(X("Dimensions"), X(dim_stream.str()));
98  p_attr_element->appendChild(p_hype_element);
99 
100  /*
101  * e.g. <DataItem Dimensions="3 3" Format="XML">
102  */
103  DOMElement* p_xml_element = pDomDocument->createElement(X("DataItem"));
104  p_xml_element->setAttribute(X("Format"), X("XML"));
105  p_xml_element->setAttribute(X("Dimensions"), X("3 3"));
106  p_hype_element->appendChild(p_xml_element);
107 
108  /*
109  * e.g. 0 0 0 1 1 1 1 12 1
110  */
111  std::stringstream XMLStream;
112  XMLStream << timeStep << " 0 " << var_index << " ";
113  XMLStream << "1 1 1 ";
114  /* XMLStream << "1 " << p_factory->GetHigh()-p_factory->GetLow() << " 1"; */
115  XMLStream << "1 " << num_nodes << " 1";
116  DOMText* p_xml_text = pDomDocument->createTextNode(X(XMLStream.str()));
117  p_xml_element->appendChild(p_xml_text);
118 
119  /*
120  * e.g. <DataItem Dimensions="2 12 2" Format="HDF" NumberType="Float" Precision="8">
121  */
122  DOMElement* p_hdf_element = pDomDocument->createElement(X("DataItem"));
123  p_hdf_element->setAttribute(X("Format"), X("HDF"));
124  p_hdf_element->setAttribute(X("NumberType"), X("Float"));
125  p_hdf_element->setAttribute(X("Precision"), X("8"));
126  std::stringstream hdf_dims_stream;
127  /* hdf_dims_stream << num_timesteps << " " << p_factory->GetHigh()-p_factory->GetLow() << " " << this->mNumVariables; */
128  hdf_dims_stream << num_timesteps << " " << num_nodes << " " << this->mNumVariables;
129  p_hdf_element->setAttribute(X("Dimensions"), X(hdf_dims_stream.str()));
130  p_hype_element->appendChild(p_hdf_element);
131 
132  /*
133  * e.g. ../cube_2mm_12_elements.h5:/Data
134  */
135  std::stringstream HDFStream;
136  HDFStream << "../" << AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::mFileBaseName << ".h5:/Data";
137  DOMText* p_hdf_text = pDomDocument->createTextNode(X(HDFStream.str()));
138  p_hdf_element->appendChild(p_hdf_text);
139  }
140 }
141 
142 #endif
143 
145 // Explicit instantiation
147 
148 template class Hdf5ToXdmfConverter<1,1>;
149 template class Hdf5ToXdmfConverter<1,2>;
150 template class Hdf5ToXdmfConverter<2,2>;
151 template class Hdf5ToXdmfConverter<1,3>;
152 template class Hdf5ToXdmfConverter<2,3>;
153 template class Hdf5ToXdmfConverter<3,3>;
boost::shared_ptr< Hdf5DataReader > mpReader
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)
unsigned mNumberOfTimePoints
Hdf5ToXdmfConverter(const FileFinder &rInputDirectory, const std::string &rFileBaseName, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
void AddDataOnNodes(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *pGridElement, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDomDocument, unsigned timeStep)