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