GlobalIdSet.hpp 14.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once

#include <tuple>
#include <utility>
#include <vector>

#include <dune/common/exceptions.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/common/version.hh>
#include <dune/functions/functionspacebases/nodes.hh>
#include <dune/grid/common/gridenums.hh>
#include <dune/localfunctions/common/localkey.hh>

#include <amdis/common/Apply.hpp>
#include <amdis/common/ForEach.hpp>
#include <amdis/common/TupleUtility.hpp>
#include <amdis/functions/Nodes.hpp>
#include <amdis/Output.hpp>

namespace Dune
{
  namespace Functions
  {
    // forward declarations...
    template <class RB, class TP>
    class SubspaceBasis;

    template <class MI, class IMS, class... SPB>
    class CompositePreBasis;

    template <class GV, int k, class MI>
    class LagrangePreBasis;

    template <class MI, class IMS, class SPB, std::size_t C>
    class PowerPreBasis;

    template <class GV, int k, class MI>
    class LagrangeDGPreBasis;

    template<typename GV, class MI, bool HI>
    class TaylorHoodPreBasis;
  }
}


namespace AMDiS
{
  // forward declaration
  template <class PreBasis, class TP>
  class NodeIdSet;

  /// \brief PRovide global ids for all DOFs in a global basis
  /**
   * A GlobalBasisIdSet provides and interface to retrieve unique IDs
   * (over all partitions) for all DOFs in the basis. It is implemented
   * in terms of the \ref NodeIdSet for each basis node that locally
   * on each element calculates a global unique id.
   *
   * **Examples:**
   * ```
    GlobalBasisIdSet<Basis> idSet(basis);
    for (const auto& e : elements(basis.gridView())) {
      idSet.bind(e);
      for (std::size_t i = 0; i < idSet.size(); ++i) {
        auto id = idSet.id(i); // global unique ID of i'th local DOF
        auto pt = idSet.partitionType(i); // partition type of i'th local DOF
      }
      idSet.unbind();
    }
   * ```
   *
   **/
  template <class GB>
  class GlobalBasisIdSet
  {
  public:
    using GlobalBasis = GB;
    using Tree = typename GB::LocalView::Tree;
    using size_type = std::size_t;

    using GridView = typename GlobalBasis::GridView;
    using Grid = typename GridView::Grid;
    using Element = typename GridView::template Codim<0>::Entity;
84
    using EntityIdType = typename Grid::GlobalIdSet::IdType;
85
86
    using PartitionType = Dune::PartitionType;

87
88
89
90
91
92
93
94
95
96
97
98
99
100
    struct IdType : std::pair<EntityIdType, std::size_t>
    {
      using Super = std::pair<EntityIdType, std::size_t>;

      IdType(int i = 0) : Super() {};
      using Super::Super;

      friend std::ostream& operator<<(std::ostream& os, IdType const& id)
      {
        os << "(" << id.first << "," << id.second << ")";
        return os;
      }
    };

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
    using PreBasis = typename GlobalBasis::PreBasis;
    using TreePath = typename GlobalBasis::PrefixPath;
    using NodeIdSet = AMDiS::NodeIdSet<PreBasis, TreePath>;

  public:
    GlobalBasisIdSet(GlobalBasis const& globalBasis)
      : tree_(makeNode(globalBasis.preBasis(), TreePath{}))
      , nodeIdSet_(globalBasis.gridView())
    {
      Dune::Functions::initializeTree(tree_);
    }

    /// \brief Bind the IdSet to a grid element.
    /**
     * Binding the IdSet to an element collects the ids from all DOFs
     * on that element and stores it in a local vector. Thus, the global
     * DOF ids can be extracted using \ref id() afterwards cheaply.
     **/
    void bind(Element const& element)
    {
      Dune::Functions::bindTree(tree_, element);
      nodeIdSet_.bind(tree_);
      data_.resize(size());
      nodeIdSet_.fillIn(data_.begin());
    }

