XdmfArray.cpp 39.4 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 : XdmfArray.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 ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
23

24
#include <boost/tokenizer.hpp>
25
#include <limits>
26
#include <sstream>
27
#include <utility>
28 29
#include <stack>
#include <math.h>
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
30
#include "XdmfArray.hpp"
31
#include "XdmfArrayType.hpp"
32
#include "XdmfArrayReference.hpp"
33
#include "XdmfBinaryController.hpp"
34
#include "XdmfFunction.hpp"
35
#include "XdmfHDF5Controller.hpp"
36
#include "XdmfHeavyDataController.hpp"
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
37
#include "XdmfVisitor.hpp"
38
#include "XdmfError.hpp"
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
39

40 41 42 43 44 45 46 47 48 49 50 51 52
namespace {

  std::string
  getFullHeavyDataPath(const std::string & filePath,
                       const std::map<std::string, std::string> & itemProperties)
  {
    // FIXME: for other OS (e.g. windows)
    if(filePath.size() > 0 && filePath[0] != '/') {
      // Dealing with a relative path for heavyData location
      std::map<std::string, std::string>::const_iterator xmlDir =
        itemProperties.find("XMLDir");
      if(xmlDir == itemProperties.end()) {
        XdmfError::message(XdmfError::FATAL,
53 54
                           "'XMLDir' not found in itemProperties when "
                           "building full heavy data path");
55 56 57 58 59 60 61 62 63
      }
      std::stringstream newHeavyDataPath;
      newHeavyDataPath << xmlDir->second << filePath;
      return newHeavyDataPath.str();
    }
    return filePath;
  }
  
}
64

65 66 67 68 69
XDMF_CHILDREN_IMPLEMENTATION(XdmfArray,
                             XdmfHeavyDataController,
                             HeavyDataController,
                             Name)

70
class XdmfArray::Clear : public boost::static_visitor<void> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
71
public:
72

73 74 75 76 77 78
  Clear(XdmfArray * const array) :
    mArray(array)
  {
  }

  void
79
  operator()(const boost::blank & array) const
80
  {
81
    return;
82 83 84 85
  }

  template<typename T>
  void
86
  operator()(const shared_ptr<std::vector<T> > & array) const
87
  {
88
    array->clear();
89
  }
90 91 92 93 94 95 96 97 98 99

  template<typename T>
  void
  operator()(const boost::shared_array<const T> & array) const
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

100
private: 
101
  XdmfArray * const mArray;
102 103
};

104
class XdmfArray::Erase : public boost::static_visitor<void> {
105 106
public:

107 108 109
  Erase(XdmfArray * const array,
        const unsigned int index) :
    mArray(array),
110 111 112
    mIndex(index)
  {
  }
113

114
  void
115
  operator()(const boost::blank & array) const
116 117 118 119
  {
    return;
  }

120 121
  template<typename T>
  void
122
  operator()(const shared_ptr<std::vector<T> > & array) const
123 124 125
  {
    array->erase(array->begin() + mIndex);
  }
126

127 128
  template<typename T>
  void
129
  operator()(const boost::shared_array<const T> & array) const
130 131 132 133 134 135
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

136 137
private:

138
  XdmfArray * const mArray;
139
  const unsigned int mIndex;
140 141
};

