Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
grid_tools_dof_handlers.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2001 - 2019 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 #include <deal.II/base/geometry_info.h>
17 #include <deal.II/base/point.h>
18 #include <deal.II/base/tensor.h>
19 
20 #include <deal.II/distributed/shared_tria.h>
21 #include <deal.II/distributed/tria.h>
22 
23 #include <deal.II/dofs/dof_accessor.h>
24 #include <deal.II/dofs/dof_handler.h>
25 
26 #include <deal.II/fe/mapping_q1.h>
27 #include <deal.II/fe/mapping_q_generic.h>
28 
29 #include <deal.II/grid/filtered_iterator.h>
30 #include <deal.II/grid/grid_tools.h>
31 #include <deal.II/grid/tria.h>
32 #include <deal.II/grid/tria_accessor.h>
33 #include <deal.II/grid/tria_iterator.h>
34 
35 #include <deal.II/hp/dof_handler.h>
36 #include <deal.II/hp/mapping_collection.h>
37 
38 #include <deal.II/lac/full_matrix.h>
39 
40 #include <algorithm>
41 #include <array>
42 #include <cmath>
43 #include <list>
44 #include <map>
45 #include <numeric>
46 #include <set>
47 #include <vector>
48 
49 
50 DEAL_II_NAMESPACE_OPEN
51 
52 namespace GridTools
53 {
54  template <int dim, template <int, int> class MeshType, int spacedim>
55  unsigned int
56  find_closest_vertex(const MeshType<dim, spacedim> &mesh,
57  const Point<spacedim> & p,
58  const std::vector<bool> & marked_vertices)
59  {
60  // first get the underlying
61  // triangulation from the
62  // mesh and determine vertices
63  // and used vertices
65 
66  const std::vector<Point<spacedim>> &vertices = tria.get_vertices();
67 
68  Assert(tria.get_vertices().size() == marked_vertices.size() ||
69  marked_vertices.size() == 0,
70  ExcDimensionMismatch(tria.get_vertices().size(),
71  marked_vertices.size()));
72 
73  // If p is an element of marked_vertices,
74  // and q is that of used_Vertices,
75  // the vector marked_vertices does NOT
76  // contain unused vertices if p implies q.
77  // I.e., if p is true q must be true
78  // (if p is false, q could be false or true).
79  // p implies q logic is encapsulated in ~p|q.
80  Assert(
81  marked_vertices.size() == 0 ||
82  std::equal(marked_vertices.begin(),
83  marked_vertices.end(),
84  tria.get_used_vertices().begin(),
85  [](bool p, bool q) { return !p || q; }),
86  ExcMessage(
87  "marked_vertices should be a subset of used vertices in the triangulation "
88  "but marked_vertices contains one or more vertices that are not used vertices!"));
89 
90  // In addition, if a vector bools
91  // is specified (marked_vertices)
92  // marking all the vertices which
93  // could be the potentially closest
94  // vertex to the point, use it instead
95  // of used vertices
96  const std::vector<bool> &used = (marked_vertices.size() == 0) ?
97  tria.get_used_vertices() :
98  marked_vertices;
99 
100  // At the beginning, the first
101  // used vertex is the closest one
102  std::vector<bool>::const_iterator first =
103  std::find(used.begin(), used.end(), true);
104 
105  // Assert that at least one vertex
106  // is actually used
107  Assert(first != used.end(), ExcInternalError());
108 
109  unsigned int best_vertex = std::distance(used.begin(), first);
110  double best_dist = (p - vertices[best_vertex]).norm_square();
111 
112  // For all remaining vertices, test
113  // whether they are any closer
114  for (unsigned int j = best_vertex + 1; j < vertices.size(); j++)
115  if (used[j])
116  {
117  double dist = (p - vertices[j]).norm_square();
118  if (dist < best_dist)
119  {
120  best_vertex = j;
121  best_dist = dist;
122  }
123  }
124 
125  return best_vertex;
126  }
127 
128 
129 
130  template <int dim, template <int, int> class MeshType, int spacedim>
131  unsigned int
133  const MeshType<dim, spacedim> &mesh,
134  const Point<spacedim> & p,
135  const std::vector<bool> & marked_vertices)
136  {
137  // Take a shortcut in the simple case.
138  if (mapping.preserves_vertex_locations() == true)
139  return find_closest_vertex(mesh, p, marked_vertices);
140 
141  // first get the underlying
142  // triangulation from the
143  // mesh and determine vertices
144  // and used vertices
145  const Triangulation<dim, spacedim> &tria = mesh.get_triangulation();
146 
147  auto vertices = extract_used_vertices(tria, mapping);
148 
149  Assert(tria.get_vertices().size() == marked_vertices.size() ||
150  marked_vertices.size() == 0,
151  ExcDimensionMismatch(tria.get_vertices().size(),
152  marked_vertices.size()));
153 
154  // If p is an element of marked_vertices,
155  // and q is that of used_Vertices,
156  // the vector marked_vertices does NOT
157  // contain unused vertices if p implies q.
158  // I.e., if p is true q must be true
159  // (if p is false, q could be false or true).
160  // p implies q logic is encapsulated in ~p|q.
161  Assert(
162  marked_vertices.size() == 0 ||
163  std::equal(marked_vertices.begin(),
164  marked_vertices.end(),
165  tria.get_used_vertices().begin(),
166  [](bool p, bool q) { return !p || q; }),
167  ExcMessage(
168  "marked_vertices should be a subset of used vertices in the triangulation "
169  "but marked_vertices contains one or more vertices that are not used vertices!"));
170 
171  // Remove from the map unwanted elements.
172  if (marked_vertices.size())
173  for (auto it = vertices.begin(); it != vertices.end();)
174  {
175  if (marked_vertices[it->first] == false)
176  {
177  vertices.erase(it++);
178  }
179  else
180  {
181  ++it;
182  }
183  }
184 
185  return find_closest_vertex(vertices, p);
186  }
187 
188 
189 
190  template <int dim, template <int, int> class MeshType, int spacedim>
191 #ifndef _MSC_VER
192  std::vector<typename MeshType<dim, spacedim>::active_cell_iterator>
193 #else
194  std::vector<
195  typename ::internal::
196  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>
197 #endif
198  find_cells_adjacent_to_vertex(const MeshType<dim, spacedim> &mesh,
199  const unsigned int vertex)
200  {
201  // make sure that the given vertex is
202  // an active vertex of the underlying
203  // triangulation
204  Assert(vertex < mesh.get_triangulation().n_vertices(),
205  ExcIndexRange(0, mesh.get_triangulation().n_vertices(), vertex));
206  Assert(mesh.get_triangulation().get_used_vertices()[vertex],
207  ExcVertexNotUsed(vertex));
208 
209  // use a set instead of a vector
210  // to ensure that cells are inserted only
211  // once
212  std::set<typename ::internal::
213  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>
214  adjacent_cells;
215 
216  typename ::internal::
217  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type
218  cell = mesh.begin_active(),
219  endc = mesh.end();
220 
221  // go through all active cells and look if the vertex is part of that cell
222  //
223  // in 1d, this is all we need to care about. in 2d/3d we also need to worry
224  // that the vertex might be a hanging node on a face or edge of a cell; in
225  // this case, we would want to add those cells as well on whose faces the
226  // vertex is located but for which it is not a vertex itself.
227  //
228  // getting this right is a lot simpler in 2d than in 3d. in 2d, a hanging
229  // node can only be in the middle of a face and we can query the neighboring
230  // cell from the current cell. on the other hand, in 3d a hanging node
231  // vertex can also be on an edge but there can be many other cells on
232  // this edge and we can not access them from the cell we are currently
233  // on.
234  //
235  // so, in the 3d case, if we run the algorithm as in 2d, we catch all
236  // those cells for which the vertex we seek is on a *subface*, but we
237  // miss the case of cells for which the vertex we seek is on a
238  // sub-edge for which there is no corresponding sub-face (because the
239  // immediate neighbor behind this face is not refined), see for example
240  // the bits/find_cells_adjacent_to_vertex_6 testcase. thus, if we
241  // haven't yet found the vertex for the current cell we also need to
242  // look at the mid-points of edges
243  //
244  // as a final note, deciding whether a neighbor is actually coarser is
245  // simple in the case of isotropic refinement (we just need to look at
246  // the level of the current and the neighboring cell). however, this
247  // isn't so simple if we have used anisotropic refinement since then
248  // the level of a cell is not indicative of whether it is coarser or
249  // not than the current cell. ultimately, we want to add all cells on
250  // which the vertex is, independent of whether they are coarser or
251  // finer and so in the 2d case below we simply add *any* *active* neighbor.
252  // in the worst case, we add cells multiple times to the adjacent_cells
253  // list, but std::set throws out those cells already entered
254  for (; cell != endc; ++cell)
255  {
256  for (unsigned int v = 0; v < GeometryInfo<dim>::vertices_per_cell; v++)
257  if (cell->vertex_index(v) == vertex)
258  {
259  // OK, we found a cell that contains
260  // the given vertex. We add it
261  // to the list.
262  adjacent_cells.insert(cell);
263 
264  // as explained above, in 2+d we need to check whether
265  // this vertex is on a face behind which there is a
266  // (possibly) coarser neighbor. if this is the case,
267  // then we need to also add this neighbor
268  if (dim >= 2)
269  for (unsigned int vface = 0; vface < dim; vface++)
270  {
271  const unsigned int face =
273 
274  if (!cell->at_boundary(face) &&
275  cell->neighbor(face)->active())
276  {
277  // there is a (possibly) coarser cell behind a
278  // face to which the vertex belongs. the
279  // vertex we are looking at is then either a
280  // vertex of that coarser neighbor, or it is a
281  // hanging node on one of the faces of that
282  // cell. in either case, it is adjacent to the
283  // vertex, so add it to the list as well (if
284  // the cell was already in the list then the
285  // std::set makes sure that we get it only
286  // once)
287  adjacent_cells.insert(cell->neighbor(face));
288  }
289  }
290 
291  // in any case, we have found a cell, so go to the next cell
292  goto next_cell;
293  }
294 
295  // in 3d also loop over the edges
296  if (dim >= 3)
297  {
298  for (unsigned int e = 0; e < GeometryInfo<dim>::lines_per_cell; ++e)
299  if (cell->line(e)->has_children())
300  // the only place where this vertex could have been
301  // hiding is on the mid-edge point of the edge we
302  // are looking at
303  if (cell->line(e)->child(0)->vertex_index(1) == vertex)
304  {
305  adjacent_cells.insert(cell);
306 
307  // jump out of this tangle of nested loops
308  goto next_cell;
309  }
310  }
311 
312  // in more than 3d we would probably have to do the same as
313  // above also for even lower-dimensional objects
314  Assert(dim <= 3, ExcNotImplemented());
315 
316  // move on to the next cell if we have found the
317  // vertex on the current one
318  next_cell:;
319  }
320 
321  // if this was an active vertex then there needs to have been
322  // at least one cell to which it is adjacent!
323  Assert(adjacent_cells.size() > 0, ExcInternalError());
324 
325  // return the result as a vector, rather than the set we built above
326  return std::vector<
327  typename ::internal::
328  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>(
329  adjacent_cells.begin(), adjacent_cells.end());
330  }
331 
332 
333 
334  namespace
335  {
336  template <int dim, template <int, int> class MeshType, int spacedim>
337  void
338  find_active_cell_around_point_internal(
339  const MeshType<dim, spacedim> &mesh,
340 #ifndef _MSC_VER
341  std::set<typename MeshType<dim, spacedim>::active_cell_iterator>
342  &searched_cells,
343  std::set<typename MeshType<dim, spacedim>::active_cell_iterator>
344  &adjacent_cells)
345 #else
346  std::set<
347  typename ::internal::
348  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>
349  &searched_cells,
350  std::set<
351  typename ::internal::
352  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type>
353  &adjacent_cells)
354 #endif
355  {
356 #ifndef _MSC_VER
357  using cell_iterator =
358  typename MeshType<dim, spacedim>::active_cell_iterator;
359 #else
360  using cell_iterator = typename ::internal::
361  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type;
362 #endif
363 
364  // update the searched cells
365  searched_cells.insert(adjacent_cells.begin(), adjacent_cells.end());
366  // now we to collect all neighbors
367  // of the cells in adjacent_cells we
368  // have not yet searched.
369  std::set<cell_iterator> adjacent_cells_new;
370 
371  typename std::set<cell_iterator>::const_iterator cell =
372  adjacent_cells.begin(),
373  endc =
374  adjacent_cells.end();
375  for (; cell != endc; ++cell)
376  {
377  std::vector<cell_iterator> active_neighbors;
378  get_active_neighbors<MeshType<dim, spacedim>>(*cell,
379  active_neighbors);
380  for (unsigned int i = 0; i < active_neighbors.size(); ++i)
381  if (searched_cells.find(active_neighbors[i]) ==
382  searched_cells.end())
383  adjacent_cells_new.insert(active_neighbors[i]);
384  }
385  adjacent_cells.clear();
386  adjacent_cells.insert(adjacent_cells_new.begin(),
387  adjacent_cells_new.end());
388  if (adjacent_cells.size() == 0)
389  {
390  // we haven't found any other cell that would be a
391  // neighbor of a previously found cell, but we know
392  // that we haven't checked all cells yet. that means
393  // that the domain is disconnected. in that case,
394  // choose the first previously untouched cell we
395  // can find
396  cell_iterator it = mesh.begin_active();
397  for (; it != mesh.end(); ++it)
398  if (searched_cells.find(it) == searched_cells.end())
399  {
400  adjacent_cells.insert(it);
401  break;
402  }
403  }
404  }
405 
406 
407 
408  template <int dim, template <int, int> class MeshType, int spacedim>
409 #ifndef _MSC_VER
410  std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
411  Point<dim>>
412 #else
413  std::pair<
414  typename ::internal::
415  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
416  Point<dim>>
417 #endif
418  find_active_cell_around_point_tolerance(
419  const Mapping<dim, spacedim> & mapping,
420  const MeshType<dim, spacedim> &mesh,
421  const Point<spacedim> & p,
422  const std::vector<bool> & marked_vertices,
423  const double tolerance)
424  {
425  using active_cell_iterator = typename ::internal::
426  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type;
427 
428  // The best distance is set to the
429  // maximum allowable distance from
430  // the unit cell; we assume a
431  // max. deviation of the given tolerance
432  double best_distance = tolerance;
433  int best_level = -1;
434  std::pair<active_cell_iterator, Point<dim>> best_cell;
435 
436  // Find closest vertex and determine
437  // all adjacent cells
438  std::vector<active_cell_iterator> adjacent_cells_tmp =
440  mesh, find_closest_vertex(mapping, mesh, p, marked_vertices));
441 
442  // Make sure that we have found
443  // at least one cell adjacent to vertex.
444  Assert(adjacent_cells_tmp.size() > 0, ExcInternalError());
445 
446  // Copy all the cells into a std::set
447  std::set<active_cell_iterator> adjacent_cells(adjacent_cells_tmp.begin(),
448  adjacent_cells_tmp.end());
449  std::set<active_cell_iterator> searched_cells;
450 
451  // Determine the maximal number of cells
452  // in the grid.
453  // As long as we have not found
454  // the cell and have not searched
455  // every cell in the triangulation,
456  // we keep on looking.
457  const unsigned int n_active_cells =
458  mesh.get_triangulation().n_active_cells();
459  bool found = false;
460  unsigned int cells_searched = 0;
461  while (!found && cells_searched < n_active_cells)
462  {
463  typename std::set<active_cell_iterator>::const_iterator
464  cell = adjacent_cells.begin(),
465  endc = adjacent_cells.end();
466  for (; cell != endc; ++cell)
467  {
468  try
469  {
470  const Point<dim> p_cell =
471  mapping.transform_real_to_unit_cell(*cell, p);
472 
473  // calculate the infinity norm of
474  // the distance vector to the unit cell.
475  const double dist =
477 
478  // We compare if the point is inside the
479  // unit cell (or at least not too far
480  // outside). If it is, it is also checked
481  // that the cell has a more refined state
482  if ((dist < best_distance) ||
483  ((dist == best_distance) &&
484  ((*cell)->level() > best_level)))
485  {
486  found = true;
487  best_distance = dist;
488  best_level = (*cell)->level();
489  best_cell = std::make_pair(*cell, p_cell);
490  }
491  }
492  catch (
494  &)
495  {
496  // ok, the transformation
497  // failed presumably
498  // because the point we
499  // are looking for lies
500  // outside the current
501  // cell. this means that
502  // the current cell can't
503  // be the cell around the
504  // point, so just ignore
505  // this cell and move on
506  // to the next
507  }
508  }
509 
510  // update the number of cells searched
511  cells_searched += adjacent_cells.size();
512 
513  // if the user provided a custom mask for vertices,
514  // terminate the search without trying to expand the search
515  // to all cells of the triangulation, as done below.
516  if (marked_vertices.size() > 0)
517  cells_searched = n_active_cells;
518 
519  // if we have not found the cell in
520  // question and have not yet searched every
521  // cell, we expand our search to
522  // all the not already searched neighbors of
523  // the cells in adjacent_cells. This is
524  // what find_active_cell_around_point_internal
525  // is for.
526  if (!found && cells_searched < n_active_cells)
527  {
528  find_active_cell_around_point_internal<dim, MeshType, spacedim>(
529  mesh, searched_cells, adjacent_cells);
530  }
531  }
532 
533  AssertThrow(best_cell.first.state() == IteratorState::valid,
534  ExcPointNotFound<spacedim>(p));
535 
536  return best_cell;
537  }
538  } // namespace
539 
540 
541 
542  template <int dim, template <int, int> class MeshType, int spacedim>
543 #ifndef _MSC_VER
544  typename MeshType<dim, spacedim>::active_cell_iterator
545 #else
546  typename ::internal::
547  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type
548 #endif
549  find_active_cell_around_point(const MeshType<dim, spacedim> &mesh,
550  const Point<spacedim> & p,
551  const std::vector<bool> & marked_vertices)
552  {
553  return find_active_cell_around_point<dim, MeshType, spacedim>(
554  StaticMappingQ1<dim, spacedim>::mapping, mesh, p, marked_vertices)
555  .first;
556  }
557 
558 
559 
560  template <int dim, template <int, int> class MeshType, int spacedim>
561 #ifndef _MSC_VER
562  std::pair<typename MeshType<dim, spacedim>::active_cell_iterator, Point<dim>>
563 #else
564  std::pair<typename ::internal::
565  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
566  Point<dim>>
567 #endif
569  const MeshType<dim, spacedim> &mesh,
570  const Point<spacedim> & p,
571  const std::vector<bool> & marked_vertices)
572  {
573  return find_active_cell_around_point_tolerance(
574  mapping, mesh, p, marked_vertices, 1e-10);
575  }
576 
577 
578 
579  template <int dim, template <int, int> class MeshType, int spacedim>
580 #ifndef _MSC_VER
581  std::vector<std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
582  Point<dim>>>
583 #else
584  std::vector<std::pair<
585  typename ::internal::
586  ActiveCellIterator<dim, spacedim, MeshType<dim, spacedim>>::type,
587  Point<dim>>>
588 #endif
590  const MeshType<dim, spacedim> &mesh,
591  const Point<spacedim> & p,
592  const double tolerance,
593  const std::vector<bool> &marked_vertices)
594  {
595  // first use the result of the single point function as a guess. In order
596  // not to make the other find_all_active_cells_around_point more expensive
597  // and avoid some additional logic there, we first start with one cell as
598  // given by that other function (that possibly goes through a larger set
599  // of cells) and later add a list of more cells as appropriate.
600  std::vector<
601  std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
602  Point<dim>>>
603  cells_and_points;
604  try
605  {
606  cells_and_points.push_back(find_active_cell_around_point_tolerance(
607  mapping, mesh, p, marked_vertices, tolerance));
608  }
609  catch (ExcPointNotFound<spacedim> &)
610  {}
611 
612  if (!cells_and_points.empty())
613  {
614  // check if the given point is on the surface of the unit cell. if yes,
615  // need to find all neighbors
616  const Point<dim> unit_point = cells_and_points.front().second;
617  const auto my_cell = cells_and_points.front().first;
618  Tensor<1, dim> distance_to_center;
619  unsigned int n_dirs_at_threshold = 0;
620  unsigned int last_point_at_threshold = numbers::invalid_unsigned_int;
621  for (unsigned int d = 0; d < dim; ++d)
622  {
623  distance_to_center[d] = std::abs(unit_point[d] - 0.5);
624  if (distance_to_center[d] > 0.5 - tolerance)
625  {
626  ++n_dirs_at_threshold;
627  last_point_at_threshold = d;
628  }
629  }
630 
631  std::vector<typename MeshType<dim, spacedim>::active_cell_iterator>
632  cells_to_add;
633  // point is within face -> only need neighbor
634  if (n_dirs_at_threshold == 1)
635  {
636  unsigned int neighbor_index =
637  2 * last_point_at_threshold +
638  (unit_point[last_point_at_threshold] > 0.5 ? 1 : 0);
639  if (!my_cell->at_boundary(neighbor_index))
640  cells_to_add.push_back(my_cell->neighbor(neighbor_index));
641  }
642  // corner point -> use all neighbors
643  else if (n_dirs_at_threshold == dim)
644  {
645  unsigned int local_vertex_index = 0;
646  for (unsigned int d = 0; d < dim; ++d)
647  local_vertex_index += (unit_point[d] > 0.5 ? 1 : 0) << d;
648  std::vector<typename MeshType<dim, spacedim>::active_cell_iterator>
650  mesh, my_cell->vertex_index(local_vertex_index));
651  for (const auto &cell : cells)
652  if (cell != my_cell)
653  cells_to_add.push_back(cell);
654  }
655  // point on line in 3D: We cannot simply take the intersection between
656  // the two vertices of cels because of hanging nodes. So instead we
657  // list the vertices around both points and then select the
658  // appropriate cells according to the result of read_to_unit_cell
659  // below.
660  else if (n_dirs_at_threshold == 2)
661  {
662  std::pair<unsigned int, unsigned int> vertex_indices[3];
663  unsigned int count_vertex_indices = 0;
664  unsigned int free_direction = numbers::invalid_unsigned_int;
665  for (unsigned int d = 0; d < dim; ++d)
666  {
667  if (distance_to_center[d] > 0.5 - tolerance)
668  {
669  vertex_indices[count_vertex_indices].first = d;
670  vertex_indices[count_vertex_indices].second =
671  unit_point[d] > 0.5 ? 1 : 0;
672  count_vertex_indices++;
673  }
674  else
675  free_direction = d;
676  }
677 
678  AssertDimension(count_vertex_indices, 2);
679  Assert(free_direction != numbers::invalid_unsigned_int,
680  ExcInternalError());
681 
682  const unsigned int first_vertex =
683  (vertex_indices[0].second << vertex_indices[0].first) +
684  (vertex_indices[1].second << vertex_indices[1].first);
685  for (unsigned int d = 0; d < 2; ++d)
686  {
687  auto tentative_cells = find_cells_adjacent_to_vertex(
688  mesh,
689  my_cell->vertex_index(first_vertex + (d << free_direction)));
690  for (const auto &cell : tentative_cells)
691  {
692  bool cell_not_yet_present = true;
693  for (const auto &other_cell : cells_to_add)
694  if (cell == other_cell)
695  {
696  cell_not_yet_present = false;
697  break;
698  }
699  if (cell_not_yet_present)
700  cells_to_add.push_back(cell);
701  }
702  }
703  }
704 
705  const double original_distance_to_unit_cell =
707  for (const auto &cell : cells_to_add)
708  {
709  if (cell != my_cell)
710  try
711  {
712  const Point<dim> p_unit =
713  mapping.transform_real_to_unit_cell(cell, p);
715  original_distance_to_unit_cell + tolerance)
716  cells_and_points.emplace_back(cell, p_unit);
717  }
718  catch (typename Mapping<dim>::ExcTransformationFailed &)
719  {}
720  }
721  }
722  std::sort(
723  cells_and_points.begin(),
724  cells_and_points.end(),
725  [](const std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
726  Point<dim>> &a,
727  const std::pair<typename MeshType<dim, spacedim>::active_cell_iterator,
728  Point<dim>> &b) { return a.first < b.first; });
729 
730  return cells_and_points;
731  }
732 
733 
734 
735  template <class MeshType>
736  std::vector<typename MeshType::active_cell_iterator>
738  const MeshType &mesh,
739  const std::function<bool(const typename MeshType::active_cell_iterator &)>
740  &predicate)
741  {
742  std::vector<typename MeshType::active_cell_iterator> active_halo_layer;
743  std::vector<bool> locally_active_vertices_on_subdomain(
744  mesh.get_triangulation().n_vertices(), false);
745 
746  // Find the cells for which the predicate is true
747  // These are the cells around which we wish to construct
748  // the halo layer
749  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
750  cell != mesh.end();
751  ++cell)
752  if (predicate(cell)) // True predicate --> Part of subdomain
753  for (unsigned int v = 0;
754  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
755  ++v)
756  locally_active_vertices_on_subdomain[cell->vertex_index(v)] = true;
757 
758  // Find the cells that do not conform to the predicate
759  // but share a vertex with the selected subdomain
760  // These comprise the halo layer
761  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
762  cell != mesh.end();
763  ++cell)
764  if (!predicate(cell)) // False predicate --> Potential halo cell
765  for (unsigned int v = 0;
766  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
767  ++v)
768  if (locally_active_vertices_on_subdomain[cell->vertex_index(v)] ==
769  true)
770  {
771  active_halo_layer.push_back(cell);
772  break;
773  }
774 
775  return active_halo_layer;
776  }
777 
778 
779 
780  template <class MeshType>
781  std::vector<typename MeshType::cell_iterator>
783  const MeshType &mesh,
784  const std::function<bool(const typename MeshType::cell_iterator &)>
785  & predicate,
786  const unsigned int level)
787  {
788  std::vector<typename MeshType::cell_iterator> level_halo_layer;
789  std::vector<bool> locally_active_vertices_on_level_subdomain(
790  mesh.get_triangulation().n_vertices(), false);
791 
792  // Find the cells for which the predicate is true
793  // These are the cells around which we wish to construct
794  // the halo layer
795  for (typename MeshType::cell_iterator cell = mesh.begin(level);
796  cell != mesh.end(level);
797  ++cell)
798  if (predicate(cell)) // True predicate --> Part of subdomain
799  for (unsigned int v = 0;
800  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
801  ++v)
802  locally_active_vertices_on_level_subdomain[cell->vertex_index(v)] =
803  true;
804 
805  // Find the cells that do not conform to the predicate
806  // but share a vertex with the selected subdomain on that level
807  // These comprise the halo layer
808  for (typename MeshType::cell_iterator cell = mesh.begin(level);
809  cell != mesh.end(level);
810  ++cell)
811  if (!predicate(cell)) // False predicate --> Potential halo cell
812  for (unsigned int v = 0;
813  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
814  ++v)
815  if (locally_active_vertices_on_level_subdomain[cell->vertex_index(
816  v)] == true)
817  {
818  level_halo_layer.push_back(cell);
819  break;
820  }
821 
822  return level_halo_layer;
823  }
824 
825 
826  namespace
827  {
828  template <class MeshType>
829  bool
830  contains_locally_owned_cells(
831  const std::vector<typename MeshType::active_cell_iterator> &cells)
832  {
833  for (typename std::vector<
834  typename MeshType::active_cell_iterator>::const_iterator it =
835  cells.begin();
836  it != cells.end();
837  ++it)
838  {
839  if ((*it)->is_locally_owned())
840  return true;
841  }
842  return false;
843  }
844 
845  template <class MeshType>
846  bool
847  contains_artificial_cells(
848  const std::vector<typename MeshType::active_cell_iterator> &cells)
849  {
850  for (typename std::vector<
851  typename MeshType::active_cell_iterator>::const_iterator it =
852  cells.begin();
853  it != cells.end();
854  ++it)
855  {
856  if ((*it)->is_artificial())
857  return true;
858  }
859  return false;
860  }
861  } // namespace
862 
863 
864 
865  template <class MeshType>
866  std::vector<typename MeshType::active_cell_iterator>
867  compute_ghost_cell_halo_layer(const MeshType &mesh)
868  {
869  std::function<bool(const typename MeshType::active_cell_iterator &)>
870  predicate = IteratorFilters::LocallyOwnedCell();
871 
872  const std::vector<typename MeshType::active_cell_iterator>
873  active_halo_layer = compute_active_cell_halo_layer(mesh, predicate);
874 
875  // Check that we never return locally owned or artificial cells
876  // What is left should only be the ghost cells
877  Assert(contains_locally_owned_cells<MeshType>(active_halo_layer) == false,
878  ExcMessage("Halo layer contains locally owned cells"));
879  Assert(contains_artificial_cells<MeshType>(active_halo_layer) == false,
880  ExcMessage("Halo layer contains artificial cells"));
881 
882  return active_halo_layer;
883  }
884 
885 
886 
887  template <class MeshType>
888  std::vector<typename MeshType::active_cell_iterator>
890  const MeshType &mesh,
891  const std::function<bool(const typename MeshType::active_cell_iterator &)>
892  & predicate,
893  const double layer_thickness)
894  {
895  std::vector<typename MeshType::active_cell_iterator>
896  subdomain_boundary_cells, active_cell_layer_within_distance;
897  std::vector<bool> vertices_outside_subdomain(
898  mesh.get_triangulation().n_vertices(), false);
899 
900  const unsigned int spacedim = MeshType::space_dimension;
901 
902  unsigned int n_non_predicate_cells = 0; // Number of non predicate cells
903 
904  // Find the layer of cells for which predicate is true and that
905  // are on the boundary with other cells. These are
906  // subdomain boundary cells.
907 
908  // Find the cells for which the predicate is false
909  // These are the cells which are around the predicate subdomain
910  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
911  cell != mesh.end();
912  ++cell)
913  if (!predicate(cell)) // Negation of predicate --> Not Part of subdomain
914  {
915  for (unsigned int v = 0;
916  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
917  ++v)
918  vertices_outside_subdomain[cell->vertex_index(v)] = true;
919  n_non_predicate_cells++;
920  }
921 
922  // If all the active cells conform to the predicate
923  // or if none of the active cells conform to the predicate
924  // there is no active cell layer around the predicate
925  // subdomain (within any distance)
926  if (n_non_predicate_cells == 0 ||
927  n_non_predicate_cells == mesh.get_triangulation().n_active_cells())
928  return std::vector<typename MeshType::active_cell_iterator>();
929 
930  // Find the cells that conform to the predicate
931  // but share a vertex with the cell not in the predicate subdomain
932  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
933  cell != mesh.end();
934  ++cell)
935  if (predicate(cell)) // True predicate --> Potential boundary cell of the
936  // subdomain
937  for (unsigned int v = 0;
938  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
939  ++v)
940  if (vertices_outside_subdomain[cell->vertex_index(v)] == true)
941  {
942  subdomain_boundary_cells.push_back(cell);
943  break; // No need to go through remaining vertices
944  }
945 
946  // To cheaply filter out some cells located far away from the predicate
947  // subdomain, get the bounding box of the predicate subdomain.
948  std::pair<Point<spacedim>, Point<spacedim>> bounding_box =
949  compute_bounding_box(mesh, predicate);
950 
951  // DOUBLE_EPSILON to compare really close double values
952  const double DOUBLE_EPSILON = 100. * std::numeric_limits<double>::epsilon();
953 
954  // Add layer_thickness to the bounding box
955  for (unsigned int d = 0; d < spacedim; ++d)
956  {
957  bounding_box.first[d] -= (layer_thickness + DOUBLE_EPSILON); // minp
958  bounding_box.second[d] += (layer_thickness + DOUBLE_EPSILON); // maxp
959  }
960 
961  std::vector<Point<spacedim>>
962  subdomain_boundary_cells_centers; // cache all the subdomain boundary
963  // cells centers here
964  std::vector<double>
965  subdomain_boundary_cells_radii; // cache all the subdomain boundary cells
966  // radii
967  subdomain_boundary_cells_centers.reserve(subdomain_boundary_cells.size());
968  subdomain_boundary_cells_radii.reserve(subdomain_boundary_cells.size());
969  // compute cell radius for each boundary cell of the predicate subdomain
970  for (typename std::vector<typename MeshType::active_cell_iterator>::
971  const_iterator subdomain_boundary_cell_iterator =
972  subdomain_boundary_cells.begin();
973  subdomain_boundary_cell_iterator != subdomain_boundary_cells.end();
974  ++subdomain_boundary_cell_iterator)
975  {
976  const std::pair<Point<spacedim>, double>
977  &subdomain_boundary_cell_enclosing_ball =
978  (*subdomain_boundary_cell_iterator)->enclosing_ball();
979 
980  subdomain_boundary_cells_centers.push_back(
981  subdomain_boundary_cell_enclosing_ball.first);
982  subdomain_boundary_cells_radii.push_back(
983  subdomain_boundary_cell_enclosing_ball.second);
984  }
985  AssertThrow(subdomain_boundary_cells_radii.size() ==
986  subdomain_boundary_cells_centers.size(),
987  ExcInternalError());
988 
989  // Find the cells that are within layer_thickness of predicate subdomain
990  // boundary distance but are inside the extended bounding box. Most cells
991  // might be outside the extended bounding box, so we could skip them. Those
992  // cells that are inside the extended bounding box but are not part of the
993  // predicate subdomain are possible candidates to be within the distance to
994  // the boundary cells of the predicate subdomain.
995  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
996  cell != mesh.end();
997  ++cell)
998  {
999  // Ignore all the cells that are in the predicate subdomain
1000  if (predicate(cell))
1001  continue;
1002 
1003  const std::pair<Point<spacedim>, double> &cell_enclosing_ball =
1004  cell->enclosing_ball();
1005 
1006  const Point<spacedim> cell_enclosing_ball_center =
1007  cell_enclosing_ball.first;
1008  const double cell_enclosing_ball_radius = cell_enclosing_ball.second;
1009 
1010  bool cell_inside = true; // reset for each cell
1011 
1012  for (unsigned int d = 0; d < spacedim; ++d)
1013  cell_inside &=
1014  (cell_enclosing_ball_center[d] + cell_enclosing_ball_radius >
1015  bounding_box.first[d]) &&
1016  (cell_enclosing_ball_center[d] - cell_enclosing_ball_radius <
1017  bounding_box.second[d]);
1018  // cell_inside is true if its enclosing ball intersects the extended
1019  // bounding box
1020 
1021  // Ignore all the cells that are outside the extended bounding box
1022  if (cell_inside)
1023  for (unsigned int i = 0; i < subdomain_boundary_cells_radii.size();
1024  ++i)
1025  if (cell_enclosing_ball_center.distance_square(
1026  subdomain_boundary_cells_centers[i]) <
1027  Utilities::fixed_power<2>(cell_enclosing_ball_radius +
1028  subdomain_boundary_cells_radii[i] +
1029  layer_thickness + DOUBLE_EPSILON))
1030  {
1031  active_cell_layer_within_distance.push_back(cell);
1032  break; // Exit the loop checking all the remaining subdomain
1033  // boundary cells
1034  }
1035  }
1036  return active_cell_layer_within_distance;
1037  }
1038 
1039 
1040 
1041  template <class MeshType>
1042  std::vector<typename MeshType::active_cell_iterator>
1044  const double layer_thickness)
1045  {
1046  IteratorFilters::LocallyOwnedCell locally_owned_cell_predicate;
1047  std::function<bool(const typename MeshType::active_cell_iterator &)>
1048  predicate(locally_owned_cell_predicate);
1049 
1050  const std::vector<typename MeshType::active_cell_iterator>
1051  ghost_cell_layer_within_distance =
1053  predicate,
1054  layer_thickness);
1055 
1056  // Check that we never return locally owned or artificial cells
1057  // What is left should only be the ghost cells
1058  Assert(
1059  contains_locally_owned_cells<MeshType>(
1060  ghost_cell_layer_within_distance) == false,
1061  ExcMessage(
1062  "Ghost cells within layer_thickness contains locally owned cells."));
1063  Assert(
1064  contains_artificial_cells<MeshType>(ghost_cell_layer_within_distance) ==
1065  false,
1066  ExcMessage(
1067  "Ghost cells within layer_thickness contains artificial cells."
1068  "The function compute_ghost_cell_layer_within_distance "
1069  "is probably called while using parallel::distributed::Triangulation. "
1070  "In such case please refer to the description of this function."));
1071 
1072  return ghost_cell_layer_within_distance;
1073  }
1074 
1075 
1076 
1077  template <class MeshType>
1078  std::pair<Point<MeshType::space_dimension>, Point<MeshType::space_dimension>>
1080  const MeshType &mesh,
1081  const std::function<bool(const typename MeshType::active_cell_iterator &)>
1082  &predicate)
1083  {
1084  std::vector<bool> locally_active_vertices_on_subdomain(
1085  mesh.get_triangulation().n_vertices(), false);
1086 
1087  const unsigned int spacedim = MeshType::space_dimension;
1088 
1089  // Two extreme points can define the bounding box
1090  // around the active cells that conform to the given predicate.
1092 
1093  // initialize minp and maxp with the first predicate cell center
1094  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
1095  cell != mesh.end();
1096  ++cell)
1097  if (predicate(cell))
1098  {
1099  minp = cell->center();
1100  maxp = cell->center();
1101  break;
1102  }
1103 
1104  // Run through all the cells to check if it belongs to predicate domain,
1105  // if it belongs to the predicate domain, extend the bounding box.
1106  for (typename MeshType::active_cell_iterator cell = mesh.begin_active();
1107  cell != mesh.end();
1108  ++cell)
1109  if (predicate(cell)) // True predicate --> Part of subdomain
1110  for (unsigned int v = 0;
1111  v < GeometryInfo<MeshType::dimension>::vertices_per_cell;
1112  ++v)
1113  if (locally_active_vertices_on_subdomain[cell->vertex_index(v)] ==
1114  false)
1115  {
1116  locally_active_vertices_on_subdomain[cell->vertex_index(v)] =
1117  true;
1118  for (unsigned int d = 0; d < spacedim; ++d)
1119  {
1120  minp[d] = std::min(minp[d], cell->vertex(v)[d]);
1121  maxp[d] = std::max(maxp[d], cell->vertex(v)[d]);
1122  }
1123  }
1124 
1125  return std::make_pair(minp, maxp);
1126  }
1127 
1128 
1129 
1130  template <typename MeshType>
1131  std::list<std::pair<typename MeshType::cell_iterator,
1132  typename MeshType::cell_iterator>>
1133  get_finest_common_cells(const MeshType &mesh_1, const MeshType &mesh_2)
1134  {
1135  Assert(have_same_coarse_mesh(mesh_1, mesh_2),
1136  ExcMessage("The two meshes must be represent triangulations that "
1137  "have the same coarse meshes"));
1138  // We will allow the output to contain ghost cells when we have shared
1139  // Triangulations (i.e., so that each processor will get exactly the same
1140  // list of cell pairs), but not when we have two distributed
1141  // Triangulations (so that all active cells are partitioned by processor).
1142  // Non-parallel Triangulations have no ghost or artificial cells, so they
1143  // work the same way as shared Triangulations here.
1144  bool remove_ghost_cells = false;
1145 #ifdef DEAL_II_WITH_MPI
1146  {
1147  constexpr int dim = MeshType::dimension;
1148  constexpr int spacedim = MeshType::space_dimension;
1150  *>(&mesh_1.get_triangulation()) != nullptr ||
1152  *>(&mesh_2.get_triangulation()) != nullptr)
1153  {
1154  Assert(&mesh_1.get_triangulation() == &mesh_2.get_triangulation(),
1155  ExcMessage("This function can only be used with meshes "
1156  "corresponding to distributed Triangulations when "
1157  "both Triangulations are equal."));
1158  remove_ghost_cells = true;
1159  }
1160  }
1161 #endif
1162 
1163  // the algorithm goes as follows: first, we fill a list with pairs of
1164  // iterators common to the two meshes on the coarsest level. then we
1165  // traverse the list; each time, we find a pair of iterators for which
1166  // both correspond to non-active cells, we delete this item and push the
1167  // pairs of iterators to their children to the back. if these again both
1168  // correspond to non-active cells, we will get to the later on for further
1169  // consideration
1170  using CellList = std::list<std::pair<typename MeshType::cell_iterator,
1171  typename MeshType::cell_iterator>>;
1172  CellList cell_list;
1173 
1174  // first push the coarse level cells
1175  typename MeshType::cell_iterator cell_1 = mesh_1.begin(0),
1176  cell_2 = mesh_2.begin(0);
1177  for (; cell_1 != mesh_1.end(0); ++cell_1, ++cell_2)
1178  cell_list.emplace_back(cell_1, cell_2);
1179 
1180  // then traverse list as described above
1181  typename CellList::iterator cell_pair = cell_list.begin();
1182  while (cell_pair != cell_list.end())
1183  {
1184  // if both cells in this pair have children, then erase this element
1185  // and push their children instead
1186  if (cell_pair->first->has_children() &&
1187  cell_pair->second->has_children())
1188  {
1189  Assert(cell_pair->first->refinement_case() ==
1190  cell_pair->second->refinement_case(),
1191  ExcNotImplemented());
1192  for (unsigned int c = 0; c < cell_pair->first->n_children(); ++c)
1193  cell_list.emplace_back(cell_pair->first->child(c),
1194  cell_pair->second->child(c));
1195 
1196  // erasing an iterator keeps other iterators valid, so already
1197  // advance the present iterator by one and then delete the element
1198  // we've visited before
1199  const auto previous_cell_pair = cell_pair;
1200  ++cell_pair;
1201  cell_list.erase(previous_cell_pair);
1202  }
1203  else
1204  {
1205  // at least one cell is active
1206  if (remove_ghost_cells &&
1207  ((cell_pair->first->active() &&
1208  !cell_pair->first->is_locally_owned()) ||
1209  (cell_pair->second->active() &&
1210  !cell_pair->second->is_locally_owned())))
1211  {
1212  // we only exclude ghost cells for distributed Triangulations
1213  const auto previous_cell_pair = cell_pair;
1214  ++cell_pair;
1215  cell_list.erase(previous_cell_pair);
1216  }
1217  else
1218  ++cell_pair;
1219  }
1220  }
1221 
1222  // just to make sure everything is ok, validate that all pairs have at
1223  // least one active iterator or have different refinement_cases
1224  for (cell_pair = cell_list.begin(); cell_pair != cell_list.end();
1225  ++cell_pair)
1226  Assert(cell_pair->first->active() || cell_pair->second->active() ||
1227  (cell_pair->first->refinement_case() !=
1228  cell_pair->second->refinement_case()),
1229  ExcInternalError());
1230 
1231  return cell_list;
1232  }
1233 
1234 
1235 
1236  template <int dim, int spacedim>
1237  bool
1239  const Triangulation<dim, spacedim> &mesh_2)
1240  {
1241  // make sure the two meshes have
1242  // the same number of coarse cells
1243  if (mesh_1.n_cells(0) != mesh_2.n_cells(0))
1244  return false;
1245 
1246  // if so, also make sure they have
1247  // the same vertices on the cells
1248  // of the coarse mesh
1250  mesh_1.begin(0),
1251  cell_2 =
1252  mesh_2.begin(0),
1253  endc = mesh_1.end(0);
1254  for (; cell_1 != endc; ++cell_1, ++cell_2)
1255  for (unsigned int v = 0; v < GeometryInfo<dim>::vertices_per_cell; ++v)
1256  if (cell_1->vertex(v) != cell_2->vertex(v))
1257  return false;
1258 
1259  // if we've gotten through all
1260  // this, then the meshes really
1261  // seem to have a common coarse
1262  // mesh
1263  return true;
1264  }
1265 
1266 
1267 
1268  template <typename MeshType>
1269  bool
1270  have_same_coarse_mesh(const MeshType &mesh_1, const MeshType &mesh_2)
1271  {
1272  return have_same_coarse_mesh(mesh_1.get_triangulation(),
1273  mesh_2.get_triangulation());
1274  }
1275 
1276 
1277 
1278  template <int dim, int spacedim>
1279  std::pair<typename hp::DoFHandler<dim, spacedim>::active_cell_iterator,
1280  Point<dim>>
1282  const hp::MappingCollection<dim, spacedim> &mapping,
1283  const hp::DoFHandler<dim, spacedim> & mesh,
1284  const Point<spacedim> & p)
1285  {
1286  Assert((mapping.size() == 1) ||
1287  (mapping.size() == mesh.get_fe_collection().size()),
1288  ExcMessage("Mapping collection needs to have either size 1 "
1289  "or size equal to the number of elements in "
1290  "the FECollection."));
1291 
1292  using cell_iterator =
1294 
1295  std::pair<cell_iterator, Point<dim>> best_cell;
1296  // If we have only one element in the MappingCollection,
1297  // we use find_active_cell_around_point using only one
1298  // mapping.
1299  if (mapping.size() == 1)
1300  best_cell = find_active_cell_around_point(mapping[0], mesh, p);
1301  else
1302  {
1303  // The best distance is set to the
1304  // maximum allowable distance from
1305  // the unit cell; we assume a
1306  // max. deviation of 1e-10
1307  double best_distance = 1e-10;
1308  int best_level = -1;
1309 
1310 
1311  // Find closest vertex and determine
1312  // all adjacent cells
1313  unsigned int vertex = find_closest_vertex(mesh, p);
1314 
1315  std::vector<cell_iterator> adjacent_cells_tmp =
1316  find_cells_adjacent_to_vertex(mesh, vertex);
1317 
1318  // Make sure that we have found
1319  // at least one cell adjacent to vertex.
1320  Assert(adjacent_cells_tmp.size() > 0, ExcInternalError());
1321 
1322  // Copy all the cells into a std::set
1323  std::set<cell_iterator> adjacent_cells(adjacent_cells_tmp.begin(),
1324  adjacent_cells_tmp.end());
1325  std::set<cell_iterator> searched_cells;
1326 
1327  // Determine the maximal number of cells
1328  // in the grid.
1329  // As long as we have not found
1330  // the cell and have not searched
1331  // every cell in the triangulation,
1332  // we keep on looking.
1333  const unsigned int n_cells = mesh.get_triangulation().n_cells();
1334  bool found = false;
1335  unsigned int cells_searched = 0;
1336  while (!found && cells_searched < n_cells)
1337  {
1338  typename std::set<cell_iterator>::const_iterator
1339  cell = adjacent_cells.begin(),
1340  endc = adjacent_cells.end();
1341  for (; cell != endc; ++cell)
1342  {
1343  try
1344  {
1345  const Point<dim> p_cell =
1346  mapping[(*cell)->active_fe_index()]
1347  .transform_real_to_unit_cell(*cell, p);
1348 
1349 
1350  // calculate the infinity norm of
1351  // the distance vector to the unit cell.
1352  const double dist =
1354 
1355  // We compare if the point is inside the
1356  // unit cell (or at least not too far
1357  // outside). If it is, it is also checked
1358  // that the cell has a more refined state
1359  if (dist < best_distance || (dist == best_distance &&
1360  (*cell)->level() > best_level))
1361  {
1362  found = true;
1363  best_distance = dist;
1364  best_level = (*cell)->level();
1365  best_cell = std::make_pair(*cell, p_cell);
1366  }
1367  }
1368  catch (
1369  typename MappingQGeneric<dim,
1370  spacedim>::ExcTransformationFailed &)
1371  {
1372  // ok, the transformation
1373  // failed presumably
1374  // because the point we
1375  // are looking for lies
1376  // outside the current
1377  // cell. this means that
1378  // the current cell can't
1379  // be the cell around the
1380  // point, so just ignore
1381  // this cell and move on
1382  // to the next
1383  }
1384  }
1385  // update the number of cells searched
1386  cells_searched += adjacent_cells.size();
1387  // if we have not found the cell in
1388  // question and have not yet searched every
1389  // cell, we expand our search to
1390  // all the not already searched neighbors of
1391  // the cells in adjacent_cells.
1392  if (!found && cells_searched < n_cells)
1393  {
1394  find_active_cell_around_point_internal<dim,
1396  spacedim>(
1397  mesh, searched_cells, adjacent_cells);
1398  }
1399  }
1400  }
1401 
1402  AssertThrow(best_cell.first.state() == IteratorState::valid,
1403  ExcPointNotFound<spacedim>(p));
1404 
1405  return best_cell;
1406  }
1407 
1408 
1409  template <class MeshType>
1410  std::vector<typename MeshType::active_cell_iterator>
1411  get_patch_around_cell(const typename MeshType::active_cell_iterator &cell)
1412  {
1413  Assert(cell->is_locally_owned(),
1414  ExcMessage("This function only makes sense if the cell for "
1415  "which you are asking for a patch, is locally "
1416  "owned."));
1417 
1418  std::vector<typename MeshType::active_cell_iterator> patch;
1419  patch.push_back(cell);
1420  for (unsigned int face_number = 0;
1421  face_number < GeometryInfo<MeshType::dimension>::faces_per_cell;
1422  ++face_number)
1423  if (cell->face(face_number)->at_boundary() == false)
1424  {
1425  if (cell->neighbor(face_number)->has_children() == false)
1426  patch.push_back(cell->neighbor(face_number));
1427  else
1428  // the neighbor is refined. in 2d/3d, we can simply ask for the
1429  // children of the neighbor because they can not be further refined
1430  // and, consequently, the children is active
1431  if (MeshType::dimension > 1)
1432  {
1433  for (unsigned int subface = 0;
1434  subface < cell->face(face_number)->n_children();
1435  ++subface)
1436  patch.push_back(
1437  cell->neighbor_child_on_subface(face_number, subface));
1438  }
1439  else
1440  {
1441  // in 1d, we need to work a bit harder: iterate until we find
1442  // the child by going from cell to child to child etc
1443  typename MeshType::cell_iterator neighbor =
1444  cell->neighbor(face_number);
1445  while (neighbor->has_children())
1446  neighbor = neighbor->child(1 - face_number);
1447 
1448  Assert(neighbor->neighbor(1 - face_number) == cell,
1449  ExcInternalError());
1450  patch.push_back(neighbor);
1451  }
1452  }
1453  return patch;
1454  }
1455 
1456 
1457 
1458  template <class Container>
1459  std::vector<typename Container::cell_iterator>
1461  const std::vector<typename Container::active_cell_iterator> &patch)
1462  {
1463  Assert(patch.size() > 0,
1464  ExcMessage(
1465  "Vector containing patch cells should not be an empty vector!"));
1466  // In order to extract the set of cells with the coarsest common level from
1467  // the give vector of cells: First it finds the number associated with the
1468  // minimum level of refinmenet, namely "min_level"
1469  int min_level = patch[0]->level();
1470 
1471  for (unsigned int i = 0; i < patch.size(); ++i)
1472  min_level = std::min(min_level, patch[i]->level());
1473  std::set<typename Container::cell_iterator> uniform_cells;
1474  typename std::vector<
1475  typename Container::active_cell_iterator>::const_iterator patch_cell;
1476  // it loops through all cells of the input vector
1477  for (patch_cell = patch.begin(); patch_cell != patch.end(); ++patch_cell)
1478  {
1479  // If the refinement level of each cell i the loop be equal to the
1480  // min_level, so that that cell inserted into the set of uniform_cells,
1481  // as the set of cells with the coarsest common refinement level
1482  if ((*patch_cell)->level() == min_level)
1483  uniform_cells.insert(*patch_cell);
1484  else
1485  // If not, it asks for the parent of the cell, until it finds the
1486  // parent cell with the refinement level equal to the min_level and
1487  // inserts that parent cell into the the set of uniform_cells, as the
1488  // set of cells with the coarsest common refinement level.
1489  {
1490  typename Container::cell_iterator parent = *patch_cell;
1491 
1492  while (parent->level() > min_level)
1493  parent = parent->parent();
1494  uniform_cells.insert(parent);
1495  }
1496  }
1497 
1498  return std::vector<typename Container::cell_iterator>(uniform_cells.begin(),
1499  uniform_cells.end());
1500  }
1501 
1502 
1503 
1504  template <class Container>
1505  void
1507  const std::vector<typename Container::active_cell_iterator> &patch,
1509  &local_triangulation,
1510  std::map<
1511  typename Triangulation<Container::dimension,
1512  Container::space_dimension>::active_cell_iterator,
1513  typename Container::active_cell_iterator> &patch_to_global_tria_map)
1514 
1515  {
1516  const std::vector<typename Container::cell_iterator> uniform_cells =
1517  get_cells_at_coarsest_common_level<Container>(patch);
1518  // First it creates triangulation from the vector of "uniform_cells"
1519  local_triangulation.clear();
1520  std::vector<Point<Container::space_dimension>> vertices;
1521  const unsigned int n_uniform_cells = uniform_cells.size();
1522  std::vector<CellData<Container::dimension>> cells(n_uniform_cells);
1523  unsigned int k = 0; // for enumerating cells
1524  unsigned int i = 0; // for enumerating vertices
1525  typename std::vector<typename Container::cell_iterator>::const_iterator
1526  uniform_cell;
1527  for (uniform_cell = uniform_cells.begin();
1528  uniform_cell != uniform_cells.end();
1529  ++uniform_cell)
1530  {
1531  for (unsigned int v = 0;
1532  v < GeometryInfo<Container::dimension>::vertices_per_cell;
1533  ++v)
1534  {
1536  (*uniform_cell)->vertex(v);
1537  bool repeat_vertex = false;
1538 
1539  for (unsigned int m = 0; m < i; ++m)
1540  {
1541  if (position == vertices[m])
1542  {
1543  repeat_vertex = true;
1544  cells[k].vertices[v] = m;
1545  break;
1546  }
1547  }
1548  if (repeat_vertex == false)
1549  {
1550  vertices.push_back(position);
1551  cells[k].vertices[v] = i;
1552  i = i + 1;
1553  }
1554 
1555  } // for vertices_per_cell
1556  k = k + 1;
1557  }
1558  local_triangulation.create_triangulation(vertices, cells, SubCellData());
1559  Assert(local_triangulation.n_active_cells() == uniform_cells.size(),
1560  ExcInternalError());
1561  local_triangulation.clear_user_flags();
1562  unsigned int index = 0;
1563  // Create a map between cells of class DoFHandler into class Triangulation
1564  std::map<typename Triangulation<Container::dimension,
1565  Container::space_dimension>::cell_iterator,
1566  typename Container::cell_iterator>
1567  patch_to_global_tria_map_tmp;
1568  for (typename Triangulation<Container::dimension,
1569  Container::space_dimension>::cell_iterator
1570  coarse_cell = local_triangulation.begin();
1571  coarse_cell != local_triangulation.end();
1572  ++coarse_cell, ++index)
1573  {
1574  patch_to_global_tria_map_tmp.insert(
1575  std::make_pair(coarse_cell, uniform_cells[index]));
1576  // To ensure that the cells with the same coordinates (here, we compare
1577  // their centers) are mapped into each other.
1578 
1579  Assert(coarse_cell->center().distance(uniform_cells[index]->center()) <=
1580  1e-15 * coarse_cell->diameter(),
1581  ExcInternalError());
1582  }
1583  bool refinement_necessary;
1584  // In this loop we start to do refinement on the above coarse triangulation
1585  // to reach to the same level of refinement as the patch cells are really on
1586  do
1587  {
1588  refinement_necessary = false;
1589  for (typename Triangulation<Container::dimension,
1590  Container::space_dimension>::
1591  active_cell_iterator active_tria_cell =
1592  local_triangulation.begin_active();
1593  active_tria_cell != local_triangulation.end();
1594  ++active_tria_cell)
1595  {
1596  if (patch_to_global_tria_map_tmp[active_tria_cell]->has_children())
1597  {
1598  active_tria_cell->set_refine_flag();
1599  refinement_necessary = true;
1600  }
1601  else
1602  for (unsigned int i = 0; i < patch.size(); ++i)
1603  {
1604  // Even though vertices may not be exactly the same, the
1605  // appropriate cells will match since == for TriAccessors
1606  // checks only cell level and index.
1607  if (patch_to_global_tria_map_tmp[active_tria_cell] ==
1608  patch[i])
1609  {
1610  // adjust the cell vertices of the local_triangulation to
1611  // match cell vertices of the global triangulation
1612  for (unsigned int v = 0;
1613  v < GeometryInfo<
1614  Container::dimension>::vertices_per_cell;
1615  ++v)
1616  active_tria_cell->vertex(v) = patch[i]->vertex(v);
1617 
1618  Assert(active_tria_cell->center().distance(
1619  patch_to_global_tria_map_tmp[active_tria_cell]
1620  ->center()) <=
1621  1e-15 * active_tria_cell->diameter(),
1622  ExcInternalError());
1623 
1624  active_tria_cell->set_user_flag();
1625  break;
1626  }
1627  }
1628  }
1629 
1630  if (refinement_necessary)
1631  {
1632  local_triangulation.execute_coarsening_and_refinement();
1633 
1634  for (typename Triangulation<
1635  Container::dimension,
1636  Container::space_dimension>::cell_iterator cell =
1637  local_triangulation.begin();
1638  cell != local_triangulation.end();
1639  ++cell)
1640  {
1641  if (patch_to_global_tria_map_tmp.find(cell) !=
1642  patch_to_global_tria_map_tmp.end())
1643  {
1644  if (cell->has_children())
1645  {
1646  // Note: Since the cell got children, then it should not
1647  // be in the map anymore children may be added into the
1648  // map, instead
1649 
1650  // these children may not yet be in the map
1651  for (unsigned int c = 0; c < cell->n_children(); ++c)
1652  {
1653  if (patch_to_global_tria_map_tmp.find(cell->child(
1654  c)) == patch_to_global_tria_map_tmp.end())
1655  {
1656  patch_to_global_tria_map_tmp.insert(
1657  std::make_pair(
1658  cell->child(c),
1659  patch_to_global_tria_map_tmp[cell]->child(
1660  c)));
1661 
1662  // One might be tempted to assert that the cell
1663  // being added here has the same center as the
1664  // equivalent cell in the global triangulation,
1665  // but it may not be the case. For
1666  // triangulations that have been perturbed or
1667  // smoothed, the cell indices and levels may be
1668  // the same, but the vertex locations may not.
1669  // We adjust the vertices of the cells that have
1670  // no children (ie the active cells) to be
1671  // consistent with the global triangulation
1672  // later on and add assertions at that time
1673  // to guarantee the cells in the
1674  // local_triangulation are physically at the
1675  // same locations of the cells in the patch of
1676  // the global triangulation.
1677  }
1678  }
1679  // The parent cell whose children were added
1680  // into the map should be deleted from the map
1681  patch_to_global_tria_map_tmp.erase(cell);
1682  }
1683  }
1684  }
1685  }
1686  }
1687  while (refinement_necessary);
1688 
1689 
1690  // Last assertion check to make sure we have the right cells and centers
1691  // in the map, and hence the correct vertices of the triangulation
1692  for (typename Triangulation<Container::dimension,
1693  Container::space_dimension>::cell_iterator
1694  cell = local_triangulation.begin();
1695  cell != local_triangulation.end();
1696  ++cell)
1697  {
1698  if (cell->user_flag_set())
1699  {
1700  Assert(patch_to_global_tria_map_tmp.find(cell) !=
1701  patch_to_global_tria_map_tmp.end(),
1702  ExcInternalError());
1703 
1704  Assert(cell->center().distance(
1705  patch_to_global_tria_map_tmp[cell]->center()) <=
1706  1e-15 * cell->diameter(),
1707  ExcInternalError());
1708  }
1709  }
1710 
1711 
1712  typename std::map<
1713  typename Triangulation<Container::dimension,
1714  Container::space_dimension>::cell_iterator,
1715  typename Container::cell_iterator>::iterator
1716  map_tmp_it = patch_to_global_tria_map_tmp.begin(),
1717  map_tmp_end = patch_to_global_tria_map_tmp.end();
1718  // Now we just need to take the temporary map of pairs of type cell_iterator
1719  // "patch_to_global_tria_map_tmp" making pair of active_cell_iterators so
1720  // that filling out the final map "patch_to_global_tria_map"
1721  for (; map_tmp_it != map_tmp_end; ++map_tmp_it)
1722  patch_to_global_tria_map[map_tmp_it->first] = map_tmp_it->second;
1723  }
1724 
1725 
1726 
1727  template <class DoFHandlerType>
1728  std::map<types::global_dof_index,
1729  std::vector<typename DoFHandlerType::active_cell_iterator>>
1730  get_dof_to_support_patch_map(DoFHandlerType &dof_handler)
1731  {
1732  // This is the map from global_dof_index to
1733  // a set of cells on patch. We first map into
1734  // a set because it is very likely that we
1735  // will attempt to add a cell more than once
1736  // to a particular patch and we want to preserve
1737  // uniqueness of cell iterators. std::set does this
1738  // automatically for us. Later after it is all
1739  // constructed, we will copy to a map of vectors
1740  // since that is the preferred output for other
1741  // functions.
1742  std::map<types::global_dof_index,
1743  std::set<typename DoFHandlerType::active_cell_iterator>>
1744  dof_to_set_of_cells_map;
1745 
1746  std::vector<types::global_dof_index> local_dof_indices;
1747  std::vector<types::global_dof_index> local_face_dof_indices;
1748  std::vector<types::global_dof_index> local_line_dof_indices;
1749 
1750  // a place to save the dof_handler user flags and restore them later
1751  // to maintain const of dof_handler.
1752  std::vector<bool> user_flags;
1753 
1754 
1755  // in 3d, we need pointers from active lines to the
1756  // active parent lines, so we construct it as needed.
1757  std::map<typename DoFHandlerType::active_line_iterator,
1758  typename DoFHandlerType::line_iterator>
1759  lines_to_parent_lines_map;
1760  if (DoFHandlerType::dimension == 3)
1761  {
1762  // save user flags as they will be modified and then later restored
1763  dof_handler.get_triangulation().save_user_flags(user_flags);
1764  const_cast<::Triangulation<DoFHandlerType::dimension,
1765  DoFHandlerType::space_dimension> &>(
1766  dof_handler.get_triangulation())
1767  .clear_user_flags();
1768 
1769 
1770  typename DoFHandlerType::active_cell_iterator cell = dof_handler
1771  .begin_active(),
1772  endc = dof_handler.end();
1773  for (; cell != endc; ++cell)
1774  {
1775  // We only want lines that are locally_relevant
1776  // although it doesn't hurt to have lines that
1777  // are children of ghost cells since there are
1778  // few and we don't have to use them.
1779  if (cell->is_artificial() == false)
1780  {
1781  for (unsigned int l = 0;
1782  l <
1784  ++l)
1785  if (cell->line(l)->has_children())
1786  for (unsigned int c = 0; c < cell->line(l)->n_children();
1787  ++c)
1788  {
1789  lines_to_parent_lines_map[cell->line(l)->child(c)] =
1790  cell->line(l);
1791  // set flags to know that child
1792  // line has an active parent.
1793  cell->line(l)->child(c)->set_user_flag();
1794  }
1795  }
1796  }
1797  }
1798 
1799 
1800  // We loop through all cells and add cell to the
1801  // map for the dofs that it immediately touches
1802  // and then account for all the other dofs of
1803  // which it is a part, mainly the ones that must
1804  // be added on account of adaptivity hanging node
1805  // constraints.
1806  typename DoFHandlerType::active_cell_iterator cell =
1807  dof_handler.begin_active(),
1808  endc = dof_handler.end();
1809  for (; cell != endc; ++cell)
1810  {
1811  // Need to loop through all cells that could
1812  // be in the patch of dofs on locally_owned
1813  // cells including ghost cells
1814  if (cell->is_artificial() == false)
1815  {
1816  const unsigned int n_dofs_per_cell = cell->get_fe().dofs_per_cell;
1817  local_dof_indices.resize(n_dofs_per_cell);
1818 
1819  // Take care of adding cell pointer to each
1820  // dofs that exists on cell.
1821  cell->get_dof_indices(local_dof_indices);
1822  for (unsigned int i = 0; i < n_dofs_per_cell; ++i)
1823  dof_to_set_of_cells_map[local_dof_indices[i]].insert(cell);
1824 
1825  // In the case of the adjacent cell (over
1826  // faces or edges) being more refined, we
1827  // want to add all of the children to the
1828  // patch since the support function at that
1829  // dof could be non-zero along that entire
1830  // face (or line).
1831 
1832  // Take care of dofs on neighbor faces
1833  for (unsigned int f = 0;
1834  f < GeometryInfo<DoFHandlerType::dimension>::faces_per_cell;
1835  ++f)
1836  {
1837  if (cell->face(f)->has_children())
1838  {
1839  for (unsigned int c = 0; c < cell->face(f)->n_children();
1840  ++c)
1841  {
1842  // Add cell to dofs of all subfaces
1843  //
1844  // *-------------------*----------*---------*
1845  // | | add cell | |
1846  // | |<- to dofs| |
1847  // | |of subface| |
1848  // | cell *----------*---------*
1849  // | | add cell | |
1850  // | |<- to dofs| |
1851  // | |of subface| |
1852  // *-------------------*----------*---------*
1853  //
1854  Assert(cell->face(f)->child(c)->has_children() == false,
1855  ExcInternalError());
1856 
1857  const unsigned int n_dofs_per_face =
1858  cell->get_fe().dofs_per_face;
1859  local_face_dof_indices.resize(n_dofs_per_face);
1860 
1861  cell->face(f)->child(c)->get_dof_indices(
1862  local_face_dof_indices);
1863  for (unsigned int i = 0; i < n_dofs_per_face; ++i)
1864  dof_to_set_of_cells_map[local_face_dof_indices[i]]
1865  .insert(cell);
1866  }
1867  }
1868  else if ((cell->face(f)->at_boundary() == false) &&
1869  (cell->neighbor_is_coarser(f)))
1870  {
1871  // Add cell to dofs of parent face and all
1872  // child faces of parent face
1873  //
1874  // *-------------------*----------*---------*
1875  // | | | |
1876  // | | cell | |
1877  // | add cell | | |
1878  // | to dofs -> *----------*---------*
1879  // | of parent | add cell | |
1880  // | face |<- to dofs| |
1881  // | |of subface| |
1882  // *-------------------*----------*---------*
1883  //
1884 
1885  // Add cell to all dofs of parent face
1886  std::pair<unsigned int, unsigned int>
1887  neighbor_face_no_subface_no =
1888  cell->neighbor_of_coarser_neighbor(f);
1889  unsigned int face_no = neighbor_face_no_subface_no.first;
1890  unsigned int subface = neighbor_face_no_subface_no.second;
1891 
1892  const unsigned int n_dofs_per_face =
1893  cell->get_fe().dofs_per_face;
1894  local_face_dof_indices.resize(n_dofs_per_face);
1895 
1896  cell->neighbor(f)->face(face_no)->get_dof_indices(
1897  local_face_dof_indices);
1898  for (unsigned int i = 0; i < n_dofs_per_face; ++i)
1899  dof_to_set_of_cells_map[local_face_dof_indices[i]].insert(
1900  cell);
1901 
1902  // Add cell to all dofs of children of
1903  // parent face
1904  for (unsigned int c = 0;
1905  c < cell->neighbor(f)->face(face_no)->n_children();
1906  ++c)
1907  {
1908  if (c != subface) // don't repeat work on dofs of
1909  // original cell
1910  {
1911  const unsigned int n_dofs_per_face =
1912  cell->get_fe().dofs_per_face;
1913  local_face_dof_indices.resize(n_dofs_per_face);
1914 
1915  Assert(cell->neighbor(f)
1916  ->face(face_no)
1917  ->child(c)
1918  ->has_children() == false,
1919  ExcInternalError());
1920  cell->neighbor(f)
1921  ->face(face_no)
1922  ->child(c)
1923  ->get_dof_indices(local_face_dof_indices);
1924  for (unsigned int i = 0; i < n_dofs_per_face; ++i)
1925  dof_to_set_of_cells_map[local_face_dof_indices[i]]
1926  .insert(cell);
1927  }
1928  }
1929  }
1930  }
1931 
1932 
1933  // If 3d, take care of dofs on lines in the
1934  // same pattern as faces above. That is, if
1935  // a cell's line has children, distribute
1936  // cell to dofs of children of line, and
1937  // if cell's line has an active parent, then
1938  // distribute cell to dofs on parent line
1939  // and dofs on all children of parent line.
1940  if (DoFHandlerType::dimension == 3)
1941  {
1942  for (unsigned int l = 0;
1943  l <
1945  ++l)
1946  {
1947  if (cell->line(l)->has_children())
1948  {
1949  for (unsigned int c = 0;
1950  c < cell->line(l)->n_children();
1951  ++c)
1952  {
1953  Assert(cell->line(l)->child(c)->has_children() ==
1954  false,
1955  ExcInternalError());
1956 
1957  // dofs_per_line returns number of dofs
1958  // on line not including the vertices of the line.
1959  const unsigned int n_dofs_per_line =
1960  2 * cell->get_fe().dofs_per_vertex +
1961  cell->get_fe().dofs_per_line;
1962  local_line_dof_indices.resize(n_dofs_per_line);
1963 
1964  cell->line(l)->child(c)->get_dof_indices(
1965  local_line_dof_indices);
1966  for (unsigned int i = 0; i < n_dofs_per_line; ++i)
1967  dof_to_set_of_cells_map[local_line_dof_indices[i]]
1968  .insert(cell);
1969  }
1970  }
1971  // user flag was set above to denote that
1972  // an active parent line exists so add
1973  // cell to dofs of parent and all it's
1974  // children
1975  else if (cell->line(l)->user_flag_set() == true)
1976  {
1977  typename DoFHandlerType::line_iterator parent_line =
1978  lines_to_parent_lines_map[cell->line(l)];
1979  Assert(parent_line->has_children(), ExcInternalError());
1980 
1981  // dofs_per_line returns number of dofs
1982  // on line not including the vertices of the line.
1983  const unsigned int n_dofs_per_line =
1984  2 * cell->get_fe().dofs_per_vertex +
1985  cell->get_fe().dofs_per_line;
1986  local_line_dof_indices.resize(n_dofs_per_line);
1987 
1988  parent_line->get_dof_indices(local_line_dof_indices);
1989  for (unsigned int i = 0; i < n_dofs_per_line; ++i)
1990  dof_to_set_of_cells_map[local_line_dof_indices[i]]
1991  .insert(cell);
1992 
1993  for (unsigned int c = 0; c < parent_line->n_children();
1994  ++c)
1995  {
1996  Assert(parent_line->child(c)->has_children() ==
1997  false,
1998  ExcInternalError());
1999 
2000  const unsigned int n_dofs_per_line =
2001  2 * cell->get_fe().dofs_per_vertex +
2002  cell->get_fe().dofs_per_line;
2003  local_line_dof_indices.resize(n_dofs_per_line);
2004 
2005  parent_line->child(c)->get_dof_indices(
2006  local_line_dof_indices);
2007  for (unsigned int i = 0; i < n_dofs_per_line; ++i)
2008  dof_to_set_of_cells_map[local_line_dof_indices[i]]
2009  .insert(cell);
2010  }
2011  }
2012  } // for lines l
2013  } // if DoFHandlerType::dimension == 3
2014  } // if cell->is_locally_owned()
2015  } // for cells
2016 
2017 
2018  if (DoFHandlerType::dimension == 3)
2019  {
2020  // finally, restore user flags that were changed above
2021  // to when we constructed the pointers to parent of lines
2022  // Since dof_handler is const, we must leave it unchanged.
2023  const_cast<::Triangulation<DoFHandlerType::dimension,
2024  DoFHandlerType::space_dimension> &>(
2025  dof_handler.get_triangulation())
2026  .load_user_flags(user_flags);
2027  }
2028 
2029  // Finally, we copy map of sets to
2030  // map of vectors using the std::vector::assign() function
2031  std::map<types::global_dof_index,
2032  std::vector<typename DoFHandlerType::active_cell_iterator>>
2033  dof_to_cell_patches;
2034 
2035  typename std::map<types::global_dof_index,
2036  std::set<typename DoFHandlerType::active_cell_iterator>>::
2037  iterator it = dof_to_set_of_cells_map.begin(),
2038  it_end = dof_to_set_of_cells_map.end();
2039  for (; it != it_end; ++it)
2040  dof_to_cell_patches[it->first].assign(it->second.begin(),
2041  it->second.end());
2042 
2043  return dof_to_cell_patches;
2044  }
2045 
2046  /*
2047  * Internally used in collect_periodic_faces
2048  */
2049  template <typename CellIterator>
2050  void
2051  match_periodic_face_pairs(
2052  std::set<std::pair<CellIterator, unsigned int>> &pairs1,
2053  std::set<std::pair<typename identity<CellIterator>::type, unsigned int>>
2054  & pairs2,
2055  const int direction,
2056  std::vector<PeriodicFacePair<CellIterator>> &matched_pairs,
2057  const ::Tensor<1, CellIterator::AccessorType::space_dimension>
2058  & offset,
2059  const FullMatrix<double> &matrix)
2060  {
2061  static const int space_dim = CellIterator::AccessorType::space_dimension;
2062  (void)space_dim;
2063  Assert(0 <= direction && direction < space_dim,
2064  ExcIndexRange(direction, 0, space_dim));
2065 
2066  Assert(pairs1.size() == pairs2.size(),
2067  ExcMessage("Unmatched faces on periodic boundaries"));
2068 
2069  unsigned int n_matches = 0;
2070 
2071  // Match with a complexity of O(n^2). This could be improved...
2072  std::bitset<3> orientation;
2073  using PairIterator =
2074  typename std::set<std::pair<CellIterator, unsigned int>>::const_iterator;
2075  for (PairIterator it1 = pairs1.begin(); it1 != pairs1.end(); ++it1)
2076  {
2077  for (PairIterator it2 = pairs2.begin(); it2 != pairs2.end(); ++it2)
2078  {
2079  const CellIterator cell1 = it1->first;
2080  const CellIterator cell2 = it2->first;
2081  const unsigned int face_idx1 = it1->second;
2082  const unsigned int face_idx2 = it2->second;
2083  if (GridTools::orthogonal_equality(orientation,
2084  cell1->face(face_idx1),
2085  cell2->face(face_idx2),
2086  direction,
2087  offset,
2088  matrix))
2089  {
2090  // We have a match, so insert the matching pairs and
2091  // remove the matched cell in pairs2 to speed up the
2092  // matching:
2093  const PeriodicFacePair<CellIterator> matched_face = {
2094  {cell1, cell2}, {face_idx1, face_idx2}, orientation, matrix};
2095  matched_pairs.push_back(matched_face);
2096  pairs2.erase(it2);
2097  ++n_matches;
2098  break;
2099  }
2100  }
2101  }
2102 
2103  // Assure that all faces are matched
2104  AssertThrow(n_matches == pairs1.size() && pairs2.size() == 0,
2105  ExcMessage("Unmatched faces on periodic boundaries"));
2106  }
2107 
2108 
2109 
2110  template <typename MeshType>
2111  void
2113  const MeshType & mesh,
2114  const types::boundary_id b_id,
2115  const int direction,
2116  std::vector<PeriodicFacePair<typename MeshType::cell_iterator>>
2117  & matched_pairs,
2119  const FullMatrix<double> & matrix)
2120  {
2121  static const int dim = MeshType::dimension;
2122  static const int space_dim = MeshType::space_dimension;
2123  (void)dim;
2124  (void)space_dim;
2125  Assert(0 <= direction && direction < space_dim,
2126  ExcIndexRange(direction, 0, space_dim));
2127 
2128  Assert(dim == space_dim, ExcNotImplemented());
2129 
2130  // Loop over all cells on the highest level and collect all boundary
2131  // faces 2*direction and 2*direction*1:
2132 
2133  std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs1;
2134  std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs2;
2135 
2136  for (typename MeshType::cell_iterator cell = mesh.begin(0);
2137  cell != mesh.end(0);
2138  ++cell)
2139  {
2140  const typename MeshType::face_iterator face_1 =
2141  cell->face(2 * direction);
2142  const typename MeshType::face_iterator face_2 =
2143  cell->face(2 * direction + 1);
2144 
2145  if (face_1->at_boundary() && face_1->boundary_id() == b_id)
2146  {
2147  const std::pair<typename MeshType::cell_iterator, unsigned int>
2148  pair1 = std::make_pair(cell, 2 * direction);
2149  pairs1.insert(pair1);
2150  }
2151 
2152  if (face_2->at_boundary() && face_2->boundary_id() == b_id)
2153  {
2154  const std::pair<typename MeshType::cell_iterator, unsigned int>
2155  pair2 = std::make_pair(cell, 2 * direction + 1);
2156  pairs2.insert(pair2);
2157  }
2158  }
2159 
2160  Assert(pairs1.size() == pairs2.size(),
2161  ExcMessage("Unmatched faces on periodic boundaries"));
2162 
2163  Assert(pairs1.size() > 0,
2164  ExcMessage("No new periodic face pairs have been found. "
2165  "Are you sure that you've selected the correct boundary "
2166  "id's and that the coarsest level mesh is colorized?"));
2167 
2168 #ifdef DEBUG
2169  const unsigned int size_old = matched_pairs.size();
2170 #endif
2171 
2172  // and call match_periodic_face_pairs that does the actual matching:
2173  match_periodic_face_pairs(
2174  pairs1, pairs2, direction, matched_pairs, offset, matrix);
2175 
2176 #ifdef DEBUG
2177  // check for standard orientation
2178  const unsigned int size_new = matched_pairs.size();
2179  for (unsigned int i = size_old; i < size_new; ++i)
2180  {
2181  Assert(matched_pairs[i].orientation == 1,
2182  ExcMessage(
2183  "Found a face match with non standard orientation. "
2184  "This function is only suitable for meshes with cells "
2185  "in default orientation"));
2186  }
2187 #endif
2188  }
2189 
2190 
2191 
2192  template <typename MeshType>
2193  void
2195  const MeshType & mesh,
2196  const types::boundary_id b_id1,
2197  const types::boundary_id b_id2,
2198  const int direction,
2200  & matched_pairs,
2202  const FullMatrix<double> & matrix)
2203  {
2204  static const int dim = MeshType::dimension;
2205  static const int space_dim = MeshType::space_dimension;
2206  (void)dim;
2207  (void)space_dim;
2208  Assert(0 <= direction && direction < space_dim,
2209  ExcIndexRange(direction, 0, space_dim));
2210 
2211  // Loop over all cells on the highest level and collect all boundary
2212  // faces belonging to b_id1 and b_id2:
2213 
2214  std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs1;
2215  std::set<std::pair<typename MeshType::cell_iterator, unsigned int>> pairs2;
2216 
2217  for (typename MeshType::cell_iterator cell = mesh.begin(0);
2218  cell != mesh.end(0);
2219  ++cell)
2220  {
2221  for (unsigned int i = 0; i < GeometryInfo<dim>::faces_per_cell; ++i)
2222  {
2223  const typename MeshType::face_iterator face = cell->face(i);
2224  if (face->at_boundary() && face->boundary_id() == b_id1)
2225  {
2226  const std::pair<typename MeshType::cell_iterator, unsigned int>
2227  pair1 = std::make_pair(cell, i);
2228  pairs1.insert(pair1);
2229  }
2230 
2231  if (face->at_boundary() && face->boundary_id() == b_id2)
2232  {
2233  const std::pair<typename MeshType::cell_iterator, unsigned int>
2234  pair2 = std::make_pair(cell, i);
2235  pairs2.insert(pair2);
2236  }
2237  }
2238  }
2239 
2240  Assert(pairs1.size() == pairs2.size(),
2241  ExcMessage("Unmatched faces on periodic boundaries"));
2242 
2243  Assert(pairs1.size() > 0,
2244  ExcMessage("No new periodic face pairs have been found. "
2245  "Are you sure that you've selected the correct boundary "
2246  "id's and that the coarsest level mesh is colorized?"));
2247 
2248  // and call match_periodic_face_pairs that does the actual matching:
2249  match_periodic_face_pairs(
2250  pairs1, pairs2, direction, matched_pairs, offset, matrix);
2251  }
2252 
2253 
2254 
2255  /*
2256  * Internally used in orthogonal_equality
2257  *
2258  * An orthogonal equality test for points:
2259  *
2260  * point1 and point2 are considered equal, if
2261  * matrix.point1 + offset - point2
2262  * is parallel to the unit vector in <direction>
2263  */
2264  template <int spacedim>
2265  inline bool
2266  orthogonal_equality(const Point<spacedim> & point1,
2267  const Point<spacedim> & point2,
2268  const int direction,
2269  const Tensor<1, spacedim> &offset,
2270  const FullMatrix<double> & matrix)
2271  {
2272  Assert(0 <= direction && direction < spacedim,
2273  ExcIndexRange(direction, 0, spacedim));
2274 
2275  Assert(matrix.m() == matrix.n(), ExcInternalError());
2276 
2277  Point<spacedim> distance;
2278 
2279  if (matrix.m() == spacedim)
2280  for (int i = 0; i < spacedim; ++i)
2281  for (int j = 0; j < spacedim; ++j)
2282  distance(i) += matrix(i, j) * point1(j);
2283  else
2284  distance = point1;
2285 
2286  distance += offset - point2;
2287 
2288  for (int i = 0; i < spacedim; ++i)
2289  {
2290  // Only compare coordinate-components != direction:
2291  if (i == direction)
2292  continue;
2293 
2294  if (std::abs(distance(i)) > 1.e-10)
2295  return false;
2296  }
2297 
2298  return true;
2299  }
2300 
2301 
2302  /*
2303  * Internally used in orthogonal_equality
2304  *
2305  * A lookup table to transform vertex matchings to orientation flags of
2306  * the form (face_orientation, face_flip, face_rotation)
2307  *
2308  * See the comment on the next function as well as the detailed
2309  * documentation of make_periodicity_constraints and
2310  * collect_periodic_faces for details
2311  */
2312  template <int dim>
2313  struct OrientationLookupTable
2314  {};
2315 
2316  template <>
2317  struct OrientationLookupTable<1>
2318  {
2319  using MATCH_T =
2320  std::array<unsigned int, GeometryInfo<1>::vertices_per_face>;
2321  static inline std::bitset<3>
2322  lookup(const MATCH_T &)
2323  {
2324  // The 1D case is trivial
2325  return 1; // [true ,false,false]
2326  }
2327  };
2328 
2329  template <>
2330  struct OrientationLookupTable<2>
2331  {
2332  using MATCH_T =
2333  std::array<unsigned int, GeometryInfo<2>::vertices_per_face>;
2334  static inline std::bitset<3>
2335  lookup(const MATCH_T &matching)
2336  {
2337  // In 2D matching faces (=lines) results in two cases: Either
2338  // they are aligned or flipped. We store this "line_flip"
2339  // property somewhat sloppy as "face_flip"
2340  // (always: face_orientation = true, face_rotation = false)
2341 
2342  static const MATCH_T m_tff = {{0, 1}};
2343  if (matching == m_tff)
2344  return 1; // [true ,false,false]
2345  static const MATCH_T m_ttf = {{1, 0}};
2346  if (matching == m_ttf)
2347  return 3; // [true ,true ,false]
2348  Assert(false, ExcInternalError());
2349  // what follows is dead code, but it avoids warnings about the lack
2350  // of a return value
2351  return 0;
2352  }
2353  };
2354 
2355  template <>
2356  struct OrientationLookupTable<3>
2357  {
2358  using MATCH_T =
2359  std::array<unsigned int, GeometryInfo<3>::vertices_per_face>;
2360  static inline std::bitset<3>
2361  lookup(const MATCH_T &matching)
2362  {
2363  // The full fledged 3D case. *Yay*
2364  // See the documentation in include/deal.II/base/geometry_info.h
2365  // as well as the actual implementation in source/grid/tria.cc
2366  // for more details...
2367 
2368  static const MATCH_T m_tff = {{0, 1, 2, 3}};
2369  if (matching == m_tff)
2370  return 1; // [true ,false,false]
2371  static const MATCH_T m_tft = {{1, 3, 0, 2}};
2372  if (matching == m_tft)
2373  return 5; // [true ,false,true ]
2374  static const MATCH_T m_ttf = {{3, 2, 1, 0}};
2375  if (matching == m_ttf)
2376  return 3; // [true ,true ,false]
2377  static const MATCH_T m_ttt = {{2, 0, 3, 1}};
2378  if (matching == m_ttt)
2379  return 7; // [true ,true ,true ]
2380  static const MATCH_T m_fff = {{0, 2, 1, 3}};
2381  if (matching == m_fff)
2382  return 0; // [false,false,false]
2383  static const MATCH_T m_fft = {{2, 3, 0, 1}};
2384  if (matching == m_fft)
2385  return 4; // [false,false,true ]
2386  static const MATCH_T m_ftf = {{3, 1, 2, 0}};
2387  if (matching == m_ftf)
2388  return 2; // [false,true ,false]
2389  static const MATCH_T m_ftt = {{1, 0, 3, 2}};
2390  if (matching == m_ftt)
2391  return 6; // [false,true ,true ]
2392  Assert(false, ExcInternalError());
2393  // what follows is dead code, but it avoids warnings about the lack
2394  // of a return value
2395  return 0;
2396  }
2397  };
2398 
2399 
2400 
2401  template <typename FaceIterator>
2402  inline bool orthogonal_equality(
2403  std::bitset<3> & orientation,
2404  const FaceIterator & face1,
2405  const FaceIterator & face2,
2406  const int direction,
2408  const FullMatrix<double> & matrix)
2409  {
2410  Assert(matrix.m() == matrix.n(),
2411  ExcMessage("The supplied matrix must be a square matrix"));
2412 
2413  static const int dim = FaceIterator::AccessorType::dimension;
2414 
2415  // Do a full matching of the face vertices:
2416 
2417  std::array<unsigned int, GeometryInfo<dim>::vertices_per_face> matching;
2418 
2419  std::set<unsigned int> face2_vertices;
2420  for (unsigned int i = 0; i < GeometryInfo<dim>::vertices_per_face; ++i)
2421  face2_vertices.insert(i);
2422 
2423  for (unsigned int i = 0; i < GeometryInfo<dim>::vertices_per_face; ++i)
2424  {
2425  for (std::set<unsigned int>::iterator it = face2_vertices.begin();
2426  it != face2_vertices.end();
2427  ++it)
2428  {
2429  if (orthogonal_equality(face1->vertex(i),
2430  face2->vertex(*it),
2431  direction,
2432  offset,
2433  matrix))
2434  {
2435  matching[i] = *it;
2436  face2_vertices.erase(it);
2437  break; // jump out of the innermost loop
2438  }
2439  }
2440  }
2441 
2442  // And finally, a lookup to determine the ordering bitmask:
2443  if (face2_vertices.empty())
2444  orientation = OrientationLookupTable<dim>::lookup(matching);
2445 
2446  return face2_vertices.empty();
2447  }
2448 
2449 
2450 
2451  template <typename FaceIterator>
2452  inline bool
2454  const FaceIterator & face1,
2455  const FaceIterator & face2,
2456  const int direction,
2458  const FullMatrix<double> & matrix)
2459  {
2460  // Call the function above with a dummy orientation array
2461  std::bitset<3> dummy;
2462  return orthogonal_equality(dummy, face1, face2, direction, offset, matrix);
2463  }
2464 
2465 
2466 
2467 } // namespace GridTools
2468 
2469 
2470 #include "grid_tools_dof_handlers.inst"
2471 
2472 
2473 DEAL_II_NAMESPACE_CLOSE
unsigned int n_active_cells() const
Definition: tria.cc:12545
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
void clear_user_flags()
Definition: tria.cc:11044
std::map< unsigned int, Point< spacedim > > extract_used_vertices(const Triangulation< dim, spacedim > &container, const Mapping< dim, spacedim > &mapping=StaticMappingQ1< dim, spacedim >::mapping)
Definition: grid_tools.cc:5309
Contents is actually a matrix.
bool have_same_coarse_mesh(const Triangulation< dim, spacedim > &mesh_1, const Triangulation< dim, spacedim > &mesh_2)
unsigned int n_cells() const
Definition: tria.cc:12537
BoundingBox< spacedim > compute_bounding_box(const Triangulation< dim, spacedim > &triangulation)
Definition: grid_tools.cc:420
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
std::vector< typename MeshType::active_cell_iterator > compute_ghost_cell_layer_within_distance(const MeshType &mesh, const double layer_thickness)
bool orthogonal_equality(std::bitset< 3 > &orientation, const FaceIterator &face1, const FaceIterator &face2, const int direction, const Tensor< 1, FaceIterator::AccessorType::space_dimension > &offset=Tensor< 1, FaceIterator::AccessorType::space_dimension >(), const FullMatrix< double > &matrix=FullMatrix< double >())
active_cell_iterator begin_active(const unsigned int level=0) const
Definition: tria.cc:11883
#define AssertThrow(cond, exc)
Definition: exceptions.h:1519
virtual Point< dim > transform_real_to_unit_cell(const typename Triangulation< dim, spacedim >::cell_iterator &cell, const Point< spacedim > &p) const =0
std::vector< typename MeshType::cell_iterator > compute_cell_halo_layer_on_level(const MeshType &mesh, const std::function< bool(const typename MeshType::cell_iterator &)> &predicate, const unsigned int level)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
cell_iterator begin(const unsigned int level=0) const
Definition: tria.cc:11863
static double distance_to_unit_cell(const Point< dim > &p)
MeshType< dim, spacedim >::active_cell_iterator find_active_cell_around_point(const MeshType< dim, spacedim > &mesh, const Point< spacedim > &p, const std::vector< bool > &marked_vertices={})
std::vector< typename MeshType::active_cell_iterator > compute_active_cell_halo_layer(const MeshType &mesh, const std::function< bool(const typename MeshType::active_cell_iterator &)> &predicate)
typename ActiveSelector::active_cell_iterator active_cell_iterator
Definition: dof_handler.h:303
cell_iterator end() const
Definition: tria.cc:11949
virtual void execute_coarsening_and_refinement()
Definition: tria.cc:13267
Definition: tria.h:81
static ::ExceptionBase & ExcMessage(std::string arg1)
virtual void create_triangulation(const std::vector< Point< spacedim >> &vertices, const std::vector< CellData< dim >> &cells, const SubCellData &subcelldata)
Definition: tria.cc:10504
#define Assert(cond, exc)
Definition: exceptions.h:1407
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
Abstract base class for mapping classes.
Definition: dof_tools.h:57
const std::vector< Point< spacedim > > & get_vertices() const
void build_triangulation_from_patch(const std::vector< typename Container::active_cell_iterator > &patch, Triangulation< Container::dimension, Container::space_dimension > &local_triangulation, std::map< typename Triangulation< Container::dimension, Container::space_dimension >::active_cell_iterator, typename Container::active_cell_iterator > &patch_to_global_tria_map)
const hp::FECollection< dim, spacedim > & get_fe_collection() const
std::vector< typename MeshType::active_cell_iterator > compute_ghost_cell_halo_layer(const MeshType &mesh)
unsigned int global_dof_index
Definition: types.h:89
__global__ void set(Number *val, const Number s, const size_type N)
std::map< types::global_dof_index, std::vector< typename DoFHandlerType::active_cell_iterator > > get_dof_to_support_patch_map(DoFHandlerType &dof_handler)
const std::vector< bool > & get_used_vertices() const
Definition: tria.cc:13142
std::vector< typename MeshType< dim, spacedim >::active_cell_iterator > find_cells_adjacent_to_vertex(const MeshType< dim, spacedim > &container, const unsigned int vertex_index)
Definition: grid_tools.cc:1464
const Triangulation< dim, spacedim > & get_triangulation() const
void collect_periodic_faces(const MeshType &mesh, const types::boundary_id b_id1, const types::boundary_id b_id2, const int direction, std::vector< PeriodicFacePair< typename MeshType::cell_iterator >> &matched_pairs, const Tensor< 1, MeshType::space_dimension > &offset=::Tensor< 1, MeshType::space_dimension >(), const FullMatrix< double > &matrix=FullMatrix< double >())
virtual bool preserves_vertex_locations() const =0
static ::ExceptionBase & ExcNotImplemented()
Iterator points to a valid object.
std::vector< typename Container::cell_iterator > get_cells_at_coarsest_common_level(const std::vector< typename Container::active_cell_iterator > &patch_cells)
static ::ExceptionBase & ExcVertexNotUsed(unsigned int arg1)
std::list< std::pair< typename MeshType::cell_iterator, typename MeshType::cell_iterator > > get_finest_common_cells(const MeshType &mesh_1, const MeshType &mesh_2)
unsigned int size() const
unsigned int boundary_id
Definition: types.h:111
numbers::NumberTraits< Number >::real_type distance_square(const Point< dim, Number > &p) const
std::vector< typename MeshType::active_cell_iterator > compute_active_cell_layer_within_distance(const MeshType &mesh, const std::function< bool(const typename MeshType::active_cell_iterator &)> &predicate, const double layer_thickness)
std::vector< std::pair< typename MeshType< dim, spacedim >::active_cell_iterator, Point< dim > > > find_all_active_cells_around_point(const Mapping< dim, spacedim > &mapping, const MeshType< dim, spacedim > &mesh, const Point< spacedim > &p, const double tolerance=1e-12, const std::vector< bool > &marked_vertices={})
unsigned int find_closest_vertex(const std::map< unsigned int, Point< spacedim >> &vertices, const Point< spacedim > &p)
Definition: grid_tools.cc:5328
virtual void clear()
Definition: tria.cc:10180
std::vector< typename MeshType::active_cell_iterator > get_patch_around_cell(const typename MeshType::active_cell_iterator &cell)
static ::ExceptionBase & ExcInternalError()
Triangulation< dim, spacedim > & get_triangulation()
Definition: tria.cc:13220