Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BelosBiCGStabSolMgr.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_BICGSTAB_SOLMGR_HPP
43 #define BELOS_BICGSTAB_SOLMGR_HPP
44 
49 #include "BelosConfigDefs.hpp"
50 #include "BelosTypes.hpp"
51 
52 #include "BelosLinearProblem.hpp"
53 #include "BelosSolverManager.hpp"
54 
55 #include "BelosBiCGStabIter.hpp"
58 #include "BelosStatusTestCombo.hpp"
60 #include "BelosOutputManager.hpp"
61 #ifdef BELOS_TEUCHOS_TIME_MONITOR
62 #include "Teuchos_TimeMonitor.hpp"
63 #endif
64 
81 namespace Belos {
82 
84 
85 
93  BiCGStabSolMgrLinearProblemFailure(const std::string& what_arg) : BelosError(what_arg)
94  {}};
95 
96  template<class ScalarType, class MV, class OP>
97  class BiCGStabSolMgr : public SolverManager<ScalarType,MV,OP> {
98 
99  private:
103  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
105 
106  public:
107 
109 
110 
116  BiCGStabSolMgr();
117 
129 
131  virtual ~BiCGStabSolMgr() {};
132 
136  }
138 
140 
141 
142  const LinearProblem<ScalarType,MV,OP>& getProblem() const override {
143  return *problem_;
144  }
145 
149 
153 
160  return Teuchos::tuple(timerSolve_);
161  }
162 
163 
174  MagnitudeType achievedTol() const override {
175  return achievedTol_;
176  }
177 
179  int getNumIters() const override {
180  return numIters_;
181  }
182 
186  bool isLOADetected() const override { return false; }
187 
189 
191 
192 
194  void setProblem( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem ) override { problem_ = problem; }
195 
197  void setParameters( const Teuchos::RCP<Teuchos::ParameterList> &params ) override;
198 
200 
202 
203 
207  void reset( const ResetType type ) override { if ((type & Belos::Problem) && !Teuchos::is_null(problem_)) problem_->setProblem(); }
209 
211 
212 
230  ReturnType solve() override;
231 
233 
236 
238  std::string description() const override;
239 
241  private:
242 
243  // Linear problem.
245 
246  // Output manager.
248  Teuchos::RCP<std::ostream> outputStream_;
249 
250  // Status test.
255 
256  // Current parameter list.
258 
264  mutable Teuchos::RCP<const Teuchos::ParameterList> validParams_;
265 
266  // Default solver values.
267  static constexpr int maxIters_default_ = 1000;
268  static constexpr bool showMaxResNormOnly_default_ = false;
269  static constexpr int verbosity_default_ = Belos::Errors;
270  static constexpr int outputStyle_default_ = Belos::General;
271  static constexpr int outputFreq_default_ = -1;
272  static constexpr int defQuorum_default_ = 1;
273  static constexpr const char * resScale_default_ = "Norm of Initial Residual";
274  static constexpr const char * label_default_ = "Belos";
275 
276  // Current solver values.
277  MagnitudeType convtol_,achievedTol_;
278  int maxIters_, numIters_;
279  int verbosity_, outputStyle_, outputFreq_, defQuorum_;
280  bool showMaxResNormOnly_;
281  std::string resScale_;
282 
283  // Timers.
284  std::string label_;
285  Teuchos::RCP<Teuchos::Time> timerSolve_;
286 
287  // Internal state variables.
288  bool isSet_;
289  };
290 
291 // Empty Constructor
292 template<class ScalarType, class MV, class OP>
294  outputStream_(Teuchos::rcpFromRef(std::cout)),
295  convtol_(DefaultSolverParameters::convTol),
296  maxIters_(maxIters_default_),
297  numIters_(0),
298  verbosity_(verbosity_default_),
299  outputStyle_(outputStyle_default_),
300  outputFreq_(outputFreq_default_),
301  defQuorum_(defQuorum_default_),
302  showMaxResNormOnly_(showMaxResNormOnly_default_),
303  resScale_(resScale_default_),
304  label_(label_default_),
305  isSet_(false)
306 {}
307 
308 // Basic Constructor
309 template<class ScalarType, class MV, class OP>
313  problem_(problem),
314  outputStream_(Teuchos::rcpFromRef(std::cout)),
315  convtol_(DefaultSolverParameters::convTol),
316  maxIters_(maxIters_default_),
317  numIters_(0),
318  verbosity_(verbosity_default_),
319  outputStyle_(outputStyle_default_),
320  outputFreq_(outputFreq_default_),
321  defQuorum_(defQuorum_default_),
322  showMaxResNormOnly_(showMaxResNormOnly_default_),
323  resScale_(resScale_default_),
324  label_(label_default_),
325  isSet_(false)
326 {
328  problem_.is_null (), std::invalid_argument,
329  "Belos::BiCGStabSolMgr two-argument constructor: "
330  "'problem' is null. You must supply a non-null Belos::LinearProblem "
331  "instance when calling this constructor.");
332 
333  if (! pl.is_null ()) {
334  // Set the parameters using the list that was passed in.
335  setParameters (pl);
336  }
337 }
338 
339 template<class ScalarType, class MV, class OP>
341 {
343  using Teuchos::parameterList;
344  using Teuchos::RCP;
345 
346  RCP<const ParameterList> defaultParams = getValidParameters();
347 
348  // Create the internal parameter list if one doesn't already exist.
349  if (params_.is_null()) {
350  params_ = parameterList (*defaultParams);
351  } else {
352  params->validateParameters (*defaultParams);
353  }
354 
355  // Check for maximum number of iterations
356  if (params->isParameter("Maximum Iterations")) {
357  maxIters_ = params->get("Maximum Iterations",maxIters_default_);
358 
359  // Update parameter in our list and in status test.
360  params_->set("Maximum Iterations", maxIters_);
361  if (maxIterTest_!=Teuchos::null)
362  maxIterTest_->setMaxIters( maxIters_ );
363  }
364 
365  // Check to see if the timer label changed.
366  if (params->isParameter("Timer Label")) {
367  std::string tempLabel = params->get("Timer Label", label_default_);
368 
369  // Update parameter in our list and solver timer
370  if (tempLabel != label_) {
371  label_ = tempLabel;
372  params_->set("Timer Label", label_);
373  std::string solveLabel = label_ + ": BiCGStabSolMgr total solve time";
374 #ifdef BELOS_TEUCHOS_TIME_MONITOR
375  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
376 #endif
377  }
378  }
379 
380  // Check for a change in verbosity level
381  if (params->isParameter("Verbosity")) {
382  if (Teuchos::isParameterType<int>(*params,"Verbosity")) {
383  verbosity_ = params->get("Verbosity", verbosity_default_);
384  } else {
385  verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,"Verbosity");
386  }
387 
388  // Update parameter in our list.
389  params_->set("Verbosity", verbosity_);
390  if (printer_ != Teuchos::null)
391  printer_->setVerbosity(verbosity_);
392  }
393 
394  // Check for a change in output style
395  if (params->isParameter("Output Style")) {
396  if (Teuchos::isParameterType<int>(*params,"Output Style")) {
397  outputStyle_ = params->get("Output Style", outputStyle_default_);
398  } else {
399  outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,"Output Style");
400  }
401 
402  // Reconstruct the convergence test if the explicit residual test is not being used.
403  params_->set("Output Style", outputStyle_);
404  outputTest_ = Teuchos::null;
405  }
406 
407  // output stream
408  if (params->isParameter("Output Stream")) {
409  outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,"Output Stream");
410 
411  // Update parameter in our list.
412  params_->set("Output Stream", outputStream_);
413  if (printer_ != Teuchos::null)
414  printer_->setOStream( outputStream_ );
415  }
416 
417  // frequency level
418  if (verbosity_ & Belos::StatusTestDetails) {
419  if (params->isParameter("Output Frequency")) {
420  outputFreq_ = params->get("Output Frequency", outputFreq_default_);
421  }
422 
423  // Update parameter in out list and output status test.
424  params_->set("Output Frequency", outputFreq_);
425  if (outputTest_ != Teuchos::null)
426  outputTest_->setOutputFrequency( outputFreq_ );
427  }
428 
429  // Create output manager if we need to.
430  if (printer_ == Teuchos::null) {
431  printer_ = Teuchos::rcp( new OutputManager<ScalarType>(verbosity_, outputStream_) );
432  }
433 
434  // Convergence
435  typedef Belos::StatusTestCombo<ScalarType,MV,OP> StatusTestCombo_t;
436  typedef Belos::StatusTestGenResNorm<ScalarType,MV,OP> StatusTestResNorm_t;
437 
438  // Check for convergence tolerance
439  if (params->isParameter("Convergence Tolerance")) {
440  if (params->isType<MagnitudeType> ("Convergence Tolerance")) {
441  convtol_ = params->get ("Convergence Tolerance",
442  static_cast<MagnitudeType> (DefaultSolverParameters::convTol));
443  }
444  else {
445  convtol_ = params->get ("Convergence Tolerance", DefaultSolverParameters::convTol);
446  }
447 
448  // Update parameter in our list and residual tests.
449  params_->set("Convergence Tolerance", convtol_);
450  if (convTest_ != Teuchos::null)
451  convTest_->setTolerance( convtol_ );
452  }
453 
454  if (params->isParameter("Show Maximum Residual Norm Only")) {
455  showMaxResNormOnly_ = Teuchos::getParameter<bool>(*params,"Show Maximum Residual Norm Only");
456 
457  // Update parameter in our list and residual tests
458  params_->set("Show Maximum Residual Norm Only", showMaxResNormOnly_);
459  if (convTest_ != Teuchos::null)
460  convTest_->setShowMaxResNormOnly( showMaxResNormOnly_ );
461  }
462 
463  // Check for a change in scaling, if so we need to build new residual tests.
464  bool newResTest = false;
465  {
466  // "Residual Scaling" is the old parameter name; "Implicit
467  // Residual Scaling" is the new name. We support both options for
468  // backwards compatibility.
469  std::string tempResScale = resScale_;
470  bool implicitResidualScalingName = false;
471  if (params->isParameter ("Residual Scaling")) {
472  tempResScale = params->get<std::string> ("Residual Scaling");
473  }
474  else if (params->isParameter ("Implicit Residual Scaling")) {
475  tempResScale = params->get<std::string> ("Implicit Residual Scaling");
476  implicitResidualScalingName = true;
477  }
478 
479  // Only update the scaling if it's different.
480  if (resScale_ != tempResScale) {
481  Belos::ScaleType resScaleType = convertStringToScaleType( tempResScale );
482  resScale_ = tempResScale;
483 
484  // Update parameter in our list and residual tests, using the
485  // given parameter name.
486  if (implicitResidualScalingName) {
487  params_->set ("Implicit Residual Scaling", resScale_);
488  }
489  else {
490  params_->set ("Residual Scaling", resScale_);
491  }
492 
493  if (! convTest_.is_null()) {
494  try {
495  convTest_->defineScaleForm( resScaleType, Belos::TwoNorm );
496  }
497  catch (std::exception& e) {
498  // Make sure the convergence test gets constructed again.
499  newResTest = true;
500  }
501  }
502  }
503  }
504 
505  // Get the deflation quorum, or number of converged systems before deflation is allowed
506  if (params->isParameter("Deflation Quorum")) {
507  defQuorum_ = params->get("Deflation Quorum", defQuorum_);
508  params_->set("Deflation Quorum", defQuorum_);
509  if (convTest_ != Teuchos::null)
510  convTest_->setQuorum( defQuorum_ );
511  }
512 
513  // Create status tests if we need to.
514 
515  // Basic test checks maximum iterations and native residual.
516  if (maxIterTest_ == Teuchos::null)
517  maxIterTest_ = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>( maxIters_ ) );
518 
519  // Implicit residual test, using the native residual to determine if convergence was achieved.
520  if (convTest_ == Teuchos::null || newResTest) {
521  convTest_ = Teuchos::rcp( new StatusTestResNorm_t( convtol_, defQuorum_, showMaxResNormOnly_ ) );
522  convTest_->defineScaleForm( convertStringToScaleType( resScale_ ), Belos::TwoNorm );
523  }
524 
525  if (sTest_ == Teuchos::null || newResTest)
526  sTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
527 
528  if (outputTest_ == Teuchos::null || newResTest) {
529 
530  // Create the status test output class.
531  // This class manages and formats the output from the status test.
532  StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
533  outputTest_ = stoFactory.create( printer_, sTest_, outputFreq_, Passed+Failed+Undefined );
534 
535  // Set the solver string for the output test
536  std::string solverDesc = " Pseudo Block BiCGStab ";
537  outputTest_->setSolverDesc( solverDesc );
538 
539  }
540 
541  // Create the timer if we need to.
542  if (timerSolve_ == Teuchos::null) {
543  std::string solveLabel = label_ + ": BiCGStabSolMgr total solve time";
544 #ifdef BELOS_TEUCHOS_TIME_MONITOR
545  timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
546 #endif
547  }
548 
549  // Inform the solver manager that the current parameters were set.
550  isSet_ = true;
551 }
552 
553 
554 template<class ScalarType, class MV, class OP>
557 {
559  using Teuchos::parameterList;
560  using Teuchos::RCP;
561 
562  if (validParams_.is_null()) {
563  // Set all the valid parameters and their default values.
564  RCP<ParameterList> pl = parameterList ();
565 
566  // The static_cast is to resolve an issue with older clang versions which
567  // would cause the constexpr to link fail. With c++17 the problem is resolved.
568  pl->set("Convergence Tolerance", static_cast<MagnitudeType>(DefaultSolverParameters::convTol),
569  "The relative residual tolerance that needs to be achieved by the\n"
570  "iterative solver in order for the linera system to be declared converged.");
571  pl->set("Maximum Iterations", static_cast<int>(maxIters_default_),
572  "The maximum number of block iterations allowed for each\n"
573  "set of RHS solved.");
574  pl->set("Verbosity", static_cast<int>(verbosity_default_),
575  "What type(s) of solver information should be outputted\n"
576  "to the output stream.");
577  pl->set("Output Style", static_cast<int>(outputStyle_default_),
578  "What style is used for the solver information outputted\n"
579  "to the output stream.");
580  pl->set("Output Frequency", static_cast<int>(outputFreq_default_),
581  "How often convergence information should be outputted\n"
582  "to the output stream.");
583  pl->set("Deflation Quorum", static_cast<int>(defQuorum_default_),
584  "The number of linear systems that need to converge before\n"
585  "they are deflated. This number should be <= block size.");
586  pl->set("Output Stream", Teuchos::rcpFromRef(std::cout),
587  "A reference-counted pointer to the output stream where all\n"
588  "solver output is sent.");
589  pl->set("Show Maximum Residual Norm Only", static_cast<bool>(showMaxResNormOnly_default_),
590  "When convergence information is printed, only show the maximum\n"
591  "relative residual norm when the block size is greater than one.");
592  pl->set("Implicit Residual Scaling", static_cast<const char *>(resScale_default_),
593  "The type of scaling used in the residual convergence test.");
594  // We leave the old name as a valid parameter for backwards
595  // compatibility (so that validateParametersAndSetDefaults()
596  // doesn't raise an exception if it encounters "Residual
597  // Scaling"). The new name was added for compatibility with other
598  // solvers, none of which use "Residual Scaling".
599  pl->set("Residual Scaling", static_cast<const char *>(resScale_default_),
600  "The type of scaling used in the residual convergence test. This "
601  "name is deprecated; the new name is \"Implicit Residual Scaling\".");
602  pl->set("Timer Label", static_cast<const char *>(label_default_),
603  "The string to use as a prefix for the timer labels.");
604  validParams_ = pl;
605  }
606  return validParams_;
607 }
608 
609 
610 template<class ScalarType, class MV, class OP>
612 {
613  // Set the current parameters if they were not set before.
614  // NOTE: This may occur if the user generated the solver manager with the default constructor and
615  // then didn't set any parameters using setParameters().
616  if (! isSet_) {
617  setParameters (params_);
618  }
619 
621  (! problem_->isProblemSet (), BiCGStabSolMgrLinearProblemFailure,
622  "Belos::BiCGStabSolMgr::solve: Linear problem is not ready. "
623  "You must call setProblem() on the LinearProblem before you may solve it.");
625  (problem_->isLeftPrec (), std::logic_error, "Belos::BiCGStabSolMgr::solve: "
626  "The left-preconditioned case has not yet been implemented. Please use "
627  "right preconditioning for now. If you need to use left preconditioning, "
628  "please contact the Belos developers. Left preconditioning is more "
629  "interesting in BiCGStab because whether it works depends on the initial "
630  "guess (e.g., an initial guess of all zeros might NOT work).");
631 
632  // Create indices for the linear systems to be solved.
633  int startPtr = 0;
634  int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
635  int numCurrRHS = numRHS2Solve;
636 
637  std::vector<int> currIdx( numRHS2Solve ), currIdx2( numRHS2Solve );
638  for (int i=0; i<numRHS2Solve; ++i) {
639  currIdx[i] = startPtr+i;
640  currIdx2[i]=i;
641  }
642 
643  // Inform the linear problem of the current linear system to solve.
644  problem_->setLSIndex( currIdx );
645 
647  // Parameter list (iteration)
649 
650  // Reset the status test.
651  outputTest_->reset();
652 
653  // Assume convergence is achieved, then let any failed convergence set this to false.
654  bool isConverged = true;
655 
657  // Pseudo-Block BiCGStab solver
658 
660  = Teuchos::rcp( new BiCGStabIter<ScalarType,MV,OP>(problem_,printer_,outputTest_,plist) );
661 
662  // Enter solve() iterations
663  {
664 #ifdef BELOS_TEUCHOS_TIME_MONITOR
665  Teuchos::TimeMonitor slvtimer(*timerSolve_);
666 #endif
667 
668  //bool first_time=true;
669  while ( numRHS2Solve > 0 ) {
670  // Reset the active / converged vectors from this block
671  std::vector<int> convRHSIdx;
672  std::vector<int> currRHSIdx( currIdx );
673  currRHSIdx.resize(numCurrRHS);
674 
675  // Reset the number of iterations.
676  bicgstab_iter->resetNumIters();
677 
678  // Reset the number of calls that the status test output knows about.
679  outputTest_->resetNumCalls();
680 
681  // Get the current residual for this block of linear systems.
682  Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitResVec())), currIdx );
683 
684  // Get a new state struct and initialize the solver.
686  newState.R = R_0;
687  bicgstab_iter->initializeBiCGStab(newState);
688 
689  while(1) {
690 
691  // tell block_gmres_iter to iterate
692  try {
693 
694  bicgstab_iter->iterate();
695 
697  //
698  // check convergence first
699  //
701  if ( convTest_->getStatus() == Passed ) {
702 
703  // Figure out which linear systems converged.
704  std::vector<int> convIdx = Teuchos::rcp_dynamic_cast<StatusTestGenResNorm<ScalarType,MV,OP> >(convTest_)->convIndices();
705 
706  // If the number of converged linear systems is equal to the
707  // number of current linear systems, then we are done with this block.
708  if (convIdx.size() == currRHSIdx.size())
709  break; // break from while(1){bicgstab_iter->iterate()}
710 
711  // Inform the linear problem that we are finished with this current linear system.
712  problem_->setCurrLS();
713 
714  // Reset currRHSIdx to have the right-hand sides that are left to converge for this block.
715  int have = 0;
716  std::vector<int> unconvIdx(currRHSIdx.size());
717  for (unsigned int i=0; i<currRHSIdx.size(); ++i) {
718  bool found = false;
719  for (unsigned int j=0; j<convIdx.size(); ++j) {
720  if (currRHSIdx[i] == convIdx[j]) {
721  found = true;
722  break;
723  }
724  }
725  if (!found) {
726  currIdx2[have] = currIdx2[i];
727  currRHSIdx[have++] = currRHSIdx[i];
728  }
729  }
730  currRHSIdx.resize(have);
731  currIdx2.resize(have);
732 
733  // Set the remaining indices after deflation.
734  problem_->setLSIndex( currRHSIdx );
735 
736  // Get the current residual vector.
737  std::vector<MagnitudeType> norms;
738  R_0 = MVT::CloneCopy( *(bicgstab_iter->getNativeResiduals(&norms)),currIdx2 );
739  for (int i=0; i<have; ++i) { currIdx2[i] = i; }
740 
741  // Set the new state and initialize the solver.
743  defstate.R = R_0;
744  bicgstab_iter->initializeBiCGStab(defstate);
745  }
746 
748  //
749  // check for maximum iterations
750  //
752  else if ( maxIterTest_->getStatus() == Passed ) {
753  // we don't have convergence
754  isConverged = false;
755  break; // break from while(1){bicgstab_iter->iterate()}
756  }
757 
759  //
760  // we returned from iterate(), but none of our status tests Passed.
761  // breakdown was detected within the solver iteration.
762  //
764 
765  else if ( bicgstab_iter->breakdownDetected() ) {
766  // we don't have convergence
767  isConverged = false;
768  printer_->stream(Warnings) <<
769  "Belos::BiCGStabSolMgr::solve(): Warning! Solver has experienced a breakdown!" << std::endl;
770  break; // break from while(1){bicgstab_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::BiCGStabSolMgr::solve(): Invalid return from BiCGStabIter::iterate().");
783  }
784  }
785  catch (const StatusTestNaNError& e) {
786  // A NaN was detected in the solver. Set the solution to zero and return unconverged.
787  achievedTol_ = MT::one();
788  Teuchos::RCP<MV> X = problem_->getLHS();
789  MVT::MvInit( *X, SCT::zero() );
790  printer_->stream(Warnings) << "Belos::BiCGStabSolMgr::solve(): Warning! NaN has been detected!"
791  << std::endl;
792  return Unconverged;
793  }
794  catch (const std::exception &e) {
795  printer_->stream(Errors) << "Error! Caught std::exception in BiCGStabIter::iterate() at iteration "
796  << bicgstab_iter->getNumIters() << std::endl
797  << e.what() << std::endl;
798  throw;
799  }
800  }
801 
802  // Inform the linear problem that we are finished with this block linear system.
803  problem_->setCurrLS();
804 
805  // Update indices for the linear systems to be solved.
806  startPtr += numCurrRHS;
807  numRHS2Solve -= numCurrRHS;
808 
809  if ( numRHS2Solve > 0 ) {
810 
811  numCurrRHS = numRHS2Solve;
812  currIdx.resize( numCurrRHS );
813  currIdx2.resize( numCurrRHS );
814  for (int i=0; i<numCurrRHS; ++i)
815  { currIdx[i] = startPtr+i; currIdx2[i] = i; }
816 
817  // Set the next indices.
818  problem_->setLSIndex( currIdx );
819  }
820  else {
821  currIdx.resize( numRHS2Solve );
822  }
823 
824  //first_time=false;
825  }// while ( numRHS2Solve > 0 )
826 
827  }
828 
829  // print final summary
830  sTest_->print( printer_->stream(FinalSummary) );
831 
832  // print timing information
833 #ifdef BELOS_TEUCHOS_TIME_MONITOR
834  // Calling summarize() can be expensive, so don't call unless the
835  // user wants to print out timing details. summarize() will do all
836  // the work even if it's passed a "black hole" output stream.
837  if (verbosity_ & TimingDetails)
838  Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
839 #endif
840 
841  // get iteration information for this solve
842  numIters_ = maxIterTest_->getNumIters();
843 
844 
845  // Save the convergence test value ("achieved tolerance") for this
846  // solve.
847  const std::vector<MagnitudeType>* pTestValues = convTest_->getTestValue();
848  achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
849 
850 
851  if (!isConverged ) {
852  return Unconverged; // return from BiCGStabSolMgr::solve()
853  }
854  return Converged; // return from BiCGStabSolMgr::solve()
855 }
856 
857 // This method requires the solver manager to return a std::string that describes itself.
858 template<class ScalarType, class MV, class OP>
860 {
861  std::ostringstream oss;
862  oss << "Belos::BiCGStabSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<">";
863  oss << "{";
864  oss << "}";
865  return oss.str();
866 }
867 
868 
869 
870 } // end Belos namespace
871 
872 #endif /* BELOS_BICGSTAB_SOLMGR_HPP */
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
Definition: BelosTypes.cpp:106
Collection of types and exceptions used within the Belos solvers.
Belos&#39;s basic output manager for sending information of select verbosity levels to the appropriate ou...
This class implements the pseudo-block BiCGStab iteration, where the basic BiCGStab algorithm is perf...
Class which manages the output and verbosity of the Belos solvers.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params) override
Set the parameters the solver manager should use to solve the linear problem.
Teuchos::RCP< const MV > R
The current residual.
ScaleType
The type of scaling to use on the residual norm value.
Definition: BelosTypes.hpp:120
T & get(const std::string &name, T def_value)
static RCP< Time > getNewCounter(const std::string &name)
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
A factory class for generating StatusTestOutput objects.
Structure to contain pointers to BiCGStabIteration state variables.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
An implementation of StatusTestResNorm using a family of residual norms.
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
static const double convTol
Default convergence tolerance.
Definition: BelosTypes.hpp:293
Belos::StatusTest class for specifying a maximum number of iterations.
static std::string name()
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
A factory class for generating StatusTestOutput objects.
Traits class which defines basic operations on multivectors.
Belos::StatusTest for logically combining several status tests.
BiCGStabSolMgrLinearProblemFailure(const std::string &what_arg)
bool isParameter(const std::string &name) const
A Belos::StatusTest class for specifying a maximum number of iterations.
ResetType
How to reset the solver.
Definition: BelosTypes.hpp:206
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Pure virtual base class which describes the basic interface for a solver manager. ...
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
BiCGStabSolMgr()
Empty constructor for BiCGStabSolMgr. This constructor takes no arguments and sets the default values...
virtual ~BiCGStabSolMgr()
Destructor.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
BiCGStabSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i...
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver&#39;s iterate() routine unti...
ReturnType
Whether the Belos solve converged for all linear systems.
Definition: BelosTypes.hpp:155
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
The Belos::BiCGStabSolMgr provides a powerful and fully-featured solver manager over the pseudo-block...
Teuchos::RCP< StatusTestOutput< ScalarType, MV, OP > > create(const Teuchos::RCP< OutputManager< ScalarType > > &printer, Teuchos::RCP< StatusTest< ScalarType, MV, OP > > test, int mod, int printStates)
Create the StatusTestOutput object specified by the outputStyle.
std::string description() const override
Method to return description of the block BiCGStab solver manager.
Belos::StatusTestResNorm for specifying general residual norm stopping criteria.
bool isType(const std::string &name) const
A class for extending the status testing capabilities of Belos via logical combinations.
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
Definition: BelosTypes.hpp:60
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Default parameters common to most Belos solvers.
Definition: BelosTypes.hpp:283
bool isLOADetected() const override
Return whether a loss of accuracy was detected by this solver during the most current solve...
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
Belos header file which uses auto-configuration information to include necessary C++ headers...
Teuchos::RCP< SolverManager< ScalarType, MV, OP > > clone() const override
clone for Inverted Injection (DII)
Belos concrete class for performing the pseudo-block BiCGStab iteration.
bool is_null() const

Generated on Wed Apr 24 2024 09:25:57 for Belos by doxygen 1.8.5