dune-fem  2.8-git
function/common/localcontribution.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_FUNCTION_COMMON_LOCALCONTRIBUTION_HH
2 #define DUNE_FEM_FUNCTION_COMMON_LOCALCONTRIBUTION_HH
3 
4 #include <algorithm>
5 #include <type_traits>
6 #include <utility>
7 #include <vector>
8 
9 #include <dune/common/densevector.hh>
10 #include <dune/common/ftraits.hh>
11 
16 
17 namespace Dune
18 {
19 
20  namespace Fem
21  {
22 
23  // External Forward Declarations
24  // -----------------------------
25 
26  template< class >
27  struct DiscreteFunctionTraits;
28 
29  class IsDiscreteFunction;
30 
31 
32 
33  namespace Assembly
34  {
35 
36  namespace Global
37  {
38 
39  // AddBase
40  // -------
41 
42  template< class DiscreteFunction >
43  struct AddBase< DiscreteFunction, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
44  {
45  typedef typename DiscreteFunction::DofType DofType;
46 
47  static void begin ( DiscreteFunction &df )
48  {
49  typedef typename DiscreteFunction::DiscreteFunctionSpaceType::LocalBlockIndices LocalBlockIndices;
50 
51  // clear auxiliary DoFs
52  auto &dofVector = df.dofVector();
53  for( const auto &auxiliaryDof : df.space().auxiliaryDofs() )
54  Hybrid::forEach( LocalBlockIndices(), [ &dofVector, &auxiliaryDof ] ( auto &&j ) { dofVector[ auxiliaryDof ][ j ] = DofType( 0 ); } );
55  }
56 
57  static void end ( DiscreteFunction &df ) { df.space().communicate( df, DFCommunicationOperation::Add() ); }
58  };
59 
60 
61 
62  // SetBase
63  // -------
64 
65  template< class DiscreteFunction >
66  struct SetBase< DiscreteFunction, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
67  {
68  static void begin ( DiscreteFunction &df ) {}
69  static void end ( DiscreteFunction &df ) { df.space().communicate( df, DFCommunicationOperation::Copy() ); }
70  };
71 
72  } // namespace Global
73 
74 
75 
76  // AddBase
77  // -------
78 
79  template< class DiscreteFunction >
80  struct AddBase< DiscreteFunction, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
81  {
82  typedef typename DiscreteFunction::DofType DofType;
83 
85 
86  template< class Entity, class LocalDofVector >
87  void begin ( const Entity &entity, const DiscreteFunction &df, LocalDofVector &localDofVector ) const
88  {
89  std::fill( localDofVector.begin(), localDofVector.end(), DofType( 0 ) );
90  }
91 
92  template< class Entity, class LocalDofVector >
93  void end ( const Entity &entity, LocalDofVector &localDofVector, DiscreteFunction &df ) const
94  {
95  df.addLocalDofs( entity, localDofVector );
96  }
97  };
98 
99 
100 
101  // AddScaledBase
102  // -------------
103 
104  template< class DiscreteFunction >
105  struct AddScaledBase< DiscreteFunction, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
106  : public AddBase< DiscreteFunction >
107  {
108  AddScaledBase ( typename DiscreteFunction::DofType factor ) : factor_( std::move( factor ) ) {}
109 
110  template< class Entity, class LocalDofVector >
111  void end ( const Entity &entity, LocalDofVector &localDofVector, DiscreteFunction &df ) const
112  {
113  df.addScaledLocalDofs( entity, factor_, localDofVector );
114  }
115 
116  private:
117  typename DiscreteFunction::DofType factor_;
118  };
119 
120 
121  namespace detail
122  {
123 
124  template< class DiscreteFunction, const bool getAndSet >
125  struct SetAndSelectDFImpl
126  {
127  typedef typename DiscreteFunction::DofType DofType;
128 
129  typedef Global::Set< DiscreteFunction > GlobalOperationType;
130 
131  template< class Entity, class LocalDofVector >
132  void begin ( const Entity &entity, const DiscreteFunction &df, LocalDofVector &localDofVector ) const
133  {
134  if constexpr ( getAndSet )
135  {
136  // obtain local dofs
137  df.getLocalDofs ( entity, localDofVector );
138  }
139  else
140  {
141  // reset all dofs
142  std::fill( localDofVector.begin(), localDofVector.end(), DofType( 0 ) );
143  }
144  }
145 
146  template< class Entity, class LocalDofVector >
147  void end ( const Entity &entity, LocalDofVector &localDofVector, DiscreteFunction &df ) const
148  {
149  df.setLocalDofs( entity, localDofVector );
150  }
151  };
152  }
153 
154 
155  // SetBase
156  // -------
157 
158  template< class DiscreteFunction >
159  struct SetBase< DiscreteFunction, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
160  : public detail::SetAndSelectDFImpl< DiscreteFunction, false >
161  {};
162 
163  // SetSelectedBase
164  // ---------------
165 
166  template< class DiscreteFunction >
167  struct SetSelectedBase< DiscreteFunction, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
168  : public detail::SetAndSelectDFImpl< DiscreteFunction, true >
169  {};
170 
171  } // namespace Assembly
172  }
173 
174  // consistency with Dune::DenseVector and DenseMatrix
175  template< class DiscreteFunction, template< class > class AssemblyOperation >
176  struct FieldTraits< Fem::LocalContribution< DiscreteFunction, AssemblyOperation, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > > >
177  : public FieldTraits< typename DiscreteFunction::DofType >
178  {
179  };
180 
181 
182  namespace Fem
183  {
184 
185  // LocalContribution for Discrete Functions
186  // ----------------------------------------
187 
188  template< class DiscreteFunction, template< class > class AssemblyOperation >
189  class LocalContribution< DiscreteFunction, AssemblyOperation, std::enable_if_t< std::is_base_of< Fem::IsDiscreteFunction, DiscreteFunction >::value > >
190  : public TemporaryLocalFunction< typename DiscreteFunction::DiscreteFunctionSpaceType >
191  {
192  typedef typename DiscreteFunction::DiscreteFunctionSpaceType DiscreteFunctionSpaceType;
195 
196  public:
197  typedef DiscreteFunction DiscreteFunctionType;
198  typedef AssemblyOperation< typename DiscreteFunctionTraits< DiscreteFunctionType >::DiscreteFunctionType > AssemblyOperationType;
199 
200  typedef typename DiscreteFunctionType::DiscreteFunctionSpaceType::BasisFunctionSetType BasisFunctionSetType;
201  typedef typename DiscreteFunctionType::DofType DofType;
202 
203  typedef typename DiscreteFunctionType::RangeType RangeType;
204  typedef typename RangeType::field_type RangeFieldType;
205  typedef typename DiscreteFunctionType::JacobianRangeType JacobianRangeType;
206 
207  typedef typename BaseType :: LocalDofVectorType LocalDofVectorType;
208  typedef typename LocalDofVectorType::size_type SizeType;
209 
210  typedef typename BasisFunctionSetType::EntityType EntityType;
211 
212  using BaseType::entity;
213  using BaseType::localDofVector;
214  using BaseType::axpy;
215 
216  template< class... Args >
217  explicit LocalContribution ( DiscreteFunctionType &discreteFunction, Args &&... args )
218  : BaseType( discreteFunction.space() ),
219  discreteFunction_( discreteFunction ),
220  assemblyOperation_( std::forward< Args >( args )... ),
221  bound_( false )
222  {
223  discreteFunction.template beginAssemble< typename AssemblyOperationType::GlobalOperationType >();
224  }
225 
226  LocalContribution ( const ThisType & ) = delete;
227  LocalContribution ( ThisType && ) = delete;
228 
229  ~LocalContribution () { discreteFunction().template endAssemble< typename AssemblyOperationType::GlobalOperationType >(); }
230 
231  ThisType &operator= ( const ThisType & ) = delete;
232  ThisType &operator= ( ThisType && ) = delete;
233 
234  const DiscreteFunctionType& discreteFunction () const { return discreteFunction_; }
235  DiscreteFunctionType& discreteFunction () { return discreteFunction_; }
236 
237  void bind ( const EntityType &entity )
238  {
239  BaseType::bind( entity );
240  bound_ = true;
241  assemblyOperation_.begin( entity, discreteFunction(), localDofVector() );
242  }
243 
244  void unbind ()
245  {
246  if (bound_)
247  {
248  // write back dofs to discrete function
249  assemblyOperation_.end( entity(), localDofVector(), discreteFunction() );
250  // unbind local contribution
251  BaseType::unbind();
252  }
253  }
254 
255  protected:
256  // LocalContribution is not a LocalFunction,
257  // thus disable evaluate,jacobian and hessian methods
258  using BaseType::evaluate;
259  using BaseType::evaluateQuadrature;
260  using BaseType::jacobian;
261  using BaseType::hessian;
262 
263  protected:
266  bool bound_;
267  };
268 
269  } // namespace Fem
270 
271 } // namespace Dune
272 
273 #endif // #ifndef DUNE_FEM_FUNCTION_COMMON_LOCALCONTRIBUTION_HH
Definition: bindguard.hh:11
void axpy(const T &a, const T &x, T &y)
Definition: space/basisfunctionset/functor.hh:38
static void forEach(IndexRange< T, sz > range, F &&f)
Definition: hybrid.hh:129
Definition: common/localcontribution.hh:14
Definition: common/localcontribution.hh:28
Definition: common/localcontribution.hh:31
Definition: common/localcontribution.hh:41
Definition: common/localcontribution.hh:61
Definition: common/localcontribution.hh:64
Definition: common/localcontribution.hh:67
Definition: common/localcontribution.hh:70
void end(const Entity &entity, LocalDofVector &localDofVector, DiscreteFunction &df) const
Definition: function/common/localcontribution.hh:93
void begin(const Entity &entity, const DiscreteFunction &df, LocalDofVector &localDofVector) const
Definition: function/common/localcontribution.hh:87
AddScaledBase(typename DiscreteFunction::DofType factor)
Definition: function/common/localcontribution.hh:108
void end(const Entity &entity, LocalDofVector &localDofVector, DiscreteFunction &df) const
Definition: function/common/localcontribution.hh:111
LocalContribution(DiscreteFunctionType &discreteFunction, Args &&... args)
Definition: function/common/localcontribution.hh:217
AssemblyOperation< typename DiscreteFunctionTraits< DiscreteFunctionType >::DiscreteFunctionType > AssemblyOperationType
Definition: function/common/localcontribution.hh:198
DiscreteFunctionType::DiscreteFunctionSpaceType::BasisFunctionSetType BasisFunctionSetType
Definition: function/common/localcontribution.hh:200
A temporary function carrying values for one entity.
Definition: temporary.hh:208
just copy data
Definition: commoperations.hh:127
sum up data
Definition: commoperations.hh:144