belosSolver.hh

Go to the documentation of this file.
1 
8 #ifndef BELOSSOLVER_HH_
9 #define BELOSSOLVER_HH_
10 
11 #include "belosLinProb.hh"
12 #include "sparseMatrix.hh"
13 #include <BelosSolverManager.hpp>
14 #include <BelosTpetraAdapter.hpp>
15 #include <Ifpack2_Preconditioner.hpp>
16 #include <Tpetra_DefaultPlatform.hpp>
17 #include <Tpetra_MultiVector.hpp>
18 #include <Teuchos_RCP.hpp>
19 #include <Tpetra_CrsMatrix.hpp>
20 
21 namespace concepts {
22 
23  // *********************************************************** BelosSolver **
24 
31  template<class T>
32  class BelosSolver: public VecOperator<T> {
33  public:
34 
35  //typenames for better reading
36  typedef Tpetra::MultiVector<T, int> MV;
37  typedef Tpetra::Operator<T> OP;
38  typedef Tpetra::CrsMatrix<T, int, int> CRSmat;
40  typedef Belos::SolverManager<T, MV, OP> solverManager;
41 
44  Teuchos::RCP<const Teuchos::Comm<int> > comm);
45 
47  BelosSolver(Teuchos::RCP<const Teuchos::Comm<int> > comm);
48  inline virtual ~BelosSolver() {
49  }
50  ;
51 
53  inline virtual void operator()() {
54  apply_();
55  }
56  ;
57 
62  inline virtual void operator()(const Vector<T>& fncY, Vector<T>& fncX) {
63  apply_(fncY, fncX);
64  }
65  ;
66 
70  inline void setSolverParam(Teuchos::RCP<Teuchos::ParameterList> param) {
71  solverParam_ = param;
72  }
73  ;
74 
80  inline void setSolverParam(int maxIter, int maxRestarts, double tol = 1e-10,
81  int blockSize = 1, int outFreq = 1) {
82  int verbosity = Belos::Warnings + Belos::Errors + Belos::FinalSummary;
83  solverParam_ = Teuchos::RCP<Teuchos::ParameterList>(
84  new Teuchos::ParameterList());
85  solverParam_->set("Verbosity", verbosity);
86  solverParam_->set("Block Size", blockSize);
87  solverParam_->set("Maximum Iterations", maxIter);
88 // if (solverType_ != "minres")
89 // solverParam_->set("Maximum Restarts", maxRestarts);
90  solverParam_->set("Convergence Tolerance", tol);
91  solverParam_->set("Output Frequency", outFreq);
92  solverParam_->set("Output Stream",
93  Teuchos::rcpFromRef<std::ostream>(std::cout));
94  }
95  ;
96 
98  inline Teuchos::RCP<Teuchos::ParameterList> getSolverParam() {
99  return solverParam_;
100  }
101  ;
102 
106  inline void setPrecParam(Teuchos::RCP<Teuchos::ParameterList> param) {
107  precParam_ = param;
108  }
109  ;
110 
111  //TODO right expl. for fill??
116  inline void setPrecParam(double fill, double tol = 1e-5, int absTresh = 0,
117  int relTresh = 1, double relax = 0) {
118  precParam_ = Teuchos::RCP<Teuchos::ParameterList>(
119  new Teuchos::ParameterList("IFPACK2"));
120  precParam_->set("fact: level-of-fill", fill);
121  precParam_->set("fact: ilut level-of-fill", fill);
122  precParam_->set("fact: riluk level-of-fill", fill);
123  precParam_->set("fact: absolute threshold", absTresh);
124  precParam_->set("fact: relative threshold", relTresh);
125  precParam_->set("fact: relax value", relax);
126  precParam_->set("fact: drop tolerance", tol);
127  }
128  ;
129 
131  inline Teuchos::RCP<Teuchos::ParameterList> getPrecParam() {
132  return precParam_;
133  }
134  ;
135 
139  inline void setSolverManager(Teuchos::RCP<solverManager> manager) {
140  solverType_ = "";
141  solverManager_ = manager;
142  }
143  ;
144 
148  inline Teuchos::RCP<solverManager> getSolverManager() {
149  return solverManager_;
150  }
151  ;
152 
153  inline Teuchos::RCP<LP> getLinearProblem() {
154  return lp_;
155  }
156 
163  inline void setSolverType(std::string type) {
164  solverType_ = type;
165  }
166  ;
167 
169  inline std::string getSolverType() {
170  return solverType_;
171  }
172  ;
173 
182  inline void setPrecType(std::string type) {
183  precType_ = type;
184  }
185  ;
186 
187  //Getter for the preconditioner type
188  inline std::string getPrecType() {
189  return precType_;
190  }
191  ;
192 
194  inline void prepare() {
195  //type and param should be setted
196  createPrec_();
197 
198  //type and param should be setted
199  createSolver_();
200 
201 
202  //precond
203  phasePrecInit_((Comm_->getRank() == 0));
204 
205  prepared_ = true;
206  }
207 
208  protected:
209  virtual std::ostream& info(std::ostream& os) const;
210  private:
211 
213  virtual void apply_(const Vector<T>& fncY, Vector<T>& fncX);
214 
216  virtual void apply_();
217 
219  bool createSolver_();
220 
222  bool createPrec_();
223 
225  bool phasePrecInit_(bool verbose = true);
226 
228  bool phaseSolve_(bool verbose = true);
229 
231  Teuchos::RCP<const Teuchos::Comm<int> > Comm_;
232 
234  Teuchos::RCP<LP> lp_;
235 
237  Teuchos::RCP<Teuchos::ParameterList> solverParam_;
238  Teuchos::RCP<Teuchos::ParameterList> precParam_;
239 
241  Teuchos::RCP<solverManager> solverManager_;
242 
244  Teuchos::RCP<Ifpack2::Preconditioner<T, int> > prec_;
245 
246  //type of solver/preconditioner as string
247  std::string solverType_;
248  std::string precType_;
249 
251  bool prepared_;
252  }
253  ;
254 
255  //Constructors
256  template<class T>
258  Teuchos::RCP<const Teuchos::Comm<int> > Comm) :
259  VecOperator<T>(sparse.dimY(), sparse.dimX()) {
260  Comm_ = Comm;
261  prepared_ = false;
262  lp_ = Teuchos::RCP<LP>(new LP(sparse, Comm));
263  }
264  ;
265 
266  template<class T>
267  BelosSolver<T>::BelosSolver(Teuchos::RCP<const Teuchos::Comm<int> > Comm) :
268  VecOperator<T>(1, 1) {
269  Comm_ = Comm;
270  prepared_ = false;
271  lp_ = Teuchos::RCP<LP>(new LP(Comm));
272  }
273  ;
274 
275 } /* namespace concepts */
276 #endif /* BELOSSOLVER_HH_ */
std::string solverType_
Definition: belosSolver.hh:247
Teuchos::RCP< LP > lp_
Linear Problem as Belos::LinearProblem.
Definition: belosSolver.hh:234
T type
Type of data, e.g. matrix entries.
Definition: compositions.hh:45
bool createSolver_()
Creates Solver with given params sets by setPrecParam and setPrecType.
Teuchos::RCP< Teuchos::ParameterList > solverParam_
Parameter for solver and preconditioner.
Definition: belosSolver.hh:237
Concepts interface to Trilinos Belos solver with different ifpack2 preconditioners and different iter...
Definition: belosSolver.hh:32
void setSolverParam(Teuchos::RCP< Teuchos::ParameterList > param)
Set Solver parameter list via Teuchos::parameter list.
Definition: belosSolver.hh:70
virtual void apply_()
Version for the other threads i.e. MyPID != 0.
bool phasePrecInit_(bool verbose=true)
Builds the preconditioner.
Tpetra::MultiVector< T, int > MV
Definition: belosSolver.hh:36
Teuchos::RCP< LP > getLinearProblem()
Definition: belosSolver.hh:153
virtual std::ostream & info(std::ostream &os) const
virtual void operator()(const Vector< T > &fncY, Vector< T > &fncX)
Solving operator for the root thread.
Definition: belosSolver.hh:62
std::string getPrecType()
Definition: belosSolver.hh:188
Teuchos::RCP< solverManager > getSolverManager()
Gets the solver via solver manager.
Definition: belosSolver.hh:148
Teuchos::RCP< solverManager > solverManager_
Of Type Belos::Solvermanager Handles choice of solver like CG or gmRes.
Definition: belosSolver.hh:241
std::string getSolverType()
Getter for the solver type.
Definition: belosSolver.hh:169
void prepare()
Builds the Preconditioner and the solver (sets prepare to true)
Definition: belosSolver.hh:194
Teuchos::RCP< const Teuchos::Comm< int > > Comm_
Communicator of the mpi thread.
Definition: belosSolver.hh:231
Decorator that decorates the Class Belos::LinearProblem<T, MV, OP> with interfaces to Concepts Sparse...
Definition: belosLinProb.hh:26
Teuchos::RCP< Teuchos::ParameterList > precParam_
Definition: belosSolver.hh:238
BelosSolver(concepts::SparseMatrix< T > &sparse, Teuchos::RCP< const Teuchos::Comm< int > > comm)
Constructor for the root thread.
Definition: belosSolver.hh:257
Tpetra::Operator< T > OP
Definition: belosSolver.hh:37
void setSolverManager(Teuchos::RCP< solverManager > manager)
Sets the solver via solver manager.
Definition: belosSolver.hh:139
virtual void operator()()
Solving operator for other the non-root threads.
Definition: belosSolver.hh:53
Teuchos::RCP< Teuchos::ParameterList > getPrecParam()
Gets the parameter list for the preconditioner.
Definition: belosSolver.hh:131
void setPrecType(std::string type)
Sets the preconditioner Type to one of the followings.
Definition: belosSolver.hh:182
void setSolverParam(int maxIter, int maxRestarts, double tol=1e-10, int blockSize=1, int outFreq=1)
Set Solver parameter list via values.
Definition: belosSolver.hh:80
void setSolverType(std::string type)
Sets the solver type to one of the followings.
Definition: belosSolver.hh:163
Teuchos::RCP< Teuchos::ParameterList > getSolverParam()
Get solver parameter list.
Definition: belosSolver.hh:98
Teuchos::RCP< Ifpack2::Preconditioner< T, int > > prec_
The Preconditionier.
Definition: belosSolver.hh:244
Abstract class for an operator acting on vectors only, not arbitrary functions.
Tpetra::CrsMatrix< T, int, int > CRSmat
Definition: belosSolver.hh:38
bool phaseSolve_(bool verbose=true)
Solving.
bool createPrec_()
Creates Preconditioner with given params sets by setSolverParam and setSolverType.
void setPrecParam(Teuchos::RCP< Teuchos::ParameterList > param)
Set preconditioner parameter list via Teuchos::parameter list.
Definition: belosSolver.hh:106
void setPrecParam(double fill, double tol=1e-5, int absTresh=0, int relTresh=1, double relax=0)
Set preconditoner parameter list via values.
Definition: belosSolver.hh:116
Belos::SolverManager< T, MV, OP > solverManager
Definition: belosSolver.hh:40
bool prepared_
is preconditioner and all opions set
Definition: belosSolver.hh:251
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
BelosLinProb< T, MV, OP > LP
Definition: belosSolver.hh:39
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich