Reference documentation for deal.II version 9.1.0-pre
grid_refinement.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #include <deal.II/base/utilities.h>
18 
19 #include <deal.II/lac/block_vector.h>
20 #include <deal.II/lac/trilinos_parallel_block_vector.h>
21 #include <deal.II/lac/trilinos_vector.h>
22 #include <deal.II/lac/vector.h>
23 
24 #ifdef DEAL_II_WITH_P4EST
25 
26 # include <deal.II/distributed/grid_refinement.h>
27 
28 # include <deal.II/grid/grid_refinement.h>
29 # include <deal.II/grid/tria.h>
30 # include <deal.II/grid/tria_accessor.h>
31 # include <deal.II/grid/tria_iterator.h>
32 
33 # include <algorithm>
34 # include <functional>
35 # include <limits>
36 # include <numeric>
37 
38 
39 DEAL_II_NAMESPACE_OPEN
40 
41 
42 namespace
43 {
44  template <typename number>
45  inline number
46  max_element(const ::Vector<number> &criteria)
47  {
48  return (criteria.size() > 0) ?
49  (*std::max_element(criteria.begin(), criteria.end())) :
50  std::numeric_limits<number>::min();
51  }
52 
53 
54 
55  template <typename number>
56  inline number
57  min_element(const ::Vector<number> &criteria)
58  {
59  return (criteria.size() > 0) ?
60  (*std::min_element(criteria.begin(), criteria.end())) :
61  std::numeric_limits<number>::max();
62  }
63 
64 
69  template <typename number>
70  std::pair<number, number>
71  compute_global_min_and_max_at_root(const ::Vector<number> &criteria,
72  MPI_Comm mpi_communicator)
73  {
74  // we'd like to compute the global max and min from the local ones in one
75  // MPI communication. we can do that by taking the elementwise minimum of
76  // the local min and the negative maximum over all processors
77 
78  const double local_min = min_element(criteria),
79  local_max = max_element(criteria);
80  double comp[2] = {local_min, -local_max};
81  double result[2] = {0, 0};
82 
83  // compute the minimum on processor zero
84  const int ierr =
85  MPI_Reduce(comp, result, 2, MPI_DOUBLE, MPI_MIN, 0, mpi_communicator);
86  AssertThrowMPI(ierr);
87 
88  // make sure only processor zero got something
89  if (Utilities::MPI::this_mpi_process(mpi_communicator) != 0)
90  Assert((result[0] == 0) && (result[1] == 0), ExcInternalError());
91 
92  return std::make_pair(result[0], -result[1]);
93  }
94 
95 
96 
102  template <typename number>
103  double
104  compute_global_sum(const ::Vector<number> &criteria,
105  MPI_Comm mpi_communicator)
106  {
107  double my_sum =
108  std::accumulate(criteria.begin(),
109  criteria.end(),
110  /* do accumulation in the correct data type: */
111  number());
112 
113  double result = 0;
114  // compute the minimum on processor zero
115  const int ierr =
116  MPI_Reduce(&my_sum, &result, 1, MPI_DOUBLE, MPI_SUM, 0, mpi_communicator);
117  AssertThrowMPI(ierr);
118 
119  // make sure only processor zero got something
120  if (Utilities::MPI::this_mpi_process(mpi_communicator) != 0)
121  Assert(result == 0, ExcInternalError());
122 
123  return result;
124  }
125 
126 
127 
132  template <int dim, int spacedim, typename Number>
133  void
134  get_locally_owned_indicators(
136  const ::Vector<Number> & criteria,
137  Vector<Number> &locally_owned_indicators)
138  {
139  Assert(locally_owned_indicators.size() ==
141  ExcInternalError());
142 
143  unsigned int owned_index = 0;
145  tria.begin_active();
146  cell != tria.end();
147  ++cell)
148  if (cell->subdomain_id() == tria.locally_owned_subdomain())
149  {
150  locally_owned_indicators(owned_index) =
151  criteria(cell->active_cell_index());
152  ++owned_index;
153  }
154  Assert(owned_index == tria.n_locally_owned_active_cells(),
155  ExcInternalError());
156  }
157 
158 
159  // we compute refinement thresholds by bisection of the interval spanned by
160  // the smallest and largest error indicator. this leads to a small problem:
161  // if, for example, we want to coarsen zero per cent of the cells, then we
162  // need to pick a threshold equal to the smallest indicator, but of course
163  // the bisection algorithm can never find a threshold equal to one of the
164  // end points of the interval. So we slightly increase the interval before
165  // we even start
166  void
167  adjust_interesting_range(double (&interesting_range)[2])
168  {
169  Assert(interesting_range[0] <= interesting_range[1], ExcInternalError());
170 
171  Assert(interesting_range[0] >= 0, ExcInternalError());
172 
173  // adjust the lower bound only if the end point is not equal to zero,
174  // otherwise it could happen, that the result becomes negative
175  if (interesting_range[0] > 0)
176  interesting_range[0] *= 0.99;
177 
178  if (interesting_range[1] > 0)
179  interesting_range[1] *= 1.01;
180  else
181  interesting_range[1] +=
182  0.01 * (interesting_range[1] - interesting_range[0]);
183  }
184 
185 
186 
192  template <int dim, int spacedim, typename Number>
193  void
195  const ::Vector<Number> & criteria,
196  const double top_threshold,
197  const double bottom_threshold)
198  {
199  ::GridRefinement::refine(tria, criteria, top_threshold);
200  ::GridRefinement::coarsen(tria, criteria, bottom_threshold);
201 
202  // as a final good measure, delete all flags again from cells that we don't
203  // locally own
205  tria.begin_active();
206  cell != tria.end();
207  ++cell)
208  if (cell->subdomain_id() != tria.locally_owned_subdomain())
209  {
210  cell->clear_refine_flag();
211  cell->clear_coarsen_flag();
212  }
213  }
214 
215 
216 
218  {
223  template <typename number>
224  number
225  compute_threshold(const ::Vector<number> & criteria,
226  const std::pair<double, double> &global_min_and_max,
227  const unsigned int n_target_cells,
228  MPI_Comm mpi_communicator)
229  {
230  double interesting_range[2] = {global_min_and_max.first,
231  global_min_and_max.second};
232  adjust_interesting_range(interesting_range);
233 
234  const unsigned int master_mpi_rank = 0;
235  unsigned int iteration = 0;
236 
237  do
238  {
239  int ierr = MPI_Bcast(interesting_range,
240  2,
241  MPI_DOUBLE,
242  master_mpi_rank,
243  mpi_communicator);
244  AssertThrowMPI(ierr);
245 
246  if (interesting_range[0] == interesting_range[1])
247  return interesting_range[0];
248 
249  const double test_threshold =
250  (interesting_range[0] > 0 ?
251  std::sqrt(interesting_range[0] * interesting_range[1]) :
252  (interesting_range[0] + interesting_range[1]) / 2);
253 
254  // count how many of our own elements would be above this threshold
255  // and then add to it the number for all the others
256  unsigned int my_count =
257  std::count_if(criteria.begin(),
258  criteria.end(),
259  [test_threshold](const double c) {
260  return c > test_threshold;
261  });
262 
263  unsigned int total_count;
264  ierr = MPI_Reduce(&my_count,
265  &total_count,
266  1,
267  MPI_UNSIGNED,
268  MPI_SUM,
269  master_mpi_rank,
270  mpi_communicator);
271  AssertThrowMPI(ierr);
272 
273  // now adjust the range. if we have to many cells, we take the upper
274  // half of the previous range, otherwise the lower half. if we have
275  // hit the right number, then set the range to the exact value.
276  // slave nodes also update their own interesting_range, however their
277  // results are not significant since the values will be overwritten by
278  // MPI_Bcast from the master node in next loop.
279  if (total_count > n_target_cells)
280  interesting_range[0] = test_threshold;
281  else if (total_count < n_target_cells)
282  interesting_range[1] = test_threshold;
283  else
284  interesting_range[0] = interesting_range[1] = test_threshold;
285 
286  // terminate the iteration after 25 go-arounds. this is necessary
287  // because oftentimes error indicators on cells have exactly the
288  // same value, and so there may not be a particular value that cuts
289  // the indicators in such a way that we can achieve the desired
290  // number of cells. using a maximal number of iterations means that
291  // we terminate the iteration after a fixed number N of steps if the
292  // indicators were perfectly badly distributed, and we make at most
293  // a mistake of 1/2^N in the number of cells flagged if indicators
294  // are perfectly equidistributed
295  ++iteration;
296  if (iteration == 25)
297  interesting_range[0] = interesting_range[1] = test_threshold;
298  }
299  while (true);
300 
301  Assert(false, ExcInternalError());
302  return -1;
303  }
304  } // namespace RefineAndCoarsenFixedNumber
305 
306 
307 
309  {
316  template <typename number>
317  number
318  compute_threshold(const ::Vector<number> & criteria,
319  const std::pair<double, double> &global_min_and_max,
320  const double target_error,
321  MPI_Comm mpi_communicator)
322  {
323  double interesting_range[2] = {global_min_and_max.first,
324  global_min_and_max.second};
325  adjust_interesting_range(interesting_range);
326 
327  const unsigned int master_mpi_rank = 0;
328  unsigned int iteration = 0;
329 
330  do
331  {
332  int ierr = MPI_Bcast(interesting_range,
333  2,
334  MPI_DOUBLE,
335  master_mpi_rank,
336  mpi_communicator);
337  AssertThrowMPI(ierr);
338 
339  if (interesting_range[0] == interesting_range[1])
340  {
341  // so we have found our threshold. since we adjust the range
342  // at the top of the function to be slightly larger than the
343  // actual extremes of the refinement criteria values, we can end
344  // up in a situation where the threshold is in fact larger than
345  // the maximal refinement indicator. in such cases, we get no
346  // refinement at all. thus, cap the threshold by the actual
347  // largest value
348  double final_threshold =
349  std::min(interesting_range[0], global_min_and_max.second);
350  ierr = MPI_Bcast(&final_threshold,
351  1,
352  MPI_DOUBLE,
353  master_mpi_rank,
354  mpi_communicator);
355  AssertThrowMPI(ierr);
356 
357  return final_threshold;
358  }
359 
360  const double test_threshold =
361  (interesting_range[0] > 0 ?
362  std::sqrt(interesting_range[0] * interesting_range[1]) :
363  (interesting_range[0] + interesting_range[1]) / 2);
364 
365  // accumulate the error of those our own elements above this threshold
366  // and then add to it the number for all the others
367  double my_error = 0;
368  for (unsigned int i = 0; i < criteria.size(); ++i)
369  if (criteria(i) > test_threshold)
370  my_error += criteria(i);
371 
372  double total_error;
373  ierr = MPI_Reduce(&my_error,
374  &total_error,
375  1,
376  MPI_DOUBLE,
377  MPI_SUM,
378  master_mpi_rank,
379  mpi_communicator);
380  AssertThrowMPI(ierr);
381 
382  // now adjust the range. if we have to many cells, we take the upper
383  // half of the previous range, otherwise the lower half. if we have
384  // hit the right number, then set the range to the exact value.
385  // slave nodes also update their own interesting_range, however their
386  // results are not significant since the values will be overwritten by
387  // MPI_Bcast from the master node in next loop.
388  if (total_error > target_error)
389  interesting_range[0] = test_threshold;
390  else if (total_error < target_error)
391  interesting_range[1] = test_threshold;
392  else
393  interesting_range[0] = interesting_range[1] = test_threshold;
394 
395  // terminate the iteration after 25 go-arounds. this is
396  // necessary because oftentimes error indicators on cells
397  // have exactly the same value, and so there may not be a
398  // particular value that cuts the indicators in such a way
399  // that we can achieve the desired number of cells. using a
400  // max of 25 iterations means that we terminate the
401  // iteration after 25 steps if the indicators were perfectly
402  // badly distributed, and we make at most a mistake of
403  // 1/2^25 in the number of cells flagged if indicators are
404  // perfectly equidistributed
405  ++iteration;
406  if (iteration == 25)
407  interesting_range[0] = interesting_range[1] = test_threshold;
408  }
409  while (true);
410 
411  Assert(false, ExcInternalError());
412  return -1;
413  }
414  } // namespace RefineAndCoarsenFixedFraction
415 } // namespace
416 
417 
418 
419 namespace parallel
420 {
421  namespace distributed
422  {
423  namespace GridRefinement
424  {
425  template <int dim, typename Number, int spacedim>
426  void
429  const ::Vector<Number> & criteria,
430  const double top_fraction_of_cells,
431  const double bottom_fraction_of_cells,
432  const unsigned int max_n_cells)
433  {
434  Assert(criteria.size() == tria.n_active_cells(),
435  ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
436  Assert((top_fraction_of_cells >= 0) && (top_fraction_of_cells <= 1),
438  Assert((bottom_fraction_of_cells >= 0) &&
439  (bottom_fraction_of_cells <= 1),
441  Assert(top_fraction_of_cells + bottom_fraction_of_cells <= 1,
443  Assert(criteria.is_non_negative(),
445 
446  const std::pair<double, double> adjusted_fractions =
447  ::GridRefinement::adjust_refine_and_coarsen_number_fraction<
448  dim>(tria.n_global_active_cells(),
449  max_n_cells,
450  top_fraction_of_cells,
451  bottom_fraction_of_cells);
452 
453  // first extract from the vector of indicators the ones that correspond
454  // to cells that we locally own
455  Vector<Number> locally_owned_indicators(
457  get_locally_owned_indicators(tria, criteria, locally_owned_indicators);
458 
459  MPI_Comm mpi_communicator = tria.get_communicator();
460 
461  // figure out the global max and min of the indicators. we don't need it
462  // here, but it's a collective communication call
463  const std::pair<Number, Number> global_min_and_max =
464  compute_global_min_and_max_at_root(locally_owned_indicators,
465  mpi_communicator);
466 
467 
468  double top_threshold, bottom_threshold;
469  top_threshold = RefineAndCoarsenFixedNumber::compute_threshold(
470  locally_owned_indicators,
471  global_min_and_max,
472  static_cast<unsigned int>(adjusted_fractions.first *
473  tria.n_global_active_cells()),
474  mpi_communicator);
475 
476  // compute bottom threshold only if necessary. otherwise use a threshold
477  // lower than the smallest value we have locally
478  if (adjusted_fractions.second > 0)
479  bottom_threshold = RefineAndCoarsenFixedNumber::compute_threshold(
480  locally_owned_indicators,
481  global_min_and_max,
482  static_cast<unsigned int>((1 - adjusted_fractions.second) *
483  tria.n_global_active_cells()),
484  mpi_communicator);
485  else
486  {
487  bottom_threshold =
488  *std::min_element(criteria.begin(), criteria.end());
489  bottom_threshold -= std::fabs(bottom_threshold);
490  }
491 
492  // now refine the mesh
493  mark_cells(tria, criteria, top_threshold, bottom_threshold);
494  }
495 
496 
497  template <int dim, typename Number, int spacedim>
498  void
501  const ::Vector<Number> & criteria,
502  const double top_fraction_of_error,
503  const double bottom_fraction_of_error)
504  {
505  Assert(criteria.size() == tria.n_active_cells(),
506  ExcDimensionMismatch(criteria.size(), tria.n_active_cells()));
507  Assert((top_fraction_of_error >= 0) && (top_fraction_of_error <= 1),
509  Assert((bottom_fraction_of_error >= 0) &&
510  (bottom_fraction_of_error <= 1),
512  Assert(top_fraction_of_error + bottom_fraction_of_error <= 1,
514  Assert(criteria.is_non_negative(),
516 
517  // first extract from the vector of indicators the ones that correspond
518  // to cells that we locally own
519  Vector<Number> locally_owned_indicators(
521  get_locally_owned_indicators(tria, criteria, locally_owned_indicators);
522 
523  MPI_Comm mpi_communicator = tria.get_communicator();
524 
525  // figure out the global max and min of the indicators. we don't need it
526  // here, but it's a collective communication call
527  const std::pair<double, double> global_min_and_max =
528  compute_global_min_and_max_at_root(locally_owned_indicators,
529  mpi_communicator);
530 
531  const double total_error =
532  compute_global_sum(locally_owned_indicators, mpi_communicator);
533  double top_threshold, bottom_threshold;
534  top_threshold = RefineAndCoarsenFixedFraction::compute_threshold(
535  locally_owned_indicators,
536  global_min_and_max,
537  top_fraction_of_error * total_error,
538  mpi_communicator);
539  // compute bottom threshold only if necessary. otherwise use a threshold
540  // lower than the smallest value we have locally
541  if (bottom_fraction_of_error > 0)
542  bottom_threshold = RefineAndCoarsenFixedFraction::compute_threshold(
543  locally_owned_indicators,
544  global_min_and_max,
545  (1 - bottom_fraction_of_error) * total_error,
546  mpi_communicator);
547  else
548  {
549  bottom_threshold =
550  *std::min_element(criteria.begin(), criteria.end());
551  bottom_threshold -= std::fabs(bottom_threshold);
552  }
553 
554  // now refine the mesh
555  mark_cells(tria, criteria, top_threshold, bottom_threshold);
556  }
557  } // namespace GridRefinement
558  } // namespace distributed
559 } // namespace parallel
560 
561 
562 // explicit instantiations
563 # include "grid_refinement.inst"
564 
565 DEAL_II_NAMESPACE_CLOSE
566 
567 #endif
types::subdomain_id locally_owned_subdomain() const override
Definition: tria_base.cc:335
cell_iterator end() const
Definition: tria.cc:12004
void refine_and_coarsen_fixed_fraction(parallel::distributed::Triangulation< dim, spacedim > &tria, const ::Vector< Number > &criteria, const double top_fraction_of_error, const double bottom_fraction_of_error)
unsigned int n_active_cells() const
Definition: tria.cc:12509
virtual MPI_Comm get_communicator() const
Definition: tria_base.cc:155
static::ExceptionBase & ExcNegativeCriteria()
#define Assert(cond, exc)
Definition: exceptions.h:1227
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void refine_and_coarsen_fixed_number(parallel::distributed::Triangulation< dim, spacedim > &tria, 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())
unsigned int n_locally_owned_active_cells() const
Definition: tria_base.cc:126
#define AssertThrowMPI(error_code)
Definition: exceptions.h:1443
unsigned int this_mpi_process(const MPI_Comm &mpi_communicator)
Definition: mpi.cc:80
virtual types::global_dof_index n_global_active_cells() const override
Definition: tria_base.cc:140
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: tria.cc:11935
static::ExceptionBase & ExcInvalidParameterValue()
static::ExceptionBase & ExcInternalError()