Dune Core Modules (2.9.0)

basismatrix.hh
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>
10
11#include <dune/localfunctions/utility/lfematrix.hh>
12#include <dune/localfunctions/utility/monomialbasis.hh>
13#include <dune/localfunctions/utility/polynomialbasis.hh>
14
15namespace 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 {
33 typedef LFEMatrix<Field> Matrix;
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 {
64 typedef const MonomialBasis< geometryId, F > PreBasis;
65 typedef BasisMatrixBase<PreBasis,Interpolation,Field> Base;
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 {
89 typedef const VirtualMonomialBasis< dim, F > PreBasis;
90 typedef BasisMatrixBase<PreBasis,Interpolation,Field> Base;
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 {
114 typedef const PolynomialBasis<Eval,CM,D,R> PreBasis;
115 typedef BasisMatrixBase<PreBasis,Interpolation,Field> Base;
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 {
148 typedef const PolynomialBasisWithMatrix<Eval,CM> PreBasis;
149 typedef BasisMatrixBase<PreBasis,Interpolation,Field> Base;
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
IdType Id
An integral id representing a GeometryType.
Definition: type.hh:192
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
void field_cast(const F1 &f1, F2 &f2)
a helper class to cast from one field to another
Definition: field.hh:159
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)