Reference documentation for deal.II version 9.2.0
\(\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 
90 template <typename VectorType = Vector<double>>
92 {
93 public:
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 
389 
390 #endif
SolverSelector::set_data
void set_data(const typename SolverRichardson< VectorType >::AdditionalData &data)
Definition: solver_selector.h:352
solver.h
SolverSelector::minres_data
SolverMinRes< VectorType >::AdditionalData minres_data
Definition: solver_selector.h:228
SolverCG
Definition: solver_cg.h:98
SolverCG::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverSelector
Definition: solver_selector.h:91
SolverCG::AdditionalData
Definition: solver_cg.h:111
SolverSelector::SolverSelector
SolverSelector()=default
SolverSelector::ExcSolverDoesNotExist
static ::ExceptionBase & ExcSolverDoesNotExist(std::string arg1)
VectorType
SolverMinRes::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverFGMRES::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
solver_bicgstab.h
SolverSelector::fgmres_data
SolverFGMRES< VectorType >::AdditionalData fgmres_data
Definition: solver_selector.h:243
precondition.h
Subscriptor
Definition: subscriptor.h:62
SolverSelector::gmres_data
SolverGMRES< VectorType >::AdditionalData gmres_data
Definition: solver_selector.h:238
SolverSelector::select
void select(const std::string &name)
Definition: solver_selector.h:261
SolverGMRES::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverGMRES
Definition: solver_gmres.h:178
SolverBicgstab::AdditionalData
Definition: solver_bicgstab.h:138
SolverSelector::bicgstab_data
SolverBicgstab< VectorType >::AdditionalData bicgstab_data
Definition: solver_selector.h:233
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
SolverSelector::cg_data
SolverCG< VectorType >::AdditionalData cg_data
Definition: solver_selector.h:223
SolverSelector::richardson_data
SolverRichardson< VectorType >::AdditionalData richardson_data
Definition: solver_selector.h:218
Physics::Elasticity::Kinematics::b
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)
SolverSelector::control
SmartPointer< SolverControl, SolverSelector< VectorType > > control
Definition: solver_selector.h:207
SolverSelector::solver_name
std::string solver_name
Definition: solver_selector.h:212
DeclException1
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:518
smartpointer.h
SolverMinRes::AdditionalData
Definition: solver_minres.h:79
SolverGMRES::AdditionalData
Definition: solver_gmres.h:184
SolverSelector::set_control
void set_control(SolverControl &ctrl)
Definition: solver_selector.h:314
SolverBicgstab::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
SolverSelector::get_solver_names
static std::string get_solver_names()
Definition: solver_selector.h:323
SolverBicgstab
Definition: solver_bicgstab.h:124
SolverRichardson::solve
void solve(const MatrixType &A, VectorType &x, const VectorType &b, const PreconditionerType &preconditioner)
LAPACKSupport::A
static const char A
Definition: lapack_support.h:155
SolverSelector::~SolverSelector
virtual ~SolverSelector() override=default
SmartPointer
Definition: smartpointer.h:68
solver_cg.h
SolverRichardson::AdditionalData
Definition: solver_richardson.h:69
SolverSelector::solve
void solve(const Matrix &A, VectorType &x, const VectorType &b, const Preconditioner &precond) const
Definition: solver_selector.h:271
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
vector.h
SolverMinRes
Definition: solver_minres.h:72
SolverFGMRES::AdditionalData
Definition: solver_gmres.h:470
config.h
solver_gmres.h
SolverControl
Definition: solver_control.h:67
SolverFGMRES
Definition: solver_gmres.h:464
solver_minres.h
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
SolverRichardson
Definition: solver_richardson.h:63
solver_richardson.h
solver_control.h