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

Generated on Fri Mar 10 2023 07:13:22 for Belos by doxygen 1.9.4