dune-localfunctions  2.9.0
basismatrix.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_BASISMATRIX_HH
6 #define DUNE_BASISMATRIX_HH
7 
8 #include <fstream>
9 #include <dune/common/exceptions.hh>
10 
14 
15 namespace Dune
16 {
17  /****************************************
18  * A dense matrix representation of a ''polynomial''
19  * basis. Its represent a basis as a linear
20  * combination of a second basis, i.e., a
21  * monomial basis. It is simular to the PolynomialBasis
22  * but it not derived from the LocalBasis class.
23  * It is used to define a ''pre basis''.
24  ****************************************/
25  template< class PreBasis, class Interpolation,
26  class Field >
27  struct BasisMatrix;
28 
29  template< class PreBasis, class Interpolation,
30  class Field >
31  struct BasisMatrixBase : public LFEMatrix<Field>
32  {
34 
35  BasisMatrixBase( const PreBasis& preBasis,
36  const Interpolation& localInterpolation )
37  : cols_(preBasis.size())
38  {
39  localInterpolation.interpolate( preBasis, *this );
40 
41  if ( !Matrix::invert() )
42  {
43  DUNE_THROW(MathError, "While computing basis a singular matrix was constructed!");
44  }
45  }
46  unsigned int cols () const
47  {
48  return cols_;
49  }
50  unsigned int rows () const
51  {
52  return Matrix::rows();
53  }
54  private:
55  unsigned int cols_;
56  };
57 
58  template< GeometryType::Id geometryId, class F,
59  class Interpolation,
60  class Field >
61  struct BasisMatrix< const MonomialBasis< geometryId, F >, Interpolation, Field >
62  : public BasisMatrixBase< const MonomialBasis< geometryId, F >, Interpolation, Field >
63  {
66  typedef typename Base::Matrix Matrix;
67 
68  BasisMatrix( const PreBasis& preBasis,
69  const Interpolation& localInterpolation )
70  : Base(preBasis, localInterpolation)
71  {}
72  template <class Vector>
73  void row( const unsigned int row, Vector &vec ) const
74  {
75  const unsigned int N = Matrix::rows();
76  assert( Matrix::cols() == N && vec.size() == N );
77  // note: that the transposed matrix is computed,
78  // and is square
79  for (unsigned int i=0; i<N; ++i)
80  field_cast(Matrix::operator()(i,row),vec[i]);
81  }
82  };
83  template< int dim, class F,
84  class Interpolation,
85  class Field >
86  struct BasisMatrix< const Dune::VirtualMonomialBasis< dim, F >, Interpolation, Field >
87  : public BasisMatrixBase< const VirtualMonomialBasis< dim, F >, Interpolation, Field >
88  {
91  typedef typename Base::Matrix Matrix;
92 
93  BasisMatrix( const PreBasis& preBasis,
94  const Interpolation& localInterpolation )
95  : Base(preBasis, localInterpolation)
96  {}
97  template <class Vector>
98  void row( const unsigned int row, Vector &vec ) const
99  {
100  const unsigned int N = Matrix::rows();
101  assert( Matrix::cols() == N && vec.size() == N );
102  // note: that the transposed matrix is computed,
103  // and is square
104  for (unsigned int i=0; i<N; ++i)
105  field_cast(Matrix::operator()(i,row),vec[i]);
106  }
107  };
108  template< class Eval, class CM, class D, class R,
109  class Interpolation,
110  class Field >
111  struct BasisMatrix< const PolynomialBasis<Eval,CM,D,R>, Interpolation, Field >
112  : public BasisMatrixBase< const PolynomialBasis<Eval,CM,D,R>, Interpolation, Field >
113  {
116  typedef typename Base::Matrix Matrix;
117 
118  BasisMatrix( const PreBasis& preBasis,
119  const Interpolation& localInterpolation )
120  : Base(preBasis, localInterpolation),
121  preBasis_(preBasis)
122  {}
123  unsigned int cols() const
124  {
125  return preBasis_.matrix().baseSize() ;
126  }
127  template <class Vector>
128  void row( const unsigned int row, Vector &vec ) const
129  {
130  assert( Matrix::rows() == Matrix::cols() );
131  assert( vec.size() == preBasis_.matrix().baseSize() );
132  assert( Matrix::cols() == preBasis_.size() );
133  for (unsigned int j=0; j<Matrix::cols(); ++j)
134  vec[j] = 0;
135  for (unsigned int i=0; i<Matrix::rows(); ++i)
136  preBasis_.matrix().
137  addRow(i,Base::Matrix::operator()(i,row),vec);
138  }
139  private:
140  const PreBasis& preBasis_;
141  };
142  template< class Eval, class CM,
143  class Interpolation,
144  class Field >
145  struct BasisMatrix< const PolynomialBasisWithMatrix<Eval,CM>, Interpolation, Field >
146  : public BasisMatrixBase< const PolynomialBasisWithMatrix<Eval,CM>, Interpolation, Field >
147  {
150  typedef typename Base::Matrix Matrix;
151 
152  BasisMatrix( const PreBasis& preBasis,
153  const Interpolation& localInterpolation )
154  : Base(preBasis, localInterpolation),
155  preBasis_(preBasis)
156  {}
157  unsigned int cols() const
158  {
159  return preBasis_.matrix().baseSize() ;
160  }
161  unsigned int rows () const
162  {
163  assert( Matrix::rows() == preBasis_.matrix().size() );
164  return preBasis_.matrix().size()*CM::blockSize ;
165  }
166  template <class Vector>
167  void row( const unsigned int row, Vector &vec ) const
168  {
169  unsigned int r = row / CM::blockSize;
170  assert( r < Matrix::rows() );
171  assert( Matrix::rows() == Matrix::cols() );
172  assert( vec.size() == preBasis_.matrix().baseSize() );
173  assert( Matrix::cols() == preBasis_.size() );
174  for (unsigned int j=0; j<vec.size(); ++j)
175  vec[j] = 0;
176  for (unsigned int i=0; i<Matrix::rows(); ++i)
177  preBasis_.matrix().
178  addRow(i*CM::blockSize+row%CM::blockSize,Base::Matrix::operator()(i,r),vec);
179  }
180  private:
181  const PreBasis& preBasis_;
182  };
183 }
184 
185 #endif // DUNE_BASISMATRIX_HH
Definition: bdfmcube.hh:18
void field_cast(const F1 &f1, F2 &f2)
a helper class to cast from one field to another
Definition: field.hh:159
Definition: basismatrix.hh:27
Definition: basismatrix.hh:32
unsigned int cols() const
Definition: basismatrix.hh:46
unsigned int rows() const
Definition: basismatrix.hh:50
BasisMatrixBase(const PreBasis &preBasis, const Interpolation &localInterpolation)
Definition: basismatrix.hh:35
LFEMatrix< Field > Matrix
Definition: basismatrix.hh:33
void row(const unsigned int row, Vector &vec) const
Definition: basismatrix.hh:73
const MonomialBasis< geometryId, F > PreBasis
Definition: basismatrix.hh:64
BasisMatrix(const PreBasis &preBasis, const Interpolation &localInterpolation)
Definition: basismatrix.hh:68
BasisMatrixBase< PreBasis, Interpolation, Field > Base
Definition: basismatrix.hh:65
void row(const unsigned int row, Vector &vec) const
Definition: basismatrix.hh:98
BasisMatrixBase< PreBasis, Interpolation, Field > Base
Definition: basismatrix.hh:90
const VirtualMonomialBasis< dim, F > PreBasis
Definition: basismatrix.hh:89
BasisMatrix(const PreBasis &preBasis, const Interpolation &localInterpolation)
Definition: basismatrix.hh:93
BasisMatrixBase< PreBasis, Interpolation, Field > Base
Definition: basismatrix.hh:115
BasisMatrix(const PreBasis &preBasis, const Interpolation &localInterpolation)
Definition: basismatrix.hh:118
void row(const unsigned int row, Vector &vec) const
Definition: basismatrix.hh:128
const PolynomialBasis< Eval, CM, D, R > PreBasis
Definition: basismatrix.hh:114
const PolynomialBasisWithMatrix< Eval, CM > PreBasis
Definition: basismatrix.hh:148
BasisMatrixBase< PreBasis, Interpolation, Field > Base
Definition: basismatrix.hh:149
BasisMatrix(const PreBasis &preBasis, const Interpolation &localInterpolation)
Definition: basismatrix.hh:152
void row(const unsigned int row, Vector &vec) const
Definition: basismatrix.hh:167
Definition: lfematrix.hh:18
unsigned int rows() const
Definition: lfematrix.hh:58
bool invert()
Definition: lfematrix.hh:89
Field Field
Definition: lfematrix.hh:24
Definition: monomialbasis.hh:440
Definition: monomialbasis.hh:612
Definition: polynomialbasis.hh:65
Definition: polynomialbasis.hh:348