dune-vtk  0.2
continuousdatacollector.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <numeric>
4 #include <vector>
5 
6 #include <dune/geometry/referenceelements.hh>
7 #include <dune/grid/common/partitionset.hh>
8 #include <dune/grid/utility/globalindexset.hh>
9 #include <dune/vtk/types.hh>
10 
12 
13 namespace Dune
14 {
15  namespace Vtk
16  {
18  template <class GridView, class Partition = Partitions::InteriorBorder>
20  : public UnstructuredDataCollectorInterface<GridView, ContinuousDataCollector<GridView,Partition>, Partition>
21  {
24 
25  public:
26  using Super::dim;
27  using Super::partition;
28 
29  public:
31  : Super(gridView)
32  {}
33 
35  void updateImpl ()
36  {
37  numPoints_ = 0;
38  indexMap_.resize(gridView_.size(dim));
39  auto const& indexSet = gridView_.indexSet();
40  for (auto const& vertex : vertices(gridView_, partition))
41  indexMap_[indexSet.index(vertex)] = std::int64_t(numPoints_++);
42 
43  if (gridView_.comm().size() > 1) {
44  auto&& e = elements(gridView_, partition);
45  numCells_ = std::distance(std::begin(e), std::end(e));
46  } else {
47  numCells_ = gridView_.size(0);
48  }
49  }
50 
52  std::uint64_t numPointsImpl () const
53  {
54  return numPoints_;
55  }
56 
58  template <class T>
59  std::vector<T> pointsImpl () const
60  {
61  std::vector<T> data;
62  data.reserve(numPoints_ * 3);
63  for (auto const& vertex : vertices(gridView_, partition)) {
64  auto v = vertex.geometry().center();
65  for (std::size_t j = 0; j < v.size(); ++j)
66  data.emplace_back(v[j]);
67  for (std::size_t j = v.size(); j < 3u; ++j)
68  data.emplace_back(0);
69  }
70  return data;
71  }
72 
74  std::vector<std::uint64_t> pointIdsImpl () const
75  {
76  std::vector<std::uint64_t> data;
77  data.reserve(numPoints_);
78  GlobalIndexSet<GridView> globalIndexSet(gridView_, dim);
79  for (auto const& vertex : vertices(gridView_, partition)) {
80  data.emplace_back(globalIndexSet.index(vertex));
81  }
82  return data;
83  }
84 
86  std::uint64_t numCellsImpl () const
87  {
88  return numCells_;
89  }
90 
93  Cells cellsImpl () const
94  {
95  auto const& indexSet = gridView_.indexSet();
96  auto types = indexSet.types(0);
97  int maxVertices = std::accumulate(types.begin(), types.end(), 1, [](int m, GeometryType t) {
98  auto refElem = referenceElement<double,dim>(t);
99  return std::max(m, refElem.size(dim));
100  });
101 
102  Cells cells;
103  cells.connectivity.reserve(numCells_ * maxVertices);
104  cells.offsets.reserve(numCells_);
105  cells.types.reserve(numCells_);
106 
107  std::int64_t old_o = 0;
108  for (auto const& c : elements(gridView_, partition)) {
109  Vtk::CellType cellType(c.type());
110  for (unsigned int j = 0; j < c.subEntities(dim); ++j)
111  cells.connectivity.emplace_back(indexMap_[indexSet.subIndex(c,cellType.permutation(j),dim)]);
112  cells.offsets.push_back(old_o += c.subEntities(dim));
113  cells.types.push_back(cellType.type());
114  }
115  return cells;
116  }
117 
119  template <class T, class GlobalFunction>
120  std::vector<T> pointDataImpl (GlobalFunction const& fct) const
121  {
122  std::vector<T> data(numPoints_ * fct.numComponents());
123  auto const& indexSet = gridView_.indexSet();
124  auto localFct = localFunction(fct);
125  for (auto const& e : elements(gridView_, partition)) {
126  localFct.bind(e);
127  Vtk::CellType cellType{e.type()};
128  auto refElem = referenceElement(e.geometry());
129  for (unsigned int j = 0; j < e.subEntities(dim); ++j) {
130  std::size_t idx = fct.numComponents() * indexMap_[indexSet.subIndex(e,cellType.permutation(j),dim)];
131  for (int comp = 0; comp < fct.numComponents(); ++comp)
132  data[idx + comp] = T(localFct.evaluate(comp, refElem.position(cellType.permutation(j),dim)));
133  }
134  localFct.unbind();
135  }
136  return data;
137  }
138 
139  protected:
140  using Super::gridView_;
141  std::uint64_t numPoints_ = 0;
142  std::uint64_t numCells_ = 0;
143  std::vector<std::int64_t> indexMap_;
144  };
145 
146  } // end namespace Vtk
147 } // end namespace Dune
Definition: datacollectorinterface.hh:9
Implementation of DataCollector for linear cells, with continuous data.
Definition: continuousdatacollector.hh:21
std::uint64_t numPointsImpl() const
Return number of grid vertices.
Definition: continuousdatacollector.hh:52
ContinuousDataCollector(GridView const &gridView)
Definition: continuousdatacollector.hh:30
std::vector< T > pointDataImpl(GlobalFunction const &fct) const
Evaluate the fct at the corners of the elements.
Definition: continuousdatacollector.hh:120
void updateImpl()
Collect the vertex indices.
Definition: continuousdatacollector.hh:35
std::uint64_t numCells_
Definition: continuousdatacollector.hh:142
std::uint64_t numCellsImpl() const
Return number of grid cells.
Definition: continuousdatacollector.hh:86
std::vector< std::int64_t > indexMap_
Definition: continuousdatacollector.hh:143
Cells cellsImpl() const
Definition: continuousdatacollector.hh:93
std::vector< T > pointsImpl() const
Return the coordinates of all grid vertices in the order given by the indexSet.
Definition: continuousdatacollector.hh:59
std::vector< std::uint64_t > pointIdsImpl() const
Return a vector of global unique ids of the points.
Definition: continuousdatacollector.hh:74
std::uint64_t numPoints_
Definition: continuousdatacollector.hh:141
Definition: unstructureddatacollector.hh:14
std::vector< std::int64_t > offsets
Definition: unstructureddatacollector.hh:16
std::vector< std::int64_t > connectivity
Definition: unstructureddatacollector.hh:17
std::vector< std::uint8_t > types
Definition: unstructureddatacollector.hh:15
Definition: unstructureddatacollector.hh:23
@ dim
Definition: datacollectorinterface.hh:28
static constexpr auto partition
The partitionset to collect data from.
Definition: datacollectorinterface.hh:23
Cells cells() const
Return cell types, offsets, and connectivity.
Definition: unstructureddatacollector.hh:36
Mapping of Dune geometry types to VTK cell types.
Definition: types.hh:191
std::uint8_t type() const
Return VTK Cell type.
Definition: types.hh:196
int permutation(int idx) const
Return a permutation of Dune elemenr vertices to conform to VTK element numbering.
Definition: types.hh:202