XdmfTopologyConverter.cpp 29.7 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 23
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfTopologyConverter.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.                                                 */
/*                                                                           */
/*****************************************************************************/

24
#include <cmath>
25 26 27
#include "XdmfAttribute.hpp"
#include "XdmfAttributeCenter.hpp"
#include "XdmfAttributeType.hpp"
28 29
#include "XdmfGeometry.hpp"
#include "XdmfGeometryType.hpp"
30
#include "XdmfHeavyDataWriter.hpp"
31 32
#include "XdmfSet.hpp"
#include "XdmfSetType.hpp"
33 34
#include "XdmfTopology.hpp"
#include "XdmfTopologyConverter.hpp"
35
#include "XdmfTopologyType.hpp"
36
#include "XdmfUnstructuredGrid.hpp"
37
#include "XdmfError.hpp"
38

39 40 41 42 43
//
// local methods
//
namespace {

44 45 46 47
  // Classes that perform topology conversions. Converter is the root
  // base class.  Tessellator is a subclass of Converter that deals
  // with cases where the mesh only needs to be tessellated to carry
  // out the conversion (e.g. Hexahedron_64 to Hexahedron).
48 49 50 51 52 53 54 55 56 57 58 59 60

  class Converter {

  public:

    Converter()
    {
    }

    virtual ~Converter()
    {
    }

61 62
    virtual shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
63
            const shared_ptr<const XdmfTopologyType> topologyType,
64
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const = 0;
65 66 67 68

  protected:

    struct PointComparison {
69

70
      static double epsilon() { return 1e-6; };
71

72 73 74 75 76
      bool
      operator()(const std::vector<double> & point1,
                 const std::vector<double> & point2) const
      {
        for(unsigned int i=0; i<3; ++i) {
77
          if(fabs(point1[i] - point2[i]) > epsilon()) {
78 79 80 81 82 83 84
            return point1[i] < point2[i];
          }
        }
        return false;
      }
    };

85
    unsigned int
86
    insertPointWithoutCheck(const std::vector<double> & newPoint,
87 88
                            const shared_ptr<XdmfTopology> & newConnectivity,
                            const shared_ptr<XdmfGeometry> & newPoints) const
89
    {
90 91
      const unsigned int index = newPoints->getSize() / 3;
      newConnectivity->pushBack(index);
92 93 94
      newPoints->pushBack(newPoint[0]);
      newPoints->pushBack(newPoint[1]);
      newPoints->pushBack(newPoint[2]);
95
      return index;
96 97
    }

98
    unsigned int
99 100
    insertPointWithCheck(const std::vector<double> & newPoint,
                         std::map<std::vector<double>, unsigned int, PointComparison> & coordToIdMap,
101 102
                         const shared_ptr<XdmfTopology> & newConnectivity,
                         const shared_ptr<XdmfGeometry> & newPoints) const
103
    {
104
      std::map<std::vector<double>, unsigned int, PointComparison>::const_iterator iter =
105 106 107 108
        coordToIdMap.find(newPoint);
      if(iter == coordToIdMap.end()) {
        // Not inserted before
        coordToIdMap[newPoint] = newPoints->getSize() / 3;;
109
        return insertPointWithoutCheck(newPoint, newConnectivity, newPoints);
110 111
      }
      else {
112 113 114
        const unsigned int index = iter->second;
        newConnectivity->pushBack(index);
        return index;
115 116 117 118 119 120 121 122 123 124 125 126 127
      }
    }

  };

  class Tessellator : public Converter {

  public:

