dune-vtk 0.2
lagrangegridfunction.hh
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <vector>
5
6#include <dune/common/dynvector.hh>
7#include <dune/common/version.hh>
8#include <dune/localfunctions/lagrange.hh>
13
14namespace Dune
15{
16 namespace Vtk
17 {
18 template <class GridType>
20
23 template <class GridType, class FieldType, class Context>
25 {
26 using Grid = GridType;
27 using Field = FieldType;
28
29 using Factory = GridFactory<Grid>;
31
32 public:
33 struct EntitySet
34 {
35 using Grid = GridType;
36 using Element = typename GridType::template Codim<0>::Entity;
37 using LocalCoordinate = typename Element::Geometry::LocalCoordinate;
38 using GlobalCoordinate = typename Element::Geometry::GlobalCoordinate;
39 };
40
42 using Range = DynamicVector<Field>;
44
45 private:
46 template <class LC>
47 class PointDataLocalFunction
48 {
49 using LFE = LagrangeLocalFiniteElement<LagrangePointSet, LC::dimension, FieldType, FieldType>;
50 using LB = typename LFE::Traits::LocalBasisType;
51
52 public:
53 using LocalContext = LC;
54 using Domain = typename LC::Geometry::LocalCoordinate;
55 using Range = DynamicVector<Field>;
56 using Signature = Range(Domain);
57
58 public:
60 PointDataLocalFunction (GridCreator const* creator, std::vector<Field> const* values, unsigned int comp,
61 std::vector<std::uint8_t> const* types,
62 std::vector<std::int64_t> const* offsets,
63 std::vector<std::int64_t> const* connectivity)
64 : creator_(creator)
65 , values_(values)
66 , comp_(comp)
67 , types_(types)
68 , offsets_(offsets)
69 , connectivity_(connectivity)
70 {}
71
72 PointDataLocalFunction () = default;
73
75
81 void bind (LocalContext const& element)
82 {
83 unsigned int insertionIndex = creator_->factory().insertionIndex(element);
84
85 std::int64_t shift = (insertionIndex == 0 ? 0 : (*offsets_)[insertionIndex-1]);
86 std::int64_t numNodes = (*offsets_)[insertionIndex] - shift;
87#if DUNE_VERSION_LT(DUNE_LOCALFUNCTIONS,2,8)
88 [[maybe_unused]] std::int64_t maxNumNodes = numLagrangePoints(element.type().id(), element.type().dim(), 20);
89#else
90 [[maybe_unused]] std::int64_t maxNumNodes = numLagrangePoints(element.type(), 20);
91#endif
92 VTK_ASSERT(numNodes > 0 && numNodes < maxNumNodes);
93
94 int order = creator_->order(element.type(), numNodes);
95 VTK_ASSERT(order > 0 && order < 20);
96
97 // construct a local finite-element with the corresponding order and Lagrange points
98 // as stored in the file
99 lfe_.emplace(LFE{element.type(), (unsigned int)(order)});
100 lgeo_.emplace(creator_->localGeometry(element));
101
102 // collect values on lagrange nodes
103 localValues_.resize(numNodes);
104 for (std::int64_t i = shift, i0 = 0; i < (*offsets_)[insertionIndex]; ++i, ++i0) {
105 std::int64_t idx = (*connectivity_)[i];
106 DynamicVector<Field>& v = localValues_[i0];
107 v.resize(comp_);
108 for (unsigned int j = 0; j < comp_; ++j)
109 v[j] = (*values_)[comp_*idx + j];
110 }
111 }
112
114 void unbind ()
115 {
116 lfe_.reset();
117 lgeo_.reset();
118 }
119
122 // NOTE: do we need to transform the local coordinates?
123 Range operator() (Domain const& local) const
124 {
125 assert(!!lfe_);
126 auto const& lb = lfe_->localBasis();
127 lb.evaluateFunction(lgeo_->global(local), shapeValues_);
128 assert(shapeValues_.size() == localValues_.size());
129
130 Range y(comp_, Field(0));
131 for (std::size_t i = 0; i < shapeValues_.size(); ++i)
132 y.axpy(shapeValues_[i], localValues_[i]);
133
134 return y;
135 }
136
137 private:
138 GridCreator const* creator_ = nullptr;
139 std::vector<Field> const* values_ = nullptr;
140 unsigned int comp_;
141 std::vector<std::uint8_t> const* types_ = nullptr;
142 std::vector<std::int64_t> const* offsets_ = nullptr;
143 std::vector<std::int64_t> const* connectivity_ = nullptr;
144
145 // Local Finite-Element
146 std::optional<LFE> lfe_ = std::nullopt;
147 std::optional<typename GridCreator::LocalGeometry> lgeo_ = std::nullopt;
148
149 // cache of local values
150 std::vector<DynamicVector<Field>> localValues_;
151 mutable std::vector<typename LB::Traits::RangeType> shapeValues_;
152 };
153
155 template <class LC>
156 class CellDataLocalFunction
157 {
158 public:
159 using LocalContext = LC;
160 using Domain = typename LC::Geometry::LocalCoordinate;
161 using Range = DynamicVector<Field>;
162 using Signature = Range(Domain);
163
164 public:
166 CellDataLocalFunction (GridCreator const* creator, std::vector<Field> const* values, unsigned int comp,
167 std::vector<std::uint8_t> const* /*types*/,
168 std::vector<std::int64_t> const* /*offsets*/,
169 std::vector<std::int64_t> const* /*connectivity*/)
170 : creator_(creator)
171 , values_(values)
172 , comp_(comp)
173 {}
174
175 CellDataLocalFunction () = default;
176
179 void bind (LocalContext const& element)
180 {
181 unsigned int idx = creator_->factory().insertionIndex(element);
182
183 // collect values on cells
184 DynamicVector<Field>& v = localValue_;
185 v.resize(comp_);
186
187 for (unsigned int j = 0; j < comp_; ++j)
188 v[j] = (*values_)[comp_*idx + j];
189 }
190
192 void unbind ()
193 {}
194
197 Range operator() (Domain const& local) const
198 {
199 return localValue_;
200 }
201
202 private:
203 GridCreator const* creator_ = nullptr;
204 std::vector<Field> const* values_ = nullptr;
205 unsigned int comp_;
206
207 // cache of local values
208 DynamicVector<Field> localValue_;
209 };
210
212 template <class LC>
213 using LocalFunction = std::conditional_t< std::is_same_v<Context,PointContext>,
214 PointDataLocalFunction<LC>,
215 CellDataLocalFunction<LC>>;
216
217 public:
220 LagrangeGridFunction (GridCreator const& creator, std::vector<Field> const& values,
221 std::string name ,unsigned int ncomps, Vtk::DataTypes dataType,
222 std::vector<std::uint8_t> const& types,
223 std::vector<std::int64_t> const& offsets,
224 std::vector<std::int64_t> const& connectivity)
225 : creator_(&creator)
226 , values_(&values)
227 , name_(std::move(name))
228 , ncomps_(ncomps)
229 , dataType_(dataType)
230 , types_(&types)
231 , offsets_(&offsets)
232 , connectivity_(&connectivity)
233 {}
234
236
238 Range operator() (Domain const& global) const
239 {
240 DUNE_THROW(Dune::NotImplemented, "Evaluation in global coordinates not implemented.");
241 return Range(ncomps_, 0);
242 }
243
245 EntitySet const& entitySet () const
246 {
247 return entitySet_;
248 }
249
250 std::string const& name () const
251 {
252 return name_;
253 }
254
255 int numComponents () const
256 {
257 return ncomps_;
258 }
259
261 {
262 return dataType_;
263 }
264
267 friend LocalFunction<typename EntitySet::Element> localFunction (LagrangeGridFunction const& gf)
268 {
269 return {gf.creator_, gf.values_, gf.ncomps_, gf.types_, gf.offsets_, gf.connectivity_};
270 }
271
272 private:
273 GridCreator const* creator_ = nullptr;
274 std::vector<Field> const* values_ = nullptr;
275 std::string name_ = "GridFunction";
276 unsigned int ncomps_ = 0;
278 std::vector<std::uint8_t> const* types_ = nullptr;
279 std::vector<std::int64_t> const* offsets_ = nullptr;
280 std::vector<std::int64_t> const* connectivity_ = nullptr;
281
282 EntitySet entitySet_;
283 };
284
285 } // end namespace Vtk
286} // end namespace Dune
Macro for wrapping error checks and throwing exceptions.
#define VTK_ASSERT(cond)
check if condition cond holds; otherwise, throw a VtkError.
Definition: errors.hh:29
Definition: writer.hh:13
LagrangeGridCreator(GridFactory< Grid > &) -> LagrangeGridCreator< Grid >
DataTypes
Definition: types.hh:52
Definition: lagrangegridcreator.hh:40
Grid-function representing values from a VTK file with local Lagrange interpolation of the values sto...
Definition: lagrangegridfunction.hh:25
DynamicVector< Field > Range
Definition: lagrangegridfunction.hh:42
EntitySet const & entitySet() const
Return a type that defines the element that can be iterated.
Definition: lagrangegridfunction.hh:245
Range(Domain) Signature
Definition: lagrangegridfunction.hh:43
int numComponents() const
Definition: lagrangegridfunction.hh:255
friend LocalFunction< typename EntitySet::Element > localFunction(LagrangeGridFunction const &gf)
Definition: lagrangegridfunction.hh:267
std::string const & name() const
Definition: lagrangegridfunction.hh:250
typename EntitySet::GlobalCoordinate Domain
Definition: lagrangegridfunction.hh:41
Range operator()(Domain const &global) const
Global evaluation. Not supported!
Definition: lagrangegridfunction.hh:238
LagrangeGridFunction(GridCreator const &creator, std::vector< Field > const &values, std::string name, unsigned int ncomps, Vtk::DataTypes dataType, std::vector< std::uint8_t > const &types, std::vector< std::int64_t > const &offsets, std::vector< std::int64_t > const &connectivity)
Definition: lagrangegridfunction.hh:220
Vtk::DataTypes dataType() const
Definition: lagrangegridfunction.hh:260
Definition: lagrangegridfunction.hh:34
typename Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition: lagrangegridfunction.hh:38
typename Element::Geometry::LocalCoordinate LocalCoordinate
Definition: lagrangegridfunction.hh:37
GridType Grid
Definition: lagrangegridfunction.hh:35
typename GridType::template Codim< 0 >::Entity Element
Definition: lagrangegridfunction.hh:36