XdmfTopology.cpp 5.04 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfTopology.cpp                                                    */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
23

24
#include <sstream>
25
#include "XdmfError.hpp"
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
26
#include "XdmfTopology.hpp"
27
#include "XdmfTopologyType.hpp"
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
28

29
shared_ptr<XdmfTopology>
30
XdmfTopology::New()
31
{
32
  shared_ptr<XdmfTopology> p(new XdmfTopology());
33
  return p;
34 35
}

36
XdmfTopology::XdmfTopology() :
37
  mType(XdmfTopologyType::NoTopologyType())
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
38 39 40 41 42 43 44
{
}

XdmfTopology::~XdmfTopology()
{
}

45
const std::string XdmfTopology::ItemTag = "Topology";
46

47 48
std::string
XdmfTopology::getItemTag() const
49
{
50
  return ItemTag;
51 52
}

53 54
std::map<std::string, std::string>
XdmfTopology::getItemProperties() const
55
{
56 57 58 59 60 61 62 63 64
  std::map<std::string, std::string> topologyProperties;
  mType->getProperties(topologyProperties);
  if(mType->getCellType() != XdmfTopologyType::Structured &&
     mType != XdmfTopologyType::Polyvertex()) {
    std::stringstream numElements;
    numElements << this->getNumberElements();
    topologyProperties["Dimensions"] = numElements.str();
  }
  return topologyProperties;
65 66
}

67 68
unsigned int
XdmfTopology::getNumberElements() const
69
{
70
  // deal with special cases first (mixed / no topology)
71
  if(mType->getNodesPerElement() == 0) {
72 73 74 75 76 77 78
    if(mType == XdmfTopologyType::Mixed()) {
      unsigned int index = 0;
      unsigned int numberElements = 0;
      // iterate over all values in connectivity, pulling topology type ids
      // and counting number of elements
      while(index < this->getSize()) {
        const unsigned int id = this->getValue<unsigned int>(index);
79
        const shared_ptr<const XdmfTopologyType> topologyType =
80 81
          XdmfTopologyType::New(id);
        if(topologyType == NULL) {
82
          XdmfError::message(XdmfError::FATAL,
83 84 85 86
                             "Invalid topology type id found in connectivity "
                             "when parsing mixed topology.");
        }
        if(topologyType == XdmfTopologyType::Polyvertex()) {
87
          const unsigned int numberPolyvertexElements =
88 89 90 91
            this->getValue<unsigned int>(index + 1);
          numberElements += numberPolyvertexElements;
          index += numberPolyvertexElements + 2;
        }
92
        else if(topologyType == XdmfTopologyType::Polyline(0) ||
93
                topologyType == XdmfTopologyType::Polygon(0)) {
94
          const unsigned int numberNodes =
95 96 97 98 99 100 101 102 103 104 105 106
            this->getValue<unsigned int>(index + 1);
          numberElements += 1;
          index += numberNodes + 2;
        }
        else {
          // add 1 to element count and move to next element id
          numberElements += 1;
          index += topologyType->getNodesPerElement() + 1;
        }
      }
      return numberElements;
    }
107 108 109
    return 0;
  }
  return this->getSize() / mType->getNodesPerElement();
110 111
}

112
shared_ptr<const XdmfTopologyType>
113
XdmfTopology::getType() const
114
{
115
  return mType;
116 117
}

118 119
void
XdmfTopology::populateItem(const std::map<std::string, std::string> & itemProperties,
120
                           std::vector<shared_ptr<XdmfItem> > & childItems,
121
                           const XdmfCoreReader * const reader)
122
{
123 124
  XdmfItem::populateItem(itemProperties, childItems, reader);
  mType = XdmfTopologyType::New(itemProperties);
125
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter = childItems.begin();
126 127
      iter != childItems.end();
      ++iter) {
128
    if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
129 130 131
      this->swap(array);
    }
  }
132 133
}

134
void
135
XdmfTopology::setType(const shared_ptr<const XdmfTopologyType> type)
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
136
{
137
  mType = type;
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
138
}