13#ifndef ADAPTATION_STRATEGY_HH
14#define ADAPTATION_STRATEGY_HH
22 namespace AdaptationStrategy_Detail
26 template <
class Scalar,
class IndexInt>
27 bool operator()(
const std::pair<Scalar, IndexInt>& x1,
const std::pair<Scalar,IndexInt>& x2)
29 return fabs(x1.first) > fabs(x2.first);
35 template <
class Scalar,
class IndexInt>
36 Scalar
operator()(Scalar x1,
const std::pair<Scalar,IndexInt>& x2)
48 static constexpr int dim = Grid::dimension;
49 typedef size_t IndexInt;
51 template <
typename... Args>
53 : gridManager(gridManager_), fraction(fraction_)
56 template <
class Err,
class ErrorRepresentation,
class Scalar>
57 void refineGrid_impl(Err
const& err, ErrorRepresentation& errorDistribution, Scalar tol)
60 std::cout <<
"max error2: " << std::max_element(errorDistribution.begin(), errorDistribution.end())->first << std::endl;
61 std::cout <<
"min error2: " << std::min_element(errorDistribution.begin(), errorDistribution.end())->first << std::endl;
63 std::vector<std::pair<Scalar,IndexInt> > bulkErrorDistribution(errorDistribution.begin(), errorDistribution.begin() +
static_cast<size_t>(errorDistribution.size() * fraction));
66 std::cout <<
"bulkErrorDistribution.size()=" << bulkErrorDistribution.size() << std::endl;
67 std::cout <<
"bulkErrorSquared=" << bulkErrorSquared << std::endl;
68 std::cout <<
"remaining error contribution (squared): " << std::accumulate(errorDistribution.begin()+bulkErrorDistribution.size(),errorDistribution.end(), 0.0,
AdaptationStrategy_Detail::Add()) << std::endl;
72 std::vector<int> visitedCells;
73 auto cend = err.descriptions.gridView.template end<0>();
74 for (
auto ci=gridManager.
grid().leafGridView().template begin<0>(); ci!=cend; ++ci)
76 for(
int i=0; i<bulkErrorDistribution.size(); ++i)
77 if(gridManager.
grid().leafIndexSet().index(*ci) == bulkErrorDistribution[i].second)
80 visitedCells.push_back(gridManager.
grid().leafIndexSet().index(*ci));
83 gridManager.
mark(1,*ci);
87 std::cout <<
"FIXED CELL FRACTION: marked " << counter <<
" of " << gridManager.
grid().size(0) <<
" cells for refinement." << std::endl;
99 static constexpr int dim = Grid::dimension;
100 typedef size_t IndexInt;
102 template <
typename... Args>
104 : gridManager(gridManager_), fraction(fraction_)
107 template <
class Err,
class ErrorRepresentation,
class Scalar>
108 void refineGrid_impl(Err
const& err, ErrorRepresentation& errorDistribution, Scalar tol)
110 tol *= fraction*fraction;
111 std::cout <<
"used tolerance: " << tol << std::endl;
113 std::cout <<
"max error2: " << std::max_element(errorDistribution.begin(), errorDistribution.end())->first << std::endl;
114 std::cout <<
"min error2: " << std::min_element(errorDistribution.begin(), errorDistribution.end())->first << std::endl;
116 Scalar bulkErrorSquared = 0;
117 std::vector<std::pair<Scalar,IndexInt> > bulkErrorDistribution;
119 while(bulkErrorSquared < tol)
121 bulkErrorDistribution.push_back(errorDistribution[bulkErrorDistribution.size()]);
122 bulkErrorSquared += bulkErrorDistribution.back().first;
125 std::cout <<
"bulkErrorDistribution.size()=" << bulkErrorDistribution.size() << std::endl;
126 std::cout <<
"bulkErrorSquared=" << bulkErrorSquared << std::endl;
127 std::cout <<
"remaining error contribution (squared): " << std::accumulate(errorDistribution.begin()+bulkErrorDistribution.size(),errorDistribution.end(), 0.0,
AdaptationStrategy_Detail::Add()) << std::endl;
131 std::vector<int> visitedCells;
132 auto cend = err.descriptions.gridView.template end<0>();
133 for (
auto ci=gridManager.
grid().leafGridView().template begin<0>(); ci!=cend; ++ci)
135 for(
int i=0; i<bulkErrorDistribution.size(); ++i)
136 if(gridManager.
grid().leafIndexSet().index(*ci) == bulkErrorDistribution[i].second)
139 visitedCells.push_back(gridManager.
grid().leafIndexSet().index(*ci));
142 gridManager.
mark(1,*ci);
146 std::cout <<
"FIXED ERROR FRACTION: marked " << counter <<
" of " << gridManager.
grid().size(0) <<
" cells for refinement." << std::endl;
155 template <
class Gr
id>
159 template <
typename... Args>
163 template <
class Err,
class ErrorRepresentation,
class Scalar>
164 void refineGrid_impl(Err
const& err, ErrorRepresentation& errorDistribution, Scalar tol)
166 std::cout <<
"ERROR EQUILIBRATION" << std::endl;
169 std::cout <<
"err distr" << std::endl;
196 Scalar N2 = gridManager.
grid().size(0);
198 Scalar relTol = tol/N2;
200 auto cend = gridManager.
grid().leafGridView().template end<0>();
202 Scalar minErr = errorDistribution.back().first, maxErr = errorDistribution[0].first;
206 std::cout <<
"grr" << std::endl;
207 std::cout <<
"marking cells..." << std::flush;
208 for (
auto ci=gridManager.
grid().leafGridView().template begin<0>(); ci!=cend; ++ci)
215 for(
size_t i=0; i<errorDistribution.size(); ++i)
216 if(gridManager.
grid().leafIndexSet().index(*ci) == errorDistribution[i].second)
232 if(errorDistribution[i].first > relTol){
233 std::cout <<
"marking cell at position " << i << std::endl;
235 gridManager.
mark(1,*ci);
239 std::cout <<
"done." << std::endl;
240 std::cout <<
"totalErrorSquared: " << tol <<
" -> tolerance: " << relTol << std::endl;
241 std::cout <<
"maxErr: " << maxErr <<
", minErr: " << minErr << std::endl;
242 std::cout <<
"refining " << counter <<
" of " << gridManager.
grid().size(0) <<
" cells" << std::endl;
244 std::cout <<
"adapting..." << std::flush;
246 std::cout <<
"done." << std::endl;
254 template <
class Gr
id>
258 template <
typename... Args>
262 template <
class Err,
class ErrorRepresentation,
class Scalar>
263 void refineGrid_impl(Err
const& err, ErrorRepresentation& errorDistribution, Scalar tol)
265 std::cout <<
"ERROR EQUILIBRATION" << std::endl;
268 Scalar n2 = gridManager.
grid().size(0);
269 Scalar relTol = tol/n2;
272 auto cend = gridManager.
grid().leafGridView().template end<0>();
274 Scalar minErr = errorDistribution.back().first, maxErr = errorDistribution[0].first;
275 std::vector<size_t> mainContributors;
279 std::cout <<
"marking cells..." << std::flush;
280 for (
auto ci=gridManager.
grid().leafGridView().template begin<0>(); ci!=cend; ++ci)
282 for(
size_t i=0; i<errorDistribution.size(); ++i)
283 if(gridManager.
grid().leafIndexSet().index(*ci) == errorDistribution[i].second)
285 if(errorDistribution[i].first > relTol){
286 if(errorDistribution[i].first > 4 * relTol) mainContributors.push_back(errorDistribution[i].second);
288 gridManager.
mark(1,*ci);
292 std::cout <<
"done." << std::endl;
293 std::cout <<
"totalErrorSquared: " << tol <<
" -> tolerance: " << relTol << std::endl;
294 std::cout <<
"maxErr: " << maxErr <<
", minErr: " << minErr << std::endl;
295 std::cout <<
"refining " << counter <<
" of " << gridManager.
grid().size(0) <<
" cells" << std::endl;
297 std::cout <<
"adapting..." << std::flush;
299 std::cout <<
"done." << std::endl;
ErrorEquilibration2(GridManagerBase< Grid > &gridManager_, Args...)
void refineGrid_impl(Err const &err, ErrorRepresentation &errorDistribution, Scalar tol)
void refineGrid_impl(Err const &err, ErrorRepresentation &errorDistribution, Scalar tol)
ErrorEquilibration(GridManagerBase< Grid > &gridManager_, Args...)
void refineGrid_impl(Err const &err, ErrorRepresentation &errorDistribution, Scalar tol)
FixedCellFraction(GridManagerBase< Grid > &gridManager_, double fraction_, Args...)
void refineGrid_impl(Err const &err, ErrorRepresentation &errorDistribution, Scalar tol)
FixedErrorFraction(GridManagerBase< Grid > &gridManager_, double fraction_, Args...)
Grid const & grid() const
Returns a const reference on the owned grid.
bool adaptAtOnce()
DEPRECATED Performs grid refinement without prolongating registered FE functions.
bool mark(int refCount, typename Grid::template Codim< 0 >::Entity const &cell)
Marks the given cell for refinement or coarsening.
Scalar operator()(Scalar x1, const std::pair< Scalar, IndexInt > &x2)
bool operator()(const std::pair< Scalar, IndexInt > &x1, const std::pair< Scalar, IndexInt > &x2)