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 type = element.type();
126 const auto& ref = virtualRefinement(type, subdivision_);
127 auto vEnd = ref.vEnd(ivals_);
128 for (auto vIt = ref.vBegin(ivals_); vIt != vEnd; ++vIt) {
129 std::size_t idx = 3 * (element_idx + vIt.index());
130
131 auto v = geometry.global(vIt.coords());
132 for (std::size_t j = 0; j < v.size(); ++j)
133 data[idx + j] = T(v[j]);
134 for (std::size_t j = v.size(); j < 3u; ++j)
135 data[idx + j] = T(0);
136 }
137 }
138 return data;
139 }
140
143 {
144 return numCells_;
145 }
146
148
152 {
153 Cells cells;
155 cells.offsets.reserve(this->numCells());
156 cells.types.reserve(this->numCells());
157
159 for (auto const& element : elements(gridView(), partition)) {
160 std::size_t element_idx = mapper_->index(element);
161 Vtk::CellType cellType(refinementGeometryType(element.type(),subdivision_), Vtk::CellType::LINEAR);
162
163 const auto type = element.type();
164 const auto& ref = virtualRefinement(type, subdivision_);
165 auto eEnd = ref.eEnd(ivals_);
166 for (auto eIt = ref.eBegin(ivals_); eIt != eEnd; ++eIt) {
167 auto vertices = eIt.vertexIndices();
168 for (std::size_t i = 0; i < vertices.size(); ++i) {
169 std::size_t idx = (element_idx + vertices[cellType.permutation(i)]);
171 }
172 cells.offsets.push_back(old_o += vertices.size());
174 }
175 }
176 return cells;
177 }
178
180 template <class T, class GlobalFunction>
181 std::vector<T> pointDataImpl (GlobalFunction const& fct) const
182 {
183 int nComps = fct.numComponents();
184 std::vector<T> data(this->numPoints() * nComps);
185
186 auto localFct = localFunction(fct);
187 for (auto const& element : elements(gridView(), partition)) {
188 std::size_t element_idx = mapper_->index(element);
189 localFct.bind(element);
190
191 const auto type = element.type();
192 const auto& ref = virtualRefinement(type, subdivision_);
193 auto vEnd = ref.vEnd(ivals_);
194 for (auto vIt = ref.vBegin(ivals_); vIt != vEnd; ++vIt) {
195 std::size_t idx = nComps * (element_idx + vIt.index());
196
197 for (int comp = 0; comp < nComps; ++comp)
198 data.at(idx + comp) = T(localFct.evaluate(comp, vIt.coords()));
199 }
200 localFct.unbind();
201 }
202 return data;
203 }
204
206 template <class T, class GlobalFunction>
207 std::vector<T> cellDataImpl (GlobalFunction const& fct) const
208 {
209 int nComps = fct.numComponents();
210 std::vector<T> data;
211 data.reserve(this->numCells() * nComps);
212
213 auto localFct = localFunction(fct);
214 for (auto const& element : elements(gridView(), partition)) {
215 localFct.bind(element);
216
217 const auto type = element.type();
218 const auto& ref = virtualRefinement(type, subdivision_);
219 auto eEnd = ref.eEnd(ivals_);
220 for (auto eIt = ref.eBegin(ivals_); eIt != eEnd; ++eIt) {
221 for (int comp = 0; comp < fct.numComponents(); ++comp)
222 data.emplace_back(localFct.evaluate(comp, eIt.coords()));
223 }
224 localFct.unbind();
225 }
226 return data;
227 }
228
229 private:
231 Vtk::Subdivision subdivision_;
233
234 std::uint64_t numPoints_ = 0;
235 std::uint64_t numCells_ = 0;
236 };
237
238} // 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:142
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:181
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:151
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:207
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)