    /// \brief unbind from the element
    void unbind()
    {
      nodeIdSet_.unbind();
    }

    /// \brief The number of DOFs on the current element in the basis tree
    size_type size() const
    {
      return tree_.size();
    }

    /// \brief Return the global unique ID of the `i`th DOF on the
    /// currently bound element in the basis tree.
    IdType id(size_type i) const
    {
      assert( i < data_.size() );
      return data_[i].first;
    }

    /// \brief Return the global unique ID of the entity associated to the
    /// `i`th DOF on the currently bound element in the basis tree.
    EntityIdType entityId(size_type i) const
    {
      return id(i).first;
    }

    /// \brief Return the partition type of the `i`th DOF on the
    /// currently bound element in the basis tree.
    PartitionType partitionType(size_type i) const
    {
      assert( i < data_.size() );
      return data_[i].second;
    }

  protected:
    Tree tree_;
    NodeIdSet nodeIdSet_;
    using Data = std::pair<IdType, PartitionType>;
    std::vector<Data> data_;
  };


  template <class RB, class TP>
  class GlobalBasisIdSet<Dune::Functions::SubspaceBasis<RB,TP>>
      : public GlobalBasisIdSet<RB>
  {
  public:
    GlobalBasisIdSet(Dune::Functions::SubspaceBasis<RB,TP> const& basis)
      : GlobalBasisIdSet<RB>(basis.rootBasis())
    {}
  };


  template <class PB, class TP>
  class NodeIdSet
  {
  public:
    using PreBasis = PB;
    using Node = Node_t<PreBasis,TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

    static_assert(Node::isLeaf, "Generic NodeIdSet implemented for LeafNodes only. Provide a spcialization for your node!");

  private:
    static constexpr int dim = GridView::template Codim<0>::Entity::mydimension;

  public:
    NodeIdSet(GridView const& gridView)
      : gridView_(gridView)
    {}

    /// \brief Bind the idset to a tree node
    void bind(const Node& node)
    {
      node_ = &node;
      size_ = node_->finiteElement().size();
    }

    /// \brief Unbind the idset
    void unbind()
    {
      node_ = nullptr;
    }

    /// \brief Number of DOFs in this node
    size_type size() const
    {
      return size_;
    }

    /// \brief Maps from subtree index set [0..size-1] to a globally unique id in global basis
    // [[expects: node_ != nullptr]]
    template <class It>
    It fillIn(It it, size_type shift = 0) const
    {
      const auto& gridIdSet = gridView_.grid().globalIdSet();

      for (size_type i = 0; i < size_ ; ++i, ++it) {
        Dune::LocalKey localKey = node_->finiteElement().localCoefficients().localKey(i);
        unsigned int s = localKey.subEntity();
        unsigned int c = localKey.codim();
        unsigned int idx = localKey.index();
        if (!(c == GridView::dimension || c == 0 || idx == 0))
          DUNE_THROW(Dune::NotImplemented, "Bases with more then one DoF per subentity are not supported.");

        it->first = {gridIdSet.subId(node_->element(), s, c), shift + idx};

        it->second = Dune::Hybrid::switchCases(std::make_index_sequence<dim+1>{}, c,
          [&](auto codim) { return node_->element().template subEntity<codim>(s).partitionType(); },
          [&]() {
            error_exit("Invalid codimension c = {}", c);
            return Dune::PartitionType{};
          });
      }

      return it;
    }

  protected:
    GridView gridView_;
    const Node* node_ = nullptr;
    size_type size_ = 0;
  };


  template <class MI, class IMS, class SPB, std::size_t C, class TP>
  class NodeIdSet<Dune::Functions::PowerPreBasis<MI,IMS,SPB,C>, TP>
  {
  public:
    using PreBasis = Dune::Functions::PowerPreBasis<MI,IMS,SPB,C>;
    using Node = Node_t<PreBasis,TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

