Dune Core Modules (2.11.0)

gridfunction.hh
1#pragma once
2
3#include <memory>
4#include <numeric>
5#include <string>
6#include <type_traits>
7#include <vector>
8
10#include <dune/common/referencehelper.hh>
12#include <dune/vtk/types.hh>
13#include <dune/vtk/gridfunctions/componentmapper.hh>
14#include <dune/vtk/gridfunctions/localfunction.hh>
15#include <dune/vtk/gridfunctions/rangeproxy.hh>
16#include <dune/vtk/gridfunctions/rangetype.hh>
17#include <dune/vtk/gridfunctions/vtkfunctionwrapper.hh>
18
19namespace Dune::Vtk {
20namespace Impl {
21
22template <class GridViewType, class ComponentMapperType>
23struct GridFunctionDefinition
24{
25 using GridView = GridViewType;
26 using ComponentMapper = ComponentMapperType;
27 using Element = typename GridView::template Codim<0>::Entity;
28 using Domain = typename Element::Geometry::GlobalCoordinate;
29 using LocalFunction = Vtk::LocalFunction<Element,ComponentMapper>;
30
31 // Definition of the interface a LocalFunction must fulfill
32 struct Interface
33 {
34 virtual ~Interface() = default;
35 virtual Interface* clone () const = 0;
36 virtual double evaluate (int, Domain const&) const = 0;
37 virtual LocalFunction asLocalFunction(std::vector<int> const&) const = 0;
38
39 };
40
41 // Templatized implementation of the interface
42 template <class Impl>
43 struct Model : public Interface
44 {
45 template <class I, disableCopyMove<Model,I> = 0>
46 Model(I&& impl) : impl_(std::forward<I>(impl)) {}
47 Model* clone() const final { return new Model(*this); }
48 auto& impl() { return resolveRef(impl_); }
49 const auto& impl() const { return resolveRef(impl_); }
50
51 double evaluate(int i, Domain const& x) const final {
52 return ComponentMapper::map(i, x, impl()); }
53 LocalFunction asLocalFunction(std::vector<int> const& comp) const final {
54 return LocalFunction{localFunction(impl()), comp};
55 }
56
57 private:
58 Impl impl_;
59 };
60
61 // The Concept definition of a LocalFunction
62 struct Concept
63 {
64 using LFConcept = typename LocalFunctionDefinition<Element,ComponentMapper>::Concept;
65
66 template <class F>
67 auto require(F&& f) -> decltype
68 (
69 Dune::Concept::requireConcept<LFConcept>(localFunction(f))
70 );
71 };
72};
73
74} // end namespace Impl
75
76
77
80
83template <class GridView, class ComponentMapper = DefaultComponentMapper>
85{
86 using Definition = Impl::GridFunctionDefinition<GridView,ComponentMapper>;
87 using Interface = typename Definition::Interface;
88 using Domain = typename Definition::Domain;
89 using LocalFunction = typename Definition::LocalFunction;
90
91 template <class F>
92 using Model = typename Definition::template Model<std::remove_reference_t<F>>;
93
94public:
97 template <class F,
98 class R = RangeType<ResolveRef_t<F>,Domain>>
99 GridFunction (F&& f, std::string name, int numComponents = dimRange<R>())
100 : GridFunction(std::forward<F>(f), Vtk::FieldInfo{std::move(name), numComponents, RangeTypes::UNSPECIFIED, DataTypes::FLOAT64})
101 {}
102
103 template <class F, disableCopyMove<GridFunction, F> = 0,
104 decltype(std::declval<F>().name(), bool{}) = true,
105 decltype(std::declval<F>().numComponents(), bool{}) = true,
106 decltype(std::declval<F>().dataType(), bool{}) = true>
107 GridFunction (F&& f)
108 : GridFunction(std::forward<F>(f), Vtk::FieldInfo{f.name(), f.numComponents(), Vtk::RangeTypes::UNSPECIFIED, f.dataType()})
109 {}
110
113 template <class F>
114 GridFunction (F&& f, const Vtk::FieldInfo& info)
115 : interface_(std::make_unique<Model<F>>(std::forward<F>(f)))
116 , name_(info.name())
117 {
118 static_assert(models<typename Definition::Concept,ResolveRef_t<F>>(),
119 "Implementation does not model the Function concept.");
120
121 setComponents(info.size());
122 setRangeType(info.rangeType());
123 setDataType(info.dataType());
124 }
125
128 GridFunction () = default;
129 GridFunction (GridFunction &&) = default;
130 GridFunction& operator= (GridFunction&&) = default;
131
134 : interface_(other.interface_ ? other.interface_->clone() : nullptr)
135 , name_(other.name_)
136 , components_(other.components_)
137 , dataType_(other.dataType_)
138 , rangeType_(other.rangeType_)
139 {}
140
142 GridFunction& operator= (const GridFunction& other)
143 {
144 return *this = GridFunction(other);
145 }
146
148 Impl::RangeProxy<GridFunction,Domain> operator() (Domain const& x) const
149 {
150 return {this, x};
151 }
152
154 double evaluate (int i, Domain const& x) const
155 {
156 return i < int(components_.size()) ? interface_->evaluate(components_[i], x) : 0.0;
157 }
158
160 friend LocalFunction localFunction (GridFunction const& self)
161 {
162 return self.interface_->asLocalFunction(self.components_);
163 }
164
166 std::string const& name () const
167 {
168 return name_;
169 }
170
172 void setName (std::string name)
173 {
174 name_ = std::move(name);
175 }
176
178 int numComponents () const
179 {
180 return rangeType_ == Vtk::RangeTypes::SCALAR ? 1 :
181 rangeType_ == Vtk::RangeTypes::VECTOR ? 3 :
182 rangeType_ == Vtk::RangeTypes::TENSOR ? 9 : int(components_.size());
183 }
184
186 const std::vector<int>& components () const
187 {
188 return components_;
189 }
190
192 void setComponents (std::vector<int> components)
193 {
194 components_ = components;
195 }
196
198 void setComponents (int ncomps)
199 {
200 setComponents(allComponents(ncomps));
201 }
202
204 Vtk::DataTypes dataType () const
205 {
206 return dataType_;
207 }
208
210 void setDataType (Vtk::DataTypes type)
211 {
212 dataType_ = type;
213 }
214
216 Vtk::RangeTypes rangeType () const
217 {
218 return rangeType_;
219 }
220
222 void setRangeType (Vtk::RangeTypes type, std::size_t ncomp = 1)
223 {
224 rangeType_ = type;
225 if (type == Vtk::RangeTypes::AUTO)
226 rangeType_ = rangeTypeOf(ncomp);
227 }
228
230 void setFieldInfo (const Vtk::FieldInfo& info)
231 {
232 setName(info.name());
233 setComponents(info.size());
234 setRangeType(info.rangeType());
235 setDataType(info.dataType());
236 }
237
238private:
239 std::vector<int> allComponents (int n) const
240 {
241 std::vector<int> components(n);
242 std::iota(components.begin(), components.end(), 0);
243 return components;
244 }
245
246private:
247 std::unique_ptr<Interface> interface_;
248 std::string name_{};
249 std::vector<int> components_{};
250 Vtk::DataTypes dataType_ = Vtk::DataTypes::FLOAT64;
251 Vtk::RangeTypes rangeType_ = Vtk::RangeTypes::UNSPECIFIED;
252};
253
254} // end namespace Dune::Vtk
A Vtk::GridFunction is a function-like object that can be bound to a grid element an that provides an...
Definition: gridfunction.hh:85
void setComponents(std::vector< int > components)
Set the components of the Range to visualize.
Definition: gridfunction.hh:192
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:99
int numComponents() const
Return the number of components of the Range as it is written to the file.
Definition: gridfunction.hh:178
void setName(std::string name)
Set the function name.
Definition: gridfunction.hh:172
GridFunction(F &&f, const Vtk::FieldInfo &info)
Constructor. Pass any type F supporting the Function interface wrappers and the FieldInfo describing ...
Definition: gridfunction.hh:114
void setFieldInfo(const Vtk::FieldInfo &info)
Set all the parameters from a FieldInfo object.
Definition: gridfunction.hh:230
std::string const & name() const
Return a name associated with the function.
Definition: gridfunction.hh:166
GridFunction(const GridFunction &other)
Copy constructor, clones the underlying implementation.
Definition: gridfunction.hh:133
friend LocalFunction localFunction(GridFunction const &self)
Create the associated local function.
Definition: gridfunction.hh:160
const std::vector< int > & components() const
Return the components vector.
Definition: gridfunction.hh:186
void setDataType(Vtk::DataTypes type)
Set the data-type for the components.
Definition: gridfunction.hh:210
Vtk::DataTypes dataType() const
Return the VTK Datatype associated with the functions range type.
Definition: gridfunction.hh:204
Vtk::RangeTypes rangeType() const
The category of the range, SCALAR, VECTOR, TENSOR, or UNSPECIFIED.
Definition: gridfunction.hh:216
Impl::RangeProxy< GridFunction, Domain > operator()(Domain const &x) const
Return a proxy object to access the components of the range vector.
Definition: gridfunction.hh:148
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:222
double evaluate(int i, Domain const &x) const
Evaluate the ith component of the Range value at coordinate x
Definition: gridfunction.hh:154
void setComponents(int ncomps)
Set the number of components of the Range and generate component range [0...ncomps)
Definition: gridfunction.hh:198
A Vtk::LocalFunction is a function-like object that can be bound to a grid element an that provides a...
Definition: localfunction.hh:74
Infrastructure for concepts.
Traits for type conversions and type information.
constexpr T & resolveRef(T &gf) noexcept
Helper function to resolve std::reference_wrapper.
Definition: referencehelper.hh:47
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Feb 14, 23:39, 2026)