Reference documentation for deal.II version Git 418c2197d0 2021-10-27 08:19:48 +0200
\(\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 &
89  operator[](const unsigned int level);
90 
97  const Object &
98  operator[](const unsigned int level) const;
99 
113  template <class... Args>
114  void
115  resize(const unsigned int new_minlevel,
116  const unsigned int new_maxlevel,
117  Args &&...args);
118 
126  operator=(const double d);
127 
136  void
137  clear_elements();
138 
142  unsigned int
143  min_level() const;
144 
148  unsigned int
149  max_level() const;
150 
161  template <typename ActionFunctionObjectType>
162  void
163  apply(ActionFunctionObjectType action);
164 
168  std::size_t
169  memory_consumption() const;
170 
171 private:
175  unsigned int minlevel;
176 
180  std::vector<std::shared_ptr<Object>> objects;
181 };
182 
183 
184 /* ------------------------------------------------------------------- */
185 
186 
187 template <class Object>
188 template <class... Args>
190  const unsigned int max,
191  Args &&...args)
192  : minlevel(0)
193 {
194  resize(min, max, std::forward<Args>(args)...);
195 }
196 
197 
198 template <class Object>
200  const unsigned int max)
201  : minlevel(0)
202 {
203  resize(min, max);
204 }
205 
206 
207 template <class Object>
208 Object &
210 {
211  Assert((i >= minlevel) && (i < minlevel + objects.size()),
212  ExcIndexRange(i, minlevel, minlevel + objects.size()));
213  return *objects[i - minlevel];
214 }
215 
216 
217 template <class Object>
218 const Object &
219 MGLevelObject<Object>::operator[](const unsigned int i) const
220 {
221  Assert((i >= minlevel) && (i < minlevel + objects.size()),
222  ExcIndexRange(i, minlevel, minlevel + objects.size()));
223  return *objects[i - minlevel];
224 }
225 
226 
227 template <class Object>
228 template <class... Args>
229 void
230 MGLevelObject<Object>::resize(const unsigned int new_minlevel,
231  const unsigned int new_maxlevel,
232  Args &&...args)
233 {
234  Assert(new_minlevel <= new_maxlevel, ExcInternalError());
235  // note that on clear(), the
236  // shared_ptr class takes care of
237  // deleting the object it points to
238  // by itself
239  objects.clear();
240 
241  minlevel = new_minlevel;
242  for (unsigned int i = 0; i < new_maxlevel - new_minlevel + 1; ++i)
243  objects.push_back(std::make_shared<Object>(std::forward<Args>(args)...));
244 }
245 
246 
247 template <class Object>
250 {
251  typename std::vector<std::shared_ptr<Object>>::iterator v;
252  for (v = objects.begin(); v != objects.end(); ++v)
253  **v = d;
254  return *this;
255 }
256 
257 
258 template <class Object>
259 void
261 {
262  typename std::vector<std::shared_ptr<Object>>::iterator v;
263  for (v = objects.begin(); v != objects.end(); ++v)
264  (*v)->clear();
265 }
266 
267 
268 template <class Object>
269 unsigned int
271 {
272  return minlevel;
273 }
274 
275 
276 template <class Object>
277 unsigned int
279 {
280  return minlevel + objects.size() - 1;
281 }
282 
283 template <class Object>
284 template <typename ActionFunctionObjectType>
285 void
286 MGLevelObject<Object>::apply(ActionFunctionObjectType action)
287 {
288  for (unsigned int lvl = min_level(); lvl <= max_level(); ++lvl)
289  {
290  action(lvl, (*this)[lvl]);
291  }
292 }
293 
294 
295 template <class Object>
296 std::size_t
298 {
299  std::size_t result = sizeof(*this);
300  using Iter = typename std::vector<std::shared_ptr<Object>>::const_iterator;
301  const Iter end = objects.end();
302  for (Iter o = objects.begin(); o != end; ++o)
303  result += (*o)->memory_consumption();
304 
305  return result;
306 }
307 
309 
310 #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)
std::size_t memory_consumption() const
unsigned int min_level() const
#define Assert(cond, exc)
Definition: exceptions.h:1461
unsigned int minlevel
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:401
unsigned int level
Definition: grid_out.cc:4589
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:400
static ::ExceptionBase & ExcIndexRange(std::size_t arg1, std::size_t arg2, std::size_t arg3)
MGLevelObject(const unsigned int minlevel, const unsigned int maxlevel, Args &&...args)
static ::ExceptionBase & ExcInternalError()