dune-fem 2.12-git
Loading...
Searching...
No Matches
inverseoperatorinterface.hh
Go to the documentation of this file.
1#ifndef DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
2#define DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
3
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;
30 };
31 }
32
33 template <class Traits>
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 SolverDiscreteFunctionType :: DiscreteFunctionSpaceType SolverDiscreteFunctionSpaceType;
51 typedef typename Traits :: OperatorType OperatorType;
52 typedef typename Traits :: AssembledOperatorType AssembledOperatorType;
53 typedef typename Traits :: PreconditionerType PreconditionerType;
54 typedef typename Traits :: SolverParameterType SolverParameterType;
55
56 typedef Impl::SolverInfo SolverInfoType;
57
59 typedef std::function< bool ( const RangeFunctionType &w, const RangeFunctionType &dw, double residualNorm ) > ErrorMeasureType;
60
62 static const bool preconditioningAvailable = true;
63
74
82 virtual void operator() ( const DomainFunctionType& u, RangeFunctionType& w ) const
83 {
84 opApply( u, w );
85 }
86
87
98 template <class DImpl, class RImpl>
101 {
102 opApply( u, w );
103 }
104
110 void bind ( const OperatorType &op )
111 {
112 operator_ = &op;
113 assembledOperator_ = dynamic_cast<const AssembledOperatorType*>( &op );
114 }
115
122 void bind ( const OperatorType &op, const PreconditionerType &preconditioner )
123 {
124 bind( op );
125 preconditioner_ = &preconditioner;
126 }
127
129 void unbind () { operator_ = nullptr; assembledOperator_ = nullptr; preconditioner_ = nullptr; rhs_.reset(); x_.reset(); }
130
132 int iterations () const { return iterations_; }
133
135 virtual SolverInfoType info() const { return SolverInfoType( true, iterations () ); }
136
140 virtual void setMaxLinearIterations ( const int iter ) {
141 parameter_->setMaxIterations( iter );
142 }
143
145 virtual void setMaxIterations ( const int iter ) {
146 parameter_->setMaxIterations( iter );
147 }
148
152 void setParameters( const SolverParameterType& newParams)
153 {
155 parameter_.swap( sharedNewParams );
157 }
158
160 {
161 return *parameter_;
162 }
163
164 bool verbose() const
165 {
166 return verbose_;
167 }
168
170 double averageCommTime() const
171 {
172 return -1.;
173 }
174
177 : parameter_(other.parameter_),
178 operator_(nullptr),
179 assembledOperator_(nullptr),
180 preconditioner_(nullptr),
181 rhs_(),
182 x_(),
183 iterations_(-1),
185 {}
186
187 protected:
188 // specialization that works with the solvers native storage type
190 {
191 rightHandSideCopied_ = false;
192 iterations_ = asImp().apply( u, w );
193 }
194
195 // method to create temporary discrete functions, can be overloaded in derived class
196 // this method is overloaded in PetscInverseOperator
198 const SolverDiscreteFunctionSpaceType& wSpace ) const
199 {
200 if( ! rhs_ )
201 {
202 rhs_.reset( new SolverDiscreteFunctionType( "InvOp::rhs", uSpace ) );
203 }
204
205 if( ! x_ )
206 {
207 x_.reset( new SolverDiscreteFunctionType( "InvOp::x", wSpace ) );
208 }
209 }
210
211 template <class DImpl, class RImpl>
214 {
215 if( ! assembledOperator_ )
216 DUNE_THROW(Dune::NotImplemented, "InverseOperator::operator() for matrix free operators only makes sense" <<
217 " for fixed types of domain and range functions to avoid excessive copying!");
218
219 // create temporary dfs, this can be overloaded in derived class
220 allocateMemory( u.space(), w.space() );
221
222 // copy right hand side
223 rhs_->assign( u );
225
226 // copy initial guess
227 x_->assign( w );
228
229 iterations_ = asImp().apply( *rhs_, *x_ );
230
231 // store result in destination
232 w.assign( *x_ );
233 rightHandSideCopied_ = false;
234 }
235
237
238 const OperatorType* operator_ = nullptr;
241
242 // temporary functions for solver compatibility
245
246 mutable int iterations_ = -1 ;
247 mutable bool rightHandSideCopied_ = false ;
248 mutable bool verbose_;
249 };
250 } // end namespace Fem
251} // end namespace Dune
252
253#endif // DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
virtual void operator()()=0
#define DUNE_THROW(E,...)
STL namespace.
Definition common/discretefunction.hh:86
const DiscreteFunctionSpaceType & space() const
obtain a reference to the corresponding DiscreteFunctionSpace
Definition common/discretefunction.hh:214
void assign(const DiscreteFunctionInterface< DFType > &g)
assign the DoFs of another discrete function to this one
Definition common/discretefunction.hh:455
Container for User Specified Parameters.
Definition io/parameter.hh:191
static bool verbose()
obtain the cached value for fem.verbose with default verbosity level 2
Definition io/parameter.hh:466
Definition bartonnackmaninterface.hh:17
const Traits::InverseOperatorType & asImp() const
Definition bartonnackmaninterface.hh:37
abstract operator
Definition operator.hh:34
Definition inverseoperatorinterface.hh:37
virtual void setMaxIterations(const int iter)
Definition inverseoperatorinterface.hh:145
int iterations_
Definition inverseoperatorinterface.hh:246
InverseOperatorInterface(const InverseOperatorInterface &other)
copy constructor setting defaults
Definition inverseoperatorinterface.hh:176
InverseOperatorInterface(const SolverParameterType &parameter)
default constructor
Definition inverseoperatorinterface.hh:67
double averageCommTime() const
return accumulated communication time
Definition inverseoperatorinterface.hh:170
const PreconditionerType * preconditioner_
Definition inverseoperatorinterface.hh:240
Traits::SolverParameterType SolverParameterType
Definition inverseoperatorinterface.hh:54
SolverParameterType & parameter() const
Definition inverseoperatorinterface.hh:159
std::unique_ptr< SolverDiscreteFunctionType > x_
Definition inverseoperatorinterface.hh:244
const OperatorType * operator_
Definition inverseoperatorinterface.hh:238
std::unique_ptr< SolverDiscreteFunctionType > rhs_
Definition inverseoperatorinterface.hh:243
virtual void setMaxLinearIterations(const int iter)
set number of max linear iterations to be used before an exception is thrown
Definition inverseoperatorinterface.hh:140
void unbind()
reset all pointers and internal temporary memory
Definition inverseoperatorinterface.hh:129
static const bool preconditioningAvailable
true if a preconditioner type is exported and can be set using bind( op, p )
Definition inverseoperatorinterface.hh:62
Impl::SolverInfo SolverInfoType
Definition inverseoperatorinterface.hh:56
void opApply(const DiscreteFunctionInterface< DImpl > &u, DiscreteFunctionInterface< RImpl > &w) const
Definition inverseoperatorinterface.hh:212
bool verbose() const
Definition inverseoperatorinterface.hh:164
void bind(const OperatorType &op, const PreconditionerType &preconditioner)
store pointer to linear operator and preconditioner
Definition inverseoperatorinterface.hh:122
Traits::AssembledOperatorType AssembledOperatorType
Definition inverseoperatorinterface.hh:52
Traits::SolverDiscreteFunctionType SolverDiscreteFunctionType
Definition inverseoperatorinterface.hh:49
bool rightHandSideCopied_
Definition inverseoperatorinterface.hh:247
BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType > Base2Type
Definition inverseoperatorinterface.hh:40
bool verbose_
Definition inverseoperatorinterface.hh:248
std::shared_ptr< SolverParameterType > parameter_
Definition inverseoperatorinterface.hh:236
Traits::InverseOperatorType InverseOperatorType
Definition inverseoperatorinterface.hh:44
Traits::PreconditionerType PreconditionerType
Definition inverseoperatorinterface.hh:53
virtual void allocateMemory(const SolverDiscreteFunctionSpaceType &uSpace, const SolverDiscreteFunctionSpaceType &wSpace) const
Definition inverseoperatorinterface.hh:197
BaseType::DomainFunctionType DomainFunctionType
Definition inverseoperatorinterface.hh:46
const AssembledOperatorType * assembledOperator_
Definition inverseoperatorinterface.hh:239
Traits::OperatorType BaseType
Definition inverseoperatorinterface.hh:39
void opApply(const SolverDiscreteFunctionType &u, SolverDiscreteFunctionType &w) const
Definition inverseoperatorinterface.hh:189
void bind(const OperatorType &op)
store pointer to linear operator
Definition inverseoperatorinterface.hh:110
SolverDiscreteFunctionType::DiscreteFunctionSpaceType SolverDiscreteFunctionSpaceType
Definition inverseoperatorinterface.hh:50
Traits::OperatorType OperatorType
Definition inverseoperatorinterface.hh:51
int iterations() const
return number of iterations used in previous call of application operator
Definition inverseoperatorinterface.hh:132
std::function< bool(const RangeFunctionType &w, const RangeFunctionType &dw, double residualNorm) > ErrorMeasureType
type of error measure (used by NewtonInverseOperator primnarily)
Definition inverseoperatorinterface.hh:59
BaseType::RangeFunctionType RangeFunctionType
Definition inverseoperatorinterface.hh:47
virtual SolverInfoType info() const
Return performance info about last solver call.
Definition inverseoperatorinterface.hh:135
void setParameters(const SolverParameterType &newParams)
set complete set of linear inverse operator parameters
Definition inverseoperatorinterface.hh:152
T forward(T... args)
T reset(T... args)
T swap(T... args)