DUNE-FEM (unstable)

fempreconditioning.hh
1#ifndef DUNE_FEM_FEMPRECONDITIONING_HH
2#define DUNE_FEM_FEMPRECONDITIONING_HH
3
4#include <type_traits>
5
6#include <dune/fem/function/common/discretefunction.hh>
7#include <dune/fem/operator/common/operator.hh>
8#include <dune/fem/solver/parameter.hh>
9
10#if HAVE_DUNE_ISTL
11#include <dune/istl/bvector.hh>
12#endif
13
14namespace Dune
15{
16
17 namespace Fem
18 {
19
20 // FemPreconditioningBase (default non-assembled)
21 // --------------------------------------------------
22 template< class DFImp, class OperatorImp, int method, bool assembled >
23 class FemPreconditioningBase
24 : public Operator< DFImp, DFImp >
25 {
26 public:
27 typedef DFImp DiscreteFunctionType;
28 typedef OperatorImp OperatorType;
29
30 typedef typename DiscreteFunctionType :: DofIteratorType DofIteratorType;
31 typedef typename DiscreteFunctionType :: ConstDofIteratorType ConstDofIteratorType;
32
33 public:
34 FemPreconditioningBase(const OperatorType &op) {}
35
36 virtual void operator()(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
37 {
38 DUNE_THROW(NotImplemented,"preconditioning not possible for non-assembled operators");
39 }
40
41#if HAVE_DUNE_ISTL
43 template < class YBlock, class XBlock >
44 void applyToISTLBlockVector( const BlockVector< YBlock >& d,
45 BlockVector< XBlock >& v ) const
46 {
47 DUNE_THROW(NotImplemented,"preconditioning not possible for non-assembled operators");
48 }
49#endif
50
51 protected:
52 void apply( const DiscreteFunctionType& u, DiscreteFunctionType& res ) const
53 {
54 DUNE_THROW(NotImplemented,"preconditioning not possible for non-assembled operators");
55 }
56 };
57
58
59 // FemPreconditioningBase (assembled version)
60 // ----------------------------------------------
61 template< class DFImp, class OperatorImp, int method >
62 class FemPreconditioningBase< DFImp, OperatorImp, method, true >
63 : public Operator< DFImp, DFImp >
64 {
65 public:
66 typedef DFImp DiscreteFunctionType;
67 typedef OperatorImp OperatorType;
68
69 typedef typename DiscreteFunctionType :: DofType DofType;
70 typedef typename Dune::FieldTraits< DofType >::real_type RealType;
71 typedef typename DiscreteFunctionType :: DofIteratorType DofIteratorType;
72 typedef typename DiscreteFunctionType :: ConstDofIteratorType ConstDofIteratorType;
73
74 protected:
75 typedef typename OperatorType::MatrixType MatrixType;
76
77 DiscreteFunctionType diagonalInv_;
78
79 const MatrixType& matrix_;
80
81 const int n_;
82 const double w_;
83
84 public:
85 FemPreconditioningBase( const OperatorType& assembledOperator,
86 const int n = 1,
87 const double relax = 1.0 )
88 : diagonalInv_( "diagInv", assembledOperator.rangeSpace()),
89 matrix_( assembledOperator.exportMatrix() ),
90 n_( n ),
91 w_( (method == SolverParameter::gauss_seidel ) ? 1.0 : relax )
92 {
93 // extract diagonal elements form matrix object
94 assembledOperator.extractDiagonal( diagonalInv_ );
95
96 // make consistent at border dofs
97 diagonalInv_.communicate();
98
99 // In general: store 1/diag
100 //
101 // note: We set near-zero entries to 1 to avoid NaNs. Such entries occur
102 // if DoFs are excluded from matrix setup
103 const RealType eps = 16.*std::numeric_limits< RealType >::epsilon();
104 const DofIteratorType dend = diagonalInv_.dend();
105 for( DofIteratorType dit = diagonalInv_.dbegin(); dit != dend; ++dit )
106 *dit = (std::abs( *dit ) < eps ? DofType( 1. ) : DofType( 1. ) / *dit);
107 }
108
109 virtual void operator()(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
110 {
111 apply(u, res);
112 }
113
114#if HAVE_DUNE_ISTL
116 template < class YBlock, class XBlock >
117 void applyToISTLBlockVector( const BlockVector< YBlock >& d,
118 BlockVector< XBlock >& v ) const
119 {
120 DiscreteFunctionType vTmp("fem-precon::X", diagonalInv_.space(), v );
121 DiscreteFunctionType dTmp("fem-precon::Y", diagonalInv_.space(), d );
122
123 // apply stationary iterative preconditioning
124 apply( dTmp, vTmp );
125 }
126#endif
127
128 protected:
129 void apply( const DiscreteFunctionType& u, DiscreteFunctionType& v ) const
130 {
131 v.clear();
132
133 std::unique_ptr< DiscreteFunctionType > xTmp_;
134 if constexpr ( method == SolverParameter :: jacobi )
135 {
136 xTmp_.reset( new DiscreteFunctionType( v ) );
137 }
138
139 DiscreteFunctionType& x = ( method == SolverParameter :: jacobi ) ? *xTmp_ : v ;
140
141 for( int i=0; i<n_; ++i )
142 {
143 matrix_.forwardIterative( diagonalInv_, u, x, v, w_ );
144
145 if constexpr ( method == SolverParameter :: ssor )
146 {
147 matrix_.backwardIterative( diagonalInv_, u, x, v, w_ );
148 }
149
150 // synchronize data
151 v.communicate();
152
153 if constexpr ( method == SolverParameter :: jacobi )
154 {
155 // only needed for the intermediate steps
156 if( i < n_-1 )
157 x.assign( v );
158 }
159 }
160 }
161 };
162
163
164 // FemPreconditioning
165 // ----------------------
176 template< class DFImp, class Operator, int method>
178 : public FemPreconditioningBase< DFImp, Operator, method, std::is_base_of< AssembledOperator< DFImp, DFImp >, Operator > :: value >
179 {
180 typedef FemPreconditioningBase< DFImp, Operator, method, std::is_base_of< AssembledOperator< DFImp, DFImp >, Operator > :: value >
181 BaseType;
182 public:
183 typedef Operator OperatorType;
184 FemPreconditioning(const OperatorType &op, const int n = 1, const double w = 1.0)
185 : BaseType( op )
186 {}
187 };
188
189 template <class DFImp, class Operator>
191
192 template <class DFImp, class Operator>
194
195 template <class DFImp, class Operator>
197
198 template <class DFImp, class Operator>
200
201 } // namespace Fem
202
203} // namespace Dune
204
205#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...
void communicate()
do default communication of space for this discrete function
Definition: discretefunction.hh:825
void clear()
set all degrees of freedom to zero
Definition: discretefunction.hh:731
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
void assign(const DiscreteFunctionInterface< DFType > &g)
Definition: discretefunction_inline.hh:132
Precondtioner, implementing Jacobi, Gauss-Seidel and SOR works with.
Definition: fempreconditioning.hh:179
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)