DOLFIN
DOLFIN C++ interface
BelosKrylovSolver.h
1 // Copyright (C) 2014 Chris Richardson
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #ifndef __DOLFIN_BELOS_KRYLOV_SOLVER_H
20 #define __DOLFIN_BELOS_KRYLOV_SOLVER_H
21 
22 #ifdef HAS_TRILINOS
23 
24 #include <map>
25 #include <memory>
26 
27 #include <BelosTpetraAdapter.hpp>
28 #include <BelosSolverFactory.hpp>
29 #include <Ifpack2_Factory.hpp>
30 
31 #include <dolfin/common/types.h>
32 #include "GenericLinearSolver.h"
33 #include "TpetraVector.h"
34 #include "TpetraMatrix.h"
35 #include "TrilinosPreconditioner.h"
36 
37 namespace dolfin
38 {
39 
41  class GenericMatrix;
42  class GenericVector;
43  class TpetraMatrix;
44  class TpetraVector;
45  class TrilinosPreconditioner;
46 
50 
52  {
53  public:
54 
56  typedef Tpetra::Operator<double, int, dolfin::la_index,
59  typedef Belos::LinearProblem<double, TpetraVector::vector_type,
60  op_type> problem_type;
61 
64  BelosKrylovSolver(std::string method = "default",
65  std::string preconditioner = "default");
66 
68  BelosKrylovSolver(std::string method,
69  std::shared_ptr<TrilinosPreconditioner> preconditioner);
70 
73 
75  void set_operator(std::shared_ptr<const GenericLinearOperator> A);
76 
78  void set_operators(std::shared_ptr<const GenericLinearOperator> A,
79  std::shared_ptr<const GenericLinearOperator> P);
80 
82  const TpetraMatrix& get_operator() const;
83 
85  std::size_t solve(GenericVector& x, const GenericVector& b);
86 
88  std::size_t solve(const GenericLinearOperator& A, GenericVector& x,
89  const GenericVector& b);
90 
92  std::string str(bool verbose) const;
93 
95  static std::map<std::string, std::string> methods();
96 
98  static std::map<std::string, std::string> preconditioners();
99 
102 
104  std::string parameter_type() const
105  {
106  return "krylov_solver";
107  }
108 
109  private:
110 
111  friend class Ifpack2Preconditioner;
112  friend class MueluPreconditioner;
113 
114  // Initialize solver
115  void init(const std::string& method);
116 
117  // Set operator (matrix)
118  void _set_operator(std::shared_ptr<const TpetraMatrix> A);
119 
120  // Set operator (matrix) and preconditioner matrix
121  void _set_operators(std::shared_ptr<const TpetraMatrix> A,
122  std::shared_ptr<const TpetraMatrix> P);
123 
124  // Solve linear system Ax = b and return number of iterations
125  std::size_t _solve(TpetraVector& x, const TpetraVector& b);
126 
127  // Solve linear system Ax = b and return number of iterations
128  std::size_t _solve(const TpetraMatrix& A, TpetraVector& x,
129  const TpetraVector& b);
130 
131  // Set options for solver
132  void set_options();
133 
134  void check_dimensions(const TpetraMatrix& A, const GenericVector& x,
135  const GenericVector& b) const;
136 
137  // Belos solver pointer
138  Teuchos::RCP<Belos::SolverManager<double, TpetraVector::vector_type,
139  op_type>> _solver;
140 
141  // The preconditioner, if any
142  std::shared_ptr<TrilinosPreconditioner> _prec;
143 
144  // Container for the problem, see Belos::LinearProblem
145  // documentation
146  Teuchos::RCP<problem_type> _problem;
147 
148  // Operator (the matrix)
149  std::shared_ptr<const TpetraMatrix> _matA;
150 
151  };
152 
153 }
154 
155 #endif
156 
157 #endif
void set_operator(std::shared_ptr< const GenericLinearOperator > A)
Set operator (matrix)
Definition: BelosKrylovSolver.cpp:129
static std::map< std::string, std::string > methods()
Return a list of available solver methods.
Definition: BelosKrylovSolver.cpp:50
static Parameters default_parameters()
Default parameter values.
Definition: BelosKrylovSolver.cpp:117
Belos::LinearProblem< double, TpetraVector::vector_type, op_type > problem_type
Belos problem type.
Definition: BelosKrylovSolver.h:60
Definition: adapt.h:29
Implements Muelu preconditioner from Trilinos.
Definition: MueluPreconditioner.h:40
~BelosKrylovSolver()
Destructor.
Definition: BelosKrylovSolver.cpp:112
Definition: GenericLinearOperator.h:42
Tpetra::MultiVector::node_type node_type
Node type.
Definition: TpetraVector.h:58
Definition: TpetraMatrix.h:58
Tpetra::MultiVector< double, int, dolfin::la_index, node_type > vector_type
TpetraVector vector type (scalar, local index, global index, node)
Definition: TpetraVector.h:63
Definition: BelosKrylovSolver.h:51
std::string parameter_type() const
Return parameter type: "krylov_solver" or "lu_solver".
Definition: BelosKrylovSolver.h:104
Definition: Parameters.h:94
Definition: TpetraVector.h:53
static std::map< std::string, std::string > preconditioners()
Return a list of available preconditioners.
Definition: BelosKrylovSolver.cpp:39
BelosKrylovSolver(std::string method="default", std::string preconditioner="default")
Definition: BelosKrylovSolver.cpp:67
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
This class provides a general solver for linear systems Ax = b.
Definition: GenericLinearSolver.h:37
std::size_t solve(GenericVector &x, const GenericVector &b)
Solve linear system Ax = b and return number of iterations.
Definition: BelosKrylovSolver.cpp:153
std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: BelosKrylovSolver.cpp:167
Tpetra::Operator< double, int, dolfin::la_index, TpetraVector::node_type > op_type
Tpetra operator type.
Definition: BelosKrylovSolver.h:57
Implements preconditioners using Ifpack2 from Trilinos.
Definition: Ifpack2Preconditioner.h:40
This class defines a common interface for vectors.
Definition: GenericVector.h:47
void set_operators(std::shared_ptr< const GenericLinearOperator > A, std::shared_ptr< const GenericLinearOperator > P)
Set operator (matrix) and preconditioner matrix.
Definition: BelosKrylovSolver.cpp:135
const TpetraMatrix & get_operator() const
Get operator (matrix)
Definition: BelosKrylovSolver.cpp:142