OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
BelosBottomSolver.hpp
Go to the documentation of this file.
1 //
2 // Class BelosBottomSolver
3 // Interface to Belos solvers of the Trilinos package.
4 //
5 // Copyright (c) 2017 - 2020, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // Implemented as part of the PhD thesis
9 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #include "Ippl.h"
23 
24 extern Inform* gmsg;
25 
26 template <class Level>
28  const std::shared_ptr<prec_t>& prec_p)
29  : BottomSolver<Teuchos::RCP<amr::matrix_t>,
30  Teuchos::RCP<amr::multivector_t>,
31  Level>()
32  , problem_mp( Teuchos::rcp( new problem_t() ) )
33  , prec_mp(prec_p)
34  , A_mp(Teuchos::null)
35  , reltol_m(1.0e-9)
36  , maxiter_m(100)
37 {
38  this->initSolver_m(solvertype);
39 }
40 
41 
42 template <class Level>
43 void BelosBottomSolver<Level>::solve(const Teuchos::RCP<mv_t>& x,
44  const Teuchos::RCP<mv_t>& b)
45 {
46  /*
47  * solve linear system Ax = b
48  */
49 
50  // change sign of rhs due to change of A in BelosBottomSolver::setOperator
51  b->scale(-1.0);
52 
53  problem_mp->setProblem(x, b);
54 
55  solver_mp->setProblem(problem_mp);
56 
57  Belos::ReturnType ret = solver_mp->solve();
58 
59  if ( ret != Belos::Converged ) {
60  *gmsg << "Warning: Bottom solver not converged. Achieved tolerance"
61  << " after " << solver_mp->getNumIters() << " iterations is "
62  << solver_mp->achievedTol() << "." << endl;
63  }
64 
65  // undo sign change
66  b->scale(-1.0);
67 }
68 
69 
70 template <class Level>
71 void BelosBottomSolver<Level>::setOperator(const Teuchos::RCP<matrix_t>& A,
72  Level* level_p)
73 {
74  // make positive definite --> rhs has to change sign as well
75  A_mp = Teuchos::rcp(new matrix_t(*A, Teuchos::Copy));
76  A_mp->resumeFill();
77  A_mp->scale(-1.0);
78  A_mp->fillComplete();
79 
80  if ( problem_mp == Teuchos::null )
81  throw OpalException("BelosBottomSolver::setOperator()",
82  "No problem defined.");
83 
84  problem_mp->setOperator(A_mp);
85 
86  static IpplTimings::TimerRef precTimer = IpplTimings::getTimer("AMR MG prec setup");
87 
88  if ( prec_mp != nullptr ) {
89  IpplTimings::startTimer(precTimer);
90  prec_mp->create(A_mp, level_p);
91  IpplTimings::stopTimer(precTimer);
92  problem_mp->setLeftPrec(prec_mp->get());
93  }
94 
95  this->isInitialized_m = true;
96 }
97 
98 
99 template <class Level>
101  if ( solver_mp == Teuchos::null )
102  throw OpalException("BelosBottomSolver::getNumIters()",
103  "No solver initialized.");
104 
105  return solver_mp->getNumIters();
106 }
107 
108 
109 template <class Level>
110 void BelosBottomSolver<Level>::initSolver_m(std::string solvertype) {
111 
112  Belos::SolverFactory<scalar_t, mv_t, op_t> factory;
113 
114  params_mp = Teuchos::parameterList();
115 
116  params_mp->set("Block Size", 1);
117  params_mp->set("Convergence Tolerance", reltol_m);
118  params_mp->set("Adaptive Block Size", false);
119  params_mp->set("Use Single Reduction", true);
120  params_mp->set("Explicit Residual Scaling", "Norm of RHS");
121  params_mp->set("Maximum Iterations", maxiter_m);
122  params_mp->set("Verbosity", Belos::Errors + Belos::Warnings);
123  params_mp->set("Output Frequency", 1);
124 
125  solver_mp = factory.create(solvertype, params_mp);
126 }
Inform * gmsg
Definition: Main.cpp:62
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
Some AMR types used a lot.
Definition: AmrDefs.h:33
Tpetra::MultiVector< scalar_t, local_ordinal_t, global_ordinal_t, node_t > multivector_t
Tpetra::CrsMatrix< scalar_t, local_ordinal_t, global_ordinal_t, node_t > matrix_t
void initSolver_m(std::string solvertype)
Belos::LinearProblem< scalar_t, mv_t, op_t > problem_t
void solve(const Teuchos::RCP< mv_t > &x, const Teuchos::RCP< mv_t > &b)
BelosBottomSolver(std::string solvertype="Pseudoblock CG", const std::shared_ptr< prec_t > &prec_p=nullptr)
void setOperator(const Teuchos::RCP< matrix_t > &A, Level *level_p=nullptr)
amr::matrix_t matrix_t
std::size_t getNumIters()
The base class for all OPAL exceptions.
Definition: OpalException.h:28
Definition: Inform.h:42
Timing::TimerRef TimerRef
Definition: IpplTimings.h:176
static TimerRef getTimer(const char *nm)
Definition: IpplTimings.h:182
static void stopTimer(TimerRef t)
Definition: IpplTimings.h:192
static void startTimer(TimerRef t)
Definition: IpplTimings.h:187