Reference documentation for deal.II version 8.5.1
solver_selector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2016 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 
186  static std::string get_solver_names ();
187 
192  std::string, << "Solver " << arg1 << " does not exist. Use one of "
193  << std::endl << get_solver_names());
194 
195 
196 
197 protected:
203 
207  std::string solver_name;
208 
209 private:
214 
219 
224 
229 
234 
239 };
240 
242 /* --------------------- Inline and template functions ------------------- */
243 
244 
245 template <typename VectorType>
247 {}
248 
249 
250 template <typename VectorType>
252 {}
253 
254 
255 template <typename VectorType>
256 void
257 SolverSelector<VectorType>::select(const std::string &name)
258 {
259  solver_name = name;
260 }
261 
262 
263 template <typename VectorType>
264 template<class Matrix, class Preconditioner>
265 void
267  VectorType &x,
268  const VectorType &b,
269  const Preconditioner &precond) const
270 {
271  if (solver_name=="richardson")
272  {
273  SolverRichardson<VectorType> solver(*control, richardson_data);
274  solver.solve(A,x,b,precond);
275  }
276  else if (solver_name=="cg")
277  {
278  SolverCG<VectorType> solver(*control, cg_data);
279  solver.solve(A,x,b,precond);
280  }
281  else if (solver_name=="minres")
282  {
283  SolverMinRes<VectorType> solver(*control, minres_data);
284  solver.solve(A,x,b,precond);
285  }
286  else if (solver_name=="bicgstab")
287  {
288  SolverBicgstab<VectorType> solver(*control, bicgstab_data);
289  solver.solve(A,x,b,precond);
290  }
291  else if (solver_name=="gmres")
292  {
293  SolverGMRES<VectorType> solver(*control, gmres_data);
294  solver.solve(A,x,b,precond);
295  }
296  else if (solver_name=="fgmres")
297  {
298  SolverFGMRES<VectorType> solver(*control, fgmres_data);
299  solver.solve(A,x,b,precond);
300  }
301  else
302  Assert(false,ExcSolverDoesNotExist(solver_name));
303 }
304 
305 
306 template <typename VectorType>
308 {
309  control=&ctrl;
310 }
311 
312 
313 template <typename VectorType>
315 {
316  return "richardson|cg|bicgstab|gmres|fgmres|minres";
317 }
318 
319 
320 template <typename VectorType>
322 (const typename SolverGMRES<VectorType>::AdditionalData &data)
323 {
324  gmres_data=data;
325 }
326 
327 
328 template <typename VectorType>
330 (const typename SolverFGMRES<VectorType>::AdditionalData &data)
331 {
332  fgmres_data=data;
333 }
334 
335 
336 template <typename VectorType>
339 {
340  richardson_data=data;
341 }
342 
343 
344 template <typename VectorType>
346  const typename SolverCG<VectorType>::AdditionalData &data)
347 {
348  cg_data=data;
349 }
350 
351 
352 template <typename VectorType>
354 (const typename SolverMinRes<VectorType>::AdditionalData &data)
355 {
356  minres_data=data;
357 }
358 
359 
360 template <typename VectorType>
362 (const typename SolverBicgstab<VectorType>::AdditionalData &data)
363 {
364  bicgstab_data=data;
365 }
366 
367 
368 DEAL_II_NAMESPACE_CLOSE
369 
370 #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
static ::ExceptionBase & ExcSolverDoesNotExist(std::string arg1)
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 DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:564
#define Assert(cond, exc)
Definition: exceptions.h:313
SolverRichardson< VectorType >::AdditionalData richardson_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)
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