Reference documentation for deal.II version 9.0.0
solver_selector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2018 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 () = default;
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>
251 void
252 SolverSelector<VectorType>::select(const std::string &name)
253 {
254  solver_name = name;
255 }
256 
257 
258 template <typename VectorType>
259 template <class Matrix, class Preconditioner>
260 void
262  VectorType &x,
263  const VectorType &b,
264  const Preconditioner &precond) const
265 {
266  if (solver_name=="richardson")
267  {
268  SolverRichardson<VectorType> solver(*control, richardson_data);
269  solver.solve(A,x,b,precond);
270  }
271  else if (solver_name=="cg")
272  {
273  SolverCG<VectorType> solver(*control, cg_data);
274  solver.solve(A,x,b,precond);
275  }
276  else if (solver_name=="minres")
277  {
278  SolverMinRes<VectorType> solver(*control, minres_data);
279  solver.solve(A,x,b,precond);
280  }
281  else if (solver_name=="bicgstab")
282  {
283  SolverBicgstab<VectorType> solver(*control, bicgstab_data);
284  solver.solve(A,x,b,precond);
285  }
286  else if (solver_name=="gmres")
287  {
288  SolverGMRES<VectorType> solver(*control, gmres_data);
289  solver.solve(A,x,b,precond);
290  }
291  else if (solver_name=="fgmres")
292  {
293  SolverFGMRES<VectorType> solver(*control, fgmres_data);
294  solver.solve(A,x,b,precond);
295  }
296  else
297  Assert(false,ExcSolverDoesNotExist(solver_name));
298 }
299 
300 
301 template <typename VectorType>
303 {
304  control=&ctrl;
305 }
306 
307 
308 template <typename VectorType>
310 {
311  return "richardson|cg|bicgstab|gmres|fgmres|minres";
312 }
313 
314 
315 template <typename VectorType>
317 (const typename SolverGMRES<VectorType>::AdditionalData &data)
318 {
319  gmres_data=data;
320 }
321 
322 
323 template <typename VectorType>
325 (const typename SolverFGMRES<VectorType>::AdditionalData &data)
326 {
327  fgmres_data=data;
328 }
329 
330 
331 template <typename VectorType>
334 {
335  richardson_data=data;
336 }
337 
338 
339 template <typename VectorType>
341  const typename SolverCG<VectorType>::AdditionalData &data)
342 {
343  cg_data=data;
344 }
345 
346 
347 template <typename VectorType>
349 (const typename SolverMinRes<VectorType>::AdditionalData &data)
350 {
351  minres_data=data;
352 }
353 
354 
355 template <typename VectorType>
357 (const typename SolverBicgstab<VectorType>::AdditionalData &data)
358 {
359  bicgstab_data=data;
360 }
361 
362 
363 DEAL_II_NAMESPACE_CLOSE
364 
365 #endif
void set_control(SolverControl &ctrl)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverCG< VectorType >::AdditionalData cg_data
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
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 &preconditioner)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
static std::string get_solver_names()
SolverGMRES< VectorType >::AdditionalData gmres_data
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:346
#define Assert(cond, exc)
Definition: exceptions.h:1142
SolverRichardson< VectorType >::AdditionalData richardson_data
SolverSelector()=default
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
std::string solver_name
void select(const std::string &name)
SolverFGMRES< VectorType >::AdditionalData fgmres_data
SolverMinRes< VectorType >::AdditionalData minres_data
VectorType vector_type
SmartPointer< SolverControl, SolverSelector< VectorType > > control