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> 40 template<
typename VTKWriter>
41 struct vtk_writer_traits;
44 struct vtk_writer_traits<Dune::
VTKWriter<GV> >
63 template<
typename LFS,
typename Data>
66 template<
typename LFS,
typename Data>
69 template<
typename VTKWriter,
typename Data>
74 template<
typename GFS,
typename X,
typename Pred>
78 template<
typename LFS,
typename Data>
81 template<
typename LFS,
typename Data>
84 template<
typename,
typename>
89 typedef typename X::template ConstLocalView<LFSCache> XView;
91 using EntitySet =
typename GFS::Traits::EntitySet;
92 using Cell =
typename EntitySet::Traits::Element;
93 using IndexSet =
typename EntitySet::Traits::IndexSet;
94 typedef typename IndexSet::IndexType size_type;
96 static const auto dim = EntitySet::dimension;
108 , _x_local(_lfs.maxSize())
109 , _index_set(gfs.entitySet().indexSet())
110 , _current_cell_index(
std::numeric_limits<size_type>::max())
117 auto cell_index = _index_set.uniqueIndex(cell);
118 if (_current_cell_index == cell_index)
123 _x_view.bind(_lfs_cache);
124 _x_view.read(_x_local);
126 _current_cell_index = cell_index;
140 template<
typename LFS,
typename Data>
143 typename LFS::Traits::GridView,
144 typename BasisInterfaceSwitch<
145 typename FiniteElementInterfaceSwitch<
146 typename LFS::Traits::FiniteElement
149 BasisInterfaceSwitch<
150 typename FiniteElementInterfaceSwitch<
151 typename LFS::Traits::FiniteElement
154 typename BasisInterfaceSwitch<
155 typename FiniteElementInterfaceSwitch<
156 typename LFS::Traits::FiniteElement
160 DGFTreeLeafFunction<LFS,Data>
164 typedef BasisInterfaceSwitch<
165 typename FiniteElementInterfaceSwitch<
166 typename LFS::Traits::FiniteElement
172 typename LFS::Traits::GridView,
173 typename BasisSwitch::RangeField,
174 BasisSwitch::dimRange,
175 typename BasisSwitch::Range
184 : BaseT(lfs.gridFunctionSpace().dataSetType())
187 , _basis(lfs.maxSize())
192 const typename Traits::DomainType& x,
193 typename Traits::RangeType& y)
const 197 typedef FiniteElementInterfaceSwitch<
198 typename LFS::Traits::FiniteElement
203 FESwitch::basis(_lfs.finiteElement()).evaluateFunction(x,_basis);
204 for (std::size_t i = 0; i < _lfs.size(); ++i)
205 y.axpy(_data->_x_local(_lfs,i),_basis[i]);
209 const typename Traits::GridViewType&
gridView()
const 211 return _lfs.gridFunctionSpace().gridView();
222 const shared_ptr<Data> _data;
223 mutable std::vector<typename Traits::RangeType> _basis;
229 template<
typename LFS,
typename Data>
232 typename LFS::Traits::GridView,
233 typename BasisInterfaceSwitch<
234 typename FiniteElementInterfaceSwitch<
235 typename LFS::ChildType::Traits::FiniteElement
238 TypeTree::StaticDegree<LFS>::value,
240 typename BasisInterfaceSwitch<
241 typename FiniteElementInterfaceSwitch<
242 typename LFS::ChildType::Traits::FiniteElement
245 TypeTree::StaticDegree<LFS>::value
248 DGFTreeVectorFunction<LFS,Data>
252 typedef BasisInterfaceSwitch<
253 typename FiniteElementInterfaceSwitch<
254 typename LFS::ChildType::Traits::FiniteElement
258 static_assert(BasisSwitch::dimRange == 1,
259 "Automatic conversion to vector-valued function only supported for scalar components");
263 typename LFS::Traits::GridView,
264 typename BasisSwitch::RangeField,
267 typename BasisSwitch::RangeField,
268 TypeTree::StaticDegree<LFS>::value
278 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType
RF;
279 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType
RT;
282 : BaseT(lfs.gridFunctionSpace().dataSetType())
285 , _basis(lfs.maxSize())
289 const typename Traits::DomainType& x,
290 typename Traits::RangeType& y)
const 294 typedef FiniteElementInterfaceSwitch<
295 typename ChildLFS::Traits::FiniteElement
300 for (std::size_t k = 0; k < TypeTree::degree(_lfs); ++k)
302 const ChildLFS& child_lfs = _lfs.child(k);
303 FESwitch::basis(child_lfs.finiteElement()).evaluateFunction(x,_basis);
305 for (std::size_t i = 0; i < child_lfs.size(); ++i)
306 y[k] += _data->_x_local(child_lfs,i) * _basis[i];
311 const typename Traits::GridViewType&
gridView()
const 313 return _lfs.gridFunctionSpace().gridView();
324 const shared_ptr<Data> _data;
325 mutable std::vector<typename BasisSwitch::Range> _basis;
335 template<
typename TreePath>
336 std::string
operator()(std::string component_name, TreePath tp)
const 338 if (component_name.empty())
341 if (_prefix.empty() && _suffix.empty())
344 "You need to either name all GridFunctionSpaces " 345 "written to the VTK file or provide a prefix / suffix.");
348 std::stringstream name_stream;
350 if (!_prefix.empty())
351 name_stream << _prefix << _separator;
354 for (std::size_t i = 0; i < tp.size(); ++i)
355 name_stream << (i > 0 ? _separator :
"") << tp.element(i);
357 if (!_suffix.empty())
358 name_stream << _separator << _suffix;
359 return name_stream.str();
364 return _prefix + component_name + _suffix;
382 _separator = separator;
387 std::string suffix =
"",
388 std::string separator =
"_")
391 , _separator(separator)
398 std::string _separator;
408 template<
typename VTKWriter,
typename Data,
typename NameGenerator>
410 :
public TypeTree::DefaultVisitor
411 ,
public TypeTree::DynamicTraversal
415 template<
typename LFS,
typename Child,
typename TreePath>
421 !std::is_convertible<
422 TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>,
430 template<
typename DGF,
typename TreePath>
433 std::string name = name_generator(dgf->localFunctionSpace().gridFunctionSpace().name(),tp);
434 switch (dgf->dataSetType())
436 case DGF::Output::vertexData:
439 case DGF::Output::cellData:
443 DUNE_THROW(NotImplemented,
"Unsupported data set type");
451 template<
typename LFS,
typename TreePath>
461 template<
typename LFS,
typename TreePath>
478 template<
typename LFS,
typename TreePath>
479 typename std::enable_if<
481 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
482 typename vtk_writer_traits<VTKWriter>::GridView
485 post(
const LFS& lfs, TreePath tp)
490 template<
typename LFS,
typename TreePath>
491 typename std::enable_if<
493 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
494 typename vtk_writer_traits<VTKWriter>::GridView
497 leaf(
const LFS& lfs, TreePath tp)
502 template<
typename LFS,
typename TreePath>
503 typename std::enable_if<
505 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
506 typename vtk_writer_traits<VTKWriter>::GridView
509 post(
const LFS& lfs, TreePath tp)
511 if (predicate(lfs, tp))
512 add_vector_solution(lfs,tp,TypeTree::ImplementationTag<typename LFS::Traits::GridFunctionSpace>());
516 template<
typename LFS,
typename TreePath>
517 typename std::enable_if<
519 typename LFS::Traits::GridFunctionSpace::Traits::GridView,
520 typename vtk_writer_traits<VTKWriter>::GridView
523 leaf(
const LFS& lfs, TreePath tp)
525 if (predicate(lfs, tp))
531 : vtk_writer(vtk_writer_)
533 , name_generator(name_generator_)
534 , predicate(predicate_)
546 template<
typename LFS,
typename TP>
553 template<
typename VTKWriter,
typename Data_>
560 typedef typename Data::GridFunctionSpace
GFS;
564 template<
typename NameGenerator>
569 TypeTree::applyToTree(_data->_lfs,visitor);
573 template<
typename Factory,
typename TreePath>
576 typedef typename std::remove_reference<decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
581 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
584 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
585 typedef Function<LFS,Data,Params...> DGF;
586 _vtk_writer.addCellData(
588 std::make_shared<DGF>(
589 TypeTree::child(_data->_lfs,tp)
592 std::forward<Params>(params)...
599 template<
typename Factory,
typename TreePath>
602 typedef typename std::remove_reference<decltype(*factory.create(_data->_lfs.child(tp),_data))>::type DGF;
607 template<
template<
typename...>
class Function,
typename TreePath,
typename... Params>
610 using LFS = TypeTree::ChildForTreePath<typename Data::LFS,TreePath>;
611 typedef Function<LFS,Data,Params...> DGF;
612 _vtk_writer.addVertexData(
614 std::make_shared<DGF>(
615 TypeTree::child(_data->_lfs,tp)
618 std::forward<Params>(params)...
626 : _vtk_writer(vtk_writer)
628 , _predicate(predicate)
652 const Predicate& predicate = Predicate())
664 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH size_type _current_cell_index
Definition: vtk.hh:134
static const int dim
Definition: adaptivity.hh:83
const Entity & e
Definition: localfunctionspace.hh:111
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:452
BaseT::Traits Traits
Definition: vtk.hh:276
const IndexSet & _index_set
Definition: vtk.hh:133
DGFTreeLeafFunction(const LFS &lfs, const shared_ptr< Data > &data)
Definition: vtk.hh:183
GFS GridFunctionSpace
Definition: vtk.hh:100
leaf of a function tree
Definition: function.hh:298
Data::Vector Vector
Definition: vtk.hh:561
LFSCache _lfs_cache
Definition: vtk.hh:130
XLocalVector _x_local
Definition: vtk.hh:132
OutputCollector & addSolution(const NameGenerator &name_generator)
Definition: vtk.hh:565
OutputCollector & addVertexFunction(Factory factory, TreePath tp, std::string name)
Definition: vtk.hh:600
Data::Predicate predicate
Definition: vtk.hh:540
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition: vtk.hh:288
bool operator()(const LFS &lfs, TP tp) const
Definition: vtk.hh:547
OutputCollector & addCellFunction(TreePath tp, std::string name, Params &&... params)
Definition: vtk.hh:582
DefaultFunctionNameGenerator & suffix(std::string suffix)
Definition: vtk.hh:374
typename impl::BackendVectorSelector< GridFunctionSpace, FieldType >::Type Vector
alias of the return type of BackendVectorSelector
Definition: backend/interface.hh:106
const NameGenerator & name_generator
Definition: vtk.hh:539
void evaluate(const typename Traits::ElementType &e, const typename Traits::DomainType &x, typename Traits::RangeType &y) const
Definition: vtk.hh:191
void bind(const Cell &cell)
Definition: vtk.hh:115
const LFS & localFunctionSpace() const
Definition: vtk.hh:316
Data::GridFunctionSpace GFS
Definition: vtk.hh:560
BaseT::Traits Traits
Definition: vtk.hh:181
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:462
DefaultFunctionNameGenerator & prefix(std::string prefix)
Definition: vtk.hh:368
Predicate _predicate
Definition: vtk.hh:633
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
Pred Predicate
Definition: vtk.hh:102
const LFS & localFunctionSpace() const
Definition: vtk.hh:214
DGFTreeCommonData(const GFS &gfs, const X &x)
Definition: vtk.hh:104
OutputCollector(VTKWriter &vtk_writer, const shared_ptr< Data > &data, const Predicate &predicate=Predicate())
Definition: vtk.hh:625
Helper class for common data of a DGFTree.
Definition: vtk.hh:75
Definition: gridfunctionspace/tags.hh:28
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:530
Data::Predicate Predicate
Definition: vtk.hh:562
void add_to_vtk_writer(const shared_ptr< DGF > &dgf, TreePath tp)
Definition: vtk.hh:431
Data_ Data
Common data container (hierarchic LFS, global solution data etc.)
Definition: vtk.hh:558
T Traits
Export type traits.
Definition: function.hh:192
DefaultFunctionNameGenerator defaultNameScheme()
Definition: vtk.hh:402
OutputCollector & addCellFunction(Factory factory, TreePath tp, std::string name)
Definition: vtk.hh:574
LFS _lfs
Definition: vtk.hh:129
std::enable_if< !std::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:485
LFS::ChildType ChildLFS
Definition: vtk.hh:277
X Vector
Definition: vtk.hh:101
wrap a GridFunction so it can be used with the VTKWriter from dune-grid.
Definition: vtkexport.hh:22
std::enable_if< !std::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:497
OutputCollector & addVertexFunction(TreePath tp, std::string name, Params &&... params)
Definition: vtk.hh:608
std::enable_if< std::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:523
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:648
DefaultFunctionNameGenerator(std::string prefix="", std::string suffix="", std::string separator="_")
Definition: vtk.hh:386
ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType RT
Definition: vtk.hh:279
const Traits::GridViewType & gridView() const
get a reference to the GridView
Definition: vtk.hh:209
DefaultFunctionNameGenerator & separator(std::string separator)
Definition: vtk.hh:380
VTKWriter & vtk_writer
Definition: vtk.hh:537
VTKWriter & _vtk_writer
Definition: vtk.hh:631
DGFTreeVectorFunction(const LFS &lfs, const shared_ptr< Data > &data)
Definition: vtk.hh:281
std::enable_if< std::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:509
shared_ptr< Data > data
Definition: vtk.hh:538
std::string operator()(std::string component_name, TreePath tp) const
Definition: vtk.hh:336
ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType RF
Definition: vtk.hh:278
static const unsigned int value
Definition: gridfunctionspace/tags.hh:139
Definition: gridfunctionspace/tags.hh:24
shared_ptr< Data > _data
Definition: vtk.hh:632
const Traits::GridViewType & gridView() const
get a reference to the GridView
Definition: vtk.hh:311
traits class holding the function signature, same as in local function
Definition: function.hh:176
XView _x_view
Definition: vtk.hh:131