dune-fem  2.8-git
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 
10 namespace Dune {
11  namespace Fem {
12 
13  template <class Traits>
15  public Dune::Fem::Operator< typename Traits::DiscreteFunctionType, typename Traits::DiscreteFunctionType >,
16  public BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType >
17  {
18  protected:
19  typedef typename Traits::OperatorType BaseType;
20  typedef BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType > Base2Type;
21 
22  using Base2Type :: asImp;
23 
24  typedef typename Traits::InverseOperatorType InverseOperatorType;
25  public:
26  typedef typename BaseType :: DomainFunctionType DomainFunctionType;
27  typedef typename BaseType :: RangeFunctionType RangeFunctionType;
28 
29  typedef typename Traits :: SolverDiscreteFunctionType SolverDiscreteFunctionType;
30  typedef typename Traits :: OperatorType OperatorType;
31  typedef typename Traits :: AssembledOperatorType AssembledOperatorType;
32  typedef typename Traits :: PreconditionerType PreconditionerType;
33  typedef typename Traits :: SolverParameterType SolverParameterType;
34 
39  : parameter_( std::make_shared< SolverParameterType >( parameter ) )
40  , verbose_( parameter_->verbose() && Dune::Fem::Parameter::verbose() )
41 
42  {
43  unbind();
44  }
45 
53  virtual void operator() ( const DomainFunctionType& u, RangeFunctionType& w ) const
54  {
55  opApply( u, w );
56  }
57 
58 
69  template <class DImpl, class RImpl>
72  {
73  opApply( u, w );
74  }
75 
81  void bind ( const OperatorType &op )
82  {
83  operator_ = &op;
84  assembledOperator_ = dynamic_cast<const AssembledOperatorType*>( &op );
85  }
86 
93  void bind ( const OperatorType &op, const PreconditionerType &preconditioner )
94  {
95  bind( op );
96  preconditioner_ = &preconditioner;
97  }
98 
100  void unbind () { operator_ = nullptr; assembledOperator_ = nullptr; preconditioner_ = nullptr; rhs_.reset(); x_.reset(); }
101 
103  int iterations () const { return iterations_; }
104 
108  virtual void setMaxLinearIterations ( const int iter ) {
109  parameter_->setMaxIterations( iter );
110  }
111 
113  virtual void setMaxIterations ( const int iter ) {
114  parameter_->setMaxIterations( iter );
115  }
116 
120  void setParameters( const SolverParameterType& newParams)
121  {
122  std::shared_ptr< SolverParameterType > sharedNewParams = std::make_shared< SolverParameterType > (newParams);
123  parameter_.swap( sharedNewParams );
125  }
126 
128  {
129  return *parameter_;
130  }
131 
132  bool verbose() const
133  {
134  return verbose_;
135  }
136 
138  double averageCommTime() const
139  {
140  return -1.;
141  }
142 
145  : parameter_(other.parameter_),
146  operator_(nullptr),
147  assembledOperator_(nullptr),
148  preconditioner_(nullptr),
149  rhs_(),
150  x_(),
151  iterations_(-1),
152  rightHandSideCopied_(false)
153  {}
154 
155  protected:
156  // specialization that works with the solvers native storage type
158  {
159  rightHandSideCopied_ = false;
160  iterations_ = asImp().apply( u, w );
161  }
162 
163  template <class DImpl, class RImpl>
166  {
167  if( ! assembledOperator_ )
168  DUNE_THROW(Dune::NotImplemented, "InverseOperator::operator() for matrix free operators only makes sense" <<
169  " for fixed types of domain and range functions to avoid excessive copying!");
170 
171  if( ! rhs_ )
172  {
173  rhs_.reset( new SolverDiscreteFunctionType( "InvOp::rhs", u.space() ) );
174  }
175 
176  if( ! x_ )
177  {
178  x_.reset( new SolverDiscreteFunctionType( "InvOp::x", w.space() ) );
179  }
180 
181  // copy right hand side
182  rhs_->assign( u );
183  rightHandSideCopied_ = true;
184 
185  // copy initial guess
186  x_->assign( w );
187 
188  iterations_ = asImp().apply( *rhs_, *x_ );
189 
190  // store result in destination
191  w.assign( *x_ );
192  rightHandSideCopied_ = false;
193  }
194 
195  std::shared_ptr<SolverParameterType> parameter_;
196 
197  const OperatorType* operator_ = nullptr;
200 
201  // temporary functions for solver compatibility
202  mutable std::unique_ptr< SolverDiscreteFunctionType > rhs_;
203  mutable std::unique_ptr< SolverDiscreteFunctionType > x_;
204 
205  mutable int iterations_ = -1 ;
206  mutable bool rightHandSideCopied_ = false ;
207  mutable bool verbose_;
208  };
209  } // end namespace Fem
210 } // end namespace Dune
211 
212 #endif // DUNE_FEM_SOLVER_INVERSEOPERATORINTERFACE_HH
Definition: bindguard.hh:11
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
Definition: io/parameter.hh:445
Definition: bartonnackmaninterface.hh:17
const Implementation & asImp() const
Definition: bartonnackmaninterface.hh:37
abstract operator
Definition: operator.hh:34
Definition: inverseoperatorinterface.hh:17
virtual void setMaxIterations(const int iter)
Definition: inverseoperatorinterface.hh:113
int iterations_
Definition: inverseoperatorinterface.hh:205
virtual void operator()(const DomainFunctionType &u, RangeFunctionType &w) const
application of operator to compute
Definition: inverseoperatorinterface.hh:53
InverseOperatorInterface(const InverseOperatorInterface &other)
copy constructor setting defaults
Definition: inverseoperatorinterface.hh:144
InverseOperatorInterface(const SolverParameterType &parameter)
default constructor
Definition: inverseoperatorinterface.hh:38
double averageCommTime() const
return accumulated communication time
Definition: inverseoperatorinterface.hh:138
const PreconditionerType * preconditioner_
Definition: inverseoperatorinterface.hh:199
Traits ::PreconditionerType PreconditionerType
Definition: inverseoperatorinterface.hh:32
std::unique_ptr< SolverDiscreteFunctionType > x_
Definition: inverseoperatorinterface.hh:203
const OperatorType * operator_
Definition: inverseoperatorinterface.hh:197
std::unique_ptr< SolverDiscreteFunctionType > rhs_
Definition: inverseoperatorinterface.hh:202
virtual void setMaxLinearIterations(const int iter)
set number of max linear iterations to be used before an exception is thrown
Definition: inverseoperatorinterface.hh:108
void unbind()
reset all pointers and internal temporary memory
Definition: inverseoperatorinterface.hh:100
Traits ::SolverParameterType SolverParameterType
Definition: inverseoperatorinterface.hh:33
Traits ::OperatorType OperatorType
Definition: inverseoperatorinterface.hh:30
BaseType ::DomainFunctionType DomainFunctionType
Definition: inverseoperatorinterface.hh:26
void opApply(const DiscreteFunctionInterface< DImpl > &u, DiscreteFunctionInterface< RImpl > &w) const
Definition: inverseoperatorinterface.hh:164
bool verbose() const
Definition: inverseoperatorinterface.hh:132
void bind(const OperatorType &op, const PreconditionerType &preconditioner)
store pointer to linear operator and preconditioner
Definition: inverseoperatorinterface.hh:93
BaseType ::RangeFunctionType RangeFunctionType
Definition: inverseoperatorinterface.hh:27
bool rightHandSideCopied_
Definition: inverseoperatorinterface.hh:206
BartonNackmanInterface< InverseOperatorInterface< Traits >, typename Traits::InverseOperatorType > Base2Type
Definition: inverseoperatorinterface.hh:20
bool verbose_
Definition: inverseoperatorinterface.hh:207
Traits ::SolverDiscreteFunctionType SolverDiscreteFunctionType
Definition: inverseoperatorinterface.hh:29
std::shared_ptr< SolverParameterType > parameter_
Definition: inverseoperatorinterface.hh:195
Traits::InverseOperatorType InverseOperatorType
Definition: inverseoperatorinterface.hh:24
SolverParameterType & parameter() const
Definition: inverseoperatorinterface.hh:127
const AssembledOperatorType * assembledOperator_
Definition: inverseoperatorinterface.hh:198
Traits ::AssembledOperatorType AssembledOperatorType
Definition: inverseoperatorinterface.hh:31
Traits::OperatorType BaseType
Definition: inverseoperatorinterface.hh:19
void opApply(const SolverDiscreteFunctionType &u, SolverDiscreteFunctionType &w) const
Definition: inverseoperatorinterface.hh:157
void bind(const OperatorType &op)
store pointer to linear operator
Definition: inverseoperatorinterface.hh:81
int iterations() const
return number of iterations used in previous call of application operator
Definition: inverseoperatorinterface.hh:103
void setParameters(const SolverParameterType &newParams)
set complete set of linear inverse operator parameters
Definition: inverseoperatorinterface.hh:120