DUNE PDELab (2.7)

vtk.hh
1#ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
2#define DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
3
4#include <vector>
5#include <sstream>
6
9
11
12#include <dune/localfunctions/common/interfaceswitch.hh>
13
14#include <dune/typetree/visitor.hh>
15#include <dune/typetree/traversal.hh>
16
17#include <dune/pdelab/common/function.hh>
18#include <dune/pdelab/common/vtkexport.hh>
19#include <dune/pdelab/common/vtkexport.hh>
20#include <dune/pdelab/gridfunctionspace/localfunctionspace.hh>
21#include <dune/pdelab/gridfunctionspace/lfsindexcache.hh>
23
24namespace Dune {
25
26 template<typename GV>
27 class VTKWriter;
28
29 template<typename GV>
30 class SubsamplingVTKWriter;
31
32 template<typename GV>
33 class VTKSequenceWriter;
34
35 namespace PDELab {
36
37 namespace vtk {
38
39 namespace {
40
41 template<typename VTKWriter>
42 struct vtk_writer_traits;
43
44 template<typename GV>
45 struct vtk_writer_traits<Dune::VTKWriter<GV> >
46 {
47 typedef GV GridView;
48 };
49
50 template<typename GV>
51 struct vtk_writer_traits<Dune::SubsamplingVTKWriter<GV> >
52 {
53 typedef GV GridView;
54 };
55
56 template<typename GV>
57 struct vtk_writer_traits<Dune::VTKSequenceWriter<GV> >
58 {
59 typedef GV GridView;
60 };
61
62 }
63
64 template<typename LFS, typename Data>
65 class DGFTreeLeafFunction;
66
67 template<typename LFS, typename Data>
68 class DGFTreeVectorFunction;
69
70 template<typename VTKWriter, typename Data>
71 struct OutputCollector;
72
73
75 template<typename GFS, typename X, typename Pred>
77 {
78
79 template<typename LFS, typename Data>
80 friend class DGFTreeLeafFunction;
81
82 template<typename LFS, typename Data>
83 friend class DGFTreeVectorFunction;
84
85 template<typename, typename>
86 friend struct OutputCollector;
87
89 typedef LFSIndexCache<LFS> LFSCache;
90 typedef typename X::template ConstLocalView<LFSCache> XView;
92 using EntitySet = typename GFS::Traits::EntitySet;
93 using Cell = typename EntitySet::Traits::Element;
94 using IndexSet = typename EntitySet::Traits::IndexSet;
95 typedef typename IndexSet::IndexType size_type;
96
97 static const auto dim = EntitySet::dimension;
98
99 public:
100
101 typedef GFS GridFunctionSpace;
102 typedef X Vector;
103 typedef Pred Predicate;
104
105 DGFTreeCommonData(std::shared_ptr<const GFS> gfs, std::shared_ptr<const X> x)
106 : _lfs(gfs)
107 , _lfs_cache(_lfs)
108 , _x_view(x)
109 , _x_local(_lfs.maxSize())
110 , _index_set(gfs->entitySet().indexSet())
111 , _current_cell_index(std::numeric_limits<size_type>::max())
112 , x(x)
113 {}
114
115 public:
116
117 void bind(const Cell& cell)
118 {
119 auto cell_index = _index_set.uniqueIndex(cell);
120 if (_current_cell_index == cell_index)
121 return;
122
123 _lfs.bind(cell);
124 _lfs_cache.update();
125 _x_view.bind(_lfs_cache);
126 _x_view.read(_x_local);
127 _x_view.unbind();
128 _current_cell_index = cell_index;
129 }
130
131 LFS _lfs;
132 LFSCache _lfs_cache;
133 XView _x_view;
134 XLocalVector _x_local;
135 const IndexSet& _index_set;
136 size_type _current_cell_index;
137
138 // This copy of x is stored here in order to have this object take ownership
139 // of the passed data. This is necessary to prevent a premature release.
140 std::shared_ptr<const X> x;
141 };
142
143
144
145 template<typename LFS, typename Data>
146 class DGFTreeLeafFunction
147 : public GridFunctionBase<GridFunctionTraits<
148 typename LFS::Traits::GridView,
149 typename BasisInterfaceSwitch<
150 typename FiniteElementInterfaceSwitch<
151 typename LFS::Traits::FiniteElement
152 >::Basis
153 >::RangeField,
154 BasisInterfaceSwitch<
155 typename FiniteElementInterfaceSwitch<
156 typename LFS::Traits::FiniteElement
157 >::Basis
158 >::dimRange,
159 typename BasisInterfaceSwitch<
160 typename FiniteElementInterfaceSwitch<
161 typename LFS::Traits::FiniteElement
162 >::Basis
163 >::Range
164 >,
165 DGFTreeLeafFunction<LFS,Data>
166 >
167 {
168
169 typedef BasisInterfaceSwitch<
171 typename LFS::Traits::FiniteElement
172 >::Basis
173 > BasisSwitch;
174
175 typedef GridFunctionBase<
177 typename LFS::Traits::GridView,
180 typename BasisSwitch::Range
181 >,
182 DGFTreeLeafFunction<LFS,Data>
183 > BaseT;
184
185 public:
186 typedef typename BaseT::Traits Traits;
187
188 DGFTreeLeafFunction (const LFS& lfs, const std::shared_ptr<Data>& data)
189 : BaseT(lfs.gridFunctionSpace().dataSetType())
190 , _lfs(lfs)
191 , _data(data)
192 , _basis(lfs.maxSize())
193 {}
194
195 // Evaluate
196 void evaluate (const typename Traits::ElementType& e,
197 const typename Traits::DomainType& x,
198 typename Traits::RangeType& y) const
199 {
200 _data->bind(e);
201
203 typename LFS::Traits::FiniteElement
204 > FESwitch;
205
206 y = 0;
207
208 FESwitch::basis(_lfs.finiteElement()).evaluateFunction(x,_basis);
209 for (std::size_t i = 0; i < _lfs.size(); ++i)
210 y.axpy(_data->_x_local(_lfs,i),_basis[i]);
211 }
212
214 const typename Traits::GridViewType& gridView() const
215 {
216 return _lfs.gridFunctionSpace().gridView();
217 }
218
219 const LFS& localFunctionSpace() const
220 {
221 return _lfs;
222 }
223
224 private:
225
226 const LFS& _lfs;
227 const std::shared_ptr<Data> _data;
228 mutable std::vector<typename Traits::RangeType> _basis;
229
230 };
231
232
233
234 template<typename LFS, typename Data>
235 class DGFTreeVectorFunction
236 : public GridFunctionBase<GridFunctionTraits<
237 typename LFS::Traits::GridView,
238 typename BasisInterfaceSwitch<
239 typename FiniteElementInterfaceSwitch<
240 typename LFS::ChildType::Traits::FiniteElement
241 >::Basis
242 >::RangeField,
243 TypeTree::StaticDegree<LFS>::value,
244 Dune::FieldVector<
245 typename BasisInterfaceSwitch<
246 typename FiniteElementInterfaceSwitch<
247 typename LFS::ChildType::Traits::FiniteElement
248 >::Basis
249 >::RangeField,
250 TypeTree::StaticDegree<LFS>::value
251 >
252 >,
253 DGFTreeVectorFunction<LFS,Data>
254 >
255 {
256
257 typedef BasisInterfaceSwitch<
258 typename FiniteElementInterfaceSwitch<
259 typename LFS::ChildType::Traits::FiniteElement
260 >::Basis
261 > BasisSwitch;
262
263 static_assert(BasisSwitch::dimRange == 1,
264 "Automatic conversion to vector-valued function only supported for scalar components");
265
266 typedef GridFunctionBase<
267 GridFunctionTraits<
268 typename LFS::Traits::GridView,
270 TypeTree::StaticDegree<LFS>::value,
273 TypeTree::StaticDegree<LFS>::value
274 >
275 >,
276 DGFTreeVectorFunction<LFS,Data>
277 > BaseT;
278
279 public:
280
281 typedef typename BaseT::Traits Traits;
282 typedef typename LFS::ChildType ChildLFS;
283 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType RF;
284 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType RT;
285
286 DGFTreeVectorFunction (const LFS& lfs, const std::shared_ptr<Data>& data)
287 : BaseT(lfs.gridFunctionSpace().dataSetType())
288 , _lfs(lfs)
289 , _data(data)
290 , _basis(lfs.maxSize())
291 {}
292
293 void evaluate (const typename Traits::ElementType& e,
294 const typename Traits::DomainType& x,
295 typename Traits::RangeType& y) const
296 {
297 _data->bind(e);
298
299 typedef FiniteElementInterfaceSwitch<
300 typename ChildLFS::Traits::FiniteElement
301 > FESwitch;
302
303 y = 0;
304
305 for (std::size_t k = 0; k < TypeTree::degree(_lfs); ++k)
306 {
307 const ChildLFS& child_lfs = _lfs.child(k);
308 FESwitch::basis(child_lfs.finiteElement()).evaluateFunction(x,_basis);
309
310 for (std::size_t i = 0; i < child_lfs.size(); ++i)
311 y[k] += _data->_x_local(child_lfs,i) * _basis[i];
312 }
313 }
314
316 const typename Traits::GridViewType& gridView() const
317 {
318 return _lfs.gridFunctionSpace().gridView();
319 }
320
321 const LFS& localFunctionSpace() const
322 {
323 return _lfs;
324 }
325
326 private:
327
328 const LFS& _lfs;
329 const std::shared_ptr<Data> _data;
330 mutable std::vector<typename BasisSwitch::Range> _basis;
331
332 };
333
334
335 class DefaultFunctionNameGenerator
336 {
337
338 public:
339
340 template<typename TreePath>
341 std::string operator()(std::string component_name, TreePath tp) const
342 {
343 if (component_name.empty())
344 {
345
346 if (_prefix.empty() && _suffix.empty())
347 {
348 DUNE_THROW(IOError,
349 "You need to either name all GridFunctionSpaces "
350 "written to the VTK file or provide a prefix / suffix.");
351 }
352
353 std::stringstream name_stream;
354
355 if (!_prefix.empty())
356 name_stream << _prefix << _separator;
357
358 // Build a simple name based on the component's TreePath (e.g. 0_2_3)
359 for (std::size_t i = 0; i < tp.size(); ++i)
360 name_stream << (i > 0 ? _separator : "") << tp.element(i);
361
362 if (!_suffix.empty())
363 name_stream << _separator << _suffix;
364 return name_stream.str();
365 }
366 else
367 {
368 // construct name from prefix, component name and suffix
369 return _prefix + component_name + _suffix;
370 }
371 }
372
373 DefaultFunctionNameGenerator& prefix(std::string prefix)
374 {
375 _prefix = prefix;
376 return *this;
377 }
378
379 DefaultFunctionNameGenerator& suffix(std::string suffix)
380 {
381 _suffix = suffix;
382 return *this;
383 }
384
385 DefaultFunctionNameGenerator& separator(std::string separator)
386 {
387 _separator = separator;
388 return *this;
389 }
390
391 DefaultFunctionNameGenerator(std::string prefix = "",
392 std::string suffix = "",
393 std::string separator = "_")
394 : _prefix(prefix)
395 , _suffix(suffix)
396 , _separator(separator)
397 {}
398
399 private:
400
401 std::string _prefix;
402 std::string _suffix;
403 std::string _separator;
404
405 };
406
407 inline DefaultFunctionNameGenerator defaultNameScheme()
408 {
409 return DefaultFunctionNameGenerator();
410 }
411
412
413 template<typename VTKWriter, typename Data, typename NameGenerator>
414 struct add_solution_to_vtk_writer_visitor
415 : public TypeTree::DefaultVisitor
416 , public TypeTree::DynamicTraversal
417 {
418
419
420 template<typename LFS, typename Child, typename TreePath>
421 struct VisitChild
422 {
423
424 static const bool value =
425 // Do not descend into children of VectorGridFunctionSpace
426 !std::is_convertible<
427 TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>,
428 VectorGridFunctionSpaceTag
429 >::value;
430
431 };
432
435 template<typename DGF, typename TreePath>
436 void add_to_vtk_writer(const std::shared_ptr<DGF>& dgf, TreePath tp)
437 {
438 std::string name = name_generator(dgf->localFunctionSpace().gridFunctionSpace().name(),tp);
439 switch (dgf->dataSetType())
440 {
441 case DGF::Output::vertexData:
442 vtk_writer.addVertexData(std::make_shared<VTKGridFunctionAdapter<DGF> >(dgf,name.c_str()));
443 break;
444 case DGF::Output::cellData:
445 vtk_writer.addCellData(std::make_shared<VTKGridFunctionAdapter<DGF> >(dgf,name.c_str()));
446 break;
447 default:
448 DUNE_THROW(NotImplemented,"Unsupported data set type");
449 }
450 }
451
453
456 template<typename LFS, typename TreePath>
457 void add_vector_solution(const LFS& lfs, TreePath tp, VectorGridFunctionSpaceTag tag)
458 {
459 add_to_vtk_writer(std::make_shared<DGFTreeVectorFunction<LFS,Data> >(lfs,data),tp);
460 }
461
463
466 template<typename LFS, typename TreePath>
467 void add_vector_solution(const LFS& lfs, TreePath tp, GridFunctionSpaceTag tag)
468 {
469 // do nothing here - not a vector space
470 }
471
472 // **********************************************************************
473 // Visitor functions for adding DiscreteGridFunctions to VTKWriter
474 //
475 // The visitor functions contain a switch that will make them ignore
476 // function spaces with a different underlying GridView type than
477 // the VTKWriter.
478 // This cannot happen in vanilla PDELab, but is required for MultiDomain
479 // support
480 // **********************************************************************
481
482 // don't do anything if GridView types differ
483 template<typename LFS, typename TreePath>
484 typename std::enable_if<
485 !std::is_same<
486 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
487 typename vtk_writer_traits<VTKWriter>::GridView
488 >::value
489 >::type
490 post(const LFS& lfs, TreePath tp)
491 {
492 }
493
494 // don't do anything if GridView types differ
495 template<typename LFS, typename TreePath>
496 typename std::enable_if<
497 !std::is_same<
498 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
499 typename vtk_writer_traits<VTKWriter>::GridView
500 >::value
501 >::type
502 leaf(const LFS& lfs, TreePath tp)
503 {
504 }
505
507 template<typename LFS, typename TreePath>
508 typename std::enable_if<
509 std::is_same<
510 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
511 typename vtk_writer_traits<VTKWriter>::GridView
512 >::value
513 >::type
514 post(const LFS& lfs, TreePath tp)
515 {
516 if (predicate(lfs, tp))
517 add_vector_solution(lfs,tp,TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>());
518 }
519
521 template<typename LFS, typename TreePath>
522 typename std::enable_if<
523 std::is_same<
524 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
525 typename vtk_writer_traits<VTKWriter>::GridView
526 >::value
527 >::type
528 leaf(const LFS& lfs, TreePath tp)
529 {
530 if (predicate(lfs, tp))
531 add_to_vtk_writer(std::make_shared<DGFTreeLeafFunction<LFS,Data> >(lfs,data),tp);
532 }
533
534
535 add_solution_to_vtk_writer_visitor(VTKWriter& vtk_writer_, std::shared_ptr<Data> data_, const NameGenerator& name_generator_, const typename Data::Predicate& predicate_)
536 : vtk_writer(vtk_writer_)
537 , data(data_)
538 , name_generator(name_generator_)
539 , predicate(predicate_)
540 {}
541
542 VTKWriter& vtk_writer;
543 std::shared_ptr<Data> data;
544 const NameGenerator& name_generator;
545 typename Data::Predicate predicate;
546
547 };
548
549 struct DefaultPredicate
550 {
551 template<typename LFS, typename TP>
552 bool operator()(const LFS& lfs, TP tp) const
553 {
554 return true;
555 }
556 };
557
558 template<typename VTKWriter, typename Data_>
559 struct OutputCollector
560 {
561
563 typedef Data_ Data;
564
565 typedef typename Data::GridFunctionSpace GFS;
566 typedef typename Data::Vector Vector;
567 typedef typename Data::Predicate Predicate;
568
569 template<typename NameGenerator>
570 OutputCollector& addSolution(const NameGenerator& name_generator)
571 {
572
573 add_solution_to_vtk_writer_visitor<VTKWriter,Data,NameGenerator> visitor(_vtk_writer,_data,name_generator,_predicate);
574 TypeTree::applyToTree(_data->_lfs,visitor);
575 return *this;
576 }
577
578 template<typename Factory, typename TreePath>
579 OutputCollector& addCellFunction(Factory factory, TreePath tp, std::string name)
580 {
581 typedef typename std::remove_reference<decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
582 _vtk_writer.addCellData(std::make_shared<VTKGridFunctionAdapter<DGF> >(factory.create(_data->_lfs.child(tp),_data),name));
583 return *this;
584 }
585
586 template<template<typename...> class Function, typename TreePath, typename... Params>
587 OutputCollector& addCellFunction(TreePath tp, std::string name, Params&&... params)
588 {
589 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
590 typedef Function<LFS,Data,Params...> DGF;
591 _vtk_writer.addCellData(
592 std::make_shared<VTKGridFunctionAdapter<DGF> >(
593 std::make_shared<DGF>(
594 TypeTree::child(_data->_lfs,tp)
595 ),
596 _data,
597 std::forward<Params>(params)...
598 ),
599 name
600 );
601 return *this;
602 }
603
604 template<typename Factory, typename TreePath>
605 OutputCollector& addVertexFunction(Factory factory, TreePath tp, std::string name)
606 {
607 typedef typename std::remove_reference<decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
608 _vtk_writer.addVertexData(std::make_shared<VTKGridFunctionAdapter<DGF> >(factory.create(_data->_lfs.child(tp),_data),name));
609 return *this;
610 }
611
612 template<template<typename...> class Function, typename TreePath, typename... Params>
613 OutputCollector& addVertexFunction(TreePath tp, std::string name, Params&&... params)
614 {
615 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
616 typedef Function<LFS,Data,Params...> DGF;
617 _vtk_writer.addVertexData(
618 std::make_shared<VTKGridFunctionAdapter<DGF> >(
619 std::make_shared<DGF>(
620 TypeTree::child(_data->_lfs,tp)
621 ),
622 _data,
623 std::forward<Params>(params)...
624 ),
625 name
626 );
627 return *this;
628 }
629
630 OutputCollector(VTKWriter& vtk_writer, const std::shared_ptr<Data>& data, const Predicate& predicate = Predicate())
631 : _vtk_writer(vtk_writer)
632 , _data(data)
633 , _predicate(predicate)
634 {}
635
636 VTKWriter& _vtk_writer;
637 std::shared_ptr<Data> _data;
638 Predicate _predicate;
639
640 };
641
642 } // namespace vtk
643
644 template<typename VTKWriter,
645 typename GFS,
646 typename X,
647 typename NameGenerator = vtk::DefaultFunctionNameGenerator,
648 typename Predicate = vtk::DefaultPredicate>
649 vtk::OutputCollector<
650 VTKWriter,
651 vtk::DGFTreeCommonData<GFS,X,Predicate>
652 >
653 addSolutionToVTKWriter(VTKWriter& vtk_writer,
654 const GFS& gfs,
655 const X& x,
656 const NameGenerator& name_generator = vtk::defaultNameScheme(),
657 const Predicate& predicate = Predicate())
658 {
659 typedef vtk::DGFTreeCommonData<GFS,X,Predicate> Data;
660 auto data = std::make_shared<Data>(Dune::stackobject_to_shared_ptr(gfs), Dune::stackobject_to_shared_ptr(x));
661 vtk::OutputCollector<VTKWriter,Data> collector(vtk_writer, data, predicate);
662 collector.addSolution(name_generator);
663 return collector;
664 }
665
666
667 template<typename VTKWriter,
668 typename GFS,
669 typename X,
670 typename NameGenerator = vtk::DefaultFunctionNameGenerator,
671 typename Predicate = vtk::DefaultPredicate>
672 vtk::OutputCollector<
673 VTKWriter,
674 vtk::DGFTreeCommonData<GFS,X,Predicate>
675 >
676 addSolutionToVTKWriter(VTKWriter& vtk_writer,
677 std::shared_ptr<GFS> gfs,
678 std::shared_ptr<X> x,
679 const NameGenerator& name_generator = vtk::defaultNameScheme(),
680 const Predicate& predicate = Predicate())
681 {
682 typedef vtk::DGFTreeCommonData<GFS,X,Predicate> Data;
683 vtk::OutputCollector<VTKWriter,Data> collector(vtk_writer, std::make_shared<Data>(gfs,x),predicate);
684 collector.addSolution(name_generator);
685 return collector;
686 }
687
688 } // namespace PDELab
689} // namespace Dune
690
691#endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
vector space out of a tensor product of fields.
Definition: fvector.hh:96
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:90
leaf of a function tree
Definition: function.hh:302
T Traits
Export type traits.
Definition: function.hh:193
Output::DataSetType dataSetType() const
Return the data set type of this function.
Definition: function.hh:154
Helper class for common data of a DGFTree.
Definition: vtk.hh:77
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
std::size_t degree(const Node &node)
Returns the degree of node as run time information.
Definition: nodeinterface.hh:71
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:79
void applyToTree(Tree &&tree, Visitor &&visitor)
Apply visitor to TypeTree.
Definition: traversal.hh:213
ImplementationDefined child(Node &&node, Indices... indices)
Extracts the child of a node given by a sequence of compile-time and run-time indices.
Definition: childextraction.hh:179
Dune namespace.
Definition: alignedallocator.hh:14
shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:75
This file implements the class shared_ptr (a reference counting pointer), for those systems that don'...
Switch for uniform treatment of local and global basis classes.
Definition: interfaceswitch.hh:132
static const std::size_t dimRange
export dimension of the values
Definition: interfaceswitch.hh:143
Basis::Traits::RangeField RangeField
export field type of the values
Definition: interfaceswitch.hh:141
Basis::Traits::Range Range
export vector type of the values
Definition: interfaceswitch.hh:145
Switch for uniform treatment of finite element with either the local or the global interface.
Definition: interfaceswitch.hh:27
traits class holding the function signature, same as in local function
Definition: function.hh:183
void post(T &&t, TreePath treePath) const
Method for postfix tree traversal.
Definition: visitor.hh:80
void leaf(T &&t, TreePath treePath) const
Method for leaf traversal.
Definition: visitor.hh:90
Helper classes to provide indices for geometrytypes for use in a vector.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)