142
class XdmfArray::GetArrayType :
143
  public boost::static_visitor<shared_ptr<const XdmfArrayType> > {
144 145
public:

146 147
  GetArrayType(const shared_ptr<XdmfHeavyDataController> & heavyDataController) :
    mHeavyDataController(heavyDataController)
148 149 150
  {
  }

151
  shared_ptr<const XdmfArrayType>
152 153 154 155 156
  getArrayType(const char * const) const
  {
    return XdmfArrayType::Int8();
  }

157
  shared_ptr<const XdmfArrayType>
158 159 160 161 162
  getArrayType(const short * const) const
  {
    return XdmfArrayType::Int16();
  }

163
  shared_ptr<const XdmfArrayType>
164 165 166 167 168
  getArrayType(const int * const) const
  {
    return XdmfArrayType::Int32();
  }

169
  shared_ptr<const XdmfArrayType>
170 171 172 173 174
  getArrayType(const long * const) const
  {
    return XdmfArrayType::Int64();
  }

175
  shared_ptr<const XdmfArrayType>
176 177 178 179 180
  getArrayType(const float * const) const
  {
    return XdmfArrayType::Float32();
  }

181
  shared_ptr<const XdmfArrayType>
182 183 184 185 186
  getArrayType(const double * const) const
  {
    return XdmfArrayType::Float64();
  }

187
  shared_ptr<const XdmfArrayType>
188 189 190 191 192
  getArrayType(const unsigned char * const) const
  {
    return XdmfArrayType::UInt8();
  }

193
  shared_ptr<const XdmfArrayType>
194 195 196 197 198
  getArrayType(const unsigned short * const) const
  {
    return XdmfArrayType::UInt16();
  }

199
  shared_ptr<const XdmfArrayType>
200 201 202 203 204
  getArrayType(const unsigned int * const) const
  {
    return XdmfArrayType::UInt32();
  }

Kenneth Leiter's avatar
Kenneth Leiter committed
205 206 207 208 209 210
  shared_ptr<const XdmfArrayType>
  getArrayType(const std::string * const) const
  {
    return XdmfArrayType::String();
  }

211
  shared_ptr<const XdmfArrayType>
212
  operator()(const boost::blank & array) const
213
  {
214 215
    if(mHeavyDataController) {
      return mHeavyDataController->getType();
216 217 218 219
    }
    return XdmfArrayType::Uninitialized();
  }

220
  template<typename T>
221 222
  shared_ptr<const XdmfArrayType>
  operator()(const shared_ptr<std::vector<T> > & array) const
223 224 225 226 227
  {
    return this->getArrayType(&(array.get()->operator[](0)));
  }

  template<typename T>
228
  shared_ptr<const XdmfArrayType>
229 230 231 232
  operator()(const boost::shared_array<const T> & array) const
  {
    return this->getArrayType(array.get());
  }
233 234 235 236

private:

  const shared_ptr<XdmfHeavyDataController> mHeavyDataController;
237 238 239 240 241
};

class XdmfArray::GetCapacity : public boost::static_visitor<unsigned int> {
public:

242 243 244
  GetCapacity()
  {
  }
245

246
  unsigned int
247
  operator()(const boost::blank & array) const
248 249 250 251
  {
    return 0;
  }

252 253
  template<typename T>
  unsigned int
254
  operator()(const shared_ptr<std::vector<T> > & array) const
255 256 257
  {
    return array->capacity();
  }
258 259 260

  template<typename T>
  unsigned int
261
  operator()(const boost::shared_array<const T> & array) const
262 263 264
  {
    return 0;
  }
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
265 266
};

267
class XdmfArray::GetValuesPointer :
268
  public boost::static_visitor<const void *> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
269 270
public:

271 272 273 274
  GetValuesPointer()
  {
  }

275
  const void *
276
  operator()(const boost::blank & array) const
277 278 279 280
  {
    return NULL;
  }

281
  template<typename T>
282
  const void *
283
  operator()(const shared_ptr<std::vector<T> > & array) const
284 285 286 287 288
  {
    return &array->operator[](0);
  }

  template<typename T>
289
  const void *
290 291 292 293
  operator()(const boost::shared_array<const T> & array) const
  {
    return array.get();
  }
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
294 295
};

296
class XdmfArray::GetValuesString : public boost::static_visitor<std::string> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
297 298
public:

299 300 301 302 303
  GetValuesString(const int arrayPointerNumValues) :
    mArrayPointerNumValues(arrayPointerNumValues)
  {
  }

304
  template<typename T, typename U>
305
  std::string
306
  getValuesString(const T * const array,
307 308
                  const int numValues) const
  {
309
    const int lastIndex = numValues - 1;
310 311 312 313 314

    if(lastIndex < 0) {
      return "";
    }

315
    std::stringstream toReturn;
316
    toReturn.precision(std::numeric_limits<U>::digits10 + 2);
317
    for(int i=0; i<lastIndex; ++i) {
318
      toReturn << (U)array[i] << " ";
319
    }
320
    toReturn << (U)array[lastIndex];
321 322 323
    return toReturn.str();
  }

324 325 326 327 328 329 330
  std::string
  getValuesString(const char * const array,
                  const int numValues) const
  {
    return getValuesString<char, int>(array, numValues);
  }

