Debian Codegraph @

packages/belos/tpetra/test/MultiMatrixSolve/BelosLinearMultiShiftProblem.hpp

raw
1
2 // @HEADER
3 // *****************************************************************************
4 // Belos: Block Linear Solvers Package
5 //
6 // Copyright 2004-2016 NTESS and the Belos contributors.
7 // SPDX-License-Identifier: BSD-3-Clause
8 // *****************************************************************************
9 // @HEADER
10
11 #ifndef BELOS_LINEAR_MULTIPROBLEM_HPP
12 #define BELOS_LINEAR_MULTIPROBLEM_HPP
13
14 /// \file BelosLinearMultiShiftProblem.hpp
15 /// \brief Class which describes the linear problem to be solved by
16 /// the iterative solver.
17 #include "BelosLinearProblem.hpp"
18 #include "BelosMultiVecTraits.hpp"
19 #include "BelosOperatorTraits.hpp"
20 #include "Teuchos_ParameterList.hpp"
21 #include "Teuchos_TimeMonitor.hpp"
22
23 namespace Belos {
24
25 //@}
26
27 /// \class LinearMultiShiftProblem
28 /// \brief A linear system to solve multiple matrix and right-hand sides.
29 ///
30 /// This class encapsulates the general information needed for
31 /// solving a set of linear systems of equations using an iterative method.
32 /// A_i*x_i = b_i for i=1,...,N, where A_i is A+i*I
33 ///
34 /// \tparam ScalarType The type of the entries in the matrix and
35 /// vectors.
36 /// \tparam MV The (multi)vector type.
37 /// \tparam OP The operator type. (Operators are functions that
38 /// take a multivector as input and compute a multivector as
39 /// output.)
40 template <class ScalarType, class MV, class OP>
41 class LinearMultiShiftProblem : public LinearProblem<ScalarType, MV, OP> {
42 public:
43
44 //! @name Constructors/Destructor
45 //@{
46
47 /// \brief Default constructor.
48 ///
49 /// Creates an empty Belos::LinearMultiShiftProblem instance. The operator
50 /// A, left-hand-side X and right-hand-side B must be set using
51 /// the \c setOperator(), \c setLHS(), and \c setRHS() methods
52 /// respectively.
53 LinearMultiShiftProblem (void);
54 /// \brief Unpreconditioned linear system constructor.
55 ///
56 /// Creates an unpreconditioned LinearMultiShiftProblem instance with the
57 /// operator (\c A), initial guess (\c X), and right hand side (\c
58 /// B). Preconditioners can be set using the \c setLeftPrec() and
59 /// \c setRightPrec() methods.
60 LinearMultiShiftProblem (const Teuchos::RCP<const OP> &A,
61 const Teuchos::RCP<MV> &X,
62 const Teuchos::RCP<const MV> &B);
63
64 //! Destructor (declared virtual for memory safety of derived classes).
65 virtual ~LinearMultiShiftProblem (void) {}
66
67 //@}
68
69 //! @name Set / Reset method
70 //@{
71
72 /// \brief Set up the linear problem manager.
73 ///
74 /// Call this method if you want to solve the linear system with a
75 /// different left- or right-hand side, or if you want to prepare
76 /// the linear problem to solve the linear system that was already
77 /// passed in. (In the latter case, call this method with the
78 /// default arguments.) The internal flags will be set as if the
79 /// linear system manager was just initialized, and the initial
80 /// residual will be computed.
81 ///
82 /// Many of Belos' solvers require that this method has been
83 /// called on the linear problem, before they can solve it.
84 ///
85 /// \param newX [in/out] If you want to solve the linear system
86 /// with a different left-hand side, pass it in here.
87 /// Otherwise, set this to null (the default value).
88 ///
89 /// \param newB [in] If you want to solve the linear system with a
90 /// different right-hand side, pass it in here. Otherwise, set
91 /// this to null (the default value).
92 ///
93 /// \return Whether the linear problem was successfully set up.
94 /// Successful setup requires at least that the matrix operator
95 /// A, the left-hand side X, and the right-hand side B all be
96 /// non-null.
97 bool
98 setProblem (const Teuchos::RCP<MV> &newX = Teuchos::null,
99 const Teuchos::RCP<const MV> &newB = Teuchos::null);
100
101
102 void
103 setShift( bool addShift )
104 {
105 addShift_ = addShift;
106 }
107
108 //@}
109
110 //! @name Apply / Compute methods
111 //@{
112
113 /// \brief Apply ONLY the operator to \c x, returning \c y.
114 ///
115 /// This method only applies the linear problem's operator,
116 /// without any preconditioners that may have been defined.
117 /// Flexible variants of Krylov methods will use this method. If
118 /// no operator has been defined, this method just copies x into
119 /// y.
120 void applyOp( const MV& x, MV& y ) const;
121
122 //@}
123
124 private:
125 bool addShift_;
126
127 using LinearProblem<ScalarType, MV, OP>::A_;
128 using LinearProblem<ScalarType, MV, OP>::X_;
129 using LinearProblem<ScalarType, MV, OP>::curX_;
130 using LinearProblem<ScalarType, MV, OP>::B_;
131 using LinearProblem<ScalarType, MV, OP>::curB_;
132 using LinearProblem<ScalarType, MV, OP>::R0_;
133 using LinearProblem<ScalarType, MV, OP>::PR0_;
134 using LinearProblem<ScalarType, MV, OP>::R0_user_;
135 using LinearProblem<ScalarType, MV, OP>::PR0_user_;
136 using LinearProblem<ScalarType, MV, OP>::LP_;
137 using LinearProblem<ScalarType, MV, OP>::RP_;
138 using LinearProblem<ScalarType, MV, OP>::timerOp_;
139 using LinearProblem<ScalarType, MV, OP>::timerPrec_;
140 using LinearProblem<ScalarType, MV, OP>::blocksize_;
141 using LinearProblem<ScalarType, MV, OP>::num2Solve_;
142 using LinearProblem<ScalarType, MV, OP>::rhsIndex_;
143 using LinearProblem<ScalarType, MV, OP>::lsNum_;
144 using LinearProblem<ScalarType, MV, OP>::isSet_;
145 using LinearProblem<ScalarType, MV, OP>::solutionUpdated_;
146 using LinearProblem<ScalarType, MV, OP>::label_;
147
148 typedef MultiVecTraits<ScalarType,MV> MVT;
149 typedef OperatorTraits<ScalarType,MV,OP> OPT;
150 };
151
152 //--------------------------------------------
153 // Constructor Implementations
154 //--------------------------------------------
155
156 template <class ScalarType, class MV, class OP>
157 LinearMultiShiftProblem<ScalarType,MV,OP>::LinearMultiShiftProblem(void)
158 : LinearProblem<ScalarType,MV,OP>(),
159 addShift_(true)
160 {}
161
162 template <class ScalarType, class MV, class OP>
163 LinearMultiShiftProblem<ScalarType,MV,OP>::LinearMultiShiftProblem(const Teuchos::RCP<const OP> &A,
164 const Teuchos::RCP<MV> &X,
165 const Teuchos::RCP<const MV> &B
166 )
167 : LinearProblem<ScalarType,MV,OP>(A, X, B),
168 addShift_(true)
169 {
170 int numVecs = MVT::GetNumberVecs( *B );
171 rhsIndex_.resize(numVecs);
172 for (int i=0; i<numVecs; ++i)
173 rhsIndex_[i] = i;
174 }
175
176 template <class ScalarType, class MV, class OP>
177 bool
178 LinearMultiShiftProblem<ScalarType,MV,OP>::
179 setProblem (const Teuchos::RCP<MV> &newX,
180 const Teuchos::RCP<const MV> &newB)
181 {
182 // Create timers if the haven't been created yet.
183 if (timerOp_ == Teuchos::null) {
184 std::string opLabel = label_ + ": Operation Op*x";
185 #ifdef BELOS_TEUCHOS_TIME_MONITOR
186 timerOp_ = Teuchos::TimeMonitor::getNewCounter( opLabel );
187 #endif
188 }
189 if (timerPrec_ == Teuchos::null) {
190 std::string precLabel = label_ + ": Operation Prec*x";
191 #ifdef BELOS_TEUCHOS_TIME_MONITOR
192 timerPrec_ = Teuchos::TimeMonitor::getNewCounter( precLabel );
193 #endif
194 }
195
196 // Set the linear system using the arguments newX and newB
197 if (newX != Teuchos::null)
198 X_ = newX;
199 if (newB != Teuchos::null)
200 B_ = newB;
201
202 // Invalidate the current linear system indices and multivectors.
203 curX_ = Teuchos::null;
204 curB_ = Teuchos::null;
205
206 // Temporarily set the rhsIndex_ to all the vectors of the linear system.
207 // This is necessary for the applyOp to perform correctly in computing the
208 // initial residual. It will be reset at the end of this method.
209 int numVecs = MVT::GetNumberVecs( *B_ );
210 rhsIndex_.resize(numVecs);
211 for (int i=0; i<numVecs; ++i)
212 rhsIndex_[i] = i;
213
214 // If we didn't set a matrix A, a left-hand side X, or a
215 // right-hand side B, then we didn't set the problem.
216 if (A_ == Teuchos::null || X_ == Teuchos::null || B_ == Teuchos::null) {
217 isSet_ = false;
218 return isSet_;
219 }
220
221 // Reset whether the solution has been updated. (We're just
222 // setting the problem now, so of course we haven't updated the
223 // solution yet.)
224 solutionUpdated_ = false;
225 // Compute the initial residuals.
226 if(Teuchos::is_null(R0_user_)) {
227 if (R0_==Teuchos::null || MVT::GetNumberVecs( *R0_ )!=MVT::GetNumberVecs( *B_ )) {
228 R0_ = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) );
229 }
230 this->computeCurrResVec( &*R0_, &*X_, &*B_ );
231
232 if (LP_!=Teuchos::null) {
233 if (PR0_==Teuchos::null || (PR0_==R0_) || (MVT::GetNumberVecs(*PR0_)!=MVT::GetNumberVecs(*B_))) {
234 PR0_ = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) );
235 }
236 this->applyLeftPrec( *R0_, *PR0_ );
237 }
238 else {
239 PR0_ = R0_;
240 }
241 }
242 else { // User specified the residuals
243 // If the user did not specify the right sized residual, create one and set R0_user_ to null.
244 if (MVT::GetNumberVecs( *R0_user_ )!=MVT::GetNumberVecs( *B_ )) {
245 Teuchos::RCP<MV> helper = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) );
246 this->computeCurrResVec( &*helper, &*X_, &*B_ );
247 R0_user_ = Teuchos::null;
248 R0_ = helper;
249 }
250
251 if (LP_!=Teuchos::null) {
252 // If the user provided preconditioned residual is the wrong size or pointing at
253 // the wrong object, create one and set the PR0_user_ to null.
254 if (PR0_user_==Teuchos::null || (PR0_user_==R0_) || (PR0_user_==R0_user_)
255 || (MVT::GetNumberVecs(*PR0_user_)!=MVT::GetNumberVecs(*B_))) {
256 Teuchos::RCP<MV> helper = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) );
257
258 // Get the initial residual from getInitResVec because R0_user_ may be null from above.
259 this->applyLeftPrec( *(this->getInitResVec()), *helper );
260 PR0_user_ = Teuchos::null;
261 PR0_ = helper;
262 }
263 }
264 else {
265 // The preconditioned initial residual vector is the residual vector.
266 // NOTE: R0_user_ could be null if incompatible.
267 if (R0_user_!=Teuchos::null)
268 {
269 PR0_user_ = R0_user_;
270 }
271 else
272 {
273 PR0_user_ = Teuchos::null;
274 PR0_ = R0_;
275 }
276 }
277 }
278
279 // Resize the rhsIndex_ back to zero length to correspond with null pointers for curX_ and curB_.
280 rhsIndex_.resize(0);
281
282 // The problem has been set and is ready for use.
283 isSet_ = true;
284 // Return isSet.
285 return isSet_;
286 }
287
288 template <class ScalarType, class MV, class OP>
289 void LinearMultiShiftProblem<ScalarType,MV,OP>::applyOp( const MV& x, MV& y ) const {
290
291 if (A_.get())
292 {
293 {
294 #ifdef BELOS_TEUCHOS_TIME_MONITOR
295 Teuchos::TimeMonitor OpTimer(*timerOp_);
296 #endif
297 OPT::Apply( *A_,x, y);
298 }
299 // Now add in shift based on rhsIndex_
300 if (addShift_)
301 {
302 std::vector<int> newIdx( 1 );
303 for (unsigned int i=0; i<rhsIndex_.size(); ++i)
304 {
305 newIdx[0] = i;
306 Teuchos::RCP<const MV> tmp_x = MVT::CloneView( x, newIdx );
307 Teuchos::RCP<MV> tmp_y = MVT::CloneViewNonConst( y, newIdx );
308 MVT::MvAddMv( rhsIndex_[i]*Teuchos::ScalarTraits<ScalarType>::one(), *tmp_x,
309 Teuchos::ScalarTraits<ScalarType>::one(), *tmp_y, *tmp_y );
310 }
311 }
312 }
313 else {
314 MVT::MvAddMv( Teuchos::ScalarTraits<ScalarType>::one(), x,
315 Teuchos::ScalarTraits<ScalarType>::zero(), x, y );
316 }
317 }
318
319 } // end Belos namespace
320
321 #endif /* BELOS_LINEAR_MULTIPROBLEM_HPP */
322
323