dune-vtk 2.12
Loading...
Searching...
No Matches
gridfunction.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 <algorithm>
6#include <array>
7#include <memory>
8#include <numeric>
9#include <string>
10#include <type_traits>
11#include <vector>
12
16#include <dune/vtk/types.hh>
22
23namespace Dune::Vtk {
24namespace Impl {
25
26template <class GridViewType, class ComponentMapperType>
27struct GridFunctionDefinition
28{
29 using GridView = GridViewType;
30 using ComponentMapper = ComponentMapperType;
31 using Element = typename GridView::template Codim<0>::Entity;
32 using Domain = typename Element::Geometry::GlobalCoordinate;
33 using LocalFunction = Vtk::LocalFunction<Element,ComponentMapper>;
34
35 // Definition of the interface a LocalFunction must fulfill
36 struct Interface
37 {
38 virtual ~Interface() = default;
39 virtual Interface* clone () const = 0;
40 virtual double evaluate (int, Domain const&) const = 0;
41 virtual LocalFunction asLocalFunction(std::vector<int> const&) const = 0;
42
43 };
44
45 // Templatized implementation of the interface
46 template <class Impl>
47 struct Model : public Interface
48 {
49 template <class I, disableCopyMove<Model,I> = 0>
50 Model(I&& impl) : impl_(std::forward<I>(impl)) {}
51 Model* clone() const final { return new Model(*this); }
52 auto& impl() { return resolveRef(impl_); }
53 const auto& impl() const { return resolveRef(impl_); }
54
55 double evaluate(int i, Domain const& x) const final {
56 return ComponentMapper::map(i, x, impl()); }
57 LocalFunction asLocalFunction(std::vector<int> const& comp) const final {
58 return LocalFunction{localFunction(impl()), comp};
59 }
60
61 private:
62 Impl impl_;
63 };
64
65 // The Concept definition of a LocalFunction
66 struct Concept
67 {
68 using LFConcept = typename LocalFunctionDefinition<Element,ComponentMapper>::Concept;
69
70 template <class F>
71 auto require(F&& f) -> decltype
72 (
73 Dune::Concept::requireConcept<LFConcept>(localFunction(f))
74 );
75 };
76};
77
78} // end namespace Impl
79
80
81
84
87template <class GridView, class ComponentMapper = DefaultComponentMapper>
89{
90 using Definition = Impl::GridFunctionDefinition<GridView,ComponentMapper>;
91 using Interface = typename Definition::Interface;
92 using Domain = typename Definition::Domain;
93 using LocalFunction = typename Definition::LocalFunction;
94
95 template <class F>
96 using Model = typename Definition::template Model<std::remove_reference_t<F>>;
97
98public:
101 template <class F,
102 class R = RangeType<ResolveRef_t<F>,Domain>>
106
109 template <class F, disableCopyMove<GridFunction, F> = 0,
110 decltype(std::declval<F>().name(), bool{}) = true,
111 decltype(std::declval<F>().numComponents(), bool{}) = true,
112 decltype(std::declval<F>().dataType(), bool{}) = true>
114 : GridFunction(std::forward<F>(f), Vtk::FieldInfo{f.name(), f.numComponents(), rangeTypeImpl(f, Dune::PriorityTag<1>{}), f.dataType()})
115 {}
116
119 template <class F>
121 : interface_(std::make_unique<Model<F>>(std::forward<F>(f)))
122 , name_(info.name())
123 , shape_(staticShape<ResolveRef_t<F>>())
124 {
126 "Implementation does not model the Function concept.");
127
128 setComponents(info.size());
129 setRangeType(info.rangeType());
130 setDataType(info.dataType());
131 }
132
135 GridFunction () = default;
138
141 : interface_(other.interface_ ? other.interface_->clone() : nullptr)
142 , name_(other.name_)
143 , components_(other.components_)
144 , shape_(other.shape_)
145 , outputMap_(other.outputMap_)
146 , dataType_(other.dataType_)
147 , rangeType_(other.rangeType_)
148 {}
149
152 {
153 return *this = GridFunction(other);
154 }
155
157 Impl::RangeProxy<GridFunction,Domain> operator() (Domain const& x) const
158 {
159 return {this, x};
160 }
161
163 double evaluate (int i, Domain const& x) const
164 {
165 return i < int(outputMap_.size()) && outputMap_[i] >= 0
166 ? interface_->evaluate(outputMap_[i], x) : 0.0;
167 }
168
170 friend LocalFunction localFunction (GridFunction const& self)
171 {
172 return self.interface_->asLocalFunction(self.outputMap_);
173 }
174
176 std::string const& name () const
177 {
178 return name_;
179 }
180
183 {
184 name_ = std::move(name);
185 }
186
188 int numComponents () const
189 {
190 return rangeType_ == Vtk::RangeTypes::SCALAR ? 1 :
191 rangeType_ == Vtk::RangeTypes::VECTOR ? 3 :
192 rangeType_ == Vtk::RangeTypes::TENSOR ? 9 : int(components_.size());
193 }
194
197 {
198 return components_;
199 }
200
203 {
204 components_ = components;
205 updateOutputMap();
206 }
207
209 void setComponents (int ncomps)
210 {
211 setComponents(allComponents(ncomps));
212 }
213
216 {
217 return dataType_;
218 }
219
222 {
223 dataType_ = type;
224 }
225
228 {
229 return rangeType_;
230 }
231
234 {
235 rangeType_ = type;
236 if (type == Vtk::RangeTypes::AUTO)
237 rangeType_ = rangeTypeOf(ncomp);
238 updateOutputMap();
239 }
240
242 void setFieldInfo (const Vtk::FieldInfo& info)
243 {
244 setName(info.name());
245 setComponents(info.size());
246 setRangeType(info.rangeType());
247 setDataType(info.dataType());
248 }
249
250private:
251 template <class F>
252 static constexpr std::array<int,2> staticShape ()
253 {
254 if constexpr (std::is_invocable_v<F const&, Domain const&>)
256 else
257 return {0,0};
258 }
259
260 // Shape {rows,cols} of the (selected) range interpreted as a tensor
261 std::array<int,2> tensorShape () const
262 {
263 const int n = components_.size();
264 if (shape_[0] > 0 && shape_[0]*shape_[1] <= n)
265 return shape_;
266 for (int k : {1,2,3})
267 if (k*k == n) return {k,k};
268 return {(n+2)/3, 3}; // unknown: fill the 3x3 tensor row-wise
269 }
270
271 // Map each output slot [0,numComponents()) to a range component, or -1 for zero padding
272 void updateOutputMap ()
273 {
274 outputMap_.assign(numComponents(), -1);
275 if (rangeType_ == Vtk::RangeTypes::TENSOR) {
276 auto [rows,cols] = tensorShape();
277 for (int r = 0; r < std::min(rows,3); ++r)
278 for (int c = 0; c < std::min(cols,3); ++c)
279 if (int k = r*cols + c; k < int(components_.size()))
280 outputMap_[3*r + c] = components_[k];
281 } else {
282 for (std::size_t i = 0; i < std::min(outputMap_.size(), components_.size()); ++i)
283 outputMap_[i] = components_[i];
284 }
285 }
286
287 std::vector<int> allComponents (int n) const
288 {
289 std::vector<int> components(n);
290 std::iota(components.begin(), components.end(), 0);
291 return components;
292 }
293
294 // Forward the range-type of `f` if available
295 template <class F>
296 static auto rangeTypeImpl (F const& f, Dune::PriorityTag<1>)
297 -> decltype(Vtk::RangeTypes(f.rangeType()))
298 {
299 return f.rangeType();
300 }
301
302 template <class F>
303 static Vtk::RangeTypes rangeTypeImpl (F const& /*f*/, Dune::PriorityTag<0>)
304 {
306 }
307
308private:
310 std::string name_{};
311 std::vector<int> components_{};
312 std::array<int,2> shape_{0,0};
313 std::vector<int> outputMap_{};
316};
317
318} // end namespace Dune::Vtk
STL namespace.
Definition datacollector.hh:19
constexpr std::array< int, 2 > rangeShape()
Static shape {rows,cols} of a matrix-valued range type, or {0,0} if unknown or not a matrix.
Definition rangetype.hh:59
RangeTypes
Type used to determine whether to limit output components to e.g. 3 (vector), or 9 (tensor)
Definition types.hh:36
DataTypes
Definition types.hh:53
Vtk::RangeTypes rangeTypeOf(Dune::VTK::FieldInfo::Type t)
Definition types.cc:57
const char * name()
auto rows(Matrix const &matrix)
auto cols(Matrix const &matrix)
SLList< T, A > & operator=(const SLList< T, A > &other)
constexpr T & resolveRef(T &gf) noexcept
virtual void operator()()=0
virtual ~Interface()
Implementation & impl()
GridView(const Implementation &imp)
ALBERTA EL Element
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
CellTypeDataCollector(GridView const &gridView, Vtk::CellType::Parametrization param=Vtk::CellType::LINEAR)
Definition celltypedatacollector.hh:42
A Vtk::GridFunction is a function-like object that can be bound to a grid element an that provides an...
Definition gridfunction.hh:89
void setComponents(std::vector< int > components)
Set the components of the Range to visualize.
Definition gridfunction.hh:202
GridFunction(F &&f, std::string name, int numComponents=dimRange< R >())
Constructor. Pass any type F supporting the Function interface wrappers the name of the function and ...
Definition gridfunction.hh:103
int numComponents() const
Return the number of components of the Range as it is written to the file.
Definition gridfunction.hh:188
void setName(std::string name)
Set the function name.
Definition gridfunction.hh:182
GridFunction(F &&f, const Vtk::FieldInfo &info)
Constructor. Pass any type F supporting the Function interface wrappers and the FieldInfo describing ...
Definition gridfunction.hh:120
GridFunction(GridFunction &&)=default
void setFieldInfo(const Vtk::FieldInfo &info)
Set all the parameters from a FieldInfo object.
Definition gridfunction.hh:242
std::string const & name() const
Return a name associated with the function.
Definition gridfunction.hh:176
GridFunction(const GridFunction &other)
Copy constructor, clones the underlying implementation.
Definition gridfunction.hh:140
GridFunction(F &&f)
Constructor. Pass any type F supporting the Function interface wrappers that provides the field infor...
Definition gridfunction.hh:113
friend LocalFunction localFunction(GridFunction const &self)
Create the associated local function.
Definition gridfunction.hh:170
const std::vector< int > & components() const
Return the components vector.
Definition gridfunction.hh:196
void setDataType(Vtk::DataTypes type)
Set the data-type for the components.
Definition gridfunction.hh:221
Vtk::DataTypes dataType() const
Return the VTK Datatype associated with the functions range type.
Definition gridfunction.hh:215
Vtk::RangeTypes rangeType() const
The category of the range, SCALAR, VECTOR, TENSOR, or UNSPECIFIED.
Definition gridfunction.hh:227
GridFunction()=default
Default Constructor, creates an invalid function that must be assigned a valid function object.
void setRangeType(Vtk::RangeTypes type, std::size_t ncomp=1)
Set the category of the range, SCALAR, VECTOR, TENSOR, or UNSPECIFIED.
Definition gridfunction.hh:233
double evaluate(int i, Domain const &x) const
Evaluate the ith component of the Range value at coordinate x
Definition gridfunction.hh:163
void setComponents(int ncomps)
Set the number of components of the Range and generate component range [0...ncomps)
Definition gridfunction.hh:209
Definition types.hh:250
int size() const
The number of components in the data field.
Definition types.hh:275
std::string const & name() const
The name of the data field.
Definition types.hh:269
Vtk::RangeTypes rangeType() const
Return the category of the stored range.
Definition types.hh:281
Vtk::DataTypes dataType() const
Return the data tpe of the data field.
Definition types.hh:287
T forward(T... args)
T iota(T... args)
T min(T... args)
T size(T... args)