Reference documentation for deal.II version 8.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
solver_selector.h
1 // ---------------------------------------------------------------------
2 // @f$Id: solver_selector.h 30036 2013-07-18 16:55:32Z maier @f$
3 //
4 // Copyright (C) 1999 - 2013 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // The deal.II library is free software; you can use it, redistribute
9 // it, and/or modify it under the terms of the GNU Lesser General
10 // Public License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 // The full text of the license can be found in the file LICENSE at
13 // the top level of the deal.II distribution.
14 //
15 // ---------------------------------------------------------------------
16 
17 #ifndef __deal2__solver_selector_h
18 #define __deal2__solver_selector_h
19 
20 
21 #include <deal.II/base/config.h>
22 #include <deal.II/base/smartpointer.h>
23 #include <deal.II/lac/solver.h>
24 #include <deal.II/lac/vector.h>
25 #include <deal.II/lac/vector_memory.h>
26 #include <deal.II/lac/solver_control.h>
27 #include <deal.II/lac/solver_cg.h>
28 #include <deal.II/lac/solver_bicgstab.h>
29 #include <deal.II/lac/solver_gmres.h>
30 #include <deal.II/lac/solver_minres.h>
31 #include <deal.II/lac/vector_memory.h>
32 #include <deal.II/lac/solver_richardson.h>
33 #include <deal.II/lac/precondition.h>
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 
40 
100 template <class VECTOR = Vector<double> >
102 {
103 public:
104 
109  SolverSelector ();
110 
120  SolverSelector (const std::string &solvername,
123 
127  ~SolverSelector();
128 
136  template<class Matrix, class Preconditioner>
137  void solve(const Matrix &A,
138  VECTOR &x,
139  const VECTOR &b,
140  const Preconditioner &precond) const;
141 
147  void select(const std::string &name);
148 
153  void set_control(SolverControl &ctrl);
154 
159  void set_data(const typename SolverRichardson<VECTOR>
160  ::AdditionalData &data);
161 
166  void set_data(const typename SolverCG<VECTOR>
167  ::AdditionalData &data);
168 
173  void set_data(const typename SolverMinRes<VECTOR>
174  ::AdditionalData &data);
175 
180  void set_data(const typename SolverBicgstab<VECTOR>
181  ::AdditionalData &data);
182 
187  void set_data(const typename SolverGMRES<VECTOR>
188  ::AdditionalData &data);
189 
194  void set_data(const typename SolverFGMRES<VECTOR>
195  ::AdditionalData &data);
196 
201  static std::string get_solver_names ();
202 
206  DeclException1 (ExcSolverDoesNotExist,
207  std::string, << "Solver " << arg1 << " does not exist. Use one of "
208  << std::endl << get_solver_names());
209 
210 
211 
212 protected:
220 
224  std::string solver_name;
225 
226 private:
231 
236 
241 
246 
251 
256 };
257 
259 /* --------------------- Inline and template functions ------------------- */
260 
261 
262 template <class VECTOR>
264 {}
265 
266 
267 template <class VECTOR>
268 SolverSelector<VECTOR>::SolverSelector(const std::string &solver_name,
269  SolverControl &control,
271  control(&control),
272  solver_name(solver_name)
273 {}
274 
275 
276 template <class VECTOR>
278 {}
279 
280 
281 template <class VECTOR>
282 void
283 SolverSelector<VECTOR>::select(const std::string &name)
284 {
285  solver_name = name;
286 }
287 
288 
289 template <class VECTOR>
290 template<class Matrix, class Preconditioner>
291 void
293  VECTOR &x,
294  const VECTOR &b,
295  const Preconditioner &precond) const
296 {
297  if (solver_name=="richardson")
298  {
299  SolverRichardson<VECTOR> solver(*control, richardson_data);
300  solver.solve(A,x,b,precond);
301  }
302  else if (solver_name=="cg")
303  {
304  SolverCG<VECTOR> solver(*control, cg_data);
305  solver.solve(A,x,b,precond);
306  }
307  else if (solver_name=="minres")
308  {
309  SolverMinRes<VECTOR> solver(*control, minres_data);
310  solver.solve(A,x,b,precond);
311  }
312  else if (solver_name=="bicgstab")
313  {
314  SolverBicgstab<VECTOR> solver(*control, bicgstab_data);
315  solver.solve(A,x,b,precond);
316  }
317  else if (solver_name=="gmres")
318  {
319  SolverGMRES<VECTOR> solver(*control, gmres_data);
320  solver.solve(A,x,b,precond);
321  }
322  else if (solver_name=="fgmres")
323  {
324  SolverFGMRES<VECTOR> solver(*control, fgmres_data);
325  solver.solve(A,x,b,precond);
326  }
327  else
328  Assert(false,ExcSolverDoesNotExist(solver_name));
329 }
330 
331 
332 template <class VECTOR>
334  SolverControl &ctrl)
335 {
336  control=&ctrl;
337 }
338 
339 
340 template <class VECTOR>
342 {
343  return "richardson|cg|bicgstab|gmres|fgmres|minres";
344 }
345 
346 
347 template <class VECTOR>
349  const typename SolverGMRES<VECTOR>::AdditionalData &data)
350 {
351  gmres_data=data;
352 }
353 
354 
355 template <class VECTOR>
357  const typename SolverFGMRES<VECTOR>::AdditionalData &data)
358 {
359  fgmres_data=data;
360 }
361 
362 
363 template <class VECTOR>
365  const typename SolverRichardson<VECTOR>::AdditionalData &data)
366 {
367  richardson_data=data;
368 }
369 
370 
371 template <class VECTOR>
373  const typename SolverCG<VECTOR>::AdditionalData &data)
374 {
375  cg_data=data;
376 }
377 
378 
379 template <class VECTOR>
381  const typename SolverMinRes<VECTOR>::AdditionalData &data)
382 {
383  minres_data=data;
384 }
385 
386 
387 template <class VECTOR>
389  const typename SolverBicgstab<VECTOR>::AdditionalData &data)
390 {
391  bicgstab_data=data;
392 }
393 
394 
395 DEAL_II_NAMESPACE_CLOSE
396 
397 #endif
DeclException1(ExcSolverDoesNotExist, std::string,<< "Solver "<< arg1<< " does not exist. Use one of "<< std::endl<< get_solver_names())
void set_control(SolverControl &ctrl)
void solve(const MATRIX &A, VECTOR &x, const VECTOR &b, const PRECONDITIONER &precondition)
SolverRichardson< VECTOR >::AdditionalData richardson_data
void solve(const MATRIX &A, VECTOR &x, const VECTOR &b, const PRECONDITIONER &precondition)
void solve(const MATRIX &A, VECTOR &x, const VECTOR &b, const PRECONDITIONER &precondition)
SmartPointer< SolverControl, SolverSelector< VECTOR > > control
#define Assert(cond, exc)
Definition: exceptions.h:299
void select(const std::string &name)
SolverFGMRES< VECTOR >::AdditionalData fgmres_data
void set_data(const typename SolverRichardson< VECTOR >::AdditionalData &data)
BlockCompressedSparsityPattern CompressedBlockSparsityPattern DEAL_II_DEPRECATED
std::string solver_name
void solve(const MATRIX &A, VECTOR &x, const VECTOR &b, const PRECONDITIONER &precondition)
void solve(const MATRIX &A, VECTOR &x, const VECTOR &b, const PRECONDITIONER &precondition)
SolverCG< VECTOR >::AdditionalData cg_data
static std::string get_solver_names()
SolverGMRES< VECTOR >::AdditionalData gmres_data
SolverBicgstab< VECTOR >::AdditionalData bicgstab_data
void solve(const Matrix &A, VECTOR &x, const VECTOR &b, const Preconditioner &precond) const
void solve(const MATRIX &A, VECTOR &x, const VECTOR &b, const PRECONDITIONER &precondition)
SolverMinRes< VECTOR >::AdditionalData minres_data