dune-vtk 2.12
Loading...
Searching...
No Matches
gridfunction.hh
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <numeric>
5#include <string>
6#include <type_traits>
7#include <vector>
8
12#include <dune/vtk/types.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>())
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>
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;
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
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
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
187 {
188 return components_;
189 }
190
193 {
194 components_ = components;
195 }
196
198 void setComponents (int ncomps)
199 {
200 setComponents(allComponents(ncomps));
201 }
202
205 {
206 return dataType_;
207 }
208
211 {
212 dataType_ = type;
213 }
214
217 {
218 return rangeType_;
219 }
220
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 {
243 return components;
244 }
245
246private:
248 std::string name_{};
249 std::vector<int> components_{};
252};
253
254} // end namespace Dune::Vtk
STL namespace.
Definition writer.hh:16
RangeTypes
Type used to determine whether to limit output components to e.g. 3 (vector), or 9 (tensor)
Definition types.hh:34
DataTypes
Definition types.hh:51
Vtk::RangeTypes rangeTypeOf(Dune::VTK::FieldInfo::Type t)
Definition types.cc:55
constexpr T & resolveRef(T &gf) noexcept
virtual void operator()()=0
virtual ~Interface()
Implementation & impl()
GridView(const Implementation &imp)
ALBERTA EL Element
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
GridFunction(GridFunction &&)=default
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
GridFunction(F &&f)
Definition gridfunction.hh:107
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
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
GridFunction & operator=(GridFunction &&)=default
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
Definition types.hh:248
int size() const
The number of components in the data field.
Definition types.hh:273
std::string const & name() const
The name of the data field.
Definition types.hh:267
Vtk::RangeTypes rangeType() const
Return the category of the stored range.
Definition types.hh:279
Vtk::DataTypes dataType() const
Return the data tpe of the data field.
Definition types.hh:285
T begin(T... args)
T end(T... args)
T forward(T... args)
T iota(T... args)
T size(T... args)