XdmfTopologyConverter.cpp 38.6 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  void handleSetConversion(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
			   const shared_ptr<XdmfUnstructuredGrid> toReturn,
			   const std::map<unsigned int, unsigned int> & oldIdToNewId,
			   const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) 
  {

    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(unsigned 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 hex_27 "
			       "node id set.");
	  }
	  toReturnSet->insert(i, iter->second);
	}
	if(releaseSet) {
	  set->release();
	}
	  
	toReturn->insert(toReturnSet);
	  
	if(heavyDataWriter) {
	  toReturnSet->accept(heavyDataWriter);
	  toReturnSet->release();
	}
      }
    }
  }

93 94 95 96
  // 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).
97 98 99 100 101 102 103 104 105 106 107 108 109

  class Converter {

  public:

    Converter()
    {
    }

    virtual ~Converter()
    {
    }

110 111
    virtual shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
112
            const shared_ptr<const XdmfTopologyType> topologyType,
113
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const = 0;
114 115 116 117

  protected:

    struct PointComparison {
118

119
      static double epsilon() { return 1e-6; };
120

121 122 123 124 125
      bool
      operator()(const std::vector<double> & point1,
                 const std::vector<double> & point2) const
      {
        for(unsigned int i=0; i<3; ++i) {
126
          if(fabs(point1[i] - point2[i]) > epsilon()) {
127 128 129 130 131 132 133
            return point1[i] < point2[i];
          }
        }
        return false;
      }
    };

134
    unsigned int
135
    insertPointWithoutCheck(const std::vector<double> & newPoint,
136 137
                            const shared_ptr<XdmfTopology> & newConnectivity,
                            const shared_ptr<XdmfGeometry> & newPoints) const
138
    {
139 140
      const unsigned int index = newPoints->getSize() / 3;
      newConnectivity->pushBack(index);
141 142 143
      newPoints->pushBack(newPoint[0]);
      newPoints->pushBack(newPoint[1]);
      newPoints->pushBack(newPoint[2]);
144
      return index;
145 146
    }

147
    unsigned int
148 149
    insertPointWithCheck(const std::vector<double> & newPoint,
                         std::map<std::vector<double>, unsigned int, PointComparison> & coordToIdMap,
150 151
                         const shared_ptr<XdmfTopology> & newConnectivity,
                         const shared_ptr<XdmfGeometry> & newPoints) const
152
    {
153
      std::map<std::vector<double>, unsigned int, PointComparison>::const_iterator iter =
154 155 156 157
        coordToIdMap.find(newPoint);
      if(iter == coordToIdMap.end()) {
        // Not inserted before
        coordToIdMap[newPoint] = newPoints->getSize() / 3;;
158
        return insertPointWithoutCheck(newPoint, newConnectivity, newPoints);
159 160
      }
      else {
161 162 163
        const unsigned int index = iter->second;
        newConnectivity->pushBack(index);
        return index;
164 165 166 167 168 169 170 171 172 173 174 175 176
      }
    }

  };

  class Tessellator : public Converter {

  public:

    virtual ~Tessellator()
    {
    }

177 178
    shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
179
            const shared_ptr<const XdmfTopologyType> topologyType,
180
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
181
    {
182
      shared_ptr<XdmfUnstructuredGrid> toReturn =
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        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) {
214
        shared_ptr<XdmfAttribute> currAttribute =
215
          gridToConvert->getAttribute(i);
216 217 218 219 220 221 222 223 224 225
        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;
226 227
          }

228 229 230 231 232 233 234 235 236 237 238 239 240 241
          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);
          }
242

243 244
          if(releaseAttribute) {
            currAttribute->release();
245
          }
246 247 248 249 250 251
        }
        if(createdAttribute) {
          toReturn->insert(createdAttribute);
          if(heavyDataWriter) {
            if(!createdAttribute->isInitialized()) {
              createdAttribute->read();
252
            }
253 254
            createdAttribute->accept(heavyDataWriter);
            createdAttribute->release();
255
          }
256
        }
257 258 259 260 261
      }
      return toReturn;
    }

    virtual void
262 263
    tesselateTopology(shared_ptr<XdmfTopology> topologyToConvert,
                      shared_ptr<XdmfTopology> topologyToReturn) const = 0;
264 265 266 267 268 269 270 271 272 273 274 275

  protected:

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

    const unsigned int mNumTesselations;

  };