    virtual ~Tessellator()
    {
    }

128 129
    shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
130
            const shared_ptr<const XdmfTopologyType> topologyType,
131
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
132
    {
133
      shared_ptr<XdmfUnstructuredGrid> toReturn =
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        XdmfUnstructuredGrid::New();
      toReturn->setName(gridToConvert->getName());
      toReturn->setGeometry(gridToConvert->getGeometry());

      if(heavyDataWriter) {
        if(!toReturn->getGeometry()->isInitialized()) {
          toReturn->getGeometry()->read();
        }
        toReturn->getGeometry()->accept(heavyDataWriter);
        toReturn->getGeometry()->release();
      }

      bool releaseTopology;
      if(!gridToConvert->getTopology()->isInitialized()) {
        gridToConvert->getTopology()->read();
        releaseTopology = true;
      }

      this->tesselateTopology(gridToConvert->getTopology(),
                              toReturn->getTopology());

      if(releaseTopology) {
        gridToConvert->getTopology()->release();
      }

      if(heavyDataWriter) {
        toReturn->getTopology()->accept(heavyDataWriter);
        toReturn->getTopology()->release();
      }

      for(unsigned int i=0; i<gridToConvert->getNumberAttributes(); ++i) {
165
        shared_ptr<XdmfAttribute> currAttribute =
166
          gridToConvert->getAttribute(i);
167 168 169 170 171 172 173 174 175 176
        shared_ptr<XdmfAttribute> createdAttribute =
          shared_ptr<XdmfAttribute>();
        if(currAttribute->getCenter() == XdmfAttributeCenter::Node()) {
          createdAttribute = currAttribute;
        }
        else if(currAttribute->getCenter() == XdmfAttributeCenter::Cell()) {
          bool releaseAttribute = false;
          if(!currAttribute->isInitialized()) {
            currAttribute->read();
            releaseAttribute = true;
177 178
          }

179 180 181 182 183 184 185 186 187 188 189 190 191 192
          createdAttribute = XdmfAttribute::New();
          createdAttribute->setName(currAttribute->getName());
          createdAttribute->setType(currAttribute->getType());
          createdAttribute->setCenter(currAttribute->getCenter());
          createdAttribute->initialize(currAttribute->getArrayType(),
                                       currAttribute->getSize() * mNumTesselations);
          for(unsigned int j=0; j<currAttribute->getSize(); ++j) {
            createdAttribute->insert(j * mNumTesselations,
                                     currAttribute,
                                     j,
                                     mNumTesselations,
                                     1,
                                     0);
          }
193

194 195
          if(releaseAttribute) {
            currAttribute->release();
196
          }
197 198 199 200 201 202
        }
        if(createdAttribute) {
          toReturn->insert(createdAttribute);
          if(heavyDataWriter) {
            if(!createdAttribute->isInitialized()) {
              createdAttribute->read();
203
            }
204 205
            createdAttribute->accept(heavyDataWriter);
            createdAttribute->release();
206
          }
207
        }
208 209 210 211 212
      }
      return toReturn;
    }

    virtual void
213 214
    tesselateTopology(shared_ptr<XdmfTopology> topologyToConvert,
                      shared_ptr<XdmfTopology> topologyToReturn) const = 0;
215 216 217 218 219 220 221 222 223 224 225 226

  protected:

    Tessellator(const unsigned int numTesselations) :
      mNumTesselations(numTesselations)
    {
    }

    const unsigned int mNumTesselations;

  };

227 228
  template <unsigned int ORDER, bool ISSPECTRAL>
  class HexahedronToHighOrderHexahedron : public Converter {
229 230 231

