lagrangedatacollector.hh 6.14 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
#pragma once

#include <cassert>
#include <map>
#include <vector>

#include <dune/geometry/referenceelements.hh>
#include <dune/grid/common/partitionset.hh>

#include <dune/vtk/forward.hh>
#include <dune/vtk/vtktypes.hh>
#include <dune/vtk/utility/lagrangepoints.hh>

#include "unstructureddatacollector.hh"

namespace Dune {

/// Implementation of \ref DataCollector for lagrange cells
template <class GridView, int ORDER = -1>
class LagrangeDataCollector
    : public UnstructuredDataCollectorInterface<GridView, LagrangeDataCollector<GridView,ORDER>, Partitions::All>
{
  using Self = LagrangeDataCollector;
  using Super = UnstructuredDataCollectorInterface<GridView, Self, Partitions::All>;

public:
  static_assert(ORDER != 0, "Order 0 not supported");
  using Super::dim;
  using Super::partition; // NOTE: lagrange data-collector currently implemented for the All partition only

public:
  LagrangeDataCollector (GridView const& gridView, int order = (ORDER < 0 ? 2 : ORDER))
    : Super(gridView)
    , order_(order)
  {
    assert(order > 0 && "Order 0 not supported");
    assert(ORDER < 0 || order == ORDER);
  }

  /// Construct the point sets
  void updateImpl ()
  {
    auto const& indexSet = gridView_.indexSet();

    pointSets_.clear();
    for (auto gt : indexSet.types(0))
      pointSets_.emplace(gt, order_);

    for (auto& pointSet : pointSets_)
      pointSet.second.build(pointSet.first);

    numPoints_ = indexSet.size(dim);
    for (auto const& pointSet : pointSets_) {
      auto gt = pointSet.first;
      auto refElem = referenceElement<double,dim>(gt);
      numPoints_ += (pointSet.second.size() - refElem.size(dim)) * indexSet.size(gt);
    }
  }

  /// Return number of lagrange nodes
  std::uint64_t numPointsImpl () const
  {
    return numPoints_;
  }

  /// Return a vector of point coordinates.
  /**
   * The vector of point coordinates is composed of vertex coordinates first and second
   * edge center coordinates.
   **/
  template <class T>
  std::vector<T> pointsImpl () const
  {
    std::vector<T> data(this->numPoints() * 3);
    auto const& indexSet = gridView_.indexSet();

    std::size_t shift = indexSet.size(dim);

    for (auto const& element : elements(gridView_, partition)) {
      auto geometry = element.geometry();
      auto refElem = referenceElement<T,dim>(element.type());

      auto const& pointSet = pointSets_.at(element.type());
      unsigned int vertexDOFs = refElem.size(dim);
      unsigned int innerDOFs = pointSet.size() - vertexDOFs;

      for (std::size_t i = 0; i < pointSet.size(); ++i) {
        auto const& p = pointSet[i];
        if (i < vertexDOFs)
          assert(p.localKey().codim() == dim);

        auto const& localKey = p.localKey();
        std::size_t idx = 3 * (localKey.codim() == dim
            ? indexSet.subIndex(element, localKey.subEntity(), dim)
            : innerDOFs*indexSet.index(element) + (i - vertexDOFs) + shift);

        auto v = geometry.global(p.point());
        for (std::size_t j = 0; j < v.size(); ++j)
          data[idx + j] = T(v[j]);
        for (std::size_t j = v.size(); j < 3u; ++j)
          data[idx + j] = T(0);
      }
    }
    return data;
  }

  /// Return number of grid cells
  std::uint64_t numCellsImpl () const
  {
    return gridView_.size(0);
  }

  /// \brief Return cell types, offsets, and connectivity. \see Cells
  /**
   * The cell connectivity is composed of cell vertices first and second cell edges,
   * where the indices are grouped [vertex-indices..., (#vertices)+edge-indices...]
   **/
  Cells cellsImpl () const
  {
    Cells cells;
    cells.connectivity.reserve(this->numPoints());
    cells.offsets.reserve(this->numCells());
    cells.types.reserve(this->numCells());

    auto const& indexSet = gridView_.indexSet();
    std::size_t shift = indexSet.size(dim);

    std::int64_t old_o = 0;
    for (auto const& element : elements(gridView_, partition)) {
      auto refElem = referenceElement<double,dim>(element.type());
      Vtk::CellType cellType(element.type(), Vtk::LAGRANGE);

      auto const& pointSet = pointSets_.at(element.type());
      unsigned int vertexDOFs = refElem.size(dim);
      unsigned int innerDOFs = pointSet.size() - vertexDOFs;

      for (std::size_t i = 0; i < pointSet.size(); ++i) {
        auto const& p = pointSet[i];
        auto const& localKey = p.localKey();
        std::size_t idx = (localKey.codim() == dim
            ? indexSet.subIndex(element, localKey.subEntity(), dim)
            : innerDOFs*indexSet.index(element) + (i - vertexDOFs) + shift);
        cells.connectivity.push_back(idx);
      }

      cells.offsets.push_back(old_o += pointSet.size());
      cells.types.push_back(cellType.type());
    }
    return cells;
  }

  /// Evaluate the `fct` at element vertices and edge centers in the same order as the point coords.
  template <class T, class GlobalFunction>
  std::vector<T> pointDataImpl (GlobalFunction const& fct) const
  {
    int nComps = fct.ncomps();
    std::vector<T> data(this->numPoints() * nComps);
    auto const& indexSet = gridView_.indexSet();

    std::size_t shift = indexSet.size(dim);

    auto localFct = localFunction(fct);
    for (auto const& element : elements(gridView_, partition)) {
      localFct.bind(element);
      auto refElem = referenceElement<T,dim>(element.type());

      auto const& pointSet = pointSets_.at(element.type());
      unsigned int vertexDOFs = refElem.size(dim);
      unsigned int innerDOFs = pointSet.size() - vertexDOFs;

      for (std::size_t i = 0; i < pointSet.size(); ++i) {
        auto const& p = pointSet[i];
        auto const& localKey = p.localKey();
        std::size_t idx = nComps * (localKey.codim() == dim
            ? indexSet.subIndex(element, localKey.subEntity(), dim)
            : innerDOFs*indexSet.index(element) + (i - vertexDOFs) + shift);

        for (int comp = 0; comp < nComps; ++comp)
          data[idx + comp] = T(localFct.evaluate(comp, p.point()));
      }
      localFct.unbind();
    }
    return data;
  }

protected:
  using Super::gridView_;

  unsigned int order_;
  std::uint64_t numPoints_ = 0;

  using PointSet = VtkLagrangePointSet<typename GridView::ctype, GridView::dimension>;
  std::map<GeometryType, PointSet> pointSets_;
};

} // end namespace Dune