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 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
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>
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 {
154 parameter_.swap( sharedNewParams );
156 }
157
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
176 : parameter_(other.parameter_),
177 operator_(nullptr),
178 assembledOperator_(nullptr),
179 preconditioner_(nullptr),
180 rhs_(),
181 x_(),
182 iterations_(-1),
184 {}
185
186 protected:
187 // specialization that works with the solvers native storage type
189 {
190 rightHandSideCopied_ = false;
191 iterations_ = asImp().apply( u, w );
192 }
193
194 template <class DImpl, class RImpl>
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 );
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
227
228 const OperatorType* operator_ = nullptr;
231
232 // temporary functions for solver compatibility
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
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:144
int iterations_
Definition inverseoperatorinterface.hh:236
InverseOperatorInterface(const InverseOperatorInterface &other)
copy constructor setting defaults
Definition inverseoperatorinterface.hh:175
InverseOperatorInterface(const SolverParameterType &parameter)
default constructor
Definition inverseoperatorinterface.hh:66
double averageCommTime() const
return accumulated communication time
Definition inverseoperatorinterface.hh:169
const PreconditionerType * preconditioner_
Definition inverseoperatorinterface.hh:230
Traits::SolverParameterType SolverParameterType
Definition inverseoperatorinterface.hh:53
SolverParameterType & parameter() const
Definition inverseoperatorinterface.hh:158
std::unique_ptr< SolverDiscreteFunctionType > x_
Definition inverseoperatorinterface.hh:234
const OperatorType * operator_
Definition inverseoperatorinterface.hh:228
std::unique_ptr< SolverDiscreteFunctionType > rhs_
Definition inverseoperatorinterface.hh:233
virtual void setMaxLinearIterations(const int iter)
set number of max linear iterations to be used before an exception is thrown
Definition inverseoperatorinterface.hh:139
void unbind()
reset all pointers and internal temporary memory
Definition inverseoperatorinterface.hh:128
static const bool preconditioningAvailable
true if a preconditioner type is exported and can be set using bind( op, p )
Definition inverseoperatorinterface.hh:61
Impl::SolverInfo SolverInfoType
Definition inverseoperatorinterface.hh:55
void opApply(const DiscreteFunctionInterface< DImpl > &u, DiscreteFunctionInterface< RImpl > &w) const
Definition inverseoperatorinterface.hh:195
bool verbose() const
Definition inverseoperatorinterface.hh:163
void bind(const OperatorType &op, const PreconditionerType &preconditioner)
store pointer to linear operator and preconditioner
Definition inverseoperatorinterface.hh:121
Traits::AssembledOperatorType AssembledOperatorType
Definition inverseoperatorinterface.hh:51
Traits::SolverDiscreteFunctionType SolverDiscreteFunctionType
Definition inverseoperatorinterface.hh:49
bool rightHandSideCopied_
Definition inverseoperatorinterface.hh:237
BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType > Base2Type
Definition inverseoperatorinterface.hh:40
bool verbose_
Definition inverseoperatorinterface.hh:238
std::shared_ptr< SolverParameterType > parameter_
Definition inverseoperatorinterface.hh:226
Traits::InverseOperatorType InverseOperatorType
Definition inverseoperatorinterface.hh:44
Traits::PreconditionerType PreconditionerType
Definition inverseoperatorinterface.hh:52
BaseType::DomainFunctionType DomainFunctionType
Definition inverseoperatorinterface.hh:46
const AssembledOperatorType * assembledOperator_
Definition inverseoperatorinterface.hh:229
Traits::OperatorType BaseType
Definition inverseoperatorinterface.hh:39
void opApply(const SolverDiscreteFunctionType &u, SolverDiscreteFunctionType &w) const
Definition inverseoperatorinterface.hh:188
void bind(const OperatorType &op)
store pointer to linear operator
Definition inverseoperatorinterface.hh:109
Traits::OperatorType OperatorType
Definition inverseoperatorinterface.hh:50
int iterations() const
return number of iterations used in previous call of application operator
Definition inverseoperatorinterface.hh:131
std::function< bool(const RangeFunctionType &w, const RangeFunctionType &dw, double residualNorm) > ErrorMeasureType
type of error measure (used by NewtonInverseOperator primnarily)
Definition inverseoperatorinterface.hh:58
BaseType::RangeFunctionType RangeFunctionType
Definition inverseoperatorinterface.hh:47
virtual SolverInfoType info() const
Return performance info about last solver call.
Definition inverseoperatorinterface.hh:134
void setParameters(const SolverParameterType &newParams)
set complete set of linear inverse operator parameters
Definition inverseoperatorinterface.hh:151
T forward(T... args)
T reset(T... args)
T swap(T... args)