Reference documentation for deal.II version Git 47870e2865 2020-01-25 16:13:11 -0500
\(\newcommand{\vcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\vcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
mapping_q_cache.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 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/memory_consumption.h>
17 #include <deal.II/base/work_stream.h>
18 
19 #include <deal.II/fe/mapping_q_cache.h>
20 
21 #include <functional>
22 
23 DEAL_II_NAMESPACE_OPEN
24 
25 template <int dim, int spacedim>
27  const unsigned int polynomial_degree)
28  : MappingQGeneric<dim, spacedim>(polynomial_degree)
29 {}
30 
31 
32 
33 template <int dim, int spacedim>
35  const MappingQCache<dim, spacedim> &mapping)
36  : MappingQGeneric<dim, spacedim>(mapping)
38 {}
39 
40 
41 
42 template <int dim, int spacedim>
44 {
45  // When this object goes out of scope, we want the cache to get cleared and
46  // free its memory before the signal is disconnected in order to not work on
47  // invalid memory that has been left back by freeing an object of this
48  // class.
49  support_point_cache.reset();
50  clear_signal.disconnect();
51 }
52 
53 
54 
55 template <int dim, int spacedim>
56 std::unique_ptr<Mapping<dim, spacedim>>
58 {
59  return std_cxx14::make_unique<MappingQCache<dim, spacedim>>(*this);
60 }
61 
62 
63 
64 template <int dim, int spacedim>
65 bool
67 {
68  return false;
69 }
70 
71 
72 
73 template <int dim, int spacedim>
74 void
76  const Triangulation<dim, spacedim> & triangulation,
77  const MappingQGeneric<dim, spacedim> &mapping)
78 {
79  AssertDimension(this->get_degree(), mapping.get_degree());
80  initialize(triangulation,
81  [&mapping](const typename Triangulation<dim>::cell_iterator &cell)
82  -> std::vector<Point<spacedim>> {
83  return mapping.compute_mapping_support_points(cell);
84  });
85 }
86 
87 
88 
89 template <int dim, int spacedim>
90 void
92  const Triangulation<dim, spacedim> &triangulation,
93  const std::function<std::vector<Point<spacedim>>(
95  &compute_points_on_cell)
96 {
97  clear_signal = triangulation.signals.any_change.connect(
98  [&]() -> void { this->support_point_cache.reset(); });
99 
101  std::make_shared<std::vector<std::vector<std::vector<Point<spacedim>>>>>(
102  triangulation.n_levels());
103  for (unsigned int l = 0; l < triangulation.n_levels(); ++l)
104  (*support_point_cache)[l].resize(triangulation.n_raw_cells(l));
105 
107  triangulation.begin(),
108  triangulation.end(),
109  [&](const typename Triangulation<dim, spacedim>::cell_iterator &cell,
110  void *,
111  void *) {
112  (*support_point_cache)[cell->level()][cell->index()] =
113  compute_points_on_cell(cell);
115  (*support_point_cache)[cell->level()][cell->index()].size(),
116  Utilities::pow(this->get_degree() + 1, dim));
117  },
118  /* copier */ std::function<void(void *)>(),
119  /* scratch_data */ nullptr,
120  /* copy_data */ nullptr,
122  /* chunk_size = */ 1);
123 }
124 
125 
126 
127 template <int dim, int spacedim>
128 std::size_t
130 {
131  if (support_point_cache.get() != nullptr)
132  return sizeof(*this) +
134  else
135  return sizeof(*this);
136 }
137 
138 
139 
140 template <int dim, int spacedim>
141 std::vector<Point<spacedim>>
143  const typename Triangulation<dim, spacedim>::cell_iterator &cell) const
144 {
145  Assert(support_point_cache.get() != nullptr,
146  ExcMessage("Must call MappingQCache::initialize() before "
147  "using it or after mesh has changed!"));
148 
149  AssertIndexRange(cell->level(), support_point_cache->size());
150  AssertIndexRange(cell->index(), (*support_point_cache)[cell->level()].size());
151  return (*support_point_cache)[cell->level()][cell->index()];
152 }
153 
154 
155 
156 //--------------------------- Explicit instantiations -----------------------
157 #include "mapping_q_cache.inst"
158 
159 
160 DEAL_II_NAMESPACE_CLOSE
virtual bool preserves_vertex_locations() const override
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1571
unsigned int n_raw_cells(const unsigned int level) const
Definition: tria.cc:12794
#define AssertIndexRange(index, range)
Definition: exceptions.h:1641
cell_iterator begin(const unsigned int level=0) const
Definition: tria.cc:12039
unsigned int n_levels() const
std::size_t memory_consumption() const
cell_iterator end() const
Definition: tria.cc:12125
virtual std::unique_ptr< Mapping< dim, spacedim > > clone() const override
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1411
Signals signals
Definition: tria.h:2239
constexpr unsigned int pow(const unsigned int base, const int iexp)
Definition: utilities.h:481
void initialize(const Triangulation< dim, spacedim > &triangulation, const MappingQGeneric< dim, spacedim > &mapping)
unsigned int get_degree() const
boost::signals2::connection clear_signal
MappingQCache(const unsigned int polynomial_degree)
virtual std::vector< Point< spacedim > > compute_mapping_support_points(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const
void run(const std::vector< std::vector< Iterator >> &colored_iterators, Worker worker, Copier copier, const ScratchData &sample_scratch_data, const CopyData &sample_copy_data, const unsigned int queue_length=2 *MultithreadInfo::n_threads(), const unsigned int chunk_size=8)
Definition: work_stream.h:1187
static unsigned int n_threads()
virtual std::vector< Point< spacedim > > compute_mapping_support_points(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const override
std::shared_ptr< std::vector< std::vector< std::vector< Point< spacedim > > > > > support_point_cache
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)