Commit 46189e19 authored by Ken Leiter (Civ's avatar Ken Leiter (Civ

ENH: Add ability to write HDF5 files as part of XdmfVisitor operation.

parent 5c280140
......@@ -8,9 +8,13 @@ set(BUILD_SHARED_LIBS true)
find_package(Boost REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message(${Boost_INCLUDE_DIRS})
endif(Boost_FOUND)
find_package(HDF5 REQUIRED)
if(HDF5_FOUND)
include_directories(${HDF5_INCLUDE_DIR})
endif(HDF5_FOUND)
option(XDMF_WRAP_PYTHON ON)
if(XDMF_WRAP_PYTHON)
find_package(PythonLibs REQUIRED)
......@@ -37,6 +41,7 @@ set(XdmfSources
)
add_library(Xdmf ${XdmfSources})
target_link_libraries(Xdmf ${HDF5_LIBRARIES})
if(XDMF_WRAP_PYTHON)
add_library(_Xdmf MODULE XdmfPython)
......
......@@ -47,9 +47,9 @@ public:
void setAttributeType(const XdmfAttributeType& attributeType);
/**
* Get the name of the AttributeType associated with this Attribute.
* Get the name of the XdmfAttributeType associated with this Attribute.
*
* @return std::string containing name of the AttributeType.
* @return std::string containing name of the XdmfAttributeType.
*/
std::string getAttributeTypeAsString() const;
......@@ -70,16 +70,21 @@ public:
/**
* Get the name of the XdmfAttributeCenter associated with this Attribute.
*
* @return std::string containing name of the AttributeCenter.
* @return std::string containing name of the XdmfAttributeCenter.
*/
std::string getAttributeCenterAsString() const;
/**
* Set the number of values this attribute contains.
*
* @param an int giving the number of values.
*/
void setNumberValues(int numValues);
void write(boost::shared_ptr<XdmfVisitor> visitor) const;
virtual std::string printSelf() const;
void setNumberValues(int numValues);
protected:
XdmfAttribute();
......
......@@ -26,30 +26,93 @@ std::string XdmfDataItem::printSelf() const
}
template <>
void XdmfDataItem::setData<int>(int* dataPointer)
void XdmfDataItem::setValues<char>(char* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int8();
}
template <>
void XdmfDataItem::setValues<unsigned char>(unsigned char* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::UInt8();
}
template <>
void XdmfDataItem::setValues<short>(short* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int16();
}
template <>
void XdmfDataItem::setValues<unsigned short>(unsigned short* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::UInt16();
}
template <>
void XdmfDataItem::setValues<int>(int* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int32();
}
template <>
int* XdmfDataItem::getData<int>()
void XdmfDataItem::setValues<unsigned int>(unsigned int* dataPointer)
{
return mDataPointer;
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::UInt32();
}
template <>
const int* const XdmfDataItem::getData<int>() const
void XdmfDataItem::setValues<long>(long* dataPointer)
{
return mDataPointer;
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Int64();
}
int XdmfDataItem::getNumberValues() const
template <>
void XdmfDataItem::setValues<float>(float* dataPointer)
{
return mNumberValues;
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Float32();
}
template <>
void XdmfDataItem::setValues<double>(double* dataPointer)
{
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Float64();
}
void XdmfDataItem::write(boost::shared_ptr<XdmfVisitor> visitor) const
template <typename T>
void XdmfDataItem::setValues(T* dataPointer)
{
visitor->visit(this);
mDataPointer = dataPointer;
mDataType = XdmfDataItemType::Unknown();
}
template <>
int* XdmfDataItem::getValues<int>()
{
return (int*)mDataPointer;
}
template <>
const int* const XdmfDataItem::getValues<int>() const
{
return (int*)mDataPointer;
}
int XdmfDataItem::getNumberValues() const
{
return mNumberValues;
}
//void XdmfDataItem::write(boost::shared_ptr<XdmfVisitor> visitor) const
//{
// visitor->visit(this);
//}
......@@ -16,19 +16,19 @@ class XdmfDataItem : public XdmfItem {
public:
XdmfNewMacro(XdmfDataItem);
//XdmfNewMacro(XdmfDataItem);
template <typename T> void setData(T * dataPointer);
template <typename T> void setValues(T * dataPointer);
template <typename T> T* getData();
template <typename T> T* getValues();
template <typename T> const T* const getData() const;
template <typename T> const T* const getValues() const;
int getNumberValues() const;
std::string printSelf() const;
void write(boost::shared_ptr<XdmfVisitor> visitor) const;
void write(boost::shared_ptr<XdmfVisitor> visitor) const = 0;
protected:
......@@ -42,7 +42,7 @@ private:
XdmfDataItem(const XdmfDataItem&); // Not implemented.
void operator=(const XdmfDataItem&); // Not implemented.
int* mDataPointer;
void* mDataPointer;
XdmfDataItemType mDataType;
};
......
......@@ -62,6 +62,8 @@ protected:
private:
static XdmfDataItemType* UnknownPtr;
std::string mName;
};
......
/**
* An XdmfItem replaces the XdmfElement class in the previous version of Xdmf. An XdmfElement represents and item that can be
* visited by an XdmfLightDataVisitor and have it's contents added to an XdmfFile. These include XdmfGrids, XdmfSets,
* An XdmfItem replaces the XdmfElement class in the previous version of Xdmf. An XdmfItem represents an item that can be
* visited by an XdmfVisitor and have it's contents added to an xdmf file. These include XdmfGrids, XdmfSets,
* XdmfTopologies, etc.
*
* This is an abstract base class.
......
......@@ -13,13 +13,17 @@
XdmfVisitor::XdmfVisitor() :
mTabIndex(0),
xmlData()
xmlData(),
mLightDataLimit(0),
mHeavyFileName("output.h5"),
hdf5Handle(H5Fcreate("output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT))
{
std::cout << "Created Visitor " << this << std::endl;
}
XdmfVisitor::~XdmfVisitor()
{
herr_t status = H5Fclose(hdf5Handle);
std::cout << "Deleted Visitor " << this << std::endl;
}
......@@ -27,25 +31,71 @@ void XdmfVisitor::visit(const XdmfAttribute * const attribute)
{
xmlData << std::setw(mTabIndex) << "" << "<Attribute Name=\"" << attribute->getName() << "\" AttributeType=\"" << attribute->getAttributeTypeAsString() << "\" Center=\"" << attribute->getAttributeCenterAsString() << "\">\n";
mTabIndex++;
dataHierarchy.push_back(attribute->getName());
visit((XdmfDataItem*)attribute);
dataHierarchy.pop_back();
mTabIndex--;
xmlData << std::setw(mTabIndex) << "" << "</Attribute>\n";
}
void XdmfVisitor::visit(const XdmfDataItem * const dataItem)
{
xmlData << std::setw(mTabIndex) << "" << "<DataItem Format=\"XML\" DataType=\"Int\" Precision=\"4\" Dimensions=\"" << dataItem->getNumberValues() << "\">";
const int* const intPointer = dataItem->getData<int>();
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>();
mTabIndex++;
for (unsigned int i=0; i<dataItem->getNumberValues(); ++i)
if(dataItem->getNumberValues() > mLightDataLimit)
{
if (i % 10 == 0)
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)
{
xmlData << "\n" << std::setw(mTabIndex) << "" << intPointer[i] << " ";
/* 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);
}
}
else
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)
{
xmlData << intPointer[i] << " ";
if (i % 10 == 0)
{
xmlData << "\n" << std::setw(mTabIndex) << "" << intPointer[i] << " ";
}
else
{
xmlData << intPointer[i] << " ";
}
}
}
mTabIndex--;
......@@ -68,7 +118,9 @@ void XdmfVisitor::visit(const XdmfGeometry * const geometry)
{
xmlData << std::setw(mTabIndex) << "" << "<Geometry GeometryType=\"" << geometry->getGeometryTypeAsString() << "\">\n";
mTabIndex++;
dataHierarchy.push_back("XYZ");
visit((XdmfDataItem*)geometry);
dataHierarchy.pop_back();
mTabIndex--;
xmlData << std::setw(mTabIndex) << "" << "</Geometry>\n";
}
......@@ -77,12 +129,14 @@ void XdmfVisitor::visit(const XdmfGrid * const grid)
{
xmlData << std::setw(mTabIndex) << "" << "<Grid Name=\"" << grid->getName() <<"\">\n";
mTabIndex++;
dataHierarchy.push_back(grid->getName());
visit(grid->getGeometry().get());
visit(grid->getTopology().get());
for(unsigned int i=0; i<grid->getNumberOfAttributes(); i++)
for(unsigned int i=0; i<grid->getNumberOfAttributes(); ++i)
{
visit(grid->getAttribute(i).get());
}
dataHierarchy.pop_back();
mTabIndex--;
xmlData << std::setw(mTabIndex) << "" << "</Grid>\n";
}
......@@ -91,7 +145,9 @@ void XdmfVisitor::visit(const XdmfTopology * const topology)
{
xmlData << std::setw(mTabIndex) << "" << "<Topology TopologyType=\"" << topology->getTopologyTypeAsString() << "\" NumberOfElements=\"" << topology->getNumberElements() << "\">\n";
mTabIndex++;
dataHierarchy.push_back("Connectivity");
visit((XdmfDataItem*)topology);
dataHierarchy.pop_back();
mTabIndex--;
xmlData << std::setw(mTabIndex) << "" << "</Topology>\n";
}
......@@ -100,3 +156,14 @@ std::string XdmfVisitor::printSelf() const
{
return "XdmfVisitor:\n" + xmlData.str();
}
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();
}
......@@ -13,7 +13,9 @@ class XdmfGrid;
class XdmfTopology;
// Includes
#include <hdf5.h>
#include <sstream>
#include <vector>
#include "XdmfObject.hpp"
class XdmfVisitor : public XdmfObject {
......@@ -24,7 +26,7 @@ public:
virtual void visit(const XdmfAttribute * const attribute);
virtual void visit(const XdmfDataItem * const geometry);
virtual void visit(const XdmfDataItem * const dataItem);
virtual void visit(const XdmfDomain * const domain);
......@@ -46,8 +48,15 @@ private:
XdmfVisitor(const XdmfVisitor&); // Not implemented.
void operator=(const XdmfVisitor&); // Not implemented.
std::string getHDF5GroupName();
int mTabIndex;
std::stringstream xmlData;
hid_t hdf5Handle;
std::vector<std::string> dataHierarchy;
int mLightDataLimit;
std::string mHeavyFileName;
};
#endif /* XDMFVISITOR_HPP_ */
......@@ -2,7 +2,7 @@
int main(int argc, char* argv[])
{
boost::shared_ptr<XdmfDataItem> dataItem = XdmfDataItem::New();
int intArray[10] = {1,2,3,4,5,6,7,8,9,10};
dataItem->setData(intArray);
//boost::shared_ptr<XdmfDataItem> dataItem = XdmfDataItem::New();
//int intArray[10] = {1,2,3,4,5,6,7,8,9,10};
//dataItem->setValues(intArray);
}
......@@ -18,13 +18,13 @@ int main(int argc, char* argv[])
0, 0, -1, 1, 0, -1, 3, 0, -2, 0, 1, -1, 1, 1, -1, 3, 2, -2};
grid->getGeometry()->setNumberPoints(12);
grid->getGeometry()->setGeometryType(XdmfGeometryType::XYZ());
grid->getGeometry()->setData(points);
grid->getGeometry()->setValues(points);
// Set Topology
int connectivity[] = {0, 1, 7, 6, 3, 4, 10, 9, 1, 2, 8, 7, 4, 5, 11, 10};
grid->getTopology()->setNumberElements(2);
grid->getTopology()->setTopologyType(XdmfTopologyType::Hexahedron());
grid->getTopology()->setData(connectivity);
grid->getTopology()->setValues(connectivity);
// Add Node Attribute
boost::shared_ptr<XdmfAttribute> nodalAttribute = XdmfAttribute::New();
......@@ -33,7 +33,7 @@ int main(int argc, char* argv[])
nodalAttribute->setNumberValues(12);
nodalAttribute->setAttributeType(XdmfAttributeType::Scalar());
nodalAttribute->setAttributeCenter(XdmfAttributeCenter::Node());
nodalAttribute->setData(nodeValues);
nodalAttribute->setValues(nodeValues);
// Add Cell Attribute
boost::shared_ptr<XdmfAttribute> cellAttribute = XdmfAttribute::New();
......@@ -42,7 +42,7 @@ int main(int argc, char* argv[])
cellAttribute->setNumberValues(2);
cellAttribute->setAttributeType(XdmfAttributeType::Scalar());
cellAttribute->setAttributeCenter(XdmfAttributeCenter::Cell());
cellAttribute->setData(cellValues);
cellAttribute->setValues(cellValues);
grid->insert(nodalAttribute);
grid->insert(cellAttribute);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment