12#ifndef GRIDMANAGER_HH_
13#define GRIDMANAGER_HH_
21#include <boost/signals2.hpp>
22#include <boost/range/iterator_range.hpp>
24#include <dune/common/config.h>
25#include <dune/grid/common/capabilities.hh>
26#include <dune/grid/common/gridview.hh>
27#include <dune/grid/io/file/dgfparser/gridptr.hh>
39 template<
class G,
class T>
class CellData;
89 template <
class Gr
idView>
96 typedef typename GridView::template Codim<0>::Iterator
CellIterator;
119 for (
int n=1; n<=nmax; ++n)
120 cellRanges[n].reserve(n+1);
123 size_t const size = gridView.size(0);
140 for (
CellIterator i=gridView.template begin<0>(); i!=gridView.template end<0>(); ++i, ++j)
141 for (
int n=1; n<=nmax; ++n)
142 if (j >= cellRanges[n].size()*size/n)
143 cellRanges[n].push_back(i);
149 for (
int n=1; n<=nmax; ++n)
150 while (cellRanges[n].size()<n+1)
151 cellRanges[n].push_back(gridView.template end<0>());
164 boost::iterator_range<CellIterator>
range(
int n,
int k)
const
166 assert(1<=n && n<cellRanges.size());
168 return boost::iterator_range<CellIterator>(cellRanges[n][k],cellRanges[n][k+1]);
177 return static_cast<int>(cellRanges.size())-1;
181 std::vector<std::vector<CellIterator>> cellRanges;
187 template <
class Gr
idView>
190 using IndexSet =
typename GridView::IndexSet;
191 constexpr static int dim = GridView::dimension;
194 using Index =
typename IndexSet::IndexType;
226 IndexSet
const& is = gv.indexSet();
227 for (
int codim=0; codim<=dim; ++codim)
228 indices[codim].resize(is.size(codim));
235 for (
int codim=0; codim<=dim; ++codim)
237 std::iota(begin(indices[codim]),end(indices[codim]),n);
238 n += indices[codim].size();
253 assert(0<=codim && codim<=dim);
254 assert(0<=i && i<indices[codim].size());
255 return indices[codim][i];
259 std::array<std::vector<Index>,dim+1> indices;
335 bool mark(
int refCount,
typename Grid::template Codim<0>::Entity
const& cell)
340 return gridptr->mark(refCount,cell);
352 for(imark=cellData.
begin(); imark != imark_end; ++imark)
354 auto ret = this->
mark(imark->first,imark->second);
356 if (!ret && imark->first!=0)
357 std::cout <<
"GRIDMANAGER: Warning: Cannot mark element!" <<
'\n';
384 std::cout <<
"after refinement: " <<
gridptr->size(0) <<
" cells, "
385 <<
gridptr->size(Grid::dimension) <<
" vertices.\n";
397 bool result =
adapt();
400 std::cout <<
"GRIDMANAGER: Nothing has been refined!" <<
'\n';
409 for (
auto const& element: elements(
gridptr->leafGridView()))
422 if (this->markCount > 0)
427 timer.
start(
"before refinement");
429 timer.
stop(
"before refinement");
432 std::cout <<
"\nGRIDMANAGER: mesh is refined from " << this->
gridptr->size(0) <<
" cells " << std::flush;
434 timer.
start(
"adapt");
435 result = adaptGrid();
440 std::cout <<
"to " << this->
gridptr->size(0) <<
" cells" <<
'\n';
442 timer.
start(
"after refinement");
444 timer.
stop(
"after refinement");
464 timer.
start(
"before refinement");
466 timer.
stop(
"before refinement");
468 timer.
start(
"adapt");
469 refineGrid(refCount);
473 timer.
start(
"after refinement");
475 timer.
stop(
"after refinement");
509 verbose = verbosity;
return *
this;
527 return gridIsThreadSafe_ || enforceConcurrentReads_;
543 std::lock_guard<std::mutex> lock(cellRangeMutex);
559 int const level = gridView.template begin<0>()->level();
560 assert(level<=
grid().maxLevel());
562 std::lock_guard<std::mutex> lock(cellRangeMutex);
563 if (levelRanges[level].maxRanges()<0)
566 return levelRanges[level];
577 std::lock_guard<std::mutex> lock(cellRangeMutex);
578 if (!leafEntityNumbers)
579 leafEntityNumbers = std::make_unique<Numbering>(
grid().leafGridView(),Numbering::SORTBYCODIM);
581 return *leafEntityNumbers;
587 virtual void refineGrid(
int refcount) = 0;
588 virtual bool adaptGrid() = 0;
602 leafEntityNumbers.reset();
607 levelRanges.resize(
grid().maxLevel()+1);
624 const bool gridIsThreadSafe_;
627 bool enforceConcurrentReads_;
630 mutable std::mutex cellRangeMutex;
631 mutable std::unique_ptr<CellRanges<typename Grid::LeafGridView>> leafRanges;
632 mutable std::vector<CellRanges<typename Grid::LevelGridView>> levelRanges;
633 mutable std::unique_ptr<EntityNumbering<typename Grid::LeafGridView>> leafEntityNumbers;
728 virtual void refineGrid(
int refCount)
730 this->
gridptr->globalRefine(refCount);
734 virtual bool adaptGrid()
749 template <
class Gr
id>
752 for (
auto const& cell: elements(gridManager.
grid().leafGridView()))
753 if (cell.hasBoundaryIntersections())
754 gridManager.
mark(1,cell);
Class that stores information for each cell of a grid.
CellDataVector::const_iterator end() const
Return a const_iterator on data.end()
CellDataVector::const_iterator begin() const
Return a const_iterator on data.begin()
A class storing cell ranges of roughly equal size for multithreaded mesh traversal.
GridView::template Codim< 0 >::Iterator CellIterator
The type of iterator to step through the cells of the given view.
int maxRanges() const
Obtain the maximum number of ranges. Default constructed "empty" cell ranges return a negative value.
CellRanges< GridView > & operator=(CellRanges< GridView > &&)=default
Move assignment.
boost::iterator_range< CellIterator > range(int n, int k) const
Obtain cell range.
CellRanges(CellRanges &&)=default
Move constructor.
CellRanges()=default
Default constructor. This creates a pretty useless empty cell ranges object.
CellRanges(int nmax, GridView const &gridView)
Constructs cell ranges.
EntityNumbering(GridView const &gv, SortOrder order)
SortOrder
Defines the available orderings.
@ CUTHILLMCKEE
sorts all entities according to their adjacency by Cuthill-McKee This provides small bandwidth of sti...
@ SEQUENTIAL
sorts cells according to the cell iterator of the grid view, and places lower codim entities close to...
@ SORTBYCODIM
sorts cells first, then faces, edges, and vertices last
typename IndexSet::IndexType Index
Index operator()(int codim, Index i) const
Basic functionality for managing grids and their refinement.
GridManagerBase(Dune::GridPtr< Grid > grid_, bool verbose_, bool enforceConcurrentReads=false)
friend class FEFunctionSpace
friend class AdaptiveGrid
void globalRefine(int refCount)
Refines each leaf cell of the grid and prolongates registered FE functions to the new leaf grid view.
size_t countMarked() const
Reports the number of marked elements.
GridManagerBase< Grid > Self
virtual void update()=0
Must be overloaded by derived classes to adjust internal state immediately after mesh refinement.
std::shared_ptr< Grid > gridptr
bool preAdapt()
Calls grid.preAdapt().
bool adapt()
Refines each marked leaf cell of the grid and prolongates registered FE functions to the new leaf gri...
CellRanges< typename Grid::LeafGridView > const & cellRanges(typename Grid::LeafGridView const &) const
Returns a CellRanges object for the given grid view. The cell ranges are created on demand,...
Grid const & grid() const
Returns a const reference on the owned grid.
bool adaptAtOnce()
Performs grid refinement.
void mark(CellData< Grid, S > const &cellData)
Marks all Entities of a grid according to the corresponding entries in a CellData.
bool gridIsThreadSafe() const
Returns true if concurrent read accesses to the grid do not lead to data races.
void postAdapt()
Calls grid.postAdapt().
EntityNumbering< typename Grid::LeafGridView > const & entityNumbering(typename Grid::LeafGridView const &) const
Returns an EntityNumbering object for the given grid view.
void flushMarks()
Remove all cell marks from the grid.
GridManagerBase(std::unique_ptr< Grid > &&grid_, bool verbose_, bool enforceConcurrentReads=false)
std::shared_ptr< Grid const > gridShared() const
Provides a read-only shared pointer to the underlying grid.
CellRanges< typename Grid::LevelGridView > const & cellRanges(typename Grid::LevelGridView const &gridView) const
Returns a CellRanges object for the given grid view.
bool mark(int refCount, typename Grid::template Codim< 0 >::Entity const &cell)
Marks the given cell for refinement or coarsening.
virtual ~GridManagerBase()
GridManagerBase(Grid *&&grid_, bool verbose_, bool enforceConcurrentReads=false)
Self & enforceConcurrentReads(bool enforceConcurrentReads)
Self & setVerbosity(const bool verbosity)
sets the verbosity level of the grid manager
GridManager(std::unique_ptr< Grid > &&grid_, bool verbose=false)
Construction from a unique_ptr<Grid>
virtual void update()
Must be overloaded by derived classes to adjust internal state immediately after mesh refinement.
GridManager(Dune::GridPtr< Grid > grid_, bool verbose=false)
Construction from a GridPtr<Grid>
typename VariableSetDescription::Grid Grid
GridManager(Grid *&&grid_, bool verbose=false)
Construction from a Grid*.
static NumaThreadPool & instance(int maxThreads=std::numeric_limits< int >::max())
Returns a globally unique thread pool instance.
A scope guard object that automatically closes a timing section on destruction.
Supports gathering and reporting execution times information for nested program parts.
static Timings & instance()
Returns a reference to a single default instance.
void stop(std::string const &name)
Stops the timing of given section.
struct Times const * start(std::string const &name)
Starts or continues the timing of given section.
boost::signals2::signal< void(Status)> informAboutRefinement
A signal that is emitted thrice on grid modifications, once before adaptation takes place and twice a...
void refineAtBoundary(GridManagerBase< Grid > &gridManager)
A convenience routine that refines all cells incident to a boundary face.
Status
The argument type of the signal that is emitted before and after grid adaptation.
@ BeforeRefinementComplete
int const LagrangeCubeShapeFunction< ctype, dimension, Scalar, O >::order
A class that provides access to signals that are emitted from the grid manager on various occasions.