1#ifndef DUNE_FEMPY_PY_DEFAULT_MARKING_HH
2#define DUNE_FEMPY_PY_DEFAULT_MARKING_HH
6#include <dune/geometry/referenceelements.hh>
7#include <dune/fem/gridpart/common/gridpart.hh>
8#include <dune/fem/function/localfunction/const.hh>
14 namespace GridAdaptation
18 static const int refine = 1;
19 static const int keep = 0;
20 static const int coarsen = -1;
24 template <
class Gr
id,
class Indicator>
27 mark( Grid& grid, Indicator& indicator,
28 const double refineTolerance,
const double coarsenTolerance,
29 const int minLevel = 0,
31 const double minVolume = -1.,
32 double maxVolume = -1.0,
33 const bool markNeighbors =
false,
34 const bool statistics =
false )
36 Dune::Fem::ConstLocalFunction<Indicator> localIndicator(indicator);
37 typename Indicator::RangeType value;
39 std::array< int, 2 > sumMarks = {{ 0,0 }};
41 int& refMarked = sumMarks[ 0 ];
42 int& crsMarked = sumMarks[ 1 ];
47 const bool useVolumeRefinement = minVolume > 0.0;
48 const bool useVolumeCoarsening = maxVolume > 0.0;
49 if ( ! useVolumeCoarsening )
55 const auto& gridPart = indicator.space().gridPart();
57 for (
const auto &e : indicator.space())
59 if (!e.isLeaf())
continue;
61 const auto &gridEntity = Dune::Fem::gridEntity(e);
62 int marked = grid.getMark(gridEntity);
63 if (marked==1)
continue;
65 localIndicator.bind(e);
66 const auto ¢er = Dune::referenceElement< typename Grid::ctype, Grid::dimension>( e.type() ).position(0,0);
67 localIndicator.evaluate(center,value);
68 double eta = std::abs(value[0]);
69 const int level = e.level();
72 const double volume = (useVolumeRefinement || useVolumeCoarsening) ? localIndicator.geometry().volume() : 0.0;
77 if (eta>refineTolerance && level<maxLevel && volume>minVolume)
79 refMarked += grid.mark( Marker::refine, gridEntity);
82 const auto iEnd = gridPart.iend( e );
83 for(
auto it = gridPart.ibegin( e ); it != iEnd; ++it )
85 const auto& intersection = *it;
86 if( intersection.neighbor() )
88 const auto& outside = intersection.outside();
89 const double outsideVol = (useVolumeRefinement) ? outside.geometry().volume() : 0.0;
90 if (outside.level()<maxLevel && outsideVol>minVolume )
91 refMarked += grid.mark( Marker::refine, Dune::Fem::gridEntity(outside) );
97 else if (eta<coarsenTolerance && level>minLevel && volume<maxVolume )
98 crsMarked += grid.mark( Marker::coarsen, gridEntity);
100 grid.mark(Marker::keep, gridEntity);
106 gridPart.comm().sum( sumMarks.data(), 2 );
107 return std::make_pair( refMarked, crsMarked );
110 return std::make_pair(
int(-1),
int(-1));
116 namespace SpaceAdaptation
119 template <
class Space,
class Indicator>
121 std::pair< int, int >
122 mark( Space& space, Indicator& indicator,
123 const double refineTolerance,
const double coarsenTolerance,
124 const int minOrder,
const int maxOrder,
125 const bool markNeighbors =
false,
126 const bool statistics =
false )
128 Dune::Fem::ConstLocalFunction<Indicator> localIndicator(indicator);
129 typename Indicator::RangeType value;
130 if( minOrder < 0 && minOrder > space.order() )
133 if( maxOrder < minOrder && maxOrder > space.order() )
136 const auto& gridPart = space.gridPart();
138 std::array< int, 2 > sumMarks = {{ 0,0 }};
139 int& refMarked = sumMarks[ 0 ];
140 int& crsMarked = sumMarks[ 1 ];
142 std::vector< bool > marked;
144 marked.resize( gridPart.indexSet().size( 0 ),
false );
146 for (
const auto &e : space)
148 localIndicator.bind(e);
150 localIndicator.evaluate(center,value);
151 double eta = std::abs(value[0]);
153 const bool markedAlready = ( markNeighbors ) ? marked[ gridPart.indexSet().index( e ) ] :
false;
156 const int currentOrder = space.order( e );
157 int polOrder = currentOrder;
164 if( space.getMark( e ) > polOrder )
170 if (eta > refineTolerance && polOrder < maxOrder)
177 const auto iEnd = gridPart.iend( e );
178 for(
auto it = gridPart.ibegin( e ); it != iEnd; ++it )
180 const auto& intersection = *it;
181 if( intersection.neighbor() )
183 const auto& outside = intersection.outside();
185 const int nbOrder = space.getMark( outside );
186 if ( polOrder > nbOrder )
188 space.mark( polOrder, outside );
192 marked[ gridPart.indexSet().index( outside ) ] =
true;
198 else if (! markedAlready && eta < coarsenTolerance && polOrder > minOrder)
205 if( polOrder > currentOrder )
207 else if ( polOrder < currentOrder )
211 space.mark( polOrder, e );
215 marked[ gridPart.indexSet().index( e ) ] =
true;
221 gridPart.comm().sum( sumMarks.data(), 2 );
222 return std::make_pair( refMarked, crsMarked );
225 return std::make_pair(
int(-1),
int(-1));
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:375
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
unspecified value type referenceElement(T &&... t)
Returns a reference element for the objects t....
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:485
Dune namespace.
Definition: alignedallocator.hh:13