dune-pdelab  2.4.1
gridfunctionspacebase.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_GRIDFUNCTIONSPACEBASE_HH
4 #define DUNE_PDELAB_GRIDFUNCTIONSPACE_GRIDFUNCTIONSPACEBASE_HH
5 
6 #include <dune/typetree/visitor.hh>
7 #include <dune/typetree/traversal.hh>
8 
10 
11 namespace Dune {
12  namespace PDELab {
13 
17 
18 #ifndef DOXYGEN
19 
20  // forward declaration for friend declaration
21  template<typename GFS, typename GFSTraits>
22  class GridFunctionSpaceBase;
23 
24  namespace impl {
25 
26  struct reset_root_space_flag
27  : public TypeTree::DirectChildrenVisitor
28  , public TypeTree::DynamicTraversal
29  {
30 
31  template<typename GFS, typename Child, typename TreePath, typename ChildIndex>
32  void afterChild(const GFS& gfs, Child& child, TreePath, ChildIndex) const
33  {
34  if (child._initialized && child._is_root_space)
35  {
36  DUNE_THROW(GridFunctionSpaceHierarchyError,"initialized space cannot become part of larger GridFunctionSpace tree");
37  }
38  child._is_root_space = false;
39  }
40 
41  };
42 
43  template<typename size_type>
44  struct update_ordering_data;
45 
46  // helper class with minimal dependencies. Orderings keep a pointer to this structure and populate it
47  // during their update procedure.
48 
49  template<typename size_type>
50  class GridFunctionSpaceOrderingData
51  {
52 
53  template<typename,typename>
54  friend class ::Dune::PDELab::GridFunctionSpaceBase;
55 
56  template<typename>
57  friend struct update_ordering_data;
58 
59  GridFunctionSpaceOrderingData()
60  : _size(0)
61  , _block_count(0)
62  , _global_size(0)
63  , _max_local_size(0)
64  , _is_root_space(true)
65  , _initialized(false)
66  , _size_available(true)
67  {}
68 
69  size_type _size;
70  size_type _block_count;
71  size_type _global_size;
72  size_type _max_local_size;
73  bool _is_root_space;
74  bool _initialized;
75  bool _size_available;
76 
77  };
78 
79  template<typename size_type>
80  struct update_ordering_data
81  : public TypeTree::TreeVisitor
82  , public TypeTree::DynamicTraversal
83  {
84 
85  typedef GridFunctionSpaceOrderingData<size_type> Data;
86 
87  template<typename Ordering>
88  void update(const Ordering& ordering, bool is_root)
89  {
90  if (ordering._gfs_data)
91  {
92  Data& data = *ordering._gfs_data;
93  // if (data._initialized && data._is_root_space && !is_root)
94  // {
95  // DUNE_THROW(GridFunctionSpaceHierarchyError,"former root space is now part of a larger tree");
96  // }
97  data._initialized = true;
98  data._global_size = _global_size;
99  data._max_local_size = _max_local_size;
100  data._size_available = ordering.update_gfs_data_size(data._size,data._block_count);
101  }
102  }
103 
104  template<typename Ordering, typename TreePath>
105  void leaf(const Ordering& ordering, TreePath tp)
106  {
107  update(ordering,tp.size() == 0);
108  }
109 
110  template<typename Ordering, typename TreePath>
111  void post(const Ordering& ordering, TreePath tp)
112  {
113  update(ordering,tp.size() == 0);
114  }
115 
116  template<typename Ordering>
117  explicit update_ordering_data(const Ordering& ordering)
118  : _global_size(ordering.size())
119  , _max_local_size(ordering.maxLocalSize())
120  {}
121 
122  const size_type _global_size;
123  const size_type _max_local_size;
124 
125  };
126 
127 
128  } // namespace impl
129 
130 #endif // DOXYGEN
131 
132 
133  template<typename GFS, typename GFSTraits>
135  : public impl::GridFunctionSpaceOrderingData<typename GFSTraits::SizeType>
136  {
137 
138  friend struct impl::reset_root_space_flag;
139 
140  public:
141 
142  typedef GFSTraits Traits;
143 
144  template<typename Backend_, typename OrderingTag_>
145  GridFunctionSpaceBase(Backend_&& backend, OrderingTag_&& ordering_tag)
146  : _backend(std::forward<Backend_>(backend))
147  , _ordering_tag(std::forward<OrderingTag_>(ordering_tag))
148  {
149  TypeTree::applyToTree(gfs(),impl::reset_root_space_flag());
150  }
151 
152  typename Traits::SizeType size() const
153  {
154  if (!_initialized)
155  {
156  DUNE_THROW(UninitializedGridFunctionSpaceError,"space is not initialized");
157  }
158  if (!_size_available)
159  {
161  "Size cannot be calculated at this point in the GFS tree.");
162  }
163  return _size;
164  }
165 
166  typename Traits::SizeType blockCount() const
167  {
168  if (!_initialized)
169  {
170  DUNE_THROW(UninitializedGridFunctionSpaceError,"space is not initialized");
171  }
172  if (!_size_available)
173  {
175  "Block count cannot be calculated at this point in the GFS tree.");
176  }
177  return _block_count;
178  }
179 
180  typename Traits::SizeType globalSize() const
181  {
182  if (!_initialized)
183  {
184  DUNE_THROW(UninitializedGridFunctionSpaceError,"space is not initialized");
185  }
186  return _global_size;
187  }
188 
190  typename Traits::SizeType maxLocalSize () const
191  {
192  if (!_initialized)
193  {
194  DUNE_THROW(UninitializedGridFunctionSpaceError,"space is not initialized");
195  }
196  return _max_local_size;
197  }
198 
200 
205  void update(bool force = false)
206  {
207  std::cout << "Updating entity set" << std::endl;
208  auto entity_set = gfs().entitySet();
209  entity_set.update(force);
210  // We bypass the normal access using ordering() here to avoid a double
211  // update if the Ordering has not been created yet.
212  if (!gfs()._ordering)
213  gfs().create_ordering();
214  update(*gfs()._ordering);
215  }
216 
217  const std::string& name() const
218  {
219  return _name;
220  }
221 
222  void name(const std::string& name)
223  {
224  _name = name;
225  }
226 
228  {
229  return _backend;
230  }
231 
232  const typename Traits::Backend& backend() const
233  {
234  return _backend;
235  }
236 
238  {
239  return _ordering_tag;
240  }
241 
242  const typename Traits::OrderingTag& orderingTag() const
243  {
244  return _ordering_tag;
245  }
246 
247  bool isRootSpace() const
248  {
249  return _is_root_space;
250  }
251 
252  protected:
253 
254  template<typename Ordering>
255  void update(Ordering& ordering) const
256  {
257  if (!_is_root_space)
258  {
259  DUNE_THROW(GridFunctionSpaceHierarchyError,"update() may only be called on the root of the function space hierarchy");
260  }
261  ordering.update();
262  TypeTree::applyToTree(ordering,impl::update_ordering_data<typename Traits::SizeType>(ordering));
263  }
264 
265  private:
266 
267  typedef impl::GridFunctionSpaceOrderingData<typename GFSTraits::SizeType> BaseT;
268 
269  GFS& gfs()
270  {
271  return static_cast<GFS&>(*this);
272  }
273 
274  const GFS& gfs() const
275  {
276  return static_cast<const GFS&>(*this);
277  }
278 
279  std::string _name;
280  typename Traits::Backend _backend;
281  typename Traits::OrderingTag _ordering_tag;
282 
283  using BaseT::_size;
284  using BaseT::_block_count;
285  using BaseT::_global_size;
286  using BaseT::_max_local_size;
287  using BaseT::_is_root_space;
288  using BaseT::_initialized;
289  using BaseT::_size_available;
290 
291  };
292 
293 
294  } // namespace PDELab
295 } // namespace Dune
296 
297 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_GRIDFUNCTIONSPACEBASE_HH
GridFunctionSpaceBase(Backend_ &&backend, OrderingTag_ &&ordering_tag)
Definition: gridfunctionspacebase.hh:145
Traits::Backend & backend()
Definition: gridfunctionspacebase.hh:227
STL namespace.
const std::string & name() const
Definition: gridfunctionspacebase.hh:217
GFSTraits Traits
Definition: gridfunctionspacebase.hh:142
Traits::SizeType maxLocalSize() const
get max dimension of shape function space
Definition: gridfunctionspacebase.hh:190
void name(const std::string &name)
Definition: gridfunctionspacebase.hh:222
Traits::SizeType size() const
Definition: gridfunctionspacebase.hh:152
Definition: adaptivity.hh:27
B::size_type SizeType
short cut for size type exported by Backend
Definition: powercompositegridfunctionspacebase.hh:63
Called a GridFunctionSpace method that requires initialization of the space.
Definition: exceptions.hh:28
Traits::SizeType blockCount() const
Definition: gridfunctionspacebase.hh:166
Traits::SizeType globalSize() const
Definition: gridfunctionspacebase.hh:180
void update(Ordering &ordering) const
Definition: gridfunctionspacebase.hh:255
const Traits::OrderingTag & orderingTag() const
Definition: gridfunctionspacebase.hh:242
Traits::OrderingTag & orderingTag()
Definition: gridfunctionspacebase.hh:237
const Traits::Backend & backend() const
Definition: gridfunctionspacebase.hh:232
Definition: gridfunctionspacebase.hh:134
bool isRootSpace() const
Definition: gridfunctionspacebase.hh:247
PDELab-specific exceptions.
void update(bool force=false)
Update the indexing information of the GridFunctionSpace.
Definition: gridfunctionspacebase.hh:205
O OrderingTag
Definition: powercompositegridfunctionspacebase.hh:60
B Backend
Definition: powercompositegridfunctionspacebase.hh:55