Commit b94acdde authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add ability to read XML files from disk into Xdmf structures in memory.

parent 5e8d6200
......@@ -47,6 +47,7 @@ set(XdmfSources
XdmfItem
XdmfItemProperty
XdmfObject
XdmfReader
XdmfTopology
XdmfTopologyType
XdmfVisitor
......
......@@ -14,11 +14,12 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
#include <XdmfGeometry.hpp>
#include <XdmfGeometryType.hpp>
#include <XdmfGrid.hpp>
#include <XdmfHDF5Controller.hpp>
#include <XdmfHDF5Controller.hpp>
#include <XdmfHDF5Writer.hpp>
#include <XdmfItem.hpp>
#include <XdmfItemProperty.hpp>
#include <XdmfObject.hpp>
#include <XdmfReader.hpp>
#include <XdmfTopology.hpp>
#include <XdmfTopologyType.hpp>
#include <XdmfVisitor.hpp>
......@@ -58,6 +59,7 @@ namespace boost {
%template(XdmfHDF5WriterPtr) boost::shared_ptr<XdmfHDF5Writer>;
%template(XdmfItemPtr) boost::shared_ptr<XdmfItem>;
%template(XdmfObjPtr) boost::shared_ptr<XdmfObject>;
%template(XdmfReaderPtr) boost::shared_ptr<XdmfReader>;
%template(XdmfTopologyPtr) boost::shared_ptr<XdmfTopology>;
%template(XdmfVisitorPtr) boost::shared_ptr<XdmfVisitor>;
%template(XdmfWriterPtr) boost::shared_ptr<XdmfWriter>;
......@@ -91,6 +93,7 @@ SWIG_SHARED_PTR_DERIVED(XdmfHDF5Writer, XdmfVisitor);
SWIG_SHARED_PTR_DERIVED(XdmfHDF5Writer, Loki::BaseVisitor);
SWIG_SHARED_PTR_DERIVED(XdmfHDF5Writer, XdmfObject);
SWIG_SHARED_PTR_DERIVED(XdmfItem, XdmfObject);
SWIG_SHARED_PTR_DERIVED(XdmfReader, XdmfObject);
SWIG_SHARED_PTR_DERIVED(XdmfTopology, XdmfDataItem);
SWIG_SHARED_PTR_DERIVED(XdmfTopology, XdmfItem);
SWIG_SHARED_PTR_DERIVED(XdmfTopology, XdmfObject);
......@@ -103,15 +106,15 @@ SWIG_SHARED_PTR_DERIVED(XdmfWriter, XdmfObject);
%include XdmfObject.hpp
%include XdmfItem.hpp
%include XdmfDataItem.hpp
%include XdmfItemProperty.hpp
%include XdmfVisitor.hpp
%include XdmfHDF5Controller.hpp
%include XdmfHDF5Writer.hpp
%include XdmfReader.hpp
%include XdmfWriter.hpp
%include XdmfDataItem.hpp
%include XdmfAttribute.hpp
%include XdmfAttributeCenter.hpp
%include XdmfAttributeType.hpp
......
......@@ -5,10 +5,12 @@
* Author: kleiter
*/
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
#include <sstream>
#include "XdmfArray.hpp"
#include "XdmfHDF5Controller.hpp"
#include "XdmfVisitor.hpp"
#include <sstream>
class XdmfArray::Clear : public boost::static_visitor <void> {
public:
......@@ -363,6 +365,8 @@ XdmfArray::~XdmfArray()
std::cout << "Deleted Array " << this << std::endl;
}
std::string XdmfArray::ItemTag = "DataItem";
void XdmfArray::copyValues(const unsigned int startIndex, const boost::shared_ptr<const XdmfArray> values, const unsigned int valuesStartIndex, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride)
{
if(mHaveArrayPointer)
......@@ -410,7 +414,14 @@ boost::shared_ptr<const XdmfHDF5Controller> XdmfArray::getHDF5Controller() const
std::map<std::string, std::string> XdmfArray::getItemProperties() const
{
std::map<std::string, std::string> arrayProperties;
arrayProperties["Format"] = "HDF";
if(mHDF5Controller)
{
arrayProperties["Format"] = "HDF";
}
else
{
arrayProperties["Format"] = "XML";
}
arrayProperties["DataType"] = this->getType();
std::stringstream precision;
precision << this->getPrecision();
......@@ -423,7 +434,7 @@ std::map<std::string, std::string> XdmfArray::getItemProperties() const
std::string XdmfArray::getItemTag() const
{
return "DataItem";
return ItemTag;
}
unsigned int XdmfArray::getPrecision() const
......@@ -503,6 +514,50 @@ std::string XdmfArray::getValuesString() const
return "";
}
void XdmfArray::initialize(const std::string & type, const unsigned int precision)
{
if(type.compare("Char") == 0 && precision == 1)
{
this->initialize<char>();
}
else if(type.compare("Short") == 0 && precision == 2)
{
this->initialize<short>();
}
else if(type.compare("Int") == 0 && precision == 4)
{
this->initialize<int>();
}
else if(type.compare("Long") == 0 && precision == 8)
{
this->initialize<long>();
}
else if(type.compare("Float") == 0 && precision == 4)
{
this->initialize<float>();
}
else if(type.compare("Double") == 0 && precision == 8)
{
this->initialize<double>();
}
else if(type.compare("UChar") == 0 && precision == 8)
{
this->initialize<unsigned char>();
}
else if(type.compare("UShort") == 0 && precision == 8)
{
this->initialize<unsigned short>();
}
else if(type.compare("UInt") == 0 && precision == 8)
{
this->initialize<unsigned int>();
}
else
{
assert(false);
}
}
void XdmfArray::internalizeArrayPointer()
{
if(mHaveArrayPointer)
......@@ -511,6 +566,79 @@ void XdmfArray::internalizeArrayPointer()
}
}
void XdmfArray::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems)
{
std::string contentVal;
int precisionVal;
int sizeVal;
std::string typeVal;
std::map<std::string, std::string>::const_iterator content = itemProperties.find("Content");
if(content != itemProperties.end())
{
contentVal = content->second;
}
else
{
assert(false);
}
std::map<std::string, std::string>::const_iterator precision = itemProperties.find("Precision");
if(precision != itemProperties.end())
{
precisionVal = atoi(precision->second.c_str());
}
else
{
assert(false);
}
std::map<std::string, std::string>::const_iterator size = itemProperties.find("Dimensions");
if(size != itemProperties.end())
{
sizeVal = atoi(size->second.c_str());
}
else
{
assert(false);
}
std::map<std::string, std::string>::const_iterator type = itemProperties.find("DataType");
if(type != itemProperties.end())
{
typeVal = type->second;
}
else
{
assert(false);
}
std::map<std::string, std::string>::const_iterator format = itemProperties.find("Format");
if(format != itemProperties.end())
{
if(format->second.compare("HDF") == 0)
{
mHDF5Controller = XdmfHDF5Controller::New(contentVal, precisionVal, sizeVal, typeVal);
}
else if(format->second.compare("XML") == 0)
{
this->initialize(typeVal, precisionVal);
this->reserve(sizeVal);
boost::char_separator<char> sep(" ");
boost::tokenizer<boost::char_separator<char> > tokens(contentVal, sep);
BOOST_FOREACH(std::string t, tokens)
{
double val = atof(t.c_str());
this->pushBack(val);
}
}
else
{
assert(false);
}
}
else
{
assert(false);
}
}
void XdmfArray::read()
{
if(mHDF5Controller)
......
......@@ -51,6 +51,7 @@ public:
XdmfNewMacro(XdmfArray);
LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem)
friend class XdmfHDF5Writer;
static std::string ItemTag;
/**
* Copy values from an XdmfArray into this array.
......@@ -178,13 +179,19 @@ public:
template <typename T>
boost::shared_ptr<std::vector<T> > initialize();
/**
* Copy a value to the back of this array
*/
template <typename T>
void pushBack(T & value);
/**
* Read data from disk into memory.
*/
void read();
/**
* Release all data held by this XdmfArray.
* Release all data from currently held in memory.
*/
void release();
......@@ -200,10 +207,10 @@ public:
* to val. If numValues is less than the current size, values at indices larger than numValues are removed.
*
* @param numValues the number of values to resize this array to.
* @param val the number to initialize newly created values to, if needed.
* @param value the number to initialize newly created values to, if needed.
*/
template<typename T>
void resize(const unsigned int numValues, const T & val = 0);
void resize(const unsigned int numValues, const T & value = 0);
/**
* Attach an hdf5 controller to this XdmfArray.
......@@ -277,6 +284,7 @@ protected:
XdmfArray();
virtual ~XdmfArray();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......@@ -287,7 +295,7 @@ private:
class Clear;
class CopyArrayValues;
template<typename T>
template <typename T>
class CopyValues;
class GetCapacity;
......@@ -296,20 +304,26 @@ private:
class GetSize;
class GetType;
template<typename T>
template <typename T>
class GetValuesCopy;
class GetValuesPointer;
class GetValuesString;
class InternalizeArrayPointer;
class NewArray;
template <typename T>
class PushBack;
class Reserve;
template<typename T>
template <typename T>
class Resize;
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
......
// Includes
#include "XdmfArray.hpp"
template<typename T>
template <typename T>
class XdmfArray::CopyValues : public boost::static_visitor <void> {
public:
......@@ -41,7 +41,7 @@ private:
const unsigned int mValuesStride;
};
template<typename T>
template <typename T>
class XdmfArray::GetValuesCopy : public boost::static_visitor <void> {
public:
......@@ -81,7 +81,27 @@ private:
const unsigned int mValuesStride;
};
template<typename T>
template <typename T>
class XdmfArray::PushBack : public boost::static_visitor <void> {
public:
PushBack(const T & val) :
mVal(val)
{
}
template<typename U>
void operator()(boost::shared_ptr<std::vector<U> > & array) const
{
array->push_back((U)mVal);
}
private:
const T & mVal;
};
template <typename T>
class XdmfArray::Resize : public boost::static_visitor <void> {
public:
......@@ -110,7 +130,7 @@ struct XdmfArray::NullDeleter
}
};
template<typename T>
template <typename T>
void XdmfArray::copyValues(const unsigned int startIndex, const T * const valuesPointer, const unsigned int numValues, const unsigned int arrayStride, const unsigned int valuesStride)
{
if(mHaveArrayPointer)
......@@ -121,7 +141,7 @@ void XdmfArray::copyValues(const unsigned int startIndex, const T * const values
{
initialize<T>();
}
boost::apply_visitor( CopyValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
boost::apply_visitor(CopyValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
}
template <typename T>
......@@ -167,11 +187,7 @@ void XdmfArray::getValuesCopy(const unsigned int startIndex, T * valuesPointer,
{
boost::apply_visitor(GetValuesCopy<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArrayPointer);
}
else if(mHDF5Controller)
{
assert("Need to complete");
}
assert("Throw Exception No Data");
assert(false);
}
template <typename T>
......@@ -193,8 +209,18 @@ boost::shared_ptr<std::vector<T> > XdmfArray::initialize()
return newArray;
}
template <typename T>
void XdmfArray::pushBack(T & value)
{
if(mHaveArrayPointer)
{
internalizeArrayPointer();
}
return boost::apply_visitor(PushBack<T>(value), mArray);
}
template<typename T>
void XdmfArray::resize(const unsigned int numValues, const T & val)
void XdmfArray::resize(const unsigned int numValues, const T & value)
{
if(mHaveArrayPointer)
{
......@@ -204,10 +230,10 @@ void XdmfArray::resize(const unsigned int numValues, const T & val)
{
initialize<T>();
}
return boost::apply_visitor(Resize<T>(numValues, val), mArray);
return boost::apply_visitor(Resize<T>(numValues, value), mArray);
}
template<typename T>
template <typename T>
void XdmfArray::setValues(const T * const arrayPointer, const unsigned int numValues, const bool transferOwnership)
{
// Remove contents of internal array.
......@@ -229,7 +255,7 @@ void XdmfArray::setValues(const T * const arrayPointer, const unsigned int numVa
mArrayPointerNumValues = numValues;
}
template<typename T>
template <typename T>
void XdmfArray::setValues(std::vector<T> & array, const bool transferOwnership)
{
if(mHaveArrayPointer)
......@@ -249,7 +275,7 @@ void XdmfArray::setValues(std::vector<T> & array, const bool transferOwnership)
mHaveArray = true;
}
template<typename T>
template <typename T>
void XdmfArray::setValues(boost::shared_ptr<std::vector<T> > array)
{
if(mHaveArrayPointer)
......@@ -260,7 +286,7 @@ void XdmfArray::setValues(boost::shared_ptr<std::vector<T> > array)
mHaveArray = true;
}
template<typename T>
template <typename T>
bool XdmfArray::swap(std::vector<T> & array)
{
if(mHaveArrayPointer)
......@@ -283,7 +309,7 @@ bool XdmfArray::swap(std::vector<T> & array)
}
}
template<typename T>
template <typename T>
bool XdmfArray::swap(boost::shared_ptr<std::vector<T> > array)
{
return this->swap(*array.get());
......
......@@ -20,6 +20,8 @@ XdmfAttribute::~XdmfAttribute()
std::cout << "Deleted Attribute " << this << std::endl;
}
std::string XdmfAttribute::ItemTag = "Attribute";
XdmfAttributeCenter XdmfAttribute::getAttributeCenter() const
{
return mAttributeCenter;
......@@ -41,7 +43,7 @@ std::map<std::string, std::string> XdmfAttribute::getItemProperties() const
std::string XdmfAttribute::getItemTag() const
{
return "Attribute";
return ItemTag;
}
std::string XdmfAttribute::getName() const
......@@ -49,6 +51,23 @@ std::string XdmfAttribute::getName() const
return mName;
}
void XdmfAttribute::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems)
{
std::map<std::string, std::string>::const_iterator name = itemProperties.find("Name");
if(name != itemProperties.end())
{
mName = name->second;
}
else
{
assert(false);
}
mAttributeCenter = XdmfAttributeCenter::New(itemProperties);
mAttributeType = XdmfAttributeType::New(itemProperties);
XdmfDataItem::populateItem(itemProperties, childItems);
}
void XdmfAttribute::setAttributeCenter(const XdmfAttributeCenter & attributeCenter)
{
mAttributeCenter = attributeCenter;
......
......@@ -18,6 +18,7 @@ public:
XdmfNewMacro(XdmfAttribute);
LOKI_DEFINE_VISITABLE(XdmfAttribute, XdmfDataItem)
static std::string ItemTag;
/**
* Get the XdmfAttributeCenter associated with this attribute.
......@@ -69,6 +70,7 @@ protected:
XdmfAttribute();
virtual ~XdmfAttribute();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -35,13 +35,48 @@ XdmfAttributeCenter XdmfAttributeCenter::Node()
XdmfAttributeCenter::XdmfAttributeCenter(const std::string & name) :
mName(name)
{};
{
}
XdmfAttributeCenter::XdmfAttributeCenter(const XdmfAttributeCenter & attributeCenter):
mName(attributeCenter.mName)
{
}
XdmfAttributeCenter XdmfAttributeCenter::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator center = itemProperties.find("Center");
if(center != itemProperties.end())
{
const std::string centerVal = center->second;
if(centerVal.compare("Grid") == 0)
{
return Grid();
}
else if(centerVal.compare("Cell") == 0)
{
return Cell();
}
else if(centerVal.compare("Face") == 0)
{
return Face();
}
else if(centerVal.compare("Edge") == 0)
{
return Edge();
}
else if(centerVal.compare("Node") == 0)
{
return Node();
}
else
{
assert(false);
}
}
assert(false);
}
XdmfAttributeCenter& XdmfAttributeCenter::operator=(const XdmfAttributeCenter & attributeCenter)
{
if(this != &attributeCenter)
......@@ -68,5 +103,5 @@ std::string XdmfAttributeCenter::getName() const
void XdmfAttributeCenter::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
collectedProperties["Center"] = this->mName;
collectedProperties["Center"] = mName;
}
......@@ -23,6 +23,8 @@ class XdmfAttributeCenter : public XdmfItemProperty {
public:
friend class XdmfAttribute;
// Supported Xdmf Attribute Centers
static XdmfAttributeCenter Grid();
static XdmfAttributeCenter Cell();
......@@ -71,6 +73,8 @@ protected:
private:
static XdmfAttributeCenter New(const std::map<std::string, std::string> & itemProperties);
std::string mName;
};
......
......@@ -45,13 +45,56 @@ XdmfAttributeType XdmfAttributeType::GlobalId()
XdmfAttributeType::XdmfAttributeType(const std::string & name) :
mName(name)
{};
{
}
XdmfAttributeType::XdmfAttributeType(const XdmfAttributeType & attributeType):
mName(attributeType.mName)
{
}
XdmfAttributeType XdmfAttributeType::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator type = itemProperties.find("AttributeType");
if(type != itemProperties.end())
{
const std::string typeVal = type->second;
if(typeVal.compare("None") == 0)
{
return NoAttributeType();
}
else if(typeVal.compare("Scalar") == 0)
{
return Scalar();
}
else if(typeVal.compare("Vector") == 0)
{
return Vector();
}
else if(typeVal.compare("Tensor") == 0)
{
return Tensor();
}
else if(typeVal.compare("Matrix") == 0)
{
return Matrix();
}
else if(typeVal.compare("Tensor6") == 0)
{
return Tensor6();
}
else if(typeVal.compare("GlobalId") == 0)
{
return GlobalId();
}
else
{
assert(false);
}
}
assert(false);
}
XdmfAttributeType& XdmfAttributeType::operator=(const XdmfAttributeType & attributeType)
{
if(this != &attributeType)
......@@ -78,5 +121,5 @@ std::string XdmfAttributeType::getName() const
void XdmfAttributeType::getProperties(std::map<std::string, std::string> & collectedProperties) const
{
collectedProperties["AttributeType"] = this->mName;
collectedProperties["AttributeType"] = mName;
}
......@@ -25,6 +25,8 @@ class XdmfAttributeType : public XdmfItemProperty {
public:
friend class XdmfAttribute;
// Supported Xdmf Attribute Types
static XdmfAttributeType NoAttributeType();
static XdmfAttributeType Scalar();
......@@ -75,6 +77,8 @@ protected:
private:
static XdmfAttributeType New(const std::map<std::string, std::string> & itemProperties);
std::string mName;
};
......
......@@ -29,6 +29,17 @@ boost::shared_ptr<const XdmfArray> XdmfDataItem::getArray() const
return mArray;
}
void XdmfDataItem::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems)
{
for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter)
{
if(boost::shared_ptr<XdmfArray> array = boost::shared_dynamic_cast<XdmfArray>(*iter))
{
this->setArray(array);
}
}
}
void XdmfDataItem::setArray(boost::shared_ptr<XdmfArray> array)
{
mArray = array;
......
......@@ -46,6 +46,7 @@ protected:
XdmfDataItem();
virtual ~XdmfDataItem();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem > > & childItems);
private:
......
......@@ -18,6 +18,8 @@ XdmfDomain::~XdmfDomain()
std::cout << "Deleted Domain " << this << std::endl;
}
std::string XdmfDomain::ItemTag = "Domain";
void XdmfDomain::insert(boost::shared_ptr<XdmfGrid> grid)
{
mGrids.push_back(grid);
......@@ -51,7 +53,7 @@ std::map<std::string, std::string> XdmfDomain::getItemProperties() const
std::string XdmfDomain::getItemTag() const
{
return "Domain";
return ItemTag;
}
unsigned int XdmfDomain::getNumberOfGrids() const
......@@ -59,6 +61,14 @@ unsigned int XdmfDomain::getNumberOfGrids() const
return mGrids.size();
}
void XdmfDomain::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems)
{
for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin(); iter != childItems.end(); ++iter)
{
this->insert(boost::shared_dynamic_cast<XdmfGrid>(*iter));
}
}
void XdmfDomain::traverse(boost::shared_ptr<Loki::BaseVisitor> visitor)
{
for(std::vector<boost::shared_ptr<XdmfGrid> >::const_iterator iter = mGrids.begin(); iter != mGrids.end(); ++iter)
......
......@@ -20,6 +20,7 @@ public:
XdmfNewMacro(XdmfDomain);
LOKI_DEFINE_VISITABLE(XdmfDomain, XdmfItem)
static std::string ItemTag;
/**