3 #ifndef DUNE_FOAMGRID_HH
4 #define DUNE_FOAMGRID_HH
16 #include <type_traits>
19 #include <dune/common/version.hh>
20 #if DUNE_VERSION_LT(DUNE_COMMON,2,7)
21 #include <dune/common/parallel/collectivecommunication.hh>
23 #include <dune/common/parallel/communication.hh>
26 #include <dune/common/stdstreams.hh>
27 #include <dune/grid/common/capabilities.hh>
28 #include <dune/grid/common/grid.hh>
48 template <
int dimgr
id,
int dimworld,
class ctype =
double>
53 template<
int dimgr
id,
int dimworld,
class ctype>
75 CollectiveCommunication<Dune::FoamGrid<dimgrid, dimworld, ctype> > ,
76 DefaultLevelGridViewTraits,
77 DefaultLeafGridViewTraits,
89 template <
int dimgr
id,
int dimworld,
class ct>
91 public GridDefaultImplementation <dimgrid, dimworld, ct, FoamGridFamily<dimgrid, dimworld, ct> >
102 template<
int codim, PartitionIteratorType pitype,
class Gr
idImp_>
105 template<
int codim, PartitionIteratorType pitype,
class Gr
idImp_>
108 template <
class Gr
idType_>
111 template <
int dimgr
id_,
int dimworld_,
class ct_>
114 template<
int codim_,
int dim_,
class Gr
idImp_>
120 static_assert(dimgrid==1 || dimgrid==2,
"Use FoamGrid only for 1d and 2d grids!");
138 : entityImps_(makeEntityImps_())
140 , leafIndexSet_(*this)
143 , numBoundarySegments_(0)
151 for (
size_t i=0; i<levelIndexSets_.size(); i++)
152 if (levelIndexSets_[i])
153 delete (levelIndexSets_[i]);
160 return entityImps_.size()-1;
166 typename Traits::template Codim<codim>::LevelIterator
lbegin (
int level)
const {
168 DUNE_THROW(Dune::GridError,
"LevelIterator in nonexisting level " << level <<
" requested!");
176 typename Traits::template Codim<codim>::LevelIterator
lend (
int level)
const {
178 DUNE_THROW(GridError,
"LevelIterator in nonexisting level " << level <<
" requested!");
185 template<
int codim, PartitionIteratorType PiType>
186 typename Traits::template Codim<codim>::template Partition<PiType>::LevelIterator
lbegin (
int level)
const {
188 DUNE_THROW(Dune::GridError,
"LevelIterator in nonexisting level " << level <<
" requested!");
195 template<
int codim, PartitionIteratorType PiType>
196 typename Traits::template Codim<codim>::template Partition<PiType>::LevelIterator
lend (
int level)
const {
198 DUNE_THROW(GridError,
"LevelIterator in nonexisting level " << level <<
" requested!");
206 typename Traits::template Codim<codim>::LeafIterator
leafbegin()
const {
213 typename Traits::template Codim<codim>::LeafIterator
leafend()
const {
219 template<
int codim, PartitionIteratorType PiType>
220 typename Traits::template Codim<codim>::template Partition<PiType>::LeafIterator
leafbegin()
const {
226 template<
int codim, PartitionIteratorType PiType>
227 typename Traits::template Codim<codim>::template Partition<PiType>::LeafIterator
leafend()
const {
234 int size (
int level,
int codim)
const {
237 if ((codim==2 && dimgrid==2) || (codim==1 && dimgrid==1))
238 return std::get<0>(entityImps_[level]).size();
239 if ((codim==1 && dimgrid==2))
240 return std::get<1>(entityImps_[level]).size();
242 return std::get<dimgrid>(entityImps_[level]).size();
255 int size (
int level, GeometryType type)
const {
261 int size (GeometryType type)
const
269 return numBoundarySegments_;
288 DUNE_THROW(GridError,
"LevelIndexSet for nonexisting level " << level <<
" requested!");
290 if (! levelIndexSets_[level])
293 levelIndexSets_[level]->update();
295 return *levelIndexSets_[level];
302 return leafIndexSet_;
306 template <
class EntitySeed>
307 static typename Traits::template Codim<EntitySeed::codimension>::Entity
310 const int codim = EntitySeed::codimension;
311 typedef typename Traits::template Codim<codim>::Entity Entity;
335 bool mark(
int refCount,
const typename Traits::template Codim<0>::Entity & e)
355 int getMark(
const typename Traits::template Codim<0>::Entity & e)
const
357 switch(e.impl().target_->markState_)
389 void setPosition(
const typename Traits::template Codim<dimgrid>::Entity & e,
390 const FieldVector<ctype, dimworld>& pos);
401 if(!growing_) initializeGrowth_();
420 const std::vector<unsigned int>& vertices)
423 assert(type.isTriangle() || type.isLine());
424 assert(vertices.size() == dimgrid + 1);
429 assert(vertices.size() == newElement.vertex_.size());
431 for(std::size_t i = 0; i < vertices.size(); i++)
433 if(
int(vertices[i]) >= this->leafGridView().
size(dimgrid))
436 auto vIt = verticesToInsert_.begin();
437 std::advance(vIt, vertices[i] - this->leafGridView().
size(dimgrid));
438 newElement.vertex_[i] = &*vIt;
443 if(!growing_) initializeGrowth_();
445 assert(indexToVertexMap_[vertices[i]]->isLeaf());
446 newElement.vertex_[i] = indexToVertexMap_[vertices[i]];
449 newElement.isNew_ =
true;
450 newElement.growthInsertionIndex_ = elementsToInsert_.size()-1;
451 return newElement.growthInsertionIndex_;
462 const std::vector<unsigned int>& vertices,
463 const std::shared_ptr<VirtualFunction<FieldVector<ctype,dimgrid>,FieldVector<ctype,dimworld> > >& elementParametrization)
467 elementsToInsert_.back().elementParametrization_ = elementParametrization;
499 int idx = e.impl().target_->growthInsertionIndex_;
501 return static_cast<unsigned int>(idx);
514 int idx = e.impl().target_->growthInsertionIndex_;
516 return static_cast<unsigned int>(idx);
549 template<
class DataHandle>
558 DUNE_THROW(Dune::NotImplemented,
"Load balancing not implemented. Foamgrid does not run in parallel yet!");
573 template<
class T,
template<
class>
class P,
int codim>
574 void communicate (T& t, InterfaceType iftype, CommunicationDirection dir,
int level)
const
580 template<
class DataHandle>
581 void communicate (DataHandle& data, InterfaceType iftype, CommunicationDirection dir,
int level)
const
584 template<
class DataHandle>
585 void communicate (DataHandle& data, InterfaceType iftype, CommunicationDirection dir)
const
589 const typename Traits::CollectiveCommunication&
comm ()
const
603 void initializeGrowth_()
606 indexToVertexMap_.resize(this->leafGridView().
size(dimgrid));
607 for (
const auto& vertex : vertices(this->leafGridView()))
618 void erasePointersToEntities(std::list<FoamGridEntityImp<dimgrid, dimgrid ,dimworld, ctype> >& elements);
625 void eraseVanishedEntities(std::list<FoamGridEntityImp<i, dimgrid, dimworld, ctype> >& levelEntities);
629 void coarsenSimplexElement(FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>& element);
634 void refineSimplexElement(FoamGridEntityImp<2, dimgrid, dimworld, ctype>& element,
637 void refineSimplexElement(FoamGridEntityImp<1, dimgrid, dimworld, ctype>& element,
641 bool removeSimplexElement(FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>& element);
652 void overwriteFineLevelNeighbours(FoamGridEntityImp<dimgrid-1, dimgrid, dimworld, ctype>& edge,
653 const FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>* son,
654 const FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>* father);
662 void addElementForFacet(
const FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>* element,
663 FoamGridEntityImp<dimgrid-1, dimgrid, dimworld, ctype>* facet);
666 void addNewFacet(FoamGridEntityImp<0, dimgrid, dimworld, ctype>* &facet,
667 std::array<FoamGridEntityImp<0, dimgrid, dimworld, ctype>*,dimgrid> vertexArray,
671 void addNewFacet(FoamGridEntityImp<1, dimgrid, dimworld, ctype>* &facet,
672 std::array<FoamGridEntityImp<0, dimgrid, dimworld, ctype>*,dimgrid> vertexArray,
681 unsigned int getNextFreeId()
683 return freeIdCounter_++;
687 void computeTwoZeroConnectivity()
690 for(
int level = 0; level <=
maxLevel(); ++level)
693 for(
auto&& vertex : std::get<0>(entityImps_[level]))
694 vertex.elements_.clear();
696 for(
auto eIt = std::get<dimgrid>(entityImps_[level]).begin(); eIt != std::get<dimgrid>(entityImps_[level]).end(); ++eIt)
697 for(
auto&& vertex : eIt->vertex_)
698 vertex->elements_.push_back(&*eIt);
703 typename Traits::CollectiveCommunication ccobj_;
705 template<std::size_t... dimEntity>
706 static auto makeEntityImpsImpl_(std::index_sequence<dimEntity...>, std::size_t numLevels)
707 {
return std::vector<std::tuple<std::list<FoamGridEntityImp<dimEntity, dimgrid, dimworld, ctype>>...>>(numLevels); }
710 static auto makeEntityImps_(std::size_t numLevels = 1)
711 {
return makeEntityImpsImpl_(std::make_index_sequence<dimgrid+1>{}, numLevels); }
714 using EntityImps = std::decay_t<decltype(makeEntityImps_())>;
717 using EntityTuple =
typename EntityImps::value_type;
720 EntityImps entityImps_;
723 mutable std::vector<FoamGridLevelIndexSet<const FoamGrid>*> levelIndexSets_;
732 unsigned int freeIdCounter_;
738 std::size_t numBoundarySegments_;
744 std::vector<FoamGridEntityImp<0, dimgrid, dimworld, ctype>* > indexToVertexMap_;
747 std::vector<FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype>* > elementsToRemove_;
750 std::list<FoamGridEntityImp<0, dimgrid, dimworld, ctype> > verticesToInsert_;
753 std::list<FoamGridEntityImp<dimgrid, dimgrid, dimworld, ctype> > elementsToInsert_;
763 namespace Capabilities
769 template<
int dimgr
id,
int dimworld,
class ctype,
int codim>
770 struct hasEntity<
FoamGrid<dimgrid, dimworld, ctype>, codim>
772 static const bool v =
true;
776 template <
int dimgr
id,
int dimworld,
class ctype>
777 struct isLevelwiseConforming<
FoamGrid<dimgrid, dimworld, ctype> >
779 static const bool v =
false;
783 template <
int dimgr
id,
int dimworld,
class ctype>
784 struct isLeafwiseConforming<
FoamGrid<dimgrid, dimworld, ctype> >
786 static const bool v =
false;
The FoamGridEntity class.
Implementation of EntitySeed for the FoamGrid grid manager.
The specialization of the generic GridFactory for FoamGrid.
The FoamGridGeometry class.
The FoamGridHierarchicIterator class.
The index and id sets for the FoamGrid class.
The FoamGridLeafIntersectionIterator and FoamGridLevelIntersectionIterator classes.
The FoamGridLeafIterator class.
The FoamGridLevelIterator class.
Element specialization of FoamGridEntityImp. Element is a grid entity of topological codimension 0 an...
Definition: foamgridelements.hh:18
The implementation of entities in a FoamGrid.
Definition: foamgridentity.hh:54
Iterator over all entities of a given codimension and level of a grid.
Definition: foamgridleveliterator.hh:18
Definition: foamgridintersectioniterators.hh:239
Iterator over all element neighborsMesh entities of codimension 0 ("elements") allow to visit all nei...
Definition: foamgridintersectioniterators.hh:28
Iterator over the descendants of an entity.Mesh entities of codimension 0 ("elements") allow to visit...
Definition: foamgridhierarchiciterator.hh:24
The EntitySeed class provides the minmal information needed to restore an Entity using the grid.
Definition: foamgridentityseed.hh:21
Specialization of the generic GridFactory for FoamGrid<dimgrid, dimworld>
Definition: foamgridfactory.hh:31
Definition: foamgridgeometry.hh:21
Definition: foamgridindexsets.hh:26
Definition: foamgridindexsets.hh:200
Definition: foamgridindexsets.hh:417
Definition: foamgridintersections.hh:252
Iterator over all element neighborsMesh entities of codimension 0 ("elements") allow to visit all nei...
Definition: foamgridintersections.hh:386
Iterator over all entities of a given codimension and level of a grid.
Definition: foamgridleafiterator.hh:15
The actual entity implementation.
Definition: foamgridvertex.hh:47
Vertex specialization of FoamGridEntityImp.
Definition: foamgridvertex.hh:53
int growthInsertionIndex_
If this vertex was created in a growth step this will be the index of insertion So if this is the fir...
Definition: foamgridvertex.hh:148
bool isNew_
If the vertex was newly inserted (at run-time)
Definition: foamgridvertex.hh:141
void removeElement(const typename Traits::template Codim< 0 >::Entity &e)
Mark an element for removal from the grid.
Definition: foamgrid.hh:474
const Traits::CollectiveCommunication & comm() const
Definition: foamgrid.hh:589
unsigned int overlapSize(int level, int codim) const
Size of the overlap on a given level.
Definition: foamgrid.hh:537
int size(int level, GeometryType type) const
number of entities per level, codim and geometry type in this process
Definition: foamgrid.hh:255
Traits::template Codim< codim >::template Partition< PiType >::LeafIterator leafend() const
one past the end of the sequence of leaf entities
Definition: foamgrid.hh:227
size_t numBoundarySegments() const
The number of boundary edges on the coarsest level.
Definition: foamgrid.hh:267
Traits::template Codim< codim >::template Partition< PiType >::LevelIterator lbegin(int level) const
Iterator to first entity of given codim on level.
Definition: foamgrid.hh:186
void setPosition(const typename Traits::template Codim< dimgrid >::Entity &e, const FieldVector< ctype, dimworld > &pos)
Sets a (leaf) vertex to a new position.
Definition: foamgrid.cc:1416
void communicate(T &t, InterfaceType iftype, CommunicationDirection dir, int level) const
The communication interface.
Definition: foamgrid.hh:574
const Traits::LocalIdSet & localIdSet() const
Access to the LocalIdSet.
Definition: foamgrid.hh:279
FoamGrid()
Constructor, constructs an empty grid.
Definition: foamgrid.hh:137
Traits::template Codim< codim >::LevelIterator lbegin(int level) const
Iterator to first entity of given codim on level.
Definition: foamgrid.hh:166
friend class FoamGridLeafIndexSet< const FoamGrid >
Definition: foamgrid.hh:95
friend class GridFactory
Definition: foamgrid.hh:109
bool preGrow()
Book-keeping routine to be called before growth.
Definition: foamgrid.cc:994
FoamGridFamily< dimgrid, dimworld, ct >::Traits Traits
Exports various types belonging to this grid class.
Definition: foamgrid.hh:130
bool mark(int refCount, const typename Traits::template Codim< 0 >::Entity &e)
Mark entity for refinement.
Definition: foamgrid.hh:335
FoamGridFamily< dimgrid, dimworld, ct > GridFamily
FoamGrid is only implemented for 1 and 2 dimension.
Definition: foamgrid.hh:120
unsigned int insertVertex(const FieldVector< ctype, dimworld > &pos)
Add new vertex to be added the grid.
Definition: foamgrid.hh:399
const Traits::LeafIndexSet & leafIndexSet() const
Access to the LeafIndexSet.
Definition: foamgrid.hh:300
~FoamGrid()
Destructor.
Definition: foamgrid.hh:148
unsigned int growthInsertionIndex(const typename Traits::template Codim< 0 >::Entity &e) const
The index of insertion if the element was created in the current growth step. If this is the first el...
Definition: foamgrid.hh:497
Traits::template Codim< codim >::template Partition< PiType >::LevelIterator lend(int level) const
one past the end on this level
Definition: foamgrid.hh:196
void postGrow()
Clean up isNew markers.
Definition: foamgrid.cc:1319
friend class FoamGridLevelIndexSet< const FoamGrid >
Definition: foamgrid.hh:94
int getMark(const typename Traits::template Codim< 0 >::Entity &e) const
Return refinement mark for entity.
Definition: foamgrid.hh:355
unsigned int insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices)
Add a new element to be added to the grid.
Definition: foamgrid.hh:419
void communicate(DataHandle &data, InterfaceType iftype, CommunicationDirection dir, int level) const
Definition: foamgrid.hh:581
friend class FoamGridIdSet< const FoamGrid >
Definition: foamgrid.hh:96
bool loadBalance(DataHandle &data)
Distributes this grid over the available nodes in a distributed machine.
Definition: foamgrid.hh:550
const Traits::LevelIndexSet & levelIndexSet(int level) const
Access to the LevelIndexSets.
Definition: foamgrid.hh:285
bool preAdapt()
Book-keeping routine to be called before adaptation.
Definition: foamgrid.cc:152
void postAdapt()
Clean up refinement markers.
Definition: foamgrid.cc:317
void communicate(DataHandle &data, InterfaceType iftype, CommunicationDirection dir) const
Definition: foamgrid.hh:585
int size(GeometryType type) const
number of leaf entities per codim and geometry type in this process
Definition: foamgrid.hh:261
const Traits::GlobalIdSet & globalIdSet() const
Access to the GlobalIdSet.
Definition: foamgrid.hh:273
void globalRefine(int refCount=1)
Refine the grid uniformly.
Definition: foamgrid.cc:3
Traits::template Codim< codim >::LevelIterator lend(int level) const
one past the end on this level
Definition: foamgrid.hh:176
ct ctype
The type used to store coordinates.
Definition: foamgrid.hh:133
bool loadBalance()
Definition: foamgrid.hh:555
unsigned int overlapSize(int codim) const
Size of the overlap on the leaf level.
Definition: foamgrid.hh:525
unsigned int ghostSize(int level, int codim) const
Size of the ghost cell layer on a given level.
Definition: foamgrid.hh:543
unsigned int ghostSize(int codim) const
Size of the ghost cell layer on the leaf level.
Definition: foamgrid.hh:531
static Traits::template Codim< EntitySeed::codimension >::Entity entity(const EntitySeed &seed)
Create an Entity from an EntitySeed.
Definition: foamgrid.hh:308
int maxLevel() const
Definition: foamgrid.hh:159
Traits::template Codim< codim >::LeafIterator leafbegin() const
Iterator to first leaf entity of given codim.
Definition: foamgrid.hh:206
bool grow()
Triggers the grid growth process.
Definition: foamgrid.cc:1001
unsigned int growthInsertionIndex(const typename Traits::template Codim< dimgrid >::Entity &e) const
The index of insertion if the vertex was created in the current growth step. If this is the first ver...
Definition: foamgrid.hh:512
int size(int level, int codim) const
Number of grid entities per level and codim.
Definition: foamgrid.hh:234
bool adapt()
Triggers the grid refinement process.
Definition: foamgrid.cc:206
int size(int codim) const
number of leaf entities per codim in this process
Definition: foamgrid.hh:249
Traits::template Codim< codim >::LeafIterator leafend() const
one past the end of the sequence of leaf entities
Definition: foamgrid.hh:213
unsigned int insertElement(const GeometryType &type, const std::vector< unsigned int > &vertices, const std::shared_ptr< VirtualFunction< FieldVector< ctype, dimgrid >, FieldVector< ctype, dimworld > > > &elementParametrization)
Add a new element to be added to the grid.
Definition: foamgrid.hh:461
Traits::template Codim< codim >::template Partition< PiType >::LeafIterator leafbegin() const
Iterator to first leaf entity of given codim.
Definition: foamgrid.hh:220
Encapsulates loads of types exported by FoamGrid.
Definition: foamgrid.hh:55
GridTraits< dimgrid, dimworld, Dune::FoamGrid< dimgrid, dimworld, ctype >, FoamGridGeometry, FoamGridEntity, FoamGridLevelIterator, FoamGridLeafIntersection, FoamGridLevelIntersection, FoamGridLeafIntersectionIterator, FoamGridLevelIntersectionIterator, FoamGridHierarchicIterator, FoamGridLeafIterator, FoamGridLevelIndexSet< const FoamGrid< dimgrid, dimworld, ctype > >, FoamGridLeafIndexSet< const FoamGrid< dimgrid, dimworld, ctype > >, FoamGridIdSet< const FoamGrid< dimgrid, dimworld, ctype > >, unsigned int, FoamGridIdSet< const FoamGrid< dimgrid, dimworld, ctype > >, unsigned int, CollectiveCommunication< Dune::FoamGrid< dimgrid, dimworld, ctype > >, DefaultLevelGridViewTraits, DefaultLeafGridViewTraits, FoamGridEntitySeed > Traits
Definition: foamgrid.hh:79