dune-vtk 2.12
Loading...
Searching...
No Matches
lagrangegridfunction.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 <optional>
6#include <vector>
7
15
16namespace Dune::Vtk
17{
18 template <class GridType>
20
23 template <class GridType, class RangeType, class Context>
25 {
26 using Grid = GridType;
27 using Field = typename Dune::FieldTraits<RangeType>::field_type;
28
31
32 // The range must fulfill the DenseVector interface. Scalar types
33 // are simply wrapped into a FieldVector<T,1> on extraction. This
34 // can then be converted into the type `T` on evaluation.
37
38 public:
39 struct EntitySet
40 {
41 using Grid = GridType;
42 using Element = typename GridType::template Codim<0>::Entity;
43 using LocalCoordinate = typename Element::Geometry::LocalCoordinate;
44 using GlobalCoordinate = typename Element::Geometry::GlobalCoordinate;
45 };
46
50
51 private:
52 template <class LC>
53 class PointDataLocalFunction
54 {
56 using LB = typename LFE::Traits::LocalBasisType;
57
58 public:
59 using LocalContext = LC;
60 using Domain = typename LC::Geometry::LocalCoordinate;
61 using Range = RangeType;
62 using Signature = Range(Domain);
63
64 public:
66 PointDataLocalFunction (GridCreator const* creator, std::vector<Field> const* values, unsigned int comp,
67 std::vector<std::uint8_t> const* types,
68 std::vector<std::int64_t> const* offsets,
69 std::vector<std::int64_t> const* connectivity)
70 : creator_(creator)
71 , values_(values)
72 , comp_(comp)
73 , types_(types)
74 , offsets_(offsets)
75 , connectivity_(connectivity)
76 {}
77
78 PointDataLocalFunction () = default;
79
81
87 void bind (LocalContext const& element)
88 {
89 unsigned int insertionIndex = creator_->factory().insertionIndex(element);
90
91 std::int64_t shift = (insertionIndex == 0 ? 0 : (*offsets_)[insertionIndex-1]);
92 std::int64_t numNodes = (*offsets_)[insertionIndex] - shift;
95
96 int order = creator_->order(element.type(), numNodes);
97 VTK_ASSERT(order > 0 && order < 20);
98
99 // construct a local finite-element with the corresponding order and Lagrange points
100 // as stored in the file
101 lfe_.emplace(LFE{element.type(), (unsigned int)(order)});
102 lgeo_.emplace(creator_->localGeometry(element));
103
104 // collect values on lagrange nodes
105 localValues_.resize(numNodes);
106 for (std::int64_t i = shift, i0 = 0; i < (*offsets_)[insertionIndex]; ++i, ++i0) {
107 std::int64_t idx = (*connectivity_)[i];
108 auto& v = localValues_[i0];
109 Impl::resizeRange(v,comp_);
110 for (unsigned int j = 0; j < comp_; ++j)
111 v[j] = (*values_)[comp_*idx + j];
112 }
113 }
114
116 void unbind ()
117 {
118 lfe_.reset();
119 lgeo_.reset();
120 }
121
124 // NOTE: do we need to transform the local coordinates?
125 Range operator() (Domain const& local) const
126 {
127 assert(!!lfe_);
128 auto const& lb = lfe_->localBasis();
129 lb.evaluateFunction(lgeo_->global(local), shapeValues_);
130 assert(shapeValues_.size() == localValues_.size());
131
132 auto y = Impl::constructRange<RangeWrapperType>(comp_, 0);
133 for (std::size_t i = 0; i < shapeValues_.size(); ++i)
134 y.axpy(shapeValues_[i][0], localValues_[i]);
135
136 return Impl::unwrapRange<Range>(y);
137 }
138
139 friend DummyLocalFunctionDerivative<LocalContext,Range,Domain> derivative (PointDataLocalFunction const&) { return {}; }
140
141 private:
142 GridCreator const* creator_ = nullptr;
143 std::vector<Field> const* values_ = nullptr;
144 unsigned int comp_;
145 std::vector<std::uint8_t> const* types_ = nullptr;
146 std::vector<std::int64_t> const* offsets_ = nullptr;
147 std::vector<std::int64_t> const* connectivity_ = nullptr;
148
149 // Local Finite-Element
150 std::optional<LFE> lfe_ = std::nullopt;
152
153 // cache of local values
156 };
157
159 template <class LC>
160 class CellDataLocalFunction
161 {
162 public:
163 using LocalContext = LC;
164 using Domain = typename LC::Geometry::LocalCoordinate;
165 using Range = RangeType;
166 using Signature = Range(Domain);
167
168 public:
170 CellDataLocalFunction (GridCreator const* creator, std::vector<Field> const* values, unsigned int comp,
171 std::vector<std::uint8_t> const* /*types*/,
172 std::vector<std::int64_t> const* /*offsets*/,
173 std::vector<std::int64_t> const* /*connectivity*/)
174 : creator_(creator)
175 , values_(values)
176 , comp_(comp)
177 {}
178
179 CellDataLocalFunction () = default;
180
183 void bind (LocalContext const& element)
184 {
185 unsigned int idx = creator_->factory().insertionIndex(element);
186
187 // collect values on cells
188 auto& v = localValue_;
189 Impl::resizeRange(v,comp_);
190
191 for (unsigned int j = 0; j < comp_; ++j)
192 v[j] = (*values_)[comp_*idx + j];
193 }
194
196 void unbind ()
197 {}
198
201 Range operator() (Domain const& local) const
202 {
203 return Impl::unwrapRange<Range>(localValue_);
204 }
205
206 private:
207 GridCreator const* creator_ = nullptr;
208 std::vector<Field> const* values_ = nullptr;
209 unsigned int comp_;
210
211 // cache of local values
212 RangeWrapperType localValue_;
213 };
214
216 template <class LC>
218 PointDataLocalFunction<LC>,
219 CellDataLocalFunction<LC>>;
220
221 public:
225 std::string name ,unsigned int ncomps, Vtk::DataTypes dataType,
226 std::vector<std::uint8_t> const& types,
227 std::vector<std::int64_t> const& offsets,
228 std::vector<std::int64_t> const& connectivity)
229 : creator_(&creator)
230 , values_(&values)
231 , name_(std::move(name))
232 , ncomps_(ncomps)
233 , dataType_(dataType)
234 , types_(&types)
235 , offsets_(&offsets)
236 , connectivity_(&connectivity)
237 {}
238
240
243 {
244 DUNE_THROW(Dune::NotImplemented, "Evaluation in global coordinates not implemented.");
245 return Impl::constructRange<Range>(ncomps_, 0);
246 }
247
249 EntitySet const& entitySet () const
250 {
251 return entitySet_;
252 }
253
254 std::string const& name () const
255 {
256 return name_;
257 }
258
259 int numComponents () const
260 {
261 return ncomps_;
262 }
263
265 {
266 return dataType_;
267 }
268
272 {
273 return {gf.creator_, gf.values_, gf.ncomps_, gf.types_, gf.offsets_, gf.connectivity_};
274 }
275
276 private:
277 GridCreator const* creator_ = nullptr;
278 std::vector<Field> const* values_ = nullptr;
279 std::string name_ = "GridFunction";
280 unsigned int ncomps_ = 0;
282 std::vector<std::uint8_t> const* types_ = nullptr;
283 std::vector<std::int64_t> const* offsets_ = nullptr;
284 std::vector<std::int64_t> const* connectivity_ = nullptr;
285
286 EntitySet entitySet_;
287 };
288
289} // 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:31
STL namespace.
Definition datacollector.hh:19
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:24
DataTypes
Definition types.hh:53
virtual void operator()()=0
#define DUNE_THROW(E,...)
const GlobalIndex & global() const
LocalIndex & local()
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
Grid-function representing values from a VTK file with local Lagrange interpolation of the values sto...
Definition lagrangegridfunction.hh:25
EntitySet const & entitySet() const
Return a type that defines the element that can be iterated.
Definition lagrangegridfunction.hh:249
int numComponents() const
Definition lagrangegridfunction.hh:259
friend LocalFunction< typename EntitySet::Element > localFunction(LagrangeGridFunction const &gf)
Definition lagrangegridfunction.hh:271
Vtk::DataTypes dataType() const
Definition lagrangegridfunction.hh:264
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:224
typename EntitySet::GlobalCoordinate Domain
Definition lagrangegridfunction.hh:47
std::string const & name() const
Definition lagrangegridfunction.hh:254
RangeType Range
Definition lagrangegridfunction.hh:48
Definition lagrangegridfunction.hh:40
typename Element::Geometry::GlobalCoordinate GlobalCoordinate
Definition lagrangegridfunction.hh:44
typename GridType::template Codim< 0 >::Entity Element
Definition lagrangegridfunction.hh:42
GridType Grid
Definition lagrangegridfunction.hh:41
typename Element::Geometry::LocalCoordinate LocalCoordinate
Definition lagrangegridfunction.hh:43
T bind(T... args)