XdmfArray.cpp 39.1 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
{
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
709
  return mName;
710 711
}

Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
712 713 714
XdmfArray::ReadMode
XdmfArray::getReadMode() const
{
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
715
  return mReadMode;
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
716 717
}

718 719
unsigned int
XdmfArray::getSize() const
720
{
721
  return boost::apply_visitor(Size(this), 
722
                              mArray);
723 724
}

725 726
shared_ptr<XdmfArrayReference>
XdmfArray::getReference()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
727
{
728 729 730 731 732 733 734
  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
735 736
}

737 738
void *
XdmfArray::getValuesInternal()
739
{
740 741
  return const_cast<void *>
    (static_cast<const XdmfArray &>(*this).getValuesInternal());
742 743
}

744 745
const void *
XdmfArray::getValuesInternal() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
746
{
747
  return boost::apply_visitor(GetValuesPointer(), 
748
                              mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
749 750
}

751 752
std::string
XdmfArray::getValuesString() const
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
753
{
754
  return boost::apply_visitor(GetValuesString(mArrayPointerNumValues), 
755
                              mArray);
756 757
}

758 759 760 761 762 763 764 765 766 767
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
{
768
  if (mHeavyDataControllers.size() > 0) {
769 770 771 772 773 774 775
    return mHeavyDataControllers[0];
  }
  else {
    return shared_ptr<XdmfHeavyDataController>();
  }
}

776
void
777
XdmfArray::initialize(const shared_ptr<const XdmfArrayType> & arrayType,
778
                      const unsigned int size)
779
{
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
  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);
  }
807 808 809
  else if(arrayType == XdmfArrayType::String()) {
    this->initialize<std::string>(size);
  }
810 811 812 813
  else if(arrayType == XdmfArrayType::Uninitialized()) {
    this->release();
  }
  else {
814 815
    XdmfError::message(XdmfError::FATAL, 
                       "Array of unsupported type in XdmfArray::initialize");
816 817 818 819
  }
}

void
820
XdmfArray::initialize(const shared_ptr<const XdmfArrayType> & arrayType,
821 822 823 824 825 826 827 828 829 830 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
                      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)
{
860 861 862 863 864
  // 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
865 866
      && startIndex.size() == this->getDimensions().size()
      && this->getDimensions().size() == arrayStride.size())) {
867
    // Pull data from values
868 869 870 871 872 873 874 875 876 877 878 879
    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;
880
    // End when the last index is incremented
881
    while (indexVector[indexVector.size()-1] < 1) {
882
      // Initialize the section of the array you're pulling from
883 884 885
      unsigned int startTotal = 0;
      dimTotal = 1;
      for (unsigned int i = 0; i < values->getDimensions().size(); ++i) {
886 887
        // Stride doesn't factor in to the first dimension
        // Since it's being used with the insert call
888 889 890 891
        if (i == 0) {
          startTotal += valuesStartIndex[i] * dimTotal;
        }
        else {
892 893
          startTotal += valuesStartIndex[i] * dimTotal
                        + valuesStride[i] * dimTotal * indexVector[i-1];
894 895 896
        }
        dimTotal *= values->getDimensions()[i];
      }
897 898 899 900 901 902 903
      // Insert the subsection
      holderArray->insert(holderoffset,
                          values,
                          startTotal,
                          numValues[0],
                          1,
                          valuesStride[0]);
904
      holderoffset+=numValues[0];
905
      // Increment up the vector
906 907 908
      bool increment = true;
      for (unsigned int i = 0; i < indexVector.size() && increment; ++i) {
        indexVector[i]++;
909
        // To keep the loop from breaking at the end
910 911 912 913 914 915 916 917 918 919
        if (i+1 < numValues.size()) {
          if (indexVector[i] >= numValues[i+1]) {
            indexVector[i] = indexVector[i] % numValues[i+1];
          }
          else {
            increment = false;
          }
        }
      }
    }
920 921
    // Values being inserted retrieved
    // Use an variation of the last loop to insert into this array
922 923 924 925 926 927

    indexVector.clear();
    for (unsigned int i = 0; i < this->getDimensions().size(); ++i) {
      indexVector.push_back(0);
    }
    holderoffset = 0;
928
    // End when the last index is incremented
929
    while (indexVector[indexVector.size()-1] < 1) {
930
      // Initialize the section of the array you're pulling from
931 932 933
      unsigned int startTotal = 0;
      dimTotal = 1;
      for (unsigned int i = 0; i < this->getDimensions().size(); ++i) {
934 935 936
        if (i == 0) {
          // Stride doesn't factor in to the first dimension
          // Since it's being used with the insert call
937 938 939 940 941 942 943
          startTotal += startIndex[i] * dimTotal;
        }
        else {
          startTotal += startIndex[i] * dimTotal + arrayStride[i] * dimTotal * indexVector[i-1];
        }
        dimTotal *= this->getDimensions()[i];
      }
944
      // Insert the subsection
945 946
      this->insert(startTotal, holderArray, holderoffset, numInserted[0], arrayStride[0], 1);
      holderoffset+=numInserted[0];
947
      // Increment up the vector
948 949 950
      bool increment = true;
      for (unsigned int i = 0; i < indexVector.size() && increment; ++i) {
        indexVector[i]++;
951 952
        if (i+1 < numInserted.size()) {
          // To keep the loop from breaking at the end
953 954 955 956 957 958 959 960 961 962 963
          if (indexVector[i] >= numInserted[i+1]) {
            indexVector[i] = indexVector[i] % numInserted[i+1];
          }
          else {
            increment = false;
          }
        }
      }
    }
  }
  else {
964 965 966 967
    // Throw an error
    if (!(values->getDimensions().size() == valuesStartIndex.size()
          && valuesStartIndex.size() == numValues.size()
          && numValues.size() == valuesStride.size())) {
968 969 970 971
      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");
972
    }
973
    else if (!(numInserted.size() == startIndex.size()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
974 975
               && startIndex.size() == this->getDimensions().size()
               && this->getDimensions().size() == arrayStride.size())) {
976 977 978 979
      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");
980 981 982 983
    }
  }
}

