00001 #ifndef DUNE_SUBGRID_HH
00002 #define DUNE_SUBGRID_HH
00003
00008 #include <string>
00009 #include <map>
00010 #include <vector>
00011
00012 #include <dune/common/collectivecommunication.hh>
00013 #include <dune/grid/common/capabilities.hh>
00014 #include <dune/grid/common/grid.hh>
00015 #include <dune/common/timer.hh>
00016
00017
00018 #include "subgrid/subgridgeometry.hh"
00019 #include "subgrid/subgridentity.hh"
00020 #include "subgrid/subgridentitypointer.hh"
00021 #include "subgrid/subgridintersectioniterator.hh"
00022 #include "subgrid/subgridleveliterator.hh"
00023 #include "subgrid/subgridleafiterator.hh"
00024 #include "subgrid/subgridhierarchiciterator.hh"
00025 #include "subgrid/subgridindexstorage.hh"
00026 #include "subgrid/subgridindexsets.hh"
00027
00028 namespace Dune {
00029
00030
00032 template <class HostGridType, int codim>
00033 class SubGridHelpers {
00034 public:
00035
00036 static void markSubEntities(const typename HostGridType::template Codim<0>::Entity& entity,
00037 std::vector<std::vector<bool> > entityFields[],
00038 const HostGridType* hostGrid)
00039 {
00041 const int dim = HostGridType::dimension;
00042 const int level = entity.level();
00043
00044 for (int i=0; i<entity.template count<codim>(); i++)
00045 entityFields[dim-codim][level][hostGrid->levelIndexSet(level).template subIndex<codim>(entity,i)] = true;
00046
00047 SubGridHelpers<HostGridType,codim-1>::markSubEntities(entity, entityFields, hostGrid);
00048 }
00049 };
00050
00051
00052 template <class HostGridType>
00053 class SubGridHelpers<HostGridType,0> {
00054 public:
00055
00056 static void markSubEntities(
00057 const typename HostGridType::template Codim<0>::Entity& entity,
00058 std::vector<std::vector<bool> > entityFields[],
00059 const HostGridType* hostGrid)
00060 {}
00061 };
00062
00063
00064
00065
00066
00067 template <int dim, class HostGrid, bool MapIndexStorage>
00068 class SubGrid;
00069
00070
00071
00072
00073 template<int dim, class HostGrid, bool MapIndexStorage>
00074 struct SubGridFamily
00075 {
00076 typedef GridTraits<
00077 dim,
00078 HostGrid::dimensionworld,
00079 Dune::SubGrid<dim,HostGrid,MapIndexStorage>,
00080 SubGridGeometry,
00081 SubGridEntity,
00082 SubGridEntityPointer,
00083 SubGridLevelIterator,
00084 SubGridLeafIntersectionIterator,
00085 SubGridLevelIntersectionIterator,
00086 SubGridLeafIntersectionIterator,
00087 SubGridLevelIntersectionIterator,
00088 SubGridHierarchicIterator,
00089 SubGridLeafIterator,
00090 SubGridLevelIndexSet< const SubGrid<dim,HostGrid,MapIndexStorage> >,
00091 SubGridLevelIndexSetTypes< const SubGrid<dim,HostGrid,MapIndexStorage> >,
00092 SubGridLeafIndexSet< const SubGrid<dim,HostGrid,MapIndexStorage> >,
00093 SubGridLeafIndexSetTypes< const SubGrid<dim,HostGrid,MapIndexStorage> >,
00094 SubGridGlobalIdSet< const SubGrid<dim,HostGrid,MapIndexStorage> >,
00095 typename HostGrid::Traits::GlobalIdSet::IdType,
00096 SubGridLocalIdSet< const SubGrid<dim,HostGrid,MapIndexStorage> >,
00097 typename HostGrid::Traits::LocalIdSet::IdType,
00098 CollectiveCommunication<SubGrid<dim,HostGrid,MapIndexStorage> >
00099 > Traits;
00100 };
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00116 template <int dim, class HostGrid, bool MapIndexStorage=false>
00117 class SubGrid :
00118 public GridDefaultImplementation <dim, HostGrid::dimensionworld, double, SubGridFamily<dim,HostGrid,MapIndexStorage> >
00119 {
00120
00121 friend class SubGridLevelIndexSet<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00122 friend class SubGridLeafIndexSet<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00123 friend class SubGridGlobalIdSet<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00124 friend class SubGridLocalIdSet<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00125 friend class SubGridHierarchicIterator<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00126 friend class SubGridLevelIntersectionIterator<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00127 friend class SubGridLeafIntersectionIterator<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00128 friend class SubGridBasicIntersectionIterator<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00129 friend class SubGridIndexStorageBase<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00130
00131 friend class SubGridMapIndexStorage<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00132 friend class SubGridVectorIndexStorage<const SubGrid<dim,HostGrid,MapIndexStorage> >;
00133
00134 template<int codim, PartitionIteratorType pitype, class GridImp_>
00135 friend class SubGridLevelIterator;
00136
00137 template<int codim, PartitionIteratorType pitype, class GridImp_>
00138 friend class SubGridLeafIterator;
00139
00140
00141 template<int codim_, int dim_, class GridImp_>
00142 friend class SubGridEntity;
00143
00144 public:
00145
00147 typedef HostGrid HostGridType;
00148
00149 typedef typename SelectType<MapIndexStorage,
00150 SubGridMapIndexStorage<const SubGrid<dim,HostGrid,MapIndexStorage> >,
00151 SubGridVectorIndexStorage<const SubGrid<dim,HostGrid,MapIndexStorage> > >::Type IndexStorageType;
00152
00153
00154
00155
00156
00158 typedef SubGridFamily<dim,HostGrid,MapIndexStorage> GridFamily;
00159
00161 typedef typename SubGridFamily<dim,HostGrid,MapIndexStorage>::Traits Traits;
00162
00164 typedef typename HostGrid::ctype ctype;
00165
00166 private:
00167
00169
00170
00171 HostGrid* hostgrid_;
00172
00173 public:
00174
00175
00178 explicit SubGrid(HostGrid& hostgrid) :
00179 hostgrid_(&hostgrid),
00180 indexStorage(*this),
00181 globalIdSet_(*this),
00182 localIdSet_(*this)
00183 {
00184 maxLevelDiff_ = 1;
00185
00186 adaptationStep_ = nothingDone;
00187 hostAdaptationStep_ = nothingDone;
00188 }
00189
00190
00192 ~SubGrid()
00193 {
00194
00195 for (size_t i=0; i<levelIndexSets_.size(); i++)
00196 if (levelIndexSets_[i])
00197 delete (levelIndexSets_[i]);
00198 }
00199
00200
00202 std::string name() const
00203 {
00204 return "SubGrid";
00205 }
00206
00207
00210 int maxLevel() const {
00211 return maxLevel_;
00212 }
00213
00214
00216 template<int codim>
00217 typename Traits::template Codim<codim>::LevelIterator lbegin (int level) const{
00218 return SubGridLevelIterator<codim,All_Partition, const SubGrid<dim,HostGrid,MapIndexStorage> >(this, level);
00219 }
00220
00221
00223 template<int codim>
00224 typename Traits::template Codim<codim>::LevelIterator lend (int level) const{
00225 return SubGridLevelIterator<codim,All_Partition, const SubGrid<dim,HostGrid,MapIndexStorage> >(this, level, true);
00226 }
00227
00228
00230 template<int codim, PartitionIteratorType PiType>
00231 typename Traits::template Codim<codim>::template Partition<PiType>::LevelIterator lbegin (int level) const{
00232 return SubGridLevelIterator<codim,PiType, const SubGrid<dim,HostGrid,MapIndexStorage> >(this, level);
00233 }
00234
00235
00237 template<int codim, PartitionIteratorType PiType>
00238 typename Traits::template Codim<codim>::template Partition<PiType>::LevelIterator lend (int level) const{
00239 return SubGridLevelIterator<codim,PiType, const SubGrid<dim,HostGrid,MapIndexStorage> >(this, level, true);
00240 }
00241
00242
00244 template<int codim>
00245 typename Traits::template Codim<codim>::LeafIterator leafbegin() const {
00246 return SubGridLeafIterator<codim,All_Partition, const SubGrid<dim,HostGrid,MapIndexStorage> >(this);
00247 }
00248
00249
00251 template<int codim>
00252 typename Traits::template Codim<codim>::LeafIterator leafend() const {
00253 return SubGridLeafIterator<codim,All_Partition, const SubGrid<dim,HostGrid,MapIndexStorage> >(this, true);
00254 }
00255
00256
00258 template<int codim, PartitionIteratorType PiType>
00259 typename Traits::template Codim<codim>::template Partition<PiType>::LeafIterator leafbegin() const {
00260 return SubGridLeafIterator<codim,PiType, const SubGrid<dim,HostGrid,MapIndexStorage> >(this);
00261 }
00262
00263
00265 template<int codim, PartitionIteratorType PiType>
00266 typename Traits::template Codim<codim>::template Partition<PiType>::LeafIterator leafend() const {
00267 return SubGridLeafIterator<codim,PiType, const SubGrid<dim,HostGrid,MapIndexStorage> >(this, true);
00268 }
00269
00270
00273 int size (int level, int codim) const {
00274 return levelIndexSets_[level]->size(codim);
00275 }
00276
00277
00279 int size (int codim) const{
00280 return leafIndexSet_.size(codim);
00281 }
00282
00283
00285 int size (int level, GeometryType type) const {
00286 return levelIndexSets_[level]->size(type);
00287 }
00288
00289
00291 int size (GeometryType type) const
00292 {
00293 return leafIndexSet_.size(type);
00294 }
00295
00296
00298 const typename Traits::GlobalIdSet& globalIdSet() const{
00299 return globalIdSet_;
00300 }
00301
00302
00304 const typename Traits::LocalIdSet& localIdSet() const{
00305 return localIdSet_;
00306 }
00307
00308
00310 const typename Traits::LevelIndexSet& levelIndexSet(int level) const
00311 {
00312 if (level<0 || level>maxLevel())
00313 DUNE_THROW(GridError, "levelIndexSet of nonexisting level " << level << " requested!");
00314 return *levelIndexSets_[level];
00315 }
00316
00317
00319 const typename Traits::LeafIndexSet& leafIndexSet() const
00320 {
00321 return leafIndexSet_;
00322 }
00323
00324
00327
00328
00332 void globalRefine (int refCount)
00333 {
00334 typedef typename Traits::template Codim<0>::LeafIterator ElementLeafIterator;
00335 ElementLeafIterator it = leafbegin<0>();
00336 ElementLeafIterator end = leafend<0>();
00337 for(; it!=end; ++it)
00338 mark(1, *it);
00339 preAdapt();
00340 adapt();
00341 }
00342
00343
00345 bool mark(int refCount, const typename Traits::template Codim<0>::EntityPointer & e) DUNE_DEPRECATED
00346 {
00347 return mark(refCount, *e);
00348 }
00349
00350
00361 bool mark(int refCount, const typename Traits::template Codim<0>::Entity & e)
00362 {
00363 if (adaptationStep_==preAdaptDone)
00364 {
00365 std::cout << "You did not call adapt() after preAdapt() ! Calling adapt() automatically." << std::endl;
00366 adapt();
00367 }
00368 if (adaptationStep_==adaptDone)
00369 {
00370 std::cout << "You did not call postAdapt() after adapt() ! Calling postAdapt() automatically." << std::endl;
00371 postAdapt();
00372 }
00373 adaptationStep_ = nothingDone;
00374 hostAdaptationStep_ = nothingDone;
00375
00376 if (not(e.isLeaf()) or (refCount>1) or (refCount<-1))
00377 return false;
00378
00379 int level = e.level();
00380 int index = levelIndexSet(level).index(e);
00381
00382 if (refCount==1)
00383 {
00384 refinementMark_[level][index] = true;
00385 coarseningMark_[level][index] = false;
00386 }
00387 if (refCount==-1)
00388 {
00389 if (level>0)
00390 {
00391 refinementMark_[level][index] = false;
00392 coarseningMark_[level][index] = true;
00393 }
00394 else
00395 return false;
00396 }
00397 if (refCount==0)
00398 {
00399 refinementMark_[level][index] = false;
00400 coarseningMark_[level][index] = false;
00401 }
00402 return true;
00403 }
00404
00405
00407 int getMark(const typename Traits::template Codim<0>::EntityPointer & e) const DUNE_DEPRECATED
00408 {
00409 return getMark(*e);
00410 }
00411
00412
00417 int getMark(const typename Traits::template Codim<0>::Entity & e) const
00418 {
00419 if ((adaptationStep_==preAdaptDone) or (adaptationStep_==adaptDone))
00420 DUNE_THROW(InvalidStateException, "You can not use getMark() after preAdapt() or adapt() !");
00421
00422 if (not(e.isLeaf()))
00423 return 0;
00424
00425 int level = e.level();
00426 int index = levelIndexSet(level).index(e);
00427
00428 if (refinementMark_[level][index])
00429 return 1;
00430 if (coarseningMark_[level][index])
00431 return -1;
00432
00433 return 0;
00434 }
00435
00436
00437 private:
00438 class NeighborLevels
00439 {
00440 public:
00441 NeighborLevels(const SubGrid<dim,HostGrid,MapIndexStorage>& subgrid) :
00442 subgrid_(subgrid),
00443 maxElementLevel_(subgrid.size(dim), 0)
00444 {}
00445
00446 char getMaxNeighborLevel(const typename Traits::template Codim<0>::Entity & e) const
00447 {
00448 char maxNeighborLevel = 0;
00449 for (int i = 0; i<e.template count<dim>(); ++i)
00450 {
00451 int nodeIndex = subgrid_.leafIndexSet().template subIndex<dim>(e, i);
00452 if (maxNeighborLevel < maxElementLevel_[nodeIndex])
00453 maxNeighborLevel = maxElementLevel_[nodeIndex];
00454 }
00455 return maxNeighborLevel;
00456 }
00457
00458 void setElementLevel(const typename Traits::template Codim<0>::Entity & e, char newLevel)
00459 {
00460 for (int i = 0; i<e.template count<dim>(); ++i)
00461 {
00462 int nodeIndex = subgrid_.leafIndexSet().template subIndex<dim>(e, i);
00463 if (maxElementLevel_[nodeIndex] < newLevel)
00464 maxElementLevel_[nodeIndex] = newLevel;
00465 }
00466 }
00467
00468 private:
00469 const SubGrid<dim,HostGrid,MapIndexStorage>& subgrid_;
00470 std::vector<char> maxElementLevel_;
00471 };
00472
00473 public:
00475 bool preAdapt()
00476 {
00477 if (adaptationStep_==preAdaptDone)
00478 {
00479 std::cout << "You already called call preAdapt() ! Aborting preAdapt()." << std::endl;
00480 return false;
00481 }
00482 if (adaptationStep_==adaptDone)
00483 {
00484 std::cout << "You did not call postAdapt() after adapt() ! Calling postAdapt() automatically." << std::endl;
00485 postAdapt();
00486 }
00487 adaptationStep_ = nothingDone;
00488 hostAdaptationStep_ = nothingDone;
00489
00490 #ifdef SUBGRID_VERBOSE
00491 std::cout << "preadapt 1 (start)" << std::endl;
00492 #endif
00493
00494 typedef typename Traits::template Codim<0>::LevelIterator ElementLevelIterator;
00495 typedef typename Traits::template Codim<0>::LeafIterator ElementLeafIterator;
00496 typedef typename Traits::template Codim<0>::Entity::HierarchicIterator HierarchicIterator;
00497 typedef typename Traits::LevelIndexSet LevelIndexSet;
00498 typedef typename Traits::template Codim<0>::Entity::EntityPointer ElementPointer;
00499
00500 NeighborLevels neighborLevels(*this);
00501
00502 ElementLeafIterator it = leafbegin<0>();
00503 ElementLeafIterator end = leafend<0>();
00504 for (; it!=end; ++it)
00505 {
00506 int index = levelIndexSet(it->level()).index(*it);
00507 int newLevel = it->level();
00508 if (refinementMark_[it->level()][index])
00509 ++newLevel;
00510 if (coarseningMark_[it->level()][index])
00511 --newLevel;
00512
00513 neighborLevels.setElementLevel(*it, newLevel);
00514 }
00515
00516 for (int level=maxLevel(); level>=0; --level)
00517 {
00518 bool hasImplicitRefinementMarks = false;
00519 bool hasCoarseningMarks = false;
00520
00521 ElementLevelIterator it = lbegin<0>(level);
00522 ElementLevelIterator end = lend<0>(level);
00523 for (; it!=end; ++it)
00524 {
00525 if(it->isLeaf())
00526 {
00527
00528 char maxNeighborLevel = neighborLevels.getMaxNeighborLevel(*it);
00529
00530
00531 int minAllowedLevel = maxNeighborLevel-maxLevelDiff_;
00532
00533 int index = levelIndexSet(level).index(*it);
00534
00535
00536 if ((level<minAllowedLevel) and not(refinementMark_[level][index]))
00537 {
00538 refinementMark_[level][index] = true;
00539 coarseningMark_[level][index] = false;
00540 neighborLevels.setElementLevel(*it, level+1);
00541 hasImplicitRefinementMarks = true;
00542 }
00543
00544
00545 if ((level-1<minAllowedLevel) and (coarseningMark_[level][index]))
00546 {
00547 coarseningMark_[level][index] = false;
00548 neighborLevels.setElementLevel(*it, level);
00549 }
00550
00551 if (coarseningMark_[level][index])
00552 hasCoarseningMarks = true;
00553 }
00554 }
00555
00556
00557
00558 if (hasCoarseningMarks and hasImplicitRefinementMarks)
00559 {
00560 it = lbegin<0>(level);
00561 end = lend<0>(level);
00562 for (; it!=end; ++it)
00563 {
00564 if(it->isLeaf())
00565 {
00566
00567 char maxNeighborLevel = neighborLevels.getMaxNeighborLevel(*it);
00568
00569
00570 int minAllowedLevel = maxNeighborLevel-maxLevelDiff_;
00571
00572 int index = levelIndexSet(level).index(*it);
00573
00574
00575 if ((level-1<minAllowedLevel) and (coarseningMark_[level][index]))
00576 {
00577 coarseningMark_[level][index] = false;
00578 neighborLevels.setElementLevel(*it, level);
00579 }
00580 }
00581 }
00582 }
00583 }
00584
00585 #ifdef SUBGRID_VERBOSE
00586 std::cout << "preadapt 2 (refinement/coarsening marks for neighbours set)" << std::endl;
00587 #endif
00588
00589 bool mightBeCoarsened = false;
00590 bool callHostgridAdapt = false;
00591
00592 it = leafbegin<0>();
00593 end = leafend<0>();
00594 for (; it!=end; ++it)
00595 {
00596 int level = it->level();
00597 int index = levelIndexSet(level).index(*it);
00598
00599
00600 if (coarseningMark_[level][index])
00601 {
00602 int fatherLevel = it->father()->level();
00603 int fatherIndex = levelIndexSet(fatherLevel).index(*(it->father()));
00604
00605
00606
00607
00608
00609 if (not(refinementMark_[fatherLevel][fatherIndex]) and not(coarseningMark_[fatherLevel][fatherIndex]))
00610 {
00611
00612 HierarchicIterator hIt = it->father()->hbegin(level);
00613 HierarchicIterator hEnd = it->father()->hend(level);
00614 for (; hIt!=hEnd; ++hIt)
00615 {
00616
00617
00618 int brotherIndex = levelIndexSet(level).index(*hIt);
00619 if (not(coarseningMark_[level][brotherIndex]))
00620 {
00621 refinementMark_[fatherLevel][fatherIndex] = true;
00622 break;
00623 }
00624 }
00625
00626
00627 if (not(refinementMark_[fatherLevel][fatherIndex]))
00628 coarseningMark_[fatherLevel][fatherIndex] = true;
00629 }
00630
00631
00632
00633 if (refinementMark_[fatherLevel][fatherIndex])
00634 coarseningMark_[level][index] = false;
00635 }
00636 else
00637 {
00638 if (refinementMark_[level][index])
00639 {
00640 if (getRealImplementation(*it).hostEntity_->isLeaf())
00641 {
00642 hostgrid_->mark(1, *(getRealImplementation(*it).hostEntity_));
00643 callHostgridAdapt = true;
00644 }
00645 }
00646 }
00647 }
00648
00649 #ifdef SUBGRID_VERBOSE
00650 std::cout << "preadapt 3 (checked coarsening marks, marked host grid)" << std::endl;
00651 #endif
00652
00653 if (callHostgridAdapt)
00654 {
00655 hostgrid_->preAdapt();
00656
00657 hostAdaptationStep_ = preAdaptDone;
00658
00659 #ifdef SUBGRID_VERBOSE
00660 std::cout << "preadapt 4 (host grid preadapt called)" << std::endl;
00661 #endif
00662 }
00663
00664 adaptationStep_ = preAdaptDone;
00665
00666 return mightBeCoarsened;
00667 }
00668
00669
00671 bool adapt()
00672 {
00673 if ((adaptationStep_==nothingDone) or (adaptationStep_==postAdaptDone))
00674 {
00675 std::cout << "You did not call preAdapt() before adapt() ! Calling preAdapt() automatically." << std::endl;
00676 preAdapt();
00677 }
00678 if (adaptationStep_==adaptDone)
00679 {
00680 std::cout << "You already called call adapt() ! Aborting adapt()." << std::endl;
00681 return false;
00682 }
00683
00684 typedef typename Traits::template Codim<0>::LevelIterator ElementLevelIterator;
00685 typedef typename HostGrid::Traits::template Codim<0>::LevelIterator HostElementLevelIterator;
00686 typedef typename HostGrid::template Codim<0>::Entity::HierarchicIterator HostHierarchicIterator;
00687
00688 typedef typename Traits::GlobalIdSet GlobalIdSet;
00689 typedef typename GlobalIdSet::IdType GlobalIdType;
00690
00691 #ifdef SUBGRID_VERBOSE
00692 std::cout << "adapt 1 (start)" << std::endl;
00693 #endif
00694
00695 std::map<GlobalIdType, bool> elements;
00696 for(int level=0; level <= maxLevel(); ++level)
00697 {
00698 ElementLevelIterator it = lbegin<0>(level);
00699 ElementLevelIterator end = lend<0>(level);
00700 for (;it!=end; ++it)
00701 {
00702 int level = it->level();
00703 int index = levelIndexSet(level).index(*it);
00704
00705
00706
00707
00708
00709 if (it->isLeaf())
00710 {
00711
00712 if (not(coarseningMark_[level][index]))
00713 elements[globalIdSet().id(*it)] = refinementMark_[level][index];
00714 }
00715 else
00716 elements[globalIdSet().id(*it)] = false;
00717 }
00718 }
00719
00720 #ifdef SUBGRID_VERBOSE
00721 std::cout << "adapt 2 (element ids stored)" << std::endl;
00722 #endif
00723
00724 if (hostAdaptationStep_==preAdaptDone)
00725 {
00726 hostgrid_->adapt();
00727
00728 hostAdaptationStep_ = adaptDone;
00729
00730 #ifdef SUBGRID_VERBOSE
00731 std::cout << "adapt 3 (host grid adapt called)" << std::endl;
00732 #endif
00733 }
00734
00735 createBegin();
00736
00737
00738
00739 for(int level=0; level <= hostgrid_->maxLevel(); ++level)
00740 {
00741 HostElementLevelIterator it = hostgrid_->lbegin<0>(level);
00742 HostElementLevelIterator end = hostgrid_->lend<0>(level);
00743 for (;it!=end; ++it)
00744 {
00745 typename std::map<GlobalIdType, bool>::iterator contained = elements.find(hostgrid_->globalIdSet().id(*it));
00746 if (contained != elements.end())
00747 {
00748 insertRaw(*it);
00749 if (contained->second)
00750 {
00751 HostHierarchicIterator hIt = it->hbegin(it->level()+1);
00752 HostHierarchicIterator hEnd = it->hend(it->level()+1);
00753 for (; hIt!=hEnd; ++hIt)
00754 insertRaw(*hIt);
00755 }
00756 }
00757 }
00758 }
00759
00760 #ifdef SUBGRID_VERBOSE
00761 std::cout << "adapt 4 (subgrid elements inserted)" << std::endl;
00762 #endif
00763
00764 createEnd();
00765
00766 #ifdef SUBGRID_VERBOSE
00767 std::cout << "adapt 5 (subgrid created)" << std::endl;
00768 #endif
00769
00770 for(int level=0; level < maxLevel(); ++level)
00771 {
00772 ElementLevelIterator it = lbegin<0>(level);
00773 ElementLevelIterator end = lend<0>(level);
00774 for (;it!=end; ++it)
00775 {
00776 typename std::map<GlobalIdType, bool>::const_iterator e = elements.find(globalIdSet().id(*it));
00777 if (e != elements.end())
00778 {
00779 if (e->second == true)
00780 refinementMark_[level][levelIndexSet(level).index(*it)] = true;
00781 }
00782 }
00783 }
00784
00785 #ifdef SUBGRID_VERBOSE
00786 std::cout << "adapt 6 (wasRefined marks set)" << std::endl;
00787 #endif
00788
00789 adaptationStep_ = adaptDone;
00790
00791 return true;
00792 }
00793
00794
00796 void postAdapt(){
00797 if (adaptationStep_==nothingDone)
00798 {
00799 std::cout << "You did not call preAdapt() before adapt() ! Calling preAdapt() automatically." << std::endl;
00800 preAdapt();
00801 }
00802 if (adaptationStep_==preAdaptDone)
00803 {
00804 std::cout << "You did not call adapt() before postAdaptt() ! Calling adapt() automatically." << std::endl;
00805 adapt();
00806 }
00807 if (adaptationStep_==postAdaptDone)
00808 {
00809 std::cout << "You already called call postAdapt() ! Aborting postAdapt()." << std::endl;
00810 return;
00811 }
00812
00813 #ifdef SUBGRID_VERBOSE
00814 std::cout << "postadapt 1 (start)" << std::endl;
00815 #endif
00816
00817 for (int level=0; level<=maxLevel(); ++level)
00818 refinementMark_[level].assign(refinementMark_[level].size(), false);
00819
00820 if (hostAdaptationStep_ == adaptDone)
00821 {
00822 hostgrid_->postAdapt();
00823
00824 hostAdaptationStep_ = postAdaptDone;
00825
00826 #ifdef SUBGRID_VERBOSE
00827 std::cout << "postadapt 2 (host grid postadapt called)" << std::endl;
00828 #endif
00829 }
00830
00831 adaptationStep_ = postAdaptDone;
00832
00833 return;
00834 }
00835
00840 unsigned int overlapSize(int codim) const {
00841 return hostgrid_->overlapSize(codim);
00842 }
00843
00844
00846 unsigned int ghostSize(int codim) const {
00847 return hostgrid_->ghostSize(codim);
00848 }
00849
00850
00852 unsigned int overlapSize(int level, int codim) const {
00853 return hostgrid_->overlapSize(level,codim);
00854 }
00855
00856
00858 unsigned int ghostSize(int level, int codim) const {
00859 return hostgrid_->ghostSize(level,codim);
00860 }
00861
00862
00863 #if 0
00864
00869 void loadBalance(int strategy, int minlevel, int depth, int maxlevel, int minelement){
00870 DUNE_THROW(NotImplemented, "SubGrid::loadBalance()");
00871 }
00872
00884 template<class T, template<class> class P, int codim>
00885 void communicate (T& t, InterfaceType iftype, CommunicationDirection dir, int level);
00886
00891 template<class DataHandle>
00892 void communicate (DataHandle& data, InterfaceType iftype, CommunicationDirection dir, int level) const
00893 {}
00894
00895 template<class DataHandle>
00896 void communicate (DataHandle& data, InterfaceType iftype, CommunicationDirection dir) const
00897 {}
00898 #endif
00899
00900
00902 const CollectiveCommunication<SubGrid>& comm () const
00903 {
00904 return ccobj;
00905 }
00906
00907
00908
00909
00910
00911
00914
00926 void createBegin()
00927 {
00928 for (int i=0; i<=dim; ++i) {
00929 entities_[i].resize(hostgrid_->maxLevel()+1);
00930
00931 for (int j=0; j<=hostgrid_->maxLevel(); ++j)
00932 {
00933 entities_[i][j].resize(hostgrid_->size(j, dim-i));
00934 entities_[i][j].assign(entities_[i][j].size(), false);
00935 }
00936 }
00937 insertRawCalled = false;
00938 insertPartialCalled = false;
00939 }
00940
00941
00950 void createEnd()
00951 {
00952 typedef typename HostGridType::template Codim<0>::LevelIterator HostElementLevelIterator;
00953
00954
00955
00956 setMaxLevel();
00957
00958
00959
00960
00961
00962 for (int i=0; i<=maxLevel(); i++)
00963 {
00964 HostElementLevelIterator eIt = hostgrid_->template lbegin<0>(i);
00965 HostElementLevelIterator eEndIt = hostgrid_->template lend<0>(i);
00966
00967 for (; eIt!=eEndIt; ++eIt)
00968 {
00969
00970
00971 if (entities_[dim][i][hostgrid_->levelIndexSet(i).index(*eIt)])
00972 SubGridHelpers<HostGridType, dim>::markSubEntities(*eIt, entities_, hostgrid_);
00973 }
00974 }
00975
00976
00977
00978
00979 setIndices();
00980
00981 refinementMark_.resize(maxLevel_+1);
00982 coarseningMark_.resize(maxLevel_+1);
00983 for (int j=0; j<=maxLevel_; j++)
00984 {
00985 refinementMark_[j].resize(size(j, 0), false);
00986 coarseningMark_[j].resize(size(j, 0), false);
00987 refinementMark_[j].assign(refinementMark_[j].size(), false);
00988 coarseningMark_[j].assign(coarseningMark_[j].size(), false);
00989 }
00990 }
00991
00992
01002 void insertRaw(const typename HostGrid::Traits::template Codim<HostGrid::dimension-dim>::Entity & e )
01003 {
01004 insertRawCalled = true;
01005
01006 const int level = e.level();
01007
01008
01009 entities_[dim][level][hostgrid_->levelIndexSet(level).index(e)] = true;
01010 }
01011
01012
01024 void add(const typename HostGrid::Traits::template Codim<HostGrid::dimension-dim>::EntityPointer & e ) DUNE_DEPRECATED
01025 {
01026 insertRaw(*e);
01027 }
01028
01029
01030
01036 void insertLevel(int level)
01037 {
01038 for (int i=0; i<=dim; ++i)
01039 {
01040 for (int l=0; l<=level; ++l)
01041 entities_[i][l].assign(entities_[i][l].size(), true);
01042 }
01043 }
01044
01045
01053 void addLevel(int level) DUNE_DEPRECATED
01054 {
01055 insertLevel(level);
01056 }
01057
01058
01064 void insertLeaf()
01065 {
01066 for (int i=0; i<=dim; ++i)
01067 {
01068 for (int l=0; l<=hostgrid_->maxLevel(); ++l)
01069 entities_[i][l].assign(entities_[i][l].size(), true);
01070 }
01071 }
01072
01073
01081 void addLeaf() DUNE_DEPRECATED
01082 {
01083 insertLeaf();
01084 }
01085
01086
01092 template <class Container>
01093 void insertSet(const Container& idContainer)
01094 {
01095 typedef typename HostGrid::Traits::template Codim<0>::LevelIterator HostElementLevelIterator;
01096
01097 for(int level=0; level <= hostgrid_->maxLevel(); ++level)
01098 {
01099 HostElementLevelIterator it = hostgrid_->lbegin<0>(level);
01100 HostElementLevelIterator end = hostgrid_->lend<0>(level);
01101 for (;it!=end; ++it)
01102 {
01103 typename Container::const_iterator contained = idContainer.find(hostgrid_->globalIdSet().id(*it));
01104 if (contained != idContainer.end())
01105 insert(*it);
01106 }
01107 }
01108 }
01109
01110
01118 template <class Container>
01119 void addAllWithAncestor(const Container& idContainer)
01120 {
01121 insertSet(idContainer);
01122 }
01123
01124
01130 void insert(const typename HostGrid::Traits::template Codim<HostGrid::dimension-dim>::Entity & e )
01131 {
01132 typedef typename HostGrid::template Codim<0>::Entity::HierarchicIterator HostGridHierarchicIterator;
01133 typedef typename HostGrid::template Codim<0>::Entity::EntityPointer HostGridElementPointer;
01134
01135
01136 int level = e.level();
01137 if (level == 0)
01138 entities_[dim][level][hostgrid_->levelIndexSet(level).index(e)] = true;
01139 else
01140 {
01141 HostGridElementPointer father = e.father();
01142
01143 HostGridHierarchicIterator childrenIt = father->hbegin(level);
01144 HostGridHierarchicIterator childrenEnd = father->hend(level);
01145 for(; childrenIt!=childrenEnd; ++childrenIt)
01146 entities_[dim][level][hostgrid_->levelIndexSet(level).index(*childrenIt)] = true;
01147
01148
01149 bool fatherInserted = entities_[dim][father->level()][hostgrid_->levelIndexSet(father->level()).index(*father)];
01150 if (not(fatherInserted) or insertRawCalled or insertPartialCalled)
01151 insert(*father);
01152 }
01153 }
01154
01155
01163 void addWithAncestor(const typename HostGrid::Traits::template Codim<HostGrid::dimension-dim>::EntityPointer & e ) DUNE_DEPRECATED
01164 {
01165 insert(*e);
01166 }
01167
01168
01177 void insertPartial(const typename HostGrid::Traits::template Codim<HostGrid::dimension-dim>::Entity & e )
01178 {
01179 insertPartialCalled = true;;
01180
01181 typedef typename HostGrid::template Codim<0>::Entity::EntityPointer HostGridElementPointer;
01182
01183 int level = e.level();
01184 entities_[dim][level][hostgrid_->levelIndexSet(level).index(e)] = true;
01185
01186 if (level > 0)
01187 {
01188 HostGridElementPointer father = e.father();
01189
01190 bool fatherInserted = entities_[dim][father->level()][hostgrid_->levelIndexSet(father->level()).index(*father)];
01191 if (not(fatherInserted) or insertRawCalled)
01192 insertPartial(*father);
01193 }
01194 }
01195
01196
01207 void addPartial(const typename HostGrid::Traits::template Codim<HostGrid::dimension-dim>::EntityPointer & e ) DUNE_DEPRECATED
01208 {
01209 insertPartial(*e);
01210 }
01211
01217
01218
01220 template <int codim>
01221 bool contains(const typename HostGrid::Traits::template Codim<codim>::Entity& e ) const
01222 {
01223 const int level = e.level();
01224 return entities_[dim-codim][level][hostgrid_->levelIndexSet(level).index(e)];
01225 }
01226
01227
01229
01230 template <int codim>
01231 bool contains(const typename HostGrid::Traits::template Codim<codim>::EntityPointer& e ) const
01232 {
01233 return contains<codim>(*e);
01234 }
01235
01236
01238 HostGridType& getHostGrid() const
01239 {
01240 return *hostgrid_;
01241 }
01242
01243
01245 template <int codim>
01246 typename HostGrid::Traits::template Codim<codim>::EntityPointer getHostEntity(const typename Traits::template Codim<codim>::Entity& e) const
01247 {
01248 return getRealImplementation(e).hostEntity_;
01249 }
01250
01251
01253
01254 template <int codim>
01255 typename Traits::template Codim<codim>::EntityPointer getSubGridEntity(const typename HostGrid::Traits::template Codim<codim>::Entity& e) const
01256 {
01257 if (not(contains(e)))
01258 DUNE_THROW(GridError, "Hostgrid entity is not contained in the subgrid!");
01259 typedef typename HostGrid::Traits::template Codim<codim>::EntityPointer HostGridEntityPointer;
01260 return SubGridEntityPointer<codim,const SubGrid<dim,HostGrid,MapIndexStorage> >(this, HostGridEntityPointer(e));
01261 }
01262
01263
01269 bool hostGridAdapted ()
01270 {
01271 return (hostAdaptationStep_!=nothingDone);
01272 }
01273
01274
01276 void setMaxLevelDifference(int maxLevelDiff)
01277 {
01278 maxLevelDiff_ = maxLevelDiff;
01279 return;
01280 }
01281
01282
01284 template<class ElementTransfer>
01285 void transfer(ElementTransfer& elementTransfer) const
01286 {
01287 typedef typename Traits::template Codim<0>::LeafIterator ElementLeafIterator;
01288 typedef typename HostGrid::template Codim<0>::Entity::EntityPointer HostGridElementPointer;
01289 typedef typename HostGrid::template Codim<0>::Entity::HierarchicIterator HostGridHierarchicIterator;
01290
01291 int hostMaxLevel = hostgrid_->maxLevel();
01292
01293 elementTransfer.pre();
01294
01295 ElementLeafIterator it = leafbegin<0>();
01296 ElementLeafIterator end = leafend<0>();
01297 for (; it!=end; ++it)
01298 {
01299 HostGridElementPointer hostElement = getHostEntity<0>(*it);
01300 if (hostElement->isLeaf())
01301 elementTransfer.transfer(it, hostElement);
01302 else
01303 {
01304 HostGridHierarchicIterator hIt = hostElement->hbegin(hostMaxLevel);
01305 HostGridHierarchicIterator hEnd = hostElement->hend(hostMaxLevel);
01306 for (; hIt!=hEnd; ++hIt)
01307 {
01308 if (hIt->isLeaf())
01309 elementTransfer.transfer(it, hIt);
01310 }
01311 }
01312 }
01313 elementTransfer.post();
01314 }
01315
01316
01340 bool shrinkHostGrid(int maxAdaptations = 100, bool recreateSubgrid = true)
01341 {
01342 if (adaptationStep_==preAdaptDone)
01343 {
01344 std::cout << "You did not call adapt() after preAdapt() ! Calling adapt() automatically." << std::endl;
01345 adapt();
01346 }
01347 if (adaptationStep_==adaptDone)
01348 {
01349 std::cout << "You did not call postAdapt() after adapt() ! Calling postAdapt() automatically." << std::endl;
01350 postAdapt();
01351 }
01352 adaptationStep_ = nothingDone;
01353 hostAdaptationStep_ = nothingDone;
01354
01355 typedef typename Traits::template Codim<0>::LeafIterator ElementLeafIterator;
01356 typedef typename HostGrid::Traits::template Codim<0>::LeafIterator HostElementLeafIterator;
01357 typedef typename HostGrid::Traits::template Codim<0>::LevelIterator HostElementLevelIterator;
01358
01359 typedef typename Traits::GlobalIdSet GlobalIdSet;
01360 typedef typename GlobalIdSet::IdType GlobalIdType;
01361
01362
01363 std::map<GlobalIdType, int> elements;
01364
01365
01366 int subgridMaxLevel = maxLevel();
01367 ElementLeafIterator it = leafbegin<0>();
01368 ElementLeafIterator end = leafend<0>();
01369 for (;it!=end; ++it)
01370 elements[globalIdSet().id(*it)] = it->level();
01371
01372
01373 bool hostGridChanged = true;
01374 int hostGridLeafElementSize;
01375 int adaptations = 0;
01376 while ((adaptations<maxAdaptations) and hostGridChanged)
01377 {
01378 hostGridLeafElementSize = hostgrid_->size(0);
01379
01380 HostElementLeafIterator it = hostgrid_->leafbegin<0>();
01381 HostElementLeafIterator end = hostgrid_->leafend<0>();
01382 for (;it!=end; ++it)
01383 {
01384 if (it->level() > subgridMaxLevel)
01385 hostgrid_->mark(-1, *it);
01386 else
01387 {
01388 typename std::map<GlobalIdType, int>::const_iterator found = elements.find(hostgrid_->globalIdSet().id(*it));
01389 if (found == elements.end())
01390 hostgrid_->mark(-1, *it);
01391 else if (found->second < it->level())
01392 hostgrid_->mark(-1, *it);
01393 }
01394 }
01395 hostgrid_->preAdapt();
01396 hostgrid_->adapt();
01397 hostgrid_->postAdapt();
01398
01399 ++adaptations;
01400 if (hostGridLeafElementSize == hostgrid_->size(0))
01401 hostGridChanged = false;
01402 }
01403
01404
01405 if (recreateSubgrid)
01406 {
01407 createBegin();
01408 for(int level=0; level <= subgridMaxLevel; ++level)
01409 {
01410 HostElementLevelIterator it = hostgrid_->lbegin<0>(level);
01411 HostElementLevelIterator end = hostgrid_->lend<0>(level);
01412 for (;it!=end; ++it)
01413 {
01414 typename std::map<GlobalIdType, int>::iterator found = elements.find(hostgrid_->globalIdSet().id(*it));
01415 if (found != elements.end())
01416 {
01417 if (found->second == it->level())
01418 insertPartial(*it);
01419 }
01420 }
01421 }
01422 createEnd();
01423 }
01424
01425 return hostGridChanged;
01426 }
01427
01428
01430 void report() const
01431 {
01432 indexStorage.report();
01433
01434 std::cout << "Subgrid covers (leaf level): ";
01435 std::cout << size(dim) << " of " << hostgrid_->size(dim) << " nodes";
01436 std::cout << " ~ " << size(dim) *100 / hostgrid_->size(dim) << " %" << std::endl;
01437 std::cout.precision(8);
01438
01439 std::cout << " ";
01440 std::cout << size(0) << " of " << hostgrid_->size(0) << " elements";
01441 std::cout.precision(3);
01442 std::cout << " ~ " << size(0) *100 / hostgrid_->size(0) << " %" << std::endl;
01443 std::cout.precision(8);
01444
01445 return;
01446 }
01447
01451 protected:
01452
01454 template <int codim>
01455 bool isLeaf(const typename Traits::template Codim<codim>::Entity& e) const
01456 {
01457 return indexStorage.template isLeaf<codim>(e);
01458 }
01459
01461 template <int codim>
01462 bool isLeaf(const typename HostGrid::Traits::template Codim<codim>::Entity& e) const
01463 {
01464 return indexStorage.template isLeaf<codim>(e);
01465 }
01466
01468 bool wasRefined(const typename Traits::template Codim<0>::Entity& e) const
01469 {
01470 if (adaptationStep_!=adaptDone)
01471 return false;
01472
01473 int level = e.level();
01474 int index = levelIndexSet(level).index(e);
01475 return refinementMark_[level][index];
01476 }
01477
01479 IndexStorageType indexStorage;
01480
01481
01482 private:
01483
01485 void setMaxLevel() {
01486 maxLevel_ = -1;
01487 for (int i=entities_[dim].size()-1; i>=0; i--)
01488 if (countBits(entities_[dim][i]) > 0) {
01489 maxLevel_ = i;
01490 break;
01491 }
01492 }
01493
01494 int countBits(const std::vector<bool>& bits) const {
01495 int n = 0;
01496 for (size_t i=0; i<bits.size(); i++)
01497 n += (bits[i]) ? 1 : 0;
01498 return n;
01499 }
01500
01501
01503 void setIndices()
01504 {
01505 #ifdef SUBGRID_VERBOSE
01506 Timer timer;
01507 #endif
01508 localIdSet_.update();
01509
01510 globalIdSet_.update();
01511
01512 indexStorage.update();
01513
01514
01515
01516
01517 for (int i=levelIndexSets_.size(); i<=maxLevel(); i++)
01518 {
01519 SubGridLevelIndexSet<const SubGrid<dim,HostGrid,MapIndexStorage> >* p
01520 = new SubGridLevelIndexSet<const SubGrid<dim,HostGrid,MapIndexStorage> >();
01521 levelIndexSets_.push_back(p);
01522 }
01523
01524 for (int i=0; i<=maxLevel(); i++)
01525 if (levelIndexSets_[i])
01526 levelIndexSets_[i]->update(*this, i);
01527
01528 leafIndexSet_.update(*this);
01529
01530 #ifdef SUBGRID_VERBOSE
01531 std::cout << "SubGrid indices set in " << timer.elapsed() << " seconds." << std::endl;
01532 #endif
01533 }
01534
01538 std::vector<std::vector<bool> > entities_[dim+1];
01539
01541 std::vector<std::vector<bool> > refinementMark_;
01542
01544 std::vector<std::vector<bool> > coarseningMark_;
01545
01547 int maxLevel_;
01548
01550 CollectiveCommunication<SubGrid> ccobj;
01551
01553 std::vector<SubGridLevelIndexSet<const SubGrid<dim,HostGrid,MapIndexStorage> >*> levelIndexSets_;
01554
01556 SubGridLeafIndexSet<const SubGrid<dim,HostGrid,MapIndexStorage> > leafIndexSet_;
01557
01559 SubGridGlobalIdSet<const SubGrid<dim,HostGrid,MapIndexStorage> > globalIdSet_;
01560
01562 SubGridLocalIdSet<const SubGrid<dim,HostGrid,MapIndexStorage> > localIdSet_;
01563
01565 int maxLevelDiff_;
01566
01568 enum AdaptationStep {nothingDone, preAdaptDone, adaptDone, postAdaptDone};
01569
01571 AdaptationStep adaptationStep_;
01572
01574 AdaptationStep hostAdaptationStep_;
01575
01577 bool insertRawCalled;
01578
01580 bool insertPartialCalled;
01581
01582 };
01583
01584
01585
01586
01587 namespace Capabilities
01588 {
01590 template<int dim, class HostGrid, int codim>
01591 struct hasEntity< SubGrid<dim,HostGrid,false>, codim>
01592 {
01593 static const bool v = hasEntity<HostGrid,codim>::v;
01594 };
01595
01596
01598 template<int dim, class HostGrid, int MapIndexStorage>
01599 struct isParallel< SubGrid<dim,HostGrid,MapIndexStorage> >
01600 {
01601 static const bool v = isParallel<HostGrid>::v;
01602 };
01603
01604
01606 template<int dim, class HostGrid, int MapIndexStorage>
01607 struct hasHangingNodes< SubGrid<dim,HostGrid,MapIndexStorage> >
01608 {
01609 static const bool v = hasHangingNodes<HostGrid>::v;
01610 };
01611
01613 template<int dim, class HostGrid, int MapIndexStorage>
01614 struct isLevelwiseConforming< SubGrid<dim,HostGrid,MapIndexStorage> >
01615 {
01616 static const bool v = isLevelwiseConforming<HostGrid>::v;
01617 };
01618
01620 template<int dim, class HostGrid, int MapIndexStorage>
01621 struct isLeafwiseConforming< SubGrid<dim,HostGrid,MapIndexStorage> >
01622 {
01623 static const bool v = false;
01624 };
01625 }
01626
01627 }
01628
01629 #endif