Reference documentation for deal.II version 9.0.0
mg_level_object.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2018 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_mg_level_object_h
17 #define dealii_mg_level_object_h
18 
19 #include <deal.II/base/subscriptor.h>
20 #include <vector>
21 #include <memory>
22 
23 DEAL_II_NAMESPACE_OPEN
24 
25 
46 template <class Object>
47 class MGLevelObject : public Subscriptor
48 {
49 public:
69  MGLevelObject (const unsigned int minlevel = 0,
70  const unsigned int maxlevel = 0);
71 
75  Object &operator[] (const unsigned int level);
76 
83  const Object &operator[] (const unsigned int level) const;
84 
96  void resize (const unsigned int new_minlevel,
97  const unsigned int new_maxlevel);
98 
105  MGLevelObject<Object> &operator = (const double d);
106 
117  DEAL_II_DEPRECATED
118  void clear();
119 
128  void clear_elements();
129 
133  unsigned int min_level () const;
134 
138  unsigned int max_level () const;
139 
150  template <typename ActionFunctionObjectType>
151  void apply (ActionFunctionObjectType action);
152 
156  std::size_t memory_consumption () const;
157 
158 private:
162  unsigned int minlevel;
163 
167  std::vector<std::shared_ptr<Object> > objects;
168 };
169 
170 
171 /* ------------------------------------------------------------------- */
172 
173 
174 template <class Object>
176  const unsigned int max)
177  :
178  minlevel(0)
179 {
180  resize (min, max);
181 }
182 
183 
184 template <class Object>
185 Object &
187 {
188  Assert((i>=minlevel) && (i<minlevel+objects.size()),
189  ExcIndexRange (i, minlevel, minlevel+objects.size()));
190  return *objects[i-minlevel];
191 }
192 
193 
194 template <class Object>
195 const Object &
196 MGLevelObject<Object>::operator[] (const unsigned int i) const
197 {
198  Assert((i>=minlevel) && (i<minlevel+objects.size()),
199  ExcIndexRange (i, minlevel, minlevel+objects.size()));
200  return *objects[i-minlevel];
201 }
202 
203 
204 template <class Object>
205 void
206 MGLevelObject<Object>::resize (const unsigned int new_minlevel,
207  const unsigned int new_maxlevel)
208 {
209  Assert (new_minlevel <= new_maxlevel, ExcInternalError());
210  // note that on clear(), the
211  // shared_ptr class takes care of
212  // deleting the object it points to
213  // by itself
214  objects.clear ();
215 
216  minlevel = new_minlevel;
217  for (unsigned int i=0; i<new_maxlevel-new_minlevel+1; ++i)
218  objects.push_back(std::make_shared<Object> ());
219 }
220 
221 
222 template <class Object>
225 {
226  typename std::vector<std::shared_ptr<Object> >::iterator v;
227  for (v = objects.begin(); v != objects.end(); ++v)
228  **v=d;
229  return *this;
230 }
231 
232 
233 template <class Object>
234 void
236 {
237  // Avoid code duplication in deprecated call by calling replacing function
238  clear_elements();
239 }
240 
241 
242 template <class Object>
243 void
245 {
246  typename std::vector<std::shared_ptr<Object> >::iterator v;
247  for (v = objects.begin(); v != objects.end(); ++v)
248  (*v)->clear();
249 }
250 
251 
252 template <class Object>
253 unsigned int
255 {
256  return minlevel;
257 }
258 
259 
260 template <class Object>
261 unsigned int
263 {
264  return minlevel + objects.size() - 1;
265 }
266 
267 template <class Object>
268 template <typename ActionFunctionObjectType>
269 void
270 MGLevelObject<Object>::apply (ActionFunctionObjectType action)
271 {
272  for (unsigned int lvl = min_level(); lvl <= max_level(); ++lvl)
273  {
274  action (lvl, (*this)[lvl]);
275  }
276 }
277 
278 
279 template <class Object>
280 std::size_t
282 {
283  std::size_t result = sizeof(*this);
284  typedef typename std::vector<std::shared_ptr<Object> >::const_iterator Iter;
285  const Iter end = objects.end();
286  for (Iter o=objects.begin(); o!=end; ++o)
287  result += (*o)->memory_consumption();
288 
289  return result;
290 }
291 
292 DEAL_II_NAMESPACE_CLOSE
293 
294 #endif
unsigned int max_level() const
Object & operator[](const unsigned int level)
MGLevelObject(const unsigned int minlevel=0, const unsigned int maxlevel=0)
std::vector< std::shared_ptr< Object > > objects
void apply(ActionFunctionObjectType action)
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::size_t memory_consumption() const
unsigned int min_level() const
VectorizedArray< Number > min(const ::VectorizedArray< Number > &x, const ::VectorizedArray< Number > &y)
#define Assert(cond, exc)
Definition: exceptions.h:1142
unsigned int minlevel
MGLevelObject< Object > & operator=(const double d)
void resize(const unsigned int new_minlevel, const unsigned int new_maxlevel)
VectorizedArray< Number > max(const ::VectorizedArray< Number > &x, const ::VectorizedArray< Number > &y)
static ::ExceptionBase & ExcInternalError()