XdmfAggregate.cpp 7.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfAggregate.cpp                                                   */
/*                                                                           */
/*  Author:                                                                  */
/*     Andrew Burns                                                          */
/*     andrew.j.burns2@us.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2013 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.                                                 */
/*                                                                           */
/*****************************************************************************/

#include <numeric>
#include <functional>
26
#include <boost/tokenizer.hpp>
27 28 29 30 31 32 33 34 35 36 37 38 39
#include "XdmfArray.hpp"
#include "XdmfError.hpp"
#include "XdmfAggregate.hpp"
#include "XdmfVisitor.hpp"
#include "XdmfWriter.hpp"
#include "string.h"

XDMF_CHILDREN_IMPLEMENTATION(XdmfAggregate, XdmfArray, Array, Name)

XdmfAggregate::XdmfAggregate()
{
}

40 41 42 43 44
XdmfAggregate::XdmfAggregate(XdmfAggregate & refAggregate) :
  XdmfArrayReference(refAggregate)
{
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
XdmfAggregate::~XdmfAggregate()
{
}

const std::string XdmfAggregate::ItemTag = "Aggregate";

shared_ptr<XdmfAggregate>
XdmfAggregate::New()
{
  shared_ptr<XdmfAggregate> p(new XdmfAggregate());
  return p;
}

std::vector<unsigned int> XdmfAggregate::getDimensions() const
{
  std::vector<unsigned int> testDims = mArrays[0]->getDimensions();

  bool isSame = true;

  for(std::vector<shared_ptr<XdmfArray> >::const_iterator iter =
        mArrays.begin();
      iter != mArrays.end() && isSame;
      ++iter) {
    std::vector<unsigned int> compareDims = (*iter)->getDimensions();
    if (compareDims.size() == testDims.size())
    {
      for (unsigned int i = 0; i < testDims.size(); ++i)
      {
        if (compareDims[i] != testDims[i])
        {
          isSame = false;
          break;
        }
      }
    }
    else
    {
      isSame = false;
      break;
    }
  }

  if (isSame)
  {
89
    testDims.push_back(mArrays.size());
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    return testDims;
  }
  else
  {
    std::vector<unsigned int> returnDims;
    returnDims.push_back(this->getSize());
    return returnDims;
  }
}

std::map<std::string, std::string>
XdmfAggregate::getItemProperties() const
{
  std::map<std::string, std::string> aggregateMap = XdmfArrayReference::getItemProperties();

  return aggregateMap;
}

std::string
XdmfAggregate::getItemTag() const
{
  return ItemTag;
}

unsigned int
XdmfAggregate::getSize() const
{
  unsigned int total = 0;
  for(std::vector<shared_ptr<XdmfArray> >::const_iterator iter =
        mArrays.begin();
      iter != mArrays.end();
      ++iter) {
    total += (*iter)->getSize();
  }
  return total;
}

void
XdmfAggregate::populateItem(const std::map<std::string, std::string> & itemProperties,
                            const std::vector<shared_ptr<XdmfItem> > & childItems,
                            const XdmfCoreReader * const reader)
{
  bool placeholderFound = false;
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
        childItems.begin();
      iter != childItems.end();
      ++iter) {
    if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
      if (!placeholderFound) {
        placeholderFound = true;
      }
      else {
        this->insert(array);
      }
/*
      this->swap(array);
      if (array->getReference()) {
        this->setReference(array->getReference());
        this->setReadMode(XdmfArray::Reference);
      }
      break;
*/
    }
  }
}

shared_ptr<XdmfArray>
XdmfAggregate::read() const
{
  shared_ptr<XdmfArray> returnArray = XdmfArray::New();

  if (mArrays.size() > 0)
  {
    if (!mArrays[0]->isInitialized()) {
      mArrays[0]->read();
    }
    returnArray->insert(0, mArrays[0], 0, mArrays[0]->getSize(),  1, 1);
    if (mArrays.size() > 1)
    {
      unsigned int offset = mArrays[0]->getSize();
      for (unsigned int i = 1; i < mArrays.size(); ++i)
      {
        if (!mArrays[i]->isInitialized()) {
          mArrays[i]->read();
        }
        returnArray->insert(offset, mArrays[i], 0, mArrays[i]->getSize(), 1, 1);
        offset += mArrays[i]->getSize();
      }
    }
  }

  return returnArray;
}

