Belos Version of the Day
Loading...
Searching...
No Matches
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
54
55#include "BelosTFQMRIter.hpp"
61#ifdef BELOS_TEUCHOS_TIME_MONITOR
62#include "Teuchos_TimeMonitor.hpp"
63#endif
64
78namespace Belos {
79
81
82
90 TFQMRSolMgrLinearProblemFailure(const std::string& what_arg) : BelosError(what_arg)
91 {}};
92
93 template<class ScalarType, class MV, class OP>
94 class TFQMRSolMgr : public SolverManager<ScalarType,MV,OP> {
95
96 private:
99 typedef Teuchos::ScalarTraits<ScalarType> SCT;
100 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
101 typedef Teuchos::ScalarTraits<MagnitudeType> MT;
102
103 public:
104
106
107
113 TFQMRSolMgr();
114
131 TFQMRSolMgr( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
132 const Teuchos::RCP<Teuchos::ParameterList> &pl );
133
135 virtual ~TFQMRSolMgr() {};
136
138 Teuchos::RCP<SolverManager<ScalarType, MV, OP> > clone () const override {
139 return Teuchos::rcp(new TFQMRSolMgr<ScalarType,MV,OP>);
140 }
142
144
145
147 return *problem_;
148 }
149
152 Teuchos::RCP<const Teuchos::ParameterList> getValidParameters() const override;
153
156 Teuchos::RCP<const Teuchos::ParameterList> getCurrentParameters() const override { return params_; }
157
163 Teuchos::Array<Teuchos::RCP<Teuchos::Time> > getTimers() const {
164 return Teuchos::tuple(timerSolve_);
165 }
166
172 MagnitudeType achievedTol() const override {
173 return achievedTol_;
174 }
175
177 int getNumIters() const override {
178 return numIters_;
179 }
180
188 bool isLOADetected() const override { return false; }
190
192
193
195 void setProblem( const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem ) override { problem_ = problem; }
196
198 void setParameters( const Teuchos::RCP<Teuchos::ParameterList> &params ) override;
199
201
203
208 void reset( const ResetType type ) override { if ((type & Belos::Problem) && !Teuchos::is_null(problem_)) problem_->setProblem(); }
210
212
213
231 ReturnType solve() override;
232
234
236
238 std::string description() const override;
240
241 private:
242
243 // Method for checking current status test against defined linear problem.
244 bool checkStatusTest();
245
246 // Linear problem.
247 Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > problem_;
248
249 // Output manager.
250 Teuchos::RCP<OutputManager<ScalarType> > printer_;
251 Teuchos::RCP<std::ostream> outputStream_;
252
253 // Status test.
254 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > sTest_;
255 Teuchos::RCP<StatusTestMaxIters<ScalarType,MV,OP> > maxIterTest_;
256 Teuchos::RCP<StatusTest<ScalarType,MV,OP> > convTest_;
257 Teuchos::RCP<StatusTestGenResNorm<ScalarType,MV,OP> > expConvTest_, impConvTest_;
258 Teuchos::RCP<StatusTestOutput<ScalarType,MV,OP> > outputTest_;
259
260 // Current parameter list.
261 Teuchos::RCP<Teuchos::ParameterList> params_;
262
263 // Default solver values.
264 static constexpr int maxIters_default_ = 1000;
265 static constexpr bool expResTest_default_ = false;
266 static constexpr int verbosity_default_ = Belos::Errors;
267 static constexpr int outputStyle_default_ = Belos::General;
268 static constexpr int outputFreq_default_ = -1;
269 static constexpr const char * impResScale_default_ = "Norm of Preconditioned Initial Residual";
270 static constexpr const char * expResScale_default_ = "Norm of Initial Residual";
271 static constexpr const char * label_default_ = "Belos";
272
273 // Current solver values.
274 MagnitudeType convtol_, impTolScale_, achievedTol_;
275 int maxIters_, numIters_;
276 int verbosity_, outputStyle_, outputFreq_;
277 int blockSize_;
278 bool expResTest_;
279 std::string impResScale_, expResScale_;
280
281 // Timers.
282 std::string label_;
283 Teuchos::RCP<Teuchos::Time> timerSolve_;
284
285 // Internal state variables.
286 bool isSet_, isSTSet_;
287 };
288
289
290// Empty Constructor
291template<class ScalarType, class MV, class OP>
293 outputStream_(Teuchos::rcpFromRef(std::cout)),
294 convtol_(DefaultSolverParameters::convTol),
295 impTolScale_(DefaultSolverParameters::impTolScale),
296 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
297 maxIters_(maxIters_default_),
298 numIters_(0),
299 verbosity_(verbosity_default_),
300 outputStyle_(outputStyle_default_),
301 outputFreq_(outputFreq_default_),
302 blockSize_(1),
303 expResTest_(expResTest_default_),
304 impResScale_(impResScale_default_),
305 expResScale_(expResScale_default_),
306 label_(label_default_),
307 isSet_(false),
308 isSTSet_(false)
309{}
310
311
312// Basic Constructor
313template<class ScalarType, class MV, class OP>
315 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > &problem,
316 const Teuchos::RCP<Teuchos::ParameterList> &pl ) :
317 problem_(problem),
318 outputStream_(Teuchos::rcpFromRef(std::cout)),
319 convtol_(DefaultSolverParameters::convTol),
320 impTolScale_(DefaultSolverParameters::impTolScale),
321 achievedTol_(Teuchos::ScalarTraits<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>::zero()),
322 maxIters_(maxIters_default_),
323 numIters_(0),
324 verbosity_(verbosity_default_),
325 outputStyle_(outputStyle_default_),
326 outputFreq_(outputFreq_default_),
327 blockSize_(1),
328 expResTest_(expResTest_default_),
329 impResScale_(impResScale_default_),
330 expResScale_(expResScale_default_),
331 label_(label_default_),
332 isSet_(false),
333 isSTSet_(false)
334{
335 TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null, std::invalid_argument, "Problem not given to solver manager.");
336
337 // If the parameter list pointer is null, then set the current parameters to the default parameter list.
338 if ( !is_null(pl) ) {
339 setParameters( pl );
340 }
341}
342
343template<class ScalarType, class MV, class OP>
344void TFQMRSolMgr<ScalarType,MV,OP>::setParameters( const Teuchos::RCP<Teuchos::ParameterList> &params )
345{
346 // Create the internal parameter list if ones doesn't already exist.
347 if (params_ == Teuchos::null) {
348 params_ = Teuchos::rcp( new Teuchos::ParameterList(*getValidParameters()) );
349 }
350 else {
351 params->validateParameters(*getValidParameters());
352 }
353
354 // Check for maximum number of iterations
355 if (params->isParameter("Maximum Iterations")) {
356 maxIters_ = params->get("Maximum Iterations",maxIters_default_);
357
358 // Update parameter in our list and in status test.
359 params_->set("Maximum Iterations", maxIters_);
360 if (maxIterTest_!=Teuchos::null)
361 maxIterTest_->setMaxIters( maxIters_ );
362 }
363
364 // Check for blocksize
365 if (params->isParameter("Block Size")) {
366 blockSize_ = params->get("Block Size",1);
367 TEUCHOS_TEST_FOR_EXCEPTION(blockSize_ != 1, std::invalid_argument,
368 "Belos::TFQMRSolMgr: \"Block Size\" must be 1.");
369
370 // Update parameter in our list.
371 params_->set("Block Size", blockSize_);
372 }
373
374 // Check to see if the timer label changed.
375 if (params->isParameter("Timer Label")) {
376 std::string tempLabel = params->get("Timer Label", label_default_);
377
378 // Update parameter in our list and solver timer
379 if (tempLabel != label_) {
380 label_ = tempLabel;
381 params_->set("Timer Label", label_);
382 std::string solveLabel = label_ + ": TFQMRSolMgr total solve time";
383#ifdef BELOS_TEUCHOS_TIME_MONITOR
384 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
385#endif
386 }
387 }
388
389 // Check for a change in verbosity level
390 if (params->isParameter("Verbosity")) {
391 if (Teuchos::isParameterType<int>(*params,"Verbosity")) {
392 verbosity_ = params->get("Verbosity", verbosity_default_);
393 } else {
394 verbosity_ = (int)Teuchos::getParameter<Belos::MsgType>(*params,"Verbosity");
395 }
396
397 // Update parameter in our list.
398 params_->set("Verbosity", verbosity_);
399 if (printer_ != Teuchos::null)
400 printer_->setVerbosity(verbosity_);
401 }
402
403 // Check for a change in output style
404 if (params->isParameter("Output Style")) {
405 if (Teuchos::isParameterType<int>(*params,"Output Style")) {
406 outputStyle_ = params->get("Output Style", outputStyle_default_);
407 } else {
408 outputStyle_ = (int)Teuchos::getParameter<Belos::OutputType>(*params,"Output Style");
409 }
410
411 // Reconstruct the convergence test if the explicit residual test is not being used.
412 params_->set("Output Style", outputStyle_);
413 isSTSet_ = false;
414 }
415
416 // output stream
417 if (params->isParameter("Output Stream")) {
418 outputStream_ = Teuchos::getParameter<Teuchos::RCP<std::ostream> >(*params,"Output Stream");
419
420 // Update parameter in our list.
421 params_->set("Output Stream", outputStream_);
422 if (printer_ != Teuchos::null)
423 printer_->setOStream( outputStream_ );
424 }
425
426 // frequency level
427 if (verbosity_ & Belos::StatusTestDetails) {
428 if (params->isParameter("Output Frequency")) {
429 outputFreq_ = params->get("Output Frequency", outputFreq_default_);
430 }
431
432 // Update parameter in out list and output status test.
433 params_->set("Output Frequency", outputFreq_);
434 if (outputTest_ != Teuchos::null)
435 outputTest_->setOutputFrequency( outputFreq_ );
436 }
437
438 // Create output manager if we need to.
439 if (printer_ == Teuchos::null) {
440 printer_ = Teuchos::rcp( new OutputManager<ScalarType>(verbosity_, outputStream_) );
441 }
442
443 // Check for convergence tolerance
444 if (params->isParameter("Convergence Tolerance")) {
445 if (params->isType<MagnitudeType> ("Convergence Tolerance")) {
446 convtol_ = params->get ("Convergence Tolerance",
447 static_cast<MagnitudeType> (DefaultSolverParameters::convTol));
448 }
449 else {
450 convtol_ = params->get ("Convergence Tolerance", DefaultSolverParameters::convTol);
451 }
452
453 // Update parameter in our list.
454 params_->set("Convergence Tolerance", convtol_);
455 isSTSet_ = false;
456 }
457
458 // Check for implicit residual scaling
459 if (params->isParameter("Implicit Tolerance Scale Factor")) {
460 if (params->isType<MagnitudeType> ("Implicit Tolerance Scale Factor")) {
461 impTolScale_ = params->get ("Implicit Tolerance Scale Factor",
462 static_cast<MagnitudeType> (DefaultSolverParameters::impTolScale));
463
464 }
465 else {
466 impTolScale_ = params->get ("Implicit Tolerance Scale Factor",
468 }
469
470 // Update parameter in our list.
471 params_->set("Implicit Tolerance Scale Factor", impTolScale_);
472 isSTSet_ = false;
473 }
474
475 // Check for a change in scaling, if so we need to build new residual tests.
476 if (params->isParameter("Implicit Residual Scaling")) {
477 std::string tempImpResScale = Teuchos::getParameter<std::string>( *params, "Implicit Residual Scaling" );
478
479 // Only update the scaling if it's different.
480 if (impResScale_ != tempImpResScale) {
481 impResScale_ = tempImpResScale;
482
483 // Update parameter in our list and residual tests
484 params_->set("Implicit Residual Scaling", impResScale_);
485
486 // Make sure the convergence test gets constructed again.
487 isSTSet_ = false;
488 }
489 }
490
491 if (params->isParameter("Explicit Residual Scaling")) {
492 std::string tempExpResScale = Teuchos::getParameter<std::string>( *params, "Explicit Residual Scaling" );
493
494 // Only update the scaling if it's different.
495 if (expResScale_ != tempExpResScale) {
496 expResScale_ = tempExpResScale;
497
498 // Update parameter in our list and residual tests
499 params_->set("Explicit Residual Scaling", expResScale_);
500
501 // Make sure the convergence test gets constructed again.
502 isSTSet_ = false;
503 }
504 }
505
506 if (params->isParameter("Explicit Residual Test")) {
507 expResTest_ = Teuchos::getParameter<bool>( *params,"Explicit Residual Test" );
508
509 // Reconstruct the convergence test if the explicit residual test is not being used.
510 params_->set("Explicit Residual Test", expResTest_);
511 if (expConvTest_ == Teuchos::null) {
512 isSTSet_ = false;
513 }
514 }
515
516 // Create the timer if we need to.
517 if (timerSolve_ == Teuchos::null) {
518 std::string solveLabel = label_ + ": TFQMRSolMgr total solve time";
519#ifdef BELOS_TEUCHOS_TIME_MONITOR
520 timerSolve_ = Teuchos::TimeMonitor::getNewCounter(solveLabel);
521#endif
522 }
523
524 // Inform the solver manager that the current parameters were set.
525 isSet_ = true;
526}
527
528
529// Check the status test versus the defined linear problem
530template<class ScalarType, class MV, class OP>
532
533 typedef Belos::StatusTestCombo<ScalarType,MV,OP> StatusTestCombo_t;
534 typedef Belos::StatusTestGenResNorm<ScalarType,MV,OP> StatusTestGenResNorm_t;
535
536 // Basic test checks maximum iterations and native residual.
537 maxIterTest_ = Teuchos::rcp( new StatusTestMaxIters<ScalarType,MV,OP>( maxIters_ ) );
538
539 if (expResTest_) {
540
541 // Implicit residual test, using the native residual to determine if convergence was achieved.
542 Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
543 Teuchos::rcp( new StatusTestGenResNorm_t( impTolScale_*convtol_ ) );
544 tmpImpConvTest->defineScaleForm( convertStringToScaleType(impResScale_), Belos::TwoNorm );
545 impConvTest_ = tmpImpConvTest;
546
547 // Explicit residual test once the native residual is below the tolerance
548 Teuchos::RCP<StatusTestGenResNorm_t> tmpExpConvTest =
549 Teuchos::rcp( new StatusTestGenResNorm_t( convtol_ ) );
550 tmpExpConvTest->defineResForm( StatusTestGenResNorm_t::Explicit, Belos::TwoNorm );
551 tmpExpConvTest->defineScaleForm( convertStringToScaleType(expResScale_), Belos::TwoNorm );
552 expConvTest_ = tmpExpConvTest;
553
554 // The convergence test is a combination of the "cheap" implicit test and explicit test.
555 convTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::SEQ, impConvTest_, expConvTest_ ) );
556 }
557 else {
558
559 // Implicit residual test, using the native residual to determine if convergence was achieved.
560 Teuchos::RCP<StatusTestGenResNorm_t> tmpImpConvTest =
561 Teuchos::rcp( new StatusTestGenResNorm_t( convtol_ ) );
562 tmpImpConvTest->defineScaleForm( convertStringToScaleType(impResScale_), Belos::TwoNorm );
563 impConvTest_ = tmpImpConvTest;
564
565 // Set the explicit and total convergence test to this implicit test that checks for accuracy loss.
566 expConvTest_ = impConvTest_;
567 convTest_ = impConvTest_;
568 }
569 sTest_ = Teuchos::rcp( new StatusTestCombo_t( StatusTestCombo_t::OR, maxIterTest_, convTest_ ) );
570
571 // Create the status test output class.
572 // This class manages and formats the output from the status test.
573 StatusTestOutputFactory<ScalarType,MV,OP> stoFactory( outputStyle_ );
574 outputTest_ = stoFactory.create( printer_, sTest_, outputFreq_, Passed+Failed+Undefined );
575
576 // Set the solver string for the output test
577 std::string solverDesc = " TFQMR ";
578 outputTest_->setSolverDesc( solverDesc );
579
580
581 // The status test is now set.
582 isSTSet_ = true;
583
584 return false;
585}
586
587
588template<class ScalarType, class MV, class OP>
589Teuchos::RCP<const Teuchos::ParameterList>
591{
592 static Teuchos::RCP<const Teuchos::ParameterList> validPL;
593
594 // Set all the valid parameters and their default values.
595 if(is_null(validPL)) {
596 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
597
598 // The static_cast is to resolve an issue with older clang versions which
599 // would cause the constexpr to link fail. With c++17 the problem is resolved.
600 pl->set("Convergence Tolerance", static_cast<MagnitudeType>(DefaultSolverParameters::convTol),
601 "The relative residual tolerance that needs to be achieved by the\n"
602 "iterative solver in order for the linear system to be declared converged.");
603 pl->set("Implicit Tolerance Scale Factor", static_cast<MagnitudeType>(DefaultSolverParameters::impTolScale),
604 "The scale factor used by the implicit residual test when explicit residual\n"
605 "testing is used. May enable faster convergence when TFQMR bound is too loose.");
606 pl->set("Maximum Iterations", static_cast<int>(maxIters_default_),
607 "The maximum number of block iterations allowed for each\n"
608 "set of RHS solved.");
609 pl->set("Verbosity", static_cast<int>(verbosity_default_),
610 "What type(s) of solver information should be outputted\n"
611 "to the output stream.");
612 pl->set("Output Style", static_cast<int>(outputStyle_default_),
613 "What style is used for the solver information outputted\n"
614 "to the output stream.");
615 pl->set("Output Frequency", static_cast<int>(outputFreq_default_),
616 "How often convergence information should be outputted\n"
617 "to the output stream.");
618 pl->set("Output Stream", Teuchos::rcpFromRef(std::cout),
619 "A reference-counted pointer to the output stream where all\n"
620 "solver output is sent.");
621 pl->set("Explicit Residual Test", static_cast<bool>(expResTest_default_),
622 "Whether the explicitly computed residual should be used in the convergence test.");
623 pl->set("Implicit Residual Scaling", static_cast<const char *>(impResScale_default_),
624 "The type of scaling used in the implicit residual convergence test.");
625 pl->set("Explicit Residual Scaling", static_cast<const char *>(expResScale_default_),
626 "The type of scaling used in the explicit residual convergence test.");
627 pl->set("Timer Label", static_cast<const char *>(label_default_),
628 "The string to use as a prefix for the timer labels.");
629 validPL = pl;
630 }
631 return validPL;
632}
633
634
635// solve()
636template<class ScalarType, class MV, class OP>
638
639 // Set the current parameters if they were not set before.
640 // NOTE: This may occur if the user generated the solver manager with the default constructor and
641 // then didn't set any parameters using setParameters().
642 if (!isSet_) {
643 setParameters(Teuchos::parameterList(*getValidParameters()));
644 }
645
646 TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,TFQMRSolMgrLinearProblemFailure,
647 "Belos::TFQMRSolMgr::solve(): Linear problem is not a valid object.");
648
649 TEUCHOS_TEST_FOR_EXCEPTION(!problem_->isProblemSet(),TFQMRSolMgrLinearProblemFailure,
650 "Belos::TFQMRSolMgr::solve(): Linear problem is not ready, setProblem() has not been called.");
651
652 if (!isSTSet_) {
653 TEUCHOS_TEST_FOR_EXCEPTION( checkStatusTest(),TFQMRSolMgrLinearProblemFailure,
654 "Belos::TFQMRSolMgr::solve(): Linear problem and requested status tests are incompatible.");
655 }
656
657 // Create indices for the linear systems to be solved.
658 int startPtr = 0;
659 int numRHS2Solve = MVT::GetNumberVecs( *(problem_->getRHS()) );
660 int numCurrRHS = blockSize_;
661
662 std::vector<int> currIdx, currIdx2;
663
664 // The index set is generated that informs the linear problem that some linear systems are augmented.
665 currIdx.resize( blockSize_ );
666 currIdx2.resize( blockSize_ );
667 for (int i=0; i<numCurrRHS; ++i)
668 { currIdx[i] = startPtr+i; currIdx2[i]=i; }
669
670 // Inform the linear problem of the current linear system to solve.
671 problem_->setLSIndex( currIdx );
672
674 // Parameter list
675 Teuchos::ParameterList plist;
676 plist.set("Block Size",blockSize_);
677
678 // Reset the status test.
679 outputTest_->reset();
680
681 // Assume convergence is achieved, then let any failed convergence set this to false.
682 bool isConverged = true;
683
685 // TFQMR solver
686
687 Teuchos::RCP<TFQMRIter<ScalarType,MV,OP> > tfqmr_iter =
688 Teuchos::rcp( new TFQMRIter<ScalarType,MV,OP>(problem_,printer_,outputTest_,plist) );
689
690 // Enter solve() iterations
691 {
692#ifdef BELOS_TEUCHOS_TIME_MONITOR
693 Teuchos::TimeMonitor slvtimer(*timerSolve_);
694#endif
695
696 while ( numRHS2Solve > 0 ) {
697 //
698 // Reset the active / converged vectors from this block
699 std::vector<int> convRHSIdx;
700 std::vector<int> currRHSIdx( currIdx );
701 currRHSIdx.resize(numCurrRHS);
702
703 // Reset the number of iterations.
704 tfqmr_iter->resetNumIters();
705
706 // Reset the number of calls that the status test output knows about.
707 outputTest_->resetNumCalls();
708
709 // Get the current residual for this block of linear systems.
710 Teuchos::RCP<MV> R_0 = MVT::CloneViewNonConst( *(Teuchos::rcp_const_cast<MV>(problem_->getInitPrecResVec())), currIdx );
711
712 // Set the new state and initialize the solver.
714 newstate.R = R_0;
715 tfqmr_iter->initializeTFQMR(newstate);
716
717 while(1) {
718
719 // tell tfqmr_iter to iterate
720 try {
721 tfqmr_iter->iterate();
722
724 //
725 // check convergence first
726 //
728 if ( convTest_->getStatus() == Passed ) {
729 // We have convergence of the linear system.
730 break; // break from while(1){tfqmr_iter->iterate()}
731 }
733 //
734 // check for maximum iterations
735 //
737 else if ( maxIterTest_->getStatus() == Passed ) {
738 // we don't have convergence
739 isConverged = false;
740 break; // break from while(1){tfqmr_iter->iterate()}
741 }
742
744 //
745 // we returned from iterate(), but none of our status tests Passed.
746 // something is wrong, and it is probably our fault.
747 //
749
750 else {
751 TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
752 "Belos::TFQMRSolMgr::solve(): Invalid return from TFQMRIter::iterate().");
753 }
754 }
755 catch (const std::exception &e) {
756 printer_->stream(Errors) << "Error! Caught std::exception in TFQMRIter::iterate() at iteration "
757 << tfqmr_iter->getNumIters() << std::endl
758 << e.what() << std::endl;
759 throw;
760 }
761 }
762
763 // Update the current solution with the update computed by the iteration object.
764 problem_->updateSolution( tfqmr_iter->getCurrentUpdate(), true );
765
766 // Inform the linear problem that we are finished with this block linear system.
767 problem_->setCurrLS();
768
769 // Update indices for the linear systems to be solved.
770 startPtr += numCurrRHS;
771 numRHS2Solve -= numCurrRHS;
772 if ( numRHS2Solve > 0 ) {
773 numCurrRHS = blockSize_;
774
775 currIdx.resize( blockSize_ );
776 currIdx2.resize( blockSize_ );
777 for (int i=0; i<numCurrRHS; ++i)
778 { currIdx[i] = startPtr+i; currIdx2[i] = i; }
779 // Set the next indices.
780 problem_->setLSIndex( currIdx );
781
782 // Set the new blocksize for the solver.
783 tfqmr_iter->setBlockSize( blockSize_ );
784 }
785 else {
786 currIdx.resize( numRHS2Solve );
787 }
788
789 }// while ( numRHS2Solve > 0 )
790
791 }
792
793 // print final summary
794 sTest_->print( printer_->stream(FinalSummary) );
795
796 // print timing information
797#ifdef BELOS_TEUCHOS_TIME_MONITOR
798 // Calling summarize() can be expensive, so don't call unless the
799 // user wants to print out timing details. summarize() will do all
800 // the work even if it's passed a "black hole" output stream.
801 if (verbosity_ & TimingDetails)
802 Teuchos::TimeMonitor::summarize( printer_->stream(TimingDetails) );
803#endif
804
805 // get iteration information for this solve
806 numIters_ = maxIterTest_->getNumIters();
807
808 // Save the convergence test value ("achieved tolerance") for this
809 // solve. For this solver, convTest_ may either be a single
810 // (implicit) residual norm test, or a combination of two residual
811 // norm tests. In the latter case, the master convergence test
812 // convTest_ is a SEQ combo of the implicit resp. explicit tests.
813 // If the implicit test never passes, then the explicit test won't
814 // ever be executed. This manifests as
815 // expConvTest_->getTestValue()->size() < 1. We deal with this case
816 // by using the values returned by impConvTest_->getTestValue().
817 {
818 // We'll fetch the vector of residual norms one way or the other.
819 const std::vector<MagnitudeType>* pTestValues = NULL;
820 if (expResTest_) {
821 pTestValues = expConvTest_->getTestValue();
822 if (pTestValues == NULL || pTestValues->size() < 1) {
823 pTestValues = impConvTest_->getTestValue();
824 }
825 }
826 else {
827 // Only the implicit residual norm test is being used.
828 pTestValues = impConvTest_->getTestValue();
829 }
830 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues == NULL, std::logic_error,
831 "Belos::TFQMRSolMgr::solve(): The implicit convergence test's "
832 "getTestValue() method returned NULL. Please report this bug to the "
833 "Belos developers.");
834 TEUCHOS_TEST_FOR_EXCEPTION(pTestValues->size() < 1, std::logic_error,
835 "Belos::TMQMRSolMgr::solve(): The implicit convergence test's "
836 "getTestValue() method returned a vector of length zero. Please report "
837 "this bug to the Belos developers.");
838
839 // FIXME (mfh 12 Dec 2011) Does pTestValues really contain the
840 // achieved tolerances for all vectors in the current solve(), or
841 // just for the vectors from the last deflation?
842 achievedTol_ = *std::max_element (pTestValues->begin(), pTestValues->end());
843 }
844
845 if (!isConverged) {
846 return Unconverged; // return from TFQMRSolMgr::solve()
847 }
848 return Converged; // return from TFQMRSolMgr::solve()
849}
850
851// This method requires the solver manager to return a std::string that describes itself.
852template<class ScalarType, class MV, class OP>
854{
855 std::ostringstream oss;
856 oss << "Belos::TFQMRSolMgr<...,"<<Teuchos::ScalarTraits<ScalarType>::name()<<">";
857 oss << "{}";
858 return oss.str();
859}
860
861} // end Belos namespace
862
863#endif /* BELOS_TFQMR_SOLMGR_HPP */
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.
Belos concrete class for generating iterations with the preconditioned tranpose-free QMR (TFQMR) meth...
Collection of types and exceptions used within the Belos solvers.
Parent class to all Belos exceptions.
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.
A Belos::StatusTest class for specifying a maximum number of iterations.
This class implements the preconditioned transpose-free QMR algorithm for solving non-Hermitian linea...
TFQMRSolMgrLinearProblemFailure is thrown when the linear problem is not setup (i....
TFQMRSolMgrLinearProblemFailure(const std::string &what_arg)
The Belos::TFQMRSolMgr provides a powerful and fully-featured solver manager over the TFQMR linear so...
void reset(const ResetType type) override
Performs a reset of the solver manager specified by the ResetType. This informs the solver manager th...
virtual ~TFQMRSolMgr()
Destructor.
Teuchos::RCP< const Teuchos::ParameterList > getCurrentParameters() const override
Get a parameter list containing the current parameters for this object.
void setProblem(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem) override
Set the linear problem that needs to be solved.
int getNumIters() const override
Get the iteration count for the most recent call to solve().
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
Return the timers for this object.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Get a parameter list containing the valid parameters for this object.
MagnitudeType achievedTol() const override
Tolerance achieved by the last solve() invocation.
std::string description() const override
Method to return description of the TFQMR solver manager.
const LinearProblem< ScalarType, MV, OP > & getProblem() const override
Return a reference to the linear problem being solved by this solver manager.
ReturnType solve() override
This method performs possibly repeated calls to the underlying linear solver's iterate() routine unti...
TFQMRSolMgr()
Empty constructor for TFQMRSolMgr. This constructor takes no arguments and sets the default values fo...
bool isLOADetected() const override
Whether loss of accuracy was detected during the last solve() invocation.
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.
ScaleType convertStringToScaleType(const std::string &scaleType)
Convert the given string to its ScaleType enum value.
@ StatusTestDetails
@ FinalSummary
@ TimingDetails
ReturnType
Whether the Belos solve converged for all linear systems.
@ Unconverged
ResetType
How to reset the solver.
Default parameters common to most Belos solvers.
static const double impTolScale
"Implicit Tolerance Scale Factor"
static const double convTol
Default convergence tolerance.
Structure to contain pointers to TFQMRIter state variables.
Teuchos::RCP< const MV > R
The current residual basis.

Generated for Belos by doxygen 1.10.0