dune-fem 2.12-git
Loading...
Searching...
No Matches
fempreconditioning.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_FEMPRECONDITIONING_HH
2#define DUNE_FEM_FEMPRECONDITIONING_HH
3
4#include <type_traits>
5
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 >
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:
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;
71 typedef typename DiscreteFunctionType :: DofIteratorType DofIteratorType;
72 typedef typename DiscreteFunctionType :: ConstDofIteratorType ConstDofIteratorType;
73
74 protected:
75 typedef typename OperatorType::MatrixType MatrixType;
76
78
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
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:
130 {
131 v.clear();
132
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 {
181 BaseType;
182 public:
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
#define DUNE_THROW(E,...)
Dune::Fem::Double abs(const Dune::Fem::Double &a)
Definition double.hh:942
abstract operator
Definition operator.hh:34
Definition fempreconditioning.hh:25
DFImp DiscreteFunctionType
Definition fempreconditioning.hh:27
OperatorImp OperatorType
Definition fempreconditioning.hh:28
FemPreconditioningBase(const OperatorType &op)
Definition fempreconditioning.hh:34
virtual void operator()(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
application operator
Definition fempreconditioning.hh:36
DiscreteFunctionType::ConstDofIteratorType ConstDofIteratorType
Definition fempreconditioning.hh:31
void apply(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
Definition fempreconditioning.hh:52
DiscreteFunctionType::DofIteratorType DofIteratorType
Definition fempreconditioning.hh:30
OperatorType::MatrixType MatrixType
Definition fempreconditioning.hh:75
const double w_
Definition fempreconditioning.hh:82
const MatrixType & matrix_
Definition fempreconditioning.hh:79
DiscreteFunctionType::DofIteratorType DofIteratorType
Definition fempreconditioning.hh:71
FemPreconditioningBase(const OperatorType &assembledOperator, const int n=1, const double relax=1.0)
Definition fempreconditioning.hh:85
DiscreteFunctionType::DofType DofType
Definition fempreconditioning.hh:69
virtual void operator()(const DiscreteFunctionType &u, DiscreteFunctionType &res) const
application operator
Definition fempreconditioning.hh:109
Dune::FieldTraits< DofType >::real_type RealType
Definition fempreconditioning.hh:70
OperatorImp OperatorType
Definition fempreconditioning.hh:67
DiscreteFunctionType diagonalInv_
Definition fempreconditioning.hh:77
DiscreteFunctionType::ConstDofIteratorType ConstDofIteratorType
Definition fempreconditioning.hh:72
void apply(const DiscreteFunctionType &u, DiscreteFunctionType &v) const
Definition fempreconditioning.hh:129
DFImp DiscreteFunctionType
Definition fempreconditioning.hh:66
Precondtioner, implementing Jacobi, Gauss-Seidel and SOR works with.
Definition fempreconditioning.hh:179
Operator OperatorType
Definition fempreconditioning.hh:183
FemPreconditioning(const OperatorType &op, const int n=1, const double w=1.0)
Definition fempreconditioning.hh:184
Definition solver/parameter.hh:25
T epsilon(T... args)
T reset(T... args)