Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
Hdf5ToXdmfConverter.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "Hdf5ToXdmfConverter.hpp"
37
38template <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
61template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
62void 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
144// Explicit instantiation
145template class Hdf5ToXdmfConverter<1,1>;
146template class Hdf5ToXdmfConverter<1,2>;
147template class Hdf5ToXdmfConverter<2,2>;
148template class Hdf5ToXdmfConverter<1,3>;
149template class Hdf5ToXdmfConverter<2,3>;
150template class Hdf5ToXdmfConverter<3,3>;
boost::shared_ptr< Hdf5DataReader > mpReader
void AddDataOnNodes(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *pGridElement, XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *pDomDocument, unsigned timeStep)
Hdf5ToXdmfConverter(const FileFinder &rInputDirectory, const std::string &rFileBaseName, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
unsigned mNumberOfTimePoints
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)