  public:

232
    HexahedronToHighOrderHexahedron()
233 234 235
    {
    }

236
    virtual ~HexahedronToHighOrderHexahedron()
237 238 239
    {
    }

240 241 242 243 244 245
    void
    calculateIntermediatePoint(std::vector<double> & result,
                               const std::vector<double> & point1,
                               const std::vector<double> & point2,
                               int index,
                               bool spectral) const
246
    {
247 248 249
      const double scalar = points[index];
      for (int i=0; i<3; i++)
        result[i] = point1[i]+scalar*(point2[i]-point1[i]);
250 251 252
    }


253 254
    shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
255
            const shared_ptr<const XdmfTopologyType> topologyType,
256
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
257
    {
258 259

      shared_ptr<XdmfUnstructuredGrid> toReturn = XdmfUnstructuredGrid::New();
260 261
      toReturn->setName(gridToConvert->getName());

262 263 264 265 266
      shared_ptr<XdmfGeometry> geometry = gridToConvert->getGeometry();
      shared_ptr<XdmfGeometry> toReturnGeometry = toReturn->getGeometry();

      toReturnGeometry->setType(geometry->getType());
      toReturnGeometry->initialize(geometry->getArrayType());
267 268

      bool releaseGeometry = false;
269 270
      if(!geometry->isInitialized()) {
        geometry->read();
271 272 273
        releaseGeometry = true;
      }

274 275
      shared_ptr<XdmfTopology> topology = gridToConvert->getTopology();
      shared_ptr<XdmfTopology> toReturnTopology = toReturn->getTopology();
276

277 278 279 280
      toReturn->getTopology()->setType(topologyType);
      toReturnTopology->initialize(topology->getArrayType());
      toReturnTopology->reserve(mNodesPerElement *
                                topology->getNumberElements());
281 282

      bool releaseTopology = false;
283 284 285
      if(!topology->isInitialized()) {
        topology->read();
        releaseTopology = true;
286 287 288
      }

      std::map<std::vector<double>, unsigned int, PointComparison> coordToIdMap;
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
      std::map<unsigned int, unsigned int> oldIdToNewId;

      // allocate storage for values used in loop
      unsigned int zeroIndex;
      unsigned int oneIndex;
      unsigned int twoIndex;
      unsigned int threeIndex;
      unsigned int fourIndex;
      unsigned int fiveIndex;
      unsigned int sixIndex;
      unsigned int sevenIndex;
      std::vector<double> elementCorner0(3);
      std::vector<double> elementCorner1(3);
      std::vector<double> elementCorner2(3);
      std::vector<double> elementCorner3(3);
      std::vector<double> elementCorner4(3);
      std::vector<double> elementCorner5(3);
      std::vector<double> elementCorner6(3);
      std::vector<double> elementCorner7(3);
      std::vector<double> planeCorner0(3);
      std::vector<double> planeCorner1(3);
      std::vector<double> planeCorner2(3);
      std::vector<double> planeCorner3(3);
      std::vector<double> lineEndPoint0(3);
      std::vector<double> lineEndPoint1(3);
      std::vector<double> point(3);

      unsigned int offset = 0;
      for(unsigned int elem = 0; elem<topology->getNumberElements(); ++elem) {

        //
        // get indices of coner vertices of the element
        //
        zeroIndex = topology->getValue<unsigned int>(offset++);
        oneIndex = topology->getValue<unsigned int>(offset++);
        twoIndex = topology->getValue<unsigned int>(offset++);
        threeIndex = topology->getValue<unsigned int>(offset++);
        fourIndex = topology->getValue<unsigned int>(offset++);
        fiveIndex = topology->getValue<unsigned int>(offset++);
        sixIndex = topology->getValue<unsigned int>(offset++);
        sevenIndex = topology->getValue<unsigned int>(offset++);

        // get locations of corner vertices of the element
        geometry->getValues(zeroIndex * 3,
                            &(elementCorner0[0]),
                            3);
        geometry->getValues(oneIndex * 3,
                            &(elementCorner1[0]),
                            3);
        geometry->getValues(twoIndex * 3,
                            &(elementCorner2[0]),
                            3);
        geometry->getValues(threeIndex * 3,
                            &(elementCorner3[0]),
                            3);
        geometry->getValues(fourIndex * 3,
                            &(elementCorner4[0]),
                            3);
        geometry->getValues(fiveIndex * 3,
                            &(elementCorner5[0]),
                            3);
        geometry->getValues(sixIndex * 3,
                            &(elementCorner6[0]),
                            3);
        geometry->getValues(sevenIndex * 3,
                            &(elementCorner7[0]),
                            3);

        // loop over i, j, k directions of element isolation i, j, and
        // k planes
        for(unsigned int i=0; i<mNodesPerEdge; ++i){
          // calculate corners of i plane
          calculateIntermediatePoint(planeCorner0,
                                     elementCorner0,
                                     elementCorner1,
                                     i,
                                     true);
          calculateIntermediatePoint(planeCorner1,
                                     elementCorner4,
                                     elementCorner5,
                                     i,
                                     true);
          calculateIntermediatePoint(planeCorner2,
                                     elementCorner3,
                                     elementCorner2,
                                     i,
                                     true);
          calculateIntermediatePoint(planeCorner3,
                                     elementCorner7,
                                     elementCorner6,
                                     i,
                                     true);

          for(unsigned int j=0; j<mNodesPerEdge; ++j) {
            // calculate endpoints of j slice of i plane
            calculateIntermediatePoint(lineEndPoint0,
                                       planeCorner0,
                                       planeCorner2,
                                       j,
                                       true);
            calculateIntermediatePoint(lineEndPoint1,
                                       planeCorner1,
                                       planeCorner3,
                                       j,
                                       true);

            for(unsigned int k=0; k<mNodesPerEdge; ++k) {
              // calculate point to add to mesh
              calculateIntermediatePoint(point,
                                         lineEndPoint0,
                                         lineEndPoint1,
                                         k,
                                         true);
              if((i == 0 || i == ORDER) ||
                 (j == 0 || j == ORDER) ||
                 (k == 0 || k == ORDER)) {
                unsigned int newIndex = 
                  this->insertPointWithCheck(point,
                                             coordToIdMap,
                                             toReturnTopology,
                                             toReturnGeometry);
                if((i == 0 || i == ORDER) &&
                   (j == 0 || j == ORDER) &&
                   (k == 0 || k == ORDER)) {
                  if(i == 0) {
                    if(j == 0) {
                      if(k == 0) {
                        oldIdToNewId[zeroIndex] = newIndex;
                      }
                      else {
                        oldIdToNewId[fourIndex] = newIndex;
                      }
                    }
                    else if(k == 0) {
                      oldIdToNewId[threeIndex] = newIndex;
                    }
                    else {
                      oldIdToNewId[sevenIndex] = newIndex;
                    }
                  }
                  else {
                    if(j == 0) {
                      if(k == 0) {
                        oldIdToNewId[oneIndex] = newIndex;
                      }
                      else {
                        oldIdToNewId[fiveIndex] = newIndex;
                      }
                    }
                    else if(k == 0) {
                      oldIdToNewId[twoIndex] = newIndex;
                    }
                    else {
                      oldIdToNewId[sixIndex] = newIndex;
                    }
                  }
                }
              }
              else {
                this->insertPointWithoutCheck(point,
                                              toReturnTopology,
                                              toReturnGeometry);
              }
            }
          }
454 455
        }
      }
456

457
      if(releaseTopology) {
458 459 460 461
        topology->release();
      }
      if(releaseGeometry) {
        geometry->release();
462
      }
463

464 465 466 467 468 469 470
      if(heavyDataWriter) {
        toReturnTopology->accept(heavyDataWriter);
        toReturnTopology->release();
        toReturnGeometry->accept(heavyDataWriter);
        toReturnGeometry->release();
      }

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      // handle sets
      for(unsigned int i=0; i<gridToConvert->getNumberSets(); ++i) {
        const shared_ptr<XdmfSet> set = gridToConvert->getSet(i);
        const shared_ptr<const XdmfSetType> setType = set->getType();
        if(setType == XdmfSetType::Cell()) {
          toReturn->insert(set);
        }
        else if(setType == XdmfSetType::Node()) {
          bool releaseSet = false;
          if(!set->isInitialized()) {
            set->read();
            releaseSet = true;
          }
          shared_ptr<XdmfSet> toReturnSet = XdmfSet::New();
          toReturnSet->setName(set->getName());
          toReturnSet->setType(set->getType());
          toReturnSet->initialize(set->getArrayType(),
                                  set->getSize());
                                  
          for(int i=0; i<set->getSize(); ++i) {
            const unsigned int nodeId = set->getValue<unsigned int>(i);
            std::map<unsigned int, unsigned int>::const_iterator iter =
              oldIdToNewId.find(nodeId);
            if(iter == oldIdToNewId.end()) {
              XdmfError::message(XdmfError::FATAL, 
                                 "Error converting hex node id set to high "
                                 "order node id set.");
            }
            toReturnSet->insert(i, iter->second);
          }
          if(releaseSet) {
            set->release();
          }
504

505
          toReturn->insert(toReturnSet);
506

507 508 509 510 511 512 513
          if(heavyDataWriter) {
            toReturnSet->accept(heavyDataWriter);
            toReturnSet->release();
          }
        }
      }
      return toReturn;
514 515 516
    }

