dune-fem  2.8-git
operator/common/localcontribution.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_OPERATOR_COMMON_LOCALCONTRIBUTION_HH
2 #define DUNE_FEM_OPERATOR_COMMON_LOCALCONTRIBUTION_HH
3 
4 #include <algorithm>
5 #include <type_traits>
6 #include <vector>
7 #include <utility>
8 
9 #include <dune/common/densematrix.hh>
10 #include <dune/common/dynvector.hh>
11 
18 
19 namespace Dune
20 {
21 
22  namespace Fem
23  {
24 
25  namespace Assembly
26  {
27 
28  namespace Global
29  {
30 
31  // AddBase
32  // -------
33 
34  template< class AssembledOperator >
35  struct AddBase< AssembledOperator, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
36  {
37  template <class DF, class RF>
39  template <class DF, class RF>
41  };
42 
43 
44 
45  // SetBase
46  // -------
47 
48  template< class AssembledOperator >
49  struct SetBase< AssembledOperator, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
50  {
51  template <class DF, class RF>
53  template <class DF, class RF>
55  };
56 
57  } // namespace Global
58 
59 
60 
61 
62  namespace Impl
63  {
64 
65  template< class LocalMatrix >
66  struct LocalMatrixGetter
67  {
68  typedef typename LocalMatrix::value_type value_type;
69 
70  explicit LocalMatrixGetter ( const LocalMatrix &localMatrix ) : localMatrix_( localMatrix ) {}
71 
72  decltype( auto ) operator[] ( int row ) const { return localMatrix_[ row ]; }
73 
74  value_type get ( int row, int col ) const { return localMatrix_[ row ][ col ]; }
75 
76  const LocalMatrix & localMatrix() const { return localMatrix_; }
77 
78  private:
79  const LocalMatrix &localMatrix_;
80  };
81 
82  } // namespace Impl
83 
84 
85 
86  // AddBase
87  // -------
88 
89  template< class AssembledOperator >
90  struct AddBase< AssembledOperator, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
91  {
93 
95 
96  template< class DomainEntity, class RangeEntity, class LocalMatrix >
97  void begin ( const DomainEntity &domainEntity, const RangeEntity &rangeEntity, const AssembledOperator &op, LocalMatrix &localMatrix ) const
98  {
99  localMatrix.clear();
100  }
101 
102  template< class DomainEntity, class RangeEntity, class LocalMatrix >
103  void end ( const DomainEntity &domainEntity, const RangeEntity &rangeEntity, LocalMatrix &localMatrix, AssembledOperator &op ) const
104  {
105  op.addLocalMatrix( domainEntity, rangeEntity, Impl::LocalMatrixGetter< LocalMatrix >( localMatrix ) );
106  }
107  };
108 
109 
110 
111  // AddScaledBase
112  // -------------
113 
114  template< class AssembledOperator >
115  struct AddScaledBase< AssembledOperator, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
116  : public AddBase< AssembledOperator >
117  {
119 
120  AddScaledBase ( value_type factor ) : factor_( std::move( factor ) ) {}
121 
122  template< class DomainEntity, class RangeEntity, class LocalMatrix >
123  void end ( const DomainEntity &domainEntity, const RangeEntity &rangeEntity, LocalMatrix &localMatrix, AssembledOperator &op ) const
124  {
125  op.addScaledLocalMatrix( domainEntity, rangeEntity, Impl::LocalMatrixGetter< LocalMatrix >( localMatrix ), factor_ );
126  }
127 
128  private:
129  value_type factor_;
130  };
131 
132 
133 
134  // SetAndSeclectBaseImpl
135  // ---------------------
136 
137  template< class AssembledOperator, const bool getAndSet >
139  {
141 
143 
144  template< class DomainEntity, class RangeEntity, class LocalMatrix >
145  void begin ( const DomainEntity &domainEntity, const RangeEntity &rangeEntity, const AssembledOperator &op, LocalMatrix &localMatrix ) const
146  {
147  if constexpr ( getAndSet )
148  {
149  // get values first for modification
150  op.getLocalMatrix( domainEntity, rangeEntity, localMatrix );
151  }
152  else // set only
153  {
154  localMatrix.clear();
155  }
156  }
157 
158  template< class DomainEntity, class RangeEntity, class LocalMatrix >
159  void end ( const DomainEntity &domainEntity, const RangeEntity &rangeEntity, LocalMatrix &localMatrix, AssembledOperator &op ) const
160  {
161  op.setLocalMatrix( domainEntity, rangeEntity, Impl::LocalMatrixGetter< LocalMatrix >( localMatrix ) );
162  }
163  };
164 
165  // SetBase
166  // -------
167  template< class AssembledOperator >
168  struct SetBase< AssembledOperator, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
169  : public SetAndSeclectBaseImpl< AssembledOperator, false > // set only
170  {
171  };
172 
173  // SetSelectedBase
174  // ---------------
175 
176  template< class AssembledOperator >
177  struct SetSelectedBase< AssembledOperator, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
178  : public SetAndSeclectBaseImpl< AssembledOperator, true > // get and set
179  {
180  };
181 
182  } // namespace Assembly
183 
184  } // namespace Fem
185 
186 
187  namespace Fem
188  {
189 
190  // LocalContribution for Assembled Operators
191  // -----------------------------------------
192 
193  template< class AssembledOperator, template< class > class AssemblyOperation >
194  class LocalContribution< AssembledOperator, AssemblyOperation, std::enable_if_t< Fem::IsAssembledOperator< AssembledOperator >::value > >
195  : public TemporaryLocalMatrix< typename AssembledOperator::DomainFunctionType::DiscreteFunctionSpaceType,
196  typename AssembledOperator::RangeFunctionType::DiscreteFunctionSpaceType
197  >
198  {
199  public:
201  typedef AssemblyOperation< AssembledOperator > AssemblyOperationType;
202 
203  typedef typename AssembledOperatorType::DomainFunctionType::DiscreteFunctionSpaceType DomainSpaceType;
204  typedef typename AssembledOperatorType::RangeFunctionType::DiscreteFunctionSpaceType RangeSpaceType;
205 
206  private:
209 
210  public:
211  typedef typename DomainSpaceType::BasisFunctionSetType DomainBasisFunctionSetType;
212  typedef typename RangeSpaceType::BasisFunctionSetType RangeBasisFunctionSetType;
213 
214  typedef typename DomainBasisFunctionSetType::EntityType DomainEntityType;
215  typedef typename RangeBasisFunctionSetType::EntityType RangeEntityType;
216 
218 
219  typedef typename BaseType :: MatrixEntriesType LocalMatrixEntriesType;
220  typedef typename LocalMatrixEntriesType::size_type SizeType;
221 
224 
225  private:
226  template< class T, class... U >
227  struct InTypeRange
228  : public std::integral_constant< bool, Std::Or( std::is_same< T, U >::value... ) >
229  {};
230 
231  template< class T >
232  struct IsRangeValue
233  : public InTypeRange< T, typename RangeBasisFunctionSetType::RangeType,
234  typename RangeBasisFunctionSetType::JacobianRangeType,
235  typename RangeBasisFunctionSetType::HessianRangeType >
236  {};
237 
238  struct ColIndexMapper
239  {
240  ColIndexMapper ( SizeType j, SizeType cols ) : j_( j ), cols_( cols ) {}
241 
242  SizeType operator[] ( SizeType i ) const { return (i*cols_ + j_); }
243 
244  private:
245  SizeType j_, cols_;
246  };
247 
248  protected:
249  using BaseType::mat_cols;
250 
251  public:
252  using BaseType::domainBasisFunctionSet;
253  using BaseType::rangeBasisFunctionSet;
254 
255  template< class... Args >
256  explicit LocalContribution ( AssembledOperator &assembledOperator, Args &&... args )
257  : BaseType( assembledOperator.domainSpace(), assembledOperator.rangeSpace() ),
258  assembledOperator_( assembledOperator ),
259  assemblyOperation_( std::forward< Args >( args )... )
260  {
261  assembledOperator.template beginAssemble< typename AssemblyOperationType::GlobalOperationType >();
262  }
263 
264  LocalContribution ( const ThisType & ) = delete;
265  LocalContribution ( ThisType && ) = delete;
266 
267  ~LocalContribution () { assembledOperator().template endAssemble< typename AssemblyOperationType::GlobalOperationType >(); }
268 
269  ThisType &operator= ( const ThisType & ) = delete;
270  ThisType &operator= ( ThisType && ) = delete;
271 
272  const AssembledOperatorType &assembledOperator () const { return assembledOperator_; }
273  AssembledOperatorType &assembledOperator () { return assembledOperator_; }
274 
276  {
277  return SubVector< LocalMatrixEntriesType, ColIndexMapper >( localMatrixEntries(), ColIndexMapper( j, mat_cols() ) );
278  }
279 
281  {
282  return SubVector< const LocalMatrixEntriesType, ColIndexMapper >( localMatrixEntries(), ColIndexMapper( j, mat_cols() ) );
283  }
284 
285  // inherited from DenseMatrix
286  using BaseType :: axpy;
287 
288  template< class Point, class... Factors >
289  auto axpy ( const Point &x, const Factors &... factors )
290  -> std::enable_if_t< Std::And( (IsRangeValue< std::decay_t< decltype( std::declval< Factors & >()[ 0 ] ) > >::value)... ) >
291  {
292  const SizeType matCols = mat_cols();
293  for( SizeType j = 0; j < matCols; ++j )
294  {
295  auto col = column( j );
296  rangeBasisFunctionSet().axpy( x, factors[ j ]..., col );
297  }
298  }
299 
311  int order () const { return domainBasisFunctionSet().order() + rangeBasisFunctionSet().order(); }
312 
314  void bind ( const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity )
315  {
316  BaseType::bind( domainEntity, rangeEntity );
317  assemblyOperation_.begin( domainEntity, rangeEntity, assembledOperator(), *this );
318  }
319 
321  void unbind ()
322  {
323  assemblyOperation_.end( domainBasisFunctionSet().entity(), rangeBasisFunctionSet().entity(), *this, assembledOperator() );
324  BaseType::unbind();
325  }
326 
328  const LocalMatrixEntriesType &localMatrixEntries () const { return fields_; }
331 
332  protected:
333  using BaseType::fields_;
336  };
337 
338  } // namespace Fem
339 
340 } // namespace Dune
341 
342 #endif // #ifndef DUNE_FEM_OPERATOR_COMMON_LOCALCONTRIBUTION_HH
Definition: bindguard.hh:11
std::tuple_element< i, Tuple >::type & get(Dune::TypeIndexedTuple< Tuple, Types > &tuple)
Definition: typeindexedtuple.hh:122
void axpy(const T &a, const T &x, T &y)
Definition: space/basisfunctionset/functor.hh:38
Definition: common/localcontribution.hh:14
Definition: common/localcontribution.hh:28
Definition: common/localcontribution.hh:31
Definition: common/localcontribution.hh:41
Definition: common/localcontribution.hh:51
Definition: common/localcontribution.hh:61
Definition: common/localcontribution.hh:64
Definition: common/localcontribution.hh:67
Definition: common/localcontribution.hh:70
static void begin(Dune::Fem::AssembledOperator< DF, RF > &op)
Definition: operator/common/localcontribution.hh:38
static void end(Dune::Fem::AssembledOperator< DF, RF > &op)
Definition: operator/common/localcontribution.hh:40
static void end(Dune::Fem::AssembledOperator< DF, RF > &op)
Definition: operator/common/localcontribution.hh:54
static void begin(Dune::Fem::AssembledOperator< DF, RF > &op)
Definition: operator/common/localcontribution.hh:52
void begin(const DomainEntity &domainEntity, const RangeEntity &rangeEntity, const AssembledOperator &op, LocalMatrix &localMatrix) const
Definition: operator/common/localcontribution.hh:97
Global::Add< AssembledOperator > GlobalOperationType
Definition: operator/common/localcontribution.hh:94
void end(const DomainEntity &domainEntity, const RangeEntity &rangeEntity, LocalMatrix &localMatrix, AssembledOperator &op) const
Definition: operator/common/localcontribution.hh:103
AssembledOperator::RangeFieldType value_type
Definition: operator/common/localcontribution.hh:92
AssembledOperator::RangeFieldType value_type
Definition: operator/common/localcontribution.hh:118
void end(const DomainEntity &domainEntity, const RangeEntity &rangeEntity, LocalMatrix &localMatrix, AssembledOperator &op) const
Definition: operator/common/localcontribution.hh:123
Definition: operator/common/localcontribution.hh:139
AssembledOperator::RangeFieldType value_type
Definition: operator/common/localcontribution.hh:140
void end(const DomainEntity &domainEntity, const RangeEntity &rangeEntity, LocalMatrix &localMatrix, AssembledOperator &op) const
Definition: operator/common/localcontribution.hh:159
void begin(const DomainEntity &domainEntity, const RangeEntity &rangeEntity, const AssembledOperator &op, LocalMatrix &localMatrix) const
Definition: operator/common/localcontribution.hh:145
Global::Set< AssembledOperator > GlobalOperationType
Definition: operator/common/localcontribution.hh:142
LocalMatrixEntriesType & localMatrixEntries()
return reference to vector of local matrix entries
Definition: operator/common/localcontribution.hh:330
auto axpy(const Point &x, const Factors &... factors) -> std::enable_if_t< Std::And((IsRangeValue< std::decay_t< decltype(std::declval< Factors & >()[0]) > >::value)...) >
Definition: operator/common/localcontribution.hh:289
AssembledOperatorType::RangeFunctionType::DiscreteFunctionSpaceType RangeSpaceType
Definition: operator/common/localcontribution.hh:204
SubVector< LocalMatrixEntriesType, ColIndexMapper > column(SizeType j)
Definition: operator/common/localcontribution.hh:275
RowReferenceVector< const value_type > const_row_reference
Definition: operator/common/localcontribution.hh:223
int order() const
obtain the order of this local contribution
Definition: operator/common/localcontribution.hh:311
void bind(const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity)
initialize the local matrix to entities
Definition: operator/common/localcontribution.hh:314
DomainSpaceType::BasisFunctionSetType DomainBasisFunctionSetType
Definition: operator/common/localcontribution.hh:211
DomainBasisFunctionSetType::EntityType DomainEntityType
Definition: operator/common/localcontribution.hh:214
AssemblyOperation< AssembledOperator > AssemblyOperationType
Definition: operator/common/localcontribution.hh:201
AssembledOperatorType::DomainFunctionType::DiscreteFunctionSpaceType DomainSpaceType
Definition: operator/common/localcontribution.hh:203
RangeSpaceType::BasisFunctionSetType RangeBasisFunctionSetType
Definition: operator/common/localcontribution.hh:212
SubVector< const LocalMatrixEntriesType, ColIndexMapper > column(SizeType j) const
Definition: operator/common/localcontribution.hh:280
LocalContribution(AssembledOperator &assembledOperator, Args &&... args)
Definition: operator/common/localcontribution.hh:256
const LocalMatrixEntriesType & localMatrixEntries() const
return const reference to vector of local matrix entries
Definition: operator/common/localcontribution.hh:328
RangeFunction::RangeFieldType RangeFieldType
field type of the operator's range
Definition: operator.hh:43
abstract matrix operator
Definition: operator.hh:124
virtual void flushAssembly()
commit intermediate states of linear operator assembly
Definition: operator.hh:127
A local matrix with a small array as storage.
Definition: temporarylocalmatrix.hh:100
Definition: rowreferencevector.hh:56
An implementation of DenseVector to extract a portion, not necessarly contiguos, of a vector.
Definition: subvector.hh:161