3.1-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
vtkfunction.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef VTK_FUNCTION_HH
25#define VTK_FUNCTION_HH
26
27#include <string>
28#include <memory>
29
30#include <dune/grid/common/mcmgmapper.hh>
31#include <dune/grid/io/file/vtk/common.hh>
32#include <dune/grid/io/file/vtk/function.hh>
33
35
36namespace Dumux {
37namespace Vtk {
38
47template <typename GridView, typename Mapper, typename F>
48struct VectorP0VTKFunction : Dune::VTKFunction<GridView>
49{
50 enum { dim = GridView::dimension };
51 using ctype = typename GridView::ctype;
52 using Element = typename GridView::template Codim<0>::Entity;
53
54public:
55
57 virtual int ncomps() const { return nComps_; }
58
60 virtual std::string name() const { return name_; }
61
63 virtual double evaluate(int mycomp, const Element& e, const Dune::FieldVector<ctype, dim>&) const
64 { return accessChooser_(mycomp, mapper_.index(e), IsIndexable<decltype(field_[0])>()); }
65
67 VectorP0VTKFunction(const GridView& gridView, const Mapper& mapper, const F& field, const std::string& name, int nComps)
68 : field_(field), name_(name), nComps_(nComps), mapper_(mapper)
69 {
70 if (field.size()!=(unsigned int)(mapper.size()))
71 DUNE_THROW(Dune::IOError, "VectorP0VTKFunction: size mismatch between field "
72 << name << " (" << field.size() << ") and mapper (" << mapper.size() << ")");
73 }
74
75private:
76
78 double accessChooser_(int mycomp, int i, std::true_type) const
79 { return vectorFieldAccess_(mycomp, i, IsIndexable<decltype(field_[0][0])>()); }
80
82 double accessChooser_(int mycomp, int i, std::false_type) const { return field_[i]; }
83
85 double vectorFieldAccess_(int mycomp, int i, std::false_type) const { return field_[i][mycomp]; }
86
88 double vectorFieldAccess_(int mycomp, int i, std::true_type) const { DUNE_THROW(Dune::InvalidStateException, "Invalid field type"); }
89
90 const F& field_;
91 const std::string name_;
92 int nComps_;
93 const Mapper& mapper_;
94};
95
104template <typename GridView, typename Mapper, typename F>
105struct VectorP1VTKFunction : Dune::VTKFunction<GridView>
106{
107 enum { dim = GridView::dimension };
108 using ctype = typename GridView::ctype;
109 using Element = typename GridView::template Codim<0>::Entity;
110
111public:
112
114 virtual int ncomps() const { return nComps_; }
115
117 virtual std::string name() const { return name_; }
118
120 virtual double evaluate(int mycomp, const Element& e, const Dune::FieldVector<ctype, dim>& xi) const
121 {
122 const unsigned int dim = Element::mydimension;
123 const unsigned int nVertices = e.subEntities(dim);
124
125 std::vector<Dune::FieldVector<ctype, 1>> cornerValues(nVertices);
126 for (unsigned i = 0; i < nVertices; ++i)
127 cornerValues[i] = accessChooser_(mycomp, mapper_.subIndex(e, i, dim), IsIndexable<decltype(field_[0])>());
128
129 // (Ab)use the MultiLinearGeometry class to do multi-linear interpolation between scalars
130 const Dune::MultiLinearGeometry<ctype, dim, 1> interpolation(e.type(), std::move(cornerValues));
131 return interpolation.global(xi);
132 }
133
135 VectorP1VTKFunction(const GridView& gridView, const Mapper& mapper, const F& field, const std::string& name, int nComps)
136 : field_(field), name_(name), nComps_(nComps), mapper_(mapper)
137 {
138 if (field.size()!=(unsigned int)( mapper.size() ))
139 DUNE_THROW(Dune::IOError, "VectorP1VTKFunction: size mismatch between field "
140 << name << " (" << field.size() << ") and mapper (" << mapper.size() << ")");
141 }
142private:
143
145 double accessChooser_(int mycomp, int i, std::true_type) const
146 { return vectorFieldAccess_(mycomp, i, IsIndexable<decltype(field_[0][0])>()); }
147
149 double accessChooser_(int mycomp, int i, std::false_type) const { return field_[i]; }
150
152 double vectorFieldAccess_(int mycomp, int i, std::false_type) const { return field_[i][mycomp]; }
153
155 double vectorFieldAccess_(int mycomp, int i, std::true_type) const { DUNE_THROW(Dune::InvalidStateException, "Invalid field type"); }
156
157 const F& field_;
158 const std::string name_;
159 int nComps_;
160 const Mapper& mapper_;
161};
162
175template <typename GridView, typename Mapper, typename F>
176struct VectorP1NonConformingVTKFunction : Dune::VTKFunction<GridView>
177{
178 enum { dim = GridView::dimension };
179 using ctype = typename GridView::ctype;
180 using Element = typename GridView::template Codim<0>::Entity;
181
182public:
183
185 virtual int ncomps() const { return nComps_; }
186
188 virtual std::string name() const { return name_; }
189
191 virtual double evaluate(int mycomp, const Element& e, const Dune::FieldVector<ctype, dim>& xi) const
192 {
193 const unsigned int dim = Element::mydimension;
194 const unsigned int nVertices = e.subEntities(dim);
195
196 std::vector<Dune::FieldVector<ctype, 1>> cornerValues(nVertices);
197 for (unsigned i = 0; i < nVertices; ++i)
198 cornerValues[i] = accessChooser_(mycomp, mapper_.index(e), i, IsIndexable<decltype(field_[0])>());
199
200 // (Ab)use the MultiLinearGeometry class to do multi-linear interpolation between scalars
201 const Dune::MultiLinearGeometry<ctype, dim, 1> interpolation(e.type(), std::move(cornerValues));
202 return interpolation.global(xi);
203 }
204
206 VectorP1NonConformingVTKFunction(const GridView& gridView, const Mapper& mapper, const F& field, const std::string& name, int nComps)
207 : field_(field), name_(name), nComps_(nComps), mapper_(mapper)
208 {
209 if (field.size()!=(unsigned int)(mapper.size()))
210 DUNE_THROW(Dune::IOError, "VectorP1NonConformingVTKFunction: size mismatch between field "
211 << name << " (" << field.size() << ") and mapper (" << mapper.size() << ")");
212 }
213private:
214
216 double accessChooser_(int mycomp, int eIdx, int cornerIdx, std::true_type) const
217 { return fieldAccess_(mycomp, eIdx, cornerIdx, IsIndexable<decltype(field_[0][0])>()); }
218
220 double accessChooser_(int mycomp, int eIdx, int cornerIdx, std::false_type) const
221 { DUNE_THROW(Dune::InvalidStateException, "Invalid field type"); }
222
224 double fieldAccess_(int mycomp, int eIdx, int cornerIdx, std::false_type) const
225 { return field_[eIdx][cornerIdx]; }
226
228 double fieldAccess_(int mycomp, int eIdx, int cornerIdx, std::true_type) const
229 { return field_[eIdx][cornerIdx][mycomp]; }
230
231 const F& field_;
232 const std::string name_;
233 int nComps_;
234 const Mapper& mapper_;
235};
236
243template<class GridView>
244class Field
245{
246 enum { dim = GridView::dimension };
247 using ctype = typename GridView::ctype;
248 using Element = typename GridView::template Codim<0>::Entity;
249
250public:
251 // template constructor selects the right VTKFunction implementation
252 template <typename F, class Mapper>
253 Field(const GridView& gridView, const Mapper& mapper, F const& f,
254 const std::string& name, int numComp = 1, int codim = 0,
255 Dune::VTK::DataMode dm = Dune::VTK::conforming)
256 : codim_(codim)
257 {
258 if (codim == GridView::dimension)
259 {
260 if (dm == Dune::VTK::conforming)
261 field_ = std::make_shared< VectorP1VTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp);
262 else
263 field_ = std::make_shared< VectorP1NonConformingVTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp);
264 }
265 else if (codim == 0)
266 field_ = std::make_shared< VectorP0VTKFunction<GridView, Mapper, F> >(gridView, mapper, f, name, numComp);
267 else
268 DUNE_THROW(Dune::NotImplemented, "Only element or vertex quantities allowed.");
269 }
270
271 virtual ~Field() {}
272
274 virtual std::string name () const { return field_->name(); }
275
277 virtual int ncomps() const { return field_->ncomps(); }
278
280 int codim() const { return codim_; }
281
283 virtual double evaluate(int mycomp,
284 const Element &element,
285 const Dune::FieldVector< ctype, dim > &xi) const
286 { return field_->evaluate(mycomp, element, xi); }
287
289 std::shared_ptr<const Dune::VTKFunction<GridView>> get() const
290 { return field_; }
291
292private:
293 int codim_;
294 // can point to anything fulfilling the VTKFunction interface
295 std::shared_ptr<Dune::VTKFunction<GridView>> field_;
296};
297
298} // end namespace Vtk
299
300} // end namespace Dumux
301
302#endif
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
typename Dune::is_indexable< T, I > IsIndexable
We define our own is_indexable type in order to avoid several version checks throughout dumux....
Definition: typetraits.hh:48
a VTK function that supports both scalar and vector values for each element
Definition: vtkfunction.hh:49
typename GridView::ctype ctype
Definition: vtkfunction.hh:51
virtual double evaluate(int mycomp, const Element &e, const Dune::FieldVector< ctype, dim > &) const
evaluate
Definition: vtkfunction.hh:63
virtual std::string name() const
get name
Definition: vtkfunction.hh:60
typename GridView::template Codim< 0 >::Entity Element
Definition: vtkfunction.hh:52
virtual int ncomps() const
return number of components
Definition: vtkfunction.hh:57
VectorP0VTKFunction(const GridView &gridView, const Mapper &mapper, const F &field, const std::string &name, int nComps)
Constructor.
Definition: vtkfunction.hh:67
@ dim
Definition: vtkfunction.hh:50
a VTK function that supports both scalar and vector values for each vertex
Definition: vtkfunction.hh:106
@ dim
Definition: vtkfunction.hh:107
VectorP1VTKFunction(const GridView &gridView, const Mapper &mapper, const F &field, const std::string &name, int nComps)
Constructor.
Definition: vtkfunction.hh:135
virtual int ncomps() const
return number of components
Definition: vtkfunction.hh:114
virtual double evaluate(int mycomp, const Element &e, const Dune::FieldVector< ctype, dim > &xi) const
evaluate
Definition: vtkfunction.hh:120
virtual std::string name() const
get name
Definition: vtkfunction.hh:117
typename GridView::template Codim< 0 >::Entity Element
Definition: vtkfunction.hh:109
typename GridView::ctype ctype
Definition: vtkfunction.hh:108
A VTK function that supports both scalar and vector values for each vertex. This expects the data to ...
Definition: vtkfunction.hh:177
virtual double evaluate(int mycomp, const Element &e, const Dune::FieldVector< ctype, dim > &xi) const
evaluate
Definition: vtkfunction.hh:191
virtual int ncomps() const
return number of components
Definition: vtkfunction.hh:185
typename GridView::template Codim< 0 >::Entity Element
Definition: vtkfunction.hh:180
VectorP1NonConformingVTKFunction(const GridView &gridView, const Mapper &mapper, const F &field, const std::string &name, int nComps)
Constructor.
Definition: vtkfunction.hh:206
@ dim
Definition: vtkfunction.hh:178
typename GridView::ctype ctype
Definition: vtkfunction.hh:179
virtual std::string name() const
get name
Definition: vtkfunction.hh:188
struct that can hold any field that fulfills the VTKFunction interface
Definition: vtkfunction.hh:245
virtual ~Field()
Definition: vtkfunction.hh:271
std::shared_ptr< const Dune::VTKFunction< GridView > > get() const
returns the underlying vtk function
Definition: vtkfunction.hh:289
virtual std::string name() const
return the name of this field
Definition: vtkfunction.hh:274
virtual int ncomps() const
return the number of components of this field
Definition: vtkfunction.hh:277
int codim() const
codimension of the entities on which the field values live
Definition: vtkfunction.hh:280
virtual double evaluate(int mycomp, const Element &element, const Dune::FieldVector< ctype, dim > &xi) const
element-local evaluation of the field
Definition: vtkfunction.hh:283
Field(const GridView &gridView, const Mapper &mapper, F const &f, const std::string &name, int numComp=1, int codim=0, Dune::VTK::DataMode dm=Dune::VTK::conforming)
Definition: vtkfunction.hh:253