ug4
selector_multi_grid.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Sebastian Reiter
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
35 // ...
37 
38 #ifndef __H__LIBGRID__SELECTOR_MULTI_GRID__
39 #define __H__LIBGRID__SELECTOR_MULTI_GRID__
40 
41 #include <cassert>
42 #include "selector_interface.h"
43 #include "../multi_grid.h"
44 #include "../lib_grid_messages.h"
45 
46 namespace ug
47 {
49 // predeclarations
50 class MultiGrid;
51 
57 // MGSelector
59 
108 {
109  public:
112 
116  template <class TElem, class TMGSelector, class TLevelIterator>
118  {
119  public:
121  typedef std::forward_iterator_tag iterator_category;
122  typedef size_t difference_type;
123  typedef TElem** pointer;
124  typedef TElem* value_type;
126 
127  MGSelectionIterator() : m_sel(NULL), m_lvl(0) {}
128 
131  typename geometry_traits<TElem>::iterator>& iter)
132  {
133  m_sel = iter.m_sel;
134  m_lvl = iter.m_lvl;
135  m_iter = iter.m_iter;
136  }
137 
138  this_type operator ++() {increment(); return *this;}
139  this_type operator ++(int unused) {this_type i = *this; increment(); return i;}
140 
141  bool operator ==(const this_type& iter) const {return equal(iter);}
142  bool operator !=(const this_type& iter) const {return !equal(iter);}
143 
144  value_type operator *() {return dereference();}
145  value_type operator *() const {return dereference();}
146 
147  private:
148  friend class MGSelector;
149  // friend class MGSelectionIterator<TElem, const MGSelector,
150  // typename MGSelector::traits<TElem>::const_level_iterator>;
151  typedef TLevelIterator level_iterator;
152 
153  MGSelectionIterator(TMGSelector* sel, int lvl,
154  level_iterator iter)
155  {
156  m_sel = sel;
157  m_lvl = lvl;
158  m_iter = iter;
159  }
160 
161  inline bool equal(const this_type& other) const
162  {
163  return m_iter == other.m_iter;
164  }
165 
167  void increment()
168  {
169  ++m_iter;
170  while((m_iter == m_sel->template end<TElem>(m_lvl))
171  && (m_lvl + 1 < m_sel->num_levels()))
172  {
173  ++m_lvl;
174  m_iter = m_sel->template begin<TElem>(m_lvl);
175  }
176  }
177 
179  inline value_type dereference() const
180  {
181  return *m_iter;
182  }
183 
184  private:
185  TMGSelector* m_sel;
186  size_t m_lvl;
188  };
189 
191  template <class TElem>
192  struct traits{
193  typedef TElem* value_t;
196  typedef MGSelectionIterator<TElem, MGSelector,
198  typedef MGSelectionIterator<TElem, const MGSelector,
200  };
201 
202 
203  MGSelector(uint supportedElements = SE_ALL);
204  MGSelector(MultiGrid& grid, uint supportedElements = SE_ALL);
205  virtual ~MGSelector();
206 
207  void assign_grid(MultiGrid& grid);
208  void assign_grid(MultiGrid* grid);
209  inline MultiGrid* multi_grid() {return m_pMultiGrid;}
210 
212 
214  // forwards to protected ISelector method. This rather complicated setup
215  // is required to avoid virtual method calls during construction.
216  inline void set_supported_elements(uint shElements);
217 
219 
220  // forwards to protected ISelector method. This rather complicated setup
221  // is required to avoid virtual method calls during construction.
222  inline void enable_element_support(uint shElements);
223 
225 
226  // forwards to protected ISelector method. This rather complicated setup
227  // is required to avoid virtual method calls during construction.
228  void disable_element_support(uint shElements);
229 
230  inline size_t num_levels() const {return m_levels.size();}
231  inline size_t top_level() const
232  {
233  size_t l = m_levels.size();
234  if(l == 0)
235  return 0;
236  else
237  return l - 1;
238  }
239 
240  virtual void clear();
241 
242  template <class TElem>
243  inline void clear();
244 
245  void clear(int level);
246 
247  template <class TElem>
248  inline void clear(int level);
249 
250  template <class TElem>
251  inline size_t num(int level) const;
252 
253  inline size_t num(int level) const;
254 
255  template <class TElem>
256  inline size_t num() const;
257 
258  inline size_t num() const;
259 
260  // empty
261  inline bool empty(int level) const;
262 
263  template <class TElem>
264  inline bool empty(int level) const;
265 
266  inline bool empty() const;
267 
268  template <class TElem>
269  inline bool empty() const;
270 
271  // begin
272  template <class TElem>
273  inline typename traits<TElem>::iterator
274  begin();
275 
276  template <class TElem>
277  inline typename traits<TElem>::const_iterator
278  begin() const;
279 
280  template <class TElem>
281  inline typename traits<TElem>::level_iterator
282  begin(int level);
283 
284  template <class TElem>
285  inline typename traits<TElem>::const_level_iterator
286  begin(int level) const;
287 
288  // end
289  template <class TElem>
290  inline typename traits<TElem>::iterator
291  end();
292 
293  template <class TElem>
294  inline typename traits<TElem>::const_iterator
295  end() const;
296 
297  template <class TElem>
298  inline typename traits<TElem>::level_iterator
299  end(int level);
300 
301  template <class TElem>
302  inline typename traits<TElem>::const_level_iterator
303  end(int level) const;
304 
305  // convenience begin and end
306  inline traits<Vertex>::level_iterator vertices_begin(int level) {return begin<Vertex>(level);}
307  inline traits<Vertex>::level_iterator vertices_end(int level) {return end<Vertex>(level);}
308  inline traits<Edge>::level_iterator edges_begin(int level) {return begin<Edge>(level);}
309  inline traits<Edge>::level_iterator edges_end(int level) {return end<Edge>(level);}
310  inline traits<Face>::level_iterator faces_begin(int level) {return begin<Face>(level);}
311  inline traits<Face>::level_iterator faces_end(int level) {return end<Face>(level);}
312  inline traits<Volume>::level_iterator volumes_begin(int level) {return begin<Volume>(level);}
313  inline traits<Volume>::level_iterator volumes_end(int level) {return end<Volume>(level);}
314 
316 
318  template <class TElem> TElem* front(int level);
319 
321 
323  template <class TElem> TElem* back(int level);
324 
325  // geometric-object-collection
326  virtual GridObjectCollection get_grid_objects() const;
327 
328  // callbacks that allows us to clean-up
329  // derived from GridObserver
330  /*
331  virtual void registered_at_grid(Grid* grid);
332  virtual void unregistered_from_grid(Grid* grid);
333  */
334  virtual void grid_to_be_destroyed(Grid* grid);
335 
337  virtual bool contains_vertices() const {return num<Vertex>() > 0;}
338 
340  virtual bool contains_edges() const {return num<Edge>() > 0;}
341 
343  virtual bool contains_faces() const {return num<Face>() > 0;}
344 
346  virtual bool contains_volumes() const {return num<Volume>() > 0;}
347 
348  protected:
349  void clear_lists();
350 
351  virtual void add_to_list(Vertex* elem);
352  virtual void add_to_list(Edge* elem);
353  virtual void add_to_list(Face* elem);
354  virtual void add_to_list(Volume* elem);
355 
356  virtual void erase_from_list(Vertex* elem);
357  virtual void erase_from_list(Edge* elem);
358  virtual void erase_from_list(Face* elem);
359  virtual void erase_from_list(Volume* elem);
360 
361  protected:
366 
371 
372  struct Level{
377  };
378  typedef std::vector<Level*> LevelVec;
379 
380  protected:
382  template <class TElem> inline
384  section_container(int level);
385 
387  template <class TElem> inline
389  section_container(int level) const;
390 
391  template <class TElem>
392  inline int get_section_index() const;
393 
394  inline void level_required(int newSize);
395  void add_level();
396 
398  void cleanup();
399 
401 
406  {
407  assert(is_selected(o) && "object not selected.");
408  return section_container<Vertex>(m_pMultiGrid->get_level(o)).
409  get_container().get_iterator(o);
410  }
411 
412  inline EdgeSectionContainer::iterator
414  {
415  assert(is_selected(o) && "object not selected");
416  return section_container<Edge>(m_pMultiGrid->get_level(o)).
417  get_container().get_iterator(o);
418  }
419 
420  inline FaceSectionContainer::iterator
422  {
423  assert(is_selected(o) && "object not selected");
424  return section_container<Face>(m_pMultiGrid->get_level(o)).
425  get_container().get_iterator(o);
426  }
427 
428  inline VolumeSectionContainer::iterator
430  {
431  assert(is_selected(o) && "object not selected");
432  return section_container<Volume>(m_pMultiGrid->get_level(o)).
433  get_container().get_iterator(o);
434  }
438  void multigrid_changed(const GridMessage_MultiGridChanged& gm);
439 
440  private:
441  MGSelector(const MGSelector& sel){};
442 
443  protected:
448 
449  // callback-id (automatically unregisters callback, when the selector is deleted).
451 
452  // we use a shared attachment for the entry-lists of all section containers
457 };
458 
461 }// end of namespace
462 
464 // include implementation
466 
467 #endif
A special iterator which allows to iterate over elements in a AttachedElementList.
Definition: attached_list.h:52
Base-class for edges.
Definition: grid_base_objects.h:397
Faces are 2-dimensional objects.
Definition: grid_base_objects.h:510
Manages the elements of a grid and their interconnection.
Definition: grid.h:132
A message sent by the MultiGrid, if something special happened.
Definition: lib_grid_messages.h:51
a helper class that holds a collection of possibly unconnected geometric-objects.
Definition: grid_object_collection.h:96
base-implementation of a selector
Definition: selector_interface.h:126
Grid::traits< Face >::SectionContainer FaceSectionContainer
Definition: selector_interface.h:325
Grid::traits< Edge >::AttachedElementList AttachedEdgeList
Definition: selector_interface.h:319
Grid::traits< Vertex >::AttachedElementList AttachedVertexList
Definition: selector_interface.h:318
Grid::traits< Edge >::SectionContainer EdgeSectionContainer
Definition: selector_interface.h:324
Grid::traits< Face >::AttachedElementList AttachedFaceList
Definition: selector_interface.h:320
Grid::traits< Vertex >::SectionContainer VertexSectionContainer
Definition: selector_interface.h:323
Grid::traits< Volume >::SectionContainer VolumeSectionContainer
Definition: selector_interface.h:326
Grid::traits< Volume >::AttachedElementList AttachedVolumeList
Definition: selector_interface.h:321
Definition: selector_multi_grid.h:118
TElem * value_type
Definition: selector_multi_grid.h:124
size_t difference_type
Definition: selector_multi_grid.h:122
size_t m_lvl
Definition: selector_multi_grid.h:186
std::forward_iterator_tag iterator_category
Definition: selector_multi_grid.h:121
value_type & reference
Definition: selector_multi_grid.h:125
TLevelIterator level_iterator
Definition: selector_multi_grid.h:151
MGSelectionIterator(TMGSelector *sel, int lvl, level_iterator iter)
Definition: selector_multi_grid.h:153
value_type dereference() const
dereference
Definition: selector_multi_grid.h:179
MGSelectionIterator()
Definition: selector_multi_grid.h:127
TMGSelector * m_sel
Definition: selector_multi_grid.h:185
TElem ** pointer
Definition: selector_multi_grid.h:123
MGSelectionIterator this_type
Definition: selector_multi_grid.h:120
level_iterator m_iter
Definition: selector_multi_grid.h:187
MGSelectionIterator(const MGSelectionIterator< TElem, MGSelector, typename geometry_traits< TElem >::iterator > &iter)
copy constructor that allows creation of const-iterators from non-const iterators
Definition: selector_multi_grid.h:130
void increment()
returns next valid iterator
Definition: selector_multi_grid.h:167
bool equal(const this_type &other) const
Definition: selector_multi_grid.h:161
specialization of ISelector for grids of class MultiGrid.
Definition: selector_multi_grid.h:108
MultiGrid * m_pMultiGrid
Copy Constructor not yet implemented!
Definition: selector_multi_grid.h:441
virtual bool contains_faces() const
returns true if the selector contains faces
Definition: selector_multi_grid.h:343
traits< Vertex >::level_iterator vertices_begin(int level)
Definition: selector_multi_grid.h:306
AttachedVertexList::AEntry m_aSharedEntryVRT
Definition: selector_multi_grid.h:453
traits< Volume >::level_iterator volumes_begin(int level)
Definition: selector_multi_grid.h:312
VertexIterator m_tmpVEnd
Definition: selector_multi_grid.h:447
AttachedFaceList::AEntry m_aSharedEntryFACE
Definition: selector_multi_grid.h:455
traits< Edge >::level_iterator edges_begin(int level)
Definition: selector_multi_grid.h:308
EdgeSectionContainer::iterator get_level_iterator(Edge *o)
returns the iterator at which the given element lies in the section container
Definition: selector_multi_grid.h:413
VertexSectionContainer::iterator get_level_iterator(Vertex *o)
returns the iterator at which the given element lies in the section container
Definition: selector_multi_grid.h:405
size_t top_level() const
Definition: selector_multi_grid.h:231
LevelVec m_levels
Definition: selector_multi_grid.h:445
traits< Vertex >::level_iterator vertices_end(int level)
Definition: selector_multi_grid.h:307
std::vector< Level * > LevelVec
Definition: selector_multi_grid.h:378
traits< Face >::level_iterator faces_begin(int level)
Definition: selector_multi_grid.h:310
MessageHub::SPCallbackId m_callbackId
Definition: selector_multi_grid.h:450
traits< Edge >::level_iterator edges_end(int level)
Definition: selector_multi_grid.h:309
FaceSectionContainer::iterator get_level_iterator(Face *o)
returns the iterator at which the given element lies in the section container
Definition: selector_multi_grid.h:421
AttachedVolumeList::AEntry m_aSharedEntryVOL
Definition: selector_multi_grid.h:456
traits< Volume >::level_iterator volumes_end(int level)
Definition: selector_multi_grid.h:313
MGSelector(const MGSelector &sel)
Definition: selector_multi_grid.h:441
traits< Face >::level_iterator faces_end(int level)
Definition: selector_multi_grid.h:311
MultiGrid grid_type
Definition: selector_multi_grid.h:111
virtual bool contains_vertices() const
returns true if the selector contains vertices
Definition: selector_multi_grid.h:337
VertexIterator m_tmpVBegin
Definition: selector_multi_grid.h:446
AttachedEdgeList::AEntry m_aSharedEntryEDGE
Definition: selector_multi_grid.h:454
virtual bool contains_volumes() const
returns true if the selector contains volumes
Definition: selector_multi_grid.h:346
size_t num_levels() const
Definition: selector_multi_grid.h:230
MultiGrid * multi_grid()
Definition: selector_multi_grid.h:209
virtual bool contains_edges() const
returns true if the selector contains edges
Definition: selector_multi_grid.h:340
VolumeSectionContainer::iterator get_level_iterator(Volume *o)
returns the iterator at which the given element lies in the section container
Definition: selector_multi_grid.h:429
ISelector BaseClass
Definition: selector_multi_grid.h:110
Definition: multi_grid.h:72
Base-class for all vertex-types.
Definition: grid_base_objects.h:231
Volumes are 3-dimensional objects.
Definition: grid_base_objects.h:754
Definition: grid_base_object_traits.h:68
@ SE_ALL
Definition: selector_interface.h:63
#define UG_API
Definition: ug_config.h:65
unsigned int uint
Definition: types.h:114
bool operator==(const MathVector< N, T > &v, const MathVector< N, T > &w)
Definition: math_vector.h:523
bool operator!=(const MathVector< N, T > &v, const MathVector< N, T > &w)
Definition: math_vector.h:552
the ug namespace
ElementStorage< Vertex >::SectionContainer::iterator VertexIterator
This Iterator will be used as base-class for iterators of specialized geometric objects.
Definition: grid_base_object_traits.h:73
MatVec_Expression< L, R > operator*(const AlphaMat_Expression< L > &l, const R &r)
create a MatVec_Expression by (alpha*MATRIX) * VECTOR
Definition: template_expressions.h:223
ReferenceObjectID operator++(ReferenceObjectID &roid, int)
Definition: grid_base_objects.h:89
Definition: selector_multi_grid.h:372
FaceSectionContainer m_faces
Definition: selector_multi_grid.h:375
VolumeSectionContainer m_volumes
Definition: selector_multi_grid.h:376
VertexSectionContainer m_vertices
Definition: selector_multi_grid.h:373
EdgeSectionContainer m_edges
Definition: selector_multi_grid.h:374
The traits class holds some important types for each element-type.
Definition: selector_multi_grid.h:192
geometry_traits< TElem >::const_iterator const_level_iterator
Definition: selector_multi_grid.h:195
TElem * value_t
Definition: selector_multi_grid.h:193
MGSelectionIterator< TElem, MGSelector, level_iterator > iterator
Definition: selector_multi_grid.h:197
MGSelectionIterator< TElem, const MGSelector, const_level_iterator > const_iterator
Definition: selector_multi_grid.h:199
geometry_traits< TElem >::iterator level_iterator
Definition: selector_multi_grid.h:194