version 3.8
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-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
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
24
25namespace Dumux {
26
31template<class TypeTag>
33{
36 using Element = typename GridView::template Codim<0>::Entity;
40
41 enum { saturationIdx = Indices::saturationIdx };
42
43public:
54 TwoPGridAdaptIndicator(std::shared_ptr<const GridGeometry> gridGeometry, const std::string& paramGroup = "")
55 : gridGeometry_(gridGeometry)
56 , refineBound_(std::numeric_limits<Scalar>::max())
57 , coarsenBound_(std::numeric_limits<Scalar>::lowest())
58 , maxSaturationDelta_(gridGeometry_->gridView().size(0), 0.0)
59 , minLevel_(getParamFromGroup<std::size_t>(paramGroup, "Adaptive.MinLevel", 0))
60 , maxLevel_(getParamFromGroup<std::size_t>(paramGroup, "Adaptive.MaxLevel", 0))
61 {}
62
68 void setMinLevel(std::size_t minLevel)
69 {
70 minLevel_ = minLevel;
71 }
72
78 void setMaxLevel(std::size_t maxLevel)
79 {
80 maxLevel_ = maxLevel;
81 }
82
89 void setLevels(std::size_t minLevel, std::size_t maxLevel)
90 {
91 minLevel_ = minLevel;
92 maxLevel_ = maxLevel;
93 }
94
104 void calculate(const SolutionVector& sol,
105 Scalar refineTol = 0.05,
106 Scalar coarsenTol = 0.001)
107 {
109 refineBound_ = std::numeric_limits<Scalar>::max();
110 coarsenBound_ = std::numeric_limits<Scalar>::lowest();
111 maxSaturationDelta_.assign(gridGeometry_->gridView().size(0), 0.0);
112
114 if (minLevel_ >= maxLevel_)
115 return;
116
118 if (coarsenTol > refineTol)
119 DUNE_THROW(Dune::InvalidStateException, "Refine tolerance must be higher than coarsen tolerance");
120
122 Scalar globalMax = std::numeric_limits<Scalar>::lowest();
123 Scalar globalMin = std::numeric_limits<Scalar>::max();
124
126 for (const auto& element : elements(gridGeometry_->gridView()))
127 {
129 const auto globalIdxI = gridGeometry_->elementMapper().index(element);
130
132 const auto geometry = element.geometry();
133 const auto elemSol = elementSolution(element, sol, *gridGeometry_);
134 const Scalar satI = evalSolution(element, geometry, *gridGeometry_, elemSol, geometry.center())[saturationIdx];
135
137 using std::min;
138 using std::max;
139 globalMin = min(satI, globalMin);
140 globalMax = max(satI, globalMax);
141
143 for (const auto& intersection : intersections(gridGeometry_->gridView(), element))
144 {
146 if (intersection.neighbor())
147 {
149 const auto outside = intersection.outside();
150 const auto globalIdxJ = gridGeometry_->elementMapper().index(outside);
151
153 if (element.level() > outside.level() || (element.level() == outside.level() && globalIdxI < globalIdxJ))
154 {
156 const auto outsideGeometry = outside.geometry();
157 const auto elemSolJ = elementSolution(outside, sol, *gridGeometry_);
158 const Scalar satJ = evalSolution(outside, outsideGeometry, *gridGeometry_, elemSolJ, outsideGeometry.center())[saturationIdx];
159
160 using std::abs;
161 Scalar localdelta = abs(satI - satJ);
162 maxSaturationDelta_[globalIdxI] = max(maxSaturationDelta_[globalIdxI], localdelta);
163 maxSaturationDelta_[globalIdxJ] = max(maxSaturationDelta_[globalIdxJ], localdelta);
164 }
165 }
166 }
167 }
168
170 const auto globalDelta = globalMax - globalMin;
171
173 refineBound_ = refineTol*globalDelta;
174 coarsenBound_ = coarsenTol*globalDelta;
175
176// TODO: fix adaptive simulations in parallel
177//#if HAVE_MPI
178// // communicate updated values
179// using DataHandle = VectorExchange<ElementMapper, ScalarSolutionType>;
180// DataHandle dataHandle(problem_.elementMapper(), maxSaturationDelta_);
181// problem_.gridView().template communicate<DataHandle>(dataHandle,
182// Dune::InteriorBorder_All_Interface,
183// Dune::ForwardCommunication);
184//
185// using std::max;
186// refineBound_ = problem_.gridView().comm().max(refineBound_);
187// coarsenBound_ = problem_.gridView().comm().max(coarsenBound_);
188//
189//#endif
190
192 for (const auto& element : elements(gridGeometry_->gridView(), Dune::Partitions::interior))
193 if (this->operator()(element) > 0)
194 checkNeighborsRefine_(element);
195 }
196
206 int operator() (const Element& element) const
207 {
208 if (element.hasFather()
209 && maxSaturationDelta_[gridGeometry_->elementMapper().index(element)] < coarsenBound_)
210 {
211 return -1;
212 }
213 else if (element.level() < maxLevel_
214 && maxSaturationDelta_[gridGeometry_->elementMapper().index(element)] > refineBound_)
215 {
216 return 1;
217 }
218 else
219 return 0;
220 }
221
222private:
234 bool checkNeighborsRefine_(const Element &element, std::size_t level = 1)
235 {
236 for(const auto& intersection : intersections(gridGeometry_->gridView(), element))
237 {
238 if(!intersection.neighbor())
239 continue;
240
241 // obtain outside element
242 const auto outside = intersection.outside();
243
244 // only mark non-ghost elements
245 if (outside.partitionType() == Dune::GhostEntity)
246 continue;
247
248 if (outside.level() < maxLevel_ && outside.level() < element.level())
249 {
250 // ensure refinement for outside element
251 maxSaturationDelta_[gridGeometry_->elementMapper().index(outside)] = std::numeric_limits<Scalar>::max();
252 if(level < maxLevel_)
253 checkNeighborsRefine_(outside, ++level);
254 }
255 }
256
257 return true;
258 }
259
260 std::shared_ptr<const GridGeometry> gridGeometry_;
261
262 Scalar refineBound_;
263 Scalar coarsenBound_;
264 std::vector< Scalar > maxSaturationDelta_;
265 std::size_t minLevel_;
266 std::size_t maxLevel_;
267};
268
269} // end namespace Dumux
270
271#endif
Class defining a standard, saturation dependent indicator for grid adaptation.
Definition: gridadaptindicator.hh:33
void setMinLevel(std::size_t minLevel)
Function to set the minimum allowed level.
Definition: gridadaptindicator.hh:68
int operator()(const Element &element) const
function call operator to return mark
Definition: gridadaptindicator.hh:206
void setMaxLevel(std::size_t maxLevel)
Function to set the maximum allowed level.
Definition: gridadaptindicator.hh:78
TwoPGridAdaptIndicator(std::shared_ptr< const GridGeometry > gridGeometry, const std::string &paramGroup="")
The Constructor.
Definition: gridadaptindicator.hh:54
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:104
void setLevels(std::size_t minLevel, std::size_t maxLevel)
Function to set the minimum/maximum allowed levels.
Definition: gridadaptindicator.hh:89
Defines all properties used in Dumux.
Element solution classes and factory functions.
free functions for the evaluation of primary variables inside elements.
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:152
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:149
typename GetProp< TypeTag, Property >::type GetPropType
get the type alias defined in the property
Definition: propertysystem.hh:296
Definition: adapt.hh:17
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.