276 277 278 279 280 281 282 283 284 285 286 287 288 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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 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 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  class HexahedronToHexahedron27 : public Converter {

  public:

    HexahedronToHexahedron27()
    {
    }

    virtual ~HexahedronToHexahedron27()
    {
    }

    void
    calculateMidPoint(std::vector<double> & result,
		      const std::vector<double> & point1,
		      const std::vector<double> & point2) const
    {
      for (int i=0; i<3; i++)
        result[i] = (point1[i]+point2[i]) / 2.0;
    }

    shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
            const shared_ptr<const XdmfTopologyType> topologyType,
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
    {

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

      shared_ptr<XdmfGeometry> geometry = gridToConvert->getGeometry();
      shared_ptr<XdmfGeometry> toReturnGeometry = toReturn->getGeometry();

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

      bool releaseGeometry = false;
      if(!geometry->isInitialized()) {
	geometry->read();
	releaseGeometry = true;
      }

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

      toReturn->getTopology()->setType(topologyType);
      toReturnTopology->initialize(topology->getArrayType());
      toReturnTopology->reserve(topologyType->getNodesPerElement() *
				topology->getNumberElements());

      bool releaseTopology = false;
      if(!topology->isInitialized()) {
	topology->read();
	releaseTopology = true;
      }

      std::map<std::vector<double>, unsigned int, PointComparison> coordToIdMap;
      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> newPoint(3);

      unsigned int offset = 0;
      unsigned int newIndex = 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);

	// insert corner points
	newIndex = this->insertPointWithCheck(elementCorner0,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[zeroIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner1,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[oneIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner2,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[twoIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner3,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[threeIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner4,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[fourIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner5,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[fiveIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner6,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[sixIndex] = newIndex;
	newIndex = this->insertPointWithCheck(elementCorner7,
					      coordToIdMap,
					      toReturnTopology,
					      toReturnGeometry);
	oldIdToNewId[sevenIndex] = newIndex;

	// insert additional points
	calculateMidPoint(newPoint,
			  elementCorner0,
			  elementCorner1);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner1,
			  elementCorner2);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner2,
			  elementCorner3);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner0,
			  elementCorner3);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner4,
			  elementCorner5);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner5,
			  elementCorner6);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner6,
			  elementCorner7);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner4,
			  elementCorner7);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner0,
			  elementCorner4);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner1,
			  elementCorner5);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner2,
			  elementCorner6);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner3,
			  elementCorner7);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner0,
			  elementCorner7);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner1,
			  elementCorner6);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner1,
			  elementCorner4);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner2,
			  elementCorner7);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner0,
			  elementCorner2);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner4,
			  elementCorner6);
	this->insertPointWithCheck(newPoint,
				   coordToIdMap,
				   toReturnTopology,
				   toReturnGeometry);
	calculateMidPoint(newPoint,
			  elementCorner0,
			  elementCorner6);
	this->insertPointWithoutCheck(newPoint,
				      toReturnTopology,
				      toReturnGeometry);
       
      }
 
      if(releaseTopology) {
	topology->release();
      }
      if(releaseGeometry) {
	geometry->release();
      }
       
      if(heavyDataWriter) {
	toReturnTopology->accept(heavyDataWriter);
	toReturnTopology->release();
	toReturnGeometry->accept(heavyDataWriter);
	toReturnGeometry->release();
      }

      handleSetConversion(gridToConvert,
			  toReturn,
			  oldIdToNewId,
			  heavyDataWriter);
       
      return toReturn;
      
    }
    
  };

