lagrangedatacollector.hh 6.74 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
#pragma once

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

#include <dune/geometry/referenceelements.hh>
#include <dune/grid/common/partitionset.hh>
9
#include <dune/grid/common/partitionset.hh>
Stenger, Florian's avatar
Stenger, Florian committed
10
#include <dune/vtk/types.hh>
Praetorius, Simon's avatar
Praetorius, Simon committed
11
12
13
14
#include <dune/vtk/utility/lagrangepoints.hh>

#include "unstructureddatacollector.hh"

Stenger, Florian's avatar
Stenger, Florian committed
15
namespace Dune
Praetorius, Simon's avatar
Praetorius, Simon committed
16
{
Stenger, Florian's avatar
Stenger, Florian committed
17
  namespace Vtk
Praetorius, Simon's avatar
Praetorius, Simon committed
18
  {
Stenger, Florian's avatar
Stenger, Florian committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    /// 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:
33
      LagrangeDataCollector (GridView const& gridView, int order = ORDER)
Stenger, Florian's avatar
Stenger, Florian committed
34
35
36
37
38
39
        : Super(gridView)
        , order_(order)
      {
        assert(order > 0 && "Order 0 not supported");
        assert(ORDER < 0 || order == ORDER);
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
40

Stenger, Florian's avatar
Stenger, Florian committed
41
42
43
44
      /// Construct the point sets
      void updateImpl ()
      {
        auto const& indexSet = gridView_.indexSet();
Praetorius, Simon's avatar
Praetorius, Simon committed
45

Stenger, Florian's avatar
Stenger, Florian committed
46
47
48
        pointSets_.clear();
        for (auto gt : indexSet.types(0))
          pointSets_.emplace(gt, order_);
Praetorius, Simon's avatar
Praetorius, Simon committed
49

Stenger, Florian's avatar
Stenger, Florian committed
50
51
52
53
54
55
56
57
58
        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);
        }
Praetorius, Simon's avatar
Praetorius, Simon committed
59
60
      }

Stenger, Florian's avatar
Stenger, Florian committed
61
62
63
64
      /// Return number of lagrange nodes
      std::uint64_t numPointsImpl () const
      {
        return numPoints_;
Praetorius, Simon's avatar
Praetorius, Simon committed
65
66
      }

Stenger, Florian's avatar
Stenger, Florian committed
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
      /// 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;
      }
Praetorius, Simon's avatar
Praetorius, Simon committed
107

Stenger, Florian's avatar
Stenger, Florian committed
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
      /// 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;
Praetorius, Simon's avatar
Praetorius, Simon committed
185
186
      }

Stenger, Florian's avatar
Stenger, Florian committed
187
188
    protected:
      using Super::gridView_;
Praetorius, Simon's avatar
Praetorius, Simon committed
189

Stenger, Florian's avatar
Stenger, Florian committed
190
191
      unsigned int order_;
      std::uint64_t numPoints_ = 0;
Praetorius, Simon's avatar
Praetorius, Simon committed
192

Stenger, Florian's avatar
Stenger, Florian committed
193
194
195
      using PointSet = LagrangePointSet<typename GridView::ctype, GridView::dimension>;
      std::map<GeometryType, PointSet> pointSets_;
    };
Praetorius, Simon's avatar
Praetorius, Simon committed
196

Stenger, Florian's avatar
Stenger, Florian committed
197
  } // end namespace Vtk
Praetorius, Simon's avatar
Praetorius, Simon committed
198
} // end namespace Dune