331 332 333 334
  std::string
  getValuesString(const unsigned char * const array,
                  const int numValues) const
  {
335
    return getValuesString<unsigned char, int>(array, numValues);
336 337 338 339 340 341 342
  }

  template<typename T>
  std::string
  getValuesString(const T * const array,
                  const int numValues) const
  {
343
    return getValuesString<T, T>(array, numValues);
344 345
  }

346
  std::string
347
  operator()(const boost::blank & array) const
348 349 350 351
  {
    return "";
  }

352 353
  template<typename T>
  std::string
354
  operator()(const shared_ptr<std::vector<T> > & array) const
355 356 357 358 359 360 361 362 363 364
  {
    return getValuesString(&(array->operator[](0)), array->size());
  }

  template<typename T>
  std::string
  operator()(const boost::shared_array<const T> & array) const
  {
    return getValuesString(array.get(), mArrayPointerNumValues);
  }
365 366 367

private:

368
  const unsigned int mArrayPointerNumValues;
369 370
};

371 372 373
class XdmfArray::InsertArray : public boost::static_visitor<void> {
public:

374 375
  InsertArray(XdmfArray * const array,
              const unsigned int startIndex,
376 377 378
              const unsigned int valuesStartIndex,
              const unsigned int numValues,
              const unsigned int arrayStride,
379
              const unsigned int valuesStride,
380 381 382
              std::vector<unsigned int> & dimensions,
              const shared_ptr<const XdmfArray> & arrayToCopy) :
    mArray(array),
383 384 385 386
    mStartIndex(startIndex),
    mValuesStartIndex(valuesStartIndex),
    mNumValues(numValues),
    mArrayStride(arrayStride),
387
    mValuesStride(valuesStride),
388 389
    mDimensions(dimensions),
    mArrayToCopy(arrayToCopy)
390 391 392 393
  {
  }

  void
394
  operator()(const boost::blank & array) const
395 396 397 398 399 400 401 402 403
  {
    mArray->initialize(mArrayToCopy->getArrayType());
    boost::apply_visitor(*this,
                         mArray->mArray);
  }

  template<typename T>
  void
  operator()(const shared_ptr<std::vector<T> > & array) const
404
  {
405
    unsigned int size = mStartIndex + (mNumValues - 1) * mArrayStride + 1;
406 407
    if(array->size() < size) {
      array->resize(size);
408
      mDimensions.clear();
409
    }
410 411 412 413 414 415 416 417 418 419 420 421 422 423
    mArrayToCopy->getValues(mValuesStartIndex,
                            &(array->operator[](mStartIndex)),
                            mNumValues,
                            mValuesStride,
                            mArrayStride);
  }

  template<typename T>
  void
  operator()(const boost::shared_array<const T> & array) const
  {
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
424
  }
425 426 427

private:

428
  XdmfArray * const mArray;
429 430 431 432 433
  const unsigned int mStartIndex;
  const unsigned int mValuesStartIndex;
  const unsigned int mNumValues;
  const unsigned int mArrayStride;
  const unsigned int mValuesStride;
434
  std::vector<unsigned int> & mDimensions;
435
  const shared_ptr<const XdmfArray> mArrayToCopy;
436 437
};

438
class XdmfArray::InternalizeArrayPointer : public boost::static_visitor<void> {
439 440
public:

441 442 443 444 445
  InternalizeArrayPointer(XdmfArray * const array) :
    mArray(array)
  {
  }

446
  void
447
  operator()(const boost::blank & array) const
448 449 450 451 452 453
  {
    return;
  }

  template<typename T>
  void
454
  operator()(const shared_ptr<std::vector<T> > & array) const
455 456 457 458
  {
    return;
  }

459 460 461 462
  template<typename T>
  void
  operator()(const boost::shared_array<const T> & array) const
  {
463 464 465 466
    const T * const pointer = array.get();
    shared_ptr<std::vector<T> > newArray(new std::vector<T>(pointer,
                                                            pointer + mArray->mArrayPointerNumValues));
    mArray->mArray = newArray;
467 468
    mArray->mArrayPointerNumValues = 0;
  }
469 470 471

private:

472
  XdmfArray * const mArray;
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
473 474
};

475 476 477 478 479 480 481 482
class XdmfArray::IsInitialized : public boost::static_visitor<bool> {
public:

