Reference documentation for deal.II version 8.4.2
solver_selector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2015 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__solver_selector_h
17 #define dealii__solver_selector_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/smartpointer.h>
22 #include <deal.II/lac/solver.h>
23 #include <deal.II/lac/vector.h>
24 #include <deal.II/lac/vector_memory.h>
25 #include <deal.II/lac/solver_control.h>
26 #include <deal.II/lac/solver_cg.h>
27 #include <deal.II/lac/solver_bicgstab.h>
28 #include <deal.II/lac/solver_gmres.h>
29 #include <deal.II/lac/solver_minres.h>
30 #include <deal.II/lac/vector_memory.h>
31 #include <deal.II/lac/solver_richardson.h>
32 #include <deal.II/lac/precondition.h>
33 
34 DEAL_II_NAMESPACE_OPEN
35 
36 
39 
97 template <typename VectorType = Vector<double> >
99 {
100 public:
104  typedef VectorType vector_type;
105 
109  SolverSelector ();
110 
114  ~SolverSelector();
115 
121  template<class Matrix, class Preconditioner>
122  void solve (const Matrix &A,
123  VectorType &x,
124  const VectorType &b,
125  const Preconditioner &precond) const;
126 
131  void select(const std::string &name);
132 
136  void set_control(SolverControl &ctrl);
137 
141  void set_data(const typename SolverRichardson<VectorType>
142  ::AdditionalData &data);
143 
147  void set_data(const typename SolverCG<VectorType>
148  ::AdditionalData &data);
149 
153  void set_data(const typename SolverMinRes<VectorType>
154  ::AdditionalData &data);
155 
159  void set_data(const typename SolverBicgstab<VectorType>
160  ::AdditionalData &data);
161 
165  void set_data(const typename SolverGMRES<VectorType>
166  ::AdditionalData &data);
167 
171  void set_data(const typename SolverFGMRES<VectorType>
172  ::AdditionalData &data);
173 
177  static std::string get_solver_names ();
178 
182  DeclException1 (ExcSolverDoesNotExist,
183  std::string, << "Solver " << arg1 << " does not exist. Use one of "
184  << std::endl << get_solver_names());
185 
186 
187 
188 protected:
194 
198  std::string solver_name;
199 
200 private:
205 
210 
215 
220 
225 
230 };
231 
233 /* --------------------- Inline and template functions ------------------- */
234 
235 
236 template <typename VectorType>
238 {}
239 
240 
241 template <typename VectorType>
243 {}
244 
245 
246 template <typename VectorType>
247 void
248 SolverSelector<VectorType>::select(const std::string &name)
249 {
250  solver_name = name;
251 }
252 
253 
254 template <typename VectorType>
255 template<class Matrix, class Preconditioner>
256 void
258  VectorType &x,
259  const VectorType &b,
260  const Preconditioner &precond) const
261 {
262  if (solver_name=="richardson")
263  {
265  solver.solve(A,x,b,precond);
266  }
267  else if (solver_name=="cg")
268  {
270  solver.solve(A,x,b,precond);
271  }
272  else if (solver_name=="minres")
273  {
275  solver.solve(A,x,b,precond);
276  }
277  else if (solver_name=="bicgstab")
278  {
280  solver.solve(A,x,b,precond);
281  }
282  else if (solver_name=="gmres")
283  {
285  solver.solve(A,x,b,precond);
286  }
287  else if (solver_name=="fgmres")
288  {
290  solver.solve(A,x,b,precond);
291  }
292  else
293  Assert(false,ExcSolverDoesNotExist(solver_name));
294 }
295 
296 
297 template <typename VectorType>
299 {
300  control=&ctrl;
301 }
302 
303 
304 template <typename VectorType>
306 {
307  return "richardson|cg|bicgstab|gmres|fgmres|minres";
308 }
309 
310 
311 template <typename VectorType>
313 (const typename SolverGMRES<VectorType>::AdditionalData &data)
314 {
315  gmres_data=data;
316 }
317 
318 
319 template <typename VectorType>
321 (const typename SolverFGMRES<VectorType>::AdditionalData &data)
322 {
323  fgmres_data=data;
324 }
325 
326 
327 template <typename VectorType>
330 {
331  richardson_data=data;
332 }
333 
334 
335 template <typename VectorType>
337  const typename SolverCG<VectorType>::AdditionalData &data)
338 {
339  cg_data=data;
340 }
341 
342 
343 template <typename VectorType>
345 (const typename SolverMinRes<VectorType>::AdditionalData &data)
346 {
347  minres_data=data;
348 }
349 
350 
351 template <typename VectorType>
353 (const typename SolverBicgstab<VectorType>::AdditionalData &data)
354 {
355  bicgstab_data=data;
356 }
357 
358 
359 DEAL_II_NAMESPACE_CLOSE
360 
361 #endif
void set_control(SolverControl &ctrl)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
SolverCG< VectorType >::AdditionalData cg_data
void solve(const Matrix &A, VectorType &x, const VectorType &b, const Preconditioner &precond) const
SolverBicgstab< VectorType >::AdditionalData bicgstab_data
void set_data(const typename SolverRichardson< VectorType > ::AdditionalData &data)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
static std::string get_solver_names()
SolverGMRES< VectorType >::AdditionalData gmres_data
#define Assert(cond, exc)
Definition: exceptions.h:294
SolverRichardson< VectorType >::AdditionalData richardson_data
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
DeclException1(ExcSolverDoesNotExist, std::string,<< "Solver "<< arg1<< " does not exist. Use one of "<< std::endl<< get_solver_names())
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
std::string solver_name
void select(const std::string &name)
SolverFGMRES< VectorType >::AdditionalData fgmres_data
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &precondition)
SolverMinRes< VectorType >::AdditionalData minres_data
VectorType vector_type
SmartPointer< SolverControl, SolverSelector< VectorType > > control