Reference documentation for deal.II version Git 7baca85e57 2019-08-23 17:29:05 -0400
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
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 
81  clear_signal = triangulation.signals.any_change.connect(
82  [&]() -> void { this->support_point_cache.reset(); });
83 
85  std::make_shared<std::vector<std::vector<std::vector<Point<spacedim>>>>>(
86  triangulation.n_levels());
87  for (unsigned int l = 0; l < triangulation.n_levels(); ++l)
88  (*support_point_cache)[l].resize(triangulation.n_raw_cells(l));
89 
91  triangulation.begin(),
92  triangulation.end(),
93  [&](const typename Triangulation<dim, spacedim>::cell_iterator &cell,
94  void *,
95  void *) {
96  (*support_point_cache)[cell->level()][cell->index()] =
97  mapping.compute_mapping_support_points(cell);
98  },
99  /* copier */ std::function<void(void *)>(),
100  /* scratch_data */ nullptr,
101  /* copy_data */ nullptr,
103  /* chunk_size = */ 1);
104 }
105 
106 
107 
108 template <int dim, int spacedim>
109 std::size_t
111 {
112  if (support_point_cache.get() != nullptr)
113  return sizeof(*this) +
115  else
116  return sizeof(*this);
117 }
118 
119 
120 
121 template <int dim, int spacedim>
122 std::vector<Point<spacedim>>
124  const typename Triangulation<dim, spacedim>::cell_iterator &cell) const
125 {
126  Assert(support_point_cache.get() != nullptr,
127  ExcMessage("Must call MappingQCache::initialize() before "
128  "using it or after mesh has changed!"));
129 
130  AssertIndexRange(cell->level(), support_point_cache->size());
131  AssertIndexRange(cell->index(), (*support_point_cache)[cell->level()].size());
132  return (*support_point_cache)[cell->level()][cell->index()];
133 }
134 
135 
136 
137 //--------------------------- Explicit instantiations -----------------------
138 #include "mapping_q_cache.inst"
139 
140 
141 DEAL_II_NAMESPACE_CLOSE
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
virtual std::unique_ptr< Mapping< dim, spacedim > > clone() const override
unsigned int n_raw_cells(const unsigned int level) const
Definition: tria.cc:12652
std::size_t memory_consumption() const
#define AssertIndexRange(index, range)
Definition: exceptions.h:1637
std::shared_ptr< std::vector< std::vector< std::vector< Point< spacedim > > > > > support_point_cache
cell_iterator begin(const unsigned int level=0) const
Definition: tria.cc:11897
unsigned int n_levels() const
virtual std::vector< Point< spacedim > > compute_mapping_support_points(const typename Triangulation< dim, spacedim >::cell_iterator &cell) const override
cell_iterator end() const
Definition: tria.cc:11983
static ::ExceptionBase & ExcMessage(std::string arg1)
MappingQCache(const unsigned int polynomial_degree)
#define Assert(cond, exc)
Definition: exceptions.h:1407
Signals signals
Definition: tria.h:2395
virtual bool preserves_vertex_locations() const override
unsigned int get_degree() const
void initialize(const Triangulation< dim, spacedim > &triangulation, const MappingQGeneric< dim, spacedim > &mapping)
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:1167
static unsigned int n_threads()
boost::signals2::connection clear_signal
std::enable_if< std::is_fundamental< T >::value, std::size_t >::type memory_consumption(const T &t)