DUNE-FEM (unstable)

diagonalpreconditioner.hh
1#ifndef DUNE_FEM_DIAGONALPRECONDITIONER_HH
2#define DUNE_FEM_DIAGONALPRECONDITIONER_HH
3
4#include <type_traits>
5
6#include <dune/fem/function/common/discretefunction.hh>
7#include <dune/fem/operator/common/operator.hh>
8
9#if HAVE_DUNE_ISTL
10#include <dune/istl/bvector.hh>
11#endif
12
13namespace Dune
14{
15
16 namespace Fem
17 {
18
19 // DiagonalPreconditionerBase (default non-assembled)
20 // --------------------------------------------------
21 template< class DFImp, class OperatorImp, bool assembled >
22 class DiagonalPreconditionerBase
23 : public Operator< DFImp, DFImp >
24 {
25 public:
26 typedef DFImp DiscreteFunctionType;
27 typedef OperatorImp OperatorType;
28
29 typedef typename DiscreteFunctionType :: DofIteratorType DofIteratorType;
30 typedef typename DiscreteFunctionType :: ConstDofIteratorType ConstDofIteratorType;
31
32 public:
33 DiagonalPreconditionerBase(const OperatorType &op) {}
34
35 virtual void operator()(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
36 {
37 DUNE_THROW(NotImplemented,"preconditioning not possible for non-assembled operators");
38 }
39
40#if HAVE_DUNE_ISTL
42 template < class YBlock, class XBlock >
43 void applyToISTLBlockVector( const BlockVector< YBlock >& d,
44 BlockVector< XBlock >& v ) const
45 {
46 DUNE_THROW(NotImplemented,"preconditioning not possible for non-assembled operators");
47 }
48#endif
49
50 protected:
51 void apply( const DiscreteFunctionType& u, DiscreteFunctionType& res ) const
52 {
53 DUNE_THROW(NotImplemented,"preconditioning not possible for non-assembled operators");
54 }
55 };
56
57
58 // DiagonalPreconditionerBase (assembled version)
59 // ----------------------------------------------
60 template< class DFImp, class OperatorImp >
61 class DiagonalPreconditionerBase< DFImp, OperatorImp, true >
62 : public Operator< DFImp, DFImp >
63 {
64 public:
65 typedef DFImp DiscreteFunctionType;
66 typedef OperatorImp OperatorType;
67
68 typedef typename DiscreteFunctionType :: DofType DofType;
69 typedef typename Dune::FieldTraits< DofType >::real_type RealType;
70 typedef typename DiscreteFunctionType :: DofIteratorType DofIteratorType;
71 typedef typename DiscreteFunctionType :: ConstDofIteratorType ConstDofIteratorType;
72
73 protected:
74 typedef typename OperatorType::MatrixType MatrixType;
75
76 DiscreteFunctionType diagonalInv_;
77
78 const MatrixType& matrix_;
79
80 public:
81 DiagonalPreconditionerBase( const OperatorType& assembledOperator )
82 : diagonalInv_( "diag-preconditioning", assembledOperator.rangeSpace()),
83 matrix_( assembledOperator.exportMatrix() )
84
85 {
86 // extract diagonal elements form matrix object
87 assembledOperator.extractDiagonal( diagonalInv_ );
88
89 // make consistent at border dofs
90 diagonalInv_.communicate();
91
92 // In general: store 1/diag
93 //
94 // note: We set near-zero entries to 1 to avoid NaNs. Such entries occur
95 // if DoFs are excluded from matrix setup
96 const RealType eps = 16.*std::numeric_limits< RealType >::epsilon();
97 const DofIteratorType dend = diagonalInv_.dend();
98 for( DofIteratorType dit = diagonalInv_.dbegin(); dit != dend; ++dit )
99 *dit = (std::abs( *dit ) < eps ? DofType( 1. ) : DofType( 1. ) / *dit);
100 }
101
102 virtual void operator()(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
103 {
104 apply(u, res);
105 }
106
107#if HAVE_DUNE_ISTL
109 template < class YBlock, class XBlock >
110 void applyToISTLBlockVector( const BlockVector< YBlock >& d,
111 BlockVector< XBlock >& v ) const
112 {
113 DiscreteFunctionType vTmp("diag-precon::X", diagonalInv_.space(), v );
114 DiscreteFunctionType dTmp("diag-precon::Y", diagonalInv_.space(), d );
115
116 // apply 1/diagonal
117 apply( dTmp, vTmp );
118 }
119#endif
120
121
122 protected:
123 void apply( const DiscreteFunctionType& u, DiscreteFunctionType& res ) const
124 {
125 /*
126 matrix_.sor( diagonalInv_, u, res, 1.0 );
127 */
128
129 ConstDofIteratorType uIt = u.dbegin();
130 ConstDofIteratorType diagInv = diagonalInv_.dbegin();
131
132 const DofIteratorType resEnd = res.dend();
133
134 // apply 1/diagonal
135 for(DofIteratorType resIt = res.dbegin();
136 resIt != resEnd; ++ resIt, ++diagInv, ++ uIt )
137 {
138 assert( diagInv != diagonalInv_.dend() );
139 assert( uIt != u.dend() );
140 (*resIt) = (*uIt) * (*diagInv);
141 }
142 }
143
144 };
145
146
147 // DiagonalPreconditioner
148 // ----------------------
159 template< class DFImp, class Operator>
161 : public DiagonalPreconditionerBase< DFImp, Operator, std::is_base_of< AssembledOperator< DFImp, DFImp >, Operator > :: value >
162 {
163 typedef DiagonalPreconditionerBase< DFImp, Operator, std::is_base_of< AssembledOperator< DFImp, DFImp >, Operator > :: value >
164 BaseType;
165 public:
166 typedef Operator OperatorType;
167 DiagonalPreconditioner(const OperatorType &op)
168 : BaseType( op )
169 {}
170 };
171
172 } // namespace Fem
173
174} // namespace Dune
175
176#endif // #ifndef DUNE_FEM_DIAGONALPRECONDITIONER_HH
This file implements a vector space as a tensor product of a given vector space. The number of compon...
Precondtioner, multiplies with inverse of the diagonal works with.
Definition: diagonalpreconditioner.hh:162
void communicate()
do default communication of space for this discrete function
Definition: discretefunction.hh:825
Traits::ConstDofIteratorType ConstDofIteratorType
type of the const dof iterator
Definition: discretefunction.hh:628
ConstDofIteratorType dbegin() const
Obtain the constant iterator pointing to the first dof.
Definition: discretefunction.hh:761
ConstDofIteratorType dend() const
Obtain the constant iterator pointing to the last dof.
Definition: discretefunction.hh:773
Traits::DofIteratorType DofIteratorType
type of the dof iterator
Definition: discretefunction.hh:626
forward declaration
Definition: discretefunction.hh:51
const DiscreteFunctionSpaceType & space() const
obtain a reference to the corresponding DiscreteFunctionSpace
Definition: discretefunction.hh:709
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
abstract operator
Definition: operator.hh:34
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)