Reference documentation for deal.II version 9.1.0-pre
precondition_selector.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1999 - 2017 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_precondition_selector_h
17 #define dealii_precondition_selector_h
18 
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/smartpointer.h>
23 
24 #include <string>
25 
26 DEAL_II_NAMESPACE_OPEN
27 
28 template <class number>
29 class Vector;
30 template <class number>
31 class SparseMatrix;
32 
33 
97 template <typename MatrixType = SparseMatrix<double>,
98  typename VectorType = ::Vector<double>>
100 {
101 public:
105  using size_type = typename MatrixType::size_type;
106 
111  PreconditionSelector(const std::string & preconditioning,
112  const typename VectorType::value_type &omega = 1.);
113 
117  virtual ~PreconditionSelector() override;
118 
123  void
124  use_matrix(const MatrixType &M);
125 
130  size_type
131  m() const;
132 
137  size_type
138  n() const;
139 
144  virtual void
145  vmult(VectorType &dst, const VectorType &src) const;
146 
151  virtual void
152  Tvmult(VectorType &dst, const VectorType &src) const;
153 
164  static std::string
166 
177 
179 protected:
183  std::string preconditioning;
184 
185 private:
191  A;
192 
196  const typename VectorType::value_type omega;
197 };
198 
200 /* --------------------- Inline and template functions ------------------- */
201 
202 
203 template <typename MatrixType, typename VectorType>
205  const std::string & preconditioning,
206  const typename VectorType::value_type &omega)
207  : preconditioning(preconditioning)
208  , omega(omega)
209 {}
210 
211 
212 template <typename MatrixType, typename VectorType>
214 {
215  // release the matrix A
216  A = nullptr;
217 }
218 
219 
220 template <typename MatrixType, typename VectorType>
221 void
223 {
224  A = &M;
225 }
226 
227 
228 template <typename MatrixType, typename VectorType>
231 {
232  Assert(A != nullptr, ExcNoMatrixGivenToUse());
233  return A->m();
234 }
235 
236 
237 template <typename MatrixType, typename VectorType>
240 {
241  Assert(A != nullptr, ExcNoMatrixGivenToUse());
242  return A->n();
243 }
244 
245 
246 
247 template <typename MatrixType, typename VectorType>
248 void
250  const VectorType &src) const
251 {
252  if (preconditioning == "none")
253  {
254  dst = src;
255  }
256  else
257  {
258  Assert(A != nullptr, ExcNoMatrixGivenToUse());
259 
260  if (preconditioning == "jacobi")
261  {
262  A->precondition_Jacobi(dst, src, omega);
263  }
264  else if (preconditioning == "sor")
265  {
266  A->precondition_SOR(dst, src, omega);
267  }
268  else if (preconditioning == "ssor")
269  {
270  A->precondition_SSOR(dst, src, omega);
271  }
272  else
273  Assert(false, ExcNotImplemented());
274  }
275 }
276 
277 
278 template <typename MatrixType, typename VectorType>
279 void
281  VectorType & dst,
282  const VectorType &src) const
283 {
284  if (preconditioning == "none")
285  {
286  dst = src;
287  }
288  else
289  {
290  Assert(A != nullptr, ExcNoMatrixGivenToUse());
291 
292  if (preconditioning == "jacobi")
293  {
294  A->precondition_Jacobi(dst, src, omega); // Symmetric operation
295  }
296  else if (preconditioning == "sor")
297  {
298  A->precondition_TSOR(dst, src, omega);
299  }
300  else if (preconditioning == "ssor")
301  {
302  A->precondition_SSOR(dst, src, omega); // Symmetric operation
303  }
304  else
305  Assert(false, ExcNotImplemented());
306  }
307 }
308 
309 
310 template <typename MatrixType, typename VectorType>
311 std::string
313 {
314  return "none|jacobi|sor|ssor";
315 }
316 
317 
318 DEAL_II_NAMESPACE_CLOSE
319 
320 #endif
virtual void vmult(VectorType &dst, const VectorType &src) const
SmartPointer< const MatrixType, PreconditionSelector< MatrixType, VectorType > > A
void use_matrix(const MatrixType &M)
virtual void Tvmult(VectorType &dst, const VectorType &src) const
const VectorType::value_type omega
static::ExceptionBase & ExcNoMatrixGivenToUse()
#define Assert(cond, exc)
Definition: exceptions.h:1227
#define DeclException0(Exception0)
Definition: exceptions.h:385
PreconditionSelector(const std::string &preconditioning, const typename VectorType::value_type &omega=1.)
virtual ~PreconditionSelector() override
static std::string get_precondition_names()
typename MatrixType::size_type size_type
static::ExceptionBase & ExcNotImplemented()