dune-fem  2.4.1-rc
pardginverseoperators.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_SOLVER_PARDGINVERSEOPERATORS_HH
2 #define DUNE_FEM_SOLVER_PARDGINVERSEOPERATORS_HH
3 
4 #include <dune/common/nullptr.hh>
5 
10 
11 #ifdef USE_PARDG_ODE_SOLVER
12 
13 namespace Dune
14 {
15 
16  namespace Fem
17  {
18 
19  // Internal Forward Declarations
20  // -----------------------------
21 
22  template< class DomainFunction, class RangeFunction = DomainFunction >
23  class ParDGOperator;
24 
25 
26 
27  // ParDGOperator for AdaptiveDiscreteFunction
28  // ------------------------------------------
29 
30  template< class DomainFunctionSpace, class RangeFunctionSpace >
31  class ParDGOperator< AdaptiveDiscreteFunction< DomainFunctionSpace >, AdaptiveDiscreteFunction< RangeFunctionSpace > >
32  : public PARDG::Function
33  {
34  typedef ParDGOperator< AdaptiveDiscreteFunction< DomainFunctionSpace >, AdaptiveDiscreteFunction< RangeFunctionSpace > > ThisType;
35 
36  typedef AdaptiveDiscreteFunction< DomainFunctionSpace > DomainFunctionType;
37  typedef AdaptiveDiscreteFunction< RangeFunctionSpace > RangeFunctionType;
38 
39  public:
40  typedef Operator< DomainFunctionType, RangeFunctionType > OperatorType;
41 
42  typedef typename DomainFunctionType::DiscreteFunctionSpaceType DomainFunctionSpaceType;
43  typedef typename RangeFunctionType::DiscreteFunctionSpaceType RangeFunctionSpaceType;
44 
45  ParDGOperator ( const OperatorType &op, const DomainFunctionSpaceType &domainSpace, const RangeFunctionSpaceType &rangeSpace )
46  : operator_( op ),
47  domainSpace_( domainSpace ),
48  rangeSpace_( rangeSpace )
49  {}
50 
51  void operator() ( const double *u, double *w, int i = 0 )
52  {
53  DomainFunctionType uFunction( "ParDGOperator u", domainSpace_, u );
54  RangeFunctionType wFunction( "ParDGOperator w", rangeSpace_, w );
55  operator_( uFunction, wFunction );
56  }
57 
58  int dim_of_argument( int i = 0 ) const
59  {
60  assert( i == 0 );
61  return domainSpace_.size();
62  }
63 
64  int dim_of_value ( int i = 0 ) const
65  {
66  assert( i == 0 );
67  return rangeSpace_.size();
68  }
69 
70  private:
71  const OperatorType &operator_;
72  const DomainFunctionSpaceType &domainSpace_;
73  const RangeFunctionSpaceType &rangeSpace_;
74  };
75 
76 
77 
78 
79  // ParDGGeneralizedMinResInverseOperator
80  // -------------------------------------
81 
82  template< class DiscreteFunction >
83  class ParDGGeneralizedMinResInverseOperator
84  : public Operator< DiscreteFunction, DiscreteFunction >
85  {
87 
88  typedef ParDGOperator< DiscreteFunction, DiscreteFunction > ParDGOperatorType;
89 
90  public:
91  typedef typename BaseType::DomainFunctionType DomainFunctionType;
92  typedef typename BaseType::RangeFunctionType RangeFunctionType;
93 
95  typedef Operator< DiscreteFunction, DiscreteFunction > PreconditionerType;
96 
97  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op,
98  double redEps, double absLimit, unsigned int maxIterations, bool verbose,
99  const ParameterReader &parameter = Parameter::container() )
100  : ParDGGeneralizedMinResInverseOperator( op, nullptr, redEps, absLimit, maxIterations, verbose, parameter ) {}
101 
102  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op, double redEps, double absLimit,
103  const ParameterReader &parameter = Parameter::container() )
104  : ParDGGeneralizedMinResInverseOperator( op, nullptr, redEps, absLimit, std::numeric_limits< unsigned int >::max(), false, parameter )
105  {}
106 
107  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op, double redEps, double absLimit,
108  unsigned int maxIterations,
109  const ParameterReader &parameter = Parameter::container() )
110  : ParDGGeneralizedMinResInverseOperator( op, nullptr, redEps, absLimit, maxIterations, false, parameter ) {}
111 
112 
113  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op, const PreconditionerType &preconditioner,
114  double redEps, double absLimit, unsigned int maxIterations, bool verbose,
115  const ParameterReader &parameter = Parameter::container() )
116  : ParDGGeneralizedMinResInverseOperator( op, &preconditioner, redEps, absLimit, maxIterations, verbose, parameter ) {}
117 
118  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op, const PreconditionerType &preconditioner,
119  double redEps, double absLimit,
120  const ParameterReader &parameter = Parameter::container() )
121  : ParDGGeneralizedMinResInverseOperator( op, &preconditioner, redEps, absLimit, std::numeric_limits< unsigned int >::max(), false, parameter )
122  {}
123 
124  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op, const PreconditionerType &preconditioner,
125  double redEps, double absLimit, unsigned int maxIterations,
126  const ParameterReader &parameter = Parameter::container() )
127  : ParDGGeneralizedMinResInverseOperator( op, &preconditioner, redEps, absLimit, maxIterations, false, parameter ) {}
128 
129 
130  virtual void operator() ( const DomainFunctionType &u, RangeFunctionType &w ) const
131  {
132  ParDGOperatorType parDGOperator( operator_, w.space(), u.space() );
133  if( preconditioner_ )
134  {
135  ParDGOperatorType parDGPreconditioner( *preconditioner_, w.space(), w.space() );
136  solver_.set_preconditioner( parDGPreconditioner );
137  solver_.solve( parDGOperator, w.leakPointer(), u.leakPointer() );
138  solver_.unset_preconditioner();
139  }
140  else
141  solver_.solve( parDGOperator, w.leakPointer(), u.leakPointer() );
142  }
143 
144  unsigned int iterations () const
145  {
146  return solver_.number_of_iterations();
147  }
148 
149  private:
150  ParDGGeneralizedMinResInverseOperator ( const OperatorType &op,
151  const PreconditionerType *preconditioner,
152  double redEps, double absLimit,
153  unsigned int maxIterations, bool verbose,
154  const ParameterReader &parameter = Parameter::container() )
155  : solver_( PARDG::Communicator::instance(), parameter.getValue< int >( "fem.solver.gmres.restart", 20 ) ),
156  operator_( op ),
157  preconditioner_( preconditioner )
158  {
159  static const std::string errorTypeTable[] = { "absolute", "relative" };
160  // errormeassure used in the linear solver
161  int errorType = parameter.getEnum( "fem.solver.errormeasure", errorTypeTable, 0 );
162  if (errorType == 1)
163  solver_.set_tolerance( redEps, true);
164  else
165  solver_.set_tolerance( absLimit, false );
166 
167  maxIterations = std::min( (unsigned int)std::numeric_limits< int >::max(), maxIterations );
168  solver_.set_max_number_of_iterations( int( maxIterations ) );
169 
170  // only set output when general verbose mode is enabled
171  // (basically to avoid output on every rank)
172  if( verbose && Parameter :: verbose() )
173  {
174  solver_.IterativeSolver::set_output( std::cout );
175  solver_.DynamicalObject::set_output( std::cout );
176  }
177  }
178 
179  mutable PARDG::GMRES solver_;
180  const OperatorType &operator_;
181  const PreconditionerType *preconditioner_;
182  };
183 
184  // ParDGBiCGStabInverseOperator
185  // ----------------------------
186 
187  template< class DiscreteFunction >
188  class ParDGBiCGStabInverseOperator
189  : public Operator< DiscreteFunction, DiscreteFunction >
190  {
192 
193  typedef ParDGOperator< DiscreteFunction, DiscreteFunction > ParDGOperatorType;
194 
195  public:
196  typedef typename BaseType::DomainFunctionType DomainFunctionType;
197  typedef typename BaseType::RangeFunctionType RangeFunctionType;
198 
200  typedef Operator< DiscreteFunction, DiscreteFunction > PreconditionerType;
201 
202  ParDGBiCGStabInverseOperator ( const OperatorType &op,
203  double redEps, double absLimit, unsigned int maxIterations, bool verbose,
204  const ParameterReader &parameter = Parameter::container() )
205  : ParDGBiCGStabInverseOperator( op, nullptr, redEps, absLimit, maxIterations, verbose, parameter ) {}
206 
207  ParDGBiCGStabInverseOperator ( const OperatorType &op,
208  double redEps, double absLimit,
209  const ParameterReader &parameter = Parameter::container() )
210  : ParDGBiCGStabInverseOperator( op, nullptr, redEps, absLimit, std::numeric_limits< unsigned int >::max(), false, parameter ) {}
211 
212  ParDGBiCGStabInverseOperator ( const OperatorType &op,
213  double redEps, double absLimit, unsigned int maxIterations,
214  const ParameterReader &parameter = Parameter::container() )
215  : ParDGBiCGStabInverseOperator( op, nullptr, redEps, absLimit, maxIterations, false, parameter ) {}
216 
217 
218  ParDGBiCGStabInverseOperator ( const OperatorType &op, const PreconditionerType &preconditioner,
219  double redEps, double absLimit, unsigned int maxIterations, bool verbose,
220  const ParameterReader &parameter = Parameter::container() )
221  : ParDGBiCGStabInverseOperator( op, &preconditioner, redEps, absLimit, maxIterations, verbose, parameter ) {}
222 
223  ParDGBiCGStabInverseOperator ( const OperatorType &op, const PreconditionerType &preconditioner,
224  double redEps, double absLimit,
225  const ParameterReader &parameter = Parameter::container() )
226  : ParDGBiCGStabInverseOperator( op, &preconditioner, redEps, absLimit, std::numeric_limits< unsigned int >::max(), false, parameter ) {}
227 
228  ParDGBiCGStabInverseOperator ( const OperatorType &op, const PreconditionerType &preconditioner,
229  double redEps, double absLimit, unsigned int maxIterations,
230  const ParameterReader &parameter = Parameter::container() )
231  : ParDGBiCGStabInverseOperator( op, &preconditioner, redEps, absLimit, maxIterations, false, parameter ) {}
232 
233  virtual void operator() ( const DomainFunctionType &u, RangeFunctionType &w ) const
234  {
235  ParDGOperatorType parDGOperator( operator_, w.space(), u.space() );
236  if( preconditioner_ )
237  {
238  ParDGOperatorType parDGPreconditioner( *preconditioner_, w.space(), w.space() );
239  solver_.set_preconditioner( parDGPreconditioner );
240  solver_.solve( parDGOperator, w.leakPointer(), u.leakPointer() );
241  solver_.unset_preconditioner();
242  }
243  else
244  solver_.solve( parDGOperator, w.leakPointer(), u.leakPointer() );
245  }
246 
247  unsigned int iterations () const
248  {
249  return solver_.number_of_iterations();
250  }
251 
252  private:
253  ParDGBiCGStabInverseOperator ( const OperatorType &op, const PreconditionerType *preconditioner,
254  double redEps, double absLimit, unsigned int maxIterations, bool verbose,
255  const ParameterReader &parameter )
256  : solver_( PARDG::Communicator::instance() ),
257  operator_( op ),
258  preconditioner_( preconditioner )
259  {
260  static const std::string errorTypeTable[] = { "absolute", "relative" };
261  // errormeassure used in the linear solver
262  int errorType = parameter.getEnum( "fem.solver.errormeasure", errorTypeTable, 0 );
263  if (errorType == 1)
264  solver_.set_tolerance( redEps, true);
265  else
266  solver_.set_tolerance( absLimit, false );
267 
268  maxIterations = std::min( (unsigned int)std::numeric_limits< int >::max(), maxIterations );
269  solver_.set_max_number_of_iterations( int( maxIterations ) );
270 
271  // only set output when general verbose mode is enabled
272  // (basically to avoid output on every rank)
273  if( verbose && Parameter :: verbose() )
274  {
275  solver_.IterativeSolver::set_output( std::cout );
276  solver_.DynamicalObject::set_output( std::cout );
277  }
278  }
279 
280  mutable PARDG::BICGSTAB solver_;
281  const OperatorType &operator_;
282  const PreconditionerType *preconditioner_;
283  };
284 
285  } // namespace Fem
286 
287 } // namespace Dune
288 
289 #endif // #ifdef USE_PARDG_ODE_SOLVER
290 
291 #endif // #ifndef DUNE_FEM_SOLVER_PARDGINVERSEOPERATORS_HH
static constexpr T min(T a)
Definition: utility.hh:81
BasicParameterReader< std::function< const std::string *(const std::string &, const std::string *) > > ParameterReader
Definition: reader.hh:264
static constexpr T max(T a)
Definition: utility.hh:65
static double max(const Double &v, const double p)
Definition: double.hh:387
Definition: coordinate.hh:4
static ParameterContainer & container()
Definition: io/parameter.hh:190
STL namespace.
static bool verbose()
obtain the cached value for fem.verbose
Definition: io/parameter.hh:444
#define PARDG
Definition: pardg.hh:26