Belos Version of the Day
Loading...
Searching...
No Matches
BelosFixedPointIter.hpp
Go to the documentation of this file.
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_FIXEDPOINT_ITER_HPP
11#define BELOS_FIXEDPOINT_ITER_HPP
12
17#include "BelosConfigDefs.hpp"
18#include "BelosTypes.hpp"
20
23#include "BelosStatusTest.hpp"
26
27#include "Teuchos_ScalarTraits.hpp"
28#include "Teuchos_ParameterList.hpp"
29#include "Teuchos_TimeMonitor.hpp"
30
40namespace Belos {
41
42template<class ScalarType, class MV, class OP, class DM>
43class FixedPointIter : virtual public FixedPointIteration<ScalarType,MV,OP,DM> {
44
45 public:
46
47 //
48 // Convenience typedefs
49 //
52 typedef Teuchos::ScalarTraits<ScalarType> SCT;
53 typedef typename SCT::magnitudeType MagnitudeType;
54
56
57
64 const Teuchos::RCP<OutputManager<ScalarType> > &printer,
65 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > &tester,
66 Teuchos::ParameterList &params );
67
69 virtual ~FixedPointIter() {};
71
72
74
75
88 void iterate();
89
105
114
127
129
130
132
133
135 int getNumIters() const { return iter_; }
136
138 void resetNumIters( int iter = 0 ) { iter_ = iter; }
139
142 Teuchos::RCP<const MV> getNativeResiduals( std::vector<MagnitudeType> * /* norms */ ) const { return R_; }
143
145
147 Teuchos::RCP<MV> getCurrentUpdate() const { return Teuchos::null; }
148
150
152
153
155 const LinearProblem<ScalarType,MV,OP,DM>& getProblem() const { return *lp_; }
156
158 int getBlockSize() const { return numRHS_; }
159
161 void setBlockSize(int blockSize);
162
164 bool isInitialized() { return initialized_; }
165
167
168 private:
169
170 //
171 // Internal methods
172 //
174 void setStateSize();
175
176 //
177 // Classes inputed through constructor that define the linear problem to be solved.
178 //
179 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
180 const Teuchos::RCP<OutputManager<ScalarType> > om_;
181 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
182
183 // Algorithmic parameters
184 //
185 // blockSize_ is the solver block size.
186 int numRHS_;
187
188 //
189 // Current solver state
190 //
191 // initialized_ specifies that the basis vectors have been initialized and the iterate() routine
192 // is capable of running; _initialize is controlled by the initialize() member method
193 // For the implications of the state of initialized_, please see documentation for initialize()
194 bool initialized_;
195
196 // stateStorageInitialized_ specifies that the state storage has been initialized.
197 // This initialization may be postponed if the linear problem was generated without
198 // the right-hand side or solution vectors.
199 bool stateStorageInitialized_;
200
201 // Current number of iterations performed.
202 int iter_;
203
204 //
205 // State Storage
206 //
207 // Residual
208 Teuchos::RCP<MV> R_;
209 //
210 // Preconditioned residual
211 Teuchos::RCP<MV> Z_;
212 //
213
214};
215
217 // Constructor.
218 template<class ScalarType, class MV, class OP, class DM>
220 const Teuchos::RCP<OutputManager<ScalarType> > &printer,
221 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > &tester,
222 Teuchos::ParameterList &params ):
223 lp_(problem),
224 om_(printer),
225 stest_(tester),
226 numRHS_(0),
227 initialized_(false),
228 stateStorageInitialized_(false),
229 iter_(0)
230 {
231 setBlockSize(params.get("Block Size",MVT::GetNumberVecs(*problem->getCurrRHSVec())));
232 }
233
235 // Setup the state storage.
236 template<class ScalarType, class MV, class OP, class DM>
238 {
239 if (!stateStorageInitialized_) {
240 // Check if there is any multivector to clone from.
241 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
242 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
243 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
244 stateStorageInitialized_ = false;
245 return;
246 }
247 else {
248
249 // Initialize the state storage
250 // If the subspace has not be initialized before, generate it using the LHS or RHS from lp_.
251 if (R_ == Teuchos::null) {
252 // Get the multivector that is not null.
253 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
254 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
255 "Belos::FixedPointIter::setStateSize(): linear problem does not specify multivectors to clone from.");
256 R_ = MVT::Clone( *tmp, numRHS_ );
257 Z_ = MVT::Clone( *tmp, numRHS_ );
258 }
259
260 // State storage has now been initialized.
261 stateStorageInitialized_ = true;
262 }
263 }
264 }
265
267 // Set the block size and make necessary adjustments.
268 template<class ScalarType, class MV, class OP, class DM>
270 {
271 // This routine only allocates space; it doesn't not perform any computation
272 // any change in size will invalidate the state of the solver.
273
274 TEUCHOS_TEST_FOR_EXCEPTION(blockSize != MVT::GetNumberVecs(*lp_->getCurrRHSVec()), std::invalid_argument, "Belos::FixedPointIter::setBlockSize size must match # RHS.");
275
276 TEUCHOS_TEST_FOR_EXCEPTION(blockSize <= 0, std::invalid_argument, "Belos::FixedPointIter::setBlockSize was passed a non-positive argument.");
277 if (blockSize == numRHS_) {
278 // do nothing
279 return;
280 }
281
282 if (blockSize!=numRHS_)
283 stateStorageInitialized_ = false;
284
285 numRHS_ = blockSize;
286
287 initialized_ = false;
288
289 // Use the current blockSize_ to initialize the state storage.
290 setStateSize();
291
292 }
293
295 // Initialize this iteration object
296 template<class ScalarType, class MV, class OP, class DM>
298 {
299 // Initialize the state storage if it isn't already.
300 if (!stateStorageInitialized_)
301 setStateSize();
302
303 TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
304 "Belos::FixedPointIter::initialize(): Cannot initialize state storage!");
305
306 // NOTE: In FixedPointIter R_, the initial residual, is required!!!
307 //
308 std::string errstr("Belos::FixedPointIter::initialize(): Specified multivectors must have a consistent length and width.");
309
310 if (newstate.R != Teuchos::null) {
311 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*R_) != MVT::GetNumberVecs(*newstate.R),
312 std::invalid_argument, errstr );
313
314 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetGlobalLength(*newstate.R) != MVT::GetGlobalLength(*R_),
315 std::invalid_argument, errstr );
316 TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.R) != numRHS_,
317 std::invalid_argument, errstr );
318
319 // Copy basis vectors from newstate into V
320 if (newstate.R != R_) {
321 // copy over the initial residual (unpreconditioned).
322 MVT::Assign( *newstate.R, *R_ );
323 }
324
325 }
326 else {
327 TEUCHOS_TEST_FOR_EXCEPTION(newstate.R == Teuchos::null,std::invalid_argument,
328 "Belos::FixedPointIter::initialize(): FixedPointIterationState does not have initial residual.");
329 }
330
331 // The solver is initialized
332 initialized_ = true;
333 }
334
335
337 // Iterate until the status test informs us we should stop.
338 template<class ScalarType, class MV, class OP, class DM>
340 {
341 //
342 // Allocate/initialize data structures
343 //
344 if (initialized_ == false) {
345 initialize();
346 }
347
348 // Create convenience variables for zero and one.
349 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
350 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero(); // unused
351
352 // Get the current solution vector.
353 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
354
355 // Temp vector
356 Teuchos::RCP<MV> tmp = MVT::Clone( *R_, numRHS_ );
357
358 if (lp_->getRightPrec() != Teuchos::null) {
359 // Set rhs to initial residual
360 Teuchos::RCP<MV> rhs = MVT::CloneCopy( *R_ );
361
362 // Zero initial guess
363 MVT::MvInit( *Z_, zero );
364
366 // Iterate until the status test tells us to stop.
367 //
368 while (stest_->checkStatus(this) != Passed) {
369
370 // Increment the iteration
371 iter_++;
372
373 // Apply preconditioner
374 lp_->applyRightPrec( *R_, *tmp );
375
376 // Update solution vector
377 MVT::MvAddMv( one, *cur_soln_vec, one, *tmp, *cur_soln_vec );
378 lp_->updateSolution();
379
380 // Update solution vector
381 MVT::MvAddMv( one, *Z_, one, *tmp, *Z_ );
382
383 // Compute new residual
384 lp_->applyOp (*Z_, *tmp );
385 MVT::MvAddMv( one, *rhs, -one, *tmp, *R_ );
386
387 } // end while (sTest_->checkStatus(this) != Passed)
388
389 } else {
390 Teuchos::RCP<const MV> rhs = lp_->getCurrRHSVec();
391
393 // Iterate until the status test tells us to stop.
394 //
395 while (stest_->checkStatus(this) != Passed) {
396
397 // Increment the iteration
398 iter_++;
399
400 // Compute initial preconditioned residual
401 if ( lp_->getLeftPrec() != Teuchos::null ) {
402 lp_->applyLeftPrec( *R_, *Z_ );
403 }
404 else {
405 Z_ = R_;
406 }
407
408 // Update solution vector
409 MVT::MvAddMv(one,*cur_soln_vec,one,*Z_,*cur_soln_vec);
410 lp_->updateSolution();
411
412 // Compute new residual
413 lp_->applyOp(*cur_soln_vec,*tmp);
414 MVT::MvAddMv(one,*rhs,-one,*tmp,*R_);
415
416 } // end while (sTest_->checkStatus(this) != Passed)
417 }
418 }
419
420} // end Belos namespace
421
422#endif /* BELOS_FIXEDPOINT_ITER_HPP */
Belos header file which uses auto-configuration information to include necessary C++ headers.
Pure virtual base class which augments the basic interface for a fixed point linear solver iteration.
Class which describes the linear problem to be solved by the iterative solver.
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
This class implements the preconditioned fixed point iteration.
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Get the norms of the residuals native to the solver.
FixedPointIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
void iterate()
This method performs Fixed Point iterations until the status test indicates the need to stop or an er...
int getNumIters() const
Get the current iteration count.
MultiVecTraits< ScalarType, MV, DM > MVT
FixedPointIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, Teuchos::ParameterList &params)
FixedPointIter constructor with linear problem, solver utilities, and parameter list of solver option...
OperatorTraits< ScalarType, MV, OP > OPT
SCT::magnitudeType MagnitudeType
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
bool isInitialized()
States whether the solver has been initialized or not.
void initializeFixedPoint(FixedPointIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
virtual ~FixedPointIter()
Destructor.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).

Generated for Belos by doxygen 1.9.8