Commit e4ed9f66 authored by Kenneth Leiter's avatar Kenneth Leiter

ENH: Add XdmfTime and allow it to be attached to XdmfGrid. Add some...

ENH: Add XdmfTime and allow it to be attached to XdmfGrid.  Add some convenience functions in XdmfGrid
to get and remove XdmfAttributes and XdmfSets by name.
parent 9f1c72b8
......@@ -23,6 +23,7 @@ set(XdmfSources
XdmfReader
XdmfSet
XdmfSetType
XdmfTime
XdmfTopology
XdmfTopologyType
)
......
......@@ -34,6 +34,7 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
#include <XdmfReader.hpp>
#include <XdmfSet.hpp>
#include <XdmfSetType.hpp>
#include <XdmfTime.hpp>
#include <XdmfTopology.hpp>
#include <XdmfTopologyType.hpp>
%}
......@@ -74,6 +75,10 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
{
$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<XdmfTime> time = boost::shared_dynamic_cast<XdmfTime>($1))
{
$result = SWIG_NewPointerObj(SWIG_as_voidptr(new boost::shared_ptr<XdmfTime>(time)), SWIGTYPE_p_boost__shared_ptrT_XdmfTime_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);
......@@ -98,6 +103,7 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
%shared_ptr(XdmfReader)
%shared_ptr(XdmfSet)
%shared_ptr(XdmfSetType)
%shared_ptr(XdmfTime)
%shared_ptr(XdmfTopology)
%shared_ptr(XdmfTopologyType)
......@@ -114,5 +120,6 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i
%include XdmfReader.hpp
%include XdmfSet.hpp
%include XdmfSetType.hpp
%include XdmfTime.hpp
%include XdmfTopology.hpp
%include XdmfTopologyType.hpp
\ No newline at end of file
......@@ -9,10 +9,12 @@
#include "XdmfGrid.hpp"
#include "XdmfGeometry.hpp"
#include "XdmfSet.hpp"
#include "XdmfTime.hpp"
#include "XdmfTopology.hpp"
XdmfGrid::XdmfGrid() :
mGeometry(XdmfGeometry::New()),
mTime(boost::shared_ptr<XdmfTime>()),
mTopology(XdmfTopology::New()),
mName("Grid")
{
......@@ -40,16 +42,16 @@ boost::shared_ptr<const XdmfAttribute> XdmfGrid::getAttribute(const unsigned int
return mAttributes[index];
}
boost::shared_ptr<XdmfAttribute> XdmfGrid::getAttribute(const std::string & attributeName)
boost::shared_ptr<XdmfAttribute> XdmfGrid::getAttribute(const std::string & name)
{
return boost::const_pointer_cast<XdmfAttribute>(static_cast<const XdmfGrid &>(*this).getAttribute(attributeName));
return boost::const_pointer_cast<XdmfAttribute>(static_cast<const XdmfGrid &>(*this).getAttribute(name));
}
boost::shared_ptr<const XdmfAttribute> XdmfGrid::getAttribute(const std::string & attributeName) const
boost::shared_ptr<const XdmfAttribute> XdmfGrid::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(attributeName) == 0)
if((*iter)->getName().compare(name) == 0)
{
return *iter;
}
......@@ -108,6 +110,33 @@ boost::shared_ptr<const XdmfSet> XdmfGrid::getSet(const unsigned int index) cons
return mSets[index];
}
boost::shared_ptr<XdmfSet> XdmfGrid::getSet(const std::string & name)
{
return boost::const_pointer_cast<XdmfSet>(static_cast<const XdmfGrid &>(*this).getSet(name));
}
boost::shared_ptr<const XdmfSet> XdmfGrid::getSet(const std::string & name) const
{
for(std::vector<boost::shared_ptr<XdmfSet> >::const_iterator iter = mSets.begin(); iter != mSets.end(); ++iter)
{
if((*iter)->getName().compare(name) == 0)
{
return *iter;
}
}
return boost::shared_ptr<XdmfSet>();
}
boost::shared_ptr<XdmfTime> XdmfGrid::getTime()
{
return boost::const_pointer_cast<XdmfTime>(static_cast<const XdmfGrid &>(*this).getTime());
}
boost::shared_ptr<const XdmfTime> XdmfGrid::getTime() const
{
return mTime;
}
boost::shared_ptr<XdmfTopology> XdmfGrid::getTopology()
{
return boost::const_pointer_cast<XdmfTopology>(static_cast<const XdmfGrid &>(*this).getTopology());
......@@ -153,6 +182,10 @@ void XdmfGrid::populateItem(const std::map<std::string, std::string> & itemPrope
{
this->insert(set);
}
else if(boost::shared_ptr<XdmfTime> time = boost::shared_dynamic_cast<XdmfTime>(*iter))
{
mTime = time;
}
else if(boost::shared_ptr<XdmfTopology> topology = boost::shared_dynamic_cast<XdmfTopology>(*iter))
{
mTopology = topology;
......@@ -173,6 +206,19 @@ void XdmfGrid::removeAttribute(const unsigned int index)
mAttributes.erase(mAttributes.begin() + index);
}
void XdmfGrid::removeAttribute(const std::string & name)
{
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 XdmfGrid::removeSet(const unsigned int index)
{
if(index >= mSets.size())
......@@ -182,6 +228,19 @@ void XdmfGrid::removeSet(const unsigned int index)
mSets.erase(mSets.begin() + index);
}
void XdmfGrid::removeSet(const std::string & name)
{
for(std::vector<boost::shared_ptr<XdmfSet> >::iterator iter = mSets.begin(); iter != mSets.end(); ++iter)
{
if((*iter)->getName().compare(name) == 0)
{
mSets.erase(iter);
return;
}
}
return;
}
void XdmfGrid::setGeometry(const boost::shared_ptr<XdmfGeometry> geometry)
{
mGeometry = geometry;
......@@ -192,6 +251,11 @@ void XdmfGrid::setName(const std::string & name)
mName = name;
}
void XdmfGrid::setTime(const boost::shared_ptr<XdmfTime> time)
{
mTime = time;
}
void XdmfGrid::setTopology(const boost::shared_ptr<XdmfTopology> topology)
{
mTopology = topology;
......@@ -199,6 +263,10 @@ void XdmfGrid::setTopology(const boost::shared_ptr<XdmfTopology> topology)
void XdmfGrid::traverse(const boost::shared_ptr<XdmfBaseVisitor> visitor) const
{
if(mTime != NULL)
{
mTime->accept(visitor);
}
mGeometry->accept(visitor);
mTopology->accept(visitor);
for(std::vector<boost::shared_ptr<XdmfAttribute> >::const_iterator iter = mAttributes.begin(); iter != mAttributes.end(); ++iter)
......
......@@ -5,6 +5,7 @@
class XdmfAttribute;
class XdmfGeometry;
class XdmfSet;
class XdmfTime;
class XdmfTopology;
// Includes
......@@ -47,18 +48,18 @@ public:
/**
* Get an XdmfAttribute attached to this grid by name.
*
* @param attributeName the name of the XdmfAttribute to retrieve.
* @param name the name of the XdmfAttribute to retrieve.
* @return requested XdmfAttribute, if not found a NULL pointer is returned.
*/
boost::shared_ptr<XdmfAttribute> getAttribute(const std::string & attributeName);
boost::shared_ptr<XdmfAttribute> getAttribute(const std::string & name);
/**
* Get an XdmfAttribute attached to this grid by name (const version).
*
* @param attributeName the name of the XdmfAttribute to retrieve.
* @param name the name of the XdmfAttribute to retrieve.
* @return requested XdmfAttribute, if not found a NULL pointer is returned.
*/
boost::shared_ptr<const XdmfAttribute> getAttribute(const std::string & attributeName) const;
boost::shared_ptr<const XdmfAttribute> getAttribute(const std::string & name) const;
/**
* Get the XdmfGeometry associated with this grid.
......@@ -115,6 +116,36 @@ public:
*/
boost::shared_ptr<const XdmfSet> getSet(const unsigned int index) const;
/**
* Get an XdmfSet attached to this grid by name.
*
* @param name the name of the XdmfSet to retrieve.
* @return requested XdmfSet, if not found a NULL pointer is returned.
*/
boost::shared_ptr<XdmfSet> getSet(const std::string & name);
/**
* Get an XdmfSet attached to this grid by name (const version).
*
* @param name the name of the XdmfSet to retrieve.
* @return requested XdmfSet, if not found a NULL pointer is returned.
*/
boost::shared_ptr<const XdmfSet> getSet(const std::string & name) const;
/**
* Get the XdmfTime attached to this grid.
*
* @return pointer to the XdmfTime attached to this grid. If no XdmfTime is attached, return NULL.
*/
boost::shared_ptr<XdmfTime> getTime();
/**
* Get the XdmfTime attached to this grid (const version).
*
* @return pointer to the XdmfTime attached to this grid. If no XdmfTime is attached, return NULL.
*/
boost::shared_ptr<const XdmfTime> getTime() const;
/**
* Get the XdmfTopology associated with this grid.
*
......@@ -150,6 +181,13 @@ public:
*/
void removeAttribute(const unsigned int index);
/**
* Remove an XdmfAttribute from the grid by name. If no XdmfAttribute having the name is found, no attributes are removed.
*
* @param name of the XdmfAttribute to remove.
*/
void removeAttribute(const std::string & name);
/**
* Remove an XdmfSet from the grid.
*
......@@ -157,6 +195,13 @@ public:
*/
void removeSet(const unsigned int index);
/**
* Remove an XdmfSet from the grid by name. If no XdmfSet having the name is found, no sets are removed.
*
* @param name of the XdmfSet to remove.
*/
void removeSet(const std::string & name);
/**
* Set the XdmfGeometry associated with this grid.
*
......@@ -164,6 +209,13 @@ public:
*/
void setGeometry(const boost::shared_ptr<XdmfGeometry> geometry);
/**
* Set the XdmfTime associated with this grid.
*
* @param time an XdmfTime to attach to this grid.
*/
void setTime(const boost::shared_ptr<XdmfTime> time);
/**
* Set the XdmfTopology associated with this grid.
*
......@@ -195,6 +247,7 @@ private:
std::vector<boost::shared_ptr<XdmfAttribute> > mAttributes;
boost::shared_ptr<XdmfGeometry> mGeometry;
std::vector<boost::shared_ptr<XdmfSet> > mSets;
boost::shared_ptr<XdmfTime> mTime;
boost::shared_ptr<XdmfTopology> mTopology;
};
......
......@@ -7,6 +7,7 @@
#include "XdmfInformation.hpp"
#include "XdmfItemFactory.hpp"
#include "XdmfSet.hpp"
#include "XdmfTime.hpp"
#include "XdmfTopology.hpp"
XdmfItemFactory::XdmfItemFactory()
......@@ -56,6 +57,10 @@ boost::shared_ptr<XdmfItem> XdmfItemFactory::createItem(const std::string & item
{
newItem = XdmfSet::New();
}
else if(itemTag.compare(XdmfTime::ItemTag) == 0)
{
newItem = XdmfTime::New();
}
else if(itemTag.compare(XdmfTopology::ItemTag) == 0)
{
newItem = XdmfTopology::New();
......
/*
* XdmfTime.cpp
*
* Created on: Jan 29, 2010
* Author: kleiter
*/
#include <sstream>
#include "XdmfTime.hpp"
XdmfTime::XdmfTime(const double & value) :
mValue(value)
{
std::cout << "Created Time " << this << std::endl;
}
XdmfTime::~XdmfTime()
{
std::cout << "Deleted Time " << this << std::endl;
}
const std::string XdmfTime::ItemTag = "Time";
std::map<std::string, std::string> XdmfTime::getItemProperties() const
{
std::map<std::string, std::string> timeProperties;
std::stringstream value;
value << mValue;
timeProperties["Value"] = value.str();
return timeProperties;
}
std::string XdmfTime::getItemTag() const
{
return ItemTag;
}
double XdmfTime::getValue() const
{
return mValue;
}
void XdmfTime::populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems)
{
std::map<std::string, std::string>::const_iterator value = itemProperties.find("Value");
if(value != itemProperties.end())
{
mValue = atof(value->second.c_str());
}
else
{
assert(false);
}
}
void XdmfTime::setValue(const double & value)
{
mValue = value;
}
#ifndef XDMFTIME_HPP_
#define XDMFTIME_HPP_
// Includes
#include "XdmfItem.hpp"
/**
* @brief Time specification for an XdmfGrid Item.
*
* An XdmfTime sets a time value for an XdmfGrid item.
*/
class XdmfTime : public XdmfItem {
public:
/**
* Create a new XdmfTime.
*
* @param timeValue the value of the XdmfTime to create.
* @return the new XdmfTime.
*/
static boost::shared_ptr<XdmfTime> New(const double & value = 0)
{
boost::shared_ptr<XdmfTime> p(new XdmfTime(value));
return p;
}
virtual ~XdmfTime();
LOKI_DEFINE_VISITABLE(XdmfTime, XdmfItem)
static const std::string ItemTag;
std::map<std::string, std::string> getItemProperties() const;
std::string getItemTag() const;
/**
* Get the time value associated with this XdmfTime.
*
* @return a double containing the time value.
*/
double getValue() const;
/**
* Set the time value associated with this XdmfTime.
*
* @return a double containing the time value.
*/
void setValue(const double & time);
protected:
XdmfTime(const double & value);
virtual void populateItem(const std::map<std::string, std::string> & itemProperties, std::vector<boost::shared_ptr<XdmfItem> > & childItems);
private:
XdmfTime(const XdmfTime & time); // Not implemented.
void operator=(const XdmfTime & time); // Not implemented.
double mValue;
};
#endif /* XDMFTIME_HPP_ */
......@@ -5,6 +5,7 @@ set(XdmfCxxTests
TestXdmfGridCollection
TestXdmfReader
TestXdmfSet
TestXdmfTime
TestXdmfTopology
TestXdmfVisitorValueCounter
TestXdmfWriter
......
......@@ -12,21 +12,36 @@ int main(int argc, char* argv[])
std::cout << grid->getName() << std::endl;
boost::shared_ptr<XdmfAttribute> attr = XdmfAttribute::New();
attr->setName("foo");
assert(grid->getNumberOfAttributes() == 0);
grid->insert(attr);
assert(grid->getNumberOfAttributes() == 1);
assert(grid->getAttribute(0) == attr);
assert(grid->getAttribute("foo1") == NULL);
assert(grid->getAttribute("foo") == attr);
boost::shared_ptr<XdmfSet> set = XdmfSet::New();
set->setName("foo");
assert(grid->getNumberOfSets() == 0);
grid->insert(set);
assert(grid->getNumberOfSets() == 1);
assert(grid->getSet(0) == set);
assert(grid->getSet("foo1") == NULL);
assert(grid->getSet("foo") == set);
grid->insert(attr);
grid->insert(set);
assert(grid->getNumberOfAttributes() == 2);
assert(grid->getNumberOfSets() == 2);
grid->removeAttribute(0);
grid->removeSet(0);
assert(grid->getNumberOfAttributes() == 1);
assert(grid->getNumberOfSets() == 1);
grid->removeAttribute("foo");
grid->removeSet("foo");
assert(grid->getNumberOfAttributes() == 0);
assert(grid->getNumberOfSets() == 0);
return 0;
}
......@@ -39,6 +39,5 @@ int main(int argc, char* argv[])
assert(firstContents.compare(secondContents) == 0);
return 0;
}
#include "XdmfTime.hpp"
int main(int argc, char* argv[])
{
boost::shared_ptr<XdmfTime> time = XdmfTime::New();
assert(time->getValue() == 0);
time->setValue(50);
boost::shared_ptr<XdmfTime> time2 = XdmfTime::New(50);
assert(time->getValue() == time2->getValue());
return 0;
}
......@@ -8,6 +8,7 @@
#include "XdmfGrid.hpp"
#include "XdmfSet.hpp"
#include "XdmfSetType.hpp"
#include "XdmfTime.hpp"
#include "XdmfTopology.hpp"
#include "XdmfTopologyType.hpp"
......@@ -22,6 +23,7 @@ public:
* Nodal Attributes = 1
* Number of Sets = 1
* Nodal Set = 1
* Time = 100
* Total Number of Values = 69
*/
static boost::shared_ptr<XdmfGrid> createHexahedron()
......@@ -64,6 +66,10 @@ public:
nodeSet->setSetType(XdmfSetType::Node());
nodeSet->getArray()->copyValues(0, &nodeIds[0], 3);
// Add time
boost::shared_ptr<XdmfTime> time = XdmfTime::New(100);
grid->setTime(time);
grid->insert(nodalAttribute);
grid->insert(cellAttribute);
grid->insert(nodeSet);
......
......@@ -34,6 +34,7 @@ swig -v -c++ -python -o XdmfUtilsPython.cpp XdmfUtils.i
#include <XdmfReader.hpp>
#include <XdmfSet.hpp>
#include <XdmfSetType.hpp>
#include <XdmfTime.hpp>
#include <XdmfTopology.hpp>
#include <XdmfTopologyType.hpp>
......
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