Commit 16a914c6 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Revert changes to XdmfSet to go back to XdmfArray implementation. Add Attributes to XdmfSet.

parent 983479ed
......@@ -92,12 +92,6 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
}
}
%include std_set.i
%template(std_set_uint) std::set<unsigned int>;
// Abstract Base Classes
%template() Loki::Visitor<XdmfSet>;
// Shared Pointer Templates
%shared_ptr(XdmfAttribute)
%shared_ptr(XdmfAttributeCenter)
......
......@@ -5,7 +5,7 @@
* Author: kleiter
*/
#include "XdmfArray.hpp"
#include "XdmfAttribute.hpp"
#include "XdmfHDF5Controller.hpp"
#include "XdmfSet.hpp"
#include "XdmfSetType.hpp"
......@@ -17,7 +17,6 @@ boost::shared_ptr<XdmfSet> XdmfSet::New()
}
XdmfSet::XdmfSet() :
mHDF5Controller(boost::shared_ptr<XdmfHDF5Controller>()),
mName(""),
mSetType(XdmfSetType::NoSetType())
{
......@@ -29,14 +28,35 @@ XdmfSet::~XdmfSet()
const std::string XdmfSet::ItemTag = "Set";
boost::shared_ptr<XdmfHDF5Controller> XdmfSet::getHDF5Controller()
boost::shared_ptr<XdmfAttribute> XdmfSet::getAttribute(const unsigned int index)
{
return boost::const_pointer_cast<XdmfHDF5Controller>(static_cast<const XdmfSet &>(*this).getHDF5Controller());
return boost::const_pointer_cast<XdmfAttribute>(static_cast<const XdmfSet &>(*this).getAttribute(index));
}
boost::shared_ptr<const XdmfHDF5Controller> XdmfSet::getHDF5Controller() const
boost::shared_ptr<const XdmfAttribute> XdmfSet::getAttribute(const unsigned int index) const
{
return mHDF5Controller;
if(index < mAttributes.size())
{
return mAttributes[index];
}
return boost::shared_ptr<XdmfAttribute>();
}
boost::shared_ptr<XdmfAttribute> XdmfSet::getAttribute(const std::string & name)
{
return boost::const_pointer_cast<XdmfAttribute>(static_cast<const XdmfSet &>(*this).getAttribute(name));
}
boost::shared_ptr<const XdmfAttribute> XdmfSet::getAttribute(const std::string & name) const
{
for(std::vector<boost::shared_ptr<XdmfAttribute> >::const_iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
{
if((*iter)->getName().compare(name) == 0)
{
return *iter;
}
}
return boost::shared_ptr<XdmfAttribute>();
}
std::map<std::string, std::string> XdmfSet::getItemProperties() const
......@@ -57,14 +77,19 @@ std::string XdmfSet::getName() const
return mName;
}
unsigned int XdmfSet::getNumberAttributes() const
{
return mAttributes.size();
}
boost::shared_ptr<const XdmfSetType> XdmfSet::getType() const
{
return mSetType;
}
bool XdmfSet::isInitialized() const
void XdmfSet::insert(const boost::shared_ptr<XdmfAttribute> attribute)
{
return std::set<unsigned int>::size() > 0;
mAttributes.push_back(attribute);
}
void XdmfSet::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems, const XdmfCoreReader * const reader)
......@@ -78,47 +103,40 @@ void XdmfSet::populateItem(const std::map<std::string, std::string> & itemProper
{
assert(false);
}
mSetType = XdmfSetType::New(itemProperties);
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))
{
if(array->isInitialized())
{
for(unsigned int i=0; i<array->size(); ++i)
{
this->insert(array->getValueCopy<unsigned int>(i));
}
}
else
{
mHDF5Controller = array->getHDF5Controller();
}
this->swap(array);
// TODO: If multiple dataitems.
}
}
mSetType = XdmfSetType::New(itemProperties);
}
void XdmfSet::read()
{
if(mHDF5Controller)
{
boost::shared_ptr<XdmfArray> setValues = XdmfArray::New();
mHDF5Controller->read(setValues.get());
for(unsigned int i=0; i<setValues->size(); ++i)
else if(boost::shared_ptr<XdmfAttribute> attribute = boost::shared_dynamic_cast<XdmfAttribute>(*iter))
{
this->insert(setValues->getValueCopy<unsigned int>(i));
this->insert(attribute);
}
}
}
void XdmfSet::release()
void XdmfSet::removeAttribute(const unsigned int index)
{
std::set<unsigned int>::clear();
if(index < mAttributes.size())
{
mAttributes.erase(mAttributes.begin() + index);
}
}
void XdmfSet::setHDF5Controller(const boost::shared_ptr<XdmfHDF5Controller> hdf5Controller)
void XdmfSet::removeAttribute(const std::string & name)
{
mHDF5Controller = hdf5Controller;
for(std::vector<boost::shared_ptr<XdmfAttribute> >::iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
{
if((*iter)->getName().compare(name) == 0)
{
mAttributes.erase(iter);
return;
}
}
return;
}
void XdmfSet::setName(const std::string & name)
......@@ -131,28 +149,11 @@ void XdmfSet::setType(const boost::shared_ptr<const XdmfSetType> setType)
mSetType = setType;
}
std::size_t XdmfSet::size() const
{
if(std::set<unsigned int>::size() != 0)
{
return std::set<unsigned int>::size();
}
else if(mHDF5Controller)
{
return mHDF5Controller->size();
}
return 0;
}
void XdmfSet::traverse(const boost::shared_ptr<XdmfBaseVisitor> visitor)
{
boost::shared_ptr<XdmfArray> setValues = XdmfArray::New();
setValues->reserve(std::set<unsigned int>::size());
for(XdmfSet::const_iterator iter = this->begin(); iter != this->end(); ++iter)
XdmfArray::traverse(visitor);
for(std::vector<boost::shared_ptr<XdmfAttribute> >::const_iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
{
setValues->pushBack(*iter);
(*iter)->accept(visitor);
}
setValues->setHDF5Controller(mHDF5Controller);
setValues->accept(visitor);
mHDF5Controller = setValues->getHDF5Controller();
}
......@@ -2,12 +2,12 @@
#define XDMFSET_HPP_
// Forward Declarations
class XdmfAttribute;
class XdmfHDF5Controller;
class XdmfSetType;
// Includes
#include <set>
#include "XdmfItem.hpp"
#include "XdmfArray.hpp"
/**
* @brief Holds a collection of individual nodes, cells, faces, or edges that are part of an XdmfGrid.
......@@ -17,8 +17,7 @@ class XdmfSetType;
* set are determined by their id. An XdmfSet can have XdmfAttributes attached that contain extra values
* attached to the elements in the set.
*/
class XdmfSet : public XdmfItem,
public std::set<unsigned int> {
class XdmfSet : public XdmfArray {
public:
......@@ -31,22 +30,40 @@ public:
virtual ~XdmfSet();
LOKI_DEFINE_VISITABLE(XdmfSet, XdmfItem)
LOKI_DEFINE_VISITABLE(XdmfSet, XdmfArray)
static const std::string ItemTag;
/**
* Get the hdf5 controller attached to this set.
* Get an attribute attached to this set by index.
*
* @return the hdf5 controller attached to this set.
* @param index of the attribute to retrieve.
* @return requested attribute. If not found a NULL pointer is returned.
*/
boost::shared_ptr<XdmfHDF5Controller> getHDF5Controller();
boost::shared_ptr<XdmfAttribute> getAttribute(const unsigned int index);
/**
* Get the hdf5 controller attached to this set (const version).
* Get an attribute attached to this set by index (const version).
*
* @return the hdf5 controller attached to this set.
* @param index of the attribute to retrieve.
* @return requested attribute. If not found a NULL pointer is returned.
*/
boost::shared_ptr<const XdmfHDF5Controller> getHDF5Controller() const;
boost::shared_ptr<const XdmfAttribute> getAttribute(const unsigned int index) const;
/**
* Get an attribute attached to this set by name.
*
* @param name of the attribute to retrieve.
* @return requested attribute. If not found a NULL pointer is returned.
*/
boost::shared_ptr<XdmfAttribute> getAttribute(const std::string & name);
/**
* Get an attribute attached to this set by name (const version).
*
* @param name the name of the attribute to retrieve.
* @return requested attribute. If not found a NULL pointer is returned.
*/
boost::shared_ptr<const XdmfAttribute> getAttribute(const std::string & name) const;
std::map<std::string, std::string> getItemProperties() const;
......@@ -60,33 +77,39 @@ public:
std::string getName() const;
/**
* Get the XdmfSetType associated with this set.
* Get the number of attributes attached to this grid.
*
* @return XdmfSetType of this set.
* @return the number of attributes attached to this grid.
*/
boost::shared_ptr<const XdmfSetType> getType() const;
unsigned int getNumberAttributes() const;
/**
* Returns whether the set is initialized (contains values in memory).
* Get the XdmfSetType associated with this set.
*
* @return XdmfSetType of this set.
*/
bool isInitialized() const;
boost::shared_ptr<const XdmfSetType> getType() const;
/**
* Read data from disk into memory.
* Insert an attribute into the set.
*
* @param attribute an XdmfAttribute to attach to this set.
*/
void read();
void insert(const boost::shared_ptr<XdmfAttribute> attribute);
/**
* Release all data held in memory. The HDF5Controller remains attached.
* Remove an attribute from the set by index. If no attribute is at that index, no attributes are removed.
*
* @param index of the attribute to remove.
*/
void release();
void removeAttribute(const unsigned int index);
/**
* Attach an hdf5 controller to this set.
* Remove an attribute from the set by name. If no attribute having the name is found, no attributes are removed.
*
* @param hdf5Controller to attach to this set.
* @param name of the attribute to remove.
*/
void setHDF5Controller(const boost::shared_ptr<XdmfHDF5Controller> hdf5Controller);
void removeAttribute(const std::string & name);
/**
* Set the name of the set.
......@@ -102,8 +125,6 @@ public:
*/
void setType(const boost::shared_ptr<const XdmfSetType> setType);
std::size_t size() const;
void traverse(const boost::shared_ptr<XdmfBaseVisitor> visitor);
protected:
......@@ -116,8 +137,8 @@ private:
XdmfSet(const XdmfSet & set); // Not implemented.
void operator=(const XdmfSet & set); // Not implemented.
std::vector<boost::shared_ptr<XdmfAttribute> > mAttributes;
std::string mName;
boost::shared_ptr<XdmfHDF5Controller> mHDF5Controller;
boost::shared_ptr<const XdmfSetType> mSetType;
};
......
#include "XdmfAttribute.hpp"
#include "XdmfSet.hpp"
#include "XdmfSetType.hpp"
......@@ -16,11 +17,21 @@ int main(int argc, char* argv[])
set->setName("TEST");
assert(set->getName().compare("TEST") == 0);
assert(set->isInitialized() == false);
assert(set->find(0) == set->end());
set->insert(0);
assert(set->isInitialized() == true);
assert(set->find(0) != set->end());
assert(set->getNumberAttributes() == 0);
assert(set->getAttribute("") == NULL);
assert(set->getAttribute(0) == NULL);
boost::shared_ptr<XdmfAttribute> attribute = XdmfAttribute::New();
attribute->setName("foo");
set->insert(attribute);
assert(set->getNumberAttributes() == 1);
assert(set->getAttribute(0) == attribute);
assert(set->getAttribute("foo") == attribute);
set->removeAttribute(0);
assert(set->getNumberAttributes() == 0);
set->insert(attribute);
set->removeAttribute("no");
assert(set->getNumberAttributes() == 1);
set->removeAttribute("foo");
assert(set->getNumberAttributes() == 0);
return 0;
}
......@@ -4,8 +4,7 @@
// Make a new XdmfVisitor that simply counts number of values
class XdmfVisitorValueCounter : public XdmfVisitor,
public Loki::Visitor<XdmfArray>,
public Loki::Visitor<XdmfSet> {
public Loki::Visitor<XdmfArray> {
public:
......@@ -34,11 +33,6 @@ public:
mCount += array.size();
}
void visit(XdmfSet & set, boost::shared_ptr<Loki::BaseVisitor> visitor)
{
mCount += set.size();
}
protected:
XdmfVisitorValueCounter() :
......
......@@ -63,7 +63,7 @@ public:
int nodeIds[] = {0, 1, 2};
nodeSet->setName("Node Set");
nodeSet->setType(XdmfSetType::Node());
nodeSet->insert(nodeIds, nodeIds + 3);
nodeSet->copyValues(0, &nodeIds[0], 3);
// Add Time
boost::shared_ptr<XdmfTime> time = XdmfTime::New(100);
......
......@@ -10,7 +10,6 @@ if __name__ == "__main__":
set.setType(XdmfSetType.Node())
assert(set.getType() == XdmfSetType.Node())
assert(set.size() == 0)
set.insert(1)
#assert(set.size() == 1)
#print set.find("1")
set.copyValueAsInt32(0, 1)
assert(set.size() == 1)
......@@ -288,16 +288,17 @@ boost::shared_ptr<XdmfGrid> XdmfExodusReader::read(const std::string & fileName,
if (num_nodes_in_set > 0)
{
int * node_set_node_list = new int[num_nodes_in_set];
ex_get_node_set(exodusHandle, nodeSetIds[i], node_set_node_list);
boost::shared_ptr<XdmfSet> set = XdmfSet::New();
set->setName(node_set_names[i]);
set->setType(XdmfSetType::Node());
set->initialize(XdmfArrayType::Int32(), num_nodes_in_set);
int * setPointer = (int*)set->getValuesPointer();
ex_get_node_set(exodusHandle, nodeSetIds[i], setPointer);
// Subtract all node ids by 1 since exodus indices start at 1
for(unsigned int j=0; j<num_nodes_in_set; ++j)
{
set->insert(node_set_node_list[j] - 1);
setPointer[i]--;
}
toReturn->insert(set);
......@@ -307,8 +308,6 @@ boost::shared_ptr<XdmfGrid> XdmfExodusReader::read(const std::string & fileName,
set->accept(heavyDataWriter);
set->release();
}
delete [] node_set_node_list;
}
delete [] node_set_names[i];
}
......
......@@ -409,11 +409,11 @@ void XdmfExodusWriter::write(const std::string & filePath, const boost::shared_p
{
ex_put_side_set_param(exodusHandle, setId + i, numValues, 0);
int * values = new int[numValues];
unsigned int k=0;
for(XdmfSet::const_iterator iter = currSet->begin(); iter != currSet->end(); ++iter, ++k)
currSet->getValuesCopy(0, values, numValues);
for(unsigned int k=0; k<numValues; ++k)
{
// Add 1 to xdmf ids because exodus ids begin at 1
values[k] = *iter + 1;
values[k]++;
}
ex_put_side_set(exodusHandle, setId + i, values, NULL);
ex_put_name(exodusHandle, EX_SIDE_SET, setId + i, name.c_str());
......@@ -423,11 +423,11 @@ void XdmfExodusWriter::write(const std::string & filePath, const boost::shared_p
{
ex_put_node_set_param(exodusHandle, setId + i, numValues, 0);
int * values = new int[numValues];
unsigned int k=0;
for(XdmfSet::const_iterator iter = currSet->begin(); iter != currSet->end(); ++iter, ++k)
currSet->getValuesCopy(0, values, numValues);
for(unsigned int k=0; k<numValues; ++k)
{
// Add 1 to xdmf ids because exodus ids begin at 1
values[k] = *iter + 1;
values[k]++;
}
ex_put_node_set(exodusHandle, setId + i, values);
ex_put_name(exodusHandle, EX_NODE_SET, setId + i, name.c_str());
......
......@@ -355,24 +355,24 @@ boost::shared_ptr<XdmfGridCollection> XdmfPartitioner::partition(const boost::sh
if(currSet->getType() == XdmfSetType::Cell() || currSet->getType() == XdmfSetType::Face() || currSet->getType() == XdmfSetType::Edge())
{
for(XdmfSet::const_iterator iter = currSet->begin(); iter != currSet->end(); ++iter)
for(unsigned int k=0; k<currSet->size(); ++k)
{
std::vector<unsigned int>::const_iterator val = std::find(currElemIds.begin(), currElemIds.end(), *iter);
std::vector<unsigned int>::const_iterator val = std::find(currElemIds.begin(), currElemIds.end(), currSet->getValueCopy<unsigned int>(k));
if(val != currElemIds.end())
{
unsigned int valToPush = val - currElemIds.begin();
partitionedSet->insert(valToPush);
partitionedSet->pushBack(valToPush);
}
}
}
else if(currSet->getType() == XdmfSetType::Node())
{
for(XdmfSet::const_iterator iter = currSet->begin(); iter != currSet->end(); ++iter)
for(unsigned int k=0; k<currSet->size(); ++k)
{
std::map<unsigned int, unsigned int>::const_iterator val = currNodeMap.find(*iter);
std::map<unsigned int, unsigned int>::const_iterator val = currNodeMap.find(currSet->getValueCopy<unsigned int>(k));
if(val != currNodeMap.end())
{
partitionedSet->insert(val->second);
partitionedSet->pushBack(val->second);
}
}
}
......
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