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
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
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
Ken Leiter (Civ ARL/CISD) kleiter committed
37
#include "XdmfVisitor.hpp"
38
#include "XdmfError.hpp"
Ken Leiter (Civ ARL/CISD) kleiter's avatar
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
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();
  }

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
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
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
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
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
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
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
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
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
Ken Leiter (Civ ARL/CISD) kleiter committed
756
{
757
  return boost::apply_visitor(GetValuesPointer(), 
758
                              mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
759 760
}

761 762
std::string
XdmfArray::getValuesString() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
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);
  }
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
996
{
997
  return boost::apply_visitor(IsInitialized(),
998
                                mArray);
999 1000
}

1001 1002
void
XdmfArray::internalizeArrayPointer()
1003
{
1004
  boost::apply_visitor(InternalizeArrayPointer(this), 
1005
                       mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
1006 1007
}

1008 1009
void
XdmfArray::populateItem(const std::map<std::string, std::string> & itemProperties,
1010
                        const std::vector<shared_ptr<XdmfItem> > & childItems,
1011
                        const XdmfCoreReader * const reader)
1012
{
1013

1014
  // This inserts any XdmfInformation in childItems into the object.
1015 1016
  XdmfItem::populateItem(itemProperties, childItems, reader);

1017
  bool filled = false;
1018

1019 1020 1021
  // Check for Function
  std::map<std::string, std::string>::const_iterator itemType =
    itemProperties.find("ItemType");
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
  if (itemType !=  itemProperties.end()) {
    if (itemType->second.compare("Function") == 0) {
      std::map<std::string, std::string>::const_iterator expressionLocation =
        itemProperties.find("Function");
      if (expressionLocation ==  itemProperties.end()) {
        XdmfError::message(XdmfError::FATAL,
                           "'Function' not found in itemProperties for Function"
                           " ItemType in XdmfArray::populateItem");
      }
      std::string expression = expressionLocation->second;

      // Convert from old format to new Variable format
      // $X -> ValX
      size_t loc = expression.find("$");

      while (loc != std::string::npos) {
        expression.replace(loc, 1, "Val");
        loc = expression.find("$", loc);
     }

      // Create Variable list

      std::map<std::string, shared_ptr<XdmfArray> > variableMap;

      unsigned int variableIndex = 0;
      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)) {
          std::stringstream variableKey;
          variableKey << "Val" << variableIndex;
          variableMap[variableKey.str()] = array;
          variableIndex++;
        }
      }
1059

1060
      shared_ptr<XdmfFunction> function = XdmfFunction::New(expression, variableMap);
1061

1062 1063 1064
      this->setReference(function);
      this->setReadMode(XdmfArray::Reference);
      filled = true;
1065 1066
    }
  }
1067

