ENH: Add TestXdmfVisitorValueCounter to demonstrate how to write visitor...

ENH: Add TestXdmfVisitorValueCounter to demonstrate how to write visitor operations over the Xdmf graph that do something other than write to disk.  Separate hexahedron grid example into a header file so that other test cases can use it for testing.
parent 8e0b09fb
......@@ -6,6 +6,7 @@ set(XdmfCxxTests
TestXdmfGrid
TestXdmfTopology
TestXdmfVisitor
TestXdmfVisitorValueCounter
)
foreach(test ${XdmfCxxTests})
......
#include "XdmfAttribute.hpp"
#include "XdmfArray.hpp"
#include "XdmfDomain.hpp"
#include "XdmfGeometry.hpp"
#include "XdmfGrid.hpp"
#include "XdmfTopology.hpp"
#include "XdmfVisitor.hpp"
#include "XdmfTestDataGenerator.hpp"
int main(int argc, char* argv[])
{
boost::shared_ptr<XdmfVisitor> visitor = XdmfVisitor::New();
visitor->setLightDataLimit(10);
boost::shared_ptr<XdmfGrid> grid = XdmfGrid::New();
grid->setName("test");
// Set Geometry
double points[] = {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,
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()->copyValues(0, &points[0], 36);
// Set Topology
int connectivity[] = {0, 1, 7, 6, 3, 4, 10, 9, 1, 2, 8, 7, 4, 5, 11, 10};
grid->getTopology()->setTopologyType(XdmfTopologyType::Hexahedron());
grid->getTopology()->getArray()->copyValues(0, &connectivity[0], 16);
// Add Node Attribute
boost::shared_ptr<XdmfAttribute> nodalAttribute = XdmfAttribute::New();
int nodeValues[] = {100, 200, 300, 300, 400, 500, 300, 400, 500, 500, 600, 700};
nodalAttribute->setName("Nodal Attribute");
nodalAttribute->setAttributeType(XdmfAttributeType::Scalar());
nodalAttribute->setAttributeCenter(XdmfAttributeCenter::Node());
nodalAttribute->getArray()->copyValues(0, &nodeValues[0], 12);
// Add Cell Attribute
boost::shared_ptr<XdmfAttribute> cellAttribute = XdmfAttribute::New();
int cellValues[] = {100, 200};
cellAttribute->setName("Cell Attribute");
cellAttribute->setAttributeType(XdmfAttributeType::Scalar());
cellAttribute->setAttributeCenter(XdmfAttributeCenter::Cell());
cellAttribute->getArray()->copyValues(0, &cellValues[0], 2);
grid->insert(nodalAttribute);
grid->insert(cellAttribute);
boost::shared_ptr<XdmfGrid> grid = XdmfTestDataGenerator::createHexahedron();
boost::shared_ptr<XdmfDomain> domain = XdmfDomain::New();
domain->insert(grid);
......
#include "XdmfVisitor.hpp"
#include "XdmfTestDataGenerator.hpp"
// Make a new XdmfVisitor that simply counts number of values
class XdmfVisitorValueCounter : public XdmfVisitor {
public:
XdmfNewMacro(XdmfVisitorValueCounter);
int getCount()
{
return mCount;
}
virtual void visit(const XdmfArray * const array, boost::shared_ptr<XdmfVisitor> visitor)
{
mCount += array->getSize();
}
protected:
XdmfVisitorValueCounter() :
mCount(0)
{
}
~XdmfVisitorValueCounter()
{
};
private:
int mCount;
};
int main(int argc, char* argv[])
{
boost::shared_ptr<XdmfVisitorValueCounter> visitor = XdmfVisitorValueCounter::New();
boost::shared_ptr<XdmfGrid> grid = XdmfTestDataGenerator::createHexahedron();
grid->write(visitor);
assert(visitor->getCount() == 66);
return 0;
}
#include "XdmfAttribute.hpp"
#include "XdmfArray.hpp"
#include "XdmfDomain.hpp"
#include "XdmfGeometry.hpp"
#include "XdmfGrid.hpp"
#include "XdmfTopology.hpp"
class XdmfTestDataGenerator {
public:
/**
* Number of Cells = 2
* Number of Points = 12
* Number of Attributes = 2
* Cell Attributes = 1
* Nodal Attributes = 1
* Total Number of Values = 66
*
*/
//
//
static boost::shared_ptr<XdmfGrid> createHexahedron()
{
boost::shared_ptr<XdmfGrid> grid = XdmfGrid::New();
grid->setName("Hexahedron");
// Set Geometry
double points[] = {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,
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()->copyValues(0, &points[0], 36);
// Set Topology
int connectivity[] = {0, 1, 7, 6, 3, 4, 10, 9, 1, 2, 8, 7, 4, 5, 11, 10};
grid->getTopology()->setTopologyType(XdmfTopologyType::Hexahedron());
grid->getTopology()->getArray()->copyValues(0, &connectivity[0], 16);
// Add Node Attribute
boost::shared_ptr<XdmfAttribute> nodalAttribute = XdmfAttribute::New();
int nodeValues[] = {100, 200, 300, 300, 400, 500, 300, 400, 500, 500, 600, 700};
nodalAttribute->setName("Nodal Attribute");
nodalAttribute->setAttributeType(XdmfAttributeType::Scalar());
nodalAttribute->setAttributeCenter(XdmfAttributeCenter::Node());
nodalAttribute->getArray()->copyValues(0, &nodeValues[0], 12);
// Add Cell Attribute
boost::shared_ptr<XdmfAttribute> cellAttribute = XdmfAttribute::New();
int cellValues[] = {100, 200};
cellAttribute->setName("Cell Attribute");
cellAttribute->setAttributeType(XdmfAttributeType::Scalar());
cellAttribute->setAttributeCenter(XdmfAttributeCenter::Cell());
cellAttribute->getArray()->copyValues(0, &cellValues[0], 2);
grid->insert(nodalAttribute);
grid->insert(cellAttribute);
return grid;
}
};
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