597 598
  template <unsigned int ORDER, bool ISSPECTRAL>
  class HexahedronToHighOrderHexahedron : public Converter {
599 600 601

  public:

602
    HexahedronToHighOrderHexahedron()
603 604 605
    {
    }

606
    virtual ~HexahedronToHighOrderHexahedron()
607 608 609
    {
    }

610 611 612 613 614 615
    void
    calculateIntermediatePoint(std::vector<double> & result,
                               const std::vector<double> & point1,
                               const std::vector<double> & point2,
                               int index,
                               bool spectral) const
616
    {
617 618 619
      const double scalar = points[index];
      for (int i=0; i<3; i++)
        result[i] = point1[i]+scalar*(point2[i]-point1[i]);
620 621
    }

622 623
    shared_ptr<XdmfUnstructuredGrid>
    convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
624
            const shared_ptr<const XdmfTopologyType> topologyType,
625
            const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
626
    {
627 628

      shared_ptr<XdmfUnstructuredGrid> toReturn = XdmfUnstructuredGrid::New();
629 630
      toReturn->setName(gridToConvert->getName());

631 632 633 634 635
      shared_ptr<XdmfGeometry> geometry = gridToConvert->getGeometry();
      shared_ptr<XdmfGeometry> toReturnGeometry = toReturn->getGeometry();

      toReturnGeometry->setType(geometry->getType());
      toReturnGeometry->initialize(geometry->getArrayType());
636 637

      bool releaseGeometry = false;
638 639
      if(!geometry->isInitialized()) {
        geometry->read();
640 641 642
        releaseGeometry = true;
      }

643 644
      shared_ptr<XdmfTopology> topology = gridToConvert->getTopology();
      shared_ptr<XdmfTopology> toReturnTopology = toReturn->getTopology();
645

646 647
      toReturn->getTopology()->setType(topologyType);
      toReturnTopology->initialize(topology->getArrayType());
648
      toReturnTopology->reserve(topologyType->getNodesPerElement() *
649
                                topology->getNumberElements());
650 651

      bool releaseTopology = false;
652 653 654
      if(!topology->isInitialized()) {
        topology->read();
        releaseTopology = true;
655 656 657
      }

      std::map<std::vector<double>, unsigned int, PointComparison> coordToIdMap;
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 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 758 759 760 761 762 763 764 765 766 767 768 769 770 771
      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)) {
772
                const unsigned int newIndex = 
773 774 775 776 777 778 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 807 808 809 810 811 812 813 814 815 816 817 818 819 820
                  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);
              }
            }
          }
821 822
        }
      }
823

824
      if(releaseTopology) {
825 826 827 828
        topology->release();
      }
      if(releaseGeometry) {
        geometry->release();
829
      }
830

831 832 833 834 835 836 837
      if(heavyDataWriter) {
        toReturnTopology->accept(heavyDataWriter);
        toReturnTopology->release();
        toReturnGeometry->accept(heavyDataWriter);
        toReturnGeometry->release();
      }

838 839 840 841
      handleSetConversion(gridToConvert,
			  toReturn,
			  oldIdToNewId,
			  heavyDataWriter);
842

843
      return toReturn;
844 845 846
    }

  private:
847 848
    static const unsigned int mNodesPerEdge = ORDER + 1;
    static const double points[];
849 850 851

  };

852 853 854 855 856 857 858
  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
  };
859

860 861 862 863 864 865 866 867
  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
  };
868

869 870 871 872 873 874 875 876 877
  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
  };
878

879 880 881 882 883 884 885 886 887 888
  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
  };
889

890 891 892 893 894 895 896 897 898 899 900
  template <>
  const double HexahedronToHighOrderHexahedron<7, true>::points[] = {
    0.0,
    0.064129925745196714,
    0.20414990928342885,
    0.39535039104876057,
    0.60464960895123943,
    0.79585009071657109,
    0.93587007425480329,
    1.0
  };
901

902 903 904 905 906 907 908 909 910 911 912 913
  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
  };
914

915 916 917 918 919 920 921 922 923 924 925 926
  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
927 928
  };

929 930 931 932 933 934 935 936 937 938 939 940 941 942
  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
  };
943

944 945 946 947 948 949 950
  template <>
  const double HexahedronToHighOrderHexahedron<3, false>::points[] = {
    0.0,
    1.0/3.0,
    2.0/3.0,
    1.0
  };
951

952 953 954 955 956 957 958 959
  template <>
  const double HexahedronToHighOrderHexahedron<4, false>::points[] = {
    0.0,
    0.25,
    0.5,
    0.75,
    1.0
  };
960

961 962 963 964 965 966 967 968 969
  template <>
  const double HexahedronToHighOrderHexahedron<5, false>::points[] = {
    0.0,
    0.2,
    0.4,
    0.6,
    0.8,
    1.0
  };
970

971 972 973 974 975 976 977 978 979 980
  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
  };
981

982 983 984 985 986 987 988 989 990 991 992
  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
  };
993

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  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
  };
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
  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
1019 1020
  };

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  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
  };
1035

