Reference documentation for deal.II version Git d9b14c2d54 2019-09-14 18:13:29 -0600
\(\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  update_flags = update_flags & ~update_used_vertices_rtree;
118  }
119  return used_vertices_rtree;
120  }
121 
122 
123 
124  template <int dim, int spacedim>
125  const RTree<
126  std::pair<BoundingBox<spacedim>,
129  {
130  if (update_flags & update_cell_bounding_boxes_rtree)
131  {
132  std::vector<std::pair<
135  boxes(tria->n_active_cells());
136  unsigned int i = 0;
137  for (const auto &cell : tria->active_cell_iterators())
138  boxes[i++] = std::make_pair(mapping->get_bounding_box(cell), cell);
139 
140  cell_bounding_boxes_rtree = pack_rtree(boxes);
141  update_flags = update_flags & ~update_cell_bounding_boxes_rtree;
142  }
143  return cell_bounding_boxes_rtree;
144  }
145 
146 
147 #ifdef DEAL_II_WITH_NANOFLANN
148  template <int dim, int spacedim>
149  const KDTree<spacedim> &
151  {
152  if (update_flags & update_vertex_kdtree)
153  {
154  vertex_kdtree.set_points(tria->get_vertices());
155  update_flags = update_flags & ~update_vertex_kdtree;
156  }
157  return vertex_kdtree;
158  }
159 #endif
160 
161 
162 
163  template <int dim, int spacedim>
164  const RTree<std::pair<BoundingBox<spacedim>, unsigned int>> &
166  {
167  if (update_flags & update_covering_rtree)
168  {
169  // TO DO: the locally owned portion of the domain
170  // might consists of more separate pieces: a single
171  // bounding box might not always be the best choice.
172 
173  // Note: we create the local variable ptr here, because gcc 4.8.5
174  // fails to compile if we pass the variable directly.
175  IteratorFilters::LocallyOwnedCell locally_owned_cell_predicate;
176  std::function<bool(
178  ptr(locally_owned_cell_predicate);
179 
180  const BoundingBox<spacedim> bbox =
181  GridTools::compute_bounding_box(get_triangulation(), ptr);
182 
183 
184  std::vector<BoundingBox<spacedim>> bbox_v(1, bbox);
185  if (const auto tria_mpi =
186  dynamic_cast<const parallel::TriangulationBase<dim, spacedim> *>(
187  &(*tria)))
188  {
190  bbox_v, tria_mpi->get_communicator());
191  }
192  else
193  {
194  covering_rtree =
195  GridTools::build_global_description_tree(bbox_v, MPI_COMM_SELF);
196  }
197  update_flags = update_flags & ~update_covering_rtree;
198  }
199 
200  return covering_rtree;
201  }
202 
203 #include "grid_tools_cache.inst"
204 
205 } // namespace GridTools
206 
207 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:5301
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:2154
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:5459
Signals signals
Definition: tria.h:2408
Abstract base class for mapping classes.
Definition: mapping.h:301
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:1599