Commit e6f105ce authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Improve python interface by rewriting swig wrapping and moving XdmfObject

destructors to be publicly accessible.  Modify tests to work with new wrapping.
parent 5b153544
This diff is collapsed.
......@@ -431,7 +431,12 @@ boost::shared_ptr<const XdmfArrayType> XdmfArray::getType() const
return XdmfArrayType::Uninitialized();
}
const void * const XdmfArray::getValuesPointer() const
void * XdmfArray::getValuesPointer()
{
return const_cast<void *>(static_cast<const XdmfArray &>(*this).getValuesPointer());
}
const void * XdmfArray::getValuesPointer() const
{
if(mHaveArray)
{
......
......@@ -20,7 +20,7 @@ class XdmfHDF5Controller;
* By Copy:
*
* copyValues
* getCopyValues
* getValuesCopy
*
* XdmfArray stores its own copy of the data. Modifications to the data stored in the XdmfArray will
* not change values stored in the original array.
......@@ -35,21 +35,23 @@ class XdmfHDF5Controller;
* array.
*
* Xdmf supports the following datatypes:
* Char
* Short
* Int
* Long
* Float
* Double
* Unsigned Char
* Unsigned Short
* Unsigned Int
* Int8
* Int16
* Int32
* Int64
* Float32
* Float64
* UInt8
* UInt16
* UInt32
*/
class XdmfArray : public XdmfItem {
public:
XdmfNewMacro(XdmfArray);
virtual ~XdmfArray();
LOKI_DEFINE_VISITABLE(XdmfArray, XdmfItem)
friend class XdmfHDF5Writer;
static std::string ItemTag;
......@@ -67,7 +69,7 @@ public:
void copyValues(const unsigned int startIndex, const boost::shared_ptr<const XdmfArray> values, const unsigned int valuesStartIndex= 0, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1);
/**
* Copy values from an array into this array.
* Copy values into this array.
*
* @param startIndex the index in this XdmfArray to begin insertion.
* @param valuesPointer a pointer to the values to copy into this XdmfArray.
......@@ -144,14 +146,6 @@ public:
template <typename T>
boost::shared_ptr<std::vector<T> > getValues();
/**
* Get a smart pointer to the values stored in this array (const version).
*
* @return a smart pointer to the internal vector of values stored in this array.
*/
//template <typename T>
//const boost::shared_ptr<const std::vector<T> > getValues() const;
/**
* Get a copy of the values stored in this array
*
......@@ -164,12 +158,19 @@ public:
template <typename T>
void getValuesCopy(const unsigned int startIndex, T * const valuesPointer, const unsigned int numValues = 1, const unsigned int arrayStride = 1, const unsigned int valuesStride = 1) const;
/**
* Get a pointer to the values stored in this array.
*
* @return a void pointer to the first value stored in this array.
*/
void * getValuesPointer();
/**
* Get a pointer to the values stored in this array (const version).
*
* @return a void pointer to the first value stored in this array.
*/
const void * const getValuesPointer() const;
const void * getValuesPointer() const;
/**
* Get the values stored in this array as a string.
......@@ -300,7 +301,6 @@ public:
protected:
XdmfArray();
virtual ~XdmfArray();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -27,8 +27,9 @@ class XdmfArrayType : public XdmfItemProperty {
public:
virtual ~XdmfArrayType();
friend class XdmfArray;
template <typename T> friend void boost::checked_delete(T * x);
// Supported XdmfArrayTypes
static boost::shared_ptr<const XdmfArrayType> Uninitialized();
......@@ -71,7 +72,6 @@ protected:
* @param precision the precision, in bytes, of the XdmfArrayType to construct.
*/
XdmfArrayType(const std::string & name, const unsigned int precision);
~XdmfArrayType();
private:
......
......@@ -19,6 +19,8 @@ class XdmfAttribute : public XdmfDataItem {
public:
XdmfNewMacro(XdmfAttribute);
virtual ~XdmfAttribute();
LOKI_DEFINE_VISITABLE(XdmfAttribute, XdmfDataItem)
static std::string ItemTag;
......@@ -71,7 +73,6 @@ public:
protected:
XdmfAttribute();
virtual ~XdmfAttribute();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -22,8 +22,9 @@ class XdmfAttributeCenter : public XdmfItemProperty {
public:
virtual ~XdmfAttributeCenter();
friend class XdmfAttribute;
template <typename T> friend void boost::checked_delete(T * x);
// Supported Xdmf Attribute Centers
static boost::shared_ptr<const XdmfAttributeCenter> Grid();
......@@ -60,7 +61,6 @@ protected:
* @param name the name of the XdmfAttributeCenter to construct.
*/
XdmfAttributeCenter(const std::string & name);
~XdmfAttributeCenter();
private:
......
......@@ -24,8 +24,9 @@ class XdmfAttributeType : public XdmfItemProperty {
public:
virtual ~XdmfAttributeType();
friend class XdmfAttribute;
template <typename T> friend void boost::checked_delete(T * x);
// Supported Xdmf Attribute Types
static boost::shared_ptr<const XdmfAttributeType> NoAttributeType();
......@@ -64,7 +65,6 @@ protected:
* @param name the name of the XdmfAttributeType to construct.
*/
XdmfAttributeType(const std::string & name);
~XdmfAttributeType();
private:
......
......@@ -17,6 +17,8 @@ class XdmfDataItem : public XdmfItem {
public:
virtual ~XdmfDataItem();
LOKI_DEFINE_VISITABLE(XdmfDataItem, XdmfItem)
/**
......@@ -45,7 +47,6 @@ public:
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 @@ class XdmfDomain : public XdmfItem {
public:
XdmfNewMacro(XdmfDomain);
virtual ~XdmfDomain();
LOKI_DEFINE_VISITABLE(XdmfDomain, XdmfItem)
static std::string ItemTag;
......@@ -67,7 +69,6 @@ public:
protected:
XdmfDomain();
virtual ~XdmfDomain();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -19,6 +19,8 @@ class XdmfGeometry : public XdmfDataItem {
public:
XdmfNewMacro(XdmfGeometry);
virtual ~XdmfGeometry();
LOKI_DEFINE_VISITABLE(XdmfGeometry, XdmfDataItem)
static std::string ItemTag;
......@@ -48,7 +50,6 @@ public:
protected:
XdmfGeometry();
virtual ~XdmfGeometry();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -68,6 +68,10 @@ XdmfGeometryType::XdmfGeometryType(const std::string& name, const int& dimension
{
}
XdmfGeometryType::~XdmfGeometryType()
{
}
boost::shared_ptr<const XdmfGeometryType> XdmfGeometryType::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator type = itemProperties.find("GeometryType");
......
......@@ -26,8 +26,9 @@ class XdmfGeometryType : public XdmfItemProperty {
public:
virtual ~XdmfGeometryType();
friend class XdmfGeometry;
template <typename T> friend void boost::checked_delete(T * x);
// Supported Xdmf Geometry Types
static boost::shared_ptr<const XdmfGeometryType> NoGeometryType();
......
......@@ -23,6 +23,8 @@ class XdmfGrid : public XdmfItem {
public:
XdmfNewMacro(XdmfGrid);
virtual ~XdmfGrid();
LOKI_DEFINE_VISITABLE(XdmfGrid, XdmfItem)
static std::string ItemTag;
......@@ -181,7 +183,6 @@ public:
protected:
XdmfGrid();
virtual ~XdmfGrid();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
std::string mName;
......
......@@ -20,6 +20,8 @@ class XdmfGridCollection : public XdmfGrid {
public:
XdmfNewMacro(XdmfGridCollection);
virtual ~XdmfGridCollection();
LOKI_DEFINE_VISITABLE(XdmfGridCollection, XdmfGrid)
static std::string ItemTag;
......@@ -81,7 +83,6 @@ public:
protected:
XdmfGridCollection();
virtual ~XdmfGridCollection();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -31,6 +31,10 @@ XdmfGridCollectionType::XdmfGridCollectionType(const std::string & name) :
{
}
XdmfGridCollectionType::~XdmfGridCollectionType()
{
}
boost::shared_ptr<const XdmfGridCollectionType> XdmfGridCollectionType::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator type = itemProperties.find("CollectionType");
......
......@@ -20,8 +20,9 @@ class XdmfGridCollectionType : public XdmfItemProperty {
public:
virtual ~XdmfGridCollectionType();
friend class XdmfGridCollection;
template <typename T> friend void boost::checked_delete(T * x);
// Supported XdmfGridCollectionTypes
static boost::shared_ptr<const XdmfGridCollectionType> NoCollectionType();
......
......@@ -63,73 +63,15 @@ boost::shared_ptr<const XdmfArrayType> XdmfHDF5Controller::getType() const
void XdmfHDF5Controller::read(XdmfArray * const array)
{
hid_t hdf5Handle = H5Fopen(mHDF5FilePath.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
hid_t hdf5Handle = H5Fopen(mHDF5FilePath.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
hid_t dataset = H5Dopen(hdf5Handle, mDataSetName.c_str(), H5P_DEFAULT);
hid_t dataspace = H5Dget_space(dataset);
hssize_t numVals = H5Sget_simple_extent_npoints(dataspace);
hid_t datatype = H5Dget_type(dataset);
void * valuesPointer;
if(H5Tequal(datatype, H5T_NATIVE_CHAR))
{
boost::shared_ptr<std::vector<char> > arrayValues = array->initialize<char>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_SHORT))
{
boost::shared_ptr<std::vector<short> > arrayValues = array->initialize<short>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_INT))
{
boost::shared_ptr<std::vector<int> > arrayValues = array->initialize<int>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_LONG))
{
boost::shared_ptr<std::vector<long> > arrayValues = array->initialize<long>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_FLOAT))
{
boost::shared_ptr<std::vector<float> > arrayValues = array->initialize<float>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_DOUBLE))
{
boost::shared_ptr<std::vector<double> > arrayValues = array->initialize<double>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_UCHAR))
{
boost::shared_ptr<std::vector<unsigned char> > arrayValues = array->initialize<unsigned char>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_USHORT))
{
boost::shared_ptr<std::vector<unsigned short> > arrayValues = array->initialize<unsigned short>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else if(H5Tequal(datatype, H5T_NATIVE_UINT))
{
boost::shared_ptr<std::vector<unsigned int> > arrayValues = array->initialize<unsigned int>();
array->resize(numVals, 0);
valuesPointer = &arrayValues->operator[](0);
}
else
{
assert(false);
}
H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, valuesPointer);
array->initialize(mType);
array->resize(numVals, 0);
H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, array->getValuesPointer());
herr_t status;
status = H5Tclose(datatype);
......
......@@ -19,7 +19,7 @@ class XdmfHDF5Controller : public XdmfObject {
public:
template <typename T> friend void boost::checked_delete(T * x);
virtual ~XdmfHDF5Controller();
/**
* Create a new controller for an hdf5 data set on disk.
......@@ -75,7 +75,6 @@ public:
protected:
XdmfHDF5Controller(const std::string & dataSetPath, const unsigned int size, const boost::shared_ptr<const XdmfArrayType> type);
virtual ~XdmfHDF5Controller();
private:
......
......@@ -28,12 +28,12 @@ class XdmfHDF5Writer : public XdmfVisitor,
public:
virtual ~XdmfHDF5Writer();
enum Mode {
Default, Overwrite, Append
};
template <typename T> friend void boost::checked_delete(T * x);
/**
* Construct XdmfHDF5Writer
*
......@@ -65,7 +65,6 @@ public:
protected:
XdmfHDF5Writer(const std::string & hdf5FilePath);
virtual ~XdmfHDF5Writer();
private:
......
......@@ -29,6 +29,8 @@ class XdmfItem : public XdmfObject,
public:
virtual ~XdmfItem();
LOKI_DEFINE_VISITABLE_BASE()
friend class XdmfReader;
......@@ -54,7 +56,6 @@ public:
protected:
XdmfItem();
virtual ~XdmfItem();
/**
* Populates an item using a map of key/value property pairs and a vector of its child items. This is used to
......
......@@ -18,6 +18,8 @@ class XdmfItemProperty : public XdmfObject {
public:
virtual ~XdmfItemProperty();
/**
* Retrieve the key/value pairs that this XdmfItemProperty contains by inserting into the passed vector.
*
......@@ -28,7 +30,6 @@ public:
protected:
XdmfItemProperty();
virtual ~XdmfItemProperty();
private:
......
......@@ -6,7 +6,6 @@
#include <boost/shared_ptr.hpp>
#define XdmfNewMacro(type) \
template <typename T> friend void boost::checked_delete(T * x); \
static boost::shared_ptr<type> New() \
{ \
boost::shared_ptr<type> p(new type()); \
......@@ -22,10 +21,11 @@ class XdmfObject {
public:
virtual ~XdmfObject();
protected:
XdmfObject();
virtual ~XdmfObject();
};
......
......@@ -10,7 +10,6 @@
#include "XdmfSet.hpp"
#include "XdmfTopology.hpp"
#include <libxml/xmlreader.h>
#include "XdmfReader.hpp"
......
......@@ -18,13 +18,13 @@ class XdmfReader : public XdmfObject {
public:
XdmfNewMacro(XdmfReader);
virtual ~XdmfReader();
boost::shared_ptr<XdmfItem> read(const std::string & fileName) const;
protected:
XdmfReader();
virtual ~XdmfReader();
private:
......
......@@ -20,6 +20,8 @@ class XdmfSet : public XdmfDataItem {
public:
XdmfNewMacro(XdmfSet);
virtual ~XdmfSet();
LOKI_DEFINE_VISITABLE(XdmfSet, XdmfDataItem)
static std::string ItemTag;
......@@ -58,7 +60,6 @@ public:
protected:
XdmfSet();
virtual ~XdmfSet();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -43,6 +43,10 @@ XdmfSetType::XdmfSetType(const std::string & name) :
{
}
XdmfSetType::~XdmfSetType()
{
}
boost::shared_ptr<const XdmfSetType> XdmfSetType::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator type = itemProperties.find("SetType");
......
......@@ -21,8 +21,9 @@ class XdmfSetType : public XdmfItemProperty {
public:
virtual ~XdmfSetType();
friend class XdmfSet;
template <typename T> friend void boost::checked_delete(T * x);
// Supported Xdmf Set Types
static boost::shared_ptr<const XdmfSetType> NoSetType();
......
......@@ -19,6 +19,8 @@ class XdmfTopology : public XdmfDataItem {
public:
XdmfNewMacro(XdmfTopology);
virtual ~XdmfTopology();
LOKI_DEFINE_VISITABLE(XdmfTopology, XdmfDataItem)
static std::string ItemTag;
......@@ -50,7 +52,6 @@ public:
protected:
XdmfTopology();
virtual ~XdmfTopology();
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
......
......@@ -186,6 +186,10 @@ XdmfTopologyType::XdmfTopologyType(const int& nodesPerElement, const std::string
{
}
XdmfTopologyType::~XdmfTopologyType()
{
}
boost::shared_ptr<const XdmfTopologyType> XdmfTopologyType::New(const std::map<std::string, std::string> & itemProperties)
{
std::map<std::string, std::string>::const_iterator type = itemProperties.find("TopologyType");
......
......@@ -39,8 +39,9 @@ class XdmfTopologyType : public XdmfItemProperty {
public:
virtual ~XdmfTopologyType();
friend class XdmfTopology;
template <typename T> friend void boost::checked_delete(T * x);
enum CellType {
NoCellType, Linear, Quadratic, Cubic, Arbitrary, Structured
......
......@@ -20,12 +20,12 @@ class XdmfVisitor : public XdmfObject,
public:
virtual ~XdmfVisitor();
virtual void visit(XdmfItem & item, const boost::shared_ptr<XdmfBaseVisitor> visitor);
protected:
XdmfVisitor();
virtual ~XdmfVisitor();
private:
......
......@@ -21,7 +21,7 @@ class XdmfWriter : public XdmfVisitor,
public:
template <typename T> friend void boost::checked_delete(T * x);
virtual ~XdmfWriter();
/**
* Create a new XdmfWriter to write Xdmf data to disk. This will create its own hdf5 writer based on the xmlFileName.
......@@ -99,7 +99,6 @@ protected:
XdmfWriter(const std::string & xmlFilePath);
XdmfWriter(const std::string & xmlFilePath, boost::shared_ptr<XdmfHDF5Writer> hdf5Writer);
virtual ~XdmfWriter();
private:
......
......@@ -9,6 +9,9 @@ class XdmfVisitorValueCounter : public XdmfVisitor,
public:
XdmfNewMacro(XdmfVisitorValueCounter);
~XdmfVisitorValueCounter()
{
};
int getCount()
{
......@@ -27,10 +30,6 @@ protected:
{
}
~XdmfVisitorValueCounter()
{
};
private:
int mCount;
......
......@@ -4,66 +4,48 @@ if __name__ == "__main__":
values1 = [0, 1, 2, 3]
array1 = XdmfArray.New()
array1.copyValuesAsChar(0, values1)
array1.copyValuesAsInt8(0, values1)
print array1.getValuesString()
print array1.getType() + " " + str(array1.getPrecision())
assert array1.getType() == "Char"
assert array1.getPrecision() == 1
assert array1.getType() == XdmfArrayType.Int8()
array2 = XdmfArray.New()
array2.copyValuesAsShort(0, values1)
array2.copyValuesAsInt16(0, values1)
print array2.getValuesString()
print array2.getType() + " " + str(array2.getPrecision())
assert array2.getType() == "Short"
assert array2.getPrecision() == 2
assert array2.getType() == XdmfArrayType.Int16()
array3 = XdmfArray.New()
array3.copyValuesAsInt(0, values1[2:4])
array3.copyValuesAsInt32(0, values1[2:4])
print array3.getValuesString()
print array3.getType() + " " + str(array3.getPrecision())
assert array3.getType() == "Int"
assert array3.getPrecision() == 4
assert array3.getType() == XdmfArrayType.Int32()
# Stride
array4 = XdmfArray.New()
array4.copyValuesAsLong(0, values1[0:4:2])
array4.copyValuesAsInt64(0, values1[0:4:2])
print array4.getValuesString()
print array4.getType() + " " + str(array4.getPrecision())
assert array4.getType() == "Int"
assert array4.getPrecision() == 8
assert array4.getType() == XdmfArrayType.Int64()
# Reverse
array5 = XdmfArray.New()
array5.copyValuesAsFloat(0, values1[::-1])
array5.copyValuesAsFloat32(0, values1[::-1])
print array5.getValuesString()
print array5.getType() + " " + str(array5.getPrecision())
assert array5.getType() == "Float"
assert array5.getPrecision() == 4
assert array5.getType() == XdmfArrayType.Float32()
array6 = XdmfArray.New()
array6.copyValuesAsDouble(0, values1)
array6.copyValuesAsFloat64(0, values1)
print array6.getValuesString()
print array6.getType() + " " + str(array6.getPrecision())
assert array6.getType() == "Float"
assert array6.getPrecision() == 8
assert array6.getType() == XdmfArrayType.Float64()
array7 = XdmfArray.New()
array7.copyValuesAsUChar(0, values1)
array7.copyValuesAsUInt8(0, values1)
print array7.getValuesString()
print array7.getType() + " " + str(array7.getPrecision())
assert array7.getType() == "UChar"
assert array7.getPrecision() == 1
assert array7.getType() == XdmfArrayType.UInt8()
array8 = XdmfArray.New()
array8.copyValuesAsUShort(0, values1)
array8.copyValuesAsUInt16(0, values1)
print array8.getValuesString()
print array8.getType() + " " + str(array8.getPrecision())
assert array8.getType() == "UShort"
assert array8.getPrecision() == 2
assert array8.getType() == XdmfArrayType.UInt16()
array9 = XdmfArray.New()
array9.copyValuesAsUInt(0, values1)
array9.copyValuesAsUInt32(0, values1)
print array9.getValuesString()
print array9.getType() + " " + str(array9.getPrecision())
assert array9.getType() == "UInt"
assert array9.getPrecision() == 4
assert array9.getType() == XdmfArrayType.UInt32()
......@@ -10,25 +10,25 @@ if __name__ == "__main__":
0.1, 0.1, -1.1, 1.1, 0.1, -1.1, 3.1, 0.1, -2.1, 0.1, 1.1, -1.1, 1.1, 1.1, -1.1, 3.1, 2.1, -2.1]
grid.getGeometry().setGeometryType(XdmfGeometryType.XYZ())
grid.getGeometry().getArray().copyValuesAsDouble(0, points)