dune-fem  2.4.1-rc
localmatrix.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_LOCALMATRIX_HH
2 #define DUNE_FEM_LOCALMATRIX_HH
3 
4 //- Dune includes
6 
7 namespace Dune
8 {
9 
10  namespace Fem
11  {
12 
17  //- forward declaration of MatrixColumnObject, see below
18  template <class Traits>
20 
21 
22 
23  // LocalMatrixInterface
24  // --------------------
25 
27  template< class LocalMatrixTraits >
29  : public BartonNackmanInterface< LocalMatrixInterface< LocalMatrixTraits >,
30  typename LocalMatrixTraits::LocalMatrixType >
31  {
34  typename LocalMatrixTraits::LocalMatrixType >
35  BaseType;
36 
37  public:
39  typedef LocalMatrixTraits Traits;
40 
42  typedef ThisType LocalMatrixInterfaceType;
43 
45  typedef typename Traits :: LocalMatrixType LocalMatrixType;
46 
48  typedef typename Traits :: RangeFieldType RangeFieldType;
49 
51  typedef typename Traits :: DomainSpaceType DomainSpaceType;
52 
54  typedef typename Traits :: RangeSpaceType RangeSpaceType;
55 
57  typedef typename DomainSpaceType :: BasisFunctionSetType
59 
61  typedef typename RangeSpaceType :: BasisFunctionSetType
63 
64  typedef typename DomainSpaceType::EntityType DomainEntityType;
65  typedef typename RangeSpaceType::EntityType RangeEntityType;
66 
68  typedef typename Traits :: LittleBlockType LittleBlockType;
69 
71 
72  protected:
73  using BaseType::asImp;
74 
77  {}
78 
79  public:
84  void init ( const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity )
85  {
86  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION
87  ( asImp().init( domainEntity, rangeEntity ) );
88  }
89 
96  void add ( const int localRow,
97  const int localCol,
98  const RangeFieldType &value )
99  {
100  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(
101  asImp().add(localRow,localCol,value));
102  }
103 
110  void set ( const int localRow,
111  const int localCol,
112  const RangeFieldType &value )
113  {
114  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(
115  asImp().set(localRow,localCol,value));
116  }
117 
121  void clearRow( const int localRow )
122  {
123  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(
124  asImp().clearRow( localRow ));
125  }
126 
131  void clearCol( const int localCol )
132  {
133  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(
134  asImp().clearCol( localCol ));
135  }
136 
137 
143  template <class DomainLocalFunctionType,
144  class RangeLocalFunctionType>
145  void multiplyAdd(const DomainLocalFunctionType& lhs,
146  RangeLocalFunctionType& rhs) const
147  {
148  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(
149  asImp().multiplyAdd( lhs, rhs ) );
150  }
151 
158  const RangeFieldType get ( const int localRow,
159  const int localCol ) const
160  {
161  CHECK_INTERFACE_IMPLEMENTATION(
162  asImp().get(localRow,localCol));
163  return asImp().get(localRow,localCol);
164  }
165 
169  void scale ( const RangeFieldType& scalar )
170  {
171  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(
172  asImp().scale( scalar ) );
173  }
174 
176  void clear ()
177  {
178  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(asImp().clear());
179  }
180 
182  void resort ()
183  {
184  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(asImp().resort());
185  }
186 
188  int rows () const
189  {
190  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().rows() );
191  return asImp().rows();
192  }
193 
195  int columns () const
196  {
197  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION( asImp().columns() );
198  return asImp().columns();
199  }
200 
202  const DomainSpaceType &domainSpace () const
203  {
204  CHECK_INTERFACE_IMPLEMENTATION( asImp().domainSpace() );
205  return asImp().domainSpace();
206  }
207 
209  const RangeSpaceType &rangeSpace () const
210  {
211  CHECK_INTERFACE_IMPLEMENTATION( asImp().rangeSpace() );
212  return asImp().rangeSpace();
213  }
214 
217  {
218  CHECK_INTERFACE_IMPLEMENTATION( asImp().domainBasisFunctionSet() );
219  return asImp().domainBasisFunctionSet();
220  }
221 
224  {
225  CHECK_INTERFACE_IMPLEMENTATION( asImp().rangeBasisFunctionSet() );
226  return asImp().rangeBasisFunctionSet();
227  }
228 
229  const DomainEntityType &domainEntity () const
230  {
231  CHECK_INTERFACE_IMPLEMENTATION( asImp().domainEntity() );
232  return asImp().domainEntity();
233  }
234 
235  const RangeEntityType &rangeEntity () const
236  {
237  CHECK_INTERFACE_IMPLEMENTATION( asImp().rangeEntity() );
238  return asImp().rangeEntity();
239  }
240 
247  MatrixColumnType column( const unsigned int col )
248  {
249  return MatrixColumnType( asImp(), col );
250  }
251 
253  void finalize()
254  {
255  CHECK_AND_CALL_INTERFACE_IMPLEMENTATION(asImp().finalize());
256  }
257  };
258 
259 
260 
261  // LocalMatrixDefault
262  // ------------------
263 
265  template< class LocalMatrixTraits >
267  : public LocalMatrixInterface< LocalMatrixTraits >
268  {
271 
272  public:
273  typedef LocalMatrixTraits Traits;
274 
275  typedef typename BaseType::DomainSpaceType DomainSpaceType;
276  typedef typename BaseType::RangeSpaceType RangeSpaceType;
277 
278  typedef typename BaseType::DomainBasisFunctionSetType DomainBasisFunctionSetType;
279  typedef typename BaseType::RangeBasisFunctionSetType RangeBasisFunctionSetType;
280 
281  typedef typename BaseType::DomainEntityType DomainEntityType;
282  typedef typename BaseType::RangeEntityType RangeEntityType;
283 
284  protected:
285  const DomainSpaceType &domainSpace_;
286  const RangeSpaceType &rangeSpace_;
287 
288  DomainBasisFunctionSetType domainBaseSet_;
289  RangeBasisFunctionSetType rangeBaseSet_;
290 
291  protected:
292  LocalMatrixDefault ( const DomainSpaceType &domainSpace,
293  const RangeSpaceType &rangeSpace )
294  : domainSpace_( domainSpace ),
295  rangeSpace_( rangeSpace ),
296  domainBaseSet_(),
297  rangeBaseSet_()
298  {}
299 
300  template< class DomainEntityType, class RangeEntityType >
301  LocalMatrixDefault ( const DomainSpaceType &domainSpace,
302  const RangeSpaceType &rangeSpace,
303  const DomainEntityType &domainEntity,
304  const RangeEntityType &rangeEntity )
305  : domainSpace_( domainSpace ),
306  rangeSpace_( rangeSpace ),
307  domainBaseSet_( domainSpace.basisFunctionSet( domainEntity ) ),
308  rangeBaseSet_( rangeSpace.basisFunctionSet( rangeEntity ) )
309  {}
310 
312  : domainSpace_( org.domainSpace_ ),
313  rangeSpace_( org.rangeSpace_ ),
314  domainBaseSet_( org.domainBaseSet_ ),
315  rangeBaseSet_( org.rangeBaseSet_ )
316  {}
317 
318  public:
320  void init ( const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity )
321  {
322  domainBaseSet_ = domainSpace_.basisFunctionSet( domainEntity );
323  rangeBaseSet_ = rangeSpace_.basisFunctionSet( rangeEntity );
324  }
325 
327  void resort () {}
328 
330  void finalize () {}
331 
333  int rows () const { return rangeBaseSet_.size(); }
334 
336  int columns () const { return domainBaseSet_.size(); }
337 
339  const DomainSpaceType &domainSpace () const { return domainSpace_; }
340 
342  const RangeSpaceType &rangeSpace () const { return rangeSpace_; }
343 
345  const DomainBasisFunctionSetType &domainBasisFunctionSet () const
346  {
347  return domainBaseSet_;
348  }
349 
351  const RangeBasisFunctionSetType &rangeBasisFunctionSet () const
352  {
353  return rangeBaseSet_;
354  }
355 
356  const DomainEntityType &domainEntity () const { return domainBasisFunctionSet().entity(); }
357  const RangeEntityType &rangeEntity () const { return rangeBasisFunctionSet().entity(); }
358 
360  template <class DomainLocalFunctionType,
361  class RangeLocalFunctionType>
362  void multiplyAdd(const DomainLocalFunctionType& lhs,
363  RangeLocalFunctionType& rhs) const
364  {
365  const int row = this->rows();
366  const int col = this->columns();
367  for(int i=0; i<row; ++i)
368  {
369  for(int j=0; j<col; ++j)
370  {
371  rhs[i] += this->get(i,j) * lhs[j];
372  }
373  }
374  }
375 
377  void clearRow( const int localRow )
378  {
379  const int col = this->columns();
380  for(int j = 0; j < col; ++j)
381  {
382  this->set(localRow, j, 0);
383  }
384  }
385 
387  void clearCol( const int localCol )
388  {
389  const int row = this->rows();
390  for(int i = 0; i < row; ++i)
391  {
392  this->set(i, localCol, 0);
393  }
394  }
395  };
396 
397  template <class Traits>
398  class MatrixColumnObject
399  {
400  public:
402  typedef typename Traits :: LocalMatrixType LocalMatrixType;
403 
405  typedef typename Traits :: RangeSpaceType RangeSpaceType;
406 
408  typedef typename RangeSpaceType :: RangeType RangeType ;
410  typedef typename RangeSpaceType :: JacobianRangeType JacobianRangeType ;
412  typedef typename RangeSpaceType :: RangeFieldType RangeFieldType ;
413 
414  protected:
415  // reference to local matrix
416  LocalMatrixType& localMatrix_;
417  // local column number
418  const unsigned int column_;
419 
421  MatrixColumnObject( LocalMatrixType& localMatrix, const unsigned int col )
422  : localMatrix_( localMatrix ),
423  column_( col )
424  {
425  }
426 
427  // at the moment only allow LocalMatrixInterface to construct this object
428  friend class LocalMatrixInterface< Traits >;
429 
430  public:
431 
444  template <class RangeVectorType>
445  void axpy( const RangeVectorType& phi,
446  const RangeType& factor,
447  const RangeFieldType& weight = RangeFieldType(1) )
448  {
449  const unsigned int numBasisFunctions = localMatrix_.rows();
450  assert( phi.size() >= numBasisFunctions );
451  for( unsigned int row = 0; row < numBasisFunctions; ++ row )
452  {
453  RangeFieldType value = factor * phi[ row ];
454  localMatrix_.add( row, column_, weight * value );
455  }
456  }
457 
470  template <class JacobianVectorType>
471  void axpy( const JacobianVectorType& dphi,
472  const JacobianRangeType& jacobianFactor,
473  const RangeFieldType& weight = RangeFieldType(1) )
474  {
475  const unsigned int numBasisFunctions = localMatrix_.rows();
476  assert( dphi.size() >= numBasisFunctions );
477  for( unsigned int row = 0; row < numBasisFunctions; ++ row )
478  {
479  RangeFieldType value = 0;
480  for( int k = 0; k < jacobianFactor.rows; ++k )
481  value += jacobianFactor[ k ] * dphi[ row ][ k ];
482 
483  localMatrix_.add( row, column_, weight * value );
484  }
485  }
486 
501  template <class RangeVectorType, class JacobianVectorType>
502  void axpy( const RangeVectorType& phi,
503  const JacobianVectorType& dphi,
504  const RangeType& factor,
505  const JacobianRangeType& jacobianFactor,
506  const RangeFieldType& weight = RangeFieldType(1) )
507  {
508  const unsigned int numBasisFunctions = localMatrix_.rows();
509  assert( phi.size() >= numBasisFunctions );
510  assert( dphi.size() >= numBasisFunctions );
511  for( unsigned int row = 0; row < numBasisFunctions; ++ row )
512  {
513  RangeFieldType value = factor * phi[ row ];
514  for( int k = 0; k < jacobianFactor.rows; ++k )
515  value += jacobianFactor[ k ] * dphi[ row ][ k ];
516 
517  localMatrix_.add( row, column_, weight * value );
518  }
519  }
520  };
521 
523 
524  } // namespace Fem
525 
526 } // namespace Dune
527 
528 #endif // #ifndef DUNE_FEM_LOCALMATRIX_HH
Traits::DomainSpaceType DomainSpaceType
type of domain discrete function space
Definition: localmatrix.hh:51
int rows() const
get number of rows within the matrix
Definition: localmatrix.hh:188
const DomainBasisFunctionSetType & domainBasisFunctionSet() const
access to the base function set within the domain space
Definition: localmatrix.hh:345
void multiplyAdd(const DomainLocalFunctionType &lhs, RangeLocalFunctionType &rhs) const
multiply left hand side with local matrix and add to right hand side rhs += Matrix * lhs ...
Definition: localmatrix.hh:145
Traits::RangeSpaceType RangeSpaceType
type of domain discrete function space
Definition: localmatrix.hh:405
BaseType::DomainEntityType DomainEntityType
Definition: localmatrix.hh:281
LocalMatrixDefault(const DomainSpaceType &domainSpace, const RangeSpaceType &rangeSpace)
Definition: localmatrix.hh:292
void axpy(const RangeVectorType &phi, const JacobianVectorType &dphi, const RangeType &factor, const JacobianRangeType &jacobianFactor, const RangeFieldType &weight=RangeFieldType(1))
axpy operation for local matrices
Definition: localmatrix.hh:502
LocalMatrixInterface()
constructor
Definition: localmatrix.hh:76
RangeSpaceType::EntityType RangeEntityType
Definition: localmatrix.hh:65
Traits::RangeFieldType RangeFieldType
type of range field
Definition: localmatrix.hh:48
MatrixColumnObject(LocalMatrixType &localMatrix, const unsigned int col)
constructor taking local matrix and column number
Definition: localmatrix.hh:421
const DomainSpaceType & domainSpace() const
access to the domain space
Definition: localmatrix.hh:339
void init(const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity)
initialize the local matrix to entities
Definition: localmatrix.hh:320
const RangeEntityType & rangeEntity() const
Definition: localmatrix.hh:357
int columns() const
get number of columns within the matrix
Definition: localmatrix.hh:336
const Implementation & asImp() const
Definition: bartonnackmaninterface.hh:37
void clearCol(const int localCol)
ser column entries to zero
Definition: localmatrix.hh:387
const DomainSpaceType & domainSpace() const
access to the domain space
Definition: localmatrix.hh:202
void clearRow(const int localRow)
set row to zero values
Definition: localmatrix.hh:121
Default implementation for local matrix classes.
Definition: localmatrix.hh:266
const unsigned int column_
Definition: localmatrix.hh:418
Traits::LocalMatrixType LocalMatrixType
type of local matrix implementation
Definition: localmatrix.hh:402
MatrixColumnObject< Traits > MatrixColumnType
Definition: localmatrix.hh:70
DomainSpaceType::BasisFunctionSetType DomainBasisFunctionSetType
type of base function sets within domain function space
Definition: localmatrix.hh:58
ThisType LocalMatrixInterfaceType
type of this interface
Definition: localmatrix.hh:42
Interface for local matrix classes.
Definition: localmatrix.hh:28
const RangeSpaceType & rangeSpace() const
access to the range space
Definition: localmatrix.hh:342
RangeBasisFunctionSetType rangeBaseSet_
Definition: localmatrix.hh:289
LocalMatrixTraits Traits
Definition: localmatrix.hh:273
MatrixColumnType column(const unsigned int col)
return column object for local matrix which contains axpy methods for convenience ...
Definition: localmatrix.hh:247
const DomainEntityType & domainEntity() const
Definition: localmatrix.hh:356
LocalMatrixTraits Traits
type of traits class
Definition: localmatrix.hh:39
RangeSpaceType::BasisFunctionSetType RangeBasisFunctionSetType
type of base function sets within range function space
Definition: localmatrix.hh:62
void resort()
resort ordering in global matrix (if possible)
Definition: localmatrix.hh:327
void axpy(const RangeVectorType &phi, const RangeType &factor, const RangeFieldType &weight=RangeFieldType(1))
axpy operation for local matrices
Definition: localmatrix.hh:445
RangeSpaceType::JacobianRangeType JacobianRangeType
type of jacobian range
Definition: localmatrix.hh:410
Definition: coordinate.hh:4
void resort()
resort ordering in global matrix (if possible)
Definition: localmatrix.hh:182
LocalMatrixType & localMatrix_
Definition: localmatrix.hh:416
void finalize()
finalize local matrix setup and possibly add values to real matrix
Definition: localmatrix.hh:253
BaseType::DomainSpaceType DomainSpaceType
Definition: localmatrix.hh:275
Traits::LittleBlockType LittleBlockType
Definition: localmatrix.hh:68
BaseType::RangeSpaceType RangeSpaceType
Definition: localmatrix.hh:276
Traits::RangeSpaceType RangeSpaceType
type of range discrete function space
Definition: localmatrix.hh:54
RangeSpaceType::RangeFieldType RangeFieldType
type of range field
Definition: localmatrix.hh:412
void scale(const RangeFieldType &scalar)
scale matrix with scalar value
Definition: localmatrix.hh:169
void init(const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity)
initialize the local matrix to entities
Definition: localmatrix.hh:84
LocalMatrixDefault(const LocalMatrixDefault &org)
Definition: localmatrix.hh:311
LocalMatrixDefault(const DomainSpaceType &domainSpace, const RangeSpaceType &rangeSpace, const DomainEntityType &domainEntity, const RangeEntityType &rangeEntity)
Definition: localmatrix.hh:301
int rows() const
get number of rows within the matrix
Definition: localmatrix.hh:333
void multiplyAdd(const DomainLocalFunctionType &lhs, RangeLocalFunctionType &rhs) const
multiply left hand side with local matrix and add to right hand side rhs += Matrix * lhs ...
Definition: localmatrix.hh:362
BaseType::RangeBasisFunctionSetType RangeBasisFunctionSetType
Definition: localmatrix.hh:279
void finalize()
finalize local matrix setup and possibly add values to real matrix
Definition: localmatrix.hh:330
const DomainSpaceType & domainSpace_
Definition: localmatrix.hh:285
Definition: localmatrix.hh:19
int columns() const
get number of columns within the matrix
Definition: localmatrix.hh:195
Traits::LocalMatrixType LocalMatrixType
type of local matrix implementation
Definition: localmatrix.hh:45
const RangeBasisFunctionSetType & rangeBasisFunctionSet() const
access to the base function set within the range space
Definition: localmatrix.hh:223
RangeSpaceType::RangeType RangeType
type of range
Definition: localmatrix.hh:408
DomainBasisFunctionSetType domainBaseSet_
Definition: localmatrix.hh:288
void clearCol(const int localCol)
ser column entries to zero
Definition: localmatrix.hh:131
const RangeSpaceType & rangeSpace_
Definition: localmatrix.hh:286
BaseType::DomainBasisFunctionSetType DomainBasisFunctionSetType
Definition: localmatrix.hh:278
BaseType::RangeEntityType RangeEntityType
Definition: localmatrix.hh:282
const RangeBasisFunctionSetType & rangeBasisFunctionSet() const
access to the base function set within the range space
Definition: localmatrix.hh:351
const RangeEntityType & rangeEntity() const
Definition: localmatrix.hh:235
const RangeSpaceType & rangeSpace() const
access to the range space
Definition: localmatrix.hh:209
void clearRow(const int localRow)
set row to zero values
Definition: localmatrix.hh:377
void axpy(const JacobianVectorType &dphi, const JacobianRangeType &jacobianFactor, const RangeFieldType &weight=RangeFieldType(1))
axpy operation for local matrices
Definition: localmatrix.hh:471
DomainSpaceType::EntityType DomainEntityType
Definition: localmatrix.hh:64
const DomainEntityType & domainEntity() const
Definition: localmatrix.hh:229
void add(const int localRow, const int localCol, const RangeFieldType &value)
add value to matrix entry (row,col) where row and col are local row and local column ...
Definition: localmatrix.hh:96
Definition: bartonnackmaninterface.hh:15
void clear()
set all entries of local matrix to zero
Definition: localmatrix.hh:176
const DomainBasisFunctionSetType & domainBasisFunctionSet() const
access to the base function set within the domain space
Definition: localmatrix.hh:216