dune-vtk 2.12
Loading...
Searching...
No Matches
lagrangegridfunction.hh
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <vector>
5
13
14namespace Dune::Vtk
15{
16 template <class GridType>
18
21 template <class GridType, class RangeType, class Context>
23 {
24 using Grid = GridType;
25 using Field = typename Dune::FieldTraits<RangeType>::field_type;
26
29
30 // The range must fulfill the DenseVector interface. Scalar types
31 // are simply wrapped into a FieldVector<T,1> on extraction. This
32 // can then be converted into the type `T` on evaluation.
35
36 public:
37 struct EntitySet
38 {
39 using Grid = GridType;
40 using Element = typename GridType::template Codim<0>::Entity;
41 using LocalCoordinate = typename Element::Geometry::LocalCoordinate;
42 using GlobalCoordinate = typename Element::Geometry::GlobalCoordinate;
43 };
44
48
49 private:
50 template <class LC>
51 class PointDataLocalFunction
52 {
54 using LB = typename LFE::Traits::LocalBasisType;
55
56 public:
57 using LocalContext = LC;
58 using Domain = typename LC::Geometry::LocalCoordinate;
59 using Range = RangeType;
60 using Signature = Range(Domain);
61
62 public:
64 PointDataLocalFunction (GridCreator const* creator, std::vector<Field> const* values, unsigned int comp,
65 std::vector<std::uint8_t> const* types,
66 std::vector<std::int64_t> const* offsets,
67 std::vector<std::int64_t> const* connectivity)
68 : creator_(creator)
69 , values_(values)
70 , comp_(comp)
71 , types_(types)
72 , offsets_(offsets)
73 , connectivity_(connectivity)
74 {}
75
76 PointDataLocalFunction () = default;
77
79
85 void bind (LocalContext const& element)
86 {
87 unsigned int insertionIndex = creator_->factory().insertionIndex(element);
88
89 std::int64_t shift = (insertionIndex == 0 ? 0 : (*offsets_)[insertionIndex-1]);
90 std::int64_t numNodes = (*offsets_)[insertionIndex] - shift;
91 [[maybe_unused]] std::int64_t maxNumNodes = numLagrangePoints(element.type(), 20);
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 auto& v = localValues_[i0];
107 Impl::resizeRange(v,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 auto y = Impl::constructRange<RangeWrapperType>(comp_, 0);
131 for (std::size_t i = 0; i < shapeValues_.size(); ++i)
132 y.axpy(shapeValues_[i], localValues_[i]);
133
134 return Range(y);
135 }
136
137 friend DummyLocalFunctionDerivative<LocalContext,Range,Domain> derivative (PointDataLocalFunction const&) { return {}; }
138
139 private:
140 GridCreator const* creator_ = nullptr;
141 std::vector<Field> const* values_ = nullptr;
142 unsigned int comp_;
143 std::vector<std::uint8_t> const* types_ = nullptr;
144 std::vector<std::int64_t> const* offsets_ = nullptr;
145 std::vector<std::int64_t> const* connectivity_ = nullptr;
146
147 // Local Finite-Element
148 std::optional<LFE> lfe_ = std::nullopt;
150
151 // cache of local values
154 };
155
157 template <class LC>
158 class CellDataLocalFunction
159 {
160 public:
161 using LocalContext = LC;
162 using Domain = typename LC::Geometry::LocalCoordinate;
163 using Range = RangeType;
164 using Signature = Range(Domain);
165
166 public:
168 CellDataLocalFunction (GridCreator const* creator, std::vector<Field> const* values, unsigned int comp,
169 std::vector<std::uint8_t> const* /*types*/,
170 std::vector<std::int64_t> const* /*offsets*/,
171 std::vector<std::int64_t> const* /*connectivity*/)
172 : creator_(creator)
173 , values_(values)
174 , comp_(comp)
175 {}
176
177 CellDataLocalFunction () = default;
178
181 void bind (LocalContext const& element)
182 {
183 unsigned int idx = creator_->factory().insertionIndex(element);
184
185 // collect values on cells
186 auto& v = localValue_;
187 Impl::resizeRange(v,comp_);
188
189 for (unsigned int j = 0; j < comp_; ++j)
190 v[j] = (*values_)[comp_*idx + j];
191 }
192
194 void unbind ()
195 {}
196
199 Range operator() (Domain const& local) const
200 {
201 return Range(localValue_);
202 }
203
204 private:
205 GridCreator const* creator_ = nullptr;
206 std::vector<Field> const* values_ = nullptr;
207 unsigned int comp_;
208
209 // cache of local values
210 RangeWrapperType localValue_;
211 };
212
214 template <class LC>
216 PointDataLocalFunction<LC>,
217 CellDataLocalFunction<LC>>;
218
219 public:
223 std::string name ,unsigned int ncomps, Vtk::DataTypes dataType,
224 std::vector<std::uint8_t> const& types,
225 std::vector<std::int64_t> const& offsets,
226 std::vector<std::int64_t> const& connectivity)
227 : creator_(&creator)
228 , values_(&values)
229 , name_(std::move(name))
230 , ncomps_(ncomps)
231 , dataType_(dataType)
232 , types_(&types)
233 , offsets_(&offsets)
234 , connectivity_(&connectivity)
235 {}
236
238
241 {
242 DUNE_THROW(Dune::NotImplemented, "Evaluation in global coordinates not implemented.");
243 return Impl::constructRange<Range>(ncomps_, 0);
244 }
245
247 EntitySet const& entitySet () const
248 {
249 return entitySet_;
250 }
251
252 std::string const& name () const
253 {
254 return name_;
255 }
256
257 int numComponents () const
258 {
259 return ncomps_;
260 }
261
263 {
264 return dataType_;
265 }
266
270 {
271 return {gf.creator_, gf.values_, gf.ncomps_, gf.types_, gf.offsets_, gf.connectivity_};
272 }
273
274 private:
275 GridCreator const* creator_ = nullptr;
276 std::vector<Field> const* values_ = nullptr;
277 std::string name_ = "GridFunction";
278 unsigned int ncomps_ = 0;
280 std::vector<std::uint8_t> const* types_ = nullptr;
281 std::vector<std::int64_t> const* offsets_ = nullptr;
282 std::vector<std::int64_t> const* connectivity_ = nullptr;
283
284 EntitySet entitySet_;
285 };
286
287} // end namespace Dune::Vtk
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
STL namespace.
Definition writer.hh:16
LagrangeGridCreator(GridFactory< Grid > &) -> LagrangeGridCreator< Grid >
std::decay_t< std::invoke_result_t< F, D > > RangeType
Result type of the function F when called with an argument of type D.
Definition rangetype.hh:21
DataTypes
Definition types.hh:51
virtual void operator()()=0
#define DUNE_THROW(E,...)
const GlobalIndex & global() const
LocalIndex & local()
Definition lagrangegridcreator.hh:38
Grid-function representing values from a VTK file with local Lagrange interpolation of the values sto...
Definition lagrangegridfunction.hh:23
EntitySet const & entitySet() const
Return a type that defines the element that can be iterated.
Definition lagrangegridfunction.hh:247
int numComponents() const
Definition lagrangegridfunction.hh:257
friend LocalFunction< typename EntitySet::Element > localFunction(LagrangeGridFunction const &gf)
Definition lagrangegridfunction.hh:269
Vtk::DataTypes dataType() const
Definition lagrangegridfunction.hh:262
Range(Domain) Signature
Definition lagrangegridfunction.hh:47
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:222
typename EntitySet::GlobalCoordinate Domain
Definition lagrangegridfunction.hh:45
std::string const & name() const
Definition lagrangegridfunction.hh:252
RangeType Range
Definition lagrangegridfunction.hh:46
Definition lagrangegridfunction.hh:38
typename Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition lagrangegridfunction.hh:42
typename GridType::template Codim< 0 >::Entity Element
Definition lagrangegridfunction.hh:40
GridType Grid
Definition lagrangegridfunction.hh:39
typename Element::Geometry::LocalCoordinate LocalCoordinate
Definition lagrangegridfunction.hh:41
T bind(T... args)