GlobalIdSet.hpp 15.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
#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>

14
#include <amdis/Output.hpp>
15
#include <amdis/common/Apply.hpp>
16
#include <amdis/common/ConceptsBase.hpp>
17
18
19
#include <amdis/common/ForEach.hpp>
#include <amdis/common/TupleUtility.hpp>
#include <amdis/functions/Nodes.hpp>
20
#include <amdis/utility/Twist.hpp>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

namespace Dune
{
  namespace Functions
  {
    // forward declarations...
    template <class GV, int k, class MI>
    class LagrangeDGPreBasis;

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


namespace AMDiS
{
  // forward declaration
39
  template <class PreBasis, class TP, class NodeTag = typename Node_t<PreBasis,TP>::NodeTag>
40
41
  class NodeIdSet;

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
  /// \brief Type of a global used used to represent DOFs uniquely
  /**
   * A global DOF id is a pair an id of the entity the DOF is related to and
   * the local number on that entity. See also LocalBasisKey.
   **/
  template <class EntityIdType>
  class GlobalIdType : public std::pair<EntityIdType, std::size_t>
  {
    using Super = std::pair<EntityIdType, std::size_t>;

  public:
    GlobalIdType(std::size_t i = 0)
      : Super(EntityIdType{}, i)
    {}

    using Super::Super;

    /// Increment the second index only
    GlobalIdType& operator++()
    {
      ++this->second;
      return *this;
    }

    // Add to the second index only
    GlobalIdType operator+(std::size_t shift) const
    {
      return GlobalIdType{this->first, this->second + shift};
    }

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

  template <class Basis>
  using GlobalIdType_t
    = GlobalIdType<typename Basis::GridView::Grid::GlobalIdSet::IdType>;


84
  /// \brief Provide global ids for all DOFs in a global basis
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  /**
   * 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();
    }
   * ```
   *
   **/
105
  template <class GB, class = std::void_t<> >
106
107
108
109
110
111
112
113
114
115
  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;
116
    using EntityIdType = typename Grid::GlobalIdSet::IdType;
117
    using PartitionType = Dune::PartitionType;
118
    using IdType = GlobalIdType_t<GB>;
119

120
121
122
    using PreBasis = typename GlobalBasis::PreBasis;
    using TreePath = typename GlobalBasis::PrefixPath;
    using NodeIdSet = AMDiS::NodeIdSet<PreBasis, TreePath>;
123
    using Twist = AMDiS::Twist<typename Grid::GlobalIdSet, GridView::dimension>;
124
125
126
127
128

  public:
    GlobalBasisIdSet(GlobalBasis const& globalBasis)
      : tree_(makeNode(globalBasis.preBasis(), TreePath{}))
      , nodeIdSet_(globalBasis.gridView())
129
      , twist_(globalBasis.gridView().grid().globalIdSet())
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    {
      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_);
144
      twist_.bind(element);
145
      data_.resize(size());
146
      nodeIdSet_.fillIn(twist_, data_.begin());
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
    }

    /// \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_;
187
    Twist twist_;
188
189
190
191
192
    using Data = std::pair<IdType, PartitionType>;
    std::vector<Data> data_;
  };


193
194
  // Specialization for SubspaceBasis
  template <class Basis>
195
  class GlobalBasisIdSet<Basis, std::void_t<typename Basis::RootBasis>>
196
      : public GlobalBasisIdSet<typename Basis::RootBasis>
197
198
  {
  public:
199
200
    GlobalBasisIdSet(Basis const& basis)
      : GlobalBasisIdSet<typename Basis::RootBasis>(basis.rootBasis())
201
202
203
204
    {}
  };


205
  template <class PB, class TP, class NodeTag>
206
207
208
209
210
211
212
213
  class NodeIdSet
  {
  public:
    using PreBasis = PB;
    using Node = Node_t<PreBasis,TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

214
    static_assert(Node::isLeaf, "Generic NodeIdSet implemented for LeafNodes only. Provide a specialization for your node!");
215
216
217
218
219
220
221

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

