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;
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);
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  auto 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());
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  {
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  auto ref_el = ReferenceElements<typename Traits::GridView::ctype,Traits::GridView::dimension>::general(cell.type());
240 
241  for (std::size_t i = 0; i < coeffs.size(); ++i)
242  {
243  const LocalKey& key = coeffs.localKey(i);
244  GeometryType gt = ref_el.type(key.subEntity(),key.codim());
245  const size_type geometry_type_index = GlobalGeometryTypeIndex::index(gt);
246 
247  const size_type entity_index = _es.indexSet().subIndex(cell,key.subEntity(),key.codim());
248  const size_type index = _gt_entity_offsets[geometry_type_index] + entity_index;
249  _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));
250  }
251 
253  {
254  for (size_type i = 0; i < _local_gt_dof_sizes.size(); ++i)
255  {
256  if (_local_gt_dof_sizes[i] > 0)
257  {
258  if (_gt_dof_sizes[i] == 0)
260  else if (_gt_dof_sizes[i] != _local_gt_dof_sizes[i])
261  {
262  _fixed_size_possible = false;
263  break;
264  }
265  }
266  else
267  {
268  // catch special case where some grid entities sometimes don't have any DOFs associated with
269  // them, but are const size if there are any DOFs, i.e. a pattern of #(DOFs) == 0 || #(DOFs) == c > 0
271  }
272  }
273  }
274  }
275 
276 
277  void finalize_non_fixed_size_update()
278  {
280  {
281  // free per-entity offsets
282  _entity_dof_offsets = std::vector<typename Traits::SizeType>();
283  _fixed_size = true;
284  _codim_fixed_size.set();
285  }
286  else
287  {
288  // convert per-entity sizes to offsets
289  std::partial_sum(_entity_dof_offsets.begin(),_entity_dof_offsets.end(),_entity_dof_offsets.begin());
290  _fixed_size = false;
291  _codim_fixed_size.reset();
292  }
293  }
294 
295 
296  typename Traits::SizeType maxLocalSize() const
297  {
298  return _max_local_size;
299  }
300 
301  private:
302 
303  bool update_gfs_data_size(typename Traits::SizeType& size, typename Traits::SizeType& block_count) const
304  {
305  return false;
306  }
307 
308  protected:
309 
310  shared_ptr<const FEM> _fem;
311  typename FESwitch::Store _fe_store;
312 
313  ES _es;
317  const bool _container_blocked;
318 
321  std::vector<bool> _gt_used;
322 
323  std::vector<typename Traits::SizeType> _gt_entity_offsets;
324  std::vector<typename Traits::SizeType> _gt_dof_sizes;
325  std::vector<typename Traits::SizeType> _entity_dof_offsets;
326  std::vector<typename Traits::SizeType> _local_gt_dof_sizes;
327 
328  // This is only here to make the visitor happy that traverses all
329  // Orderings to manipulate the contained GFSData
330  GFSData* _gfs_data;
331 
332  };
333 
335 
336  } // namespace PDELab
337 } // namespace Dune
338 
339 #endif // DUNE_PDELAB_ORDERING_DIRECTLEAFLOCALORDERING_HH
Dune::PDELab::DirectLeafLocalOrdering
Definition: directleaflocalordering.hh:25
Dune::PDELab::DirectLeafLocalOrdering::_codim_fixed_size
Traits::CodimFlag _codim_fixed_size
Definition: directleaflocalordering.hh:320
Dune::PDELab::LeafGridViewOrdering
Gridview ordering for leaf spaces.
Definition: leafgridviewordering.hh:18
Dune::PDELab::DirectLeafLocalOrdering::_codim_used
Traits::CodimFlag _codim_used
Definition: directleaflocalordering.hh:319
Dune::PDELab::DirectLeafLocalOrdering::finiteElementMap
const FEM & finiteElementMap() const
Definition: directleaflocalordering.hh:126
Dune::PDELab::DirectLeafLocalOrdering::map_lfs_indices
void map_lfs_indices(const ItIn begin, const ItIn end, ItOut out) const
Definition: directleaflocalordering.hh:57
Dune::PDELab::DirectLeafLocalOrdering::_es
ES _es
Definition: directleaflocalordering.hh:313
Dune::PDELab::DirectLeafLocalOrdering::_max_local_size
Traits::SizeType _max_local_size
Definition: directleaflocalordering.hh:316
Dune::PDELab::OrderingTraits::CodimFlag
std::bitset< max_dim > CodimFlag
Definition: ordering/utility.hh:194
dim
static const int dim
Definition: adaptivity.hh:84
index
std::size_t index
Definition: interpolate.hh:118
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::DirectLeafLocalOrdering::_gt_used
std::vector< bool > _gt_used
Definition: directleaflocalordering.hh:321
Dune::PDELab::DirectLeafLocalOrdering::_fe_store
FESwitch::Store _fe_store
Definition: directleaflocalordering.hh:311
Dune::PDELab::DirectLeafLocalOrdering::extract_entity_indices
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
Dune::PDELab::DirectLeafLocalOrdering::offset
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
Dune::PDELab::DirectLeafLocalOrdering::_gt_dof_sizes
std::vector< typename Traits::SizeType > _gt_dof_sizes
Definition: directleaflocalordering.hh:324
gridfunctionspacebase.hh
Dune::PDELab::DirectLeafLocalOrdering::_fixed_size_possible
bool _fixed_size_possible
Definition: directleaflocalordering.hh:315
Dune::PDELab::LocalOrderingTraits
Definition: ordering/utility.hh:208
Dune::PDELab::LeafOrderingBase
Generic infrastructure for orderings for leaf spaces.
Definition: leaforderingbase.hh:21
utility.hh
Dune::PDELab::DirectLeafLocalOrdering::map_local_index
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
Dune::PDELab::DirectLeafLocalOrdering::_local_gt_dof_sizes
std::vector< typename Traits::SizeType > _local_gt_dof_sizes
Definition: directleaflocalordering.hh:326
Dune::PDELab::DirectLeafLocalOrdering::_entity_dof_offsets
std::vector< typename Traits::SizeType > _entity_dof_offsets
Definition: directleaflocalordering.hh:325
Dune::PDELab::DirectLeafLocalOrdering::size
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
Dune::PDELab::OrderingTraits::SizeType
DI::size_type SizeType
Definition: ordering/utility.hh:201
Dune::PDELab::DirectLeafLocalOrdering::size
Traits::SizeType size(const typename Traits::DOFIndex::EntityIndex &index) const
Definition: directleaflocalordering.hh:80
Dune::PDELab::DirectLeafLocalOrdering::_fem
shared_ptr< const FEM > _fem
Definition: directleaflocalordering.hh:310
Dune::PDELab::DirectLeafLocalOrdering::DirectLeafLocalOrdering
DirectLeafLocalOrdering(const shared_ptr< const FEM > &fem, const ES &es)
Definition: directleaflocalordering.hh:113
Dune::PDELab::OrderingTraits::TreeIndexView
DI::View::TreeIndex TreeIndexView
Definition: ordering/utility.hh:199
Dune::PDELab::DirectLeafLocalOrdering::_gt_entity_offsets
std::vector< typename Traits::SizeType > _gt_entity_offsets
Definition: directleaflocalordering.hh:323
Dune::PDELab::DirectLeafLocalOrdering::size
Traits::SizeType size(const typename Traits::SizeType geometry_type_index, const typename Traits::SizeType entity_index) const
Definition: directleaflocalordering.hh:88
s
const std::string s
Definition: function.hh:830
Dune::PDELab::DirectLeafLocalOrdering::_container_blocked
const bool _container_blocked
Definition: directleaflocalordering.hh:317
Dune::PDELab::SimpleOrderingTraits::SizeType
std::size_t SizeType
Definition: ordering/utility.hh:162
Dune::PDELab::DirectLeafLocalOrdering::entitySet
const Traits::EntitySet & entitySet() const
Definition: directleaflocalordering.hh:121
Dune::PDELab::DirectLeafLocalOrdering::Traits
LocalOrderingTraits< ES, DI, CI > Traits
Definition: directleaflocalordering.hh:40
Dune::PDELab::LocalOrderingTraits::EntitySet
ES EntitySet
Definition: ordering/utility.hh:214
Dune::PDELab::DirectLeafLocalOrdering::_gfs_data
GFSData * _gfs_data
Definition: directleaflocalordering.hh:330
Dune::PDELab::SimpleOrderingTraits::ContainerIndex
CI ContainerIndex
Definition: ordering/utility.hh:160
Dune::PDELab::DirectLeafLocalOrdering::_fixed_size
bool _fixed_size
Definition: directleaflocalordering.hh:314