dune-vtk 2.12
Loading...
Searching...
No Matches
subdivisiondatacollector.hh
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <map>
5#include <optional>
6#include <vector>
7
13#include <dune/vtk/types.hh>
14
16
17namespace Dune::Vtk
18{
19 enum class Subdivision {
20 REFINEMENT, // refine cubes into sub-cubes
21 TRIANGULATION // refine all elements into simplices
22 };
23
33 template <class GridView>
35 : public UnstructuredDataCollectorInterface<GridView, SubdivisionDataCollector<GridView>, Partitions::All>
36 {
39
40 static constexpr int dim = GridView::dimension;
41
42 // GeometryType of the divided cell, depends on the Subdivision property
43 static Dune::GeometryType refinementGeometryType (Dune::GeometryType const& gt, Vtk::Subdivision subdivision)
44 {
46 return Dune::GeometryTypes::simplex(gt.dim());
47 else if (gt.isPrism() || gt.isPyramid())
48 return Dune::GeometryTypes::simplex(3);
49 else
50 return gt;
51 }
52
53 static auto& virtualRefinement (Dune::GeometryType const& gt, Vtk::Subdivision subdivision)
54 {
55 Dune::GeometryType coerceTo = refinementGeometryType(gt, subdivision);
56 return Dune::buildRefinement<dim, typename GridView::ctype>(gt, coerceTo);
57 }
58
59 // The mapper contains n nodes per element interior but no nodes on other codimensions, where n
60 // refers to the number of vertices per element in a virtual refinement.
61 auto makeMapper () const
62 {
63 auto layout = [subdivision=subdivision_, ivals=ivals_](Dune::GeometryType gt, int dimgrid) -> unsigned int {
64 if (int(gt.dim()) == dimgrid)
65 return virtualRefinement(gt, subdivision).nVertices(ivals);
66 else
67 return 0;
68 };
70 }
71
72 public:
73 using Super::partition; // NOTE: subdivision data-collector currently implemented for the All partition only
74 using Super::gridView;
75
76 public:
92
94 void updateImpl ()
95 {
96 auto const& indexSet = gridView().indexSet();
97
98 mapper_.emplace(makeMapper());
99 numPoints_ = mapper_->size();
100 numCells_ = 0;
101 for (auto gt : indexSet.types(0)) {
102 const auto& ref = virtualRefinement(gt, subdivision_);
103 numCells_ += indexSet.size(gt)*ref.nElements(ivals_);
104 }
105 }
106
109 {
110 return numPoints_;
111 }
112
114
117 template <class T>
119 {
120 std::vector<T> data(this->numPoints() * 3);
121 for (auto const& element : elements(gridView(), partition)) {
122 auto geometry = element.geometry();
123 std::size_t element_idx = mapper_->index(element);
124
125 const auto& ref = virtualRefinement(element.type(), subdivision_);
126 auto vEnd = ref.vEnd(ivals_);
127 for (auto vIt = ref.vBegin(ivals_); vIt != vEnd; ++vIt) {
128 std::size_t idx = 3 * (element_idx + vIt.index());
129
130 auto v = geometry.global(vIt.coords());
131 for (std::size_t j = 0; j < v.size(); ++j)
132 data[idx + j] = T(v[j]);
133 for (std::size_t j = v.size(); j < 3u; ++j)
134 data[idx + j] = T(0);
135 }
136 }
137 return data;
138 }
139
142 {
143 return numCells_;
144 }
145
147
151 {
152 Cells cells;
154 cells.offsets.reserve(this->numCells());
155 cells.types.reserve(this->numCells());
156
158 for (auto const& element : elements(gridView(), partition)) {
159 std::size_t element_idx = mapper_->index(element);
160 Vtk::CellType cellType(refinementGeometryType(element.type(),subdivision_), Vtk::CellType::LINEAR);
161
162 const auto& ref = virtualRefinement(element.type(), subdivision_);
163 auto eEnd = ref.eEnd(ivals_);
164 for (auto eIt = ref.eBegin(ivals_); eIt != eEnd; ++eIt) {
165 auto vertices = eIt.vertexIndices();
166 for (std::size_t i = 0; i < vertices.size(); ++i) {
167 std::size_t idx = (element_idx + vertices[cellType.permutation(i)]);
169 }
170 cells.offsets.push_back(old_o += vertices.size());
172 }
173 }
174 return cells;
175 }
176
178 template <class T, class GlobalFunction>
179 std::vector<T> pointDataImpl (GlobalFunction const& fct) const
180 {
181 int nComps = fct.numComponents();
182 std::vector<T> data(this->numPoints() * nComps);
183
184 auto localFct = localFunction(fct);
185 for (auto const& element : elements(gridView(), partition)) {
186 std::size_t element_idx = mapper_->index(element);
187 localFct.bind(element);
188
189 const auto& ref = virtualRefinement(element.type(), subdivision_);
190 auto vEnd = ref.vEnd(ivals_);
191 for (auto vIt = ref.vBegin(ivals_); vIt != vEnd; ++vIt) {
192 std::size_t idx = nComps * (element_idx + vIt.index());
193
194 for (int comp = 0; comp < nComps; ++comp)
195 data.at(idx + comp) = T(localFct.evaluate(comp, vIt.coords()));
196 }
197 localFct.unbind();
198 }
199 return data;
200 }
201
203 template <class T, class GlobalFunction>
204 std::vector<T> cellDataImpl (GlobalFunction const& fct) const
205 {
206 int nComps = fct.numComponents();
207 std::vector<T> data;
208 data.reserve(this->numCells() * nComps);
209
210 auto localFct = localFunction(fct);
211 for (auto const& element : elements(gridView(), partition)) {
212 localFct.bind(element);
213
214 const auto& ref = virtualRefinement(element.type(), subdivision_);
215 auto eEnd = ref.eEnd(ivals_);
216 for (auto eIt = ref.eBegin(ivals_); eIt != eEnd; ++eIt) {
217 for (int comp = 0; comp < fct.numComponents(); ++comp)
218 data.emplace_back(localFct.evaluate(comp, eIt.coords()));
219 }
220 localFct.unbind();
221 }
222 return data;
223 }
224
225 private:
227 Vtk::Subdivision subdivision_;
229
230 std::uint64_t numPoints_ = 0;
231 std::uint64_t numCells_ = 0;
232 };
233
234} // end namespace Dune::Vtk
Definition writer.hh:16
Subdivision
Definition subdivisiondatacollector.hh:19
auto size(GeometryType type) const
Types types(int codim) const
std::uint64_t numCells() const
Return the number of cells in (this partition of the) grid.
Definition datacollectorinterface.hh:57
std::uint64_t numPoints() const
Return the number of points in (this partition of the) grid.
Definition datacollectorinterface.hh:63
GV GridView
Type of the bound grid view.
Definition datacollectorinterface.hh:21
GridView const & gridView() const
Return the bound grid view.
Definition datacollectorinterface.hh:39
Implementation of DataCollector with refined cells.
Definition subdivisiondatacollector.hh:36
std::uint64_t numCellsImpl() const
Return number of grid cells.
Definition subdivisiondatacollector.hh:141
std::vector< T > pointDataImpl(GlobalFunction const &fct) const
Evaluate the fct at element vertices and edge centers in the same order as the point coords.
Definition subdivisiondatacollector.hh:179
SubdivisionDataCollector(GridView const &gridView, Dune::RefinementIntervals ivals=Dune::RefinementIntervals(1), Vtk::Subdivision subdivision=Vtk::Subdivision::REFINEMENT)
Construct a data collector with virtual element refinement.
Definition subdivisiondatacollector.hh:83
void updateImpl()
Construct the virtual element refinements.
Definition subdivisiondatacollector.hh:94
Cells cellsImpl() const
Return cell types, offsets, and connectivity.
Definition subdivisiondatacollector.hh:150
std::vector< T > pointsImpl() const
Return a vector of point coordinates.
Definition subdivisiondatacollector.hh:118
std::uint64_t numPointsImpl() const
Return number of points in the grid.
Definition subdivisiondatacollector.hh:108
std::vector< T > cellDataImpl(GlobalFunction const &fct) const
Evaluate the fct at cell centers.
Definition subdivisiondatacollector.hh:204
Definition unstructureddatacollector.hh:12
std::vector< std::int64_t > offsets
Definition unstructureddatacollector.hh:14
std::vector< std::int64_t > connectivity
Definition unstructureddatacollector.hh:15
std::vector< std::uint8_t > types
Definition unstructureddatacollector.hh:13
Definition unstructureddatacollector.hh:26
static constexpr auto partition
The partitionset to collect data from.
Definition datacollectorinterface.hh:24
Cells cells() const
Return cell types, offsets, and connectivity.
Definition unstructureddatacollector.hh:45
Mapping of Dune geometry types to VTK cell types.
Definition types.hh:159
@ LINEAR
Definition types.hh:161
T push_back(T... args)
T reserve(T... args)