Reference documentation for deal.II version GIT 9042b9283b 2023-12-02 14:50:02+00:00
\(\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 - 2022 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 
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 
119  template <class Matrix, class Preconditioner>
120  void
121  solve(const Matrix &A,
122  VectorType &x,
123  const VectorType &b,
124  const Preconditioner &precond) const;
125 
130  void
131  select(const std::string &name);
132 
136  void
137  set_control(SolverControl &ctrl);
138 
142  void
144 
148  void
149  set_data(const typename SolverCG<VectorType>::AdditionalData &data);
150 
154  void
156 
160  void
162 
166  void
168 
172  void
174 
187  static std::string
189 
194  std::string,
195  << "Solver " << arg1 << " does not exist. Use one of "
196  << std::endl
198 
199 
200 
201 protected:
207 
211  std::string solver_name;
212 
213 private:
218 
223 
228 
233 
238 
243 };
244 
246 /* --------------------- Inline and template functions ------------------- */
247 
248 
249 template <typename VectorType>
251  SolverControl &solver_control)
252  : solver_name(name)
253  , control(&solver_control)
254 {}
255 
256 
257 
258 template <typename VectorType>
259 void
260 SolverSelector<VectorType>::select(const std::string &name)
261 {
262  solver_name = name;
263 }
264 
265 
266 
267 template <typename VectorType>
268 template <class Matrix, class Preconditioner>
269 void
271  VectorType &x,
272  const VectorType &b,
273  const Preconditioner &precond) const
274 {
275  if (solver_name == "richardson")
276  {
277  SolverRichardson<VectorType> solver(*control, richardson_data);
278  solver.solve(A, x, b, precond);
279  }
280  else if (solver_name == "cg")
281  {
282  SolverCG<VectorType> solver(*control, cg_data);
283  solver.solve(A, x, b, precond);
284  }
285  else if (solver_name == "minres")
286  {
287  SolverMinRes<VectorType> solver(*control, minres_data);
288  solver.solve(A, x, b, precond);
289  }
290  else if (solver_name == "bicgstab")
291  {
292  SolverBicgstab<VectorType> solver(*control, bicgstab_data);
293  solver.solve(A, x, b, precond);
294  }
295  else if (solver_name == "gmres")
296  {
297  SolverGMRES<VectorType> solver(*control, gmres_data);
298  solver.solve(A, x, b, precond);
299  }
300  else if (solver_name == "fgmres")
301  {
302  SolverFGMRES<VectorType> solver(*control, fgmres_data);
303  solver.solve(A, x, b, precond);
304  }
305  else
306  Assert(false, ExcSolverDoesNotExist(solver_name));
307 }
308 
309 
310 
311 template <typename VectorType>
312 void
314 {
315  control = &ctrl;
316 }
317 
318 
319 
320 template <typename VectorType>
321 std::string
323 {
324  return "richardson|cg|bicgstab|gmres|fgmres|minres";
325 }
326 
327 
328 
329 template <typename VectorType>
330 void
332  const typename SolverGMRES<VectorType>::AdditionalData &data)
333 {
334  gmres_data = data;
335 }
336 
337 
338 
339 template <typename VectorType>
340 void
342  const typename SolverFGMRES<VectorType>::AdditionalData &data)
343 {
344  fgmres_data = data;
345 }
346 
347 
348 
349 template <typename VectorType>
350 void
353 {
354  richardson_data = data;
355 }
356 
357 
358 
359 template <typename VectorType>
360 void
362  const typename SolverCG<VectorType>::AdditionalData &data)
363 {
364  cg_data = data;
365 }
366 
367 
368 
369 template <typename VectorType>
370 void
372  const typename SolverMinRes<VectorType>::AdditionalData &data)
373 {
374  minres_data = data;
375 }
376 
377 
378 
379 template <typename VectorType>
380 void
382  const typename SolverBicgstab<VectorType>::AdditionalData &data)
383 {
384  bicgstab_data = data;
385 }
386 
388 
389 #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:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
#define Assert(cond, exc)
Definition: exceptions.h:1631
static ::ExceptionBase & ExcSolverDoesNotExist(std::string arg1)
#define DeclException1(Exception1, type1, outsequence)
Definition: exceptions.h:517
static const char A
SymmetricTensor< 2, dim, Number > b(const Tensor< 2, dim, Number > &F)