Debian Codegraph @

packages/belos/tpetra/src/solvers/Belos_Tpetra_SolverManagerBase.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef BELOS_TPETRA_SOLVER_MANAGER_BASE_HPP
11 #define BELOS_TPETRA_SOLVER_MANAGER_BASE_HPP
12
13 #include "BelosLinearProblem.hpp"
14 #include "BelosSolverManager.hpp"
15 #include "Belos_Tpetra_Krylov.hpp"
16
17 namespace BelosTpetra {
18 namespace Impl {
19
20 namespace ErrMsgs {
21 static constexpr char linearProblemNotSet[] = "The linear problem has not "
22 "yet been set. Please call setProblem with a nonnull argument before "
23 "calling this method.";
24 static constexpr char solverNull[] = "The solver implementation (solver_) "
25 "is null. This should never happen. "
26 "Please report this bug to the Belos developers.";
27 }
28
29 template<class SC = Tpetra::MultiVector<>::scalar_type,
30 class MV = Tpetra::MultiVector<SC>,
31 class OP = Tpetra::Operator<SC>>
32 class SolverManagerBase :
33 public Belos::SolverManager<SC, MV, OP>
34 {
35 public:
36 using linear_problem_type = Belos::LinearProblem<SC, MV, OP>;
37
38 SolverManagerBase () = delete;
39
40 SolverManagerBase (const Teuchos::RCP<Krylov<SC, MV, OP>>& solver,
41 const Teuchos::RCP<Teuchos::ParameterList>& params = Teuchos::null) :
42 solver_ (solver),
43 lastSolverOutput_ {}
44 {
45 const char prefix[] = "SolverManagerBase::SolverManagerBase: ";
46 TEUCHOS_TEST_FOR_EXCEPTION
47 (solver.get () == nullptr, std::invalid_argument, prefix << ErrMsgs::solverNull);
48 if (params.get () != nullptr) {
49 this->setParameters (params);
50 }
51 }
52
53 virtual ~SolverManagerBase () = default;
54
55 const linear_problem_type& getProblem () const override {
56 const char prefix[] = "SolverManagerBase::getProblem: ";
57 TEUCHOS_TEST_FOR_EXCEPTION
58 (this->problem_.get () == nullptr, std::runtime_error,
59 prefix << ErrMsgs::linearProblemNotSet);
60 return *(this->problem_);
61 }
62
63 //! Get valid parameters
64 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters () const override {
65 using Teuchos::ParameterList;
66 using Teuchos::RCP;
67 const char prefix[] = "SolverManagerBase::getValidParameters: ";
68
69 TEUCHOS_TEST_FOR_EXCEPTION
70 (this->solver_.get () == nullptr, std::logic_error, prefix << ErrMsgs::solverNull);
71 RCP<ParameterList> params (new ParameterList ("SolverManagerBase"));
72 const bool defaultValues = true;
73 this->solver_->getParameters (*params, defaultValues);
74 return params;
75 }
76
77 //! Get current parameters
78 Teuchos::RCP<const Teuchos::ParameterList> getCurrentParameters () const override {
79 using Teuchos::ParameterList;
80 using Teuchos::RCP;
81 const char prefix[] = "SolverManagerBase::getCurrentParameters: ";
82
83 TEUCHOS_TEST_FOR_EXCEPTION
84 (this->solver_.get () == nullptr, std::logic_error, prefix << ErrMsgs::solverNull);
85 RCP<ParameterList> params (new ParameterList ("SolverManagerBase"));
86 const bool defaultValues = false;
87 this->solver_->getParameters (*params, defaultValues);
88 return params;
89 }
90
91 //! Get number of iterations
92 int getNumIters () const override {
93 return this->lastSolverOutput_.numIters;
94 }
95
96 //! Get number of restarts
97 int getNumRests () const {
98 return this->lastSolverOutput_.numRests;
99 }
100
101 bool isLOADetected () const override {
102 return false; // this solver doesn't attempt to detect loss of accuracy
103 }
104
105 void
106 setProblem (const Teuchos::RCP<linear_problem_type>& problem) override
107 {
108 const char prefix[] = "SolverManagerBase::setProblem: ";
109 TEUCHOS_TEST_FOR_EXCEPTION
110 (this->solver_.get () == nullptr, std::logic_error, prefix << ErrMsgs::solverNull);
111
112 if (problem.is_null ()) {
113 this->solver_->setMatrix (Teuchos::null);
114 }
115 else {
116 if (this->solver_->getMatrix ().get () != problem->getOperator ().get ()) {
117 // setMatrix resets state, so only call if necessary.
118 this->solver_->setMatrix (problem->getOperator ());
119 }
120 if (problem->getRightPrec ().get () != nullptr &&
121 this->solver_->getPreconditioner () != problem->getRightPrec ()) {
122 this->solver_->setRightPrec (problem->getRightPrec ());
123 }
124 if ((problem->getLeftPrec ()).get() != nullptr &&
125 this->solver_->getPreconditioner () != problem->getLeftPrec ()) {
126 this->solver_->setLeftPrec (problem->getLeftPrec ());
127 }
128 }
129 this->problem_ = problem;
130 }
131
132 void setParameters (const Teuchos::RCP<Teuchos::ParameterList>& params) override {
133 const char prefix[] = "SolverManagerBase::setParameters: ";
134 TEUCHOS_TEST_FOR_EXCEPTION
135 (this->solver_.get () == nullptr, std::logic_error, prefix << ErrMsgs::solverNull);
136 if (! params.is_null ()) {
137 this->solver_->setParameters (*params);
138 }
139 }
140
141 void reset (const Belos::ResetType /* type */ ) override {
142 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "Not implemented");
143 }
144
145 Belos::ReturnType solve () override {
146 using Teuchos::RCP;
147 const char prefix[] = "SolverManagerBase::solve: ";
148
149 TEUCHOS_TEST_FOR_EXCEPTION
150 (this->solver_.get () == nullptr, std::logic_error,
151 prefix << ErrMsgs::solverNull);
152 TEUCHOS_TEST_FOR_EXCEPTION
153 (this->problem_.is_null (), std::runtime_error,
154 prefix << ErrMsgs::linearProblemNotSet);
155
156 RCP<const MV> B = problem_->getRHS ();
157 TEUCHOS_TEST_FOR_EXCEPTION
158 (B.is_null (), std::runtime_error, "The linear problem's right-hand "
159 "side(s) B has/have not yet been set. Please call setProblem with "
160 "a nonnull argument before calling this method.");
161 RCP<MV> X = problem_->getLHS ();
162 TEUCHOS_TEST_FOR_EXCEPTION
163 (X.is_null (), std::runtime_error, "The linear problem's left-hand "
164 "side(s) X has/have not yet been set. Please call setProblem with "
165 "a nonnull argument before calling this method.");
166
167 lastSolverOutput_ = solver_->solve (*X, *B);
168 return lastSolverOutput_.converged ? Belos::Converged : Belos::Unconverged;
169 }
170
171 typename Teuchos::ScalarTraits<SC>::magnitudeType achievedTol() const override {
172 using STS = Teuchos::ScalarTraits<SC>;
173 using real_type = typename STS::magnitudeType;
174 const SC one = STS::one ();
175
176 auto A = solver_->getMatrix ();
177 auto B = problem_->getRHS ();
178 auto X = problem_->getLHS ();
179
180 const size_t numVecs = B->getNumVectors ();
181 MV R (X->getMap (), numVecs, false);
182 A->apply (*X, R);
183 R.update (one, *B, -one);
184
185 Teuchos::Array<real_type> norms (numVecs);
186 R.norm2 (norms); // residual norm
187 real_type max_norm = norms[0];
188 for (size_t j = 1; j < numVecs; j++) {
189 if (norms[j] > max_norm) {
190 max_norm = norms[j];
191 }
192 }
193 return max_norm;
194 }
195
196 private:
197 Teuchos::RCP<Krylov<SC, MV, OP>> solver_;
198 //! Output of the last solve, not including the solution (multi)vector.
199 SolverOutput<SC> lastSolverOutput_;
200 Teuchos::RCP<linear_problem_type> problem_;
201 };
202
203 } // namespace Impl
204 } // namespace BelosTpetra
205
206 #endif // BELOS_TPETRA_SOLVER_MANAGER_BASE_HPP
207