dune-fem  2.4.1-rc
densematrix.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_DENSEMATRIX_HH
2 #define DUNE_FEM_DENSEMATRIX_HH
3 
10 
11 namespace Dune
12 {
13 
14  namespace Fem
15  {
16 
17  // DenseRowMatrix
18  // --------------
19 
20  template< class F >
22  {
24 
25  public:
26  typedef F Field;
27 
28  template< class RF >
29  class Row;
30 
32  : rows_( 0 ),
33  cols_( 0 ),
34  fields_( 0 )
35  {}
36 
37  DenseRowMatrix ( unsigned int rows, unsigned int cols )
38  : rows_( 0 ),
39  cols_( 0 ),
40  fields_( 0 )
41  {
42  reserve( rows, cols );
43  }
44 
46  {
47  delete[] fields_;
48  }
49 
50  unsigned int rows () const
51  {
52  return rows_;
53  }
54 
55  unsigned int cols () const
56  {
57  return cols_;
58  }
59 
60  const Field &operator() ( const unsigned int row, const unsigned int col ) const
61  {
62  assert( (row < rows()) && (col < cols()) );
63  return fields_[ row*cols() + col ];
64  }
65 
66  Field &operator() ( const unsigned int row, const unsigned int col )
67  {
68  assert( (row < rows()) && (col < cols()) );
69  return fields_[ row*cols() + col ];
70  }
71 
72  void add(const unsigned int row, const unsigned int col, const Field& value )
73  {
74  assert( (row < rows()) && (col < cols()) );
75  fields_[ row*cols() + col ] += value;
76  }
77 
78  Row< const Field > operator[] ( const unsigned int row ) const
79  {
80  assert( row < rows() );
81  return Row< const Field >( cols(), fields_ + row*cols() );
82  }
83 
84  Row< Field > operator[] ( const unsigned int row )
85  {
86  assert( row < rows() );
87  return Row< Field >( cols(), fields_ + row*cols() );
88  }
89 
90  void clear ()
91  {
92  Field *end = fields_ + (rows_*cols_);
93  for( Field *it = fields_; it != end; ++it )
94  *it = Field( 0 );
95  }
96 
97  void multiply ( const Field *x, Field *y ) const
98  {
99  for( unsigned int row = 0; row < rows(); ++row )
100  {
101  const Field *fields = fields_ + row*cols();
102  y[ row ] = Field( 0 );
103  for( unsigned int col = 0; col < cols(); ++col )
104  y[ row ] += fields[ col ] * x[ col ];
105  }
106  }
107 
108  void reserve ( unsigned int rows, unsigned int cols )
109  {
110  if( (rows != rows_) || (cols != cols_) )
111  {
112  delete[] fields_;
113  fields_ = new Field[ rows*cols ];
114  rows_ = rows;
115  cols_ = cols;
116  }
117  // Martin: Is clearing required, here?
118  clear();
119  }
120 
121  void print( std::ostream& s ) const
122  {
123  s.precision( 6 );
124  for( unsigned int row = 0; row < rows(); ++row )
125  {
126  const Field *fields = fields_ + row*cols();
127  for( unsigned int col = 0; col < cols(); ++col )
128  s << fields[ col ] << " ";
129 
130  s << std::endl;
131  }
132  }
133 
134  private:
135  unsigned int rows_;
136  unsigned int cols_;
137  Field *fields_;
138  };
139 
140 
141 
142  // DenseRowMatrix::Row
143  // -------------------
144 
145  template< class F >
146  template< class RF >
147  class DenseRowMatrix< F >::Row
148  {
149  typedef Row< RF > ThisType;
150 
151  template< class > friend class Row;
152 
153  public:
154  Row ( unsigned int cols, RF *fields )
155  : cols_( cols ),
156  fields_( fields )
157  {}
158 
159  Row ( const Row< F > &row )
160  : cols_( row.cols_ ),
161  fields_( row.fields_ )
162  {}
163 
164  const RF &operator[] ( const unsigned int col ) const
165  {
166  assert( col < size() );
167  return fields_[ col ];
168  }
169 
170  RF &operator[] ( const unsigned int col )
171  {
172  assert( col < size() );
173  return fields_[ col ];
174  }
175 
176  void clear ()
177  {
178  Field *const end = fields_ + size();
179  for( Field *it = fields_; it != end; ++it )
180  *it = Field( 0 );
181  }
182 
183  unsigned int size () const
184  {
185  return cols_;
186  }
187 
188  private:
189  unsigned int cols_;
190  RF *fields_;
191  };
192 
193 
194 
195  // DenseRowMatrixObject
196  // --------------------
197 
198  template< class DomainSpace, class RangeSpace >
200  : public OEMMatrix
201  {
203 
204  public:
205  typedef DomainSpace DomainSpaceType;
206  typedef RangeSpace RangeSpaceType;
207 
208  typedef typename RangeSpaceType::RangeFieldType Field;
209 
210  typedef typename DomainSpace::GridType::template Codim< 0 >::Entity ColEntityType;
211  typedef typename RangeSpace::GridType::template Codim< 0 >::Entity RowEntityType;
212 
214 
215  private:
216  class LocalMatrixTraits;
217  class LocalMatrix;
218  class LocalMatrixFactory;
219 
221 
222  public:
224 
225  DenseRowMatrixObject ( const DomainSpaceType &domainSpace,
226  const RangeSpaceType &rangeSpace )
227  : domainSpace_( domainSpace ),
228  rangeSpace_( rangeSpace ),
229  domainSequence_( -1 ),
230  rangeSequence_( -1 ),
231  localMatrixFactory_( *this ),
232  localMatrixStack_( localMatrixFactory_ )
233  {}
234 
235  MatrixType &matrix ()
236  {
237  return matrix_;
238  }
239 
240  LocalMatrixType localMatrix ( const RowEntityType &rowEntity,
241  const ColEntityType &colEntity )
242  {
243  return LocalMatrixType( localMatrixStack_, rowEntity, colEntity );
244  }
245 
246  void clear ()
247  {
248  matrix_.clear();
249  }
250 
251  template< class Stencil >
252  void reserve ( const Stencil &stencil, bool verbose = false )
253  {
254  if( (domainSequence_ != domainSpace().sequence()) || (rangeSequence_ != rangeSpace().sequence()) )
255  {
256  matrix_.reserve( rangeSpace().size(), domainSpace().size() );
257  domainSequence_ = domainSpace().sequence();
258  rangeSequence_ = rangeSpace().sequence();
259  }
260  }
261 
262  template< class DomainFunction, class RangeFunction >
263  void apply ( const DomainFunction &u, RangeFunction &w ) const
264  {
265  matrix_.multiply( u.leakPointer(), w.leakPointer() );
266  rangeSpace().communicate( w );
267  }
268 
269  Field ddotOEM ( const Field *v, const Field *w ) const
270  {
271  typedef AdaptiveDiscreteFunction< RangeSpaceType > RangeFunction;
272  RangeFunction vFunction( "v (ddotOEM)", rangeSpace(), v );
273  RangeFunction wFunction( "w (ddotOEM)", rangeSpace(), w );
274  return vFunction.scalarProductDofs( wFunction );
275  }
276 
277  void multOEM ( const Field *u, Field *w ) const
278  {
279  matrix_.multiply( u, w );
280 
281  typedef AdaptiveDiscreteFunction< RangeSpaceType > RangeFunction;
282  RangeFunction wFunction( "w (multOEM)", rangeSpace(), w );
283  rangeSpace().communicate( wFunction );
284  }
285 
286  const DomainSpace &domainSpace () const
287  {
288  return domainSpace_;
289  }
290 
291  const RangeSpace &rangeSpace () const
292  {
293  return rangeSpace_;
294  }
295 
296  private:
297  const DomainSpaceType &domainSpace_;
298  const RangeSpaceType &rangeSpace_;
299 
300  int domainSequence_;
301  int rangeSequence_;
302 
303  MatrixType matrix_;
304 
305  LocalMatrixFactory localMatrixFactory_;
306  mutable LocalMatrixStack localMatrixStack_;
307  };
308 
309 
310 
311  // DenseRowMatrixObject::LocalMatrixTraits
312  // ---------------------------------------
313 
314  template< class DomainSpace, class RangeSpace >
315  class DenseRowMatrixObject< DomainSpace, RangeSpace >::LocalMatrixTraits
316  {
318 
319  public:
322 
324  typedef RangeFieldType LittleBlockType;
325 
327  };
328 
329 
330 
331  // DenseRowMatrixObject::LocalMatrix
332  // ---------------------------------
333 
334  template< class DomainSpace, class RangeSpace >
335  class DenseRowMatrixObject< DomainSpace, RangeSpace >::LocalMatrix
336  : public LocalMatrixDefault< LocalMatrixTraits >
337  {
339 
340  typedef LocalMatrix ThisType;
342 
343  public:
345 
347 
348  typedef typename Traits::RangeFieldType RangeFieldType;
349  typedef typename Traits::LittleBlockType LittleBlockType;
350  typedef RangeFieldType DofType;
351 
352  LocalMatrix ( MatrixType &matrix,
353  const DomainSpaceType &domainSpace,
354  const RangeSpaceType &rangeSpace )
355  : BaseType( domainSpace, rangeSpace ),
356  matrix_( matrix )
357  {}
358 
359  private:
360  LocalMatrix ( const ThisType & );
361  ThisType &operator= ( const ThisType & );
362 
363  public:
364  void init ( const RowEntityType &rowEntity, const ColEntityType &colEntity )
365  {
366  BaseType::init( rowEntity, colEntity );
367 
368  map( rangeSpace().mapper(), rowEntity, rowIndices_ );
369  map( domainSpace().mapper(), colEntity, colIndices_ );
370  }
371 
372  int rows () const
373  {
374  return rowIndices_.size();
375  }
376 
377  int cols () const
378  {
379  return colIndices_.size();
380  }
381 
382  void add ( const int row, const int col, const DofType &value )
383  {
384  assert( (row >= 0) && (row < rows()) );
385  assert( (col >= 0) && (col < cols()) );
386  matrix_( rowIndices_[ row ], colIndices_[ col ] ) += value;
387  }
388 
389  const DofType &get ( const int row, const int col ) const
390  {
391  assert( (row >= 0) && (row < rows()) );
392  assert( (col >= 0) && (col < cols()) );
393  return matrix_( rowIndices_[ row ], colIndices_[ col ] );
394  }
395 
396  void set ( const int row, const int col, const DofType &value )
397  {
398  assert( (row >= 0) && (row < rows()) );
399  assert( (col >= 0) && (col < cols()) );
400  matrix_( rowIndices_[ row ], colIndices_[ col ] ) = value;
401  }
402 
403  void clearRow ( const int row )
404  {
405  assert( (row >= 0) && (row < rows()) );
406  const unsigned int rowIndex = rowIndices_[ row ];
407  matrix_[ rowIndex ].clear();
408  }
409 
410  void unitRow ( const int row )
411  {
412  clearRow( row );
413  set( row, row, DofType( 1 ) );
414  }
415 
416  void clear ()
417  {
418  typedef std::vector< unsigned int >::const_iterator Iterator;
419  const Iterator rowEnd = rowIndices_.end();
420  for( Iterator rowIt = rowIndices_.begin(); rowIt != rowEnd; ++rowIt )
421  {
422  const Iterator colEnd = colIndices_.end();
423  for( Iterator colIt = colIndices_.begin(); colIt != colEnd; ++colIt )
424  matrix_( *rowIt, *colIt ) = DofType( 0 );
425  }
426  }
427 
428  void scale ( const DofType &value )
429  {
430  typedef std::vector< unsigned int >::const_iterator Iterator;
431  const Iterator rowEnd = rowIndices_.end();
432  for( Iterator rowIt = rowIndices_.begin(); rowIt != rowEnd; ++rowIt )
433  {
434  const Iterator colEnd = colIndices_.end();
435  for( Iterator colIt = colIndices_.begin(); colIt != colEnd; ++colIt )
436  matrix_( *rowIt, *colIt ) *= value;
437  }
438  }
439 
440  private:
441  template< class Mapper, class Entity >
442  void map ( const Mapper &mapper, const Entity &entity, std::vector< unsigned int > &indices )
443  {
444  indices.resize( mapper.numDofs( entity ) );
445  mapper.mapEach( entity, Fem::AssignFunctor< std::vector< unsigned int > >( indices ) );
446  }
447 
448  protected:
449  using BaseType::domainSpace_;
450  using BaseType::rangeSpace_;
451 
452  private:
453  MatrixType &matrix_;
454 
455  std::vector< unsigned int > rowIndices_;
456  std::vector< unsigned int > colIndices_;
457  };
458 
459 
460 
461  // DenseRowMatrixObject::LocalMatrixFactory
462  // ----------------------------------------
463 
464  template< class DomainSpace, class RangeSpace >
465  class DenseRowMatrixObject< DomainSpace, RangeSpace >::LocalMatrixFactory
466  {
468 
469  public:
471 
472  LocalMatrixFactory ( MatrixObject &matrixObject )
473  : matrixObject_( &matrixObject )
474  {}
475 
476  ObjectType *newObject () const
477  {
478  return new ObjectType( matrixObject_->matrix_, matrixObject_->domainSpace_, matrixObject_->rangeSpace_ );
479  }
480 
481  private:
482  MatrixObject *matrixObject_;
483  };
484 
485  } // namespace Fem
486 
487 } // namespace Dune
488 
489 #endif // #ifndef DUNE_FEM_DENSEMATRIX_HH
Traits::DomainSpaceType DomainSpaceType
type of domain discrete function space
Definition: localmatrix.hh:51
DenseRowMatrix< Field > MatrixType
Definition: densematrix.hh:213
RangeSpace RangeSpaceType
Definition: densematrix.hh:206
const DomainSpace & domainSpace() const
Definition: densematrix.hh:286
MatrixObject::LocalMatrix LocalMatrixType
Definition: densematrix.hh:326
Definition: localmatrixwrapper.hh:16
LocalMatrix(MatrixType &matrix, const DomainSpaceType &domainSpace, const RangeSpaceType &rangeSpace)
Definition: densematrix.hh:352
Row(const Row< F > &row)
Definition: densematrix.hh:159
ObjectType * newObject() const
Definition: densematrix.hh:476
Definition: densematrix.hh:21
LocalMatrix ObjectType
Definition: densematrix.hh:470
DenseRowMatrix(unsigned int rows, unsigned int cols)
Definition: densematrix.hh:37
RangeFieldType DofType
Definition: densematrix.hh:350
LocalMatrixWrapper< LocalMatrixStack > LocalMatrixType
Definition: densematrix.hh:223
default implementation for a general operator stencil
Definition: stencil.hh:31
Traits::LittleBlockType LittleBlockType
Definition: densematrix.hh:349
RangeSpace::GridType::template Codim< 0 >::Entity RowEntityType
Definition: densematrix.hh:211
Traits::RangeFieldType RangeFieldType
Definition: densematrix.hh:348
Definition: adaptivefunction/adaptivefunction.hh:34
void add(const int row, const int col, const DofType &value)
Definition: densematrix.hh:382
void clear()
Definition: densematrix.hh:90
MatrixObject::MatrixType MatrixType
Definition: densematrix.hh:346
Default implementation for local matrix classes.
Definition: localmatrix.hh:266
const RangeSpace & rangeSpace() const
Definition: densematrix.hh:291
void apply(const DomainFunction &u, RangeFunction &w) const
Definition: densematrix.hh:263
void print(std::ostream &s) const
Definition: densematrix.hh:121
Interface for local matrix classes.
Definition: localmatrix.hh:28
void clearRow(const int row)
Definition: densematrix.hh:403
Definition: misc/functor.hh:30
LocalMatrixFactory(MatrixObject &matrixObject)
Definition: densematrix.hh:472
int rows() const
Definition: densematrix.hh:372
~DenseRowMatrix()
Definition: densematrix.hh:45
MatrixObject::RangeSpaceType RangeSpaceType
Definition: densematrix.hh:321
void init(const RowEntityType &rowEntity, const ColEntityType &colEntity)
Definition: densematrix.hh:364
LocalMatrixTraits Traits
Definition: densematrix.hh:344
F Field
Definition: densematrix.hh:26
RangeSpaceType::RangeFieldType Field
Definition: densematrix.hh:208
const Field & operator()(const unsigned int row, const unsigned int col) const
Definition: densematrix.hh:60
void reserve(const Stencil &stencil, bool verbose=false)
Definition: densematrix.hh:252
void clear()
Definition: densematrix.hh:416
void reserve(unsigned int rows, unsigned int cols)
Definition: densematrix.hh:108
Definition: coordinate.hh:4
Definition: densematrix.hh:335
unsigned int cols() const
Definition: densematrix.hh:55
Definition: densematrix.hh:29
RangeFieldType LittleBlockType
Definition: densematrix.hh:324
LocalMatrixType localMatrix(const RowEntityType &rowEntity, const ColEntityType &colEntity)
Definition: densematrix.hh:240
DomainSpace DomainSpaceType
Definition: densematrix.hh:205
Row(unsigned int cols, RF *fields)
Definition: densematrix.hh:154
Traits::RangeSpaceType RangeSpaceType
type of range discrete function space
Definition: localmatrix.hh:54
MatrixObject::Field RangeFieldType
Definition: densematrix.hh:323
MatrixObject::DomainSpaceType DomainSpaceType
Definition: densematrix.hh:320
unsigned int size() const
Definition: densematrix.hh:183
interface for matrices to be used with OEM sovlers
Definition: oemsolver/oemsolver.hh:31
void scale(const DofType &value)
Definition: densematrix.hh:428
Row< const Field > operator[](const unsigned int row) const
Definition: densematrix.hh:78
DomainSpace::GridType::template Codim< 0 >::Entity ColEntityType
Definition: densematrix.hh:210
Definition: densematrix.hh:199
Field ddotOEM(const Field *v, const Field *w) const
Definition: densematrix.hh:269
DenseRowMatrix()
Definition: densematrix.hh:31
MatrixType & matrix()
Definition: densematrix.hh:235
void multiply(const Field *x, Field *y) const
Definition: densematrix.hh:97
void unitRow(const int row)
Definition: densematrix.hh:410
void add(const unsigned int row, const unsigned int col, const Field &value)
Definition: densematrix.hh:72
void clear()
Definition: densematrix.hh:246
DenseRowMatrixObject(const DomainSpaceType &domainSpace, const RangeSpaceType &rangeSpace)
Definition: densematrix.hh:225
void multOEM(const Field *u, Field *w) const
Definition: densematrix.hh:277
void clear()
Definition: densematrix.hh:176
Definition: bartonnackmaninterface.hh:15
unsigned int rows() const
Definition: densematrix.hh:50
int cols() const
Definition: densematrix.hh:377