ENH:

	Add XdmfArray to store values --- modify XdmfDataItem to contain an XdmfArray.
	Modify Tests to use new XdmfArray.
	Modify XdmfVisitor to use new XdmfArray.
	Add Python Tests and move Cxx tests to their own directory.
parent c92bcfad
# Add a python test from a python file
# One cannot simply do:
# SET(ENV{PYTHONPATH} ${LIBRARY_OUTPUT_PATH})
# SET(my_test "from test_mymodule import *\;test_mymodule()")
# ADD_TEST(PYTHON-TEST-MYMODULE python -c ${my_test})
# Since cmake is only transmitting the ADD_TEST line to ctest thus you are loosing
# the env var. The only way to store the env var is to physically write in the cmake script
# whatever PYTHONPATH you want and then add the test as 'cmake -P python_test.cmake'
#
# Usage:
# SET_SOURCE_FILES_PROPERTIES(test.py PROPERTIES PYTHONPATH
# "${LIBRARY_OUTPUT_PATH}:${VTK_DIR}")
# ADD_PYTHON_TEST(PYTHON-TEST test.py)
# Need python interpreter:
FIND_PACKAGE(PythonInterp REQUIRED)
MACRO(ADD_PYTHON_TEST TESTNAME FILENAME)
GET_SOURCE_FILE_PROPERTY(loc ${FILENAME} LOCATION)
GET_SOURCE_FILE_PROPERTY(pyenv ${FILENAME} PYTHONPATH)
STRING(REGEX REPLACE ";" " " wo_semicolumn "${ARGN}")
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake
"
SET(ENV{PYTHONPATH} ${pyenv})
EXECUTE_PROCESS(
COMMAND ${PYTHON_EXECUTABLE} ${loc} ${wo_semicolumn}
#WORKING_DIRECTORY @LIBRARY_OUTPUT_PATH@
RESULT_VARIABLE import_res
OUTPUT_VARIABLE import_output
ERROR_VARIABLE import_output
)
# Pass the output back to ctest
MESSAGE("\${import_output}")
IF(import_res)
MESSAGE(SEND_ERROR "\${import_res}")
ENDIF(import_res)
"
)
ADD_TEST(${TESTNAME} cmake -P ${CMAKE_CURRENT_BINARY_DIR}/${TESTNAME}.cmake)
ENDMACRO(ADD_PYTHON_TEST)
# Byte compile recursively a directory (DIRNAME)
MACRO(ADD_PYTHON_COMPILEALL_TEST DIRNAME)
# First get the path:
GET_FILENAME_COMPONENT(temp_path "${PYTHON_LIBRARIES}" PATH)
# Find the python script:
GET_FILENAME_COMPONENT(PYTHON_COMPILE_ALL_PY "${temp_path}/../compileall.py" ABSOLUTE)
# add test, use DIRNAME to create uniq name for the test:
ADD_TEST(COMPILE_ALL-${DIRNAME} ${PYTHON_EXECUTABLE} "${PYTHON_COMPILE_ALL_PY}" -q ${DIRNAME})
ENDMACRO(ADD_PYTHON_COMPILEALL_TEST)
project(Xdmf2)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMake ${CMAKE_MODULE_PATH})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(BUILD_SHARED_LIBS true)
......@@ -29,12 +30,12 @@ if(XDMF_WRAP_PYTHON)
endif(XDMF_WRAP_PYTHON)
set(XdmfSources
XdmfArray
XdmfAttribute
XdmfAttributeCenter
XdmfAttributeType
XdmfDataItem
XdmfDataItemType
XdmfDomain
XdmfDomain
XdmfGeometry
XdmfGeometryType
XdmfGrid
......@@ -63,6 +64,4 @@ option(BUILD_TESTING OFF)
if(BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
add_executable(XdmfTest test.cpp)
target_link_libraries(XdmfTest Xdmf)
endif(BUILD_TESTING)
......@@ -15,6 +15,7 @@ namespace boost {
}
%template(XdmfAttributePtr) boost::shared_ptr<XdmfAttribute>;
%template(XdmfArrayPtr) boost::shared_ptr<XdmfArray>;
%template(XdmfDataItemPtr) boost::shared_ptr<XdmfDataItem>;
%template(XdmfDomainPtr) boost::shared_ptr<XdmfDomain>;
%template(XdmfGeometryPtr) boost::shared_ptr<XdmfGeometry>;
......@@ -26,11 +27,11 @@ namespace boost {
%module Xdmf
%{
#include <XdmfArray.hpp>
#include <XdmfAttribute.hpp>
#include <XdmfAttributeCenter.hpp>
#include <XdmfAttributeType.hpp>
#include <XdmfDataItem.hpp>
#include <XdmfDataItemType.hpp>
#include <XdmfDomain.hpp>
#include <XdmfGeometry.hpp>
#include <XdmfGeometryType.hpp>
......@@ -48,14 +49,36 @@ namespace boost {
%include XdmfVisitor.hpp
%include XdmfDataItem.hpp
%include XdmfDataItemType.hpp
%include XdmfAttribute.hpp
%include XdmfAttributeCenter.hpp
%include XdmfAttributeType.hpp
%include XdmfArray.hpp
%include XdmfDomain.hpp
%include XdmfGeometry.hpp
%include XdmfGeometryType.hpp
%include XdmfGrid.hpp
%include XdmfTopology.hpp
%include XdmfTopologyType.hpp
%template(CharVector) std::vector<char>;
%template(ShortVector) std::vector<short>;
%template(IntVector) std::vector<int>;
%template(LongVector) std::vector<long>;
%template(FloatVector) std::vector<float>;
%template(DoubleVector) std::vector<double>;
%template(UCharVector) std::vector<unsigned char>;
%template(UShortVector) std::vector<unsigned short>;
%template(UIntVector) std::vector<unsigned int>;
%extend XdmfArray {
%template(setValues) setValues<char>;
%template(setValues) setValues<short>;
%template(setValues) setValues<int>;
%template(setValues) setValues<long>;
%template(setValues) setValues<float>;
%template(setValues) setValues<double>;
%template(setValues) setValues<unsigned char>;
%template(setValues) setValues<unsigned short>;
%template(setValues) setValues<unsigned int>;
};
This diff is collapsed.
/*
* XdmfArray.cpp
*
* Created on: Jan 29, 2010
* Author: kleiter
*/
#include "XdmfArray.hpp"
#include "XdmfVisitor.hpp"
#include <sstream>
class XdmfArrayGetPrecision : public boost::static_visitor <int> {
public:
XdmfArrayGetPrecision()
{
}
int operator()(const std::vector<char> & array) const
{
return 1;
}
int operator()(const std::vector<short> & array) const
{
return 2;
}
int operator()(const std::vector<int> & array) const
{
return 4;
}
int operator()(const std::vector<long> & array) const
{
return 8;
}
int operator()(const std::vector<float> & array) const
{
return 4;
}
int operator()(const std::vector<double> & array) const
{
return 8;
}
int operator()(const std::vector<unsigned char> & array) const
{
return 1;
}
int operator()(const std::vector<unsigned short> & array) const
{
return 2;
}
int operator()(const std::vector<unsigned int> & array) const
{
return 4;
}
};
class XdmfArrayGetType : public boost::static_visitor <std::string> {
public:
XdmfArrayGetType()
{
}
std::string operator()(const std::vector<char> & array) const
{
return "Char";
}
std::string operator()(const std::vector<short> & array) const
{
return "Short";
}
std::string operator()(const std::vector<int> & array) const
{
return "Int";
}
std::string operator()(const std::vector<long> & array) const
{
return "Int";
}
std::string operator()(const std::vector<float> & array) const
{
return "Float";
}
std::string operator()(const std::vector<double> & array) const
{
return "Float";
}
std::string operator()(const std::vector<unsigned char> & array) const
{
return "UChar";
}
std::string operator()(const std::vector<unsigned short> & array) const
{
return "UShort";
}
std::string operator()(const std::vector<unsigned int> & array) const
{
return "UInt";
}
};
class XdmfArrayGetHDF5Type : public boost::static_visitor <hid_t> {
public:
XdmfArrayGetHDF5Type()
{
}
hid_t operator()(const std::vector<char> & array) const
{
return H5T_NATIVE_CHAR;
}
hid_t operator()(const std::vector<short> & array) const
{
return H5T_NATIVE_SHORT;
}
hid_t operator()(const std::vector<int> & array) const
{
return H5T_NATIVE_INT;
}
hid_t operator()(const std::vector<long> & array) const
{
return H5T_NATIVE_LONG;
}
hid_t operator()(const std::vector<float> & array) const
{
return H5T_NATIVE_FLOAT;
}
hid_t operator()(const std::vector<double> & array) const
{
return H5T_NATIVE_DOUBLE;
}
hid_t operator()(const std::vector<unsigned char> & array) const
{
return H5T_NATIVE_UCHAR;
}
hid_t operator()(const std::vector<unsigned short> & array) const
{
return H5T_NATIVE_USHORT;
}
hid_t operator()(const std::vector<unsigned int> & array) const
{
return H5T_NATIVE_UINT;
}
};
class XdmfArrayGetSize : public boost::static_visitor <int> {
public:
XdmfArrayGetSize()
{
}
template<typename T> int operator()(const std::vector<T> & array) const
{
return array.size();
}
};
class XdmfArrayGetValues : public boost::static_visitor <std::string> {
public:
XdmfArrayGetValues()
{
}
std::string operator()(const std::vector<char> & array) const
{
std::stringstream toReturn;
for(int i=0; i<array.size(); ++i)
{
toReturn << (int)(array[i]) << " ";
}
return toReturn.str();
}
template<typename T> std::string operator()(const std::vector<T> & array) const
{
std::stringstream toReturn;
for(int i=0; i<array.size(); ++i)
{
toReturn << array[i] << " ";
}
return toReturn.str();
}
};
class XdmfArrayGetValuesPointer : public boost::static_visitor <void*> {
public:
XdmfArrayGetValuesPointer()
{
}
template<typename T> void* operator()(std::vector<T> & array) const
{
return &array[0];
}
};
class XdmfArrayGetValuesPointerConst : public boost::static_visitor <const void* const> {
public:
XdmfArrayGetValuesPointerConst()
{
}
template<typename T> const void* const operator()(const std::vector<T> & array) const
{
return &array[0];
}
};
XdmfArray::XdmfArray() :
mInitialized(false)
{
std::cout << "Created Array " << this << std::endl;
}
XdmfArray::~XdmfArray()
{
std::cout << "Deleted Array " << this << std::endl;
}
hid_t XdmfArray::getHDF5Type() const
{
return boost::apply_visitor( XdmfArrayGetHDF5Type(), mArray);
}
int XdmfArray::getPrecision() const
{
return boost::apply_visitor( XdmfArrayGetPrecision(), mArray);
}
int XdmfArray::getSize() const
{
return boost::apply_visitor( XdmfArrayGetSize(), mArray);
}
std::string XdmfArray::getType() const
{
return boost::apply_visitor( XdmfArrayGetType(), mArray);
}
std::string XdmfArray::getValues() const
{
return boost::apply_visitor( XdmfArrayGetValues(), mArray);
}
void* XdmfArray::getValuesPointer()
{
return boost::apply_visitor( XdmfArrayGetValuesPointer(), mArray);
}
const void* const XdmfArray::getValuesPointer() const
{
return boost::apply_visitor( XdmfArrayGetValuesPointerConst(), mArray);
}
std::string XdmfArray::printSelf() const
{
return "";
}
void XdmfArray::write(boost::shared_ptr<XdmfVisitor> visitor) const
{
visitor->visit(this);
}
/*
* XdmfArray.hpp
*
* Created on: Jan 25, 2010
* Author: kleiter
*/
#ifndef XDMFARRAY_HPP_
#define XDMFARRAY_HPP_
// Includes
#include "XdmfItem.hpp"
#include <boost/variant.hpp>
#include <hdf5.h>
#include <vector>
template<typename T>
class XdmfArraySetValues : public boost::static_visitor <void> {
public:
XdmfArraySetValues(int startIndex, T* valuesPointer, int numValues, int arrayStride = 1, int valuesStride = 1) :
mStartIndex(startIndex),
mValuesPointer(valuesPointer),
mNumValues(numValues),
mArrayStride(arrayStride),
mValuesStride(valuesStride)
{
}
template<typename U> void operator()(std::vector<U> & array) const
{
int size = mStartIndex + mNumValues;
if(mArrayStride > 1)
{
size = mStartIndex + mNumValues * mArrayStride - 1;
}
if(array.size() < size)
{
array.resize(size);
}
for(int i=0; i<mNumValues; ++i)
{
array[mStartIndex + i*mArrayStride] = (U)mValuesPointer[i*mValuesStride];
}
}
private:
int mStartIndex;
T* mValuesPointer;
int mNumValues;
int mArrayStride;
int mValuesStride;
};
class XdmfArray : public XdmfItem {
public:
XdmfNewMacro(XdmfArray);
/**
* Get the data type of this array.
*
* @return a string containing the Xdmf data type for the array, this is one of
* Char, Short, Int, Float, UChar, UShort, UInt.
*/
virtual std::string getType() const;
/**
* Get the hdf5 data type of this array.
*
* @return a hid_t value containing the hdf5 data type for the array.
*/
virtual hid_t getHDF5Type() const;
/**
* Get the precision, in bytes, of the data type of this array.
*
* @return a int containing the precision, in bytes, of the data type of this array.
*/
virtual int getPrecision() const;
/**
* Get the number of values stored in this array.
*
* @return a int containing the number of values stored in this array.
*/
virtual int getSize() const;
/**
* Get the values stored in this array as a string.
*
* @return a string containing the contents of the array.
*/
virtual std::string getValues() const;
/**
* Get a pointer to the values stored in this array.
*
* @return a void pointer to the first value stored in this array.
*/
virtual 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.
*/
virtual const void* const getValuesPointer() const;
virtual std::string printSelf() const;
/**
* Copy the values from a pointer into this array.
*
* @param startIndex the index in this array to begin insertion.
* @param valuesPointer a pointer to the values to copy into this array.
* @param numValues the number of values to copy into this array.
* @param arrayStride number of values to stride in this array between each copy.
* @param valuesStride number of values to stride in the pointer between each copy.
*/
template<typename T> void setValues(int startIndex, T* valuesPointer, int numValues = 1, int arrayStride = 1, int valuesStride = 1)
{
if(!mInitialized)
{
// Set type of variant to type of pointer
mArray = std::vector<T>();
mInitialized = true;
}
boost::apply_visitor( XdmfArraySetValues<T>(startIndex, valuesPointer, numValues, arrayStride, valuesStride), mArray);
}
/**
* Copy the values from a vector into this array
*
* @param array the vector to copy into this array.
*/
template<typename T> void setValues(std::vector<T> & array)
{
mArray = array;
}
virtual void write(boost::shared_ptr<XdmfVisitor>) const;
protected:
XdmfArray();
virtual ~XdmfArray();
private:
bool mInitialized;
boost::variant<std::vector<char>,
std::vector<short>,
std::vector<int>,
std::vector<long>,
std::vector<float>,
std::vector<double>,
std::vector<unsigned char>,
std::vector<unsigned short>,
std::vector<unsigned int> > mArray;
};
#endif /* XDMFARRAY_HPP_ */
......@@ -61,11 +61,6 @@ void XdmfAttribute::setName(const std::string& name)
mName= name;
}
void XdmfAttribute::setNumberValues(int numValues)
{
mNumberValues = numValues;
}
void XdmfAttribute::write(boost::shared_ptr<XdmfVisitor> visitor) const
{
visitor->visit(this);
......
......@@ -8,6 +8,7 @@
#ifndef XDMFATTRIBUTE_HPP_
#define XDMFATTRIBUTE_HPP_
// Includes
#include "XdmfAttributeCenter.hpp"
#include "XdmfAttributeType.hpp"
#include "XdmfDataItem.hpp"
......@@ -74,13 +75,6 @@ public:
*/
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;
......
......@@ -5,12 +5,11 @@
* Author: kleiter
*/
#include "XdmfArray.hpp"
#include "XdmfDataItem.hpp"
#include "XdmfVisitor.hpp"
XdmfDataItem::XdmfDataItem() :
mDataItemType(XdmfDataItemType::Unknown()),
mNumberValues(0)
mArray(XdmfArray::New())
{
std::cout << "Created DataItem " << this << std::endl;
}
......@@ -25,126 +24,17 @@ std::string XdmfDataItem::printSelf() const
return "XdmfDataItem";
}
template <>
void XdmfDataItem::setValues<char>(char* dataPointer)
void XdmfDataItem::setArray(boost::shared_ptr<XdmfArray> array)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Int8();
mArray = array;
}
template <>
void XdmfDataItem::setValues<unsigned char>(unsigned char* dataPointer)
boost::shared_ptr<XdmfArray> XdmfDataItem::getArray()
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::UInt8();
return mArray;
}
template <>
void XdmfDataItem::setValues<short>(short* dataPointer)
boost::shared_ptr<const XdmfArray> XdmfDataItem::getArray() const
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Int16();
return mArray;
}
template <>
void XdmfDataItem::setValues<unsigned short>(unsigned short* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::UInt16();
}
template <>
void XdmfDataItem::setValues<int>(int* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Int32();
}
template <>
void XdmfDataItem::setValues<unsigned int>(unsigned int* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::UInt32();
}
template <>
void XdmfDataItem::setValues<long>(long* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Int64();
}
template <>
void XdmfDataItem::setValues<float>(float* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Float32();
}
template <>
void XdmfDataItem::setValues<double>(double* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Float64();
}
template <typename T>
void XdmfDataItem::setValues(T* dataPointer)
{
mDataPointer = dataPointer;
mDataItemType = XdmfDataItemType::Unknown();
}
void* XdmfDataItem::getValues()
{
return mDataPointer;
}
const void* const XdmfDataItem::getValues() const
{
return mDataPointer;
}
int XdmfDataItem::getNumberValues() const
{
return mNumberValues;
}
hid_t XdmfDataItem::getHDF5DataType() const
{
return mDataItemType.getHDF5DataType();
}
std::string XdmfDataItem::getName() const
{
return mDataItemType.getName</