  protected:
    using SubPreBasis = typename PreBasis::SubPreBasis;
    using SubTreePath = decltype(Dune::TypeTree::push_back(std::declval<TP>(), std::size_t(0)));
    using SubNodeIdSet = NodeIdSet<SubPreBasis, SubTreePath>;
    static const std::size_t children = C;

  public:
    NodeIdSet(GridView const& gridView)
      : subIds_(gridView)
    {}

    /// \brief Bind the idset to a tree node
    void bind(const Node& node)
    {
      node_ = &node;
      subIds_.bind(node.child(0));
    }

    /// \brief Unbind the idset
    void unbind()
    {
      node_ = nullptr;
      subIds_.unbind();
    }

    /// \brief Number of DOFs in this node
    size_type size() const
    {
      return node_->size();
    }

    /// \brief Maps from subtree index set [0..size-1] to a globally unique id in global basis
    // [[expects: node_ != nullptr]]
    template <class It>
    It fillIn(It it, size_type shift = 0) const
    {
      for (std::size_t child = 0; child < children; ++child)
      {
        size_type subTreeSize = subIds_.size();
        it = subIds_.fillIn(it, shift);
        shift += subTreeSize;
      }
      return it;
    }

  protected:
    SubNodeIdSet subIds_;
    const Node* node_ = nullptr;
  };


  template <class MI, class IMS, class... SPB, class TP>
  class NodeIdSet<Dune::Functions::CompositePreBasis<MI,IMS,SPB...>, TP>
  {
  public:
    using PreBasis = Dune::Functions::CompositePreBasis<MI,IMS,SPB...>;
    using Node = Node_t<PreBasis,TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

  protected:
    static const std::size_t children = sizeof...(SPB);
    using ChildIndices = std::make_index_sequence<children>;

    // The I'th SubPreBasis
    template <std::size_t I>
    using SubPreBasis = typename PreBasis::template SubPreBasis<I>;

    template <std::size_t I>
    using SubTreePath = decltype(Dune::TypeTree::push_back(std::declval<TP>(), Dune::index_constant<I>{}));

    template <std::size_t I>
    using SubNodeIdSet = NodeIdSet<SubPreBasis<I>, SubTreePath<I>>;

    // A tuple of NodeIdSets for the SubPreBases
    using IdsTuple = IndexMapTuple_t<std::make_index_sequence<children>, SubNodeIdSet>;

  public:
    NodeIdSet(GridView const& gridView)
      : idsTuple_(Tools::apply_indices<children>([&](auto... i) {
          return std::make_tuple(SubNodeIdSet<VALUE(i)>(gridView)...);
          }))
    {}

    /// \brief Bind the idset to a tree node
    void bind(const Node& node)
    {
      node_ = &node;
      Tools::for_range<0,children>([&](auto i) {
        std::get<VALUE(i)>(idsTuple_).bind(node.child(i));
      });
    }

    /// \brief Unbind the idset
    void unbind()
    {
      node_ = nullptr;
      Tools::for_each(idsTuple_, [](auto& ids) {
        ids.unbind();
      });
    }

    /// \brief Number of DOFs in this node
    size_type size() const
    {
      return node_->size();
    }

    /// \brief Maps from subtree index set [0..size-1] to a globally unique id in global basis
    // [[expects: node_ != nullptr]]
    template <class It>
    It fillIn(It it, size_type shift = 0) const
    {
      Tools::for_each(idsTuple_, [&](auto const& ids)
      {
        size_type subTreeSize = ids.size();
        it = ids.fillIn(it, shift);
        shift += subTreeSize;
      });
      return it;
    }

  private:
    IdsTuple idsTuple_;
    const Node* node_ = nullptr;
  };


  template <class GV, class MI, bool HI, class TP>
  class NodeIdSet<Dune::Functions::TaylorHoodPreBasis<GV,MI,HI>, TP>
  {
  public:
    using PreBasis = Dune::Functions::TaylorHoodPreBasis<GV,MI,HI>;
    using Node = Node_t<PreBasis,TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

