XdmfVisitor.cpp 5.14 KB
Newer Older
1
2
3
// Kenneth Leiter
// Xdmf Smart Pointer Test

4
#include "XdmfAttribute.hpp"
5
#include "XdmfDataItem.hpp"
Kenneth Leiter's avatar
Kenneth Leiter committed
6
#include "XdmfDomain.hpp"
7
8
9
#include "XdmfGeometry.hpp"
#include "XdmfGrid.hpp"
#include "XdmfTopology.hpp"
10
11
#include "XdmfVisitor.hpp"

12
13
14
15
#include <iomanip>

XdmfVisitor::XdmfVisitor() :
	mTabIndex(0),
16
	xmlData(),
17
	mLightDataLimit(100),
18
19
	mHeavyFileName("output.h5"),
	hdf5Handle(H5Fcreate("output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT))
20
{
21
	std::cout << "Created Visitor " << this << std::endl;
22
23
24
25
}

XdmfVisitor::~XdmfVisitor()
{
26
	herr_t status = H5Fclose(hdf5Handle);
27
28
29
30
31
	std::cout << "Deleted Visitor " << this << std::endl;
}

void XdmfVisitor::visit(const XdmfAttribute * const attribute)
{
32
33
	xmlData << std::setw(mTabIndex) << "" << "<Attribute Name=\"" << attribute->getName() << "\" AttributeType=\"" << attribute->getAttributeTypeAsString() << "\" Center=\"" << attribute->getAttributeCenterAsString() << "\">\n";
	mTabIndex++;
34
	dataHierarchy.push_back(attribute->getName());
35
	visit((XdmfDataItem*)attribute);
36
	dataHierarchy.pop_back();
37
	mTabIndex--;
38
39
40
	xmlData << std::setw(mTabIndex) << "" << "</Attribute>\n";
}

41
42
void XdmfVisitor::visit(const XdmfDataItem * const dataItem)
{
43
44
45
46
47
48
49
50
	std::string format = "XML";
	if(dataItem->getNumberValues() > mLightDataLimit)
	{
		format = "HDF";
	}
	xmlData << std::setw(mTabIndex) << "" << "<DataItem Format=\"" << format << "\" DataType=\"Int\" Precision=\"4\" Dimensions=\"" << dataItem->getNumberValues() << "\">";

	const int* const intPointer = dataItem->getValues<int>();
51
	mTabIndex++;
52
	if(dataItem->getNumberValues() > mLightDataLimit)
53
	{
54
55
56
57
58
59
60
61
		std::cout << hdf5Handle << std::endl;
		herr_t status;
		hsize_t size = dataItem->getNumberValues();
		hid_t dataspace = H5Screate_simple(1, &size, NULL);
		hid_t handle = hdf5Handle;
		std::string groupName = getHDF5GroupName();
		// Need to make sure this group exists before we add to it.
		if(dataHierarchy.size() > 1)
62
		{
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
			/* Save old error handler */
			H5E_auto_t old_func;
			void* old_client_data;
			H5Eget_auto(0, &old_func, &old_client_data);

			/* Turn off error handling */
			H5Eset_auto2(0, NULL, NULL);

			/* Probe. May fail, but that's okay */
			handle = H5Gopen(hdf5Handle, groupName.c_str(), H5P_DEFAULT);

			/* Restore previous error handler */
			H5Eset_auto2(0, old_func, old_client_data);
			if(handle < 0)
			{
				handle = H5Gcreate(hdf5Handle, groupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
			}
80
		}
81
82
83
84
85
86
87
88
89
		hid_t dataset = H5Dcreate(handle, dataHierarchy.back().c_str(), H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
		xmlData << "\n" << std::setw(mTabIndex) << "" << mHeavyFileName << ":" << groupName << "/" << dataHierarchy.back();
		status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, dataItem->getValues<int>());
		status = H5Dclose(dataset);
		status = H5Sclose(dataspace);
	}
	else
	{
		for (unsigned int i=0; i<dataItem->getNumberValues(); ++i)
90
		{
91
92
93
94
95
96
97
98
			if (i % 10 == 0)
			{
				xmlData << "\n" << std::setw(mTabIndex) << "" << intPointer[i] << " ";
			}
			else
			{
				xmlData << intPointer[i] << " ";
			}
99
100
101
102
		}
	}
	mTabIndex--;
	xmlData << "\n" << std::setw(mTabIndex) << "" << "</DataItem>\n";
103
104
}

Kenneth Leiter's avatar
Kenneth Leiter committed
105
106
107
108
109
110
111
112
113
114
115
116
void XdmfVisitor::visit(const XdmfDomain * const domain)
{
	xmlData << std::setw(mTabIndex) << "" << "<Domain>\n";
	mTabIndex++;
	for(unsigned int i=0; i<domain->getNumberOfGrids(); ++i)
	{
		visit(domain->getGrid(i).get());
	}
	mTabIndex--;
	xmlData << std::setw(mTabIndex) << "" << "</Domain>\n";
}

117
118
119
void XdmfVisitor::visit(const XdmfGeometry * const geometry)
{
	xmlData << std::setw(mTabIndex) << "" << "<Geometry GeometryType=\"" << geometry->getGeometryTypeAsString() << "\">\n";
120
	mTabIndex++;
121
	dataHierarchy.push_back("XYZ");
122
	visit((XdmfDataItem*)geometry);
123
	dataHierarchy.pop_back();
124
	mTabIndex--;
125
126
127
128
129
130
131
	xmlData << std::setw(mTabIndex) << "" << "</Geometry>\n";
}

void XdmfVisitor::visit(const XdmfGrid * const grid)
{
	xmlData << std::setw(mTabIndex) << "" << "<Grid Name=\"" << grid->getName() <<"\">\n";
	mTabIndex++;
132
	dataHierarchy.push_back(grid->getName());
133
134
	visit(grid->getGeometry().get());
	visit(grid->getTopology().get());
135
	for(unsigned int i=0; i<grid->getNumberOfAttributes(); ++i)
136
137
138
	{
		visit(grid->getAttribute(i).get());
	}
139
	dataHierarchy.pop_back();
140
141
	mTabIndex--;
	xmlData << std::setw(mTabIndex) << "" << "</Grid>\n";
142
143
}

144
void XdmfVisitor::visit(const XdmfTopology * const topology)
145
{
146
147
	xmlData << std::setw(mTabIndex) << "" << "<Topology TopologyType=\"" << topology->getTopologyTypeAsString() << "\" NumberOfElements=\"" << topology->getNumberElements() << "\">\n";
	mTabIndex++;
148
	dataHierarchy.push_back("Connectivity");
149
	visit((XdmfDataItem*)topology);
150
	dataHierarchy.pop_back();
151
	mTabIndex--;
152
	xmlData << std::setw(mTabIndex) << "" << "</Topology>\n";
153
154
}

155
std::string XdmfVisitor::printSelf() const
156
{
157
	return "XdmfVisitor:\n" + xmlData.str();
158
}
159

160
161
162
163
164
165
166
167
168
169
int XdmfVisitor::getLightDataLimit() const
{
	return mLightDataLimit;
}

void XdmfVisitor::setLightDataLimit(int numValues)
{
	mLightDataLimit = numValues;
}

170
171
172
173
174
175
176
177
178
179
std::string XdmfVisitor::getHDF5GroupName()
{
	std::stringstream datasetName;
	for(unsigned int i=0; i<dataHierarchy.size() - 1; ++i)
	{
		datasetName << "/" << dataHierarchy[i];
	}
	std::cout << datasetName.str() << std::endl;
	return datasetName.str();
}