XdmfWriter.cpp 6.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
// Kenneth Leiter
// Xdmf Smart Pointer Test

#include "XdmfArray.hpp"
#include "XdmfAttribute.hpp"
#include "XdmfDomain.hpp"
#include "XdmfGeometry.hpp"
#include "XdmfGrid.hpp"
#include "XdmfTopology.hpp"
#include "XdmfWriter.hpp"

//#include <iomanip>

XdmfWriter::XdmfWriter() :
	mLightDataLimit(100),
	mHeavyFileName("output.h5"),
	hdf5Handle(H5Fcreate("output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)),
	xmlDocument(xmlNewDoc((xmlChar*)"1.0")),
	xmlCurrentNode(xmlNewNode(NULL, (xmlChar*)"Xdmf"))
{
	xmlDocSetRootElement(xmlDocument, xmlCurrentNode);
	std::cout << "Created visitor " << this << std::endl;
}

XdmfWriter::~XdmfWriter()
{
	xmlSaveFormatFile("output.xmf", xmlDocument, 1);
	xmlFreeDoc(xmlDocument);
	xmlCleanupParser();
	herr_t status = H5Fclose(hdf5Handle);
	std::cout << "Deleted visitor " << this << std::endl;
}

void XdmfWriter::visit(XdmfAttribute & attribute, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	std::cout << "Writing Attribute" << std::endl;
	xmlNodePtr parentNode = xmlCurrentNode;
	xmlCurrentNode = xmlNewChild(xmlCurrentNode, NULL, (xmlChar*)"Attribute", NULL);
	xmlNewProp(xmlCurrentNode, (xmlChar*)"Name", (xmlChar*)attribute.getName().c_str());
	xmlNewProp(xmlCurrentNode, (xmlChar*)"AttributeType", (xmlChar*)attribute.getAttributeType().getName().c_str());
	xmlNewProp(xmlCurrentNode, (xmlChar*)"Center", (xmlChar*)attribute.getAttributeCenter().getName().c_str());

	dataHierarchy.push_back(attribute.getName());
	attribute.traverse(visitor);
	dataHierarchy.pop_back();

	xmlCurrentNode = parentNode;
}

void XdmfWriter::visit(XdmfArray & array, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	std::cout << "Writing Array" << std::endl;
	xmlNodePtr parentNode = xmlCurrentNode;
	xmlCurrentNode = xmlNewChild(xmlCurrentNode, NULL, (xmlChar*)"DataItem", NULL);

	std::string format = "XML";
	if(array.getSize() > mLightDataLimit)
	{
		format = "HDF";
	}

	xmlNewProp(xmlCurrentNode, (xmlChar*)"Format", (xmlChar*)format.c_str());
	xmlNewProp(xmlCurrentNode, (xmlChar*)"DataType", (xmlChar*)array.getType().c_str());
	std::stringstream precisionString;
	precisionString << array.getPrecision();
	xmlNewProp(xmlCurrentNode, (xmlChar*)"Precision", (xmlChar*)precisionString.str().c_str());
	std::stringstream dimensionString;
	dimensionString << array.getSize();
	xmlNewProp(xmlCurrentNode, (xmlChar*)"Dimensions", (xmlChar*)dimensionString.str().c_str());

	std::stringstream xmlTextValues;
	if(array.getSize() > mLightDataLimit)
	{
		herr_t status;
		hsize_t size = array.getSize();
		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)
		{
			// 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);
			}
		}
		hid_t dataset = H5Dcreate(handle, dataHierarchy.back().c_str(), array.getHDF5Type(), dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
		xmlTextValues << mHeavyFileName << ":" << groupName << "/" << dataHierarchy.back();
		status = H5Dwrite(dataset, array.getHDF5Type(), H5S_ALL, H5S_ALL, H5P_DEFAULT, array.getValuesPointer());
		status = H5Dclose(dataset);
		status = H5Sclose(dataspace);
	}
	else
	{
		xmlTextValues << array.getValuesString();
	}

	xmlAddChild(xmlCurrentNode, xmlNewText((xmlChar*)xmlTextValues.str().c_str()));
	xmlCurrentNode = parentNode;
}

void XdmfWriter::visit(XdmfDomain & domain, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	std::cout << "Writing Domain" << std::endl;
	xmlNodePtr parentNode = xmlCurrentNode;
	xmlCurrentNode = xmlNewChild(xmlCurrentNode, NULL, (xmlChar*)"Domain", NULL);

	domain.traverse(visitor);

	xmlCurrentNode = parentNode;
}

void XdmfWriter::visit(XdmfGeometry & geometry, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	std::cout << "Writing Geometry" << std::endl;
	xmlNodePtr parentNode = xmlCurrentNode;
	xmlCurrentNode = xmlNewChild(xmlCurrentNode, NULL, (xmlChar*)"Geometry", NULL);
	xmlNewProp(xmlCurrentNode, (xmlChar*)"GeometryType", (xmlChar*)geometry.getGeometryType().getName().c_str());

	dataHierarchy.push_back("XYZ");
	geometry.traverse(visitor);
	dataHierarchy.pop_back();

	xmlCurrentNode = parentNode;
}

void XdmfWriter::visit(XdmfGrid & grid, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	std::cout << "Writing Grid" << std::endl;
	xmlNodePtr parentNode = xmlCurrentNode;
    xmlCurrentNode = xmlNewChild(xmlCurrentNode, NULL, (xmlChar*)"Grid", NULL);
	xmlNewProp(xmlCurrentNode, (xmlChar*)"Name", (xmlChar*)grid.getName().c_str());

	dataHierarchy.push_back(grid.getName());
	grid.traverse(visitor);
	dataHierarchy.pop_back();

	xmlCurrentNode = parentNode;
}

void XdmfWriter::visit(XdmfTopology & topology, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
	std::cout << "Writing Topology" << std::endl;
	xmlNodePtr parentNode = xmlCurrentNode;
	xmlCurrentNode = xmlNewChild(xmlCurrentNode, NULL, (xmlChar*)"Topology", NULL);
	xmlNewProp(xmlCurrentNode, (xmlChar*)"TopologyType", (xmlChar*)topology.getTopologyType().getName().c_str());
	std::stringstream numberElementsString;
	numberElementsString << topology.getNumberElements();
	xmlNewProp(xmlCurrentNode, (xmlChar*)"NumberOfElements", (xmlChar*)numberElementsString.str().c_str());

	dataHierarchy.push_back("Connectivity");
	topology.traverse(visitor);
	dataHierarchy.pop_back();

	xmlCurrentNode = parentNode;
}

unsigned int XdmfWriter::getLightDataLimit() const
{
	return mLightDataLimit;
}

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

std::string XdmfWriter::getHDF5GroupName()
{
	std::stringstream datasetName;
	for(unsigned int i=0; i<dataHierarchy.size() - 1; ++i)
	{
		datasetName << "/" << dataHierarchy[i];
	}
	return datasetName.str();
}