984 985
bool
XdmfArray::isInitialized() const
986
{
987
  return boost::apply_visitor(IsInitialized(),
988
                                mArray);
989 990
}

991 992
void
XdmfArray::internalizeArrayPointer()
993
{
994
  boost::apply_visitor(InternalizeArrayPointer(this), 
995
                       mArray);
Ken Leiter (Civ ARL/CISD) kleiter's avatar
Ken Leiter (Civ ARL/CISD) kleiter committed
996 997
}

998 999
void
XdmfArray::populateItem(const std::map<std::string, std::string> & itemProperties,
1000
                        const std::vector<shared_ptr<XdmfItem> > & childItems,
1001
                        const XdmfCoreReader * const reader)
1002
{
1003

1004
  // This inserts any XdmfInformation in childItems into the object.
1005 1006
  XdmfItem::populateItem(itemProperties, childItems, reader);

1007
  bool filled = false;
1008

1009 1010 1011
  // Check for Function
  std::map<std::string, std::string>::const_iterator itemType =
    itemProperties.find("ItemType");
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 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
  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++;
        }
      }
1049

1050
      shared_ptr<XdmfFunction> function = XdmfFunction::New(expression, variableMap);
1051

1052 1053 1054
      this->setReference(function);
      this->setReadMode(XdmfArray::Reference);
      filled = true;
1055 1056
    }
  }
1057

1058
  if (!filled) {
1059

1060 1061
    const shared_ptr<const XdmfArrayType> arrayType = 
      XdmfArrayType::New(itemProperties);
1062

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    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;
1086
      }
1087
      else {
1088 1089 1090
        subcontent = splitString.substr(0, barSplit);
        splitString = splitString.substr(barSplit+1);
        barSplit++;
1091
      }
1092 1093
      contentVals.push_back(subcontent);
    }
1094

1095 1096 1097 1098 1099 1100
    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");
1101
    }
1102 1103 1104 1105 1106 1107
   
    boost::tokenizer<> tokens(dimensions->second);
    for(boost::tokenizer<>::const_iterator iter = tokens.begin();
        iter != tokens.end();
        ++iter) {
      mDimensions.push_back(atoi((*iter).c_str()));
1108
    }
1109 1110 1111 1112 1113 1114 1115

    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");
1116
    }
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    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");
        }
1130

1131 1132 1133 1134 1135 1136 1137 1138 1139 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
        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;
1174
      }
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    }
    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);
        }
1189 1190
      }
      else {
1191 1192 1193 1194 1195 1196
        for(boost::tokenizer<boost::char_separator<char> >::const_iterator
              iter = tokens.begin();
            iter != tokens.end();
            ++iter, ++index) {
          this->insert(index, atof((*iter).c_str()));
        }
1197 1198
      }
    }
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    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);
        }
      }
1219

1220 1221 1222 1223 1224 1225
      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());
      }
1226

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
      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");
    }
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
  }

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

1254 1255
void
XdmfArray::read()
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1256 1257 1258 1259 1260 1261
{
  switch (mReadMode)
  {
    case XdmfArray::Controller:
      this->readController();
      break;
1262 1263
    case XdmfArray::Reference:
      this->readReference();
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1264 1265
      break;
    default:
1266 1267
      XdmfError::message(XdmfError::FATAL,
                         "Error: Invalid Read Mode");
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1268 1269 1270 1271 1272
  }
}

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

1324
void
1325
XdmfArray::readReference()
1326
{
1327
  shared_ptr<XdmfArray> tempArray = mReference->read();
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
1328 1329 1330
  this->swap(tempArray);
}

1331 1332
void
XdmfArray::release()
1333
{
1334 1335
  mArray = boost::blank();
  mArrayPointerNumValues = 0;
1336
  mDimensions.clear();
1337 1338
}

1339 1340
void
XdmfArray::reserve(const unsigned int size)
1341
{
1342 1343 1344
  boost::apply_visitor(Reserve(this,
                               size),
                       mArray);
1345