Commit 12f100b7 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add python wrapping for XdmfUtils and modify some swigisms to get reader...

ENH: Add python wrapping for XdmfUtils and modify some swigisms to get reader working properly in python.
parent e7913d9d
......@@ -40,15 +40,18 @@ if(XDMF_BUILD_TESTING)
add_subdirectory(tests)
endif(XDMF_BUILD_TESTING)
option(XDMF_BUILD_UTILS OFF)
if(XDMF_BUILD_UTILS)
add_subdirectory(utils)
endif(XDMF_BUILD_UTILS)
if(XDMF_WRAP_PYTHON)
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR})
set_source_files_properties(Xdmf.i PROPERTIES CPLUSPLUS ON)
swig_add_module(Xdmf python Xdmf.i)
swig_link_libraries(Xdmf Xdmf ${PYTHON_LIBRARIES})
endif(XDMF_WRAP_PYTHON)
option(XDMF_BUILD_UTILS OFF)
if(XDMF_BUILD_UTILS)
add_subdirectory(utils)
endif(XDMF_BUILD_UTILS)
......@@ -43,6 +43,42 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
from XdmfCore import *
}
%typemap(out) boost::shared_ptr<XdmfItem> {
if(boost::shared_ptr<XdmfAttribute> attribute = boost::shared_dynamic_cast<XdmfAttribute>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfAttribute>(attribute)), SWIGTYPE_p_boost__shared_ptrT_XdmfAttribute_t, SWIG_POINTER_OWN);
}
else if(boost::shared_ptr<XdmfDomain> domain = boost::shared_dynamic_cast<XdmfDomain>($1))
{
boost::shared_ptr<XdmfDomain> * newDomain = new boost::shared_ptr<XdmfDomain>(domain);
$result = SWIG_NewPointerObj(SWIG_as_voidptr(newDomain), SWIGTYPE_p_boost__shared_ptrT_XdmfDomain_t, SWIG_POINTER_OWN);
}
else if(boost::shared_ptr<XdmfGeometry> geometry = boost::shared_dynamic_cast<XdmfGeometry>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfGeometry>(geometry)), SWIGTYPE_p_boost__shared_ptrT_XdmfGeometry_t, SWIG_POINTER_OWN);
}
else if(boost::shared_ptr<XdmfGrid> grid = boost::shared_dynamic_cast<XdmfGrid>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfGrid>(grid)), SWIGTYPE_p_boost__shared_ptrT_XdmfGrid_t, SWIG_POINTER_OWN);
}
else if(boost::shared_ptr<XdmfGridCollection> gridCollection = boost::shared_dynamic_cast<XdmfGridCollection>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfGridCollection>(gridCollection)), SWIGTYPE_p_boost__shared_ptrT_XdmfGridCollection_t, SWIG_POINTER_OWN);
}
else if(boost::shared_ptr<XdmfSet> set = boost::shared_dynamic_cast<XdmfSet>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfSet>(set)), SWIGTYPE_p_boost__shared_ptrT_XdmfSet_t, SWIG_POINTER_OWN);
}
else if(boost::shared_ptr<XdmfTopology> topology = boost::shared_dynamic_cast<XdmfTopology>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfTopology>(topology)), SWIGTYPE_p_boost__shared_ptrT_XdmfTopology_t, SWIG_POINTER_OWN);
}
else
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfItem>($1)), SWIGTYPE_p_boost__shared_ptrT_XdmfItem_t, SWIG_POINTER_OWN);
}
}
// Shared Pointer Templates
%shared_ptr(XdmfAttribute)
%shared_ptr(XdmfAttributeCenter)
......@@ -60,13 +96,6 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
%shared_ptr(XdmfTopology)
%shared_ptr(XdmfTopologyType)
// Abstract Base Classes
%template() Loki::Visitor<XdmfAttribute>;
%template() Loki::Visitor<XdmfDomain>;
%template() Loki::Visitor<XdmfGeometry>;
%template() Loki::Visitor<XdmfGrid>;
%template() Loki::Visitor<XdmfTopology>;
%include XdmfAttribute.hpp
%include XdmfAttributeCenter.hpp
%include XdmfAttributeType.hpp
......@@ -81,4 +110,4 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
%include XdmfSet.hpp
%include XdmfSetType.hpp
%include XdmfTopology.hpp
%include XdmfTopologyType.hpp
%include XdmfTopologyType.hpp
\ No newline at end of file
......@@ -15,3 +15,8 @@ XdmfReader::~XdmfReader()
std::cout << "Deleted XdmfReader " << this << std::endl;
}
boost::shared_ptr<XdmfItem> XdmfReader::read(const std::string & fileName) const
{
return XdmfCoreReader::read(fileName);
}
......@@ -17,6 +17,8 @@ public:
XdmfNewMacro(XdmfReader);
virtual ~XdmfReader();
boost::shared_ptr<XdmfItem> read(const std::string & fileName) const;
protected:
XdmfReader();
......
......@@ -23,7 +23,7 @@ public:
virtual ~XdmfSet();
LOKI_DEFINE_VISITABLE(XdmfSet, XdmfDataItem)
const static std::string ItemTag;
static const std::string ItemTag;
/**
* Get the XdmfSetType associated with this set.
......
......@@ -51,6 +51,7 @@ if(XDMF_WRAP_PYTHON)
include(${SWIG_USE_FILE})
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
set(CMAKE_SWIG_OUTDIR ${CMAKE_BINARY_DIR})
set_source_files_properties(XdmfCore.i PROPERTIES CPLUSPLUS ON)
swig_add_module(XdmfCore python XdmfCore.i)
swig_link_libraries(XdmfCore XdmfCore ${PYTHON_LIBRARIES})
......
......@@ -30,3 +30,9 @@ endforeach(executable ${XdmfUtilsExecutables})
if(XDMF_BUILD_TESTING)
add_subdirectory(tests)
endif(XDMF_BUILD_TESTING)
if(XDMF_WRAP_PYTHON)
set_source_files_properties(XdmfUtils.i PROPERTIES CPLUSPLUS ON)
swig_add_module(XdmfUtils python XdmfUtils.i)
swig_link_libraries(XdmfUtils XdmfUtils ${PYTHON_LIBRARIES})
endif(XDMF_WRAP_PYTHON)
......@@ -63,24 +63,22 @@ public:
rightPoint[2] = (1.0/3.0)*(2*point2[2] + point1[2]);
}
inline void insertPointWithoutCheck(std::vector<double> & newPoint, std::map<std::vector<double>, unsigned int, PointComparison> & coordToIdMap, const boost::shared_ptr<XdmfArray> newConnectivity, const boost::shared_ptr<XdmfArray> newPoints) const
inline void insertPointWithoutCheck(std::vector<double> & newPoint, const boost::shared_ptr<XdmfArray> & newConnectivity, const boost::shared_ptr<XdmfArray> & newPoints) const
{
unsigned int newId = newPoints->getSize() / 3;
newConnectivity->pushBack(newId);
newConnectivity->pushBack(newPoints->getSize() / 3);
newPoints->pushBack(newPoint[0]);
newPoints->pushBack(newPoint[1]);
newPoints->pushBack(newPoint[2]);
}
inline void insertPointWithCheck(std::vector<double> & newPoint, std::map<std::vector<double>, unsigned int, PointComparison> & coordToIdMap, const boost::shared_ptr<XdmfArray> newConnectivity, const boost::shared_ptr<XdmfArray> newPoints) const
inline void insertPointWithCheck(std::vector<double> & newPoint, std::map<std::vector<double>, unsigned int, PointComparison> & coordToIdMap, const boost::shared_ptr<XdmfArray> & newConnectivity, const boost::shared_ptr<XdmfArray> & newPoints) const
{
std::map<std::vector<double>, unsigned int>::const_iterator iter = coordToIdMap.find(newPoint);
if(iter == coordToIdMap.end())
{
// Not inserted before
unsigned int newId = newPoints->getSize() / 3;
coordToIdMap[newPoint] = newId;
insertPointWithoutCheck(newPoint, coordToIdMap, newConnectivity, newPoints);
coordToIdMap[newPoint] = newPoints->getSize() / 3;;
insertPointWithoutCheck(newPoint, newConnectivity, newPoints);
}
else
{
......@@ -98,6 +96,12 @@ public:
boost::shared_ptr<XdmfArray> newPoints = toReturn->getGeometry()->getArray();
newPoints->initialize(gridToConvert->getGeometry()->getArray()->getType());
newPoints->resize(gridToConvert->getGeometry()->getArray()->getSize(), 0);
if(!gridToConvert->getGeometry()->getArray()->isInitialized())
{
gridToConvert->getGeometry()->getArray()->read();
}
// Copy all geometry values from old grid into new grid because we are keeping all old points.
newPoints->copyValues(0, gridToConvert->getGeometry()->getArray(), 0, gridToConvert->getGeometry()->getArray()->getSize());
......@@ -105,6 +109,11 @@ public:
newConnectivity->initialize(gridToConvert->getTopology()->getArray()->getType());
newConnectivity->reserve(64 * gridToConvert->getTopology()->getNumberElements());
if(!gridToConvert->getTopology()->getArray()->isInitialized())
{
gridToConvert->getTopology()->getArray()->read();
}
std::vector<double> leftPoint(3);
std::vector<double> rightPoint(3);
std::map<std::vector<double>, unsigned int, PointComparison> coordToIdMap;
......@@ -116,7 +125,7 @@ public:
localNodes.reserve(44);
for(int j=0; j<8; ++j)
{
gridToConvert->getGeometry()->getArray()->getValuesCopy((8 * i + j) * 3, &localNodes[j][0], 3);
gridToConvert->getGeometry()->getArray()->getValuesCopy(gridToConvert->getTopology()->getArray()->getValueCopy<unsigned int>(8*i + j) * 3, &localNodes[j][0], 3);
}
// Add old connectivity information to newConnectivity.
......@@ -293,25 +302,24 @@ public:
// Case 28
this->computeInteriorPoints(leftPoint, rightPoint, localNodes[33], localNodes[34]);
this->insertPointWithoutCheck(leftPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(leftPoint, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, newConnectivity, newPoints);
// Case 29
this->computeInteriorPoints(leftPoint, rightPoint, localNodes[35], localNodes[32]);
this->insertPointWithoutCheck(leftPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(leftPoint, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, newConnectivity, newPoints);
// Case 30
this->computeInteriorPoints(leftPoint, rightPoint, localNodes[41], localNodes[42]);
this->insertPointWithoutCheck(leftPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(leftPoint, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, newConnectivity, newPoints);
// Case 31
this->computeInteriorPoints(leftPoint, rightPoint, localNodes[43], localNodes[40]);
this->insertPointWithoutCheck(leftPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, coordToIdMap, newConnectivity, newPoints);
this->insertPointWithoutCheck(leftPoint, newConnectivity, newPoints);
this->insertPointWithoutCheck(rightPoint, newConnectivity, newPoints);
}
return toReturn;
}
};
......
/*
XdmfUtilsPython.cpp:
swig -v -c++ -python -o XdmfUtilsPython.cpp XdmfUtils.i
*/
%module XdmfUtils
%{
// XdmfCore Includes
#include <XdmfArray.hpp>
#include <XdmfArrayType.hpp>
#include <XdmfCoreItemFactory.hpp>
#include <XdmfCoreReader.hpp>
#include <XdmfDataItem.hpp>
#include <XdmfHDF5Controller.hpp>
#include <XdmfHDF5Writer.hpp>
#include <XdmfItem.hpp>
#include <XdmfItemProperty.hpp>
#include <XdmfObject.hpp>
#include <XdmfVisitor.hpp>
#include <XdmfWriter.hpp>
// Xdmf Includes
#include <XdmfAttribute.hpp>
#include <XdmfAttributeCenter.hpp>
#include <XdmfAttributeType.hpp>
#include <XdmfDomain.hpp>
#include <XdmfGeometry.hpp>
#include <XdmfGeometryType.hpp>
#include <XdmfGrid.hpp>
#include <XdmfGridCollection.hpp>
#include <XdmfGridCollectionType.hpp>
#include <XdmfItemFactory.hpp>
#include <XdmfReader.hpp>
#include <XdmfSet.hpp>
#include <XdmfSetType.hpp>
#include <XdmfTopology.hpp>
#include <XdmfTopologyType.hpp>
// XdmfUtils Includes
//#include <XdmfPartitioner.hpp>
#include <XdmfTopologyConverter.hpp>
%}
%import Xdmf.i
//%pythoncode {
// from Xdmf import *
//}
// Shared Pointer Templates
//%shared_ptr(XdmfPartitioner)
%shared_ptr(XdmfTopologyConverter)
//%include XdmfPartitioner.hpp
%include XdmfTopologyConverter.hpp
......@@ -32,9 +32,6 @@ int main(int argc, char* argv[])
*/
boost::shared_ptr<XdmfGrid> hex64Grid = converter->convert(hexGrid, XdmfTopologyType::Hexahedron_64());
boost::shared_ptr<XdmfWriter> writer = XdmfWriter::New("hex64.xmf");
hex64Grid->accept(writer);
assert(hex64Grid->getGeometry()->getGeometryType() == XdmfGeometryType::XYZ());
assert(hex64Grid->getGeometry()->getNumberPoints() == 64);
double expectedPoints[192] = {0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0.333333333333333, 0, 0, 0.666666666666667, 0, 0, 1, 0.333333333333333,
......@@ -59,6 +56,7 @@ int main(int argc, char* argv[])
assert(fabs(expectedPoints[i] - hex64Grid->getGeometry()->getArray()->getValueCopy<double>(i)) < epsilon);
}
assert(hex64Grid->getTopology()->getTopologyType() == XdmfTopologyType::Hexahedron_64());
assert(hex64Grid->getTopology()->getNumberElements() == 1);
for(unsigned int i=0; i<64; ++i)
{
assert(i == hex64Grid->getTopology()->getArray()->getValueCopy<unsigned int>(i));
......
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