Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
solver_selector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2019 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 
34 DEAL_II_NAMESPACE_OPEN
35 
36 
39 
90 template <typename VectorType = Vector<double>>
92 {
93 public:
97  using vector_type = VectorType;
98 
102  SolverSelector() = default;
103 
108  SolverSelector(const std::string &name, SolverControl &control);
109 
113  virtual ~SolverSelector() override = default;
114 
120  template <class Matrix, class Preconditioner>
121  void
122  solve(const Matrix & A,
123  VectorType & x,
124  const VectorType & b,
125  const Preconditioner &precond) const;
126 
131  void
132  select(const std::string &name);
133 
137  void
138  set_control(SolverControl &ctrl);
139 
143  void
145 
149  void
150  set_data(const typename SolverCG<VectorType>::AdditionalData &data);
151 
155  void
157 
161  void
163 
167  void
169 
173  void
175 
188  static std::string
190 
195  std::string,
196  << "Solver " << arg1 << " does not exist. Use one of "
197  << std::endl
198  << get_solver_names());
199 
200 
201 
202 protected:
208 
212  std::string solver_name;
213 
214 private:
219 
224 
229 
234 
239 
244 };
245 
247 /* --------------------- Inline and template functions ------------------- */
248 
249 
250 template <typename VectorType>
252  SolverControl & solver_control)
253  : solver_name(name)
254  , control(&solver_control)
255 {}
256 
257 
258 
259 template <typename VectorType>
260 void
261 SolverSelector<VectorType>::select(const std::string &name)
262 {
263  solver_name = name;
264 }
265 
266 
267 
268 template <typename VectorType>
269 template <class Matrix, class Preconditioner>
270 void
272  VectorType & x,
273  const VectorType & b,
274  const Preconditioner &precond) const
275 {
276  if (solver_name == "richardson")
277  {
278  SolverRichardson<VectorType> solver(*control, richardson_data);
279  solver.solve(A, x, b, precond);
280  }
281  else if (solver_name == "cg")
282  {
283  SolverCG<VectorType> solver(*control, cg_data);
284  solver.solve(A, x, b, precond);
285  }
286  else if (solver_name == "minres")
287  {
288  SolverMinRes<VectorType> solver(*control, minres_data);
289  solver.solve(A, x, b, precond);
290  }
291  else if (solver_name == "bicgstab")
292  {
293  SolverBicgstab<VectorType> solver(*control, bicgstab_data);
294  solver.solve(A, x, b, precond);
295  }
296  else if (solver_name == "gmres")
297  {
298  SolverGMRES<VectorType> solver(*control, gmres_data);
299  solver.solve(A, x, b, precond);
300  }
301  else if (solver_name == "fgmres")
302  {
303  SolverFGMRES<VectorType> solver(*control, fgmres_data);
304  solver.solve(A, x, b, precond);
305  }
306  else
307  Assert(false, ExcSolverDoesNotExist(solver_name));
308 }
309 
310 
311 
312 template <typename VectorType>
313 void
315 {
316  control = &ctrl;
317 }
318 
319 
320 
321 template <typename VectorType>
322 std::string
324 {
325  return "richardson|cg|bicgstab|gmres|fgmres|minres";
326 }
327 
328 
329 
330 template <typename VectorType>
331 void
333  const typename SolverGMRES<VectorType>::AdditionalData &data)
334 {
335  gmres_data = data;
336 }
337 
338 
339 
340 template <typename VectorType>
341 void
343  const typename SolverFGMRES<VectorType>::AdditionalData &data)
344 {
345  fgmres_data = data;
346 }
347 
348 
349 
350 template <typename VectorType>
351 void
354 {
355  richardson_data = data;
356 }
357 
358 
359 
360 template <typename VectorType>
361 void
363  const typename SolverCG<VectorType>::AdditionalData &data)
364 {
365  cg_data = data;
366 }
367 
368 
369 
370 template <typename VectorType>
371 void
373  const typename SolverMinRes<VectorType>::AdditionalData &data)
374 {
375  minres_data = data;
376 }
377 
378 
379 
380 template <typename VectorType>
381 void
383  const typename SolverBicgstab<VectorType>::AdditionalData &data)
384 {
385  bicgstab_data = data;
386 }
387 
388 DEAL_II_NAMESPACE_CLOSE
389 
390 #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
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()
virtual ~SolverSelector() override=default
SolverGMRES< VectorType >::AdditionalData gmres_data
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:518
#define Assert(cond, exc)
Definition: exceptions.h:1407
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
std::string solver_name
void select(const std::string &name)
SolverFGMRES< VectorType >::AdditionalData fgmres_data
SolverMinRes< VectorType >::AdditionalData minres_data