linear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2018 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_LINEAR_SOLVER_H
21 #define LIBMESH_LINEAR_SOLVER_H
22 
23 // Local includes
24 #include "libmesh/libmesh_common.h"
25 #include "libmesh/enum_subset_solve_mode.h" // SUBSET_ZERO
27 #include "libmesh/libmesh.h"
29 #include "libmesh/auto_ptr.h" // deprecated
30 
31 #ifdef LIBMESH_FORWARD_DECLARE_ENUMS
32 namespace libMesh
33 {
34 enum SolverPackage : int;
35 enum PreconditionerType : int;
36 enum SolverType : int;
37 enum LinearConvergenceReason : int;
38 }
39 #else
44 #endif
45 
46 // C++ includes
47 #include <cstddef>
48 #include <vector>
49 #include <memory>
50 
51 namespace libMesh
52 {
53 
54 // forward declarations
55 template <typename T> class SparseMatrix;
56 template <typename T> class NumericVector;
57 template <typename T> class ShellMatrix;
58 template <typename T> class Preconditioner;
59 class System;
61 
69 template <typename T>
70 class LinearSolver : public ReferenceCountedObject<LinearSolver<T>>,
71  public ParallelObject
72 {
73 public:
74 
79 
83  virtual ~LinearSolver ();
84 
89  static std::unique_ptr<LinearSolver<T>> build(const libMesh::Parallel::Communicator & comm_in,
90  const SolverPackage solver_package = libMesh::default_solver_package());
91 
96  bool initialized () const { return _is_initialized; }
97 
101  virtual void clear () {}
102 
107  virtual void init (const char * name = nullptr) = 0;
108 
117  virtual void init_names (const System &) {}
118 
122  SolverType solver_type () const { return _solver_type; }
123 
127  void set_solver_type (const SolverType st)
128  { _solver_type = st; }
129 
134 
139 
143  void attach_preconditioner(Preconditioner<T> * preconditioner);
144 
149  virtual void reuse_preconditioner(bool );
150 
156 
164  virtual void restrict_solve_to (const std::vector<unsigned int> * const dofs,
165  const SubsetSolveMode subset_solve_mode=SUBSET_ZERO);
166 
174  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Matrix
175  NumericVector<T> &, // Solution vector
176  NumericVector<T> &, // RHS vector
177  const double, // Stopping tolerance
178  const unsigned int) = 0; // N. Iterations
179 
187  virtual std::pair<unsigned int, Real> adjoint_solve (SparseMatrix<T> &, // System Matrix
188  NumericVector<T> &, // Solution vector
189  NumericVector<T> &, // RHS vector
190  const double, // Stopping tolerance
191  const unsigned int); // N. Iterations
192 
198  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Matrix
199  SparseMatrix<T> &, // Preconditioning Matrix
200  NumericVector<T> &, // Solution vector
201  NumericVector<T> &, // RHS vector
202  const double, // Stopping tolerance
203  const unsigned int) = 0; // N. Iterations
204 
211  std::pair<unsigned int, Real> solve (SparseMatrix<T> & matrix,
212  SparseMatrix<T> * precond_matrix,
213  NumericVector<T> &, // Solution vector
214  NumericVector<T> &, // RHS vector
215  const double, // Stopping tolerance
216  const unsigned int); // N. Iterations
217 
218 
219 
223  virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T> & shell_matrix,
224  NumericVector<T> &, // Solution vector
225  NumericVector<T> &, // RHS vector
226  const double, // Stopping tolerance
227  const unsigned int) = 0; // N. Iterations
228 
229 
235  virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T> & shell_matrix,
236  const SparseMatrix<T> & precond_matrix,
237  NumericVector<T> &, // Solution vector
238  NumericVector<T> &, // RHS vector
239  const double, // Stopping tolerance
240  const unsigned int) = 0; // N. Iterations
241 
242 
247  std::pair<unsigned int, Real> solve (const ShellMatrix<T> & matrix,
248  const SparseMatrix<T> * precond_matrix,
249  NumericVector<T> &, // Solution vector
250  NumericVector<T> &, // RHS vector
251  const double, // Stopping tolerance
252  const unsigned int); // N. Iterations
253 
254 
259  virtual void print_converged_reason() const;
260 
264  virtual LinearConvergenceReason get_converged_reason() const = 0;
265 
269  void set_solver_configuration(SolverConfiguration & solver_configuration);
270 
271 protected:
272 
273 
278 
283 
288 
293 
301 
307 };
308 
309 
310 
311 
312 /*----------------------- inline functions ----------------------------------*/
313 template <typename T>
314 inline
316 {
317  this->clear ();
318 }
319 
320 template <typename T>
321 inline
323 {
324  return same_preconditioner;
325 }
326 
327 template <typename T>
328 inline
329 std::pair<unsigned int, Real>
331  SparseMatrix<T> * pc_mat,
332  NumericVector<T> & sol,
333  NumericVector<T> & rhs,
334  const double tol,
335  const unsigned int n_iter)
336 {
337  if (pc_mat)
338  return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
339  else
340  return this->solve(mat, sol, rhs, tol, n_iter);
341 }
342 
343 
344 template <typename T>
345 inline
346 std::pair<unsigned int, Real>
348  const SparseMatrix<T> * pc_mat,
349  NumericVector<T> & sol,
350  NumericVector<T> & rhs,
351  const double tol,
352  const unsigned int n_iter)
353 {
354  if (pc_mat)
355  return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
356  else
357  return this->solve(mat, sol, rhs, tol, n_iter);
358 }
359 
360 } // namespace libMesh
361 
362 
363 #endif // LIBMESH_LINEAR_SOLVER_H
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
virtual void clear()
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
Definition: linear_solver.C:57
Preconditioner< T > * _preconditioner
virtual void print_converged_reason() const
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: diff_context.h:40
SolverPackage default_solver_package()
Definition: libmesh.C:971
LinearSolver(const libMesh::Parallel::Communicator &comm_in)
Definition: linear_solver.C:42
virtual void init(const char *name=nullptr)=0
bool initialized() const
Definition: linear_solver.h:96
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:92
SolverConfiguration * _solver_configuration
void set_preconditioner_type(const PreconditionerType pct)
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
void set_solver_type(const SolverType st)
An object whose state is distributed along a set of processors.
void attach_preconditioner(Preconditioner< T > *preconditioner)
void set_solver_configuration(SolverConfiguration &solver_configuration)
PreconditionerType preconditioner_type() const
Definition: linear_solver.C:98
virtual LinearConvergenceReason get_converged_reason() const =0
virtual void reuse_preconditioner(bool)
PreconditionerType _preconditioner_type
virtual void restrict_solve_to(const std::vector< unsigned int > *const dofs, const SubsetSolveMode subset_solve_mode=SUBSET_ZERO)
SolverType solver_type() const
virtual void init_names(const System &)