1 #ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH 2 #define DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH 7 #include <dune/common/exceptions.hh> 9 #include <dune/geometry/typeindex.hh> 11 #include <dune/localfunctions/common/interfaceswitch.hh> 13 #include <dune/typetree/visitor.hh> 14 #include <dune/typetree/traversal.hh> 43 template<
typename VTKWriter>
44 struct vtk_writer_traits;
47 struct vtk_writer_traits<Dune::
VTKWriter<GV> >
72 template<
typename LFS,
typename Data>
75 template<
typename LFS,
typename Data>
78 template<
typename VTKWriter,
typename Data>
83 template<
typename GFS,
typename X,
typename Pred>
87 template<
typename LFS,
typename Data>
90 template<
typename LFS,
typename Data>
93 template<
typename,
typename>
98 typedef typename X::template ConstLocalView<LFSCache> XView;
100 using EntitySet =
typename GFS::Traits::EntitySet;
101 using Cell =
typename EntitySet::Traits::Element;
102 using IndexSet =
typename EntitySet::Traits::IndexSet;
103 typedef typename GFS::Traits::SizeType size_type;
105 static const decltype(EntitySet::dimension)
dim = EntitySet::dimension;
117 , _x_local(_lfs.maxSize())
118 , _index_set(gfs.entitySet().indexSet())
119 , _current_cell_index(
std::numeric_limits<size_type>::max())
126 auto cell_index = _index_set.uniqueIndex(cell);
127 if (_current_cell_index == cell_index)
132 _x_view.bind(_lfs_cache);
133 _x_view.read(_x_local);
135 _current_cell_index = cell_index;
149 template<
typename LFS,
typename Data>
151 :
public TypeTree::LeafNode
153 typename LFS::Traits::GridView,
154 typename BasisInterfaceSwitch<
155 typename FiniteElementInterfaceSwitch<
156 typename LFS::Traits::FiniteElement
159 BasisInterfaceSwitch<
160 typename FiniteElementInterfaceSwitch<
161 typename LFS::Traits::FiniteElement
164 typename BasisInterfaceSwitch<
165 typename FiniteElementInterfaceSwitch<
166 typename LFS::Traits::FiniteElement
170 DGFTreeLeafFunction<LFS,Data>
174 typedef BasisInterfaceSwitch<
175 typename FiniteElementInterfaceSwitch<
176 typename LFS::Traits::FiniteElement
182 typename LFS::Traits::GridView,
183 typename BasisSwitch::RangeField,
184 BasisSwitch::dimRange,
185 typename BasisSwitch::Range
194 : BaseT(lfs.gridFunctionSpace().dataSetType())
197 , _basis(lfs.maxSize())
202 const typename Traits::DomainType& x,
203 typename Traits::RangeType& y)
const 207 typedef FiniteElementInterfaceSwitch<
208 typename LFS::Traits::FiniteElement
213 FESwitch::basis(_lfs.finiteElement()).evaluateFunction(x,_basis);
214 for (std::size_t i = 0; i < _lfs.size(); ++i)
215 y.axpy(_data->_x_local(_lfs,i),_basis[i]);
219 const typename Traits::GridViewType&
gridView()
const 221 return _lfs.gridFunctionSpace().gridView();
232 const shared_ptr<Data> _data;
233 mutable std::vector<typename Traits::RangeType> _basis;
239 template<
typename LFS,
typename Data>
241 :
public TypeTree::LeafNode
243 typename LFS::Traits::GridView,
244 typename BasisInterfaceSwitch<
245 typename FiniteElementInterfaceSwitch<
246 typename LFS::ChildType::Traits::FiniteElement
251 typename BasisInterfaceSwitch<
252 typename FiniteElementInterfaceSwitch<
253 typename LFS::ChildType::Traits::FiniteElement
259 DGFTreeVectorFunction<LFS,Data>
263 typedef BasisInterfaceSwitch<
264 typename FiniteElementInterfaceSwitch<
265 typename LFS::ChildType::Traits::FiniteElement
269 static_assert(BasisSwitch::dimRange == 1,
270 "Automatic conversion to vector-valued function only supported for scalar components");
274 typename LFS::Traits::GridView,
275 typename BasisSwitch::RangeField,
278 typename BasisSwitch::RangeField,
289 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType
RF;
290 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType
RT;
293 : BaseT(lfs.gridFunctionSpace().dataSetType())
296 , _basis(lfs.maxSize())
300 const typename Traits::DomainType& x,
301 typename Traits::RangeType& y)
const 305 typedef FiniteElementInterfaceSwitch<
306 typename ChildLFS::Traits::FiniteElement
311 for (std::size_t k = 0; k < LFS::CHILDREN; ++k)
313 const ChildLFS& child_lfs = _lfs.child(k);
314 FESwitch::basis(child_lfs.finiteElement()).evaluateFunction(x,_basis);
316 for (std::size_t i = 0; i < child_lfs.size(); ++i)
317 y[k] += _data->_x_local(child_lfs,i) * _basis[i];
322 const typename Traits::GridViewType&
gridView()
const 324 return _lfs.gridFunctionSpace().gridView();
335 const shared_ptr<Data> _data;
336 mutable std::vector<typename BasisSwitch::Range> _basis;
346 template<
typename TreePath>
347 std::string
operator()(std::string component_name, TreePath tp)
const 349 if (component_name.empty())
352 if (_prefix.empty() && _suffix.empty())
355 "You need to either name all GridFunctionSpaces " 356 "written to the VTK file or provide a prefix / suffix.");
359 std::stringstream name_stream;
361 if (!_prefix.empty())
362 name_stream << _prefix << _separator;
365 for (std::size_t i = 0; i < tp.size(); ++i)
366 name_stream << (i > 0 ? _separator :
"") << tp.element(i);
368 if (!_suffix.empty())
369 name_stream << _separator << _suffix;
370 return name_stream.str();
375 return _prefix + component_name + _suffix;
393 _separator = separator;
398 std::string suffix =
"",
399 std::string separator =
"_")
402 , _separator(separator)
409 std::string _separator;
419 template<
typename VTKWriter,
typename Data,
typename NameGenerator>
421 :
public TypeTree::DefaultVisitor
422 ,
public TypeTree::DynamicTraversal
426 template<
typename LFS,
typename Child,
typename TreePath>
432 !std::is_convertible<
433 typename LFS::Traits::GridFunctionSpace::ImplementationTag,
441 template<
typename DGF,
typename TreePath>
444 std::string name = name_generator(dgf->localFunctionSpace().gridFunctionSpace().name(),tp);
445 switch (dgf->dataSetType())
447 case DGF::Output::vertexData:
450 case DGF::Output::cellData:
454 DUNE_THROW(NotImplemented,
"Unsupported data set type");
462 template<
typename LFS,
typename TreePath>
472 template<
typename LFS,
typename TreePath>
489 template<
typename LFS,
typename TreePath>
492 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
493 typename vtk_writer_traits<VTKWriter>::GridView
496 post(
const LFS& lfs, TreePath tp)
501 template<
typename LFS,
typename TreePath>
504 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
505 typename vtk_writer_traits<VTKWriter>::GridView
508 leaf(
const LFS& lfs, TreePath tp)
513 template<
typename LFS,
typename TreePath>
516 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
517 typename vtk_writer_traits<VTKWriter>::GridView
520 post(
const LFS& lfs, TreePath tp)
523 add_vector_solution(lfs,tp,
typename LFS::Traits::GridFunctionSpace::ImplementationTag());
527 template<
typename LFS,
typename TreePath>
530 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
531 typename vtk_writer_traits<VTKWriter>::GridView
534 leaf(
const LFS& lfs, TreePath tp)
542 : vtk_writer(vtk_writer_)
544 , name_generator(name_generator_)
545 , predicate(predicate_)
564 template<
typename VTKWriter,
typename Data_>
571 typedef typename Data::GridFunctionSpace
GFS;
575 template<
typename NameGenerator>
580 TypeTree::applyToTree(_data->_lfs,visitor);
584 template<
typename Factory,
typename TreePath>
587 typedef typename std::remove_reference<decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
592 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
595 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
596 typedef Function<LFS,Data,Params...> DGF;
597 _vtk_writer.addCellData(
599 std::make_shared<DGF>(
600 TypeTree::child(_data->_lfs,tp)
603 std::forward<Params>(params)...
610 template<
typename Factory,
typename TreePath>
613 typedef typename std::remove_reference<decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
618 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
621 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
622 typedef Function<LFS,Data,Params...> DGF;
623 _vtk_writer.addVertexData(
625 std::make_shared<DGF>(
626 TypeTree::child(_data->_lfs,tp)
629 std::forward<Params>(params)...
637 : _vtk_writer(vtk_writer)
639 , _predicate(predicate)
663 const Predicate& predicate = Predicate())
675 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH BaseT::Traits Traits
Definition: vtk.hh:191
XLocalVector _x_local
Definition: vtk.hh:141
Data::GridFunctionSpace GFS
Definition: vtk.hh:571
OutputCollector & addVertexFunction(Factory factory, TreePath tp, std::string name)
Definition: vtk.hh:611
VTKWriter & _vtk_writer
Definition: vtk.hh:642
bool operator()(const T &t) const
Definition: vtk.hh:558
X Vector
Definition: vtk.hh:110
static const unsigned int value
Definition: gridfunctionspace/tags.hh:177
DefaultFunctionNameGenerator & prefix(std::string prefix)
Definition: vtk.hh:379
ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType RT
Definition: vtk.hh:290
std::string operator()(std::string component_name, TreePath tp) const
Definition: vtk.hh:347
OutputCollector(VTKWriter &vtk_writer, const shared_ptr< Data > &data, const Predicate &predicate=Predicate())
Definition: vtk.hh:636
LFS::ChildType ChildLFS
Definition: vtk.hh:288
DefaultFunctionNameGenerator(std::string prefix="", std::string suffix="", std::string separator="_")
Definition: vtk.hh:397
OutputCollector & addCellFunction(TreePath tp, std::string name, Params &&...params)
Definition: vtk.hh:593
BaseT::Traits Traits
Definition: vtk.hh:287
Data::Vector Vector
Definition: vtk.hh:572
static const int dim
Definition: adaptivity.hh:83
shared_ptr< Data > _data
Definition: vtk.hh:643
Definition: adaptivity.hh:27
Data::Predicate predicate
Definition: vtk.hh:551
void add_to_vtk_writer(const shared_ptr< DGF > &dgf, TreePath tp)
Definition: vtk.hh:442
OutputCollector & addSolution(const NameGenerator &name_generator)
Definition: vtk.hh:576
XView _x_view
Definition: vtk.hh:140
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition: vtk.hh:299
Definition: gridfunctionspace/tags.hh:28
enable_if< !is_same< typename LFS::Traits::GridFunctionSpace::Traits::GridView, typename vtk_writer_traits< VTKWriter >::GridView >::value >::type leaf(const LFS &lfs, TreePath tp)
Definition: vtk.hh:508
DefaultFunctionNameGenerator & suffix(std::string suffix)
Definition: vtk.hh:385
OutputCollector & addCellFunction(Factory factory, TreePath tp, std::string name)
Definition: vtk.hh:585
DGFTreeVectorFunction(const LFS &lfs, const shared_ptr< Data > &data)
Definition: vtk.hh:292
const Entity & e
Definition: localfunctionspace.hh:111
DefaultFunctionNameGenerator defaultNameScheme()
Definition: vtk.hh:413
add_solution_to_vtk_writer_visitor(VTKWriter &vtk_writer_, shared_ptr< Data > data_, const NameGenerator &name_generator_, const typename Data::Predicate &predicate_)
Definition: vtk.hh:541
const Traits::GridViewType & gridView() const
get a reference to the GridView
Definition: vtk.hh:322
GFS GridFunctionSpace
Definition: vtk.hh:109
ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType RF
Definition: vtk.hh:289
const NameGenerator & name_generator
Definition: vtk.hh:550
const LFS & localFunctionSpace() const
Definition: vtk.hh:224
T Traits
Export type traits.
Definition: function.hh:191
Predicate _predicate
Definition: vtk.hh:644
Data::Predicate Predicate
Definition: vtk.hh:573
void bind(const Cell &cell)
Definition: vtk.hh:124
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:473
OutputCollector & addVertexFunction(TreePath tp, std::string name, Params &&...params)
Definition: vtk.hh:619
enable_if< !is_same< typename LFS::Traits::GridFunctionSpace::Traits::GridView, typename vtk_writer_traits< VTKWriter >::GridView >::value >::type post(const LFS &lfs, TreePath tp)
Definition: vtk.hh:496
Definition: gridfunctionspace/tags.hh:24
const Traits::GridViewType & gridView() const
get a reference to the GridView
Definition: vtk.hh:219
typename impl::BackendVectorSelector< GridFunctionSpace, FieldType >::Type Vector
alias of the return type of BackendVectorSelector
Definition: backend/interface.hh:113
const IndexSet & _index_set
Definition: vtk.hh:142
enable_if< is_same< typename LFS::Traits::GridFunctionSpace::Traits::GridView, typename vtk_writer_traits< VTKWriter >::GridView >::value >::type post(const LFS &lfs, TreePath tp)
Handle VectorGridFunctionSpace components in here.
Definition: vtk.hh:520
DefaultFunctionNameGenerator & separator(std::string separator)
Definition: vtk.hh:391
VTKWriter & vtk_writer
Definition: vtk.hh:548
Pred Predicate
Definition: vtk.hh:111
Data_ Data
Common data container (hierarchic LFS, global solution data etc.)
Definition: vtk.hh:569
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:463
wrap a GridFunction so it can be used with the VTKWriter from dune-grid.
Definition: vtkexport.hh:22
enable_if< is_same< typename LFS::Traits::GridFunctionSpace::Traits::GridView, typename vtk_writer_traits< VTKWriter >::GridView >::value >::type leaf(const LFS &lfs, TreePath tp)
Create a standard leaf function for leaf GridFunctionSpaces.
Definition: vtk.hh:534
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:659
size_type _current_cell_index
Definition: vtk.hh:143
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition: vtk.hh:201
Helper class for common data of a DGFTree.
Definition: vtk.hh:84
DGFTreeLeafFunction(const LFS &lfs, const shared_ptr< Data > &data)
Definition: vtk.hh:193
LFS _lfs
Definition: vtk.hh:138
a GridFunction maps x in DomainType to y in RangeType
Definition: function.hh:186
shared_ptr< Data > data
Definition: vtk.hh:549
const LFS & localFunctionSpace() const
Definition: vtk.hh:327
LFSCache _lfs_cache
Definition: vtk.hh:139
traits class holding the function signature, same as in local function
Definition: function.hh:175