Belos  Version of the Day
BelosTFQMRSolMgr.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #ifndef BELOS_TFQMR_SOLMGR_HPP
43 #define BELOS_TFQMR_SOLMGR_HPP
44 
49 #include "BelosConfigDefs.hpp"
50 #include "BelosTypes.hpp"
51 
52 #include "BelosLinearProblem.hpp"
53 #include "BelosSolverManager.hpp"
54 
55 #include "BelosTFQMRIter.hpp"
58 #include "BelosStatusTestCombo.hpp"
60 #include "BelosOutputManager.hpp"
61 #ifdef BELOS_TEUCHOS_TIME_MONITOR
62 #include "Teuchos_TimeMonitor.hpp"
63 #endif
64 
78 namespace Belos {
79 
81 
82 
90  TFQMRSolMgrLinearProblemFailure(const std::string& what_arg) : BelosError(what_arg)
91  {}};
92 
99  class TFQMRSolMgrOrthoFailure : public BelosError {public:
100  TFQMRSolMgrOrthoFailure(const std::string& what_arg) : BelosError(what_arg)
101  {}};
102 
103  template<class ScalarType, class MV, class OP>
104  class TFQMRSolMgr : public SolverManager<ScalarType,MV,OP> {
105 
106  private:
109  typedef Teuchos::ScalarTraits<ScalarType> SCT;
110  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
111  typedef Teuchos::ScalarTraits<MagnitudeType> MT;
112 
113  public:
114 
116 
117 
123  TFQMRSolMgr();
124 
141  TFQMRSolMgr( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
142  const Teuchos::RCP<Teuchos::ParameterList> &pl );
143 
145  virtual ~TFQMRSolMgr() {};
147 
149 
150 
152  return *problem_;
153  }
154 
157  Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const;
158 
161  Teuchos::RCP<const Teuchos::ParameterList> getCurrentParameters() const { return params_; }
162 
168  Teuchos::Array<Teuchos::RCP<Teuchos::Time> > getTimers() const {
169  return Teuchos::tuple(timerSolve_);
170  }
171 
177  MagnitudeType achievedTol() const {
178  return achievedTol_;
179  }
180 
182  int getNumIters() const {
183  return numIters_;
184  }
185 
193  bool isLOADetected() const { return false; }
194 
196 
198 
199 
201  void setProblem( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem ) { problem_ = problem; }
202 
204  void setParameters( const Teuchos::RCP<Teuchos::ParameterList> &params );
205 
207 
209 
210 
214  void reset( const ResetType type ) { if ((type & Belos::Problem) && !Teuchos::is_null(problem_)) problem_->setProblem(); }
216 
218 
219 
237  ReturnType solve();
238 
240 
243 
245  std::string description() const;
246 
248 
249  private:
250 
251  // Method for checking current status test against defined linear problem.
252  bool checkStatusTest();
253 
254  // Linear problem.
255  Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
256 
257  // Output manager.
258  Teuchos::RCP<OutputManager<ScalarType> > printer_;
259  Teuchos::RCP<std::ostream> outputStream_;
260 
261  // Status test.
262  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
263  Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
264  Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
265  Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_, impConvTest_;
266  Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
267 
268  // Current parameter list.
269  Teuchos::RCP<Teuchos::ParameterList> params_;
270 
271  // Default solver values.
272  static const MagnitudeType convtol_default_;
273  static const MagnitudeType impTolScale_default_;
274  static const int maxIters_default_;
275  static const bool expResTest_default_;
276  static const int verbosity_default_;
277  static const int outputStyle_default_;
278  static const int outputFreq_default_;
279  static const std::string impResScale_default_;
280  static const std::string expResScale_default_;
281  static const std::string label_default_;
282  static const Teuchos::RCP<std::ostream> outputStream_default_;
283 
284  // Current solver values.
285  MagnitudeType convtol_, impTolScale_, achievedTol_;
286  int maxIters_, numIters_;
287  int verbosity_, outputStyle_, outputFreq_;
288  int blockSize_;
289  bool expResTest_;
290  std::string impResScale_, expResScale_;
291 
292  // Timers.
293  std::string label_;
294  Teuchos::RCP<Teuchos::Time> timerSolve_;
295 
296  // Internal state variables.
297  bool isSet_, isSTSet_;
298  };
299 
300 
301 // Default solver values.
302 template<class ScalarType, class MV, class OP>
303 const typename TFQMRSolMgr<ScalarType,MV,OP>::MagnitudeType TFQMRSolMgr<ScalarType,MV,OP>::convtol_default_ = 1e-8;
304 
305 template<class ScalarType, class MV, class OP>
306 const typename TFQMRSolMgr<ScalarType,MV,OP>::MagnitudeType TFQMRSolMgr<ScalarType,MV,OP>::impTolScale_default_ = 10.0;
307 
308 template<class ScalarType, class MV, class OP>
309 const int TFQMRSolMgr<ScalarType,MV,OP>::maxIters_default_ = 1000;
310 
311 template<class ScalarType, class MV, class OP>
312 const bool TFQMRSolMgr<ScalarType,MV,OP>::expResTest_default_ = false;
313 
314 template<class ScalarType, class MV, class OP>
315 const int TFQMRSolMgr<ScalarType,MV,OP>::verbosity_default_ = Belos::Errors;
316 
317 template<class ScalarType, class MV, class OP>
318 const int TFQMRSolMgr<ScalarType,MV,OP>::outputStyle_default_ = Belos::General;
319 
320 template<class ScalarType, class MV, class OP>
321 const int TFQMRSolMgr<ScalarType,MV,OP>::outputFreq_default_ = -1;
322 
323 template<class ScalarType, class MV, class OP>
324 const std::string TFQMRSolMgr<ScalarType,MV,OP>::impResScale_default_ = "Norm of Preconditioned Initial Residual";
325 
326 template<class ScalarType, class MV, class OP>
327 const std::string TFQMRSolMgr<ScalarType,MV,OP>::expResScale_default_ = "Norm of Initial Residual";
328 
329 template<class ScalarType, class MV, class OP>
330 const std::string TFQMRSolMgr<ScalarType,MV,OP>::label_default_ = "Belos";
331 
332 template<class ScalarType, class MV, class OP>
333 const Teuchos::RCP<std::ostream> TFQMRSolMgr<ScalarType,MV,OP>::outputStream_default_ = Teuchos::rcp(&std::cout,false);
334 
335 
336 // Empty Constructor
337 template<class ScalarType, class MV, class OP>
339  outputStream_(outputStream_default_),
340  convtol_(convtol_default_),
341  impTolScale_(impTolScale_default_),
342  achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
343  maxIters_(maxIters_default_),
344  numIters_(0),
345  verbosity_(verbosity_default_),
346  outputStyle_(outputStyle_default_),
347  outputFreq_(outputFreq_default_),
348  blockSize_(1),
349  expResTest_(expResTest_default_),
350  impResScale_(impResScale_default_),
351  expResScale_(expResScale_default_),
352  label_(label_default_),
353  isSet_(false),
354  isSTSet_(false)
355 {}
356 
357 
358 // Basic Constructor
359 template<class ScalarType, class MV, class OP>
361  const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
362  const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
363  problem_(problem),
364  outputStream_(outputStream_default_),
365  convtol_(convtol_default_),
366  impTolScale_(impTolScale_default_),
367  achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
368  maxIters_(maxIters_default_),
369  numIters_(0),
370  verbosity_(verbosity_default_),
371  outputStyle_(outputStyle_default_),
372  outputFreq_(outputFreq_default_),
373  blockSize_(1),
374  expResTest_(expResTest_default_),
375  impResScale_(impResScale_default_),
376  expResScale_(expResScale_default_),
377  label_(label_default_),
378  isSet_(false),
379  isSTSet_(false)
380 {
381  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
382 
383  // If the parameter list pointer is null, then set the current parameters to the default parameter list.
384  if ( !is_null(pl) ) {
385  setParameters( pl );
386  }
387 }
388 
389 template<class ScalarType, class MV, class OP>
390 void TFQMRSolMgr<ScalarType,MV,OP>::setParameters( const Teuchos::RCP<Teuchos::ParameterList> &params )
391 {
392  // Create the internal parameter list if ones doesn't already exist.
393  if (params_ == Teuchos::null) {
394  params_ = Teuchos::rcp( new Teuchos::ParameterList(*getValidParameters()) );
395  }
396  else {
397  params->validateParameters(*getValidParameters());
398  }
399 
400  // Check for maximum number of iterations
401  if (params->isParameter("Maximum Iterations")) {
402  maxIters_ = params->get("Maximum Iterations",maxIters_default_);
403 
404  // Update parameter in our list and in status test.
405  params_->set("Maximum Iterations", maxIters_);
406  if (maxIterTest_!=Teuchos::null)
407  maxIterTest_->setMaxIters( maxIters_ );
408  }
409 
410  // Check for blocksize
411  if (params->isParameter("Block Size")) {
412  blockSize_ = params->get("Block Size",1);
413  TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ != 1, std::invalid_argument,
414  "Belos::TFQMRSolMgr: \"Block Size\" must be 1.");
415 
416  // Update parameter in our list.
417  params_->set("Block Size", blockSize_);
418  }
419 
420  // Check to see if the timer label changed.
421  if (params->isParameter("Timer Label")) {
422  std::string tempLabel = params->get("Timer Label", label_default_);
423 
424  // Update parameter in our list and solver timer
425  if (tempLabel != label_) {
426  label_ = tempLabel;
427  params_->set("Timer Label", label_);
428  std::string solveLabel = label_ + ": TFQMRSolMgr total solve time";
429 #ifdef BELOS_TEUCHOS_TIME_MONITOR
430  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
431 #endif
432  }
433  }
434 
435  // Check for a change in verbosity level
436  if (params->isParameter("Verbosity")) {
437  if (Teuchos::isParameterType<int>(*params,"Verbosity")) {
438  verbosity_ = params->get("Verbosity", verbosity_default_);
439  } else {
440  verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,"Verbosity");
441  }
442 
443  // Update parameter in our list.
444  params_->set("Verbosity", verbosity_);
445  if (printer_ != Teuchos::null)
446  printer_->setVerbosity(verbosity_);
447  }
448 
449  // Check for a change in output style
450  if (params->isParameter("Output Style")) {
451  if (Teuchos::isParameterType<int>(*params,"Output Style")) {
452  outputStyle_ = params->get("Output Style", outputStyle_default_);
453  } else {
454  outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,"Output Style");
455  }
456 
457  // Reconstruct the convergence test if the explicit residual test is not being used.
458  params_->set("Output Style", outputStyle_);
459  isSTSet_ = false;
460  }
461 
462  // output stream
463  if (params->isParameter("Output Stream")) {
464  outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,"Output Stream");
465 
466  // Update parameter in our list.
467  params_->set("Output Stream", outputStream_);
468  if (printer_ != Teuchos::null)
469  printer_->setOStream( outputStream_ );
470  }
471 
472  // frequency level
473  if (verbosity_ & Belos::StatusTestDetails) {
474  if (params->isParameter("Output Frequency")) {
475  outputFreq_ = params->get("Output Frequency", outputFreq_default_);
476  }
477 
478  // Update parameter in out list and output status test.
479  params_->set("Output Frequency", outputFreq_);
480  if (outputTest_ != Teuchos::null)
481  outputTest_->setOutputFrequency( outputFreq_ );
482  }
483 
484  // Create output manager if we need to.
485  if (printer_ == Teuchos::null) {
486  printer_ = Teuchos::rcp( new OutputManager<ScalarType>(verbosity_, outputStream_) );
487  }
488 
489  // Check for convergence tolerance
490  if (params->isParameter("Convergence Tolerance")) {
491  convtol_ = params->get("Convergence Tolerance",convtol_default_);
492 
493  // Update parameter in our list.
494  params_->set("Convergence Tolerance", convtol_);
495  isSTSet_ = false;
496  }
497 
498  // Check for implicit residual scaling
499  if (params->isParameter("Implicit Tolerance Scale Factor")) {
500  impTolScale_ = params->get("Implicit Tolerance Scale Factor",impTolScale_default_);
501 
502  // Update parameter in our list.
503  params_->set("Implicit Tolerance Scale Factor", impTolScale_);
504  isSTSet_ = false;
505  }
506 
507  // Check for a change in scaling, if so we need to build new residual tests.
508  if (params->isParameter("Implicit Residual Scaling")) {
509  std::string tempImpResScale = Teuchos::getParameter<std::string>( *params, "Implicit Residual Scaling" );
510 
511  // Only update the scaling if it's different.
512  if (impResScale_ != tempImpResScale) {
513  impResScale_ = tempImpResScale;
514 
515  // Update parameter in our list and residual tests
516  params_->set("Implicit Residual Scaling", impResScale_);
517 
518  // Make sure the convergence test gets constructed again.
519  isSTSet_ = false;
520  }
521  }
522 
523  if (params->isParameter("Explicit Residual Scaling")) {
524  std::string tempExpResScale = Teuchos::getParameter<std::string>( *params, "Explicit Residual Scaling" );
525 
526  // Only update the scaling if it's different.
527  if (expResScale_ != tempExpResScale) {
528  expResScale_ = tempExpResScale;
529 
530  // Update parameter in our list and residual tests
531  params_->set("Explicit Residual Scaling", expResScale_);
532 
533  // Make sure the convergence test gets constructed again.
534  isSTSet_ = false;
535  }
536  }
537 
538  if (params->isParameter("Explicit Residual Test")) {
539  expResTest_ = Teuchos::getParameter<bool>( *params,"Explicit Residual Test" );
540 
541  // Reconstruct the convergence test if the explicit residual test is not being used.
542  params_->set("Explicit Residual Test", expResTest_);
543  if (expConvTest_ == Teuchos::null) {
544  isSTSet_ = false;
545  }
546  }
547 
548  // Create the timer if we need to.
549  if (timerSolve_ == Teuchos::null) {
550  std::string solveLabel = label_ + ": TFQMRSolMgr total solve time";
551 #ifdef BELOS_TEUCHOS_TIME_MONITOR
552  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
553 #endif
554  }
555 
556  // Inform the solver manager that the current parameters were set.
557  isSet_ = true;
558 }
559 
560 
561 // Check the status test versus the defined linear problem
562 template<class ScalarType, class MV, class OP>
564 
565  typedef Belos::StatusTestCombo<ScalarType,MV,OP> StatusTestCombo_t;
566  typedef Belos::StatusTestGenResNorm<ScalarType,MV,OP> StatusTestGenResNorm_t;
567 
568  // Basic test checks maximum iterations and native residual.
569  maxIterTest_ = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>( maxIters_ ) );
570 
571  if (expResTest_) {
572 
573  // Implicit residual test, using the native residual to determine if convergence was achieved.
574  Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
575  Teuchos::rcp( new StatusTestGenResNorm_t( impTolScale_*convtol_ ) );
576  tmpImpConvTest->defineScaleForm( convertStringToScaleType(impResScale_), Belos::TwoNorm );
577  impConvTest_ = tmpImpConvTest;
578 
579  // Explicit residual test once the native residual is below the tolerance
580  Teuchos::RCP<StatusTestGenResNorm_t> tmpExpConvTest =
581  Teuchos::rcp( new StatusTestGenResNorm_t( convtol_ ) );
582  tmpExpConvTest->defineResForm( StatusTestGenResNorm_t::Explicit, Belos::TwoNorm );
583  tmpExpConvTest->defineScaleForm( convertStringToScaleType(expResScale_), Belos::TwoNorm );
584  expConvTest_ = tmpExpConvTest;
585 
586  // The convergence test is a combination of the "cheap" implicit test and explicit test.
587  convTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
588  }
589  else {
590 
591  // Implicit residual test, using the native residual to determine if convergence was achieved.
592  Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
593  Teuchos::rcp( new StatusTestGenResNorm_t( convtol_ ) );
594  tmpImpConvTest->defineScaleForm( convertStringToScaleType(impResScale_), Belos::TwoNorm );
595  impConvTest_ = tmpImpConvTest;
596 
597  // Set the explicit and total convergence test to this implicit test that checks for accuracy loss.
598  expConvTest_ = impConvTest_;
599  convTest_ = impConvTest_;
600  }
601  sTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
602 
603  // Create the status test output class.
604  // This class manages and formats the output from the status test.
605  StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
606  outputTest_ = stoFactory.create( printer_, sTest_, outputFreq_, Passed+Failed+Undefined );
607 
608  // Set the solver string for the output test
609  std::string solverDesc = " TFQMR ";
610  outputTest_->setSolverDesc( solverDesc );
611 
612 
613  // The status test is now set.
614  isSTSet_ = true;
615 
616  return false;
617 }
618 
619 
620 template<class ScalarType, class MV, class OP>
621 Teuchos::RCP<const Teuchos::ParameterList>
623 {
624  static Teuchos::RCP<const Teuchos::ParameterList> validPL;
625 
626  // Set all the valid parameters and their default values.
627  if(is_null(validPL)) {
628  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
629  pl->set("Convergence Tolerance", convtol_default_,
630  "The relative residual tolerance that needs to be achieved by the\n"
631  "iterative solver in order for the linear system to be declared converged.");
632  pl->set("Implicit Tolerance Scale Factor", impTolScale_default_,
633  "The scale factor used by the implicit residual test when explicit residual\n"
634  "testing is used. May enable faster convergence when TFQMR bound is too loose.");
635  pl->set("Maximum Iterations", maxIters_default_,
636  "The maximum number of block iterations allowed for each\n"
637  "set of RHS solved.");
638  pl->set("Verbosity", verbosity_default_,
639  "What type(s) of solver information should be outputted\n"
640  "to the output stream.");
641  pl->set("Output Style", outputStyle_default_,
642  "What style is used for the solver information outputted\n"
643  "to the output stream.");
644  pl->set("Output Frequency", outputFreq_default_,
645  "How often convergence information should be outputted\n"
646  "to the output stream.");
647  pl->set("Output Stream", outputStream_default_,
648  "A reference-counted pointer to the output stream where all\n"
649  "solver output is sent.");
650  pl->set("Explicit Residual Test", expResTest_default_,
651  "Whether the explicitly computed residual should be used in the convergence test.");
652  pl->set("Implicit Residual Scaling", impResScale_default_,
653  "The type of scaling used in the implicit residual convergence test.");
654  pl->set("Explicit Residual Scaling", expResScale_default_,
655  "The type of scaling used in the explicit residual convergence test.");
656  pl->set("Timer Label", label_default_,
657  "The string to use as a prefix for the timer labels.");
658  // pl->set("Restart Timers", restartTimers_);
659  validPL = pl;
660  }
661  return validPL;
662 }
663 
664 
665 // solve()
666 template<class ScalarType, class MV, class OP>
668 
669  // Set the current parameters if they were not set before.
670  // NOTE: This may occur if the user generated the solver manager with the default constructor and
671  // then didn't set any parameters using setParameters().
672  if (!isSet_) {
673  setParameters(Teuchos::parameterList(*getValidParameters()));
674  }
675 
676  TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,TFQMRSolMgrLinearProblemFailure,
677  "Belos::TFQMRSolMgr::solve(): Linear problem is not a valid object.");
678 
679  TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(),TFQMRSolMgrLinearProblemFailure,
680  "Belos::TFQMRSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
681 
682  if (!isSTSet_) {
683  TEUCHOS_TEST_FOR_EXCEPTION( checkStatusTest(),TFQMRSolMgrLinearProblemFailure,
684  "Belos::TFQMRSolMgr::solve(): Linear problem and requested status tests are incompatible.");
685  }
686 
687  // Create indices for the linear systems to be solved.
688  int startPtr = 0;
689  int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
690  int numCurrRHS = blockSize_;
691 
692  std::vector<int> currIdx, currIdx2;
693 
694  // The index set is generated that informs the linear problem that some linear systems are augmented.
695  currIdx.resize( blockSize_ );
696  currIdx2.resize( blockSize_ );
697  for (int i=0; i<numCurrRHS; ++i)
698  { currIdx[i] = startPtr+i; currIdx2[i]=i; }
699 
700  // Inform the linear problem of the current linear system to solve.
701  problem_->setLSIndex( currIdx );
702 
704  // Parameter list
705  Teuchos::ParameterList plist;
706  plist.set("Block Size",blockSize_);
707 
708  // Reset the status test.
709  outputTest_->reset();
710 
711  // Assume convergence is achieved, then let any failed convergence set this to false.
712  bool isConverged = true;
713 
715  // TFQMR solver
716 
717  Teuchos::RCP<TFQMRIter<ScalarType,MV,OP> > tfqmr_iter =
718  Teuchos::rcp( new TFQMRIter<ScalarType,MV,OP>(problem_,printer_,outputTest_,plist) );
719 
720  // Enter solve() iterations
721  {
722 #ifdef BELOS_TEUCHOS_TIME_MONITOR
723  Teuchos::TimeMonitor slvtimer(*timerSolve_);
724 #endif
725 
726  while ( numRHS2Solve > 0 ) {
727  //
728  // Reset the active / converged vectors from this block
729  std::vector<int> convRHSIdx;
730  std::vector<int> currRHSIdx( currIdx );
731  currRHSIdx.resize(numCurrRHS);
732 
733  // Reset the number of iterations.
734  tfqmr_iter->resetNumIters();
735 
736  // Reset the number of calls that the status test output knows about.
737  outputTest_->resetNumCalls();
738 
739  // Get the current residual for this block of linear systems.
740  Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())), currIdx );
741 
742  // Set the new state and initialize the solver.
744  newstate.R = R_0;
745  tfqmr_iter->initializeTFQMR(newstate);
746 
747  while(1) {
748 
749  // tell tfqmr_iter to iterate
750  try {
751  tfqmr_iter->iterate();
752 
754  //
755  // check convergence first
756  //
758  if ( convTest_->getStatus() == Passed ) {
759  // We have convergence of the linear system.
760  break; // break from while(1){tfqmr_iter->iterate()}
761  }
763  //
764  // check for maximum iterations
765  //
767  else if ( maxIterTest_->getStatus() == Passed ) {
768  // we don't have convergence
769  isConverged = false;
770  break; // break from while(1){tfqmr_iter->iterate()}
771  }
772 
774  //
775  // we returned from iterate(), but none of our status tests Passed.
776  // something is wrong, and it is probably our fault.
777  //
779 
780  else {
781  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
782  "Belos::TFQMRSolMgr::solve(): Invalid return from TFQMRIter::iterate().");
783  }
784  }
785  catch (const std::exception &e) {
786  printer_->stream(Errors) << "Error! Caught std::exception in TFQMRIter::iterate() at iteration "
787  << tfqmr_iter->getNumIters() << std::endl
788  << e.what() << std::endl;
789  throw;
790  }
791  }
792 
793  // Update the current solution with the update computed by the iteration object.
794  problem_->updateSolution( tfqmr_iter->getCurrentUpdate(), true );
795 
796  // Inform the linear problem that we are finished with this block linear system.
797  problem_->setCurrLS();
798 
799  // Update indices for the linear systems to be solved.
800  startPtr += numCurrRHS;
801  numRHS2Solve -= numCurrRHS;
802  if ( numRHS2Solve > 0 ) {
803  numCurrRHS = blockSize_;
804 
805  currIdx.resize( blockSize_ );
806  currIdx2.resize( blockSize_ );
807  for (int i=0; i<numCurrRHS; ++i)
808  { currIdx[i] = startPtr+i; currIdx2[i] = i; }
809  // Set the next indices.
810  problem_->setLSIndex( currIdx );
811 
812  // Set the new blocksize for the solver.
813  tfqmr_iter->setBlockSize( blockSize_ );
814  }
815  else {
816  currIdx.resize( numRHS2Solve );
817  }
818 
819  }// while ( numRHS2Solve > 0 )
820 
821  }
822 
823  // print final summary
824  sTest_->print( printer_->stream(FinalSummary) );
825 
826  // print timing information
827 #ifdef BELOS_TEUCHOS_TIME_MONITOR
828  // Calling summarize() can be expensive, so don't call unless the
829  // user wants to print out timing details. summarize() will do all
830  // the work even if it's passed a "black hole" output stream.
831  if (verbosity_ & TimingDetails)
832  Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
833 #endif
834 
835  // get iteration information for this solve
836  numIters_ = maxIterTest_->getNumIters();
837 
838  // Save the convergence test value ("achieved tolerance") for this
839  // solve. For this solver, convTest_ may either be a single
840  // (implicit) residual norm test, or a combination of two residual
841  // norm tests. In the latter case, the master convergence test
842  // convTest_ is a SEQ combo of the implicit resp. explicit tests.
843  // If the implicit test never passes, then the explicit test won't
844  // ever be executed. This manifests as
845  // expConvTest_->getTestValue()->size() < 1. We deal with this case
846  // by using the values returned by impConvTest_->getTestValue().
847  {
848  // We'll fetch the vector of residual norms one way or the other.
849  const std::vector<MagnitudeType>* pTestValues = NULL;
850  if (expResTest_) {
851  pTestValues = expConvTest_->getTestValue();
852  if (pTestValues == NULL || pTestValues->size() < 1) {
853  pTestValues = impConvTest_->getTestValue();
854  }
855  }
856  else {
857  // Only the implicit residual norm test is being used.
858  pTestValues = impConvTest_->getTestValue();
859  }
860  TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
861  "Belos::TFQMRSolMgr::solve(): The implicit convergence test's "
862  "getTestValue() method returned NULL. Please report this bug to the "
863  "Belos developers.");
864  TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
865  "Belos::TMQMRSolMgr::solve(): The implicit convergence test's "
866  "getTestValue() method returned a vector of length zero. Please report "
867  "this bug to the Belos developers.");
868 
869  // FIXME (mfh 12 Dec 2011) Does pTestValues really contain the
870  // achieved tolerances for all vectors in the current solve(), or
871  // just for the vectors from the last deflation?
872  achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
873  }
874 
875  if (!isConverged) {
876  return Unconverged; // return from TFQMRSolMgr::solve()
877  }
878  return Converged; // return from TFQMRSolMgr::solve()
879 }
880 
881 // This method requires the solver manager to return a std::string that describes itself.
882 template<class ScalarType, class MV, class OP>
884 {
885  std::ostringstream oss;
886  oss << "Belos::TFQMRSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<">";
887  oss << "{}";
888  return oss.str();
889 }
890 
891 } // end Belos namespace
892 
893 #endif /* BELOS_TFQMR_SOLMGR_HPP */
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Definition: BelosTypes.cpp:87
Collection of types and exceptions used within the Belos solvers.
This class implements the preconditioned transpose-free QMR algorithm for solving non-Hermitian linea...
Belos&#39;s basic output manager for sending information of select verbosity levels to the appropriate ou...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the parameters the solver manager should use to solve the linear problem.
Class which manages the output and verbosity of the Belos solvers.
void reset(const ResetType type)
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
An implementation of StatusTestResNorm using a family of residual norms.
Belos concrete class for generating iterations with the preconditioned tranpose-free QMR (TFQMR) meth...
Teuchos::RCP< const MV > R
The current residual basis.
Belos::StatusTest class for specifying a maximum number of iterations.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem)
Set the linear problem that needs to be solved.
A factory class for generating StatusTestOutput objects.
TFQMRSolMgrOrthoFailure(const std::string &what_arg)
std::string description() const
Method to return description of the TFQMR solver manager.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
Definition: BelosTypes.hpp:205
Pure virtual base class which describes the basic interface for a solver manager. ...
A linear system to solve, and its associated information.
Structure to contain pointers to TFQMRIter state variables.
Class which describes the linear problem to be solved by the iterative solver.
bool isLOADetected() const
Whether loss of accuracy was detected during the last solve() invocation.
TFQMRSolMgr()
Empty constructor for TFQMRSolMgr. This constructor takes no arguments and sets the default values fo...
virtual ~TFQMRSolMgr()
Destructor.
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:154
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Return a reference to the linear problem being solved by this solver manager.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const
Get a parameter list containing the current parameters for this object.
ReturnType solve()
This method performs possibly repeated calls to the underlying linear solver&#39;s iterate() routine unti...
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
int getNumIters() const
Get the iteration count for the most recent call to solve().
A class for extending the status testing capabilities of Belos via logical combinations.
TFQMRSolMgrLinearProblemFailure(const std::string &what_arg)
Class which defines basic traits for the operator type.
TFQMRSolMgrOrthoFailure is thrown when the orthogonalization manager is unable to generate orthonorma...
MagnitudeType achievedTol() const
Tolerance achieved by the last solve() invocation.
Parent class to all Belos exceptions.
Definition: BelosTypes.hpp:60
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
TFQMRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i.e.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Get a parameter list containing the valid parameters for this object.
Belos header file which uses auto-configuration information to include necessary C++ headers...

Generated for Belos by doxygen 1.8.14