1#ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
2#define DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
12#include <dune/localfunctions/common/interfaceswitch.hh>
14#include <dune/typetree/visitor.hh>
15#include <dune/typetree/traversal.hh>
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>
30 class SubsamplingVTKWriter;
33 class VTKSequenceWriter;
41 template<
typename VTKWriter>
42 struct vtk_writer_traits;
45 struct vtk_writer_traits<
Dune::VTKWriter<GV> >
51 struct vtk_writer_traits<
Dune::SubsamplingVTKWriter<GV> >
57 struct vtk_writer_traits<
Dune::VTKSequenceWriter<GV> >
64 template<
typename LFS,
typename Data>
65 class DGFTreeLeafFunction;
67 template<
typename LFS,
typename Data>
68 class DGFTreeVectorFunction;
70 template<
typename VTKWriter,
typename Data>
71 struct OutputCollector;
75 template<
typename GFS,
typename X,
typename Pred>
79 template<
typename LFS,
typename Data>
80 friend class DGFTreeLeafFunction;
82 template<
typename LFS,
typename Data>
83 friend class DGFTreeVectorFunction;
85 template<
typename,
typename>
86 friend struct OutputCollector;
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;
97 static const auto dim = EntitySet::dimension;
101 typedef GFS GridFunctionSpace;
103 typedef Pred Predicate;
109 , _x_local(_lfs.maxSize())
110 , _index_set(gfs->entitySet().indexSet())
117 void bind(
const Cell& cell)
119 auto cell_index = _index_set.uniqueIndex(cell);
120 if (_current_cell_index == cell_index)
125 _x_view.bind(_lfs_cache);
126 _x_view.read(_x_local);
128 _current_cell_index = cell_index;
135 const IndexSet& _index_set;
136 size_type _current_cell_index;
140 std::shared_ptr<const X> x;
145 template<
typename LFS,
typename Data>
146 class DGFTreeLeafFunction
148 typename LFS::Traits::GridView,
149 typename BasisInterfaceSwitch<
150 typename FiniteElementInterfaceSwitch<
151 typename LFS::Traits::FiniteElement
154 BasisInterfaceSwitch<
155 typename FiniteElementInterfaceSwitch<
156 typename LFS::Traits::FiniteElement
159 typename BasisInterfaceSwitch<
160 typename FiniteElementInterfaceSwitch<
161 typename LFS::Traits::FiniteElement
165 DGFTreeLeafFunction<LFS,Data>
171 typename LFS::Traits::FiniteElement
177 typename LFS::Traits::GridView,
182 DGFTreeLeafFunction<LFS,Data>
188 DGFTreeLeafFunction (
const LFS& lfs,
const std::shared_ptr<Data>& data)
189 : BaseT(lfs.gridFunctionSpace().dataSetType())
192 , _basis(lfs.maxSize())
196 void evaluate (
const typename Traits::ElementType& e,
197 const typename Traits::DomainType& x,
198 typename Traits::RangeType& y)
const
203 typename LFS::Traits::FiniteElement
209 FESwitch::basis(_lfs.finiteElement()).evaluateFunction(x,_basis);
210 for (std::size_t i = 0; i < _lfs.size(); ++i)
211 y.axpy(_data->_x_local(_lfs,i),_basis[i]);
216 const typename Traits::GridViewType& gridView()
const
218 return _lfs.gridFunctionSpace().gridView();
221 const LFS& localFunctionSpace()
const
229 const std::shared_ptr<Data> _data;
230 mutable std::vector<typename Traits::RangeType> _basis;
236 template<
typename LFS,
typename Data>
237 class DGFTreeVectorFunction
238 :
public GridFunctionBase<GridFunctionTraits<
239 typename LFS::Traits::GridView,
240 typename BasisInterfaceSwitch<
241 typename FiniteElementInterfaceSwitch<
242 typename LFS::ChildType::Traits::FiniteElement
245 TypeTree::StaticDegree<LFS>::value,
247 typename BasisInterfaceSwitch<
248 typename FiniteElementInterfaceSwitch<
249 typename LFS::ChildType::Traits::FiniteElement
252 TypeTree::StaticDegree<LFS>::value
255 DGFTreeVectorFunction<LFS,Data>
259 typedef BasisInterfaceSwitch<
260 typename FiniteElementInterfaceSwitch<
261 typename LFS::ChildType::Traits::FiniteElement
266 "Automatic conversion to vector-valued function only supported for scalar components");
268 typedef GridFunctionBase<
270 typename LFS::Traits::GridView,
272 TypeTree::StaticDegree<LFS>::value,
275 TypeTree::StaticDegree<LFS>::value
278 DGFTreeVectorFunction<LFS,Data>
284 typedef typename LFS::ChildType ChildLFS;
285 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType RF;
286 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType RT;
288 DGFTreeVectorFunction (
const LFS& lfs,
const std::shared_ptr<Data>& data)
292 , _basis(lfs.maxSize())
295 void evaluate (
const typename Traits::ElementType& e,
296 const typename Traits::DomainType& x,
297 typename Traits::RangeType& y)
const
301 typedef FiniteElementInterfaceSwitch<
302 typename ChildLFS::Traits::FiniteElement
309 const ChildLFS& child_lfs = _lfs.child(k);
310 FESwitch::basis(child_lfs.finiteElement()).evaluateFunction(x,_basis);
312 for (std::size_t i = 0; i < child_lfs.size(); ++i)
313 y[k] += _data->_x_local(child_lfs,i) * _basis[i];
318 const typename Traits::GridViewType& gridView()
const
320 return _lfs.gridFunctionSpace().gridView();
323 const LFS& localFunctionSpace()
const
331 const std::shared_ptr<Data> _data;
332 mutable std::vector<typename BasisSwitch::Range> _basis;
337 class DefaultFunctionNameGenerator
342 template<
typename TreePath>
343 std::string operator()(std::string component_name, TreePath tp)
const
345 if (component_name.empty())
348 if (_prefix.empty() && _suffix.empty())
351 "You need to either name all GridFunctionSpaces "
352 "written to the VTK file or provide a prefix / suffix.");
355 std::stringstream name_stream;
357 if (!_prefix.empty())
358 name_stream << _prefix << _separator;
361 for (std::size_t i = 0; i < tp.size(); ++i)
362 name_stream << (i > 0 ? _separator :
"") << tp.element(i);
364 if (!_suffix.empty())
365 name_stream << _separator << _suffix;
366 return name_stream.str();
371 return _prefix + component_name + _suffix;
375 DefaultFunctionNameGenerator& prefix(std::string prefix)
381 DefaultFunctionNameGenerator& suffix(std::string suffix)
387 DefaultFunctionNameGenerator& separator(std::string separator)
389 _separator = separator;
393 DefaultFunctionNameGenerator(std::string prefix =
"",
394 std::string suffix =
"",
395 std::string separator =
"_")
398 , _separator(separator)
405 std::string _separator;
409 inline DefaultFunctionNameGenerator defaultNameScheme()
411 return DefaultFunctionNameGenerator();
415 template<
typename VTKWriter,
typename Data,
typename NameGenerator>
416 struct add_solution_to_vtk_writer_visitor
417 :
public TypeTree::DefaultVisitor
418 ,
public TypeTree::DynamicTraversal
422 template<
typename LFS,
typename Child,
typename TreePath>
426 static const bool value =
428 !std::is_convertible<
429 TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>,
430 VectorGridFunctionSpaceTag
437 template<
typename DGF,
typename TreePath>
438 void add_to_vtk_writer(
const std::shared_ptr<DGF>& dgf, TreePath tp)
440 std::string name = name_generator(dgf->localFunctionSpace().gridFunctionSpace().name(),tp);
441 switch (dgf->dataSetType())
443 case DGF::Output::vertexData:
444 vtk_writer.addVertexData(std::make_shared<VTKGridFunctionAdapter<DGF> >(dgf,name.c_str()));
446 case DGF::Output::cellData:
447 vtk_writer.addCellData(std::make_shared<VTKGridFunctionAdapter<DGF> >(dgf,name.c_str()));
450 DUNE_THROW(NotImplemented,
"Unsupported data set type");
458 template<
typename LFS,
typename TreePath>
459 void add_vector_solution(
const LFS& lfs, TreePath tp, VectorGridFunctionSpaceTag tag)
461 add_to_vtk_writer(std::make_shared<DGFTreeVectorFunction<LFS,Data> >(lfs,data),tp);
468 template<
typename LFS,
typename TreePath>
469 void add_vector_solution(
const LFS& lfs, TreePath tp, GridFunctionSpaceTag tag)
485 template<
typename LFS,
typename TreePath>
486 typename std::enable_if<
488 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
489 typename vtk_writer_traits<VTKWriter>::GridView
492 post(
const LFS& lfs, TreePath tp)
497 template<
typename LFS,
typename TreePath>
498 typename std::enable_if<
500 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
501 typename vtk_writer_traits<VTKWriter>::GridView
504 leaf(
const LFS& lfs, TreePath tp)
509 template<
typename LFS,
typename TreePath>
510 typename std::enable_if<
512 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
513 typename vtk_writer_traits<VTKWriter>::GridView
516 post(
const LFS& lfs, TreePath tp)
518 if (predicate(lfs, tp))
519 add_vector_solution(lfs,tp,TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>());
523 template<
typename LFS,
typename TreePath>
524 typename std::enable_if<
526 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
527 typename vtk_writer_traits<VTKWriter>::GridView
530 leaf(
const LFS& lfs, TreePath tp)
532 if (predicate(lfs, tp))
533 add_to_vtk_writer(std::make_shared<DGFTreeLeafFunction<LFS,Data> >(lfs,data),tp);
537 add_solution_to_vtk_writer_visitor(VTKWriter& vtk_writer_, std::shared_ptr<Data> data_,
const NameGenerator& name_generator_,
const typename Data::Predicate& predicate_)
538 : vtk_writer(vtk_writer_)
540 , name_generator(name_generator_)
541 , predicate(predicate_)
544 VTKWriter& vtk_writer;
545 std::shared_ptr<Data> data;
546 const NameGenerator& name_generator;
547 typename Data::Predicate predicate;
551 struct DefaultPredicate
553 template<
typename LFS,
typename TP>
554 bool operator()(
const LFS& lfs, TP tp)
const
560 template<
typename VTKWriter,
typename Data_>
561 struct OutputCollector
567 typedef typename Data::GridFunctionSpace GFS;
568 typedef typename Data::Vector Vector;
569 typedef typename Data::Predicate Predicate;
571 template<
typename NameGenerator>
572 OutputCollector& addSolution(
const NameGenerator& name_generator)
575 add_solution_to_vtk_writer_visitor<VTKWriter,Data,NameGenerator> visitor(_vtk_writer,_data,name_generator,_predicate);
580 template<
typename Factory,
typename TreePath>
581 OutputCollector& addCellFunction(Factory factory, TreePath tp, std::string name)
583 typedef typename std::remove_reference<
decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
584 _vtk_writer.addCellData(std::make_shared<VTKGridFunctionAdapter<DGF> >(factory.create(_data->_lfs.child(tp),_data),name));
588 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
589 OutputCollector& addCellFunction(TreePath tp, std::string name, Params&&... params)
591 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
592 typedef Function<LFS,Data,Params...> DGF;
593 _vtk_writer.addCellData(
594 std::make_shared<VTKGridFunctionAdapter<DGF> >(
595 std::make_shared<DGF>(
599 std::forward<Params>(params)...
606 template<
typename Factory,
typename TreePath>
607 OutputCollector& addVertexFunction(Factory factory, TreePath tp, std::string name)
609 typedef typename std::remove_reference<
decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
610 _vtk_writer.addVertexData(std::make_shared<VTKGridFunctionAdapter<DGF> >(factory.create(_data->_lfs.child(tp),_data),name));
614 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
615 OutputCollector& addVertexFunction(TreePath tp, std::string name, Params&&... params)
617 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
618 typedef Function<LFS,Data,Params...> DGF;
619 _vtk_writer.addVertexData(
620 std::make_shared<VTKGridFunctionAdapter<DGF> >(
621 std::make_shared<DGF>(
625 std::forward<Params>(params)...
632 OutputCollector(VTKWriter& vtk_writer,
const std::shared_ptr<Data>& data,
const Predicate& predicate = Predicate())
633 : _vtk_writer(vtk_writer)
635 , _predicate(predicate)
638 VTKWriter& _vtk_writer;
639 std::shared_ptr<Data> _data;
640 Predicate _predicate;
646 template<
typename VTKWriter,
649 typename NameGenerator = vtk::DefaultFunctionNameGenerator,
650 typename Predicate = vtk::DefaultPredicate>
651 vtk::OutputCollector<
653 vtk::DGFTreeCommonData<GFS,X,Predicate>
655 addSolutionToVTKWriter(VTKWriter& vtk_writer,
658 const NameGenerator& name_generator = vtk::defaultNameScheme(),
659 const Predicate& predicate = Predicate())
661 typedef vtk::DGFTreeCommonData<GFS,X,Predicate> Data;
663 vtk::OutputCollector<VTKWriter,Data> collector(vtk_writer, data, predicate);
664 collector.addSolution(name_generator);
669 template<
typename VTKWriter,
672 typename NameGenerator = vtk::DefaultFunctionNameGenerator,
673 typename Predicate = vtk::DefaultPredicate>
674 vtk::OutputCollector<
676 vtk::DGFTreeCommonData<GFS,X,Predicate>
678 addSolutionToVTKWriter(VTKWriter& vtk_writer,
679 std::shared_ptr<GFS> gfs,
680 std::shared_ptr<X> x,
681 const NameGenerator& name_generator = vtk::defaultNameScheme(),
682 const Predicate& predicate = Predicate())
684 typedef vtk::DGFTreeCommonData<GFS,X,Predicate> Data;
685 vtk::OutputCollector<VTKWriter,Data> collector(vtk_writer, std::make_shared<Data>(gfs,x),predicate);
686 collector.addSolution(name_generator);
vector space out of a tensor product of fields.
Definition: fvector.hh:91
IndexTypeImp IndexType
The type used for the indices.
Definition: indexidset.hh:92
leaf of a function tree
Definition: function.hh:302
T Traits
Export type traits.
Definition: function.hh:193
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Evaluate the GridFunction at given position.
Definition: function.hh:208
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:218
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
std::size_t degree(const Node &node)
Returns the degree of node as run time information.
Definition: nodeinterface.hh:79
void applyToTree(Tree &&tree, Visitor &&visitor)
Apply visitor to TypeTree.
Definition: traversal.hh:239
Dune namespace.
Definition: alignedallocator.hh:13
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:72
This file implements several utilities related to std::shared_ptr.
Switch for uniform treatment of local and global basis classes.
Definition: interfaceswitch.hh:154
static const std::size_t dimRange
export dimension of the values
Definition: interfaceswitch.hh:165
Basis::Traits::RangeField RangeField
export field type of the values
Definition: interfaceswitch.hh:163
Basis::Traits::Range Range
export vector type of the values
Definition: interfaceswitch.hh:167
Switch for uniform treatment of finite element with either the local or the global interface.
Definition: interfaceswitch.hh:30
traits class holding the function signature, same as in local function
Definition: function.hh:183
void post(T &&, TreePath) const
Method for postfix tree traversal.
Definition: visitor.hh:83
void leaf(T &&, TreePath) const
Method for leaf traversal.
Definition: visitor.hh:93
Helper classes to provide indices for geometrytypes for use in a vector.