dune-pdelab  2.5-dev
directleaflocalordering.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 
4 #ifndef DUNE_PDELAB_ORDERING_DIRECTLEAFLOCALORDERING_HH
5 #define DUNE_PDELAB_ORDERING_DIRECTLEAFLOCALORDERING_HH
6 
7 #include <dune/typetree/leafnode.hh>
8 
9 #include <dune/geometry/referenceelements.hh>
10 #include <dune/localfunctions/common/interfaceswitch.hh>
11 #include <dune/localfunctions/common/localkey.hh>
14 
15 #include <vector>
16 #include <numeric>
17 
18 namespace Dune {
19  namespace PDELab {
20 
23 
24  template<typename OrderingTag, typename FEM, typename ES, typename DI, typename CI>
26  : public TypeTree::LeafNode
27  {
28 
29  template<typename>
30  friend class LeafGridViewOrdering;
31 
32  template<typename>
33  friend class LeafOrderingBase;
34 
35  template<typename size_type>
36  friend struct ::Dune::PDELab::impl::update_ordering_data;
37 
38  public:
39 
41 
42  private:
43 
44  typedef impl::GridFunctionSpaceOrderingData<typename Traits::SizeType> GFSData;
45 
46  public:
47 
48  void map_local_index(const typename Traits::SizeType geometry_type_index,
49  const typename Traits::SizeType entity_index,
50  typename Traits::TreeIndexView mi,
51  typename Traits::ContainerIndex& ci) const
52  {
53  DUNE_THROW(NotImplemented,"not implemented");
54  }
55 
56  template<typename ItIn, typename ItOut>
57  void map_lfs_indices(const ItIn begin, const ItIn end, ItOut out) const
58  {
59  // don't do anything - this is handled by the specialized GridViewOrdering
60  }
61 
62  template<typename CIOutIterator, typename DIOutIterator = DummyDOFIndexIterator>
63  typename Traits::SizeType
64  extract_entity_indices(const typename Traits::DOFIndex::EntityIndex& ei,
65  typename Traits::SizeType child_index,
66  CIOutIterator ci_out, const CIOutIterator ci_end,
67  DIOutIterator di_out = DIOutIterator()) const
68  {
69  const typename Traits::SizeType s = size(ei);
70 
71  // Handle DOF indices
72  for (typename Traits::SizeType i = 0; i < s; ++i, ++di_out)
73  di_out->treeIndex().push_back(i);
74 
75  // only return the size, as the tree visitor expects that from all leaf nodes.
76  // The actual index processing is done by the specialized GridViewOrdering.
77  return s;
78  }
79 
80  typename Traits::SizeType size(const typename Traits::DOFIndex::EntityIndex& index) const
81  {
82  return size(
83  Traits::DOFIndexAccessor::GeometryIndex::geometryType(index),
84  Traits::DOFIndexAccessor::GeometryIndex::entityIndex(index)
85  );
86  }
87 
88  typename Traits::SizeType size(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index) const
89  {
90  typedef typename Traits::SizeType size_type;
91  if (_fixed_size)
92  return _gt_dof_sizes[geometry_type_index];
93  else if (_gt_used[geometry_type_index])
94  {
95  const size_type index = _gt_entity_offsets[geometry_type_index] + entity_index;
96  return _entity_dof_offsets[index+1] - _entity_dof_offsets[index];
97  }
98  else
99  return 0;
100  }
101 
102  typename Traits::SizeType size(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index, const typename Traits::SizeType child_index) const
103  {
104  DUNE_THROW(NotImplemented,"not implemented");
105  }
106 
107  typename Traits::SizeType offset(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index, const typename Traits::SizeType child_index) const
108  {
109  assert(child_index == 0);
110  return 0;
111  }
112 
113  DirectLeafLocalOrdering(const shared_ptr<const FEM>& fem, const ES& es)
114  : _fem(fem)
115  , _es(es)
116  , _fixed_size(false)
117  , _container_blocked(false)
118  , _gfs_data(nullptr)
119  {}
120 
121  const typename Traits::EntitySet& entitySet() const
122  {
123  return _es;
124  }
125 
126  const FEM& finiteElementMap() const
127  {
128  return *_fem;
129  }
130 
131  private:
132 
133  typedef FiniteElementInterfaceSwitch<
134  typename FEM::Traits::FiniteElement
135  > FESwitch;
136 
137 
138  void update_a_priori_fixed_size()
139  {
140  _fixed_size = _fem->fixedSize();
141  }
142 
143  template<typename CodimMask>
144  void collect_used_codims(CodimMask& codims) const
145  {
146  for (typename ES::dim_type codim = 0; codim <= ES::dimension; ++codim)
147  if (_fem->hasDOFs(codim))
148  codims.set(codim);
149  }
150 
151  template<typename It>
152  void update_fixed_size(It it, const It end)
153  {
154  assert(_fixed_size);
155 
156  _max_local_size = _fem->maxLocalSize();
157 
158  typedef typename Traits::SizeType size_type;
159  const size_type dim = Traits::GridView::dimension;
160  _codim_used.reset();
161  _gt_used.assign(GlobalGeometryTypeIndex::size(dim),false);
162  _gt_dof_sizes.assign(GlobalGeometryTypeIndex::size(dim),0);
163  for (; it != end; ++it)
164  {
165  size_type size = _fem->size(*it);
166  _gt_dof_sizes[GlobalGeometryTypeIndex::index(*it)] = size;
167  _gt_used[GlobalGeometryTypeIndex::index(*it)] = size > 0;
168  _codim_used[dim - it->dim()] = _codim_used[dim - it->dim()] || (size > 0);
169  }
170 
171  _codim_fixed_size.set();
172  }
173 
174 
175  void pre_collect_used_geometry_types_from_cell()
176  {
177  typedef typename Traits::SizeType size_type;
178  const size_type dim = Traits::GridView::dimension;
179 
180  _codim_used.reset();
181  _gt_used.assign(GlobalGeometryTypeIndex::size(dim),false);
182  _gt_dof_sizes.assign(GlobalGeometryTypeIndex::size(dim),0);
183  _local_gt_dof_sizes.resize(GlobalGeometryTypeIndex::size(dim));
184  _max_local_size = 0;
185  _fixed_size_possible = true;
186  }
187 
188 
189  void collect_used_geometry_types_from_cell(const typename Traits::GridView::template Codim<0>::Entity& cell)
190  {
191  FESwitch::setStore(_fe_store,_fem->find(cell));
192 
193  const typename FESwitch::Coefficients& coeffs =
194  FESwitch::coefficients(*_fe_store);
195 
196  _max_local_size = std::max(_max_local_size,coeffs.size());
197 
198  const ReferenceElement<typename Traits::GridView::ctype, Traits::GridView::dimension>& ref_el = ReferenceElements<typename Traits::GridView::ctype,Traits::GridView::dimension>::general(cell.type());
199 
200  for (std::size_t i = 0; i < coeffs.size(); ++i)
201  {
202  const LocalKey& key = coeffs.localKey(i);
203  GeometryType gt = ref_el.type(key.subEntity(),key.codim());
204  _gt_used[GlobalGeometryTypeIndex::index(gt)] = true;
205  _codim_used.set(key.codim());
206  }
207  }
208 
209 
210  template<typename It>
211  void allocate_entity_offset_vector(It it, const It end)
212  {
213  _gt_entity_offsets.assign(GlobalGeometryTypeIndex::size(ES::dimension) + 1,0);
214  for (; it != end; ++it)
215  {
216  if (_gt_used[GlobalGeometryTypeIndex::index(*it)])
217  _gt_entity_offsets[GlobalGeometryTypeIndex::index(*it) + 1] = _es.indexSet().size(*it);
218  }
219  std::partial_sum(_gt_entity_offsets.begin(),_gt_entity_offsets.end(),_gt_entity_offsets.begin());
220  _entity_dof_offsets.assign(_gt_entity_offsets.back() + 1,0);
221 
222  // Don't claim fixed size for any codim for now
223  _codim_fixed_size.reset();
224  }
225 
226 
227  void extract_per_entity_sizes_from_cell(const typename Traits::GridView::template Codim<0>::Entity& cell)
228  {
229  if (this->_fixed_size_possible)
230  std::fill(_local_gt_dof_sizes.begin(),_local_gt_dof_sizes.end(),0);
231 
232  FESwitch::setStore(_fe_store,_fem->find(cell));
233 
234  const typename FESwitch::Coefficients& coeffs =
235  FESwitch::coefficients(*_fe_store);
236 
237  typedef typename Traits::SizeType size_type;
238 
239  const ReferenceElement<typename Traits::GridView::ctype,Traits::GridView::dimension>& ref_el =
240  ReferenceElements<typename Traits::GridView::ctype,Traits::GridView::dimension>::general(cell.type());
241 
242  for (std::size_t i = 0; i < coeffs.size(); ++i)
243  {
244  const LocalKey& key = coeffs.localKey(i);
245  GeometryType gt = ref_el.type(key.subEntity(),key.codim());
246  const size_type geometry_type_index = GlobalGeometryTypeIndex::index(gt);
247 
248  const size_type entity_index = _es.indexSet().subIndex(cell,key.subEntity(),key.codim());
249  const size_type index = _gt_entity_offsets[geometry_type_index] + entity_index;
250  _local_gt_dof_sizes[geometry_type_index] = _entity_dof_offsets[index+1] = std::max(_entity_dof_offsets[index+1],static_cast<size_type>(key.index() + 1));
251  }
252 
254  {
255  for (size_type i = 0; i < _local_gt_dof_sizes.size(); ++i)
256  {
257  if (_local_gt_dof_sizes[i] > 0)
258  {
259  if (_gt_dof_sizes[i] == 0)
261  else if (_gt_dof_sizes[i] != _local_gt_dof_sizes[i])
262  {
263  _fixed_size_possible = false;
264  break;
265  }
266  }
267  else
268  {
269  // catch special case where some grid entities sometimes don't have any DOFs associated with
270  // them, but are const size if there are any DOFs, i.e. a pattern of #(DOFs) == 0 || #(DOFs) == c > 0
272  }
273  }
274  }
275  }
276 
277 
278  void finalize_non_fixed_size_update()
279  {
281  {
282  // free per-entity offsets
283  _entity_dof_offsets = std::vector<typename Traits::SizeType>();
284  _fixed_size = true;
285  _codim_fixed_size.set();
286  }
287  else
288  {
289  // convert per-entity sizes to offsets
290  std::partial_sum(_entity_dof_offsets.begin(),_entity_dof_offsets.end(),_entity_dof_offsets.begin());
291  _fixed_size = false;
292  _codim_fixed_size.reset();
293  }
294  }
295 
296 
297  typename Traits::SizeType maxLocalSize() const
298  {
299  return _max_local_size;
300  }
301 
302  private:
303 
304  bool update_gfs_data_size(typename Traits::SizeType& size, typename Traits::SizeType& block_count) const
305  {
306  return false;
307  }
308 
309  protected:
310 
311  shared_ptr<const FEM> _fem;
312  typename FESwitch::Store _fe_store;
313 
314  ES _es;
318  const bool _container_blocked;
319 
322  std::vector<bool> _gt_used;
323 
324  std::vector<typename Traits::SizeType> _gt_entity_offsets;
325  std::vector<typename Traits::SizeType> _gt_dof_sizes;
326  std::vector<typename Traits::SizeType> _entity_dof_offsets;
327  std::vector<typename Traits::SizeType> _local_gt_dof_sizes;
328 
329  // This is only here to make the visitor happy that traverses all
330  // Orderings to manipulate the contained GFSData
331  GFSData* _gfs_data;
332 
333  };
334 
336 
337  } // namespace PDELab
338 } // namespace Dune
339 
340 #endif // DUNE_PDELAB_ORDERING_DIRECTLEAFLOCALORDERING_HH
static const int dim
Definition: adaptivity.hh:83
void map_local_index(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index, typename Traits::TreeIndexView mi, typename Traits::ContainerIndex &ci) const
Definition: directleaflocalordering.hh:48
std::bitset< max_dim > CodimFlag
Definition: ordering/utility.hh:194
Traits::CodimFlag _codim_fixed_size
Definition: directleaflocalordering.hh:321
std::vector< typename Traits::SizeType > _gt_entity_offsets
Definition: directleaflocalordering.hh:324
Generic infrastructure for orderings for leaf spaces.
Definition: leaforderingbase.hh:21
Definition: ordering/utility.hh:208
Traits::SizeType size(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index) const
Definition: directleaflocalordering.hh:88
const FEM & finiteElementMap() const
Definition: directleaflocalordering.hh:126
const std::string s
Definition: function.hh:1101
void map_lfs_indices(const ItIn begin, const ItIn end, ItOut out) const
Definition: directleaflocalordering.hh:57
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
Traits::SizeType extract_entity_indices(const typename Traits::DOFIndex::EntityIndex &ei, typename Traits::SizeType child_index, CIOutIterator ci_out, const CIOutIterator ci_end, DIOutIterator di_out=DIOutIterator()) const
Definition: directleaflocalordering.hh:64
FESwitch::Store _fe_store
Definition: directleaflocalordering.hh:312
bool _fixed_size
Definition: directleaflocalordering.hh:315
Gridview ordering for leaf spaces.
Definition: leafgridviewordering.hh:18
Traits::SizeType _max_local_size
Definition: directleaflocalordering.hh:317
ES EntitySet
Definition: ordering/utility.hh:214
const bool _container_blocked
Definition: directleaflocalordering.hh:318
std::vector< typename Traits::SizeType > _gt_dof_sizes
Definition: directleaflocalordering.hh:325
Traits::SizeType offset(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index, const typename Traits::SizeType child_index) const
Definition: directleaflocalordering.hh:107
ES _es
Definition: directleaflocalordering.hh:314
DirectLeafLocalOrdering(const shared_ptr< const FEM > &fem, const ES &es)
Definition: directleaflocalordering.hh:113
DI::size_type SizeType
Definition: ordering/utility.hh:201
GFSData * _gfs_data
Definition: directleaflocalordering.hh:331
CI ContainerIndex
Definition: ordering/utility.hh:160
bool _fixed_size_possible
Definition: directleaflocalordering.hh:316
Definition: gridviewordering.hh:23
Definition: directleaflocalordering.hh:25
DI::View::TreeIndex TreeIndexView
Definition: ordering/utility.hh:199
const Traits::EntitySet & entitySet() const
Definition: directleaflocalordering.hh:121
LocalOrderingTraits< ES, DI, CI > Traits
Definition: directleaflocalordering.hh:40
Definition: gridviewordering.hh:80
Traits::SizeType size(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index, const typename Traits::SizeType child_index) const
Definition: directleaflocalordering.hh:102
Traits::SizeType size(const typename Traits::DOFIndex::EntityIndex &index) const
Definition: directleaflocalordering.hh:80
std::vector< bool > _gt_used
Definition: directleaflocalordering.hh:322
std::vector< typename Traits::SizeType > _local_gt_dof_sizes
Definition: directleaflocalordering.hh:327
std::vector< typename Traits::SizeType > _entity_dof_offsets
Definition: directleaflocalordering.hh:326
shared_ptr< const FEM > _fem
Definition: directleaflocalordering.hh:311
Traits::CodimFlag _codim_used
Definition: directleaflocalordering.hh:320