discontinuousdatacollector.hh 3.66 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
#pragma once

#include <dune/vtk/datacollector.hh>

namespace Dune { namespace experimental
{

/// Implementation of \ref DataCollector for linear cells, with discontinuous data.
template <class GridView>
class DiscontinuousDataCollector
    : public DataCollectorInterface<GridView, DiscontinuousDataCollector<GridView>>
{
  enum { dim = GridView::dimension };

  using Self = DiscontinuousDataCollector;
  using Super = DataCollectorInterface<GridView, Self>;
  using Super::gridView_;

public:
  DiscontinuousDataCollector (GridView const& gridView)
    : Super(gridView)
  {
    this->update();
  }

  /// Create an index map the uniquely assignes an index to each pair (element,corner)
  void updateImpl ()
  {
    numPoints_ = 0;
    indexMap_.resize(gridView_.size(dim));
    std::int64_t vertex_idx = 0;
    auto const& indexSet = gridView_.indexSet();
33
    for (auto const& c : elements(gridView_, Partitions::interior)) {
Praetorius, Simon's avatar
Praetorius, Simon committed
34
      numPoints_ += c.subEntities(dim);
35
      for (unsigned int i = 0; i < c.subEntities(dim); ++i)
Praetorius, Simon's avatar
Praetorius, Simon committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        indexMap_[indexSet.subIndex(c, i, dim)] = vertex_idx++;
    }
  }

  /// The number of pointsi approx. #cell * #corners-per-cell
  std::uint64_t numPointsImpl () const
  {
    return numPoints_;
  }

  /// Return the coordinates of the corners of all cells
  template <class T>
  std::vector<T> pointsImpl () const
  {
    std::vector<T> data(this->numPoints() * 3);
    auto const& indexSet = gridView_.indexSet();
52
    for (auto const& element : elements(gridView_, Partitions::interior)) {
53
      for (unsigned int i = 0; i < element.subEntities(dim); ++i) {
Praetorius, Simon's avatar
Praetorius, Simon committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        std::size_t idx = 3 * indexMap_[indexSet.subIndex(element, i, dim)];
        auto v = element.geometry().corner(i);
        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;
  }

  /// Connect the corners of each cell. The leads to a global discontinuous grid
  Cells cellsImpl () const
  {
    Cells cells;
    cells.connectivity.reserve(this->numPoints());
    cells.offsets.reserve(this->numCells());
    cells.types.reserve(this->numCells());

    std::int64_t old_o = 0;
    auto const& indexSet = gridView_.indexSet();
75
    for (auto const& c : elements(gridView_, Partitions::interior)) {
Praetorius, Simon's avatar
Praetorius, Simon committed
76
      Vtk::CellType cellType(c.type());
77
      for (unsigned int j = 0; j < c.subEntities(dim); ++j) {
Praetorius, Simon's avatar
Praetorius, Simon committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
        std::int64_t vertex_idx = indexMap_[indexSet.subIndex(c,cellType.permutation(j),dim)];
        cells.connectivity.push_back(vertex_idx);
      }
      cells.offsets.push_back(old_o += c.subEntities(dim));
      cells.types.push_back(cellType.type());
    }

    return cells;
  }

  /// Evaluate the `fct` in the corners of each cell
  template <class T, class GlobalFunction>
  std::vector<T> pointDataImpl (GlobalFunction const& fct) const
  {
    std::vector<T> data(this->numPoints() * fct.ncomps());
    auto const& indexSet = gridView_.indexSet();
    auto localFct = localFunction(fct);
95
    for (auto const& e : elements(gridView_, Partitions::interior)) {
Praetorius, Simon's avatar
Praetorius, Simon committed
96
97
98
      localFct.bind(e);
      Vtk::CellType cellType{e.type()};
      auto refElem = referenceElement(e.geometry());
99
      for (unsigned int j = 0; j < e.subEntities(dim); ++j) {
Praetorius, Simon's avatar
Praetorius, Simon committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        std::size_t idx = fct.ncomps() * indexMap_[indexSet.subIndex(e, cellType.permutation(j), dim)];
        for (int comp = 0; comp < fct.ncomps(); ++comp)
          data[idx + comp] = T(localFct.evaluate(comp, refElem.position(cellType.permutation(j),dim)));
      }
      localFct.unbind();
    }
    return data;
  }

private:
  std::uint64_t numPoints_ = 0;
  std::vector<std::int64_t> indexMap_;
};

}} // end namespace Dune::experimental