  private:
517 518 519 520
    static const unsigned int mNodesPerEdge = ORDER + 1;
    static const unsigned int mNodesPerElement = mNodesPerEdge *
      mNodesPerEdge * mNodesPerEdge;
    static const double points[];
521 522 523

  };

524 525 526 527 528 529 530
  template <>
  const double HexahedronToHighOrderHexahedron<3, true>::points[] = {
    0.0,
    0.5-0.1*sqrt(5.0),
    0.5+0.1*sqrt(5.0),
    1.0
  };
531

532 533 534 535 536 537 538 539
  template <>
  const double HexahedronToHighOrderHexahedron<4, true>::points[] = {
    0.0,
    0.5-sqrt(21.0)/14.0,
    0.5,
    0.5+sqrt(21.0)/14.0,
    1.0
  };
540

541 542 543 544 545 546 547 548 549
  template <>
  const double HexahedronToHighOrderHexahedron<5, true>::points[] = {
    0.0,
    0.5-sqrt((7.0+2.0*sqrt(7.0))/84.0),
    0.5-sqrt((7.0-2.0*sqrt(7.0))/84.0),
    0.5+sqrt((7.0-2.0*sqrt(7.0))/84.0),
    0.5+sqrt((7.0+2.0*sqrt(7.0))/84.0),
    1.0
  };
550

551 552 553 554 555 556 557 558 559 560
  template <>
  const double HexahedronToHighOrderHexahedron<6, true>::points[] = {
    0.0,
    0.5-sqrt((15.0+2.0*sqrt(15.0))/132.0),
    0.5-sqrt((15.0-2.0*sqrt(15.0))/132.0),
    0.5,
    0.5+sqrt((15.0-2.0*sqrt(15.0))/132.0),
    0.5+sqrt((15.0+2.0*sqrt(15.0))/132.0),
    1.0
  };
561

562 563 564 565 566 567 568 569 570 571 572
  template <>
  const double HexahedronToHighOrderHexahedron<7, true>::points[] = {
    0.0,
    0.064129925745196714,
    0.20414990928342885,
    0.39535039104876057,
    0.60464960895123943,
    0.79585009071657109,
    0.93587007425480329,
    1.0
  };
573

574 575 576 577 578 579 580 581 582 583 584 585
  template <>
  const double HexahedronToHighOrderHexahedron<8, true>::points[] = {
    0.0,
    0.050121002294269912,
    0.16140686024463108,
    0.31844126808691087,
    0.5,
    0.68155873191308913,
    0.83859313975536898,
    0.94987899770573003,
    1.0
  };
586

587 588 589 590 591 592 593 594 595 596 597 598
  template <>
  const double HexahedronToHighOrderHexahedron<9, true>::points[] = {
    0.0,
    0.040233045916770627,
    0.13061306744724743,
    0.26103752509477773,
    0.4173605211668065,
    0.58263947883319345,
    0.73896247490522227,
    0.86938693255275257,
    0.95976695408322943,
    1.0
599 600
  };

601 602 603 604 605 606 607 608 609 610 611 612 613 614
  template <>
  const double HexahedronToHighOrderHexahedron<10, true>::points[] = {
    0.0,
    0.032999284795970474,
    0.10775826316842779,
    0.21738233650189748,
    0.35212093220653029,
    0.5,
    0.64787906779346971,
    0.78261766349810258,
    0.89224173683157226,
    0.96700071520402953,
    1.0
  };
615

616 617 618 619 620 621 622
  template <>
  const double HexahedronToHighOrderHexahedron<3, false>::points[] = {
    0.0,
    1.0/3.0,
    2.0/3.0,
    1.0
  };
623

624 625 626 627 628 629 630 631
  template <>
  const double HexahedronToHighOrderHexahedron<4, false>::points[] = {
    0.0,
    0.25,
    0.5,
    0.75,
    1.0
  };
632

633 634 635 636 637 638 639 640 641
  template <>
  const double HexahedronToHighOrderHexahedron<5, false>::points[] = {
    0.0,
    0.2,
    0.4,
    0.6,
    0.8,
    1.0
  };
642

643 644 645 646 647 648 649 650 651 652
  template <>
  const double HexahedronToHighOrderHexahedron<6, false>::points[] = {
    0.0,
    1.0/6.0,
    1.0/3.0,
    0.5,
    2.0/3.0,
    5.0/6.0,
    1.0
  };
653

654 655 656 657 658 659 660 661 662 663 664
  template <>
  const double HexahedronToHighOrderHexahedron<7, false>::points[] = {
    0.0,
    1.0/7.0,
    2.0/7.0,
    3.0/7.0,
    4.0/7.0,
    5.0/7.0,
    6.0/7.0,
    1.0
  };
665

666 667 668 669 670 671 672 673 674 675 676 677
  template <>
  const double HexahedronToHighOrderHexahedron<8, false>::points[] = {
    0.0,
    0.125,
    0.25,
    0.375,
    0.5,
    0.625,
    0.75,
    0.875,
    1.0
  };
678

679 680 681 682 683 684 685 686 687 688 689 690
  template <>
  const double HexahedronToHighOrderHexahedron<9, false>::points[] = {
    0.0,
    1.0/9.0,
    2.0/9.0,
    1.0/3.0,
    4.0/9.0,
    5.0/9.0,
    2.0/3.0,
    7.0/9.0,
    8.0/9.0,
    1.0
691 692
  };

693 694 695 696 697 698 699 700 701 702 703 704 705 706
  template <>
  const double HexahedronToHighOrderHexahedron<10, false>::points[] = {
    0.0,
    0.1,
    0.2,
    0.3,
    0.4,
    0.5,
    0.6,
    0.7,
    0.8,
    0.9,
    1.0
  };
707

708 709
  template <unsigned int ORDER>
  class HighOrderHexahedronToHexahedron : public Tessellator {
710 711 712

