Reference documentation for deal.II version 9.1.0-pre
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.md at
12 // the top level directory of deal.II.
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 
22 #include <deal.II/base/smartpointer.h>
23 
24 #include <deal.II/lac/precondition.h>
25 #include <deal.II/lac/solver.h>
26 #include <deal.II/lac/solver_bicgstab.h>
27 #include <deal.II/lac/solver_cg.h>
28 #include <deal.II/lac/solver_control.h>
29 #include <deal.II/lac/solver_gmres.h>
30 #include <deal.II/lac/solver_minres.h>
31 #include <deal.II/lac/solver_richardson.h>
32 #include <deal.II/lac/vector.h>
33 #include <deal.II/lac/vector_memory.h>
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 
40 
98 template <typename VectorType = Vector<double>>
100 {
101 public:
105  using vector_type = VectorType;
106 
110  SolverSelector() = default;
111 
115  ~SolverSelector() override;
116 
122  template <class Matrix, class Preconditioner>
123  void
124  solve(const Matrix & A,
125  VectorType & x,
126  const VectorType & b,
127  const Preconditioner &precond) const;
128 
133  void
134  select(const std::string &name);
135 
139  void
140  set_control(SolverControl &ctrl);
141 
145  void
147 
151  void
152  set_data(const typename SolverCG<VectorType>::AdditionalData &data);
153 
157  void
159 
163  void
165 
169  void
171 
175  void
177 
190  static std::string
192 
197  std::string,
198  << "Solver " << arg1 << " does not exist. Use one of "
199  << std::endl
200  << get_solver_names());
201 
202 
203 
204 protected:
210 
214  std::string solver_name;
215 
216 private:
221 
226 
231 
236 
241 
246 };
247 
249 /* --------------------- Inline and template functions ------------------- */
250 
251 
252 template <typename VectorType>
254 {}
255 
256 
257 template <typename VectorType>
258 void
259 SolverSelector<VectorType>::select(const std::string &name)
260 {
261  solver_name = name;
262 }
263 
264 
265 template <typename VectorType>
266 template <class Matrix, class Preconditioner>
267 void
269  VectorType & x,
270  const VectorType & b,
271  const Preconditioner &precond) const
272 {
273  if (solver_name == "richardson")
274  {
276  solver.solve(A, x, b, precond);
277  }
278  else if (solver_name == "cg")
279  {
281  solver.solve(A, x, b, precond);
282  }
283  else if (solver_name == "minres")
284  {
286  solver.solve(A, x, b, precond);
287  }
288  else if (solver_name == "bicgstab")
289  {
291  solver.solve(A, x, b, precond);
292  }
293  else if (solver_name == "gmres")
294  {
296  solver.solve(A, x, b, precond);
297  }
298  else if (solver_name == "fgmres")
299  {
301  solver.solve(A, x, b, precond);
302  }
303  else
305 }
306 
307 
308 template <typename VectorType>
309 void
311 {
312  control = &ctrl;
313 }
314 
315 
316 template <typename VectorType>
317 std::string
319 {
320  return "richardson|cg|bicgstab|gmres|fgmres|minres";
321 }
322 
323 
324 template <typename VectorType>
325 void
327  const typename SolverGMRES<VectorType>::AdditionalData &data)
328 {
329  gmres_data = data;
330 }
331 
332 
333 template <typename VectorType>
334 void
336  const typename SolverFGMRES<VectorType>::AdditionalData &data)
337 {
338  fgmres_data = data;
339 }
340 
341 
342 template <typename VectorType>
343 void
346 {
347  richardson_data = data;
348 }
349 
350 
351 template <typename VectorType>
352 void
354  const typename SolverCG<VectorType>::AdditionalData &data)
355 {
356  cg_data = data;
357 }
358 
359 
360 template <typename VectorType>
361 void
363  const typename SolverMinRes<VectorType>::AdditionalData &data)
364 {
365  minres_data = data;
366 }
367 
368 
369 template <typename VectorType>
370 void
372  const typename SolverBicgstab<VectorType>::AdditionalData &data)
373 {
374  bicgstab_data = data;
375 }
376 
377 
378 DEAL_II_NAMESPACE_CLOSE
379 
380 #endif
void set_control(SolverControl &ctrl)
void solve(const Matrix &A, VectorType &x, const VectorType &b, const Preconditioner &precond) const
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)
static::ExceptionBase & ExcSolverDoesNotExist(std::string arg1)
SolverBicgstab< VectorType >::AdditionalData bicgstab_data
SmartPointer< SolverControl, SolverSelector< VectorType > > control
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:408
#define Assert(cond, exc)
Definition: exceptions.h:1227
SolverRichardson< VectorType >::AdditionalData richardson_data
SolverSelector()=default
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
void set_data(const typename SolverRichardson< VectorType >::AdditionalData &data)
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
VectorType vector_type
~SolverSelector() override
std::string solver_name
void select(const std::string &name)
SolverFGMRES< VectorType >::AdditionalData fgmres_data
SolverMinRes< VectorType >::AdditionalData minres_data