void
XdmfAggregate::traverse(const shared_ptr<XdmfBaseVisitor> visitor)
{
  XdmfItem::traverse(visitor);

  bool originalXPath;

  if (shared_ptr<XdmfWriter> writer =
        shared_dynamic_cast<XdmfWriter>(visitor)) {
      originalXPath = writer->getWriteXPaths();
      writer->setWriteXPaths(false);
  }

  shared_ptr<XdmfArray> spacerarray = XdmfArray::New();
  spacerarray->pushBack((int)0);
  spacerarray->accept(visitor);

  if (shared_ptr<XdmfWriter> writer =
        shared_dynamic_cast<XdmfWriter>(visitor)) {
      writer->setWriteXPaths(originalXPath);
  }

  for (unsigned int i = 0; i < mArrays.size(); ++i)
  {
    mArrays[i]->accept(visitor);
  }
}

// C Wrappers

XDMFAGGREGATE * XdmfAggregateNew()
{
216 217 218 219 220 221 222 223 224 225
  try
  {
    shared_ptr<XdmfAggregate> generatedAggregate = XdmfAggregate::New();
    return (XDMFAGGREGATE *)((void *)(new XdmfAggregate(*generatedAggregate.get())));
  }
  catch (...)
  {
    shared_ptr<XdmfAggregate> generatedAggregate = XdmfAggregate::New();
    return (XDMFAGGREGATE *)((void *)(new XdmfAggregate(*generatedAggregate.get())));
  }
226 227 228 229 230
}

XDMFARRAY *
XdmfAggregateGetArray(XDMFAGGREGATE * aggregate, unsigned int index)
{
231
  return (XDMFARRAY *)((void *)(((XdmfAggregate *)(aggregate))->getArray(index).get()));
232 233 234 235 236
}

XDMFARRAY *
XdmfAggregateGetArrayByName(XDMFAGGREGATE * aggregate, char * name)
{
237
  return (XDMFARRAY *)((void *)(((XdmfAggregate *)(aggregate))->getArray(name).get()));
238 239 240 241 242
}

unsigned int
XdmfAggregateGetNumberArrays(XDMFAGGREGATE * aggregate)
{
243
  return ((XdmfAggregate *)(aggregate))->getNumberArrays();
244 245 246 247 248
}

void
XdmfAggregateInsertArray(XDMFAGGREGATE * aggregate, XDMFARRAY * array, int transferOwnership)
{
249 250 251 252 253 254
  if (transferOwnership) {
    ((XdmfAggregate *)(aggregate))->insert(shared_ptr<XdmfArray>((XdmfArray *)array));
  }
  else {
    ((XdmfAggregate *)(aggregate))->insert(shared_ptr<XdmfArray>((XdmfArray *)array, XdmfNullDeleter()));
  }
255 256 257 258 259
}

void
XdmfAggregateRemoveArray(XDMFAGGREGATE * aggregate, unsigned int index)
{
260
  ((XdmfAggregate *)(aggregate))->removeArray(index);
261 262 263 264 265
}

void
XdmfAggregateRemoveArrayByName(XDMFAGGREGATE * aggregate, char * name)
{
266
  ((XdmfAggregate *)(aggregate))->removeArray(name);
267 268 269 270 271 272
}

// C Wrappers for parent classes are generated by macros

XDMF_ITEM_C_CHILD_WRAPPER(XdmfAggregate, XDMFAGGREGATE)
XDMF_ARRAYREFERENCE_C_CHILD_WRAPPER(XdmfAggregate, XDMFAGGREGATE)