version 3.11-dev
Loading...
Searching...
No Matches
gridadaptindicator.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12
13#ifndef DUMUX_TWOP_ADAPTION_INDICATOR_HH
14#define DUMUX_TWOP_ADAPTION_INDICATOR_HH
15
16#include <memory>
17#include <dune/common/exceptions.hh>
18#include <dune/grid/common/partitionset.hh>
19
26
27namespace Dumux {
28
33template<class TypeTag>
35{
38 using Element = typename GridView::template Codim<0>::Entity;
42
43 enum { saturationIdx = Indices::saturationIdx };
44
45public:
56 TwoPGridAdaptIndicator(std::shared_ptr<const GridGeometry> gridGeometry, const std::string& paramGroup = "")
57 : gridGeometry_(gridGeometry)
58 , refineBound_(std::numeric_limits<Scalar>::max())
59 , coarsenBound_(std::numeric_limits<Scalar>::lowest())
60 , maxSaturationDelta_(gridGeometry_->gridView().size(0), 0.0)
61 , minLevel_(getParamFromGroup<std::size_t>(paramGroup, "Adaptive.MinLevel", 0))
62 , maxLevel_(getParamFromGroup<std::size_t>(paramGroup, "Adaptive.MaxLevel", 0))
63 {}
64
70 void setMinLevel(std::size_t minLevel)
71 {
72 minLevel_ = minLevel;
73 }
74
80 void setMaxLevel(std::size_t maxLevel)
81 {
82 maxLevel_ = maxLevel;
83 }
84
91 void setLevels(std::size_t minLevel, std::size_t maxLevel)
92 {
93 minLevel_ = minLevel;
94 maxLevel_ = maxLevel;
95 }
96
106 void calculate(const SolutionVector& sol,
107 Scalar refineTol = 0.05,
108 Scalar coarsenTol = 0.001)
109 {
110 const auto& gridView = gridGeometry_->gridView();
111
113 refineBound_ = std::numeric_limits<Scalar>::max();
114 coarsenBound_ = std::numeric_limits<Scalar>::lowest();
115 maxSaturationDelta_.assign(gridView.size(0), 0.0);
116
118 if (minLevel_ >= maxLevel_)
119 return;
120
122 if (coarsenTol > refineTol)
123 DUNE_THROW(Dune::InvalidStateException, "Refine tolerance must be higher than coarsen tolerance");
124
126 Scalar globalMax = std::numeric_limits<Scalar>::lowest();
127 Scalar globalMin = std::numeric_limits<Scalar>::max();
128
130 for (const auto& element : elements(gridView, Dune::Partitions::interior))
131 {
133 const auto globalIdxI = gridGeometry_->elementMapper().index(element);
134
136 const auto geometry = element.geometry();
137 const auto elemSol = elementSolution(element, sol, *gridGeometry_);
138 const Scalar satI = evalSolution(element, geometry, *gridGeometry_, elemSol, geometry.center())[saturationIdx];
139
141 using std::min;
142 using std::max;
143 globalMin = min(satI, globalMin);
144 globalMax = max(satI, globalMax);
145
147 for (const auto& intersection : intersections(gridView, element))
148 {
150 if (intersection.neighbor())
151 {
153 const auto outside = intersection.outside();
154 const auto globalIdxJ = gridGeometry_->elementMapper().index(outside);
155
157 if (element.level() > outside.level() || (element.level() == outside.level() && globalIdxI < globalIdxJ))
158 {
160 const auto outsideGeometry = outside.geometry();
161 const auto elemSolJ = elementSolution(outside, sol, *gridGeometry_);
162 const Scalar satJ = evalSolution(outside, outsideGeometry, *gridGeometry_, elemSolJ, outsideGeometry.center())[saturationIdx];
163
164 using std::abs;
165 Scalar localdelta = abs(satI - satJ);
166 maxSaturationDelta_[globalIdxI] = max(maxSaturationDelta_[globalIdxI], localdelta);
167 maxSaturationDelta_[globalIdxJ] = max(maxSaturationDelta_[globalIdxJ], localdelta);
168 }
169 }
170 }
171 }
172
174 const auto globalDelta = globalMax - globalMin;
175
177 refineBound_ = refineTol*globalDelta;
178 coarsenBound_ = coarsenTol*globalDelta;
179
180#if HAVE_MPI
181 // communicate updated values
183 DataHandle dataHandle(gridGeometry_->elementMapper(), maxSaturationDelta_);
184 if constexpr (Dune::Capabilities::canCommunicate<typename GridGeometry::GridView::Traits::Grid, 0>::v)
185 gridView.template communicate<DataHandle>(
186 dataHandle, Dune::InteriorBorder_All_Interface, Dune::ForwardCommunication
187 );
188 else
189 DUNE_THROW(
190 Dune::InvalidStateException,
191 "Cannot call calculate indicator on grid that cannot communicate codim-0-entities."
192 );
193
194 using std::max;
195 refineBound_ = gridView.comm().max(refineBound_);
196 coarsenBound_ = gridView.comm().max(coarsenBound_);
197#endif
198
200 for (const auto& element : elements(gridView))
201 if (this->operator()(element) > 0)
202 checkNeighborsRefine_(element);
203 }
204
214 int operator() (const Element& element) const
215 {
216 if (element.hasFather()
217 && maxSaturationDelta_[gridGeometry_->elementMapper().index(element)] < coarsenBound_)
218 {
219 return -1;
220 }
221 else if (element.level() < maxLevel_
222 && maxSaturationDelta_[gridGeometry_->elementMapper().index(element)] > refineBound_)
223 {
224 return 1;
225 }
226 else
227 return 0;
228 }
229
230private:
242 bool checkNeighborsRefine_(const Element &element, std::size_t level = 1)
243 {
244 for(const auto& intersection : intersections(gridGeometry_->gridView(), element))
245 {
246 if(!intersection.neighbor())
247 continue;
248
249 // obtain outside element
250 const auto outside = intersection.outside();
251
252 // only mark non-ghost elements
253 if (outside.partitionType() == Dune::GhostEntity)
254 continue;
255
256 if (outside.level() < maxLevel_ && outside.level() < element.level())
257 {
258 // ensure refinement for outside element
259 assert(outside.isLeaf());
260 maxSaturationDelta_[gridGeometry_->elementMapper().index(outside)]
261 = std::numeric_limits<Scalar>::max();
262
263 if (level < maxLevel_)
264 checkNeighborsRefine_(outside, ++level);
265 }
266 }
267
268 return true;
269 }
270
271 std::shared_ptr<const GridGeometry> gridGeometry_;
272
273 Scalar refineBound_;
274 Scalar coarsenBound_;
275 std::vector< Scalar > maxSaturationDelta_;
276 std::size_t minLevel_;
277 std::size_t maxLevel_;
278};
279
280} // end namespace Dumux
281
282#endif
void setMinLevel(std::size_t minLevel)
Function to set the minimum allowed level.
Definition gridadaptindicator.hh:70
int operator()(const Element &element) const
function call operator to return mark
Definition gridadaptindicator.hh:214
void setMaxLevel(std::size_t maxLevel)
Function to set the maximum allowed level.
Definition gridadaptindicator.hh:80
TwoPGridAdaptIndicator(std::shared_ptr< const GridGeometry > gridGeometry, const std::string &paramGroup="")
The Constructor.
Definition gridadaptindicator.hh:56
void calculate(const SolutionVector &sol, Scalar refineTol=0.05, Scalar coarsenTol=0.001)
Calculates the indicator used for refinement/coarsening for each grid cell.
Definition gridadaptindicator.hh:106
void setLevels(std::size_t minLevel, std::size_t maxLevel)
Function to set the minimum/maximum allowed levels.
Definition gridadaptindicator.hh:91
Defines all properties used in Dumux.
Element solution classes and factory functions.
free functions for the evaluation of primary variables inside elements.
dune-grid capabilities compatibility layer
PrimaryVariables evalSolution(const Element &element, const typename Element::Geometry &geometry, const typename FVElementGeometry::GridGeometry &gridGeometry, const CVFEElementSolution< FVElementGeometry, PrimaryVariables > &elemSol, const typename Element::Geometry::GlobalCoordinate &globalPos, bool ignoreState=false)
Interpolates a given box element solution at a given global position. Uses the finite element cache o...
Definition evalsolution.hh:197
auto elementSolution(const Element &element, const SolutionVector &sol, const GridGeometry &gg) -> std::enable_if_t< GridGeometry::discMethod==DiscretizationMethods::cctpfa||GridGeometry::discMethod==DiscretizationMethods::ccmpfa, CCElementSolution< typename GridGeometry::LocalView, std::decay_t< decltype(std::declval< SolutionVector >()[0])> > >
Make an element solution for cell-centered schemes.
Definition cellcentered/elementsolution.hh:101
T getParamFromGroup(Args &&... args)
A free function to get a parameter from the parameter tree singleton with a model group.
Definition parameters.hh:165
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition propertysystem.hh:296
Definition adapt.hh:17
VectorCommDataHandle< Mapper, Vector, codim, Detail::Max, DataType > VectorCommDataHandleMax
Definition vectorcommdatahandle.hh:220
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Contains a class to exchange entries of a vector.