libMesh
linear_solver.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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
26 #include "libmesh/reference_counted_object.h"
27 #include "libmesh/libmesh.h"
28 #include "libmesh/parallel_object.h"
29 
30 #ifdef LIBMESH_FORWARD_DECLARE_ENUMS
31 namespace libMesh
32 {
33 enum SolverPackage : int;
34 enum PreconditionerType : int;
35 enum SolverType : int;
37 }
38 #else
39 #include "libmesh/enum_solver_package.h"
40 #include "libmesh/enum_preconditioner_type.h"
41 #include "libmesh/enum_solver_type.h"
42 #include "libmesh/enum_convergence_flags.h"
43 #endif
44 
45 // C++ includes
46 #include <cstddef>
47 #include <vector>
48 #include <memory>
49 
50 namespace libMesh
51 {
52 
53 // forward declarations
54 template <typename T> class SparseMatrix;
55 template <typename T> class NumericVector;
56 template <typename T> class ShellMatrix;
57 template <typename T> class Preconditioner;
58 class System;
59 class SolverConfiguration;
60 
68 template <typename T>
69 class LinearSolver : public ReferenceCountedObject<LinearSolver<T>>,
70  public ParallelObject
71 {
72 public:
73 
77  LinearSolver (const libMesh::Parallel::Communicator & comm_in);
78 
82  virtual ~LinearSolver ();
83 
88  static std::unique_ptr<LinearSolver<T>> build(const libMesh::Parallel::Communicator & comm_in,
89  const SolverPackage solver_package = libMesh::default_solver_package());
90 
95  bool initialized () const { return _is_initialized; }
96 
100  virtual void clear () {}
101 
106  virtual void init (const char * name = nullptr) = 0;
107 
116  virtual void init_names (const System &) {}
117 
121  SolverType solver_type () const { return _solver_type; }
122 
126  void set_solver_type (const SolverType st)
127  { _solver_type = st; }
128 
133 
138 
142  void attach_preconditioner(Preconditioner<T> * preconditioner);
143 
148  virtual void reuse_preconditioner(bool );
149 
155 
163  virtual void restrict_solve_to (const std::vector<unsigned int> * const dofs,
164  const SubsetSolveMode subset_solve_mode=SUBSET_ZERO);
165 
173  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Matrix
174  NumericVector<T> &, // Solution vector
175  NumericVector<T> &, // RHS vector
176  const double, // Stopping tolerance
177  const unsigned int) = 0; // N. Iterations
178 
186  virtual std::pair<unsigned int, Real> adjoint_solve (SparseMatrix<T> &, // System Matrix
187  NumericVector<T> &, // Solution vector
188  NumericVector<T> &, // RHS vector
189  const double, // Stopping tolerance
190  const unsigned int); // N. Iterations
191 
197  virtual std::pair<unsigned int, Real> solve (SparseMatrix<T> &, // System Matrix
198  SparseMatrix<T> &, // Preconditioning Matrix
199  NumericVector<T> &, // Solution vector
200  NumericVector<T> &, // RHS vector
201  const double, // Stopping tolerance
202  const unsigned int) = 0; // N. Iterations
203 
210  std::pair<unsigned int, Real> solve (SparseMatrix<T> & matrix,
211  SparseMatrix<T> * precond_matrix,
212  NumericVector<T> &, // Solution vector
213  NumericVector<T> &, // RHS vector
214  const double, // Stopping tolerance
215  const unsigned int); // N. Iterations
216 
217 
218 
222  virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T> & shell_matrix,
223  NumericVector<T> &, // Solution vector
224  NumericVector<T> &, // RHS vector
225  const double, // Stopping tolerance
226  const unsigned int) = 0; // N. Iterations
227 
228 
234  virtual std::pair<unsigned int, Real> solve (const ShellMatrix<T> & shell_matrix,
235  const SparseMatrix<T> & precond_matrix,
236  NumericVector<T> &, // Solution vector
237  NumericVector<T> &, // RHS vector
238  const double, // Stopping tolerance
239  const unsigned int) = 0; // N. Iterations
240 
241 
246  std::pair<unsigned int, Real> solve (const ShellMatrix<T> & matrix,
247  const SparseMatrix<T> * precond_matrix,
248  NumericVector<T> &, // Solution vector
249  NumericVector<T> &, // RHS vector
250  const double, // Stopping tolerance
251  const unsigned int); // N. Iterations
252 
253 
258  virtual void print_converged_reason() const;
259 
263  virtual LinearConvergenceReason get_converged_reason() const = 0;
264 
268  void set_solver_configuration(SolverConfiguration & solver_configuration);
269 
270 protected:
271 
272 
277 
282 
287 
292 
300 
306 };
307 
308 
309 
310 
311 /*----------------------- inline functions ----------------------------------*/
312 template <typename T>
313 inline
315 {
316  this->clear ();
317 }
318 
319 template <typename T>
320 inline
322 {
323  return same_preconditioner;
324 }
325 
326 template <typename T>
327 inline
328 std::pair<unsigned int, Real>
330  SparseMatrix<T> * pc_mat,
331  NumericVector<T> & sol,
332  NumericVector<T> & rhs,
333  const double tol,
334  const unsigned int n_iter)
335 {
336  if (pc_mat)
337  return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
338  else
339  return this->solve(mat, sol, rhs, tol, n_iter);
340 }
341 
342 
343 template <typename T>
344 inline
345 std::pair<unsigned int, Real>
347  const SparseMatrix<T> * pc_mat,
348  NumericVector<T> & sol,
349  NumericVector<T> & rhs,
350  const double tol,
351  const unsigned int n_iter)
352 {
353  if (pc_mat)
354  return this->solve(mat, *pc_mat, sol, rhs, tol, n_iter);
355  else
356  return this->solve(mat, sol, rhs, tol, n_iter);
357 }
358 
359 } // namespace libMesh
360 
361 
362 #endif // LIBMESH_LINEAR_SOLVER_H
libMesh::LinearSolver::reuse_preconditioner
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
Definition: linear_solver.C:129
libMesh::ShellMatrix
Generic shell matrix, i.e.
Definition: eigen_preconditioner.h:36
libMesh::System
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:100
libMesh::SolverPackage
SolverPackage
Defines an enum for various linear solver packages.
Definition: enum_solver_package.h:34
libMesh::LinearSolver::get_converged_reason
virtual LinearConvergenceReason get_converged_reason() const =0
libMesh::LinearSolver::set_solver_configuration
void set_solver_configuration(SolverConfiguration &solver_configuration)
Set the solver configuration object.
Definition: linear_solver.C:177
libMesh::LinearSolver::restrict_solve_to
virtual void restrict_solve_to(const std::vector< unsigned int > *const dofs, const SubsetSolveMode subset_solve_mode=SUBSET_ZERO)
After calling this method, all successive solves will be restricted to the given set of dofs,...
Definition: linear_solver.C:136
libMesh::LinearSolver::LinearSolver
LinearSolver(const libMesh::Parallel::Communicator &comm_in)
Constructor.
Definition: linear_solver.C:42
libMesh::LinearSolver::solve
virtual std::pair< unsigned int, Real > solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)=0
This function calls the solver _solver_type preconditioned with the _preconditioner_type precondition...
libMesh::ReferenceCountedObject
This class implements reference counting.
Definition: reference_counted_object.h:65
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::LinearSolver::_solver_type
SolverType _solver_type
Enum stating which type of iterative solver to use.
Definition: linear_solver.h:276
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
libMesh::LinearSolver::init_names
virtual void init_names(const System &)
Apply names to the system to be solved.
Definition: linear_solver.h:116
libMesh::LinearSolver::init
virtual void init(const char *name=nullptr)=0
Initialize data structures if not done so already.
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::LinearSolver::_preconditioner_type
PreconditionerType _preconditioner_type
Enum stating with type of preconditioner to use.
Definition: linear_solver.h:281
libMesh::SUBSET_ZERO
Definition: enum_subset_solve_mode.h:37
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::LinearSolver::build
static std::unique_ptr< LinearSolver< T > > build(const libMesh::Parallel::Communicator &comm_in, const SolverPackage solver_package=libMesh::default_solver_package())
Builds a LinearSolver using the linear solver package specified by solver_package.
Definition: linear_solver.C:57
libMesh::LinearConvergenceReason
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).
Definition: enum_convergence_flags.h:33
libMesh::LinearSolver::set_solver_type
void set_solver_type(const SolverType st)
Sets the type of solver to use.
Definition: linear_solver.h:126
libMesh::LinearSolver::clear
virtual void clear()
Release all memory and clear data structures.
Definition: linear_solver.h:100
libMesh::LinearSolver::_preconditioner
Preconditioner< T > * _preconditioner
Holds the Preconditioner object to be used for the linear solves.
Definition: linear_solver.h:291
libMesh::Preconditioner
This class provides a uniform interface for preconditioners.
Definition: preconditioner.h:66
libMesh::LinearSolver::~LinearSolver
virtual ~LinearSolver()
Destructor.
Definition: linear_solver.h:314
libMesh::LinearSolver::solver_type
SolverType solver_type() const
Definition: linear_solver.h:121
libMesh::PreconditionerType
PreconditionerType
Defines an enum for preconditioner types.
Definition: enum_preconditioner_type.h:33
libMesh::LinearSolver::print_converged_reason
virtual void print_converged_reason() const
Prints a useful message about why the latest linear solve con(di)verged.
Definition: linear_solver.C:170
libMesh::LinearSolver::_is_initialized
bool _is_initialized
Flag indicating if the data structures have been initialized.
Definition: linear_solver.h:286
libMesh::LinearSolver::adjoint_solve
virtual std::pair< unsigned int, Real > adjoint_solve(SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
Function to solve the adjoint system.
Definition: linear_solver.C:145
libMesh::LinearSolver::same_preconditioner
bool same_preconditioner
Boolean flag to indicate whether we want to use an identical preconditioner to the previous solve.
Definition: linear_solver.h:299
libMesh::LinearSolver::get_same_preconditioner
bool get_same_preconditioner()
Definition: linear_solver.h:321
libMesh::LinearSolver::initialized
bool initialized() const
Definition: linear_solver.h:95
libMesh::LinearSolver
This base class can be inherited from to provide interfaces to linear solvers from different packages...
Definition: linear_solver.h:69
libMesh::SubsetSolveMode
SubsetSolveMode
Definition: enum_subset_solve_mode.h:35
libMesh::LinearSolver::_solver_configuration
SolverConfiguration * _solver_configuration
Optionally store a SolverOptions object that can be used to set parameters like solver type,...
Definition: linear_solver.h:305
libMesh::LinearSolver::set_preconditioner_type
void set_preconditioner_type(const PreconditionerType pct)
Sets the type of preconditioner to use.
Definition: linear_solver.C:108
libMesh::SolverConfiguration
This class stores solver configuration data, e.g.
Definition: solver_configuration.h:39
libMesh::LinearSolver::preconditioner_type
PreconditionerType preconditioner_type() const
Definition: linear_solver.C:98
libMesh::SolverType
SolverType
Defines an enum for iterative solver types.
Definition: enum_solver_type.h:33
libMesh::ParallelObject
An object whose state is distributed along a set of processors.
Definition: parallel_object.h:55
int
void ErrorVector unsigned int
Definition: adjoints_ex3.C:360
libMesh::LinearSolver::attach_preconditioner
void attach_preconditioner(Preconditioner< T > *preconditioner)
Attaches a Preconditioner object to be used.
Definition: linear_solver.C:118
libMesh::Quality::name
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42