  IsInitialized()
  {
  }

  bool
483
  operator()(const boost::blank &) const
484 485 486 487 488 489
  {
    return false;
  }

  template<typename T>
  bool
490
  operator()(const shared_ptr<std::vector<T> > &) const
491 492 493 494 495 496
  {
    return true;
  }

  template<typename T>
  bool
497
  operator()(const T &) const
498 499 500 501 502
  {
    return true;
  }
};

503
class XdmfArray::Reserve : public boost::static_visitor<void> {
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
504 505
public:

506 507 508 509
  Reserve(XdmfArray * const array,
          const unsigned int size):
    mArray(array),
    mSize(size)
510 511 512 513
  {
  }

  void
514
  operator()(const boost::blank & array) const
515
  {
516
    mArray->mTmpReserveSize = mSize;
517
  }
518

519 520 521
  template<typename T>
  void
  operator()(shared_ptr<std::vector<T> > & array) const
522
  {
523
    array->reserve(mSize);
524
  }
525

526 527
  template<typename T>
  void
528
  operator()(const boost::shared_array<const T> & array) const
529
  {
530 531 532
    mArray->internalizeArrayPointer();
    boost::apply_visitor(*this,
                         mArray->mArray);
533
  }
534 535 536

private:

537
  XdmfArray * const mArray;
538
  const unsigned int mSize;
539 540
};

541 542 543
class XdmfArray::Size : public boost::static_visitor<unsigned int> {
public:

544 545 546 547 548 549
  Size(const XdmfArray * const array) :
    mArray(array)
  {
  }

  unsigned int
550
  operator()(const boost::blank & array) const
551
  {
552 553 554
    unsigned int total = 0;
    for (unsigned int i = 0; i < mArray->mHeavyDataControllers.size(); ++i) {
      total += mArray->mHeavyDataControllers[i]->getSize();
555
    }
556
    return total;
557
  }
558

559 560
  template<typename T>
  unsigned int
561
  operator()(const shared_ptr<std::vector<T> > & array) const
562 563 564
  {
    return array->size();
  }
565 566 567

  template<typename T>
  unsigned int
568
  operator()(const boost::shared_array<const T> & array) const
569 570 571 572 573 574
  {
    return mArray->mArrayPointerNumValues;
  }

private:

575
  const XdmfArray * const mArray; 
576 577
};

578
shared_ptr<XdmfArray>
579
XdmfArray::New()
580
{
581
  shared_ptr<XdmfArray> p(new XdmfArray());
582
  return p;
583 584
}

Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
585
XdmfArray::XdmfArray() :
586 587
  mArrayPointerNumValues(0),
  mName(""),
588
  mTmpReserveSize(0),
589
  mReadMode(XdmfArray::Controller)
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
590 591 592 593 594 595 596
{
}

XdmfArray::~XdmfArray()
{
}

597
const std::string XdmfArray::ItemTag = "DataItem";
598

599 600
void
XdmfArray::clear()
601
{
602
  boost::apply_visitor(Clear(this), 
603 604
                       mArray);
  mDimensions.clear();
605 606
}

607 608
void
XdmfArray::erase(const unsigned int index)
609
{
610 611 612 613
  boost::apply_visitor(Erase(this,
                             index),
                       mArray);
  mDimensions.clear();
614 615
}

616 617
shared_ptr<const XdmfArrayType>
XdmfArray::getArrayType() const
618
{
619 620 621
  if (mHeavyDataControllers.size()>0) {
    return boost::apply_visitor(GetArrayType(mHeavyDataControllers[0]), 
                                mArray);
622
  }
623 624 625
  else {
    return boost::apply_visitor(GetArrayType(shared_ptr<XdmfHDF5Controller>()),
                                mArray);
626
  }
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
}

unsigned int
XdmfArray::getCapacity() const
{
  return boost::apply_visitor(GetCapacity(), 
                              mArray);
}

