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