Dune Core Modules (2.11.0)

codimdatacollector.hh
1#pragma once
2
3#include <numeric>
4#include <vector>
5
6#include <dune/geometry/referenceelements.hh>
7#include <dune/grid/common/partitionset.hh>
9#include <dune/vtk/types.hh>
10
11#include <dune/vtk/datacollectors/unstructureddatacollector.hh>
12
13namespace Dune::Vtk
14{
16 template <class GridView, class Partition = Partitions::InteriorBorder>
18 : public UnstructuredDataCollectorInterface<GridView, CodimDataCollector<GridView,Partition>, Partition>
19 {
21 using Super = UnstructuredDataCollectorInterface<GridView, Self, Partition>;
22
23 public:
24 using Super::dim;
25 using Super::partition;
26 using Super::gridView;
27
28 public:
29 explicit CodimDataCollector (GridView const& gridView, int codim = 1)
30 : Super(gridView)
31 , codim_(codim)
32 {}
33
35 void updateImpl ()
36 {
37 numPoints_ = gridView().size(dim);
38 numCells_ = gridView().size(codim_);
39 }
40
42 std::uint64_t numPointsImpl () const
43 {
44 return numPoints_;
45 }
46
48 template <class T>
49 std::vector<T> pointsImpl () const
50 {
51 std::vector<T> data(numPoints_ * 3);
52 auto const& indexSet = gridView().indexSet();
53 for (auto const& vertex : vertices(gridView(), partition)) {
54 auto v = vertex.geometry().center();
55 std::size_t idx = 3*indexSet.index(vertex);
56 for (std::size_t j = 0; j < v.size(); ++j)
57 data[idx+j] = v[j];
58 for (std::size_t j = v.size(); j < 3u; ++j)
59 data[idx+j] = T(0);
60 }
61 return data;
62 }
63
65 std::vector<std::uint64_t> pointIdsImpl () const
66 {
67 std::vector<std::uint64_t> data(numPoints_);
68 auto const& indexSet = gridView().indexSet();
69 GlobalIndexSet<GridView> globalIndexSet(gridView(), dim);
70 for (auto const& vertex : vertices(gridView(), partition)) {
71 data[indexSet.index(vertex)] = globalIndexSet.index(vertex);
72 }
73 return data;
74 }
75
77 std::uint64_t numCellsImpl () const
78 {
79 return numCells_;
80 }
81
84 Cells cellsImpl () const
85 {
86 auto const& indexSet = gridView().indexSet();
87 auto types = indexSet.types(codim_);
88 int maxVertices = std::accumulate(types.begin(), types.end(), 1, [codim=codim_](int m, GeometryType t) {
89 switch (codim) {
90 case 0:
91 return std::max(m, referenceElement<double,dim>(t).size(dim));
92 case 1: if constexpr (dim >= 1) {
93 return std::max(m, referenceElement<double,dim-1>(t).size(dim-1)); } break;
94 case 2: if constexpr (dim >= 2) {
95 return std::max(m, referenceElement<double,dim-2>(t).size(dim-2)); } break;
96 case 3: if constexpr (dim >= 3) {
97 return std::max(m, referenceElement<double,dim-3>(t).size(dim-3)); } break;
98 default:
99 return std::max(m, referenceElement<double,0>(t).size(0));
100 }
101 return m;
102 });
103
104 Cells cells;
105 cells.connectivity.reserve(numCells_ * maxVertices);
106 cells.offsets.reserve(numCells_);
107 cells.types.reserve(numCells_);
108
109 std::vector<bool> visited(gridView().size(codim_), false);
110 std::int64_t old_o = 0;
111 for (auto const& c : elements(gridView(), partition)) {
112 auto refElem = referenceElement(c);
113 for (int i = 0; i < refElem.size(codim_); ++i) {
114 Vtk::CellType cellType(refElem.type(i,codim_));
115 std::size_t idx = indexSet.subIndex(c,i,codim_);
116 if (!visited[idx]) {
117 visited[idx] = true;
118 for (int j = 0; j < refElem.size(i,codim_,dim); ++j)
119 cells.connectivity.push_back(indexSet.subIndex(c,
120 refElem.subEntity(i,codim_,cellType.permutation(j),dim), dim));
121 cells.offsets.push_back(old_o += refElem.size(i,codim_,dim));
122 cells.types.push_back(cellType.type());
123 }
124 }
125 }
126 return cells;
127 }
128
130 template <class T, class GlobalFunction>
131 std::vector<T> pointDataImpl (GlobalFunction const& fct) const
132 {
133 std::vector<T> data(numPoints_ * fct.numComponents());
134 auto const& indexSet = gridView().indexSet();
135 auto localFct = localFunction(fct);
136 std::vector<bool> visited(gridView().size(codim_), false);
137 for (auto const& c : elements(gridView(), partition)) {
138 localFct.bind(c);
139 auto refElem = referenceElement(c);
140 for (int i = 0; i < refElem.size(codim_); ++i) {
141 Vtk::CellType cellType(refElem.type(i,codim_));
142 std::size_t edge_idx = indexSet.subIndex(c,i,codim_);
143 if (!visited[edge_idx]) {
144 visited[edge_idx] = true;
145 for (int j = 0; j < refElem.size(i,codim_,dim); ++j) {
146 std::size_t cj = refElem.subEntity(i,codim_,cellType.permutation(j),dim);
147 std::size_t idx = fct.numComponents() * indexSet.subIndex(c,cj,dim);
148 auto local = refElem.position(cj,dim);
149 for (int comp = 0; comp < fct.numComponents(); ++comp)
150 data[idx + comp] = T(localFct.evaluate(comp, local));
151 }
152 }
153 }
154 localFct.unbind();
155 }
156 return data;
157 }
158
160 template <class T, class GlobalFunction>
161 std::vector<T> cellDataImpl (GlobalFunction const& fct) const
162 {
163 std::vector<T> data;
164 data.reserve(numCells_ * fct.numComponents());
165 auto const& indexSet = gridView().indexSet();
166 auto localFct = localFunction(fct);
167 std::vector<bool> visited(gridView().size(codim_), false);
168 for (auto const& c : elements(gridView(), partition)) {
169 localFct.bind(c);
170 auto refElem = referenceElement(c);
171 for (int i = 0; i < refElem.size(codim_); ++i) {
172 std::size_t edge_idx = indexSet.subIndex(c,i,codim_);
173 if (!visited[edge_idx]) {
174 visited[edge_idx] = true;
175 auto local = refElem.position(i,codim_);
176 for (int comp = 0; comp < fct.numComponents(); ++comp)
177 data.push_back(T(localFct.evaluate(comp, local)));
178 }
179 }
180 localFct.unbind();
181 }
182 return data;
183 }
184
185 private:
186 int codim_ = 1;
187 std::uint64_t numPoints_ = 0;
188 std::uint64_t numCells_ = 0;
189 };
190
191} // end namespace Dune::Vtk
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
Calculate globally unique index over all processes in a Dune grid.
Definition: globalindexset.hh:63
Index index(const Entity &entity) const
Return the global index of a given entity.
Definition: globalindexset.hh:453
Implementation of DataCollector for linear cells, with continuous data.
Definition: codimdatacollector.hh:19
void updateImpl()
Collect the vertex indices.
Definition: codimdatacollector.hh:35
std::vector< T > pointDataImpl(GlobalFunction const &fct) const
Evaluate the fct at the corners of the element edges.
Definition: codimdatacollector.hh:131
std::uint64_t numCellsImpl() const
Return number of grid cells.
Definition: codimdatacollector.hh:77
std::vector< T > cellDataImpl(GlobalFunction const &fct) const
Evaluate the fct at the corners of the element edges.
Definition: codimdatacollector.hh:161
std::uint64_t numPointsImpl() const
Return number of grid vertices.
Definition: codimdatacollector.hh:42
std::vector< std::uint64_t > pointIdsImpl() const
Return a vector of global unique ids of the points.
Definition: codimdatacollector.hh:65
std::vector< T > pointsImpl() const
Return the coordinates of all grid vertices in the order given by the indexSet.
Definition: codimdatacollector.hh:49
Cells cellsImpl() const
Definition: codimdatacollector.hh:84
static constexpr auto partition
The partitionset to collect data from.
Definition: datacollectorinterface.hh:24
GridView GridView
Type of the bound grid view.
Definition: datacollectorinterface.hh:21
GridView const & gridView() const
Return the bound grid view.
Definition: datacollectorinterface.hh:39
Provides a globally unique index for all entities of a distributed Dune grid.
unspecified value type referenceElement(T &&... t)
Returns a reference element for the objects t....
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
constexpr T accumulate(Range &&range, T value, F &&f)
Accumulate values.
Definition: hybridutilities.hh:284
auto elements(const SubDomainGridView< HostGridView > &subDomainGridView)
ADL findable access to element range for a SubDomainGridView.
Definition: subdomain.hh:487
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Mapping of Dune geometry types to VTK cell types.
Definition: types.hh:159
int permutation(int idx) const
Return a permutation of Dune element vertices to conform to VTK element numbering.
Definition: types.hh:222
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Feb 14, 23:39, 2026)