1036 1037
  template <unsigned int ORDER>
  class HighOrderHexahedronToHexahedron : public Tessellator {
1038 1039 1040

  public:

1041 1042
    HighOrderHexahedronToHexahedron() :
      Tessellator(ORDER * ORDER * ORDER)
1043 1044 1045 1046
    {
    }

    void
1047 1048
    tesselateTopology(shared_ptr<XdmfTopology> topologyToConvert,
                      shared_ptr<XdmfTopology> topologyToReturn) const
1049 1050 1051
    {
      topologyToReturn->setType(XdmfTopologyType::Hexahedron());
      topologyToReturn->initialize(topologyToConvert->getArrayType(),
1052
                                   8 * ORDER * ORDER * ORDER * topologyToConvert->getNumberElements());
1053 1054

      unsigned int newIndex = 0;
1055 1056 1057 1058
      int indexA = 0;
      int indexB = mNodesPerEdge * mNodesPerEdge;
      int indexC = mNodesPerEdge * mNodesPerEdge + mNodesPerEdge;
      int indexD = mNodesPerEdge;
1059
      for(unsigned int i=0; i<topologyToConvert->getNumberElements(); ++i) {
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
        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;
1086 1087 1088
      }
    }

1089 1090
  private:
    static const unsigned int mNodesPerEdge = (ORDER + 1);
1091 1092

  };
1093

1094
}
1095

1096
shared_ptr<XdmfTopologyConverter>
1097
XdmfTopologyConverter::New()
1098
{
1099
  shared_ptr<XdmfTopologyConverter> p(new XdmfTopologyConverter());
1100
  return p;
1101 1102
}

1103 1104 1105 1106 1107 1108 1109 1110
XdmfTopologyConverter::XdmfTopologyConverter()
{
}

XdmfTopologyConverter::~XdmfTopologyConverter()
{
}

1111 1112 1113
shared_ptr<XdmfUnstructuredGrid>
XdmfTopologyConverter::convert(const shared_ptr<XdmfUnstructuredGrid> gridToConvert,
                               const shared_ptr<const XdmfTopologyType> topologyType,
1114
                               unsigned int options,
1115
                               const shared_ptr<XdmfHeavyDataWriter> heavyDataWriter) const
1116
{
1117
  // Make sure geometry and topology are non null
1118
  if(!(gridToConvert->getGeometry() && gridToConvert->getTopology()))
1119 1120 1121
    XdmfError::message(XdmfError::FATAL,
                       "Current grid's geometry or topology is null "
                       "in XdmfTopologyConverter::convert");
1122

1123
  shared_ptr<const XdmfTopologyType> topologyTypeToConvert =
1124 1125 1126 1127 1128 1129 1130
    gridToConvert->getTopology()->getType();
  if(topologyTypeToConvert == topologyType) {
    // No conversion necessary
    return gridToConvert;
  }

  if(gridToConvert->getGeometry()->getType() != XdmfGeometryType::XYZ()) {
1131 1132 1133
    XdmfError::message(XdmfError::FATAL,
                       "Grid to convert's type is not 'XYZ' in "
                       "XdmfTopologyConverter::convert");
1134 1135 1136 1137
  }

  Converter * converter = NULL;
  if(topologyTypeToConvert == XdmfTopologyType::Hexahedron()) {
1138 1139 1140
    if(topologyType == XdmfTopologyType::Hexahedron_27()) {
      converter = new HexahedronToHexahedron27();
    }
1141
    if(topologyType == XdmfTopologyType::Hexahedron_64()) {
1142 1143 1144 1145 1146 1147
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<3, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<3, false>();
      }
1148 1149
    }
    else if(topologyType == XdmfTopologyType::Hexahedron_125()) {
1150 1151 1152 1153 1154 1155
      if(options == 1) {
        converter = new HexahedronToHighOrderHexahedron<4, true>();
      }
      else {
        converter = new HexahedronToHighOrderHexahedron<4, false>();
      }
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 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
    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>();
      }
1204 1205 1206 1207
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_64()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
1208
      converter = new HighOrderHexahedronToHexahedron<3>();
1209 1210 1211 1212
    }
  }
  else if(topologyTypeToConvert == XdmfTopologyType::Hexahedron_125()) {
    if(topologyType == XdmfTopologyType::Hexahedron()) {
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
      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>();
1224 1225
    }
  }
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
  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>();
    }
  }

1247
  if(converter) {
1248
    shared_ptr<XdmfUnstructuredGrid> toReturn =
1249 1250 1251
      converter->convert(gridToConvert,
                         topologyType,
                         heavyDataWriter);
1252 1253 1254 1255
    delete converter;
    return toReturn;
  }
  else {
1256 1257 1258
    XdmfError::message(XdmfError::FATAL,
                       "Cannot convert topology type in "
                       "XdmfTopologyConverter::convert");
1259
  }
Kenneth Leiter's avatar
Kenneth Leiter committed
1260

1261
}