1068
  if (!filled) {
1069

1070 1071
    const shared_ptr<const XdmfArrayType> arrayType = 
      XdmfArrayType::New(itemProperties);
1072

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
    std::map<std::string, std::string>::const_iterator content =
      itemProperties.find("Content");
    if(content == itemProperties.end()) {
      XdmfError::message(XdmfError::FATAL,
                         "'Content' not found in itemProperties in "
                         "XdmfArray::populateItem");
    }

    unsigned int contentIndex;

    const std::string & contentVal = content->second;

    std::vector<std::string> contentVals;

    // Split the content based on "|" characters
    size_t barSplit = 0;
    std::string splitString(contentVal);
    std::string subcontent;
    while (barSplit != std::string::npos) {
      barSplit = 0;
      barSplit = splitString.find_first_of("|", barSplit);
      if (barSplit == std::string::npos) {
        subcontent = splitString;
1096
      }
1097
      else {
1098 1099 1100
        subcontent = splitString.substr(0, barSplit);
        splitString = splitString.substr(barSplit+1);
        barSplit++;
1101
      }
1102 1103
      contentVals.push_back(subcontent);
    }
1104

1105 1106 1107 1108 1109 1110
    std::map<std::string, std::string>::const_iterator dimensions =
      itemProperties.find("Dimensions");
    if(dimensions == itemProperties.end()) {
      XdmfError::message(XdmfError::FATAL, 
                         "'Dimensions' not found in itemProperties in "
                         "XdmfArray::populateItem");
1111
    }
1112 1113 1114 1115 1116 1117
   
    boost::tokenizer<> tokens(dimensions->second);
    for(boost::tokenizer<>::const_iterator iter = tokens.begin();
        iter != tokens.end();
        ++iter) {
      mDimensions.push_back(atoi((*iter).c_str()));
1118
    }
1119 1120 1121 1122 1123 1124 1125

    std::map<std::string, std::string>::const_iterator format =
      itemProperties.find("Format");
    if(format == itemProperties.end()) {
      XdmfError::message(XdmfError::FATAL, 
                         "'Format' not found in itemProperties in "
                         "XdmfArray::populateItem");
1126
    }
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
    const std::string & formatVal = format->second;

    if(formatVal.compare("HDF") == 0) {
      contentIndex = 0;
      int contentStep = 2;
      while (contentIndex < contentVals.size()) {
        size_t colonLocation = contentVals[contentIndex].find(":");
        if(colonLocation == std::string::npos) {
          XdmfError::message(XdmfError::FATAL, 
                             "':' not found in content in "
                             "XdmfArray::populateItem -- double check an HDF5 "
                             "data set is specified for the file");
        }
1140

1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
        std::string hdf5Path = 
          contentVals[contentIndex].substr(0, colonLocation);
        std::string dataSetPath = 
          contentVals[contentIndex].substr(colonLocation+1);

        hdf5Path = getFullHeavyDataPath(hdf5Path,
                                        itemProperties);

        // Parse dimensions from the content
        std::vector<unsigned int> contentDims;
        if (contentVals.size() > contentIndex+1) {
          // This is the string that contains the dimensions
          boost::tokenizer<> dimtokens(contentVals[contentIndex+1]);
          for(boost::tokenizer<>::const_iterator iter = dimtokens.begin();
              iter != dimtokens.end();
              ++iter) {
            contentDims.push_back(atoi((*iter).c_str()));
          }
          contentStep = 2;
          // If this works then the dimension content should be skipped over
        }
        else {
          // If it fails then it means that the next content is not a dimension string
          // In this case it is assumed that the controller will have
          // dimensions equal to the array
          for (unsigned int j = 0; j < mDimensions.size(); ++j) {
            contentDims.push_back(mDimensions[j]);
          }
          contentStep = 1;
        }

        mHeavyDataControllers.push_back(
          XdmfHDF5Controller::New(hdf5Path,
                                  dataSetPath,
                                  arrayType,
                                  std::vector<unsigned int>(contentDims.size(),
                                                            0),
                                  std::vector<unsigned int>(contentDims.size(),
                                                            1),
                                  contentDims,
                                  contentDims)
          );
        contentIndex += contentStep;
1184
      }
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
    }
    else if(formatVal.compare("XML") == 0) {
      this->initialize(arrayType,
                       mDimensions);
      unsigned int index = 0;
      boost::char_separator<char> sep(" \t\n");
      boost::tokenizer<boost::char_separator<char> > tokens(contentVals[0], sep);
      if(arrayType == XdmfArrayType::String()) {
        for(boost::tokenizer<boost::char_separator<char> >::const_iterator
              iter = tokens.begin();
            iter != tokens.end();
            ++iter, ++index) {
          this->insert(index, *iter);
        }
1199 1200
      }
      else {
1201 1202 1203 1204 1205 1206
        for(boost::tokenizer<boost::char_separator<char> >::const_iterator
              iter = tokens.begin();
            iter != tokens.end();
            ++iter, ++index) {
          this->insert(index, atof((*iter).c_str()));
        }
1207 1208
      }
    }
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
    else if(formatVal.compare("Binary") == 0) {

      XdmfBinaryController::Endian endian = XdmfBinaryController::NATIVE;
      std::map<std::string, std::string>::const_iterator endianIter =
        itemProperties.find("Endian");
      if(endianIter != itemProperties.end()) {
        if(endianIter->second.compare("Big") == 0) {
          endian =  XdmfBinaryController::BIG;
        }
        else if(endianIter->second.compare("Little") == 0) {
          endian =  XdmfBinaryController::LITTLE;
        }
        else if(endianIter->second.compare("Native") == 0) {
          endian =  XdmfBinaryController::NATIVE;
        }
        else {
          XdmfError(XdmfError::FATAL,
                    "Invalid endianness type: " + endianIter->second);
        }
      }
1229

1230 1231 1232 1233 1234 1235
      unsigned int seek = 0;
      std::map<std::string, std::string>::const_iterator seekIter =
        itemProperties.find("Seek");
      if(seekIter != itemProperties.end()) {
        seek = std::atoi(seekIter->second.c_str());
      }
1236

1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
      const std::string binaryPath = getFullHeavyDataPath(contentVals[0],
                                                          itemProperties);

      mHeavyDataControllers.push_back(XdmfBinaryController::New(binaryPath,
                                                                arrayType,
                                                                endian,
                                                                seek,
                                                                mDimensions));
    }
    else {
      XdmfError::message(XdmfError::FATAL, 
                         "Neither 'HDF' nor 'XML' specified as 'Format' "
                         "in XdmfArray::populateItem");
    }
1251

1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
  }

  std::map<std::string, std::string>::const_iterator name =
    itemProperties.find("Name");
  if(name != itemProperties.end()) {
    mName = name->second;
  }
  else {
    mName = "";
  }
