3#ifndef DUNE_PDELAB_GRIDFUNCTIONSPACE_LOCALFUNCTIONSPACE_HH
4#define DUNE_PDELAB_GRIDFUNCTIONSPACE_LOCALFUNCTIONSPACE_HH
12#include <dune/geometry/referenceelements.hh>
14#include <dune/localfunctions/common/interfaceswitch.hh>
15#include <dune/localfunctions/common/localkey.hh>
17#include <dune/typetree/typetree.hh>
19#include <dune/pdelab/gridfunctionspace/tags.hh>
37 template<
typename =
int>
38 struct PropagateGlobalStorageVisitor
39 :
public TypeTree::TreeVisitor
40 ,
public TypeTree::DynamicTraversal
43 template<
typename LFS,
typename Child,
typename TreePath,
typename ChildIndex>
44 void beforeChild(
const LFS& lfs,
Child&
child, TreePath
treePath, ChildIndex childIndex)
const
46 child._dof_indices = lfs._dof_indices;
53 template<
typename =
int>
54 struct ClearSizeVisitor
55 :
public TypeTree::TreeVisitor
56 ,
public TypeTree::DynamicTraversal
59 template<
typename Node,
typename TreePath>
60 void pre(Node& node, TreePath
treePath)
65 template<
typename Node,
typename TreePath>
66 void leaf(Node& node, TreePath
treePath)
72 ClearSizeVisitor(std::size_t offset_)
76 const std::size_t offset;
81 template<
typename Entity,
bool fast>
82 struct ComputeSizeVisitor
83 :
public TypeTree::TreeVisitor
84 ,
public TypeTree::DynamicTraversal
87 template<
typename Node,
typename TreePath>
88 void pre(Node& node, TreePath
treePath)
93 template<
typename Node,
typename TreePath>
94 void post(Node& node, TreePath
treePath)
96 node.n = offset - node.offset;
99 template<
typename Node,
typename TreePath>
100 void leaf(Node& node, TreePath
treePath)
102 node.offset = offset;
103 node.unbindFiniteElement();
104 node._in_entity_set = node.pgfs->entitySet().contains(e);
105 if (not node._in_entity_set) {
108 node.n = node.pgfs->finiteElementMap().maxLocalSize();
109 node.bindFiniteElement(node.pgfs->finiteElementMap().find(e));
111 node.bindFiniteElement(node.pgfs->finiteElementMap().find(e));
112 node.n = Node::FESwitch::basis(node.finiteElement()).size();
117 ComputeSizeVisitor(
const Entity& entity, std::size_t offset_ = 0)
128 template<
typename Entity,
bool fast>
129 struct FillIndicesVisitor
130 :
public TypeTree::TreeVisitor
131 ,
public TypeTree::DynamicTraversal
134 template<
typename Node,
typename TreePath>
135 void leaf(Node& node, TreePath
treePath)
138 node.dofIndices(e,node._dof_indices->begin()+node.offset,node._dof_indices->begin()+node.offset+node.n,std::integral_constant<bool,fast>{});
141 template<
typename Node,
typename Child,
typename TreePath,
typename ChildIndex>
142 void afterChild(
const Node& node,
const Child&
child, TreePath
treePath, ChildIndex childIndex)
149 for (std::size_t i = 0; i<
child.n; ++i)
152 (*node._dof_indices)[
child.offset+i].treeIndex().push_back(childIndex);
156 FillIndicesVisitor(
const Entity& entity)
170 template<
typename GFS,
typename DI>
193 template <
typename GFS,
typename DOFIndex>
194 class LocalFunctionSpaceBaseNode
196 typedef typename GFS::Traits::Backend B;
199 friend struct PropagateGlobalStorageVisitor;
201 template<
typename,
bool>
202 friend struct ComputeSizeVisitor;
204 template<
typename,
bool>
205 friend struct FillIndicesVisitor;
207 template<
typename LFS,
typename C,
typename Tag,
bool>
208 friend class LFSIndexCacheBase;
214 LocalFunctionSpaceBaseNode (std::shared_ptr<const GFS> gfs)
216 , _dof_index_storage()
217 , _dof_indices(&_dof_index_storage)
222 typename Traits::IndexContainer::size_type size ()
const
227 std::size_t subSpaceDepth()
const
233 typename Traits::IndexContainer::size_type maxSize ()
const
236 return _dof_indices->size();
246 typename Traits::IndexContainer::size_type localVectorSize ()
const
248 return _dof_indices->size();
252 typename Traits::IndexContainer::size_type localIndex (
typename Traits::IndexContainer::size_type index)
const
265 const typename Traits::DOFIndex& dofIndex(
typename Traits::IndexContainer::size_type index)
const
267 return (*_dof_indices)[offset + index];
273 std::cout << n <<
" indices = (";
274 for (
typename Traits::IndexContainer::size_type k=0; k<n; k++)
275 std::cout << (*_dof_indices)[localIndex(k)] <<
" ";
276 std::cout <<
")" << std::endl;
280 const GFS& gridFunctionSpace()
const
286 template<
typename NodeType>
287 void setup(NodeType& node)
289 _dof_index_storage.resize(gridFunctionSpace().ordering().maxLocalSize());
293 std::shared_ptr<GFS const> pgfs;
296 typename Traits::IndexContainer::size_type n;
297 typename Traits::IndexContainer::size_type offset;
301 template<
typename GFS,
typename DOFIndex>
308 typedef typename GFS::Traits::GridViewType
GridView;
310 using EntitySet =
typename GFS::Traits::EntitySet;
316 template <
typename GFS,
typename DOFIndex>
317 class GridViewLocalFunctionSpaceBaseNode :
318 public LocalFunctionSpaceBaseNode<GFS,DOFIndex>
320 typedef typename GFS::Traits::Backend B;
321 typedef LocalFunctionSpaceBaseNode<GFS,DOFIndex> BaseT;
327 GridViewLocalFunctionSpaceBaseNode (std::shared_ptr<const GFS> gfs)
345 template<
typename NodeType,
bool fast = false>
346 void bind (NodeType& node,
const typename Traits::Element& e, std::integral_constant<bool,fast> = std::integral_constant<bool,fast>{});
349 template <
typename GFS,
typename DOFIndex>
350 template <
typename NodeType,
bool fast>
351 void GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex>::bind (NodeType& node,
353 std::integral_constant<bool,fast>)
356 assert(&node ==
this);
359 ComputeSizeVisitor<Element,fast> csv(e);
364 FillIndicesVisitor<Element,fast> fiv(e);
373 template<
typename GFS,
typename DOFIndex,
typename N>
381 template<
typename GFS,
typename DOFIndex,
typename ChildLFS, std::
size_t k>
382 class PowerLocalFunctionSpaceNode :
383 public GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex>,
386 typedef GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex> BaseT;
390 friend struct PropagateGlobalStorageVisitor;
393 friend struct ClearSizeVisitor;
395 template<
typename,
bool>
396 friend struct ComputeSizeVisitor;
398 template<
typename,
bool>
399 friend struct FillIndicesVisitor;
407 template<
typename Transformation>
408 PowerLocalFunctionSpaceNode (std::shared_ptr<const GFS> gfs,
409 const Transformation& t,
410 const std::array<std::shared_ptr<ChildLFS>,k>& children)
415 template<
typename Transformation>
416 PowerLocalFunctionSpaceNode (
const GFS& gfs,
417 const Transformation& t,
418 const std::array<std::shared_ptr<ChildLFS>,k>& children)
424 template<
bool fast = false>
425 void bind (
const typename Traits::Element& e, std::integral_constant<bool,fast> fast_ = std::integral_constant<bool,fast>{})
428 BaseT::bind(*
this,e,fast_);
435 template<
typename SourceNode,
typename Transformation>
436 struct power_gfs_to_lfs_template
438 template<
typename TC>
441 typedef PowerLocalFunctionSpaceNode<SourceNode,typename Transformation::DOFIndex,TC,TypeTree::StaticDegree<SourceNode>::value> type;
446 template<
typename PowerGr
idFunctionSpace,
typename Params>
447 TypeTree::TemplatizedGenericPowerNodeTransformation<
448 PowerGridFunctionSpace,
450 power_gfs_to_lfs_template<PowerGridFunctionSpace,gfs_to_lfs<Params> >::template result
460 template<
typename GFS,
typename DOFIndex,
typename... Children>
461 class CompositeLocalFunctionSpaceNode
462 :
public GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex>
463 ,
public TypeTree::CompositeNode<Children...>
465 typedef GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex> BaseT;
466 typedef TypeTree::CompositeNode<Children...> NodeType;
469 friend struct PropagateGlobalStorageVisitor;
472 friend struct ClearSizeVisitor;
474 template<
typename,
bool>
475 friend struct ComputeSizeVisitor;
477 template<
typename,
bool>
478 friend struct FillIndicesVisitor;
481 typedef PowerCompositeLocalFunctionSpaceTraits<GFS,DOFIndex,CompositeLocalFunctionSpaceNode> Traits;
485 template<
typename Transformation>
486 CompositeLocalFunctionSpaceNode (std::shared_ptr<const GFS> gfs,
487 const Transformation& t,
488 std::shared_ptr<Children>... children)
490 , NodeType(children...)
493 template<
typename Transformation>
494 CompositeLocalFunctionSpaceNode (
const GFS& gfs,
495 const Transformation& t,
496 std::shared_ptr<Children>... children)
498 , NodeType(children...)
502 template<
bool fast = false>
503 void bind (
const typename Traits::Element& e, std::integral_constant<bool,fast> fast_ = std::integral_constant<bool,fast>{})
506 BaseT::bind(*
this,e,fast_);
512 template<
typename SourceNode,
typename Transformation>
513 struct composite_gfs_to_lfs_template
515 template<
typename... TC>
518 typedef CompositeLocalFunctionSpaceNode<SourceNode,
typename Transformation::DOFIndex,TC...> type;
523 template<
typename CompositeGr
idFunctionSpace,
typename Params>
524 TypeTree::TemplatizedGenericCompositeNodeTransformation<
525 CompositeGridFunctionSpace,
527 composite_gfs_to_lfs_template<CompositeGridFunctionSpace,gfs_to_lfs<Params> >::template result
537 template<
typename GFS,
typename DOFIndex,
typename N>
543 typedef typename GFS::Traits::FiniteElementType FiniteElement;
548 typedef typename GFS::Traits::ConstraintsType Constraints;
553 template<
typename GFS,
typename DOFIndex>
555 :
public GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex>
558 typedef GridViewLocalFunctionSpaceBaseNode<GFS,DOFIndex> BaseT;
561 friend struct PropagateGlobalStorageVisitor;
564 friend struct ClearSizeVisitor;
566 template<
typename,
bool>
567 friend struct ComputeSizeVisitor;
569 template<
typename,
bool>
570 friend struct FillIndicesVisitor;
585 template<
typename Transformation>
591 template<
typename Transformation>
600 assert((_in_entity_set && pfe) &&
601 "Local function spaces outside their entity set should not "
602 "request `finiteElement()`. To check if function has support "
603 "use: `size()!= 0`");
611 return this->pgfs->constraints();
615 template<
typename Entity,
typename DOFIndexIterator,
bool fast>
616 void dofIndices(
const Entity& e, DOFIndexIterator it, DOFIndexIterator endit, std::integral_constant<bool,fast>)
618 using EntitySet =
typename GFS::Traits::EntitySet;
619 auto es = this->gridFunctionSpace().entitySet();
621 if (not es.contains(e))
625 auto index = es.indexSet().index(e);
626 GFS::Ordering::Traits::DOFIndexAccessor::store(*it,
gt, index, 0);
637 for (std::size_t i = 0; i < std::size_t(coeffs.size()); ++i, ++it) {
639 auto gt = refEl.type(coeffs.localKey(i).subEntity(),
640 coeffs.localKey(i).codim());
643 auto index = es.indexSet().subIndex(
644 e, coeffs.localKey(i).subEntity(), coeffs.localKey(i).codim());
647 GFS::Ordering::Traits::DOFIndexAccessor::store(
648 *it,
gt, index, coeffs.localKey(i).index());
657 template<
typename GC,
typename LC>
658 void insert_constraints (
const LC& lc, GC& gc)
const
661 typedef typename LC::const_iterator local_col_iterator;
662 typedef typename LC::value_type::second_type::const_iterator local_row_iterator;
663 typedef typename GC::iterator global_col_iterator;
664 typedef typename GC::value_type::second_type global_row_type;
666 for (local_col_iterator cit=lc.begin(); cit!=lc.end(); ++cit)
670 global_col_iterator gcit = gc.insert(std::make_pair(std::ref(this->dofIndex(cit->first)),global_row_type())).first;
673 for (local_row_iterator rit=(cit->second).begin(); rit!=(cit->second).end(); ++rit)
674 gcit->second[this->dofIndex(rit->first)] = rit->second;
679 template<
bool fast = false>
680 void bind (
const typename Traits::Element& e, std::integral_constant<bool,fast> fast_ = std::integral_constant<bool,fast>{})
683 BaseT::bind(*
this,e,fast_);
698 if constexpr (std::is_rvalue_reference_v<FE&&>) {
699 static_assert(std::is_move_constructible_v<FE>);
700 static_assert(std::is_move_assignable_v<FE>);
702 (*spe) = std::move(fe);
704 spe = std::make_shared<typename Traits::FiniteElementType>(std::move(fe));
718 std::shared_ptr<typename Traits::FiniteElementType> spe;
723 template<
typename Gr
idFunctionSpace,
typename Params>
724 TypeTree::GenericLeafNodeTransformation<
727 LeafLocalFunctionSpaceNode<GridFunctionSpace,typename gfs_to_lfs<Params>::DOFIndex>
729 registerNodeTransformation(GridFunctionSpace* gfs, gfs_to_lfs<Params>* t, LeafGridFunctionSpaceTag* tag);
735 template <
typename GFS,
typename TAG=AnySpaceTag>
736 class LocalFunctionSpace;
751 template <
typename GFS,
typename TAG>
756 typedef typename BaseT::Traits::IndexContainer::size_type I;
757 typedef typename BaseT::Traits::IndexContainer::size_type LocalIndex;
760 friend struct PropagateGlobalStorageVisitor;
763 friend struct ClearSizeVisitor;
766 friend struct ComputeSizeVisitor;
769 friend struct FillIndicesVisitor;
772 typedef typename BaseT::Traits
Traits;
786 this->_dof_indices = &(this->_dof_index_storage);
790 LocalIndex localIndex (
typename Traits::IndexContainer::size_type index)
const
792 return LocalIndex(BaseT::localIndex(index));
798 void getChild ()
const;
806 template <
typename GFS>
813 friend struct PropagateGlobalStorageVisitor;
816 friend struct ClearSizeVisitor;
818 template<
typename,
bool>
819 friend struct ComputeSizeVisitor;
821 template<
typename,
bool>
822 friend struct FillIndicesVisitor;
827 : BaseT(TypeTree::TransformTree<GFS,gfs_to_lfs<GFS> >::transform(gfs))
829 this->_dof_indices = &(this->_dof_index_storage);
833 LocalFunctionSpace(std::shared_ptr<const GFS> pgfs)
834 : BaseT(*TypeTree::TransformTree<GFS,gfs_to_lfs<GFS> >::transform_storage(pgfs))
836 this->_dof_indices = &(this->_dof_index_storage);
840 LocalFunctionSpace(
const LocalFunctionSpace & lfs)
846 this->_dof_indices = &(this->_dof_index_storage);
Wrapper class for entities.
Definition: entity.hh:64
single component local function space
Definition: localfunctionspace.hh:557
const Traits::FiniteElementType & finiteElement() const
get finite element
Definition: localfunctionspace.hh:598
void unbindFiniteElement() noexcept
Release view of the bound finite element.
Definition: localfunctionspace.hh:712
void dofIndices(const Entity &e, DOFIndexIterator it, DOFIndexIterator endit, std::integral_constant< bool, fast >)
Calculates the multiindices associated with the given entity.
Definition: localfunctionspace.hh:616
const Traits::ConstraintsType & constraints() const
get constraints engine
Definition: localfunctionspace.hh:609
LeafLocalFunctionSpaceNode(std::shared_ptr< const GFS > gfs, const Transformation &t)
initialize with grid function space
Definition: localfunctionspace.hh:586
void bind(const typename Traits::Element &e, std::integral_constant< bool, fast > fast_=std::integral_constant< bool, fast >{})
bind local function space to entity
Definition: localfunctionspace.hh:680
void bindFiniteElement(FE &&fe)
Binds a finite element to the local space If the finite element is lvalue, the caller (i....
Definition: localfunctionspace.hh:696
Create a local function space from a global function space.
Definition: localfunctionspace.hh:754
Base class for leaf nodes in a dune-typetree.
Definition: leafnode.hh:26
Collect k instances of type T within a dune-typetree.
Definition: powernode.hh:50
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:156
typename std::decay_t< T >::ImplementationTag ImplementationTag
Returns the implementation tag of the given Node.
Definition: nodeinterface.hh:71
constexpr HybridTreePath< T... > treePath(const T &... t)
Constructs a new HybridTreePath from the given indices.
Definition: treepath.hh:188
void applyToTree(Tree &&tree, Visitor &&visitor)
Apply visitor to TypeTree.
Definition: traversal.hh:237
Dune namespace.
Definition: alignedallocator.hh:11
std::shared_ptr< T > stackobject_to_shared_ptr(T &t)
Create a shared_ptr for a stack-allocated object.
Definition: shared_ptr.hh:70
This file implements several utilities related to std::shared_ptr.
Standard Dune debug streams.
Switch for uniform treatment of finite element with either the local or the global interface.
Definition: interfaceswitch.hh:28
static const Coefficients & coefficients(const FiniteElement &fe)
access coefficients
Definition: interfaceswitch.hh:43
FiniteElement::Traits::Coefficients Coefficients
export the type of the coefficients
Definition: interfaceswitch.hh:34
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:196
Definition: localfunctionspacetags.hh:40
traits for local function space on a gridview
Definition: localfunctionspace.hh:303
GFS::Traits::GridViewType GridViewType
Type of the grid view that the underlying grid function space is defined on.
Definition: localfunctionspace.hh:305
GFS::Traits::GridViewType GridView
Type of the grid view that the underlying grid function space is defined on.
Definition: localfunctionspace.hh:308
typename EntitySet::Element Element
Type of codim 0 entity in the grid.
Definition: localfunctionspace.hh:313
Tag denoting a LeafLocalFunctionSpace.
Definition: tags.hh:200
traits for single component local function space
Definition: localfunctionspace.hh:539
GFS::Traits::ConstraintsType ConstraintsType
Type of constraints engine.
Definition: localfunctionspace.hh:546
GFS::Traits::FiniteElementType FiniteElementType
Type of local finite element.
Definition: localfunctionspace.hh:541
traits mapping global function space information to local function space
Definition: localfunctionspace.hh:172
GFS GridFunctionSpace
Type of the underlying grid function space.
Definition: localfunctionspace.hh:177
std::vector< SizeType > IndexContainer
Type of container to store indices.
Definition: localfunctionspace.hh:183
GFS::Traits::SizeType SizeType
Type to store indices from Backend.
Definition: localfunctionspace.hh:180
GFS GridFunctionSpaceType
Type of the underlying grid function space.
Definition: localfunctionspace.hh:174
DI DOFIndex
Type of MultiIndex associated with this LocalFunctionSpace.
Definition: localfunctionspace.hh:186
std::vector< DI > DOFIndexContainer
Type of container to store multiindices.
Definition: localfunctionspace.hh:189
traits for multi component local function space
Definition: localfunctionspace.hh:375
N NodeType
type of local function space node
Definition: localfunctionspace.hh:377
Tag denoting a PowerLocalFunctionSpace.
Definition: tags.hh:194