  public:

713 714
    HighOrderHexahedronToHexahedron() :
      Tessellator(ORDER * ORDER * ORDER)
715 716 717 718
    {
    }

    void
719 720
    tesselateTopology(shared_ptr<XdmfTopology> topologyToConvert,
                      shared_ptr<XdmfTopology> topologyToReturn) const
721 722 723
    {
      topologyToReturn->setType(XdmfTopologyType::Hexahedron());
      topologyToReturn->initialize(topologyToConvert->getArrayType(),
724
                                   8 * ORDER * ORDER * ORDER * topologyToConvert->getNumberElements());
725 726

      unsigned int newIndex = 0;
727 728 729 730
      int indexA = 0;
      int indexB = mNodesPerEdge * mNodesPerEdge;
      int indexC = mNodesPerEdge * mNodesPerEdge + mNodesPerEdge;
      int indexD = mNodesPerEdge;
731
      for(unsigned int i=0; i<topologyToConvert->getNumberElements(); ++i) {
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
        for(unsigned int j=0; j<ORDER; ++j) {
          for(unsigned int k=0; k<ORDER; ++k) {
            for(unsigned int l=0; l<ORDER; ++l){
              topologyToReturn->insert(newIndex++, topologyToConvert, indexA++);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexB++);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexC++);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexD++);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexA);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexB);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexC);
              topologyToReturn->insert(newIndex++, topologyToConvert, indexD);
            }
            indexA++;
            indexB++;
            indexC++;
            indexD++;
          }
          indexA += mNodesPerEdge;
          indexB += mNodesPerEdge;
          indexC += mNodesPerEdge;
          indexD += mNodesPerEdge;
        }
        indexA += mNodesPerEdge * mNodesPerEdge;
        indexB += mNodesPerEdge * mNodesPerEdge;
        indexC += mNodesPerEdge * mNodesPerEdge;
        indexD += mNodesPerEdge * mNodesPerEdge;
