Commit 5b153544 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add XdmfArrayType to store data type / precision information which should make interaction

between XdmfArray and XdmfHDF5Controller a bit more robust.
parent 9c5aaf4b
......@@ -34,6 +34,7 @@ endif(XDMF_WRAP_PYTHON)
set(XdmfSources
XdmfArray
XdmfArrayType
XdmfAttribute
XdmfAttributeCenter
XdmfAttributeType
......@@ -61,19 +62,19 @@ set(XdmfSources
add_library(Xdmf ${XdmfSources})
target_link_libraries(Xdmf ${HDF5_LIBRARIES} ${LIBXML2_LIBRARIES})
option(BUILD_DOCUMENTATION OFF)
if(BUILD_DOCUMENTATION)
option(XDMF_BUILD_DOCUMENTATION OFF)
if(XDMF_BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif(BUILD_DOCUMENTATION)
endif(XDMF_BUILD_DOCUMENTATION)
option(BUILD_TESTING OFF)
if(BUILD_TESTING)
option(XDMF_BUILD_TESTING OFF)
if(XDMF_BUILD_TESTING)
enable_testing()
include(CTest)
add_subdirectory(tests)
endif(BUILD_TESTING)
endif(XDMF_BUILD_TESTING)
option(BUILD_UTILS OFF)
if(BUILD_UTILS)
option(XDMF_BUILD_UTILS OFF)
if(XDMF_BUILD_UTILS)
add_subdirectory(utils)
endif(BUILD_UTILS)
endif(XDMF_BUILD_UTILS)
This diff is collapsed.
......@@ -2,6 +2,7 @@
#define XDMFARRAY_HPP_
// Forward Declarations
class XdmfArrayType;
class XdmfHDF5Controller;
// Includes
......@@ -112,13 +113,6 @@ public:
*/
boost::shared_ptr<const XdmfHDF5Controller> getHDF5Controller() const;
/**
* Get the precision, in bytes, of the data type of this array.
*
* @return the precision, in bytes, of the data type of this array.
*/
unsigned int getPrecision() const;
/**
* Get the number of values stored in this array.
*
......@@ -132,7 +126,7 @@ public:
* @return a string containing the Xdmf data type for the array, this is one of
* Char, Short, Int, Float, UChar, UShort, UInt.
*/
std::string getType() const;
boost::shared_ptr<const XdmfArrayType> getType() const;
/**
* Get a copy of a single value stored in this array.
......@@ -192,6 +186,11 @@ public:
template <typename T>
boost::shared_ptr<std::vector<T> > initialize();
/**
* Initializes the array to contain an empty container of a particular XdmfArrayType.
*/
void initialize(const boost::shared_ptr<const XdmfArrayType> arrayType);
/**
* Returns whether the array is initialized (contains values in memory).
*/
......@@ -319,7 +318,6 @@ private:
class Erase;
class GetCapacity;
class GetHDF5Type;
class GetPrecision;
class GetSize;
class GetType;
......@@ -341,8 +339,6 @@ private:
struct NullDeleter;
void initialize(const std::string & type, const unsigned int precision);
/**
* After setValues(const T * const array) is called, XdmfArray stores a pointer that is not allowed to be modified through
* the XdmfArray API. If the user desires to modify the contents of the pointer, they must do so without calling any
......
......@@ -2,7 +2,7 @@
#include "XdmfArray.hpp"
template <typename T>
class XdmfArray::CopyValues : public boost::static_visitor <void> {
class XdmfArray::CopyValues : public boost::static_visitor<void> {
public:
CopyValues(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride) :
......@@ -42,7 +42,7 @@ private:
};
template <typename T>
class XdmfArray::GetValuesCopy : public boost::static_visitor <void> {
class XdmfArray::GetValuesCopy : public boost::static_visitor<void> {
public:
GetValuesCopy(const unsigned int startIndex, T * valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride) :
......@@ -82,7 +82,7 @@ private:
};
template <typename T>
class XdmfArray::PushBack : public boost::static_visitor <void> {
class XdmfArray::PushBack : public boost::static_visitor<void> {
public:
PushBack(const T & val) :
......@@ -102,7 +102,7 @@ private:
};
template <typename T>
class XdmfArray::Resize : public boost::static_visitor <void> {
class XdmfArray::Resize : public boost::static_visitor<void> {
public:
Resize(const unsigned int numValues, const T & val) :
......
/*
* XdmfArrayType.cpp
*
* Created on: Jan 29, 2010
* Author: kleiter
*/
#include <sstream>
#include "XdmfArrayType.hpp"
// Supported XdmfArrayTypes
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Uninitialized()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("None", 0));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int8()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Char", 1));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int16()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Short", 2));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int32()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Int", 4));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Int64()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Int", 8));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Float32()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Float", 4));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::Float64()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("Float", 8));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::UInt8()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UChar", 1));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::UInt16()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UShort", 2));
return p;
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::UInt32()
{
static boost::shared_ptr<const XdmfArrayType> p(new XdmfArrayType("UInt", 4));
return p;
}
XdmfArrayType::XdmfArrayType(const std::string & name, const unsigned int precision) :
mName(name),
mPrecision(precision)
{
}
XdmfArrayType::~XdmfArrayType()
{
}
boost::shared_ptr<const XdmfArrayType> XdmfArrayType::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator type = itemProperties.find("DataType");
std::map<std::string, std::string>::const_iterator precision = itemProperties.find("Precision");
if(type != itemProperties.end() && precision != itemProperties.end())
{
const std::string typeVal = type->second;
const unsigned int precisionVal = atoi(precision->second.c_str());
if(typeVal.compare("None") == 0)
{
return Uninitialized();
}
else if(typeVal.compare("Char") == 0)
{
return Int8();
}
else if(typeVal.compare("Short") == 0)
{
return Int16();
}
else if(typeVal.compare("Int") == 0 && precisionVal == 4)
{
return Int32();
}
else if(typeVal.compare("Int") == 0 && precisionVal == 8)
{
return Int64();
}
else if(typeVal.compare("Float") == 0 && precisionVal == 4)
{
return Float32();
}
else if(typeVal.compare("Float") == 0 && precisionVal == 8)
{
return Float64();
}
else if(typeVal.compare("UChar") == 0)
{
return UInt8();
}
else if(typeVal.compare("UShort") == 0)
{
return UInt16();
}
else if(typeVal.compare("UInt") == 0)
{
return UInt32();
}
else
{
assert(false);
}
}
assert(false);
}
bool XdmfArrayType::operator==(const XdmfArrayType & arrayType) const
{
return mName.compare(arrayType.mName) == 0 && mPrecision == arrayType.mPrecision;
}
bool XdmfArrayType::operator!=(const XdmfArrayType & arrayType) const
{
return !this->operator==(arrayType);
}
void XdmfArrayType::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
collectedProperties["DataType"] = mName;
std::stringstream precision;
precision << mPrecision;
collectedProperties["Precision"] = precision.str();
}
#ifndef XDMFARRAYTYPE_HPP_
#define XDMFARRAYTYPE_HPP_
// Includes
#include "XdmfItemProperty.hpp"
/**
* @brief Property describing what types of values an XdmfArray contains.
*
* XdmfArrayType is a property used by XdmfArray to specify the types of values stored in an
* XdmfArray. A specific XdmfArrayType can be created by calling one of the static methods
* in the class, i.e. XdmfArrayType::Int32().
*
* Xdmf supports the following attribute types:
* Uninitialized
* Int8
* Int16
* Int32
* Int64
* Float32
* Float64
* UInt8
* UInt16
* UInt32
*/
class XdmfArrayType : public XdmfItemProperty {
public:
friend class XdmfArray;
template <typename T> friend void boost::checked_delete(T * x);
// Supported XdmfArrayTypes
static boost::shared_ptr<const XdmfArrayType> Uninitialized();
static boost::shared_ptr<const XdmfArrayType> Int8();
static boost::shared_ptr<const XdmfArrayType> Int16();
static boost::shared_ptr<const XdmfArrayType> Int32();
static boost::shared_ptr<const XdmfArrayType> Int64();
static boost::shared_ptr<const XdmfArrayType> Float32();
static boost::shared_ptr<const XdmfArrayType> Float64();
static boost::shared_ptr<const XdmfArrayType> UInt8();
static boost::shared_ptr<const XdmfArrayType> UInt16();
static boost::shared_ptr<const XdmfArrayType> UInt32();
void getProperties(std::map<std::string, std::string> & collectedProperties) const;
/*
* Compare two XdmfArrayTypes for equality.
*
* @param arrayType an XdmfArrayType to compare equality to.
* @return true if the XdmfArrayTypes are equal.
*/
bool operator==(const XdmfArrayType & arrayType) const;
/**
* Compare two XdmfArrayTypes for inequality.
*
* @param arrayType an XdmfArrayType to compare inequality to.
* @return true if the XdmfArrayTypes are not equal.
*/
bool operator!=(const XdmfArrayType & arrayType) const;
protected:
/**
* Protected constructor for XdmfArrayType. The constructor is protected because all array types supported
* by Xdmf should be accessed through more specific static methods that construct XdmfArrayTypes - i.e.
* XdmfArrayType::Float64().
*
* @param name the name of the XdmfArrayType to construct.
* @param precision the precision, in bytes, of the XdmfArrayType to construct.
*/
XdmfArrayType(const std::string & name, const unsigned int precision);
~XdmfArrayType();
private:
XdmfArrayType(const XdmfArrayType & arrayType); // Not implemented.
void operator=(const XdmfArrayType & arrayType); // Not implemented.
static boost::shared_ptr<const XdmfArrayType> New(const std::map<std::string, std::string> & itemProperties);
unsigned int mPrecision;
std::string mName;
};
#endif /* XDMFARRAYTYPE_HPP_ */
......@@ -135,5 +135,5 @@ unsigned int XdmfGeometryType::getDimensions() const
void XdmfGeometryType::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
collectedProperties["GeometryType"] = this->mName;
collectedProperties["GeometryType"] = mName;
}
......@@ -4,10 +4,10 @@
#include <hdf5.h>
#include <sstream>
#include "XdmfArray.hpp"
#include "XdmfArrayType.hpp"
#include "XdmfHDF5Controller.hpp"
XdmfHDF5Controller::XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int precision, const unsigned int size, const std::string & type) :
mPrecision(precision),
XdmfHDF5Controller::XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type) :
mSize(size),
mType(type)
{
......@@ -51,17 +51,12 @@ std::string XdmfHDF5Controller::getHDF5FilePath() const
return mHDF5FilePath;
}
unsigned int XdmfHDF5Controller::getPrecision() const
{
return mPrecision;
}
unsigned int XdmfHDF5Controller::getSize() const
{
return mSize;
}
std::string XdmfHDF5Controller::getType() const
boost::shared_ptr<const XdmfArrayType> XdmfHDF5Controller::getType() const
{
return mType;
}
......
......@@ -3,6 +3,7 @@
// Forward Declarations
class XdmfArray;
class XdmfArrayType;
// Includes
#include "XdmfObject.hpp"
......@@ -23,9 +24,9 @@ public:
/**
* Create a new controller for an hdf5 data set on disk.
*/
static boost::shared_ptr<XdmfHDF5Controller> New(const std::string & dataSetPath, const unsigned int precision, const unsigned int size, const std::string & type)
static boost::shared_ptr<XdmfHDF5Controller> New(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type)
{
boost::shared_ptr<XdmfHDF5Controller> p(new XdmfHDF5Controller(dataSetPath, precision, size, type));
boost::shared_ptr<XdmfHDF5Controller> p(new XdmfHDF5Controller(dataSetPath, size, type));
return p;
}
......@@ -50,13 +51,6 @@ public:
*/
std::string getHDF5FilePath() const;
/**
* Get the precision of the hdf5 data set owned by this controller.
*
* @return a int containing the precision of the hdf5 data set.
*/
unsigned int getPrecision() const;
/**
* Get the size of the hdf5 data set owned by this controller.
*
......@@ -65,11 +59,11 @@ public:
unsigned int getSize() const;
/**
* Get the data type of the hdf5 data set owned by this controller.
* Get the array type of the hdf5 data set owned by this controller.
*
* @return a std::string containing the name of the data type of the hdf5 data set.
* @return an XdmfArrayType containing the array type of the hdf5 data set.
*/
std::string getType() const;
boost::shared_ptr<const XdmfArrayType> getType() const;
/**
* Read data owned by this controller on disk into the passed XdmfArray.
......@@ -80,7 +74,7 @@ public:
protected:
XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int precision, const unsigned int size, const std::string & type);
XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type);
virtual ~XdmfHDF5Controller();
private:
......@@ -90,9 +84,8 @@ private:
std::string mDataSetName;
std::string mHDF5FilePath;
unsigned int mPrecision;
unsigned int mSize;
std::string mType;
boost::shared_ptr<const XdmfArrayType> mType;
};
#endif /* XDMFHDF5CONTROLLER_HPP_ */
......@@ -233,8 +233,8 @@ void XdmfHDF5Writer::visit(XdmfArray & array, const boost::shared_ptr<XdmfBaseVi
// Attach a new controller to the array if needed.
if(mImpl->mMode == Default || !array.mHDF5Controller)
{
boost::shared_ptr<XdmfHDF5Controller> newDataSetController = XdmfHDF5Controller::New(writtenDataSet.str(), array.getPrecision(),
array.getSize(), array.getType());
boost::shared_ptr<XdmfHDF5Controller> newDataSetController = XdmfHDF5Controller::New(writtenDataSet.str(),
array.getSize(), array.getType());
array.setHDF5Controller(newDataSetController);
mImpl->mDataSetId++;
}
......
......@@ -6,6 +6,7 @@
*/
#include <map>
#include <sstream>
#include "XdmfTopologyType.hpp"
// Supported XdmfTopologyTypes
......@@ -334,9 +335,11 @@ unsigned int XdmfTopologyType::getNodesPerElement() const
void XdmfTopologyType::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
collectedProperties["TopologyType"] = this->mName;
collectedProperties["TopologyType"] = mName;
if(mName.compare("Polygon") == 0)
{
collectedProperties["NodesPerElement"] = this->mNodesPerElement;
std::stringstream nodesPerElement;
nodesPerElement << mNodesPerElement;
collectedProperties["NodesPerElement"] = nodesPerElement.str();
}
}
#include "XdmfArray.hpp"
#include "XdmfArrayType.hpp"
int main(int argc, char* argv[])
{
......@@ -14,14 +15,12 @@ int main(int argc, char* argv[])
*/
boost::shared_ptr<XdmfArray> array = XdmfArray::New();
assert(array->getSize() == 0);
assert(array->getType() == "None");
assert(array->getPrecision() == 0);
assert(array->getType() == XdmfArrayType::Uninitialized());
assert(array->getValuesString() == "");
assert(array->getValuesPointer() == NULL);
array->copyValues(0, &values[0], 4, 1, 1);
assert(array->getSize() == 4);
assert(array->getType().compare("Int") == 0);
assert(array->getPrecision() == 4);
assert(array->getType() == XdmfArrayType::Int32());
assert(array->getValuesString().compare("1 2 3 4 ") == 0);
const int * const arrayPointer = (const int * const)array->getValuesPointer();
assert(arrayPointer[0] == 1);
......@@ -48,8 +47,7 @@ int main(int argc, char* argv[])
boost::shared_ptr<XdmfArray> array2 = XdmfArray::New();
array2->copyValues(0, &values[0], 2, 2, 1);
assert(array2->getSize() == 3);
assert(array2->getType().compare("Int") == 0);
assert(array2->getPrecision() == 4);
assert(array2->getType() == XdmfArrayType::Int32());
assert(array2->getValuesString().compare("1 0 2 ") == 0);
storedValues = array2->getValues<int>();
assert((*storedValues)[0] == 1);
......@@ -62,8 +60,7 @@ int main(int argc, char* argv[])
boost::shared_ptr<XdmfArray> array3 = XdmfArray::New();
array3->copyValues(0, &values[0], 2, 1, 2);
assert(array3->getSize() == 2);
assert(array3->getType().compare("Int") == 0);
assert(array3->getPrecision() == 4);
assert(array3->getType() == XdmfArrayType::Int32());
assert(array3->getValuesString().compare("1 3 ") == 0);
storedValues = array3->getValues<int>();
assert((*storedValues)[0] == 1);
......@@ -75,8 +72,6 @@ int main(int argc, char* argv[])
boost::shared_ptr<XdmfArray> array4 = XdmfArray::New();
array4->copyValues(0, &values[0], 2, 2, 2);
assert(array4->getSize() == 3);
assert(array4->getType().compare("Int") == 0);
assert(array4->getPrecision() == 4);
assert(array4->getValuesString().compare("1 0 3 ") == 0);
storedValues = array4->getValues<int>();
assert((*storedValues)[0] == 1);
......@@ -90,8 +85,6 @@ int main(int argc, char* argv[])
boost::shared_ptr<XdmfArray> array5 = XdmfArray::New();
array5->copyValues(0, array, 1, 3);
assert(array5->getSize() == 3);
assert(array5->getType().compare("Int") == 0);
assert(array5->getPrecision() == 4);
storedValues = array5->getValues<int>();
assert(array5->getSize() == 3);
assert(array->getSize() == 4);
......@@ -113,8 +106,7 @@ int main(int argc, char* argv[])
*/
array5->setValues(values, 2, 0);
assert(array5->getSize() == 2);
assert(array5->getType().compare("Int") == 0);
assert(array5->getPrecision() == 4);
assert(array5->getType() == XdmfArrayType::Int32());
assert(array5->getValuesString().compare("1 2 ") == 0);
const int * const array5Pointer = (const int * const)array5->getValuesPointer();
assert(array5Pointer[0] == 1);
......@@ -143,8 +135,7 @@ int main(int argc, char* argv[])
doubleValues[2] = 10.1;
array5->setValues(doubleValues, 3, 1);
assert(array5->getSize() == 3);
assert(array5->getType().compare("Float") == 0);
assert(array5->getPrecision() == 8);
assert(array5->getType() == XdmfArrayType::Float64());
assert(array5->getValuesString().compare("0 1.1 10.1 ") == 0);
//
......@@ -163,8 +154,7 @@ int main(int argc, char* argv[])
boost::shared_ptr<XdmfArray> array6 = XdmfArray::New();
array6->setValues(values2);
assert(array6->getSize() == 5);
assert(array6->getType().compare("Char") == 0);
assert(array6->getPrecision() == 1);
assert(array6->getType() == XdmfArrayType::Int8());
assert(array6->getValuesString().compare("-2 -1 0 1 2 ") == 0);
// Assert we have the same values!
boost::shared_ptr<std::vector<char> > storedValues2 = array6->getValues<char>();
......@@ -202,8 +192,7 @@ int main(int argc, char* argv[])
array7->swap(values3);
assert(values3.size() == 0);
assert(array7->getSize() == 3);
assert(array7->getType().compare("Short") == 0);
assert(array7->getPrecision() == 2);
assert(array7->getType() == XdmfArrayType::Int16());
boost::shared_ptr<std::vector<short> > storedValues3 = array7->getValues<short>();
assert((*storedValues3)[0] == -1);
assert((*storedValues3)[1] == 0);
......@@ -216,8 +205,7 @@ int main(int argc, char* argv[])
array7->swap(values2);
assert(storedValues2->size() == 0);
assert(array7->getSize() == 6);
assert(array7->getType().compare("Char") == 0);
assert(array7->getPrecision() == 1);
assert(array7->getType() == XdmfArrayType::Int8());
/**
* Swap values from an XdmfArray (with copy)
......
option(BUILD_PARTITIONER OFF)
option(XDMF_BUILD_PARTITIONER OFF)
set(XdmfUtilsExectutables)
set(XdmfUtilsSources)
set(XdmfUtilsLinkLibraries Xdmf)
if(BUILD_PARTITIONER)
if(XDMF_BUILD_PARTITIONER)
find_package(Metis REQUIRED)
if(METIS_FOUND)
include_directories(${METIS_INCLUDE_DIR})
......@@ -12,7 +12,7 @@ if(BUILD_PARTITIONER)
set(XdmfUtilsExectutables ${XdmfUtilsExectutables} XdmfPartitioner)
set(XdmfUtilsSources ${XdmfUtilsSources} XdmfPartitioner)
set(XdmfUtilsLinkLibraries ${XdmfUtilsLinkLibraries} ${METIS_LIBRARIES})
endif(BUILD_PARTITIONER)
endif(XDMF_BUILD_PARTITIONER)
add_library(XdmfUtils ${XdmfUtilsSources})
target_link_libraries(XdmfUtils ${XdmfUtilsLinkLibraries})
......@@ -21,4 +21,4 @@ foreach(executable ${XdmfUtilsExectutables})
add_executable(${executable} ${executable})
set_target_properties(${executable} PROPERTIES COMPILE_FLAGS -DBUILD_EXE)
target_link_libraries(${executable} XdmfUtils)
endforeach(executable ${XdmfUtilsExectutables})
\ No newline at end of file
endforeach(executable ${XdmfUtilsExectutables})
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