23#ifndef DUMUX_GRIDADAPT_HH
24#define DUMUX_GRIDADAPT_HH
26#include <unordered_map>
27#include <dune/grid/common/partitionset.hh>
45template<
class TypeTag,
bool adaptive>
52 using Grid =
typename GridView::Grid;
53 using LeafGridView =
typename Grid::LeafGridView;
54 using Element =
typename Grid::template Codim<0>::Entity;
60 using IdSet =
typename Grid::Traits::LocalIdSet;
61 using IdType =
typename IdSet::IdType;
69 : problem_(problem), adaptionIndicator_(problem), marked_(0), coarsened_(0)
71 levelMin_ = getParam<int>(
"GridAdapt.MinLevel");
72 levelMax_ = getParam<int>(
"GridAdapt.MaxLevel");
73 adaptationInterval_ = getParam<int>(
"GridAdapt.AdaptionInterval", 1);
76 Dune::dgrave << __FILE__<<
":" <<__LINE__
77 <<
" : Dune cannot coarsen to gridlevels smaller 0! "<< std::endl;
89 adaptionIndicator_.init();
91 if (!getParam<bool>(
"GridAdapt.EnableInitializationIndicator"))
94 AdaptionInitializationIndicator adaptionInitIndicator(problem_, adaptionIndicator_);
96 int maxIter = 2*levelMax_;
98 while (iter <= maxIter)
107 int shouldInitialize = adaptionInitIndicator.initializeModel();
108 if (problem_.grid().comm().max(shouldInitialize))
109 problem_.model().initialize();
150 template<
class Indicator>
154 marked_ = coarsened_ = 0;
157 if (problem_.timeManager().timeStepIndex() % adaptationInterval_ != 0)
163 indicator.calculateIndicator();
169 int sumMarked = problem_.grid().comm().sum(marked_);
170 int sumCoarsened = problem_.grid().comm().sum(coarsened_);
171 if (sumMarked == 0 && sumCoarsened == 0)
174 Dune::dinfo << marked_ <<
" cells have been marked_ to be refined, "
175 << coarsened_ <<
" to be coarsened." << std::endl;
178 problem_.grid().preAdapt();
182 problem_.variables().storePrimVars(problem_);
185 problem_.grid().adapt();
190 problem_.variables().elementMapper().update();
193 problem_.variables().adaptVariableSize(problem_.variables().elementMapper().size());
196 problem_.variables().reconstructPrimVars(problem_);
199 problem_.grid().postAdapt();
202 problem_.postAdapt();
211 template<
class Indicator>
214 using CoarsenMarkerType = std::unordered_map<IdType, IdType>;
215 CoarsenMarkerType coarsenMarker;
216 const IdSet& idSet(problem_.grid().localIdSet());
218 for (
const auto& element : elements(problem_.gridView()))
221 if (element.partitionType() == Dune::GhostEntity)
225 if (indicator.refine(element) && element.level() < levelMax_)
227 problem_.grid().mark( 1, element);
231 checkNeighborsRefine_(element);
233 if (indicator.coarsen(element) && element.hasFather())
235 IdType idx = idSet.id(element.father());
236 auto it = coarsenMarker.find(idx);
237 if (it != coarsenMarker.end())
243 coarsenMarker[idx] = 1;
248 for (
const auto& element : elements(problem_.gridView()))
251 if (element.partitionType() == Dune::GhostEntity)
254 if (indicator.coarsen(element) && element.level() > levelMin_)
256 IdType idx = idSet.id(element.father());
257 auto it = coarsenMarker.find(idx);
258 if (it != coarsenMarker.end())
260 if (problem_.grid().getMark(element) == 0
261 && it->second == element.geometry().corners())
264 bool coarsenPossible =
true;
265 for(
const auto& intersection : intersections(problem_.gridView(), element))
267 if(intersection.neighbor())
269 auto outside = intersection.outside();
270 if ((problem_.grid().getMark(outside) > 0)
271 || outside.level() > element.level())
273 coarsenPossible =
false;
280 problem_.grid().mark( -1, element );
294 int sumMarked = problem_.grid().comm().sum(marked_);
295 int sumCoarsened = problem_.grid().comm().sum(coarsened_);
297 return (sumMarked != 0 || sumCoarsened != 0);
309 Dune::dgrave << __FILE__<<
":" <<__LINE__
310 <<
" : Dune cannot coarsen to gridlevels smaller 0! "<< std::endl;
340 return adaptionIndicator_;
345 return adaptionIndicator_;
361 bool checkNeighborsRefine_(
const Element &entity,
int level = 1)
364 for(
const auto& intersection : intersections(problem_.gridView(), entity))
366 if(!intersection.neighbor())
369 auto outside = intersection.outside();
372 if (outside.partitionType() == Dune::GhostEntity)
375 if ((outside.level() < levelMax_)
376 && (outside.level() < entity.level()))
378 problem_.grid().mark(1, outside);
381 if(level != levelMax_)
382 checkNeighborsRefine_(outside, ++level);
398 void forceRefineRatio(
int maxLevelDelta = 1)
400 LeafGridView leafGridView = problem_.gridView();
402 problem_.grid().postAdapt();
408 for (
const auto& element : elements(problem_.gridView()))
411 if (element.partitionType() == Dune::GhostEntity)
415 for (
const auto& intersection : intersections(leafGridView, element))
417 if(!intersection.neighbor())
420 if (element.level() + maxLevelDelta < intersection.outside().level())
422 problem_.grid().mark( 1, element );
430 problem_.grid().adapt();
432 problem_.grid().postAdapt();
440 AdaptionIndicator adaptionIndicator_;
448 int adaptationInterval_;
458template<
class TypeTag>
464 using ScalarSolutionType =
typename SolutionTypes::ScalarSolution;
480 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 (equivalent to old macro GET_PROP(...))
Definition: propertysystem.hh:140
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:149
Standard Module for h-adaptive simulations.
Definition: gridadapt.hh:47
void adaptGrid(Indicator &indicator)
Method to adapt the grid with individual indicator vector.
Definition: gridadapt.hh:151
GridAdapt(Problem &problem)
Definition: gridadapt.hh:68
int getMaxLevel() const
Returns maximum refinement level.
Definition: gridadapt.hh:322
AdaptionIndicator & adaptionIndicator()
Definition: gridadapt.hh:338
AdaptionIndicator & adaptionIndicator() const
Definition: gridadapt.hh:343
void adaptGrid()
Standard method to adapt the grid.
Definition: gridadapt.hh:129
void setLevels(int levMin, int levMax)
Definition: gridadapt.hh:306
bool wasAdapted()
Returns true if grid cells have been marked for adaptation.
Definition: gridadapt.hh:292
void markElements(Indicator &indicator)
Definition: gridadapt.hh:212
void init()
Initalization method of the h-adaptive module.
Definition: gridadapt.hh:87
int getMinLevel() const
Returns minimum refinement level.
Definition: gridadapt.hh:333
GridAdapt(Problem &problem)
Definition: gridadapt.hh:482
void adaptGrid()
Definition: gridadapt.hh:469
void setLevels(int, int)
Definition: gridadapt.hh:475
void setIndicator(const ScalarSolutionType &, const Scalar &, const Scalar &)
Definition: gridadapt.hh:479
void init()
Definition: gridadapt.hh:467
bool wasAdapted()
Definition: gridadapt.hh:471
void setTolerance(int, int)
Definition: gridadapt.hh:477
Base file for properties related to sequential models.