  public:
    NodeIdSet(GridView const& gridView)
      : gridView_(gridView)
222
      , sizes_{}
223
224
225
226
227
228
229
    {}

    /// \brief Bind the idset to a tree node
    void bind(const Node& node)
    {
      node_ = &node;
      size_ = node_->finiteElement().size();
230
231
232
233
234
235
236
237
238

      std::fill(std::begin(sizes_), std::end(sizes_), 0u);
      for (size_type i = 0; i < size_ ; ++i) {
        Dune::LocalKey const& localKey = node_->finiteElement().localCoefficients().localKey(i);
        sizes_[localKey.codim()]++;
      }
      auto refElem = Dune::referenceElement<double,GridView::dimension>(node_->element().type());
      for (size_type c = 0; c <= GridView::dimension ; ++c)
        sizes_[c] /= refElem.size(c);
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    }

    /// \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
254
255
    template <class Twist, class It>
    It fillIn(Twist const& twist, It it, size_type shift = 0) const
256
    {
257
      assert(node_ != nullptr);
258
259
260
261
262
263
      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();
264
        it->first = {gridIdSet.subId(node_->element(), s, c), shift + twist.get(localKey,sizes_[c])};
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

        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;
281
    std::array<unsigned int,GridView::dimension+1> sizes_;
282
283
284
  };


285
286
  // Specialization for PowerBasis
  template <class PreBasis, class TP>
287
  class NodeIdSet<PreBasis, TP, Dune::TypeTree::PowerNodeTag>
288
289
290
291
292
293
294
295
296
297
  {
  public:
    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>;
298
    static const std::size_t children = Node::CHILDREN;
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

  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
326
327
    template <class Twist, class It>
    It fillIn(Twist const& twist, It it, size_type shift = 0) const
328
    {
329
      assert(node_ != nullptr);
330
331
332
      for (std::size_t child = 0; child < children; ++child)
      {
        size_type subTreeSize = subIds_.size();
333
        it = subIds_.fillIn(twist, it, shift);
334
335
336
337
338
339
340
341
342
343
344
        shift += subTreeSize;
      }
      return it;
    }

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


345
346
  // Specialization for CompositePreBasis
  template <class PreBasis, class TP>
347
  class NodeIdSet<PreBasis, TP, Dune::TypeTree::CompositeNodeTag>
348
349
350
351
352
353
354
  {
  public:
    using Node = Node_t<PreBasis,TP>;
    using GridView = typename PreBasis::GridView;
    using size_type = std::size_t;

  protected:
355
    static const std::size_t children = Node::CHILDREN;
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
    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
403
404
    template <class Twist, class It>
    It fillIn(Twist const& twist, It it, size_type shift = 0) const
405
    {
406
      assert(node_ != nullptr);
407
408
409
      Tools::for_each(idsTuple_, [&](auto const& ids)
      {
        size_type subTreeSize = ids.size();
410
        it = ids.fillIn(twist, it, shift);
411
412
413
414
415
416
417
418
419
420
421
422
        shift += subTreeSize;
      });
      return it;
    }

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


  template <class GV, class MI, bool HI, class TP>
423
  class NodeIdSet<Dune::Functions::TaylorHoodPreBasis<GV,MI,HI>, TP, Dune::TypeTree::CompositeNodeTag>
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
  {
  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
476
477
    template <class Twist, class It>
    It fillIn(Twist const& twist, It it, size_type shift = 0) const
478
    {
479
      assert(node_ != nullptr);
480
481
      for (int child = 0; child < dow; ++child) {
        size_type subTreeSize = pq2NodeIdSet_.size();
482
        it = pq2NodeIdSet_.fillIn(twist, it, shift);
483
484
        shift += subTreeSize;
      }
485
      it = pq1NodeIdSet_.fillIn(twist, it, shift);
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
522
523
524
525
526
527
528
      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
529
530
    template <class Twist, class It>
    It fillIn(Twist const& /*twist*/, It it, size_type shift = 0) const
531
    {
532
      assert(node_ != nullptr);
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
      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