23#ifndef DUMUX_GRIDADAPT_HH
24#define DUMUX_GRIDADAPT_HH
26#include <unordered_map>
27#include <dune/grid/common/partitionset.hh>
46template<
class TypeTag,
bool adaptive>
53 using Grid =
typename GridView::Grid;
54 using LeafGridView =
typename Grid::LeafGridView;
55 using Element =
typename Grid::template Codim<0>::Entity;
61 using IdSet =
typename Grid::Traits::LocalIdSet;
62 using IdType =
typename IdSet::IdType;
70 : problem_(problem), adaptionIndicator_(problem), marked_(0), coarsened_(0)
72 levelMin_ = getParam<int>(
"GridAdapt.MinLevel");
73 levelMax_ = getParam<int>(
"GridAdapt.MaxLevel");
74 adaptationInterval_ = getParam<int>(
"GridAdapt.AdaptionInterval", 1);
77 Dune::dgrave << __FILE__<<
":" <<__LINE__
78 <<
" : Dune cannot coarsen to gridlevels smaller 0! "<< std::endl;
90 adaptionIndicator_.init();
92 if (!getParam<bool>(
"GridAdapt.EnableInitializationIndicator"))
95 AdaptionInitializationIndicator adaptionInitIndicator(problem_, adaptionIndicator_);
97 int maxIter = 2*levelMax_;
99 while (iter <= maxIter)
108 int shouldInitialize = adaptionInitIndicator.initializeModel();
109 if (problem_.grid().comm().max(shouldInitialize))
110 problem_.model().initialize();
151 template<
class Indicator>
155 marked_ = coarsened_ = 0;
158 if (problem_.timeManager().timeStepIndex() % adaptationInterval_ != 0)
164 indicator.calculateIndicator();
170 int sumMarked = problem_.grid().comm().sum(marked_);
171 int sumCoarsened = problem_.grid().comm().sum(coarsened_);
172 if (sumMarked == 0 && sumCoarsened == 0)
175 Dune::dinfo << marked_ <<
" cells have been marked_ to be refined, "
176 << coarsened_ <<
" to be coarsened." << std::endl;
179 problem_.grid().preAdapt();
183 problem_.variables().storePrimVars(problem_);
186 problem_.grid().adapt();
191 using ElementMapper = std::decay_t<
decltype(problem_.variables().elementMapper())>;
192 if constexpr (Deprecated::hasUpdateGridView<ElementMapper, GridView>())
193 problem_.variables().elementMapper().update(problem_.gridView());
195 Deprecated::update(problem_.variables().elementMapper());
198 problem_.variables().adaptVariableSize(problem_.variables().elementMapper().size());
201 problem_.variables().reconstructPrimVars(problem_);
204 problem_.grid().postAdapt();
207 problem_.postAdapt();
216 template<
class Indicator>
219 using CoarsenMarkerType = std::unordered_map<IdType, IdType>;
220 CoarsenMarkerType coarsenMarker;
221 const IdSet& idSet(problem_.grid().localIdSet());
223 for (
const auto& element : elements(problem_.gridView()))
226 if (element.partitionType() == Dune::GhostEntity)
230 if (indicator.refine(element) && element.level() < levelMax_)
232 problem_.grid().mark( 1, element);
236 checkNeighborsRefine_(element);
238 if (indicator.coarsen(element) && element.hasFather())
240 IdType idx = idSet.id(element.father());
241 auto it = coarsenMarker.find(idx);
242 if (it != coarsenMarker.end())
248 coarsenMarker[idx] = 1;
253 for (
const auto& element : elements(problem_.gridView()))
256 if (element.partitionType() == Dune::GhostEntity)
259 if (indicator.coarsen(element) && element.level() > levelMin_)
261 IdType idx = idSet.id(element.father());
262 auto it = coarsenMarker.find(idx);
263 if (it != coarsenMarker.end())
265 if (problem_.grid().getMark(element) == 0
266 && it->second == element.geometry().corners())
269 bool coarsenPossible =
true;
270 for(
const auto& intersection : intersections(problem_.gridView(), element))
272 if(intersection.neighbor())
274 auto outside = intersection.outside();
275 if ((problem_.grid().getMark(outside) > 0)
276 || outside.level() > element.level())
278 coarsenPossible =
false;
285 problem_.grid().mark( -1, element );
299 int sumMarked = problem_.grid().comm().sum(marked_);
300 int sumCoarsened = problem_.grid().comm().sum(coarsened_);
302 return (sumMarked != 0 || sumCoarsened != 0);
314 Dune::dgrave << __FILE__<<
":" <<__LINE__
315 <<
" : Dune cannot coarsen to gridlevels smaller 0! "<< std::endl;
345 return adaptionIndicator_;
350 return adaptionIndicator_;
366 bool checkNeighborsRefine_(
const Element &entity,
int level = 1)
369 for(
const auto& intersection : intersections(problem_.gridView(), entity))
371 if(!intersection.neighbor())
374 auto outside = intersection.outside();
377 if (outside.partitionType() == Dune::GhostEntity)
380 if ((outside.level() < levelMax_)
381 && (outside.level() < entity.level()))
383 problem_.grid().mark(1, outside);
386 if(level != levelMax_)
387 checkNeighborsRefine_(outside, ++level);
403 void forceRefineRatio(
int maxLevelDelta = 1)
405 LeafGridView leafGridView = problem_.gridView();
407 problem_.grid().postAdapt();
413 for (
const auto& element : elements(problem_.gridView()))
416 if (
element.partitionType() == Dune::GhostEntity)
420 for (
const auto& intersection : intersections(leafGridView, element))
422 if(!intersection.neighbor())
425 if (
element.level() + maxLevelDelta < intersection.outside().level())
427 problem_.grid().mark( 1, element );
435 problem_.grid().adapt();
437 problem_.grid().postAdapt();
445 AdaptionIndicator adaptionIndicator_;
453 int adaptationInterval_;
463template<
class TypeTag>
469 using ScalarSolutionType =
typename SolutionTypes::ScalarSolution;
485 const Scalar&,
const Scalar&)
Defines a type tag and some fundamental properties for linear solvers.
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type GetProp
get the type of a property
Definition: propertysystem.hh:141
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:150
Standard Module for h-adaptive simulations.
Definition: gridadapt.hh:48
void adaptGrid(Indicator &indicator)
Method to adapt the grid with individual indicator vector.
Definition: gridadapt.hh:152
GridAdapt(Problem &problem)
Definition: gridadapt.hh:69
int getMaxLevel() const
Returns maximum refinement level.
Definition: gridadapt.hh:327
AdaptionIndicator & adaptionIndicator()
Definition: gridadapt.hh:343
AdaptionIndicator & adaptionIndicator() const
Definition: gridadapt.hh:348
void adaptGrid()
Standard method to adapt the grid.
Definition: gridadapt.hh:130
void setLevels(int levMin, int levMax)
Definition: gridadapt.hh:311
bool wasAdapted()
Returns true if grid cells have been marked for adaptation.
Definition: gridadapt.hh:297
void markElements(Indicator &indicator)
Definition: gridadapt.hh:217
void init()
Initalization method of the h-adaptive module.
Definition: gridadapt.hh:88
int getMinLevel() const
Returns minimum refinement level.
Definition: gridadapt.hh:338
GridAdapt(Problem &problem)
Definition: gridadapt.hh:487
void adaptGrid()
Definition: gridadapt.hh:474
void setLevels(int, int)
Definition: gridadapt.hh:480
void setIndicator(const ScalarSolutionType &, const Scalar &, const Scalar &)
Definition: gridadapt.hh:484
void init()
Definition: gridadapt.hh:472
bool wasAdapted()
Definition: gridadapt.hh:476
void setTolerance(int, int)
Definition: gridadapt.hh:482
Base file for properties related to sequential models.