758 759 760
      }
    }

761 762 763 764
  private:
    static const unsigned int mNodesPerEdge = (ORDER + 1);
    static const unsigned int mNodesPerElement = mNodesPerEdge *
      mNodesPerEdge * mNodesPerEdge;
765 766

  };
767

768
}
769

770
shared_ptr<XdmfTopologyConverter>
771
XdmfTopologyConverter::New()
772
{
773
  shared_ptr<XdmfTopologyConverter> p(new XdmfTopologyConverter());
774
  return p;
775 776
}

777 778 779 780 781 782 783 784
XdmfTopologyConverter::XdmfTopologyConverter()
{
}

XdmfTopologyConverter::~XdmfTopologyConverter()
{
}

785 786 787
shared_ptr<XdmfUnstructuredGrid>
XdmfTopologyConverter::convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
                               const shared_ptr<const XdmfTopologyType> topologyType,
788
                               unsigned int options,
789
                               const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
790
{
791
  // Make sure geometry and topology are non null
792
  if(!(gridToConvert->getGeometry() && gridToConvert->getTopology()))
793 794 795
    XdmfError::message(XdmfError::FATAL,
                       "Current grid's geometry or topology is null "
                       "in XdmfTopologyConverter::convert");
796

797
  shared_ptr<const XdmfTopologyType> topologyTypeToConvert =
798 799 800 801 802 803 804
    gridToConvert->getTopology()->getType();
  if(topologyTypeToConvert == topologyType) {
    // No conversion necessary
    return gridToConvert;
  }

  if(gridToConvert->getGeometry()->getType() != XdmfGeometryType::XYZ()) {
805 806 807
    XdmfError::message(XdmfError::FATAL,
                       "Grid to convert's type is not 'XYZ' in "
                       "XdmfTopologyConverter::convert");
808 809 810 811 812
  }

  Converter * converter = NULL;
  if(topologyTypeToConvert == XdmfTopologyType::Hexahedron()) {
    if(topologyType == XdmfTopologyType::Hexahedron_64()) {
813 814 815 816 817 818
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<3, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<3, false>();
      }
819 820
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_125()) {
821 822 823 824 825 826
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<4, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<4, false>();
      }
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 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
    else if(topologyType == XdmfTopologyType::Hexahedron_216()) {
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<5, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<5, false>();
      }
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_343()) {
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<6, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<6, false>();
      }
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_512()) {
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<7, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<7, false>();
      }
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_729()) {
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<8, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<8, false>();
      }
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_1000()) {
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<9, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<9, false>();
      }
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_1331()) {
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<10, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<10, false>();
      }
875 876 877 878
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_64()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
879
      converter = new HighOrderHexahedronToHexahedron<3>();
880 881 882 883
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_125()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
884 885 886 887 888 889 890 891 892 893 894
      converter = new HighOrderHexahedronToHexahedron<4>();
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_216()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
      converter = new HighOrderHexahedronToHexahedron<5>();
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_343()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
      converter = new HighOrderHexahedronToHexahedron<6>();
895 896
    }
  }
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_512()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
      converter = new HighOrderHexahedronToHexahedron<7>();
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_729()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
      converter = new HighOrderHexahedronToHexahedron<8>();
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_1000()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
      converter = new HighOrderHexahedronToHexahedron<9>();
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_1331()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
      converter = new HighOrderHexahedronToHexahedron<10>();
    }
  }

918
  if(converter) {
919
    shared_ptr<XdmfUnstructuredGrid> toReturn =
920 921 922
      converter->convert(gridToConvert,
                         topologyType,
                         heavyDataWriter);
923 924 925 926
    delete converter;
    return toReturn;
  }
  else {
927 928 929
    XdmfError::message(XdmfError::FATAL,
                       "Cannot convert topology type in "
                       "XdmfTopologyConverter::convert");
930
  }
931
}