structureddatacollector.hh 3.95 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
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
#pragma once

#include <dune/vtk/datacollector.hh>

namespace Dune { namespace experimental
{

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

  using Self = StructuredDataCollector;
  using Super = DataCollectorInterface<GridView, Self>;
  using Super::gridView_;
  using ctype = typename GridView::ctype;

  struct CoordLess
  {
    template <class T, int N>
    bool operator() (FieldVector<T,N> const& lhs, FieldVector<T,N> const& rhs) const
    {
      for (int i = N-1; i >= 0; --i) {
        if (std::abs(lhs[i] - rhs[i]) < std::numeric_limits<T>::epsilon())
          continue;
        return lhs[i] < rhs[i];
      }
      return false;
    }
  };

public:
  StructuredDataCollector (GridView const& gridView)
    : Super(gridView)
  {}

  /// Return number of grid vertices
  std::uint64_t numPointsImpl () const
  {
    return gridView_.size(dim);
  }

  std::array<int, 6> const& extent () const
  {
    return extent_;
  }

  auto const& origin () const
  {
    return origin_;
  }

  auto const& spacing () const
  {
    return spacing_;
  }

  void updateImpl ()
  {
    // TODO: extract this information from the grid
    int extent = GridView::dimension == 1 ? gridView_.size(dim) :
                 GridView::dimension == 2 ? isqrt(gridView_.size(dim)) :
                 GridView::dimension == 3 ? icbrt(gridView_.size(dim)) : 0;
    for (int i = 0; i < 3; ++i) {
      if (GridView::dimension > i) {
        extent_[2*i] = 0;
        extent_[2*i+1] = extent-1;
        spacing_[i] = gridView_.grid().domainSize()[i] / (extent-1);
      } else {
        extent_[2*i] = extent_[2*i+1] = 0;
        spacing_[i] = 0;
      }

      origin_[i] = 0;
    }
  }

  /// Return the coordinates of all grid vertices in the order given by the indexSet
  template <class T>
  std::vector<T> pointsImpl () const
  {
    std::vector<T> data(this->numPoints() * 3);
    auto const& indexSet = gridView_.indexSet();
    for (auto const& vertex : vertices(gridView_)) {
      std::size_t idx = 3 * indexSet.index(vertex);
      auto v = vertex.geometry().center();
      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;
  }

  /// Evaluate the `fct` at the corners of the elements
  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);
    for (auto const& e : elements(gridView_)) {
      localFct.bind(e);
      Vtk::CellType cellType{e.type()};
      auto refElem = referenceElement(e.geometry());
      for (int j = 0; j < e.subEntities(dim); ++j) {
        int k = cellType.permutation(j);
        std::size_t idx = fct.ncomps() * indexSet.subIndex(e,k,dim);
        for (int comp = 0; comp < fct.ncomps(); ++comp)
          data[idx + comp] = T(localFct.evaluate(comp, refElem.position(k,dim)));
      }
      localFct.unbind();
    }
    return data;
  }

private:
  static constexpr std::uint32_t isqrt (std::uint64_t x)
  {
    std::uint32_t c = 0x8000;
    std::uint32_t g = 0x8000;

    while (true) {
      if (g*g > x)
        g ^= c;
      c >>= 1;
      if (c == 0)
        return g;
      g |= c;
    }
  }

  static constexpr std::uint32_t icbrt (std::uint64_t x)
  {
    std::uint32_t y = 0;
    for (int s = 63; s >= 0; s -= 3) {
      y += y;
      std::uint64_t b = 3*y*(std::uint64_t(y) + 1) + 1;
      if ((x >> s) >= b) {
        x -= b << s;
        y++;
      }
    }
    return y;
  }

private:
  std::array<int, 6> extent_;
  FieldVector<ctype,3> spacing_;
  FieldVector<ctype,3> origin_;
  std::vector<std::size_t> indexMap_;
};

}} // end namespace Dune::experimental