std::vector<unsigned int>
XdmfArray::getDimensions() const
{
  if(mDimensions.size() == 0) {
    if(!this->isInitialized() && mHeavyDataControllers.size() > 0) {
      std::vector<unsigned int> returnDimensions;
      std::vector<unsigned int> tempDimensions;
643
      // Find the controller with the most dimensions
644 645 646 647 648 649 650 651 652 653
      int dimControllerIndex = 0;
      unsigned int dimSizeMax = 0;
      unsigned int dimTotal = 0;
      for (unsigned int i = 0; i < mHeavyDataControllers.size(); ++i) {
        dimTotal += mHeavyDataControllers[i]->getSize();
        if (mHeavyDataControllers[i]->getSize() > dimSizeMax) {
          dimSizeMax = mHeavyDataControllers[i]->getSize();
          dimControllerIndex = i;
        }
      }
654
      // Total up the size of the lower dimensions
655
      int controllerDimensionSubtotal = 1;
656 657 658
      for (unsigned int i = 0;
           i < mHeavyDataControllers[dimControllerIndex]->getDimensions().size() - 1;
           ++i) {
659 660 661
        returnDimensions.push_back(mHeavyDataControllers[dimControllerIndex]->getDimensions()[i]);
        controllerDimensionSubtotal *= mHeavyDataControllers[dimControllerIndex]->getDimensions()[i];
      }
662
      // Divide the total contained by the dimensions by the size of the lower dimensions
663 664
      returnDimensions.push_back(dimTotal/controllerDimensionSubtotal);
      return returnDimensions;
665
    }
666 667
    const unsigned int size = this->getSize();
    return std::vector<unsigned int>(1, size);
668
  }
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
  return mDimensions;
}

std::string
XdmfArray::getDimensionsString() const
{
  const std::vector<unsigned int> dimensions = this->getDimensions();
  return GetValuesString(dimensions.size()).getValuesString(&dimensions[0],
                                                            dimensions.size());
}

std::map<std::string, std::string>
XdmfArray::getItemProperties() const
{
  std::map<std::string, std::string> arrayProperties;
684 685
  if(mHeavyDataControllers.size() > 0) {
    mHeavyDataControllers[0]->getProperties(arrayProperties);
686
  }
687 688
  else {
    arrayProperties.insert(std::make_pair("Format", "XML"));
689
  }
690 691 692 693
  arrayProperties.insert(std::make_pair("Dimensions", 
                                        this->getDimensionsString()));
  if(mName.compare("") != 0) {
    arrayProperties.insert(std::make_pair("Name", mName));
694
  }
695
  shared_ptr<const XdmfArrayType> type = this->getArrayType();
696 697
  type->getProperties(arrayProperties);
  return arrayProperties;
698 699
}

700 701
std::string
XdmfArray::getItemTag() const
702
{
703
  return ItemTag;
704 705
}

706 707
std::string
XdmfArray::getName() const
708
{
709 710 711 712 713 714
  if (mName.c_str() == NULL) {
    return "";
  }
  else {
    return mName;
  }
715 716
}

Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
717 718 719
XdmfArray::ReadMode
XdmfArray::getReadMode() const
{
720 721 722 723 724 725
  if (mReadMode) {
    return mReadMode;
  }
  else {
    return XdmfArray::Controller;
  }
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
726 727
}

728 729
unsigned int
XdmfArray::getSize() const
730
{
731
  return boost::apply_visitor(Size(this), 
732
                              mArray);
733 734
}

735 736
shared_ptr<XdmfArrayReference>
XdmfArray::getReference()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
737
{
738 739 740 741 742 743 744
  if (mReference) {
    return mReference;
  }
  else {
    // Returning arbitrary Reference since one isn't defined
    return shared_ptr<XdmfArrayReference>();
  }
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
745 746
}

747 748
void *
XdmfArray::getValuesInternal()
749
{
750 751
  return const_cast<void *>
    (static_cast<const XdmfArray &>(*this).getValuesInternal());
752 753
}

