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
189 :
BaseT(lfs.gridFunctionSpace().dataSetType())
192 , _basis(lfs.maxSize())
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>
235 class DGFTreeVectorFunction
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,
272 typename BasisSwitch::RangeField,
276 DGFTreeVectorFunction<LFS,Data>
283 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeFieldType
RF;
284 typedef typename ChildLFS::Traits::FiniteElement::Traits::LocalBasisType::Traits::RangeType
RT;
287 :
BaseT(lfs.gridFunctionSpace().dataSetType())
290 , _basis(lfs.maxSize())
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_>
559 struct OutputCollector
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())
691 #endif // DUNE_PDELAB_GRIDFUNCTIONSPACE_VTK_HH