  private:
    using PTP = decltype(Dune::TypeTree::push_back(std::declval<TP>(), Dune::index_constant<1>{}));
    using VTP = decltype(Dune::TypeTree::push_back(std::declval<TP>(), Dune::index_constant<0>{}));
    using V0TP = decltype(Dune::TypeTree::push_back(std::declval<VTP>(), std::size_t(0)));

    // Note: PreBasis::PQ1PreBasis is private
    using PQ1PreBasis = typename NodeIndexSet_t<PreBasis,TP>::PQ1NodeIndexSet::PreBasis;
    using PQ2PreBasis = typename NodeIndexSet_t<PreBasis,TP>::PQ2NodeIndexSet::PreBasis;

    using PQ1NodeIdSet = NodeIdSet<PQ1PreBasis, PTP>; // pressure
    using PQ2NodeIdSet = NodeIdSet<PQ2PreBasis, V0TP>; // velocity

  private:
    static constexpr int dow = GridView::dimensionworld;

  public:
    NodeIdSet(GridView const& gridView)
      : pq1NodeIdSet_(gridView)
      , pq2NodeIdSet_(gridView)
    {}

    /// \brief Bind the idset to a tree node
    void bind(const Node& node)
    {
      node_ = &node;
      using namespace Dune::Indices;
      pq1NodeIdSet_.bind(node.child(_1));
      pq2NodeIdSet_.bind(node.child(_0, 0));
    }

    /// \brief Unbind the idset
    void unbind()
    {
      pq1NodeIdSet_.unbind();
      pq2NodeIdSet_.unbind();
      node_ = nullptr;
    }

    /// \brief Number of DOFs in this node
    size_type size() const
    {
      return node_->size();
    }

    /// \brief Maps from subtree index set [0..size-1] to a globally unique id in global basis
    // [[expects: node_ != nullptr]]
    template <class It>
    It fillIn(It it, size_type shift = 0) const
    {
      for (int child = 0; child < dow; ++child) {
        size_type subTreeSize = pq2NodeIdSet_.size();
        it = pq2NodeIdSet_.fillIn(it, shift);
        shift += subTreeSize;
      }
      it = pq1NodeIdSet_.fillIn(it, shift);
      return it;
    }

  protected:
    PQ1NodeIdSet pq1NodeIdSet_;
    PQ2NodeIdSet pq2NodeIdSet_;
    const Node* node_ = nullptr;
  };


  template <class GV, int k, class MI, class TP>
  class NodeIdSet<Dune::Functions::LagrangeDGPreBasis<GV, k, MI>, TP>
  {
  public:
    using PreBasis = Dune::Functions::LagrangeDGPreBasis<GV, k, MI>;
    using Node = Node_t<PreBasis, TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

    NodeIdSet(GridView const& gridView)
      : gridView_(gridView)
    {}

    /// \brief Bind the idset to a tree node
    void bind(const Node& node)
    {
      node_ = &node;
      size_ = node_->finiteElement().size();
    }

    /// \brief Unbind the idset
    void unbind()
    {
      node_ = nullptr;
    }

    /// \brief Number of DOFs in this node
    size_type size() const
    {
      return size_;
    }

    /// \brief Maps from subtree index set [0..size-1] to a globally unique id in global basis
    // [[expects: node_ != nullptr]]
    template <class It>
    It fillIn(It it, size_type shift = 0) const
    {
      const auto& gridIdSet = gridView_.grid().globalIdSet();
      auto elementId = gridIdSet.id(node_->element());

      for (size_type i = 0; i < size_; ++i, ++it)
      {
        it->first = {elementId, shift + i};
        it->second = node_->element().partitionType();
      }

      return it;
    }

  protected:
    GridView gridView_;
    const Node* node_ = nullptr;
    size_type size_ = 0;
  };


} // end namespace AMDiS