Reference documentation for deal.II version Git ede8f93e86 2020-12-03 14:59:20 -0700
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
mg_level_object.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2020 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 #ifndef dealii_mg_level_object_h
17 #define dealii_mg_level_object_h
18 
19 #include <deal.II/base/config.h>
20 
22 
23 #include <memory>
24 #include <vector>
25 
27 
28 
48 template <class Object>
49 class MGLevelObject : public Subscriptor
50 {
51 public:
73  template <class... Args>
74  MGLevelObject(const unsigned int minlevel,
75  const unsigned int maxlevel,
76  Args &&... args);
77 
82  MGLevelObject(const unsigned int minlevel = 0,
83  const unsigned int maxlevel = 0);
84 
88  Object &operator[](const unsigned int level);
89 
96  const Object &operator[](const unsigned int level) const;
97 
111  template <class... Args>
112  void
113  resize(const unsigned int new_minlevel,
114  const unsigned int new_maxlevel,
115  Args &&... args);
116 
124  operator=(const double d);
125 
134  void
135  clear_elements();
136 
140  unsigned int
141  min_level() const;
142 
146  unsigned int
147  max_level() const;
148 
159  template <typename ActionFunctionObjectType>
160  void
161  apply(ActionFunctionObjectType action);
162 
166  std::size_t
167  memory_consumption() const;
168 
169 private:
173  unsigned int minlevel;
174 
178  std::vector<std::shared_ptr<Object>> objects;
179 };
180 
181 
182 /* ------------------------------------------------------------------- */
183 
184 
185 template <class Object>
186 template <class... Args>
188  const unsigned int max,
189  Args &&... args)
190  : minlevel(0)
191 {
192  resize(min, max, std::forward<Args>(args)...);
193 }
194 
195 
196 template <class Object>
198  const unsigned int max)
199  : minlevel(0)
200 {
201  resize(min, max);
202 }
203 
204 
205 template <class Object>
206 Object &MGLevelObject<Object>::operator[](const unsigned int i)
207 {
208  Assert((i >= minlevel) && (i < minlevel + objects.size()),
209  ExcIndexRange(i, minlevel, minlevel + objects.size()));
210  return *objects[i - minlevel];
211 }
212 
213 
214 template <class Object>
215 const Object &MGLevelObject<Object>::operator[](const unsigned int i) const
216 {
217  Assert((i >= minlevel) && (i < minlevel + objects.size()),
218  ExcIndexRange(i, minlevel, minlevel + objects.size()));
219  return *objects[i - minlevel];
220 }
221 
222 
223 template <class Object>
224 template <class... Args>
225 void
226 MGLevelObject<Object>::resize(const unsigned int new_minlevel,
227  const unsigned int new_maxlevel,
228  Args &&... args)
229 {
230  Assert(new_minlevel <= new_maxlevel, ExcInternalError());
231  // note that on clear(), the
232  // shared_ptr class takes care of
233  // deleting the object it points to
234  // by itself
235  objects.clear();
236 
237  minlevel = new_minlevel;
238  for (unsigned int i = 0; i < new_maxlevel - new_minlevel + 1; ++i)
239  objects.push_back(std::make_shared<Object>(std::forward<Args>(args)...));
240 }
241 
242 
243 template <class Object>
246 {
247  typename std::vector<std::shared_ptr<Object>>::iterator v;
248  for (v = objects.begin(); v != objects.end(); ++v)
249  **v = d;
250  return *this;
251 }
252 
253 
254 template <class Object>
255 void
257 {
258  typename std::vector<std::shared_ptr<Object>>::iterator v;
259  for (v = objects.begin(); v != objects.end(); ++v)
260  (*v)->clear();
261 }
262 
263 
264 template <class Object>
265 unsigned int
267 {
268  return minlevel;
269 }
270 
271 
272 template <class Object>
273 unsigned int
275 {
276  return minlevel + objects.size() - 1;
277 }
278 
279 template <class Object>
280 template <typename ActionFunctionObjectType>
281 void
282 MGLevelObject<Object>::apply(ActionFunctionObjectType action)
283 {
284  for (unsigned int lvl = min_level(); lvl <= max_level(); ++lvl)
285  {
286  action(lvl, (*this)[lvl]);
287  }
288 }
289 
290 
291 template <class Object>
292 std::size_t
294 {
295  std::size_t result = sizeof(*this);
296  using Iter = typename std::vector<std::shared_ptr<Object>>::const_iterator;
297  const Iter end = objects.end();
298  for (Iter o = objects.begin(); o != end; ++o)
299  result += (*o)->memory_consumption();
300 
301  return result;
302 }
303 
305 
306 #endif
unsigned int max_level() const
Object & operator[](const unsigned int level)
std::vector< std::shared_ptr< Object > > objects
void resize(const unsigned int new_minlevel, const unsigned int new_maxlevel, Args &&... args)
void apply(ActionFunctionObjectType action)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::size_t memory_consumption() const
unsigned int min_level() const
#define Assert(cond, exc)
Definition: exceptions.h:1466
unsigned int minlevel
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:372
unsigned int level
Definition: grid_out.cc:4343
VectorType::value_type * end(VectorType &V)
SymmetricTensor< 2, dim, Number > d(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
MGLevelObject< Object > & operator=(const double d)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:371
T min(const T &t, const MPI_Comm &mpi_communicator)
MGLevelObject(const unsigned int minlevel, const unsigned int maxlevel, Args &&... args)
T max(const T &t, const MPI_Comm &mpi_communicator)
static ::ExceptionBase & ExcInternalError()