1#ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
2#define DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH
7#include <dune/common/exceptions.hh>
8#include <dune/common/shared_ptr.hh>
10#include <dune/geometry/typeindex.hh>
12#include <dune/localfunctions/common/interfaceswitch.hh>
14#include <dune/typetree/visitor.hh>
15#include <dune/typetree/traversal.hh>
41 template<
typename VTKWriter>
42 struct vtk_writer_traits;
64 template<
typename LFS,
typename Data>
67 template<
typename LFS,
typename Data>
70 template<
typename VTKWriter,
typename Data>
75 template<
typename GFS,
typename X,
typename Pred>
79 template<
typename LFS,
typename Data>
82 template<
typename LFS,
typename Data>
85 template<
typename,
typename>
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;
97 static const auto dim = EntitySet::dimension;
119 auto cell_index =
_index_set.uniqueIndex(cell);
140 std::shared_ptr<const X>
x;
145 template<
typename LFS,
typename Data>
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>
169 typedef BasisInterfaceSwitch<
170 typename FiniteElementInterfaceSwitch<
171 typename LFS::Traits::FiniteElement
177 typename LFS::Traits::GridView,
178 typename BasisSwitch::RangeField,
179 BasisSwitch::dimRange,
180 typename BasisSwitch::Range
192 , _basis(lfs.maxSize())
196 void evaluate (
const typename Traits::ElementType& e,
197 const typename Traits::DomainType& x,
198 typename Traits::RangeType& y)
const
202 typedef FiniteElementInterfaceSwitch<
203 typename LFS::Traits::FiniteElement
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]);
214 const typename Traits::GridViewType&
gridView()
const
216 return _lfs.gridFunctionSpace().gridView();
227 const std::shared_ptr<Data> _data;
228 mutable std::vector<typename Traits::RangeType> _basis;
234 template<
typename LFS,
typename Data>
237 typename LFS::Traits::GridView,
238 typename BasisInterfaceSwitch<
239 typename FiniteElementInterfaceSwitch<
240 typename LFS::ChildType::Traits::FiniteElement
243 TypeTree::StaticDegree<LFS>::value,
245 typename BasisInterfaceSwitch<
246 typename FiniteElementInterfaceSwitch<
247 typename LFS::ChildType::Traits::FiniteElement
250 TypeTree::StaticDegree<LFS>::value
253 DGFTreeVectorFunction<LFS,Data>
257 typedef BasisInterfaceSwitch<
258 typename FiniteElementInterfaceSwitch<
259 typename LFS::ChildType::Traits::FiniteElement
263 static_assert(BasisSwitch::dimRange == 1,
264 "Automatic conversion to vector-valued function only supported for scalar components");
268 typename LFS::Traits::GridView,
269 typename BasisSwitch::RangeField,
270 TypeTree::StaticDegree<LFS>::value,
272 typename BasisSwitch::RangeField,
273 TypeTree::StaticDegree<LFS>::value
283 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType
RF;
284 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType
RT;
290 , _basis(lfs.maxSize())
293 void evaluate (
const typename Traits::ElementType& e,
294 const typename Traits::DomainType& x,
295 typename Traits::RangeType& y)
const
299 typedef FiniteElementInterfaceSwitch<
300 typename ChildLFS::Traits::FiniteElement
305 for (std::size_t k = 0; k < TypeTree::degree(_lfs); ++k)
307 const ChildLFS& child_lfs = _lfs.child(k);
308 FESwitch::basis(child_lfs.finiteElement()).evaluateFunction(x,_basis);
310 for (std::size_t i = 0; i < child_lfs.size(); ++i)
311 y[k] += _data->_x_local(child_lfs,i) * _basis[i];
316 const typename Traits::GridViewType&
gridView()
const
318 return _lfs.gridFunctionSpace().gridView();
329 const std::shared_ptr<Data> _data;
330 mutable std::vector<typename BasisSwitch::Range> _basis;
340 template<
typename TreePath>
341 std::string
operator()(std::string component_name, TreePath tp)
const
343 if (component_name.empty())
346 if (_prefix.empty() && _suffix.empty())
349 "You need to either name all GridFunctionSpaces "
350 "written to the VTK file or provide a prefix / suffix.");
353 std::stringstream name_stream;
355 if (!_prefix.empty())
356 name_stream << _prefix << _separator;
359 for (std::size_t i = 0; i < tp.size(); ++i)
360 name_stream << (i > 0 ? _separator :
"") << tp.element(i);
362 if (!_suffix.empty())
363 name_stream << _separator << _suffix;
364 return name_stream.str();
369 return _prefix + component_name + _suffix;
403 std::string _separator;
413 template<
typename VTKWriter,
typename Data,
typename NameGenerator>
415 :
public TypeTree::DefaultVisitor
416 ,
public TypeTree::DynamicTraversal
420 template<
typename LFS,
typename Child,
typename TreePath>
426 !std::is_convertible<
427 TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>,
435 template<
typename DGF,
typename TreePath>
438 std::string name =
name_generator(dgf->localFunctionSpace().gridFunctionSpace().name(),tp);
439 switch (dgf->dataSetType())
441 case DGF::Output::vertexData:
444 case DGF::Output::cellData:
448 DUNE_THROW(NotImplemented,
"Unsupported data set type");
456 template<
typename LFS,
typename TreePath>
466 template<
typename LFS,
typename TreePath>
483 template<
typename LFS,
typename TreePath>
484 typename std::enable_if<
486 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
487 typename vtk_writer_traits<VTKWriter>::GridView
490 post(
const LFS& lfs, TreePath tp)
495 template<
typename LFS,
typename TreePath>
496 typename std::enable_if<
498 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
499 typename vtk_writer_traits<VTKWriter>::GridView
502 leaf(
const LFS& lfs, TreePath tp)
507 template<
typename LFS,
typename TreePath>
508 typename std::enable_if<
510 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
511 typename vtk_writer_traits<VTKWriter>::GridView
514 post(
const LFS& lfs, TreePath tp)
517 add_vector_solution(lfs,tp,TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>());
521 template<
typename LFS,
typename TreePath>
522 typename std::enable_if<
524 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
525 typename vtk_writer_traits<VTKWriter>::GridView
528 leaf(
const LFS& lfs, TreePath tp)
551 template<
typename LFS,
typename TP>
558 template<
typename VTKWriter,
typename Data_>
565 typedef typename Data::GridFunctionSpace
GFS;
569 template<
typename NameGenerator>
574 TypeTree::applyToTree(
_data->_lfs,visitor);
578 template<
typename Factory,
typename TreePath>
581 typedef typename std::remove_reference<
decltype(*factory.create(
_data->_lfs.child(tp),
_data))>::type DGF;
586 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
589 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
590 typedef Function<LFS,
Data,Params...> DGF;
593 std::make_shared<DGF>(
594 TypeTree::child(
_data->_lfs,tp)
597 std::forward<Params>(params)...
604 template<
typename Factory,
typename TreePath>
607 typedef typename std::remove_reference<
decltype(*factory.create(
_data->_lfs.child(tp),
_data))>::type DGF;
612 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
615 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
616 typedef Function<LFS,
Data,Params...> DGF;
619 std::make_shared<DGF>(
620 TypeTree::child(
_data->_lfs,tp)
623 std::forward<Params>(params)...
657 const Predicate& predicate = Predicate())
660 auto data = std::make_shared<Data>(Dune::stackobject_to_shared_ptr(gfs), Dune::stackobject_to_shared_ptr(x));
670 typename NameGenerator = vtk::DefaultFunctionNameGenerator,
671 typename Predicate = vtk::DefaultPredicate>
672 vtk::OutputCollector<
674 vtk::DGFTreeCommonData<GFS,X,Predicate>
677 std::shared_ptr<GFS> gfs,
678 std::shared_ptr<X> x,
680 const Predicate& predicate = Predicate())
static const int dim
Definition adaptivity.hh:84
For backward compatibility – Do not use this!
Definition adaptivity.hh:28
vtk::OutputCollector< VTKWriter, vtk::DGFTreeCommonData< GFS, X, Predicate > > addSolutionToVTKWriter(VTKWriter &vtk_writer, const GFS &gfs, const X &x, const NameGenerator &name_generator=vtk::defaultNameScheme(), const Predicate &predicate=Predicate())
Definition vtk.hh:653
DefaultFunctionNameGenerator defaultNameScheme()
Definition vtk.hh:407
Output::DataSetType dataSetType() const
Return the data set type of this function.
Definition function.hh:154
traits class holding the function signature, same as in local function
Definition function.hh:183
T Traits
Export type traits.
Definition function.hh:193
leaf of a function tree
Definition function.hh:302
wrap a GridFunction so it can be used with the VTKWriter from dune-grid.
Definition vtkexport.hh:25
Definition lfsindexcache.hh:979
Create a local function space from a global function space.
Definition localfunctionspace.hh:717
A container for storing data associated with the degrees of freedom of a LocalFunctionSpace.
Definition localvector.hh:184
Definition gridfunctionspace/tags.hh:24
Definition gridfunctionspace/tags.hh:28
BaseT::Traits Traits
Definition vtk.hh:186
const LFS & localFunctionSpace() const
Definition vtk.hh:219
DGFTreeLeafFunction(const LFS &lfs, const std::shared_ptr< Data > &data)
Definition vtk.hh:188
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition vtk.hh:196
const Traits::GridViewType & gridView() const
get a reference to the GridView
Definition vtk.hh:214
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition vtk.hh:293
ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType RF
Definition vtk.hh:283
DGFTreeVectorFunction(const LFS &lfs, const std::shared_ptr< Data > &data)
Definition vtk.hh:286
const Traits::GridViewType & gridView() const
get a reference to the GridView
Definition vtk.hh:316
ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType RT
Definition vtk.hh:284
const LFS & localFunctionSpace() const
Definition vtk.hh:321
BaseT::Traits Traits
Definition vtk.hh:281
LFS::ChildType ChildLFS
Definition vtk.hh:282
OutputCollector & addCellFunction(Factory factory, TreePath tp, std::string name)
Definition vtk.hh:579
OutputCollector & addVertexFunction(TreePath tp, std::string name, Params &&... params)
Definition vtk.hh:613
VTKWriter & _vtk_writer
Definition vtk.hh:636
Predicate _predicate
Definition vtk.hh:638
Data::Predicate Predicate
Definition vtk.hh:567
Data::GridFunctionSpace GFS
Definition vtk.hh:565
OutputCollector & addCellFunction(TreePath tp, std::string name, Params &&... params)
Definition vtk.hh:587
OutputCollector & addSolution(const NameGenerator &name_generator)
Definition vtk.hh:570
std::shared_ptr< Data > _data
Definition vtk.hh:637
Data_ Data
Common data container (hierarchic LFS, global solution data etc.)
Definition vtk.hh:563
OutputCollector(VTKWriter &vtk_writer, const std::shared_ptr< Data > &data, const Predicate &predicate=Predicate())
Definition vtk.hh:630
OutputCollector & addVertexFunction(Factory factory, TreePath tp, std::string name)
Definition vtk.hh:605
Data::Vector Vector
Definition vtk.hh:566
Helper class for common data of a DGFTree.
Definition vtk.hh:77
size_type _current_cell_index
Definition vtk.hh:136
std::shared_ptr< const X > x
Definition vtk.hh:140
void bind(const Cell &cell)
Definition vtk.hh:117
Pred Predicate
Definition vtk.hh:103
LFSCache _lfs_cache
Definition vtk.hh:132
X Vector
Definition vtk.hh:102
GFS GridFunctionSpace
Definition vtk.hh:101
XLocalVector _x_local
Definition vtk.hh:134
XView _x_view
Definition vtk.hh:133
DGFTreeCommonData(std::shared_ptr< const GFS > gfs, std::shared_ptr< const X > x)
Definition vtk.hh:105
LFS _lfs
Definition vtk.hh:131
const IndexSet & _index_set
Definition vtk.hh:135
DefaultFunctionNameGenerator(std::string prefix="", std::string suffix="", std::string separator="_")
Definition vtk.hh:391
DefaultFunctionNameGenerator & separator(std::string separator)
Definition vtk.hh:385
DefaultFunctionNameGenerator & prefix(std::string prefix)
Definition vtk.hh:373
DefaultFunctionNameGenerator & suffix(std::string suffix)
Definition vtk.hh:379
std::string operator()(std::string component_name, TreePath tp) const
Definition vtk.hh:341
std::enable_if< std::is_same< typenameLFS::Traits::GridFunctionSpace::Traits::GridView, typenamevtk_writer_traits< VTKWriter >::GridView >::value >::type leaf(const LFS &lfs, TreePath tp)
Create a standard leaf function for leaf GridFunctionSpaces.
Definition vtk.hh:528
std::shared_ptr< Data > data
Definition vtk.hh:543
std::enable_if<!std::is_same< typenameLFS::Traits::GridFunctionSpace::Traits::GridView, typenamevtk_writer_traits< VTKWriter >::GridView >::value >::type leaf(const LFS &lfs, TreePath tp)
Definition vtk.hh:502
Data::Predicate predicate
Definition vtk.hh:545
void add_vector_solution(const LFS &lfs, TreePath tp, VectorGridFunctionSpaceTag tag)
Tag dispatch-based switch that creates a vector-valued function for a VectorGridFunctionSpace.
Definition vtk.hh:457
add_solution_to_vtk_writer_visitor(VTKWriter &vtk_writer_, std::shared_ptr< Data > data_, const NameGenerator &name_generator_, const typename Data::Predicate &predicate_)
Definition vtk.hh:535
std::enable_if<!std::is_same< typenameLFS::Traits::GridFunctionSpace::Traits::GridView, typenamevtk_writer_traits< VTKWriter >::GridView >::value >::type post(const LFS &lfs, TreePath tp)
Definition vtk.hh:490
const NameGenerator & name_generator
Definition vtk.hh:544
VTKWriter & vtk_writer
Definition vtk.hh:542
void add_vector_solution(const LFS &lfs, TreePath tp, GridFunctionSpaceTag tag)
Tag dispatch-based switch that creates a vector-valued function for a VectorGridFunctionSpace.
Definition vtk.hh:467
void add_to_vtk_writer(const std::shared_ptr< DGF > &dgf, TreePath tp)
Definition vtk.hh:436
std::enable_if< std::is_same< typenameLFS::Traits::GridFunctionSpace::Traits::GridView, typenamevtk_writer_traits< VTKWriter >::GridView >::value >::type post(const LFS &lfs, TreePath tp)
Handle VectorGridFunctionSpace components in here.
Definition vtk.hh:514
static const bool value
Definition vtk.hh:424
bool operator()(const LFS &lfs, TP tp) const
Definition vtk.hh:552
static const unsigned int value
Definition gridfunctionspace/tags.hh:139