DUNE-FEM (unstable)

inverseoperatorinterface.hh
1#ifndef DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
2#define DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
3
4#include <dune/fem/function/common/discretefunction.hh>
5#include <dune/fem/operator/common/operator.hh>
6#include <dune/fem/io/parameter.hh>
7#include <dune/fem/solver/parameter.hh>
8#include <dune/fem/misc/bartonnackmaninterface.hh>
9
10namespace Dune {
11 namespace Fem {
12
13 namespace Impl {
15 struct SolverInfo
16 {
17 SolverInfo(bool pconverged,int plinearIterations,int pnonlinearIterations, const std::vector<double>& ptiming)
18 : converged(pconverged), linearIterations(plinearIterations),
19 nonlinearIterations(pnonlinearIterations), timing(ptiming)
20 {}
21
22 SolverInfo(bool pconverged,int plinearIterations)
23 : SolverInfo(pconverged, plinearIterations, 0, std::vector<double> ())
24 {}
25
26 bool converged;
27 int linearIterations;
28 int nonlinearIterations;
29 std::vector<double> timing;
30 };
31 }
32
33 template <class Traits>
34 class InverseOperatorInterface :
35 public Dune::Fem::Operator< typename Traits::DiscreteFunctionType, typename Traits::DiscreteFunctionType >,
36 public BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType >
37 {
38 protected:
39 typedef typename Traits::OperatorType BaseType;
40 typedef BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType > Base2Type;
41
42 using Base2Type :: asImp;
43
44 typedef typename Traits::InverseOperatorType InverseOperatorType;
45 public:
46 typedef typename BaseType :: DomainFunctionType DomainFunctionType;
47 typedef typename BaseType :: RangeFunctionType RangeFunctionType;
48
49 typedef typename Traits :: SolverDiscreteFunctionType SolverDiscreteFunctionType;
50 typedef typename Traits :: OperatorType OperatorType;
51 typedef typename Traits :: AssembledOperatorType AssembledOperatorType;
52 typedef typename Traits :: PreconditionerType PreconditionerType;
53 typedef typename Traits :: SolverParameterType SolverParameterType;
54
55 typedef Impl::SolverInfo SolverInfoType;
56
58 typedef std::function< bool ( const RangeFunctionType &w, const RangeFunctionType &dw, double residualNorm ) > ErrorMeasureType;
59
61 static const bool preconditioningAvailable = true;
62
66 InverseOperatorInterface( const SolverParameterType& parameter )
67 : parameter_( std::make_shared< SolverParameterType >( parameter ) )
68 , verbose_( parameter_->verbose() && Dune::Fem::Parameter::verbose() )
69
70 {
71 unbind();
72 }
73
81 virtual void operator() ( const DomainFunctionType& u, RangeFunctionType& w ) const
82 {
83 opApply( u, w );
84 }
85
86
97 template <class DImpl, class RImpl>
98 void operator() ( const DiscreteFunctionInterface< DImpl >&u,
99 DiscreteFunctionInterface< RImpl >& w ) const
100 {
101 opApply( u, w );
102 }
103
109 void bind ( const OperatorType &op )
110 {
111 operator_ = &op;
112 assembledOperator_ = dynamic_cast<const AssembledOperatorType*>( &op );
113 }
114
121 void bind ( const OperatorType &op, const PreconditionerType &preconditioner )
122 {
123 bind( op );
124 preconditioner_ = &preconditioner;
125 }
126
128 void unbind () { operator_ = nullptr; assembledOperator_ = nullptr; preconditioner_ = nullptr; rhs_.reset(); x_.reset(); }
129
131 int iterations () const { return iterations_; }
132
134 virtual SolverInfoType info() const { return SolverInfoType( true, iterations () ); }
135
139 virtual void setMaxLinearIterations ( const int iter ) {
140 parameter_->setMaxIterations( iter );
141 }
142
144 virtual void setMaxIterations ( const int iter ) {
145 parameter_->setMaxIterations( iter );
146 }
147
151 void setParameters( const SolverParameterType& newParams)
152 {
153 std::shared_ptr< SolverParameterType > sharedNewParams = std::make_shared< SolverParameterType > (newParams);
154 parameter_.swap( sharedNewParams );
155 verbose_ = parameter_->verbose() && Dune::Fem::Parameter::verbose();
156 }
157
158 SolverParameterType& parameter () const
159 {
160 return *parameter_;
161 }
162
163 bool verbose() const
164 {
165 return verbose_;
166 }
167
169 double averageCommTime() const
170 {
171 return -1.;
172 }
173
175 InverseOperatorInterface(const InverseOperatorInterface &other)
176 : parameter_(other.parameter_),
177 operator_(nullptr),
178 assembledOperator_(nullptr),
179 preconditioner_(nullptr),
180 rhs_(),
181 x_(),
182 iterations_(-1),
183 rightHandSideCopied_(false)
184 {}
185
186 protected:
187 // specialization that works with the solvers native storage type
188 void opApply( const SolverDiscreteFunctionType& u, SolverDiscreteFunctionType& w ) const
189 {
190 rightHandSideCopied_ = false;
191 iterations_ = asImp().apply( u, w );
192 }
193
194 template <class DImpl, class RImpl>
195 void opApply( const DiscreteFunctionInterface< DImpl >&u,
196 DiscreteFunctionInterface< RImpl >& w ) const
197 {
198 if( ! assembledOperator_ )
199 DUNE_THROW(Dune::NotImplemented, "InverseOperator::operator() for matrix free operators only makes sense" <<
200 " for fixed types of domain and range functions to avoid excessive copying!");
201
202 if( ! rhs_ )
203 {
204 rhs_.reset( new SolverDiscreteFunctionType( "InvOp::rhs", u.space() ) );
205 }
206
207 if( ! x_ )
208 {
209 x_.reset( new SolverDiscreteFunctionType( "InvOp::x", w.space() ) );
210 }
211
212 // copy right hand side
213 rhs_->assign( u );
214 rightHandSideCopied_ = true;
215
216 // copy initial guess
217 x_->assign( w );
218
219 iterations_ = asImp().apply( *rhs_, *x_ );
220
221 // store result in destination
222 w.assign( *x_ );
223 rightHandSideCopied_ = false;
224 }
225
226 std::shared_ptr<SolverParameterType> parameter_;
227
228 const OperatorType* operator_ = nullptr;
229 const AssembledOperatorType* assembledOperator_ = nullptr;
230 const PreconditionerType* preconditioner_ = nullptr;
231
232 // temporary functions for solver compatibility
233 mutable std::unique_ptr< SolverDiscreteFunctionType > rhs_;
234 mutable std::unique_ptr< SolverDiscreteFunctionType > x_;
235
236 mutable int iterations_ = -1 ;
237 mutable bool rightHandSideCopied_ = false ;
238 mutable bool verbose_;
239 };
240 } // end namespace Fem
241} // end namespace Dune
242
243#endif // DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
Inverse operator base on CG method. Uses a runtime parameter fem.preconditioning which enables diagon...
Definition: cginverseoperator.hh:408
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition: parameter.hh:466
Default exception for dummy implementations.
Definition: exceptions.hh:263
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
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)