754 755
const void *
XdmfArray::getValuesInternal() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
756
{
757
  return boost::apply_visitor(GetValuesPointer(), 
758
                              mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
759 760
}

761 762
std::string
XdmfArray::getValuesString() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
ENH:  
Ken Leiter (Civ ARL/CISD) kleiter committed
763
{
764
  return boost::apply_visitor(GetValuesString(mArrayPointerNumValues), 
765
                              mArray);
766 767
}

768 769 770 771 772 773 774 775 776 777
shared_ptr<XdmfHeavyDataController>
XdmfArray::getHeavyDataController()
{
  return boost::const_pointer_cast<XdmfHeavyDataController>
    (static_cast<const XdmfArray &>(*this).getHeavyDataController(0));
}

shared_ptr<const XdmfHeavyDataController>
XdmfArray::getHeavyDataController() const
{
778
  if (mHeavyDataControllers.size() > 0) {
779 780 781 782 783 784 785
    return mHeavyDataControllers[0];
  }
  else {
    return shared_ptr<XdmfHeavyDataController>();
  }
}

786
void
787
XdmfArray::initialize(const shared_ptr<const XdmfArrayType> & arrayType,
788
                      const unsigned int size)
789
{
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
  if(arrayType == XdmfArrayType::Int8()) {
    this->initialize<char>(size);
  }
  else if(arrayType == XdmfArrayType::Int16()) {
    this->initialize<short>(size);
  }
  else if(arrayType == XdmfArrayType::Int32()) {
    this->initialize<int>(size);
  }
  else if(arrayType == XdmfArrayType::Int64()) {
    this->initialize<long>(size);
  }
  else if(arrayType == XdmfArrayType::Float32()) {
    this->initialize<float>(size);
  }
  else if(arrayType == XdmfArrayType::Float64()) {
    this->initialize<double>(size);
  }
  else if(arrayType == XdmfArrayType::UInt8()) {
    this->initialize<unsigned char>(size);
  }
  else if(arrayType == XdmfArrayType::UInt16()) {
    this->initialize<unsigned short>(size);
  }
  else if(arrayType == XdmfArrayType::UInt32()) {
    this->initialize<unsigned int>(size);
  }
Kenneth Leiter's avatar
Kenneth Leiter committed
817 818 819
  else if(arrayType == XdmfArrayType::String()) {
    this->initialize<std::string>(size);
  }
820 821 822 823
  else if(arrayType == XdmfArrayType::Uninitialized()) {
    this->release();
  }
  else {
824 825
    XdmfError::message(XdmfError::FATAL, 
                       "Array of unsupported type in XdmfArray::initialize");
826 827 828 829
  }
}

void
830
XdmfArray::initialize(const shared_ptr<const XdmfArrayType> & arrayType,
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
                      const std::vector<unsigned int> & dimensions)
{
  mDimensions = dimensions;
  const unsigned int size = std::accumulate(dimensions.begin(),
                                            dimensions.end(),
                                            1,
                                            std::multiplies<unsigned int>());
  return this->initialize(arrayType, size);
}

void
XdmfArray::insert(const unsigned int startIndex,
                  const shared_ptr<const XdmfArray> values,
                  const unsigned int valuesStartIndex,
                  const unsigned int numValues,
                  const unsigned int arrayStride,
                  const unsigned int valuesStride)
{
  boost::apply_visitor(InsertArray(this,
                                   startIndex,
                                   valuesStartIndex,
                                   numValues,
                                   arrayStride,
                                   valuesStride,
                                   mDimensions,
                                   values),
                       mArray);
}


void
XdmfArray::insert(const std::vector<unsigned int> startIndex,
                  const shared_ptr<const XdmfArray> values,
                  const std::vector<unsigned int> valuesStartIndex,
                  const std::vector<unsigned int> numValues,
                  const std::vector<unsigned int> numInserted,
                  const std::vector<unsigned int> arrayStride,
                  const std::vector<unsigned int> valuesStride)
{
870 871 872 873 874
  // Ensuring dimensions match up when pulling data
  if ((values->getDimensions().size() == valuesStartIndex.size()
      && valuesStartIndex.size() == numValues.size()
      && numValues.size() == valuesStride.size())
      && (numInserted.size() == startIndex.size()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
875 876
      && startIndex.size() == this->getDimensions().size()
      && this->getDimensions().size() == arrayStride.size())) {
877
    // Pull data from values
878 879 880 881 882 883 884 885 886 887 888 889
    std::vector<unsigned int > dimTotalVector;
    unsigned int dimTotal = 1;
    for (unsigned int i = 0; i < values->getDimensions().size(); ++i) {
      dimTotalVector.push_back(dimTotal);
      dimTotal *= values->getDimensions()[i];
    }
    std::vector<unsigned int> indexVector;
    for (unsigned int i = 0; i < values->getDimensions().size(); ++i) {
      indexVector.push_back(0);
    }
    shared_ptr<XdmfArray> holderArray = XdmfArray::New();
    unsigned int holderoffset = 0;
890
    // End when the last index is incremented
891
    while (indexVector[indexVector.size()-1] < 1) {
892
      // Initialize the section of the array you're pulling from
893 894 895
      unsigned int startTotal = 0;
      dimTotal = 1;
      for (unsigned int i = 0; i < values->getDimensions().size(); ++i) {
896 897
        // Stride doesn't factor in to the first dimension
        // Since it's being used with the insert call
898 899 900 901
        if (i == 0) {
          startTotal += valuesStartIndex[i] * dimTotal;
        }
        else {
902 903
          startTotal += valuesStartIndex[i] * dimTotal
                        + valuesStride[i] * dimTotal * indexVector[i-1];
904 905 906
        }
        dimTotal *= values->getDimensions()[i];
      }
907 908 909 910 911 912 913
      // Insert the subsection
      holderArray->insert(holderoffset,
                          values,
                          startTotal,
                          numValues[0],
                          1,
                          valuesStride[0]);
914
      holderoffset+=numValues[0];
915
      // Increment up the vector
916 917 918
      bool increment = true;
      for (unsigned int i = 0; i < indexVector.size() && increment; ++i) {
        indexVector[i]++;
919
        // To keep the loop from breaking at the end
920 921 922 923 924 925 926 927 928 929
        if (i+1 < numValues.size()) {
          if (indexVector[i] >= numValues[i+1]) {
            indexVector[i] = indexVector[i] % numValues[i+1];
          }
          else {
            increment = false;
          }
        }
      }
    }
930 931
    // Values being inserted retrieved
    // Use an variation of the last loop to insert into this array
932 933 934 935 936 937

    indexVector.clear();
    for (unsigned int i = 0; i < this->getDimensions().size(); ++i) {
      indexVector.push_back(0);
    }
    holderoffset = 0;
938
    // End when the last index is incremented
939
    while (indexVector[indexVector.size()-1] < 1) {
940
      // Initialize the section of the array you're pulling from
941 942 943
      unsigned int startTotal = 0;
      dimTotal = 1;
      for (unsigned int i = 0; i < this->getDimensions().size(); ++i) {
944 945 946
        if (i == 0) {
          // Stride doesn't factor in to the first dimension
          // Since it's being used with the insert call
947 948 949 950 951 952 953
          startTotal += startIndex[i] * dimTotal;
        }
        else {
          startTotal += startIndex[i] * dimTotal + arrayStride[i] * dimTotal * indexVector[i-1];
        }
        dimTotal *= this->getDimensions()[i];
      }
954
      // Insert the subsection
955 956
      this->insert(startTotal, holderArray, holderoffset, numInserted[0], arrayStride[0], 1);
      holderoffset+=numInserted[0];
957
      // Increment up the vector
958 959 960
      bool increment = true;
      for (unsigned int i = 0; i < indexVector.size() && increment; ++i) {
        indexVector[i]++;
961 962
        if (i+1 < numInserted.size()) {
          // To keep the loop from breaking at the end
963 964 965 966 967 968 969 970 971 972 973
          if (indexVector[i] >= numInserted[i+1]) {
            indexVector[i] = indexVector[i] % numInserted[i+1];
          }
          else {
            increment = false;
          }
        }
      }
    }
  }
  else {
974 975 976 977
    // Throw an error
    if (!(values->getDimensions().size() == valuesStartIndex.size()
          && valuesStartIndex.size() == numValues.size()
          && numValues.size() == valuesStride.size())) {
978 979 980 981
      XdmfError::message(XdmfError::FATAL,
                         "Error: Number of starts, strides, and/or values "
                         "retrieved does not match up with the dimensions "
                         "of the array being retrieved from");
982
    }
983
    else if (!(numInserted.size() == startIndex.size()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
984 985
               && startIndex.size() == this->getDimensions().size()
               && this->getDimensions().size() == arrayStride.size())) {
986 987 988 989
      XdmfError::message(XdmfError::FATAL,
                         "Error: Number of starts, strides, and/or values "
                         "written does not match up with the dimensions "
                         "of the array being inserted into");
990 991 992 993
    }
  }
}

994 995
bool
XdmfArray::isInitialized() const