dune-pdelab  2.5-dev
datahandleprovider.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_DATAHANDLEPROVIDER_HH
4 #define DUNE_PDELAB_GRIDFUNCTIONSPACE_DATAHANDLEPROVIDER_HH
5 
6 #include <vector>
7 #include <stack>
8 
9 #include <dune/common/typetraits.hh>
10 #include <dune/common/reservedvector.hh>
11 #include <dune/typetree/visitor.hh>
12 
14 
15 namespace Dune {
16  namespace PDELab {
17 
18  namespace {
19 
20  template<typename EntityIndex>
21  struct get_size_for_entity
22  : public TypeTree::TreeVisitor
23  , public TypeTree::DynamicTraversal
24  {
25 
26  template<typename Ordering, typename TreePath>
27  void leaf(const Ordering& ordering, TreePath tp)
28  {
29  _size += ordering.size(_entity_index);
30  }
31 
32  get_size_for_entity(const EntityIndex& entity_index)
33  : _size(0)
34  , _entity_index(entity_index)
35  {}
36 
37  std::size_t size() const
38  {
39  return _size;
40  }
41 
42  private:
43 
44  std::size_t _size;
45  const EntityIndex& _entity_index;
46 
47  };
48 
49 
50  template<typename EntityIndex, typename OffsetIterator>
51  struct get_leaf_offsets_for_entity
52  : public TypeTree::TreeVisitor
53  , public TypeTree::DynamicTraversal
54  {
55 
56  template<typename Ordering, typename TreePath>
57  void leaf(const Ordering& ordering, TreePath tp)
58  {
59  *(++_oit) = ordering.size(_entity_index);
60  }
61 
62  get_leaf_offsets_for_entity(const EntityIndex& entity_index, OffsetIterator oit)
63  : _oit(oit)
64  , _entity_index(entity_index)
65  {}
66 
68  OffsetIterator offsetIterator() const
69  {
70  return _oit;
71  }
72 
73  private:
74 
75  OffsetIterator _oit;
76  const EntityIndex& _entity_index;
77 
78  };
79 
80 
81  template<typename DOFIndex, typename ContainerIndex, std::size_t tree_depth, bool map_dof_indices = false>
82  struct indices_for_entity
83  : public TypeTree::TreeVisitor
84  , public TypeTree::DynamicTraversal
85  {
86 
87  typedef std::size_t size_type;
88  typedef typename DOFIndex::EntityIndex EntityIndex;
89  typedef typename std::vector<ContainerIndex>::iterator CIIterator;
90  typedef typename std::conditional<
91  map_dof_indices,
92  typename std::vector<DOFIndex>::iterator,
93  DummyDOFIndexIterator
94  >::type DIIterator;
95 
96 
97  template<typename Ordering, typename Child, typename TreePath, typename ChildIndex>
98  void beforeChild(const Ordering& ordering, const Child& child, TreePath tp, ChildIndex childIndex)
99  {
100  _stack.push(std::make_pair(_ci_it,_di_it));
101  }
102 
103  template<typename Ordering, typename TreePath>
104  void leaf(const Ordering& ordering, TreePath tp)
105  {
106  size_type size = ordering.extract_entity_indices(_entity_index,
107  tp.back(),
108  _ci_it,
109  _ci_end,
110  _di_it);
111 
112  _ci_end += size;
113  _ci_it = _ci_end;
114  _di_end += size;
115  _di_it = _di_end;
116  }
117 
118  template<typename Ordering, typename Child, typename TreePath, typename ChildIndex>
119  void afterChild(const Ordering& ordering, const Child& child, TreePath tp, ChildIndex childIndex)
120  {
121  // pop
122  ordering.extract_entity_indices(_entity_index,
123  childIndex,
124  _stack.top().first,
125  _ci_end);
126 
127  if (Ordering::consume_tree_index)
128  for (DIIterator it = _stack.top().second;
129  it != _di_end;
130  ++it)
131  it->treeIndex().push_back(childIndex);
132 
133  _stack.pop();
134  }
135 
136 
137  indices_for_entity(const EntityIndex& entity_index,
138  CIIterator ci_begin,
139  DIIterator di_begin = DIIterator())
140  : _entity_index(entity_index)
141  , _ci_it(ci_begin)
142  , _ci_end(ci_begin)
143  , _di_it(di_begin)
144  , _di_end(di_begin)
145  {}
146 
147 
148  // Exposed for multidomain support
149  CIIterator ci_end() const
150  {
151  return _ci_end;
152  }
153 
154  // Exposed for multidomain support
155  DIIterator di_end() const
156  {
157  return _di_end;
158  }
159 
160  private:
161 
162  const EntityIndex& _entity_index;
163  CIIterator _ci_it;
164  CIIterator _ci_end;
165  DIIterator _di_it;
166  DIIterator _di_end;
167 
168  std::stack<
169  std::pair<
170  CIIterator,
171  DIIterator
172  >,
173  ReservedVector<
174  std::pair<
175  CIIterator,
176  DIIterator
177  >,
178  tree_depth
179  >
180  > _stack;
181  };
182 
183  } // anonymous namespace
184 
185 
186  template<typename GFS>
188  {
189 
190  public:
191 
192  typedef std::size_t size_type;
193 
194  //------------------------------
195  // generic data handle interface
196  //------------------------------
197 
199  bool dataHandleContains (int codim) const
200  {
201  return gfs().ordering().contains(codim);
202  }
203 
205  bool dataHandleFixedSize (int codim) const
206  {
207  return gfs().ordering().fixedSize(codim);
208  }
209 
211 
225  constexpr bool sendLeafSizes() const
226  {
227  return false;
228  }
229 
234  template<typename Entity>
235  size_type dataHandleSize (const Entity& e) const
236  {
237  typedef typename GFS::Ordering Ordering;
238 
239  typedef typename Ordering::Traits::DOFIndex::EntityIndex EntityIndex;
240  EntityIndex ei;
241 
242  Ordering::Traits::DOFIndexAccessor::GeometryIndex::store(
243  ei,
244  e.type(),
245  gfs().gridView().indexSet().index(e)
246  );
247 
248  get_size_for_entity<EntityIndex> get_size(ei);
249  TypeTree::applyToTree(gfs().ordering(),get_size);
250 
251  return get_size.size();
252  }
253 
254  template<typename V, typename EntityIndex>
255  void setup_dof_indices(V& v, size_type n, const EntityIndex& ei, std::integral_constant<bool,true>) const
256  {
257  v.resize(n);
258  for (typename V::iterator it = v.begin(),
259  endit = v.end();
260  it != endit;
261  ++it)
262  {
263  it->treeIndex().clear();
264  it->entityIndex() = ei;
265  }
266  }
267 
268  template<typename V, typename EntityIndex>
269  void setup_dof_indices(V& v, size_type n, const EntityIndex& ei, std::integral_constant<bool,false>) const
270  {}
271 
272  template<typename V>
273  typename V::iterator dof_indices_begin(V& v, std::integral_constant<bool,true>) const
274  {
275  return v.begin();
276  }
277 
278  template<typename V>
279  DummyDOFIndexIterator dof_indices_begin(V& v, std::integral_constant<bool,false>) const
280  {
281  return DummyDOFIndexIterator();
282  }
283 
285  template<typename Entity, typename ContainerIndex, typename DOFIndex, typename OffsetIterator, bool map_dof_indices>
286  void dataHandleIndices (const Entity& e,
287  std::vector<ContainerIndex>& container_indices,
288  std::vector<DOFIndex>& dof_indices,
289  OffsetIterator oit,
290  std::integral_constant<bool,map_dof_indices> map_dof_indices_value
291  ) const
292  {
293  typedef typename GFS::Ordering Ordering;
294 
296  "dataHandleContainerIndices() called with invalid ContainerIndex type.");
297 
298  typedef typename Ordering::Traits::DOFIndex::EntityIndex EntityIndex;
299  EntityIndex ei;
300 
301  Ordering::Traits::DOFIndexAccessor::GeometryIndex::store(
302  ei,
303  e.type(),
304  gfs().entitySet().indexSet().index(e)
305  );
306 
307  get_leaf_offsets_for_entity<EntityIndex,OffsetIterator> get_offsets(ei,oit);
308  TypeTree::applyToTree(gfs().ordering(),get_offsets);
309  OffsetIterator end_oit = oit + (TypeTree::TreeInfo<Ordering>::leafCount + 1);
310 
311  // convert sizes to offsets - last entry contains total size
312  std::partial_sum(oit,end_oit,oit);
313  size_type size = *(oit + TypeTree::TreeInfo<Ordering>::leafCount);
314 
315  container_indices.resize(size);
316  // Clear index state
317  for (typename std::vector<ContainerIndex>::iterator it = container_indices.begin(),
318  endit = container_indices.end();
319  it != endit;
320  ++it)
321  it->clear();
322 
323  setup_dof_indices(dof_indices,size,ei,map_dof_indices_value);
324 
325  indices_for_entity<
326  DOFIndex,
327  ContainerIndex,
328  TypeTree::TreeInfo<Ordering>::depth,
329  map_dof_indices
330  > extract_indices(ei,container_indices.begin(),dof_indices_begin(dof_indices,map_dof_indices_value));
331  TypeTree::applyToTree(gfs().ordering(),extract_indices);
332 
333  }
334 
335  protected:
336 
337  const GFS& gfs() const
338  {
339  return static_cast<const GFS&>(*this);
340  }
341 
342  };
343 
344  } // namespace PDELab
345 } // namespace Dune
346 
347 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_DATAHANDLEPROVIDER_HH
constexpr bool sendLeafSizes() const
Returns true if the sizes of the leaf orderings in this tree should be sent as part of the communcati...
Definition: datahandleprovider.hh:225
size_type dataHandleSize(const Entity &e) const
Definition: datahandleprovider.hh:235
void dataHandleIndices(const Entity &e, std::vector< ContainerIndex > &container_indices, std::vector< DOFIndex > &dof_indices, OffsetIterator oit, std::integral_constant< bool, map_dof_indices > map_dof_indices_value) const
return vector of global indices associated with the given entity
Definition: datahandleprovider.hh:286
DummyDOFIndexIterator dof_indices_begin(V &v, std::integral_constant< bool, false >) const
Definition: datahandleprovider.hh:279
void setup_dof_indices(V &v, size_type n, const EntityIndex &ei, std::integral_constant< bool, false >) const
Definition: datahandleprovider.hh:269
V::iterator dof_indices_begin(V &v, std::integral_constant< bool, true >) const
Definition: datahandleprovider.hh:273
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
bool dataHandleContains(int codim) const
returns true if data for this codim should be communicated
Definition: datahandleprovider.hh:199
bool dataHandleFixedSize(int codim) const
returns true if size per entity of given dim and codim is a constant
Definition: datahandleprovider.hh:205
const Entity & e
Definition: localfunctionspace.hh:111
std::size_t size_type
Definition: datahandleprovider.hh:192
Dummy iterator type over DOF indices.
Definition: ordering/utility.hh:288
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139
const GFS & gfs() const
Definition: datahandleprovider.hh:337
std::array< T, entity_capacity > EntityIndex
Definition: dofindex.hh:157
Definition: datahandleprovider.hh:187
void setup_dof_indices(V &v, size_type n, const EntityIndex &ei, std::integral_constant< bool, true >) const
Definition: datahandleprovider.hh:255
A multi-index representing a degree of freedom in a GridFunctionSpace.
Definition: dofindex.hh:146