datacollector.hh 3.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#include <algorithm>
#include <cstdint>
#include <vector>

#include "vtktypes.hh"

namespace Dune { namespace experimental {

struct Cells
{
  std::vector<std::uint8_t> types;
  std::vector<std::int64_t> offsets;
  std::vector<std::int64_t> connectivity;
};

Praetorius, Simon's avatar
Praetorius, Simon committed
18
template <class GridView, class Derived>
19
20
21
class DataCollectorInterface
{
public:
Praetorius, Simon's avatar
Praetorius, Simon committed
22
23
24
25
26

  DataCollectorInterface (GridView const& gridView)
    : gridView_(gridView)
  {}

27
  /// \brief Return the number of points in the grid
Praetorius, Simon's avatar
Praetorius, Simon committed
28
29
30
31
  std::uint64_t numPoints () const
  {
    return asDerived().numPointsImpl();
  }
32
33

  /// \brief Return the number of cells in the grid
Praetorius, Simon's avatar
Praetorius, Simon committed
34
35
36
37
  std::uint64_t numCells () const
  {
    return asDerived().numCellsImpl();
  }
38
39

  /// Update the DataCollector on the current GridView
Praetorius, Simon's avatar
Praetorius, Simon committed
40
41
42
43
  void update ()
  {
    asDerived().updateImpl();
  }
44
45
46
47
48
49
50
51
52

  /// \brief Return a flat vector of point coordinates
  /**
   * All coordinates are extended to 3 components and concatenated.
   * [p0_x, p0_y, p0_z, p1_x, p1_y, p1_z, ...]
   * If the GridView::dimensionworld < 3, the remaining components are
   * set to 0
   **/
  template <class T>
Praetorius, Simon's avatar
Praetorius, Simon committed
53
54
55
56
  std::vector<T> points () const
  {
    return asDerived().template pointsImpl<T>();
  }
57
58

  /// \brief Return cell types, offsets, and connectivity. \see Cells
Praetorius, Simon's avatar
Praetorius, Simon committed
59
60
61
62
  Cells cells () const
  {
    return asDerived().cellsImpl();
  }
63
64
65
66
67
68
69
70
71
72
73

  /// \brief Return a flat vector of function values evaluated at the grid points.
  /**
   * In case of a vector valued function, flat the vector entries:
   * [fct(p0)_0, fct(p0)_1, fct(p0)_2, fct(p1)_0, ...]
   * where the vector dimension must be 3 (possible extended by 0s)
   * In case of tensor valued function, flat the tensor row-wise:
   * [fct(p0)_00, fct(p0)_01, fct(p0)_02, fct(p0)_10, fct(p0)_11, fct(p0)_12, fct(p0)_20...]
   * where the tensor dimension must be 3x3 (possible extended by 0s)
   **/
  template <class T, class GlobalFunction>
Praetorius, Simon's avatar
Praetorius, Simon committed
74
75
76
77
  std::vector<T> pointData (GlobalFunction const& fct) const
  {
    return asDerived().template pointDataImpl<T>(fct);
  }
78
79
80

  /// \brief Return a flat vector of function values evaluated at the grid cells. \see pointData.
  template <class T, class GlobalFunction>
Praetorius, Simon's avatar
Praetorius, Simon committed
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
  std::vector<T> cellData (GlobalFunction const& fct) const
  {
    return asDerived().template cellDataImpl<T>(fct);
  }


protected: // cast to derived type

  Derived& asDerived ()
  {
    return static_cast<Derived&>(*this);
  }

  const Derived& asDerived () const
  {
    return static_cast<const Derived&>(*this);
  }


protected: // default implementations

  std::uint64_t numCellsImpl () const
  {
    return gridView_.size(0);
  }

  void updateImpl () { /* do nothing */ }

  // Evaluate `fct` in center of cell
  template <class T, class GlobalFunction>
  std::vector<T> cellDataImpl (GlobalFunction const& fct) const
  {
    std::vector<T> data(numCells() * fct.ncomps());
    auto const& indexSet = gridView_.indexSet();
    auto localFct = localFunction(fct);
    for (auto const& e : elements(gridView_)) {
      localFct.bind(e);
      auto geometry = e.geometry();
      std::size_t idx = fct.ncomps() * indexSet.index(e);
      for (int comp = 0; comp < fct.ncomps(); ++comp)
        data[idx + comp] = T(localFct.evaluate(comp, geometry.center()));
      localFct.unbind();
    }
    return data;
  }


protected:

  GridView gridView_;
131
132
133
};

}} // end namespace Dune::experimental