Reference documentation for deal.II version 9.3.2
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
solver_selector.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2020 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 
23 
25 #include <deal.II/lac/solver.h>
27 #include <deal.II/lac/solver_cg.h>
32 #include <deal.II/lac/vector.h>
33 
35 
36 
39 
88 template <typename VectorType = Vector<double>>
90 {
91 public:
95  using vector_type = VectorType;
96 
100  SolverSelector() = default;
101 
106  SolverSelector(const std::string &name, SolverControl &control);
107 
111  virtual ~SolverSelector() override = default;
112 
117  template <class Matrix, class Preconditioner>
118  void
119  solve(const Matrix & A,
120  VectorType & x,
121  const VectorType & b,
122  const Preconditioner &precond) const;
123 
128  void
129  select(const std::string &name);
130 
134  void
135  set_control(SolverControl &ctrl);
136 
140  void
142 
146  void
147  set_data(const typename SolverCG<VectorType>::AdditionalData &data);
148 
152  void
154 
158  void
160 
164  void
166 
170  void
172 
185  static std::string
187 
192  std::string,
193  << "Solver " << arg1 << " does not exist. Use one of "
194  << std::endl
196 
197 
198 
199 protected:
205 
209  std::string solver_name;
210 
211 private:
216 
221 
226 
231 
236 
241 };
242 
244 /* --------------------- Inline and template functions ------------------- */
245 
246 
247 template <typename VectorType>
249  SolverControl & solver_control)
250  : solver_name(name)
251  , control(&solver_control)
252 {}
253 
254 
255 
256 template <typename VectorType>
257 void
258 SolverSelector<VectorType>::select(const std::string &name)
259 {
260  solver_name = name;
261 }
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  {
275  SolverRichardson<VectorType> solver(*control, richardson_data);
276  solver.solve(A, x, b, precond);
277  }
278  else if (solver_name == "cg")
279  {
280  SolverCG<VectorType> solver(*control, cg_data);
281  solver.solve(A, x, b, precond);
282  }
283  else if (solver_name == "minres")
284  {
285  SolverMinRes<VectorType> solver(*control, minres_data);
286  solver.solve(A, x, b, precond);
287  }
288  else if (solver_name == "bicgstab")
289  {
290  SolverBicgstab<VectorType> solver(*control, bicgstab_data);
291  solver.solve(A, x, b, precond);
292  }
293  else if (solver_name == "gmres")
294  {
295  SolverGMRES<VectorType> solver(*control, gmres_data);
296  solver.solve(A, x, b, precond);
297  }
298  else if (solver_name == "fgmres")
299  {
300  SolverFGMRES<VectorType> solver(*control, fgmres_data);
301  solver.solve(A, x, b, precond);
302  }
303  else
304  Assert(false, ExcSolverDoesNotExist(solver_name));
305 }
306 
307 
308 
309 template <typename VectorType>
310 void
312 {
313  control = &ctrl;
314 }
315 
316 
317 
318 template <typename VectorType>
319 std::string
321 {
322  return "richardson|cg|bicgstab|gmres|fgmres|minres";
323 }
324 
325 
326 
327 template <typename VectorType>
328 void
330  const typename SolverGMRES<VectorType>::AdditionalData &data)
331 {
332  gmres_data = data;
333 }
334 
335 
336 
337 template <typename VectorType>
338 void
340  const typename SolverFGMRES<VectorType>::AdditionalData &data)
341 {
342  fgmres_data = data;
343 }
344 
345 
346 
347 template <typename VectorType>
348 void
351 {
352  richardson_data = data;
353 }
354 
355 
356 
357 template <typename VectorType>
358 void
360  const typename SolverCG<VectorType>::AdditionalData &data)
361 {
362  cg_data = data;
363 }
364 
365 
366 
367 template <typename VectorType>
368 void
370  const typename SolverMinRes<VectorType>::AdditionalData &data)
371 {
372  minres_data = data;
373 }
374 
375 
376 
377 template <typename VectorType>
378 void
380  const typename SolverBicgstab<VectorType>::AdditionalData &data)
381 {
382  bicgstab_data = data;
383 }
384 
386 
387 #endif
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)
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)
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)
SolverGMRES< VectorType >::AdditionalData gmres_data
VectorType vector_type
virtual ~SolverSelector() override=default
SolverBicgstab< VectorType >::AdditionalData bicgstab_data
void select(const std::string &name)
static std::string get_solver_names()
void set_control(SolverControl &ctrl)
SolverFGMRES< VectorType >::AdditionalData fgmres_data
SolverMinRes< VectorType >::AdditionalData minres_data
void solve(const Matrix &A, VectorType &x, const VectorType &b, const Preconditioner &precond) const
void set_data(const typename SolverRichardson< VectorType >::AdditionalData &data)
SmartPointer< SolverControl, SolverSelector< VectorType > > control
std::string solver_name
SolverSelector()=default
SolverCG< VectorType >::AdditionalData cg_data
SolverRichardson< VectorType >::AdditionalData richardson_data
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:396
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:397
#define Assert(cond, exc)
Definition: exceptions.h:1465
static ::ExceptionBase & ExcSolverDoesNotExist(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:515
static const char A
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)