Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
grid_tools_cache.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 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/bounding_box.h>
17 #include <deal.II/base/mpi.h>
18 
19 #include <deal.II/grid/filtered_iterator.h>
20 #include <deal.II/grid/grid_tools.h>
21 #include <deal.II/grid/grid_tools_cache.h>
22 
23 #include <boost/geometry.hpp>
24 
25 DEAL_II_NAMESPACE_OPEN
26 
27 namespace GridTools
28 {
29  template <int dim, int spacedim>
31  const Mapping<dim, spacedim> & mapping)
32  : update_flags(update_all)
33  , tria(&tria)
34  , mapping(&mapping)
35  {
36  tria_signal =
37  tria.signals.any_change.connect([&]() { mark_for_update(update_all); });
38  }
39 
40  template <int dim, int spacedim>
42  {
43  // Make sure that the signals that was attached to the triangulation
44  // is removed here.
45  if (tria_signal.connected())
46  tria_signal.disconnect();
47  }
48 
49 
50 
51  template <int dim, int spacedim>
52  void
54  {
55  update_flags |= flags;
56  }
57 
58 
59 
60  template <int dim, int spacedim>
61  const std::vector<
62  std::set<typename Triangulation<dim, spacedim>::active_cell_iterator>> &
64  {
65  if (update_flags & update_vertex_to_cell_map)
66  {
67  vertex_to_cells = GridTools::vertex_to_cell_map(*tria);
68  update_flags = update_flags & ~update_vertex_to_cell_map;
69  }
70  return vertex_to_cells;
71  }
72 
73 
74 
75  template <int dim, int spacedim>
76  const std::vector<std::vector<Tensor<1, spacedim>>> &
78  {
80  {
81  vertex_to_cell_centers = GridTools::vertex_to_cell_centers_directions(
82  *tria, get_vertex_to_cell_map());
83  update_flags = update_flags & ~update_vertex_to_cell_centers_directions;
84  }
85  return vertex_to_cell_centers;
86  }
87 
88 
89 
90  template <int dim, int spacedim>
91  const std::map<unsigned int, Point<spacedim>> &
93  {
94  if (update_flags & update_used_vertices)
95  {
96  used_vertices = GridTools::extract_used_vertices(*tria, *mapping);
97  update_flags = update_flags & ~update_used_vertices;
98  }
99  return used_vertices;
100  }
101 
102 
103 
104  template <int dim, int spacedim>
105  const RTree<std::pair<Point<spacedim>, unsigned int>> &
107  {
108  if (update_flags & update_used_vertices_rtree)
109  {
110  const auto &used_vertices = get_used_vertices();
111  std::vector<std::pair<Point<spacedim>, unsigned int>> vertices(
112  used_vertices.size());
113  unsigned int i = 0;
114  for (const auto &it : used_vertices)
115  vertices[i++] = std::make_pair(it.second, it.first);
116  used_vertices_rtree = pack_rtree(vertices);
117  }
118  return used_vertices_rtree;
119  }
120 
121 
122 
123  template <int dim, int spacedim>
124  const RTree<
125  std::pair<BoundingBox<spacedim>,
128  {
129  if (update_flags & update_cell_bounding_boxes_rtree)
130  {
131  std::vector<std::pair<
134  boxes(tria->n_active_cells());
135  unsigned int i = 0;
136  for (const auto &cell : tria->active_cell_iterators())
137  boxes[i++] = std::make_pair(mapping->get_bounding_box(cell), cell);
138 
139  cell_bounding_boxes_rtree = pack_rtree(boxes);
140  }
141  return cell_bounding_boxes_rtree;
142  }
143 
144 
145 #ifdef DEAL_II_WITH_NANOFLANN
146  template <int dim, int spacedim>
147  const KDTree<spacedim> &
149  {
150  if (update_flags & update_vertex_kdtree)
151  {
152  vertex_kdtree.set_points(tria->get_vertices());
153  update_flags = update_flags & ~update_vertex_kdtree;
154  }
155  return vertex_kdtree;
156  }
157 #endif
158 
159 
160 
161  template <int dim, int spacedim>
162  const RTree<std::pair<BoundingBox<spacedim>, unsigned int>> &
164  {
165  if (update_flags & update_covering_rtree)
166  {
167  // TO DO: the locally owned portion of the domain
168  // might consists of more separate pieces: a single
169  // bounding box might not always be the best choice.
170 
171  // Note: we create the local variable ptr here, because gcc 4.8.5
172  // fails to compile if we pass the variable directly.
173  IteratorFilters::LocallyOwnedCell locally_owned_cell_predicate;
174  std::function<bool(
176  ptr(locally_owned_cell_predicate);
177 
178  const BoundingBox<spacedim> bbox =
179  GridTools::compute_bounding_box(get_triangulation(), ptr);
180 
181 
182  std::vector<BoundingBox<spacedim>> bbox_v(1, bbox);
183  if (const auto tria_mpi =
184  dynamic_cast<const parallel::Triangulation<dim, spacedim> *>(
185  &(*tria)))
186  {
188  bbox_v, tria_mpi->get_communicator());
189  }
190  else
191  {
192  covering_rtree =
193  GridTools::build_global_description_tree(bbox_v, MPI_COMM_SELF);
194  }
195  update_flags = update_flags & ~update_covering_rtree;
196  }
197 
198  return covering_rtree;
199  }
200 
201 #include "grid_tools_cache.inst"
202 
203 } // namespace GridTools
204 
205 DEAL_II_NAMESPACE_CLOSE
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
BoundingBox< spacedim > compute_bounding_box(const Triangulation< dim, spacedim > &triangulation)
Definition: grid_tools.cc:420
Cache(const Triangulation< dim, spacedim > &tria, const Mapping< dim, spacedim > &mapping=StaticMappingQ1< dim, spacedim >::mapping)
std::vector< std::set< typename Triangulation< dim, spacedim >::active_cell_iterator > > vertex_to_cell_map(const Triangulation< dim, spacedim > &triangulation)
Definition: grid_tools.cc:2152
RTree< std::pair< BoundingBox< spacedim >, unsigned int > > build_global_description_tree(const std::vector< BoundingBox< spacedim >> &local_description, MPI_Comm mpi_communicator)
Definition: grid_tools.cc:5467
Definition: tria.h:81
Signals signals
Definition: tria.h:2394
Abstract base class for mapping classes.
Definition: dof_tools.h:57
std::vector< std::vector< Tensor< 1, spacedim > > > vertex_to_cell_centers_directions(const Triangulation< dim, spacedim > &mesh, const std::vector< std::set< typename Triangulation< dim, spacedim >::active_cell_iterator >> &vertex_to_cells)
Definition: grid_tools.cc:1597