belosSolver_decl.hh

Go to the documentation of this file.
1 
8 #ifndef BELOSSOLVER_DECL_HH_
9 #define BELOSSOLVER_DECL_HH_
10 
11 #include "belosSolver.hh"
12 #include <BelosLinearProblem.hpp>
13 #include <BelosBlockGmresSolMgr.hpp>
14 #include <BelosPseudoBlockCGSolMgr.hpp>
15 #include <BelosBlockCGSolMgr.hpp>
16 #include <BelosMinresSolMgr.hpp>
17 #include <BelosPseudoBlockGmresSolMgr.hpp>
18 #include <Teuchos_ArrayView.hpp>
19 #include <Ifpack2_Factory.hpp>
20 #include "ctype.h"
21 
26 namespace concepts {
27 
34  template<class T>
35  Teuchos::RCP<
36  Belos::SolverManager<T, Tpetra::MultiVector<T, int>, Tpetra::Operator<T> > > createBelosSolverMgr(
37  std::string solverType,
38  const Teuchos::RCP<Teuchos::ParameterList>& solverParam,
39  Teuchos::RCP<
40  Belos::LinearProblem<T, Tpetra::MultiVector<T, int>,
41  Tpetra::Operator<T> > > linearProblem)
42 
43  {
44  typedef Tpetra::MultiVector<T, int> MV;
45  typedef Tpetra::Operator<T> OP;
46 
47  std::transform(solverType.begin(), solverType.end(), solverType.begin(),
48  ::tolower);
49 
50  if (solverType == "blockgmres") {
51  return Teuchos::RCP<Belos::BlockGmresSolMgr<T, MV, OP> >(
52  new Belos::BlockGmresSolMgr<T, MV, OP>(linearProblem, solverParam));
53  } else if (solverType == "gmres") {
54  return Teuchos::RCP<Belos::PseudoBlockGmresSolMgr<T, MV, OP> >(
55  new Belos::PseudoBlockGmresSolMgr<T, MV, OP>(linearProblem,
56  solverParam));
57 
58 // } else if (solverType == "minres") {
59 // return Teuchos::RCP<Belos::MinresSolMgr<T, MV, OP> >(
60 // new Belos::MinresSolMgr<T, MV, OP>(linearProblem, solverParam));
61 //
62  } else if (solverType == "blockcg") {
63  return Teuchos::rcp(
64  new Belos::BlockCGSolMgr<T, MV, OP>(linearProblem, solverParam));
65  }
66 
67  else if (solverType == "pseudocg") {
68  return Teuchos::rcp(
69  new Belos::PseudoBlockCGSolMgr<T, MV, OP>(linearProblem, solverParam));
70  } else {
71  std::cerr << "ERROR: solver type '" << solverType << "' unknown."
72  << std::endl;
73  return Teuchos::null;
74  }
75 
76  }
77 
86  template<class T>
87  Teuchos::RCP<Ifpack2::Preconditioner<T> > createIfpackPrec(
88  std::string precType, Teuchos::RCP<Teuchos::ParameterList> precParam,
89  const Teuchos::RCP<const Tpetra::CrsMatrix<T, int> > A) {
90 
91  Ifpack2::Factory factory;
92  try {
93  // Set up the preconditioner of that type.
94  typename Teuchos::RCP<Ifpack2::Preconditioner<T, int> > prec =
95  factory.create(precType, A);
96  prec->setParameters(*precParam);
97  return prec;
98  } catch (std::exception &e) {
99  std::cerr << "ERROR: preconditioner type '" << precType << "' unknown."
100  << std::endl;
101  }
102  return Teuchos::null;
103 
104  }
105 
106  template<class T>
108  prec_ = createIfpackPrec<T>(precType_, precParam_, lp_->getCrsMat());
109  return !Teuchos::is_null(prec_);
110  }
111 
112  template<class T>
114  solverManager_ = createBelosSolverMgr(solverType_, solverParam_,
115  Teuchos::RCP<Belos::LinearProblem<T, MV, OP> >(lp_));
116  solverManager_->setProblem(lp_);
117  return !Teuchos::is_null(solverManager_);
118  }
119 
120  template<class T>
121  bool BelosSolver<T>::phasePrecInit_(bool verbose) {
122  Teuchos::Time timer("precond");
123 // prec_->setParam(precParam);
124  prec_->initialize();
125  timer.start();
126  prec_->compute();
127  timer.stop();
128  lp_->setLeftPrec(prec_);
129 // lp_->setRightPrec(prec_);
130  if (verbose) {
131  std::cout << "... Finished Computing Ifpack2 preconditioner (time: "
132  << timer.totalElapsedTime() << "s)" << std::endl;
133  }
134 
135  typename Teuchos::ScalarTraits<T>::magnitudeType condest =
136  prec_->computeCondEst(Ifpack2::Cheap);
137  if (verbose) {
138  std::cout << "Condition estimate(cheap) for preconditioner: " << condest;
139  std::cout << std::endl;
140  }
141  return true;
142  }
143  ;
144 
145  template<class T>
146  bool BelosSolver<T>::phaseSolve_(bool verbose) {
147  verbose = (Comm_->getRank() == 0);
148 
149  // Signal that we are done setting up the linear problem
150  bool ierr = lp_->setProblem();
151 
152  // Check the return from setProblem(). If this is true, there was NO
153  // error. This probably means we did not specify enough information for
154  // the eigenproblem.
155  conceptsAssert(ierr == true, concepts::Assertion());
156 
157  Belos::ReturnType solverRet = solverManager_->solve();
158 
159  // Check return code of the solver: Unconverged, Failed, or OK
160  switch (solverRet) {
161 
162  // UNCONVERGED
163  case Belos::Unconverged:
164  std::cout << "##### Iterative solver did not converge!" << std::endl;
165  throw std::logic_error("Belos did not converge!");
166 
167  // CONVERGED
168  case Belos::Converged:
169  if (verbose)
170  std::cout << "Iterative solver converged!" << std::endl;
171  return true;
172  }
174  return false;
175  }
176  ;
177 
178  template<class T>
179  void BelosSolver<T>::apply_(const Vector<T>& fncY, Vector<T>& fncX) {
180  lp_->setConceptsRHS(fncY, Comm_);
181  //solve
182  try {
183  phaseSolve_();
184  } catch (std::logic_error& e) {
185  std::cerr << "***** ERROR during solving process: " << e.what()
186  << std::endl;
187  }
188  lp_->writeSolution(fncX, Comm_);
189 
190  }
191 
192  template<class T>
194  lp_->setConceptsRHS(Comm_);
195  try {
196  phaseSolve_();
197  } catch (std::logic_error& e) {
198  std::cerr << "***** ERROR during solving process: " << e.what()
199  << std::endl;
200  }
201  lp_->writeSolution(Comm_);
202 
203  }
204 
205 } // namespace
206 #endif /* BELOSSOLVER_DECL_HH_ */
207 
Teuchos::RCP< Belos::SolverManager< T, Tpetra::MultiVector< T, int >, Tpetra::Operator< T > > > createBelosSolverMgr(std::string solverType, const Teuchos::RCP< Teuchos::ParameterList > &solverParam, Teuchos::RCP< Belos::LinearProblem< T, Tpetra::MultiVector< T, int >, Tpetra::Operator< T > > > linearProblem)
Sets the solver type to one of the followings.
bool createSolver_()
Creates Solver with given params sets by setPrecParam and setPrecType.
virtual void apply_()
Version for the other threads i.e. MyPID != 0.
bool phasePrecInit_(bool verbose=true)
Builds the preconditioner.
#define conceptsAssert(cond, exc)
Assert that a certain condition is fulfilled.
Definition: exceptions.hh:394
Exception class for assertions.
Definition: exceptions.hh:258
bool phaseSolve_(bool verbose=true)
Solving.
char tolower(const char ch)
bool createPrec_()
Creates Preconditioner with given params sets by setSolverParam and setSolverType.
Teuchos::RCP< Ifpack2::Preconditioner< T > > createIfpackPrec(std::string precType, Teuchos::RCP< Teuchos::ParameterList > precParam, const Teuchos::RCP< const Tpetra::CrsMatrix< T, int > > A)
precType can assume the following parameters
Basic namespace for Concepts-2.
Definition: pml_formula.h:16
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich