Reference documentation for deal.II version GIT relicensing-1399-g2b3a69e058 2024-08-09 08:50:02+00:00
Searching...
No Matches
grid_refinement.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
4// Copyright (C) 2000 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15
17
19
21#include <deal.II/grid/tria.h>
24
25#include <deal.II/lac/vector.h>
26
27#include <algorithm>
28#include <cmath>
29#include <fstream>
30#include <functional>
31#include <limits>
32#include <numeric>
33
35
36namespace
37{
45 template <int dim, int spacedim, typename Number>
46 void
50 const double top_fraction,
51 const double bottom_fraction,
52 const unsigned int max_n_cells)
53 {
54 // sort the criteria in descending order in an auxiliary vector, which we
55 // have to sum up and compare with @p{fraction_of_error*total_error}
57 std::sort(criteria_sorted.begin(),
58 criteria_sorted.end(),
59 std::greater<double>());
60
61 const double total_error = criteria_sorted.l1_norm();
62
63 // compute thresholds
65 for (double sum = 0;
66 (sum < top_fraction * total_error) && (pp != criteria_sorted.end());
67 ++pp)
68 sum += *pp;
69 double top_threshold =
70 (pp != criteria_sorted.begin() ? (*pp + *(pp - 1)) / 2 : *pp);
71
73 for (double sum = 0; (sum < bottom_fraction * total_error) &&
74 (qq != criteria_sorted.begin() - 1);
75 --qq)
76 sum += *qq;
77 double bottom_threshold =
78 ((qq != criteria_sorted.end() - 1) ? (*qq + *(qq + 1)) / 2 : 0.);
79
80 // we now have an idea how many cells we
81 // are going to refine and coarsen. we use
82 // this information to see whether we are
83 // over the limit and if so use a function
84 // that knows how to deal with this
85 // situation
86
87 // note, that at this point, we have no
88 // information about anisotropically refined
89 // cells, thus use the situation of purely
90 // isotropic refinement as guess for a mixed
91 // refinemnt as well.
92 const unsigned int refine_cells = pp - criteria_sorted.begin(),
93 coarsen_cells = criteria_sorted.end() - 1 - qq;
94
95 if (static_cast<unsigned int>(
96 tria.n_active_cells() +
100 {
102 criteria,
103 1. * refine_cells /
104 criteria.size(),
105 1. * coarsen_cells /
106 criteria.size(),
108 return;
109 }
110
111 // in some rare cases it may happen that
112 // both thresholds are the same (e.g. if
113 // there are many cells with the same
114 // error indicator). That would mean that
115 // all cells will be flagged for
116 // refinement or coarsening, but some will
117 // be flagged for both, namely those for
118 // which the indicator equals the
119 // thresholds. This is forbidden, however.
120 //
121 // In some rare cases with very few cells
122 // we also could get integer round off
123 // errors and get problems with
124 // the top and bottom fractions.
125 //
126 // In these case we arbitrarily reduce the
127 // bottom threshold by one permille below
128 // the top threshold
129 //
130 // Finally, in some cases
131 // (especially involving symmetric
132 // solutions) there are many cells
133 // with the same error indicator
134 // values. if there are many with
135 // indicator equal to the top
136 // threshold, no refinement will
137 // take place below; to avoid this
138 // case, we also lower the top
139 // threshold if it equals the
140 // largest indicator and the
141 // top_fraction!=1
142 const double max_criterion = *(criteria_sorted.begin()),
143 min_criterion = *(criteria_sorted.end() - 1);
144
145 if ((top_threshold == max_criterion) && (top_fraction != 1))
146 top_threshold *= 0.999;
147
150
151 // actually flag cells
154
157 }
158} // namespace
159
160
161
162template <int dim, typename Number, int spacedim>
163void
166 const double threshold,
167 const unsigned int max_to_mark)
168{
169 Assert(criteria.size() == tria.n_active_cells(),
170 ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
171 Assert(criteria.is_non_negative(), ExcNegativeCriteria());
172
173 // when all indicators are zero we
174 // do not need to refine but only
175 // to coarsen
176 if (criteria.all_zero())
177 return;
178
179 const unsigned int n_cells = criteria.size();
180
181 // TODO: This is undocumented, looks fishy and seems unnecessary
182
183 double new_threshold = threshold;
184 // when threshold==0 find the
185 // smallest value in criteria
186 // greater 0
187 if (new_threshold == 0)
188 {
190 for (unsigned int index = 1; index < n_cells; ++index)
191 if (criteria(index) > 0 && (criteria(index) < new_threshold))
192 new_threshold = criteria(index);
193 }
194
195 unsigned int marked = 0;
196 for (const auto &cell : tria.active_cell_iterators())
198 &tria) == nullptr ||
199 cell->is_locally_owned()) &&
200 std::fabs(criteria(cell->active_cell_index())) >= new_threshold)
201 {
204 break;
205 ++marked;
206 cell->set_refine_flag();
207 }
208}
209
210
211
212template <int dim, typename Number, int spacedim>
213void
216 const double threshold)
217{
218 Assert(criteria.size() == tria.n_active_cells(),
219 ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
220 Assert(criteria.is_non_negative(), ExcNegativeCriteria());
221
222 for (const auto &cell : tria.active_cell_iterators())
224 &tria) == nullptr ||
225 cell->is_locally_owned()) &&
226 std::fabs(criteria(cell->active_cell_index())) <= threshold)
227 if (!cell->refine_flag_set())
228 cell->set_coarsen_flag();
229}
230
231
232
233template <int dim>
234std::pair<double, double>
238 const double top_fraction,
239 const double bottom_fraction)
240{
241 Assert(top_fraction >= 0, ExcInvalidParameterValue());
242 Assert(top_fraction <= 1, ExcInvalidParameterValue());
243 Assert(bottom_fraction >= 0, ExcInvalidParameterValue());
244 Assert(bottom_fraction <= 1, ExcInvalidParameterValue());
246 1 + 10 * std::numeric_limits<double>::epsilon(),
247 ExcInvalidParameterValue());
248
251
252 const double cell_increase_on_refine =
254 const double cell_decrease_on_coarsen =
256
257 std::pair<double, double> adjusted_fractions(top_fraction, bottom_fraction);
258 // first we have to see whether we
259 // currently already exceed the target
260 // number of cells
262 {
263 // if yes, then we need to stop
264 // refining cells and instead try to
265 // only coarsen as many as it would
266 // take to get to the target
267
268 // as we have no information on cells
269 // being refined isotropically or
270 // anisotropically, assume isotropic
271 // refinement here, though that may
272 // result in a worse approximation
273 adjusted_fractions.first = 0;
278 }
279 // otherwise, see if we would exceed the
280 // maximum desired number of cells with the
281 // number of cells that are likely going to
282 // result from refinement. here, each cell
283 // to be refined is replaced by
284 // C=GeometryInfo<dim>::max_children_per_cell
285 // new cells, i.e. there will be C-1 more
286 // cells than before. similarly, C cells
287 // will be replaced by 1
288
289 // again, this is true for isotropically
290 // refined cells. we take this as an
291 // approximation of a mixed refinement.
292 else if (static_cast<types::global_cell_index>(
295 {
296 // we have to adjust the
297 // fractions. assume we want
298 // alpha*refine_fraction and
299 // alpha*coarsen_fraction as new
300 // fractions and the resulting number
301 // of cells to be equal to
302 // max_n_cells. this leads to the
303 // following equation for alpha
304 const double alpha = 1. * (max_n_cells - current_n_cells) /
307
310 }
312}
313
314
315
316template <int dim, typename Number, int spacedim>
317void
321 const double top_fraction,
322 const double bottom_fraction,
323 const unsigned int max_n_cells)
324{
325 // correct number of cells is
326 // checked in @p{refine}
327 Assert((top_fraction >= 0) && (top_fraction <= 1),
328 ExcInvalidParameterValue());
329 Assert((bottom_fraction >= 0) && (bottom_fraction <= 1),
330 ExcInvalidParameterValue());
332 1 + 10 * std::numeric_limits<double>::epsilon(),
333 ExcInvalidParameterValue());
334 Assert(criteria.is_non_negative(), ExcNegativeCriteria());
335
336 const std::pair<double, double> adjusted_fractions =
341
342 const int refine_cells =
343 static_cast<int>(adjusted_fractions.first * criteria.size());
344 const int coarsen_cells =
345 static_cast<int>(adjusted_fractions.second * criteria.size());
346
348 {
350 if (refine_cells)
351 {
352 if (static_cast<size_t>(refine_cells) == criteria.size())
353 refine(tria, criteria, std::numeric_limits<double>::lowest());
354 else
355 {
356 std::nth_element(tmp.begin(),
357 tmp.begin() + refine_cells - 1,
358 tmp.end(),
359 std::greater<double>());
360 refine(tria, criteria, *(tmp.begin() + refine_cells - 1));
361 }
362 }
363
364 if (coarsen_cells)
365 {
366 if (static_cast<size_t>(coarsen_cells) == criteria.size())
367 coarsen(tria, criteria, std::numeric_limits<double>::max());
368 else
369 {
370 std::nth_element(tmp.begin(),
371 tmp.begin() + tmp.size() - coarsen_cells,
372 tmp.end(),
373 std::greater<double>());
374 coarsen(tria,
375 criteria,
376 *(tmp.begin() + tmp.size() - coarsen_cells));
377 }
378 }
379 }
380}
381
382
383
384template <int dim, typename Number, int spacedim>
385void
389 const double top_fraction,
390 const double bottom_fraction,
391 const unsigned int max_n_cells,
393{
394 // correct number of cells is checked in @p{refine}
395 Assert((top_fraction >= 0) && (top_fraction <= 1),
396 ExcInvalidParameterValue());
397 Assert((bottom_fraction >= 0) && (bottom_fraction <= 1),
398 ExcInvalidParameterValue());
400 1 + 10 * std::numeric_limits<double>::epsilon(),
401 ExcInvalidParameterValue());
402 Assert(criteria.is_non_negative(), ExcNegativeCriteria());
403
404 switch (norm_type)
405 {
407 // evaluate norms on subsets and compare them as
408 // c_0 + c_1 + ... < fraction * l1-norm(c)
411 break;
412
414 {
415 // we do not want to evaluate norms on subsets as:
416 // sqrt(c_0^2 + c_1^2 + ...) < fraction * l2-norm(c)
417 // instead take the square of both sides of the equation
418 // and evaluate:
419 // c_0^2 + c_1^2 + ... < fraction^2 * l1-norm(c.c)
420 // we adjust all parameters accordingly
422 std::transform(criteria.begin(),
423 criteria.end(),
424 criteria_squared.begin(),
425 [](Number c) { return c * c; });
426
434 }
435 break;
436
437 default:
439 break;
440 }
441}
442
443
444
445template <int dim, typename Number, int spacedim>
446void
449 const unsigned int order)
450{
451 Assert(criteria.size() == tria.n_active_cells(),
452 ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
453 Assert(criteria.is_non_negative(), ExcNegativeCriteria());
454
455 // get a decreasing order on the error indicator
456 std::vector<unsigned int> cell_indices(criteria.size());
457 std::iota(cell_indices.begin(), cell_indices.end(), 0u);
458
459 std::sort(cell_indices.begin(),
460 cell_indices.end(),
461 [&criteria](const unsigned int left, const unsigned int right) {
462 return criteria[left] > criteria[right];
463 });
464
465 double expected_error_reduction = 0;
466 const double original_error = criteria.l1_norm();
467
468 const std::size_t N = criteria.size();
469
470 // minimize the cost functional discussed in the documentation
471 double min_cost = std::numeric_limits<double>::max();
472 std::size_t min_arg = 0;
473
474 const double reduction_factor = (1. - std::pow(2., -1. * order));
475 for (std::size_t M = 0; M < criteria.size(); ++M)
476 {
478
479 const double cost =
480 std::pow(((Utilities::fixed_power<dim>(2) - 1) * (1 + M) + N),
481 static_cast<double>(order) / dim) *
483 if (cost <= min_cost)
484 {
485 min_cost = cost;
486 min_arg = M;
487 }
488 }
489
490 refine(tria, criteria, criteria(cell_indices[min_arg]));
491}
492
493
494// explicit instantiations
495#include "grid_refinement.inst"
496
Number l1_norm(const Tensor< 2, dim, Number > &t)
Definition tensor.h:3015
const value_type * const_iterator
Definition vector.h:142
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
#define DEAL_II_NOT_IMPLEMENTED()
void refine(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold, const unsigned int max_to_mark=numbers::invalid_unsigned_int)
void refine_and_coarsen_fixed_number(Triangulation< dim, spacedim > &triangulation, const Vector< Number > &criteria, const double top_fraction_of_cells, const double bottom_fraction_of_cells, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max())
void refine_and_coarsen_optimize(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const unsigned int order=2)
void coarsen(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double threshold)
std::pair< double, double > adjust_refine_and_coarsen_number_fraction(const types::global_cell_index current_n_cells, const types::global_cell_index max_n_cells, const double top_fraction_of_cells, const double bottom_fraction_of_cells)
void refine_and_coarsen_fixed_fraction(Triangulation< dim, spacedim > &tria, const Vector< Number > &criteria, const double top_fraction, const double bottom_fraction, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max(), const VectorTools::NormType norm_type=VectorTools::L1_norm)
T sum(const T &t, const MPI_Comm mpi_communicator)
static const unsigned int invalid_unsigned_int
Definition types.h:220
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > pow(const ::VectorizedArray< Number, width > &, const Number p)