Dune Core Modules (2.11.0)

subdivisiondatacollector.hh
1#pragma once
2
3#include <cassert>
4#include <map>
5#include <optional>
6#include <vector>
7
8#include <dune/geometry/referenceelement.hh>
11#include <dune/grid/common/partitionset.hh>
13#include <dune/vtk/types.hh>
14
15#include "unstructureddatacollector.hh"
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 {
38 using Super = UnstructuredDataCollectorInterface<GridView, Self, Partitions::All>;
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 {
45 if (subdivision == Vtk::Subdivision::TRIANGULATION)
46 return Dune::GeometryTypes::simplex(gt.dim());
47 else if (gt.isPrism() || gt.isPyramid())
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:
85 Vtk::Subdivision subdivision = Vtk::Subdivision::REFINEMENT)
86 : Super(gridView)
87 , ivals_(ivals)
88 , subdivision_(subdivision)
89 {
90 assert(ivals_.intervals() > 0);
91 }
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
108 std::uint64_t numPointsImpl () const
109 {
110 return numPoints_;
111 }
112
114
117 template <class T>
118 std::vector<T> pointsImpl () const
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
141 std::uint64_t numCellsImpl () const
142 {
143 return numCells_;
144 }
145
147
150 Cells cellsImpl () const
151 {
152 Cells cells;
153 cells.connectivity.reserve(this->numPoints());
154 cells.offsets.reserve(this->numCells());
155 cells.types.reserve(this->numCells());
156
157 std::int64_t old_o = 0;
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)]);
168 cells.connectivity.push_back(std::int64_t(idx));
169 }
170 cells.offsets.push_back(old_o += vertices.size());
171 cells.types.push_back(cellType.type());
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_;
228 std::optional<Dune::MultipleCodimMultipleGeomTypeMapper<GridView>> mapper_;
229
230 std::uint64_t numPoints_ = 0;
231 std::uint64_t numCells_ = 0;
232 };
233
234} // end namespace Dune::Vtk
Unique label for each type of entities that can occur in DUNE grids.
Definition: type.hh:114
Implementation class for a multiple codim and multiple geometry type mapper.
Definition: mcmgmapper.hh:129
Holds the number of refined intervals per axis needed for virtual and static refinement.
Definition: base.cc:94
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
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
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
This file simply includes all Refinement implementations so you don't have to do them separately.
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
static constexpr int dimension
The dimension of the grid.
Definition: gridview.hh:134
constexpr GeometryType simplex(unsigned int dim)
Returns a GeometryType representing a simplex of dimension dim.
Definition: type.hh:453
auto elements(const SubDomainGridView< HostGridView > &subDomainGridView)
ADL findable access to element range for a SubDomainGridView.
Definition: subdomain.hh:487
Mapper for multiple codim and multiple geometry types.
Mapping of Dune geometry types to VTK cell types.
Definition: types.hh:159
std::uint8_t type() const
Return VTK Cell type.
Definition: types.hh:211
int permutation(int idx) const
Return a permutation of Dune element vertices to conform to VTK element numbering.
Definition: types.hh:222
This file contains the virtual wrapper around refinement.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Feb 14, 23:39, 2026)