1262 1263
}

1264 1265
void
XdmfArray::read()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1266 1267 1268 1269 1270 1271
{
  switch (mReadMode)
  {
    case XdmfArray::Controller:
      this->readController();
      break;
1272 1273
    case XdmfArray::Reference:
      this->readReference();
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1274 1275
      break;
    default:
1276 1277
      XdmfError::message(XdmfError::FATAL,
                         "Error: Invalid Read Mode");
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1278 1279 1280 1281 1282
  }
}

void
XdmfArray::readController()
1283
{
1284
  if(mHeavyDataControllers.size() > 1) {
1285
    this->release();
1286
    for (unsigned int i = 0; i < mHeavyDataControllers.size(); ++i) {
1287 1288
      shared_ptr<XdmfArray> tempArray = XdmfArray::New();
      mHeavyDataControllers[i]->read(tempArray.get());
1289
      unsigned int dimTotal = 1;
1290
      for (unsigned int j = 0; j < mHeavyDataControllers[i]->getDimensions().size(); ++j) {
1291 1292
        dimTotal *= mHeavyDataControllers[i]->getDimensions()[j];
      }
1293 1294 1295 1296
      this->insert(mHeavyDataControllers[i]->getArrayOffset(), tempArray, 0, dimTotal, 1, 1);
    }
    std::vector<unsigned int> returnDimensions;
    std::vector<unsigned int> tempDimensions;
1297
    // Find the controller with the most dimensions
1298
    int dimControllerIndex = 0;
1299
    unsigned int dimSizeMax = 0;
1300
    unsigned int dimTotal = 0;
1301
    for (unsigned int i = 0; i < mHeavyDataControllers.size(); ++i) {
1302 1303 1304 1305 1306 1307
        dimTotal += mHeavyDataControllers[i]->getSize();
        if (mHeavyDataControllers[i]->getSize() > dimSizeMax) {
          dimSizeMax = mHeavyDataControllers[i]->getSize();
          dimControllerIndex = i;
        }
    }
1308
    // Total up the size of the lower dimensions
1309
    int controllerDimensionSubtotal = 1;
1310 1311 1312
    for (unsigned int i = 0;
         i < mHeavyDataControllers[dimControllerIndex]->getDimensions().size() - 1;
         ++i) {
1313 1314
      returnDimensions.push_back(mHeavyDataControllers[dimControllerIndex]->getDimensions()[i]);
      controllerDimensionSubtotal *= mHeavyDataControllers[dimControllerIndex]->getDimensions()[i];
1315
    }
1316
    // Divide the total contained by the dimensions by the size of the lower dimensions
1317 1318
    returnDimensions.push_back(dimTotal/controllerDimensionSubtotal);
    mDimensions = returnDimensions;
1319
  }
1320
  else if (mHeavyDataControllers.size() == 1 && mHeavyDataControllers[0]->getArrayOffset() == 0) {
1321 1322 1323 1324
    this->release();
    mHeavyDataControllers[0]->read(this);
    mDimensions = mHeavyDataControllers[0]->getDimensions();
  }
1325 1326 1327 1328 1329 1330 1331
  else if (mHeavyDataControllers.size() == 1 && mHeavyDataControllers[0]->getArrayOffset() > 0) {
    this->release();
    shared_ptr<XdmfArray> tempArray = XdmfArray::New();
    mHeavyDataControllers[0]->read(tempArray.get());
    this->insert(mHeavyDataControllers[0]->getArrayOffset(), tempArray, 0, mHeavyDataControllers[0]->getSize(), 1, 1);
    mDimensions = mHeavyDataControllers[0]->getDimensions();
  }
1332 1333
}

1334
void
1335
XdmfArray::readReference()
1336
{
1337
  shared_ptr<XdmfArray> tempArray = mReference->read();
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1338 1339 1340
  this->swap(tempArray);
}

1341 1342
void
XdmfArray::release()
1343
{
1344 1345
  mArray = boost::blank();
  mArrayPointerNumValues = 0;
1346
  mDimensions.clear();
1347 1348
}

1349 1350
void
XdmfArray::reserve(const unsigned int size)
1351
{
1352 1353 1354
  boost::apply_visitor(Reserve(this,
                               size),
                       mArray);
1355 1356
}

Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
void
XdmfArray::setHeavyDataController(shared_ptr<XdmfHeavyDataController> newController)
{
  // Since this is replacing the previous version which was designed to
  // completely replace the controller of the array
  // It will clear the current controllers before adding the new one in
  mHeavyDataControllers.clear();
  mHeavyDataControllers.push_back(newController);